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