correction after cherry-pick and merge in last commit
[unres.git] / source / cluster / wham / src / probabl.F
1       subroutine probabl(ib,nlist,ncon,*)
2 ! construct the conformational ensembles at REMD temperatures
3       implicit none
4       include "DIMENSIONS"
5       include "sizesclu.dat"
6 #ifdef MPI
7       include "mpif.h"
8       include "COMMON.MPI"
9       integer ierror,errcode,status(MPI_STATUS_SIZE) 
10 #endif
11       include "COMMON.IOUNITS"
12       include "COMMON.FREE"
13       include "COMMON.FFIELD"
14       include "COMMON.INTERACT"
15       include "COMMON.SBRIDGE"
16       include "COMMON.CHAIN"
17       include "COMMON.CLUSTER"
18       real*4 csingle(3,maxres2)
19       double precision fT(5),fTprim(5),fTbis(5),quot,quotl1,quotl,kfacl,
20      &  eprim,ebis,temper,kfac/2.4d0/,T0/300.0d0/
21       double precision etot,evdw,evdw2,ees,evdw1,ebe,etors,escloc,
22      &      ehpb,ecorr,ecorr5,ecorr6,eello_turn4,eello_turn3,
23      &      eturn6,eel_loc,edihcnstr,etors_d,estr,evdw2_14,esccor
24       integer i,ii,ik,iproc,iscor,j,k,l,ib,nlist,ncon
25       double precision qfree,sumprob,eini,efree,rmsdev
26       character*80 bxname
27       character*2 licz1
28       character*5 ctemper
29       integer ilen
30       external ilen
31       real*4 Fdimless(maxconf),Fdimless_(maxconf)
32       double precision energia(0:max_ene)
33       double precision totfree_(maxconf),entfac_(maxconf)
34       do i=1,ncon
35         list_conf(i)=i
36       enddo
37 c      do i=1,ncon
38 c        write (iout,*) i,list_conf(i)
39 c      enddo
40 c      do i=1,ncon
41 c        write(iout,*) "entrop before", entfac(i),i
42 c      enddo
43
44 #ifdef MPI
45       write (iout,*) me," indstart",indstart(me)," indend",indend(me)
46       call daread_ccoords(indstart(me),indend(me))
47 #endif
48 c      do i=1,ncon
49 c        write(iout,*) "entrop after", entfac(i),i
50 c      enddo
51
52 c      write (iout,*) "ncon",ncon
53
54       temper=1.0d0/(beta_h(ib)*1.987D-3)
55 c      write (iout,*) "ib",ib," beta_h",beta_h(ib)," temper",temper
56 c      quot=1.0d0/(T0*beta_h(ib)*1.987D-3)
57 c      quotl=1.0d0
58 c      kfacl=1.0d0
59 c      do l=1,5
60 c        quotl1=quotl
61 c        quotl=quotl*quot
62 c        kfacl=kfacl*kfac
63 c        fT(l)=kfacl/(kfacl-1.0d0+quotl)
64 c      enddo
65             if (rescale_mode.eq.1) then
66               quot=1.0d0/(T0*beta_h(ib)*1.987D-3)
67               quotl=1.0d0
68               kfacl=1.0d0
69               do l=1,5
70                 quotl1=quotl
71                 quotl=quotl*quot
72                 kfacl=kfacl*kfac
73                 fT(l)=kfacl/(kfacl-1.0d0+quotl)
74               enddo
75             else if (rescale_mode.eq.2) then
76               quot=1.0d0/(T0*beta_h(ib)*1.987D-3)
77               quotl=1.0d0
78               do l=1,5
79                 quotl=quotl*quot
80                 fT(l)=1.12692801104297249644d0/
81      &             dlog(dexp(quotl)+dexp(-quotl))
82               enddo
83 c              write (iout,*) 1.0d0/(beta_h(ib)*1.987D-3),ft
84               call flush(iout)
85             endif
86
87 #ifdef MPI
88       do i=1,scount(me)
89         ii=i+indstart(me)-1
90 #else
91       do i=1,ncon
92         ii=i
93 #endif
94 c        write (iout,*) "i",i," ii",ii
95         call flush(iout)
96         if (ib.eq.1) then
97           do j=1,nres
98             do k=1,3
99               c(k,j)=allcart(k,j,i)
100               c(k,j+nres)=allcart(k,j+nres,i)
101             enddo
102           enddo
103           do k=1,3
104             c(k,nres+1)=c(k,1)
105             c(k,nres+nres)=c(k,nres)
106           enddo
107           nss=nss_all(i)
108           do j=1,nss
109             ihpb(j)=ihpb_all(j,i)
110             jhpb(j)=jhpb_all(j,i)
111           enddo 
112           call int_from_cart1(.false.)
113           call etotal(energia(0),fT)
114           totfree(i)=energia(0)
115 #define DEBUG
116 #ifdef DEBUG
117           write (iout,*) i," energia",(energia(j),j=0,20)
118           call enerprint(energia(0),ft)
119           call flush(iout)
120 #endif
121 #undef DEBUG
122           do k=1,max_ene
123             enetb(k,i)=energia(k)
124           enddo
125         endif
126         evdw=enetb(1,i)
127 #ifdef SCP14
128         evdw2_14=enetb(17,i)
129         evdw2=enetb(2,i)+evdw2_14
130 #else
131         evdw2=enetb(2,i)
132         evdw2_14=0.0d0
133 #endif
134 #ifdef SPLITELE
135         ees=enetb(3,i)
136         evdw1=enetb(16,i)
137 #else
138         ees=enetb(3,i)
139         evdw1=0.0d0
140 #endif
141         ecorr=enetb(4,i)
142         ecorr5=enetb(5,i)
143         ecorr6=enetb(6,i)
144 cc        if (wcorr6.eq.0) ecorr6=0.0d0
145         eel_loc=enetb(7,i)
146         eello_turn3=enetb(8,i)
147         eello_turn4=enetb(9,i)
148         eturn6=enetb(10,i)
149         ebe=enetb(11,i)
150         escloc=enetb(12,i)
151         etors=enetb(13,i)
152         etors_d=enetb(14,i)
153         ehpb=enetb(15,i)
154         estr=enetb(18,i)
155         esccor=enetb(19,i)
156         edihcnstr=enetb(20,i)
157 #ifdef SPLITELE
158         etot=wsc*evdw+wscp*evdw2+ft(1)*welec*ees+wvdwpp*evdw1
159      &  +wang*ebe+ft(1)*wtor*etors+wscloc*escloc
160      &  +wstrain*ehpb+ft(3)*wcorr*ecorr+ft(4)*wcorr5*ecorr5
161      &  +ft(5)*wcorr6*ecorr6+ft(3)*wturn4*eello_turn4
162      &  +ft(2)*wturn3*eello_turn3
163      &  +ft(5)*wturn6*eturn6+ft(2)*wel_loc*eel_loc
164      &  +edihcnstr+ft(2)*wtor_d*etors_d+ft(1)*wsccor*esccor
165      &  +wbond*estr
166 #else
167         etot=wsc*evdw+wscp*evdw2+ft(1)*welec*(ees+evdw1)
168      &  +wang*ebe+ft(1)*wtor*etors+wscloc*escloc
169      &  +wstrain*ehpb+ft(3)*wcorr*ecorr+ft(4)*wcorr5*ecorr5
170      &  +ft(5)*wcorr6*ecorr6+ft(3)*wturn4*eello_turn4
171      &  +ft(2)*wturn3*eello_turn3
172      &  +ft(5)*wturn6*eturn6+ft(2)*wel_loc*eel_loc+edihcnstr
173      &  +ft(2)*wtor_d*etors_d+ft(1)*wsccor*esccor
174      &  +wbond*estr
175 #endif
176 #ifdef MPI
177         Fdimless_(i)=beta_h(ib)*etot+entfac(ii)
178         totfree_(i)=etot
179 #ifdef DEBUG
180         write (iout,*) "etrop", i,ii,ib,
181      &   1.0d0/(1.987d-3*beta_h(ib)),totfree(i),
182      &   entfac(ii),Fdimless_(i)
183 #endif
184 #else
185         Fdimless(i)=beta_h(ib)*etot+entfac(ii)
186         totfree(i)=etot
187 #ifdef DEBUG
188         write (iout,*) "etrop", i,ii,ib,
189      &   1.0d0/(1.987d-3*beta_h(ib)),totfree(i),
190      &   entfac(ii),Fdimless(i)
191 #endif
192 #endif
193       enddo   ! i
194 #ifdef MPI
195       call MPI_Gatherv(Fdimless_(1),scount(me),
196      & MPI_REAL,Fdimless(1),
197      & scount(0),idispl(0),MPI_REAL,Master,
198      & MPI_COMM_WORLD, IERROR)
199       call MPI_Gatherv(totfree_(1),scount(me),
200      & MPI_DOUBLE_PRECISION,totfree(1),
201      & scount(0),idispl(0),MPI_DOUBLE_PRECISION,Master,
202      & MPI_COMM_WORLD, IERROR)
203       call MPI_Gatherv(entfac(indstart(me)+1),scount(me),
204      & MPI_DOUBLE_PRECISION,entfac_(1),
205      & scount(0),idispl(0),MPI_DOUBLE_PRECISION,Master,
206      & MPI_COMM_WORLD, IERROR)
207       if (me.eq.Master) then
208         do i=1,ncon
209           entfac(i)=entfac_(i)
210         enddo
211 #endif
212 c#define DEBUG
213 #ifdef DEBUG
214         write (iout,*) "The FDIMLESS array before sorting"
215         do i=1,ncon
216           write (iout,*) i,fdimless(i)
217         enddo
218 #endif
219         call mysort1(ncon,Fdimless,list_conf)
220 #ifdef DEBUG
221         write (iout,*) "The FDIMLESS array after sorting"
222         do i=1,ncon
223           write (iout,*) i,list_conf(i),fdimless(i)
224         enddo
225 #endif
226 c#undef DEBUG
227         do i=1,ncon
228           totfree(i)=fdimless(i)
229         enddo
230         qfree=0.0d0
231         do i=1,ncon
232           qfree=qfree+dexp(dble(-fdimless(i)+fdimless(1)))
233         enddo
234 c        write (iout,*) "qfree",qfree
235         nlist=1
236         sumprob=0.0
237         do i=1,min0(ncon,maxstr_proc)-1 
238           sumprob=sumprob+dexp(dble(-fdimless(i)+fdimless(1)))/qfree 
239 #ifdef DEBUG
240           write (iout,*) 'i=',i,ib,beta_h(ib),
241      &     1.0d0/(1.987d-3*beta_h(ib)),list_conf(i),
242      &     totfree(list_conf(i)),
243      &     -entfac(list_conf(i)),fdimless(i),sumprob
244 #endif
245           if (sumprob.gt.prob_limit) goto 122
246 c          if (sumprob.gt.1.00d0) goto 122
247           nlist=nlist+1
248         enddo  
249   122   continue
250 #ifdef MPI
251       endif
252       call MPI_Bcast(nlist, 1, MPI_INTEGER, Master, MPI_COMM_WORLD, 
253      &   IERROR)
254       call MPI_Bcast(list_conf,nlist,MPI_INTEGER,Master,MPI_COMM_WORLD,
255      &   IERROR)
256 c      do iproc=0,nprocs
257 c        write (iout,*) "iproc",iproc," indstart",indstart(iproc),
258 c     &   " indend",indend(iproc) 
259 c      enddo
260       write (iout,*) "nlist",nlist
261 #endif
262       return
263       end
264 !--------------------------------------------------
265       subroutine mysort1(n, x, ipermut)
266       implicit none
267       integer i,j,imax,ipm,n
268       real x(n)
269       integer ipermut(n)
270       real xtemp
271       do i=1,n
272         xtemp=x(i)
273         imax=i
274         do j=i+1,n
275           if (x(j).lt.xtemp) then
276             imax=j
277             xtemp=x(j)
278           endif
279         enddo
280         x(imax)=x(i)
281         x(i)=xtemp
282         ipm=ipermut(imax)
283         ipermut(imax)=ipermut(i)
284         ipermut(i)=ipm
285       enddo
286       return
287       end