Commit changes Adam
[unres.git] / source / cluster / wham / src-restraint-DFA / initialize_p.F
1       subroutine initialize
2
3 C Define constants and zero out tables.
4 C
5       implicit real*8 (a-h,o-z)
6       include 'DIMENSIONS'
7       include 'sizesclu.dat'
8 #ifdef MPI
9       include 'mpif.h'
10 #endif
11       include 'COMMON.IOUNITS'
12       include 'COMMON.CHAIN'
13       include 'COMMON.INTERACT'
14       include 'COMMON.GEO'
15       include 'COMMON.LOCAL'
16       include 'COMMON.TORSION'
17       include 'COMMON.FFIELD'
18       include 'COMMON.SBRIDGE'
19       include 'COMMON.MINIM' 
20       include 'COMMON.DERIV'
21       include "COMMON.NAMES"
22       include "COMMON.TIME1"
23 C
24 C The following is just to define auxiliary variables used in angle conversion
25 C
26       pi=4.0D0*datan(1.0D0)
27       dwapi=2.0D0*pi
28       dwapi3=dwapi/3.0D0
29       pipol=0.5D0*pi
30       deg2rad=pi/180.0D0
31       rad2deg=1.0D0/deg2rad
32       angmin=10.0D0*deg2rad
33       Rgas = 1.987D-3
34 C
35 C Define I/O units.
36 C
37       inp=    1
38       iout=   2
39       ipdbin= 3
40       ipdb=   7
41       imol2= 18
42       jplot= 19
43       jstatin=10
44       imol2=  4
45       igeom=  8
46       intin=  9
47       ithep= 11
48       irotam=12
49       itorp= 13
50       itordp= 23
51       ielep= 14
52       isidep=15 
53       isidep1=22
54       iscpp=25
55       icbase=16
56       ifourier=20
57       istat= 17
58       ibond=28
59       isccor=29
60       jrms=30
61 C
62 C Set default weights of the energy terms.
63 C
64       wlong=1.0D0
65       welec=1.0D0
66       wtor =1.0D0
67       wang =1.0D0
68       wscloc=1.0D0
69       wstrain=1.0D0
70 C
71 C Zero out tables.
72 C
73       ndih_constr=0
74       do i=1,maxres2
75         do j=1,3
76           c(j,i)=0.0D0
77           dc(j,i)=0.0D0
78         enddo
79       enddo
80       do i=1,maxres
81         do j=1,3
82           xloc(j,i)=0.0D0
83         enddo
84       enddo
85       do i=1,ntyp
86         do j=1,ntyp
87           aa(i,j)=0.0D0
88           bb(i,j)=0.0D0
89           augm(i,j)=0.0D0
90           sigma(i,j)=0.0D0
91           r0(i,j)=0.0D0
92           chi(i,j)=0.0D0
93         enddo
94         do j=1,2
95           bad(i,j)=0.0D0
96         enddo
97         chip(i)=0.0D0
98         alp(i)=0.0D0
99         sigma0(i)=0.0D0
100         sigii(i)=0.0D0
101         rr0(i)=0.0D0
102         a0thet(i)=0.0D0
103         do j=1,2
104           athet(j,i)=0.0D0
105           bthet(j,i)=0.0D0
106         enddo
107         do j=0,3
108           polthet(j,i)=0.0D0
109         enddo
110         do j=1,3
111           gthet(j,i)=0.0D0
112         enddo
113         theta0(i)=0.0D0
114         sig0(i)=0.0D0
115         sigc0(i)=0.0D0
116         do j=1,maxlob
117           bsc(j,i)=0.0D0
118           do k=1,3
119             censc(k,j,i)=0.0D0
120           enddo
121           do k=1,3
122             do l=1,3
123               gaussc(l,k,j,i)=0.0D0
124             enddo
125           enddo
126           nlob(i)=0
127         enddo
128       enddo
129       nlob(ntyp1)=0
130       dsc(ntyp1)=0.0D0
131       do i=1,maxtor
132         itortyp(i)=0
133         do j=1,maxtor
134           do k=1,maxterm
135             v1(k,j,i)=0.0D0
136             v2(k,j,i)=0.0D0
137           enddo
138         enddo
139       enddo
140       do i=1,maxres
141         itype(i)=0
142         itel(i)=0
143       enddo
144 C Initialize the bridge arrays
145       ns=0
146       nss=0 
147       nhpb=0
148       do i=1,maxss
149         iss(i)=0
150       enddo
151       do i=1,maxss
152         dhpb(i)=0.0D0
153       enddo
154       do i=1,maxss
155         ihpb(i)=0
156         jhpb(i)=0
157       enddo
158       do i=1,maxres
159         dyn_ss_mask(i)=.false.
160       enddo
161 C
162 C Initialize timing.
163 C
164       call set_timers
165 C
166 C Initialize variables used in minimization.
167 C   
168 c     maxfun=5000
169 c     maxit=2000
170       maxfun=500
171       maxit=200
172       tolf=1.0D-2
173       rtolf=5.0D-4
174
175 C Initialize the variables responsible for the mode of gradient storage.
176 C
177       nfl=0
178       icg=1
179       do i=1,14
180         do j=1,14
181           if (print_order(i).eq.j) then
182             iw(print_order(i))=j
183             goto 1121
184           endif
185         enddo
186 1121    continue
187       enddo
188       calc_grad=.false.
189 C Set timers and counters for the respective routines
190       t_func = 0.0d0
191       t_grad = 0.0d0
192       t_fhel = 0.0d0
193       t_fbet = 0.0d0
194       t_ghel = 0.0d0
195       t_gbet = 0.0d0
196       t_viol = 0.0d0
197       t_gviol = 0.0d0
198       n_func = 0
199       n_grad = 0
200       n_fhel = 0
201       n_fbet = 0
202       n_ghel = 0
203       n_gbet = 0
204       n_viol = 0
205       n_gviol = 0
206       n_map = 0
207 #ifndef SPLITELE
208       nprint_ene=nprint_ene-1
209 #endif
210       return
211       end
212 c-------------------------------------------------------------------------
213       block data nazwy
214       implicit real*8 (a-h,o-z)
215       include 'DIMENSIONS'
216       include 'sizesclu.dat'
217       include 'COMMON.NAMES'
218       include 'COMMON.FFIELD'
219       data restyp /
220      &'CYS','MET','PHE','ILE','LEU','VAL','TRP','TYR','ALA','GLY','THR',
221      &'SER','GLN','ASN','GLU','ASP','HIS','ARG','LYS','PRO','D'/
222       data onelet /
223      &'C','M','F','I','L','V','W','Y','A','G','T',
224      &'S','Q','N','E','D','H','R','K','P','X'/
225       data potname /'LJ','LJK','BP','GB','GBV'/
226       data ename /
227      &   "EVDW SC-SC","EVDW2 SC-p","EES p-p","ECORR4 ","ECORR5 ",
228      &   "ECORR6 ","EELLO ","ETURN3 ","ETURN4 ","ETURN6 ",
229      &   "EBE bend","ESC SCloc","ETORS ","ETORSD ","EHPB","EVDWPP",
230      &   "ESTR","ESCCOR","EVDW2_14","EDIHCNSTR","EHOMOLOGY",
231      &   "DFA DIS","DFA TOR","DFA NEI","DFA BET"," "," "/
232       data wname /
233      &   "WSC","WSCP","WELEC","WCORR","WCORR5","WCORR6","WEL_LOC",
234      &   "WTURN3","WTURN4","WTURN6","WANG","WSCLOC","WTOR","WTORD",
235      &   "WHPB","WVDWPP","WBOND","WSCCOR","WSCP14","WDIHCNSTR",
236      &   "WHOMOLOGY","WDFAD","WDFAT","WDFAN","WDFAB"," "," "/
237       data nprint_ene /23/
238       data print_order /1,2,3,17,11,12,13,14,4,5,6,7,8,9,10,16,15,18,19,
239      &   20,21,22,23,24,25/
240       end 
241 c---------------------------------------------------------------------------
242       subroutine init_int_table
243       implicit real*8 (a-h,o-z)
244       include 'DIMENSIONS'
245       include 'sizesclu.dat'
246 #ifdef MPI
247       include 'mpif.h'
248 #endif
249 #ifdef MPL
250       include 'COMMON.INFO'
251 #endif
252       include 'COMMON.CHAIN'
253       include 'COMMON.INTERACT'
254       include 'COMMON.LOCAL'
255       include 'COMMON.SBRIDGE'
256       include 'COMMON.IOUNITS'
257       logical scheck,lprint
258 #ifdef MPL
259       integer my_sc_int(0:max_fg_Procs-1),my_sc_intt(0:max_fg_Procs),
260      & my_ele_int(0:max_fg_Procs-1),my_ele_intt(0:max_fg_Procs)
261 C... Determine the numbers of start and end SC-SC interaction 
262 C... to deal with by current processor.
263       lprint=.false.
264       if (lprint)
265      &write (iout,*) 'INIT_INT_TABLE nres=',nres,' nnt=',nnt,' nct=',nct
266       n_sc_int_tot=(nct-nnt+1)*(nct-nnt)/2-nss
267       MyRank=MyID-(MyGroup-1)*fgProcs
268       call int_bounds(n_sc_int_tot,my_sc_inds,my_sc_inde)
269       if (lprint)
270      &  write (iout,*) 'Processor',MyID,' MyRank',MyRank,
271      &  ' n_sc_int_tot',n_sc_int_tot,' my_sc_inds=',my_sc_inds,
272      &  ' my_sc_inde',my_sc_inde
273       ind_sctint=0
274       iatsc_s=0
275       iatsc_e=0
276 #endif
277       lprint=.false.
278       do i=1,maxres
279         nint_gr(i)=0
280         nscp_gr(i)=0
281         do j=1,maxint_gr
282           istart(i,1)=0
283           iend(i,1)=0
284           ielstart(i)=0
285           ielend(i)=0
286           iscpstart(i,1)=0
287           iscpend(i,1)=0    
288         enddo
289       enddo
290       ind_scint=0
291       ind_scint_old=0
292 cd    write (iout,*) 'ns=',ns,' nss=',nss,' ihpb,jhpb',
293 cd   &   (ihpb(i),jhpb(i),i=1,nss)
294       do i=nnt,nct-1
295         scheck=.false.
296         do ii=1,nss
297           if (ihpb(ii).eq.i+nres) then
298             scheck=.true.
299             if (dyn_ss) go to 10
300             jj=jhpb(ii)-nres
301             goto 10
302           endif
303         enddo
304    10   continue
305 cd      write (iout,*) 'i=',i,' scheck=',scheck,' jj=',jj
306         if (scheck) then
307           if (jj.eq.i+1) then
308 #ifdef MPL
309             write (iout,*) 'jj=i+1'
310             call int_partition(ind_scint,my_sc_inds,my_sc_inde,i,
311      & iatsc_s,iatsc_e,i+2,nct,nint_gr(i),istart(i,1),iend(i,1),*12)
312 #else
313             nint_gr(i)=1
314             istart(i,1)=i+2
315             iend(i,1)=nct
316 #endif
317           else if (jj.eq.nct) then
318 #ifdef MPL
319             write (iout,*) 'jj=nct'
320             call int_partition(ind_scint,my_sc_inds,my_sc_inde,i,
321      &  iatsc_s,iatsc_e,i+1,nct-1,nint_gr(i),istart(i,1),iend(i,1),*12)
322 #else
323             nint_gr(i)=1
324             istart(i,1)=i+1
325             iend(i,1)=nct-1
326 #endif
327           else
328 #ifdef MPL
329             call int_partition(ind_scint,my_sc_inds,my_sc_inde,i,
330      & iatsc_s,iatsc_e,i+1,jj-1,nint_gr(i),istart(i,1),iend(i,1),*12)
331             ii=nint_gr(i)+1
332             call int_partition(ind_scint,my_sc_inds,my_sc_inde,i,
333      & iatsc_s,iatsc_e,jj+1,nct,nint_gr(i),istart(i,ii),iend(i,ii),*12)
334 #else
335             nint_gr(i)=2
336             istart(i,1)=i+1
337             iend(i,1)=jj-1
338             istart(i,2)=jj+1
339             iend(i,2)=nct
340 #endif
341           endif
342         else
343 #ifdef MPL
344           call int_partition(ind_scint,my_sc_inds,my_sc_inde,i,
345      &    iatsc_s,iatsc_e,i+1,nct,nint_gr(i),istart(i,1),iend(i,1),*12)
346 #else
347           nint_gr(i)=1
348           istart(i,1)=i+1
349           iend(i,1)=nct
350           ind_scint=int_scint+nct-i
351 #endif
352         endif
353 #ifdef MPL
354         ind_scint_old=ind_scint
355 #endif
356       enddo
357    12 continue
358 #ifndef MPL
359       iatsc_s=nnt
360       iatsc_e=nct-1
361 #endif
362 #ifdef MPL
363       if (lprint) then
364         write (iout,*) 'Processor',MyID,' Group',MyGroup
365         write (iout,*) 'iatsc_s=',iatsc_s,' iatsc_e=',iatsc_e
366       endif
367 #endif
368       if (lprint) then
369       write (iout,'(a)') 'Interaction array:'
370       do i=iatsc_s,iatsc_e
371         write (iout,'(i3,2(2x,2i3))') 
372      & i,(istart(i,iint),iend(i,iint),iint=1,nint_gr(i))
373       enddo
374       endif
375       ispp=2
376 #ifdef MPL
377 C Now partition the electrostatic-interaction array
378       npept=nct-nnt
379       nele_int_tot=(npept-ispp)*(npept-ispp+1)/2
380       call int_bounds(nele_int_tot,my_ele_inds,my_ele_inde)
381       if (lprint)
382      & write (iout,*) 'Processor',MyID,' MyRank',MyRank,
383      &  ' nele_int_tot',nele_int_tot,' my_ele_inds=',my_ele_inds,
384      &               ' my_ele_inde',my_ele_inde
385       iatel_s=0
386       iatel_e=0
387       ind_eleint=0
388       ind_eleint_old=0
389       do i=nnt,nct-3
390         ijunk=0
391         call int_partition(ind_eleint,my_ele_inds,my_ele_inde,i,
392      &    iatel_s,iatel_e,i+ispp,nct-1,ijunk,ielstart(i),ielend(i),*13)
393       enddo ! i 
394    13 continue
395 #else
396       iatel_s=nnt
397       iatel_e=nct-3
398       do i=iatel_s,iatel_e
399         ielstart(i)=i+2
400         ielend(i)=nct-1
401       enddo
402 #endif
403       if (lprint) then
404         write (iout,'(a)') 'Electrostatic interaction array:'
405         do i=iatel_s,iatel_e
406           write (iout,'(i3,2(2x,2i3))') i,ielstart(i),ielend(i)
407         enddo
408       endif ! lprint
409 c     iscp=3
410       iscp=2
411 C Partition the SC-p interaction array
412 #ifdef MPL
413       nscp_int_tot=(npept-iscp+1)*(npept-iscp+1)
414       call int_bounds(nscp_int_tot,my_scp_inds,my_scp_inde)
415       if (lprint)
416      & write (iout,*) 'Processor',MyID,' MyRank',MyRank,
417      &  ' nscp_int_tot',nscp_int_tot,' my_scp_inds=',my_scp_inds,
418      &               ' my_scp_inde',my_scp_inde
419       iatscp_s=0
420       iatscp_e=0
421       ind_scpint=0
422       ind_scpint_old=0
423       do i=nnt,nct-1
424         if (i.lt.nnt+iscp) then
425 cd        write (iout,*) 'i.le.nnt+iscp'
426           call int_partition(ind_scpint,my_scp_inds,my_scp_inde,i,
427      &      iatscp_s,iatscp_e,i+iscp,nct,nscp_gr(i),iscpstart(i,1),
428      &      iscpend(i,1),*14)
429         else if (i.gt.nct-iscp) then
430 cd        write (iout,*) 'i.gt.nct-iscp'
431           call int_partition(ind_scpint,my_scp_inds,my_scp_inde,i,
432      &      iatscp_s,iatscp_e,nnt,i-iscp,nscp_gr(i),iscpstart(i,1),
433      &      iscpend(i,1),*14)
434         else
435           call int_partition(ind_scpint,my_scp_inds,my_scp_inde,i,
436      &      iatscp_s,iatscp_e,nnt,i-iscp,nscp_gr(i),iscpstart(i,1),
437      &      iscpend(i,1),*14)
438           ii=nscp_gr(i)+1
439           call int_partition(ind_scpint,my_scp_inds,my_scp_inde,i,
440      &      iatscp_s,iatscp_e,i+iscp,nct,nscp_gr(i),iscpstart(i,ii),
441      &      iscpend(i,ii),*14)
442         endif
443       enddo ! i
444    14 continue
445 #else
446       iatscp_s=nnt
447       iatscp_e=nct-1
448       do i=nnt,nct-1
449         if (i.lt.nnt+iscp) then
450           nscp_gr(i)=1
451           iscpstart(i,1)=i+iscp
452           iscpend(i,1)=nct
453         elseif (i.gt.nct-iscp) then
454           nscp_gr(i)=1
455           iscpstart(i,1)=nnt
456           iscpend(i,1)=i-iscp
457         else
458           nscp_gr(i)=2
459           iscpstart(i,1)=nnt
460           iscpend(i,1)=i-iscp
461           iscpstart(i,2)=i+iscp
462           iscpend(i,2)=nct
463         endif 
464       enddo ! i
465 #endif
466       if (lprint) then
467         write (iout,'(a)') 'SC-p interaction array:'
468         do i=iatscp_s,iatscp_e
469           write (iout,'(i3,2(2x,2i3))') 
470      &         i,(iscpstart(i,j),iscpend(i,j),j=1,nscp_gr(i))
471         enddo
472       endif ! lprint
473 C Partition local interactions
474 #ifdef MPL
475       call int_bounds(nres-2,loc_start,loc_end)
476       loc_start=loc_start+1
477       loc_end=loc_end+1
478       call int_bounds(nres-2,ithet_start,ithet_end)
479       ithet_start=ithet_start+2
480       ithet_end=ithet_end+2
481       call int_bounds(nct-nnt-2,iphi_start,iphi_end) 
482       iphi_start=iphi_start+nnt+2
483       iphi_end=iphi_end+nnt+2
484       call int_bounds(nres-3,itau_start,itau_end)
485       itau_start=itau_start+3
486       itau_end=itau_end+3
487       if (lprint) then 
488         write (iout,*) 'Processor:',MyID,
489      & ' loc_start',loc_start,' loc_end',loc_end,
490      & ' ithet_start',ithet_start,' ithet_end',ithet_end,
491      & ' iphi_start',iphi_start,' iphi_end',iphi_end
492         write (*,*) 'Processor:',MyID,
493      & ' loc_start',loc_start,' loc_end',loc_end,
494      & ' ithet_start',ithet_start,' ithet_end',ithet_end,
495      & ' iphi_start',iphi_start,' iphi_end',iphi_end
496       endif
497       if (fgprocs.gt.1 .and. MyID.eq.BossID) then
498         write(iout,'(i10,a,i10,a,i10,a/a,i3,a)') n_sc_int_tot,' SC-SC ',
499      & nele_int_tot,' electrostatic and ',nscp_int_tot,
500      & ' SC-p interactions','were distributed among',fgprocs,
501      & ' fine-grain processors.'
502       endif
503 #else
504       loc_start=2
505       loc_end=nres-1
506       ithet_start=3 
507       ithet_end=nres
508       iphi_start=nnt+3
509       iphi_end=nct
510       itau_start=4
511       itau_end=nres
512
513 #endif
514       return
515       end 
516 c---------------------------------------------------------------------------
517       subroutine int_partition(int_index,lower_index,upper_index,atom,
518      & at_start,at_end,first_atom,last_atom,int_gr,jat_start,jat_end,*)
519       implicit real*8 (a-h,o-z)
520       include 'DIMENSIONS'
521       include 'COMMON.IOUNITS'
522       integer int_index,lower_index,upper_index,atom,at_start,at_end,
523      & first_atom,last_atom,int_gr,jat_start,jat_end
524       logical lprn
525       lprn=.false.
526       if (lprn) write (iout,*) 'int_index=',int_index
527       int_index_old=int_index
528       int_index=int_index+last_atom-first_atom+1
529       if (lprn) 
530      &   write (iout,*) 'int_index=',int_index,
531      &               ' int_index_old',int_index_old,
532      &               ' lower_index=',lower_index,
533      &               ' upper_index=',upper_index,
534      &               ' atom=',atom,' first_atom=',first_atom,
535      &               ' last_atom=',last_atom
536       if (int_index.ge.lower_index) then
537         int_gr=int_gr+1
538         if (at_start.eq.0) then
539           at_start=atom
540           jat_start=first_atom-1+lower_index-int_index_old
541         else
542           jat_start=first_atom
543         endif
544         if (lprn) write (iout,*) 'jat_start',jat_start
545         if (int_index.ge.upper_index) then
546           at_end=atom
547           jat_end=first_atom-1+upper_index-int_index_old
548           return1
549         else
550           jat_end=last_atom
551         endif
552         if (lprn) write (iout,*) 'jat_end',jat_end
553       endif
554       return
555       end
556 c------------------------------------------------------------------------------
557       subroutine hpb_partition
558       implicit real*8 (a-h,o-z)
559       include 'DIMENSIONS'
560       include 'COMMON.SBRIDGE'
561       include 'COMMON.IOUNITS'
562 #ifdef MPL
563       include 'COMMON.INFO'
564       call int_bounds(nhpb,link_start,link_end)
565 #else
566       link_start=1
567       link_end=nhpb
568 #endif
569 cd    write (iout,*) 'Processor',MyID,' MyRank',MyRank,
570 cd   &  ' nhpb',nhpb,' link_start=',link_start,
571 cd   &  ' link_end',link_end
572       return
573       end
574 c------------------------------------------------------------------------------
575       subroutine homology_partition
576       implicit real*8 (a-h,o-z)
577       include 'DIMENSIONS'
578 #ifdef MPI
579       include 'mpif.h'
580 #endif
581       include 'COMMON.SBRIDGE'
582       include 'COMMON.IOUNITS'
583 c      include 'COMMON.SETUP'
584       include 'COMMON.CONTROL'
585       include 'COMMON.CHAIN'
586       include 'COMMON.INTERACT'
587       write(iout,*)"homology_partition: lim_odl=",lim_odl,
588      &   " lim_dih",lim_dih
589 #ifdef MPL
590       call int_bounds(lim_odl,link_start_homo,link_end_homo)
591       call int_bounds(lim_dih-nnt+1,idihconstr_start_homo,
592      &  idihconstr_end_homo)
593       idihconstr_start_homo=idihconstr_start_homo+nnt-1
594       idihconstr_end_homo=idihconstr_end_homo+nnt-1
595       if (me.eq.king .or. .not. out1file)
596      &  write (iout,*) 'Processor',fg_rank,' CG group',kolor,
597      &  ' absolute rank',MyRank,
598      &  ' lim_odl',lim_odl,' link_start=',link_start_homo,
599      &  ' link_end',link_end_homo,' lim_dih',lim_dih,
600      &  ' idihconstr_start_homo',idihconstr_start_homo,
601      &  ' idihconstr_end_homo',idihconstr_end_homo
602 #else
603       link_start_homo=1
604       link_end_homo=lim_odl
605       idihconstr_start_homo=nnt
606       idihconstr_end_homo=lim_dih
607       write (iout,*)
608      &  ' lim_odl',lim_odl,' link_start=',link_start_homo,
609      &  ' link_end',link_end_homo,' lim_dih',lim_dih,
610      &  ' idihconstr_start_homo',idihconstr_start_homo,
611      &  ' idihconstr_end_homo',idihconstr_end_homo
612 #endif
613       return
614       end