1 double precision function chisquare_force(n,x,g,ider)
4 include "DIMENSIONS.ZSCOPT"
10 include "COMMON.IOUNITS"
11 include "COMMON.FMATCH"
12 include "COMMON.CLASSES"
13 include "COMMON.WEIGHTS"
14 include "COMMON.VMCPAR"
15 include "COMMON.PROTFILES"
17 include "COMMON.DERIV"
18 include "COMMON.INTERACT"
19 include "COMMON.ENERGIES"
20 include "COMMON.CHAIN"
21 include "COMMON.NAMES"
23 double precision x(n),g(n),gg(max_paropt)
26 integer i,ii,iii,ind,jj,j,k,kk,l,ll,iprot,nforce
27 double precision aux,chisquare_(maxprot),
28 & g_(max_paropt),fmean,fmean_,fsquare,fsquare_,fcov,fcov_
29 integer ipass_conf,istart_conf,iend_conf
30 double precision tcpu_ini,tcpu
31 double precision energia (0:max_ene)
32 double precision force_(3,maxres2,maxstr)
34 write (iout,*) "Enter chisquare_forces, ider",ider
36 write (iout,*) "ww",ww(:n_ene)
41 c write (iout,*) "Before broadcast ider",ider,Master
43 call MPI_Bcast(ider,1,MPI_INTEGER,Master,Comm1,ierror)
44 c write (iout,*) "After broadcast ider ",ider
54 chisquare(iprot)=0.0d0
62 if (.not.fmatch(iprot)) cycle
64 call restore_molinfo(iprot)
66 if (.not. mod_other_params) then
67 open (ientin,file=bforcefiles(iprot),status="old",
68 & form="unformatted",access="direct",recl=lenrec_forces(iprot))
70 write (iout,*) "bforcefiles",bforcefiles(iprot)
73 open (icbase,file=bprotfiles_MD(iprot),status="old",
74 & form="unformatted",access="direct",recl=lenrec_MD(iprot))
76 c write (iout,*) "chisquare: files opened"
80 do i=indstart_MD(me1,iprot),indend_MD(me1,iprot),maxstr_proc
82 iend_conf=min0(i+maxstr_proc-1,indend_MD(me1,iprot))
84 do i=1,ntot_work_MD(iprot),maxstr_proc
86 iend_conf=min0(i+maxstr_proc-1,ntot_MD(iprot))
88 c write (iout,*) "i",i," istart_conf",istart_conf," iend_conf",
93 c Read the chunk of conformations off a DA scratchfile.
95 if (.not. mod_other_params) then
97 write (iout,*) "chisquare: calling daread_forces",istart_conf,
100 call daread_forces(iprot,istart_conf,iend_conf)
101 do iii=istart_conf,iend_conf
107 aux=aux+forctb(j,l,k,ii,iprot)*ww(j)
109 force(l,k,iii,iprot)=aux
114 call daread_MDcoords(iprot,istart_conf,iend_conf)
115 do iii=istart_conf,iend_conf
117 call restore_MDcoords(iprot,ii)
118 call int_from_cart1(.false.)
120 write (iout,*) "Force: CG Coordinates conf",iii,ii
122 if (itype(k).eq.10 .or. itype(k).eq.ntyp1) then
123 write (iout,'(a4,i5,3f10.3)')
124 & restyp(itype(k)),k,(c(j,k),j=1,3)
126 write (iout,'(a4,i5,3f10.3,5x,3f10.3)')
127 & restyp(itype(k)),k,(c(j,k),j=1,3),(c(j,k+nres),j=1,3)
130 call int_from_cart1(.true.)
133 call etotal(energia(0))
137 force(l,k,iii,iprot)=-gcart(l,k)
139 forctb(j,l,k,ii,iprot)=-gcompon(j,l,k)
145 if (itype(k).eq.10 .or. itype(k).eq.ntyp1) cycle
148 force(l,kk,iii,iprot)=-gxcart(l,k)
150 forctb(j,l,kk,ii,iprot)=-gcomponx(j,l,k)
155 write (iout,*) "Checking forces from components"
156 write (iout,'(2a5,3a10)')"coor","site","full",
157 & "from compon","diff"
162 aux=aux+forctb(j,l,k,ii,iprot)*ww(j)
164 write(iout,'(2i5,3f10.5)') l,k,force(l,k,iii,iprot),aux,
165 & (force(l,k,iii,iprot)-aux)/force(l,k,iii,iprot)
171 c Compute chisquae components
173 do iii=istart_conf,iend_conf
175 write (iout,*) "Forces: conformation",iii,ii
178 if (itype(j).eq.10 .or. itype(j).eq.ntyp1) then
179 write (iout,'(a4,i5,3f10.5)')
180 & restyp(itype(j)),j,(force(k,j,iii,iprot),k=1,3)
183 write (iout,'(a4,i5,3f10.5,5x,3f10.5)')
184 & restyp(itype(j)),j,(force(k,j,iii,iprot),k=1,3),
185 & (force(k,jj,iii,iprot),k=1,3)
192 chisquare(iprot)=chisquare(iprot)+
193 & (CGforce_MD(l,k,iii,iprot)-force(l,k,iii,iprot))**2
194 fmean=fmean+force(l,k,iii,iprot)
195 fsquare=fsquare+force(l,k,iii,iprot)**2
196 fcov=fcov+force(l,k,iii,iprot)*CGforce_MD(l,k,iii,iprot)
202 if (imask(l).gt.0) then
206 gg(ll)=gg(ll)+(CGforce_MD(k,j,iii,iprot)
207 & -force(k,j,iii,iprot))*forctb(l,k,j,ii,iprot)
208 c write (iout,*) "iii",iii," ii",ii,"l",l," ll",ll," j",j
209 c write (iout,*) " residual",
210 c & CGforce_MD(k,j,iii,iprot)-force(k,j,iii,iprot),
211 c & " forctab",forctb(l,k,j,ii,iprot)
212 c write (iout,*) "gg",gg(ll)
224 if (imask(l).gt.0) then
226 g(ll)=g(ll)+wforce(iprot)*gg(ll)/
227 & (3*ntot_work_MD(iprot)*nsite(iprot))
230 c write (iout,*) "chisquare ll:",ll
232 c write (iout,*) "fmean",fmean," fsquare",fsquare
235 call MPI_Reduce(fmean_,fmean,1,MPI_DOUBLE_PRECISION,
236 & MPI_SUM,Master,Comm1,ierror)
238 call MPI_Reduce(fsquare_,fsquare,1,MPI_DOUBLE_PRECISION,
239 & MPI_SUM,Master,Comm1,ierror)
241 call MPI_Reduce(fcov_,fcov,1,MPI_DOUBLE_PRECISION,
242 & MPI_SUM,Master,Comm1,ierror)
244 nforce = 3*ntot_work_MD(iprot)*nsite(iprot)
246 c write (iout,*) "ntot_work",ntot_work_MD(iprot)," nforce",nforce
247 c write (iout,*) "fmean",fmean/nforce," fsquare",fsquare/nforce,
248 c & " variance",(fsquare-nforce*fmean)/nforce
249 rcorr(iprot)=(fcov-nforce*meanforce(iprot)*fmean)
250 & /dsqrt(nforce*(fsquare-nforce*fmean*fmean)*varforce(iprot))
254 write (iout,*) "Protein",iprot," forces"
255 write (iout,"(4x,a,t55,a)") "Backbone","Sidechain"
257 do iii=indstart_MD(me1,iprot),indend_MD(me1,iprot)
259 do iii=1,ntot_work_MD(iprot)
261 write (iout,*) "Conformation",iii
264 if (itype(i).eq.10 .or.itype(i).eq.ntyp1) then
265 write (iout,'(a4,i5,3e15.5)') restyp(itype(i)),i,
266 & (CGforce_MD(j,i,iii,iprot),j=1,3)
267 write (iout,'(9x,3e15.5)') (force(j,i,iii,iprot),j=1,3)
270 write (iout,'(a4,i5,3e15.5,5x,3e15.5)')restyp(itype(i)),i,
271 & (CGforce_MD(j,i,iii,iprot),j=1,3),
272 & (CGforce_MD(j,ii,iii,iprot),j=1,3)
273 write (iout,'(9x,3e15.5,5x,3e15.5)')
274 & (force(j,i,iii,iprot),j=1,3),
275 & (force(j,ii,iii,iprot),j=1,3)
281 write(iout,*)"chisquare",chisquare(:nprot)
283 c Compute total chisquare and gradient
286 call MPI_Reduce(chisquare_,chisquare,nprot,MPI_DOUBLE_PRECISION,
287 & MPI_SUM,Master,Comm1,ierror)
290 c write (iout,*) "g before reduce",g(:n)
291 call MPI_Reduce(gg,g,n,MPI_DOUBLE_PRECISION,
292 & MPI_SUM,Master,Comm1,ierror)
293 c write (iout,*) "g after reduce",g(:n)
296 c write (iout,*) "chisquare",chisquare(:nprot)
299 if (.not.fmatch(iprot)) cycle
300 chisquare(iprot)=chisquare(iprot)/
301 & (3*ntot_work_MD(iprot)*nsite(iprot))
302 aux=aux+0.5d0*wforce(iprot)*chisquare(iprot)
305 write (iout,*) "Leave chisquare_forces"
312 write (iout,*) "fforce gather me1",me1," indstart",
313 & indstart_MD(me1,iprot)," indend",indend_MD(me1,iprot)
315 do i=indstart_MD(me1,iprot),indend_MD(me1,iprot)
318 force_(k,j,i-indstart_MD(me1,iprot)+1)=force(k,j,i,iprot)
319 force(k,j,i,iprot)=0.0d0
324 write (iout,*) "force_ scount_MD",scount_MD(me1,iprot)
325 do i=1,scount_MD(me1,iprot)
327 write (iout,'(2i5,3f10.5)') i,j,(force_(k,j,i),k=1,3)
330 write (iout,*) "scount_MD",scount_MD(:nprocs-1,iprot)
331 write (iout,*) "idispl_MD",idispl_MD(:nprocs-1,iprot)
333 call MPI_Gatherv(force_(1,1,1),scount_MD(me1,iprot),MPI_FORCE,
334 & force(1,1,1,iprot),scount_MD(0,iprot),idispl_MD(0,iprot),
335 & MPI_FORCE,Master,Comm1, IERROR)
337 write (iout,*) "Gather ierror",ierror
338 write (iout,*) "force after gather"
339 do i=1,ntot_MD(iprot)
341 write (iout,'(2i5,3f10.5)') i,j,(force(k,j,i,iprot),k=1,3)