update new files
[unres.git] / source / cluster / wham / src-M-homology / 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.CONTROL"
12       include "COMMON.IOUNITS"
13       include "COMMON.FREE"
14       include "COMMON.FFIELD"
15       include "COMMON.INTERACT"
16       include "COMMON.SBRIDGE"
17       include "COMMON.CHAIN"
18       include "COMMON.CLUSTER"
19       real*4 csingle(3,maxres2)
20       double precision fT(6),fTprim(6),fTbis(6),quot,quotl1,quotl,kfacl,
21      &  eprim,ebis,temper,kfac/2.4d0/,T0/300.0d0/
22       double precision etot,evdw,evdw2,ees,evdw1,ebe,etors,escloc,
23      &      ehpb,ecorr,ecorr5,ecorr6,eello_turn4,eello_turn3,
24      &      eturn6,eel_loc,edihcnstr,etors_d,estr,evdw2_14,esccor,
25      &      evdw_t,esaxs
26       integer i,ii,ik,iproc,iscor,j,k,l,ib,nlist,ncon
27       double precision qfree,sumprob,eini,efree,rmsdev
28       character*80 bxname
29       character*2 licz1
30       character*5 ctemper
31       integer ilen,ijk
32       external ilen
33       character*80 structure/'Structure'/
34       real*4 Fdimless(maxconf), Fdimless_buf(maxconf)
35       double precision energia(0:max_ene), totfree_buf(0:maxconf),
36      &  entfac_buf(maxconf)
37       do i=1,ncon
38         list_conf(i)=i
39       enddo
40 c      do i=1,ncon
41 c        write (iout,*) i,list_conf(i)
42 c      enddo
43 #ifdef MPI
44       write (iout,*) me," indstart",indstart(me)," indend",indend(me)
45       call daread_ccoords(indstart(me),indend(me))
46 #endif
47 C      write (iout,*) "ncon",ncon
48 C      call flush(iout)
49       temper=1.0d0/(beta_h(ib)*1.987D-3)
50 c      write (iout,*) "ib",ib," beta_h",beta_h(ib)," temper",temper
51 c      quot=1.0d0/(T0*beta_h(ib)*1.987D-3)
52 c      quotl=1.0d0
53 c      kfacl=1.0d0
54 c      do l=1,5
55 c        quotl1=quotl
56 c        quotl=quotl*quot
57 c        kfacl=kfacl*kfac
58 c        fT(l)=kfacl/(kfacl-1.0d0+quotl)
59 c      enddo
60 C#define DEBUG
61             if (rescale_mode.eq.1) then
62               quot=1.0d0/(T0*beta_h(ib)*1.987D-3)
63               quotl=1.0d0
64               kfacl=1.0d0
65               do l=1,5
66                 quotl1=quotl
67                 quotl=quotl*quot
68                 kfacl=kfacl*kfac
69                 fT(l)=kfacl/(kfacl-1.0d0+quotl)
70               enddo
71 #if defined(FUNCTH)
72               ft(6)=(320.0d0+80.0d0*dtanh((betaT-320.0d0)/80.0d0))/
73      &                  320.0d0
74               ftprim(6)=1.0d0/(320.0d0*dcosh((betaT-320.0d0)/80.0d0)**2)
75              ftbis(6)=-2.0d0*dtanh((betaT-320.0d0)/80.0d0)
76      &              /(320.0d0*80.0d0*dcosh((betaT-320.0d0)/80.0d0)**3)
77 #elif defined(FUNCT)
78               fT(6)=betaT/T0
79               ftprim(6)=1.0d0/T0
80               ftbis(6)=0.0d0
81 #else
82               fT(6)=1.0d0
83               ftprim(6)=0.0d0
84               ftbis(6)=0.0d0
85 #endif
86
87             else if (rescale_mode.eq.2) then
88               quot=1.0d0/(T0*beta_h(ib)*1.987D-3)
89               quotl=1.0d0
90               do l=1,5
91                 quotl=quotl*quot
92                 fT(l)=1.12692801104297249644d0/
93      &             dlog(dexp(quotl)+dexp(-quotl))
94               enddo
95 c              write (iout,*) 1.0d0/(beta_h(ib)*1.987D-3),ft
96 c              call flush(iout)
97 #if defined(FUNCTH)
98               ft(6)=(320.0d0+80.0d0*dtanh((betaT-320.0d0)/80.0d0))/
99      &                  320.0d0
100               ftprim(6)=1.0d0/(320.0d0*dcosh((betaT-320.0d0)/80.0d0)**2)
101              ftbis(6)=-2.0d0*dtanh((betaT-320.0d0)/80.0d0)
102      &              /(320.0d0*80.0d0*dcosh((betaT-320.0d0)/80.0d0)**3)
103 #elif defined(FUNCT)
104               fT(6)=betaT/T0
105               ftprim(6)=1.0d0/T0
106               ftbis(6)=0.0d0
107 #else
108               fT(6)=1.0d0
109               ftprim(6)=0.0d0
110               ftbis(6)=0.0d0
111 #endif
112             endif
113
114 #ifdef MPI
115       do i=1,scount(me)
116         ii=i+indstart(me)-1
117 #else
118       do i=1,ncon
119         ii=i
120 #endif
121 C        write (iout,*) "i",i," ii",ii,"ib",ib,scount(me)
122 c        call flush(iout)
123         if (ib.eq.1) then
124           do j=1,nres
125             do k=1,3
126               c(k,j)=allcart(k,j,i)
127               c(k,j+nres)=allcart(k,j+nres,i)
128 C              write(iout,*) "coord",i,j,k,allcart(k,j,i),c(k,j),
129 C     &        c(k,j+nres),allcart(k,j+nres,i)
130             enddo
131           enddo
132 C          write(iout,*) "out of j loop"
133 C          call flush(iout)
134           do k=1,3
135             c(k,nres+1)=c(k,1)
136             c(k,nres+nres)=c(k,nres)
137           enddo
138 C          write(iout,*) "after nres+nres",nss_all(i)
139 C          call flush(iout)
140           nss=nss_all(i)
141           do j=1,nss
142             ihpb(j)=ihpb_all(j,i)
143             jhpb(j)=jhpb_all(j,i)
144           enddo 
145           call int_from_cart1(.false.)
146 C          write(iout,*) "before etotal"
147 C          call flush(iout)
148           call etotal(energia(0),fT)
149           if (refstr) then
150             write (structure(9:),'(bz,i6.6)') i
151             call TMscore_sub(rmsdev,gdt_ts_tb(i),
152      &      gdt_ha_tb(i),tmscore_tb(i),Structure,.false.)
153 #ifdef DEBUG
154             write (iout,*) rmsdev,gdt_ts_tb(i),gdt_ha_tb(i),
155      &        tmscore_tb(i)
156 #endif
157           endif
158           totfree(i)=energia(0)         
159           totfree_buf(i)=totfree(i)
160 c          write (iout,'(8f10.5)') ((c(l,k),l=1,3),k=1,nres)
161 c          write (iout,'(8f10.5)') ((c(l,k+nres),l=1,3),k=nnt,nct)
162 c          call enerprint(energia(0),fT)
163 c          call pdbout(totfree(i),16,i)
164 #ifdef DEBUG
165           write (iout,*) i," energia",(energia(j),j=0,max_ene)
166           write (iout,*) "etot", etot
167           write (iout,*) "ft(6)", ft(6)
168 #endif
169           do k=1,max_ene
170             enetb(k,i)=energia(k)
171           enddo
172         endif
173         evdw=enetb(1,i)
174 c        write (iout,*) evdw
175        etot=energia(0)
176 #ifdef SCP14
177         evdw2_14=enetb(17,i)
178         evdw2=enetb(2,i)+evdw2_14
179 #else
180         evdw2=enetb(2,i)
181         evdw2_14=0.0d0
182 #endif
183 #ifdef SPLITELE
184         ees=enetb(3,i)
185         evdw1=enetb(16,i)
186 #else
187         ees=enetb(3,i)
188         evdw1=0.0d0
189 #endif
190         ecorr=enetb(4,i)
191         ecorr5=enetb(5,i)
192         ecorr6=enetb(6,i)
193         eel_loc=enetb(7,i)
194         eello_turn3=enetb(8,i)
195         eello_turn4=enetb(9,i)
196         eturn6=enetb(10,i)
197         ebe=enetb(11,i)
198         escloc=enetb(12,i)
199         etors=enetb(13,i)
200         etors_d=enetb(14,i)
201         ehpb=enetb(15,i)
202         estr=enetb(18,i)
203         esccor=enetb(19,i)
204         edihcnstr=enetb(20,i)
205         esaxs=enetb(25,i)
206 c#ifdef SPLITELE
207 c        etot=wsc*(evdw+ft(6)*evdw_t)+wscp*evdw2+
208 c     &ft(1)*welec*ees+wvdwpp*evdw1
209 c     &  +wang*ebe+ft(1)*wtor*etors+wscloc*escloc
210 c     &  +wstrain*ehpb+nss*ebr+ft(3)*wcorr*ecorr+ft(4)*wcorr5*ecorr5
211 c     &  +ft(5)*wcorr6*ecorr6+ft(3)*wturn4*eello_turn4
212 c     &  +ft(2)*wturn3*eello_turn3
213 c     &  +ft(5)*wturn6*eturn6+ft(2)*wel_loc*eel_loc
214 c     &  +edihcnstr+ft(2)*wtor_d*etors_d+ft(1)*wsccor*esccor
215 c     &  +wbond*estr
216 c#else
217 c        etot=wsc*(evdw+ft(6)*evdw_t)+wscp*evdw2+ft(1)*welec*(ees+evdw1)
218 c     &  +wang*ebe+ft(1)*wtor*etors+wscloc*escloc
219 c     &  +wstrain*ehpb+nss*ebr+ft(3)*wcorr*ecorr+ft(4)*wcorr5*ecorr5
220 c     &  +ft(5)*wcorr6*ecorr6+ft(3)*wturn4*eello_turn4
221 c     &  +ft(2)*wturn3*eello_turn3
222 c     &  +ft(5)*wturn6*eturn6+ft(2)*wel_loc*eel_loc+edihcnstr
223 c     &  +ft(2)*wtor_d*etors_d+ft(1)*wsccor*esccor
224 c     &  +wbond*estr
225 c#endif
226 #ifdef DEBUG
227         write (iout,*) "etot2", etot
228         write (iout,*) "evdw", wsc, evdw,evdw_t
229         write (iout,*) "evdw2", wscp, evdw2
230         write (iout,*) "welec", ft(1),welec,ees
231         write (iout,*) "evdw1", wvdwpp,evdw1
232         write (iout,*) "ebe", ebe,wang
233 #endif        
234         Fdimless(i)=beta_h(ib)*etot+entfac(ii)
235         Fdimless_buf(i)=Fdimless(i)
236         totfree(i)=etot
237         totfree_buf(i)=totfree(i)
238 #ifdef DEBUG
239         write (iout,*) "fdim calc", i,ii,ib,
240      &   1.0d0/(1.987d-3*beta_h(ib)),totfree(i),
241      &   entfac(ii),Fdimless(i)
242 #endif
243       enddo   ! i
244
245       do ijk=1,maxconf
246       entfac_buf(ijk)=entfac(ijk)
247       Fdimless_buf(ijk)=Fdimless(ijk)
248       enddo
249       do ijk=0,maxconf
250       totfree_buf(ijk)=totfree(ijk)
251       enddo
252
253
254 c      scount_buf=scount(me)
255 c      scount_buf2=scount(0)
256
257 c      entfac_buf(indstart(me)+1)=entfac(indstart(me)+1)
258
259 #ifdef MPI
260 c      WRITE (iout,*) "Wchodze do call MPI_Gatherv1 (Propabl)"
261       call MPI_Gatherv(Fdimless_buf(1),scount(me),
262      & MPI_REAL,Fdimless(1),
263      & scount(0),idispl(0),MPI_REAL,Master,
264      & MPI_COMM_WORLD, IERROR)
265 c      WRITE (iout,*) "Wchodze do call MPI_Gatherv2 (Propabl)"
266       call MPI_Gatherv(totfree_buf(1),scount(me),
267      & MPI_DOUBLE_PRECISION,totfree(1),
268      & scount(0),idispl(0),MPI_DOUBLE_PRECISION,Master,
269      & MPI_COMM_WORLD, IERROR)
270 c      WRITE (iout,*) "Wchodze do call MPI_Gatherv3 (Propabl)"
271       call MPI_Gatherv(entfac_buf(indstart(me)+1),scount(me),
272      & MPI_DOUBLE_PRECISION,entfac(1),
273      & scount(0),idispl(0),MPI_DOUBLE_PRECISION,Master,
274      & MPI_COMM_WORLD, IERROR)
275 c      WRITE (iout,*) "Wychodze z call MPI_Gatherv (Propabl)"
276       if (me.eq.Master) then
277 c      WRITE (iout,*) "me.eq.Master"
278 #endif
279 #ifdef DEBUG
280         write (iout,*) "The FDIMLESS array before sorting"
281         do i=1,ncon
282 c          write (iout,*) i,fdimless(i)
283         enddo
284 #endif
285 c      WRITE (iout,*) "Wchodze do call mysort1"
286         call mysort1(ncon,Fdimless,list_conf)
287 c      WRITE (iout,*) "Wychodze z call mysort1"
288 #ifdef DEBUG
289         write (iout,*) "The FDIMLESS array after sorting"
290         do i=1,ncon
291           write (iout,*) i,list_conf(i),fdimless(i)
292         enddo
293 #endif
294 c      WRITE (iout,*) "Wchodze do petli i=1,ncon totfree(i)=fdimless(i)"
295         do i=1,ncon
296           totfree(i)=fdimless(i)
297         enddo
298         qfree=0.0d0
299         do i=1,ncon
300           qfree=qfree+exp(-fdimless(i)+fdimless(1))
301 c          write (iout,*) "fdimless", fdimless(i)
302         enddo
303 c        write (iout,*) "qfree",qfree
304         nlist=1
305         sumprob=0.0
306         write (iout,*) "ncon", ncon,maxstr_proc
307         do i=1,min0(ncon,maxstr_proc)-1 
308           sumprob=sumprob+exp(-fdimless(i)+fdimless(1))/qfree 
309 #ifdef DEBUG
310           write (iout,*) "tu szukaj ponizej 7"
311           write (iout,*) i,ib,beta_h(ib),
312      &     1.0d0/(1.987d-3*beta_h(ib)),list_conf(i),
313      &     totfree(list_conf(i)),
314      &     -entfac(list_conf(i)),fdimless(i),sumprob
315 #endif
316           if (sumprob.gt.prob_limit) goto 122
317 c          if (sumprob.gt.1.00d0) goto 122
318           nlist=nlist+1
319         enddo  
320   122   continue
321 #ifdef MPI
322       endif
323       call MPI_Bcast(nlist, 1, MPI_INTEGER, Master, MPI_COMM_WORLD, 
324      &   IERROR)
325       call MPI_Bcast(list_conf,nlist,MPI_INTEGER,Master,MPI_COMM_WORLD,
326      &   IERROR)
327 c      do iproc=0,nprocs
328 c        write (iout,*) "iproc",iproc," indstart",indstart(iproc),
329 c     &   " indend",indend(iproc) 
330 c      enddo
331       write (iout,*) "nlist",nlist
332 #endif
333       write (iout,'(80(1h-)/i4,a,f6.3,a,f5.1/80(1h-))') 
334      & nlist," conformations found within",prob_limit,
335      & " probablity cut_off at temperature ",1.0d0/(1.987d-3*beta_h(ib))
336       return
337       end
338 !--------------------------------------------------
339       subroutine mysort1(n, x, ipermut)
340       implicit none
341       integer i,j,imax,ipm,n
342       real x(n)
343       integer ipermut(n)
344       real xtemp
345       do i=1,n
346         xtemp=x(i)
347         imax=i
348         do j=i+1,n
349           if (x(j).lt.xtemp) then
350             imax=j
351             xtemp=x(j)
352           endif
353         enddo
354         x(imax)=x(i)
355         x(i)=xtemp
356         ipm=ipermut(imax)
357         ipermut(imax)=ipermut(i)
358         ipermut(i)=ipm
359       enddo
360       return
361       end