Added homology restraints modified from Pawel and Magda's code
[unres.git] / source / cluster / wham / src-restraint / 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,ehomology_constr
26       character*80 bxname
27       character*2 licz1
28       character*5 ctemper
29       integer ilen
30       external ilen
31       real*4 Fdimless(maxconf)
32       double precision energia(0:max_ene)
33       do i=1,ncon
34         list_conf(i)=i
35       enddo
36 c      do i=1,ncon
37 c        write (iout,*) i,list_conf(i)
38 c      enddo
39 c      do i=1,ncon
40 c        write(iout,*) "entrop before", entfac(i),i
41 c      enddo
42
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      do i=1,ncon
48 c        write(iout,*) "entrop after", entfac(i),i
49 c      enddo
50
51 c      write (iout,*) "ncon",ncon
52
53       temper=1.0d0/(beta_h(ib)*1.987D-3)
54 c      write (iout,*) "ib",ib," beta_h",beta_h(ib)," temper",temper
55 c      quot=1.0d0/(T0*beta_h(ib)*1.987D-3)
56 c      quotl=1.0d0
57 c      kfacl=1.0d0
58 c      do l=1,5
59 c        quotl1=quotl
60 c        quotl=quotl*quot
61 c        kfacl=kfacl*kfac
62 c        fT(l)=kfacl/(kfacl-1.0d0+quotl)
63 c      enddo
64             if (rescale_mode.eq.1) then
65               quot=1.0d0/(T0*beta_h(ib)*1.987D-3)
66               quotl=1.0d0
67               kfacl=1.0d0
68               do l=1,5
69                 quotl1=quotl
70                 quotl=quotl*quot
71                 kfacl=kfacl*kfac
72                 fT(l)=kfacl/(kfacl-1.0d0+quotl)
73               enddo
74             else if (rescale_mode.eq.2) then
75               quot=1.0d0/(T0*beta_h(ib)*1.987D-3)
76               quotl=1.0d0
77               do l=1,5
78                 quotl=quotl*quot
79                 fT(l)=1.12692801104297249644d0/
80      &             dlog(dexp(quotl)+dexp(-quotl))
81               enddo
82 c              write (iout,*) 1.0d0/(beta_h(ib)*1.987D-3),ft
83               call flush(iout)
84             endif
85
86 #ifdef MPI
87       do i=1,scount(me)
88         ii=i+indstart(me)-1
89 #else
90       do i=1,ncon
91         ii=i
92 #endif
93 c        write (iout,*) "i",i," ii",ii
94         call flush(iout)
95         if (ib.eq.1) then
96           do j=1,nres
97             do k=1,3
98               c(k,j)=allcart(k,j,i)
99               c(k,j+nres)=allcart(k,j+nres,i)
100             enddo
101           enddo
102           do k=1,3
103             c(k,nres+1)=c(k,1)
104             c(k,nres+nres)=c(k,nres)
105           enddo
106           nss=nss_all(i)
107           do j=1,nss
108             ihpb(j)=ihpb_all(j,i)
109             jhpb(j)=jhpb_all(j,i)
110           enddo 
111           call int_from_cart1(.false.)
112           call etotal(energia(0),fT)
113           totfree(i)=energia(0)
114 #define DEBUG
115 #ifdef DEBUG
116           write (iout,*) i," energia",(energia(j),j=0,20)
117           call enerprint(energia(0),ft)
118           call flush(iout)
119 #endif
120 #undef DEBUG
121           do k=1,max_ene
122             enetb(k,i)=energia(k)
123           enddo
124         endif
125         evdw=enetb(1,i)
126 #ifdef SCP14
127         evdw2_14=enetb(17,i)
128         evdw2=enetb(2,i)+evdw2_14
129 #else
130         evdw2=enetb(2,i)
131         evdw2_14=0.0d0
132 #endif
133 #ifdef SPLITELE
134         ees=enetb(3,i)
135         evdw1=enetb(16,i)
136 #else
137         ees=enetb(3,i)
138         evdw1=0.0d0
139 #endif
140         ecorr=enetb(4,i)
141         ecorr5=enetb(5,i)
142         ecorr6=enetb(6,i)
143 cc        if (wcorr6.eq.0) ecorr6=0.0d0
144         eel_loc=enetb(7,i)
145         eello_turn3=enetb(8,i)
146         eello_turn4=enetb(9,i)
147         eturn6=enetb(10,i)
148         ebe=enetb(11,i)
149         escloc=enetb(12,i)
150         etors=enetb(13,i)
151         etors_d=enetb(14,i)
152         ehpb=enetb(15,i)
153         estr=enetb(18,i)
154         esccor=enetb(19,i)
155         edihcnstr=enetb(20,i)
156         ehomology_constr=enetb(21,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+ehomology_constr
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+ehomology_constr
175 #endif
176         Fdimless(i)=beta_h(ib)*etot+entfac(ii)
177         totfree(i)=etot
178 #ifdef DEBUG
179         
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       enddo   ! i
185 #ifdef MPI
186       call MPI_Gatherv(Fdimless(1),scount(me),
187      & MPI_REAL,Fdimless(1),
188      & scount(0),idispl(0),MPI_REAL,Master,
189      & MPI_COMM_WORLD, IERROR)
190       call MPI_Gatherv(totfree(1),scount(me),
191      & MPI_DOUBLE_PRECISION,totfree(1),
192      & scount(0),idispl(0),MPI_DOUBLE_PRECISION,Master,
193      & MPI_COMM_WORLD, IERROR)
194       call MPI_Gatherv(entfac(indstart(me)+1),scount(me),
195      & MPI_DOUBLE_PRECISION,entfac(1),
196      & scount(0),idispl(0),MPI_DOUBLE_PRECISION,Master,
197      & MPI_COMM_WORLD, IERROR)
198       if (me.eq.Master) then
199 #endif
200 c#define DEBUG
201 #ifdef DEBUG
202         write (iout,*) "The FDIMLESS array before sorting"
203         do i=1,ncon
204           write (iout,*) i,fdimless(i)
205         enddo
206 #endif
207         call mysort1(ncon,Fdimless,list_conf)
208 #ifdef DEBUG
209         write (iout,*) "The FDIMLESS array after sorting"
210         do i=1,ncon
211           write (iout,*) i,list_conf(i),fdimless(i)
212         enddo
213 #endif
214 c#undef DEBUG
215         do i=1,ncon
216           totfree(i)=fdimless(i)
217         enddo
218         qfree=0.0d0
219         do i=1,ncon
220           qfree=qfree+dexp(dble(-fdimless(i)+fdimless(1)))
221         enddo
222 c        write (iout,*) "qfree",qfree
223         nlist=1
224         sumprob=0.0
225         do i=1,min0(ncon,maxstr_proc)-1 
226           sumprob=sumprob+dexp(dble(-fdimless(i)+fdimless(1)))/qfree 
227 c#define DEBUG
228 #ifdef DEBUG
229           write (iout,*) 'i=',i,ib,beta_h(ib),
230      &     1.0d0/(1.987d-3*beta_h(ib)),list_conf(i),
231      &     totfree(list_conf(i)),
232      &     -entfac(list_conf(i)),fdimless(i),sumprob
233 #endif
234 c#undef DEBUG
235           if (sumprob.gt.prob_limit) goto 122
236 c          if (sumprob.gt.1.00d0) goto 122
237           nlist=nlist+1
238         enddo  
239   122   continue
240 #ifdef MPI
241       endif
242       call MPI_Bcast(nlist, 1, MPI_INTEGER, Master, MPI_COMM_WORLD, 
243      &   IERROR)
244       call MPI_Bcast(list_conf,nlist,MPI_INTEGER,Master,MPI_COMM_WORLD,
245      &   IERROR)
246 c      do iproc=0,nprocs
247 c        write (iout,*) "iproc",iproc," indstart",indstart(iproc),
248 c     &   " indend",indend(iproc) 
249 c      enddo
250       write (iout,*) "nlist",nlist
251 #endif
252       return
253       end
254 !--------------------------------------------------
255       subroutine mysort1(n, x, ipermut)
256       implicit none
257       integer i,j,imax,ipm,n
258       real x(n)
259       integer ipermut(n)
260       real xtemp
261       do i=1,n
262         xtemp=x(i)
263         imax=i
264         do j=i+1,n
265           if (x(j).lt.xtemp) then
266             imax=j
267             xtemp=x(j)
268           endif
269         enddo
270         x(imax)=x(i)
271         x(i)=xtemp
272         ipm=ipermut(imax)
273         ipermut(imax)=ipermut(i)
274         ipermut(i)=ipm
275       enddo
276       return
277       end