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