1 double precision function rdif(n,x,g,ider)
4 include "DIMENSIONS.ZSCOPT"
6 parameter (nmax=max_paropt)
11 double precision g_(max_paropt),sum_
13 include "COMMON.IOUNITS"
14 include "COMMON.PMFDERIV"
15 include "COMMON.WEIGHTS"
16 include "COMMON.TORSION"
17 double precision b1m(3,2),b2m(3,2),dm(2,2),cm(2,2),
18 & b1m2(3,2),b2m1(3,2),b11,b12,b21,b22,c11,c12,d11,d12,c1(0:3),
20 & eello_turn3_b2(3,2),eello_turn3_b1(3,2),eello_turn3_e1(2,2,2),
21 & eello_turn3_e2(2,2,2),eello_turn3_e0m1(3),eello_turn3_e0m2(3),
22 & c2(0:3),em(2,2,2),em2(2,2,2),e0m(3),e0m2(3),jac(nmax)
23 double precision v1_kcc_loc(3,3,2),v2_kcc_loc(3,3,2),
24 & etoriv1(3,3,2),etoriv2(3,3,2),d1,d2
25 double precision eneelloc3(4),eneelloc3b(3,2,2,2,4)
26 double precision theta1,theta2,gam,thetap1,thetap2,gam1,gam2,
27 & phi,cost1,cost2,sint1,sint2,
28 & cosg,sing,cos2g,sin2g,sum,d3,delta,f,fb11,fb21,fb12,fb22,fc11,
29 & fc12,fd11,fd12,etori,sint1sint2,cosphi,sinphi,sumvalc,sumvals
30 double precision conv /0.01745329251994329576d0/
31 double precision r0pp(3) /5.2739d0,5.4561d0,5.2261d0/
32 double precision epspp(3) /1.6027d0,1.4879d0,0.0779d0/
33 double precision dCACA(2) /3.784904d0,3.810180d0/
34 double precision facp1(4),facpp(4),facturn3,ddist
36 double precision x(n),g(n)
37 integer ipoint,iipoint,ip,it1,it2,itp1,itp2,i,j,k,l,ii,jj!,irt1,irt2
40 c nfun.eq.-1 : no broadcast
41 c n.eq.-1 : stop function evaluation
42 c nfun.ne.-1 .and. n.ne.-1 : master distribute variables to slaves, all do their share
44 c if (nfun.ne.-1) then
45 c call MPI_Bcast(n,1,MPI_INTEGER,Master,MPI_COMM_WORLD,ierror)
48 call MPI_Bcast(ider,1,MPI_LOGICAL,Master,MPI_COMM_WORLD,ierror)
50 c call MPI_Bcast(x(1),n,MPI_DOUBLE_PRECISION,Master,
51 c & MPI_COMM_WORLD,ierror)
53 c write (2,*) "x",(x(i),i=1,n)
56 c write (iout,*) "ider",ider," lder",lder
58 c print *,"rdif npoint",npoint
60 c Zero out gradient and hessian
73 c-- Following for ala-ala only
88 write (iout,*) "it1",it1," it2",it2," e0",e0(it1,it2)
90 c following to test with selected types
99 if (iabs(it2).le.1) then
108 facturn3=wturn_PMF*dsqrt(epspp(1))*4.2d0**3
110 facturn3=wturn_PMF*dsqrt(epspp(2))*4.2d0**3
118 facp1(4)=wello_PMF*dsqrt(epspp(1))*4.2d0**3
120 facp1(2)=wello_PMF*dsqrt(epspp(1))*4.2d0**3
122 facp1(2)=wello_PMF*dsqrt(epspp(2))*4.2d0**3
125 facp1(3)=wello_PMF*dsqrt(epspp(1))*4.2d0**3
127 facp1(3)=wello_PMF*dsqrt(epspp(2))*4.2d0**3
129 if (itp1+itp2.eq.2) then
130 facp1(1)=wello_PMF*dsqrt(epspp(1))*4.2d0**3
131 else if (itp1+itp2.eq.3) then
132 facp1(1)=wello_PMF*dsqrt(epspp(2))*4.2d0**3
134 facp1(1)=wello_PMF*dsqrt(epspp(3))*4.2d0**3
136 c print *,"itp1",itp1," itp2",itp2," facp1",facp1
139 c irt2=15+20*iabs(it2)
140 c-- The following for ALL residues
142 c irt2=17+31*iabs(it2)
143 c-- The followinig for ala-ala only
146 c print *,"it1",it1," it2",it2
147 c print *,irt1,irt2,irt1+31,irt2+31
148 c----------------------
149 c if (FOURIERSPLIT) then
152 b1m(k,i)=bnew1tor(k,i,it2)
153 b1m2(k,i)=bnew2tor(k,i,it2)
154 b2m1(k,i)=bnew1tor(k,i,it1)
155 b2m(k,i)=bnew2tor(k,i,it1)
160 cm(k,i)=ccnewtor(k,i,it2)
161 dm(k,i)=ddnewtor(k,i,it1)
167 em(k,j,i)=eenewtor(k,j,i,it1)
168 em2(k,j,i)=eenewtor(k,j,i,it2)
173 e0m(k)=e0newtor(k,it1)
174 e0m2(k)=e0newtor(k,it2)
179 c b1m(k,i)=bnew1(k,i,iabs(it2))
180 c b1m2(k,i)=bnew2(k,i,iabs(it2))
181 c b2m1(k,i)=bnew1(k,i,it1)
182 c b2m(k,i)=bnew2(k,i,it1)
187 c cm(k,i)=ccnew(k,i,iabs(it2))
188 c dm(k,i)=ddnew(k,i,it1)
194 c em(k,j,i)=eenew(k,j,i,it1)
195 c em2(k,j,i)=eenew(k,j,i,iabs(it2))
200 c e0m(k)=e0new(k,it1)
201 c e0m2(k)=e0new(k,iabs(it2))
211 c b1m2(1,1)=x(irt2+7)
212 c b1m2(1,2)=x(irt2+8)
213 c b1m2(2,1)=x(irt2+9)
214 c b1m2(2,2)=x(irt2+10)
215 c b1m2(3,1)=x(irt2+11)
216 c b1m2(3,2)=x(irt2+12)
217 c else if (it2.eq.0) then
224 c b1m2(1,1)=x(irt2+7)
226 c b1m2(2,1)=x(irt2+9)
228 c b1m2(3,1)=x(irt2+11)
232 c b1m(1,2)=-x(irt2+2)
234 c b1m(2,2)=-x(irt2+4)
236 c b1m(3,2)=-x(irt2+6)
237 c b1m2(1,1)=x(irt2+7)
238 c b1m2(1,2)=-x(irt2+8)
239 c b1m2(2,1)=x(irt2+9)
240 c b1m2(2,2)=-x(irt2+10)
241 c b1m2(3,1)=x(irt2+11)
242 c b1m2(3,2)=-x(irt2+12)
245 c b2m1(1,1)=x(irt1+1)
247 c b2m1(2,1)=x(irt1+3)
249 c b2m1(3,1)=x(irt1+5)
255 c b2m(3,1)=x(irt1+11)
258 c b2m1(1,1)=x(irt1+1)
259 c b2m1(1,2)=x(irt1+2)
260 c b2m1(2,1)=x(irt1+3)
261 c b2m1(2,2)=x(irt1+4)
262 c b2m1(3,1)=x(irt1+5)
263 c b2m1(3,2)=x(irt1+6)
267 c b2m(2,2)=x(irt1+10)
268 c b2m(3,1)=x(irt1+11)
269 c b2m(3,2)=x(irt1+12)
276 c else if (it2.eq.0) then
283 c cm(1,2)=-x(irt2+14)
285 c cm(2,2)=-x(irt2+16)
299 c em2(1,1,1)=x(irt2+21)
300 c em2(2,1,1)=x(irt2+25)
301 c em2(1,2,2)=x(irt2+24)
302 c em2(2,2,2)=x(irt2+28)
305 c em2(1,1,2)=x(irt2+22)
306 c em2(2,1,2)=x(irt2+26)
307 c em2(1,2,1)=x(irt2+23)
308 c em2(2,2,1)=x(irt2+27)
309 c em2(1,2,2)=x(irt2+24)
310 c em2(2,2,2)=x(irt2+28)
313 c else if (it2.eq.0) then
321 c em2(1,1,2)=-x(irt2+22)
322 c em2(2,1,2)=-x(irt2+26)
323 c em2(1,2,1)=-x(irt2+23)
324 c em2(2,2,1)=-x(irt2+27)
325 c em2(1,2,2)=x(irt2+24)
326 c em2(2,2,2)=x(irt2+28)
327 c e0m2(2)=-x(irt2+30)
328 c e0m2(3)=-x(irt2+31)
330 c em(1,1,1)=x(irt1+21)
331 c em(2,1,1)=x(irt1+25)
332 c em(1,2,2)=x(irt1+24)
333 c em(2,2,2)=x(irt1+28)
336 c em(1,1,2)=x(irt1+22)
337 c em(2,1,2)=x(irt1+26)
338 c em(1,2,1)=x(irt1+23)
339 c em(2,2,1)=x(irt1+27)
340 c em(1,2,2)=x(irt1+24)
341 c em(2,2,2)=x(irt1+28)
344 c else if (it1.eq.0) then
352 c Invert coefficients for L-chirality
354 write(iout,*) "it1",it1," it2",it2
355 write(iout,*) "b1i (b1m)"
357 write(iout,'(2f10.5,5x,2f10.5)') b1m(i,1),b1m(i,2)
359 write(iout,*) "b2j (b1m2)"
361 write(iout,'(2f10.5,5x,2f10.5)') b1m2(i,1),b1m2(i,2)
363 write(iout,*) "b1j (b2m1)"
365 write(iout,'(2f10.5,5x,2f10.5)') b2m1(i,1),b2m1(i,2)
367 write(iout,*) "b2j (b2m)"
369 write(iout,'(2f10.5,5x,2f10.5)') b2m(i,1),b2m(i,2)
373 write (iout,'(2f10.5)') (cm(i,j),j=1,2)
377 write (iout,'(2f10.5)') (dm(i,j),j=1,2)
379 print *,"em1",em," em2",em2," e0m",e0m," e02m",e0m2
389 v1_kcc_loc(k,l,1)=b2m(l,1)*b1m(k,1)+b2m(l,2)*b1m(k,2)
390 v2_kcc_loc(k,l,1)=b2m(l,2)*b1m(k,1)+b2m(l,1)*b1m(k,2)
395 v1_kcc_loc(k,l,2)=-(cm(k,1)*dm(l,1)-cm(k,2)*dm(l,2))
396 v2_kcc_loc(k,l,2)=-(cm(k,2)*dm(l,1)+cm(k,1)*dm(l,2))
401 if (torsion_pmf) then
403 c write (iout,*) "it1",it1," it2",it2," etori",e0(it1,it2),
404 c & " mask",mask_e0(it1,it2)
405 c write (iout,*)"mask_bnew1 1",it2,
406 c & (mask_bnew1tor(j,1,iabs(it2)),j=1,3)
407 c write (iout,*)"mask_bnew1 2",it2,
408 c & (mask_bnew1tor(j,2,iabs(it2)),j=1,3)
409 c write (iout,*)"mask_bnew2 1",it1,(mask_bnew2tor(j,1,it1),j=1,3)
410 c write (iout,*)"mask_bnew2 2",it1,(mask_bnew1tor(j,2,it1),j=1,3)
417 gam=(zz(3,i)+delta-180)*conv
427 b11=(b1m(1,1)+b1m(2,1)*cost2+b1m(3,1)*cost2*cost2)*sint2
428 b12=(b1m(1,2)+b1m(2,2)*cost2+b1m(3,2)*cost2*cost2)*sint2
429 b21=(b2m(1,1)+b2m(2,1)*cost1+b2m(3,1)*cost1*cost1)*sint1
430 b22=(b2m(1,2)+b2m(2,2)*cost1+b2m(3,2)*cost1*cost1)*sint1
431 d11=(dm(1,1)+dm(2,1)*cost1)*sint1*sint1
432 d12=(dm(1,2)+dm(2,2)*cost1)*sint1*sint1
433 c11=(cm(1,1)+cm(2,1)*cost2)*sint2*sint2
434 c12=(cm(1,2)+cm(2,2)*cost2)*sint2*sint2
436 write (iout,*) "b11",b11," b12",b12," b21",b21," b22",b22
437 write (iout,*) "d11",d11," d12",d12," c11",c11," c12",c12
438 write (iout,*) "cosg",cosg," cos2g",cos2g," sing",sing,
441 f=e0(it1,it2)+(b21*b11+b22*b12)*cosg+(b22*b11+b21*b12)*sing
442 & -(c11*d11-c12*d12)*cos2g-(c12*d11+c11*d12)*sin2g
443 fb11=b21*cosg+b22*sing
444 fb12=b22*cosg+b21*sing
445 fb21=b11*cosg+b12*sing
446 fb22=b12*cosg+b11*sing
447 fc11=-d11*cos2g-d12*sin2g
448 fc12= d12*cos2g-d11*sin2g
449 fd11=-c11*cos2g-c12*sin2g
450 fd12= c12*cos2g-c11*sin2g
452 c check with the formula from unres
466 sint1sint2=sint1sint2*sint1*sint2
471 sumvalc=sumvalc+v1_kcc_loc(l,k,j)*c1(k)*c2(l)
472 sumvals=sumvals+v2_kcc_loc(l,k,j)*c1(k)*c2(l)
473 etoriv1(l,k,j)=c1(k)*c2(l)*sint1sint2*cosphi
474 etoriv2(l,k,j)=c1(k)*c2(l)*sint1sint2*sinphi
477 c if (j.eq.1) print *,"j",j," sumvlac",sumvalc*sint1sint2,
479 c & "sumvals",sumvals*sint1sint2,b22*b11+b21*b12
480 c if (j.eq.2) print *,"j",j," sumvlac",sumvalc*sint1sint2,
481 c & -(c11*d11-c12*d12),
482 c & "sumvals",sumvals*sint1sint2,-(c12*d11+c11*d12)
483 etori=etori+(sumvalc*cosphi+sumvals*sinphi)*sint1sint2
487 c v1_kcc(k,l,1)=b1m(k,1)*b2m(l,1)+b1m(k,2)*b2m(l,2)
488 c v2_kcc(k,l,1)=b1m(k,1)*b2m(l,2)+b1m(k,2)*b2m(l,1)
493 c v1_kcc(k,l,2)=-0.25d0*(cm(k,1)*dm(l,1)-cm(k,2)*dm(l,2))
494 c v2_kcc(k,l,2)=-0.25d0*(cm(k,2)*dm(l,1)+cm(k,1)*dm(l,2))
497 c print *,"i",i," theta",zz(i,1),zz(i,2)," gamma",zz(i,3),
498 c & " f",f," etori",etori
503 write (2,'(i7,3f7.1,4f10.3)') i,(zz(j,i),j=1,3),
504 & y(1,i),f,yc(1,i),y(1,i)-yc(1,i)
506 diff(1,i)=y(1,i)-yc(1,i)
507 sum=sum+diff(1,i)*diff(1,i)*w(1,i)
509 c Compute derivatives of the energy in torsional parameters
517 if (mask_e0(it1,it2).gt.0) jac(mask_e0(it1,it2))=1.0d0
518 c Derivatives in b1 of the second residue
522 jj=iabs(mask_bnew1tor(j,1,iabs(it2)))
525 jac(jj)=jac(jj)+etoriv1(j,k,1)*b2m(k,1)
526 & +etoriv2(j,k,1)*b2m(k,2)
529 jj=iabs(mask_bnew1tor(j,2,iabs(it2)))
533 jac(jj)=jac(jj)+etoriv1(j,k,1)*b2m(k,2)
534 & +etoriv2(j,k,1)*b2m(k,1)
535 else if (it2.lt.0) then
536 jac(jj)=jac(jj)-etoriv1(j,k,1)*b2m(k,2)
537 & -etoriv2(j,k,1)*b2m(k,1)
542 c Derivatives in b2 of the first residue
545 jj=iabs(mask_bnew2tor(j,1,it1))
548 jac(jj)=jac(jj)+etoriv1(k,j,1)*b1m(k,1)
549 & +etoriv2(k,j,1)*b1m(k,2)
552 jj=iabs(mask_bnew2tor(j,2,it1))
556 jac(jj)=jac(jj)+etoriv1(k,j,1)*b1m(k,2)
557 & +etoriv2(k,j,1)*b1m(k,1)
565 jj=iabs(mask_ccnewtor(j,1,iabs(it2)))
568 c jac(jj)=jac(jj)-0.25d0*etoriv1(j,k,2)*dm(k,1)
569 c & -0.25d0*etoriv2(j,k,2)*dm(k,2)
570 jac(jj)=jac(jj)-etoriv1(j,k,2)*dm(k,1)
571 & -etoriv2(j,k,2)*dm(k,2)
574 jj=iabs(mask_ccnewtor(j,2,iabs(it2)))
578 c jac(jj+1)=jac(jj)+0.25d0*etoriv1(j,k,2)*dm(k,2)
579 c & -0.25d0*etoriv2(j,k,2)*dm(k,1)
580 jac(jj)=jac(jj)+etoriv1(j,k,2)*dm(k,2)
581 & -etoriv2(j,k,2)*dm(k,1)
582 else if (it2.lt.0) then
583 c jac(jj)=jac(jj)-0.25d0*etoriv1(j,k,2)*dm(k,2)
584 c & +0.25d0*etoriv2(j,k,2)*dm(k,1)
585 jac(jj)=jac(jj)-etoriv1(j,k,2)*dm(k,2)
586 & +etoriv2(j,k,2)*dm(k,1)
594 jj=iabs(mask_ddnewtor(j,1,it1))
597 c jac(jj)=jac(jj)-0.25d0*etoriv1(k,j,2)*cm(k,1)
598 c & -0.25d0*etoriv2(k,j,2)*cm(k,2)
599 jac(jj)=jac(jj)-etoriv1(k,j,2)*cm(k,1)
600 & -etoriv2(k,j,2)*cm(k,2)
603 jj=iabs(mask_ddnewtor(j,2,it1))
607 c jac(jj)=jac(jj)+0.25d0*etoriv1(k,j,2)*cm(k,2)
608 c & -0.25d0*etoriv2(k,j,2)*cm(k,1)
609 jac(jj)=jac(jj)+etoriv1(k,j,2)*cm(k,2)
610 & -etoriv2(k,j,2)*cm(k,1)
615 c Contributions to the gradient and the hessian
617 g(k)=g(k)+diff(1,i)*jac(k)*w(1,i)
618 c h(k,k)=h(k,k)+jac(k)**2*w(1,i)
620 c h(k,l)=h(k,l)+jac(k)*jac(l)*w(1,i)
624 print *,"i",i," jac",jac(:n)," w, diff",w(1,i),diff(1,i),
635 b1m(k,i)=bnew1(k,i,it2)
636 b1m2(k,i)=bnew2(k,i,it2)
637 b2m1(k,i)=bnew1(k,i,it1)
638 b2m(k,i)=bnew2(k,i,it1)
644 em(k,j,i)=eenew(k,j,i,it1)
645 em2(k,j,i)=eenew(k,j,i,it2)
662 gam=(zz(3,i)+delta-180)*conv
665 c eturn3 contributions
666 call eturn3PMF(theta1,theta2,gam,b2m1,b1m2,em,em2,e0m,e0m2,
667 & facturn3,d1,d2,d3,eello_turn3,eello_turn3_b2,eello_turn3_b1,
668 & eello_turn3_e1,eello_turn3_e2,eello_turn3_e0m1,eello_turn3_e0m2)
671 write (2,'(i7,3f7.1,4f10.3)') i,(zz(j,i),j=1,3),
672 & y(2,i),yc(2,i),y(2,i)-yc(2,i)
674 diff(2,i)=y(2,i)-yc(2,i)
675 sum=sum+diff(2,i)*diff(2,i)*w(2,i)
680 if (mask_turn_PMF.gt.0) jac(mask_turn_PMF)=eello_turn3/wturn_PMF
681 c Derivatives of in b1 of the first residue
684 jj=mask_bnew1(j,1,it1)
687 jac(jj)=jac(jj)+eello_turn3_b2(j,1)
691 jj=mask_bnew1(j,2,it1)
694 jac(jj)=jac(jj)+eello_turn3_b2(j,2)
698 c Derivatives of in b2 of the second residue
700 jj=mask_bnew2(j,1,iabs(it2))
702 jac(jj)=jac(jj)+eello_turn3_b1(j,1)
706 jj=mask_bnew2(j,2,iabs(it2))
709 jac(jj)=jac(jj)+eello_turn3_b1(j,2)
710 else if (it2.lt.0) then
711 jac(jj)=jac(jj)-eello_turn3_b1(j,2)
717 c Derivatives in e of the first residue
720 jj=mask_eenew(j,1,1,it1)
721 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e1(j,1,1)
722 jj=mask_eenew(j,2,2,it1)
723 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e1(j,2,2)
725 jj=mask_eenew(j,1,2,it1)
726 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e1(j,1,2)
727 jj=mask_eenew(j,2,1,it1)
728 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e1(j,2,1)
733 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e0m1(1)
736 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e0m1(2)
738 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e0m1(3)
740 c Derivatives in e of the second residue
743 jj=mask_eenew(j,1,1,iabs(it2))
744 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e2(j,1,1)
745 jj=mask_eenew(j,2,2,iabs(it2))
746 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e2(j,2,2)
748 jj=mask_eenew(j,1,2,iabs(it2))
749 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e2(j,1,2)
750 jj=mask_eenew(j,2,1,iabs(it2))
751 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e2(j,2,1)
752 else if (it2.lt.0) then
753 jj=mask_eenew(j,1,2,iabs(it2))
754 if (jj.gt.0) jac(jj)=jac(jj)-eello_turn3_e2(j,1,2)
755 jj=mask_eenew(j,2,1,iabs(it2))
756 if (jj.gt.0) jac(jj)=jac(jj)-eello_turn3_e2(j,2,1)
760 jj=mask_e0new(1,iabs(it2))
761 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e0m2(1)
763 jj=mask_e0new(2,iabs(it2))
764 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e0m2(2)
765 jj=mask_e0new(3,iabs(it2))
766 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e0m2(3)
767 else if (it2.lt.0) then
768 jj=mask_e0new(2,iabs(it2))
769 if (jj.gt.0) jac(jj)=jac(jj)-eello_turn3_e0m2(2)
770 jj=mask_e0new(3,iabs(it2))
771 if (jj.gt.0) jac(jj)=jac(jj)-eello_turn3_e0m2(3)
774 c Contributions to the gradient and the hessian
776 g(k)=g(k)+diff(2,i)*jac(k)*w(2,i)
777 c h(k,k)=h(k,k)+jac(k)**2*w(2,i)
779 c h(k,l)=h(k,l)+jac(k)*jac(l)*w(2,i)
783 print *,"i",i," jac",jac(:n)," w, diff",w(1,i),diff(1,i),
798 do ii=1,npel(it1,it2)
801 theta1=zz(1,ipoint)*conv
802 theta2=zz(2,ipoint)*conv
803 ddist=zz(3,ipoint)**3
804 thetap1=zz(4,ipoint)*conv
805 thetap2=zz(5,ipoint)*conv
806 phi=zz(6,ipoint)*conv
807 gam1=zz(7,ipoint)*conv
808 gam2=zz(8,ipoint)*conv
810 print *,"ipoint",ipoint," theta1",theta1," theta2",theta2,
811 & " dd",dd," thetap1",thetap1," thetap2", thetap2," phi",phi,
812 & " gam1",gam1," gam2",gam2
815 facpp(k)=facp1(k)/ddist
818 print *,"facp1",facp1," facpp",facpp
820 call eello3(theta1,theta2,gam1,gam2,thetap1,thetap2,
821 & phi,b2m1,b2m,b1m,b1m2,facpp,eneelloc3,eneelloc3b,lder)
823 print *,"eneelloc3",eneelloc3
826 yc(k,ipoint)=eneelloc3(k)
827 diff(k,ipoint)=y(k,ipoint)-yc(k,ipoint)
828 sum=sum+diff(k,ipoint)*diff(k,ipoint)*w(k,ipoint)
831 write (2,'(i7,7f7.1,4(2f10.3,5x))') i,(zz(j,ipoint),j=1,7),
832 & (y(k,ipoint),yc(k,ipoint),k=1,4)
836 c print *,"ipoint",ipoint," l",l
840 if (mask_ello_PMF.gt.0)
841 & jac(mask_ello_PMF)=jac(mask_ello_PMF)+eneelloc3(l)/wello_PMF
845 c jac(2)=eneelloc3(l)/wello
846 c Derivatives in b1 of the first residue
849 jj=mask_bnew1(j,1,it1)
851 if (jj.gt.0) jac(jj)=eneelloc3b(j,1,1,1,l)
852 jj=mask_bnew1(j,2,it1)
855 jac(jj)=eneelloc3b(j,2,1,1,l)
856 else if (it1.lt.0) then
857 jac(jj)=-eneelloc3b(j,2,1,1,l)
861 c print *,"bi1: jac",jac(:n)
862 c Derivatives in b2 of the first residue
865 jj=mask_bnew2(j,1,it1)
866 if (jj.gt.0) jac(jj)=eneelloc3b(j,1,2,1,l)
867 jj=mask_bnew2(j,2,it1)
870 jac(jj)=eneelloc3b(j,2,2,1,l)
871 else if (it1.lt.0) then
872 jac(jj)=-eneelloc3b(j,2,2,1,l)
876 c print *,"bi2: jac",jac(:n)
877 c Derivatives in b1 of the second residue
880 jj=mask_bnew1(j,1,iabs(it2))
881 if (jj.gt.0) jac(jj)=jac(jj)+eneelloc3b(j,1,1,2,l)
882 jj=mask_bnew1(j,2,iabs(it2))
885 jac(jj)=jac(jj)+eneelloc3b(j,2,1,2,l)
886 else if (it2.lt.0) then
887 jac(jj)=jac(jj)-eneelloc3b(j,2,1,2,l)
891 c print *,"bj1: jac",jac(:n)
892 c Derivatives in b2 of the second residue
894 jj=mask_bnew2(j,1,iabs(it2))
895 if (jj.gt.0) jac(jj)=jac(jj)+eneelloc3b(j,1,2,2,l)
896 jj=mask_bnew2(j,2,iabs(it2))
899 jac(jj)=jac(jj)+eneelloc3b(j,2,2,2,l)
900 else if (it2.lt.0) then
901 jac(jj)=jac(jj)-eneelloc3b(j,2,2,2,l)
905 c print *,"bj2: jac",jac(:n)
907 g(k)=g(k)+diff(l,ipoint)*jac(k)*w(l,ipoint)
908 c h(k,k)=h(k,k)+jac(k)**2*w(l,ipoint)
910 c h(k,ll)=h(k,ll)+jac(k)*jac(ll)*w(l,ipoint)
914 print *,"ipoint",ipoint," jac",jac(:n)," w, diff",w(l,ipoint),
915 & diff(1,ipoint)," g",g(:n)
918 c Contributions to the gradient and the hessian
929 c if (nfun.ne.-1) then
931 c write (2,*) "sum",sum
933 call MPI_Reduce(sum_,sum,1,MPI_DOUBLE_PRECISION,
934 & MPI_SUM,Master,Comm1,ierror)
936 c write (iout,*) "after reducing function lder",lder
940 call MPI_Reduce(g_(1),g(1),n,MPI_DOUBLE_PRECISION,
941 & MPI_SUM,Master,Comm1,ierror)
942 c write (iout,*) "after reducing gradient"
947 c if (nfun.ne.-1) nfun=nfun+1
950 c write (iout,*) "Exit rdif"
955 c----------------------------------------------------------------------
956 subroutine eello3(theta1,theta2,gam1,gam2,thetap1,thetap2,
957 & phi,bi1,bi2,bj1,bj2,facpp,eneelloc3,eneelloc3b,lder)
960 include "DIMENSIONS.ZSCOPT"
961 include "COMMON.IOUNITS"
962 double precision theta1,theta2,gam1,gam2,dd,thetap1,thetap2,phi
963 double precision bi1(3,2),bi2(3,2),bj1(3,2),bj2(3,2),elpp(2,2),
965 double precision eneelloc3(4),eneelloc3b(3,2,2,2,4),emat1(3,3),
966 & emat2(3,3),emati1(3,3),emati2(3,3),ematj1(3,3),ematj2(3,3)
967 double precision mui1(3),mui2(3),muj1(3),muj2(3)
968 double precision cost1,cost2,sint1,sint2,costp1,sintp1
969 double precision facpp(4)
971 common /calcdip/ cost1,cost2,sint1,sint2
972 double precision pi /3.14159265358979323844d0/
984 print '(4(a,2f10.5))',"bi1",(bi1(j,k),k=1,2)," bi2",
986 & " bj1",(bj1(j,k),k=1,2)," bj2",(bj2(j,k),k=1,2)
988 print *,"cost1",cost1," sint1",sint1," cost2",cost2," sint2",sint2
991 mui1(j+1)=(bi1(1,j)+bi1(2,j)*cost1+bi1(3,j)*cost1*cost1)*sint1
992 muj1(j+1)=(bj1(1,j)+bj1(2,j)*cost2+bj1(3,j)*cost2*cost2)*sint2
993 mui2(j+1)=(bi2(1,j)+bi2(2,j)*cost1+bi2(3,j)*cost1*cost1)*sint1
994 muj2(j+1)=(bj2(1,j)+bj2(2,j)*cost2+bj2(3,j)*cost2*cost2)*sint2
1001 print *,"Before rotation"
1002 print '(a,3f10.5)',"mui1",mui1
1003 print '(a,3f10.5)',"muj1",muj1
1004 print '(a,3f10.5)',"mui2",mui2
1005 print '(a,3f10.5)',"muj2",muj2
1007 sintp1=dsin(thetap1)
1008 costp1=dcos(thetap1)
1009 c print *,"costp1",costp1," sintp1",sintp1
1019 call coorsys(thetap2,phi,emat2)
1022 print '(3f10.5)',((emat1(j,k),k=1,3),j=1,3)
1024 print '(3f10.5)',((emat2(j,k),k=1,3),j=1,3)
1026 call rotmatsub(emat1,gam1,emati1)
1027 call rotmatsub(emat1,gam1,emati2)
1030 print '(3f10.5)',((emati1(k,j),k=1,3),j=1,3)
1032 print '(3f10.5)',((emati2(k,j),k=1,3),j=1,3)
1034 call matvecsub(emati1,mui1)
1035 call matvecsub(emati2,mui2)
1036 call rotmatsub(emat2,gam2,ematj1)
1037 call rotmatsub(emat2,gam2,ematj2)
1040 print '(3f10.5)',((ematj1(k,j),k=1,3),j=1,3)
1042 print '(3f10.5)',((ematj2(k,j),k=1,3),j=1,3)
1044 call matvecsub(ematj1,muj1)
1045 call matvecsub(ematj2,muj2)
1047 print *,"After rotation"
1048 print '(a,3f10.5)',"mui1",mui1
1049 print '(a,3f10.5)',"muj1",muj1
1050 print '(a,3f10.5)',"mui2",mui2
1051 print '(a,3f10.5)',"muj2",muj2
1053 call edipole(mui1,muj1,emati1,ematj1,facpp(1),eneelloc3(1),
1054 & eneelloc3b(1,1,1,1,1),eneelloc3b(1,1,1,2,1),-1,-1,lder)
1055 call edipole(mui2,muj1,emati2,ematj1,facpp(2),eneelloc3(2),
1056 & eneelloc3b(1,1,2,1,2),eneelloc3b(1,1,1,2,2),1,-1,lder)
1057 call edipole(mui1,muj2,emati1,ematj2,facpp(3),eneelloc3(3),
1058 & eneelloc3b(1,1,1,1,3),eneelloc3b(1,1,2,2,3),-1,1,lder)
1059 call edipole(mui2,muj2,emati2,ematj2,facpp(4),eneelloc3(4),
1060 & eneelloc3b(1,1,2,1,4),eneelloc3b(1,1,2,2,4),1,1,lder)
1063 c-----------------------------------------------------------------------------
1064 subroutine eturn3PMF(theta1,theta2,gam,b1m,b2m,em1,em2,e0m1,e0m2,
1065 & facturn,d1,d2,d3,eello_turn3,eello_turn3_b1,eello_turn3_b2,
1066 & eello_turn3_e1,eello_turn3_e2,eello_turn3_e0m1,eello_turn3_e0m2)
1068 double precision theta1,theta2,gam,cost1,cost2,sint1,sint2,cosg,
1069 & sing,sint1sq,sint2sq,aux
1070 double precision b1m(3,2),b2m(3,2),b1(2),b2(2),em1(2,2,2),
1071 & em2(2,2,2),e0m1(3),e0m2(3),e1(2,2),e2(2,2),facturn,emat1(3,3),
1072 & emat2(3,3),emat1t(3,3),mu1(2),mu2(2),e2gam(2,2),eello_turn3,
1073 & eello_turn3_b2(3,2),eello_turn3_b1(3,2),eello_turn3_e1(2,2,2),
1074 & eello_turn3_e2(2,2,2),eello_turn3_e0m1(3),eello_turn3_e0m2(3),
1075 & r1(3),r2(3),er(3),a(3,3),atemp(3,3),etemp1(2,2),etemp2(2,2),
1076 & d1,d2,d3,ddist,dd3
1077 double precision pi /3.14159265358979323844d0/
1092 mu1(1)=(b1m(1,1)+b1m(2,1)*cost1+b1m(3,1)*cost1*cost1)*sint1
1093 mu1(2)=(b1m(1,2)+b1m(2,2)*cost1+b1m(3,2)*cost1*cost1)*sint1
1094 mu2(1)=(b2m(1,1)+b2m(2,1)*cost2+b2m(3,1)*cost2*cost2)*sint2
1095 mu2(2)=(b2m(1,2)+b2m(2,2)*cost2+b2m(3,2)*cost2*cost2)*sint2
1100 e1(k,l)=(em1(1,k,l)+em1(2,k,l)*cost1)*sint1sq
1101 e2(k,l)=(em2(1,k,l)+em2(2,k,l)*cost2)*sint2sq
1104 e1(1,1)=e1(1,1)+e0m1(1)*cost1
1105 e1(1,2)=e1(1,2)+e0m1(2)+e0m1(3)*cost1
1106 e1(2,1)=e1(2,1)+e0m1(2)*cost1+e0m1(3)
1107 e1(2,2)=e1(2,2)-e0m1(1)
1108 e2(1,1)=e2(1,1)+e0m2(1)*cost2
1109 e2(1,2)=e2(1,2)+e0m2(2)+e0m2(3)*cost2
1110 e2(2,1)=e2(2,1)+e0m2(2)*cost2+e0m2(3)
1111 e2(2,2)=e2(2,2)-e0m2(1)
1113 print *,"mu1",mu1," mu2",mu2," e1",e1," e2",e2
1115 c Coordinate system of the first dipole
1127 emat1t(i,j)=emat1(j,i)
1131 print *,"theta1",theta1*180/pi," theta2",theta2*180/pi,
1134 c Calculate the reference system of the second unit
1135 call coorsys1(theta2,gam,emat2)
1136 c Dipole-dipole interaction matrix
1138 print *,"d1",d1," d2",d2," d3",d3
1139 print *,"emat1",emat1," emat2",emat2
1144 call matvecsub(emat1,r1)
1151 call matvecsub(emat2,r2)
1156 ddist=dsqrt((r2(1)-r1(1))**2+(r2(2)-r1(2))**2+(r2(3)-r1(3))**2)
1157 dd3=facturn/ddist**3
1158 er(1)=(r2(1)-r1(1))/ddist
1159 er(2)=(r2(2)-r1(2))/ddist
1160 er(3)=(r2(3)-r1(3))/ddist
1162 print *,"norm",er(1)**2+er(2)**2+er(3)**2
1163 print *,"r1",r1," r2",r2
1165 print *," dd",dd," dd3",dd3
1169 a(i,j)=-3*er(i)*er(j)*dd3
1170 c a(i,j)=-3*er(i)*er(j)
1175 c a(i,i)=1.0d0+a(i,i)
1178 print *,"a before transformation",a
1179 print *,"emat1t",emat1t
1180 print *,"emat2",emat2
1182 call matmat(emat1t,a,atemp)
1184 print *,"a after transformation",atemp
1186 call matmat(atemp,emat2,a)
1188 print *,"a after transformation",a
1190 print "(1h[,2f10.5,1h],5x,1h|,2f10.5,1h|,5x,1h|,f10.5,1h|)",
1191 & mu1(1),mu1(2),a(2,2),a(2,3),mu2(1)
1192 print "(27x,1h|,2f10.5,1h|,5x,1h|,f10.5,1h|)",
1193 & a(3,2),a(3,3),mu2(2)
1195 eello_turn3=(a(2,2)*mu1(1)*mu2(1)+a(2,3)*mu1(1)*mu2(2)
1196 & +a(3,2)*mu1(2)*mu2(1)+a(3,3)*mu1(2)*mu2(2))
1198 print *,"eello_turn3_1",eello_turn3
1200 eello_turn3_b1(1,1)=-(a(2,2)*mu2(1)+a(2,3)*mu2(2))*sint1
1201 eello_turn3_b1(2,1)=eello_turn3_b1(1,1)*cost1
1202 eello_turn3_b1(3,1)=eello_turn3_b1(2,1)*cost1
1203 eello_turn3_b1(1,2)=(a(3,2)*mu2(1)+a(3,3)*mu2(2))*sint1
1204 eello_turn3_b1(2,2)=eello_turn3_b1(1,2)*cost1
1205 eello_turn3_b1(3,2)=eello_turn3_b1(2,2)*cost1
1206 eello_turn3_b2(1,1)=-(a(2,2)*mu1(1)+a(3,2)*mu1(2))*sint2
1207 eello_turn3_b2(2,1)=eello_turn3_b2(1,1)*cost2
1208 eello_turn3_b2(3,1)=eello_turn3_b2(2,1)*cost2
1209 eello_turn3_b2(1,2)=(a(2,3)*mu1(1)+a(3,3)*mu1(2))*sint2
1210 eello_turn3_b2(2,2)=eello_turn3_b2(1,2)*cost2
1211 eello_turn3_b2(3,2)=eello_turn3_b2(2,2)*cost2
1212 e2gam(1,1)=e2(1,1)*cosg+e2(2,1)*sing
1213 e2gam(1,2)=e2(1,2)*cosg+e2(2,2)*sing
1214 e2gam(2,1)=-e2(1,1)*sing+e2(2,1)*cosg
1215 e2gam(2,2)=-e2(1,2)*sing+e2(2,2)*cosg
1230 aux=aux+a(i+1,j+1)*(e1(i,1)*e2(1,j)+e1(i,2)*e2(2,j))
1234 print *,"eello_turn3_2",aux/2
1236 eello_turn3=eello_turn3+aux/2
1238 c Derivatives in the elements of complete e1 and e2 matrices
1241 etemp1(i,j)=(a(i+1,2)*e2(j,1)+a(i+1,3)*e2(j,2))/2
1242 etemp2(i,j)=(a(2,j+1)*e1(1,i)+a(3,j+1)*e1(2,i))/2
1245 c Derivatives in the components of e1
1247 eello_turn3_e1(1,1,i)=etemp1(1,i)*sint1sq
1248 eello_turn3_e1(2,1,i)=eello_turn3_e1(1,1,i)*cost1
1249 eello_turn3_e1(1,2,i)= etemp1(2,i)*sint1sq
1250 eello_turn3_e1(2,2,i)=eello_turn3_e1(1,2,i)*cost1
1252 c Derivatives in the components of e01m
1253 eello_turn3_e0m1(1)=etemp1(1,1)*cost1-etemp1(2,2)
1254 eello_turn3_e0m1(2)=etemp1(1,2)+cost1*etemp1(2,1)
1255 eello_turn3_e0m1(3)=etemp1(1,2)*cost1+etemp1(2,1)
1256 c Derivatives in the elements of e2
1258 eello_turn3_e2(1,1,i)=-(etemp2(1,i)*cosg+etemp2(2,i)*sing)
1260 eello_turn3_e2(2,1,i)=eello_turn3_e2(1,1,i)*cost2
1261 eello_turn3_e2(1,2,i)=(-etemp2(1,i)*sing+etemp2(2,i)*cosg)
1263 eello_turn3_e2(2,2,i)=eello_turn3_e2(1,2,i)*cost2
1265 c Derivatives in the elements of e02m
1266 eello_turn3_e0m2(1)=-(etemp2(1,1)*cosg+etemp2(2,1)*sing)*cost2
1267 & +etemp2(1,2)*sing-etemp2(2,2)*cosg
1268 eello_turn3_e0m2(2)=-etemp2(1,2)*cosg-etemp2(2,2)*sing+
1269 & (-etemp2(1,1)*sing+etemp2(2,1)*cosg)*cost2
1270 eello_turn3_e0m2(3)=(-etemp2(1,2)*cosg-etemp2(2,2)*sing)*cost2
1271 & -etemp2(1,1)*sing+etemp2(2,1)*cosg
1274 c-----------------------------------------------------------------------------
1275 subroutine edipole(mui,muj,emati,ematj,facpp,ene,enebi,enebj,
1277 include "DIMENSIONS"
1278 include "DIMENSIONS.ZSCOPT"
1279 include "COMMON.IOUNITS"
1280 double precision mui(3),muj(3),emati(3,3),ematj(3,3),
1281 & facpp,ene,enebi(3,2),enebj(3,2),muimuj,cost1,sint1,cost2,sint2
1284 common /calcdip/ cost1,cost2,sint1,sint2
1285 muimuj=mui(1)*muj(1)+mui(2)*muj(2)+mui(3)*muj(3)
1286 ene=facpp*(muimuj-3*mui(3)*muj(3))
1287 if (idiri.gt.0) then
1288 enebi(1,1)=(emati(1,2)*muj(1)+emati(2,2)*muj(2)
1289 & +emati(3,2)*muj(3)-3*muj(3)*emati(3,2))*sint1*facpp
1290 enebi(2,1)=enebi(1,1)*cost1
1291 enebi(3,1)=enebi(2,1)*cost1
1292 enebi(1,2)=(emati(1,3)*muj(1)+emati(2,3)*muj(2)
1293 & +emati(3,3)*muj(3)-3*muj(3)*emati(3,3))*sint1*facpp
1294 enebi(2,2)=enebi(1,2)*cost1
1295 enebi(3,2)=enebi(2,2)*cost1
1297 enebi(1,1)=(-emati(1,2)*muj(1)-emati(2,2)*muj(2)
1298 & -emati(3,2)*muj(3)+3*muj(3)*emati(3,2))*sint1*facpp
1299 enebi(2,1)=enebi(1,1)*cost1
1300 enebi(3,1)=enebi(2,1)*cost1
1301 enebi(1,2)=(emati(1,3)*muj(1)+emati(2,3)*muj(2)
1302 & +emati(3,3)*muj(3)-3*muj(3)*emati(3,3))*sint1*facpp
1303 enebi(2,2)=enebi(1,2)*cost1
1304 enebi(3,2)=enebi(2,2)*cost1
1306 if (idirj.gt.0) then
1307 enebj(1,1)=(ematj(1,2)*mui(1)+ematj(2,2)*mui(2)
1308 & +ematj(3,2)*mui(3)-3*mui(3)*ematj(3,2))*sint2*facpp
1309 enebj(2,1)=enebj(1,1)*cost2
1310 enebj(3,1)=enebj(2,1)*cost2
1311 enebj(1,2)=(ematj(1,3)*mui(1)+ematj(2,3)*mui(2)
1312 & +ematj(3,3)*mui(3)-3*mui(3)*ematj(3,3))*sint2*facpp
1313 enebj(2,2)=enebj(1,2)*cost2
1314 enebj(3,2)=enebj(2,2)*cost2
1316 enebj(1,1)=(-ematj(1,2)*mui(1)-ematj(2,2)*mui(2)
1317 & -ematj(3,2)*mui(3)+3*mui(3)*ematj(3,2))*sint2*facpp
1318 enebj(2,1)=enebj(1,1)*cost2
1319 enebj(3,1)=enebj(2,1)*cost2
1320 enebj(1,2)=(ematj(1,3)*mui(1)+ematj(2,3)*mui(2)
1321 & +ematj(3,3)*mui(3)-3*mui(3)*ematj(3,3))*sint2*facpp
1322 enebj(2,2)=enebj(1,2)*cost2
1323 enebj(3,2)=enebj(2,2)*cost2
1327 c-----------------------------------------------------------------------------
1328 subroutine matvecsub(mat,vec)
1330 double precision mat(3,3),vec(3),auxvec(3)
1335 auxvec(i)=auxvec(i)+mat(i,j)*vec(j)
1341 c-------------------------------------------------------------------------------
1342 subroutine rotmatsub(mat,gam,matgam)
1344 double precision mat(3,3),gam,matgam(3,3),cosg,sing
1349 matgam(i,1)=mat(i,1)
1350 matgam(i,2)= mat(i,2)*cosg+mat(i,3)*sing
1351 matgam(i,3)=-mat(i,2)*sing+mat(i,3)*cosg
1355 c-----------------------------------------------------------------------------
1356 subroutine coorsys(theta,phi,emat)
1358 include "DIMENSIONS"
1359 include "DIMENSIONS.ZSCOPT"
1360 include "COMMON.IOUNITS"
1361 double precision theta,phi,emat(3,3)
1362 double precision cost,sint,cosphi,sinphi
1363 c print *,"theta",theta," phi",phi
1369 print *,"cost",cost," sint",sint," cosphi",cosphi," sinphi",sinphi
1372 emat(1,1)=sint*cosphi
1373 emat(2,1)=sint*sinphi
1378 emat(1,3)=-cost*cosphi
1379 emat(2,3)=-cost*sinphi
1383 c-----------------------------------------------------------------------------
1384 subroutine coorsys1(theta,phi,emat)
1386 include "DIMENSIONS"
1387 include "DIMENSIONS.ZSCOPT"
1388 include "COMMON.IOUNITS"
1389 double precision theta,phi,emat(3,3)
1390 double precision cost,sint,cosphi,sinphi
1391 c print *,"theta",theta," phi",phi
1397 print *,"cost",cost," sint",sint," cosphi",cosphi," sinphi",sinphi
1400 emat(1,1)=sint*cosphi
1402 emat(3,1)=-sint*sinphi
1403 emat(1,2)=-cost*cosphi
1405 emat(3,2)=cost*sinphi
1411 c-------------------------------------------------------------------------------
1412 subroutine matmat(a,b,c)
1414 double precision a(3,3),b(3,3),c(3,3)
1420 c(i,j)=c(i,j)+a(i,k)*b(k,j)