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"
18 double precision b1m(3,2),b2m(3,2),dm(2,2),cm(2,2),
19 & b1m2(3,2),b2m1(3,2),b11,b12,b21,b22,c11,c12,d11,d12,c1(0:3),
21 & eello_turn3_b2(3,2),eello_turn3_b1(3,2),eello_turn3_e1(2,2,2),
22 & eello_turn3_e2(2,2,2),eello_turn3_e0m1(3),eello_turn3_e0m2(3),
23 & c2(0:3),em(2,2,2),em2(2,2,2),e0m(3),e0m2(3),jac(nmax)
24 double precision v1_kcc_loc(3,3,2),v2_kcc_loc(3,3,2),
25 & etoriv1(3,3,2),etoriv2(3,3,2),d1,d2
26 double precision eneelloc3(4),eneelloc3b(3,2,2,2,4)
27 double precision theta1,theta2,gam,thetap1,thetap2,gam1,gam2,
28 & phi,cost1,cost2,sint1,sint2,ebendi,
29 & cosg,sing,cos2g,sin2g,sum,d3,delta,f,fb11,fb21,fb12,fb22,fc11,
30 & fc12,fd11,fd12,etori,sint1sint2,cosphi,sinphi,sumvalc,sumvals
31 double precision conv /0.01745329251994329576d0/
32 double precision r0pp(3) /5.2739d0,5.4561d0,5.2261d0/
33 double precision epspp(3) /1.6027d0,1.4879d0,0.0779d0/
34 double precision dCACA(2) /3.784904d0,3.810180d0/
35 double precision facp1(4),facpp(4),facturn3,ddist
36 double precision thetaj,costj,ebendj,chi2thet
37 double precision tschebyshev
39 double precision x(n),g(n)
40 integer ipoint,iipoint,ip,it1,it2,itp1,itp2,i,j,k,l,ii,jj!,irt1,irt2
43 c nfun.eq.-1 : no broadcast
44 c n.eq.-1 : stop function evaluation
45 c nfun.ne.-1 .and. n.ne.-1 : master distribute variables to slaves, all do their share
47 c if (nfun.ne.-1) then
48 c call MPI_Bcast(n,1,MPI_INTEGER,Master,MPI_COMM_WORLD,ierror)
51 call MPI_Bcast(ider,1,MPI_LOGICAL,Master,MPI_COMM_WORLD,ierror)
53 c call MPI_Bcast(x(1),n,MPI_DOUBLE_PRECISION,Master,
54 c & MPI_COMM_WORLD,ierror)
56 c write (2,*) "x",(x(i),i=1,n)
59 c write (iout,*) "ider",ider," lder",lder
61 c print *,"rdif npoint",npoint
63 c Zero out gradient and hessian
76 c-- Following for ala-ala only
91 write (iout,*) "it1",it1," it2",it2," e0",e0(it1,it2)
93 c following to test with selected types
102 if (iabs(it2).le.1) then
111 facturn3=wturn_PMF*dsqrt(epspp(1))*4.2d0**3
113 facturn3=wturn_PMF*dsqrt(epspp(2))*4.2d0**3
121 facp1(4)=wello_PMF*dsqrt(epspp(1))*4.2d0**3
123 facp1(2)=wello_PMF*dsqrt(epspp(1))*4.2d0**3
125 facp1(2)=wello_PMF*dsqrt(epspp(2))*4.2d0**3
128 facp1(3)=wello_PMF*dsqrt(epspp(1))*4.2d0**3
130 facp1(3)=wello_PMF*dsqrt(epspp(2))*4.2d0**3
132 if (itp1+itp2.eq.2) then
133 facp1(1)=wello_PMF*dsqrt(epspp(1))*4.2d0**3
134 else if (itp1+itp2.eq.3) then
135 facp1(1)=wello_PMF*dsqrt(epspp(2))*4.2d0**3
137 facp1(1)=wello_PMF*dsqrt(epspp(3))*4.2d0**3
139 c print *,"itp1",itp1," itp2",itp2," facp1",facp1
142 c irt2=15+20*iabs(it2)
143 c-- The following for ALL residues
145 c irt2=17+31*iabs(it2)
146 c-- The followinig for ala-ala only
149 c print *,"it1",it1," it2",it2
150 c print *,irt1,irt2,irt1+31,irt2+31
151 c----------------------
152 c if (FOURIERSPLIT) then
155 b1m(k,i)=bnew1tor(k,i,it2)
156 b1m2(k,i)=bnew2tor(k,i,it2)
157 b2m1(k,i)=bnew1tor(k,i,it1)
158 b2m(k,i)=bnew2tor(k,i,it1)
163 cm(k,i)=ccnewtor(k,i,it2)
164 dm(k,i)=ddnewtor(k,i,it1)
170 em(k,j,i)=eenewtor(k,j,i,it1)
171 em2(k,j,i)=eenewtor(k,j,i,it2)
176 e0m(k)=e0newtor(k,it1)
177 e0m2(k)=e0newtor(k,it2)
182 c b1m(k,i)=bnew1(k,i,iabs(it2))
183 c b1m2(k,i)=bnew2(k,i,iabs(it2))
184 c b2m1(k,i)=bnew1(k,i,it1)
185 c b2m(k,i)=bnew2(k,i,it1)
190 c cm(k,i)=ccnew(k,i,iabs(it2))
191 c dm(k,i)=ddnew(k,i,it1)
197 c em(k,j,i)=eenew(k,j,i,it1)
198 c em2(k,j,i)=eenew(k,j,i,iabs(it2))
203 c e0m(k)=e0new(k,it1)
204 c e0m2(k)=e0new(k,iabs(it2))
214 c b1m2(1,1)=x(irt2+7)
215 c b1m2(1,2)=x(irt2+8)
216 c b1m2(2,1)=x(irt2+9)
217 c b1m2(2,2)=x(irt2+10)
218 c b1m2(3,1)=x(irt2+11)
219 c b1m2(3,2)=x(irt2+12)
220 c else if (it2.eq.0) then
227 c b1m2(1,1)=x(irt2+7)
229 c b1m2(2,1)=x(irt2+9)
231 c b1m2(3,1)=x(irt2+11)
235 c b1m(1,2)=-x(irt2+2)
237 c b1m(2,2)=-x(irt2+4)
239 c b1m(3,2)=-x(irt2+6)
240 c b1m2(1,1)=x(irt2+7)
241 c b1m2(1,2)=-x(irt2+8)
242 c b1m2(2,1)=x(irt2+9)
243 c b1m2(2,2)=-x(irt2+10)
244 c b1m2(3,1)=x(irt2+11)
245 c b1m2(3,2)=-x(irt2+12)
248 c b2m1(1,1)=x(irt1+1)
250 c b2m1(2,1)=x(irt1+3)
252 c b2m1(3,1)=x(irt1+5)
258 c b2m(3,1)=x(irt1+11)
261 c b2m1(1,1)=x(irt1+1)
262 c b2m1(1,2)=x(irt1+2)
263 c b2m1(2,1)=x(irt1+3)
264 c b2m1(2,2)=x(irt1+4)
265 c b2m1(3,1)=x(irt1+5)
266 c b2m1(3,2)=x(irt1+6)
270 c b2m(2,2)=x(irt1+10)
271 c b2m(3,1)=x(irt1+11)
272 c b2m(3,2)=x(irt1+12)
279 c else if (it2.eq.0) then
286 c cm(1,2)=-x(irt2+14)
288 c cm(2,2)=-x(irt2+16)
302 c em2(1,1,1)=x(irt2+21)
303 c em2(2,1,1)=x(irt2+25)
304 c em2(1,2,2)=x(irt2+24)
305 c em2(2,2,2)=x(irt2+28)
308 c em2(1,1,2)=x(irt2+22)
309 c em2(2,1,2)=x(irt2+26)
310 c em2(1,2,1)=x(irt2+23)
311 c em2(2,2,1)=x(irt2+27)
312 c em2(1,2,2)=x(irt2+24)
313 c em2(2,2,2)=x(irt2+28)
316 c else if (it2.eq.0) then
324 c em2(1,1,2)=-x(irt2+22)
325 c em2(2,1,2)=-x(irt2+26)
326 c em2(1,2,1)=-x(irt2+23)
327 c em2(2,2,1)=-x(irt2+27)
328 c em2(1,2,2)=x(irt2+24)
329 c em2(2,2,2)=x(irt2+28)
330 c e0m2(2)=-x(irt2+30)
331 c e0m2(3)=-x(irt2+31)
333 c em(1,1,1)=x(irt1+21)
334 c em(2,1,1)=x(irt1+25)
335 c em(1,2,2)=x(irt1+24)
336 c em(2,2,2)=x(irt1+28)
339 c em(1,1,2)=x(irt1+22)
340 c em(2,1,2)=x(irt1+26)
341 c em(1,2,1)=x(irt1+23)
342 c em(2,2,1)=x(irt1+27)
343 c em(1,2,2)=x(irt1+24)
344 c em(2,2,2)=x(irt1+28)
347 c else if (it1.eq.0) then
355 c Invert coefficients for L-chirality
357 write(iout,*) "it1",it1," it2",it2
358 write(iout,*) "b1i (b1m)"
360 write(iout,'(2f10.5,5x,2f10.5)') b1m(i,1),b1m(i,2)
362 write(iout,*) "b2j (b1m2)"
364 write(iout,'(2f10.5,5x,2f10.5)') b1m2(i,1),b1m2(i,2)
366 write(iout,*) "b1j (b2m1)"
368 write(iout,'(2f10.5,5x,2f10.5)') b2m1(i,1),b2m1(i,2)
370 write(iout,*) "b2j (b2m)"
372 write(iout,'(2f10.5,5x,2f10.5)') b2m(i,1),b2m(i,2)
376 write (iout,'(2f10.5)') (cm(i,j),j=1,2)
380 write (iout,'(2f10.5)') (dm(i,j),j=1,2)
382 print *,"em1",em," em2",em2," e0m",e0m," e02m",e0m2
392 v1_kcc_loc(k,l,1)=b2m(l,1)*b1m(k,1)+b2m(l,2)*b1m(k,2)
393 v2_kcc_loc(k,l,1)=b2m(l,2)*b1m(k,1)+b2m(l,1)*b1m(k,2)
398 v1_kcc_loc(k,l,2)=-(cm(k,1)*dm(l,1)-cm(k,2)*dm(l,2))
399 v2_kcc_loc(k,l,2)=-(cm(k,2)*dm(l,1)+cm(k,1)*dm(l,2))
404 if (torsion_pmf) then
406 c write (iout,*) "it1",it1," it2",it2," etori",e0(it1,it2),
407 c & " mask",mask_e0(it1,it2)
408 c write (iout,*)"mask_bnew1 1",it2,
409 c & (mask_bnew1tor(j,1,iabs(it2)),j=1,3)
410 c write (iout,*)"mask_bnew1 2",it2,
411 c & (mask_bnew1tor(j,2,iabs(it2)),j=1,3)
412 c write (iout,*)"mask_bnew2 1",it1,(mask_bnew2tor(j,1,it1),j=1,3)
413 c write (iout,*)"mask_bnew2 2",it1,(mask_bnew1tor(j,2,it1),j=1,3)
420 gam=(zz(3,i)+delta-180)*conv
430 b11=(b1m(1,1)+b1m(2,1)*cost2+b1m(3,1)*cost2*cost2)*sint2
431 b12=(b1m(1,2)+b1m(2,2)*cost2+b1m(3,2)*cost2*cost2)*sint2
432 b21=(b2m(1,1)+b2m(2,1)*cost1+b2m(3,1)*cost1*cost1)*sint1
433 b22=(b2m(1,2)+b2m(2,2)*cost1+b2m(3,2)*cost1*cost1)*sint1
434 d11=(dm(1,1)+dm(2,1)*cost1)*sint1*sint1
435 d12=(dm(1,2)+dm(2,2)*cost1)*sint1*sint1
436 c11=(cm(1,1)+cm(2,1)*cost2)*sint2*sint2
437 c12=(cm(1,2)+cm(2,2)*cost2)*sint2*sint2
439 write (iout,*) "b11",b11," b12",b12," b21",b21," b22",b22
440 write (iout,*) "d11",d11," d12",d12," c11",c11," c12",c12
441 write (iout,*) "cosg",cosg," cos2g",cos2g," sing",sing,
444 f=e0(it1,it2)+(b21*b11+b22*b12)*cosg+(b22*b11+b21*b12)*sing
445 & -(c11*d11-c12*d12)*cos2g-(c12*d11+c11*d12)*sin2g
446 fb11=b21*cosg+b22*sing
447 fb12=b22*cosg+b21*sing
448 fb21=b11*cosg+b12*sing
449 fb22=b12*cosg+b11*sing
450 fc11=-d11*cos2g-d12*sin2g
451 fc12= d12*cos2g-d11*sin2g
452 fd11=-c11*cos2g-c12*sin2g
453 fd12= c12*cos2g-c11*sin2g
455 c check with the formula from unres
469 sint1sint2=sint1sint2*sint1*sint2
474 sumvalc=sumvalc+v1_kcc_loc(l,k,j)*c1(k)*c2(l)
475 sumvals=sumvals+v2_kcc_loc(l,k,j)*c1(k)*c2(l)
476 etoriv1(l,k,j)=c1(k)*c2(l)*sint1sint2*cosphi
477 etoriv2(l,k,j)=c1(k)*c2(l)*sint1sint2*sinphi
480 c if (j.eq.1) print *,"j",j," sumvlac",sumvalc*sint1sint2,
482 c & "sumvals",sumvals*sint1sint2,b22*b11+b21*b12
483 c if (j.eq.2) print *,"j",j," sumvlac",sumvalc*sint1sint2,
484 c & -(c11*d11-c12*d12),
485 c & "sumvals",sumvals*sint1sint2,-(c12*d11+c11*d12)
486 etori=etori+(sumvalc*cosphi+sumvals*sinphi)*sint1sint2
490 c v1_kcc(k,l,1)=b1m(k,1)*b2m(l,1)+b1m(k,2)*b2m(l,2)
491 c v2_kcc(k,l,1)=b1m(k,1)*b2m(l,2)+b1m(k,2)*b2m(l,1)
496 c v1_kcc(k,l,2)=-0.25d0*(cm(k,1)*dm(l,1)-cm(k,2)*dm(l,2))
497 c v2_kcc(k,l,2)=-0.25d0*(cm(k,2)*dm(l,1)+cm(k,1)*dm(l,2))
500 c print *,"i",i," theta",zz(i,1),zz(i,2)," gamma",zz(i,3),
501 c & " f",f," etori",etori
506 write (2,'(i7,3f7.1,4f10.3)') i,(zz(j,i),j=1,3),
507 & y(1,i),f,yc(1,i),y(1,i)-yc(1,i)
509 diff(1,i)=y(1,i)-yc(1,i)
510 sum=sum+diff(1,i)*diff(1,i)*w(1,i)
512 c Compute derivatives of the energy in torsional parameters
520 if (mask_e0(it1,it2).gt.0) jac(mask_e0(it1,it2))=1.0d0
521 c Derivatives in b1 of the second residue
525 jj=iabs(mask_bnew1tor(j,1,iabs(it2)))
528 jac(jj)=jac(jj)+etoriv1(j,k,1)*b2m(k,1)
529 & +etoriv2(j,k,1)*b2m(k,2)
532 jj=iabs(mask_bnew1tor(j,2,iabs(it2)))
536 jac(jj)=jac(jj)+etoriv1(j,k,1)*b2m(k,2)
537 & +etoriv2(j,k,1)*b2m(k,1)
538 else if (it2.lt.0) then
539 jac(jj)=jac(jj)-etoriv1(j,k,1)*b2m(k,2)
540 & -etoriv2(j,k,1)*b2m(k,1)
545 c Derivatives in b2 of the first residue
548 jj=iabs(mask_bnew2tor(j,1,it1))
551 jac(jj)=jac(jj)+etoriv1(k,j,1)*b1m(k,1)
552 & +etoriv2(k,j,1)*b1m(k,2)
555 jj=iabs(mask_bnew2tor(j,2,it1))
559 jac(jj)=jac(jj)+etoriv1(k,j,1)*b1m(k,2)
560 & +etoriv2(k,j,1)*b1m(k,1)
568 jj=iabs(mask_ccnewtor(j,1,iabs(it2)))
571 c jac(jj)=jac(jj)-0.25d0*etoriv1(j,k,2)*dm(k,1)
572 c & -0.25d0*etoriv2(j,k,2)*dm(k,2)
573 jac(jj)=jac(jj)-etoriv1(j,k,2)*dm(k,1)
574 & -etoriv2(j,k,2)*dm(k,2)
577 jj=iabs(mask_ccnewtor(j,2,iabs(it2)))
581 c jac(jj+1)=jac(jj)+0.25d0*etoriv1(j,k,2)*dm(k,2)
582 c & -0.25d0*etoriv2(j,k,2)*dm(k,1)
583 jac(jj)=jac(jj)+etoriv1(j,k,2)*dm(k,2)
584 & -etoriv2(j,k,2)*dm(k,1)
585 else if (it2.lt.0) then
586 c jac(jj)=jac(jj)-0.25d0*etoriv1(j,k,2)*dm(k,2)
587 c & +0.25d0*etoriv2(j,k,2)*dm(k,1)
588 jac(jj)=jac(jj)-etoriv1(j,k,2)*dm(k,2)
589 & +etoriv2(j,k,2)*dm(k,1)
597 jj=iabs(mask_ddnewtor(j,1,it1))
600 c jac(jj)=jac(jj)-0.25d0*etoriv1(k,j,2)*cm(k,1)
601 c & -0.25d0*etoriv2(k,j,2)*cm(k,2)
602 jac(jj)=jac(jj)-etoriv1(k,j,2)*cm(k,1)
603 & -etoriv2(k,j,2)*cm(k,2)
606 jj=iabs(mask_ddnewtor(j,2,it1))
610 c jac(jj)=jac(jj)+0.25d0*etoriv1(k,j,2)*cm(k,2)
611 c & -0.25d0*etoriv2(k,j,2)*cm(k,1)
612 jac(jj)=jac(jj)+etoriv1(k,j,2)*cm(k,2)
613 & -etoriv2(k,j,2)*cm(k,1)
618 c Contributions to the gradient and the hessian
620 g(k)=g(k)+diff(1,i)*jac(k)*w(1,i)
621 c h(k,k)=h(k,k)+jac(k)**2*w(1,i)
623 c h(k,l)=h(k,l)+jac(k)*jac(l)*w(1,i)
627 print *,"i",i," jac",jac(:n)," w, diff",w(1,i),diff(1,i),
638 b1m(k,i)=bnew1(k,i,it2)
639 b1m2(k,i)=bnew2(k,i,it2)
640 b2m1(k,i)=bnew1(k,i,it1)
641 b2m(k,i)=bnew2(k,i,it1)
647 em(k,j,i)=eenew(k,j,i,it1)
648 em2(k,j,i)=eenew(k,j,i,it2)
665 gam=(zz(3,i)+delta-180)*conv
668 c eturn3 contributions
669 call eturn3PMF(theta1,theta2,gam,b2m1,b1m2,em,em2,e0m,e0m2,
670 & facturn3,d1,d2,d3,eello_turn3,eello_turn3_b2,eello_turn3_b1,
671 & eello_turn3_e1,eello_turn3_e2,eello_turn3_e0m1,eello_turn3_e0m2)
674 write (2,'(i7,3f7.1,4f10.3)') i,(zz(j,i),j=1,3),
675 & y(2,i),yc(2,i),y(2,i)-yc(2,i)
677 diff(2,i)=y(2,i)-yc(2,i)
678 sum=sum+diff(2,i)*diff(2,i)*w(2,i)
683 if (mask_turn_PMF.gt.0) jac(mask_turn_PMF)=eello_turn3/wturn_PMF
684 c Derivatives of in b1 of the first residue
687 jj=mask_bnew1(j,1,it1)
690 jac(jj)=jac(jj)+eello_turn3_b2(j,1)
694 jj=mask_bnew1(j,2,it1)
697 jac(jj)=jac(jj)+eello_turn3_b2(j,2)
701 c Derivatives of in b2 of the second residue
703 jj=mask_bnew2(j,1,iabs(it2))
705 jac(jj)=jac(jj)+eello_turn3_b1(j,1)
709 jj=mask_bnew2(j,2,iabs(it2))
712 jac(jj)=jac(jj)+eello_turn3_b1(j,2)
713 else if (it2.lt.0) then
714 jac(jj)=jac(jj)-eello_turn3_b1(j,2)
720 c Derivatives in e of the first residue
723 jj=mask_eenew(j,1,1,it1)
724 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e1(j,1,1)
725 jj=mask_eenew(j,2,2,it1)
726 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e1(j,2,2)
728 jj=mask_eenew(j,1,2,it1)
729 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e1(j,1,2)
730 jj=mask_eenew(j,2,1,it1)
731 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e1(j,2,1)
736 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e0m1(1)
739 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e0m1(2)
741 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e0m1(3)
743 c Derivatives in e of the second residue
746 jj=mask_eenew(j,1,1,iabs(it2))
747 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e2(j,1,1)
748 jj=mask_eenew(j,2,2,iabs(it2))
749 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e2(j,2,2)
751 jj=mask_eenew(j,1,2,iabs(it2))
752 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e2(j,1,2)
753 jj=mask_eenew(j,2,1,iabs(it2))
754 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e2(j,2,1)
755 else if (it2.lt.0) then
756 jj=mask_eenew(j,1,2,iabs(it2))
757 if (jj.gt.0) jac(jj)=jac(jj)-eello_turn3_e2(j,1,2)
758 jj=mask_eenew(j,2,1,iabs(it2))
759 if (jj.gt.0) jac(jj)=jac(jj)-eello_turn3_e2(j,2,1)
763 jj=mask_e0new(1,iabs(it2))
764 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e0m2(1)
766 jj=mask_e0new(2,iabs(it2))
767 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e0m2(2)
768 jj=mask_e0new(3,iabs(it2))
769 if (jj.gt.0) jac(jj)=jac(jj)+eello_turn3_e0m2(3)
770 else if (it2.lt.0) then
771 jj=mask_e0new(2,iabs(it2))
772 if (jj.gt.0) jac(jj)=jac(jj)-eello_turn3_e0m2(2)
773 jj=mask_e0new(3,iabs(it2))
774 if (jj.gt.0) jac(jj)=jac(jj)-eello_turn3_e0m2(3)
777 c Contributions to the gradient and the hessian
779 g(k)=g(k)+diff(2,i)*jac(k)*w(2,i)
780 c h(k,k)=h(k,k)+jac(k)**2*w(2,i)
782 c h(k,l)=h(k,l)+jac(k)*jac(l)*w(2,i)
786 print *,"i",i," jac",jac(:n)," w, diff",w(1,i),diff(1,i),
801 do ii=1,npel(it1,it2)
804 theta1=zz(1,ipoint)*conv
805 theta2=zz(2,ipoint)*conv
806 ddist=zz(3,ipoint)**3
807 thetap1=zz(4,ipoint)*conv
808 thetap2=zz(5,ipoint)*conv
809 phi=zz(6,ipoint)*conv
810 gam1=zz(7,ipoint)*conv
811 gam2=zz(8,ipoint)*conv
813 print *,"ipoint",ipoint," theta1",theta1," theta2",theta2,
814 & " dd",dd," thetap1",thetap1," thetap2", thetap2," phi",phi,
815 & " gam1",gam1," gam2",gam2
818 facpp(k)=facp1(k)/ddist
821 print *,"facp1",facp1," facpp",facpp
823 call eello3(theta1,theta2,gam1,gam2,thetap1,thetap2,
824 & phi,b2m1,b2m,b1m,b1m2,facpp,eneelloc3,eneelloc3b,lder)
826 print *,"eneelloc3",eneelloc3
829 yc(k,ipoint)=eneelloc3(k)
830 diff(k,ipoint)=y(k,ipoint)-yc(k,ipoint)
831 sum=sum+diff(k,ipoint)*diff(k,ipoint)*w(k,ipoint)
834 write (2,'(i7,7f7.1,4(2f10.3,5x))') i,(zz(j,ipoint),j=1,7),
835 & (y(k,ipoint),yc(k,ipoint),k=1,4)
839 c print *,"ipoint",ipoint," l",l
843 if (mask_ello_PMF.gt.0)
844 & jac(mask_ello_PMF)=jac(mask_ello_PMF)+eneelloc3(l)/wello_PMF
848 c jac(2)=eneelloc3(l)/wello
849 c Derivatives in b1 of the first residue
852 jj=mask_bnew1(j,1,it1)
854 if (jj.gt.0) jac(jj)=eneelloc3b(j,1,1,1,l)
855 jj=mask_bnew1(j,2,it1)
858 jac(jj)=eneelloc3b(j,2,1,1,l)
859 else if (it1.lt.0) then
860 jac(jj)=-eneelloc3b(j,2,1,1,l)
864 c print *,"bi1: jac",jac(:n)
865 c Derivatives in b2 of the first residue
868 jj=mask_bnew2(j,1,it1)
869 if (jj.gt.0) jac(jj)=eneelloc3b(j,1,2,1,l)
870 jj=mask_bnew2(j,2,it1)
873 jac(jj)=eneelloc3b(j,2,2,1,l)
874 else if (it1.lt.0) then
875 jac(jj)=-eneelloc3b(j,2,2,1,l)
879 c print *,"bi2: jac",jac(:n)
880 c Derivatives in b1 of the second residue
883 jj=mask_bnew1(j,1,iabs(it2))
884 if (jj.gt.0) jac(jj)=jac(jj)+eneelloc3b(j,1,1,2,l)
885 jj=mask_bnew1(j,2,iabs(it2))
888 jac(jj)=jac(jj)+eneelloc3b(j,2,1,2,l)
889 else if (it2.lt.0) then
890 jac(jj)=jac(jj)-eneelloc3b(j,2,1,2,l)
894 c print *,"bj1: jac",jac(:n)
895 c Derivatives in b2 of the second residue
897 jj=mask_bnew2(j,1,iabs(it2))
898 if (jj.gt.0) jac(jj)=jac(jj)+eneelloc3b(j,1,2,2,l)
899 jj=mask_bnew2(j,2,iabs(it2))
902 jac(jj)=jac(jj)+eneelloc3b(j,2,2,2,l)
903 else if (it2.lt.0) then
904 jac(jj)=jac(jj)-eneelloc3b(j,2,2,2,l)
908 c print *,"bj2: jac",jac(:n)
910 g(k)=g(k)+diff(l,ipoint)*jac(k)*w(l,ipoint)
911 c h(k,k)=h(k,k)+jac(k)**2*w(l,ipoint)
913 c h(k,ll)=h(k,ll)+jac(k)*jac(ll)*w(l,ipoint)
917 print *,"ipoint",ipoint," jac",jac(:n)," w, diff",w(l,ipoint),
918 & diff(1,ipoint)," g",g(:n)
921 c Contributions to the gradient and the hessian
932 c if (nfun.ne.-1) then
934 c write (2,*) "sum",sum
936 call MPI_Reduce(sum_,sum,1,MPI_DOUBLE_PRECISION,
937 & MPI_SUM,Master,Comm1,ierror)
939 c write (iout,*) "after reducing function lder",lder
943 call MPI_Reduce(g_(1),g(1),n,MPI_DOUBLE_PRECISION,
944 & MPI_SUM,Master,Comm1,ierror)
945 c write (iout,*) "after reducing gradient"
950 c if (nfun.ne.-1) nfun=nfun+1
953 if (me.eq.Master) then
955 c Angle PMF contribution - no parallelization
957 c if (lder) write (iout,*) "g",(g(i),i=1,n)
959 c write (iout,*) i," mask_v1bend"
960 c do k=0,nbend_kcc_TB(i)
961 c write (iout,*) k,mask_v1bend_chyb(k,i)
963 do j=1,nthet_point(i)
964 thetaj=thet_point(j,i)*deg2rad
966 ebendj=v1bend_chyb(0,i)+
967 & tschebyshev(1,nbend_kcc_Tb(i),v1bend_chyb(1,i),costj)
968 c write (iout,*) "i",i," j",j," theta",thet_point(j,i),
969 c & " pmf_thet",pmf_thet(j,i)," ebend",ebendj
970 chi2thet=chi2thet+(ebendj-pmf_thet(j,i))**2
971 & /(pmf_thet(j,i)+1.0d0)
973 ii = mask_v1bend_chyb(0,i)
974 if (ii.gt.0) g(ii)=g(ii)+wthet_PMF*(pmf_thet(j,i)-ebendj)
975 & /(pmf_thet(j,i)+1.0d0)
976 do k=1,nbend_kcc_TB(i)
977 ii = mask_v1bend_chyb(k,i)
978 if (ii.gt.0) g(ii) = g(ii)+
979 & wthet_PMF*dcos(k*thetaj)*(pmf_thet(j,i)-ebendj)
980 & /(pmf_thet(j,i)+1.0d0)
989 rdif=0.5d0*sum+0.5d0*wthet_PMF*chi2thet
990 c write (iout,*) "Exit rdif"
995 c----------------------------------------------------------------------
996 subroutine eello3(theta1,theta2,gam1,gam2,thetap1,thetap2,
997 & phi,bi1,bi2,bj1,bj2,facpp,eneelloc3,eneelloc3b,lder)
1000 include "DIMENSIONS.ZSCOPT"
1001 include "COMMON.IOUNITS"
1002 double precision theta1,theta2,gam1,gam2,dd,thetap1,thetap2,phi
1003 double precision bi1(3,2),bi2(3,2),bj1(3,2),bj2(3,2),elpp(2,2),
1005 double precision eneelloc3(4),eneelloc3b(3,2,2,2,4),emat1(3,3),
1006 & emat2(3,3),emati1(3,3),emati2(3,3),ematj1(3,3),ematj2(3,3)
1007 double precision mui1(3),mui2(3),muj1(3),muj2(3)
1008 double precision cost1,cost2,sint1,sint2,costp1,sintp1
1009 double precision facpp(4)
1011 common /calcdip/ cost1,cost2,sint1,sint2
1012 double precision pi /3.14159265358979323844d0/
1024 print '(4(a,2f10.5))',"bi1",(bi1(j,k),k=1,2)," bi2",
1026 & " bj1",(bj1(j,k),k=1,2)," bj2",(bj2(j,k),k=1,2)
1028 print *,"cost1",cost1," sint1",sint1," cost2",cost2," sint2",sint2
1031 mui1(j+1)=(bi1(1,j)+bi1(2,j)*cost1+bi1(3,j)*cost1*cost1)*sint1
1032 muj1(j+1)=(bj1(1,j)+bj1(2,j)*cost2+bj1(3,j)*cost2*cost2)*sint2
1033 mui2(j+1)=(bi2(1,j)+bi2(2,j)*cost1+bi2(3,j)*cost1*cost1)*sint1
1034 muj2(j+1)=(bj2(1,j)+bj2(2,j)*cost2+bj2(3,j)*cost2*cost2)*sint2
1041 print *,"Before rotation"
1042 print '(a,3f10.5)',"mui1",mui1
1043 print '(a,3f10.5)',"muj1",muj1
1044 print '(a,3f10.5)',"mui2",mui2
1045 print '(a,3f10.5)',"muj2",muj2
1047 sintp1=dsin(thetap1)
1048 costp1=dcos(thetap1)
1049 c print *,"costp1",costp1," sintp1",sintp1
1059 call coorsys(thetap2,phi,emat2)
1062 print '(3f10.5)',((emat1(j,k),k=1,3),j=1,3)
1064 print '(3f10.5)',((emat2(j,k),k=1,3),j=1,3)
1066 call rotmatsub(emat1,gam1,emati1)
1067 call rotmatsub(emat1,gam1,emati2)
1070 print '(3f10.5)',((emati1(k,j),k=1,3),j=1,3)
1072 print '(3f10.5)',((emati2(k,j),k=1,3),j=1,3)
1074 call matvecsub(emati1,mui1)
1075 call matvecsub(emati2,mui2)
1076 call rotmatsub(emat2,gam2,ematj1)
1077 call rotmatsub(emat2,gam2,ematj2)
1080 print '(3f10.5)',((ematj1(k,j),k=1,3),j=1,3)
1082 print '(3f10.5)',((ematj2(k,j),k=1,3),j=1,3)
1084 call matvecsub(ematj1,muj1)
1085 call matvecsub(ematj2,muj2)
1087 print *,"After rotation"
1088 print '(a,3f10.5)',"mui1",mui1
1089 print '(a,3f10.5)',"muj1",muj1
1090 print '(a,3f10.5)',"mui2",mui2
1091 print '(a,3f10.5)',"muj2",muj2
1093 call edipole(mui1,muj1,emati1,ematj1,facpp(1),eneelloc3(1),
1094 & eneelloc3b(1,1,1,1,1),eneelloc3b(1,1,1,2,1),-1,-1,lder)
1095 call edipole(mui2,muj1,emati2,ematj1,facpp(2),eneelloc3(2),
1096 & eneelloc3b(1,1,2,1,2),eneelloc3b(1,1,1,2,2),1,-1,lder)
1097 call edipole(mui1,muj2,emati1,ematj2,facpp(3),eneelloc3(3),
1098 & eneelloc3b(1,1,1,1,3),eneelloc3b(1,1,2,2,3),-1,1,lder)
1099 call edipole(mui2,muj2,emati2,ematj2,facpp(4),eneelloc3(4),
1100 & eneelloc3b(1,1,2,1,4),eneelloc3b(1,1,2,2,4),1,1,lder)
1103 c-----------------------------------------------------------------------------
1104 subroutine eturn3PMF(theta1,theta2,gam,b1m,b2m,em1,em2,e0m1,e0m2,
1105 & facturn,d1,d2,d3,eello_turn3,eello_turn3_b1,eello_turn3_b2,
1106 & eello_turn3_e1,eello_turn3_e2,eello_turn3_e0m1,eello_turn3_e0m2)
1108 double precision theta1,theta2,gam,cost1,cost2,sint1,sint2,cosg,
1109 & sing,sint1sq,sint2sq,aux
1110 double precision b1m(3,2),b2m(3,2),b1(2),b2(2),em1(2,2,2),
1111 & em2(2,2,2),e0m1(3),e0m2(3),e1(2,2),e2(2,2),facturn,emat1(3,3),
1112 & emat2(3,3),emat1t(3,3),mu1(2),mu2(2),e2gam(2,2),eello_turn3,
1113 & eello_turn3_b2(3,2),eello_turn3_b1(3,2),eello_turn3_e1(2,2,2),
1114 & eello_turn3_e2(2,2,2),eello_turn3_e0m1(3),eello_turn3_e0m2(3),
1115 & r1(3),r2(3),er(3),a(3,3),atemp(3,3),etemp1(2,2),etemp2(2,2),
1116 & d1,d2,d3,ddist,dd3
1117 double precision pi /3.14159265358979323844d0/
1132 mu1(1)=(b1m(1,1)+b1m(2,1)*cost1+b1m(3,1)*cost1*cost1)*sint1
1133 mu1(2)=(b1m(1,2)+b1m(2,2)*cost1+b1m(3,2)*cost1*cost1)*sint1
1134 mu2(1)=(b2m(1,1)+b2m(2,1)*cost2+b2m(3,1)*cost2*cost2)*sint2
1135 mu2(2)=(b2m(1,2)+b2m(2,2)*cost2+b2m(3,2)*cost2*cost2)*sint2
1140 e1(k,l)=(em1(1,k,l)+em1(2,k,l)*cost1)*sint1sq
1141 e2(k,l)=(em2(1,k,l)+em2(2,k,l)*cost2)*sint2sq
1144 e1(1,1)=e1(1,1)+e0m1(1)*cost1
1145 e1(1,2)=e1(1,2)+e0m1(2)+e0m1(3)*cost1
1146 e1(2,1)=e1(2,1)+e0m1(2)*cost1+e0m1(3)
1147 e1(2,2)=e1(2,2)-e0m1(1)
1148 e2(1,1)=e2(1,1)+e0m2(1)*cost2
1149 e2(1,2)=e2(1,2)+e0m2(2)+e0m2(3)*cost2
1150 e2(2,1)=e2(2,1)+e0m2(2)*cost2+e0m2(3)
1151 e2(2,2)=e2(2,2)-e0m2(1)
1153 print *,"mu1",mu1," mu2",mu2," e1",e1," e2",e2
1155 c Coordinate system of the first dipole
1167 emat1t(i,j)=emat1(j,i)
1171 print *,"theta1",theta1*180/pi," theta2",theta2*180/pi,
1174 c Calculate the reference system of the second unit
1175 call coorsys1(theta2,gam,emat2)
1176 c Dipole-dipole interaction matrix
1178 print *,"d1",d1," d2",d2," d3",d3
1179 print *,"emat1",emat1," emat2",emat2
1184 call matvecsub(emat1,r1)
1191 call matvecsub(emat2,r2)
1196 ddist=dsqrt((r2(1)-r1(1))**2+(r2(2)-r1(2))**2+(r2(3)-r1(3))**2)
1197 dd3=facturn/ddist**3
1198 er(1)=(r2(1)-r1(1))/ddist
1199 er(2)=(r2(2)-r1(2))/ddist
1200 er(3)=(r2(3)-r1(3))/ddist
1202 print *,"norm",er(1)**2+er(2)**2+er(3)**2
1203 print *,"r1",r1," r2",r2
1205 print *," dd",dd," dd3",dd3
1209 a(i,j)=-3*er(i)*er(j)*dd3
1210 c a(i,j)=-3*er(i)*er(j)
1215 c a(i,i)=1.0d0+a(i,i)
1218 print *,"a before transformation",a
1219 print *,"emat1t",emat1t
1220 print *,"emat2",emat2
1222 call matmat(emat1t,a,atemp)
1224 print *,"a after transformation",atemp
1226 call matmat(atemp,emat2,a)
1228 print *,"a after transformation",a
1230 print "(1h[,2f10.5,1h],5x,1h|,2f10.5,1h|,5x,1h|,f10.5,1h|)",
1231 & mu1(1),mu1(2),a(2,2),a(2,3),mu2(1)
1232 print "(27x,1h|,2f10.5,1h|,5x,1h|,f10.5,1h|)",
1233 & a(3,2),a(3,3),mu2(2)
1235 eello_turn3=(a(2,2)*mu1(1)*mu2(1)+a(2,3)*mu1(1)*mu2(2)
1236 & +a(3,2)*mu1(2)*mu2(1)+a(3,3)*mu1(2)*mu2(2))
1238 print *,"eello_turn3_1",eello_turn3
1240 eello_turn3_b1(1,1)=-(a(2,2)*mu2(1)+a(2,3)*mu2(2))*sint1
1241 eello_turn3_b1(2,1)=eello_turn3_b1(1,1)*cost1
1242 eello_turn3_b1(3,1)=eello_turn3_b1(2,1)*cost1
1243 eello_turn3_b1(1,2)=(a(3,2)*mu2(1)+a(3,3)*mu2(2))*sint1
1244 eello_turn3_b1(2,2)=eello_turn3_b1(1,2)*cost1
1245 eello_turn3_b1(3,2)=eello_turn3_b1(2,2)*cost1
1246 eello_turn3_b2(1,1)=-(a(2,2)*mu1(1)+a(3,2)*mu1(2))*sint2
1247 eello_turn3_b2(2,1)=eello_turn3_b2(1,1)*cost2
1248 eello_turn3_b2(3,1)=eello_turn3_b2(2,1)*cost2
1249 eello_turn3_b2(1,2)=(a(2,3)*mu1(1)+a(3,3)*mu1(2))*sint2
1250 eello_turn3_b2(2,2)=eello_turn3_b2(1,2)*cost2
1251 eello_turn3_b2(3,2)=eello_turn3_b2(2,2)*cost2
1252 e2gam(1,1)=e2(1,1)*cosg+e2(2,1)*sing
1253 e2gam(1,2)=e2(1,2)*cosg+e2(2,2)*sing
1254 e2gam(2,1)=-e2(1,1)*sing+e2(2,1)*cosg
1255 e2gam(2,2)=-e2(1,2)*sing+e2(2,2)*cosg
1270 aux=aux+a(i+1,j+1)*(e1(i,1)*e2(1,j)+e1(i,2)*e2(2,j))
1274 print *,"eello_turn3_2",aux/2
1276 eello_turn3=eello_turn3+aux/2
1278 c Derivatives in the elements of complete e1 and e2 matrices
1281 etemp1(i,j)=(a(i+1,2)*e2(j,1)+a(i+1,3)*e2(j,2))/2
1282 etemp2(i,j)=(a(2,j+1)*e1(1,i)+a(3,j+1)*e1(2,i))/2
1285 c Derivatives in the components of e1
1287 eello_turn3_e1(1,1,i)=etemp1(1,i)*sint1sq
1288 eello_turn3_e1(2,1,i)=eello_turn3_e1(1,1,i)*cost1
1289 eello_turn3_e1(1,2,i)= etemp1(2,i)*sint1sq
1290 eello_turn3_e1(2,2,i)=eello_turn3_e1(1,2,i)*cost1
1292 c Derivatives in the components of e01m
1293 eello_turn3_e0m1(1)=etemp1(1,1)*cost1-etemp1(2,2)
1294 eello_turn3_e0m1(2)=etemp1(1,2)+cost1*etemp1(2,1)
1295 eello_turn3_e0m1(3)=etemp1(1,2)*cost1+etemp1(2,1)
1296 c Derivatives in the elements of e2
1298 eello_turn3_e2(1,1,i)=-(etemp2(1,i)*cosg+etemp2(2,i)*sing)
1300 eello_turn3_e2(2,1,i)=eello_turn3_e2(1,1,i)*cost2
1301 eello_turn3_e2(1,2,i)=(-etemp2(1,i)*sing+etemp2(2,i)*cosg)
1303 eello_turn3_e2(2,2,i)=eello_turn3_e2(1,2,i)*cost2
1305 c Derivatives in the elements of e02m
1306 eello_turn3_e0m2(1)=-(etemp2(1,1)*cosg+etemp2(2,1)*sing)*cost2
1307 & +etemp2(1,2)*sing-etemp2(2,2)*cosg
1308 eello_turn3_e0m2(2)=-etemp2(1,2)*cosg-etemp2(2,2)*sing+
1309 & (-etemp2(1,1)*sing+etemp2(2,1)*cosg)*cost2
1310 eello_turn3_e0m2(3)=(-etemp2(1,2)*cosg-etemp2(2,2)*sing)*cost2
1311 & -etemp2(1,1)*sing+etemp2(2,1)*cosg
1314 c-----------------------------------------------------------------------------
1315 subroutine edipole(mui,muj,emati,ematj,facpp,ene,enebi,enebj,
1317 include "DIMENSIONS"
1318 include "DIMENSIONS.ZSCOPT"
1319 include "COMMON.IOUNITS"
1320 double precision mui(3),muj(3),emati(3,3),ematj(3,3),
1321 & facpp,ene,enebi(3,2),enebj(3,2),muimuj,cost1,sint1,cost2,sint2
1324 common /calcdip/ cost1,cost2,sint1,sint2
1325 muimuj=mui(1)*muj(1)+mui(2)*muj(2)+mui(3)*muj(3)
1326 ene=facpp*(muimuj-3*mui(3)*muj(3))
1327 if (idiri.gt.0) then
1328 enebi(1,1)=(emati(1,2)*muj(1)+emati(2,2)*muj(2)
1329 & +emati(3,2)*muj(3)-3*muj(3)*emati(3,2))*sint1*facpp
1330 enebi(2,1)=enebi(1,1)*cost1
1331 enebi(3,1)=enebi(2,1)*cost1
1332 enebi(1,2)=(emati(1,3)*muj(1)+emati(2,3)*muj(2)
1333 & +emati(3,3)*muj(3)-3*muj(3)*emati(3,3))*sint1*facpp
1334 enebi(2,2)=enebi(1,2)*cost1
1335 enebi(3,2)=enebi(2,2)*cost1
1337 enebi(1,1)=(-emati(1,2)*muj(1)-emati(2,2)*muj(2)
1338 & -emati(3,2)*muj(3)+3*muj(3)*emati(3,2))*sint1*facpp
1339 enebi(2,1)=enebi(1,1)*cost1
1340 enebi(3,1)=enebi(2,1)*cost1
1341 enebi(1,2)=(emati(1,3)*muj(1)+emati(2,3)*muj(2)
1342 & +emati(3,3)*muj(3)-3*muj(3)*emati(3,3))*sint1*facpp
1343 enebi(2,2)=enebi(1,2)*cost1
1344 enebi(3,2)=enebi(2,2)*cost1
1346 if (idirj.gt.0) then
1347 enebj(1,1)=(ematj(1,2)*mui(1)+ematj(2,2)*mui(2)
1348 & +ematj(3,2)*mui(3)-3*mui(3)*ematj(3,2))*sint2*facpp
1349 enebj(2,1)=enebj(1,1)*cost2
1350 enebj(3,1)=enebj(2,1)*cost2
1351 enebj(1,2)=(ematj(1,3)*mui(1)+ematj(2,3)*mui(2)
1352 & +ematj(3,3)*mui(3)-3*mui(3)*ematj(3,3))*sint2*facpp
1353 enebj(2,2)=enebj(1,2)*cost2
1354 enebj(3,2)=enebj(2,2)*cost2
1356 enebj(1,1)=(-ematj(1,2)*mui(1)-ematj(2,2)*mui(2)
1357 & -ematj(3,2)*mui(3)+3*mui(3)*ematj(3,2))*sint2*facpp
1358 enebj(2,1)=enebj(1,1)*cost2
1359 enebj(3,1)=enebj(2,1)*cost2
1360 enebj(1,2)=(ematj(1,3)*mui(1)+ematj(2,3)*mui(2)
1361 & +ematj(3,3)*mui(3)-3*mui(3)*ematj(3,3))*sint2*facpp
1362 enebj(2,2)=enebj(1,2)*cost2
1363 enebj(3,2)=enebj(2,2)*cost2
1367 c-----------------------------------------------------------------------------
1368 subroutine matvecsub(mat,vec)
1370 double precision mat(3,3),vec(3),auxvec(3)
1375 auxvec(i)=auxvec(i)+mat(i,j)*vec(j)
1381 c-------------------------------------------------------------------------------
1382 subroutine rotmatsub(mat,gam,matgam)
1384 double precision mat(3,3),gam,matgam(3,3),cosg,sing
1389 matgam(i,1)=mat(i,1)
1390 matgam(i,2)= mat(i,2)*cosg+mat(i,3)*sing
1391 matgam(i,3)=-mat(i,2)*sing+mat(i,3)*cosg
1395 c-----------------------------------------------------------------------------
1396 subroutine coorsys(theta,phi,emat)
1398 include "DIMENSIONS"
1399 include "DIMENSIONS.ZSCOPT"
1400 include "COMMON.IOUNITS"
1401 double precision theta,phi,emat(3,3)
1402 double precision cost,sint,cosphi,sinphi
1403 c print *,"theta",theta," phi",phi
1409 print *,"cost",cost," sint",sint," cosphi",cosphi," sinphi",sinphi
1412 emat(1,1)=sint*cosphi
1413 emat(2,1)=sint*sinphi
1418 emat(1,3)=-cost*cosphi
1419 emat(2,3)=-cost*sinphi
1423 c-----------------------------------------------------------------------------
1424 subroutine coorsys1(theta,phi,emat)
1426 include "DIMENSIONS"
1427 include "DIMENSIONS.ZSCOPT"
1428 include "COMMON.IOUNITS"
1429 double precision theta,phi,emat(3,3)
1430 double precision cost,sint,cosphi,sinphi
1431 c print *,"theta",theta," phi",phi
1437 print *,"cost",cost," sint",sint," cosphi",cosphi," sinphi",sinphi
1440 emat(1,1)=sint*cosphi
1442 emat(3,1)=-sint*sinphi
1443 emat(1,2)=-cost*cosphi
1445 emat(3,2)=cost*sinphi
1451 c-------------------------------------------------------------------------------
1452 subroutine matmat(a,b,c)
1454 double precision a(3,3),b(3,3),c(3,3)
1460 c(i,j)=c(i,j)+a(i,k)*b(k,j)