b851d37d3f35b054dd64dcf0a9ee66515ab0dbfa
[unres4.git] / source / unres / control.F90
1       module control
2 !-----------------------------------------------------------------------------
3       use io_units
4       use names
5       use MPI_data
6       use geometry_data
7       use energy_data
8       use control_data
9       use minim_data
10       use geometry, only:int_bounds
11 #ifndef CLUSTER
12       use csa_data
13 #ifdef WHAM_RUN
14       use wham_data
15 #endif
16 #endif
17       implicit none
18 !-----------------------------------------------------------------------------
19 ! commom.control
20 !      common /cntrl/
21 !      integer :: modecalc,iscode,indpdb,indback,indphi,iranconf,&
22 !       icheckgrad,iprint,i2ndstr,mucadyn,constr_dist,symetr
23 !      logical :: minim,refstr,pdbref,outpdb,outmol2,overlapsc,&
24 !       energy_dec,sideadd,lsecondary,read_cart,unres_pdb,&
25 !       vdisulf,searchsc,lmuca,dccart,extconf,out1file,&
26 !       gnorm_check,gradout,split_ene
27 !... minim = .true. means DO minimization.
28 !... energy_dec = .true. means print energy decomposition matrix
29 !-----------------------------------------------------------------------------
30 ! common.time1
31 !     FOUND_NAN - set by calcf to stop sumsl via stopx
32 !      COMMON/TIME1/
33       real(kind=8) :: STIME,BATIME,PREVTIM,RSTIME
34 !el      real(kind=8) :: TIMLIM,SAFETY
35 !el      real(kind=8) :: WALLTIME
36 !      COMMON/STOPTIM/
37       integer :: ISTOP
38 !      common /sumsl_flag/
39       logical :: FOUND_NAN
40 !      common /timing/
41       real(kind=8) :: t_init
42 !       time_bcast,time_reduce,time_gather,&
43 !       time_sendrecv,time_barrier_e,time_barrier_g,time_scatter,&
44        !t_eelecij,
45 !       time_allreduce,&
46 !       time_lagrangian,time_cartgrad,&
47 !       time_sumgradient,time_intcartderiv,time_inttocart,time_intfcart,&
48 !       time_mat,time_fricmatmult,&
49 !       time_scatter_fmat,time_scatter_ginv,&
50 !       time_scatter_fmatmult,time_scatter_ginvmult,&
51 !       t_eshort,t_elong,t_etotal
52 !-----------------------------------------------------------------------------
53 ! initialize_p.F
54 !-----------------------------------------------------------------------------
55 !      block data
56 !      integer,parameter :: MaxMoveType = 4
57 !      character(len=14),dimension(-1:MaxMoveType+1) :: MovTypID=(/'pool','chain regrow',&
58 !      character :: MovTypID(-1:MaxMoveType+1)=(/'pool','chain regrow',&
59 !       'multi-bond','phi','theta','side chain','total'/)
60 ! Conversion from poises to molecular unit and the gas constant
61 !el      real(kind=8) :: cPoise=2.9361d0, Rb=0.001986d0
62 !-----------------------------------------------------------------------------
63 !      common /przechowalnia/ subroutines: init_int_table,add_int,add_int_from
64       integer,dimension(:),allocatable :: iturn3_start_all,&
65         iturn3_end_all,iturn4_start_all,iturn4_end_all,iatel_s_all,&
66         iatel_e_all !(0:max_fg_procs)
67       integer,dimension(:,:),allocatable :: ielstart_all,&
68         ielend_all !(maxres,0:max_fg_procs-1)
69
70 !      common /przechowalnia/ subroutine: init_int_table
71       integer,dimension(:),allocatable :: ntask_cont_from_all,&
72         ntask_cont_to_all !(0:max_fg_procs-1)
73       integer,dimension(:,:),allocatable :: itask_cont_from_all,&
74         itask_cont_to_all !(0:max_fg_procs-1,0:max_fg_procs-1)
75 !-----------------------------------------------------------------------------
76 !
77 !
78 !-----------------------------------------------------------------------------
79       contains
80 !-----------------------------------------------------------------------------
81 ! initialize_p.F
82 !-----------------------------------------------------------------------------
83       subroutine initialize
84 !
85 ! Define constants and zero out tables.
86 !
87       use comm_iofile
88       use comm_machsw
89       use MCM_data, only: MovTypID
90 !      implicit real*8 (a-h,o-z)
91 !      include 'DIMENSIONS'
92 #ifdef MPI
93       include 'mpif.h'
94 #endif
95 #ifndef ISNAN
96       external proc_proc
97 #ifdef WINPGI
98 !MS$ATTRIBUTES C ::  proc_proc
99 #endif
100 #endif
101 !      include 'COMMON.IOUNITS'
102 !      include 'COMMON.CHAIN'
103 !      include 'COMMON.INTERACT'
104 !      include 'COMMON.GEO'
105 !      include 'COMMON.LOCAL'
106 !      include 'COMMON.TORSION'
107 !      include 'COMMON.FFIELD'
108 !      include 'COMMON.SBRIDGE'
109 !      include 'COMMON.MCM'
110 !      include 'COMMON.MINIM' 
111 !      include 'COMMON.DERIV'
112 !      include 'COMMON.SPLITELE'
113 !      implicit none
114 ! Common blocks from the diagonalization routines
115 !el      integer :: IR,IW,IP,IJK,IPK,IDAF,NAV,IODA(400)
116 !el      integer :: KDIAG,ICORFL,IXDR
117 !el      COMMON /IOFILE/ IR,IW,IP,IJK,IPK,IDAF,NAV,IODA
118 !el      COMMON /MACHSW/ KDIAG,ICORFL,IXDR
119       logical :: mask_r
120 !      real*8 text1 /'initial_i'/
121       real(kind=4) :: rr
122
123 !local variables el
124       integer :: i,j,k,l,ichir1,ichir2,iblock,m,maxit
125
126 #if .not. defined(WHAM_RUN) && .not. defined(CLUSTER)
127       mask_r=.false.
128 #ifndef ISNAN
129 ! NaNQ initialization
130       i=-1
131       rr=dacos(100.0d0)
132 #ifdef WINPGI
133       idumm=proc_proc(rr,i)
134 #elif defined(WHAM_RUN)
135       call proc_proc(rr,i)
136 #endif
137 #endif
138
139       kdiag=0
140       icorfl=0
141       iw=2
142       
143       allocate(MovTypID(-1:MaxMoveType+1))
144       MovTypID=(/'pool          ','chain regrow  ',&
145        'multi-bond    ','phi           ','theta         ',&
146        'side chain    ','total         '/)
147 #endif
148 !
149 ! The following is just to define auxiliary variables used in angle conversion
150 !
151       pi=4.0D0*datan(1.0D0)
152       dwapi=2.0D0*pi
153       dwapi3=dwapi/3.0D0
154       pipol=0.5D0*pi
155       deg2rad=pi/180.0D0
156       rad2deg=1.0D0/deg2rad
157       angmin=10.0D0*deg2rad
158 !el#ifdef CLUSTER
159 !el      Rgas = 1.987D-3
160 !el#endif
161 !
162 ! Define I/O units.
163 !
164       inp=    1
165       iout=   2
166       ipdbin= 3
167       ipdb=   7
168 #ifdef CLUSTER
169       imol2= 18
170       jplot= 19
171 !el      jstatin=10
172       imol2=  4
173       jrms=30
174 #else
175       icart = 30
176       imol2=  4
177       ithep_pdb=51
178       irotam_pdb=52
179       irest1=55
180       irest2=56
181       iifrag=57
182       ientin=18
183       ientout=19
184 !rc for write_rmsbank1  
185       izs1=21
186 !dr  include secondary structure prediction bias
187       isecpred=27
188 #endif
189       igeom=  8
190       intin=  9
191       ithep= 11
192       irotam=12
193       itorp= 13
194       itordp= 23
195       ielep= 14
196       isidep=15
197 #if defined(WHAM_RUN) || defined(CLUSTER)
198       isidep1=22 !wham
199 #else
200 !
201 ! CSA I/O units (separated from others especially for Jooyoung)
202 !
203       icsa_rbank=30
204       icsa_seed=31
205       icsa_history=32
206       icsa_bank=33
207       icsa_bank1=34
208       icsa_alpha=35
209       icsa_alpha1=36
210       icsa_bankt=37
211       icsa_int=39
212       icsa_bank_reminimized=38
213       icsa_native_int=41
214       icsa_in=40
215 !rc for ifc error 118
216       icsa_pdb=42
217 #endif
218       iscpp=25
219       icbase=16
220       ifourier=20
221       istat= 17
222       ibond = 28
223       isccor = 29
224 #ifdef WHAM_RUN
225 !
226 ! WHAM files
227 !
228       ihist=30
229       iweight=31
230       izsc=32
231 #endif
232       ibond_nucl=126
233       ithep_nucl=127
234       irotam_nucl=128
235       itorp_nucl= 129
236       itordp_nucl= 130
237 !      ielep_nucl= 131
238       isidep_nucl=132
239       iscpp_nucl=133
240
241
242
243       iliptranpar=60
244       itube=61
245 #if defined(WHAM_RUN) || defined(CLUSTER)
246 !
247 ! setting the mpi variables for WHAM
248 !
249       fgprocs=1
250       nfgtasks=1
251       nfgtasks1=1
252 #endif
253 !
254 ! Set default weights of the energy terms.
255 !
256       wsc=1.0D0 ! in wham:  wlong=1.0D0
257       welec=1.0D0
258       wtor =1.0D0
259       wang =1.0D0
260       wscloc=1.0D0
261       wstrain=1.0D0
262 !
263 ! Zero out tables.
264 !
265 !      print '(a,$)','Inside initialize'
266 !      call memmon_print_usage()
267       
268 !      do i=1,maxres2
269 !       do j=1,3
270 !         c(j,i)=0.0D0
271 !         dc(j,i)=0.0D0
272 !       enddo
273 !      enddo
274 !      do i=1,maxres
275 !       do j=1,3
276 !         xloc(j,i)=0.0D0
277 !        enddo
278 !      enddo
279 !      do i=1,ntyp
280 !       do j=1,ntyp
281 !         aa(i,j)=0.0D0
282 !         bb(i,j)=0.0D0
283 !         augm(i,j)=0.0D0
284 !         sigma(i,j)=0.0D0
285 !         r0(i,j)=0.0D0
286 !         chi(i,j)=0.0D0
287 !        enddo
288 !       do j=1,2
289 !         bad(i,j)=0.0D0
290 !        enddo
291 !       chip(i)=0.0D0
292 !       alp(i)=0.0D0
293 !       sigma0(i)=0.0D0
294 !       sigii(i)=0.0D0
295 !       rr0(i)=0.0D0
296 !       a0thet(i)=0.0D0
297 !       do j=1,2
298 !         do ichir1=-1,1
299 !          do ichir2=-1,1
300 !          athet(j,i,ichir1,ichir2)=0.0D0
301 !          bthet(j,i,ichir1,ichir2)=0.0D0
302 !          enddo
303 !         enddo
304 !        enddo
305 !        do j=0,3
306 !         polthet(j,i)=0.0D0
307 !        enddo
308 !       do j=1,3
309 !         gthet(j,i)=0.0D0
310 !        enddo
311 !       theta0(i)=0.0D0
312 !       sig0(i)=0.0D0
313 !       sigc0(i)=0.0D0
314 !       do j=1,maxlob
315 !         bsc(j,i)=0.0D0
316 !         do k=1,3
317 !           censc(k,j,i)=0.0D0
318 !          enddo
319 !          do k=1,3
320 !           do l=1,3
321 !             gaussc(l,k,j,i)=0.0D0
322 !            enddo
323 !          enddo
324 !         nlob(i)=0
325 !        enddo
326 !      enddo
327 !      nlob(ntyp1)=0
328 !      dsc(ntyp1)=0.0D0
329 !      do i=-maxtor,maxtor
330 !        itortyp(i)=0
331 !c      write (iout,*) "TU DOCHODZE",i,itortyp(i)
332 !       do iblock=1,2
333 !        do j=-maxtor,maxtor
334 !          do k=1,maxterm
335 !            v1(k,j,i,iblock)=0.0D0
336 !            v2(k,j,i,iblock)=0.0D0
337 !          enddo
338 !        enddo
339 !        enddo
340 !      enddo
341 !      do iblock=1,2
342 !       do i=-maxtor,maxtor
343 !        do j=-maxtor,maxtor
344 !         do k=-maxtor,maxtor
345 !          do l=1,maxtermd_1
346 !            v1c(1,l,i,j,k,iblock)=0.0D0
347 !            v1s(1,l,i,j,k,iblock)=0.0D0
348 !            v1c(2,l,i,j,k,iblock)=0.0D0
349 !            v1s(2,l,i,j,k,iblock)=0.0D0
350 !          enddo !l
351 !          do l=1,maxtermd_2
352 !           do m=1,maxtermd_2
353 !            v2c(m,l,i,j,k,iblock)=0.0D0
354 !            v2s(m,l,i,j,k,iblock)=0.0D0
355 !           enddo !m
356 !          enddo !l
357 !        enddo !k
358 !       enddo !j
359 !      enddo !i
360 !      enddo !iblock
361
362 !      do i=1,maxres
363 !       itype(i,1)=0
364 !       itel(i)=0
365 !      enddo
366 ! Initialize the bridge arrays
367       ns=0
368       nss=0 
369       nhpb=0
370 !      do i=1,maxss
371 !       iss(i)=0
372 !      enddo
373 !      do i=1,maxdim
374 !       dhpb(i)=0.0D0
375 !      enddo
376 !      do i=1,maxres
377 !       ihpb(i)=0
378 !       jhpb(i)=0
379 !      enddo
380 !
381 ! Initialize timing.
382 !
383       call set_timers
384 !
385 ! Initialize variables used in minimization.
386 !   
387 !c     maxfun=5000
388 !c     maxit=2000
389       maxfun=500
390       maxit=200
391       tolf=1.0D-2
392       rtolf=5.0D-4
393
394 ! Initialize the variables responsible for the mode of gradient storage.
395 !
396       nfl=0
397       icg=1
398       
399 #ifdef WHAM_RUN
400       allocate(iww(max_eneW))
401       do i=1,14
402         do j=1,14
403           if (print_order(i).eq.j) then
404             iww(print_order(i))=j
405             goto 1121
406           endif
407         enddo
408 1121    continue
409       enddo
410 #endif
411  
412 #if defined(WHAM_RUN) || defined(CLUSTER)
413       ndih_constr=0
414
415 !      allocate(ww0(max_eneW))
416 !      ww0 = reshape((/1.0d0,1.0d0,1.0d0,1.0d0,1.0d0,1.0d0,1.0d0,1.0d0,&
417 !          1.0d0,1.0d0,1.0d0,1.0d0,1.0d0,1.0d0,1.0d0,1.0d0,0.4d0,1.0d0,&
418 !          1.0d0,0.0d0,0.0/), shape(ww0))
419 !
420       calc_grad=.false.
421 ! Set timers and counters for the respective routines
422       t_func = 0.0d0
423       t_grad = 0.0d0
424       t_fhel = 0.0d0
425       t_fbet = 0.0d0
426       t_ghel = 0.0d0
427       t_gbet = 0.0d0
428       t_viol = 0.0d0
429       t_gviol = 0.0d0
430       n_func = 0
431       n_grad = 0
432       n_fhel = 0
433       n_fbet = 0
434       n_ghel = 0
435       n_gbet = 0
436       n_viol = 0
437       n_gviol = 0
438       n_map = 0
439 #endif
440 !
441 ! Initialize constants used to split the energy into long- and short-range
442 ! components
443 !
444       r_cut=2.0d0
445       rlamb=0.3d0
446 #ifndef SPLITELE
447       nprint_ene=nprint_ene-1
448 #endif
449       return
450       end subroutine initialize
451 !-----------------------------------------------------------------------------
452       subroutine init_int_table
453
454       use geometry, only:int_bounds1
455 !el      use MPI_data
456 !el      implicit none
457 !      implicit real*8 (a-h,o-z)
458 !      include 'DIMENSIONS'
459 #ifdef MPI
460       include 'mpif.h'
461       integer,dimension(15) :: blocklengths,displs
462 #endif
463 !      include 'COMMON.CONTROL'
464 !      include 'COMMON.SETUP'
465 !      include 'COMMON.CHAIN'
466 !      include 'COMMON.INTERACT'
467 !      include 'COMMON.LOCAL'
468 !      include 'COMMON.SBRIDGE'
469 !      include 'COMMON.TORCNSTR'
470 !      include 'COMMON.IOUNITS'
471 !      include 'COMMON.DERIV'
472 !      include 'COMMON.CONTACTS'
473 !el      integer,dimension(0:nfgtasks) :: iturn3_start_all,iturn3_end_all,&
474 !el        iturn4_start_all,iturn4_end_all,iatel_s_all,iatel_e_all  !(0:max_fg_procs)
475 !el      integer,dimension(nres,0:nfgtasks) :: ielstart_all,&
476 !el        ielend_all !(maxres,0:max_fg_procs-1)
477 !el      integer,dimension(0:nfgtasks-1) :: ntask_cont_from_all,&
478 !el        ntask_cont_to_all !(0:max_fg_procs-1),
479 !el      integer,dimension(0:nfgtasks-1,0:nfgtasks-1) :: itask_cont_from_all,&
480 !el        itask_cont_to_all !(0:max_fg_procs-1,0:max_fg_procs-1)
481
482 !el      common /przechowalnia/ iturn3_start_all,iturn3_end_all,&
483 !el        iturn4_start_all,iturn4_end_all,iatel_s_all,iatel_e_all,&
484 !el        ielstart_all,ielend_all,ntask_cont_from_all,itask_cont_from_all,&
485 !el        ntask_cont_to_all,itask_cont_to_all
486
487       integer :: FG_GROUP,CONT_FROM_GROUP,CONT_TO_GROUP
488       logical :: scheck,lprint,flag
489
490 !el local variables
491       integer :: ind_scint=0,ind_scint_old,ii,jj,i,j,iint,itmp
492       integer :: ind_scint_nucl=0
493 #ifdef MPI
494       integer :: my_sc_int(0:nfgtasks-1),my_ele_int(0:nfgtasks-1)
495       integer :: my_sc_intt(0:nfgtasks),my_ele_intt(0:nfgtasks)
496       integer :: n_sc_int_tot,my_sc_inde,my_sc_inds,ind_sctint,npept
497       integer :: n_sc_int_tot_nucl,my_sc_inde_nucl,my_sc_inds_nucl, &
498          ind_sctint_nucl,npept_nucl
499
500       integer :: nele_int_tot,my_ele_inds,my_ele_inde,ind_eleint_old,&
501             ind_eleint,ijunk,nele_int_tot_vdw,my_ele_inds_vdw,&
502             my_ele_inde_vdw,ind_eleint_vdw,ind_eleint_vdw_old,&
503             nscp_int_tot,my_scp_inds,my_scp_inde,ind_scpint,&
504             ind_scpint_old,nsumgrad,nlen,ngrad_start,ngrad_end,&
505             ierror,k,ierr,iaux,ncheck_to,ncheck_from,ind_typ,&
506             ichunk,int_index_old
507       integer :: nele_int_tot_nucl,my_ele_inds_nucl,my_ele_inde_nucl,&
508             ind_eleint_old_nucl,ind_eleint_nucl,nele_int_tot_vdw_nucl,&
509             my_ele_inds_vdw_nucl,my_ele_inde_vdw_nucl,ind_eleint_vdw_nucl,&
510             ind_eleint_vdw_old_nucl,nscp_int_tot_nucl,my_scp_inds_nucl,&
511             my_scp_inde_nucl,ind_scpint_nucl,ind_scpint_old_nucl
512       integer,dimension(5) :: nct_molec,nnt_molec
513 !el      allocate(itask_cont_from(0:nfgtasks-1)) !(0:max_fg_procs-1)
514 !el      allocate(itask_cont_to(0:nfgtasks-1)) !(0:max_fg_procs-1)
515
516 !... Determine the numbers of start and end SC-SC interaction
517 !... to deal with by current processor.
518 !write (iout,*) '******INIT_INT_TABLE nres=',nres,' nnt=',nnt,' nct=',nct
519       do i=0,nfgtasks-1
520         itask_cont_from(i)=fg_rank
521         itask_cont_to(i)=fg_rank
522       enddo
523       lprint=energy_dec
524       itmp=0
525       do i=1,5
526        if (nres_molec(i).eq.0) cycle
527       itmp=itmp+nres_molec(i)
528       if (itype(itmp,i).eq.ntyp1_molec(i)) then
529       nct_molec(i)=itmp-1
530       else
531       nct_molec(i)=itmp
532       endif
533       enddo
534 !      nct_molec(1)=nres_molec(1)-1
535       itmp=0
536       do i=2,5
537        itmp=itmp+nres_molec(i-1)
538       if (itype(itmp+1,i).eq.ntyp1_molec(i)) then
539       nnt_molec(i)=itmp+2
540       else
541       nnt_molec(i)=itmp+1
542       endif
543       enddo
544       print *,"nres_molec",nres_molec(:)
545       print *,"nnt_molec",nnt_molec(:)
546       print *,"nct_molec",nct_molec(:)
547 !      lprint=.true.
548       if (lprint) &
549        write (iout,*)'INIT_INT_TABLE nres=',nres,' nnt=',nnt,' nct=',nct
550       n_sc_int_tot=(nct_molec(1)-nnt+1)*(nct_molec(1)-nnt)/2-nss
551       call int_bounds(n_sc_int_tot,my_sc_inds,my_sc_inde)
552 !write (iout,*) 'INIT_INT_TABLE nres=',nres,' nnt=',nnt,' nct=',nct
553       if (lprint) &
554         write (iout,*) 'Processor',fg_rank,' CG group',kolor,&
555         ' absolute rank',MyRank,&
556         ' n_sc_int_tot',n_sc_int_tot,' my_sc_inds=',my_sc_inds,&
557         ' my_sc_inde',my_sc_inde
558       ind_sctint=0
559       iatsc_s=0
560       iatsc_e=0
561 #endif
562 !el       common /przechowalnia/
563       allocate(iturn3_start_all(0:nfgtasks))
564       allocate(iturn3_end_all(0:nfgtasks))
565       allocate(iturn4_start_all(0:nfgtasks))
566       allocate(iturn4_end_all(0:nfgtasks))
567       allocate(iatel_s_all(0:nfgtasks))
568       allocate(iatel_e_all(0:nfgtasks))
569       allocate(ielstart_all(nres,0:nfgtasks-1))
570       allocate(ielend_all(nres,0:nfgtasks-1))
571
572       allocate(ntask_cont_from_all(0:nfgtasks-1))
573       allocate(ntask_cont_to_all(0:nfgtasks-1))
574       allocate(itask_cont_from_all(0:nfgtasks-1,0:nfgtasks-1))
575       allocate(itask_cont_to_all(0:nfgtasks-1,0:nfgtasks-1))
576 !el----------
577 !      lprint=.false.
578         print *,"NCT",nct_molec(1),nct
579       do i=1,nres !el   !maxres
580         nint_gr(i)=0
581         nscp_gr(i)=0
582         ielstart(i)=0
583         ielend(i)=0
584         do j=1,maxint_gr
585           istart(i,j)=0
586           iend(i,j)=0
587           iscpstart(i,j)=0
588           iscpend(i,j)=0    
589         enddo
590       enddo
591       ind_scint=0
592       ind_scint_old=0
593 !d    write (iout,*) 'ns=',ns,' nss=',nss,' ihpb,jhpb',
594 !d   &   (ihpb(i),jhpb(i),i=1,nss)
595 !       print *,nnt,nct_molec(1)
596       do i=nnt,nct_molec(1)-1
597 !        print*, "inloop",i
598         scheck=.false.
599         if (dyn_ss) goto 10
600         do ii=1,nss
601           if (ihpb(ii).eq.i+nres) then
602             scheck=.true.
603             jj=jhpb(ii)-nres
604             goto 10
605           endif
606         enddo
607    10   continue
608 !        print *,'i=',i,' scheck=',scheck,' jj=',jj
609 !d      write (iout,*) 'i=',i,' scheck=',scheck,' jj=',jj
610         if (scheck) then
611           if (jj.eq.i+1) then
612 #ifdef MPI
613 !            write (iout,*) 'jj=i+1'
614             call int_partition(ind_scint,my_sc_inds,my_sc_inde,i,&
615        iatsc_s,iatsc_e,i+2,nct_molec(1),nint_gr(i),istart(i,1),iend(i,1),*12)
616 #else
617             nint_gr(i)=1
618             istart(i,1)=i+2
619             iend(i,1)=nct
620 #endif
621           else if (jj.eq.nct_molec(1)) then
622 #ifdef MPI
623 !            write (iout,*) 'jj=nct'
624             call int_partition(ind_scint,my_sc_inds,my_sc_inde,i,&
625         iatsc_s,iatsc_e,i+1,nct_molec(1)-1,nint_gr(i),istart(i,1),iend(i,1),*12)
626 #else
627             nint_gr(i)=1
628             istart(i,1)=i+1
629             iend(i,1)=nct_molecule(1)-1
630 #endif
631           else
632 #ifdef MPI
633             call int_partition(ind_scint,my_sc_inds,my_sc_inde,i,&
634        iatsc_s,iatsc_e,i+1,jj-1,nint_gr(i),istart(i,1),iend(i,1),*12)
635             ii=nint_gr(i)+1
636             call int_partition(ind_scint,my_sc_inds,my_sc_inde,i,&
637        iatsc_s,iatsc_e,jj+1,nct_molec(1),nint_gr(i),istart(i,ii),iend(i,ii),*12)
638          
639 #else
640             nint_gr(i)=2
641             istart(i,1)=i+1
642             iend(i,1)=jj-1
643             istart(i,2)=jj+1
644             iend(i,2)=nct_molec(1)
645 #endif
646           endif
647         else
648 #ifdef MPI
649 !          print *,"i for EVDW",iatsc_s,iatsc_e,istart(i,1),iend(i,1),&
650 !          i+1,nct_molec(1),nint_gr(i),ind_scint,my_sc_inds,my_sc_inde,i
651           call int_partition(ind_scint,my_sc_inds,my_sc_inde,i,&
652           iatsc_s,iatsc_e,i+1,nct_molec(1),nint_gr(i), &
653           istart(i,1),iend(i,1),*12)
654 !          print *,"i for EVDW",iatsc_s,iatsc_e,istart(i,1),iend(i,1)
655 #else
656           nint_gr(i)=1
657           istart(i,1)=i+1
658           iend(i,1)=nct_molec(1)
659           ind_scint=ind_scint+nct_molec(1)-i
660 #endif
661         endif
662 #ifdef MPI
663         ind_scint_old=ind_scint
664 #endif
665       enddo
666    12 continue
667 !      print *,"i for EVDW",iatsc_s,iatsc_e,istart(i,1),iend(i,1)
668
669 #ifndef MPI
670       iatsc_s=nnt
671       iatsc_e=nct-1
672 #endif
673       if (iatsc_s.eq.0) iatsc_s=1
674 !----------------- scaling for nucleic acid GB
675       n_sc_int_tot_nucl=(nct_molec(2)-nnt_molec(2)+1)*(nct_molec(2)-nnt_molec(2))/2
676       call int_bounds(n_sc_int_tot_nucl,my_sc_inds_nucl,my_sc_inde_nucl)
677 !write (iout,*) 'INIT_INT_TABLE nres=',nres,' nnt=',nnt,' nct=',nct
678       if (lprint) &
679         write (iout,*) 'Processor',fg_rank,' CG group',kolor,&
680         ' absolute rank',MyRank,&
681         ' n_sc_int_tot',n_sc_int_tot_nucl,' my_sc_inds=',my_sc_inds_nucl,&
682         ' my_sc_inde',my_sc_inde_nucl
683       ind_sctint_nucl=0
684       iatsc_s_nucl=0
685       iatsc_e_nucl=0
686       do i=1,nres !el   !maxres
687         nint_gr_nucl(i)=0
688         nscp_gr_nucl(i)=0
689         ielstart_nucl(i)=0
690         ielend_nucl(i)=0
691         do j=1,maxint_gr
692           istart_nucl(i,j)=0
693           iend_nucl(i,j)=0
694           iscpstart_nucl(i,j)=0
695           iscpend_nucl(i,j)=0
696         enddo
697       enddo
698       do i=nnt_molec(2),nct_molec(2)-1
699 !        print*, "inloop2",i
700       call int_partition(ind_scint_nucl,my_sc_inds_nucl,my_sc_inde_nucl,i,&
701            iatsc_s_nucl,iatsc_e_nucl,i+1,nct_molec(2),nint_gr_nucl(i), &
702            istart_nucl(i,1),iend_nucl(i,1),*112)
703         print *,istart_nucl(i,1)
704       enddo
705   112  continue
706        if (iatsc_s_nucl.eq.0) iatsc_s_nucl=1
707        print *,"tu mam",iatsc_s_nucl,iatsc_e_nucl
708
709 #ifdef MPI
710       if (lprint) write (*,*) 'Processor',fg_rank,' CG Group',kolor,&
711          ' absolute rank',myrank,' iatsc_s=',iatsc_s,' iatsc_e=',iatsc_e
712 #endif
713 !      lprint=.true.
714       if (lprint) then
715       write (iout,'(a)') 'Interaction array:'
716       do i=iatsc_s,iatsc_e
717         write (iout,'(i3,2(2x,2i3))') &
718        i,(istart(i,iint),iend(i,iint),iint=1,nint_gr(i))
719       enddo
720       endif
721 !      lprint=.false.
722       write (iout,'(a)') 'Interaction array2:' 
723       do i=iatsc_s_nucl,iatsc_e_nucl
724         write (iout,'(i3,2(2x,2i4))') &
725        i,(istart_nucl(i,iint),iend_nucl(i,iint),iint=1,nint_gr_nucl(i))
726       enddo
727
728       ispp=4 !?? wham ispp=2
729 #ifdef MPI
730 ! Now partition the electrostatic-interaction array
731       if (itype(nres_molec(1),1).eq.ntyp1_molec(1)) then
732       npept=nres_molec(1)-nnt-1
733       else
734       npept=nres_molec(1)-nnt
735       endif
736       nele_int_tot=(npept-ispp)*(npept-ispp+1)/2
737       call int_bounds(nele_int_tot,my_ele_inds,my_ele_inde)
738       if (lprint) &
739        write (*,*) 'Processor',fg_rank,' CG group',kolor,&
740         ' absolute rank',MyRank,&
741         ' nele_int_tot',nele_int_tot,' my_ele_inds=',my_ele_inds,&
742                     ' my_ele_inde',my_ele_inde
743       iatel_s=0
744       iatel_e=0
745       ind_eleint=0
746       ind_eleint_old=0
747 !      if (itype(nres_molec(1),1).eq.ntyp1_molec(1)) then
748 !      nct_molec(1)=nres_molec(1)-1
749 !      else
750 !      nct_molec(1)=nres_molec(1)
751 !      endif
752 !       print *,"nct",nct,nct_molec(1),itype(nres_molec(1),1),ntyp_molec(1)
753       do i=nnt,nct_molec(1)-3
754         ijunk=0
755         call int_partition(ind_eleint,my_ele_inds,my_ele_inde,i,&
756           iatel_s,iatel_e,i+ispp,nct_molec(1)-1,ijunk,ielstart(i),ielend(i),*13)
757       enddo ! i 
758    13 continue
759       if (iatel_s.eq.0) iatel_s=1
760 !----------now nucleic acid
761 !     if (itype(nres_molec(2),2).eq.ntyp1_molec(2)) then
762       npept_nucl=nct_molec(2)-nnt_molec(2)
763 !     else
764 !     npept_nucl=nct_molec(2)-nnt_molec(2)
765 !     endif
766       nele_int_tot_nucl=(npept_nucl-ispp)*(npept_nucl-ispp+1)/2
767       call int_bounds(nele_int_tot_nucl,my_ele_inds_nucl,my_ele_inde_nucl)
768       if (lprint) &
769        write (*,*) 'Processor',fg_rank,' CG group',kolor,&
770         ' absolute rank',MyRank,&
771         ' nele_int_tot',nele_int_tot,' my_ele_inds=',my_ele_inds,&
772                     ' my_ele_inde',my_ele_inde
773       iatel_s_nucl=0
774       iatel_e_nucl=0
775       ind_eleint_nucl=0
776       ind_eleint_old_nucl=0
777 !      if (itype(nres_molec(1),1).eq.ntyp1_molec(1)) then
778 !      nct_molec(1)=nres_molec(1)-1
779 !      else
780 !      nct_molec(1)=nres_molec(1)
781 !      endif
782 !       print *,"nct",nct,nct_molec(1),itype(nres_molec(1),1),ntyp_molec(1)
783       do i=nnt_molec(2),nct_molec(2)-3
784         ijunk=0
785         call int_partition(ind_eleint_nucl,my_ele_inds_nucl,my_ele_inde_nucl,i,&
786           iatel_s_nucl,iatel_e_nucl,i+ispp,nct_molec(2)-1,&
787           ijunk,ielstart_nucl(i),ielend_nucl(i),*113)
788       enddo ! i 
789   113 continue
790       if (iatel_s.eq.0) iatel_s=1
791
792       nele_int_tot_vdw=(npept-2)*(npept-2+1)/2
793 !      write (iout,*) "nele_int_tot_vdw",nele_int_tot_vdw
794       call int_bounds(nele_int_tot_vdw,my_ele_inds_vdw,my_ele_inde_vdw)
795 !      write (iout,*) "my_ele_inds_vdw",my_ele_inds_vdw,
796 !     & " my_ele_inde_vdw",my_ele_inde_vdw
797       ind_eleint_vdw=0
798       ind_eleint_vdw_old=0
799       iatel_s_vdw=0
800       iatel_e_vdw=0
801       do i=nnt,nct_molec(1)-3
802         ijunk=0
803         call int_partition(ind_eleint_vdw,my_ele_inds_vdw,&
804           my_ele_inde_vdw,i,&
805           iatel_s_vdw,iatel_e_vdw,i+2,nct_molec(1)-1,ijunk,ielstart_vdw(i),&
806           ielend_vdw(i),*15)
807 !        write (iout,*) i," ielstart_vdw",ielstart_vdw(i),
808 !     &   " ielend_vdw",ielend_vdw(i)
809       enddo ! i 
810       if (iatel_s_vdw.eq.0) iatel_s_vdw=1
811    15 continue
812       if (iatel_s.eq.0) iatel_s=1
813
814       nele_int_tot_vdw_nucl=(npept_nucl-2)*(npept_nucl-2+1)/2
815 !      write (iout,*) "nele_int_tot_vdw",nele_int_tot_vdw
816       call int_bounds(nele_int_tot_vdw_nucl,my_ele_inds_vdw_nucl,&
817         my_ele_inde_vdw_nucl)
818 !      write (iout,*) "my_ele_inds_vdw",my_ele_inds_vdw,
819 !     & " my_ele_inde_vdw",my_ele_inde_vdw
820       ind_eleint_vdw_nucl=0
821       ind_eleint_vdw_old_nucl=0
822       iatel_s_vdw_nucl=0
823       iatel_e_vdw_nucl=0
824       do i=nnt_molec(2),nct_molec(2)-3
825         ijunk=0
826         call int_partition(ind_eleint_vdw_nucl,my_ele_inds_vdw_nucl,&
827           my_ele_inde_vdw_nucl,i,&
828           iatel_s_vdw_nucl,iatel_e_vdw_nucl,i+2,nct_molec(2)-1,&
829           ijunk,ielstart_vdw_nucl(i),&
830           ielend_vdw(i),*115)
831 !        write (iout,*) i," ielstart_vdw",ielstart_vdw(i),
832 !     &   " ielend_vdw",ielend_vdw(i)
833       enddo ! i 
834       if (iatel_s_vdw.eq.0) iatel_s_vdw=1
835   115 continue
836
837 #else
838       iatel_s=nnt
839       iatel_e=nct_molec(1)-5 ! ?? wham iatel_e=nct-3
840       do i=iatel_s,iatel_e
841         ielstart(i)=i+4 ! ?? wham +2
842         ielend(i)=nct_molec(1)-1
843       enddo
844       iatel_s_vdw=nnt
845       iatel_e_vdw=nct_molec(1)-3
846       do i=iatel_s_vdw,iatel_e_vdw
847         ielstart_vdw(i)=i+2
848         ielend_vdw(i)=nct_molec(1)-1
849       enddo
850 #endif
851       if (lprint) then
852         write (*,'(a)') 'Processor',fg_rank,' CG group',kolor,&
853         ' absolute rank',MyRank
854         write (iout,*) 'Electrostatic interaction array:'
855         do i=iatel_s,iatel_e
856           write (iout,'(i3,2(2x,2i3))') i,ielstart(i),ielend(i)
857         enddo
858       endif ! lprint
859 !     iscp=3
860       iscp=2
861       iscp_nucl=2
862 ! Partition the SC-p interaction array
863 #ifdef MPI
864       nscp_int_tot=(npept-iscp+1)*(npept-iscp+1)
865       call int_bounds(nscp_int_tot,my_scp_inds,my_scp_inde)
866       if (lprint) write (iout,*) 'Processor',fg_rank,' CG group',kolor,&
867         ' absolute rank',myrank,&
868         ' nscp_int_tot',nscp_int_tot,' my_scp_inds=',my_scp_inds,&
869                     ' my_scp_inde',my_scp_inde
870       iatscp_s=0
871       iatscp_e=0
872       ind_scpint=0
873       ind_scpint_old=0
874       do i=nnt,nct_molec(1)-1
875         if (i.lt.nnt+iscp) then
876 !d        write (iout,*) 'i.le.nnt+iscp'
877           call int_partition(ind_scpint,my_scp_inds,my_scp_inde,i,&
878             iatscp_s,iatscp_e,i+iscp,nct_molec(1),nscp_gr(i),iscpstart(i,1),&
879             iscpend(i,1),*14)
880         else if (i.gt.nct-iscp) then
881 !d        write (iout,*) 'i.gt.nct-iscp'
882           call int_partition(ind_scpint,my_scp_inds,my_scp_inde,i,&
883             iatscp_s,iatscp_e,nnt,i-iscp,nscp_gr(i),iscpstart(i,1),&
884             iscpend(i,1),*14)
885         else
886           call int_partition(ind_scpint,my_scp_inds,my_scp_inde,i,&
887             iatscp_s,iatscp_e,nnt,i-iscp,nscp_gr(i),iscpstart(i,1),&
888            iscpend(i,1),*14)
889           ii=nscp_gr(i)+1
890           call int_partition(ind_scpint,my_scp_inds,my_scp_inde,i,&
891             iatscp_s,iatscp_e,i+iscp,nct_molec(1),nscp_gr(i),iscpstart(i,ii),&
892             iscpend(i,ii),*14)
893         endif
894       enddo ! i
895    14 continue
896       print *,"before inloop3",iatscp_s,iatscp_e,iscp_nucl
897       nscp_int_tot_nucl=(npept_nucl-iscp_nucl+1)*(npept_nucl-iscp_nucl+1)
898       call int_bounds(nscp_int_tot_nucl,my_scp_inds_nucl,my_scp_inde_nucl)
899       if (lprint) write (iout,*) 'Processor',fg_rank,' CG group',kolor,&
900         ' absolute rank',myrank,&
901         ' nscp_int_tot',nscp_int_tot_nucl,' my_scp_inds=',my_scp_inds_nucl,&
902                     ' my_scp_inde',my_scp_inde_nucl
903       print *,"nscp_int_tot_nucl",nscp_int_tot_nucl,my_scp_inds_nucl,my_scp_inde_nucl
904       iatscp_s_nucl=0
905       iatscp_e_nucl=0
906       ind_scpint_nucl=0
907       ind_scpint_old_nucl=0
908       do i=nnt_molec(2),nct_molec(2)-1
909         print *,"inloop3",i,nnt_molec(2)+iscp,nct_molec(2)-iscp
910         if (i.lt.nnt_molec(2)+iscp) then
911 !d        write (iout,*) 'i.le.nnt+iscp'
912           call int_partition(ind_scpint_nucl,my_scp_inds_nucl,&
913             my_scp_inde_nucl,i,iatscp_s_nucl,iatscp_e_nucl,i+iscp,&
914             nct_molec(2),nscp_gr_nucl(i),iscpstart_nucl(i,1),&
915             iscpend_nucl(i,1),*114)
916         else if (i.gt.nct_molec(2)-iscp) then
917 !d        write (iout,*) 'i.gt.nct-iscp'
918           call int_partition(ind_scpint_nucl,my_scp_inds_nucl,&
919             my_scp_inde_nucl,i,&
920             iatscp_s_nucl,iatscp_e_nucl,nnt_molec(2),i-iscp,nscp_gr_nucl(i),&
921             iscpstart_nucl(i,1),&
922             iscpend_nucl(i,1),*114)
923         else
924           call int_partition(ind_scpint_nucl,my_scp_inds_nucl,&
925             my_scp_inde_nucl,i,iatscp_s_nucl,iatscp_e_nucl,nnt_molec(2),&
926             i-iscp,nscp_gr_nucl(i),iscpstart_nucl(i,1),&
927            iscpend_nucl(i,1),*114)
928           ii=nscp_gr_nucl(i)+1
929           call int_partition(ind_scpint_nucl,my_scp_inds_nucl,&
930             my_scp_inde_nucl,i,iatscp_s_nucl,iatscp_e_nucl,i+iscp,&
931             nct_molec(2),nscp_gr_nucl(i),iscpstart_nucl(i,ii),&
932             iscpend_nucl(i,ii),*114)
933         endif
934       enddo ! i
935   114 continue
936       print *, "after inloop3",iatscp_s_nucl,iatscp_e_nucl
937 #else
938       iatscp_s=nnt
939       iatscp_e=nct_molec(1)-1
940       do i=nnt,nct_molec(1)-1
941         if (i.lt.nnt+iscp) then
942           nscp_gr(i)=1
943           iscpstart(i,1)=i+iscp
944           iscpend(i,1)=nct_molec(1)
945         elseif (i.gt.nct-iscp) then
946           nscp_gr(i)=1
947           iscpstart(i,1)=nnt
948           iscpend(i,1)=i-iscp
949         else
950           nscp_gr(i)=2
951           iscpstart(i,1)=nnt
952           iscpend(i,1)=i-iscp
953           iscpstart(i,2)=i+iscp
954           iscpend(i,2)=nct_molec(1)
955         endif 
956       enddo ! i
957 #endif
958       if (iatscp_s.eq.0) iatscp_s=1
959       if (lprint) then
960         write (iout,'(a)') 'SC-p interaction array:'
961         do i=iatscp_s,iatscp_e
962           write (iout,'(i3,2(2x,2i3))') &
963               i,(iscpstart(i,j),iscpend(i,j),j=1,nscp_gr(i))
964         enddo
965       endif ! lprint
966 ! Partition local interactions
967 #ifdef MPI
968       call int_bounds(nres_molec(1)-2,loc_start,loc_end)
969       loc_start=loc_start+1
970       loc_end=loc_end+1
971       call int_bounds(nres_molec(1)-2,ithet_start,ithet_end)
972       ithet_start=ithet_start+2
973       ithet_end=ithet_end+2
974       call int_bounds(nres_molec(2)-2,ithet_nucl_start,ithet_nucl_end)
975       ithet_nucl_start=ithet_nucl_start+2+nres_molec(1)
976       ithet_nucl_end=ithet_nucl_end+2+nres_molec(1)
977       call int_bounds(nct_molec(1)-nnt-2,iturn3_start,iturn3_end) 
978       iturn3_start=iturn3_start+nnt
979       iphi_start=iturn3_start+2
980       iturn3_end=iturn3_end+nnt
981       iphi_end=iturn3_end+2
982       iturn3_start=iturn3_start-1
983       iturn3_end=iturn3_end-1
984       call int_bounds(nct_molec(2)-nnt_molec(2)-2,iphi_nucl_start,iphi_nucl_end)
985       iphi_nucl_start=iphi_nucl_start+nnt_molec(2)+2
986       iphi_nucl_end=iphi_nucl_end+nnt_molec(2)+2
987       print *,"KURDE",iphi_nucl_start,iphi_nucl_end
988       call int_bounds(nres_molec(1)-3,itau_start,itau_end)
989       itau_start=itau_start+3
990       itau_end=itau_end+3
991       call int_bounds(nres_molec(1)-3,iphi1_start,iphi1_end)
992       iphi1_start=iphi1_start+3
993       iphi1_end=iphi1_end+3
994       call int_bounds(nct_molec(1)-nnt-3,iturn4_start,iturn4_end) 
995       iturn4_start=iturn4_start+nnt
996       iphid_start=iturn4_start+2
997       iturn4_end=iturn4_end+nnt
998       iphid_end=iturn4_end+2
999       iturn4_start=iturn4_start-1
1000       iturn4_end=iturn4_end-1
1001 !      print *,"TUTUTU",nres_molec(1),nres
1002       call int_bounds(nres_molec(1)-2,ibond_start,ibond_end) 
1003       ibond_start=ibond_start+1
1004       ibond_end=ibond_end+1
1005 !      print *,ibond_start,ibond_end
1006       call int_bounds(nct_molec(1)-nnt,ibondp_start,ibondp_end) 
1007       ibondp_start=ibondp_start+nnt
1008       ibondp_end=ibondp_end+nnt
1009      call int_bounds(nres_molec(2)-2,ibond_nucl_start,ibond_nucl_end)
1010       ibond_nucl_start=ibond_nucl_start+nnt_molec(2)-1
1011       ibond_nucl_end=ibond_nucl_end+nnt_molec(2)-1
1012       print *,"NUCLibond",ibond_nucl_start,ibond_nucl_end
1013       print *, "before devision",nnt_molec(2),nct_molec(2)-nnt_molec(2)
1014       call int_bounds(nct_molec(2)-nnt_molec(2),ibondp_nucl_start,ibondp_nucl_end)
1015       ibondp_nucl_start=ibondp_nucl_start+nnt_molec(2)
1016       ibondp_nucl_end=ibondp_nucl_end+nnt_molec(2)
1017       print *,"NUCLibond2",ibondp_nucl_start,ibondp_nucl_end
1018
1019
1020       call int_bounds1(nres_molec(1)-1,ivec_start,ivec_end) 
1021 !      print *,"Processor",myrank,fg_rank,fg_rank1,
1022 !     &  " ivec_start",ivec_start," ivec_end",ivec_end
1023       iset_start=loc_start+2
1024       iset_end=loc_end+2
1025       call int_bounds(nres_molec(1),ilip_start,ilip_end)
1026       ilip_start=ilip_start
1027       ilip_end=ilip_end
1028       call int_bounds(nres_molec(1)-1,itube_start,itube_end)
1029       itube_start=itube_start
1030       itube_end=itube_end
1031       if (ndih_constr.eq.0) then
1032         idihconstr_start=1
1033         idihconstr_end=0
1034       else
1035         call int_bounds(ndih_constr,idihconstr_start,idihconstr_end)
1036       endif
1037       if (ntheta_constr.eq.0) then
1038         ithetaconstr_start=1
1039         ithetaconstr_end=0
1040       else
1041         call int_bounds &
1042        (ntheta_constr,ithetaconstr_start,ithetaconstr_end)
1043       endif
1044
1045 !      nsumgrad=(nres-nnt)*(nres-nnt+1)/2
1046 !      nlen=nres-nnt+1
1047       nsumgrad=(nres-nnt)*(nres-nnt+1)/2
1048       nlen=nres-nnt+1
1049       call int_bounds(nsumgrad,ngrad_start,ngrad_end)
1050       igrad_start=((2*nlen+1) &
1051          -sqrt(float((2*nlen-1)**2-8*(ngrad_start-1))))/2
1052       igrad_end=((2*nlen+1) &
1053          -sqrt(float((2*nlen-1)**2-8*(ngrad_end-1))))/2
1054 !el      allocate(jgrad_start(igrad_start:igrad_end))
1055 !el      allocate(jgrad_end(igrad_start:igrad_end)) !(maxres)
1056       jgrad_start(igrad_start)= &
1057          ngrad_start-(2*nlen-igrad_start)*(igrad_start-1)/2 &
1058          +igrad_start
1059       jgrad_end(igrad_start)=nres
1060       if (igrad_end.gt.igrad_start) jgrad_start(igrad_end)=igrad_end+1
1061       jgrad_end(igrad_end)=ngrad_end-(2*nlen-igrad_end)*(igrad_end-1)/2 &
1062           +igrad_end
1063       do i=igrad_start+1,igrad_end-1
1064         jgrad_start(i)=i+1
1065         jgrad_end(i)=nres
1066       enddo
1067       if (lprint) then 
1068         write (*,*) 'Processor:',fg_rank,' CG group',kolor,&
1069        ' absolute rank',myrank,&
1070        ' loc_start',loc_start,' loc_end',loc_end,&
1071        ' ithet_start',ithet_start,' ithet_end',ithet_end,&
1072        ' iphi_start',iphi_start,' iphi_end',iphi_end,&
1073        ' iphid_start',iphid_start,' iphid_end',iphid_end,&
1074        ' ibond_start',ibond_start,' ibond_end',ibond_end,&
1075        ' ibondp_start',ibondp_start,' ibondp_end',ibondp_end,&
1076        ' iturn3_start',iturn3_start,' iturn3_end',iturn3_end,&
1077        ' iturn4_start',iturn4_start,' iturn4_end',iturn4_end,&
1078        ' ivec_start',ivec_start,' ivec_end',ivec_end,&
1079        ' iset_start',iset_start,' iset_end',iset_end,&
1080        ' idihconstr_start',idihconstr_start,' idihconstr_end',&
1081          idihconstr_end
1082        write (*,*) 'Processor:',fg_rank,myrank,' igrad_start',&
1083          igrad_start,' igrad_end',igrad_end,' ngrad_start',ngrad_start,&
1084          ' ngrad_end',ngrad_end
1085 !       do i=igrad_start,igrad_end
1086 !         write(*,*) 'Processor:',fg_rank,myrank,i,&
1087 !          jgrad_start(i),jgrad_end(i)
1088 !       enddo
1089       endif
1090       if (nfgtasks.gt.1) then
1091         call MPI_Allgather(ivec_start,1,MPI_INTEGER,ivec_displ(0),1,&
1092           MPI_INTEGER,FG_COMM1,IERROR)
1093         iaux=ivec_end-ivec_start+1
1094         call MPI_Allgather(iaux,1,MPI_INTEGER,ivec_count(0),1,&
1095           MPI_INTEGER,FG_COMM1,IERROR)
1096         call MPI_Allgather(iset_start-2,1,MPI_INTEGER,iset_displ(0),1,&
1097           MPI_INTEGER,FG_COMM,IERROR)
1098         iaux=iset_end-iset_start+1
1099         call MPI_Allgather(iaux,1,MPI_INTEGER,iset_count(0),1,&
1100           MPI_INTEGER,FG_COMM,IERROR)
1101         call MPI_Allgather(ibond_start,1,MPI_INTEGER,ibond_displ(0),1,&
1102           MPI_INTEGER,FG_COMM,IERROR)
1103         iaux=ibond_end-ibond_start+1
1104         call MPI_Allgather(iaux,1,MPI_INTEGER,ibond_count(0),1,&
1105           MPI_INTEGER,FG_COMM,IERROR)
1106         call MPI_Allgather(ithet_start,1,MPI_INTEGER,ithet_displ(0),1,&
1107           MPI_INTEGER,FG_COMM,IERROR)
1108         iaux=ithet_end-ithet_start+1
1109         call MPI_Allgather(iaux,1,MPI_INTEGER,ithet_count(0),1,&
1110           MPI_INTEGER,FG_COMM,IERROR)
1111         call MPI_Allgather(iphi_start,1,MPI_INTEGER,iphi_displ(0),1,&
1112           MPI_INTEGER,FG_COMM,IERROR)
1113         iaux=iphi_end-iphi_start+1
1114         call MPI_Allgather(iaux,1,MPI_INTEGER,iphi_count(0),1,&
1115           MPI_INTEGER,FG_COMM,IERROR)
1116         call MPI_Allgather(iphi1_start,1,MPI_INTEGER,iphi1_displ(0),1,&
1117           MPI_INTEGER,FG_COMM,IERROR)
1118         iaux=iphi1_end-iphi1_start+1
1119         call MPI_Allgather(iaux,1,MPI_INTEGER,iphi1_count(0),1,&
1120           MPI_INTEGER,FG_COMM,IERROR)
1121         do i=0,nfgtasks-1
1122           do j=1,nres
1123             ielstart_all(j,i)=0
1124             ielend_all(j,i)=0
1125           enddo
1126         enddo
1127         call MPI_Allgather(iturn3_start,1,MPI_INTEGER,&
1128           iturn3_start_all(0),1,MPI_INTEGER,FG_COMM,IERROR)
1129         call MPI_Allgather(iturn4_start,1,MPI_INTEGER,&
1130           iturn4_start_all(0),1,MPI_INTEGER,FG_COMM,IERROR)
1131         call MPI_Allgather(iturn3_end,1,MPI_INTEGER,&
1132           iturn3_end_all(0),1,MPI_INTEGER,FG_COMM,IERROR)
1133         call MPI_Allgather(iturn4_end,1,MPI_INTEGER,&
1134           iturn4_end_all(0),1,MPI_INTEGER,FG_COMM,IERROR)
1135         call MPI_Allgather(iatel_s,1,MPI_INTEGER,&
1136           iatel_s_all(0),1,MPI_INTEGER,FG_COMM,IERROR)
1137         call MPI_Allgather(iatel_e,1,MPI_INTEGER,&
1138           iatel_e_all(0),1,MPI_INTEGER,FG_COMM,IERROR)
1139         call MPI_Allgather(ielstart(1),nres,MPI_INTEGER,&
1140           ielstart_all(1,0),nres,MPI_INTEGER,FG_COMM,IERROR)
1141         call MPI_Allgather(ielend(1),nres,MPI_INTEGER,&
1142           ielend_all(1,0),nres,MPI_INTEGER,FG_COMM,IERROR)
1143         if (lprint) then
1144         write (iout,*) "iatel_s_all",(iatel_s_all(i),i=0,nfgtasks)
1145         write (iout,*) "iatel_e_all",(iatel_e_all(i),i=0,nfgtasks)
1146         write (iout,*) "iturn3_start_all",&
1147           (iturn3_start_all(i),i=0,nfgtasks-1)
1148         write (iout,*) "iturn3_end_all",&
1149           (iturn3_end_all(i),i=0,nfgtasks-1)
1150         write (iout,*) "iturn4_start_all",&
1151           (iturn4_start_all(i),i=0,nfgtasks-1)
1152         write (iout,*) "iturn4_end_all",&
1153           (iturn4_end_all(i),i=0,nfgtasks-1)
1154         write (iout,*) "The ielstart_all array"
1155         do i=nnt,nct
1156           write (iout,'(20i4)') i,(ielstart_all(i,j),j=0,nfgtasks-1)
1157         enddo
1158         write (iout,*) "The ielend_all array"
1159         do i=nnt,nct
1160           write (iout,'(20i4)') i,(ielend_all(i,j),j=0,nfgtasks-1)
1161         enddo
1162         call flush(iout)
1163         endif
1164         ntask_cont_from=0
1165         ntask_cont_to=0
1166         itask_cont_from(0)=fg_rank
1167         itask_cont_to(0)=fg_rank
1168         flag=.false.
1169 !el        allocate(iturn3_sent(4,iturn3_start:iturn3_end))
1170 !el        allocate(iturn4_sent(4,iturn4_start:iturn4_end)) !(4,maxres)
1171         do ii=iturn3_start,iturn3_end
1172           call add_int(ii,ii+2,iturn3_sent(1,ii),&
1173                       ntask_cont_to,itask_cont_to,flag)
1174         enddo
1175         do ii=iturn4_start,iturn4_end
1176           call add_int(ii,ii+3,iturn4_sent(1,ii),&
1177                       ntask_cont_to,itask_cont_to,flag)
1178         enddo
1179         do ii=iturn3_start,iturn3_end
1180           call add_int_from(ii,ii+2,ntask_cont_from,itask_cont_from)
1181         enddo
1182         do ii=iturn4_start,iturn4_end
1183           call add_int_from(ii,ii+3,ntask_cont_from,itask_cont_from)
1184         enddo
1185         if (lprint) then
1186         write (iout,*) "After turn3 ntask_cont_from",ntask_cont_from,&
1187          " ntask_cont_to",ntask_cont_to
1188         write (iout,*) "itask_cont_from",&
1189           (itask_cont_from(i),i=1,ntask_cont_from)
1190         write (iout,*) "itask_cont_to",&
1191           (itask_cont_to(i),i=1,ntask_cont_to)
1192         call flush(iout)
1193         endif
1194 !        write (iout,*) "Loop forward"
1195 !        call flush(iout)
1196         do i=iatel_s,iatel_e
1197 !          write (iout,*) "from loop i=",i
1198 !          call flush(iout)
1199           do j=ielstart(i),ielend(i)
1200             call add_int_from(i,j,ntask_cont_from,itask_cont_from)
1201           enddo
1202         enddo
1203 !        write (iout,*) "Loop backward iatel_e-1",iatel_e-1,
1204 !     &     " iatel_e",iatel_e
1205 !        call flush(iout)
1206         nat_sent=0
1207         do i=iatel_s,iatel_e
1208 !          write (iout,*) "i",i," ielstart",ielstart(i),
1209 !     &      " ielend",ielend(i)
1210 !          call flush(iout)
1211           flag=.false.
1212           do j=ielstart(i),ielend(i)
1213             call add_int(i,j,iint_sent(1,j,nat_sent+1),ntask_cont_to,&
1214                         itask_cont_to,flag)
1215           enddo
1216           if (flag) then
1217             nat_sent=nat_sent+1
1218             iat_sent(nat_sent)=i
1219           endif
1220         enddo
1221         if (lprint) then
1222         write (iout,*)"After longrange ntask_cont_from",ntask_cont_from,&
1223          " ntask_cont_to",ntask_cont_to
1224         write (iout,*) "itask_cont_from",&
1225           (itask_cont_from(i),i=1,ntask_cont_from)
1226         write (iout,*) "itask_cont_to",&
1227           (itask_cont_to(i),i=1,ntask_cont_to)
1228         call flush(iout)
1229         write (iout,*) "iint_sent"
1230         do i=1,nat_sent
1231           ii=iat_sent(i)
1232           write (iout,'(20i4)') ii,(j,(iint_sent(k,j,i),k=1,4),&
1233             j=ielstart(ii),ielend(ii))
1234         enddo
1235         write (iout,*) "iturn3_sent iturn3_start",iturn3_start,&
1236           " iturn3_end",iturn3_end
1237         write (iout,'(20i4)') (i,(iturn3_sent(j,i),j=1,4),&
1238            i=iturn3_start,iturn3_end)
1239         write (iout,*) "iturn4_sent iturn4_start",iturn4_start,&
1240           " iturn4_end",iturn4_end
1241         write (iout,'(20i4)') (i,(iturn4_sent(j,i),j=1,4),&
1242            i=iturn4_start,iturn4_end)
1243         call flush(iout)
1244         endif
1245         call MPI_Gather(ntask_cont_from,1,MPI_INTEGER,&
1246          ntask_cont_from_all,1,MPI_INTEGER,king,FG_COMM,IERR)
1247 !        write (iout,*) "Gather ntask_cont_from ended"
1248 !        call flush(iout)
1249         call MPI_Gather(itask_cont_from(0),nfgtasks,MPI_INTEGER,&
1250          itask_cont_from_all(0,0),nfgtasks,MPI_INTEGER,king,&
1251          FG_COMM,IERR)
1252 !        write (iout,*) "Gather itask_cont_from ended"
1253 !        call flush(iout)
1254         call MPI_Gather(ntask_cont_to,1,MPI_INTEGER,ntask_cont_to_all,&
1255          1,MPI_INTEGER,king,FG_COMM,IERR)
1256 !        write (iout,*) "Gather ntask_cont_to ended"
1257 !        call flush(iout)
1258         call MPI_Gather(itask_cont_to,nfgtasks,MPI_INTEGER,&
1259          itask_cont_to_all,nfgtasks,MPI_INTEGER,king,FG_COMM,IERR)
1260 !        write (iout,*) "Gather itask_cont_to ended"
1261 !        call flush(iout)
1262         if (fg_rank.eq.king) then
1263           write (iout,*)"Contact receive task map (proc, #tasks, tasks)"
1264           do i=0,nfgtasks-1
1265             write (iout,'(20i4)') i,ntask_cont_from_all(i),&
1266               (itask_cont_from_all(j,i),j=1,ntask_cont_from_all(i)) 
1267           enddo
1268           write (iout,*)
1269           call flush(iout)
1270           write (iout,*) "Contact send task map (proc, #tasks, tasks)"
1271           do i=0,nfgtasks-1
1272             write (iout,'(20i4)') i,ntask_cont_to_all(i),&
1273              (itask_cont_to_all(j,i),j=1,ntask_cont_to_all(i)) 
1274           enddo
1275           write (iout,*)
1276           call flush(iout)
1277 ! Check if every send will have a matching receive
1278           ncheck_to=0
1279           ncheck_from=0
1280           do i=0,nfgtasks-1
1281             ncheck_to=ncheck_to+ntask_cont_to_all(i)
1282             ncheck_from=ncheck_from+ntask_cont_from_all(i)
1283           enddo
1284           write (iout,*) "Control sums",ncheck_from,ncheck_to
1285           if (ncheck_from.ne.ncheck_to) then
1286             write (iout,*) "Error: #receive differs from #send."
1287             write (iout,*) "Terminating program...!"
1288             call flush(iout)
1289             flag=.false.
1290           else
1291             flag=.true.
1292             do i=0,nfgtasks-1
1293               do j=1,ntask_cont_to_all(i)
1294                 ii=itask_cont_to_all(j,i)
1295                 do k=1,ntask_cont_from_all(ii)
1296                   if (itask_cont_from_all(k,ii).eq.i) then
1297                     if(lprint)write(iout,*)"Matching send/receive",i,ii
1298                     exit
1299                   endif
1300                 enddo
1301                 if (k.eq.ntask_cont_from_all(ii)+1) then
1302                   flag=.false.
1303                   write (iout,*) "Error: send by",j," to",ii,&
1304                     " would have no matching receive"
1305                 endif
1306               enddo
1307             enddo
1308           endif
1309           if (.not.flag) then
1310             write (iout,*) "Unmatched sends; terminating program"
1311             call flush(iout)
1312           endif
1313         endif
1314         call MPI_Bcast(flag,1,MPI_LOGICAL,king,FG_COMM,IERROR)
1315 !        write (iout,*) "flag broadcast ended flag=",flag
1316 !        call flush(iout)
1317         if (.not.flag) then
1318           call MPI_Finalize(IERROR)
1319           stop "Error in INIT_INT_TABLE: unmatched send/receive."
1320         endif
1321         call MPI_Comm_group(FG_COMM,fg_group,IERR)
1322 !        write (iout,*) "MPI_Comm_group ended"
1323 !        call flush(iout)
1324         call MPI_Group_incl(fg_group,ntask_cont_from+1,&
1325           itask_cont_from(0),CONT_FROM_GROUP,IERR)
1326         call MPI_Group_incl(fg_group,ntask_cont_to+1,itask_cont_to(0),&
1327           CONT_TO_GROUP,IERR)
1328         do i=1,nat_sent
1329           ii=iat_sent(i)
1330           iaux=4*(ielend(ii)-ielstart(ii)+1)
1331           call MPI_Group_translate_ranks(fg_group,iaux,&
1332             iint_sent(1,ielstart(ii),i),CONT_TO_GROUP,&
1333             iint_sent_local(1,ielstart(ii),i),IERR )
1334 !          write (iout,*) "Ranks translated i=",i
1335 !          call flush(iout)
1336         enddo
1337         iaux=4*(iturn3_end-iturn3_start+1)
1338         call MPI_Group_translate_ranks(fg_group,iaux,&
1339            iturn3_sent(1,iturn3_start),CONT_TO_GROUP,&
1340            iturn3_sent_local(1,iturn3_start),IERR)
1341         iaux=4*(iturn4_end-iturn4_start+1)
1342         call MPI_Group_translate_ranks(fg_group,iaux,&
1343            iturn4_sent(1,iturn4_start),CONT_TO_GROUP,&
1344            iturn4_sent_local(1,iturn4_start),IERR)
1345         if (lprint) then
1346         write (iout,*) "iint_sent_local"
1347         do i=1,nat_sent
1348           ii=iat_sent(i)
1349           write (iout,'(20i4)') ii,(j,(iint_sent_local(k,j,i),k=1,4),&
1350             j=ielstart(ii),ielend(ii))
1351           call flush(iout)
1352         enddo
1353         write (iout,*) "iturn3_sent_local iturn3_start",iturn3_start,&
1354           " iturn3_end",iturn3_end
1355         write (iout,'(20i4)') (i,(iturn3_sent_local(j,i),j=1,4),&
1356            i=iturn3_start,iturn3_end)
1357         write (iout,*) "iturn4_sent_local iturn4_start",iturn4_start,&
1358           " iturn4_end",iturn4_end
1359         write (iout,'(20i4)') (i,(iturn4_sent_local(j,i),j=1,4),&
1360            i=iturn4_start,iturn4_end)
1361         call flush(iout)
1362         endif
1363         call MPI_Group_free(fg_group,ierr)
1364         call MPI_Group_free(cont_from_group,ierr)
1365         call MPI_Group_free(cont_to_group,ierr)
1366         call MPI_Type_contiguous(3,MPI_DOUBLE_PRECISION,MPI_UYZ,IERROR)
1367         call MPI_Type_commit(MPI_UYZ,IERROR)
1368         call MPI_Type_contiguous(18,MPI_DOUBLE_PRECISION,MPI_UYZGRAD,&
1369           IERROR)
1370         call MPI_Type_commit(MPI_UYZGRAD,IERROR)
1371         call MPI_Type_contiguous(2,MPI_DOUBLE_PRECISION,MPI_MU,IERROR)
1372         call MPI_Type_commit(MPI_MU,IERROR)
1373         call MPI_Type_contiguous(4,MPI_DOUBLE_PRECISION,MPI_MAT1,IERROR)
1374         call MPI_Type_commit(MPI_MAT1,IERROR)
1375         call MPI_Type_contiguous(8,MPI_DOUBLE_PRECISION,MPI_MAT2,IERROR)
1376         call MPI_Type_commit(MPI_MAT2,IERROR)
1377         call MPI_Type_contiguous(6,MPI_DOUBLE_PRECISION,MPI_THET,IERROR)
1378         call MPI_Type_commit(MPI_THET,IERROR)
1379         call MPI_Type_contiguous(9,MPI_DOUBLE_PRECISION,MPI_GAM,IERROR)
1380         call MPI_Type_commit(MPI_GAM,IERROR)
1381
1382 !el        allocate(lentyp(0:nfgtasks-1))
1383 #ifndef MATGATHER
1384 ! 9/22/08 Derived types to send matrices which appear in correlation terms
1385         do i=0,nfgtasks-1
1386           if (ivec_count(i).eq.ivec_count(0)) then
1387             lentyp(i)=0
1388           else
1389             lentyp(i)=1
1390           endif
1391         enddo
1392         do ind_typ=lentyp(0),lentyp(nfgtasks-1)
1393         if (ind_typ.eq.0) then
1394           ichunk=ivec_count(0)
1395         else
1396           ichunk=ivec_count(1)
1397         endif
1398 !        do i=1,4
1399 !          blocklengths(i)=4
1400 !        enddo
1401 !        displs(1)=0
1402 !        do i=2,4
1403 !          displs(i)=displs(i-1)+blocklengths(i-1)*maxres
1404 !        enddo
1405 !        do i=1,4
1406 !          blocklengths(i)=blocklengths(i)*ichunk
1407 !        enddo
1408 !        write (iout,*) "blocklengths and displs"
1409 !        do i=1,4
1410 !          write (iout,*) i,blocklengths(i),displs(i)
1411 !        enddo
1412 !        call flush(iout)
1413 !        call MPI_Type_indexed(4,blocklengths(1),displs(1),
1414 !     &    MPI_DOUBLE_PRECISION,MPI_ROTAT1(ind_typ),IERROR)
1415 !        call MPI_Type_commit(MPI_ROTAT1(ind_typ),IERROR)
1416 !        write (iout,*) "MPI_ROTAT1",MPI_ROTAT1 
1417 !        do i=1,4
1418 !          blocklengths(i)=2
1419 !        enddo
1420 !        displs(1)=0
1421 !        do i=2,4
1422 !          displs(i)=displs(i-1)+blocklengths(i-1)*maxres
1423 !        enddo
1424 !        do i=1,4
1425 !          blocklengths(i)=blocklengths(i)*ichunk
1426 !        enddo
1427 !        write (iout,*) "blocklengths and displs"
1428 !        do i=1,4
1429 !          write (iout,*) i,blocklengths(i),displs(i)
1430 !        enddo
1431 !        call flush(iout)
1432 !        call MPI_Type_indexed(4,blocklengths(1),displs(1),
1433 !     &    MPI_DOUBLE_PRECISION,MPI_ROTAT2(ind_typ),IERROR)
1434 !        call MPI_Type_commit(MPI_ROTAT2(ind_typ),IERROR)
1435 !        write (iout,*) "MPI_ROTAT2",MPI_ROTAT2 
1436         do i=1,8
1437           blocklengths(i)=2
1438         enddo
1439         displs(1)=0
1440         do i=2,8
1441           displs(i)=displs(i-1)+blocklengths(i-1)*nres !maxres
1442         enddo
1443         do i=1,15
1444           blocklengths(i)=blocklengths(i)*ichunk
1445         enddo
1446         call MPI_Type_indexed(8,blocklengths,displs,&
1447           MPI_DOUBLE_PRECISION,MPI_PRECOMP11(ind_typ),IERROR)
1448         call MPI_Type_commit(MPI_PRECOMP11(ind_typ),IERROR)
1449         do i=1,8
1450           blocklengths(i)=4
1451         enddo
1452         displs(1)=0
1453         do i=2,8
1454           displs(i)=displs(i-1)+blocklengths(i-1)*nres !maxres
1455         enddo
1456         do i=1,15
1457           blocklengths(i)=blocklengths(i)*ichunk
1458         enddo
1459         call MPI_Type_indexed(8,blocklengths,displs,&
1460           MPI_DOUBLE_PRECISION,MPI_PRECOMP12(ind_typ),IERROR)
1461         call MPI_Type_commit(MPI_PRECOMP12(ind_typ),IERROR)
1462         do i=1,6
1463           blocklengths(i)=4
1464         enddo
1465         displs(1)=0
1466         do i=2,6
1467           displs(i)=displs(i-1)+blocklengths(i-1)*nres !maxres
1468         enddo
1469         do i=1,6
1470           blocklengths(i)=blocklengths(i)*ichunk
1471         enddo
1472         call MPI_Type_indexed(6,blocklengths,displs,&
1473           MPI_DOUBLE_PRECISION,MPI_PRECOMP22(ind_typ),IERROR)
1474         call MPI_Type_commit(MPI_PRECOMP22(ind_typ),IERROR)
1475         do i=1,2
1476           blocklengths(i)=8
1477         enddo
1478         displs(1)=0
1479         do i=2,2
1480           displs(i)=displs(i-1)+blocklengths(i-1)*nres !maxres
1481         enddo
1482         do i=1,2
1483           blocklengths(i)=blocklengths(i)*ichunk
1484         enddo
1485         call MPI_Type_indexed(2,blocklengths,displs,&
1486           MPI_DOUBLE_PRECISION,MPI_PRECOMP23(ind_typ),IERROR)
1487         call MPI_Type_commit(MPI_PRECOMP23(ind_typ),IERROR)
1488         do i=1,4
1489           blocklengths(i)=1
1490         enddo
1491         displs(1)=0
1492         do i=2,4
1493           displs(i)=displs(i-1)+blocklengths(i-1)*nres !maxres
1494         enddo
1495         do i=1,4
1496           blocklengths(i)=blocklengths(i)*ichunk
1497         enddo
1498         call MPI_Type_indexed(4,blocklengths,displs,&
1499           MPI_DOUBLE_PRECISION,MPI_ROTAT_OLD(ind_typ),IERROR)
1500         call MPI_Type_commit(MPI_ROTAT_OLD(ind_typ),IERROR)
1501         enddo
1502 #endif
1503       endif
1504       iint_start=ivec_start+1
1505       iint_end=ivec_end+1
1506       do i=0,nfgtasks-1
1507           iint_count(i)=ivec_count(i)
1508           iint_displ(i)=ivec_displ(i)
1509           ivec_displ(i)=ivec_displ(i)-1
1510           iset_displ(i)=iset_displ(i)-1
1511           ithet_displ(i)=ithet_displ(i)-1
1512           iphi_displ(i)=iphi_displ(i)-1
1513           iphi1_displ(i)=iphi1_displ(i)-1
1514           ibond_displ(i)=ibond_displ(i)-1
1515       enddo
1516       if (nfgtasks.gt.1 .and. fg_rank.eq.king &
1517           .and. (me.eq.0 .or. .not. out1file)) then
1518         write (iout,*) "IVEC_DISPL, IVEC_COUNT, ISET_START, ISET_COUNT"
1519         do i=0,nfgtasks-1
1520           write (iout,*) i,ivec_displ(i),ivec_count(i),iset_displ(i),&
1521             iset_count(i)
1522         enddo
1523         write (iout,*) "iphi_start",iphi_start," iphi_end",iphi_end,&
1524           " iphi1_start",iphi1_start," iphi1_end",iphi1_end
1525         write (iout,*)"IPHI_COUNT, IPHI_DISPL, IPHI1_COUNT, IPHI1_DISPL"
1526         do i=0,nfgtasks-1
1527           write (iout,*) i,iphi_count(i),iphi_displ(i),iphi1_count(i),&
1528             iphi1_displ(i)
1529         enddo
1530         write(iout,'(i10,a,i10,a,i10,a/a,i3,a)') n_sc_int_tot,' SC-SC ',&
1531           nele_int_tot,' electrostatic and ',nscp_int_tot,&
1532           ' SC-p interactions','were distributed among',nfgtasks,&
1533           ' fine-grain processors.'
1534       endif
1535 #else
1536       loc_start=2
1537       loc_end=nres_molec(1)-1
1538       ithet_start=3 
1539       ithet_end=nres_molec(1)
1540       ithet_nucl_start=3+nres_molec(1)
1541       ithet_nucl_end=nres_molec(1)+nres_molec(2)
1542       iturn3_start=nnt
1543       iturn3_end=nct_molec(1)-3
1544       iturn4_start=nnt
1545       iturn4_end=nct_molec(1)-4
1546       iphi_start=nnt+3
1547       iphi_end=nct_molec(1)
1548       iphi1_start=4
1549       iphi1_end=nres_molec(1)
1550       iphi_nucl_start=4+nres_molec(1)
1551       iphi_nucl_end=nres_molec(1)+nres_molec(2)
1552       idihconstr_start=1
1553       idihconstr_end=ndih_constr
1554       ithetaconstr_start=1
1555       ithetaconstr_end=ntheta_constr
1556       iphid_start=iphi_start
1557       iphid_end=iphi_end-1
1558       itau_start=4
1559       itau_end=nres_molec(1)
1560       ibond_start=2
1561       ibond_end=nres_molec(1)-1
1562       ibond_nucl_start=2+nres_molec(1)
1563       ibond_nucl_end=nres_molec(2)-1
1564       ibondp_start=nnt
1565       ibondp_end=nct_molec(1)-1
1566       ibondp_nucl_start=nnt_molec(2)
1567       ibondp_nucl_end=nct_molec(2)
1568       ivec_start=1
1569       ivec_end=nres_molec(1)-1
1570       iset_start=3
1571       iset_end=nres_molec(1)+1
1572       iint_start=2
1573       iint_end=nres_molec(1)-1
1574       ilip_start=1
1575       ilip_end=nres_molec(1)
1576       itube_start=1
1577       itube_end=nres_molec(1)
1578 #endif
1579 !el       common /przechowalnia/
1580 !      deallocate(iturn3_start_all)
1581 !      deallocate(iturn3_end_all)
1582 !      deallocate(iturn4_start_all)
1583 !      deallocate(iturn4_end_all)
1584 !      deallocate(iatel_s_all)
1585 !      deallocate(iatel_e_all)
1586 !      deallocate(ielstart_all)
1587 !      deallocate(ielend_all)
1588
1589 !      deallocate(ntask_cont_from_all)
1590 !      deallocate(ntask_cont_to_all)
1591 !      deallocate(itask_cont_from_all)
1592 !      deallocate(itask_cont_to_all)
1593 !el----------
1594       return
1595       end subroutine init_int_table
1596 #ifdef MPI
1597 !-----------------------------------------------------------------------------
1598       subroutine add_int(ii,jj,itask,ntask_cont_to,itask_cont_to,flag)
1599
1600 !el      implicit none
1601 !      include "DIMENSIONS"
1602 !      include "COMMON.INTERACT"
1603 !      include "COMMON.SETUP"
1604 !      include "COMMON.IOUNITS"
1605       integer :: ii,jj,ntask_cont_to
1606       integer,dimension(4) :: itask
1607       integer :: itask_cont_to(0:nfgtasks-1)    !(0:max_fg_procs-1)
1608       logical :: flag
1609 !el      integer,dimension(0:nfgtasks) :: iturn3_start_all,iturn3_end_all,iturn4_start_all,&
1610 !el       iturn4_end_all,iatel_s_all,iatel_e_all        !(0:max_fg_procs)
1611 !el      integer,dimension(nres,0:nfgtasks-1) :: ielstart_all,ielend_all        !(maxres,0:max_fg_procs-1)
1612 !el      common /przechowalnia/ iturn3_start_all,iturn3_end_all,iturn4_start_all,&
1613 !el       iturn4_end_all,iatel_s_all,iatel_e_all,ielstart_all,ielend_all
1614       integer :: iproc,isent,k,l
1615 ! Determines whether to send interaction ii,jj to other processors; a given
1616 ! interaction can be sent to at most 2 processors.
1617 ! Sets flag=.true. if interaction ii,jj needs to be sent to at least 
1618 ! one processor, otherwise flag is unchanged from the input value.
1619       isent=0
1620       itask(1)=fg_rank
1621       itask(2)=fg_rank
1622       itask(3)=fg_rank
1623       itask(4)=fg_rank
1624 !      write (iout,*) "ii",ii," jj",jj
1625 ! Loop over processors to check if anybody could need interaction ii,jj
1626       do iproc=0,fg_rank-1
1627 ! Check if the interaction matches any turn3 at iproc
1628         do k=iturn3_start_all(iproc),iturn3_end_all(iproc)
1629           l=k+2
1630           if (k.eq.ii-1 .and. l.eq.jj-1 .or. k.eq.ii-1 .and. l.eq.jj+1 &
1631          .or. k.eq.ii+1 .and. l.eq.jj+1 .or. k.eq.ii+1 .and. l.eq.jj-1) &
1632           then 
1633 !            write (iout,*) "turn3 to iproc",iproc," ij",ii,jj,"kl",k,l
1634 !            call flush(iout)
1635             flag=.true.
1636             if (iproc.ne.itask(1).and.iproc.ne.itask(2) &
1637               .and.iproc.ne.itask(3).and.iproc.ne.itask(4)) then
1638               isent=isent+1
1639               itask(isent)=iproc
1640               call add_task(iproc,ntask_cont_to,itask_cont_to)
1641             endif
1642           endif
1643         enddo
1644 ! Check if the interaction matches any turn4 at iproc
1645         do k=iturn4_start_all(iproc),iturn4_end_all(iproc)
1646           l=k+3
1647           if (k.eq.ii-1 .and. l.eq.jj-1 .or. k.eq.ii-1 .and. l.eq.jj+1 &
1648          .or. k.eq.ii+1 .and. l.eq.jj+1 .or. k.eq.ii+1 .and. l.eq.jj-1) &
1649           then 
1650 !            write (iout,*) "turn3 to iproc",iproc," ij",ii,jj," kl",k,l
1651 !            call flush(iout)
1652             flag=.true.
1653             if (iproc.ne.itask(1).and.iproc.ne.itask(2) &
1654               .and.iproc.ne.itask(3).and.iproc.ne.itask(4)) then
1655               isent=isent+1
1656               itask(isent)=iproc
1657               call add_task(iproc,ntask_cont_to,itask_cont_to)
1658             endif
1659           endif
1660         enddo
1661         if (iatel_s_all(iproc).gt.0 .and. iatel_e_all(iproc).gt.0 .and. &
1662         iatel_s_all(iproc).le.ii-1 .and. iatel_e_all(iproc).ge.ii-1)then
1663           if (ielstart_all(ii-1,iproc).le.jj-1.and. &
1664               ielend_all(ii-1,iproc).ge.jj-1) then
1665             flag=.true.
1666             if (iproc.ne.itask(1).and.iproc.ne.itask(2) &
1667               .and.iproc.ne.itask(3).and.iproc.ne.itask(4)) then
1668               isent=isent+1
1669               itask(isent)=iproc
1670               call add_task(iproc,ntask_cont_to,itask_cont_to)
1671             endif
1672           endif
1673           if (ielstart_all(ii-1,iproc).le.jj+1.and. &
1674               ielend_all(ii-1,iproc).ge.jj+1) then
1675             flag=.true.
1676             if (iproc.ne.itask(1).and.iproc.ne.itask(2) &
1677               .and.iproc.ne.itask(3).and.iproc.ne.itask(4)) then
1678               isent=isent+1
1679               itask(isent)=iproc
1680               call add_task(iproc,ntask_cont_to,itask_cont_to)
1681             endif
1682           endif
1683         endif
1684       enddo
1685       return
1686       end subroutine add_int
1687 !-----------------------------------------------------------------------------
1688       subroutine add_int_from(ii,jj,ntask_cont_from,itask_cont_from)
1689
1690 !el      use MPI_data
1691 !el      implicit none
1692 !      include "DIMENSIONS"
1693 !      include "COMMON.INTERACT"
1694 !      include "COMMON.SETUP"
1695 !      include "COMMON.IOUNITS"
1696       integer :: ii,jj,itask(2),ntask_cont_from,&
1697        itask_cont_from(0:nfgtasks-1)    !(0:max_fg_procs)
1698       logical :: flag
1699 !el      integer,dimension(0:nfgtasks) :: iturn3_start_all,iturn3_end_all,&
1700 !el       iturn4_start_all,iturn4_end_all,iatel_s_all,iatel_e_all       !(0:max_fg_procs)
1701 !el      integer,dimension(nres,0:nfgtasks-1) :: ielstart_all,ielend_all        !(maxres,0:max_fg_procs-1)
1702 !el      common /przechowalnia/ iturn3_start_all,iturn3_end_all,iturn4_start_all,&
1703 !el       iturn4_end_all,iatel_s_all,iatel_e_all,ielstart_all,ielend_all
1704       integer :: iproc,k,l
1705       do iproc=fg_rank+1,nfgtasks-1
1706         do k=iturn3_start_all(iproc),iturn3_end_all(iproc)
1707           l=k+2
1708           if (k.eq.ii+1 .and. l.eq.jj+1 .or. k.eq.ii+1.and.l.eq.jj-1 &
1709          .or. k.eq.ii-1 .and. l.eq.jj-1 .or. k.eq.ii-1 .and. l.eq.jj+1) &
1710           then
1711 !            write (iout,*)"turn3 from iproc",iproc," ij",ii,jj," kl",k,l
1712             call add_task(iproc,ntask_cont_from,itask_cont_from)
1713           endif
1714         enddo 
1715         do k=iturn4_start_all(iproc),iturn4_end_all(iproc)
1716           l=k+3
1717           if (k.eq.ii+1 .and. l.eq.jj+1 .or. k.eq.ii+1.and.l.eq.jj-1 &
1718          .or. k.eq.ii-1 .and. l.eq.jj-1 .or. k.eq.ii-1 .and. l.eq.jj+1) &
1719           then
1720 !            write (iout,*)"turn4 from iproc",iproc," ij",ii,jj," kl",k,l
1721             call add_task(iproc,ntask_cont_from,itask_cont_from)
1722           endif
1723         enddo 
1724         if (iatel_s_all(iproc).gt.0 .and. iatel_e_all(iproc).gt.0) then
1725           if (ii+1.ge.iatel_s_all(iproc).and.ii+1.le.iatel_e_all(iproc)) &
1726           then
1727             if (jj+1.ge.ielstart_all(ii+1,iproc).and. &
1728                 jj+1.le.ielend_all(ii+1,iproc)) then
1729               call add_task(iproc,ntask_cont_from,itask_cont_from)
1730             endif            
1731             if (jj-1.ge.ielstart_all(ii+1,iproc).and. &
1732                 jj-1.le.ielend_all(ii+1,iproc)) then
1733               call add_task(iproc,ntask_cont_from,itask_cont_from)
1734             endif
1735           endif
1736           if (ii-1.ge.iatel_s_all(iproc).and.ii-1.le.iatel_e_all(iproc)) &
1737           then
1738             if (jj-1.ge.ielstart_all(ii-1,iproc).and. &
1739                 jj-1.le.ielend_all(ii-1,iproc)) then
1740               call add_task(iproc,ntask_cont_from,itask_cont_from)
1741             endif
1742             if (jj+1.ge.ielstart_all(ii-1,iproc).and. &
1743                 jj+1.le.ielend_all(ii-1,iproc)) then
1744                call add_task(iproc,ntask_cont_from,itask_cont_from)
1745             endif
1746           endif
1747         endif
1748       enddo
1749       return
1750       end subroutine add_int_from
1751 !-----------------------------------------------------------------------------
1752       subroutine add_task(iproc,ntask_cont,itask_cont)
1753
1754 !el      use MPI_data
1755 !el      implicit none
1756 !      include "DIMENSIONS"
1757       integer :: iproc,ntask_cont,itask_cont(0:nfgtasks-1)      !(0:max_fg_procs-1)
1758       integer :: ii
1759       do ii=1,ntask_cont
1760         if (itask_cont(ii).eq.iproc) return
1761       enddo
1762       ntask_cont=ntask_cont+1
1763       itask_cont(ntask_cont)=iproc
1764       return
1765       end subroutine add_task
1766 #endif
1767 !-----------------------------------------------------------------------------
1768 #if defined MPI || defined WHAM_RUN
1769       subroutine int_partition(int_index,lower_index,upper_index,atom,&
1770        at_start,at_end,first_atom,last_atom,int_gr,jat_start,jat_end,*)
1771
1772 !      implicit real*8 (a-h,o-z)
1773 !      include 'DIMENSIONS'
1774 !      include 'COMMON.IOUNITS'
1775       integer :: int_index,lower_index,upper_index,atom,at_start,at_end,&
1776        first_atom,last_atom,int_gr,jat_start,jat_end,int_index_old
1777       logical :: lprn
1778       lprn=.false.
1779       if (lprn) write (iout,*) 'int_index=',int_index
1780       int_index_old=int_index
1781       int_index=int_index+last_atom-first_atom+1
1782       if (lprn) &
1783          write (iout,*) 'int_index=',int_index,&
1784                      ' int_index_old',int_index_old,&
1785                      ' lower_index=',lower_index,&
1786                      ' upper_index=',upper_index,&
1787                      ' atom=',atom,' first_atom=',first_atom,&
1788                      ' last_atom=',last_atom
1789       if (int_index.ge.lower_index) then
1790         int_gr=int_gr+1
1791         if (at_start.eq.0) then
1792           at_start=atom
1793           jat_start=first_atom-1+lower_index-int_index_old
1794         else
1795           jat_start=first_atom
1796         endif
1797         if (lprn) write (iout,*) 'jat_start',jat_start
1798         if (int_index.ge.upper_index) then
1799           at_end=atom
1800           jat_end=first_atom-1+upper_index-int_index_old
1801           return 1
1802         else
1803           jat_end=last_atom
1804         endif
1805         if (lprn) write (iout,*) 'jat_end',jat_end
1806       endif
1807       return
1808       end subroutine int_partition
1809 #endif
1810 !-----------------------------------------------------------------------------
1811 #ifndef CLUSTER
1812       subroutine hpb_partition
1813
1814 !      implicit real*8 (a-h,o-z)
1815 !      include 'DIMENSIONS'
1816 #ifdef MPI
1817       include 'mpif.h'
1818 #endif
1819 !      include 'COMMON.SBRIDGE'
1820 !      include 'COMMON.IOUNITS'
1821 !      include 'COMMON.SETUP'
1822 #ifdef MPI
1823       call int_bounds(nhpb,link_start,link_end)
1824       write (iout,*) 'Processor',fg_rank,' CG group',kolor,&
1825         ' absolute rank',MyRank,&
1826         ' nhpb',nhpb,' link_start=',link_start,&
1827         ' link_end',link_end
1828 #else
1829       link_start=1
1830       link_end=nhpb
1831 #endif
1832       return
1833       end subroutine hpb_partition
1834 #endif
1835 !-----------------------------------------------------------------------------
1836 ! misc.f in module io_base
1837 !-----------------------------------------------------------------------------
1838 !-----------------------------------------------------------------------------
1839 ! parmread.F
1840 !-----------------------------------------------------------------------------
1841       subroutine getenv_loc(var, val)
1842
1843       character(*) :: var, val
1844
1845 #ifdef WINIFL
1846       character(len=2000) :: line
1847 !el      external ilen
1848
1849       open (196,file='env',status='old',readonly,shared)
1850       iread=0
1851 !      write(*,*)'looking for ',var
1852 10    read(196,*,err=11,end=11)line
1853       iread=index(line,var)
1854 !      write(*,*)iread,' ',var,' ',line
1855       if (iread.eq.0) go to 10 
1856 !      write(*,*)'---> ',line
1857 11    continue
1858       if(iread.eq.0) then
1859 !       write(*,*)'CHUJ'
1860        val=''
1861       else
1862        iread=iread+ilen(var)+1
1863        read (line(iread:),*,err=12,end=12) val
1864 !       write(*,*)'OK: ',var,' = ',val
1865       endif
1866       close(196)
1867       return
1868 12    val=''
1869       close(196)
1870 #elif (defined CRAY)
1871       integer :: lennam,lenval,ierror
1872 !
1873 !        getenv using a POSIX call, useful on the T3D
1874 !        Sept 1996, comment out error check on advice of H. Pritchard
1875 !
1876       lennam = len(var)
1877       if(lennam.le.0) stop '--error calling getenv--'
1878       call pxfgetenv(var,lennam,val,lenval,ierror)
1879 !-HP- if(ierror.ne.0) stop '--error returned by pxfgetenv--'
1880 #else
1881       call getenv(var,val)
1882 #endif
1883
1884       return
1885       end subroutine getenv_loc
1886 !-----------------------------------------------------------------------------
1887 ! readrtns_CSA.F
1888 !-----------------------------------------------------------------------------
1889       subroutine setup_var
1890
1891       integer :: i
1892 !      implicit real*8 (a-h,o-z)
1893 !      include 'DIMENSIONS'
1894 !      include 'COMMON.IOUNITS'
1895 !      include 'COMMON.GEO'
1896 !      include 'COMMON.VAR'
1897 !      include 'COMMON.INTERACT'
1898 !      include 'COMMON.LOCAL'
1899 !      include 'COMMON.NAMES'
1900 !      include 'COMMON.CHAIN'
1901 !      include 'COMMON.FFIELD'
1902 !      include 'COMMON.SBRIDGE'
1903 !      include 'COMMON.HEADER'
1904 !      include 'COMMON.CONTROL'
1905 !      include 'COMMON.DBASE'
1906 !      include 'COMMON.THREAD'
1907 !      include 'COMMON.TIME1'
1908 ! Set up variable list.
1909       ntheta=nres-2
1910       nphi=nres-3
1911       nvar=ntheta+nphi
1912       nside=0
1913       do i=2,nres-1
1914 #ifdef WHAM_RUN
1915         if (itype(i,1).ne.10) then
1916 #else
1917         if (itype(i,1).ne.10 .and. itype(i,1).ne.ntyp1) then
1918 #endif
1919           nside=nside+1
1920           ialph(i,1)=nvar+nside
1921           ialph(nside,2)=i
1922         endif
1923       enddo
1924       if (indphi.gt.0) then
1925         nvar=nphi
1926       else if (indback.gt.0) then
1927         nvar=nphi+ntheta
1928       else
1929         nvar=nvar+2*nside
1930       endif
1931 !d    write (iout,'(3i4)') (i,ialph(i,1),ialph(i,2),i=2,nres-1)
1932       return
1933       end subroutine setup_var
1934 !-----------------------------------------------------------------------------
1935 ! rescode.f
1936 !-----------------------------------------------------------------------------
1937       integer function rescode(iseq,nam,itype,molecule)
1938
1939       use io_base, only: ucase
1940 !      implicit real*8 (a-h,o-z)
1941 !      include 'DIMENSIONS'
1942 !      include 'COMMON.NAMES'
1943 !      include 'COMMON.IOUNITS'
1944       character(len=3) :: nam   !,ucase
1945       integer :: iseq,itype,i
1946       integer :: molecule
1947       print *,molecule,nam
1948       if (molecule.eq.1) then 
1949       if (itype.eq.0) then
1950
1951       do i=-ntyp1_molec(molecule),ntyp1_molec(molecule)
1952         if (ucase(nam).eq.restyp(i,molecule)) then
1953           rescode=i
1954           return
1955         endif
1956       enddo
1957
1958       else
1959
1960       do i=-ntyp1_molec(molecule),ntyp1_molec(molecule)
1961         if (nam(1:1).eq.onelet(i)) then
1962           rescode=i
1963           return  
1964         endif  
1965       enddo
1966
1967       endif
1968       else if (molecule.eq.2) then
1969       do i=1,ntyp1_molec(molecule)
1970          print *,nam(1:1),restyp(i,molecule)(1:1) 
1971         if (nam(2:2).eq.restyp(i,molecule)(1:1)) then
1972           rescode=i
1973           return
1974         endif
1975       enddo
1976       else if (molecule.eq.3) then
1977        write(iout,*) "SUGAR not yet implemented"
1978        stop
1979       else if (molecule.eq.4) then
1980        write(iout,*) "Explicit LIPID not yet implemented"
1981        stop
1982       else if (molecule.eq.5) then
1983       do i=1,ntyp1_molec(molecule)
1984         print *,i,restyp(i,molecule)(1:2)
1985         if (ucase(nam(1:2)).eq.restyp(i,molecule)(1:2)) then
1986           rescode=i
1987           return
1988         endif
1989       enddo
1990       else   
1991        write(iout,*) "molecule not defined"
1992       endif
1993       write (iout,10) iseq,nam
1994       stop
1995    10 format ('**** Error - residue',i4,' has an unresolved name ',a3)
1996       end function rescode
1997       integer function sugarcode(sugar,ires)
1998       character sugar
1999       integer ires
2000       if (sugar.eq.'D') then
2001         sugarcode=1
2002       else if (sugar.eq.' ') then
2003         sugarcode=2
2004       else
2005         write (iout,*) 'UNKNOWN sugar type for residue',ires,' ',sugar
2006         stop
2007       endif
2008       return
2009       end function sugarcode
2010
2011 !-----------------------------------------------------------------------------
2012 ! timing.F
2013 !-----------------------------------------------------------------------------
2014 ! $Date: 1994/10/05 16:41:52 $
2015 ! $Revision: 2.2 $
2016 !
2017       subroutine set_timers
2018 !
2019 !el      implicit none
2020 !el      real(kind=8) :: tcpu
2021 !      include 'COMMON.TIME1'
2022 !#ifdef MP
2023 #ifdef MPI
2024       include 'mpif.h'
2025 #endif
2026 ! Diminish the assigned time limit a little so that there is some time to
2027 ! end a batch job
2028 !     timlim=batime-150.0
2029 ! Calculate the initial time, if it is not zero (e.g. for the SUN).
2030       stime=tcpu()
2031 #if .not. defined(WHAM_RUN) && .not. defined(CLUSTER)
2032 #ifdef MPI
2033       walltime=MPI_WTIME()
2034       time_reduce=0.0d0
2035       time_allreduce=0.0d0
2036       time_bcast=0.0d0
2037       time_gather=0.0d0
2038       time_sendrecv=0.0d0
2039       time_scatter=0.0d0
2040       time_scatter_fmat=0.0d0
2041       time_scatter_ginv=0.0d0
2042       time_scatter_fmatmult=0.0d0
2043       time_scatter_ginvmult=0.0d0
2044       time_barrier_e=0.0d0
2045       time_barrier_g=0.0d0
2046       time_enecalc=0.0d0
2047       time_sumene=0.0d0
2048       time_lagrangian=0.0d0
2049       time_sumgradient=0.0d0
2050       time_intcartderiv=0.0d0
2051       time_inttocart=0.0d0
2052       time_ginvmult=0.0d0
2053       time_fricmatmult=0.0d0
2054       time_cartgrad=0.0d0
2055       time_bcastc=0.0d0
2056       time_bcast7=0.0d0
2057       time_bcastw=0.0d0
2058       time_intfcart=0.0d0
2059       time_vec=0.0d0
2060       time_mat=0.0d0
2061       time_fric=0.0d0
2062       time_stoch=0.0d0
2063       time_fricmatmult=0.0d0
2064       time_fsample=0.0d0
2065 #endif
2066 #endif
2067 !d    print *,' in SET_TIMERS stime=',stime
2068       return
2069       end subroutine set_timers
2070 !-----------------------------------------------------------------------------
2071 #ifndef CLUSTER
2072       logical function stopx(nf)
2073 ! This function returns .true. if one of the following reasons to exit SUMSL
2074 ! occurs. The "reason" code is stored in WHATSUP passed thru a COMMON block:
2075 !
2076 !... WHATSUP = 0 - go on, no reason to stop. Stopx will return .false.
2077 !...           1 - Time up in current node;
2078 !...           2 - STOP signal was received from another node because the
2079 !...               node's task was accomplished (parallel only);
2080 !...          -1 - STOP signal was received from another node because of error;
2081 !...          -2 - STOP signal was received from another node, because 
2082 !...               the node's time was up.
2083 !      implicit real*8 (a-h,o-z)
2084 !      include 'DIMENSIONS'
2085 !el#ifdef WHAM_RUN
2086 !el      use control_data, only:WhatsUp
2087 !el#endif
2088 #ifdef MP
2089 !el      use MPI_data   !include 'COMMON.INFO'
2090       include 'mpif.h'
2091 #endif
2092       integer :: nf
2093 !el      logical :: ovrtim
2094
2095 !      include 'COMMON.IOUNITS'
2096 !      include 'COMMON.TIME1'
2097       integer :: Kwita
2098
2099 !d    print *,'Processor',MyID,' NF=',nf
2100 !d      write (iout,*) "stopx: ",nf
2101 #ifndef WHAM_RUN
2102 #ifndef MPI
2103       if (ovrtim()) then
2104 ! Finish if time is up.
2105          stopx = .true.
2106          WhatsUp=1
2107 #ifdef MPL
2108       else if (mod(nf,100).eq.0) then
2109 ! Other processors might have finished. Check this every 100th function 
2110 ! evaluation.
2111 ! Master checks if any other processor has sent accepted conformation(s) to it. 
2112          if (MyID.ne.MasterID) call receive_mcm_info
2113          if (MyID.eq.MasterID) call receive_conf
2114 !d       print *,'Processor ',MyID,' is checking STOP: nf=',nf
2115          call recv_stop_sig(Kwita)
2116          if (Kwita.eq.-1) then
2117            write (iout,'(a,i4,a,i5)') 'Processor',&
2118            MyID,' has received STOP signal in STOPX; NF=',nf
2119            write (*,'(a,i4,a,i5)') 'Processor',&
2120            MyID,' has received STOP signal in STOPX; NF=',nf
2121            stopx=.true.
2122            WhatsUp=2
2123          elseif (Kwita.eq.-2) then
2124            write (iout,*) &
2125           'Processor',MyID,' received TIMEUP-STOP signal in SUMSL.'
2126            write (*,*) &
2127           'Processor',MyID,' received TIMEUP-STOP signal in SUMSL.'
2128            WhatsUp=-2
2129            stopx=.true.  
2130          else if (Kwita.eq.-3) then
2131            write (iout,*) &
2132           'Processor',MyID,' received ERROR-STOP signal in SUMSL.'
2133            write (*,*) &
2134           'Processor',MyID,' received ERROR-STOP signal in SUMSL.'
2135            WhatsUp=-1
2136            stopx=.true.
2137          else
2138            stopx=.false.
2139            WhatsUp=0
2140          endif
2141 #endif
2142       else
2143          stopx = .false.
2144          WhatsUp=0
2145       endif
2146 #else
2147       stopx=.false.
2148 !d      write (iout,*) "stopx set at .false."
2149 #endif
2150
2151 #ifdef OSF
2152 ! Check for FOUND_NAN flag
2153       if (FOUND_NAN) then
2154         write(iout,*)"   ***   stopx : Found a NaN"
2155         stopx=.true.
2156       endif
2157 #endif
2158 #else
2159       if (ovrtim()) then
2160 ! Finish if time is up.
2161          stopx = .true.
2162          WhatsUp=1
2163       else if (cutoffviol) then
2164         stopx = .true.
2165         WhatsUp=2
2166       else
2167         stopx=.false.
2168       endif
2169 #endif
2170       return
2171       end function stopx
2172 !-----------------------------------------------------------------------------
2173 #else
2174       logical function stopx(nf)
2175 !
2176 !     ..................................................................
2177 !
2178 !     *****PURPOSE...
2179 !     THIS FUNCTION MAY SERVE AS THE STOPX (ASYNCHRONOUS INTERRUPTION)
2180 !     FUNCTION FOR THE NL2SOL (NONLINEAR LEAST-SQUARES) PACKAGE AT
2181 !     THOSE INSTALLATIONS WHICH DO NOT WISH TO IMPLEMENT A
2182 !     DYNAMIC STOPX.
2183 !
2184 !     *****ALGORITHM NOTES...
2185 !     AT INSTALLATIONS WHERE THE NL2SOL SYSTEM IS USED
2186 !     INTERACTIVELY, THIS DUMMY STOPX SHOULD BE REPLACED BY A
2187 !     FUNCTION THAT RETURNS .TRUE. IF AND ONLY IF THE INTERRUPT
2188 !     (BREAK) KEY HAS BEEN PRESSED SINCE THE LAST CALL ON STOPX.
2189 !
2190 !     $$$ MODIFIED FOR USE AS  THE TIMER ROUTINE.
2191 !     $$$                              WHEN THE TIME LIMIT HAS BEEN
2192 !     $$$ REACHED     STOPX IS SET TO .TRUE  AND INITIATES (IN ITSUM)
2193 !     $$$ AND ORDERLY EXIT OUT OF SUMSL.  IF ARRAYS IV AND V ARE
2194 !     $$$ SAVED, THE SUMSL ROUTINES CAN BE RESTARTED AT THE SAME
2195 !     $$$ POINT AT WHICH THEY WERE INTERRUPTED.
2196 !
2197 !     ..................................................................
2198 !
2199 !      include 'DIMENSIONS'
2200       integer :: nf
2201 !      logical ovrtim
2202 !      include 'COMMON.IOUNITS'
2203 !      include 'COMMON.TIME1'
2204 #ifdef MPL
2205 !     include 'COMMON.INFO'
2206       integer :: Kwita
2207
2208 !d    print *,'Processor',MyID,' NF=',nf
2209 #endif
2210       if (ovrtim()) then
2211 ! Finish if time is up.
2212          stopx = .true.
2213 #ifdef MPL
2214       else if (mod(nf,100).eq.0) then
2215 ! Other processors might have finished. Check this every 100th function 
2216 ! evaluation.
2217 !d       print *,'Processor ',MyID,' is checking STOP: nf=',nf
2218          call recv_stop_sig(Kwita)
2219          if (Kwita.eq.-1) then
2220            write (iout,'(a,i4,a,i5)') 'Processor',&
2221            MyID,' has received STOP signal in STOPX; NF=',nf
2222            write (*,'(a,i4,a,i5)') 'Processor',&
2223            MyID,' has received STOP signal in STOPX; NF=',nf
2224            stopx=.true.
2225          else
2226            stopx=.false.
2227          endif
2228 #endif
2229       else
2230          stopx = .false.
2231       endif
2232       return
2233       end function stopx
2234 #endif
2235 !-----------------------------------------------------------------------------
2236       logical function ovrtim()
2237
2238 !      include 'DIMENSIONS'
2239 !      include 'COMMON.IOUNITS'
2240 !      include 'COMMON.TIME1'
2241 !el      real(kind=8) :: tcpu
2242       real(kind=8) :: curtim
2243 #ifdef MPI
2244       include "mpif.h"
2245       curtim = MPI_Wtime()-walltime
2246 #else
2247       curtim= tcpu()
2248 #endif
2249 !  curtim is the current time in seconds.
2250 !      write (iout,*) "curtim",curtim," timlim",timlim," safety",safety
2251 #ifndef WHAM_RUN
2252       if (curtim .ge. timlim - safety) then
2253         write (iout,'(a,f10.2,a,f10.2,a,f10.2,a)') &
2254         "***************** Elapsed time (",curtim,&
2255         " s) is within the safety limit (",safety,&
2256         " s) of the allocated time (",timlim," s). Terminating."
2257         ovrtim=.true.
2258       else
2259         ovrtim=.false.
2260       endif
2261 #else
2262       ovrtim=.false.
2263 #endif
2264 !elwrite (iout,*) "ovrtim",ovrtim
2265       return
2266       end function ovrtim
2267 !-----------------------------------------------------------------------------
2268       real(kind=8) function tcpu()
2269
2270 !      include 'COMMON.TIME1'
2271       real(kind=8) :: seconds
2272 #ifdef ES9000
2273 !***************************
2274 ! Next definition for EAGLE (ibm-es9000)
2275       real(kind=8) :: micseconds
2276       integer :: rcode
2277       tcpu=cputime(micseconds,rcode)
2278       tcpu=(micseconds/1.0E6) - stime
2279 !***************************
2280 #endif
2281 #ifdef SUN
2282 !***************************
2283 ! Next definitions for sun
2284       REAL(kind=8) ::  ECPU,ETIME,ETCPU
2285       real(kind=8),dimension(2) :: tarray
2286       tcpu=etime(tarray)
2287       tcpu=tarray(1)
2288 !***************************
2289 #endif
2290 #ifdef KSR
2291 !***************************
2292 ! Next definitions for ksr
2293 ! this function uses the ksr timer ALL_SECONDS from the PMON library to
2294 ! return the elapsed time in seconds
2295       tcpu= all_seconds() - stime
2296 !***************************
2297 #endif
2298 #ifdef SGI
2299 !***************************
2300 ! Next definitions for sgi
2301       real(kind=4) :: timar(2), etime
2302       seconds = etime(timar)
2303 !d    print *,'seconds=',seconds,' stime=',stime
2304 !      usrsec = timar(1)
2305 !      syssec = timar(2)
2306       tcpu=seconds - stime
2307 !***************************
2308 #endif
2309
2310 #ifdef LINUX
2311 !***************************
2312 ! Next definitions for sgi
2313       real(kind=4) :: timar(2), etime
2314       seconds = etime(timar)
2315 !d    print *,'seconds=',seconds,' stime=',stime
2316 !      usrsec = timar(1)
2317 !      syssec = timar(2)
2318       tcpu=seconds - stime
2319 !***************************
2320 #endif
2321
2322
2323 #ifdef CRAY
2324 !***************************
2325 ! Next definitions for Cray
2326 !     call date(curdat)
2327 !     curdat=curdat(1:9)
2328 !     call clock(curtim)
2329 !     curtim=curtim(1:8)
2330       cpusec = second()
2331       tcpu=cpusec - stime
2332 !***************************
2333 #endif
2334 #ifdef AIX
2335 !***************************
2336 ! Next definitions for RS6000
2337        integer(kind=4) :: i1,mclock
2338        i1 = mclock()
2339        tcpu = (i1+0.0D0)/100.0D0
2340 #endif
2341 #ifdef WINPGI
2342 !***************************
2343 ! next definitions for windows NT Digital fortran
2344        real(kind=4) :: time_real
2345        call cpu_time(time_real)
2346        tcpu = time_real
2347 #endif
2348 #ifdef WINIFL
2349 !***************************
2350 ! next definitions for windows NT Digital fortran
2351        real(kind=4) :: time_real
2352        call cpu_time(time_real)
2353        tcpu = time_real
2354 #endif
2355       tcpu = 0d0 !el
2356       return
2357       end function tcpu
2358 !-----------------------------------------------------------------------------
2359 #ifndef CLUSTER
2360       subroutine dajczas(rntime,hrtime,mintime,sectime)
2361
2362 !      include 'COMMON.IOUNITS'
2363       integer :: ihr,imn,isc
2364       real(kind=8) :: rntime,hrtime,mintime,sectime 
2365       hrtime=rntime/3600.0D0 
2366       hrtime=aint(hrtime)
2367       mintime=aint((rntime-3600.0D0*hrtime)/60.0D0)
2368       sectime=aint((rntime-3600.0D0*hrtime-60.0D0*mintime)+0.5D0)
2369       if (sectime.eq.60.0D0) then
2370         sectime=0.0D0
2371         mintime=mintime+1.0D0
2372       endif
2373       ihr=hrtime
2374       imn=mintime
2375       isc=sectime
2376       write (iout,328) ihr,imn,isc
2377   328 FORMAT(//'***** Computation time: ',I4  ,' hours ',I2  ,&
2378                ' minutes ', I2  ,' seconds *****')       
2379       return
2380       end subroutine dajczas
2381 !-----------------------------------------------------------------------------
2382       subroutine print_detailed_timing
2383
2384 !el      use MPI_data
2385 !      implicit real*8 (a-h,o-z)
2386 !      include 'DIMENSIONS'
2387 #ifdef MPI
2388       include 'mpif.h'
2389 #endif
2390 !      include 'COMMON.IOUNITS'
2391 !      include 'COMMON.TIME1'
2392 !      include 'COMMON.SETUP'
2393       real(kind=8) :: time1,time_barrier
2394       time_barrier = 0.0d0
2395 #ifdef MPI !el
2396       time1=MPI_WTIME()
2397 #endif !el
2398          write (iout,'(80(1h=)/a/(80(1h=)))') &
2399           "Details of FG communication time"
2400          write (*,'(7(a40,1pe15.5/),40(1h-)/a40,1pe15.5/80(1h=))') &
2401           "BROADCAST:",time_bcast,"REDUCE:",time_reduce,&
2402           "GATHER:",time_gather,&
2403           "SCATTER:",time_scatter,"SENDRECV:",time_sendrecv,&
2404           "BARRIER ene",time_barrier_e,&
2405           "BARRIER grad",time_barrier_g,&
2406           "TOTAL:",&
2407           time_bcast+time_reduce+time_gather+time_scatter+time_sendrecv
2408          write (*,*) fg_rank,myrank,&
2409            ': Total wall clock time',time1-walltime,' sec'
2410          write (*,*) "Processor",fg_rank,myrank,&
2411            ": BROADCAST time",time_bcast," REDUCE time",&
2412             time_reduce," GATHER time",time_gather," SCATTER time",&
2413             time_scatter,&
2414            " SCATTER fmatmult",time_scatter_fmatmult,&
2415            " SCATTER ginvmult",time_scatter_ginvmult,&
2416            " SCATTER fmat",time_scatter_fmat,&
2417            " SCATTER ginv",time_scatter_ginv,&
2418             " SENDRECV",time_sendrecv,&
2419             " BARRIER ene",time_barrier_e,&
2420             " BARRIER GRAD",time_barrier_g,&
2421             " BCAST7",time_bcast7," BCASTC",time_bcastc,&
2422             " BCASTW",time_bcastw," ALLREDUCE",time_allreduce,&
2423             " TOTAL",&
2424             time_bcast+time_reduce+time_gather+time_scatter+ &
2425             time_sendrecv+time_barrier+time_bcastc
2426 !el#endif
2427          write (*,*) "Processor",fg_rank,myrank," enecalc",time_enecalc
2428          write (*,*) "Processor",fg_rank,myrank," sumene",time_sumene
2429          write (*,*) "Processor",fg_rank,myrank," intfromcart",&
2430            time_intfcart
2431          write (*,*) "Processor",fg_rank,myrank," vecandderiv",&
2432            time_vec
2433          write (*,*) "Processor",fg_rank,myrank," setmatrices",&
2434            time_mat
2435          write (*,*) "Processor",fg_rank,myrank," ginvmult",&
2436            time_ginvmult
2437          write (*,*) "Processor",fg_rank,myrank," fricmatmult",&
2438            time_fricmatmult
2439          write (*,*) "Processor",fg_rank,myrank," inttocart",&
2440            time_inttocart
2441          write (*,*) "Processor",fg_rank,myrank," sumgradient",&
2442            time_sumgradient
2443          write (*,*) "Processor",fg_rank,myrank," intcartderiv",&
2444            time_intcartderiv
2445          if (fg_rank.eq.0) then
2446            write (*,*) "Processor",fg_rank,myrank," lagrangian",&
2447              time_lagrangian
2448            write (*,*) "Processor",fg_rank,myrank," cartgrad",&
2449              time_cartgrad
2450          endif
2451       return
2452       end subroutine print_detailed_timing
2453 #endif
2454 !-----------------------------------------------------------------------------
2455 !-----------------------------------------------------------------------------
2456       end module control