Merge branch 'homology' of mmka.chem.univ.gda.pl:unres into homology
[unres.git] / source / unres / src_MD / energy_p_new_barrier.F
1       subroutine etotal(energia)
2       implicit real*8 (a-h,o-z)
3       include 'DIMENSIONS'
4 #ifndef ISNAN
5       external proc_proc
6 #ifdef WINPGI
7 cMS$ATTRIBUTES C ::  proc_proc
8 #endif
9 #endif
10 #ifdef MPI
11       include "mpif.h"
12       double precision weights_(n_ene)
13 #endif
14       include 'COMMON.SETUP'
15       include 'COMMON.IOUNITS'
16       double precision energia(0:n_ene)
17       include 'COMMON.LOCAL'
18       include 'COMMON.FFIELD'
19       include 'COMMON.DERIV'
20       include 'COMMON.INTERACT'
21       include 'COMMON.SBRIDGE'
22       include 'COMMON.CHAIN'
23       include 'COMMON.VAR'
24       include 'COMMON.MD'
25       include 'COMMON.CONTROL'
26       include 'COMMON.TIME1'
27 #ifdef MPI      
28 c      print*,"ETOTAL Processor",fg_rank," absolute rank",myrank,
29 c     & " nfgtasks",nfgtasks
30       call flush(iout)
31       if (nfgtasks.gt.1) then
32 #ifdef MPI
33         time00=MPI_Wtime()
34 #else
35         time00=tcpu()
36 #endif
37 C FG slaves call the following matching MPI_Bcast in ERGASTULUM
38         if (fg_rank.eq.0) then
39           call MPI_Bcast(0,1,MPI_INTEGER,king,FG_COMM,IERROR)
40 c          print *,"Processor",myrank," BROADCAST iorder"
41 C FG master sets up the WEIGHTS_ array which will be broadcast to the 
42 C FG slaves as WEIGHTS array.
43           weights_(1)=wsc
44           weights_(2)=wscp
45           weights_(3)=welec
46           weights_(4)=wcorr
47           weights_(5)=wcorr5
48           weights_(6)=wcorr6
49           weights_(7)=wel_loc
50           weights_(8)=wturn3
51           weights_(9)=wturn4
52           weights_(10)=wturn6
53           weights_(11)=wang
54           weights_(12)=wscloc
55           weights_(13)=wtor
56           weights_(14)=wtor_d
57           weights_(15)=wstrain
58           weights_(16)=wvdwpp
59           weights_(17)=wbond
60           weights_(18)=scal14
61           weights_(21)=wsccor
62           weights_(22)=wsct
63 C FG Master broadcasts the WEIGHTS_ array
64           call MPI_Bcast(weights_(1),n_ene,
65      &        MPI_DOUBLE_PRECISION,king,FG_COMM,IERROR)
66         else
67 C FG slaves receive the WEIGHTS array
68           call MPI_Bcast(weights(1),n_ene,
69      &        MPI_DOUBLE_PRECISION,king,FG_COMM,IERROR)
70           wsc=weights(1)
71           wscp=weights(2)
72           welec=weights(3)
73           wcorr=weights(4)
74           wcorr5=weights(5)
75           wcorr6=weights(6)
76           wel_loc=weights(7)
77           wturn3=weights(8)
78           wturn4=weights(9)
79           wturn6=weights(10)
80           wang=weights(11)
81           wscloc=weights(12)
82           wtor=weights(13)
83           wtor_d=weights(14)
84           wstrain=weights(15)
85           wvdwpp=weights(16)
86           wbond=weights(17)
87           scal14=weights(18)
88           wsccor=weights(21)
89           wsct=weights(22)
90         endif
91         time_Bcast=time_Bcast+MPI_Wtime()-time00
92         time_Bcastw=time_Bcastw+MPI_Wtime()-time00
93 c        call chainbuild_cart
94       endif
95 c      write(iout,*) 'Processor',myrank,' calling etotal ipot=',ipot
96 c      print *,'Processor',myrank,' nnt=',nnt,' nct=',nct
97 #else
98 c      if (modecalc.eq.12.or.modecalc.eq.14) then
99 c        call int_from_cart1(.false.)
100 c      endif
101 #endif     
102 #ifdef TIMING
103 #ifdef MPI
104       time00=MPI_Wtime()
105 #else
106       time00=tcpu()
107 #endif
108 #endif
109
110 C Compute the side-chain and electrostatic interaction energy
111 C
112       goto (101,102,103,104,105,106) ipot
113 C Lennard-Jones potential.
114   101 call elj(evdw,evdw_p,evdw_m)
115 cd    print '(a)','Exit ELJ'
116       goto 107
117 C Lennard-Jones-Kihara potential (shifted).
118   102 call eljk(evdw,evdw_p,evdw_m)
119       goto 107
120 C Berne-Pechukas potential (dilated LJ, angular dependence).
121   103 call ebp(evdw,evdw_p,evdw_m)
122       goto 107
123 C Gay-Berne potential (shifted LJ, angular dependence).
124   104 call egb(evdw,evdw_p,evdw_m)
125       goto 107
126 C Gay-Berne-Vorobjev potential (shifted LJ, angular dependence).
127   105 call egbv(evdw,evdw_p,evdw_m)
128       goto 107
129 C Soft-sphere potential
130   106 call e_softsphere(evdw)
131 C
132 C Calculate electrostatic (H-bonding) energy of the main chain.
133 C
134   107 continue
135 C     BARTEK for dfa test!
136       if (wdfa_dist.gt.0) then 
137         call edfad(edfadis)
138       else
139         edfadis=0
140       endif
141 c      print*, 'edfad is finished!', edfadis
142       if (wdfa_tor.gt.0) then
143         call edfat(edfator)
144       else
145         edfator=0
146       endif
147 c      print*, 'edfat is finished!', edfator
148       if (wdfa_nei.gt.0) then
149         call edfan(edfanei)
150       else
151         edfanei=0
152       endif    
153 c      print*, 'edfan is finished!', edfanei
154       if (wdfa_beta.gt.0) then 
155         call edfab(edfabet)
156       else
157         edfabet=0
158       endif
159 c      print*, 'edfab is finished!', edfabet
160 cmc
161 cmc Sep-06: egb takes care of dynamic ss bonds too
162 cmc
163 c      if (dyn_ss) call dyn_set_nss
164
165 c      print *,"Processor",myrank," computed USCSC"
166 #ifdef TIMING
167 #ifdef MPI
168       time01=MPI_Wtime() 
169 #else
170       time00=tcpu()
171 #endif
172 #endif
173       call vec_and_deriv
174 #ifdef TIMING
175 #ifdef MPI
176       time_vec=time_vec+MPI_Wtime()-time01
177 #else
178       time_vec=time_vec+tcpu()-time01
179 #endif
180 #endif
181 c      print *,"Processor",myrank," left VEC_AND_DERIV"
182       if (ipot.lt.6) then
183 #ifdef SPLITELE
184          if (welec.gt.0d0.or.wvdwpp.gt.0d0.or.wel_loc.gt.0d0.or.
185      &       wturn3.gt.0d0.or.wturn4.gt.0d0 .or. wcorr.gt.0.0d0
186      &       .or. wcorr4.gt.0.0d0 .or. wcorr5.gt.0.d0
187      &       .or. wcorr6.gt.0.0d0 .or. wturn6.gt.0.0d0 ) then
188 #else
189          if (welec.gt.0d0.or.wel_loc.gt.0d0.or.
190      &       wturn3.gt.0d0.or.wturn4.gt.0d0 .or. wcorr.gt.0.0d0
191      &       .or. wcorr4.gt.0.0d0 .or. wcorr5.gt.0.d0 
192      &       .or. wcorr6.gt.0.0d0 .or. wturn6.gt.0.0d0 ) then
193 #endif
194             call eelec(ees,evdw1,eel_loc,eello_turn3,eello_turn4)
195          else
196             ees=0.0d0
197             evdw1=0.0d0
198             eel_loc=0.0d0
199             eello_turn3=0.0d0
200             eello_turn4=0.0d0
201          endif
202       else
203 c        write (iout,*) "Soft-spheer ELEC potential"
204         call eelec_soft_sphere(ees,evdw1,eel_loc,eello_turn3,
205      &   eello_turn4)
206       endif
207 c      print *,"Processor",myrank," computed UELEC"
208 C
209 C Calculate excluded-volume interaction energy between peptide groups
210 C and side chains.
211 C
212       if (ipot.lt.6) then
213        if(wscp.gt.0d0) then
214         call escp(evdw2,evdw2_14)
215        else
216         evdw2=0
217         evdw2_14=0
218        endif
219       else
220 c        write (iout,*) "Soft-sphere SCP potential"
221         call escp_soft_sphere(evdw2,evdw2_14)
222       endif
223 c
224 c Calculate the bond-stretching energy
225 c
226       call ebond(estr)
227
228 C Calculate the disulfide-bridge and other energy and the contributions
229 C from other distance constraints.
230 cd    print *,'Calling EHPB'
231       call edis(ehpb)
232 cd    print *,'EHPB exitted succesfully.'
233 C
234 C Calculate the virtual-bond-angle energy.
235 C
236       if (wang.gt.0d0) then
237         call ebend(ebe)
238       else
239         ebe=0
240       endif
241 c      print *,"Processor",myrank," computed UB"
242 C
243 C Calculate the SC local energy.
244 C
245       call esc(escloc)
246 c      print *,"Processor",myrank," computed USC"
247 C
248 C Calculate the virtual-bond torsional energy.
249 C
250 cd    print *,'nterm=',nterm
251       if (wtor.gt.0) then
252        call etor(etors,edihcnstr)
253       else
254        etors=0
255        edihcnstr=0
256       endif
257
258       if (constr_homology.ge.1) then
259         call e_modeller(ehomology_constr)
260 c        print *,'iset=',iset,'me=',me,ehomology_constr,
261 c     &  'Processor',fg_rank,' CG group',kolor,
262 c     &  ' absolute rank',MyRank
263       else
264         ehomology_constr=0.0d0
265       endif
266
267
268 c      write(iout,*) ehomology_constr
269 c      print *,"Processor",myrank," computed Utor"
270 C
271 C 6/23/01 Calculate double-torsional energy
272 C
273       if (wtor_d.gt.0) then
274        call etor_d(etors_d)
275       else
276        etors_d=0
277       endif
278 c      print *,"Processor",myrank," computed Utord"
279 C
280 C 21/5/07 Calculate local sicdechain correlation energy
281 C
282       if (wsccor.gt.0.0d0) then
283         call eback_sc_corr(esccor)
284       else
285         esccor=0.0d0
286       endif
287 c      print *,"Processor",myrank," computed Usccorr"
288
289 C 12/1/95 Multi-body terms
290 C
291       n_corr=0
292       n_corr1=0
293       if ((wcorr4.gt.0.0d0 .or. wcorr5.gt.0.0d0 .or. wcorr6.gt.0.0d0 
294      &    .or. wturn6.gt.0.0d0) .and. ipot.lt.6) then
295          call multibody_eello(ecorr,ecorr5,ecorr6,eturn6,n_corr,n_corr1)
296 cd         write(2,*)'multibody_eello n_corr=',n_corr,' n_corr1=',n_corr1,
297 cd     &" ecorr",ecorr," ecorr5",ecorr5," ecorr6",ecorr6," eturn6",eturn6
298       else
299          ecorr=0.0d0
300          ecorr5=0.0d0
301          ecorr6=0.0d0
302          eturn6=0.0d0
303       endif
304       if ((wcorr4.eq.0.0d0 .and. wcorr.gt.0.0d0) .and. ipot.lt.6) then
305          call multibody_hb(ecorr,ecorr5,ecorr6,n_corr,n_corr1)
306 cd         write (iout,*) "multibody_hb ecorr",ecorr
307       endif
308 c      print *,"Processor",myrank," computed Ucorr"
309
310 C If performing constraint dynamics, call the constraint energy
311 C  after the equilibration time
312       if(usampl.and.totT.gt.eq_time) then
313 c         write (iout,*) "CALL TO ECONSTR_BACK"
314          call EconstrQ   
315          call Econstr_back
316       else
317          Uconst=0.0d0
318          Uconst_back=0.0d0
319       endif
320 #ifdef TIMING
321 #ifdef MPI
322       time_enecalc=time_enecalc+MPI_Wtime()-time00
323 #else
324       time_enecalc=time_enecalc+tcpu()-time00
325 #endif
326 #endif
327 c      print *,"Processor",myrank," computed Uconstr"
328 #ifdef TIMING
329 #ifdef MPI
330       time00=MPI_Wtime()
331 #else
332       time00=tcpu()
333 #endif
334 #endif
335 c
336 C Sum the energies
337 C
338       energia(1)=evdw
339 #ifdef SCP14
340       energia(2)=evdw2-evdw2_14
341       energia(18)=evdw2_14
342 #else
343       energia(2)=evdw2
344       energia(18)=0.0d0
345 #endif
346 #ifdef SPLITELE
347       energia(3)=ees
348       energia(16)=evdw1
349 #else
350       energia(3)=ees+evdw1
351       energia(16)=0.0d0
352 #endif
353       energia(4)=ecorr
354       energia(5)=ecorr5
355       energia(6)=ecorr6
356       energia(7)=eel_loc
357       energia(8)=eello_turn3
358       energia(9)=eello_turn4
359       energia(10)=eturn6
360       energia(11)=ebe
361       energia(12)=escloc
362       energia(13)=etors
363       energia(14)=etors_d
364       energia(15)=ehpb
365       energia(19)=edihcnstr
366       energia(17)=estr
367       energia(20)=Uconst+Uconst_back
368       energia(21)=esccor
369       energia(22)=evdw_p
370       energia(23)=evdw_m
371       energia(24)=ehomology_constr
372       energia(25)=edfadis
373       energia(26)=edfator
374       energia(27)=edfanei
375       energia(28)=edfabet
376 c      print *," Processor",myrank," calls SUM_ENERGY"
377       call sum_energy(energia,.true.)
378       if (dyn_ss) call dyn_set_nss
379 c      print *," Processor",myrank," left SUM_ENERGY"
380 #ifdef TIMING
381 #ifdef MPI
382       time_sumene=time_sumene+MPI_Wtime()-time00
383 #else
384       time_sumene=time_sumene+tcpu()-time00
385 #endif
386 #endif
387       return
388       end
389 c-------------------------------------------------------------------------------
390       subroutine sum_energy(energia,reduce)
391       implicit real*8 (a-h,o-z)
392       include 'DIMENSIONS'
393 #ifndef ISNAN
394       external proc_proc
395 #ifdef WINPGI
396 cMS$ATTRIBUTES C ::  proc_proc
397 #endif
398 #endif
399 #ifdef MPI
400       include "mpif.h"
401 #endif
402       include 'COMMON.SETUP'
403       include 'COMMON.IOUNITS'
404       double precision energia(0:n_ene),enebuff(0:n_ene+1)
405       include 'COMMON.FFIELD'
406       include 'COMMON.DERIV'
407       include 'COMMON.INTERACT'
408       include 'COMMON.SBRIDGE'
409       include 'COMMON.CHAIN'
410       include 'COMMON.VAR'
411       include 'COMMON.CONTROL'
412       include 'COMMON.TIME1'
413       logical reduce
414 #ifdef MPI
415       if (nfgtasks.gt.1 .and. reduce) then
416 #ifdef DEBUG
417         write (iout,*) "energies before REDUCE"
418         call enerprint(energia)
419         call flush(iout)
420 #endif
421         do i=0,n_ene
422           enebuff(i)=energia(i)
423         enddo
424         time00=MPI_Wtime()
425         call MPI_Barrier(FG_COMM,IERR)
426         time_barrier_e=time_barrier_e+MPI_Wtime()-time00
427         time00=MPI_Wtime()
428         call MPI_Reduce(enebuff(0),energia(0),n_ene+1,
429      &    MPI_DOUBLE_PRECISION,MPI_SUM,king,FG_COMM,IERR)
430 #ifdef DEBUG
431         write (iout,*) "energies after REDUCE"
432         call enerprint(energia)
433         call flush(iout)
434 #endif
435         time_Reduce=time_Reduce+MPI_Wtime()-time00
436       endif
437       if (fg_rank.eq.0) then
438 #endif
439 #ifdef TSCSC
440       evdw=energia(22)+wsct*energia(23)
441 #else
442       evdw=energia(1)
443 #endif
444 #ifdef SCP14
445       evdw2=energia(2)+energia(18)
446       evdw2_14=energia(18)
447 #else
448       evdw2=energia(2)
449 #endif
450 #ifdef SPLITELE
451       ees=energia(3)
452       evdw1=energia(16)
453 #else
454       ees=energia(3)
455       evdw1=0.0d0
456 #endif
457       ecorr=energia(4)
458       ecorr5=energia(5)
459       ecorr6=energia(6)
460       eel_loc=energia(7)
461       eello_turn3=energia(8)
462       eello_turn4=energia(9)
463       eturn6=energia(10)
464       ebe=energia(11)
465       escloc=energia(12)
466       etors=energia(13)
467       etors_d=energia(14)
468       ehpb=energia(15)
469       edihcnstr=energia(19)
470       estr=energia(17)
471       Uconst=energia(20)
472       esccor=energia(21)
473       ehomology_constr=energia(24)
474       edfadis=energia(25)
475       edfator=energia(26)
476       edfanei=energia(27)
477       edfabet=energia(28)
478 #ifdef SPLITELE
479       etot=wsc*evdw+wscp*evdw2+welec*ees+wvdwpp*evdw1
480      & +wang*ebe+wtor*etors+wscloc*escloc
481      & +wstrain*ehpb+wcorr*ecorr+wcorr5*ecorr5
482      & +wcorr6*ecorr6+wturn4*eello_turn4+wturn3*eello_turn3
483      & +wturn6*eturn6+wel_loc*eel_loc+edihcnstr+wtor_d*etors_d
484      & +wbond*estr+Uconst+wsccor*esccor+ehomology_constr
485      & +wdfa_dist*edfadis+wdfa_tor*edfator+wdfa_nei*edfanei
486      & +wdfa_beta*edfabet    
487 #else
488       etot=wsc*evdw+wscp*evdw2+welec*(ees+evdw1)
489      & +wang*ebe+wtor*etors+wscloc*escloc
490      & +wstrain*ehpb+wcorr*ecorr+wcorr5*ecorr5
491      & +wcorr6*ecorr6+wturn4*eello_turn4+wturn3*eello_turn3
492      & +wturn6*eturn6+wel_loc*eel_loc+edihcnstr+wtor_d*etors_d
493      & +wbond*estr+Uconst+wsccor*esccor+ehomology_constr
494      & +wdfa_dist*edfadis+wdfa_tor*edfator+wdfa_nei*edfanei
495      & +wdfa_beta*edfabet    
496 #endif
497       energia(0)=etot
498 c detecting NaNQ
499 #ifdef ISNAN
500 #ifdef AIX
501       if (isnan(etot).ne.0) energia(0)=1.0d+99
502 #else
503       if (isnan(etot)) energia(0)=1.0d+99
504 #endif
505 #else
506       i=0
507 #ifdef WINPGI
508       idumm=proc_proc(etot,i)
509 #else
510       call proc_proc(etot,i)
511 #endif
512       if(i.eq.1)energia(0)=1.0d+99
513 #endif
514 #ifdef MPI
515       endif
516 #endif
517       return
518       end
519 c-------------------------------------------------------------------------------
520       subroutine sum_gradient
521       implicit real*8 (a-h,o-z)
522       include 'DIMENSIONS'
523 #ifndef ISNAN
524       external proc_proc
525 #ifdef WINPGI
526 cMS$ATTRIBUTES C ::  proc_proc
527 #endif
528 #endif
529 #ifdef MPI
530       include 'mpif.h'
531 #endif
532       double precision gradbufc(3,maxres),gradbufx(3,maxres),
533      &  glocbuf(4*maxres),gradbufc_sum(3,maxres),gloc_scbuf(3,maxres)
534       include 'COMMON.SETUP'
535       include 'COMMON.IOUNITS'
536       include 'COMMON.FFIELD'
537       include 'COMMON.DERIV'
538       include 'COMMON.INTERACT'
539       include 'COMMON.SBRIDGE'
540       include 'COMMON.CHAIN'
541       include 'COMMON.VAR'
542       include 'COMMON.CONTROL'
543       include 'COMMON.TIME1'
544       include 'COMMON.MAXGRAD'
545       include 'COMMON.SCCOR'
546 #ifdef TIMING
547 #ifdef MPI
548       time01=MPI_Wtime()
549 #else
550       time01=tcpu()
551 #endif
552 #endif
553 #ifdef DEBUG
554       write (iout,*) "sum_gradient gvdwc, gvdwx"
555       do i=1,nres
556         write (iout,'(i3,3f10.5,5x,3f10.5,5x,3f10.5,5x,3f10.5)') 
557      &   i,(gvdwx(j,i),j=1,3),(gvdwcT(j,i),j=1,3),(gvdwc(j,i),j=1,3),
558      &   (gvdwcT(j,i),j=1,3)
559       enddo
560       call flush(iout)
561 #endif
562 #ifdef MPI
563 C FG slaves call the following matching MPI_Bcast in ERGASTULUM
564         if (nfgtasks.gt.1 .and. fg_rank.eq.0) 
565      &    call MPI_Bcast(1,1,MPI_INTEGER,king,FG_COMM,IERROR)
566 #endif
567 C
568 C 9/29/08 AL Transform parts of gradients in site coordinates to the gradient
569 C            in virtual-bond-vector coordinates
570 C
571 #ifdef DEBUG
572 c      write (iout,*) "gel_loc gel_loc_long and gel_loc_loc"
573 c      do i=1,nres-1
574 c        write (iout,'(i5,3f10.5,2x,3f10.5,2x,f10.5)') 
575 c     &   i,(gel_loc(j,i),j=1,3),(gel_loc_long(j,i),j=1,3),gel_loc_loc(i)
576 c      enddo
577 c      write (iout,*) "gel_loc_tur3 gel_loc_turn4"
578 c      do i=1,nres-1
579 c        write (iout,'(i5,3f10.5,2x,f10.5)') 
580 c     &  i,(gcorr4_turn(j,i),j=1,3),gel_loc_turn4(i)
581 c      enddo
582       write (iout,*) "gradcorr5 gradcorr5_long gradcorr5_loc"
583       do i=1,nres
584         write (iout,'(i3,3f10.5,5x,3f10.5,5x,f10.5)') 
585      &   i,(gradcorr5(j,i),j=1,3),(gradcorr5_long(j,i),j=1,3),
586      &   g_corr5_loc(i)
587       enddo
588       call flush(iout)
589 #endif
590 #ifdef SPLITELE
591 #ifdef TSCSC
592       do i=1,nct
593         do j=1,3
594           gradbufc(j,i)=wsc*gvdwc(j,i)+wsc*wscT*gvdwcT(j,i)+
595      &                wscp*(gvdwc_scp(j,i)+gvdwc_scpp(j,i))+
596      &                welec*gelc_long(j,i)+wvdwpp*gvdwpp(j,i)+
597      &                wel_loc*gel_loc_long(j,i)+
598      &                wcorr*gradcorr_long(j,i)+
599      &                wcorr5*gradcorr5_long(j,i)+
600      &                wcorr6*gradcorr6_long(j,i)+
601      &                wturn6*gcorr6_turn_long(j,i)+
602      &                wstrain*ghpbc(j,i)+
603      &                wdfa_dist*gdfad(j,i)+
604      &                wdfa_tor*gdfat(j,i)+
605      &                wdfa_nei*gdfan(j,i)+
606      &                wdfa_beta*gdfab(j,i)
607         enddo
608       enddo 
609 #else
610       do i=1,nct
611         do j=1,3
612           gradbufc(j,i)=wsc*gvdwc(j,i)+
613      &                wscp*(gvdwc_scp(j,i)+gvdwc_scpp(j,i))+
614      &                welec*gelc_long(j,i)+wvdwpp*gvdwpp(j,i)+
615      &                wel_loc*gel_loc_long(j,i)+
616      &                wcorr*gradcorr_long(j,i)+
617      &                wcorr5*gradcorr5_long(j,i)+
618      &                wcorr6*gradcorr6_long(j,i)+
619      &                wturn6*gcorr6_turn_long(j,i)+
620      &                wstrain*ghpbc(j,i)+
621      &                wdfa_dist*gdfad(j,i)+
622      &                wdfa_tor*gdfat(j,i)+
623      &                wdfa_nei*gdfan(j,i)+
624      &                wdfa_beta*gdfab(j,i)
625         enddo
626       enddo 
627 #endif
628 #else
629       do i=1,nct
630         do j=1,3
631           gradbufc(j,i)=wsc*gvdwc(j,i)+
632      &                wscp*(gvdwc_scp(j,i)+gvdwc_scpp(j,i))+
633      &                welec*gelc_long(j,i)+
634      &                wbond*gradb(j,i)+
635      &                wel_loc*gel_loc_long(j,i)+
636      &                wcorr*gradcorr_long(j,i)+
637      &                wcorr5*gradcorr5_long(j,i)+
638      &                wcorr6*gradcorr6_long(j,i)+
639      &                wturn6*gcorr6_turn_long(j,i)+
640      &                wstrain*ghpbc(j,i)+
641      &                wdfa_dist*gdfad(j,i)+
642      &                wdfa_tor*gdfat(j,i)+
643      &                wdfa_nei*gdfan(j,i)+
644      &                wdfa_beta*gdfab(j,i)
645         enddo
646       enddo 
647 #endif
648 #ifdef MPI
649       if (nfgtasks.gt.1) then
650       time00=MPI_Wtime()
651 #ifdef DEBUG
652       write (iout,*) "gradbufc before allreduce"
653       do i=1,nres
654         write (iout,'(i3,3f10.5)') i,(gradbufc(j,i),j=1,3)
655       enddo
656       call flush(iout)
657 #endif
658       do i=1,nres
659         do j=1,3
660           gradbufc_sum(j,i)=gradbufc(j,i)
661         enddo
662       enddo
663 c      call MPI_AllReduce(gradbufc(1,1),gradbufc_sum(1,1),3*nres,
664 c     &    MPI_DOUBLE_PRECISION,MPI_SUM,FG_COMM,IERR)
665 c      time_reduce=time_reduce+MPI_Wtime()-time00
666 #ifdef DEBUG
667 c      write (iout,*) "gradbufc_sum after allreduce"
668 c      do i=1,nres
669 c        write (iout,'(i3,3f10.5)') i,(gradbufc_sum(j,i),j=1,3)
670 c      enddo
671 c      call flush(iout)
672 #endif
673 #ifdef TIMING
674 c      time_allreduce=time_allreduce+MPI_Wtime()-time00
675 #endif
676       do i=nnt,nres
677         do k=1,3
678           gradbufc(k,i)=0.0d0
679         enddo
680       enddo
681 #ifdef DEBUG
682       write (iout,*) "igrad_start",igrad_start," igrad_end",igrad_end
683       write (iout,*) (i," jgrad_start",jgrad_start(i),
684      &                  " jgrad_end  ",jgrad_end(i),
685      &                  i=igrad_start,igrad_end)
686 #endif
687 c
688 c Obsolete and inefficient code; we can make the effort O(n) and, therefore,
689 c do not parallelize this part.
690 c
691 c      do i=igrad_start,igrad_end
692 c        do j=jgrad_start(i),jgrad_end(i)
693 c          do k=1,3
694 c            gradbufc(k,i)=gradbufc(k,i)+gradbufc_sum(k,j)
695 c          enddo
696 c        enddo
697 c      enddo
698       do j=1,3
699         gradbufc(j,nres-1)=gradbufc_sum(j,nres)
700       enddo
701       do i=nres-2,nnt,-1
702         do j=1,3
703           gradbufc(j,i)=gradbufc(j,i+1)+gradbufc_sum(j,i+1)
704         enddo
705       enddo
706 #ifdef DEBUG
707       write (iout,*) "gradbufc after summing"
708       do i=1,nres
709         write (iout,'(i3,3f10.5)') i,(gradbufc(j,i),j=1,3)
710       enddo
711       call flush(iout)
712 #endif
713       else
714 #endif
715 #ifdef DEBUG
716       write (iout,*) "gradbufc"
717       do i=1,nres
718         write (iout,'(i3,3f10.5)') i,(gradbufc(j,i),j=1,3)
719       enddo
720       call flush(iout)
721 #endif
722       do i=1,nres
723         do j=1,3
724           gradbufc_sum(j,i)=gradbufc(j,i)
725           gradbufc(j,i)=0.0d0
726         enddo
727       enddo
728       do j=1,3
729         gradbufc(j,nres-1)=gradbufc_sum(j,nres)
730       enddo
731       do i=nres-2,nnt,-1
732         do j=1,3
733           gradbufc(j,i)=gradbufc(j,i+1)+gradbufc_sum(j,i+1)
734         enddo
735       enddo
736 c      do i=nnt,nres-1
737 c        do k=1,3
738 c          gradbufc(k,i)=0.0d0
739 c        enddo
740 c        do j=i+1,nres
741 c          do k=1,3
742 c            gradbufc(k,i)=gradbufc(k,i)+gradbufc(k,j)
743 c          enddo
744 c        enddo
745 c      enddo
746 #ifdef DEBUG
747       write (iout,*) "gradbufc after summing"
748       do i=1,nres
749         write (iout,'(i3,3f10.5)') i,(gradbufc(j,i),j=1,3)
750       enddo
751       call flush(iout)
752 #endif
753 #ifdef MPI
754       endif
755 #endif
756       do k=1,3
757         gradbufc(k,nres)=0.0d0
758       enddo
759       do i=1,nct
760         do j=1,3
761 #ifdef SPLITELE
762           gradc(j,i,icg)=gradbufc(j,i)+welec*gelc(j,i)+
763      &                wel_loc*gel_loc(j,i)+
764      &                0.5d0*(wscp*gvdwc_scpp(j,i)+
765      &                welec*gelc_long(j,i)+wvdwpp*gvdwpp(j,i)+
766      &                wel_loc*gel_loc_long(j,i)+
767      &                wcorr*gradcorr_long(j,i)+
768      &                wcorr5*gradcorr5_long(j,i)+
769      &                wcorr6*gradcorr6_long(j,i)+
770      &                wturn6*gcorr6_turn_long(j,i))+
771      &                wbond*gradb(j,i)+
772      &                wcorr*gradcorr(j,i)+
773      &                wturn3*gcorr3_turn(j,i)+
774      &                wturn4*gcorr4_turn(j,i)+
775      &                wcorr5*gradcorr5(j,i)+
776      &                wcorr6*gradcorr6(j,i)+
777      &                wturn6*gcorr6_turn(j,i)+
778      &                wsccor*gsccorc(j,i)
779      &               +wscloc*gscloc(j,i)
780 #else
781           gradc(j,i,icg)=gradbufc(j,i)+welec*gelc(j,i)+
782      &                wel_loc*gel_loc(j,i)+
783      &                0.5d0*(wscp*gvdwc_scpp(j,i)+
784      &                welec*gelc_long(j,i)+
785      &                wel_loc*gel_loc_long(j,i)+
786      &                wcorr*gcorr_long(j,i)+
787      &                wcorr5*gradcorr5_long(j,i)+
788      &                wcorr6*gradcorr6_long(j,i)+
789      &                wturn6*gcorr6_turn_long(j,i))+
790      &                wbond*gradb(j,i)+
791      &                wcorr*gradcorr(j,i)+
792      &                wturn3*gcorr3_turn(j,i)+
793      &                wturn4*gcorr4_turn(j,i)+
794      &                wcorr5*gradcorr5(j,i)+
795      &                wcorr6*gradcorr6(j,i)+
796      &                wturn6*gcorr6_turn(j,i)+
797      &                wsccor*gsccorc(j,i)
798      &               +wscloc*gscloc(j,i)
799 #endif
800 #ifdef TSCSC
801           gradx(j,i,icg)=wsc*gvdwx(j,i)+wsc*wscT*gvdwxT(j,i)+
802      &                  wscp*gradx_scp(j,i)+
803      &                  wbond*gradbx(j,i)+
804      &                  wstrain*ghpbx(j,i)+wcorr*gradxorr(j,i)+
805      &                  wsccor*gsccorx(j,i)
806      &                 +wscloc*gsclocx(j,i)
807 #else
808           gradx(j,i,icg)=wsc*gvdwx(j,i)+wscp*gradx_scp(j,i)+
809      &                  wbond*gradbx(j,i)+
810      &                  wstrain*ghpbx(j,i)+wcorr*gradxorr(j,i)+
811      &                  wsccor*gsccorx(j,i)
812      &                 +wscloc*gsclocx(j,i)
813 #endif
814         enddo
815       enddo 
816 #ifdef DEBUG
817       write (iout,*) "gloc before adding corr"
818       do i=1,4*nres
819         write (iout,*) i,gloc(i,icg)
820       enddo
821 #endif
822       do i=1,nres-3
823         gloc(i,icg)=gloc(i,icg)+wcorr*gcorr_loc(i)
824      &   +wcorr5*g_corr5_loc(i)
825      &   +wcorr6*g_corr6_loc(i)
826      &   +wturn4*gel_loc_turn4(i)
827      &   +wturn3*gel_loc_turn3(i)
828      &   +wturn6*gel_loc_turn6(i)
829      &   +wel_loc*gel_loc_loc(i)
830       enddo
831 #ifdef DEBUG
832       write (iout,*) "gloc after adding corr"
833       do i=1,4*nres
834         write (iout,*) i,gloc(i,icg)
835       enddo
836 #endif
837 #ifdef MPI
838       if (nfgtasks.gt.1) then
839         do j=1,3
840           do i=1,nres
841             gradbufc(j,i)=gradc(j,i,icg)
842             gradbufx(j,i)=gradx(j,i,icg)
843           enddo
844         enddo
845         do i=1,4*nres
846           glocbuf(i)=gloc(i,icg)
847         enddo
848 #ifdef DEBUG
849       write (iout,*) "gloc_sc before reduce"
850       do i=1,nres
851        do j=1,3
852         write (iout,*) i,j,gloc_sc(j,i,icg)
853        enddo
854       enddo
855 #endif
856         do i=1,nres
857          do j=1,3
858           gloc_scbuf(j,i)=gloc_sc(j,i,icg)
859          enddo
860         enddo
861         time00=MPI_Wtime()
862         call MPI_Barrier(FG_COMM,IERR)
863         time_barrier_g=time_barrier_g+MPI_Wtime()-time00
864         time00=MPI_Wtime()
865         call MPI_Reduce(gradbufc(1,1),gradc(1,1,icg),3*nres,
866      &    MPI_DOUBLE_PRECISION,MPI_SUM,king,FG_COMM,IERR)
867         call MPI_Reduce(gradbufx(1,1),gradx(1,1,icg),3*nres,
868      &    MPI_DOUBLE_PRECISION,MPI_SUM,king,FG_COMM,IERR)
869         call MPI_Reduce(glocbuf(1),gloc(1,icg),4*nres,
870      &    MPI_DOUBLE_PRECISION,MPI_SUM,king,FG_COMM,IERR)
871         call MPI_Reduce(gloc_scbuf(1,1),gloc_sc(1,1,icg),3*nres,
872      &    MPI_DOUBLE_PRECISION,MPI_SUM,king,FG_COMM,IERR)
873         time_reduce=time_reduce+MPI_Wtime()-time00
874 #ifdef DEBUG
875       write (iout,*) "gloc_sc after reduce"
876       do i=1,nres
877        do j=1,3
878         write (iout,*) i,j,gloc_sc(j,i,icg)
879        enddo
880       enddo
881 #endif
882 #ifdef DEBUG
883       write (iout,*) "gloc after reduce"
884       do i=1,4*nres
885         write (iout,*) i,gloc(i,icg)
886       enddo
887 #endif
888       endif
889 #endif
890       if (gnorm_check) then
891 c
892 c Compute the maximum elements of the gradient
893 c
894       gvdwc_max=0.0d0
895       gvdwc_scp_max=0.0d0
896       gelc_max=0.0d0
897       gvdwpp_max=0.0d0
898       gradb_max=0.0d0
899       ghpbc_max=0.0d0
900       gradcorr_max=0.0d0
901       gel_loc_max=0.0d0
902       gcorr3_turn_max=0.0d0
903       gcorr4_turn_max=0.0d0
904       gradcorr5_max=0.0d0
905       gradcorr6_max=0.0d0
906       gcorr6_turn_max=0.0d0
907       gsccorc_max=0.0d0
908       gscloc_max=0.0d0
909       gvdwx_max=0.0d0
910       gradx_scp_max=0.0d0
911       ghpbx_max=0.0d0
912       gradxorr_max=0.0d0
913       gsccorx_max=0.0d0
914       gsclocx_max=0.0d0
915       do i=1,nct
916         gvdwc_norm=dsqrt(scalar(gvdwc(1,i),gvdwc(1,i)))
917         if (gvdwc_norm.gt.gvdwc_max) gvdwc_max=gvdwc_norm
918 #ifdef TSCSC
919         gvdwc_norm=dsqrt(scalar(gvdwcT(1,i),gvdwcT(1,i)))
920         if (gvdwc_norm.gt.gvdwc_max) gvdwc_max=gvdwc_norm          
921 #endif
922         gvdwc_scp_norm=dsqrt(scalar(gvdwc_scp(1,i),gvdwc_scp(1,i)))
923         if (gvdwc_scp_norm.gt.gvdwc_scp_max) 
924      &   gvdwc_scp_max=gvdwc_scp_norm
925         gelc_norm=dsqrt(scalar(gelc(1,i),gelc(1,i)))
926         if (gelc_norm.gt.gelc_max) gelc_max=gelc_norm
927         gvdwpp_norm=dsqrt(scalar(gvdwpp(1,i),gvdwpp(1,i)))
928         if (gvdwpp_norm.gt.gvdwpp_max) gvdwpp_max=gvdwpp_norm
929         gradb_norm=dsqrt(scalar(gradb(1,i),gradb(1,i)))
930         if (gradb_norm.gt.gradb_max) gradb_max=gradb_norm
931         ghpbc_norm=dsqrt(scalar(ghpbc(1,i),ghpbc(1,i)))
932         if (ghpbc_norm.gt.ghpbc_max) ghpbc_max=ghpbc_norm
933         gradcorr_norm=dsqrt(scalar(gradcorr(1,i),gradcorr(1,i)))
934         if (gradcorr_norm.gt.gradcorr_max) gradcorr_max=gradcorr_norm
935         gel_loc_norm=dsqrt(scalar(gel_loc(1,i),gel_loc(1,i)))
936         if (gel_loc_norm.gt.gel_loc_max) gel_loc_max=gel_loc_norm
937         gcorr3_turn_norm=dsqrt(scalar(gcorr3_turn(1,i),
938      &    gcorr3_turn(1,i)))
939         if (gcorr3_turn_norm.gt.gcorr3_turn_max) 
940      &    gcorr3_turn_max=gcorr3_turn_norm
941         gcorr4_turn_norm=dsqrt(scalar(gcorr4_turn(1,i),
942      &    gcorr4_turn(1,i)))
943         if (gcorr4_turn_norm.gt.gcorr4_turn_max) 
944      &    gcorr4_turn_max=gcorr4_turn_norm
945         gradcorr5_norm=dsqrt(scalar(gradcorr5(1,i),gradcorr5(1,i)))
946         if (gradcorr5_norm.gt.gradcorr5_max) 
947      &    gradcorr5_max=gradcorr5_norm
948         gradcorr6_norm=dsqrt(scalar(gradcorr6(1,i),gradcorr6(1,i)))
949         if (gradcorr6_norm.gt.gradcorr6_max) gcorr6_max=gradcorr6_norm
950         gcorr6_turn_norm=dsqrt(scalar(gcorr6_turn(1,i),
951      &    gcorr6_turn(1,i)))
952         if (gcorr6_turn_norm.gt.gcorr6_turn_max) 
953      &    gcorr6_turn_max=gcorr6_turn_norm
954         gsccorr_norm=dsqrt(scalar(gsccorc(1,i),gsccorc(1,i)))
955         if (gsccorr_norm.gt.gsccorr_max) gsccorr_max=gsccorr_norm
956         gscloc_norm=dsqrt(scalar(gscloc(1,i),gscloc(1,i)))
957         if (gscloc_norm.gt.gscloc_max) gscloc_max=gscloc_norm
958         gvdwx_norm=dsqrt(scalar(gvdwx(1,i),gvdwx(1,i)))
959         if (gvdwx_norm.gt.gvdwx_max) gvdwx_max=gvdwx_norm
960 #ifdef TSCSC
961         gvdwx_norm=dsqrt(scalar(gvdwxT(1,i),gvdwxT(1,i)))
962         if (gvdwx_norm.gt.gvdwx_max) gvdwx_max=gvdwx_norm
963 #endif
964         gradx_scp_norm=dsqrt(scalar(gradx_scp(1,i),gradx_scp(1,i)))
965         if (gradx_scp_norm.gt.gradx_scp_max) 
966      &    gradx_scp_max=gradx_scp_norm
967         ghpbx_norm=dsqrt(scalar(ghpbx(1,i),ghpbx(1,i)))
968         if (ghpbx_norm.gt.ghpbx_max) ghpbx_max=ghpbx_norm
969         gradxorr_norm=dsqrt(scalar(gradxorr(1,i),gradxorr(1,i)))
970         if (gradxorr_norm.gt.gradxorr_max) gradxorr_max=gradxorr_norm
971         gsccorrx_norm=dsqrt(scalar(gsccorx(1,i),gsccorx(1,i)))
972         if (gsccorrx_norm.gt.gsccorrx_max) gsccorrx_max=gsccorrx_norm
973         gsclocx_norm=dsqrt(scalar(gsclocx(1,i),gsclocx(1,i)))
974         if (gsclocx_norm.gt.gsclocx_max) gsclocx_max=gsclocx_norm
975       enddo 
976       if (gradout) then
977 #ifdef AIX
978         open(istat,file=statname,position="append")
979 #else
980         open(istat,file=statname,access="append")
981 #endif
982         write (istat,'(1h#,21f10.2)') gvdwc_max,gvdwc_scp_max,
983      &     gelc_max,gvdwpp_max,gradb_max,ghpbc_max,
984      &     gradcorr_max,gel_loc_max,gcorr3_turn_max,gcorr4_turn_max,
985      &     gradcorr5_max,gradcorr6_max,gcorr6_turn_max,gsccorc_max,
986      &     gscloc_max,gvdwx_max,gradx_scp_max,ghpbx_max,gradxorr_max,
987      &     gsccorx_max,gsclocx_max
988         close(istat)
989         if (gvdwc_max.gt.1.0d4) then
990           write (iout,*) "gvdwc gvdwx gradb gradbx"
991           do i=nnt,nct
992             write(iout,'(i5,4(3f10.2,5x))') i,(gvdwc(j,i),gvdwx(j,i),
993      &        gradb(j,i),gradbx(j,i),j=1,3)
994           enddo
995           call pdbout(0.0d0,'cipiszcze',iout)
996           call flush(iout)
997         endif
998       endif
999       endif
1000 #ifdef DEBUG
1001       write (iout,*) "gradc gradx gloc"
1002       do i=1,nres
1003         write (iout,'(i5,3f10.5,5x,3f10.5,5x,f10.5)') 
1004      &   i,(gradc(j,i,icg),j=1,3),(gradx(j,i,icg),j=1,3),gloc(i,icg)
1005       enddo 
1006 #endif
1007 #ifdef TIMING
1008 #ifdef MPI
1009       time_sumgradient=time_sumgradient+MPI_Wtime()-time01
1010 #else
1011       time_sumgradient=time_sumgradient+tcpu()-time01
1012 #endif
1013 #endif
1014       return
1015       end
1016 c-------------------------------------------------------------------------------
1017       subroutine rescale_weights(t_bath)
1018       implicit real*8 (a-h,o-z)
1019       include 'DIMENSIONS'
1020       include 'COMMON.IOUNITS'
1021       include 'COMMON.FFIELD'
1022       include 'COMMON.SBRIDGE'
1023       double precision kfac /2.4d0/
1024       double precision x,x2,x3,x4,x5,licznik /1.12692801104297249644/
1025 c      facT=temp0/t_bath
1026 c      facT=2*temp0/(t_bath+temp0)
1027       if (rescale_mode.eq.0) then
1028         facT=1.0d0
1029         facT2=1.0d0
1030         facT3=1.0d0
1031         facT4=1.0d0
1032         facT5=1.0d0
1033       else if (rescale_mode.eq.1) then
1034         facT=kfac/(kfac-1.0d0+t_bath/temp0)
1035         facT2=kfac**2/(kfac**2-1.0d0+(t_bath/temp0)**2)
1036         facT3=kfac**3/(kfac**3-1.0d0+(t_bath/temp0)**3)
1037         facT4=kfac**4/(kfac**4-1.0d0+(t_bath/temp0)**4)
1038         facT5=kfac**5/(kfac**5-1.0d0+(t_bath/temp0)**5)
1039       else if (rescale_mode.eq.2) then
1040         x=t_bath/temp0
1041         x2=x*x
1042         x3=x2*x
1043         x4=x3*x
1044         x5=x4*x
1045         facT=licznik/dlog(dexp(x)+dexp(-x))
1046         facT2=licznik/dlog(dexp(x2)+dexp(-x2))
1047         facT3=licznik/dlog(dexp(x3)+dexp(-x3))
1048         facT4=licznik/dlog(dexp(x4)+dexp(-x4))
1049         facT5=licznik/dlog(dexp(x5)+dexp(-x5))
1050       else
1051         write (iout,*) "Wrong RESCALE_MODE",rescale_mode
1052         write (*,*) "Wrong RESCALE_MODE",rescale_mode
1053 #ifdef MPI
1054        call MPI_Finalize(MPI_COMM_WORLD,IERROR)
1055 #endif
1056        stop 555
1057       endif
1058       welec=weights(3)*fact
1059       wcorr=weights(4)*fact3
1060       wcorr5=weights(5)*fact4
1061       wcorr6=weights(6)*fact5
1062       wel_loc=weights(7)*fact2
1063       wturn3=weights(8)*fact2
1064       wturn4=weights(9)*fact3
1065       wturn6=weights(10)*fact5
1066       wtor=weights(13)*fact
1067       wtor_d=weights(14)*fact2
1068       wsccor=weights(21)*fact
1069 #ifdef TSCSC
1070 c      wsct=t_bath/temp0
1071       wsct=(320.0+80.0*dtanh((t_bath-320.0)/80.0))/320.0
1072 #endif
1073       return
1074       end
1075 C------------------------------------------------------------------------
1076       subroutine enerprint(energia)
1077       implicit real*8 (a-h,o-z)
1078       include 'DIMENSIONS'
1079       include 'COMMON.IOUNITS'
1080       include 'COMMON.FFIELD'
1081       include 'COMMON.SBRIDGE'
1082       include 'COMMON.MD'
1083       double precision energia(0:n_ene)
1084       etot=energia(0)
1085 #ifdef TSCSC
1086       evdw=energia(22)+wsct*energia(23)
1087 #else
1088       evdw=energia(1)
1089 #endif
1090       evdw2=energia(2)
1091 #ifdef SCP14
1092       evdw2=energia(2)+energia(18)
1093 #else
1094       evdw2=energia(2)
1095 #endif
1096       ees=energia(3)
1097 #ifdef SPLITELE
1098       evdw1=energia(16)
1099 #endif
1100       ecorr=energia(4)
1101       ecorr5=energia(5)
1102       ecorr6=energia(6)
1103       eel_loc=energia(7)
1104       eello_turn3=energia(8)
1105       eello_turn4=energia(9)
1106       eello_turn6=energia(10)
1107       ebe=energia(11)
1108       escloc=energia(12)
1109       etors=energia(13)
1110       etors_d=energia(14)
1111       ehpb=energia(15)
1112       edihcnstr=energia(19)
1113       estr=energia(17)
1114       Uconst=energia(20)
1115       esccor=energia(21)
1116       ehomology_constr=energia(24)
1117 C     Bartek
1118       edfadis = energia(25)
1119       edfator = energia(26)
1120       edfanei = energia(27)
1121       edfabet = energia(28)
1122
1123 #ifdef SPLITELE
1124       write (iout,10) evdw,wsc,evdw2,wscp,ees,welec,evdw1,wvdwpp,
1125      &  estr,wbond,ebe,wang,
1126      &  escloc,wscloc,etors,wtor,etors_d,wtor_d,ehpb,wstrain,
1127      &  ecorr,wcorr,
1128      &  ecorr5,wcorr5,ecorr6,wcorr6,eel_loc,wel_loc,eello_turn3,wturn3,
1129      &  eello_turn4,wturn4,eello_turn6,wturn6,esccor,wsccor,
1130      &  edihcnstr,ehomology_constr, ebr*nss,
1131      &  Uconst,edfadis,wdfa_dist,edfator,wdfa_tor,edfanei,wdfa_nei,
1132      &  edfabet,wdfa_beta,etot
1133    10 format (/'Virtual-chain energies:'//
1134      & 'EVDW=  ',1pE16.6,' WEIGHT=',1pE16.6,' (SC-SC)'/
1135      & 'EVDW2= ',1pE16.6,' WEIGHT=',1pE16.6,' (SC-p)'/
1136      & 'EES=   ',1pE16.6,' WEIGHT=',1pE16.6,' (p-p)'/
1137      & 'EVDWPP=',1pE16.6,' WEIGHT=',1pE16.6,' (p-p VDW)'/
1138      & 'ESTR=  ',1pE16.6,' WEIGHT=',1pE16.6,' (stretching)'/
1139      & 'EBE=   ',1pE16.6,' WEIGHT=',1pE16.6,' (bending)'/
1140      & 'ESC=   ',1pE16.6,' WEIGHT=',1pE16.6,' (SC local)'/
1141      & 'ETORS= ',1pE16.6,' WEIGHT=',1pE16.6,' (torsional)'/
1142      & 'ETORSD=',1pE16.6,' WEIGHT=',1pE16.6,' (double torsional)'/
1143      & 'EHPB=  ',1pE16.6,' WEIGHT=',1pE16.6,
1144      & ' (SS bridges & dist. cnstr.)'/
1145      & 'ECORR4=',1pE16.6,' WEIGHT=',1pE16.6,' (multi-body)'/
1146      & 'ECORR5=',1pE16.6,' WEIGHT=',1pE16.6,' (multi-body)'/
1147      & 'ECORR6=',1pE16.6,' WEIGHT=',1pE16.6,' (multi-body)'/
1148      & 'EELLO= ',1pE16.6,' WEIGHT=',1pE16.6,' (electrostatic-local)'/
1149      & 'ETURN3=',1pE16.6,' WEIGHT=',1pE16.6,' (turns, 3rd order)'/
1150      & 'ETURN4=',1pE16.6,' WEIGHT=',1pE16.6,' (turns, 4th order)'/
1151      & 'ETURN6=',1pE16.6,' WEIGHT=',1pE16.6,' (turns, 6th order)'/
1152      & 'ESCCOR=',1pE16.6,' WEIGHT=',1pE16.6,' (backbone-rotamer corr)'/
1153      & 'EDIHC= ',1pE16.6,' (dihedral angle constraints)'/
1154      & 'H_CONS=',1pE16.6,' (Homology model constraints energy)'/
1155      & 'ESS=   ',1pE16.6,' (disulfide-bridge intrinsic energy)'/
1156      & 'UCONST= ',1pE16.6,' (Constraint energy)'/ 
1157      & 'EDFAD= ',1pE16.6,' WEIGHT=',1pE16.6,' (DFA distance energy)'/ 
1158      & 'EDFAT= ',1pE16.6,' WEIGHT=',1pE16.6,' (DFA torsion energy)'/ 
1159      & 'EDFAN= ',1pE16.6,' WEIGHT=',1pE16.6,' (DFA NCa energy)'/ 
1160      & 'EDFAB= ',1pE16.6,' WEIGHT=',1pE16.6,' (DFA Beta energy)'/ 
1161      & 'ETOT=  ',1pE16.6,' (total)')
1162 #else
1163       write (iout,10) evdw,wsc,evdw2,wscp,ees,welec,
1164      &  estr,wbond,ebe,wang,
1165      &  escloc,wscloc,etors,wtor,etors_d,wtor_d,ehpb,wstrain,
1166      &  ecorr,wcorr,
1167      &  ecorr5,wcorr5,ecorr6,wcorr6,eel_loc,wel_loc,eello_turn3,wturn3,
1168      &  eello_turn4,wturn4,eello_turn6,wturn6,esccor,wsccro,edihcnstr,
1169      &  ehomology_constr,ebr*nss,Uconst,edfadis,wdfa_dist,edfator,
1170      &  wdfa_tor,edfanei,wdfa_nei,edfabet,wdfa_beta,
1171      &  etot
1172    10 format (/'Virtual-chain energies:'//
1173      & 'EVDW=  ',1pE16.6,' WEIGHT=',1pD16.6,' (SC-SC)'/
1174      & 'EVDW2= ',1pE16.6,' WEIGHT=',1pD16.6,' (SC-p)'/
1175      & 'EES=   ',1pE16.6,' WEIGHT=',1pD16.6,' (p-p)'/
1176      & 'ESTR=  ',1pE16.6,' WEIGHT=',1pD16.6,' (stretching)'/
1177      & 'EBE=   ',1pE16.6,' WEIGHT=',1pD16.6,' (bending)'/
1178      & 'ESC=   ',1pE16.6,' WEIGHT=',1pD16.6,' (SC local)'/
1179      & 'ETORS= ',1pE16.6,' WEIGHT=',1pD16.6,' (torsional)'/
1180      & 'ETORSD=',1pE16.6,' WEIGHT=',1pD16.6,' (double torsional)'/
1181      & 'EHBP=  ',1pE16.6,' WEIGHT=',1pD16.6,
1182      & ' (SS bridges & dist. cnstr.)'/
1183      & 'ECORR4=',1pE16.6,' WEIGHT=',1pD16.6,' (multi-body)'/
1184      & 'ECORR5=',1pE16.6,' WEIGHT=',1pD16.6,' (multi-body)'/
1185      & 'ECORR6=',1pE16.6,' WEIGHT=',1pD16.6,' (multi-body)'/
1186      & 'EELLO= ',1pE16.6,' WEIGHT=',1pD16.6,' (electrostatic-local)'/
1187      & 'ETURN3=',1pE16.6,' WEIGHT=',1pD16.6,' (turns, 3rd order)'/
1188      & 'ETURN4=',1pE16.6,' WEIGHT=',1pD16.6,' (turns, 4th order)'/
1189      & 'ETURN6=',1pE16.6,' WEIGHT=',1pD16.6,' (turns, 6th order)'/
1190      & 'ESCCOR=',1pE16.6,' WEIGHT=',1pD16.6,' (backbone-rotamer corr)'/
1191      & 'EDIHC= ',1pE16.6,' (dihedral angle constraints)'/
1192      & 'H_CONS=',1pE16.6,' (Homology model constraints energy)'/
1193      & 'ESS=   ',1pE16.6,' (disulfide-bridge intrinsic energy)'/
1194      & 'UCONST=',1pE16.6,' (Constraint energy)'/ 
1195      & 'EDFAD= ',1pE16.6,' WEIGHT=',1pD16.6,' (DFA distance energy)'/ 
1196      & 'EDFAT= ',1pE16.6,' WEIGHT=',1pD16.6,' (DFA torsion energy)'/ 
1197      & 'EDFAN= ',1pE16.6,' WEIGHT=',1pD16.6,' (DFA NCa energy)'/ 
1198      & 'EDFAB= ',1pE16.6,' WEIGHT=',1pD16.6,' (DFA Beta energy)'/ 
1199      & 'ETOT=  ',1pE16.6,' (total)')
1200 #endif
1201       return
1202       end
1203 C-----------------------------------------------------------------------
1204       subroutine elj(evdw,evdw_p,evdw_m)
1205 C
1206 C This subroutine calculates the interaction energy of nonbonded side chains
1207 C assuming the LJ potential of interaction.
1208 C
1209       implicit real*8 (a-h,o-z)
1210       include 'DIMENSIONS'
1211       parameter (accur=1.0d-10)
1212       include 'COMMON.GEO'
1213       include 'COMMON.VAR'
1214       include 'COMMON.LOCAL'
1215       include 'COMMON.CHAIN'
1216       include 'COMMON.DERIV'
1217       include 'COMMON.INTERACT'
1218       include 'COMMON.TORSION'
1219       include 'COMMON.SBRIDGE'
1220       include 'COMMON.NAMES'
1221       include 'COMMON.IOUNITS'
1222       include 'COMMON.CONTACTS'
1223       dimension gg(3)
1224 c      write(iout,*)'Entering ELJ nnt=',nnt,' nct=',nct,' expon=',expon
1225       evdw=0.0D0
1226       do i=iatsc_s,iatsc_e
1227         itypi=itype(i)
1228         itypi1=itype(i+1)
1229         xi=c(1,nres+i)
1230         yi=c(2,nres+i)
1231         zi=c(3,nres+i)
1232 C Change 12/1/95
1233         num_conti=0
1234 C
1235 C Calculate SC interaction energy.
1236 C
1237         do iint=1,nint_gr(i)
1238 cd        write (iout,*) 'i=',i,' iint=',iint,' istart=',istart(i,iint),
1239 cd   &                  'iend=',iend(i,iint)
1240           do j=istart(i,iint),iend(i,iint)
1241             itypj=itype(j)
1242             xj=c(1,nres+j)-xi
1243             yj=c(2,nres+j)-yi
1244             zj=c(3,nres+j)-zi
1245 C Change 12/1/95 to calculate four-body interactions
1246             rij=xj*xj+yj*yj+zj*zj
1247             rrij=1.0D0/rij
1248 c           write (iout,*)'i=',i,' j=',j,' itypi=',itypi,' itypj=',itypj
1249             eps0ij=eps(itypi,itypj)
1250             fac=rrij**expon2
1251             e1=fac*fac*aa(itypi,itypj)
1252             e2=fac*bb(itypi,itypj)
1253             evdwij=e1+e2
1254 cd          sigm=dabs(aa(itypi,itypj)/bb(itypi,itypj))**(1.0D0/6.0D0)
1255 cd          epsi=bb(itypi,itypj)**2/aa(itypi,itypj)
1256 cd          write (iout,'(2(a3,i3,2x),6(1pd12.4)/2(3(1pd12.4),5x)/)')
1257 cd   &        restyp(itypi),i,restyp(itypj),j,aa(itypi,itypj),
1258 cd   &        bb(itypi,itypj),1.0D0/dsqrt(rrij),evdwij,epsi,sigm,
1259 cd   &        (c(k,i),k=1,3),(c(k,j),k=1,3)
1260 #ifdef TSCSC
1261             if (bb(itypi,itypj).gt.0) then
1262                evdw_p=evdw_p+evdwij
1263             else
1264                evdw_m=evdw_m+evdwij
1265             endif
1266 #else
1267             evdw=evdw+evdwij
1268 #endif
1269
1270 C Calculate the components of the gradient in DC and X
1271 C
1272             fac=-rrij*(e1+evdwij)
1273             gg(1)=xj*fac
1274             gg(2)=yj*fac
1275             gg(3)=zj*fac
1276 #ifdef TSCSC
1277             if (bb(itypi,itypj).gt.0.0d0) then
1278               do k=1,3
1279                 gvdwx(k,i)=gvdwx(k,i)-gg(k)
1280                 gvdwx(k,j)=gvdwx(k,j)+gg(k)
1281                 gvdwc(k,i)=gvdwc(k,i)-gg(k)
1282                 gvdwc(k,j)=gvdwc(k,j)+gg(k)
1283               enddo
1284             else
1285               do k=1,3
1286                 gvdwxT(k,i)=gvdwxT(k,i)-gg(k)
1287                 gvdwxT(k,j)=gvdwxT(k,j)+gg(k)
1288                 gvdwcT(k,i)=gvdwcT(k,i)-gg(k)
1289                 gvdwcT(k,j)=gvdwcT(k,j)+gg(k)
1290               enddo
1291             endif
1292 #else
1293             do k=1,3
1294               gvdwx(k,i)=gvdwx(k,i)-gg(k)
1295               gvdwx(k,j)=gvdwx(k,j)+gg(k)
1296               gvdwc(k,i)=gvdwc(k,i)-gg(k)
1297               gvdwc(k,j)=gvdwc(k,j)+gg(k)
1298             enddo
1299 #endif
1300 cgrad            do k=i,j-1
1301 cgrad              do l=1,3
1302 cgrad                gvdwc(l,k)=gvdwc(l,k)+gg(l)
1303 cgrad              enddo
1304 cgrad            enddo
1305 C
1306 C 12/1/95, revised on 5/20/97
1307 C
1308 C Calculate the contact function. The ith column of the array JCONT will 
1309 C contain the numbers of atoms that make contacts with the atom I (of numbers
1310 C greater than I). The arrays FACONT and GACONT will contain the values of
1311 C the contact function and its derivative.
1312 C
1313 C Uncomment next line, if the correlation interactions include EVDW explicitly.
1314 c           if (j.gt.i+1 .and. evdwij.le.0.0D0) then
1315 C Uncomment next line, if the correlation interactions are contact function only
1316             if (j.gt.i+1.and. eps0ij.gt.0.0D0) then
1317               rij=dsqrt(rij)
1318               sigij=sigma(itypi,itypj)
1319               r0ij=rs0(itypi,itypj)
1320 C
1321 C Check whether the SC's are not too far to make a contact.
1322 C
1323               rcut=1.5d0*r0ij
1324               call gcont(rij,rcut,1.0d0,0.2d0*rcut,fcont,fprimcont)
1325 C Add a new contact, if the SC's are close enough, but not too close (r<sigma).
1326 C
1327               if (fcont.gt.0.0D0) then
1328 C If the SC-SC distance if close to sigma, apply spline.
1329 cAdam           call gcont(-rij,-1.03d0*sigij,2.0d0*sigij,1.0d0,
1330 cAdam &             fcont1,fprimcont1)
1331 cAdam           fcont1=1.0d0-fcont1
1332 cAdam           if (fcont1.gt.0.0d0) then
1333 cAdam             fprimcont=fprimcont*fcont1+fcont*fprimcont1
1334 cAdam             fcont=fcont*fcont1
1335 cAdam           endif
1336 C Uncomment following 4 lines to have the geometric average of the epsilon0's
1337 cga             eps0ij=1.0d0/dsqrt(eps0ij)
1338 cga             do k=1,3
1339 cga               gg(k)=gg(k)*eps0ij
1340 cga             enddo
1341 cga             eps0ij=-evdwij*eps0ij
1342 C Uncomment for AL's type of SC correlation interactions.
1343 cadam           eps0ij=-evdwij
1344                 num_conti=num_conti+1
1345                 jcont(num_conti,i)=j
1346                 facont(num_conti,i)=fcont*eps0ij
1347                 fprimcont=eps0ij*fprimcont/rij
1348                 fcont=expon*fcont
1349 cAdam           gacont(1,num_conti,i)=-fprimcont*xj+fcont*gg(1)
1350 cAdam           gacont(2,num_conti,i)=-fprimcont*yj+fcont*gg(2)
1351 cAdam           gacont(3,num_conti,i)=-fprimcont*zj+fcont*gg(3)
1352 C Uncomment following 3 lines for Skolnick's type of SC correlation.
1353                 gacont(1,num_conti,i)=-fprimcont*xj
1354                 gacont(2,num_conti,i)=-fprimcont*yj
1355                 gacont(3,num_conti,i)=-fprimcont*zj
1356 cd              write (iout,'(2i5,2f10.5)') i,j,rij,facont(num_conti,i)
1357 cd              write (iout,'(2i3,3f10.5)') 
1358 cd   &           i,j,(gacont(kk,num_conti,i),kk=1,3)
1359               endif
1360             endif
1361           enddo      ! j
1362         enddo        ! iint
1363 C Change 12/1/95
1364         num_cont(i)=num_conti
1365       enddo          ! i
1366       do i=1,nct
1367         do j=1,3
1368           gvdwc(j,i)=expon*gvdwc(j,i)
1369           gvdwx(j,i)=expon*gvdwx(j,i)
1370         enddo
1371       enddo
1372 C******************************************************************************
1373 C
1374 C                              N O T E !!!
1375 C
1376 C To save time, the factor of EXPON has been extracted from ALL components
1377 C of GVDWC and GRADX. Remember to multiply them by this factor before further 
1378 C use!
1379 C
1380 C******************************************************************************
1381       return
1382       end
1383 C-----------------------------------------------------------------------------
1384       subroutine eljk(evdw,evdw_p,evdw_m)
1385 C
1386 C This subroutine calculates the interaction energy of nonbonded side chains
1387 C assuming the LJK potential of interaction.
1388 C
1389       implicit real*8 (a-h,o-z)
1390       include 'DIMENSIONS'
1391       include 'COMMON.GEO'
1392       include 'COMMON.VAR'
1393       include 'COMMON.LOCAL'
1394       include 'COMMON.CHAIN'
1395       include 'COMMON.DERIV'
1396       include 'COMMON.INTERACT'
1397       include 'COMMON.IOUNITS'
1398       include 'COMMON.NAMES'
1399       dimension gg(3)
1400       logical scheck
1401 c     print *,'Entering ELJK nnt=',nnt,' nct=',nct,' expon=',expon
1402       evdw=0.0D0
1403       do i=iatsc_s,iatsc_e
1404         itypi=itype(i)
1405         itypi1=itype(i+1)
1406         xi=c(1,nres+i)
1407         yi=c(2,nres+i)
1408         zi=c(3,nres+i)
1409 C
1410 C Calculate SC interaction energy.
1411 C
1412         do iint=1,nint_gr(i)
1413           do j=istart(i,iint),iend(i,iint)
1414             itypj=itype(j)
1415             xj=c(1,nres+j)-xi
1416             yj=c(2,nres+j)-yi
1417             zj=c(3,nres+j)-zi
1418             rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
1419             fac_augm=rrij**expon
1420             e_augm=augm(itypi,itypj)*fac_augm
1421             r_inv_ij=dsqrt(rrij)
1422             rij=1.0D0/r_inv_ij 
1423             r_shift_inv=1.0D0/(rij+r0(itypi,itypj)-sigma(itypi,itypj))
1424             fac=r_shift_inv**expon
1425             e1=fac*fac*aa(itypi,itypj)
1426             e2=fac*bb(itypi,itypj)
1427             evdwij=e_augm+e1+e2
1428 cd          sigm=dabs(aa(itypi,itypj)/bb(itypi,itypj))**(1.0D0/6.0D0)
1429 cd          epsi=bb(itypi,itypj)**2/aa(itypi,itypj)
1430 cd          write (iout,'(2(a3,i3,2x),8(1pd12.4)/2(3(1pd12.4),5x)/)')
1431 cd   &        restyp(itypi),i,restyp(itypj),j,aa(itypi,itypj),
1432 cd   &        bb(itypi,itypj),augm(itypi,itypj),epsi,sigm,
1433 cd   &        sigma(itypi,itypj),1.0D0/dsqrt(rrij),evdwij,
1434 cd   &        (c(k,i),k=1,3),(c(k,j),k=1,3)
1435 #ifdef TSCSC
1436             if (bb(itypi,itypj).gt.0) then
1437                evdw_p=evdw_p+evdwij
1438             else
1439                evdw_m=evdw_m+evdwij
1440             endif
1441 #else
1442             evdw=evdw+evdwij
1443 #endif
1444
1445 C Calculate the components of the gradient in DC and X
1446 C
1447             fac=-2.0D0*rrij*e_augm-r_inv_ij*r_shift_inv*(e1+e1+e2)
1448             gg(1)=xj*fac
1449             gg(2)=yj*fac
1450             gg(3)=zj*fac
1451 #ifdef TSCSC
1452             if (bb(itypi,itypj).gt.0.0d0) then
1453               do k=1,3
1454                 gvdwx(k,i)=gvdwx(k,i)-gg(k)
1455                 gvdwx(k,j)=gvdwx(k,j)+gg(k)
1456                 gvdwc(k,i)=gvdwc(k,i)-gg(k)
1457                 gvdwc(k,j)=gvdwc(k,j)+gg(k)
1458               enddo
1459             else
1460               do k=1,3
1461                 gvdwxT(k,i)=gvdwxT(k,i)-gg(k)
1462                 gvdwxT(k,j)=gvdwxT(k,j)+gg(k)
1463                 gvdwcT(k,i)=gvdwcT(k,i)-gg(k)
1464                 gvdwcT(k,j)=gvdwcT(k,j)+gg(k)
1465               enddo
1466             endif
1467 #else
1468             do k=1,3
1469               gvdwx(k,i)=gvdwx(k,i)-gg(k)
1470               gvdwx(k,j)=gvdwx(k,j)+gg(k)
1471               gvdwc(k,i)=gvdwc(k,i)-gg(k)
1472               gvdwc(k,j)=gvdwc(k,j)+gg(k)
1473             enddo
1474 #endif
1475 cgrad            do k=i,j-1
1476 cgrad              do l=1,3
1477 cgrad                gvdwc(l,k)=gvdwc(l,k)+gg(l)
1478 cgrad              enddo
1479 cgrad            enddo
1480           enddo      ! j
1481         enddo        ! iint
1482       enddo          ! i
1483       do i=1,nct
1484         do j=1,3
1485           gvdwc(j,i)=expon*gvdwc(j,i)
1486           gvdwx(j,i)=expon*gvdwx(j,i)
1487         enddo
1488       enddo
1489       return
1490       end
1491 C-----------------------------------------------------------------------------
1492       subroutine ebp(evdw,evdw_p,evdw_m)
1493 C
1494 C This subroutine calculates the interaction energy of nonbonded side chains
1495 C assuming the Berne-Pechukas potential of interaction.
1496 C
1497       implicit real*8 (a-h,o-z)
1498       include 'DIMENSIONS'
1499       include 'COMMON.GEO'
1500       include 'COMMON.VAR'
1501       include 'COMMON.LOCAL'
1502       include 'COMMON.CHAIN'
1503       include 'COMMON.DERIV'
1504       include 'COMMON.NAMES'
1505       include 'COMMON.INTERACT'
1506       include 'COMMON.IOUNITS'
1507       include 'COMMON.CALC'
1508       common /srutu/ icall
1509 c     double precision rrsave(maxdim)
1510       logical lprn
1511       evdw=0.0D0
1512 c     print *,'Entering EBP nnt=',nnt,' nct=',nct,' expon=',expon
1513       evdw=0.0D0
1514 c     if (icall.eq.0) then
1515 c       lprn=.true.
1516 c     else
1517         lprn=.false.
1518 c     endif
1519       ind=0
1520       do i=iatsc_s,iatsc_e
1521         itypi=itype(i)
1522         itypi1=itype(i+1)
1523         xi=c(1,nres+i)
1524         yi=c(2,nres+i)
1525         zi=c(3,nres+i)
1526         dxi=dc_norm(1,nres+i)
1527         dyi=dc_norm(2,nres+i)
1528         dzi=dc_norm(3,nres+i)
1529 c        dsci_inv=dsc_inv(itypi)
1530         dsci_inv=vbld_inv(i+nres)
1531 C
1532 C Calculate SC interaction energy.
1533 C
1534         do iint=1,nint_gr(i)
1535           do j=istart(i,iint),iend(i,iint)
1536             ind=ind+1
1537             itypj=itype(j)
1538 c            dscj_inv=dsc_inv(itypj)
1539             dscj_inv=vbld_inv(j+nres)
1540             chi1=chi(itypi,itypj)
1541             chi2=chi(itypj,itypi)
1542             chi12=chi1*chi2
1543             chip1=chip(itypi)
1544             chip2=chip(itypj)
1545             chip12=chip1*chip2
1546             alf1=alp(itypi)
1547             alf2=alp(itypj)
1548             alf12=0.5D0*(alf1+alf2)
1549 C For diagnostics only!!!
1550 c           chi1=0.0D0
1551 c           chi2=0.0D0
1552 c           chi12=0.0D0
1553 c           chip1=0.0D0
1554 c           chip2=0.0D0
1555 c           chip12=0.0D0
1556 c           alf1=0.0D0
1557 c           alf2=0.0D0
1558 c           alf12=0.0D0
1559             xj=c(1,nres+j)-xi
1560             yj=c(2,nres+j)-yi
1561             zj=c(3,nres+j)-zi
1562             dxj=dc_norm(1,nres+j)
1563             dyj=dc_norm(2,nres+j)
1564             dzj=dc_norm(3,nres+j)
1565             rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
1566 cd          if (icall.eq.0) then
1567 cd            rrsave(ind)=rrij
1568 cd          else
1569 cd            rrij=rrsave(ind)
1570 cd          endif
1571             rij=dsqrt(rrij)
1572 C Calculate the angle-dependent terms of energy & contributions to derivatives.
1573             call sc_angular
1574 C Calculate whole angle-dependent part of epsilon and contributions
1575 C to its derivatives
1576             fac=(rrij*sigsq)**expon2
1577             e1=fac*fac*aa(itypi,itypj)
1578             e2=fac*bb(itypi,itypj)
1579             evdwij=eps1*eps2rt*eps3rt*(e1+e2)
1580             eps2der=evdwij*eps3rt
1581             eps3der=evdwij*eps2rt
1582             evdwij=evdwij*eps2rt*eps3rt
1583 #ifdef TSCSC
1584             if (bb(itypi,itypj).gt.0) then
1585                evdw_p=evdw_p+evdwij
1586             else
1587                evdw_m=evdw_m+evdwij
1588             endif
1589 #else
1590             evdw=evdw+evdwij
1591 #endif
1592             if (lprn) then
1593             sigm=dabs(aa(itypi,itypj)/bb(itypi,itypj))**(1.0D0/6.0D0)
1594             epsi=bb(itypi,itypj)**2/aa(itypi,itypj)
1595 cd            write (iout,'(2(a3,i3,2x),15(0pf7.3))')
1596 cd     &        restyp(itypi),i,restyp(itypj),j,
1597 cd     &        epsi,sigm,chi1,chi2,chip1,chip2,
1598 cd     &        eps1,eps2rt**2,eps3rt**2,1.0D0/dsqrt(sigsq),
1599 cd     &        om1,om2,om12,1.0D0/dsqrt(rrij),
1600 cd     &        evdwij
1601             endif
1602 C Calculate gradient components.
1603             e1=e1*eps1*eps2rt**2*eps3rt**2
1604             fac=-expon*(e1+evdwij)
1605             sigder=fac/sigsq
1606             fac=rrij*fac
1607 C Calculate radial part of the gradient
1608             gg(1)=xj*fac
1609             gg(2)=yj*fac
1610             gg(3)=zj*fac
1611 C Calculate the angular part of the gradient and sum add the contributions
1612 C to the appropriate components of the Cartesian gradient.
1613 #ifdef TSCSC
1614             if (bb(itypi,itypj).gt.0) then
1615                call sc_grad
1616             else
1617                call sc_grad_T
1618             endif
1619 #else
1620             call sc_grad
1621 #endif
1622           enddo      ! j
1623         enddo        ! iint
1624       enddo          ! i
1625 c     stop
1626       return
1627       end
1628 C-----------------------------------------------------------------------------
1629       subroutine egb(evdw,evdw_p,evdw_m)
1630 C
1631 C This subroutine calculates the interaction energy of nonbonded side chains
1632 C assuming the Gay-Berne potential of interaction.
1633 C
1634       implicit real*8 (a-h,o-z)
1635       include 'DIMENSIONS'
1636       include 'COMMON.GEO'
1637       include 'COMMON.VAR'
1638       include 'COMMON.LOCAL'
1639       include 'COMMON.CHAIN'
1640       include 'COMMON.DERIV'
1641       include 'COMMON.NAMES'
1642       include 'COMMON.INTERACT'
1643       include 'COMMON.IOUNITS'
1644       include 'COMMON.CALC'
1645       include 'COMMON.CONTROL'
1646       include 'COMMON.SBRIDGE'
1647       logical lprn
1648       evdw=0.0D0
1649 ccccc      energy_dec=.false.
1650 c     print *,'Entering EGB nnt=',nnt,' nct=',nct,' expon=',expon
1651       evdw=0.0D0
1652       evdw_p=0.0D0
1653       evdw_m=0.0D0
1654       lprn=.false.
1655 c     if (icall.eq.0) lprn=.false.
1656       ind=0
1657       do i=iatsc_s,iatsc_e
1658         itypi=itype(i)
1659         itypi1=itype(i+1)
1660         xi=c(1,nres+i)
1661         yi=c(2,nres+i)
1662         zi=c(3,nres+i)
1663         dxi=dc_norm(1,nres+i)
1664         dyi=dc_norm(2,nres+i)
1665         dzi=dc_norm(3,nres+i)
1666 c        dsci_inv=dsc_inv(itypi)
1667         dsci_inv=vbld_inv(i+nres)
1668 c        write (iout,*) "i",i,dsc_inv(itypi),dsci_inv,1.0d0/vbld(i+nres)
1669 c        write (iout,*) "dcnori",dxi*dxi+dyi*dyi+dzi*dzi
1670 C
1671 C Calculate SC interaction energy.
1672 C
1673         do iint=1,nint_gr(i)
1674           do j=istart(i,iint),iend(i,iint)
1675             IF (dyn_ss_mask(i).and.dyn_ss_mask(j)) THEN
1676               call dyn_ssbond_ene(i,j,evdwij)
1677               evdw=evdw+evdwij
1678               if (energy_dec) write (iout,'(a6,2i5,0pf7.3,a3)') 
1679      &                        'evdw',i,j,evdwij,' ss'
1680             ELSE
1681             ind=ind+1
1682             itypj=itype(j)
1683 c            dscj_inv=dsc_inv(itypj)
1684             dscj_inv=vbld_inv(j+nres)
1685 c            write (iout,*) "j",j,dsc_inv(itypj),dscj_inv,
1686 c     &       1.0d0/vbld(j+nres)
1687 c            write (iout,*) "i",i," j", j," itype",itype(i),itype(j)
1688             sig0ij=sigma(itypi,itypj)
1689             chi1=chi(itypi,itypj)
1690             chi2=chi(itypj,itypi)
1691             chi12=chi1*chi2
1692             chip1=chip(itypi)
1693             chip2=chip(itypj)
1694             chip12=chip1*chip2
1695             alf1=alp(itypi)
1696             alf2=alp(itypj)
1697             alf12=0.5D0*(alf1+alf2)
1698 C For diagnostics only!!!
1699 c           chi1=0.0D0
1700 c           chi2=0.0D0
1701 c           chi12=0.0D0
1702 c           chip1=0.0D0
1703 c           chip2=0.0D0
1704 c           chip12=0.0D0
1705 c           alf1=0.0D0
1706 c           alf2=0.0D0
1707 c           alf12=0.0D0
1708             xj=c(1,nres+j)-xi
1709             yj=c(2,nres+j)-yi
1710             zj=c(3,nres+j)-zi
1711             dxj=dc_norm(1,nres+j)
1712             dyj=dc_norm(2,nres+j)
1713             dzj=dc_norm(3,nres+j)
1714 c            write (iout,*) "dcnorj",dxi*dxi+dyi*dyi+dzi*dzi
1715 c            write (iout,*) "j",j," dc_norm",
1716 c     &       dc_norm(1,nres+j),dc_norm(2,nres+j),dc_norm(3,nres+j)
1717             rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
1718             rij=dsqrt(rrij)
1719 C Calculate angle-dependent terms of energy and contributions to their
1720 C derivatives.
1721             call sc_angular
1722             sigsq=1.0D0/sigsq
1723             sig=sig0ij*dsqrt(sigsq)
1724             rij_shift=1.0D0/rij-sig+sig0ij
1725 c for diagnostics; uncomment
1726 c            rij_shift=1.2*sig0ij
1727 C I hate to put IF's in the loops, but here don't have another choice!!!!
1728             if (rij_shift.le.0.0D0) then
1729               evdw=1.0D20
1730 cd              write (iout,'(2(a3,i3,2x),17(0pf7.3))')
1731 cd     &        restyp(itypi),i,restyp(itypj),j,
1732 cd     &        rij_shift,1.0D0/rij,sig,sig0ij,sigsq,1-dsqrt(sigsq) 
1733               return
1734             endif
1735             sigder=-sig*sigsq
1736 c---------------------------------------------------------------
1737             rij_shift=1.0D0/rij_shift 
1738             fac=rij_shift**expon
1739             e1=fac*fac*aa(itypi,itypj)
1740             e2=fac*bb(itypi,itypj)
1741             evdwij=eps1*eps2rt*eps3rt*(e1+e2)
1742             eps2der=evdwij*eps3rt
1743             eps3der=evdwij*eps2rt
1744 c            write (iout,*) "sigsq",sigsq," sig",sig," eps2rt",eps2rt,
1745 c     &        " eps3rt",eps3rt," eps1",eps1," e1",e1," e2",e2
1746             evdwij=evdwij*eps2rt*eps3rt
1747 #ifdef TSCSC
1748             if (bb(itypi,itypj).gt.0) then
1749                evdw_p=evdw_p+evdwij
1750             else
1751                evdw_m=evdw_m+evdwij
1752             endif
1753 #else
1754             evdw=evdw+evdwij
1755 #endif
1756             if (lprn) then
1757             sigm=dabs(aa(itypi,itypj)/bb(itypi,itypj))**(1.0D0/6.0D0)
1758             epsi=bb(itypi,itypj)**2/aa(itypi,itypj)
1759             write (iout,'(2(a3,i3,2x),17(0pf7.3))')
1760      &        restyp(itypi),i,restyp(itypj),j,
1761      &        epsi,sigm,chi1,chi2,chip1,chip2,
1762      &        eps1,eps2rt**2,eps3rt**2,sig,sig0ij,
1763      &        om1,om2,om12,1.0D0/rij,1.0D0/rij_shift,
1764      &        evdwij
1765             endif
1766
1767             if (energy_dec) write (iout,'(a6,2i5,0pf7.3)') 
1768      &                        'evdw',i,j,evdwij
1769
1770 C Calculate gradient components.
1771             e1=e1*eps1*eps2rt**2*eps3rt**2
1772             fac=-expon*(e1+evdwij)*rij_shift
1773             sigder=fac*sigder
1774             fac=rij*fac
1775 c            fac=0.0d0
1776 C Calculate the radial part of the gradient
1777             gg(1)=xj*fac
1778             gg(2)=yj*fac
1779             gg(3)=zj*fac
1780 C Calculate angular part of the gradient.
1781 #ifdef TSCSC
1782             if (bb(itypi,itypj).gt.0) then
1783                call sc_grad
1784             else
1785                call sc_grad_T
1786             endif
1787 #else
1788             call sc_grad
1789 #endif
1790             ENDIF    ! dyn_ss            
1791           enddo      ! j
1792         enddo        ! iint
1793       enddo          ! i
1794 c      write (iout,*) "Number of loop steps in EGB:",ind
1795 cccc      energy_dec=.false.
1796       return
1797       end
1798 C-----------------------------------------------------------------------------
1799       subroutine egbv(evdw,evdw_p,evdw_m)
1800 C
1801 C This subroutine calculates the interaction energy of nonbonded side chains
1802 C assuming the Gay-Berne-Vorobjev potential of interaction.
1803 C
1804       implicit real*8 (a-h,o-z)
1805       include 'DIMENSIONS'
1806       include 'COMMON.GEO'
1807       include 'COMMON.VAR'
1808       include 'COMMON.LOCAL'
1809       include 'COMMON.CHAIN'
1810       include 'COMMON.DERIV'
1811       include 'COMMON.NAMES'
1812       include 'COMMON.INTERACT'
1813       include 'COMMON.IOUNITS'
1814       include 'COMMON.CALC'
1815       common /srutu/ icall
1816       logical lprn
1817       evdw=0.0D0
1818 c     print *,'Entering EGB nnt=',nnt,' nct=',nct,' expon=',expon
1819       evdw=0.0D0
1820       lprn=.false.
1821 c     if (icall.eq.0) lprn=.true.
1822       ind=0
1823       do i=iatsc_s,iatsc_e
1824         itypi=itype(i)
1825         itypi1=itype(i+1)
1826         xi=c(1,nres+i)
1827         yi=c(2,nres+i)
1828         zi=c(3,nres+i)
1829         dxi=dc_norm(1,nres+i)
1830         dyi=dc_norm(2,nres+i)
1831         dzi=dc_norm(3,nres+i)
1832 c        dsci_inv=dsc_inv(itypi)
1833         dsci_inv=vbld_inv(i+nres)
1834 C
1835 C Calculate SC interaction energy.
1836 C
1837         do iint=1,nint_gr(i)
1838           do j=istart(i,iint),iend(i,iint)
1839             ind=ind+1
1840             itypj=itype(j)
1841 c            dscj_inv=dsc_inv(itypj)
1842             dscj_inv=vbld_inv(j+nres)
1843             sig0ij=sigma(itypi,itypj)
1844             r0ij=r0(itypi,itypj)
1845             chi1=chi(itypi,itypj)
1846             chi2=chi(itypj,itypi)
1847             chi12=chi1*chi2
1848             chip1=chip(itypi)
1849             chip2=chip(itypj)
1850             chip12=chip1*chip2
1851             alf1=alp(itypi)
1852             alf2=alp(itypj)
1853             alf12=0.5D0*(alf1+alf2)
1854 C For diagnostics only!!!
1855 c           chi1=0.0D0
1856 c           chi2=0.0D0
1857 c           chi12=0.0D0
1858 c           chip1=0.0D0
1859 c           chip2=0.0D0
1860 c           chip12=0.0D0
1861 c           alf1=0.0D0
1862 c           alf2=0.0D0
1863 c           alf12=0.0D0
1864             xj=c(1,nres+j)-xi
1865             yj=c(2,nres+j)-yi
1866             zj=c(3,nres+j)-zi
1867             dxj=dc_norm(1,nres+j)
1868             dyj=dc_norm(2,nres+j)
1869             dzj=dc_norm(3,nres+j)
1870             rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
1871             rij=dsqrt(rrij)
1872 C Calculate angle-dependent terms of energy and contributions to their
1873 C derivatives.
1874             call sc_angular
1875             sigsq=1.0D0/sigsq
1876             sig=sig0ij*dsqrt(sigsq)
1877             rij_shift=1.0D0/rij-sig+r0ij
1878 C I hate to put IF's in the loops, but here don't have another choice!!!!
1879             if (rij_shift.le.0.0D0) then
1880               evdw=1.0D20
1881               return
1882             endif
1883             sigder=-sig*sigsq
1884 c---------------------------------------------------------------
1885             rij_shift=1.0D0/rij_shift 
1886             fac=rij_shift**expon
1887             e1=fac*fac*aa(itypi,itypj)
1888             e2=fac*bb(itypi,itypj)
1889             evdwij=eps1*eps2rt*eps3rt*(e1+e2)
1890             eps2der=evdwij*eps3rt
1891             eps3der=evdwij*eps2rt
1892             fac_augm=rrij**expon
1893             e_augm=augm(itypi,itypj)*fac_augm
1894             evdwij=evdwij*eps2rt*eps3rt
1895 #ifdef TSCSC
1896             if (bb(itypi,itypj).gt.0) then
1897                evdw_p=evdw_p+evdwij+e_augm
1898             else
1899                evdw_m=evdw_m+evdwij+e_augm
1900             endif
1901 #else
1902             evdw=evdw+evdwij+e_augm
1903 #endif
1904             if (lprn) then
1905             sigm=dabs(aa(itypi,itypj)/bb(itypi,itypj))**(1.0D0/6.0D0)
1906             epsi=bb(itypi,itypj)**2/aa(itypi,itypj)
1907             write (iout,'(2(a3,i3,2x),17(0pf7.3))')
1908      &        restyp(itypi),i,restyp(itypj),j,
1909      &        epsi,sigm,sig,(augm(itypi,itypj)/epsi)**(1.0D0/12.0D0),
1910      &        chi1,chi2,chip1,chip2,
1911      &        eps1,eps2rt**2,eps3rt**2,
1912      &        om1,om2,om12,1.0D0/rij,1.0D0/rij_shift,
1913      &        evdwij+e_augm
1914             endif
1915 C Calculate gradient components.
1916             e1=e1*eps1*eps2rt**2*eps3rt**2
1917             fac=-expon*(e1+evdwij)*rij_shift
1918             sigder=fac*sigder
1919             fac=rij*fac-2*expon*rrij*e_augm
1920 C Calculate the radial part of the gradient
1921             gg(1)=xj*fac
1922             gg(2)=yj*fac
1923             gg(3)=zj*fac
1924 C Calculate angular part of the gradient.
1925 #ifdef TSCSC
1926             if (bb(itypi,itypj).gt.0) then
1927                call sc_grad
1928             else
1929                call sc_grad_T
1930             endif
1931 #else
1932             call sc_grad
1933 #endif
1934           enddo      ! j
1935         enddo        ! iint
1936       enddo          ! i
1937       end
1938 C-----------------------------------------------------------------------------
1939       subroutine sc_angular
1940 C Calculate eps1,eps2,eps3,sigma, and parts of their derivatives in om1,om2,
1941 C om12. Called by ebp, egb, and egbv.
1942       implicit none
1943       include 'COMMON.CALC'
1944       include 'COMMON.IOUNITS'
1945       erij(1)=xj*rij
1946       erij(2)=yj*rij
1947       erij(3)=zj*rij
1948       om1=dxi*erij(1)+dyi*erij(2)+dzi*erij(3)
1949       om2=dxj*erij(1)+dyj*erij(2)+dzj*erij(3)
1950       om12=dxi*dxj+dyi*dyj+dzi*dzj
1951       chiom12=chi12*om12
1952 C Calculate eps1(om12) and its derivative in om12
1953       faceps1=1.0D0-om12*chiom12
1954       faceps1_inv=1.0D0/faceps1
1955       eps1=dsqrt(faceps1_inv)
1956 C Following variable is eps1*deps1/dom12
1957       eps1_om12=faceps1_inv*chiom12
1958 c diagnostics only
1959 c      faceps1_inv=om12
1960 c      eps1=om12
1961 c      eps1_om12=1.0d0
1962 c      write (iout,*) "om12",om12," eps1",eps1
1963 C Calculate sigma(om1,om2,om12) and the derivatives of sigma**2 in om1,om2,
1964 C and om12.
1965       om1om2=om1*om2
1966       chiom1=chi1*om1
1967       chiom2=chi2*om2
1968       facsig=om1*chiom1+om2*chiom2-2.0D0*om1om2*chiom12
1969       sigsq=1.0D0-facsig*faceps1_inv
1970       sigsq_om1=(chiom1-chiom12*om2)*faceps1_inv
1971       sigsq_om2=(chiom2-chiom12*om1)*faceps1_inv
1972       sigsq_om12=-chi12*(om1om2*faceps1-om12*facsig)*faceps1_inv**2
1973 c diagnostics only
1974 c      sigsq=1.0d0
1975 c      sigsq_om1=0.0d0
1976 c      sigsq_om2=0.0d0
1977 c      sigsq_om12=0.0d0
1978 c      write (iout,*) "chiom1",chiom1," chiom2",chiom2," chiom12",chiom12
1979 c      write (iout,*) "faceps1",faceps1," faceps1_inv",faceps1_inv,
1980 c     &    " eps1",eps1
1981 C Calculate eps2 and its derivatives in om1, om2, and om12.
1982       chipom1=chip1*om1
1983       chipom2=chip2*om2
1984       chipom12=chip12*om12
1985       facp=1.0D0-om12*chipom12
1986       facp_inv=1.0D0/facp
1987       facp1=om1*chipom1+om2*chipom2-2.0D0*om1om2*chipom12
1988 c      write (iout,*) "chipom1",chipom1," chipom2",chipom2,
1989 c     &  " chipom12",chipom12," facp",facp," facp_inv",facp_inv
1990 C Following variable is the square root of eps2
1991       eps2rt=1.0D0-facp1*facp_inv
1992 C Following three variables are the derivatives of the square root of eps
1993 C in om1, om2, and om12.
1994       eps2rt_om1=-4.0D0*(chipom1-chipom12*om2)*facp_inv
1995       eps2rt_om2=-4.0D0*(chipom2-chipom12*om1)*facp_inv
1996       eps2rt_om12=4.0D0*chip12*(om1om2*facp-om12*facp1)*facp_inv**2 
1997 C Evaluate the "asymmetric" factor in the VDW constant, eps3
1998       eps3rt=1.0D0-alf1*om1+alf2*om2-alf12*om12 
1999 c      write (iout,*) "eps2rt",eps2rt," eps3rt",eps3rt
2000 c      write (iout,*) "eps2rt_om1",eps2rt_om1," eps2rt_om2",eps2rt_om2,
2001 c     &  " eps2rt_om12",eps2rt_om12
2002 C Calculate whole angle-dependent part of epsilon and contributions
2003 C to its derivatives
2004       return
2005       end
2006
2007 C----------------------------------------------------------------------------
2008       subroutine sc_grad_T
2009       implicit real*8 (a-h,o-z)
2010       include 'DIMENSIONS'
2011       include 'COMMON.CHAIN'
2012       include 'COMMON.DERIV'
2013       include 'COMMON.CALC'
2014       include 'COMMON.IOUNITS'
2015       double precision dcosom1(3),dcosom2(3)
2016       eom1=eps2der*eps2rt_om1-2.0D0*alf1*eps3der+sigder*sigsq_om1
2017       eom2=eps2der*eps2rt_om2+2.0D0*alf2*eps3der+sigder*sigsq_om2
2018       eom12=evdwij*eps1_om12+eps2der*eps2rt_om12
2019      &     -2.0D0*alf12*eps3der+sigder*sigsq_om12
2020 c diagnostics only
2021 c      eom1=0.0d0
2022 c      eom2=0.0d0
2023 c      eom12=evdwij*eps1_om12
2024 c end diagnostics
2025 c      write (iout,*) "eps2der",eps2der," eps3der",eps3der,
2026 c     &  " sigder",sigder
2027 c      write (iout,*) "eps1_om12",eps1_om12," eps2rt_om12",eps2rt_om12
2028 c      write (iout,*) "eom1",eom1," eom2",eom2," eom12",eom12
2029       do k=1,3
2030         dcosom1(k)=rij*(dc_norm(k,nres+i)-om1*erij(k))
2031         dcosom2(k)=rij*(dc_norm(k,nres+j)-om2*erij(k))
2032       enddo
2033       do k=1,3
2034         gg(k)=gg(k)+eom1*dcosom1(k)+eom2*dcosom2(k)
2035       enddo 
2036 c      write (iout,*) "gg",(gg(k),k=1,3)
2037       do k=1,3
2038         gvdwxT(k,i)=gvdwxT(k,i)-gg(k)
2039      &            +(eom12*(dc_norm(k,nres+j)-om12*dc_norm(k,nres+i))
2040      &            +eom1*(erij(k)-om1*dc_norm(k,nres+i)))*dsci_inv
2041         gvdwxT(k,j)=gvdwxT(k,j)+gg(k)
2042      &            +(eom12*(dc_norm(k,nres+i)-om12*dc_norm(k,nres+j))
2043      &            +eom2*(erij(k)-om2*dc_norm(k,nres+j)))*dscj_inv
2044 c        write (iout,*)(eom12*(dc_norm(k,nres+j)-om12*dc_norm(k,nres+i))
2045 c     &            +eom1*(erij(k)-om1*dc_norm(k,nres+i)))*dsci_inv
2046 c        write (iout,*)(eom12*(dc_norm(k,nres+i)-om12*dc_norm(k,nres+j))
2047 c     &            +eom2*(erij(k)-om2*dc_norm(k,nres+j)))*dscj_inv
2048       enddo
2049
2050 C Calculate the components of the gradient in DC and X
2051 C
2052 cgrad      do k=i,j-1
2053 cgrad        do l=1,3
2054 cgrad          gvdwc(l,k)=gvdwc(l,k)+gg(l)
2055 cgrad        enddo
2056 cgrad      enddo
2057       do l=1,3
2058         gvdwcT(l,i)=gvdwcT(l,i)-gg(l)
2059         gvdwcT(l,j)=gvdwcT(l,j)+gg(l)
2060       enddo
2061       return
2062       end
2063
2064 C----------------------------------------------------------------------------
2065       subroutine sc_grad
2066       implicit real*8 (a-h,o-z)
2067       include 'DIMENSIONS'
2068       include 'COMMON.CHAIN'
2069       include 'COMMON.DERIV'
2070       include 'COMMON.CALC'
2071       include 'COMMON.IOUNITS'
2072       double precision dcosom1(3),dcosom2(3)
2073       eom1=eps2der*eps2rt_om1-2.0D0*alf1*eps3der+sigder*sigsq_om1
2074       eom2=eps2der*eps2rt_om2+2.0D0*alf2*eps3der+sigder*sigsq_om2
2075       eom12=evdwij*eps1_om12+eps2der*eps2rt_om12
2076      &     -2.0D0*alf12*eps3der+sigder*sigsq_om12
2077 c diagnostics only
2078 c      eom1=0.0d0
2079 c      eom2=0.0d0
2080 c      eom12=evdwij*eps1_om12
2081 c end diagnostics
2082 c      write (iout,*) "eps2der",eps2der," eps3der",eps3der,
2083 c     &  " sigder",sigder
2084 c      write (iout,*) "eps1_om12",eps1_om12," eps2rt_om12",eps2rt_om12
2085 c      write (iout,*) "eom1",eom1," eom2",eom2," eom12",eom12
2086       do k=1,3
2087         dcosom1(k)=rij*(dc_norm(k,nres+i)-om1*erij(k))
2088         dcosom2(k)=rij*(dc_norm(k,nres+j)-om2*erij(k))
2089       enddo
2090       do k=1,3
2091         gg(k)=gg(k)+eom1*dcosom1(k)+eom2*dcosom2(k)
2092       enddo 
2093 c      write (iout,*) "gg",(gg(k),k=1,3)
2094       do k=1,3
2095         gvdwx(k,i)=gvdwx(k,i)-gg(k)
2096      &            +(eom12*(dc_norm(k,nres+j)-om12*dc_norm(k,nres+i))
2097      &            +eom1*(erij(k)-om1*dc_norm(k,nres+i)))*dsci_inv
2098         gvdwx(k,j)=gvdwx(k,j)+gg(k)
2099      &            +(eom12*(dc_norm(k,nres+i)-om12*dc_norm(k,nres+j))
2100      &            +eom2*(erij(k)-om2*dc_norm(k,nres+j)))*dscj_inv
2101 c        write (iout,*)(eom12*(dc_norm(k,nres+j)-om12*dc_norm(k,nres+i))
2102 c     &            +eom1*(erij(k)-om1*dc_norm(k,nres+i)))*dsci_inv
2103 c        write (iout,*)(eom12*(dc_norm(k,nres+i)-om12*dc_norm(k,nres+j))
2104 c     &            +eom2*(erij(k)-om2*dc_norm(k,nres+j)))*dscj_inv
2105       enddo
2106
2107 C Calculate the components of the gradient in DC and X
2108 C
2109 cgrad      do k=i,j-1
2110 cgrad        do l=1,3
2111 cgrad          gvdwc(l,k)=gvdwc(l,k)+gg(l)
2112 cgrad        enddo
2113 cgrad      enddo
2114       do l=1,3
2115         gvdwc(l,i)=gvdwc(l,i)-gg(l)
2116         gvdwc(l,j)=gvdwc(l,j)+gg(l)
2117       enddo
2118       return
2119       end
2120 C-----------------------------------------------------------------------
2121       subroutine e_softsphere(evdw)
2122 C
2123 C This subroutine calculates the interaction energy of nonbonded side chains
2124 C assuming the LJ potential of interaction.
2125 C
2126       implicit real*8 (a-h,o-z)
2127       include 'DIMENSIONS'
2128       parameter (accur=1.0d-10)
2129       include 'COMMON.GEO'
2130       include 'COMMON.VAR'
2131       include 'COMMON.LOCAL'
2132       include 'COMMON.CHAIN'
2133       include 'COMMON.DERIV'
2134       include 'COMMON.INTERACT'
2135       include 'COMMON.TORSION'
2136       include 'COMMON.SBRIDGE'
2137       include 'COMMON.NAMES'
2138       include 'COMMON.IOUNITS'
2139       include 'COMMON.CONTACTS'
2140       dimension gg(3)
2141 cd    print *,'Entering Esoft_sphere nnt=',nnt,' nct=',nct
2142       evdw=0.0D0
2143       do i=iatsc_s,iatsc_e
2144         itypi=itype(i)
2145         itypi1=itype(i+1)
2146         xi=c(1,nres+i)
2147         yi=c(2,nres+i)
2148         zi=c(3,nres+i)
2149 C
2150 C Calculate SC interaction energy.
2151 C
2152         do iint=1,nint_gr(i)
2153 cd        write (iout,*) 'i=',i,' iint=',iint,' istart=',istart(i,iint),
2154 cd   &                  'iend=',iend(i,iint)
2155           do j=istart(i,iint),iend(i,iint)
2156             itypj=itype(j)
2157             xj=c(1,nres+j)-xi
2158             yj=c(2,nres+j)-yi
2159             zj=c(3,nres+j)-zi
2160             rij=xj*xj+yj*yj+zj*zj
2161 c           write (iout,*)'i=',i,' j=',j,' itypi=',itypi,' itypj=',itypj
2162             r0ij=r0(itypi,itypj)
2163             r0ijsq=r0ij*r0ij
2164 c            print *,i,j,r0ij,dsqrt(rij)
2165             if (rij.lt.r0ijsq) then
2166               evdwij=0.25d0*(rij-r0ijsq)**2
2167               fac=rij-r0ijsq
2168             else
2169               evdwij=0.0d0
2170               fac=0.0d0
2171             endif
2172             evdw=evdw+evdwij
2173
2174 C Calculate the components of the gradient in DC and X
2175 C
2176             gg(1)=xj*fac
2177             gg(2)=yj*fac
2178             gg(3)=zj*fac
2179             do k=1,3
2180               gvdwx(k,i)=gvdwx(k,i)-gg(k)
2181               gvdwx(k,j)=gvdwx(k,j)+gg(k)
2182               gvdwc(k,i)=gvdwc(k,i)-gg(k)
2183               gvdwc(k,j)=gvdwc(k,j)+gg(k)
2184             enddo
2185 cgrad            do k=i,j-1
2186 cgrad              do l=1,3
2187 cgrad                gvdwc(l,k)=gvdwc(l,k)+gg(l)
2188 cgrad              enddo
2189 cgrad            enddo
2190           enddo ! j
2191         enddo ! iint
2192       enddo ! i
2193       return
2194       end
2195 C--------------------------------------------------------------------------
2196       subroutine eelec_soft_sphere(ees,evdw1,eel_loc,eello_turn3,
2197      &              eello_turn4)
2198 C
2199 C Soft-sphere potential of p-p interaction
2200
2201       implicit real*8 (a-h,o-z)
2202       include 'DIMENSIONS'
2203       include 'COMMON.CONTROL'
2204       include 'COMMON.IOUNITS'
2205       include 'COMMON.GEO'
2206       include 'COMMON.VAR'
2207       include 'COMMON.LOCAL'
2208       include 'COMMON.CHAIN'
2209       include 'COMMON.DERIV'
2210       include 'COMMON.INTERACT'
2211       include 'COMMON.CONTACTS'
2212       include 'COMMON.TORSION'
2213       include 'COMMON.VECTORS'
2214       include 'COMMON.FFIELD'
2215       dimension ggg(3)
2216 cd      write(iout,*) 'In EELEC_soft_sphere'
2217       ees=0.0D0
2218       evdw1=0.0D0
2219       eel_loc=0.0d0 
2220       eello_turn3=0.0d0
2221       eello_turn4=0.0d0
2222       ind=0
2223       do i=iatel_s,iatel_e
2224         dxi=dc(1,i)
2225         dyi=dc(2,i)
2226         dzi=dc(3,i)
2227         xmedi=c(1,i)+0.5d0*dxi
2228         ymedi=c(2,i)+0.5d0*dyi
2229         zmedi=c(3,i)+0.5d0*dzi
2230         num_conti=0
2231 c        write (iout,*) 'i',i,' ielstart',ielstart(i),' ielend',ielend(i)
2232         do j=ielstart(i),ielend(i)
2233           ind=ind+1
2234           iteli=itel(i)
2235           itelj=itel(j)
2236           if (j.eq.i+2 .and. itelj.eq.2) iteli=2
2237           r0ij=rpp(iteli,itelj)
2238           r0ijsq=r0ij*r0ij 
2239           dxj=dc(1,j)
2240           dyj=dc(2,j)
2241           dzj=dc(3,j)
2242           xj=c(1,j)+0.5D0*dxj-xmedi
2243           yj=c(2,j)+0.5D0*dyj-ymedi
2244           zj=c(3,j)+0.5D0*dzj-zmedi
2245           rij=xj*xj+yj*yj+zj*zj
2246           if (rij.lt.r0ijsq) then
2247             evdw1ij=0.25d0*(rij-r0ijsq)**2
2248             fac=rij-r0ijsq
2249           else
2250             evdw1ij=0.0d0
2251             fac=0.0d0
2252           endif
2253           evdw1=evdw1+evdw1ij
2254 C
2255 C Calculate contributions to the Cartesian gradient.
2256 C
2257           ggg(1)=fac*xj
2258           ggg(2)=fac*yj
2259           ggg(3)=fac*zj
2260           do k=1,3
2261             gvdwpp(k,i)=gvdwpp(k,i)-ggg(k)
2262             gvdwpp(k,j)=gvdwpp(k,j)+ggg(k)
2263           enddo
2264 *
2265 * Loop over residues i+1 thru j-1.
2266 *
2267 cgrad          do k=i+1,j-1
2268 cgrad            do l=1,3
2269 cgrad              gelc(l,k)=gelc(l,k)+ggg(l)
2270 cgrad            enddo
2271 cgrad          enddo
2272         enddo ! j
2273       enddo   ! i
2274 cgrad      do i=nnt,nct-1
2275 cgrad        do k=1,3
2276 cgrad          gelc(k,i)=gelc(k,i)+0.5d0*gelc(k,i)
2277 cgrad        enddo
2278 cgrad        do j=i+1,nct-1
2279 cgrad          do k=1,3
2280 cgrad            gelc(k,i)=gelc(k,i)+gelc(k,j)
2281 cgrad          enddo
2282 cgrad        enddo
2283 cgrad      enddo
2284       return
2285       end
2286 c------------------------------------------------------------------------------
2287       subroutine vec_and_deriv
2288       implicit real*8 (a-h,o-z)
2289       include 'DIMENSIONS'
2290 #ifdef MPI
2291       include 'mpif.h'
2292 #endif
2293       include 'COMMON.IOUNITS'
2294       include 'COMMON.GEO'
2295       include 'COMMON.VAR'
2296       include 'COMMON.LOCAL'
2297       include 'COMMON.CHAIN'
2298       include 'COMMON.VECTORS'
2299       include 'COMMON.SETUP'
2300       include 'COMMON.TIME1'
2301       dimension uyder(3,3,2),uzder(3,3,2),vbld_inv_temp(2)
2302 C Compute the local reference systems. For reference system (i), the
2303 C X-axis points from CA(i) to CA(i+1), the Y axis is in the 
2304 C CA(i)-CA(i+1)-CA(i+2) plane, and the Z axis is perpendicular to this plane.
2305 #ifdef PARVEC
2306       do i=ivec_start,ivec_end
2307 #else
2308       do i=1,nres-1
2309 #endif
2310           if (i.eq.nres-1) then
2311 C Case of the last full residue
2312 C Compute the Z-axis
2313             call vecpr(dc_norm(1,i),dc_norm(1,i-1),uz(1,i))
2314             costh=dcos(pi-theta(nres))
2315             fac=1.0d0/dsqrt(1.0d0-costh*costh)
2316             do k=1,3
2317               uz(k,i)=fac*uz(k,i)
2318             enddo
2319 C Compute the derivatives of uz
2320             uzder(1,1,1)= 0.0d0
2321             uzder(2,1,1)=-dc_norm(3,i-1)
2322             uzder(3,1,1)= dc_norm(2,i-1) 
2323             uzder(1,2,1)= dc_norm(3,i-1)
2324             uzder(2,2,1)= 0.0d0
2325             uzder(3,2,1)=-dc_norm(1,i-1)
2326             uzder(1,3,1)=-dc_norm(2,i-1)
2327             uzder(2,3,1)= dc_norm(1,i-1)
2328             uzder(3,3,1)= 0.0d0
2329             uzder(1,1,2)= 0.0d0
2330             uzder(2,1,2)= dc_norm(3,i)
2331             uzder(3,1,2)=-dc_norm(2,i) 
2332             uzder(1,2,2)=-dc_norm(3,i)
2333             uzder(2,2,2)= 0.0d0
2334             uzder(3,2,2)= dc_norm(1,i)
2335             uzder(1,3,2)= dc_norm(2,i)
2336             uzder(2,3,2)=-dc_norm(1,i)
2337             uzder(3,3,2)= 0.0d0
2338 C Compute the Y-axis
2339             facy=fac
2340             do k=1,3
2341               uy(k,i)=fac*(dc_norm(k,i-1)-costh*dc_norm(k,i))
2342             enddo
2343 C Compute the derivatives of uy
2344             do j=1,3
2345               do k=1,3
2346                 uyder(k,j,1)=2*dc_norm(k,i-1)*dc_norm(j,i)
2347      &                        -dc_norm(k,i)*dc_norm(j,i-1)
2348                 uyder(k,j,2)=-dc_norm(j,i)*dc_norm(k,i)
2349               enddo
2350               uyder(j,j,1)=uyder(j,j,1)-costh
2351               uyder(j,j,2)=1.0d0+uyder(j,j,2)
2352             enddo
2353             do j=1,2
2354               do k=1,3
2355                 do l=1,3
2356                   uygrad(l,k,j,i)=uyder(l,k,j)
2357                   uzgrad(l,k,j,i)=uzder(l,k,j)
2358                 enddo
2359               enddo
2360             enddo 
2361             call unormderiv(uy(1,i),uyder(1,1,1),facy,uygrad(1,1,1,i))
2362             call unormderiv(uy(1,i),uyder(1,1,2),facy,uygrad(1,1,2,i))
2363             call unormderiv(uz(1,i),uzder(1,1,1),fac,uzgrad(1,1,1,i))
2364             call unormderiv(uz(1,i),uzder(1,1,2),fac,uzgrad(1,1,2,i))
2365           else
2366 C Other residues
2367 C Compute the Z-axis
2368             call vecpr(dc_norm(1,i),dc_norm(1,i+1),uz(1,i))
2369             costh=dcos(pi-theta(i+2))
2370             fac=1.0d0/dsqrt(1.0d0-costh*costh)
2371             do k=1,3
2372               uz(k,i)=fac*uz(k,i)
2373             enddo
2374 C Compute the derivatives of uz
2375             uzder(1,1,1)= 0.0d0
2376             uzder(2,1,1)=-dc_norm(3,i+1)
2377             uzder(3,1,1)= dc_norm(2,i+1) 
2378             uzder(1,2,1)= dc_norm(3,i+1)
2379             uzder(2,2,1)= 0.0d0
2380             uzder(3,2,1)=-dc_norm(1,i+1)
2381             uzder(1,3,1)=-dc_norm(2,i+1)
2382             uzder(2,3,1)= dc_norm(1,i+1)
2383             uzder(3,3,1)= 0.0d0
2384             uzder(1,1,2)= 0.0d0
2385             uzder(2,1,2)= dc_norm(3,i)
2386             uzder(3,1,2)=-dc_norm(2,i) 
2387             uzder(1,2,2)=-dc_norm(3,i)
2388             uzder(2,2,2)= 0.0d0
2389             uzder(3,2,2)= dc_norm(1,i)
2390             uzder(1,3,2)= dc_norm(2,i)
2391             uzder(2,3,2)=-dc_norm(1,i)
2392             uzder(3,3,2)= 0.0d0
2393 C Compute the Y-axis
2394             facy=fac
2395             do k=1,3
2396               uy(k,i)=facy*(dc_norm(k,i+1)-costh*dc_norm(k,i))
2397             enddo
2398 C Compute the derivatives of uy
2399             do j=1,3
2400               do k=1,3
2401                 uyder(k,j,1)=2*dc_norm(k,i+1)*dc_norm(j,i)
2402      &                        -dc_norm(k,i)*dc_norm(j,i+1)
2403                 uyder(k,j,2)=-dc_norm(j,i)*dc_norm(k,i)
2404               enddo
2405               uyder(j,j,1)=uyder(j,j,1)-costh
2406               uyder(j,j,2)=1.0d0+uyder(j,j,2)
2407             enddo
2408             do j=1,2
2409               do k=1,3
2410                 do l=1,3
2411                   uygrad(l,k,j,i)=uyder(l,k,j)
2412                   uzgrad(l,k,j,i)=uzder(l,k,j)
2413                 enddo
2414               enddo
2415             enddo 
2416             call unormderiv(uy(1,i),uyder(1,1,1),facy,uygrad(1,1,1,i))
2417             call unormderiv(uy(1,i),uyder(1,1,2),facy,uygrad(1,1,2,i))
2418             call unormderiv(uz(1,i),uzder(1,1,1),fac,uzgrad(1,1,1,i))
2419             call unormderiv(uz(1,i),uzder(1,1,2),fac,uzgrad(1,1,2,i))
2420           endif
2421       enddo
2422       do i=1,nres-1
2423         vbld_inv_temp(1)=vbld_inv(i+1)
2424         if (i.lt.nres-1) then
2425           vbld_inv_temp(2)=vbld_inv(i+2)
2426           else
2427           vbld_inv_temp(2)=vbld_inv(i)
2428           endif
2429         do j=1,2
2430           do k=1,3
2431             do l=1,3
2432               uygrad(l,k,j,i)=vbld_inv_temp(j)*uygrad(l,k,j,i)
2433               uzgrad(l,k,j,i)=vbld_inv_temp(j)*uzgrad(l,k,j,i)
2434             enddo
2435           enddo
2436         enddo
2437       enddo
2438 #if defined(PARVEC) && defined(MPI)
2439       if (nfgtasks1.gt.1) then
2440         time00=MPI_Wtime()
2441 c        print *,"Processor",fg_rank1,kolor1," ivec_start",ivec_start,
2442 c     &   " ivec_displ",(ivec_displ(i),i=0,nfgtasks1-1),
2443 c     &   " ivec_count",(ivec_count(i),i=0,nfgtasks1-1)
2444         call MPI_Allgatherv(uy(1,ivec_start),ivec_count(fg_rank1),
2445      &   MPI_UYZ,uy(1,1),ivec_count(0),ivec_displ(0),MPI_UYZ,
2446      &   FG_COMM1,IERR)
2447         call MPI_Allgatherv(uz(1,ivec_start),ivec_count(fg_rank1),
2448      &   MPI_UYZ,uz(1,1),ivec_count(0),ivec_displ(0),MPI_UYZ,
2449      &   FG_COMM1,IERR)
2450         call MPI_Allgatherv(uygrad(1,1,1,ivec_start),
2451      &   ivec_count(fg_rank1),MPI_UYZGRAD,uygrad(1,1,1,1),ivec_count(0),
2452      &   ivec_displ(0),MPI_UYZGRAD,FG_COMM1,IERR)
2453         call MPI_Allgatherv(uzgrad(1,1,1,ivec_start),
2454      &   ivec_count(fg_rank1),MPI_UYZGRAD,uzgrad(1,1,1,1),ivec_count(0),
2455      &   ivec_displ(0),MPI_UYZGRAD,FG_COMM1,IERR)
2456         time_gather=time_gather+MPI_Wtime()-time00
2457       endif
2458 c      if (fg_rank.eq.0) then
2459 c        write (iout,*) "Arrays UY and UZ"
2460 c        do i=1,nres-1
2461 c          write (iout,'(i5,3f10.5,5x,3f10.5)') i,(uy(k,i),k=1,3),
2462 c     &     (uz(k,i),k=1,3)
2463 c        enddo
2464 c      endif
2465 #endif
2466       return
2467       end
2468 C-----------------------------------------------------------------------------
2469       subroutine check_vecgrad
2470       implicit real*8 (a-h,o-z)
2471       include 'DIMENSIONS'
2472       include 'COMMON.IOUNITS'
2473       include 'COMMON.GEO'
2474       include 'COMMON.VAR'
2475       include 'COMMON.LOCAL'
2476       include 'COMMON.CHAIN'
2477       include 'COMMON.VECTORS'
2478       dimension uygradt(3,3,2,maxres),uzgradt(3,3,2,maxres)
2479       dimension uyt(3,maxres),uzt(3,maxres)
2480       dimension uygradn(3,3,2),uzgradn(3,3,2),erij(3)
2481       double precision delta /1.0d-7/
2482       call vec_and_deriv
2483 cd      do i=1,nres
2484 crc          write(iout,'(2i5,2(3f10.5,5x))') i,1,dc_norm(:,i)
2485 crc          write(iout,'(2i5,2(3f10.5,5x))') i,2,uy(:,i)
2486 crc          write(iout,'(2i5,2(3f10.5,5x)/)')i,3,uz(:,i)
2487 cd          write(iout,'(2i5,2(3f10.5,5x))') i,1,
2488 cd     &     (dc_norm(if90,i),if90=1,3)
2489 cd          write(iout,'(2i5,2(3f10.5,5x))') i,2,(uy(if90,i),if90=1,3)
2490 cd          write(iout,'(2i5,2(3f10.5,5x)/)')i,3,(uz(if90,i),if90=1,3)
2491 cd          write(iout,'(a)')
2492 cd      enddo
2493       do i=1,nres
2494         do j=1,2
2495           do k=1,3
2496             do l=1,3
2497               uygradt(l,k,j,i)=uygrad(l,k,j,i)
2498               uzgradt(l,k,j,i)=uzgrad(l,k,j,i)
2499             enddo
2500           enddo
2501         enddo
2502       enddo
2503       call vec_and_deriv
2504       do i=1,nres
2505         do j=1,3
2506           uyt(j,i)=uy(j,i)
2507           uzt(j,i)=uz(j,i)
2508         enddo
2509       enddo
2510       do i=1,nres
2511 cd        write (iout,*) 'i=',i
2512         do k=1,3
2513           erij(k)=dc_norm(k,i)
2514         enddo
2515         do j=1,3
2516           do k=1,3
2517             dc_norm(k,i)=erij(k)
2518           enddo
2519           dc_norm(j,i)=dc_norm(j,i)+delta
2520 c          fac=dsqrt(scalar(dc_norm(1,i),dc_norm(1,i)))
2521 c          do k=1,3
2522 c            dc_norm(k,i)=dc_norm(k,i)/fac
2523 c          enddo
2524 c          write (iout,*) (dc_norm(k,i),k=1,3)
2525 c          write (iout,*) (erij(k),k=1,3)
2526           call vec_and_deriv
2527           do k=1,3
2528             uygradn(k,j,1)=(uy(k,i)-uyt(k,i))/delta
2529             uygradn(k,j,2)=(uy(k,i-1)-uyt(k,i-1))/delta
2530             uzgradn(k,j,1)=(uz(k,i)-uzt(k,i))/delta
2531             uzgradn(k,j,2)=(uz(k,i-1)-uzt(k,i-1))/delta
2532           enddo 
2533 c          write (iout,'(i5,3f8.5,3x,3f8.5,5x,3f8.5,3x,3f8.5)') 
2534 c     &      j,(uzgradt(k,j,1,i),k=1,3),(uzgradn(k,j,1),k=1,3),
2535 c     &      (uzgradt(k,j,2,i-1),k=1,3),(uzgradn(k,j,2),k=1,3)
2536         enddo
2537         do k=1,3
2538           dc_norm(k,i)=erij(k)
2539         enddo
2540 cd        do k=1,3
2541 cd          write (iout,'(i5,3f8.5,3x,3f8.5,5x,3f8.5,3x,3f8.5)') 
2542 cd     &      k,(uygradt(k,l,1,i),l=1,3),(uygradn(k,l,1),l=1,3),
2543 cd     &      (uygradt(k,l,2,i-1),l=1,3),(uygradn(k,l,2),l=1,3)
2544 cd          write (iout,'(i5,3f8.5,3x,3f8.5,5x,3f8.5,3x,3f8.5)') 
2545 cd     &      k,(uzgradt(k,l,1,i),l=1,3),(uzgradn(k,l,1),l=1,3),
2546 cd     &      (uzgradt(k,l,2,i-1),l=1,3),(uzgradn(k,l,2),l=1,3)
2547 cd          write (iout,'(a)')
2548 cd        enddo
2549       enddo
2550       return
2551       end
2552 C--------------------------------------------------------------------------
2553       subroutine set_matrices
2554       implicit real*8 (a-h,o-z)
2555       include 'DIMENSIONS'
2556 #ifdef MPI
2557       include "mpif.h"
2558       include "COMMON.SETUP"
2559       integer IERR
2560       integer status(MPI_STATUS_SIZE)
2561 #endif
2562       include 'COMMON.IOUNITS'
2563       include 'COMMON.GEO'
2564       include 'COMMON.VAR'
2565       include 'COMMON.LOCAL'
2566       include 'COMMON.CHAIN'
2567       include 'COMMON.DERIV'
2568       include 'COMMON.INTERACT'
2569       include 'COMMON.CONTACTS'
2570       include 'COMMON.TORSION'
2571       include 'COMMON.VECTORS'
2572       include 'COMMON.FFIELD'
2573       double precision auxvec(2),auxmat(2,2)
2574 C
2575 C Compute the virtual-bond-torsional-angle dependent quantities needed
2576 C to calculate the el-loc multibody terms of various order.
2577 C
2578 #ifdef PARMAT
2579       do i=ivec_start+2,ivec_end+2
2580 #else
2581       do i=3,nres+1
2582 #endif
2583         if (i .lt. nres+1) then
2584           sin1=dsin(phi(i))
2585           cos1=dcos(phi(i))
2586           sintab(i-2)=sin1
2587           costab(i-2)=cos1
2588           obrot(1,i-2)=cos1
2589           obrot(2,i-2)=sin1
2590           sin2=dsin(2*phi(i))
2591           cos2=dcos(2*phi(i))
2592           sintab2(i-2)=sin2
2593           costab2(i-2)=cos2
2594           obrot2(1,i-2)=cos2
2595           obrot2(2,i-2)=sin2
2596           Ug(1,1,i-2)=-cos1
2597           Ug(1,2,i-2)=-sin1
2598           Ug(2,1,i-2)=-sin1
2599           Ug(2,2,i-2)= cos1
2600           Ug2(1,1,i-2)=-cos2
2601           Ug2(1,2,i-2)=-sin2
2602           Ug2(2,1,i-2)=-sin2
2603           Ug2(2,2,i-2)= cos2
2604         else
2605           costab(i-2)=1.0d0
2606           sintab(i-2)=0.0d0
2607           obrot(1,i-2)=1.0d0
2608           obrot(2,i-2)=0.0d0
2609           obrot2(1,i-2)=0.0d0
2610           obrot2(2,i-2)=0.0d0
2611           Ug(1,1,i-2)=1.0d0
2612           Ug(1,2,i-2)=0.0d0
2613           Ug(2,1,i-2)=0.0d0
2614           Ug(2,2,i-2)=1.0d0
2615           Ug2(1,1,i-2)=0.0d0
2616           Ug2(1,2,i-2)=0.0d0
2617           Ug2(2,1,i-2)=0.0d0
2618           Ug2(2,2,i-2)=0.0d0
2619         endif
2620         if (i .gt. 3 .and. i .lt. nres+1) then
2621           obrot_der(1,i-2)=-sin1
2622           obrot_der(2,i-2)= cos1
2623           Ugder(1,1,i-2)= sin1
2624           Ugder(1,2,i-2)=-cos1
2625           Ugder(2,1,i-2)=-cos1
2626           Ugder(2,2,i-2)=-sin1
2627           dwacos2=cos2+cos2
2628           dwasin2=sin2+sin2
2629           obrot2_der(1,i-2)=-dwasin2
2630           obrot2_der(2,i-2)= dwacos2
2631           Ug2der(1,1,i-2)= dwasin2
2632           Ug2der(1,2,i-2)=-dwacos2
2633           Ug2der(2,1,i-2)=-dwacos2
2634           Ug2der(2,2,i-2)=-dwasin2
2635         else
2636           obrot_der(1,i-2)=0.0d0
2637           obrot_der(2,i-2)=0.0d0
2638           Ugder(1,1,i-2)=0.0d0
2639           Ugder(1,2,i-2)=0.0d0
2640           Ugder(2,1,i-2)=0.0d0
2641           Ugder(2,2,i-2)=0.0d0
2642           obrot2_der(1,i-2)=0.0d0
2643           obrot2_der(2,i-2)=0.0d0
2644           Ug2der(1,1,i-2)=0.0d0
2645           Ug2der(1,2,i-2)=0.0d0
2646           Ug2der(2,1,i-2)=0.0d0
2647           Ug2der(2,2,i-2)=0.0d0
2648         endif
2649 c        if (i.gt. iatel_s+2 .and. i.lt.iatel_e+5) then
2650         if (i.gt. nnt+2 .and. i.lt.nct+2) then
2651           iti = itortyp(itype(i-2))
2652         else
2653           iti=ntortyp+1
2654         endif
2655 c        if (i.gt. iatel_s+1 .and. i.lt.iatel_e+4) then
2656         if (i.gt. nnt+1 .and. i.lt.nct+1) then
2657           iti1 = itortyp(itype(i-1))
2658         else
2659           iti1=ntortyp+1
2660         endif
2661 cd        write (iout,*) '*******i',i,' iti1',iti
2662 cd        write (iout,*) 'b1',b1(:,iti)
2663 cd        write (iout,*) 'b2',b2(:,iti)
2664 cd        write (iout,*) 'Ug',Ug(:,:,i-2)
2665 c        if (i .gt. iatel_s+2) then
2666         if (i .gt. nnt+2) then
2667           call matvec2(Ug(1,1,i-2),b2(1,iti),Ub2(1,i-2))
2668           call matmat2(EE(1,1,iti),Ug(1,1,i-2),EUg(1,1,i-2))
2669           if (wcorr4.gt.0.0d0 .or. wcorr5.gt.0.0d0 .or. wcorr6.gt.0.0d0) 
2670      &    then
2671           call matmat2(CC(1,1,iti),Ug(1,1,i-2),CUg(1,1,i-2))
2672           call matmat2(DD(1,1,iti),Ug(1,1,i-2),DUg(1,1,i-2))
2673           call matmat2(Dtilde(1,1,iti),Ug2(1,1,i-2),DtUg2(1,1,i-2))
2674           call matvec2(Ctilde(1,1,iti1),obrot(1,i-2),Ctobr(1,i-2))
2675           call matvec2(Dtilde(1,1,iti),obrot2(1,i-2),Dtobr2(1,i-2))
2676           endif
2677         else
2678           do k=1,2
2679             Ub2(k,i-2)=0.0d0
2680             Ctobr(k,i-2)=0.0d0 
2681             Dtobr2(k,i-2)=0.0d0
2682             do l=1,2
2683               EUg(l,k,i-2)=0.0d0
2684               CUg(l,k,i-2)=0.0d0
2685               DUg(l,k,i-2)=0.0d0
2686               DtUg2(l,k,i-2)=0.0d0
2687             enddo
2688           enddo
2689         endif
2690         call matvec2(Ugder(1,1,i-2),b2(1,iti),Ub2der(1,i-2))
2691         call matmat2(EE(1,1,iti),Ugder(1,1,i-2),EUgder(1,1,i-2))
2692         do k=1,2
2693           muder(k,i-2)=Ub2der(k,i-2)
2694         enddo
2695 c        if (i.gt. iatel_s+1 .and. i.lt.iatel_e+4) then
2696         if (i.gt. nnt+1 .and. i.lt.nct+1) then
2697           iti1 = itortyp(itype(i-1))
2698         else
2699           iti1=ntortyp+1
2700         endif
2701         do k=1,2
2702           mu(k,i-2)=Ub2(k,i-2)+b1(k,iti1)
2703         enddo
2704 cd        write (iout,*) 'mu ',mu(:,i-2)
2705 cd        write (iout,*) 'mu1',mu1(:,i-2)
2706 cd        write (iout,*) 'mu2',mu2(:,i-2)
2707         if (wcorr4.gt.0.0d0 .or. wcorr5.gt.0.0d0 .or.wcorr6.gt.0.0d0)
2708      &  then  
2709         call matmat2(CC(1,1,iti1),Ugder(1,1,i-2),CUgder(1,1,i-2))
2710         call matmat2(DD(1,1,iti),Ugder(1,1,i-2),DUgder(1,1,i-2))
2711         call matmat2(Dtilde(1,1,iti),Ug2der(1,1,i-2),DtUg2der(1,1,i-2))
2712         call matvec2(Ctilde(1,1,iti1),obrot_der(1,i-2),Ctobrder(1,i-2))
2713         call matvec2(Dtilde(1,1,iti),obrot2_der(1,i-2),Dtobr2der(1,i-2))
2714 C Vectors and matrices dependent on a single virtual-bond dihedral.
2715         call matvec2(DD(1,1,iti),b1tilde(1,iti1),auxvec(1))
2716         call matvec2(Ug2(1,1,i-2),auxvec(1),Ug2Db1t(1,i-2)) 
2717         call matvec2(Ug2der(1,1,i-2),auxvec(1),Ug2Db1tder(1,i-2)) 
2718         call matvec2(CC(1,1,iti1),Ub2(1,i-2),CUgb2(1,i-2))
2719         call matvec2(CC(1,1,iti1),Ub2der(1,i-2),CUgb2der(1,i-2))
2720         call matmat2(EUg(1,1,i-2),CC(1,1,iti1),EUgC(1,1,i-2))
2721         call matmat2(EUgder(1,1,i-2),CC(1,1,iti1),EUgCder(1,1,i-2))
2722         call matmat2(EUg(1,1,i-2),DD(1,1,iti1),EUgD(1,1,i-2))
2723         call matmat2(EUgder(1,1,i-2),DD(1,1,iti1),EUgDder(1,1,i-2))
2724         endif
2725       enddo
2726 C Matrices dependent on two consecutive virtual-bond dihedrals.
2727 C The order of matrices is from left to right.
2728       if (wcorr4.gt.0.0d0 .or. wcorr5.gt.0.0d0 .or.wcorr6.gt.0.0d0)
2729      &then
2730 c      do i=max0(ivec_start,2),ivec_end
2731       do i=2,nres-1
2732         call matmat2(DtUg2(1,1,i-1),EUg(1,1,i),DtUg2EUg(1,1,i))
2733         call matmat2(DtUg2der(1,1,i-1),EUg(1,1,i),DtUg2EUgder(1,1,1,i))
2734         call matmat2(DtUg2(1,1,i-1),EUgder(1,1,i),DtUg2EUgder(1,1,2,i))
2735         call transpose2(DtUg2(1,1,i-1),auxmat(1,1))
2736         call matmat2(auxmat(1,1),EUg(1,1,i),Ug2DtEUg(1,1,i))
2737         call matmat2(auxmat(1,1),EUgder(1,1,i),Ug2DtEUgder(1,1,2,i))
2738         call transpose2(DtUg2der(1,1,i-1),auxmat(1,1))
2739         call matmat2(auxmat(1,1),EUg(1,1,i),Ug2DtEUgder(1,1,1,i))
2740       enddo
2741       endif
2742 #if defined(MPI) && defined(PARMAT)
2743 #ifdef DEBUG
2744 c      if (fg_rank.eq.0) then
2745         write (iout,*) "Arrays UG and UGDER before GATHER"
2746         do i=1,nres-1
2747           write (iout,'(i5,4f10.5,5x,4f10.5)') i,
2748      &     ((ug(l,k,i),l=1,2),k=1,2),
2749      &     ((ugder(l,k,i),l=1,2),k=1,2)
2750         enddo
2751         write (iout,*) "Arrays UG2 and UG2DER"
2752         do i=1,nres-1
2753           write (iout,'(i5,4f10.5,5x,4f10.5)') i,
2754      &     ((ug2(l,k,i),l=1,2),k=1,2),
2755      &     ((ug2der(l,k,i),l=1,2),k=1,2)
2756         enddo
2757         write (iout,*) "Arrays OBROT OBROT2 OBROTDER and OBROT2DER"
2758         do i=1,nres-1
2759           write (iout,'(i5,4f10.5,5x,4f10.5)') i,
2760      &     (obrot(k,i),k=1,2),(obrot2(k,i),k=1,2),
2761      &     (obrot_der(k,i),k=1,2),(obrot2_der(k,i),k=1,2)
2762         enddo
2763         write (iout,*) "Arrays COSTAB SINTAB COSTAB2 and SINTAB2"
2764         do i=1,nres-1
2765           write (iout,'(i5,4f10.5,5x,4f10.5)') i,
2766      &     costab(i),sintab(i),costab2(i),sintab2(i)
2767         enddo
2768         write (iout,*) "Array MUDER"
2769         do i=1,nres-1
2770           write (iout,'(i5,2f10.5)') i,muder(1,i),muder(2,i)
2771         enddo
2772 c      endif
2773 #endif
2774       if (nfgtasks.gt.1) then
2775         time00=MPI_Wtime()
2776 c        write(iout,*)"Processor",fg_rank,kolor," ivec_start",ivec_start,
2777 c     &   " ivec_displ",(ivec_displ(i),i=0,nfgtasks-1),
2778 c     &   " ivec_count",(ivec_count(i),i=0,nfgtasks-1)
2779 #ifdef MATGATHER
2780         call MPI_Allgatherv(Ub2(1,ivec_start),ivec_count(fg_rank1),
2781      &   MPI_MU,Ub2(1,1),ivec_count(0),ivec_displ(0),MPI_MU,
2782      &   FG_COMM1,IERR)
2783         call MPI_Allgatherv(Ub2der(1,ivec_start),ivec_count(fg_rank1),
2784      &   MPI_MU,Ub2der(1,1),ivec_count(0),ivec_displ(0),MPI_MU,
2785      &   FG_COMM1,IERR)
2786         call MPI_Allgatherv(mu(1,ivec_start),ivec_count(fg_rank1),
2787      &   MPI_MU,mu(1,1),ivec_count(0),ivec_displ(0),MPI_MU,
2788      &   FG_COMM1,IERR)
2789         call MPI_Allgatherv(muder(1,ivec_start),ivec_count(fg_rank1),
2790      &   MPI_MU,muder(1,1),ivec_count(0),ivec_displ(0),MPI_MU,
2791      &   FG_COMM1,IERR)
2792         call MPI_Allgatherv(Eug(1,1,ivec_start),ivec_count(fg_rank1),
2793      &   MPI_MAT1,Eug(1,1,1),ivec_count(0),ivec_displ(0),MPI_MAT1,
2794      &   FG_COMM1,IERR)
2795         call MPI_Allgatherv(Eugder(1,1,ivec_start),ivec_count(fg_rank1),
2796      &   MPI_MAT1,Eugder(1,1,1),ivec_count(0),ivec_displ(0),MPI_MAT1,
2797      &   FG_COMM1,IERR)
2798         call MPI_Allgatherv(costab(ivec_start),ivec_count(fg_rank1),
2799      &   MPI_DOUBLE_PRECISION,costab(1),ivec_count(0),ivec_displ(0),
2800      &   MPI_DOUBLE_PRECISION,FG_COMM1,IERR)
2801         call MPI_Allgatherv(sintab(ivec_start),ivec_count(fg_rank1),
2802      &   MPI_DOUBLE_PRECISION,sintab(1),ivec_count(0),ivec_displ(0),
2803      &   MPI_DOUBLE_PRECISION,FG_COMM1,IERR)
2804         call MPI_Allgatherv(costab2(ivec_start),ivec_count(fg_rank1),
2805      &   MPI_DOUBLE_PRECISION,costab2(1),ivec_count(0),ivec_displ(0),
2806      &   MPI_DOUBLE_PRECISION,FG_COMM1,IERR)
2807         call MPI_Allgatherv(sintab2(ivec_start),ivec_count(fg_rank1),
2808      &   MPI_DOUBLE_PRECISION,sintab2(1),ivec_count(0),ivec_displ(0),
2809      &   MPI_DOUBLE_PRECISION,FG_COMM1,IERR)
2810         if (wcorr4.gt.0.0d0 .or. wcorr5.gt.0.0d0 .or. wcorr6.gt.0.0d0)
2811      &  then
2812         call MPI_Allgatherv(Ctobr(1,ivec_start),ivec_count(fg_rank1),
2813      &   MPI_MU,Ctobr(1,1),ivec_count(0),ivec_displ(0),MPI_MU,
2814      &   FG_COMM1,IERR)
2815         call MPI_Allgatherv(Ctobrder(1,ivec_start),ivec_count(fg_rank1),
2816      &   MPI_MU,Ctobrder(1,1),ivec_count(0),ivec_displ(0),MPI_MU,
2817      &   FG_COMM1,IERR)
2818         call MPI_Allgatherv(Dtobr2(1,ivec_start),ivec_count(fg_rank1),
2819      &   MPI_MU,Dtobr2(1,1),ivec_count(0),ivec_displ(0),MPI_MU,
2820      &   FG_COMM1,IERR)
2821        call MPI_Allgatherv(Dtobr2der(1,ivec_start),ivec_count(fg_rank1),
2822      &   MPI_MU,Dtobr2der(1,1),ivec_count(0),ivec_displ(0),MPI_MU,
2823      &   FG_COMM1,IERR)
2824         call MPI_Allgatherv(Ug2Db1t(1,ivec_start),ivec_count(fg_rank1),
2825      &   MPI_MU,Ug2Db1t(1,1),ivec_count(0),ivec_displ(0),MPI_MU,
2826      &   FG_COMM1,IERR)
2827         call MPI_Allgatherv(Ug2Db1tder(1,ivec_start),
2828      &   ivec_count(fg_rank1),
2829      &   MPI_MU,Ug2Db1tder(1,1),ivec_count(0),ivec_displ(0),MPI_MU,
2830      &   FG_COMM1,IERR)
2831         call MPI_Allgatherv(CUgb2(1,ivec_start),ivec_count(fg_rank1),
2832      &   MPI_MU,CUgb2(1,1),ivec_count(0),ivec_displ(0),MPI_MU,
2833      &   FG_COMM1,IERR)
2834         call MPI_Allgatherv(CUgb2der(1,ivec_start),ivec_count(fg_rank1),
2835      &   MPI_MU,CUgb2der(1,1),ivec_count(0),ivec_displ(0),MPI_MU,
2836      &   FG_COMM1,IERR)
2837         call MPI_Allgatherv(Cug(1,1,ivec_start),ivec_count(fg_rank1),
2838      &   MPI_MAT1,Cug(1,1,1),ivec_count(0),ivec_displ(0),MPI_MAT1,
2839      &   FG_COMM1,IERR)
2840         call MPI_Allgatherv(Cugder(1,1,ivec_start),ivec_count(fg_rank1),
2841      &   MPI_MAT1,Cugder(1,1,1),ivec_count(0),ivec_displ(0),MPI_MAT1,
2842      &   FG_COMM1,IERR)
2843         call MPI_Allgatherv(Dug(1,1,ivec_start),ivec_count(fg_rank1),
2844      &   MPI_MAT1,Dug(1,1,1),ivec_count(0),ivec_displ(0),MPI_MAT1,
2845      &   FG_COMM1,IERR)
2846         call MPI_Allgatherv(Dugder(1,1,ivec_start),ivec_count(fg_rank1),
2847      &   MPI_MAT1,Dugder(1,1,1),ivec_count(0),ivec_displ(0),MPI_MAT1,
2848      &   FG_COMM1,IERR)
2849         call MPI_Allgatherv(Dtug2(1,1,ivec_start),ivec_count(fg_rank1),
2850      &   MPI_MAT1,Dtug2(1,1,1),ivec_count(0),ivec_displ(0),MPI_MAT1,
2851      &   FG_COMM1,IERR)
2852         call MPI_Allgatherv(Dtug2der(1,1,ivec_start),
2853      &   ivec_count(fg_rank1),
2854      &   MPI_MAT1,Dtug2der(1,1,1),ivec_count(0),ivec_displ(0),MPI_MAT1,
2855      &   FG_COMM1,IERR)
2856         call MPI_Allgatherv(EugC(1,1,ivec_start),ivec_count(fg_rank1),
2857      &   MPI_MAT1,EugC(1,1,1),ivec_count(0),ivec_displ(0),MPI_MAT1,
2858      &   FG_COMM1,IERR)
2859        call MPI_Allgatherv(EugCder(1,1,ivec_start),ivec_count(fg_rank1),
2860      &   MPI_MAT1,EugCder(1,1,1),ivec_count(0),ivec_displ(0),MPI_MAT1,
2861      &   FG_COMM1,IERR)
2862         call MPI_Allgatherv(EugD(1,1,ivec_start),ivec_count(fg_rank1),
2863      &   MPI_MAT1,EugD(1,1,1),ivec_count(0),ivec_displ(0),MPI_MAT1,
2864      &   FG_COMM1,IERR)
2865        call MPI_Allgatherv(EugDder(1,1,ivec_start),ivec_count(fg_rank1),
2866      &   MPI_MAT1,EugDder(1,1,1),ivec_count(0),ivec_displ(0),MPI_MAT1,
2867      &   FG_COMM1,IERR)
2868         call MPI_Allgatherv(DtUg2EUg(1,1,ivec_start),
2869      &   ivec_count(fg_rank1),
2870      &   MPI_MAT1,DtUg2EUg(1,1,1),ivec_count(0),ivec_displ(0),MPI_MAT1,
2871      &   FG_COMM1,IERR)
2872         call MPI_Allgatherv(Ug2DtEUg(1,1,ivec_start),
2873      &   ivec_count(fg_rank1),
2874      &   MPI_MAT1,Ug2DtEUg(1,1,1),ivec_count(0),ivec_displ(0),MPI_MAT1,
2875      &   FG_COMM1,IERR)
2876         call MPI_Allgatherv(DtUg2EUgder(1,1,1,ivec_start),
2877      &   ivec_count(fg_rank1),
2878      &   MPI_MAT2,DtUg2EUgder(1,1,1,1),ivec_count(0),ivec_displ(0),
2879      &   MPI_MAT2,FG_COMM1,IERR)
2880         call MPI_Allgatherv(Ug2DtEUgder(1,1,1,ivec_start),
2881      &   ivec_count(fg_rank1),
2882      &   MPI_MAT2,Ug2DtEUgder(1,1,1,1),ivec_count(0),ivec_displ(0),
2883      &   MPI_MAT2,FG_COMM1,IERR)
2884         endif
2885 #else
2886 c Passes matrix info through the ring
2887       isend=fg_rank1
2888       irecv=fg_rank1-1
2889       if (irecv.lt.0) irecv=nfgtasks1-1 
2890       iprev=irecv
2891       inext=fg_rank1+1
2892       if (inext.ge.nfgtasks1) inext=0
2893       do i=1,nfgtasks1-1
2894 c        write (iout,*) "isend",isend," irecv",irecv
2895 c        call flush(iout)
2896         lensend=lentyp(isend)
2897         lenrecv=lentyp(irecv)
2898 c        write (iout,*) "lensend",lensend," lenrecv",lenrecv
2899 c        call MPI_SENDRECV(ug(1,1,ivec_displ(isend)+1),1,
2900 c     &   MPI_ROTAT1(lensend),inext,2200+isend,
2901 c     &   ug(1,1,ivec_displ(irecv)+1),1,MPI_ROTAT1(lenrecv),
2902 c     &   iprev,2200+irecv,FG_COMM,status,IERR)
2903 c        write (iout,*) "Gather ROTAT1"
2904 c        call flush(iout)
2905 c        call MPI_SENDRECV(obrot(1,ivec_displ(isend)+1),1,
2906 c     &   MPI_ROTAT2(lensend),inext,3300+isend,
2907 c     &   obrot(1,ivec_displ(irecv)+1),1,MPI_ROTAT2(lenrecv),
2908 c     &   iprev,3300+irecv,FG_COMM,status,IERR)
2909 c        write (iout,*) "Gather ROTAT2"
2910 c        call flush(iout)
2911         call MPI_SENDRECV(costab(ivec_displ(isend)+1),1,
2912      &   MPI_ROTAT_OLD(lensend),inext,4400+isend,
2913      &   costab(ivec_displ(irecv)+1),1,MPI_ROTAT_OLD(lenrecv),
2914      &   iprev,4400+irecv,FG_COMM,status,IERR)
2915 c        write (iout,*) "Gather ROTAT_OLD"
2916 c        call flush(iout)
2917         call MPI_SENDRECV(mu(1,ivec_displ(isend)+1),1,
2918      &   MPI_PRECOMP11(lensend),inext,5500+isend,
2919      &   mu(1,ivec_displ(irecv)+1),1,MPI_PRECOMP11(lenrecv),
2920      &   iprev,5500+irecv,FG_COMM,status,IERR)
2921 c        write (iout,*) "Gather PRECOMP11"
2922 c        call flush(iout)
2923         call MPI_SENDRECV(Eug(1,1,ivec_displ(isend)+1),1,
2924      &   MPI_PRECOMP12(lensend),inext,6600+isend,
2925      &   Eug(1,1,ivec_displ(irecv)+1),1,MPI_PRECOMP12(lenrecv),
2926      &   iprev,6600+irecv,FG_COMM,status,IERR)
2927 c        write (iout,*) "Gather PRECOMP12"
2928 c        call flush(iout)
2929         if (wcorr4.gt.0.0d0 .or. wcorr5.gt.0.0d0 .or. wcorr6.gt.0.0d0) 
2930      &  then
2931         call MPI_SENDRECV(ug2db1t(1,ivec_displ(isend)+1),1,
2932      &   MPI_ROTAT2(lensend),inext,7700+isend,
2933      &   ug2db1t(1,ivec_displ(irecv)+1),1,MPI_ROTAT2(lenrecv),
2934      &   iprev,7700+irecv,FG_COMM,status,IERR)
2935 c        write (iout,*) "Gather PRECOMP21"
2936 c        call flush(iout)
2937         call MPI_SENDRECV(EUgC(1,1,ivec_displ(isend)+1),1,
2938      &   MPI_PRECOMP22(lensend),inext,8800+isend,
2939      &   EUgC(1,1,ivec_displ(irecv)+1),1,MPI_PRECOMP22(lenrecv),
2940      &   iprev,8800+irecv,FG_COMM,status,IERR)
2941 c        write (iout,*) "Gather PRECOMP22"
2942 c        call flush(iout)
2943         call MPI_SENDRECV(Ug2DtEUgder(1,1,1,ivec_displ(isend)+1),1,
2944      &   MPI_PRECOMP23(lensend),inext,9900+isend,
2945      &   Ug2DtEUgder(1,1,1,ivec_displ(irecv)+1),1,
2946      &   MPI_PRECOMP23(lenrecv),
2947      &   iprev,9900+irecv,FG_COMM,status,IERR)
2948 c        write (iout,*) "Gather PRECOMP23"
2949 c        call flush(iout)
2950         endif
2951         isend=irecv
2952         irecv=irecv-1
2953         if (irecv.lt.0) irecv=nfgtasks1-1
2954       enddo
2955 #endif
2956         time_gather=time_gather+MPI_Wtime()-time00
2957       endif
2958 #ifdef DEBUG
2959 c      if (fg_rank.eq.0) then
2960         write (iout,*) "Arrays UG and UGDER"
2961         do i=1,nres-1
2962           write (iout,'(i5,4f10.5,5x,4f10.5)') i,
2963      &     ((ug(l,k,i),l=1,2),k=1,2),
2964      &     ((ugder(l,k,i),l=1,2),k=1,2)
2965         enddo
2966         write (iout,*) "Arrays UG2 and UG2DER"
2967         do i=1,nres-1
2968           write (iout,'(i5,4f10.5,5x,4f10.5)') i,
2969      &     ((ug2(l,k,i),l=1,2),k=1,2),
2970      &     ((ug2der(l,k,i),l=1,2),k=1,2)
2971         enddo
2972         write (iout,*) "Arrays OBROT OBROT2 OBROTDER and OBROT2DER"
2973         do i=1,nres-1
2974           write (iout,'(i5,4f10.5,5x,4f10.5)') i,
2975      &     (obrot(k,i),k=1,2),(obrot2(k,i),k=1,2),
2976      &     (obrot_der(k,i),k=1,2),(obrot2_der(k,i),k=1,2)
2977         enddo
2978         write (iout,*) "Arrays COSTAB SINTAB COSTAB2 and SINTAB2"
2979         do i=1,nres-1
2980           write (iout,'(i5,4f10.5,5x,4f10.5)') i,
2981      &     costab(i),sintab(i),costab2(i),sintab2(i)
2982         enddo
2983         write (iout,*) "Array MUDER"
2984         do i=1,nres-1
2985           write (iout,'(i5,2f10.5)') i,muder(1,i),muder(2,i)
2986         enddo
2987 c      endif
2988 #endif
2989 #endif
2990 cd      do i=1,nres
2991 cd        iti = itortyp(itype(i))
2992 cd        write (iout,*) i
2993 cd        do j=1,2
2994 cd        write (iout,'(2f10.5,5x,2f10.5,5x,2f10.5)') 
2995 cd     &  (EE(j,k,iti),k=1,2),(Ug(j,k,i),k=1,2),(EUg(j,k,i),k=1,2)
2996 cd        enddo
2997 cd      enddo
2998       return
2999       end
3000 C--------------------------------------------------------------------------
3001       subroutine eelec(ees,evdw1,eel_loc,eello_turn3,eello_turn4)
3002 C
3003 C This subroutine calculates the average interaction energy and its gradient
3004 C in the virtual-bond vectors between non-adjacent peptide groups, based on 
3005 C the potential described in Liwo et al., Protein Sci., 1993, 2, 1715. 
3006 C The potential depends both on the distance of peptide-group centers and on 
3007 C the orientation of the CA-CA virtual bonds.
3008
3009       implicit real*8 (a-h,o-z)
3010 #ifdef MPI
3011       include 'mpif.h'
3012 #endif
3013       include 'DIMENSIONS'
3014       include 'COMMON.CONTROL'
3015       include 'COMMON.SETUP'
3016       include 'COMMON.IOUNITS'
3017       include 'COMMON.GEO'
3018       include 'COMMON.VAR'
3019       include 'COMMON.LOCAL'
3020       include 'COMMON.CHAIN'
3021       include 'COMMON.DERIV'
3022       include 'COMMON.INTERACT'
3023       include 'COMMON.CONTACTS'
3024       include 'COMMON.TORSION'
3025       include 'COMMON.VECTORS'
3026       include 'COMMON.FFIELD'
3027       include 'COMMON.TIME1'
3028       dimension ggg(3),gggp(3),gggm(3),erij(3),dcosb(3),dcosg(3),
3029      &          erder(3,3),uryg(3,3),urzg(3,3),vryg(3,3),vrzg(3,3)
3030       double precision acipa(2,2),agg(3,4),aggi(3,4),aggi1(3,4),
3031      &    aggj(3,4),aggj1(3,4),a_temp(2,2),muij(4)
3032       common /locel/ a_temp,agg,aggi,aggi1,aggj,aggj1,a22,a23,a32,a33,
3033      &    dxi,dyi,dzi,dx_normi,dy_normi,dz_normi,xmedi,ymedi,zmedi,
3034      &    num_conti,j1,j2
3035 c 4/26/02 - AL scaling factor for 1,4 repulsive VDW interactions
3036 #ifdef MOMENT
3037       double precision scal_el /1.0d0/
3038 #else
3039       double precision scal_el /0.5d0/
3040 #endif
3041 C 12/13/98 
3042 C 13-go grudnia roku pamietnego... 
3043       double precision unmat(3,3) /1.0d0,0.0d0,0.0d0,
3044      &                   0.0d0,1.0d0,0.0d0,
3045      &                   0.0d0,0.0d0,1.0d0/
3046 cd      write(iout,*) 'In EELEC'
3047 cd      do i=1,nloctyp
3048 cd        write(iout,*) 'Type',i
3049 cd        write(iout,*) 'B1',B1(:,i)
3050 cd        write(iout,*) 'B2',B2(:,i)
3051 cd        write(iout,*) 'CC',CC(:,:,i)
3052 cd        write(iout,*) 'DD',DD(:,:,i)
3053 cd        write(iout,*) 'EE',EE(:,:,i)
3054 cd      enddo
3055 cd      call check_vecgrad
3056 cd      stop
3057       if (icheckgrad.eq.1) then
3058         do i=1,nres-1
3059           fac=1.0d0/dsqrt(scalar(dc(1,i),dc(1,i)))
3060           do k=1,3
3061             dc_norm(k,i)=dc(k,i)*fac
3062           enddo
3063 c          write (iout,*) 'i',i,' fac',fac
3064         enddo
3065       endif
3066       if (wel_loc.gt.0.0d0 .or. wcorr4.gt.0.0d0 .or. wcorr5.gt.0.0d0 
3067      &    .or. wcorr6.gt.0.0d0 .or. wturn3.gt.0.0d0 .or. 
3068      &    wturn4.gt.0.0d0 .or. wturn6.gt.0.0d0) then
3069 c        call vec_and_deriv
3070 #ifdef TIMING
3071         time01=MPI_Wtime()
3072 #endif
3073         call set_matrices
3074 #ifdef TIMING
3075         time_mat=time_mat+MPI_Wtime()-time01
3076 #endif
3077       endif
3078 cd      do i=1,nres-1
3079 cd        write (iout,*) 'i=',i
3080 cd        do k=1,3
3081 cd        write (iout,'(i5,2f10.5)') k,uy(k,i),uz(k,i)
3082 cd        enddo
3083 cd        do k=1,3
3084 cd          write (iout,'(f10.5,2x,3f10.5,2x,3f10.5)') 
3085 cd     &     uz(k,i),(uzgrad(k,l,1,i),l=1,3),(uzgrad(k,l,2,i),l=1,3)
3086 cd        enddo
3087 cd      enddo
3088       t_eelecij=0.0d0
3089       ees=0.0D0
3090       evdw1=0.0D0
3091       eel_loc=0.0d0 
3092       eello_turn3=0.0d0
3093       eello_turn4=0.0d0
3094       ind=0
3095       do i=1,nres
3096         num_cont_hb(i)=0
3097       enddo
3098 cd      print '(a)','Enter EELEC'
3099 cd      write (iout,*) 'iatel_s=',iatel_s,' iatel_e=',iatel_e
3100       do i=1,nres
3101         gel_loc_loc(i)=0.0d0
3102         gcorr_loc(i)=0.0d0
3103       enddo
3104 c
3105 c
3106 c 9/27/08 AL Split the interaction loop to ensure load balancing of turn terms
3107 C
3108 C Loop over i,i+2 and i,i+3 pairs of the peptide groups
3109 C
3110       do i=iturn3_start,iturn3_end
3111         dxi=dc(1,i)
3112         dyi=dc(2,i)
3113         dzi=dc(3,i)
3114         dx_normi=dc_norm(1,i)
3115         dy_normi=dc_norm(2,i)
3116         dz_normi=dc_norm(3,i)
3117         xmedi=c(1,i)+0.5d0*dxi
3118         ymedi=c(2,i)+0.5d0*dyi
3119         zmedi=c(3,i)+0.5d0*dzi
3120         num_conti=0
3121         call eelecij(i,i+2,ees,evdw1,eel_loc)
3122         if (wturn3.gt.0.0d0) call eturn3(i,eello_turn3)
3123         num_cont_hb(i)=num_conti
3124       enddo
3125       do i=iturn4_start,iturn4_end
3126         dxi=dc(1,i)
3127         dyi=dc(2,i)
3128         dzi=dc(3,i)
3129         dx_normi=dc_norm(1,i)
3130         dy_normi=dc_norm(2,i)
3131         dz_normi=dc_norm(3,i)
3132         xmedi=c(1,i)+0.5d0*dxi
3133         ymedi=c(2,i)+0.5d0*dyi
3134         zmedi=c(3,i)+0.5d0*dzi
3135         num_conti=num_cont_hb(i)
3136         call eelecij(i,i+3,ees,evdw1,eel_loc)
3137         if (wturn4.gt.0.0d0) call eturn4(i,eello_turn4)
3138         num_cont_hb(i)=num_conti
3139       enddo   ! i
3140 c
3141 c Loop over all pairs of interacting peptide groups except i,i+2 and i,i+3
3142 c
3143       do i=iatel_s,iatel_e
3144         dxi=dc(1,i)
3145         dyi=dc(2,i)
3146         dzi=dc(3,i)
3147         dx_normi=dc_norm(1,i)
3148         dy_normi=dc_norm(2,i)
3149         dz_normi=dc_norm(3,i)
3150         xmedi=c(1,i)+0.5d0*dxi
3151         ymedi=c(2,i)+0.5d0*dyi
3152         zmedi=c(3,i)+0.5d0*dzi
3153 c        write (iout,*) 'i',i,' ielstart',ielstart(i),' ielend',ielend(i)
3154         num_conti=num_cont_hb(i)
3155         do j=ielstart(i),ielend(i)
3156           call eelecij(i,j,ees,evdw1,eel_loc)
3157         enddo ! j
3158         num_cont_hb(i)=num_conti
3159       enddo   ! i
3160 c      write (iout,*) "Number of loop steps in EELEC:",ind
3161 cd      do i=1,nres
3162 cd        write (iout,'(i3,3f10.5,5x,3f10.5)') 
3163 cd     &     i,(gel_loc(k,i),k=1,3),gel_loc_loc(i)
3164 cd      enddo
3165 c 12/7/99 Adam eello_turn3 will be considered as a separate energy term
3166 ccc      eel_loc=eel_loc+eello_turn3
3167 cd      print *,"Processor",fg_rank," t_eelecij",t_eelecij
3168       return
3169       end
3170 C-------------------------------------------------------------------------------
3171       subroutine eelecij(i,j,ees,evdw1,eel_loc)
3172       implicit real*8 (a-h,o-z)
3173       include 'DIMENSIONS'
3174 #ifdef MPI
3175       include "mpif.h"
3176 #endif
3177       include 'COMMON.CONTROL'
3178       include 'COMMON.IOUNITS'
3179       include 'COMMON.GEO'
3180       include 'COMMON.VAR'
3181       include 'COMMON.LOCAL'
3182       include 'COMMON.CHAIN'
3183       include 'COMMON.DERIV'
3184       include 'COMMON.INTERACT'
3185       include 'COMMON.CONTACTS'
3186       include 'COMMON.TORSION'
3187       include 'COMMON.VECTORS'
3188       include 'COMMON.FFIELD'
3189       include 'COMMON.TIME1'
3190       dimension ggg(3),gggp(3),gggm(3),erij(3),dcosb(3),dcosg(3),
3191      &          erder(3,3),uryg(3,3),urzg(3,3),vryg(3,3),vrzg(3,3)
3192       double precision acipa(2,2),agg(3,4),aggi(3,4),aggi1(3,4),
3193      &    aggj(3,4),aggj1(3,4),a_temp(2,2),muij(4)
3194       common /locel/ a_temp,agg,aggi,aggi1,aggj,aggj1,a22,a23,a32,a33,
3195      &    dxi,dyi,dzi,dx_normi,dy_normi,dz_normi,xmedi,ymedi,zmedi,
3196      &    num_conti,j1,j2
3197 c 4/26/02 - AL scaling factor for 1,4 repulsive VDW interactions
3198 #ifdef MOMENT
3199       double precision scal_el /1.0d0/
3200 #else
3201       double precision scal_el /0.5d0/
3202 #endif
3203 C 12/13/98 
3204 C 13-go grudnia roku pamietnego... 
3205       double precision unmat(3,3) /1.0d0,0.0d0,0.0d0,
3206      &                   0.0d0,1.0d0,0.0d0,
3207      &                   0.0d0,0.0d0,1.0d0/
3208 c          time00=MPI_Wtime()
3209 cd      write (iout,*) "eelecij",i,j
3210 c          ind=ind+1
3211           iteli=itel(i)
3212           itelj=itel(j)
3213           if (j.eq.i+2 .and. itelj.eq.2) iteli=2
3214           aaa=app(iteli,itelj)
3215           bbb=bpp(iteli,itelj)
3216           ael6i=ael6(iteli,itelj)
3217           ael3i=ael3(iteli,itelj) 
3218           dxj=dc(1,j)
3219           dyj=dc(2,j)
3220           dzj=dc(3,j)
3221           dx_normj=dc_norm(1,j)
3222           dy_normj=dc_norm(2,j)
3223           dz_normj=dc_norm(3,j)
3224           xj=c(1,j)+0.5D0*dxj-xmedi
3225           yj=c(2,j)+0.5D0*dyj-ymedi
3226           zj=c(3,j)+0.5D0*dzj-zmedi
3227           rij=xj*xj+yj*yj+zj*zj
3228           rrmij=1.0D0/rij
3229           rij=dsqrt(rij)
3230           rmij=1.0D0/rij
3231           r3ij=rrmij*rmij
3232           r6ij=r3ij*r3ij  
3233           cosa=dx_normi*dx_normj+dy_normi*dy_normj+dz_normi*dz_normj
3234           cosb=(xj*dx_normi+yj*dy_normi+zj*dz_normi)*rmij
3235           cosg=(xj*dx_normj+yj*dy_normj+zj*dz_normj)*rmij
3236           fac=cosa-3.0D0*cosb*cosg
3237           ev1=aaa*r6ij*r6ij
3238 c 4/26/02 - AL scaling down 1,4 repulsive VDW interactions
3239           if (j.eq.i+2) ev1=scal_el*ev1
3240           ev2=bbb*r6ij
3241           fac3=ael6i*r6ij
3242           fac4=ael3i*r3ij
3243           evdwij=ev1+ev2
3244           el1=fac3*(4.0D0+fac*fac-3.0D0*(cosb*cosb+cosg*cosg))
3245           el2=fac4*fac       
3246           eesij=el1+el2
3247 C 12/26/95 - for the evaluation of multi-body H-bonding interactions
3248           ees0ij=4.0D0+fac*fac-3.0D0*(cosb*cosb+cosg*cosg)
3249           ees=ees+eesij
3250           evdw1=evdw1+evdwij
3251 cd          write(iout,'(2(2i3,2x),7(1pd12.4)/2(3(1pd12.4),5x)/)')
3252 cd     &      iteli,i,itelj,j,aaa,bbb,ael6i,ael3i,
3253 cd     &      1.0D0/dsqrt(rrmij),evdwij,eesij,
3254 cd     &      xmedi,ymedi,zmedi,xj,yj,zj
3255
3256           if (energy_dec) then 
3257               write (iout,'(a6,2i5,0pf7.3)') 'evdw1',i,j,evdwij
3258               write (iout,'(a6,2i5,0pf7.3)') 'ees',i,j,eesij
3259           endif
3260
3261 C
3262 C Calculate contributions to the Cartesian gradient.
3263 C
3264 #ifdef SPLITELE
3265           facvdw=-6*rrmij*(ev1+evdwij)
3266           facel=-3*rrmij*(el1+eesij)
3267           fac1=fac
3268           erij(1)=xj*rmij
3269           erij(2)=yj*rmij
3270           erij(3)=zj*rmij
3271 *
3272 * Radial derivatives. First process both termini of the fragment (i,j)
3273 *
3274           ggg(1)=facel*xj
3275           ggg(2)=facel*yj
3276           ggg(3)=facel*zj
3277 c          do k=1,3
3278 c            ghalf=0.5D0*ggg(k)
3279 c            gelc(k,i)=gelc(k,i)+ghalf
3280 c            gelc(k,j)=gelc(k,j)+ghalf
3281 c          enddo
3282 c 9/28/08 AL Gradient compotents will be summed only at the end
3283           do k=1,3
3284             gelc_long(k,j)=gelc_long(k,j)+ggg(k)
3285             gelc_long(k,i)=gelc_long(k,i)-ggg(k)
3286           enddo
3287 *
3288 * Loop over residues i+1 thru j-1.
3289 *
3290 cgrad          do k=i+1,j-1
3291 cgrad            do l=1,3
3292 cgrad              gelc(l,k)=gelc(l,k)+ggg(l)
3293 cgrad            enddo
3294 cgrad          enddo
3295           ggg(1)=facvdw*xj
3296           ggg(2)=facvdw*yj
3297           ggg(3)=facvdw*zj
3298 c          do k=1,3
3299 c            ghalf=0.5D0*ggg(k)
3300 c            gvdwpp(k,i)=gvdwpp(k,i)+ghalf
3301 c            gvdwpp(k,j)=gvdwpp(k,j)+ghalf
3302 c          enddo
3303 c 9/28/08 AL Gradient compotents will be summed only at the end
3304           do k=1,3
3305             gvdwpp(k,j)=gvdwpp(k,j)+ggg(k)
3306             gvdwpp(k,i)=gvdwpp(k,i)-ggg(k)
3307           enddo
3308 *
3309 * Loop over residues i+1 thru j-1.
3310 *
3311 cgrad          do k=i+1,j-1
3312 cgrad            do l=1,3
3313 cgrad              gvdwpp(l,k)=gvdwpp(l,k)+ggg(l)
3314 cgrad            enddo
3315 cgrad          enddo
3316 #else
3317           facvdw=ev1+evdwij 
3318           facel=el1+eesij  
3319           fac1=fac
3320           fac=-3*rrmij*(facvdw+facvdw+facel)
3321           erij(1)=xj*rmij
3322           erij(2)=yj*rmij
3323           erij(3)=zj*rmij
3324 *
3325 * Radial derivatives. First process both termini of the fragment (i,j)
3326
3327           ggg(1)=fac*xj
3328           ggg(2)=fac*yj
3329           ggg(3)=fac*zj
3330 c          do k=1,3
3331 c            ghalf=0.5D0*ggg(k)
3332 c            gelc(k,i)=gelc(k,i)+ghalf
3333 c            gelc(k,j)=gelc(k,j)+ghalf
3334 c          enddo
3335 c 9/28/08 AL Gradient compotents will be summed only at the end
3336           do k=1,3
3337             gelc_long(k,j)=gelc(k,j)+ggg(k)
3338             gelc_long(k,i)=gelc(k,i)-ggg(k)
3339           enddo
3340 *
3341 * Loop over residues i+1 thru j-1.
3342 *
3343 cgrad          do k=i+1,j-1
3344 cgrad            do l=1,3
3345 cgrad              gelc(l,k)=gelc(l,k)+ggg(l)
3346 cgrad            enddo
3347 cgrad          enddo
3348 c 9/28/08 AL Gradient compotents will be summed only at the end
3349           ggg(1)=facvdw*xj
3350           ggg(2)=facvdw*yj
3351           ggg(3)=facvdw*zj
3352           do k=1,3
3353             gvdwpp(k,j)=gvdwpp(k,j)+ggg(k)
3354             gvdwpp(k,i)=gvdwpp(k,i)-ggg(k)
3355           enddo
3356 #endif
3357 *
3358 * Angular part
3359 *          
3360           ecosa=2.0D0*fac3*fac1+fac4
3361           fac4=-3.0D0*fac4
3362           fac3=-6.0D0*fac3
3363           ecosb=(fac3*(fac1*cosg+cosb)+cosg*fac4)
3364           ecosg=(fac3*(fac1*cosb+cosg)+cosb*fac4)
3365           do k=1,3
3366             dcosb(k)=rmij*(dc_norm(k,i)-erij(k)*cosb)
3367             dcosg(k)=rmij*(dc_norm(k,j)-erij(k)*cosg)
3368           enddo
3369 cd        print '(2i3,2(3(1pd14.5),3x))',i,j,(dcosb(k),k=1,3),
3370 cd   &          (dcosg(k),k=1,3)
3371           do k=1,3
3372             ggg(k)=ecosb*dcosb(k)+ecosg*dcosg(k) 
3373           enddo
3374 c          do k=1,3
3375 c            ghalf=0.5D0*ggg(k)
3376 c            gelc(k,i)=gelc(k,i)+ghalf
3377 c     &               +(ecosa*(dc_norm(k,j)-cosa*dc_norm(k,i))
3378 c     &               + ecosb*(erij(k)-cosb*dc_norm(k,i)))*vbld_inv(i+1)
3379 c            gelc(k,j)=gelc(k,j)+ghalf
3380 c     &               +(ecosa*(dc_norm(k,i)-cosa*dc_norm(k,j))
3381 c     &               + ecosg*(erij(k)-cosg*dc_norm(k,j)))*vbld_inv(j+1)
3382 c          enddo
3383 cgrad          do k=i+1,j-1
3384 cgrad            do l=1,3
3385 cgrad              gelc(l,k)=gelc(l,k)+ggg(l)
3386 cgrad            enddo
3387 cgrad          enddo
3388           do k=1,3
3389             gelc(k,i)=gelc(k,i)
3390      &               +(ecosa*(dc_norm(k,j)-cosa*dc_norm(k,i))
3391      &               + ecosb*(erij(k)-cosb*dc_norm(k,i)))*vbld_inv(i+1)
3392             gelc(k,j)=gelc(k,j)
3393      &               +(ecosa*(dc_norm(k,i)-cosa*dc_norm(k,j))
3394      &               + ecosg*(erij(k)-cosg*dc_norm(k,j)))*vbld_inv(j+1)
3395             gelc_long(k,j)=gelc_long(k,j)+ggg(k)
3396             gelc_long(k,i)=gelc_long(k,i)-ggg(k)
3397           enddo
3398           IF (wel_loc.gt.0.0d0 .or. wcorr4.gt.0.0d0 .or. wcorr5.gt.0.0d0
3399      &        .or. wcorr6.gt.0.0d0 .or. wturn3.gt.0.0d0 
3400      &        .or. wturn4.gt.0.0d0 .or. wturn6.gt.0.0d0) THEN
3401 C
3402 C 9/25/99 Mixed third-order local-electrostatic terms. The local-interaction 
3403 C   energy of a peptide unit is assumed in the form of a second-order 
3404 C   Fourier series in the angles lambda1 and lambda2 (see Nishikawa et al.
3405 C   Macromolecules, 1974, 7, 797-806 for definition). This correlation terms
3406 C   are computed for EVERY pair of non-contiguous peptide groups.
3407 C
3408           if (j.lt.nres-1) then
3409             j1=j+1
3410             j2=j-1
3411           else
3412             j1=j-1
3413             j2=j-2
3414           endif
3415           kkk=0
3416           do k=1,2
3417             do l=1,2
3418               kkk=kkk+1
3419               muij(kkk)=mu(k,i)*mu(l,j)
3420             enddo
3421           enddo  
3422 cd         write (iout,*) 'EELEC: i',i,' j',j
3423 cd          write (iout,*) 'j',j,' j1',j1,' j2',j2
3424 cd          write(iout,*) 'muij',muij
3425           ury=scalar(uy(1,i),erij)
3426           urz=scalar(uz(1,i),erij)
3427           vry=scalar(uy(1,j),erij)
3428           vrz=scalar(uz(1,j),erij)
3429           a22=scalar(uy(1,i),uy(1,j))-3*ury*vry
3430           a23=scalar(uy(1,i),uz(1,j))-3*ury*vrz
3431           a32=scalar(uz(1,i),uy(1,j))-3*urz*vry
3432           a33=scalar(uz(1,i),uz(1,j))-3*urz*vrz
3433           fac=dsqrt(-ael6i)*r3ij
3434           a22=a22*fac
3435           a23=a23*fac
3436           a32=a32*fac
3437           a33=a33*fac
3438 cd          write (iout,'(4i5,4f10.5)')
3439 cd     &     i,itortyp(itype(i)),j,itortyp(itype(j)),a22,a23,a32,a33
3440 cd          write (iout,'(6f10.5)') (muij(k),k=1,4),fac,eel_loc_ij
3441 cd          write (iout,'(2(3f10.5,5x)/2(3f10.5,5x))') uy(:,i),uz(:,i),
3442 cd     &      uy(:,j),uz(:,j)
3443 cd          write (iout,'(4f10.5)') 
3444 cd     &      scalar(uy(1,i),uy(1,j)),scalar(uy(1,i),uz(1,j)),
3445 cd     &      scalar(uz(1,i),uy(1,j)),scalar(uz(1,i),uz(1,j))
3446 cd          write (iout,'(4f10.5)') ury,urz,vry,vrz
3447 cd           write (iout,'(9f10.5/)') 
3448 cd     &      fac22,a22,fac23,a23,fac32,a32,fac33,a33,eel_loc_ij
3449 C Derivatives of the elements of A in virtual-bond vectors
3450           call unormderiv(erij(1),unmat(1,1),rmij,erder(1,1))
3451           do k=1,3
3452             uryg(k,1)=scalar(erder(1,k),uy(1,i))
3453             uryg(k,2)=scalar(uygrad(1,k,1,i),erij(1))
3454             uryg(k,3)=scalar(uygrad(1,k,2,i),erij(1))
3455             urzg(k,1)=scalar(erder(1,k),uz(1,i))
3456             urzg(k,2)=scalar(uzgrad(1,k,1,i),erij(1))
3457             urzg(k,3)=scalar(uzgrad(1,k,2,i),erij(1))
3458             vryg(k,1)=scalar(erder(1,k),uy(1,j))
3459             vryg(k,2)=scalar(uygrad(1,k,1,j),erij(1))
3460             vryg(k,3)=scalar(uygrad(1,k,2,j),erij(1))
3461             vrzg(k,1)=scalar(erder(1,k),uz(1,j))
3462             vrzg(k,2)=scalar(uzgrad(1,k,1,j),erij(1))
3463             vrzg(k,3)=scalar(uzgrad(1,k,2,j),erij(1))
3464           enddo
3465 C Compute radial contributions to the gradient
3466           facr=-3.0d0*rrmij
3467           a22der=a22*facr
3468           a23der=a23*facr
3469           a32der=a32*facr
3470           a33der=a33*facr
3471           agg(1,1)=a22der*xj
3472           agg(2,1)=a22der*yj
3473           agg(3,1)=a22der*zj
3474           agg(1,2)=a23der*xj
3475           agg(2,2)=a23der*yj
3476           agg(3,2)=a23der*zj
3477           agg(1,3)=a32der*xj
3478           agg(2,3)=a32der*yj
3479           agg(3,3)=a32der*zj
3480           agg(1,4)=a33der*xj
3481           agg(2,4)=a33der*yj
3482           agg(3,4)=a33der*zj
3483 C Add the contributions coming from er
3484           fac3=-3.0d0*fac
3485           do k=1,3
3486             agg(k,1)=agg(k,1)+fac3*(uryg(k,1)*vry+vryg(k,1)*ury)
3487             agg(k,2)=agg(k,2)+fac3*(uryg(k,1)*vrz+vrzg(k,1)*ury)
3488             agg(k,3)=agg(k,3)+fac3*(urzg(k,1)*vry+vryg(k,1)*urz)
3489             agg(k,4)=agg(k,4)+fac3*(urzg(k,1)*vrz+vrzg(k,1)*urz)
3490           enddo
3491           do k=1,3
3492 C Derivatives in DC(i) 
3493 cgrad            ghalf1=0.5d0*agg(k,1)
3494 cgrad            ghalf2=0.5d0*agg(k,2)
3495 cgrad            ghalf3=0.5d0*agg(k,3)
3496 cgrad            ghalf4=0.5d0*agg(k,4)
3497             aggi(k,1)=fac*(scalar(uygrad(1,k,1,i),uy(1,j))
3498      &      -3.0d0*uryg(k,2)*vry)!+ghalf1
3499             aggi(k,2)=fac*(scalar(uygrad(1,k,1,i),uz(1,j))
3500      &      -3.0d0*uryg(k,2)*vrz)!+ghalf2
3501             aggi(k,3)=fac*(scalar(uzgrad(1,k,1,i),uy(1,j))
3502      &      -3.0d0*urzg(k,2)*vry)!+ghalf3
3503             aggi(k,4)=fac*(scalar(uzgrad(1,k,1,i),uz(1,j))
3504      &      -3.0d0*urzg(k,2)*vrz)!+ghalf4
3505 C Derivatives in DC(i+1)
3506             aggi1(k,1)=fac*(scalar(uygrad(1,k,2,i),uy(1,j))
3507      &      -3.0d0*uryg(k,3)*vry)!+agg(k,1)
3508             aggi1(k,2)=fac*(scalar(uygrad(1,k,2,i),uz(1,j))
3509      &      -3.0d0*uryg(k,3)*vrz)!+agg(k,2)
3510             aggi1(k,3)=fac*(scalar(uzgrad(1,k,2,i),uy(1,j))
3511      &      -3.0d0*urzg(k,3)*vry)!+agg(k,3)
3512             aggi1(k,4)=fac*(scalar(uzgrad(1,k,2,i),uz(1,j))
3513      &      -3.0d0*urzg(k,3)*vrz)!+agg(k,4)
3514 C Derivatives in DC(j)
3515             aggj(k,1)=fac*(scalar(uygrad(1,k,1,j),uy(1,i))
3516      &      -3.0d0*vryg(k,2)*ury)!+ghalf1
3517             aggj(k,2)=fac*(scalar(uzgrad(1,k,1,j),uy(1,i))
3518      &      -3.0d0*vrzg(k,2)*ury)!+ghalf2
3519             aggj(k,3)=fac*(scalar(uygrad(1,k,1,j),uz(1,i))
3520      &      -3.0d0*vryg(k,2)*urz)!+ghalf3
3521             aggj(k,4)=fac*(scalar(uzgrad(1,k,1,j),uz(1,i)) 
3522      &      -3.0d0*vrzg(k,2)*urz)!+ghalf4
3523 C Derivatives in DC(j+1) or DC(nres-1)
3524             aggj1(k,1)=fac*(scalar(uygrad(1,k,2,j),uy(1,i))
3525      &      -3.0d0*vryg(k,3)*ury)
3526             aggj1(k,2)=fac*(scalar(uzgrad(1,k,2,j),uy(1,i))
3527      &      -3.0d0*vrzg(k,3)*ury)
3528             aggj1(k,3)=fac*(scalar(uygrad(1,k,2,j),uz(1,i))
3529      &      -3.0d0*vryg(k,3)*urz)
3530             aggj1(k,4)=fac*(scalar(uzgrad(1,k,2,j),uz(1,i)) 
3531      &      -3.0d0*vrzg(k,3)*urz)
3532 cgrad            if (j.eq.nres-1 .and. i.lt.j-2) then
3533 cgrad              do l=1,4
3534 cgrad                aggj1(k,l)=aggj1(k,l)+agg(k,l)
3535 cgrad              enddo
3536 cgrad            endif
3537           enddo
3538           acipa(1,1)=a22
3539           acipa(1,2)=a23
3540           acipa(2,1)=a32
3541           acipa(2,2)=a33
3542           a22=-a22
3543           a23=-a23
3544           do l=1,2
3545             do k=1,3
3546               agg(k,l)=-agg(k,l)
3547               aggi(k,l)=-aggi(k,l)
3548               aggi1(k,l)=-aggi1(k,l)
3549               aggj(k,l)=-aggj(k,l)
3550               aggj1(k,l)=-aggj1(k,l)
3551             enddo
3552           enddo
3553           if (j.lt.nres-1) then
3554             a22=-a22
3555             a32=-a32
3556             do l=1,3,2
3557               do k=1,3
3558                 agg(k,l)=-agg(k,l)
3559                 aggi(k,l)=-aggi(k,l)
3560                 aggi1(k,l)=-aggi1(k,l)
3561                 aggj(k,l)=-aggj(k,l)
3562                 aggj1(k,l)=-aggj1(k,l)
3563               enddo
3564             enddo
3565           else
3566             a22=-a22
3567             a23=-a23
3568             a32=-a32
3569             a33=-a33
3570             do l=1,4
3571               do k=1,3
3572                 agg(k,l)=-agg(k,l)
3573                 aggi(k,l)=-aggi(k,l)
3574                 aggi1(k,l)=-aggi1(k,l)
3575                 aggj(k,l)=-aggj(k,l)
3576                 aggj1(k,l)=-aggj1(k,l)
3577               enddo
3578             enddo 
3579           endif    
3580           ENDIF ! WCORR
3581           IF (wel_loc.gt.0.0d0) THEN
3582 C Contribution to the local-electrostatic energy coming from the i-j pair
3583           eel_loc_ij=a22*muij(1)+a23*muij(2)+a32*muij(3)
3584      &     +a33*muij(4)
3585 cd          write (iout,*) 'i',i,' j',j,' eel_loc_ij',eel_loc_ij
3586
3587           if (energy_dec) write (iout,'(a6,2i5,0pf7.3)')
3588      &            'eelloc',i,j,eel_loc_ij
3589
3590           eel_loc=eel_loc+eel_loc_ij
3591 C Partial derivatives in virtual-bond dihedral angles gamma
3592           if (i.gt.1)
3593      &    gel_loc_loc(i-1)=gel_loc_loc(i-1)+ 
3594      &            a22*muder(1,i)*mu(1,j)+a23*muder(1,i)*mu(2,j)
3595      &           +a32*muder(2,i)*mu(1,j)+a33*muder(2,i)*mu(2,j)
3596           gel_loc_loc(j-1)=gel_loc_loc(j-1)+ 
3597      &            a22*mu(1,i)*muder(1,j)+a23*mu(1,i)*muder(2,j)
3598      &           +a32*mu(2,i)*muder(1,j)+a33*mu(2,i)*muder(2,j)
3599 C Derivatives of eello in DC(i+1) thru DC(j-1) or DC(nres-2)
3600           do l=1,3
3601             ggg(l)=agg(l,1)*muij(1)+
3602      &          agg(l,2)*muij(2)+agg(l,3)*muij(3)+agg(l,4)*muij(4)
3603             gel_loc_long(l,j)=gel_loc_long(l,j)+ggg(l)
3604             gel_loc_long(l,i)=gel_loc_long(l,i)-ggg(l)
3605 cgrad            ghalf=0.5d0*ggg(l)
3606 cgrad            gel_loc(l,i)=gel_loc(l,i)+ghalf
3607 cgrad            gel_loc(l,j)=gel_loc(l,j)+ghalf
3608           enddo
3609 cgrad          do k=i+1,j2
3610 cgrad            do l=1,3
3611 cgrad              gel_loc(l,k)=gel_loc(l,k)+ggg(l)
3612 cgrad            enddo
3613 cgrad          enddo
3614 C Remaining derivatives of eello
3615           do l=1,3
3616             gel_loc(l,i)=gel_loc(l,i)+aggi(l,1)*muij(1)+
3617      &          aggi(l,2)*muij(2)+aggi(l,3)*muij(3)+aggi(l,4)*muij(4)
3618             gel_loc(l,i+1)=gel_loc(l,i+1)+aggi1(l,1)*muij(1)+
3619      &          aggi1(l,2)*muij(2)+aggi1(l,3)*muij(3)+aggi1(l,4)*muij(4)
3620             gel_loc(l,j)=gel_loc(l,j)+aggj(l,1)*muij(1)+
3621      &          aggj(l,2)*muij(2)+aggj(l,3)*muij(3)+aggj(l,4)*muij(4)
3622             gel_loc(l,j1)=gel_loc(l,j1)+aggj1(l,1)*muij(1)+
3623      &          aggj1(l,2)*muij(2)+aggj1(l,3)*muij(3)+aggj1(l,4)*muij(4)
3624           enddo
3625           ENDIF
3626 C Change 12/26/95 to calculate four-body contributions to H-bonding energy
3627 c          if (j.gt.i+1 .and. num_conti.le.maxconts) then
3628           if (wcorr+wcorr4+wcorr5+wcorr6.gt.0.0d0
3629      &       .and. num_conti.le.maxconts) then
3630 c            write (iout,*) i,j," entered corr"
3631 C
3632 C Calculate the contact function. The ith column of the array JCONT will 
3633 C contain the numbers of atoms that make contacts with the atom I (of numbers
3634 C greater than I). The arrays FACONT and GACONT will contain the values of
3635 C the contact function and its derivative.
3636 c           r0ij=1.02D0*rpp(iteli,itelj)
3637 c           r0ij=1.11D0*rpp(iteli,itelj)
3638             r0ij=2.20D0*rpp(iteli,itelj)
3639 c           r0ij=1.55D0*rpp(iteli,itelj)
3640             call gcont(rij,r0ij,1.0D0,0.2d0*r0ij,fcont,fprimcont)
3641             if (fcont.gt.0.0D0) then
3642               num_conti=num_conti+1
3643               if (num_conti.gt.maxconts) then
3644                 write (iout,*) 'WARNING - max. # of contacts exceeded;',
3645      &                         ' will skip next contacts for this conf.'
3646               else
3647                 jcont_hb(num_conti,i)=j
3648 cd                write (iout,*) "i",i," j",j," num_conti",num_conti,
3649 cd     &           " jcont_hb",jcont_hb(num_conti,i)
3650                 IF (wcorr4.gt.0.0d0 .or. wcorr5.gt.0.0d0 .or. 
3651      &          wcorr6.gt.0.0d0 .or. wturn6.gt.0.0d0) THEN
3652 C 9/30/99 (AL) - store components necessary to evaluate higher-order loc-el
3653 C  terms.
3654                 d_cont(num_conti,i)=rij
3655 cd                write (2,'(3e15.5)') rij,r0ij+0.2d0*r0ij,rij
3656 C     --- Electrostatic-interaction matrix --- 
3657                 a_chuj(1,1,num_conti,i)=a22
3658                 a_chuj(1,2,num_conti,i)=a23
3659                 a_chuj(2,1,num_conti,i)=a32
3660                 a_chuj(2,2,num_conti,i)=a33
3661 C     --- Gradient of rij
3662                 do kkk=1,3
3663                   grij_hb_cont(kkk,num_conti,i)=erij(kkk)
3664                 enddo
3665                 kkll=0
3666                 do k=1,2
3667                   do l=1,2
3668                     kkll=kkll+1
3669                     do m=1,3
3670                       a_chuj_der(k,l,m,1,num_conti,i)=agg(m,kkll)
3671                       a_chuj_der(k,l,m,2,num_conti,i)=aggi(m,kkll)
3672                       a_chuj_der(k,l,m,3,num_conti,i)=aggi1(m,kkll)
3673                       a_chuj_der(k,l,m,4,num_conti,i)=aggj(m,kkll)
3674                       a_chuj_der(k,l,m,5,num_conti,i)=aggj1(m,kkll)
3675                     enddo
3676                   enddo
3677                 enddo
3678                 ENDIF
3679                 IF (wcorr4.eq.0.0d0 .and. wcorr.gt.0.0d0) THEN
3680 C Calculate contact energies
3681                 cosa4=4.0D0*cosa
3682                 wij=cosa-3.0D0*cosb*cosg
3683                 cosbg1=cosb+cosg
3684                 cosbg2=cosb-cosg
3685 c               fac3=dsqrt(-ael6i)/r0ij**3     
3686                 fac3=dsqrt(-ael6i)*r3ij
3687 c                 ees0pij=dsqrt(4.0D0+cosa4+wij*wij-3.0D0*cosbg1*cosbg1)
3688                 ees0tmp=4.0D0+cosa4+wij*wij-3.0D0*cosbg1*cosbg1
3689                 if (ees0tmp.gt.0) then
3690                   ees0pij=dsqrt(ees0tmp)
3691                 else
3692                   ees0pij=0
3693                 endif
3694 c                ees0mij=dsqrt(4.0D0-cosa4+wij*wij-3.0D0*cosbg2*cosbg2)
3695                 ees0tmp=4.0D0-cosa4+wij*wij-3.0D0*cosbg2*cosbg2
3696                 if (ees0tmp.gt.0) then
3697                   ees0mij=dsqrt(ees0tmp)
3698                 else
3699                   ees0mij=0
3700                 endif
3701 c               ees0mij=0.0D0
3702                 ees0p(num_conti,i)=0.5D0*fac3*(ees0pij+ees0mij)
3703                 ees0m(num_conti,i)=0.5D0*fac3*(ees0pij-ees0mij)
3704 C Diagnostics. Comment out or remove after debugging!
3705 c               ees0p(num_conti,i)=0.5D0*fac3*ees0pij
3706 c               ees0m(num_conti,i)=0.5D0*fac3*ees0mij
3707 c               ees0m(num_conti,i)=0.0D0
3708 C End diagnostics.
3709 c               write (iout,*) 'i=',i,' j=',j,' rij=',rij,' r0ij=',r0ij,
3710 c    & ' ees0ij=',ees0p(num_conti,i),ees0m(num_conti,i),' fcont=',fcont
3711 C Angular derivatives of the contact function
3712                 ees0pij1=fac3/ees0pij 
3713                 ees0mij1=fac3/ees0mij
3714                 fac3p=-3.0D0*fac3*rrmij
3715                 ees0pijp=0.5D0*fac3p*(ees0pij+ees0mij)
3716                 ees0mijp=0.5D0*fac3p*(ees0pij-ees0mij)
3717 c               ees0mij1=0.0D0
3718                 ecosa1=       ees0pij1*( 1.0D0+0.5D0*wij)
3719                 ecosb1=-1.5D0*ees0pij1*(wij*cosg+cosbg1)
3720                 ecosg1=-1.5D0*ees0pij1*(wij*cosb+cosbg1)
3721                 ecosa2=       ees0mij1*(-1.0D0+0.5D0*wij)
3722                 ecosb2=-1.5D0*ees0mij1*(wij*cosg+cosbg2) 
3723                 ecosg2=-1.5D0*ees0mij1*(wij*cosb-cosbg2)
3724                 ecosap=ecosa1+ecosa2
3725                 ecosbp=ecosb1+ecosb2
3726                 ecosgp=ecosg1+ecosg2
3727                 ecosam=ecosa1-ecosa2
3728                 ecosbm=ecosb1-ecosb2
3729                 ecosgm=ecosg1-ecosg2
3730 C Diagnostics
3731 c               ecosap=ecosa1
3732 c               ecosbp=ecosb1
3733 c               ecosgp=ecosg1
3734 c               ecosam=0.0D0
3735 c               ecosbm=0.0D0
3736 c               ecosgm=0.0D0
3737 C End diagnostics
3738                 facont_hb(num_conti,i)=fcont
3739                 fprimcont=fprimcont/rij
3740 cd              facont_hb(num_conti,i)=1.0D0
3741 C Following line is for diagnostics.
3742 cd              fprimcont=0.0D0
3743                 do k=1,3
3744                   dcosb(k)=rmij*(dc_norm(k,i)-erij(k)*cosb)
3745                   dcosg(k)=rmij*(dc_norm(k,j)-erij(k)*cosg)
3746                 enddo
3747                 do k=1,3
3748                   gggp(k)=ecosbp*dcosb(k)+ecosgp*dcosg(k)
3749                   gggm(k)=ecosbm*dcosb(k)+ecosgm*dcosg(k)
3750                 enddo
3751                 gggp(1)=gggp(1)+ees0pijp*xj
3752                 gggp(2)=gggp(2)+ees0pijp*yj
3753                 gggp(3)=gggp(3)+ees0pijp*zj
3754                 gggm(1)=gggm(1)+ees0mijp*xj
3755                 gggm(2)=gggm(2)+ees0mijp*yj
3756                 gggm(3)=gggm(3)+ees0mijp*zj
3757 C Derivatives due to the contact function
3758                 gacont_hbr(1,num_conti,i)=fprimcont*xj
3759                 gacont_hbr(2,num_conti,i)=fprimcont*yj
3760                 gacont_hbr(3,num_conti,i)=fprimcont*zj
3761                 do k=1,3
3762 c
3763 c 10/24/08 cgrad and ! comments indicate the parts of the code removed 
3764 c          following the change of gradient-summation algorithm.
3765 c
3766 cgrad                  ghalfp=0.5D0*gggp(k)
3767 cgrad                  ghalfm=0.5D0*gggm(k)
3768                   gacontp_hb1(k,num_conti,i)=!ghalfp
3769      &              +(ecosap*(dc_norm(k,j)-cosa*dc_norm(k,i))
3770      &              + ecosbp*(erij(k)-cosb*dc_norm(k,i)))*vbld_inv(i+1)
3771                   gacontp_hb2(k,num_conti,i)=!ghalfp
3772      &              +(ecosap*(dc_norm(k,i)-cosa*dc_norm(k,j))
3773      &              + ecosgp*(erij(k)-cosg*dc_norm(k,j)))*vbld_inv(j+1)
3774                   gacontp_hb3(k,num_conti,i)=gggp(k)
3775                   gacontm_hb1(k,num_conti,i)=!ghalfm
3776      &              +(ecosam*(dc_norm(k,j)-cosa*dc_norm(k,i))
3777      &              + ecosbm*(erij(k)-cosb*dc_norm(k,i)))*vbld_inv(i+1)
3778                   gacontm_hb2(k,num_conti,i)=!ghalfm
3779      &              +(ecosam*(dc_norm(k,i)-cosa*dc_norm(k,j))
3780      &              + ecosgm*(erij(k)-cosg*dc_norm(k,j)))*vbld_inv(j+1)
3781                   gacontm_hb3(k,num_conti,i)=gggm(k)
3782                 enddo
3783 C Diagnostics. Comment out or remove after debugging!
3784 cdiag           do k=1,3
3785 cdiag             gacontp_hb1(k,num_conti,i)=0.0D0
3786 cdiag             gacontp_hb2(k,num_conti,i)=0.0D0
3787 cdiag             gacontp_hb3(k,num_conti,i)=0.0D0
3788 cdiag             gacontm_hb1(k,num_conti,i)=0.0D0
3789 cdiag             gacontm_hb2(k,num_conti,i)=0.0D0
3790 cdiag             gacontm_hb3(k,num_conti,i)=0.0D0
3791 cdiag           enddo
3792               ENDIF ! wcorr
3793               endif  ! num_conti.le.maxconts
3794             endif  ! fcont.gt.0
3795           endif    ! j.gt.i+1
3796           if (wturn3.gt.0.0d0 .or. wturn4.gt.0.0d0) then
3797             do k=1,4
3798               do l=1,3
3799                 ghalf=0.5d0*agg(l,k)
3800                 aggi(l,k)=aggi(l,k)+ghalf
3801                 aggi1(l,k)=aggi1(l,k)+agg(l,k)
3802                 aggj(l,k)=aggj(l,k)+ghalf
3803               enddo
3804             enddo
3805             if (j.eq.nres-1 .and. i.lt.j-2) then
3806               do k=1,4
3807                 do l=1,3
3808                   aggj1(l,k)=aggj1(l,k)+agg(l,k)
3809                 enddo
3810               enddo
3811             endif
3812           endif
3813 c          t_eelecij=t_eelecij+MPI_Wtime()-time00
3814       return
3815       end
3816 C-----------------------------------------------------------------------------
3817       subroutine eturn3(i,eello_turn3)
3818 C Third- and fourth-order contributions from turns
3819       implicit real*8 (a-h,o-z)
3820       include 'DIMENSIONS'
3821       include 'COMMON.IOUNITS'
3822       include 'COMMON.GEO'
3823       include 'COMMON.VAR'
3824       include 'COMMON.LOCAL'
3825       include 'COMMON.CHAIN'
3826       include 'COMMON.DERIV'
3827       include 'COMMON.INTERACT'
3828       include 'COMMON.CONTACTS'
3829       include 'COMMON.TORSION'
3830       include 'COMMON.VECTORS'
3831       include 'COMMON.FFIELD'
3832       include 'COMMON.CONTROL'
3833       dimension ggg(3)
3834       double precision auxmat(2,2),auxmat1(2,2),auxmat2(2,2),pizda(2,2),
3835      &  e1t(2,2),e2t(2,2),e3t(2,2),e1tder(2,2),e2tder(2,2),e3tder(2,2),
3836      &  e1a(2,2),ae3(2,2),ae3e2(2,2),auxvec(2),auxvec1(2)
3837       double precision agg(3,4),aggi(3,4),aggi1(3,4),
3838      &    aggj(3,4),aggj1(3,4),a_temp(2,2),auxmat3(2,2)
3839       common /locel/ a_temp,agg,aggi,aggi1,aggj,aggj1,a22,a23,a32,a33,
3840      &    dxi,dyi,dzi,dx_normi,dy_normi,dz_normi,xmedi,ymedi,zmedi,
3841      &    num_conti,j1,j2
3842       j=i+2
3843 c      write (iout,*) "eturn3",i,j,j1,j2
3844       a_temp(1,1)=a22
3845       a_temp(1,2)=a23
3846       a_temp(2,1)=a32
3847       a_temp(2,2)=a33
3848 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
3849 C
3850 C               Third-order contributions
3851 C        
3852 C                 (i+2)o----(i+3)
3853 C                      | |
3854 C                      | |
3855 C                 (i+1)o----i
3856 C
3857 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC   
3858 cd        call checkint_turn3(i,a_temp,eello_turn3_num)
3859         call matmat2(EUg(1,1,i+1),EUg(1,1,i+2),auxmat(1,1))
3860         call transpose2(auxmat(1,1),auxmat1(1,1))
3861         call matmat2(a_temp(1,1),auxmat1(1,1),pizda(1,1))
3862         eello_turn3=eello_turn3+0.5d0*(pizda(1,1)+pizda(2,2))
3863         if (energy_dec) write (iout,'(a6,2i5,0pf7.3)')
3864      &          'eturn3',i,j,0.5d0*(pizda(1,1)+pizda(2,2))
3865 cd        write (2,*) 'i,',i,' j',j,'eello_turn3',
3866 cd     &    0.5d0*(pizda(1,1)+pizda(2,2)),
3867 cd     &    ' eello_turn3_num',4*eello_turn3_num
3868 C Derivatives in gamma(i)
3869         call matmat2(EUgder(1,1,i+1),EUg(1,1,i+2),auxmat2(1,1))
3870         call transpose2(auxmat2(1,1),auxmat3(1,1))
3871         call matmat2(a_temp(1,1),auxmat3(1,1),pizda(1,1))
3872         gel_loc_turn3(i)=gel_loc_turn3(i)+0.5d0*(pizda(1,1)+pizda(2,2))
3873 C Derivatives in gamma(i+1)
3874         call matmat2(EUg(1,1,i+1),EUgder(1,1,i+2),auxmat2(1,1))
3875         call transpose2(auxmat2(1,1),auxmat3(1,1))
3876         call matmat2(a_temp(1,1),auxmat3(1,1),pizda(1,1))
3877         gel_loc_turn3(i+1)=gel_loc_turn3(i+1)
3878      &    +0.5d0*(pizda(1,1)+pizda(2,2))
3879 C Cartesian derivatives
3880         do l=1,3
3881 c            ghalf1=0.5d0*agg(l,1)
3882 c            ghalf2=0.5d0*agg(l,2)
3883 c            ghalf3=0.5d0*agg(l,3)
3884 c            ghalf4=0.5d0*agg(l,4)
3885           a_temp(1,1)=aggi(l,1)!+ghalf1
3886           a_temp(1,2)=aggi(l,2)!+ghalf2
3887           a_temp(2,1)=aggi(l,3)!+ghalf3
3888           a_temp(2,2)=aggi(l,4)!+ghalf4
3889           call matmat2(a_temp(1,1),auxmat1(1,1),pizda(1,1))
3890           gcorr3_turn(l,i)=gcorr3_turn(l,i)
3891      &      +0.5d0*(pizda(1,1)+pizda(2,2))
3892           a_temp(1,1)=aggi1(l,1)!+agg(l,1)
3893           a_temp(1,2)=aggi1(l,2)!+agg(l,2)
3894           a_temp(2,1)=aggi1(l,3)!+agg(l,3)
3895           a_temp(2,2)=aggi1(l,4)!+agg(l,4)
3896           call matmat2(a_temp(1,1),auxmat1(1,1),pizda(1,1))
3897           gcorr3_turn(l,i+1)=gcorr3_turn(l,i+1)
3898      &      +0.5d0*(pizda(1,1)+pizda(2,2))
3899           a_temp(1,1)=aggj(l,1)!+ghalf1
3900           a_temp(1,2)=aggj(l,2)!+ghalf2
3901           a_temp(2,1)=aggj(l,3)!+ghalf3
3902           a_temp(2,2)=aggj(l,4)!+ghalf4
3903           call matmat2(a_temp(1,1),auxmat1(1,1),pizda(1,1))
3904           gcorr3_turn(l,j)=gcorr3_turn(l,j)
3905      &      +0.5d0*(pizda(1,1)+pizda(2,2))
3906           a_temp(1,1)=aggj1(l,1)
3907           a_temp(1,2)=aggj1(l,2)
3908           a_temp(2,1)=aggj1(l,3)
3909           a_temp(2,2)=aggj1(l,4)
3910           call matmat2(a_temp(1,1),auxmat1(1,1),pizda(1,1))
3911           gcorr3_turn(l,j1)=gcorr3_turn(l,j1)
3912      &      +0.5d0*(pizda(1,1)+pizda(2,2))
3913         enddo
3914       return
3915       end
3916 C-------------------------------------------------------------------------------
3917       subroutine eturn4(i,eello_turn4)
3918 C Third- and fourth-order contributions from turns
3919       implicit real*8 (a-h,o-z)
3920       include 'DIMENSIONS'
3921       include 'COMMON.IOUNITS'
3922       include 'COMMON.GEO'
3923       include 'COMMON.VAR'
3924       include 'COMMON.LOCAL'
3925       include 'COMMON.CHAIN'
3926       include 'COMMON.DERIV'
3927       include 'COMMON.INTERACT'
3928       include 'COMMON.CONTACTS'
3929       include 'COMMON.TORSION'
3930       include 'COMMON.VECTORS'
3931       include 'COMMON.FFIELD'
3932       include 'COMMON.CONTROL'
3933       dimension ggg(3)
3934       double precision auxmat(2,2),auxmat1(2,2),auxmat2(2,2),pizda(2,2),
3935      &  e1t(2,2),e2t(2,2),e3t(2,2),e1tder(2,2),e2tder(2,2),e3tder(2,2),
3936      &  e1a(2,2),ae3(2,2),ae3e2(2,2),auxvec(2),auxvec1(2)
3937       double precision agg(3,4),aggi(3,4),aggi1(3,4),
3938      &    aggj(3,4),aggj1(3,4),a_temp(2,2),auxmat3(2,2)
3939       common /locel/ a_temp,agg,aggi,aggi1,aggj,aggj1,a22,a23,a32,a33,
3940      &    dxi,dyi,dzi,dx_normi,dy_normi,dz_normi,xmedi,ymedi,zmedi,
3941      &    num_conti,j1,j2
3942       j=i+3
3943 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
3944 C
3945 C               Fourth-order contributions
3946 C        
3947 C                 (i+3)o----(i+4)
3948 C                     /  |
3949 C               (i+2)o   |
3950 C                     \  |
3951 C                 (i+1)o----i
3952 C
3953 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC   
3954 cd        call checkint_turn4(i,a_temp,eello_turn4_num)
3955 c        write (iout,*) "eturn4 i",i," j",j," j1",j1," j2",j2
3956         a_temp(1,1)=a22
3957         a_temp(1,2)=a23
3958         a_temp(2,1)=a32
3959         a_temp(2,2)=a33
3960         iti1=itortyp(itype(i+1))
3961         iti2=itortyp(itype(i+2))
3962         iti3=itortyp(itype(i+3))
3963 c        write(iout,*) "iti1",iti1," iti2",iti2," iti3",iti3
3964         call transpose2(EUg(1,1,i+1),e1t(1,1))
3965         call transpose2(Eug(1,1,i+2),e2t(1,1))
3966         call transpose2(Eug(1,1,i+3),e3t(1,1))
3967         call matmat2(e1t(1,1),a_temp(1,1),e1a(1,1))
3968         call matvec2(e1a(1,1),Ub2(1,i+3),auxvec(1))
3969         s1=scalar2(b1(1,iti2),auxvec(1))
3970         call matmat2(a_temp(1,1),e3t(1,1),ae3(1,1))
3971         call matvec2(ae3(1,1),Ub2(1,i+2),auxvec(1)) 
3972         s2=scalar2(b1(1,iti1),auxvec(1))
3973         call matmat2(ae3(1,1),e2t(1,1),ae3e2(1,1))
3974         call matmat2(ae3e2(1,1),e1t(1,1),pizda(1,1))
3975         s3=0.5d0*(pizda(1,1)+pizda(2,2))
3976         eello_turn4=eello_turn4-(s1+s2+s3)
3977         if (energy_dec) write (iout,'(a6,2i5,0pf7.3)')
3978      &      'eturn4',i,j,-(s1+s2+s3)
3979 cd        write (2,*) 'i,',i,' j',j,'eello_turn4',-(s1+s2+s3),
3980 cd     &    ' eello_turn4_num',8*eello_turn4_num
3981 C Derivatives in gamma(i)
3982         call transpose2(EUgder(1,1,i+1),e1tder(1,1))
3983         call matmat2(e1tder(1,1),a_temp(1,1),auxmat(1,1))
3984         call matvec2(auxmat(1,1),Ub2(1,i+3),auxvec(1))
3985         s1=scalar2(b1(1,iti2),auxvec(1))
3986         call matmat2(ae3e2(1,1),e1tder(1,1),pizda(1,1))
3987         s3=0.5d0*(pizda(1,1)+pizda(2,2))
3988         gel_loc_turn4(i)=gel_loc_turn4(i)-(s1+s3)
3989 C Derivatives in gamma(i+1)
3990         call transpose2(EUgder(1,1,i+2),e2tder(1,1))
3991         call matvec2(ae3(1,1),Ub2der(1,i+2),auxvec(1)) 
3992         s2=scalar2(b1(1,iti1),auxvec(1))
3993         call matmat2(ae3(1,1),e2tder(1,1),auxmat(1,1))
3994         call matmat2(auxmat(1,1),e1t(1,1),pizda(1,1))
3995         s3=0.5d0*(pizda(1,1)+pizda(2,2))
3996         gel_loc_turn4(i+1)=gel_loc_turn4(i+1)-(s2+s3)
3997 C Derivatives in gamma(i+2)
3998         call transpose2(EUgder(1,1,i+3),e3tder(1,1))
3999         call matvec2(e1a(1,1),Ub2der(1,i+3),auxvec(1))
4000         s1=scalar2(b1(1,iti2),auxvec(1))
4001         call matmat2(a_temp(1,1),e3tder(1,1),auxmat(1,1))
4002         call matvec2(auxmat(1,1),Ub2(1,i+2),auxvec(1)) 
4003         s2=scalar2(b1(1,iti1),auxvec(1))
4004         call matmat2(auxmat(1,1),e2t(1,1),auxmat3(1,1))
4005         call matmat2(auxmat3(1,1),e1t(1,1),pizda(1,1))
4006         s3=0.5d0*(pizda(1,1)+pizda(2,2))
4007         gel_loc_turn4(i+2)=gel_loc_turn4(i+2)-(s1+s2+s3)
4008 C Cartesian derivatives
4009 C Derivatives of this turn contributions in DC(i+2)
4010         if (j.lt.nres-1) then
4011           do l=1,3
4012             a_temp(1,1)=agg(l,1)
4013             a_temp(1,2)=agg(l,2)
4014             a_temp(2,1)=agg(l,3)
4015             a_temp(2,2)=agg(l,4)
4016             call matmat2(e1t(1,1),a_temp(1,1),e1a(1,1))
4017             call matvec2(e1a(1,1),Ub2(1,i+3),auxvec(1))
4018             s1=scalar2(b1(1,iti2),auxvec(1))
4019             call matmat2(a_temp(1,1),e3t(1,1),ae3(1,1))
4020             call matvec2(ae3(1,1),Ub2(1,i+2),auxvec(1)) 
4021             s2=scalar2(b1(1,iti1),auxvec(1))
4022             call matmat2(ae3(1,1),e2t(1,1),ae3e2(1,1))
4023             call matmat2(ae3e2(1,1),e1t(1,1),pizda(1,1))
4024             s3=0.5d0*(pizda(1,1)+pizda(2,2))
4025             ggg(l)=-(s1+s2+s3)
4026             gcorr4_turn(l,i+2)=gcorr4_turn(l,i+2)-(s1+s2+s3)
4027           enddo
4028         endif
4029 C Remaining derivatives of this turn contribution
4030         do l=1,3
4031           a_temp(1,1)=aggi(l,1)
4032           a_temp(1,2)=aggi(l,2)
4033           a_temp(2,1)=aggi(l,3)
4034           a_temp(2,2)=aggi(l,4)
4035           call matmat2(e1t(1,1),a_temp(1,1),e1a(1,1))
4036           call matvec2(e1a(1,1),Ub2(1,i+3),auxvec(1))
4037           s1=scalar2(b1(1,iti2),auxvec(1))
4038           call matmat2(a_temp(1,1),e3t(1,1),ae3(1,1))
4039           call matvec2(ae3(1,1),Ub2(1,i+2),auxvec(1)) 
4040           s2=scalar2(b1(1,iti1),auxvec(1))
4041           call matmat2(ae3(1,1),e2t(1,1),ae3e2(1,1))
4042           call matmat2(ae3e2(1,1),e1t(1,1),pizda(1,1))
4043           s3=0.5d0*(pizda(1,1)+pizda(2,2))
4044           gcorr4_turn(l,i)=gcorr4_turn(l,i)-(s1+s2+s3)
4045           a_temp(1,1)=aggi1(l,1)
4046           a_temp(1,2)=aggi1(l,2)
4047           a_temp(2,1)=aggi1(l,3)
4048           a_temp(2,2)=aggi1(l,4)
4049           call matmat2(e1t(1,1),a_temp(1,1),e1a(1,1))
4050           call matvec2(e1a(1,1),Ub2(1,i+3),auxvec(1))
4051           s1=scalar2(b1(1,iti2),auxvec(1))
4052           call matmat2(a_temp(1,1),e3t(1,1),ae3(1,1))
4053           call matvec2(ae3(1,1),Ub2(1,i+2),auxvec(1)) 
4054           s2=scalar2(b1(1,iti1),auxvec(1))
4055           call matmat2(ae3(1,1),e2t(1,1),ae3e2(1,1))
4056           call matmat2(ae3e2(1,1),e1t(1,1),pizda(1,1))
4057           s3=0.5d0*(pizda(1,1)+pizda(2,2))
4058           gcorr4_turn(l,i+1)=gcorr4_turn(l,i+1)-(s1+s2+s3)
4059           a_temp(1,1)=aggj(l,1)
4060           a_temp(1,2)=aggj(l,2)
4061           a_temp(2,1)=aggj(l,3)
4062           a_temp(2,2)=aggj(l,4)
4063           call matmat2(e1t(1,1),a_temp(1,1),e1a(1,1))
4064           call matvec2(e1a(1,1),Ub2(1,i+3),auxvec(1))
4065           s1=scalar2(b1(1,iti2),auxvec(1))
4066           call matmat2(a_temp(1,1),e3t(1,1),ae3(1,1))
4067           call matvec2(ae3(1,1),Ub2(1,i+2),auxvec(1)) 
4068           s2=scalar2(b1(1,iti1),auxvec(1))
4069           call matmat2(ae3(1,1),e2t(1,1),ae3e2(1,1))
4070           call matmat2(ae3e2(1,1),e1t(1,1),pizda(1,1))
4071           s3=0.5d0*(pizda(1,1)+pizda(2,2))
4072           gcorr4_turn(l,j)=gcorr4_turn(l,j)-(s1+s2+s3)
4073           a_temp(1,1)=aggj1(l,1)
4074           a_temp(1,2)=aggj1(l,2)
4075           a_temp(2,1)=aggj1(l,3)
4076           a_temp(2,2)=aggj1(l,4)
4077           call matmat2(e1t(1,1),a_temp(1,1),e1a(1,1))
4078           call matvec2(e1a(1,1),Ub2(1,i+3),auxvec(1))
4079           s1=scalar2(b1(1,iti2),auxvec(1))
4080           call matmat2(a_temp(1,1),e3t(1,1),ae3(1,1))
4081           call matvec2(ae3(1,1),Ub2(1,i+2),auxvec(1)) 
4082           s2=scalar2(b1(1,iti1),auxvec(1))
4083           call matmat2(ae3(1,1),e2t(1,1),ae3e2(1,1))
4084           call matmat2(ae3e2(1,1),e1t(1,1),pizda(1,1))
4085           s3=0.5d0*(pizda(1,1)+pizda(2,2))
4086 c          write (iout,*) "s1",s1," s2",s2," s3",s3," s1+s2+s3",s1+s2+s3
4087           gcorr4_turn(l,j1)=gcorr4_turn(l,j1)-(s1+s2+s3)
4088         enddo
4089       return
4090       end
4091 C-----------------------------------------------------------------------------
4092       subroutine vecpr(u,v,w)
4093       implicit real*8(a-h,o-z)
4094       dimension u(3),v(3),w(3)
4095       w(1)=u(2)*v(3)-u(3)*v(2)
4096       w(2)=-u(1)*v(3)+u(3)*v(1)
4097       w(3)=u(1)*v(2)-u(2)*v(1)
4098       return
4099       end
4100 C-----------------------------------------------------------------------------
4101       subroutine unormderiv(u,ugrad,unorm,ungrad)
4102 C This subroutine computes the derivatives of a normalized vector u, given
4103 C the derivatives computed without normalization conditions, ugrad. Returns
4104 C ungrad.
4105       implicit none
4106       double precision u(3),ugrad(3,3),unorm,ungrad(3,3)
4107       double precision vec(3)
4108       double precision scalar
4109       integer i,j
4110 c      write (2,*) 'ugrad',ugrad
4111 c      write (2,*) 'u',u
4112       do i=1,3
4113         vec(i)=scalar(ugrad(1,i),u(1))
4114       enddo
4115 c      write (2,*) 'vec',vec
4116       do i=1,3
4117         do j=1,3
4118           ungrad(j,i)=(ugrad(j,i)-u(j)*vec(i))*unorm
4119         enddo
4120       enddo
4121 c      write (2,*) 'ungrad',ungrad
4122       return
4123       end
4124 C-----------------------------------------------------------------------------
4125       subroutine escp_soft_sphere(evdw2,evdw2_14)
4126 C
4127 C This subroutine calculates the excluded-volume interaction energy between
4128 C peptide-group centers and side chains and its gradient in virtual-bond and
4129 C side-chain vectors.
4130 C
4131       implicit real*8 (a-h,o-z)
4132       include 'DIMENSIONS'
4133       include 'COMMON.GEO'
4134       include 'COMMON.VAR'
4135       include 'COMMON.LOCAL'
4136       include 'COMMON.CHAIN'
4137       include 'COMMON.DERIV'
4138       include 'COMMON.INTERACT'
4139       include 'COMMON.FFIELD'
4140       include 'COMMON.IOUNITS'
4141       include 'COMMON.CONTROL'
4142       dimension ggg(3)
4143       evdw2=0.0D0
4144       evdw2_14=0.0d0
4145       r0_scp=4.5d0
4146 cd    print '(a)','Enter ESCP'
4147 cd    write (iout,*) 'iatscp_s=',iatscp_s,' iatscp_e=',iatscp_e
4148       do i=iatscp_s,iatscp_e
4149         iteli=itel(i)
4150         xi=0.5D0*(c(1,i)+c(1,i+1))
4151         yi=0.5D0*(c(2,i)+c(2,i+1))
4152         zi=0.5D0*(c(3,i)+c(3,i+1))
4153
4154         do iint=1,nscp_gr(i)
4155
4156         do j=iscpstart(i,iint),iscpend(i,iint)
4157           itypj=itype(j)
4158 C Uncomment following three lines for SC-p interactions
4159 c         xj=c(1,nres+j)-xi
4160 c         yj=c(2,nres+j)-yi
4161 c         zj=c(3,nres+j)-zi
4162 C Uncomment following three lines for Ca-p interactions
4163           xj=c(1,j)-xi
4164           yj=c(2,j)-yi
4165           zj=c(3,j)-zi
4166           rij=xj*xj+yj*yj+zj*zj
4167           r0ij=r0_scp
4168           r0ijsq=r0ij*r0ij
4169           if (rij.lt.r0ijsq) then
4170             evdwij=0.25d0*(rij-r0ijsq)**2
4171             fac=rij-r0ijsq
4172           else
4173             evdwij=0.0d0
4174             fac=0.0d0
4175           endif 
4176           evdw2=evdw2+evdwij
4177 C
4178 C Calculate contributions to the gradient in the virtual-bond and SC vectors.
4179 C
4180           ggg(1)=xj*fac
4181           ggg(2)=yj*fac
4182           ggg(3)=zj*fac
4183 cgrad          if (j.lt.i) then
4184 cd          write (iout,*) 'j<i'
4185 C Uncomment following three lines for SC-p interactions
4186 c           do k=1,3
4187 c             gradx_scp(k,j)=gradx_scp(k,j)+ggg(k)
4188 c           enddo
4189 cgrad          else
4190 cd          write (iout,*) 'j>i'
4191 cgrad            do k=1,3
4192 cgrad              ggg(k)=-ggg(k)
4193 C Uncomment following line for SC-p interactions
4194 c             gradx_scp(k,j)=gradx_scp(k,j)-ggg(k)
4195 cgrad            enddo
4196 cgrad          endif
4197 cgrad          do k=1,3
4198 cgrad            gvdwc_scp(k,i)=gvdwc_scp(k,i)-0.5D0*ggg(k)
4199 cgrad          enddo
4200 cgrad          kstart=min0(i+1,j)
4201 cgrad          kend=max0(i-1,j-1)
4202 cd        write (iout,*) 'i=',i,' j=',j,' kstart=',kstart,' kend=',kend
4203 cd        write (iout,*) ggg(1),ggg(2),ggg(3)
4204 cgrad          do k=kstart,kend
4205 cgrad            do l=1,3
4206 cgrad              gvdwc_scp(l,k)=gvdwc_scp(l,k)-ggg(l)
4207 cgrad            enddo
4208 cgrad          enddo
4209           do k=1,3
4210             gvdwc_scpp(k,i)=gvdwc_scpp(k,i)-ggg(k)
4211             gvdwc_scp(k,j)=gvdwc_scp(k,j)+ggg(k)
4212           enddo
4213         enddo
4214
4215         enddo ! iint
4216       enddo ! i
4217       return
4218       end
4219 C-----------------------------------------------------------------------------
4220       subroutine escp(evdw2,evdw2_14)
4221 C
4222 C This subroutine calculates the excluded-volume interaction energy between
4223 C peptide-group centers and side chains and its gradient in virtual-bond and
4224 C side-chain vectors.
4225 C
4226       implicit real*8 (a-h,o-z)
4227       include 'DIMENSIONS'
4228       include 'COMMON.GEO'
4229       include 'COMMON.VAR'
4230       include 'COMMON.LOCAL'
4231       include 'COMMON.CHAIN'
4232       include 'COMMON.DERIV'
4233       include 'COMMON.INTERACT'
4234       include 'COMMON.FFIELD'
4235       include 'COMMON.IOUNITS'
4236       include 'COMMON.CONTROL'
4237       dimension ggg(3)
4238       evdw2=0.0D0
4239       evdw2_14=0.0d0
4240 cd    print '(a)','Enter ESCP'
4241 cd    write (iout,*) 'iatscp_s=',iatscp_s,' iatscp_e=',iatscp_e
4242       do i=iatscp_s,iatscp_e
4243         iteli=itel(i)
4244         xi=0.5D0*(c(1,i)+c(1,i+1))
4245         yi=0.5D0*(c(2,i)+c(2,i+1))
4246         zi=0.5D0*(c(3,i)+c(3,i+1))
4247
4248         do iint=1,nscp_gr(i)
4249
4250         do j=iscpstart(i,iint),iscpend(i,iint)
4251           itypj=itype(j)
4252 C Uncomment following three lines for SC-p interactions
4253 c         xj=c(1,nres+j)-xi
4254 c         yj=c(2,nres+j)-yi
4255 c         zj=c(3,nres+j)-zi
4256 C Uncomment following three lines for Ca-p interactions
4257           xj=c(1,j)-xi
4258           yj=c(2,j)-yi
4259           zj=c(3,j)-zi
4260           rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
4261           fac=rrij**expon2
4262           e1=fac*fac*aad(itypj,iteli)
4263           e2=fac*bad(itypj,iteli)
4264           if (iabs(j-i) .le. 2) then
4265             e1=scal14*e1
4266             e2=scal14*e2
4267             evdw2_14=evdw2_14+e1+e2
4268           endif
4269           evdwij=e1+e2
4270           evdw2=evdw2+evdwij
4271           if (energy_dec) write (iout,'(a6,2i5,0pf7.3)')
4272      &        'evdw2',i,j,evdwij
4273 C
4274 C Calculate contributions to the gradient in the virtual-bond and SC vectors.
4275 C
4276           fac=-(evdwij+e1)*rrij
4277           ggg(1)=xj*fac
4278           ggg(2)=yj*fac
4279           ggg(3)=zj*fac
4280 cgrad          if (j.lt.i) then
4281 cd          write (iout,*) 'j<i'
4282 C Uncomment following three lines for SC-p interactions
4283 c           do k=1,3
4284 c             gradx_scp(k,j)=gradx_scp(k,j)+ggg(k)
4285 c           enddo
4286 cgrad          else
4287 cd          write (iout,*) 'j>i'
4288 cgrad            do k=1,3
4289 cgrad              ggg(k)=-ggg(k)
4290 C Uncomment following line for SC-p interactions
4291 ccgrad             gradx_scp(k,j)=gradx_scp(k,j)-ggg(k)
4292 c             gradx_scp(k,j)=gradx_scp(k,j)+ggg(k)
4293 cgrad            enddo
4294 cgrad          endif
4295 cgrad          do k=1,3
4296 cgrad            gvdwc_scp(k,i)=gvdwc_scp(k,i)-0.5D0*ggg(k)
4297 cgrad          enddo
4298 cgrad          kstart=min0(i+1,j)
4299 cgrad          kend=max0(i-1,j-1)
4300 cd        write (iout,*) 'i=',i,' j=',j,' kstart=',kstart,' kend=',kend
4301 cd        write (iout,*) ggg(1),ggg(2),ggg(3)
4302 cgrad          do k=kstart,kend
4303 cgrad            do l=1,3
4304 cgrad              gvdwc_scp(l,k)=gvdwc_scp(l,k)-ggg(l)
4305 cgrad            enddo
4306 cgrad          enddo
4307           do k=1,3
4308             gvdwc_scpp(k,i)=gvdwc_scpp(k,i)-ggg(k)
4309             gvdwc_scp(k,j)=gvdwc_scp(k,j)+ggg(k)
4310           enddo
4311         enddo
4312
4313         enddo ! iint
4314       enddo ! i
4315       do i=1,nct
4316         do j=1,3
4317           gvdwc_scp(j,i)=expon*gvdwc_scp(j,i)
4318           gvdwc_scpp(j,i)=expon*gvdwc_scpp(j,i)
4319           gradx_scp(j,i)=expon*gradx_scp(j,i)
4320         enddo
4321       enddo
4322 C******************************************************************************
4323 C
4324 C                              N O T E !!!
4325 C
4326 C To save time the factor EXPON has been extracted from ALL components
4327 C of GVDWC and GRADX. Remember to multiply them by this factor before further 
4328 C use!
4329 C
4330 C******************************************************************************
4331       return
4332       end
4333 C--------------------------------------------------------------------------
4334       subroutine edis(ehpb)
4335
4336 C Evaluate bridge-strain energy and its gradient in virtual-bond and SC vectors.
4337 C
4338       implicit real*8 (a-h,o-z)
4339       include 'DIMENSIONS'
4340       include 'COMMON.SBRIDGE'
4341       include 'COMMON.CHAIN'
4342       include 'COMMON.DERIV'
4343       include 'COMMON.VAR'
4344       include 'COMMON.INTERACT'
4345       include 'COMMON.IOUNITS'
4346       dimension ggg(3)
4347       ehpb=0.0D0
4348 cd      write(iout,*)'edis: nhpb=',nhpb,' fbr=',fbr
4349 cd      write(iout,*)'link_start=',link_start,' link_end=',link_end
4350       if (link_end.eq.0) return
4351       do i=link_start,link_end
4352 C If ihpb(i) and jhpb(i) > NRES, this is a SC-SC distance, otherwise a
4353 C CA-CA distance used in regularization of structure.
4354         ii=ihpb(i)
4355         jj=jhpb(i)
4356 C iii and jjj point to the residues for which the distance is assigned.
4357         if (ii.gt.nres) then
4358           iii=ii-nres
4359           jjj=jj-nres 
4360         else
4361           iii=ii
4362           jjj=jj
4363         endif
4364 c        write (iout,*) "i",i," ii",ii," iii",iii," jj",jj," jjj",jjj,
4365 c     &    dhpb(i),dhpb1(i),forcon(i)
4366 C 24/11/03 AL: SS bridges handled separately because of introducing a specific
4367 C    distance and angle dependent SS bond potential.
4368 cmc        if (ii.gt.nres .and. itype(iii).eq.1 .and. itype(jjj).eq.1) then
4369 C 18/07/06 MC: Use the convention that the first nss pairs are SS bonds
4370         if (.not.dyn_ss .and. i.le.nss) then
4371 C 15/02/13 CC dynamic SSbond - additional check
4372          if (ii.gt.nres 
4373      &       .and. itype(iii).eq.1 .and. itype(jjj).eq.1) then 
4374           call ssbond_ene(iii,jjj,eij)
4375           ehpb=ehpb+2*eij
4376          endif
4377 cd          write (iout,*) "eij",eij
4378         else if (ii.gt.nres .and. jj.gt.nres) then
4379 c Restraints from contact prediction
4380           dd=dist(ii,jj)
4381           if (dhpb1(i).gt.0.0d0) then
4382             ehpb=ehpb+2*forcon(i)*gnmr1(dd,dhpb(i),dhpb1(i))
4383             fac=forcon(i)*gnmr1prim(dd,dhpb(i),dhpb1(i))/dd
4384 c            write (iout,*) "beta nmr",
4385 c     &        dd,2*forcon(i)*gnmr1(dd,dhpb(i),dhpb1(i))
4386           else
4387             dd=dist(ii,jj)
4388             rdis=dd-dhpb(i)
4389 C Get the force constant corresponding to this distance.
4390             waga=forcon(i)
4391 C Calculate the contribution to energy.
4392             ehpb=ehpb+waga*rdis*rdis
4393 c            write (iout,*) "beta reg",dd,waga*rdis*rdis
4394 C
4395 C Evaluate gradient.
4396 C
4397             fac=waga*rdis/dd
4398           endif  
4399           do j=1,3
4400             ggg(j)=fac*(c(j,jj)-c(j,ii))
4401           enddo
4402           do j=1,3
4403             ghpbx(j,iii)=ghpbx(j,iii)-ggg(j)
4404             ghpbx(j,jjj)=ghpbx(j,jjj)+ggg(j)
4405           enddo
4406           do k=1,3
4407             ghpbc(k,jjj)=ghpbc(k,jjj)+ggg(k)
4408             ghpbc(k,iii)=ghpbc(k,iii)-ggg(k)
4409           enddo
4410         else
4411 C Calculate the distance between the two points and its difference from the
4412 C target distance.
4413           dd=dist(ii,jj)
4414           if (dhpb1(i).gt.0.0d0) then
4415             ehpb=ehpb+2*forcon(i)*gnmr1(dd,dhpb(i),dhpb1(i))
4416             fac=forcon(i)*gnmr1prim(dd,dhpb(i),dhpb1(i))/dd
4417 c            write (iout,*) "alph nmr",
4418 c     &        dd,2*forcon(i)*gnmr1(dd,dhpb(i),dhpb1(i))
4419           else
4420             rdis=dd-dhpb(i)
4421 C Get the force constant corresponding to this distance.
4422             waga=forcon(i)
4423 C Calculate the contribution to energy.
4424             ehpb=ehpb+waga*rdis*rdis
4425 c            write (iout,*) "alpha reg",dd,waga*rdis*rdis
4426 C
4427 C Evaluate gradient.
4428 C
4429             fac=waga*rdis/dd
4430           endif
4431 cd      print *,'i=',i,' ii=',ii,' jj=',jj,' dhpb=',dhpb(i),' dd=',dd,
4432 cd   &   ' waga=',waga,' fac=',fac
4433             do j=1,3
4434               ggg(j)=fac*(c(j,jj)-c(j,ii))
4435             enddo
4436 cd      print '(i3,3(1pe14.5))',i,(ggg(j),j=1,3)
4437 C If this is a SC-SC distance, we need to calculate the contributions to the
4438 C Cartesian gradient in the SC vectors (ghpbx).
4439           if (iii.lt.ii) then
4440           do j=1,3
4441             ghpbx(j,iii)=ghpbx(j,iii)-ggg(j)
4442             ghpbx(j,jjj)=ghpbx(j,jjj)+ggg(j)
4443           enddo
4444           endif
4445 cgrad        do j=iii,jjj-1
4446 cgrad          do k=1,3
4447 cgrad            ghpbc(k,j)=ghpbc(k,j)+ggg(k)
4448 cgrad          enddo
4449 cgrad        enddo
4450           do k=1,3
4451             ghpbc(k,jjj)=ghpbc(k,jjj)+ggg(k)
4452             ghpbc(k,iii)=ghpbc(k,iii)-ggg(k)
4453           enddo
4454         endif
4455       enddo
4456       ehpb=0.5D0*ehpb
4457       return
4458       end
4459 C--------------------------------------------------------------------------
4460       subroutine ssbond_ene(i,j,eij)
4461
4462 C Calculate the distance and angle dependent SS-bond potential energy
4463 C using a free-energy function derived based on RHF/6-31G** ab initio
4464 C calculations of diethyl disulfide.
4465 C
4466 C A. Liwo and U. Kozlowska, 11/24/03
4467 C
4468       implicit real*8 (a-h,o-z)
4469       include 'DIMENSIONS'
4470       include 'COMMON.SBRIDGE'
4471       include 'COMMON.CHAIN'
4472       include 'COMMON.DERIV'
4473       include 'COMMON.LOCAL'
4474       include 'COMMON.INTERACT'
4475       include 'COMMON.VAR'
4476       include 'COMMON.IOUNITS'
4477       double precision erij(3),dcosom1(3),dcosom2(3),gg(3)
4478       itypi=itype(i)
4479       xi=c(1,nres+i)
4480       yi=c(2,nres+i)
4481       zi=c(3,nres+i)
4482       dxi=dc_norm(1,nres+i)
4483       dyi=dc_norm(2,nres+i)
4484       dzi=dc_norm(3,nres+i)
4485 c      dsci_inv=dsc_inv(itypi)
4486       dsci_inv=vbld_inv(nres+i)
4487       itypj=itype(j)
4488 c      dscj_inv=dsc_inv(itypj)
4489       dscj_inv=vbld_inv(nres+j)
4490       xj=c(1,nres+j)-xi
4491       yj=c(2,nres+j)-yi
4492       zj=c(3,nres+j)-zi
4493       dxj=dc_norm(1,nres+j)
4494       dyj=dc_norm(2,nres+j)
4495       dzj=dc_norm(3,nres+j)
4496       rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
4497       rij=dsqrt(rrij)
4498       erij(1)=xj*rij
4499       erij(2)=yj*rij
4500       erij(3)=zj*rij
4501       om1=dxi*erij(1)+dyi*erij(2)+dzi*erij(3)
4502       om2=dxj*erij(1)+dyj*erij(2)+dzj*erij(3)
4503       om12=dxi*dxj+dyi*dyj+dzi*dzj
4504       do k=1,3
4505         dcosom1(k)=rij*(dc_norm(k,nres+i)-om1*erij(k))
4506         dcosom2(k)=rij*(dc_norm(k,nres+j)-om2*erij(k))
4507       enddo
4508       rij=1.0d0/rij
4509       deltad=rij-d0cm
4510       deltat1=1.0d0-om1
4511       deltat2=1.0d0+om2
4512       deltat12=om2-om1+2.0d0
4513       cosphi=om12-om1*om2
4514       eij=akcm*deltad*deltad+akth*(deltat1*deltat1+deltat2*deltat2)
4515      &  +akct*deltad*deltat12+ebr
4516      &  +v1ss*cosphi+v2ss*cosphi*cosphi+v3ss*cosphi*cosphi*cosphi
4517 c      write(iout,*) i,j,"rij",rij,"d0cm",d0cm," akcm",akcm," akth",akth,
4518 c     &  " akct",akct," deltad",deltad," deltat",deltat1,deltat2,
4519 c     &  " deltat12",deltat12," eij",eij 
4520       ed=2*akcm*deltad+akct*deltat12
4521       pom1=akct*deltad
4522       pom2=v1ss+2*v2ss*cosphi+3*v3ss*cosphi*cosphi
4523       eom1=-2*akth*deltat1-pom1-om2*pom2
4524       eom2= 2*akth*deltat2+pom1-om1*pom2
4525       eom12=pom2
4526       do k=1,3
4527         ggk=ed*erij(k)+eom1*dcosom1(k)+eom2*dcosom2(k)
4528         ghpbx(k,i)=ghpbx(k,i)-ggk
4529      &            +(eom12*(dc_norm(k,nres+j)-om12*dc_norm(k,nres+i))
4530      &            +eom1*(erij(k)-om1*dc_norm(k,nres+i)))*dsci_inv
4531         ghpbx(k,j)=ghpbx(k,j)+ggk
4532      &            +(eom12*(dc_norm(k,nres+i)-om12*dc_norm(k,nres+j))
4533      &            +eom2*(erij(k)-om2*dc_norm(k,nres+j)))*dscj_inv
4534         ghpbc(k,i)=ghpbc(k,i)-ggk
4535         ghpbc(k,j)=ghpbc(k,j)+ggk
4536       enddo
4537 C
4538 C Calculate the components of the gradient in DC and X
4539 C
4540 cgrad      do k=i,j-1
4541 cgrad        do l=1,3
4542 cgrad          ghpbc(l,k)=ghpbc(l,k)+gg(l)
4543 cgrad        enddo
4544 cgrad      enddo
4545       return
4546       end
4547 C--------------------------------------------------------------------------
4548       subroutine ebond(estr)
4549 c
4550 c Evaluate the energy of stretching of the CA-CA and CA-SC virtual bonds
4551 c
4552       implicit real*8 (a-h,o-z)
4553       include 'DIMENSIONS'
4554       include 'COMMON.LOCAL'
4555       include 'COMMON.GEO'
4556       include 'COMMON.INTERACT'
4557       include 'COMMON.DERIV'
4558       include 'COMMON.VAR'
4559       include 'COMMON.CHAIN'
4560       include 'COMMON.IOUNITS'
4561       include 'COMMON.NAMES'
4562       include 'COMMON.FFIELD'
4563       include 'COMMON.CONTROL'
4564       include 'COMMON.SETUP'
4565       double precision u(3),ud(3)
4566       estr=0.0d0
4567       do i=ibondp_start,ibondp_end
4568         diff = vbld(i)-vbldp0
4569 c        write (iout,*) i,vbld(i),vbldp0,diff,AKP*diff*diff
4570         estr=estr+diff*diff
4571         do j=1,3
4572           gradb(j,i-1)=AKP*diff*dc(j,i-1)/vbld(i)
4573         enddo
4574 c        write (iout,'(i5,3f10.5)') i,(gradb(j,i-1),j=1,3)
4575       enddo
4576       estr=0.5d0*AKP*estr
4577 c
4578 c 09/18/07 AL: multimodal bond potential based on AM1 CA-SC PMF's included
4579 c
4580       do i=ibond_start,ibond_end
4581         iti=itype(i)
4582         if (iti.ne.10) then
4583           nbi=nbondterm(iti)
4584           if (nbi.eq.1) then
4585             diff=vbld(i+nres)-vbldsc0(1,iti)
4586 c            write (iout,*) i,iti,vbld(i+nres),vbldsc0(1,iti),diff,
4587 c     &      AKSC(1,iti),AKSC(1,iti)*diff*diff
4588             estr=estr+0.5d0*AKSC(1,iti)*diff*diff
4589             do j=1,3
4590               gradbx(j,i)=AKSC(1,iti)*diff*dc(j,i+nres)/vbld(i+nres)
4591             enddo
4592           else
4593             do j=1,nbi
4594               diff=vbld(i+nres)-vbldsc0(j,iti) 
4595               ud(j)=aksc(j,iti)*diff
4596               u(j)=abond0(j,iti)+0.5d0*ud(j)*diff
4597             enddo
4598             uprod=u(1)
4599             do j=2,nbi
4600               uprod=uprod*u(j)
4601             enddo
4602             usum=0.0d0
4603             usumsqder=0.0d0
4604             do j=1,nbi
4605               uprod1=1.0d0
4606               uprod2=1.0d0
4607               do k=1,nbi
4608                 if (k.ne.j) then
4609                   uprod1=uprod1*u(k)
4610                   uprod2=uprod2*u(k)*u(k)
4611                 endif
4612               enddo
4613               usum=usum+uprod1
4614               usumsqder=usumsqder+ud(j)*uprod2   
4615             enddo
4616             estr=estr+uprod/usum
4617             do j=1,3
4618              gradbx(j,i)=usumsqder/(usum*usum)*dc(j,i+nres)/vbld(i+nres)
4619             enddo
4620           endif
4621         endif
4622       enddo
4623       return
4624       end 
4625 #ifdef CRYST_THETA
4626 C--------------------------------------------------------------------------
4627       subroutine ebend(etheta)
4628 C
4629 C Evaluate the virtual-bond-angle energy given the virtual-bond dihedral
4630 C angles gamma and its derivatives in consecutive thetas and gammas.
4631 C
4632       implicit real*8 (a-h,o-z)
4633       include 'DIMENSIONS'
4634       include 'COMMON.LOCAL'
4635       include 'COMMON.GEO'
4636       include 'COMMON.INTERACT'
4637       include 'COMMON.DERIV'
4638       include 'COMMON.VAR'
4639       include 'COMMON.CHAIN'
4640       include 'COMMON.IOUNITS'
4641       include 'COMMON.NAMES'
4642       include 'COMMON.FFIELD'
4643       include 'COMMON.CONTROL'
4644       common /calcthet/ term1,term2,termm,diffak,ratak,
4645      & ak,aktc,termpre,termexp,sigc,sig0i,time11,time12,sigcsq,
4646      & delthe0,sig0inv,sigtc,sigsqtc,delthec,it
4647       double precision y(2),z(2)
4648       delta=0.02d0*pi
4649 c      time11=dexp(-2*time)
4650 c      time12=1.0d0
4651       etheta=0.0D0
4652 c     write (*,'(a,i2)') 'EBEND ICG=',icg
4653       do i=ithet_start,ithet_end
4654 C Zero the energy function and its derivative at 0 or pi.
4655         call splinthet(theta(i),0.5d0*delta,ss,ssd)
4656         it=itype(i-1)
4657         if (i.gt.3) then
4658 #ifdef OSF
4659           phii=phi(i)
4660           if (phii.ne.phii) phii=150.0
4661 #else
4662           phii=phi(i)
4663 #endif
4664           y(1)=dcos(phii)
4665           y(2)=dsin(phii)
4666         else 
4667           y(1)=0.0D0
4668           y(2)=0.0D0
4669         endif
4670         if (i.lt.nres) then
4671 #ifdef OSF
4672           phii1=phi(i+1)
4673           if (phii1.ne.phii1) phii1=150.0
4674           phii1=pinorm(phii1)
4675           z(1)=cos(phii1)
4676 #else
4677           phii1=phi(i+1)
4678           z(1)=dcos(phii1)
4679 #endif
4680           z(2)=dsin(phii1)
4681         else
4682           z(1)=0.0D0
4683           z(2)=0.0D0
4684         endif  
4685 C Calculate the "mean" value of theta from the part of the distribution
4686 C dependent on the adjacent virtual-bond-valence angles (gamma1 & gamma2).
4687 C In following comments this theta will be referred to as t_c.
4688         thet_pred_mean=0.0d0
4689         do k=1,2
4690           athetk=athet(k,it)
4691           bthetk=bthet(k,it)
4692           thet_pred_mean=thet_pred_mean+athetk*y(k)+bthetk*z(k)
4693         enddo
4694         dthett=thet_pred_mean*ssd
4695         thet_pred_mean=thet_pred_mean*ss+a0thet(it)
4696 C Derivatives of the "mean" values in gamma1 and gamma2.
4697         dthetg1=(-athet(1,it)*y(2)+athet(2,it)*y(1))*ss
4698         dthetg2=(-bthet(1,it)*z(2)+bthet(2,it)*z(1))*ss
4699         if (theta(i).gt.pi-delta) then
4700           call theteng(pi-delta,thet_pred_mean,theta0(it),f0,fprim0,
4701      &         E_tc0)
4702           call mixder(pi-delta,thet_pred_mean,theta0(it),fprim_tc0)
4703           call theteng(pi,thet_pred_mean,theta0(it),f1,fprim1,E_tc1)
4704           call spline1(theta(i),pi-delta,delta,f0,f1,fprim0,ethetai,
4705      &        E_theta)
4706           call spline2(theta(i),pi-delta,delta,E_tc0,E_tc1,fprim_tc0,
4707      &        E_tc)
4708         else if (theta(i).lt.delta) then
4709           call theteng(delta,thet_pred_mean,theta0(it),f0,fprim0,E_tc0)
4710           call theteng(0.0d0,thet_pred_mean,theta0(it),f1,fprim1,E_tc1)
4711           call spline1(theta(i),delta,-delta,f0,f1,fprim0,ethetai,
4712      &        E_theta)
4713           call mixder(delta,thet_pred_mean,theta0(it),fprim_tc0)
4714           call spline2(theta(i),delta,-delta,E_tc0,E_tc1,fprim_tc0,
4715      &        E_tc)
4716         else
4717           call theteng(theta(i),thet_pred_mean,theta0(it),ethetai,
4718      &        E_theta,E_tc)
4719         endif
4720         etheta=etheta+ethetai
4721         if (energy_dec) write (iout,'(a6,i5,0pf7.3)')
4722      &      'ebend',i,ethetai
4723         if (i.gt.3) gloc(i-3,icg)=gloc(i-3,icg)+wang*E_tc*dthetg1
4724         if (i.lt.nres) gloc(i-2,icg)=gloc(i-2,icg)+wang*E_tc*dthetg2
4725         gloc(nphi+i-2,icg)=wang*(E_theta+E_tc*dthett)+gloc(nphi+i-2,icg)
4726       enddo
4727 C Ufff.... We've done all this!!! 
4728       return
4729       end
4730 C---------------------------------------------------------------------------
4731       subroutine theteng(thetai,thet_pred_mean,theta0i,ethetai,E_theta,
4732      &     E_tc)
4733       implicit real*8 (a-h,o-z)
4734       include 'DIMENSIONS'
4735       include 'COMMON.LOCAL'
4736       include 'COMMON.IOUNITS'
4737       common /calcthet/ term1,term2,termm,diffak,ratak,
4738      & ak,aktc,termpre,termexp,sigc,sig0i,time11,time12,sigcsq,
4739      & delthe0,sig0inv,sigtc,sigsqtc,delthec,it
4740 C Calculate the contributions to both Gaussian lobes.
4741 C 6/6/97 - Deform the Gaussians using the factor of 1/(1+time)
4742 C The "polynomial part" of the "standard deviation" of this part of 
4743 C the distribution.
4744         sig=polthet(3,it)
4745         do j=2,0,-1
4746           sig=sig*thet_pred_mean+polthet(j,it)
4747         enddo
4748 C Derivative of the "interior part" of the "standard deviation of the" 
4749 C gamma-dependent Gaussian lobe in t_c.
4750         sigtc=3*polthet(3,it)
4751         do j=2,1,-1
4752           sigtc=sigtc*thet_pred_mean+j*polthet(j,it)
4753         enddo
4754         sigtc=sig*sigtc
4755 C Set the parameters of both Gaussian lobes of the distribution.
4756 C "Standard deviation" of the gamma-dependent Gaussian lobe (sigtc)
4757         fac=sig*sig+sigc0(it)
4758         sigcsq=fac+fac
4759         sigc=1.0D0/sigcsq
4760 C Following variable (sigsqtc) is -(1/2)d[sigma(t_c)**(-2))]/dt_c
4761         sigsqtc=-4.0D0*sigcsq*sigtc
4762 c       print *,i,sig,sigtc,sigsqtc
4763 C Following variable (sigtc) is d[sigma(t_c)]/dt_c
4764         sigtc=-sigtc/(fac*fac)
4765 C Following variable is sigma(t_c)**(-2)
4766         sigcsq=sigcsq*sigcsq
4767         sig0i=sig0(it)
4768         sig0inv=1.0D0/sig0i**2
4769         delthec=thetai-thet_pred_mean
4770         delthe0=thetai-theta0i
4771         term1=-0.5D0*sigcsq*delthec*delthec
4772         term2=-0.5D0*sig0inv*delthe0*delthe0
4773 C Following fuzzy logic is to avoid underflows in dexp and subsequent INFs and
4774 C NaNs in taking the logarithm. We extract the largest exponent which is added
4775 C to the energy (this being the log of the distribution) at the end of energy
4776 C term evaluation for this virtual-bond angle.
4777         if (term1.gt.term2) then
4778           termm=term1
4779           term2=dexp(term2-termm)
4780           term1=1.0d0
4781         else
4782           termm=term2
4783           term1=dexp(term1-termm)
4784           term2=1.0d0
4785         endif
4786 C The ratio between the gamma-independent and gamma-dependent lobes of
4787 C the distribution is a Gaussian function of thet_pred_mean too.
4788         diffak=gthet(2,it)-thet_pred_mean
4789         ratak=diffak/gthet(3,it)**2
4790         ak=dexp(gthet(1,it)-0.5D0*diffak*ratak)
4791 C Let's differentiate it in thet_pred_mean NOW.
4792         aktc=ak*ratak
4793 C Now put together the distribution terms to make complete distribution.
4794         termexp=term1+ak*term2
4795         termpre=sigc+ak*sig0i
4796 C Contribution of the bending energy from this theta is just the -log of
4797 C the sum of the contributions from the two lobes and the pre-exponential
4798 C factor. Simple enough, isn't it?
4799         ethetai=(-dlog(termexp)-termm+dlog(termpre))
4800 C NOW the derivatives!!!
4801 C 6/6/97 Take into account the deformation.
4802         E_theta=(delthec*sigcsq*term1
4803      &       +ak*delthe0*sig0inv*term2)/termexp
4804         E_tc=((sigtc+aktc*sig0i)/termpre
4805      &      -((delthec*sigcsq+delthec*delthec*sigsqtc)*term1+
4806      &       aktc*term2)/termexp)
4807       return
4808       end
4809 c-----------------------------------------------------------------------------
4810       subroutine mixder(thetai,thet_pred_mean,theta0i,E_tc_t)
4811       implicit real*8 (a-h,o-z)
4812       include 'DIMENSIONS'
4813       include 'COMMON.LOCAL'
4814       include 'COMMON.IOUNITS'
4815       common /calcthet/ term1,term2,termm,diffak,ratak,
4816      & ak,aktc,termpre,termexp,sigc,sig0i,time11,time12,sigcsq,
4817      & delthe0,sig0inv,sigtc,sigsqtc,delthec,it
4818       delthec=thetai-thet_pred_mean
4819       delthe0=thetai-theta0i
4820 C "Thank you" to MAPLE (probably spared one day of hand-differentiation).
4821       t3 = thetai-thet_pred_mean
4822       t6 = t3**2
4823       t9 = term1
4824       t12 = t3*sigcsq
4825       t14 = t12+t6*sigsqtc
4826       t16 = 1.0d0
4827       t21 = thetai-theta0i
4828       t23 = t21**2
4829       t26 = term2
4830       t27 = t21*t26
4831       t32 = termexp
4832       t40 = t32**2
4833       E_tc_t = -((sigcsq+2.D0*t3*sigsqtc)*t9-t14*sigcsq*t3*t16*t9
4834      & -aktc*sig0inv*t27)/t32+(t14*t9+aktc*t26)/t40
4835      & *(-t12*t9-ak*sig0inv*t27)
4836       return
4837       end
4838 #else
4839 C--------------------------------------------------------------------------
4840       subroutine ebend(etheta)
4841 C
4842 C Evaluate the virtual-bond-angle energy given the virtual-bond dihedral
4843 C angles gamma and its derivatives in consecutive thetas and gammas.
4844 C ab initio-derived potentials from 
4845 c Kozlowska et al., J. Phys.: Condens. Matter 19 (2007) 285203
4846 C
4847       implicit real*8 (a-h,o-z)
4848       include 'DIMENSIONS'
4849       include 'COMMON.LOCAL'
4850       include 'COMMON.GEO'
4851       include 'COMMON.INTERACT'
4852       include 'COMMON.DERIV'
4853       include 'COMMON.VAR'
4854       include 'COMMON.CHAIN'
4855       include 'COMMON.IOUNITS'
4856       include 'COMMON.NAMES'
4857       include 'COMMON.FFIELD'
4858       include 'COMMON.CONTROL'
4859       double precision coskt(mmaxtheterm),sinkt(mmaxtheterm),
4860      & cosph1(maxsingle),sinph1(maxsingle),cosph2(maxsingle),
4861      & sinph2(maxsingle),cosph1ph2(maxdouble,maxdouble),
4862      & sinph1ph2(maxdouble,maxdouble)
4863       logical lprn /.false./, lprn1 /.false./
4864       etheta=0.0D0
4865       do i=ithet_start,ithet_end
4866         if ((itype(i-1).eq.ntyp1).or.(itype(i-2).eq.ntyp1).or.
4867      &(itype(i).eq.ntyp1)) cycle
4868         dethetai=0.0d0
4869         dephii=0.0d0
4870         dephii1=0.0d0
4871         theti2=0.5d0*theta(i)
4872         ityp2=ithetyp(itype(i-1))
4873         do k=1,nntheterm
4874           coskt(k)=dcos(k*theti2)
4875           sinkt(k)=dsin(k*theti2)
4876         enddo
4877 C        if (i.gt.3) then
4878          if (i.gt.3 .and. itype(i-3).ne.ntyp1) then
4879 #ifdef OSF
4880           phii=phi(i)
4881           if (phii.ne.phii) phii=150.0
4882 #else
4883           phii=phi(i)
4884 #endif
4885           ityp1=ithetyp(itype(i-2))
4886           do k=1,nsingle
4887             cosph1(k)=dcos(k*phii)
4888             sinph1(k)=dsin(k*phii)
4889           enddo
4890         else
4891           phii=0.0d0
4892           ityp1=ithetyp(itype(i-2))
4893           do k=1,nsingle
4894             cosph1(k)=0.0d0
4895             sinph1(k)=0.0d0
4896           enddo 
4897         endif
4898         if ((i.lt.nres).and. itype(i+1).ne.ntyp1) then
4899 #ifdef OSF
4900           phii1=phi(i+1)
4901           if (phii1.ne.phii1) phii1=150.0
4902           phii1=pinorm(phii1)
4903 #else
4904           phii1=phi(i+1)
4905 #endif
4906           ityp3=ithetyp(itype(i))
4907           do k=1,nsingle
4908             cosph2(k)=dcos(k*phii1)
4909             sinph2(k)=dsin(k*phii1)
4910           enddo
4911         else
4912           phii1=0.0d0
4913           ityp3=ithetyp(itype(i))
4914           do k=1,nsingle
4915             cosph2(k)=0.0d0
4916             sinph2(k)=0.0d0
4917           enddo
4918         endif  
4919         ethetai=aa0thet(ityp1,ityp2,ityp3)
4920         do k=1,ndouble
4921           do l=1,k-1
4922             ccl=cosph1(l)*cosph2(k-l)
4923             ssl=sinph1(l)*sinph2(k-l)
4924             scl=sinph1(l)*cosph2(k-l)
4925             csl=cosph1(l)*sinph2(k-l)
4926             cosph1ph2(l,k)=ccl-ssl
4927             cosph1ph2(k,l)=ccl+ssl
4928             sinph1ph2(l,k)=scl+csl
4929             sinph1ph2(k,l)=scl-csl
4930           enddo
4931         enddo
4932         if (lprn) then
4933         write (iout,*) "i",i," ityp1",ityp1," ityp2",ityp2,
4934      &    " ityp3",ityp3," theti2",theti2," phii",phii," phii1",phii1
4935         write (iout,*) "coskt and sinkt"
4936         do k=1,nntheterm
4937           write (iout,*) k,coskt(k),sinkt(k)
4938         enddo
4939         endif
4940         do k=1,ntheterm
4941           ethetai=ethetai+aathet(k,ityp1,ityp2,ityp3)*sinkt(k)
4942           dethetai=dethetai+0.5d0*k*aathet(k,ityp1,ityp2,ityp3)
4943      &      *coskt(k)
4944           if (lprn)
4945      &    write (iout,*) "k",k," aathet",aathet(k,ityp1,ityp2,ityp3),
4946      &     " ethetai",ethetai
4947         enddo
4948         if (lprn) then
4949         write (iout,*) "cosph and sinph"
4950         do k=1,nsingle
4951           write (iout,*) k,cosph1(k),sinph1(k),cosph2(k),sinph2(k)
4952         enddo
4953         write (iout,*) "cosph1ph2 and sinph2ph2"
4954         do k=2,ndouble
4955           do l=1,k-1
4956             write (iout,*) l,k,cosph1ph2(l,k),cosph1ph2(k,l),
4957      &         sinph1ph2(l,k),sinph1ph2(k,l) 
4958           enddo
4959         enddo
4960         write(iout,*) "ethetai",ethetai
4961         endif
4962         do m=1,ntheterm2
4963           do k=1,nsingle
4964             aux=bbthet(k,m,ityp1,ityp2,ityp3)*cosph1(k)
4965      &         +ccthet(k,m,ityp1,ityp2,ityp3)*sinph1(k)
4966      &         +ddthet(k,m,ityp1,ityp2,ityp3)*cosph2(k)
4967      &         +eethet(k,m,ityp1,ityp2,ityp3)*sinph2(k)
4968             ethetai=ethetai+sinkt(m)*aux
4969             dethetai=dethetai+0.5d0*m*aux*coskt(m)
4970             dephii=dephii+k*sinkt(m)*(
4971      &          ccthet(k,m,ityp1,ityp2,ityp3)*cosph1(k)-
4972      &          bbthet(k,m,ityp1,ityp2,ityp3)*sinph1(k))
4973             dephii1=dephii1+k*sinkt(m)*(
4974      &          eethet(k,m,ityp1,ityp2,ityp3)*cosph2(k)-
4975      &          ddthet(k,m,ityp1,ityp2,ityp3)*sinph2(k))
4976             if (lprn)
4977      &      write (iout,*) "m",m," k",k," bbthet",
4978      &         bbthet(k,m,ityp1,ityp2,ityp3)," ccthet",
4979      &         ccthet(k,m,ityp1,ityp2,ityp3)," ddthet",
4980      &         ddthet(k,m,ityp1,ityp2,ityp3)," eethet",
4981      &         eethet(k,m,ityp1,ityp2,ityp3)," ethetai",ethetai
4982           enddo
4983         enddo
4984         if (lprn)
4985      &  write(iout,*) "ethetai",ethetai
4986         do m=1,ntheterm3
4987           do k=2,ndouble
4988             do l=1,k-1
4989               aux=ffthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)+
4990      &            ffthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l)+
4991      &            ggthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)+
4992      &            ggthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)
4993               ethetai=ethetai+sinkt(m)*aux
4994               dethetai=dethetai+0.5d0*m*coskt(m)*aux
4995               dephii=dephii+l*sinkt(m)*(
4996      &           -ffthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)-
4997      &            ffthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)+
4998      &            ggthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)+
4999      &            ggthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l))
5000               dephii1=dephii1+(k-l)*sinkt(m)*(
5001      &           -ffthet(l,k,m,ityp1,ityp2,ityp3)*sinph1ph2(l,k)+
5002      &            ffthet(k,l,m,ityp1,ityp2,ityp3)*sinph1ph2(k,l)+
5003      &            ggthet(l,k,m,ityp1,ityp2,ityp3)*cosph1ph2(l,k)-
5004      &            ggthet(k,l,m,ityp1,ityp2,ityp3)*cosph1ph2(k,l))
5005               if (lprn) then
5006               write (iout,*) "m",m," k",k," l",l," ffthet",
5007      &            ffthet(l,k,m,ityp1,ityp2,ityp3),
5008      &            ffthet(k,l,m,ityp1,ityp2,ityp3)," ggthet",
5009      &            ggthet(l,k,m,ityp1,ityp2,ityp3),
5010      &            ggthet(k,l,m,ityp1,ityp2,ityp3)," ethetai",ethetai
5011               write (iout,*) cosph1ph2(l,k)*sinkt(m),
5012      &            cosph1ph2(k,l)*sinkt(m),
5013      &            sinph1ph2(l,k)*sinkt(m),sinph1ph2(k,l)*sinkt(m)
5014               endif
5015             enddo
5016           enddo
5017         enddo
5018 10      continue
5019 c        lprn1=.true.
5020         if (lprn1) write (iout,'(a4,i2,3f8.1,9h ethetai ,f10.5)') 
5021      &  'ebe', i,theta(i)*rad2deg,phii*rad2deg,
5022      &   phii1*rad2deg,ethetai
5023 c        lprn1=.false.
5024         etheta=etheta+ethetai
5025         if (i.gt.3) gloc(i-3,icg)=gloc(i-3,icg)+wang*dephii
5026         if (i.lt.nres) gloc(i-2,icg)=gloc(i-2,icg)+wang*dephii1
5027         gloc(nphi+i-2,icg)=gloc(nphi+i-2,icg)+wang*dethetai
5028       enddo
5029       return
5030       end
5031 #endif
5032 #ifdef CRYST_SC
5033 c-----------------------------------------------------------------------------
5034       subroutine esc(escloc)
5035 C Calculate the local energy of a side chain and its derivatives in the
5036 C corresponding virtual-bond valence angles THETA and the spherical angles 
5037 C ALPHA and OMEGA.
5038       implicit real*8 (a-h,o-z)
5039       include 'DIMENSIONS'
5040       include 'COMMON.GEO'
5041       include 'COMMON.LOCAL'
5042       include 'COMMON.VAR'
5043       include 'COMMON.INTERACT'
5044       include 'COMMON.DERIV'
5045       include 'COMMON.CHAIN'
5046       include 'COMMON.IOUNITS'
5047       include 'COMMON.NAMES'
5048       include 'COMMON.FFIELD'
5049       include 'COMMON.CONTROL'
5050       double precision x(3),dersc(3),xemp(3),dersc0(3),dersc1(3),
5051      &     ddersc0(3),ddummy(3),xtemp(3),temp(3)
5052       common /sccalc/ time11,time12,time112,theti,it,nlobit
5053       delta=0.02d0*pi
5054       escloc=0.0D0
5055 c     write (iout,'(a)') 'ESC'
5056       do i=loc_start,loc_end
5057         it=itype(i)
5058         if (it.eq.10) goto 1
5059         nlobit=nlob(it)
5060 c       print *,'i=',i,' it=',it,' nlobit=',nlobit
5061 c       write (iout,*) 'i=',i,' ssa=',ssa,' ssad=',ssad
5062         theti=theta(i+1)-pipol
5063         x(1)=dtan(theti)
5064         x(2)=alph(i)
5065         x(3)=omeg(i)
5066
5067         if (x(2).gt.pi-delta) then
5068           xtemp(1)=x(1)
5069           xtemp(2)=pi-delta
5070           xtemp(3)=x(3)
5071           call enesc(xtemp,escloci0,dersc0,ddersc0,.true.)
5072           xtemp(2)=pi
5073           call enesc(xtemp,escloci1,dersc1,ddummy,.false.)
5074           call spline1(x(2),pi-delta,delta,escloci0,escloci1,dersc0(2),
5075      &        escloci,dersc(2))
5076           call spline2(x(2),pi-delta,delta,dersc0(1),dersc1(1),
5077      &        ddersc0(1),dersc(1))
5078           call spline2(x(2),pi-delta,delta,dersc0(3),dersc1(3),
5079      &        ddersc0(3),dersc(3))
5080           xtemp(2)=pi-delta
5081           call enesc_bound(xtemp,esclocbi0,dersc0,dersc12,.true.)
5082           xtemp(2)=pi
5083           call enesc_bound(xtemp,esclocbi1,dersc1,chuju,.false.)
5084           call spline1(x(2),pi-delta,delta,esclocbi0,esclocbi1,
5085      &            dersc0(2),esclocbi,dersc02)
5086           call spline2(x(2),pi-delta,delta,dersc0(1),dersc1(1),
5087      &            dersc12,dersc01)
5088           call splinthet(x(2),0.5d0*delta,ss,ssd)
5089           dersc0(1)=dersc01
5090           dersc0(2)=dersc02
5091           dersc0(3)=0.0d0
5092           do k=1,3
5093             dersc(k)=ss*dersc(k)+(1.0d0-ss)*dersc0(k)
5094           enddo
5095           dersc(2)=dersc(2)+ssd*(escloci-esclocbi)
5096 c         write (iout,*) 'i=',i,x(2)*rad2deg,escloci0,escloci,
5097 c    &             esclocbi,ss,ssd
5098           escloci=ss*escloci+(1.0d0-ss)*esclocbi
5099 c         escloci=esclocbi
5100 c         write (iout,*) escloci
5101         else if (x(2).lt.delta) then
5102           xtemp(1)=x(1)
5103           xtemp(2)=delta
5104           xtemp(3)=x(3)
5105           call enesc(xtemp,escloci0,dersc0,ddersc0,.true.)
5106           xtemp(2)=0.0d0
5107           call enesc(xtemp,escloci1,dersc1,ddummy,.false.)
5108           call spline1(x(2),delta,-delta,escloci0,escloci1,dersc0(2),
5109      &        escloci,dersc(2))
5110           call spline2(x(2),delta,-delta,dersc0(1),dersc1(1),
5111      &        ddersc0(1),dersc(1))
5112           call spline2(x(2),delta,-delta,dersc0(3),dersc1(3),
5113      &        ddersc0(3),dersc(3))
5114           xtemp(2)=delta
5115           call enesc_bound(xtemp,esclocbi0,dersc0,dersc12,.true.)
5116           xtemp(2)=0.0d0
5117           call enesc_bound(xtemp,esclocbi1,dersc1,chuju,.false.)
5118           call spline1(x(2),delta,-delta,esclocbi0,esclocbi1,
5119      &            dersc0(2),esclocbi,dersc02)
5120           call spline2(x(2),delta,-delta,dersc0(1),dersc1(1),
5121      &            dersc12,dersc01)
5122           dersc0(1)=dersc01
5123           dersc0(2)=dersc02
5124           dersc0(3)=0.0d0
5125           call splinthet(x(2),0.5d0*delta,ss,ssd)
5126           do k=1,3
5127             dersc(k)=ss*dersc(k)+(1.0d0-ss)*dersc0(k)
5128           enddo
5129           dersc(2)=dersc(2)+ssd*(escloci-esclocbi)
5130 c         write (iout,*) 'i=',i,x(2)*rad2deg,escloci0,escloci,
5131 c    &             esclocbi,ss,ssd
5132           escloci=ss*escloci+(1.0d0-ss)*esclocbi
5133 c         write (iout,*) escloci
5134         else
5135           call enesc(x,escloci,dersc,ddummy,.false.)
5136         endif
5137
5138         escloc=escloc+escloci
5139         if (energy_dec) write (iout,'(a6,i5,0pf7.3)')
5140      &     'escloc',i,escloci
5141 c       write (iout,*) 'i=',i,' escloci=',escloci,' dersc=',dersc
5142
5143         gloc(nphi+i-1,icg)=gloc(nphi+i-1,icg)+
5144      &   wscloc*dersc(1)
5145         gloc(ialph(i,1),icg)=wscloc*dersc(2)
5146         gloc(ialph(i,1)+nside,icg)=wscloc*dersc(3)
5147     1   continue
5148       enddo
5149       return
5150       end
5151 C---------------------------------------------------------------------------
5152       subroutine enesc(x,escloci,dersc,ddersc,mixed)
5153       implicit real*8 (a-h,o-z)
5154       include 'DIMENSIONS'
5155       include 'COMMON.GEO'
5156       include 'COMMON.LOCAL'
5157       include 'COMMON.IOUNITS'
5158       common /sccalc/ time11,time12,time112,theti,it,nlobit
5159       double precision x(3),z(3),Ax(3,maxlob,-1:1),dersc(3),ddersc(3)
5160       double precision contr(maxlob,-1:1)
5161       logical mixed
5162 c       write (iout,*) 'it=',it,' nlobit=',nlobit
5163         escloc_i=0.0D0
5164         do j=1,3
5165           dersc(j)=0.0D0
5166           if (mixed) ddersc(j)=0.0d0
5167         enddo
5168         x3=x(3)
5169
5170 C Because of periodicity of the dependence of the SC energy in omega we have
5171 C to add up the contributions from x(3)-2*pi, x(3), and x(3+2*pi).
5172 C To avoid underflows, first compute & store the exponents.
5173
5174         do iii=-1,1
5175
5176           x(3)=x3+iii*dwapi
5177  
5178           do j=1,nlobit
5179             do k=1,3
5180               z(k)=x(k)-censc(k,j,it)
5181             enddo
5182             do k=1,3
5183               Axk=0.0D0
5184               do l=1,3
5185                 Axk=Axk+gaussc(l,k,j,it)*z(l)
5186               enddo
5187               Ax(k,j,iii)=Axk
5188             enddo 
5189             expfac=0.0D0 
5190             do k=1,3
5191               expfac=expfac+Ax(k,j,iii)*z(k)
5192             enddo
5193             contr(j,iii)=expfac
5194           enddo ! j
5195
5196         enddo ! iii
5197
5198         x(3)=x3
5199 C As in the case of ebend, we want to avoid underflows in exponentiation and
5200 C subsequent NaNs and INFs in energy calculation.
5201 C Find the largest exponent
5202         emin=contr(1,-1)
5203         do iii=-1,1
5204           do j=1,nlobit
5205             if (emin.gt.contr(j,iii)) emin=contr(j,iii)
5206           enddo 
5207         enddo
5208         emin=0.5D0*emin
5209 cd      print *,'it=',it,' emin=',emin
5210
5211 C Compute the contribution to SC energy and derivatives
5212         do iii=-1,1
5213
5214           do j=1,nlobit
5215 #ifdef OSF
5216             adexp=bsc(j,it)-0.5D0*contr(j,iii)+emin
5217             if(adexp.ne.adexp) adexp=1.0
5218             expfac=dexp(adexp)
5219 #else
5220             expfac=dexp(bsc(j,it)-0.5D0*contr(j,iii)+emin)
5221 #endif
5222 cd          print *,'j=',j,' expfac=',expfac
5223             escloc_i=escloc_i+expfac
5224             do k=1,3
5225               dersc(k)=dersc(k)+Ax(k,j,iii)*expfac
5226             enddo
5227             if (mixed) then
5228               do k=1,3,2
5229                 ddersc(k)=ddersc(k)+(-Ax(2,j,iii)*Ax(k,j,iii)
5230      &            +gaussc(k,2,j,it))*expfac
5231               enddo
5232             endif
5233           enddo
5234
5235         enddo ! iii
5236
5237         dersc(1)=dersc(1)/cos(theti)**2
5238         ddersc(1)=ddersc(1)/cos(theti)**2
5239         ddersc(3)=ddersc(3)
5240
5241         escloci=-(dlog(escloc_i)-emin)
5242         do j=1,3
5243           dersc(j)=dersc(j)/escloc_i
5244         enddo
5245         if (mixed) then
5246           do j=1,3,2
5247             ddersc(j)=(ddersc(j)/escloc_i+dersc(2)*dersc(j))
5248           enddo
5249         endif
5250       return
5251       end
5252 C------------------------------------------------------------------------------
5253       subroutine enesc_bound(x,escloci,dersc,dersc12,mixed)
5254       implicit real*8 (a-h,o-z)
5255       include 'DIMENSIONS'
5256       include 'COMMON.GEO'
5257       include 'COMMON.LOCAL'
5258       include 'COMMON.IOUNITS'
5259       common /sccalc/ time11,time12,time112,theti,it,nlobit
5260       double precision x(3),z(3),Ax(3,maxlob),dersc(3)
5261       double precision contr(maxlob)
5262       logical mixed
5263
5264       escloc_i=0.0D0
5265
5266       do j=1,3
5267         dersc(j)=0.0D0
5268       enddo
5269
5270       do j=1,nlobit
5271         do k=1,2
5272           z(k)=x(k)-censc(k,j,it)
5273         enddo
5274         z(3)=dwapi
5275         do k=1,3
5276           Axk=0.0D0
5277           do l=1,3
5278             Axk=Axk+gaussc(l,k,j,it)*z(l)
5279           enddo
5280           Ax(k,j)=Axk
5281         enddo 
5282         expfac=0.0D0 
5283         do k=1,3
5284           expfac=expfac+Ax(k,j)*z(k)
5285         enddo
5286         contr(j)=expfac
5287       enddo ! j
5288
5289 C As in the case of ebend, we want to avoid underflows in exponentiation and
5290 C subsequent NaNs and INFs in energy calculation.
5291 C Find the largest exponent
5292       emin=contr(1)
5293       do j=1,nlobit
5294         if (emin.gt.contr(j)) emin=contr(j)
5295       enddo 
5296       emin=0.5D0*emin
5297  
5298 C Compute the contribution to SC energy and derivatives
5299
5300       dersc12=0.0d0
5301       do j=1,nlobit
5302         expfac=dexp(bsc(j,it)-0.5D0*contr(j)+emin)
5303         escloc_i=escloc_i+expfac
5304         do k=1,2
5305           dersc(k)=dersc(k)+Ax(k,j)*expfac
5306         enddo
5307         if (mixed) dersc12=dersc12+(-Ax(2,j)*Ax(1,j)
5308      &            +gaussc(1,2,j,it))*expfac
5309         dersc(3)=0.0d0
5310       enddo
5311
5312       dersc(1)=dersc(1)/cos(theti)**2
5313       dersc12=dersc12/cos(theti)**2
5314       escloci=-(dlog(escloc_i)-emin)
5315       do j=1,2
5316         dersc(j)=dersc(j)/escloc_i
5317       enddo
5318       if (mixed) dersc12=(dersc12/escloc_i+dersc(2)*dersc(1))
5319       return
5320       end
5321 #else
5322 c----------------------------------------------------------------------------------
5323       subroutine esc(escloc)
5324 C Calculate the local energy of a side chain and its derivatives in the
5325 C corresponding virtual-bond valence angles THETA and the spherical angles 
5326 C ALPHA and OMEGA derived from AM1 all-atom calculations.
5327 C added by Urszula Kozlowska. 07/11/2007
5328 C
5329       implicit real*8 (a-h,o-z)
5330       include 'DIMENSIONS'
5331       include 'COMMON.GEO'
5332       include 'COMMON.LOCAL'
5333       include 'COMMON.VAR'
5334       include 'COMMON.SCROT'
5335       include 'COMMON.INTERACT'
5336       include 'COMMON.DERIV'
5337       include 'COMMON.CHAIN'
5338       include 'COMMON.IOUNITS'
5339       include 'COMMON.NAMES'
5340       include 'COMMON.FFIELD'
5341       include 'COMMON.CONTROL'
5342       include 'COMMON.VECTORS'
5343       double precision x_prime(3),y_prime(3),z_prime(3)
5344      &    , sumene,dsc_i,dp2_i,x(65),
5345      &     xx,yy,zz,sumene1,sumene2,sumene3,sumene4,s1,s1_6,s2,s2_6,
5346      &    de_dxx,de_dyy,de_dzz,de_dt
5347       double precision s1_t,s1_6_t,s2_t,s2_6_t
5348       double precision 
5349      & dXX_Ci1(3),dYY_Ci1(3),dZZ_Ci1(3),dXX_Ci(3),
5350      & dYY_Ci(3),dZZ_Ci(3),dXX_XYZ(3),dYY_XYZ(3),dZZ_XYZ(3),
5351      & dt_dCi(3),dt_dCi1(3)
5352       common /sccalc/ time11,time12,time112,theti,it,nlobit
5353       delta=0.02d0*pi
5354       escloc=0.0D0
5355 c      write(iout,*) "ESC: loc_start",loc_start," loc_end",loc_end
5356       do i=loc_start,loc_end
5357         costtab(i+1) =dcos(theta(i+1))
5358         sinttab(i+1) =dsqrt(1-costtab(i+1)*costtab(i+1))
5359         cost2tab(i+1)=dsqrt(0.5d0*(1.0d0+costtab(i+1)))
5360         sint2tab(i+1)=dsqrt(0.5d0*(1.0d0-costtab(i+1)))
5361         cosfac2=0.5d0/(1.0d0+costtab(i+1))
5362         cosfac=dsqrt(cosfac2)
5363         sinfac2=0.5d0/(1.0d0-costtab(i+1))
5364         sinfac=dsqrt(sinfac2)
5365         it=itype(i)
5366         if (it.eq.10) goto 1
5367 c
5368 C  Compute the axes of tghe local cartesian coordinates system; store in
5369 c   x_prime, y_prime and z_prime 
5370 c
5371         do j=1,3
5372           x_prime(j) = 0.00
5373           y_prime(j) = 0.00
5374           z_prime(j) = 0.00
5375         enddo
5376 C        write(2,*) "dc_norm", dc_norm(1,i+nres),dc_norm(2,i+nres),
5377 C     &   dc_norm(3,i+nres)
5378         do j = 1,3
5379           x_prime(j) = (dc_norm(j,i) - dc_norm(j,i-1))*cosfac
5380           y_prime(j) = (dc_norm(j,i) + dc_norm(j,i-1))*sinfac
5381         enddo
5382         do j = 1,3
5383           z_prime(j) = -uz(j,i-1)
5384         enddo     
5385 c       write (2,*) "i",i
5386 c       write (2,*) "x_prime",(x_prime(j),j=1,3)
5387 c       write (2,*) "y_prime",(y_prime(j),j=1,3)
5388 c       write (2,*) "z_prime",(z_prime(j),j=1,3)
5389 c       write (2,*) "xx",scalar(x_prime(1),x_prime(1)),
5390 c      & " xy",scalar(x_prime(1),y_prime(1)),
5391 c      & " xz",scalar(x_prime(1),z_prime(1)),
5392 c      & " yy",scalar(y_prime(1),y_prime(1)),
5393 c      & " yz",scalar(y_prime(1),z_prime(1)),
5394 c      & " zz",scalar(z_prime(1),z_prime(1))
5395 c
5396 C Transform the unit vector of the ith side-chain centroid, dC_norm(*,i),
5397 C to local coordinate system. Store in xx, yy, zz.
5398 c
5399         xx=0.0d0
5400         yy=0.0d0
5401         zz=0.0d0
5402         do j = 1,3
5403           xx = xx + x_prime(j)*dc_norm(j,i+nres)
5404           yy = yy + y_prime(j)*dc_norm(j,i+nres)
5405           zz = zz + z_prime(j)*dc_norm(j,i+nres)
5406         enddo
5407
5408         xxtab(i)=xx
5409         yytab(i)=yy
5410         zztab(i)=zz
5411 C
5412 C Compute the energy of the ith side cbain
5413 C
5414 c        write (2,*) "xx",xx," yy",yy," zz",zz
5415         it=itype(i)
5416         do j = 1,65
5417           x(j) = sc_parmin(j,it) 
5418         enddo
5419 #ifdef CHECK_COORD
5420 Cc diagnostics - remove later
5421         xx1 = dcos(alph(2))
5422         yy1 = dsin(alph(2))*dcos(omeg(2))
5423         zz1 = -dsin(alph(2))*dsin(omeg(2))
5424         write(2,'(3f8.1,3f9.3,1x,3f9.3)') 
5425      &    alph(2)*rad2deg,omeg(2)*rad2deg,theta(3)*rad2deg,xx,yy,zz,
5426      &    xx1,yy1,zz1
5427 C,"  --- ", xx_w,yy_w,zz_w
5428 c end diagnostics
5429 #endif
5430         sumene1= x(1)+  x(2)*xx+  x(3)*yy+  x(4)*zz+  x(5)*xx**2
5431      &   + x(6)*yy**2+  x(7)*zz**2+  x(8)*xx*zz+  x(9)*xx*yy
5432      &   + x(10)*yy*zz
5433         sumene2=  x(11) + x(12)*xx + x(13)*yy + x(14)*zz + x(15)*xx**2
5434      & + x(16)*yy**2 + x(17)*zz**2 + x(18)*xx*zz + x(19)*xx*yy
5435      & + x(20)*yy*zz
5436         sumene3=  x(21) +x(22)*xx +x(23)*yy +x(24)*zz +x(25)*xx**2
5437      &  +x(26)*yy**2 +x(27)*zz**2 +x(28)*xx*zz +x(29)*xx*yy
5438      &  +x(30)*yy*zz +x(31)*xx**3 +x(32)*yy**3 +x(33)*zz**3
5439      &  +x(34)*(xx**2)*yy +x(35)*(xx**2)*zz +x(36)*(yy**2)*xx
5440      &  +x(37)*(yy**2)*zz +x(38)*(zz**2)*xx +x(39)*(zz**2)*yy
5441      &  +x(40)*xx*yy*zz
5442         sumene4= x(41) +x(42)*xx +x(43)*yy +x(44)*zz +x(45)*xx**2
5443      &  +x(46)*yy**2 +x(47)*zz**2 +x(48)*xx*zz +x(49)*xx*yy
5444      &  +x(50)*yy*zz +x(51)*xx**3 +x(52)*yy**3 +x(53)*zz**3
5445      &  +x(54)*(xx**2)*yy +x(55)*(xx**2)*zz +x(56)*(yy**2)*xx
5446      &  +x(57)*(yy**2)*zz +x(58)*(zz**2)*xx +x(59)*(zz**2)*yy
5447      &  +x(60)*xx*yy*zz
5448         dsc_i   = 0.743d0+x(61)
5449         dp2_i   = 1.9d0+x(62)
5450         dscp1=dsqrt(dsc_i**2+dp2_i**2-2*dsc_i*dp2_i
5451      &          *(xx*cost2tab(i+1)+yy*sint2tab(i+1)))
5452         dscp2=dsqrt(dsc_i**2+dp2_i**2-2*dsc_i*dp2_i
5453      &          *(xx*cost2tab(i+1)-yy*sint2tab(i+1)))
5454         s1=(1+x(63))/(0.1d0 + dscp1)
5455         s1_6=(1+x(64))/(0.1d0 + dscp1**6)
5456         s2=(1+x(65))/(0.1d0 + dscp2)
5457         s2_6=(1+x(65))/(0.1d0 + dscp2**6)
5458         sumene = ( sumene3*sint2tab(i+1) + sumene1)*(s1+s1_6)
5459      & + (sumene4*cost2tab(i+1) +sumene2)*(s2+s2_6)
5460 c        write(2,'(i2," sumene",7f9.3)') i,sumene1,sumene2,sumene3,
5461 c     &   sumene4,
5462 c     &   dscp1,dscp2,sumene
5463 c        sumene = enesc(x,xx,yy,zz,cost2tab(i+1),sint2tab(i+1))
5464         escloc = escloc + sumene
5465 c        write (2,*) "i",i," escloc",sumene,escloc
5466 #ifdef DEBUG
5467 C
5468 C This section to check the numerical derivatives of the energy of ith side
5469 C chain in xx, yy, zz, and theta. Use the -DDEBUG compiler option or insert
5470 C #define DEBUG in the code to turn it on.
5471 C
5472         write (2,*) "sumene               =",sumene
5473         aincr=1.0d-7
5474         xxsave=xx
5475         xx=xx+aincr
5476         write (2,*) xx,yy,zz
5477         sumenep = enesc(x,xx,yy,zz,cost2tab(i+1),sint2tab(i+1))
5478         de_dxx_num=(sumenep-sumene)/aincr
5479         xx=xxsave
5480         write (2,*) "xx+ sumene from enesc=",sumenep
5481         yysave=yy
5482         yy=yy+aincr
5483         write (2,*) xx,yy,zz
5484         sumenep = enesc(x,xx,yy,zz,cost2tab(i+1),sint2tab(i+1))
5485         de_dyy_num=(sumenep-sumene)/aincr
5486         yy=yysave
5487         write (2,*) "yy+ sumene from enesc=",sumenep
5488         zzsave=zz
5489         zz=zz+aincr
5490         write (2,*) xx,yy,zz
5491         sumenep = enesc(x,xx,yy,zz,cost2tab(i+1),sint2tab(i+1))
5492         de_dzz_num=(sumenep-sumene)/aincr
5493         zz=zzsave
5494         write (2,*) "zz+ sumene from enesc=",sumenep
5495         costsave=cost2tab(i+1)
5496         sintsave=sint2tab(i+1)
5497         cost2tab(i+1)=dcos(0.5d0*(theta(i+1)+aincr))
5498         sint2tab(i+1)=dsin(0.5d0*(theta(i+1)+aincr))
5499         sumenep = enesc(x,xx,yy,zz,cost2tab(i+1),sint2tab(i+1))
5500         de_dt_num=(sumenep-sumene)/aincr
5501         write (2,*) " t+ sumene from enesc=",sumenep
5502         cost2tab(i+1)=costsave
5503         sint2tab(i+1)=sintsave
5504 C End of diagnostics section.
5505 #endif
5506 C        
5507 C Compute the gradient of esc
5508 C
5509         pom_s1=(1.0d0+x(63))/(0.1d0 + dscp1)**2
5510         pom_s16=6*(1.0d0+x(64))/(0.1d0 + dscp1**6)**2
5511         pom_s2=(1.0d0+x(65))/(0.1d0 + dscp2)**2
5512         pom_s26=6*(1.0d0+x(65))/(0.1d0 + dscp2**6)**2
5513         pom_dx=dsc_i*dp2_i*cost2tab(i+1)
5514         pom_dy=dsc_i*dp2_i*sint2tab(i+1)
5515         pom_dt1=-0.5d0*dsc_i*dp2_i*(xx*sint2tab(i+1)-yy*cost2tab(i+1))
5516         pom_dt2=-0.5d0*dsc_i*dp2_i*(xx*sint2tab(i+1)+yy*cost2tab(i+1))
5517         pom1=(sumene3*sint2tab(i+1)+sumene1)
5518      &     *(pom_s1/dscp1+pom_s16*dscp1**4)
5519         pom2=(sumene4*cost2tab(i+1)+sumene2)
5520      &     *(pom_s2/dscp2+pom_s26*dscp2**4)
5521         sumene1x=x(2)+2*x(5)*xx+x(8)*zz+ x(9)*yy
5522         sumene3x=x(22)+2*x(25)*xx+x(28)*zz+x(29)*yy+3*x(31)*xx**2
5523      &  +2*x(34)*xx*yy +2*x(35)*xx*zz +x(36)*(yy**2) +x(38)*(zz**2)
5524      &  +x(40)*yy*zz
5525         sumene2x=x(12)+2*x(15)*xx+x(18)*zz+ x(19)*yy
5526         sumene4x=x(42)+2*x(45)*xx +x(48)*zz +x(49)*yy +3*x(51)*xx**2
5527      &  +2*x(54)*xx*yy+2*x(55)*xx*zz+x(56)*(yy**2)+x(58)*(zz**2)
5528      &  +x(60)*yy*zz
5529         de_dxx =(sumene1x+sumene3x*sint2tab(i+1))*(s1+s1_6)
5530      &        +(sumene2x+sumene4x*cost2tab(i+1))*(s2+s2_6)
5531      &        +(pom1+pom2)*pom_dx
5532 #ifdef DEBUG
5533         write(2,*), "de_dxx = ", de_dxx,de_dxx_num
5534 #endif
5535 C
5536         sumene1y=x(3) + 2*x(6)*yy + x(9)*xx + x(10)*zz
5537         sumene3y=x(23) +2*x(26)*yy +x(29)*xx +x(30)*zz +3*x(32)*yy**2
5538      &  +x(34)*(xx**2) +2*x(36)*yy*xx +2*x(37)*yy*zz +x(39)*(zz**2)
5539      &  +x(40)*xx*zz
5540         sumene2y=x(13) + 2*x(16)*yy + x(19)*xx + x(20)*zz
5541         sumene4y=x(43)+2*x(46)*yy+x(49)*xx +x(50)*zz
5542      &  +3*x(52)*yy**2+x(54)*xx**2+2*x(56)*yy*xx +2*x(57)*yy*zz
5543      &  +x(59)*zz**2 +x(60)*xx*zz
5544         de_dyy =(sumene1y+sumene3y*sint2tab(i+1))*(s1+s1_6)
5545      &        +(sumene2y+sumene4y*cost2tab(i+1))*(s2+s2_6)
5546      &        +(pom1-pom2)*pom_dy
5547 #ifdef DEBUG
5548         write(2,*), "de_dyy = ", de_dyy,de_dyy_num
5549 #endif
5550 C
5551         de_dzz =(x(24) +2*x(27)*zz +x(28)*xx +x(30)*yy
5552      &  +3*x(33)*zz**2 +x(35)*xx**2 +x(37)*yy**2 +2*x(38)*zz*xx 
5553      &  +2*x(39)*zz*yy +x(40)*xx*yy)*sint2tab(i+1)*(s1+s1_6) 
5554      &  +(x(4) + 2*x(7)*zz+  x(8)*xx + x(10)*yy)*(s1+s1_6) 
5555      &  +(x(44)+2*x(47)*zz +x(48)*xx   +x(50)*yy  +3*x(53)*zz**2   
5556      &  +x(55)*xx**2 +x(57)*(yy**2)+2*x(58)*zz*xx +2*x(59)*zz*yy  
5557      &  +x(60)*xx*yy)*cost2tab(i+1)*(s2+s2_6)
5558      &  + ( x(14) + 2*x(17)*zz+  x(18)*xx + x(20)*yy)*(s2+s2_6)
5559 #ifdef DEBUG
5560         write(2,*), "de_dzz = ", de_dzz,de_dzz_num
5561 #endif
5562 C
5563         de_dt =  0.5d0*sumene3*cost2tab(i+1)*(s1+s1_6) 
5564      &  -0.5d0*sumene4*sint2tab(i+1)*(s2+s2_6)
5565      &  +pom1*pom_dt1+pom2*pom_dt2
5566 #ifdef DEBUG
5567         write(2,*), "de_dt = ", de_dt,de_dt_num
5568 #endif
5569
5570 C
5571        cossc=scalar(dc_norm(1,i),dc_norm(1,i+nres))
5572        cossc1=scalar(dc_norm(1,i-1),dc_norm(1,i+nres))
5573        cosfac2xx=cosfac2*xx
5574        sinfac2yy=sinfac2*yy
5575        do k = 1,3
5576          dt_dCi(k) = -(dc_norm(k,i-1)+costtab(i+1)*dc_norm(k,i))*
5577      &      vbld_inv(i+1)
5578          dt_dCi1(k)= -(dc_norm(k,i)+costtab(i+1)*dc_norm(k,i-1))*
5579      &      vbld_inv(i)
5580          pom=(dC_norm(k,i+nres)-cossc*dC_norm(k,i))*vbld_inv(i+1)
5581          pom1=(dC_norm(k,i+nres)-cossc1*dC_norm(k,i-1))*vbld_inv(i)
5582 c         write (iout,*) "i",i," k",k," pom",pom," pom1",pom1,
5583 c     &    " dt_dCi",dt_dCi(k)," dt_dCi1",dt_dCi1(k)
5584 c         write (iout,*) "dC_norm",(dC_norm(j,i),j=1,3),
5585 c     &   (dC_norm(j,i-1),j=1,3)," vbld_inv",vbld_inv(i+1),vbld_inv(i)
5586          dXX_Ci(k)=pom*cosfac-dt_dCi(k)*cosfac2xx
5587          dXX_Ci1(k)=-pom1*cosfac-dt_dCi1(k)*cosfac2xx
5588          dYY_Ci(k)=pom*sinfac+dt_dCi(k)*sinfac2yy
5589          dYY_Ci1(k)=pom1*sinfac+dt_dCi1(k)*sinfac2yy
5590          dZZ_Ci1(k)=0.0d0
5591          dZZ_Ci(k)=0.0d0
5592          do j=1,3
5593            dZZ_Ci(k)=dZZ_Ci(k)-uzgrad(j,k,2,i-1)*dC_norm(j,i+nres)
5594            dZZ_Ci1(k)=dZZ_Ci1(k)-uzgrad(j,k,1,i-1)*dC_norm(j,i+nres)
5595          enddo
5596           
5597          dXX_XYZ(k)=vbld_inv(i+nres)*(x_prime(k)-xx*dC_norm(k,i+nres))
5598          dYY_XYZ(k)=vbld_inv(i+nres)*(y_prime(k)-yy*dC_norm(k,i+nres))
5599          dZZ_XYZ(k)=vbld_inv(i+nres)*(z_prime(k)-zz*dC_norm(k,i+nres))
5600 c
5601          dt_dCi(k) = -dt_dCi(k)/sinttab(i+1)
5602          dt_dCi1(k)= -dt_dCi1(k)/sinttab(i+1)
5603        enddo
5604
5605        do k=1,3
5606          dXX_Ctab(k,i)=dXX_Ci(k)
5607          dXX_C1tab(k,i)=dXX_Ci1(k)
5608          dYY_Ctab(k,i)=dYY_Ci(k)
5609          dYY_C1tab(k,i)=dYY_Ci1(k)
5610          dZZ_Ctab(k,i)=dZZ_Ci(k)
5611          dZZ_C1tab(k,i)=dZZ_Ci1(k)
5612          dXX_XYZtab(k,i)=dXX_XYZ(k)
5613          dYY_XYZtab(k,i)=dYY_XYZ(k)
5614          dZZ_XYZtab(k,i)=dZZ_XYZ(k)
5615        enddo
5616
5617        do k = 1,3
5618 c         write (iout,*) "k",k," dxx_ci1",dxx_ci1(k)," dyy_ci1",
5619 c     &    dyy_ci1(k)," dzz_ci1",dzz_ci1(k)
5620 c         write (iout,*) "k",k," dxx_ci",dxx_ci(k)," dyy_ci",
5621 c     &    dyy_ci(k)," dzz_ci",dzz_ci(k)
5622 c         write (iout,*) "k",k," dt_dci",dt_dci(k)," dt_dci",
5623 c     &    dt_dci(k)
5624 c         write (iout,*) "k",k," dxx_XYZ",dxx_XYZ(k)," dyy_XYZ",
5625 c     &    dyy_XYZ(k)," dzz_XYZ",dzz_XYZ(k) 
5626          gscloc(k,i-1)=gscloc(k,i-1)+de_dxx*dxx_ci1(k)
5627      &    +de_dyy*dyy_ci1(k)+de_dzz*dzz_ci1(k)+de_dt*dt_dCi1(k)
5628          gscloc(k,i)=gscloc(k,i)+de_dxx*dxx_Ci(k)
5629      &    +de_dyy*dyy_Ci(k)+de_dzz*dzz_Ci(k)+de_dt*dt_dCi(k)
5630          gsclocx(k,i)=                 de_dxx*dxx_XYZ(k)
5631      &    +de_dyy*dyy_XYZ(k)+de_dzz*dzz_XYZ(k)
5632        enddo
5633 c       write(iout,*) "ENERGY GRAD = ", (gscloc(k,i-1),k=1,3),
5634 c     &  (gscloc(k,i),k=1,3),(gsclocx(k,i),k=1,3)  
5635
5636 C to check gradient call subroutine check_grad
5637
5638     1 continue
5639       enddo
5640       return
5641       end
5642 c------------------------------------------------------------------------------
5643       double precision function enesc(x,xx,yy,zz,cost2,sint2)
5644       implicit none
5645       double precision x(65),xx,yy,zz,cost2,sint2,sumene1,sumene2,
5646      & sumene3,sumene4,sumene,dsc_i,dp2_i,dscp1,dscp2,s1,s1_6,s2,s2_6
5647       sumene1= x(1)+  x(2)*xx+  x(3)*yy+  x(4)*zz+  x(5)*xx**2
5648      &   + x(6)*yy**2+  x(7)*zz**2+  x(8)*xx*zz+  x(9)*xx*yy
5649      &   + x(10)*yy*zz
5650       sumene2=  x(11) + x(12)*xx + x(13)*yy + x(14)*zz + x(15)*xx**2
5651      & + x(16)*yy**2 + x(17)*zz**2 + x(18)*xx*zz + x(19)*xx*yy
5652      & + x(20)*yy*zz
5653       sumene3=  x(21) +x(22)*xx +x(23)*yy +x(24)*zz +x(25)*xx**2
5654      &  +x(26)*yy**2 +x(27)*zz**2 +x(28)*xx*zz +x(29)*xx*yy
5655      &  +x(30)*yy*zz +x(31)*xx**3 +x(32)*yy**3 +x(33)*zz**3
5656      &  +x(34)*(xx**2)*yy +x(35)*(xx**2)*zz +x(36)*(yy**2)*xx
5657      &  +x(37)*(yy**2)*zz +x(38)*(zz**2)*xx +x(39)*(zz**2)*yy
5658      &  +x(40)*xx*yy*zz
5659       sumene4= x(41) +x(42)*xx +x(43)*yy +x(44)*zz +x(45)*xx**2
5660      &  +x(46)*yy**2 +x(47)*zz**2 +x(48)*xx*zz +x(49)*xx*yy
5661      &  +x(50)*yy*zz +x(51)*xx**3 +x(52)*yy**3 +x(53)*zz**3
5662      &  +x(54)*(xx**2)*yy +x(55)*(xx**2)*zz +x(56)*(yy**2)*xx
5663      &  +x(57)*(yy**2)*zz +x(58)*(zz**2)*xx +x(59)*(zz**2)*yy
5664      &  +x(60)*xx*yy*zz
5665       dsc_i   = 0.743d0+x(61)
5666       dp2_i   = 1.9d0+x(62)
5667       dscp1=dsqrt(dsc_i**2+dp2_i**2-2*dsc_i*dp2_i
5668      &          *(xx*cost2+yy*sint2))
5669       dscp2=dsqrt(dsc_i**2+dp2_i**2-2*dsc_i*dp2_i
5670      &          *(xx*cost2-yy*sint2))
5671       s1=(1+x(63))/(0.1d0 + dscp1)
5672       s1_6=(1+x(64))/(0.1d0 + dscp1**6)
5673       s2=(1+x(65))/(0.1d0 + dscp2)
5674       s2_6=(1+x(65))/(0.1d0 + dscp2**6)
5675       sumene = ( sumene3*sint2 + sumene1)*(s1+s1_6)
5676      & + (sumene4*cost2 +sumene2)*(s2+s2_6)
5677       enesc=sumene
5678       return
5679       end
5680 #endif
5681 c------------------------------------------------------------------------------
5682       subroutine gcont(rij,r0ij,eps0ij,delta,fcont,fprimcont)
5683 C
5684 C This procedure calculates two-body contact function g(rij) and its derivative:
5685 C
5686 C           eps0ij                                     !       x < -1
5687 C g(rij) =  esp0ij*(-0.9375*x+0.625*x**3-0.1875*x**5)  ! -1 =< x =< 1
5688 C            0                                         !       x > 1
5689 C
5690 C where x=(rij-r0ij)/delta
5691 C
5692 C rij - interbody distance, r0ij - contact distance, eps0ij - contact energy
5693 C
5694       implicit none
5695       double precision rij,r0ij,eps0ij,fcont,fprimcont
5696       double precision x,x2,x4,delta
5697 c     delta=0.02D0*r0ij
5698 c      delta=0.2D0*r0ij
5699       x=(rij-r0ij)/delta
5700       if (x.lt.-1.0D0) then
5701         fcont=eps0ij
5702         fprimcont=0.0D0
5703       else if (x.le.1.0D0) then  
5704         x2=x*x
5705         x4=x2*x2
5706         fcont=eps0ij*(x*(-0.9375D0+0.6250D0*x2-0.1875D0*x4)+0.5D0)
5707         fprimcont=eps0ij * (-0.9375D0+1.8750D0*x2-0.9375D0*x4)/delta
5708       else
5709         fcont=0.0D0
5710         fprimcont=0.0D0
5711       endif
5712       return
5713       end
5714 c------------------------------------------------------------------------------
5715       subroutine splinthet(theti,delta,ss,ssder)
5716       implicit real*8 (a-h,o-z)
5717       include 'DIMENSIONS'
5718       include 'COMMON.VAR'
5719       include 'COMMON.GEO'
5720       thetup=pi-delta
5721       thetlow=delta
5722       if (theti.gt.pipol) then
5723         call gcont(theti,thetup,1.0d0,delta,ss,ssder)
5724       else
5725         call gcont(-theti,-thetlow,1.0d0,delta,ss,ssder)
5726         ssder=-ssder
5727       endif
5728       return
5729       end
5730 c------------------------------------------------------------------------------
5731       subroutine spline1(x,x0,delta,f0,f1,fprim0,f,fprim)
5732       implicit none
5733       double precision x,x0,delta,f0,f1,fprim0,f,fprim
5734       double precision ksi,ksi2,ksi3,a1,a2,a3
5735       a1=fprim0*delta/(f1-f0)
5736       a2=3.0d0-2.0d0*a1
5737       a3=a1-2.0d0
5738       ksi=(x-x0)/delta
5739       ksi2=ksi*ksi
5740       ksi3=ksi2*ksi  
5741       f=f0+(f1-f0)*ksi*(a1+ksi*(a2+a3*ksi))
5742       fprim=(f1-f0)/delta*(a1+ksi*(2*a2+3*ksi*a3))
5743       return
5744       end
5745 c------------------------------------------------------------------------------
5746       subroutine spline2(x,x0,delta,f0x,f1x,fprim0x,fx)
5747       implicit none
5748       double precision x,x0,delta,f0x,f1x,fprim0x,fx
5749       double precision ksi,ksi2,ksi3,a1,a2,a3
5750       ksi=(x-x0)/delta  
5751       ksi2=ksi*ksi
5752       ksi3=ksi2*ksi
5753       a1=fprim0x*delta
5754       a2=3*(f1x-f0x)-2*fprim0x*delta
5755       a3=fprim0x*delta-2*(f1x-f0x)
5756       fx=f0x+a1*ksi+a2*ksi2+a3*ksi3
5757       return
5758       end
5759 C-----------------------------------------------------------------------------
5760 #ifdef CRYST_TOR
5761 C-----------------------------------------------------------------------------
5762       subroutine etor(etors,edihcnstr)
5763       implicit real*8 (a-h,o-z)
5764       include 'DIMENSIONS'
5765       include 'COMMON.VAR'
5766       include 'COMMON.GEO'
5767       include 'COMMON.LOCAL'
5768       include 'COMMON.TORSION'
5769       include 'COMMON.INTERACT'
5770       include 'COMMON.DERIV'
5771       include 'COMMON.CHAIN'
5772       include 'COMMON.NAMES'
5773       include 'COMMON.IOUNITS'
5774       include 'COMMON.FFIELD'
5775       include 'COMMON.TORCNSTR'
5776       include 'COMMON.CONTROL'
5777       logical lprn
5778 C Set lprn=.true. for debugging
5779       lprn=.false.
5780 c      lprn=.true.
5781       etors=0.0D0
5782       do i=iphi_start,iphi_end
5783       etors_ii=0.0D0
5784         itori=itortyp(itype(i-2))
5785         itori1=itortyp(itype(i-1))
5786         phii=phi(i)
5787         gloci=0.0D0
5788 C Proline-Proline pair is a special case...
5789         if (itori.eq.3 .and. itori1.eq.3) then
5790           if (phii.gt.-dwapi3) then
5791             cosphi=dcos(3*phii)
5792             fac=1.0D0/(1.0D0-cosphi)
5793             etorsi=v1(1,3,3)*fac
5794             etorsi=etorsi+etorsi
5795             etors=etors+etorsi-v1(1,3,3)
5796             if (energy_dec) etors_ii=etors_ii+etorsi-v1(1,3,3)      
5797             gloci=gloci-3*fac*etorsi*dsin(3*phii)
5798           endif
5799           do j=1,3
5800             v1ij=v1(j+1,itori,itori1)
5801             v2ij=v2(j+1,itori,itori1)
5802             cosphi=dcos(j*phii)
5803             sinphi=dsin(j*phii)
5804             etors=etors+v1ij*cosphi+v2ij*sinphi+dabs(v1ij)+dabs(v2ij)
5805             if (energy_dec) etors_ii=etors_ii+
5806      &                              v2ij*sinphi+dabs(v1ij)+dabs(v2ij)
5807             gloci=gloci+j*(v2ij*cosphi-v1ij*sinphi)
5808           enddo
5809         else 
5810           do j=1,nterm_old
5811             v1ij=v1(j,itori,itori1)
5812             v2ij=v2(j,itori,itori1)
5813             cosphi=dcos(j*phii)
5814             sinphi=dsin(j*phii)
5815             etors=etors+v1ij*cosphi+v2ij*sinphi+dabs(v1ij)+dabs(v2ij)
5816             if (energy_dec) etors_ii=etors_ii+
5817      &                  v1ij*cosphi+v2ij*sinphi+dabs(v1ij)+dabs(v2ij)
5818             gloci=gloci+j*(v2ij*cosphi-v1ij*sinphi)
5819           enddo
5820         endif
5821         if (energy_dec) write (iout,'(a6,i5,0pf7.3)')
5822      &        'etor',i,etors_ii
5823         if (lprn)
5824      &  write (iout,'(2(a3,2x,i3,2x),2i3,6f8.3/26x,6f8.3/)')
5825      &  restyp(itype(i-2)),i-2,restyp(itype(i-1)),i-1,itori,itori1,
5826      &  (v1(j,itori,itori1),j=1,6),(v2(j,itori,itori1),j=1,6)
5827         gloc(i-3,icg)=gloc(i-3,icg)+wtor*gloci
5828         write (iout,*) 'i=',i,' gloc=',gloc(i-3,icg)
5829       enddo
5830 ! 6/20/98 - dihedral angle constraints
5831       edihcnstr=0.0d0
5832       do i=1,ndih_constr
5833         itori=idih_constr(i)
5834         phii=phi(itori)
5835         difi=phii-phi0(i)
5836         if (difi.gt.drange(i)) then
5837           difi=difi-drange(i)
5838           edihcnstr=edihcnstr+0.25d0*ftors*difi**4
5839           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors*difi**3
5840         else if (difi.lt.-drange(i)) then
5841           difi=difi+drange(i)
5842           edihcnstr=edihcnstr+0.25d0*ftors*difi**4
5843           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors*difi**3
5844         endif
5845 !        write (iout,'(2i5,2f8.3,2e14.5)') i,itori,rad2deg*phii,
5846 !     &    rad2deg*difi,0.25d0*ftors*difi**4,gloc(itori-3,icg)
5847       enddo
5848 !      write (iout,*) 'edihcnstr',edihcnstr
5849       return
5850       end
5851 c------------------------------------------------------------------------------
5852 c LICZENIE WIEZOW Z ROWNANIA ENERGII MODELLERA
5853       subroutine e_modeller(ehomology_constr)
5854       ehomology_constr=0.0
5855       write (iout,*) "!!!!!UWAGA, JESTEM W DZIWNEJ PETLI, TEST!!!!!"
5856       return
5857       end
5858 C !!!!!!!! NIE CZYTANE !!!!!!!!!!!
5859
5860 c------------------------------------------------------------------------------
5861       subroutine etor_d(etors_d)
5862       etors_d=0.0d0
5863       return
5864       end
5865 c----------------------------------------------------------------------------
5866 #else
5867       subroutine etor(etors,edihcnstr)
5868       implicit real*8 (a-h,o-z)
5869       include 'DIMENSIONS'
5870       include 'COMMON.VAR'
5871       include 'COMMON.GEO'
5872       include 'COMMON.LOCAL'
5873       include 'COMMON.TORSION'
5874       include 'COMMON.INTERACT'
5875       include 'COMMON.DERIV'
5876       include 'COMMON.CHAIN'
5877       include 'COMMON.NAMES'
5878       include 'COMMON.IOUNITS'
5879       include 'COMMON.FFIELD'
5880       include 'COMMON.TORCNSTR'
5881       include 'COMMON.CONTROL'
5882       logical lprn
5883 C Set lprn=.true. for debugging
5884       lprn=.false.
5885 c     lprn=.true.
5886       etors=0.0D0
5887       do i=iphi_start,iphi_end
5888       etors_ii=0.0D0
5889         itori=itortyp(itype(i-2))
5890         itori1=itortyp(itype(i-1))
5891         phii=phi(i)
5892         gloci=0.0D0
5893 C Regular cosine and sine terms
5894         do j=1,nterm(itori,itori1)
5895           v1ij=v1(j,itori,itori1)
5896           v2ij=v2(j,itori,itori1)
5897           cosphi=dcos(j*phii)
5898           sinphi=dsin(j*phii)
5899           etors=etors+v1ij*cosphi+v2ij*sinphi
5900           if (energy_dec) etors_ii=etors_ii+
5901      &                v1ij*cosphi+v2ij*sinphi
5902           gloci=gloci+j*(v2ij*cosphi-v1ij*sinphi)
5903         enddo
5904 C Lorentz terms
5905 C                         v1
5906 C  E = SUM ----------------------------------- - v1
5907 C          [v2 cos(phi/2)+v3 sin(phi/2)]^2 + 1
5908 C
5909         cosphi=dcos(0.5d0*phii)
5910         sinphi=dsin(0.5d0*phii)
5911         do j=1,nlor(itori,itori1)
5912           vl1ij=vlor1(j,itori,itori1)
5913           vl2ij=vlor2(j,itori,itori1)
5914           vl3ij=vlor3(j,itori,itori1)
5915           pom=vl2ij*cosphi+vl3ij*sinphi
5916           pom1=1.0d0/(pom*pom+1.0d0)
5917           etors=etors+vl1ij*pom1
5918           if (energy_dec) etors_ii=etors_ii+
5919      &                vl1ij*pom1
5920           pom=-pom*pom1*pom1
5921           gloci=gloci+vl1ij*(vl3ij*cosphi-vl2ij*sinphi)*pom
5922         enddo
5923 C Subtract the constant term
5924         etors=etors-v0(itori,itori1)
5925           if (energy_dec) write (iout,'(a6,i5,0pf7.3)')
5926      &         'etor',i,etors_ii-v0(itori,itori1)
5927         if (lprn)
5928      &  write (iout,'(2(a3,2x,i3,2x),2i3,6f8.3/26x,6f8.3/)')
5929      &  restyp(itype(i-2)),i-2,restyp(itype(i-1)),i-1,itori,itori1,
5930      &  (v1(j,itori,itori1),j=1,6),(v2(j,itori,itori1),j=1,6)
5931         gloc(i-3,icg)=gloc(i-3,icg)+wtor*gloci
5932 c       write (iout,*) 'i=',i,' gloc=',gloc(i-3,icg)
5933       enddo
5934 ! 6/20/98 - dihedral angle constraints
5935       edihcnstr=0.0d0
5936 c      do i=1,ndih_constr
5937       do i=idihconstr_start,idihconstr_end
5938         itori=idih_constr(i)
5939         phii=phi(itori)
5940         difi=pinorm(phii-phi0(i))
5941         if (difi.gt.drange(i)) then
5942           difi=difi-drange(i)
5943           edihcnstr=edihcnstr+0.25d0*ftors*difi**4
5944           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors*difi**3
5945         else if (difi.lt.-drange(i)) then
5946           difi=difi+drange(i)
5947           edihcnstr=edihcnstr+0.25d0*ftors*difi**4
5948           gloc(itori-3,icg)=gloc(itori-3,icg)+ftors*difi**3
5949         else
5950           difi=0.0
5951         endif
5952 c        write (iout,*) "gloci", gloc(i-3,icg)
5953 cd        write (iout,'(2i5,4f8.3,2e14.5)') i,itori,rad2deg*phii,
5954 cd     &    rad2deg*phi0(i),  rad2deg*drange(i),
5955 cd     &    rad2deg*difi,0.25d0*ftors*difi**4,gloc(itori-3,icg)
5956       enddo
5957 cd       write (iout,*) 'edihcnstr',edihcnstr
5958       return
5959       end
5960 c----------------------------------------------------------------------------
5961 c MODELLER restraint function
5962       subroutine e_modeller(ehomology_constr)
5963       implicit real*8 (a-h,o-z)
5964       include 'DIMENSIONS'
5965
5966       integer nnn, i, j, k, ki, irec, l
5967       integer katy, odleglosci, test7
5968       real*8 odleg, odleg2, odleg3, kat, kat2, kat3, gdih(max_template)
5969       real*8 Eval,Erot
5970       real*8 distance(max_template),distancek(max_template),
5971      &    min_odl,godl(max_template),dih_diff(max_template)
5972
5973 c
5974 c     FP - 30/10/2014 Temporary specifications for homology restraints
5975 c
5976       double precision utheta_i,gutheta_i,sum_gtheta,sum_sgtheta,
5977      &                 sgtheta      
5978       double precision, dimension (maxres) :: guscdiff,usc_diff
5979       double precision, dimension (max_template) ::  
5980      &           gtheta,dscdiff,uscdiffk,guscdiff2,guscdiff3,
5981      &           theta_diff
5982 c
5983
5984       include 'COMMON.SBRIDGE'
5985       include 'COMMON.CHAIN'
5986       include 'COMMON.GEO'
5987       include 'COMMON.DERIV'
5988       include 'COMMON.LOCAL'
5989       include 'COMMON.INTERACT'
5990       include 'COMMON.VAR'
5991       include 'COMMON.IOUNITS'
5992       include 'COMMON.MD'
5993       include 'COMMON.CONTROL'
5994 c
5995 c     From subroutine Econstr_back
5996 c
5997       include 'COMMON.NAMES'
5998       include 'COMMON.TIME1'
5999 c
6000
6001
6002       do i=1,19
6003         distancek(i)=9999999.9
6004       enddo
6005
6006
6007       odleg=0.0d0
6008
6009 c Pseudo-energy and gradient from homology restraints (MODELLER-like
6010 c function)
6011 C AL 5/2/14 - Introduce list of restraints
6012 c     write(iout,*) "waga_theta",waga_theta,"waga_d",waga_d
6013 #ifdef DEBUG
6014       write(iout,*) "------- dist restrs start -------"
6015 #endif
6016       do ii = link_start_homo,link_end_homo
6017          i = ires_homo(ii)
6018          j = jres_homo(ii)
6019          dij=dist(i,j)
6020 c        write (iout,*) "dij(",i,j,") =",dij
6021          do k=1,constr_homology
6022            distance(k)=odl(k,ii)-dij
6023 c          write (iout,*) "distance(",k,") =",distance(k)
6024            distancek(k)=0.5d0*distance(k)**2*sigma_odl(k,ii) ! waga_dist rmvd from Gaussian argument
6025 c          write (iout,*) "sigma_odl(",k,ii,") =",sigma_odl(k,ii)
6026 c          write (iout,*) "distancek(",k,") =",distancek(k)
6027 c          distancek(k)=0.5d0*waga_dist*distance(k)**2*sigma_odl(k,ii)
6028          enddo
6029          
6030          min_odl=minval(distancek)
6031 c        write (iout,* )"min_odl",min_odl
6032 #ifdef DEBUG
6033          write (iout,*) "ij dij",i,j,dij
6034          write (iout,*) "distance",(distance(k),k=1,constr_homology)
6035          write (iout,*) "distancek",(distancek(k),k=1,constr_homology)
6036          write (iout,* )"min_odl",min_odl
6037 #endif
6038          odleg2=0.0d0
6039          do k=1,constr_homology
6040 c Nie wiem po co to liczycie jeszcze raz!
6041 c            odleg3=-waga_dist(iset)*((distance(i,j,k)**2)/ 
6042 c     &              (2*(sigma_odl(i,j,k))**2))
6043             godl(k)=dexp(-distancek(k)+min_odl)
6044             odleg2=odleg2+godl(k)
6045
6046 ccc       write(iout,779) i,j,k, "odleg2=",odleg2, "odleg3=", odleg3,
6047 ccc     & "dEXP(odleg3)=", dEXP(odleg3),"distance(i,j,k)^2=",
6048 ccc     & distance(i,j,k)**2, "dist(i+1,j+1)=", dist(i+1,j+1),
6049 ccc     & "sigma_odl(i,j,k)=", sigma_odl(i,j,k)
6050
6051          enddo
6052 c        write (iout,*) "godl",(godl(k),k=1,constr_homology) ! exponents
6053 c        write (iout,*) "ii i j",ii,i,j," odleg2",odleg2 ! sum of exps
6054 #ifdef DEBUG
6055          write (iout,*) "godl",(godl(k),k=1,constr_homology) ! exponents
6056          write (iout,*) "ii i j",ii,i,j," odleg2",odleg2 ! sum of exps
6057 #endif
6058          odleg=odleg-dLOG(odleg2/constr_homology)+min_odl
6059 c        write (iout,*) "odleg",odleg ! sum of -ln-s
6060 c Gradient
6061          sum_godl=odleg2
6062          sum_sgodl=0.0d0
6063          do k=1,constr_homology
6064 c            godl=dexp(((-(distance(i,j,k)**2)/(2*(sigma_odl(i,j,k))**2))
6065 c     &           *waga_dist)+min_odl
6066 c          sgodl=-godl(k)*distance(k)*sigma_odl(k,ii)*waga_dist
6067            sgodl=-godl(k)*distance(k)*sigma_odl(k,ii) ! waga_dist rmvd
6068            sum_sgodl=sum_sgodl+sgodl
6069
6070 c            sgodl2=sgodl2+sgodl
6071 c      write(iout,*) i, j, k, distance(i,j,k), "W GRADIENCIE1"
6072 c      write(iout,*) "constr_homology=",constr_homology
6073 c      write(iout,*) i, j, k, "TEST K"
6074          enddo
6075
6076        if (homol_nset.gt.1)then
6077          grad_odl3=waga_dist1(iset)*sum_sgodl/(sum_godl*dij)
6078        else
6079          grad_odl3=waga_dist*sum_sgodl/(sum_godl*dij)
6080        endif
6081 c        grad_odl3=sum_sgodl/(sum_godl*dij)
6082
6083
6084 c      write(iout,*) i, j, k, distance(i,j,k), "W GRADIENCIE2"
6085 c      write(iout,*) (distance(i,j,k)**2), (2*(sigma_odl(i,j,k))**2),
6086 c     &              (-(distance(i,j,k)**2)/(2*(sigma_odl(i,j,k))**2))
6087
6088 ccc      write(iout,*) godl, sgodl, grad_odl3
6089
6090 c          grad_odl=grad_odl+grad_odl3
6091
6092          do jik=1,3
6093             ggodl=grad_odl3*(c(jik,i)-c(jik,j))
6094 ccc      write(iout,*) c(jik,i+1), c(jik,j+1), (c(jik,i+1)-c(jik,j+1))
6095 ccc      write(iout,746) "GRAD_ODL_1", i, j, jik, ggodl, 
6096 ccc     &              ghpbc(jik,i+1), ghpbc(jik,j+1)
6097             ghpbc(jik,i)=ghpbc(jik,i)+ggodl
6098             ghpbc(jik,j)=ghpbc(jik,j)-ggodl
6099 ccc      write(iout,746) "GRAD_ODL_2", i, j, jik, ggodl,
6100 ccc     &              ghpbc(jik,i+1), ghpbc(jik,j+1)
6101 c         if (i.eq.25.and.j.eq.27) then
6102 c         write(iout,*) "jik",jik,"i",i,"j",j
6103 c         write(iout,*) "sum_sgodl",sum_sgodl,"sgodl",sgodl
6104 c         write(iout,*) "grad_odl3",grad_odl3
6105 c         write(iout,*) "c(",jik,i,")",c(jik,i),"c(",jik,j,")",c(jik,j)
6106 c         write(iout,*) "ggodl",ggodl
6107 c         write(iout,*) "ghpbc(",jik,i,")",
6108 c     &                 ghpbc(jik,i),"ghpbc(",jik,j,")",
6109 c     &                 ghpbc(jik,j)   
6110 c         endif
6111          enddo
6112 ccc       write(iout,778)"TEST: odleg2=", odleg2, "DLOG(odleg2)=", 
6113 ccc     & dLOG(odleg2),"-odleg=", -odleg
6114
6115       enddo ! ii-loop for dist
6116 #ifdef DEBUG
6117       write(iout,*) "------- dist restrs end -------"
6118 c     if (waga_angle.eq.1.0d0 .or. waga_theta.eq.1.0d0 .or. 
6119 c    &     waga_d.eq.1.0d0) call sum_gradient
6120 #endif
6121 c Pseudo-energy and gradient from dihedral-angle restraints from
6122 c homology templates
6123 c      write (iout,*) "End of distance loop"
6124 c      call flush(iout)
6125       kat=0.0d0
6126 c      write (iout,*) idihconstr_start_homo,idihconstr_end_homo
6127 #ifdef DEBUG
6128       write(iout,*) "------- dih restrs start -------"
6129       do i=idihconstr_start_homo,idihconstr_end_homo
6130         write (iout,*) "gloc_init(",i,icg,")",gloc(i,icg)
6131       enddo
6132 #endif
6133       do i=idihconstr_start_homo,idihconstr_end_homo
6134         kat2=0.0d0
6135 c        betai=beta(i,i+1,i+2,i+3)
6136         betai = phi(i+3)
6137 c       write (iout,*) "betai =",betai
6138         do k=1,constr_homology
6139           dih_diff(k)=pinorm(dih(k,i)-betai)
6140 c         write (iout,*) "dih_diff(",k,") =",dih_diff(k)
6141 c          if (dih_diff(i,k).gt.3.14159) dih_diff(i,k)=
6142 c     &                                   -(6.28318-dih_diff(i,k))
6143 c          if (dih_diff(i,k).lt.-3.14159) dih_diff(i,k)=
6144 c     &                                   6.28318+dih_diff(i,k)
6145
6146           kat3=-0.5d0*dih_diff(k)**2*sigma_dih(k,i) ! waga_angle rmvd from Gaussian argument
6147 c         kat3=-0.5d0*waga_angle*dih_diff(k)**2*sigma_dih(k,i)
6148           gdih(k)=dexp(kat3)
6149           kat2=kat2+gdih(k)
6150 c          write(iout,*) "kat2=", kat2, "exp(kat3)=", exp(kat3)
6151 c          write(*,*)""
6152         enddo
6153 c       write (iout,*) "gdih",(gdih(k),k=1,constr_homology) ! exps
6154 c       write (iout,*) "i",i," betai",betai," kat2",kat2 ! sum of exps
6155 #ifdef DEBUG
6156         write (iout,*) "i",i," betai",betai," kat2",kat2
6157         write (iout,*) "gdih",(gdih(k),k=1,constr_homology)
6158 #endif
6159         if (kat2.le.1.0d-14) cycle
6160         kat=kat-dLOG(kat2/constr_homology)
6161 c       write (iout,*) "kat",kat ! sum of -ln-s
6162
6163 ccc       write(iout,778)"TEST: kat2=", kat2, "DLOG(kat2)=",
6164 ccc     & dLOG(kat2), "-kat=", -kat
6165
6166 c ----------------------------------------------------------------------
6167 c Gradient
6168 c ----------------------------------------------------------------------
6169
6170         sum_gdih=kat2
6171         sum_sgdih=0.0d0
6172         do k=1,constr_homology
6173           sgdih=-gdih(k)*dih_diff(k)*sigma_dih(k,i)  ! waga_angle rmvd
6174 c         sgdih=-gdih(k)*dih_diff(k)*sigma_dih(k,i)*waga_angle
6175           sum_sgdih=sum_sgdih+sgdih
6176         enddo
6177 c       grad_dih3=sum_sgdih/sum_gdih
6178         if (homol_nset.gt.1)then
6179          grad_dih3=waga_angle1(iset)*sum_sgdih/sum_gdih
6180         else
6181          grad_dih3=waga_angle*sum_sgdih/sum_gdih
6182         endif
6183
6184 c      write(iout,*)i,k,gdih,sgdih,beta(i+1,i+2,i+3,i+4),grad_dih3
6185 ccc      write(iout,747) "GRAD_KAT_1", i, nphi, icg, grad_dih3,
6186 ccc     & gloc(nphi+i-3,icg)
6187         gloc(i,icg)=gloc(i,icg)+grad_dih3
6188 c        if (i.eq.25) then
6189 c        write(iout,*) "i",i,"icg",icg,"gloc(",i,icg,")",gloc(i,icg)
6190 c        endif
6191 ccc      write(iout,747) "GRAD_KAT_2", i, nphi, icg, grad_dih3,
6192 ccc     & gloc(nphi+i-3,icg)
6193
6194       enddo ! i-loop for dih
6195 #ifdef DEBUG
6196       write(iout,*) "------- dih restrs end -------"
6197 #endif
6198
6199 c Pseudo-energy and gradient for theta angle restraints from
6200 c homology templates
6201 c FP 01/15 - inserted from econstr_local_test.F, loop structure
6202 c adapted
6203
6204 c
6205 c     For constr_homology reference structures (FP)
6206 c     
6207 c     Uconst_back_tot=0.0d0
6208       Eval=0.0d0
6209       Erot=0.0d0
6210 c     Econstr_back legacy
6211       do i=1,nres
6212 c     do i=ithet_start,ithet_end
6213        dutheta(i)=0.0d0
6214 c     enddo
6215 c     do i=loc_start,loc_end
6216         do j=1,3
6217           duscdiff(j,i)=0.0d0
6218           duscdiffx(j,i)=0.0d0
6219         enddo
6220       enddo
6221 c
6222 c     do iref=1,nref
6223 c     write (iout,*) "ithet_start =",ithet_start,"ithet_end =",ithet_end
6224 c     write (iout,*) "waga_theta",waga_theta
6225       if (waga_theta.gt.0.0d0) then
6226 #ifdef DEBUG
6227       write (iout,*) "usampl",usampl
6228       write(iout,*) "------- theta restrs start -------"
6229 c     do i=ithet_start,ithet_end
6230 c       write (iout,*) "gloc_init(",nphi+i,icg,")",gloc(nphi+i,icg)
6231 c     enddo
6232 #endif
6233 c     write (iout,*) "maxres",maxres,"nres",nres
6234
6235       do i=ithet_start,ithet_end
6236 c
6237 c     do i=1,nfrag_back
6238 c       ii = ifrag_back(2,i,iset)-ifrag_back(1,i,iset)
6239 c
6240 c Deviation of theta angles wrt constr_homology ref structures
6241 c
6242         utheta_i=0.0d0 ! argument of Gaussian for single k
6243         gutheta_i=0.0d0 ! Sum of Gaussians over constr_homology ref structures
6244 c       do j=ifrag_back(1,i,iset)+2,ifrag_back(2,i,iset) ! original loop
6245 c       over residues in a fragment
6246 c       write (iout,*) "theta(",i,")=",theta(i)
6247         do k=1,constr_homology
6248 c
6249 c         dtheta_i=theta(j)-thetaref(j,iref)
6250 c         dtheta_i=thetaref(k,i)-theta(i) ! original form without indexing
6251           theta_diff(k)=thetatpl(k,i)-theta(i)
6252 c
6253           utheta_i=-0.5d0*theta_diff(k)**2*sigma_theta(k,i) ! waga_theta rmvd from Gaussian argument
6254 c         utheta_i=-0.5d0*waga_theta*theta_diff(k)**2*sigma_theta(k,i) ! waga_theta?
6255           gtheta(k)=dexp(utheta_i) ! + min_utheta_i?
6256           gutheta_i=gutheta_i+dexp(utheta_i)   ! Sum of Gaussians (pk)
6257 c         Gradient for single Gaussian restraint in subr Econstr_back
6258 c         dutheta(j-2)=dutheta(j-2)+wfrag_back(1,i,iset)*dtheta_i/(ii-1)
6259 c
6260         enddo
6261 c       write (iout,*) "gtheta",(gtheta(k),k=1,constr_homology) ! exps
6262 c       write (iout,*) "i",i," gutheta_i",gutheta_i ! sum of exps
6263
6264 c
6265 c         Gradient for multiple Gaussian restraint
6266         sum_gtheta=gutheta_i
6267         sum_sgtheta=0.0d0
6268         do k=1,constr_homology
6269 c        New generalized expr for multiple Gaussian from Econstr_back
6270          sgtheta=-gtheta(k)*theta_diff(k)*sigma_theta(k,i) ! waga_theta rmvd
6271 c
6272 c        sgtheta=-gtheta(k)*theta_diff(k)*sigma_theta(k,i)*waga_theta ! right functional form?
6273           sum_sgtheta=sum_sgtheta+sgtheta ! cum variable
6274         enddo
6275 c       grad_theta3=sum_sgtheta/sum_gtheta 1/*theta(i)? s. line below
6276 c       grad_theta3=sum_sgtheta/sum_gtheta
6277 c
6278 c       Final value of gradient using same var as in Econstr_back
6279         dutheta(i-2)=sum_sgtheta/sum_gtheta*waga_theta
6280 c       dutheta(i)=sum_sgtheta/sum_gtheta
6281 c
6282 c       Uconst_back=Uconst_back+waga_theta*utheta(i) ! waga_theta added as weight
6283         Eval=Eval-dLOG(gutheta_i/constr_homology)
6284 c       write (iout,*) "utheta(",i,")=",utheta(i) ! -ln of sum of exps
6285 c       write (iout,*) "Uconst_back",Uconst_back ! sum of -ln-s
6286 c       Uconst_back=Uconst_back+utheta(i)
6287       enddo ! (i-loop for theta)
6288 #ifdef DEBUG
6289       write(iout,*) "------- theta restrs end -------"
6290 #endif
6291       endif
6292 c
6293 c Deviation of local SC geometry
6294 c
6295 c Separation of two i-loops (instructed by AL - 11/3/2014)
6296 c
6297 c     write (iout,*) "loc_start =",loc_start,"loc_end =",loc_end
6298 c     write (iout,*) "waga_d",waga_d
6299
6300 #ifdef DEBUG
6301       write(iout,*) "------- SC restrs start -------"
6302       write (iout,*) "Initial duscdiff,duscdiffx"
6303       do i=loc_start,loc_end
6304         write (iout,*) i,(duscdiff(jik,i),jik=1,3),
6305      &                 (duscdiffx(jik,i),jik=1,3)
6306       enddo
6307 #endif
6308       do i=loc_start,loc_end
6309         usc_diff_i=0.0d0 ! argument of Gaussian for single k
6310         guscdiff(i)=0.0d0 ! Sum of Gaussians over constr_homology ref structures
6311 c       do j=ifrag_back(1,i,iset)+1,ifrag_back(2,i,iset)-1 ! Econstr_back legacy
6312 c       write(iout,*) "xxtab, yytab, zztab"
6313 c       write(iout,'(i5,3f8.2)') i,xxtab(i),yytab(i),zztab(i)
6314         do k=1,constr_homology
6315 c
6316           dxx=-xxtpl(k,i)+xxtab(i) ! Diff b/w x component of ith SC vector in model and kth ref str?
6317 c                                    Original sign inverted for calc of gradients (s. Econstr_back)
6318           dyy=-yytpl(k,i)+yytab(i) ! ibid y
6319           dzz=-zztpl(k,i)+zztab(i) ! ibid z
6320 c         write(iout,*) "dxx, dyy, dzz"
6321 c         write(iout,'(2i5,3f8.2)') k,i,dxx,dyy,dzz
6322 c
6323           usc_diff_i=-0.5d0*(dxx**2+dyy**2+dzz**2)*sigma_d(k,i)  ! waga_d rmvd from Gaussian argument
6324 c         usc_diff(i)=-0.5d0*waga_d*(dxx**2+dyy**2+dzz**2)*sigma_d(k,i) ! waga_d?
6325 c         uscdiffk(k)=usc_diff(i)
6326           guscdiff2(k)=dexp(usc_diff_i) ! without min_scdiff
6327           guscdiff(i)=guscdiff(i)+dexp(usc_diff_i)   !Sum of Gaussians (pk)
6328 c          write (iout,'(i5,6f10.5)') j,xxtab(j),yytab(j),zztab(j),
6329 c     &      xxref(j),yyref(j),zzref(j)
6330         enddo
6331 c
6332 c       Gradient 
6333 c
6334 c       Generalized expression for multiple Gaussian acc to that for a single 
6335 c       Gaussian in Econstr_back as instructed by AL (FP - 03/11/2014)
6336 c
6337 c       Original implementation
6338 c       sum_guscdiff=guscdiff(i)
6339 c
6340 c       sum_sguscdiff=0.0d0
6341 c       do k=1,constr_homology
6342 c          sguscdiff=-guscdiff2(k)*dscdiff(k)*sigma_d(k,i)*waga_d !waga_d? 
6343 c          sguscdiff=-guscdiff3(k)*dscdiff(k)*sigma_d(k,i)*waga_d ! w min_uscdiff
6344 c          sum_sguscdiff=sum_sguscdiff+sguscdiff
6345 c       enddo
6346 c
6347 c       Implementation of new expressions for gradient (Jan. 2015)
6348 c
6349 c       grad_uscdiff=sum_sguscdiff/(sum_guscdiff*dtab) !?
6350         do k=1,constr_homology 
6351 c
6352 c       New calculation of dxx, dyy, and dzz corrected by AL (07/11), was missing and wrong
6353 c       before. Now the drivatives should be correct
6354 c
6355           dxx=-xxtpl(k,i)+xxtab(i) ! Diff b/w x component of ith SC vector in model and kth ref str?
6356 c                                  Original sign inverted for calc of gradients (s. Econstr_back)
6357           dyy=-yytpl(k,i)+yytab(i) ! ibid y
6358           dzz=-zztpl(k,i)+zztab(i) ! ibid z
6359 c
6360 c         New implementation
6361 c
6362           sum_guscdiff=guscdiff2(k)*!(dsqrt(dxx*dxx+dyy*dyy+dzz*dzz))* -> wrong!
6363      &                 sigma_d(k,i) ! for the grad wrt r' 
6364 c         sum_sguscdiff=sum_sguscdiff+sum_guscdiff
6365 c
6366 c
6367 c        New implementation
6368          sum_guscdiff = waga_d*sum_guscdiff
6369          do jik=1,3
6370             duscdiff(jik,i-1)=duscdiff(jik,i-1)+
6371      &      sum_guscdiff*(dXX_C1tab(jik,i)*dxx+
6372      &      dYY_C1tab(jik,i)*dyy+dZZ_C1tab(jik,i)*dzz)/guscdiff(i)
6373             duscdiff(jik,i)=duscdiff(jik,i)+
6374      &      sum_guscdiff*(dXX_Ctab(jik,i)*dxx+
6375      &      dYY_Ctab(jik,i)*dyy+dZZ_Ctab(jik,i)*dzz)/guscdiff(i)
6376             duscdiffx(jik,i)=duscdiffx(jik,i)+
6377      &      sum_guscdiff*(dXX_XYZtab(jik,i)*dxx+
6378      &      dYY_XYZtab(jik,i)*dyy+dZZ_XYZtab(jik,i)*dzz)/guscdiff(i)
6379 c
6380 #ifdef DEBUG
6381              write(iout,*) "jik",jik,"i",i
6382              write(iout,*) "dxx, dyy, dzz"
6383              write(iout,'(2i5,3f8.2)') k,i,dxx,dyy,dzz
6384              write(iout,*) "guscdiff2(",k,")",guscdiff2(k)
6385 c            write(iout,*) "sum_sguscdiff",sum_sguscdiff
6386 cc           write(iout,*) "dXX_Ctab(",jik,i,")",dXX_Ctab(jik,i)
6387 c            write(iout,*) "dYY_Ctab(",jik,i,")",dYY_Ctab(jik,i)
6388 c            write(iout,*) "dZZ_Ctab(",jik,i,")",dZZ_Ctab(jik,i)
6389 c            write(iout,*) "dXX_C1tab(",jik,i,")",dXX_C1tab(jik,i)
6390 c            write(iout,*) "dYY_C1tab(",jik,i,")",dYY_C1tab(jik,i)
6391 c            write(iout,*) "dZZ_C1tab(",jik,i,")",dZZ_C1tab(jik,i)
6392 c            write(iout,*) "dXX_XYZtab(",jik,i,")",dXX_XYZtab(jik,i)
6393 c            write(iout,*) "dYY_XYZtab(",jik,i,")",dYY_XYZtab(jik,i)
6394 c            write(iout,*) "dZZ_XYZtab(",jik,i,")",dZZ_XYZtab(jik,i)
6395 c            write(iout,*) "duscdiff(",jik,i-1,")",duscdiff(jik,i-1)
6396 c            write(iout,*) "duscdiff(",jik,i,")",duscdiff(jik,i)
6397 c            write(iout,*) "duscdiffx(",jik,i,")",duscdiffx(jik,i)
6398 c            endif
6399 #endif
6400          enddo
6401         enddo
6402 c
6403 c       uscdiff(i)=-dLOG(guscdiff(i)/(ii-1))      ! Weighting by (ii-1) required?
6404 c        usc_diff(i)=-dLOG(guscdiff(i)/constr_homology) ! + min_uscdiff ?
6405 c
6406 c        write (iout,*) i," uscdiff",uscdiff(i)
6407 c
6408 c Put together deviations from local geometry
6409
6410 c       Uconst_back=Uconst_back+wfrag_back(1,i,iset)*utheta(i)+
6411 c      &            wfrag_back(3,i,iset)*uscdiff(i)
6412         Erot=Erot-dLOG(guscdiff(i)/constr_homology)
6413 c       write (iout,*) "usc_diff(",i,")=",usc_diff(i) ! -ln of sum of exps
6414 c       write (iout,*) "Uconst_back",Uconst_back ! cum sum of -ln-s
6415 c       Uconst_back=Uconst_back+usc_diff(i)
6416 c
6417 c     Gradient of multiple Gaussian restraint (FP - 04/11/2014 - right?)
6418 c
6419 c     New implment: multiplied by sum_sguscdiff
6420 c
6421
6422       enddo ! (i-loop for dscdiff)
6423
6424 c      endif
6425
6426 #ifdef DEBUG
6427       write(iout,*) "------- SC restrs end -------"
6428         write (iout,*) "------ After SC loop in e_modeller ------"
6429         do i=loc_start,loc_end
6430          write (iout,*) "i",i," gradc",(gradc(j,i,icg),j=1,3)
6431          write (iout,*) "i",i," gradx",(gradx(j,i,icg),j=1,3)
6432         enddo
6433       if (waga_theta.eq.1.0d0) then
6434       write (iout,*) "in e_modeller after SC restr end: dutheta"
6435       do i=ithet_start,ithet_end
6436         write (iout,*) i,dutheta(i)
6437       enddo
6438       endif
6439       if (waga_d.eq.1.0d0) then
6440       write (iout,*) "e_modeller after SC loop: duscdiff/x"
6441       do i=1,nres
6442         write (iout,*) i,(duscdiff(j,i),j=1,3)
6443         write (iout,*) i,(duscdiffx(j,i),j=1,3)
6444       enddo
6445       endif
6446 #endif
6447
6448 c Total energy from homology restraints
6449 #ifdef DEBUG
6450       write (iout,*) "odleg",odleg," kat",kat
6451 #endif
6452 c
6453 c Addition of energy of theta angle and SC local geom over constr_homologs ref strs
6454 c
6455 c     ehomology_constr=odleg+kat
6456       if (homol_nset.gt.1)then
6457        ehomology_constr=waga_dist1(iset)*odleg+waga_angle1(iset)*kat
6458      &              +waga_theta*Eval
6459      &              +waga_d*Erot     
6460       else
6461        ehomology_constr=waga_dist*odleg+waga_angle*kat+waga_theta*Eval
6462      &              +waga_d*Erot
6463       endif
6464 c     write (iout,*) "odleg",odleg," kat",kat," Uconst_back",Uconst_back
6465 c     write (iout,*) "ehomology_constr",ehomology_constr
6466 c     ehomology_constr=odleg+kat+Uconst_back
6467       return
6468 c
6469 c FP 01/15 end
6470 c
6471   748 format(a8,f12.3,a6,f12.3,a7,f12.3)
6472   747 format(a12,i4,i4,i4,f8.3,f8.3)
6473   746 format(a12,i4,i4,i4,f8.3,f8.3,f8.3)
6474   778 format(a7,1X,f10.3,1X,a4,1X,f10.3,1X,a5,1X,f10.3)
6475   779 format(i3,1X,i3,1X,i2,1X,a7,1X,f7.3,1X,a7,1X,f7.3,1X,a13,1X,
6476      &       f7.3,1X,a17,1X,f9.3,1X,a10,1X,f8.3,1X,a10,1X,f8.3)
6477       end
6478
6479 c------------------------------------------------------------------------------
6480       subroutine etor_d(etors_d)
6481 C 6/23/01 Compute double torsional energy
6482       implicit real*8 (a-h,o-z)
6483       include 'DIMENSIONS'
6484       include 'COMMON.VAR'
6485       include 'COMMON.GEO'
6486       include 'COMMON.LOCAL'
6487       include 'COMMON.TORSION'
6488       include 'COMMON.INTERACT'
6489       include 'COMMON.DERIV'
6490       include 'COMMON.CHAIN'
6491       include 'COMMON.NAMES'
6492       include 'COMMON.IOUNITS'
6493       include 'COMMON.FFIELD'
6494       include 'COMMON.TORCNSTR'
6495       logical lprn
6496 C Set lprn=.true. for debugging
6497       lprn=.false.
6498 c     lprn=.true.
6499       etors_d=0.0D0
6500       do i=iphid_start,iphid_end
6501         itori=itortyp(itype(i-2))
6502         itori1=itortyp(itype(i-1))
6503         itori2=itortyp(itype(i))
6504         phii=phi(i)
6505         phii1=phi(i+1)
6506         gloci1=0.0D0
6507         gloci2=0.0D0
6508         do j=1,ntermd_1(itori,itori1,itori2)
6509           v1cij=v1c(1,j,itori,itori1,itori2)
6510           v1sij=v1s(1,j,itori,itori1,itori2)
6511           v2cij=v1c(2,j,itori,itori1,itori2)
6512           v2sij=v1s(2,j,itori,itori1,itori2)
6513           cosphi1=dcos(j*phii)
6514           sinphi1=dsin(j*phii)
6515           cosphi2=dcos(j*phii1)
6516           sinphi2=dsin(j*phii1)
6517           etors_d=etors_d+v1cij*cosphi1+v1sij*sinphi1+
6518      &     v2cij*cosphi2+v2sij*sinphi2
6519           gloci1=gloci1+j*(v1sij*cosphi1-v1cij*sinphi1)
6520           gloci2=gloci2+j*(v2sij*cosphi2-v2cij*sinphi2)
6521         enddo
6522         do k=2,ntermd_2(itori,itori1,itori2)
6523           do l=1,k-1
6524             v1cdij = v2c(k,l,itori,itori1,itori2)
6525             v2cdij = v2c(l,k,itori,itori1,itori2)
6526             v1sdij = v2s(k,l,itori,itori1,itori2)
6527             v2sdij = v2s(l,k,itori,itori1,itori2)
6528             cosphi1p2=dcos(l*phii+(k-l)*phii1)
6529             cosphi1m2=dcos(l*phii-(k-l)*phii1)
6530             sinphi1p2=dsin(l*phii+(k-l)*phii1)
6531             sinphi1m2=dsin(l*phii-(k-l)*phii1)
6532             etors_d=etors_d+v1cdij*cosphi1p2+v2cdij*cosphi1m2+
6533      &        v1sdij*sinphi1p2+v2sdij*sinphi1m2
6534             gloci1=gloci1+l*(v1sdij*cosphi1p2+v2sdij*cosphi1m2
6535      &        -v1cdij*sinphi1p2-v2cdij*sinphi1m2)
6536             gloci2=gloci2+(k-l)*(v1sdij*cosphi1p2-v2sdij*cosphi1m2
6537      &        -v1cdij*sinphi1p2+v2cdij*sinphi1m2) 
6538           enddo
6539         enddo
6540         gloc(i-3,icg)=gloc(i-3,icg)+wtor_d*gloci1
6541         gloc(i-2,icg)=gloc(i-2,icg)+wtor_d*gloci2
6542 c        write (iout,*) "gloci", gloc(i-3,icg)
6543       enddo
6544       return
6545       end
6546 #endif
6547 c------------------------------------------------------------------------------
6548       subroutine eback_sc_corr(esccor)
6549 c 7/21/2007 Correlations between the backbone-local and side-chain-local
6550 c        conformational states; temporarily implemented as differences
6551 c        between UNRES torsional potentials (dependent on three types of
6552 c        residues) and the torsional potentials dependent on all 20 types
6553 c        of residues computed from AM1  energy surfaces of terminally-blocked
6554 c        amino-acid residues.
6555       implicit real*8 (a-h,o-z)
6556       include 'DIMENSIONS'
6557       include 'COMMON.VAR'
6558       include 'COMMON.GEO'
6559       include 'COMMON.LOCAL'
6560       include 'COMMON.TORSION'
6561       include 'COMMON.SCCOR'
6562       include 'COMMON.INTERACT'
6563       include 'COMMON.DERIV'
6564       include 'COMMON.CHAIN'
6565       include 'COMMON.NAMES'
6566       include 'COMMON.IOUNITS'
6567       include 'COMMON.FFIELD'
6568       include 'COMMON.CONTROL'
6569       logical lprn
6570 C Set lprn=.true. for debugging
6571       lprn=.false.
6572 c      lprn=.true.
6573 c      write (iout,*) "EBACK_SC_COR",iphi_start,iphi_end,nterm_sccor
6574       esccor=0.0D0
6575       do i=itau_start,itau_end
6576         esccor_ii=0.0D0
6577         if ((itype(i-2).eq.ntyp1).or.(itype(i-1).eq.ntyp1)) cycle
6578         isccori=isccortyp(itype(i-2))
6579         isccori1=isccortyp(itype(i-1))
6580         phii=phi(i)
6581 cccc  Added 9 May 2012
6582 cc Tauangle is torsional engle depending on the value of first digit 
6583 c(see comment below)
6584 cc Omicron is flat angle depending on the value of first digit 
6585 c(see comment below)
6586
6587         
6588         do intertyp=1,3 !intertyp
6589 cc Added 09 May 2012 (Adasko)
6590 cc  Intertyp means interaction type of backbone mainchain correlation: 
6591 c   1 = SC...Ca...Ca...Ca
6592 c   2 = Ca...Ca...Ca...SC
6593 c   3 = SC...Ca...Ca...SCi
6594         gloci=0.0D0
6595         if (((intertyp.eq.3).and.((itype(i-2).eq.10).or.
6596      &      (itype(i-1).eq.10).or.(itype(i-2).eq.21).or.
6597      &      (itype(i-1).eq.21)))
6598      &    .or. ((intertyp.eq.1).and.((itype(i-2).eq.10)
6599      &     .or.(itype(i-2).eq.21)))
6600      &    .or.((intertyp.eq.2).and.((itype(i-1).eq.10).or.
6601      &      (itype(i-1).eq.21)))) cycle  
6602         if ((intertyp.eq.2).and.(i.eq.4).and.(itype(1).eq.21)) cycle
6603         if ((intertyp.eq.1).and.(i.eq.nres).and.(itype(nres).eq.21))
6604      & cycle
6605         do j=1,nterm_sccor(isccori,isccori1)
6606           v1ij=v1sccor(j,intertyp,isccori,isccori1)
6607           v2ij=v2sccor(j,intertyp,isccori,isccori1)
6608           cosphi=dcos(j*tauangle(intertyp,i))
6609           sinphi=dsin(j*tauangle(intertyp,i))
6610           esccor=esccor+v1ij*cosphi+v2ij*sinphi
6611           gloci=gloci+j*(v2ij*cosphi-v1ij*sinphi)
6612         enddo
6613         gloc_sc(intertyp,i-3,icg)=gloc_sc(intertyp,i-3,icg)+wsccor*gloci
6614 c        write (iout,*) "WTF",intertyp,i,itype(i),v1ij*cosphi+v2ij*sinphi
6615 c     &gloc_sc(intertyp,i-3,icg)
6616         if (lprn)
6617      &  write (iout,'(2(a3,2x,i3,2x),2i3,6f8.3/26x,6f8.3/)')
6618      &  restyp(itype(i-2)),i-2,restyp(itype(i-1)),i-1,itori,itori1,
6619      &  (v1sccor(j,intertyp,itori,itori1),j=1,6)
6620      & ,(v2sccor(j,intertyp,itori,itori1),j=1,6)
6621         gsccor_loc(i-3)=gsccor_loc(i-3)+gloci
6622        enddo !intertyp
6623       enddo
6624 c        do i=1,nres
6625 c        write (iout,*) "W@T@F",  gloc_sc(1,i,icg),gloc(i,icg)
6626 c        enddo
6627       return
6628       end
6629 c----------------------------------------------------------------------------
6630       subroutine multibody(ecorr)
6631 C This subroutine calculates multi-body contributions to energy following
6632 C the idea of Skolnick et al. If side chains I and J make a contact and
6633 C at the same time side chains I+1 and J+1 make a contact, an extra 
6634 C contribution equal to sqrt(eps(i,j)*eps(i+1,j+1)) is added.
6635       implicit real*8 (a-h,o-z)
6636       include 'DIMENSIONS'
6637       include 'COMMON.IOUNITS'
6638       include 'COMMON.DERIV'
6639       include 'COMMON.INTERACT'
6640       include 'COMMON.CONTACTS'
6641       double precision gx(3),gx1(3)
6642       logical lprn
6643
6644 C Set lprn=.true. for debugging
6645       lprn=.false.
6646
6647       if (lprn) then
6648         write (iout,'(a)') 'Contact function values:'
6649         do i=nnt,nct-2
6650           write (iout,'(i2,20(1x,i2,f10.5))') 
6651      &        i,(jcont(j,i),facont(j,i),j=1,num_cont(i))
6652         enddo
6653       endif
6654       ecorr=0.0D0
6655       do i=nnt,nct
6656         do j=1,3
6657           gradcorr(j,i)=0.0D0
6658           gradxorr(j,i)=0.0D0
6659         enddo
6660       enddo
6661       do i=nnt,nct-2
6662
6663         DO ISHIFT = 3,4
6664
6665         i1=i+ishift
6666         num_conti=num_cont(i)
6667         num_conti1=num_cont(i1)
6668         do jj=1,num_conti
6669           j=jcont(jj,i)
6670           do kk=1,num_conti1
6671             j1=jcont(kk,i1)
6672             if (j1.eq.j+ishift .or. j1.eq.j-ishift) then
6673 cd          write(iout,*)'i=',i,' j=',j,' i1=',i1,' j1=',j1,
6674 cd   &                   ' ishift=',ishift
6675 C Contacts I--J and I+ISHIFT--J+-ISHIFT1 occur simultaneously. 
6676 C The system gains extra energy.
6677               ecorr=ecorr+esccorr(i,j,i1,j1,jj,kk)
6678             endif   ! j1==j+-ishift
6679           enddo     ! kk  
6680         enddo       ! jj
6681
6682         ENDDO ! ISHIFT
6683
6684       enddo         ! i
6685       return
6686       end
6687 c------------------------------------------------------------------------------
6688       double precision function esccorr(i,j,k,l,jj,kk)
6689       implicit real*8 (a-h,o-z)
6690       include 'DIMENSIONS'
6691       include 'COMMON.IOUNITS'
6692       include 'COMMON.DERIV'
6693       include 'COMMON.INTERACT'
6694       include 'COMMON.CONTACTS'
6695       double precision gx(3),gx1(3)
6696       logical lprn
6697       lprn=.false.
6698       eij=facont(jj,i)
6699       ekl=facont(kk,k)
6700 cd    write (iout,'(4i5,3f10.5)') i,j,k,l,eij,ekl,-eij*ekl
6701 C Calculate the multi-body contribution to energy.
6702 C Calculate multi-body contributions to the gradient.
6703 cd    write (iout,'(2(2i3,3f10.5))')i,j,(gacont(m,jj,i),m=1,3),
6704 cd   & k,l,(gacont(m,kk,k),m=1,3)
6705       do m=1,3
6706         gx(m) =ekl*gacont(m,jj,i)
6707         gx1(m)=eij*gacont(m,kk,k)
6708         gradxorr(m,i)=gradxorr(m,i)-gx(m)
6709         gradxorr(m,j)=gradxorr(m,j)+gx(m)
6710         gradxorr(m,k)=gradxorr(m,k)-gx1(m)
6711         gradxorr(m,l)=gradxorr(m,l)+gx1(m)
6712       enddo
6713       do m=i,j-1
6714         do ll=1,3
6715           gradcorr(ll,m)=gradcorr(ll,m)+gx(ll)
6716         enddo
6717       enddo
6718       do m=k,l-1
6719         do ll=1,3
6720           gradcorr(ll,m)=gradcorr(ll,m)+gx1(ll)
6721         enddo
6722       enddo 
6723       esccorr=-eij*ekl
6724       return
6725       end
6726 c------------------------------------------------------------------------------
6727       subroutine multibody_hb(ecorr,ecorr5,ecorr6,n_corr,n_corr1)
6728 C This subroutine calculates multi-body contributions to hydrogen-bonding 
6729       implicit real*8 (a-h,o-z)
6730       include 'DIMENSIONS'
6731       include 'COMMON.IOUNITS'
6732 #ifdef MPI
6733       include "mpif.h"
6734       parameter (max_cont=maxconts)
6735       parameter (max_dim=26)
6736       integer source,CorrelType,CorrelID,CorrelType1,CorrelID1,Error
6737       double precision zapas(max_dim,maxconts,max_fg_procs),
6738      &  zapas_recv(max_dim,maxconts,max_fg_procs)
6739       common /przechowalnia/ zapas
6740       integer status(MPI_STATUS_SIZE),req(maxconts*2),
6741      &  status_array(MPI_STATUS_SIZE,maxconts*2)
6742 #endif
6743       include 'COMMON.SETUP'
6744       include 'COMMON.FFIELD'
6745       include 'COMMON.DERIV'
6746       include 'COMMON.INTERACT'
6747       include 'COMMON.CONTACTS'
6748       include 'COMMON.CONTROL'
6749       include 'COMMON.LOCAL'
6750       double precision gx(3),gx1(3),time00
6751       logical lprn,ldone
6752
6753 C Set lprn=.true. for debugging
6754       lprn=.false.
6755 #ifdef MPI
6756       n_corr=0
6757       n_corr1=0
6758       if (nfgtasks.le.1) goto 30
6759       if (lprn) then
6760         write (iout,'(a)') 'Contact function values before RECEIVE:'
6761         do i=nnt,nct-2
6762           write (iout,'(2i3,50(1x,i2,f5.2))') 
6763      &    i,num_cont_hb(i),(jcont_hb(j,i),facont_hb(j,i),
6764      &    j=1,num_cont_hb(i))
6765         enddo
6766       endif
6767       call flush(iout)
6768       do i=1,ntask_cont_from
6769         ncont_recv(i)=0
6770       enddo
6771       do i=1,ntask_cont_to
6772         ncont_sent(i)=0
6773       enddo
6774 c      write (iout,*) "ntask_cont_from",ntask_cont_from," ntask_cont_to",
6775 c     & ntask_cont_to
6776 C Make the list of contacts to send to send to other procesors
6777 c      write (iout,*) "limits",max0(iturn4_end-1,iatel_s),iturn3_end
6778 c      call flush(iout)
6779       do i=iturn3_start,iturn3_end
6780 c        write (iout,*) "make contact list turn3",i," num_cont",
6781 c     &    num_cont_hb(i)
6782         call add_hb_contact(i,i+2,iturn3_sent_local(1,i))
6783       enddo
6784       do i=iturn4_start,iturn4_end
6785 c        write (iout,*) "make contact list turn4",i," num_cont",
6786 c     &   num_cont_hb(i)
6787         call add_hb_contact(i,i+3,iturn4_sent_local(1,i))
6788       enddo
6789       do ii=1,nat_sent
6790         i=iat_sent(ii)
6791 c        write (iout,*) "make contact list longrange",i,ii," num_cont",
6792 c     &    num_cont_hb(i)
6793         do j=1,num_cont_hb(i)
6794         do k=1,4
6795           jjc=jcont_hb(j,i)
6796           iproc=iint_sent_local(k,jjc,ii)
6797 c          write (iout,*) "i",i," j",j," k",k," jjc",jjc," iproc",iproc
6798           if (iproc.gt.0) then
6799             ncont_sent(iproc)=ncont_sent(iproc)+1
6800             nn=ncont_sent(iproc)
6801             zapas(1,nn,iproc)=i
6802             zapas(2,nn,iproc)=jjc
6803             zapas(3,nn,iproc)=facont_hb(j,i)
6804             zapas(4,nn,iproc)=ees0p(j,i)
6805             zapas(5,nn,iproc)=ees0m(j,i)
6806             zapas(6,nn,iproc)=gacont_hbr(1,j,i)
6807             zapas(7,nn,iproc)=gacont_hbr(2,j,i)
6808             zapas(8,nn,iproc)=gacont_hbr(3,j,i)
6809             zapas(9,nn,iproc)=gacontm_hb1(1,j,i)
6810             zapas(10,nn,iproc)=gacontm_hb1(2,j,i)
6811             zapas(11,nn,iproc)=gacontm_hb1(3,j,i)
6812             zapas(12,nn,iproc)=gacontp_hb1(1,j,i)
6813             zapas(13,nn,iproc)=gacontp_hb1(2,j,i)
6814             zapas(14,nn,iproc)=gacontp_hb1(3,j,i)
6815             zapas(15,nn,iproc)=gacontm_hb2(1,j,i)
6816             zapas(16,nn,iproc)=gacontm_hb2(2,j,i)
6817             zapas(17,nn,iproc)=gacontm_hb2(3,j,i)
6818             zapas(18,nn,iproc)=gacontp_hb2(1,j,i)
6819             zapas(19,nn,iproc)=gacontp_hb2(2,j,i)
6820             zapas(20,nn,iproc)=gacontp_hb2(3,j,i)
6821             zapas(21,nn,iproc)=gacontm_hb3(1,j,i)
6822             zapas(22,nn,iproc)=gacontm_hb3(2,j,i)
6823             zapas(23,nn,iproc)=gacontm_hb3(3,j,i)
6824             zapas(24,nn,iproc)=gacontp_hb3(1,j,i)
6825             zapas(25,nn,iproc)=gacontp_hb3(2,j,i)
6826             zapas(26,nn,iproc)=gacontp_hb3(3,j,i)
6827           endif
6828         enddo
6829         enddo
6830       enddo
6831       if (lprn) then
6832       write (iout,*) 
6833      &  "Numbers of contacts to be sent to other processors",
6834      &  (ncont_sent(i),i=1,ntask_cont_to)
6835       write (iout,*) "Contacts sent"
6836       do ii=1,ntask_cont_to
6837         nn=ncont_sent(ii)
6838         iproc=itask_cont_to(ii)
6839         write (iout,*) nn," contacts to processor",iproc,
6840      &   " of CONT_TO_COMM group"
6841         do i=1,nn
6842           write(iout,'(2f5.0,4f10.5)')(zapas(j,i,ii),j=1,5)
6843         enddo
6844       enddo
6845       call flush(iout)
6846       endif
6847       CorrelType=477
6848       CorrelID=fg_rank+1
6849       CorrelType1=478
6850       CorrelID1=nfgtasks+fg_rank+1
6851       ireq=0
6852 C Receive the numbers of needed contacts from other processors 
6853       do ii=1,ntask_cont_from
6854         iproc=itask_cont_from(ii)
6855         ireq=ireq+1
6856         call MPI_Irecv(ncont_recv(ii),1,MPI_INTEGER,iproc,CorrelType,
6857      &    FG_COMM,req(ireq),IERR)
6858       enddo
6859 c      write (iout,*) "IRECV ended"
6860 c      call flush(iout)
6861 C Send the number of contacts needed by other processors
6862       do ii=1,ntask_cont_to
6863         iproc=itask_cont_to(ii)
6864         ireq=ireq+1
6865         call MPI_Isend(ncont_sent(ii),1,MPI_INTEGER,iproc,CorrelType,
6866      &    FG_COMM,req(ireq),IERR)
6867       enddo
6868 c      write (iout,*) "ISEND ended"
6869 c      write (iout,*) "number of requests (nn)",ireq
6870       call flush(iout)
6871       if (ireq.gt.0) 
6872      &  call MPI_Waitall(ireq,req,status_array,ierr)
6873 c      write (iout,*) 
6874 c     &  "Numbers of contacts to be received from other processors",
6875 c     &  (ncont_recv(i),i=1,ntask_cont_from)
6876 c      call flush(iout)
6877 C Receive contacts
6878       ireq=0
6879       do ii=1,ntask_cont_from
6880         iproc=itask_cont_from(ii)
6881         nn=ncont_recv(ii)
6882 c        write (iout,*) "Receiving",nn," contacts from processor",iproc,
6883 c     &   " of CONT_TO_COMM group"
6884         call flush(iout)
6885         if (nn.gt.0) then
6886           ireq=ireq+1
6887           call MPI_Irecv(zapas_recv(1,1,ii),nn*max_dim,
6888      &    MPI_DOUBLE_PRECISION,iproc,CorrelType1,FG_COMM,req(ireq),IERR)
6889 c          write (iout,*) "ireq,req",ireq,req(ireq)
6890         endif
6891       enddo
6892 C Send the contacts to processors that need them
6893       do ii=1,ntask_cont_to
6894         iproc=itask_cont_to(ii)
6895         nn=ncont_sent(ii)
6896 c        write (iout,*) nn," contacts to processor",iproc,
6897 c     &   " of CONT_TO_COMM group"
6898         if (nn.gt.0) then
6899           ireq=ireq+1 
6900           call MPI_Isend(zapas(1,1,ii),nn*max_dim,MPI_DOUBLE_PRECISION,
6901      &      iproc,CorrelType1,FG_COMM,req(ireq),IERR)
6902 c          write (iout,*) "ireq,req",ireq,req(ireq)
6903 c          do i=1,nn
6904 c            write(iout,'(2f5.0,4f10.5)')(zapas(j,i,ii),j=1,5)
6905 c          enddo
6906         endif  
6907       enddo
6908 c      write (iout,*) "number of requests (contacts)",ireq
6909 c      write (iout,*) "req",(req(i),i=1,4)
6910 c      call flush(iout)
6911       if (ireq.gt.0) 
6912      & call MPI_Waitall(ireq,req,status_array,ierr)
6913       do iii=1,ntask_cont_from
6914         iproc=itask_cont_from(iii)
6915         nn=ncont_recv(iii)
6916         if (lprn) then
6917         write (iout,*) "Received",nn," contacts from processor",iproc,
6918      &   " of CONT_FROM_COMM group"
6919         call flush(iout)
6920         do i=1,nn
6921           write(iout,'(2f5.0,4f10.5)')(zapas_recv(j,i,iii),j=1,5)
6922         enddo
6923         call flush(iout)
6924         endif
6925         do i=1,nn
6926           ii=zapas_recv(1,i,iii)
6927 c Flag the received contacts to prevent double-counting
6928           jj=-zapas_recv(2,i,iii)
6929 c          write (iout,*) "iii",iii," i",i," ii",ii," jj",jj
6930 c          call flush(iout)
6931           nnn=num_cont_hb(ii)+1
6932           num_cont_hb(ii)=nnn
6933           jcont_hb(nnn,ii)=jj
6934           facont_hb(nnn,ii)=zapas_recv(3,i,iii)
6935           ees0p(nnn,ii)=zapas_recv(4,i,iii)
6936           ees0m(nnn,ii)=zapas_recv(5,i,iii)
6937           gacont_hbr(1,nnn,ii)=zapas_recv(6,i,iii)
6938           gacont_hbr(2,nnn,ii)=zapas_recv(7,i,iii)
6939           gacont_hbr(3,nnn,ii)=zapas_recv(8,i,iii)
6940           gacontm_hb1(1,nnn,ii)=zapas_recv(9,i,iii)
6941           gacontm_hb1(2,nnn,ii)=zapas_recv(10,i,iii)
6942           gacontm_hb1(3,nnn,ii)=zapas_recv(11,i,iii)
6943           gacontp_hb1(1,nnn,ii)=zapas_recv(12,i,iii)
6944           gacontp_hb1(2,nnn,ii)=zapas_recv(13,i,iii)
6945           gacontp_hb1(3,nnn,ii)=zapas_recv(14,i,iii)
6946           gacontm_hb2(1,nnn,ii)=zapas_recv(15,i,iii)
6947           gacontm_hb2(2,nnn,ii)=zapas_recv(16,i,iii)
6948           gacontm_hb2(3,nnn,ii)=zapas_recv(17,i,iii)
6949           gacontp_hb2(1,nnn,ii)=zapas_recv(18,i,iii)
6950           gacontp_hb2(2,nnn,ii)=zapas_recv(19,i,iii)
6951           gacontp_hb2(3,nnn,ii)=zapas_recv(20,i,iii)
6952           gacontm_hb3(1,nnn,ii)=zapas_recv(21,i,iii)
6953           gacontm_hb3(2,nnn,ii)=zapas_recv(22,i,iii)
6954           gacontm_hb3(3,nnn,ii)=zapas_recv(23,i,iii)
6955           gacontp_hb3(1,nnn,ii)=zapas_recv(24,i,iii)
6956           gacontp_hb3(2,nnn,ii)=zapas_recv(25,i,iii)
6957           gacontp_hb3(3,nnn,ii)=zapas_recv(26,i,iii)
6958         enddo
6959       enddo
6960       call flush(iout)
6961       if (lprn) then
6962         write (iout,'(a)') 'Contact function values after receive:'
6963         do i=nnt,nct-2
6964           write (iout,'(2i3,50(1x,i3,f5.2))') 
6965      &    i,num_cont_hb(i),(jcont_hb(j,i),facont_hb(j,i),
6966      &    j=1,num_cont_hb(i))
6967         enddo
6968         call flush(iout)
6969       endif
6970    30 continue
6971 #endif
6972       if (lprn) then
6973         write (iout,'(a)') 'Contact function values:'
6974         do i=nnt,nct-2
6975           write (iout,'(2i3,50(1x,i3,f5.2))') 
6976      &    i,num_cont_hb(i),(jcont_hb(j,i),facont_hb(j,i),
6977      &    j=1,num_cont_hb(i))
6978         enddo
6979       endif
6980       ecorr=0.0D0
6981 C Remove the loop below after debugging !!!
6982       do i=nnt,nct
6983         do j=1,3
6984           gradcorr(j,i)=0.0D0
6985           gradxorr(j,i)=0.0D0
6986         enddo
6987       enddo
6988 C Calculate the local-electrostatic correlation terms
6989       do i=min0(iatel_s,iturn4_start),max0(iatel_e,iturn3_end)
6990         i1=i+1
6991         num_conti=num_cont_hb(i)
6992         num_conti1=num_cont_hb(i+1)
6993         do jj=1,num_conti
6994           j=jcont_hb(jj,i)
6995           jp=iabs(j)
6996           do kk=1,num_conti1
6997             j1=jcont_hb(kk,i1)
6998             jp1=iabs(j1)
6999 c            write (iout,*) 'i=',i,' j=',j,' i1=',i1,' j1=',j1,
7000 c     &         ' jj=',jj,' kk=',kk
7001             if ((j.gt.0 .and. j1.gt.0 .or. j.gt.0 .and. j1.lt.0 
7002      &          .or. j.lt.0 .and. j1.gt.0) .and.
7003      &         (jp1.eq.jp+1 .or. jp1.eq.jp-1)) then
7004 C Contacts I-J and (I+1)-(J+1) or (I+1)-(J-1) occur simultaneously. 
7005 C The system gains extra energy.
7006               ecorr=ecorr+ehbcorr(i,jp,i+1,jp1,jj,kk,0.72D0,0.32D0)
7007               if (energy_dec) write (iout,'(a6,2i5,0pf7.3)')
7008      &            'ecorrh',i,j,ehbcorr(i,j,i+1,j1,jj,kk,0.72D0,0.32D0)
7009               n_corr=n_corr+1
7010             else if (j1.eq.j) then
7011 C Contacts I-J and I-(J+1) occur simultaneously. 
7012 C The system loses extra energy.
7013 c             ecorr=ecorr+ehbcorr(i,j,i+1,j,jj,kk,0.60D0,-0.40D0) 
7014             endif
7015           enddo ! kk
7016           do kk=1,num_conti
7017             j1=jcont_hb(kk,i)
7018 c           write (iout,*) 'i=',i,' j=',j,' i1=',i1,' j1=',j1,
7019 c    &         ' jj=',jj,' kk=',kk
7020             if (j1.eq.j+1) then
7021 C Contacts I-J and (I+1)-J occur simultaneously. 
7022 C The system loses extra energy.
7023 c             ecorr=ecorr+ehbcorr(i,j,i,j+1,jj,kk,0.60D0,-0.40D0)
7024             endif ! j1==j+1
7025           enddo ! kk
7026         enddo ! jj
7027       enddo ! i
7028       return
7029       end
7030 c------------------------------------------------------------------------------
7031       subroutine add_hb_contact(ii,jj,itask)
7032       implicit real*8 (a-h,o-z)
7033       include "DIMENSIONS"
7034       include "COMMON.IOUNITS"
7035       integer max_cont
7036       integer max_dim
7037       parameter (max_cont=maxconts)
7038       parameter (max_dim=26)
7039       include "COMMON.CONTACTS"
7040       double precision zapas(max_dim,maxconts,max_fg_procs),
7041      &  zapas_recv(max_dim,maxconts,max_fg_procs)
7042       common /przechowalnia/ zapas
7043       integer i,j,ii,jj,iproc,itask(4),nn
7044 c      write (iout,*) "itask",itask
7045       do i=1,2
7046         iproc=itask(i)
7047         if (iproc.gt.0) then
7048           do j=1,num_cont_hb(ii)
7049             jjc=jcont_hb(j,ii)
7050 c            write (iout,*) "i",ii," j",jj," jjc",jjc
7051             if (jjc.eq.jj) then
7052               ncont_sent(iproc)=ncont_sent(iproc)+1
7053               nn=ncont_sent(iproc)
7054               zapas(1,nn,iproc)=ii
7055               zapas(2,nn,iproc)=jjc
7056               zapas(3,nn,iproc)=facont_hb(j,ii)
7057               zapas(4,nn,iproc)=ees0p(j,ii)
7058               zapas(5,nn,iproc)=ees0m(j,ii)
7059               zapas(6,nn,iproc)=gacont_hbr(1,j,ii)
7060               zapas(7,nn,iproc)=gacont_hbr(2,j,ii)
7061               zapas(8,nn,iproc)=gacont_hbr(3,j,ii)
7062               zapas(9,nn,iproc)=gacontm_hb1(1,j,ii)
7063               zapas(10,nn,iproc)=gacontm_hb1(2,j,ii)
7064               zapas(11,nn,iproc)=gacontm_hb1(3,j,ii)
7065               zapas(12,nn,iproc)=gacontp_hb1(1,j,ii)
7066               zapas(13,nn,iproc)=gacontp_hb1(2,j,ii)
7067               zapas(14,nn,iproc)=gacontp_hb1(3,j,ii)
7068               zapas(15,nn,iproc)=gacontm_hb2(1,j,ii)
7069               zapas(16,nn,iproc)=gacontm_hb2(2,j,ii)
7070               zapas(17,nn,iproc)=gacontm_hb2(3,j,ii)
7071               zapas(18,nn,iproc)=gacontp_hb2(1,j,ii)
7072               zapas(19,nn,iproc)=gacontp_hb2(2,j,ii)
7073               zapas(20,nn,iproc)=gacontp_hb2(3,j,ii)
7074               zapas(21,nn,iproc)=gacontm_hb3(1,j,ii)
7075               zapas(22,nn,iproc)=gacontm_hb3(2,j,ii)
7076               zapas(23,nn,iproc)=gacontm_hb3(3,j,ii)
7077               zapas(24,nn,iproc)=gacontp_hb3(1,j,ii)
7078               zapas(25,nn,iproc)=gacontp_hb3(2,j,ii)
7079               zapas(26,nn,iproc)=gacontp_hb3(3,j,ii)
7080               exit
7081             endif
7082           enddo
7083         endif
7084       enddo
7085       return
7086       end
7087 c------------------------------------------------------------------------------
7088       subroutine multibody_eello(ecorr,ecorr5,ecorr6,eturn6,n_corr,
7089      &  n_corr1)
7090 C This subroutine calculates multi-body contributions to hydrogen-bonding 
7091       implicit real*8 (a-h,o-z)
7092       include 'DIMENSIONS'
7093       include 'COMMON.IOUNITS'
7094 #ifdef MPI
7095       include "mpif.h"
7096       parameter (max_cont=maxconts)
7097       parameter (max_dim=70)
7098       integer source,CorrelType,CorrelID,CorrelType1,CorrelID1,Error
7099       double precision zapas(max_dim,maxconts,max_fg_procs),
7100      &  zapas_recv(max_dim,maxconts,max_fg_procs)
7101       common /przechowalnia/ zapas
7102       integer status(MPI_STATUS_SIZE),req(maxconts*2),
7103      &  status_array(MPI_STATUS_SIZE,maxconts*2)
7104 #endif
7105       include 'COMMON.SETUP'
7106       include 'COMMON.FFIELD'
7107       include 'COMMON.DERIV'
7108       include 'COMMON.LOCAL'
7109       include 'COMMON.INTERACT'
7110       include 'COMMON.CONTACTS'
7111       include 'COMMON.CHAIN'
7112       include 'COMMON.CONTROL'
7113       double precision gx(3),gx1(3)
7114       integer num_cont_hb_old(maxres)
7115       logical lprn,ldone
7116       double precision eello4,eello5,eelo6,eello_turn6
7117       external eello4,eello5,eello6,eello_turn6
7118 C Set lprn=.true. for debugging
7119       lprn=.false.
7120       eturn6=0.0d0
7121 #ifdef MPI
7122       do i=1,nres
7123         num_cont_hb_old(i)=num_cont_hb(i)
7124       enddo
7125       n_corr=0
7126       n_corr1=0
7127       if (nfgtasks.le.1) goto 30
7128       if (lprn) then
7129         write (iout,'(a)') 'Contact function values before RECEIVE:'
7130         do i=nnt,nct-2
7131           write (iout,'(2i3,50(1x,i2,f5.2))') 
7132      &    i,num_cont_hb(i),(jcont_hb(j,i),facont_hb(j,i),
7133      &    j=1,num_cont_hb(i))
7134         enddo
7135       endif
7136       call flush(iout)
7137       do i=1,ntask_cont_from
7138         ncont_recv(i)=0
7139       enddo
7140       do i=1,ntask_cont_to
7141         ncont_sent(i)=0
7142       enddo
7143 c      write (iout,*) "ntask_cont_from",ntask_cont_from," ntask_cont_to",
7144 c     & ntask_cont_to
7145 C Make the list of contacts to send to send to other procesors
7146       do i=iturn3_start,iturn3_end
7147 c        write (iout,*) "make contact list turn3",i," num_cont",
7148 c     &    num_cont_hb(i)
7149         call add_hb_contact_eello(i,i+2,iturn3_sent_local(1,i))
7150       enddo
7151       do i=iturn4_start,iturn4_end
7152 c        write (iout,*) "make contact list turn4",i," num_cont",
7153 c     &   num_cont_hb(i)
7154         call add_hb_contact_eello(i,i+3,iturn4_sent_local(1,i))
7155       enddo
7156       do ii=1,nat_sent
7157         i=iat_sent(ii)
7158 c        write (iout,*) "make contact list longrange",i,ii," num_cont",
7159 c     &    num_cont_hb(i)
7160         do j=1,num_cont_hb(i)
7161         do k=1,4
7162           jjc=jcont_hb(j,i)
7163           iproc=iint_sent_local(k,jjc,ii)
7164 c          write (iout,*) "i",i," j",j," k",k," jjc",jjc," iproc",iproc
7165           if (iproc.ne.0) then
7166             ncont_sent(iproc)=ncont_sent(iproc)+1
7167             nn=ncont_sent(iproc)
7168             zapas(1,nn,iproc)=i
7169             zapas(2,nn,iproc)=jjc
7170             zapas(3,nn,iproc)=d_cont(j,i)
7171             ind=3
7172             do kk=1,3
7173               ind=ind+1
7174               zapas(ind,nn,iproc)=grij_hb_cont(kk,j,i)
7175             enddo
7176             do kk=1,2
7177               do ll=1,2
7178                 ind=ind+1
7179                 zapas(ind,nn,iproc)=a_chuj(ll,kk,j,i)
7180               enddo
7181             enddo
7182             do jj=1,5
7183               do kk=1,3
7184                 do ll=1,2
7185                   do mm=1,2
7186                     ind=ind+1
7187                     zapas(ind,nn,iproc)=a_chuj_der(mm,ll,kk,jj,j,i)
7188                   enddo
7189                 enddo
7190               enddo
7191             enddo
7192           endif
7193         enddo
7194         enddo
7195       enddo
7196       if (lprn) then
7197       write (iout,*) 
7198      &  "Numbers of contacts to be sent to other processors",
7199      &  (ncont_sent(i),i=1,ntask_cont_to)
7200       write (iout,*) "Contacts sent"
7201       do ii=1,ntask_cont_to
7202         nn=ncont_sent(ii)
7203         iproc=itask_cont_to(ii)
7204         write (iout,*) nn," contacts to processor",iproc,
7205      &   " of CONT_TO_COMM group"
7206         do i=1,nn
7207           write(iout,'(2f5.0,10f10.5)')(zapas(j,i,ii),j=1,10)
7208         enddo
7209       enddo
7210       call flush(iout)
7211       endif
7212       CorrelType=477
7213       CorrelID=fg_rank+1
7214       CorrelType1=478
7215       CorrelID1=nfgtasks+fg_rank+1
7216       ireq=0
7217 C Receive the numbers of needed contacts from other processors 
7218       do ii=1,ntask_cont_from
7219         iproc=itask_cont_from(ii)
7220         ireq=ireq+1
7221         call MPI_Irecv(ncont_recv(ii),1,MPI_INTEGER,iproc,CorrelType,
7222      &    FG_COMM,req(ireq),IERR)
7223       enddo
7224 c      write (iout,*) "IRECV ended"
7225 c      call flush(iout)
7226 C Send the number of contacts needed by other processors
7227       do ii=1,ntask_cont_to
7228         iproc=itask_cont_to(ii)
7229         ireq=ireq+1
7230         call MPI_Isend(ncont_sent(ii),1,MPI_INTEGER,iproc,CorrelType,
7231      &    FG_COMM,req(ireq),IERR)
7232       enddo
7233 c      write (iout,*) "ISEND ended"
7234 c      write (iout,*) "number of requests (nn)",ireq
7235       call flush(iout)
7236       if (ireq.gt.0) 
7237      &  call MPI_Waitall(ireq,req,status_array,ierr)
7238 c      write (iout,*) 
7239 c     &  "Numbers of contacts to be received from other processors",
7240 c     &  (ncont_recv(i),i=1,ntask_cont_from)
7241 c      call flush(iout)
7242 C Receive contacts
7243       ireq=0
7244       do ii=1,ntask_cont_from
7245         iproc=itask_cont_from(ii)
7246         nn=ncont_recv(ii)
7247 c        write (iout,*) "Receiving",nn," contacts from processor",iproc,
7248 c     &   " of CONT_TO_COMM group"
7249         call flush(iout)
7250         if (nn.gt.0) then
7251           ireq=ireq+1
7252           call MPI_Irecv(zapas_recv(1,1,ii),nn*max_dim,
7253      &    MPI_DOUBLE_PRECISION,iproc,CorrelType1,FG_COMM,req(ireq),IERR)
7254 c          write (iout,*) "ireq,req",ireq,req(ireq)
7255         endif
7256       enddo
7257 C Send the contacts to processors that need them
7258       do ii=1,ntask_cont_to
7259         iproc=itask_cont_to(ii)
7260         nn=ncont_sent(ii)
7261 c        write (iout,*) nn," contacts to processor",iproc,
7262 c     &   " of CONT_TO_COMM group"
7263         if (nn.gt.0) then
7264           ireq=ireq+1 
7265           call MPI_Isend(zapas(1,1,ii),nn*max_dim,MPI_DOUBLE_PRECISION,
7266      &      iproc,CorrelType1,FG_COMM,req(ireq),IERR)
7267 c          write (iout,*) "ireq,req",ireq,req(ireq)
7268 c          do i=1,nn
7269 c            write(iout,'(2f5.0,4f10.5)')(zapas(j,i,ii),j=1,5)
7270 c          enddo
7271         endif  
7272       enddo
7273 c      write (iout,*) "number of requests (contacts)",ireq
7274 c      write (iout,*) "req",(req(i),i=1,4)
7275 c      call flush(iout)
7276       if (ireq.gt.0) 
7277      & call MPI_Waitall(ireq,req,status_array,ierr)
7278       do iii=1,ntask_cont_from
7279         iproc=itask_cont_from(iii)
7280         nn=ncont_recv(iii)
7281         if (lprn) then
7282         write (iout,*) "Received",nn," contacts from processor",iproc,
7283      &   " of CONT_FROM_COMM group"
7284         call flush(iout)
7285         do i=1,nn
7286           write(iout,'(2f5.0,10f10.5)')(zapas_recv(j,i,iii),j=1,10)
7287         enddo
7288         call flush(iout)
7289         endif
7290         do i=1,nn
7291           ii=zapas_recv(1,i,iii)
7292 c Flag the received contacts to prevent double-counting
7293           jj=-zapas_recv(2,i,iii)
7294 c          write (iout,*) "iii",iii," i",i," ii",ii," jj",jj
7295 c          call flush(iout)
7296           nnn=num_cont_hb(ii)+1
7297           num_cont_hb(ii)=nnn
7298           jcont_hb(nnn,ii)=jj
7299           d_cont(nnn,ii)=zapas_recv(3,i,iii)
7300           ind=3
7301           do kk=1,3
7302             ind=ind+1
7303             grij_hb_cont(kk,nnn,ii)=zapas_recv(ind,i,iii)
7304           enddo
7305           do kk=1,2
7306             do ll=1,2
7307               ind=ind+1
7308               a_chuj(ll,kk,nnn,ii)=zapas_recv(ind,i,iii)
7309             enddo
7310           enddo
7311           do jj=1,5
7312             do kk=1,3
7313               do ll=1,2
7314                 do mm=1,2
7315                   ind=ind+1
7316                   a_chuj_der(mm,ll,kk,jj,nnn,ii)=zapas_recv(ind,i,iii)
7317                 enddo
7318               enddo
7319             enddo
7320           enddo
7321         enddo
7322       enddo
7323       call flush(iout)
7324       if (lprn) then
7325         write (iout,'(a)') 'Contact function values after receive:'
7326         do i=nnt,nct-2
7327           write (iout,'(2i3,50(1x,i3,5f6.3))') 
7328      &    i,num_cont_hb(i),(jcont_hb(j,i),d_cont(j,i),
7329      &    ((a_chuj(ll,kk,j,i),ll=1,2),kk=1,2),j=1,num_cont_hb(i))
7330         enddo
7331         call flush(iout)
7332       endif
7333    30 continue
7334 #endif
7335       if (lprn) then
7336         write (iout,'(a)') 'Contact function values:'
7337         do i=nnt,nct-2
7338           write (iout,'(2i3,50(1x,i2,5f6.3))') 
7339      &    i,num_cont_hb(i),(jcont_hb(j,i),d_cont(j,i),
7340      &    ((a_chuj(ll,kk,j,i),ll=1,2),kk=1,2),j=1,num_cont_hb(i))
7341         enddo
7342       endif
7343       ecorr=0.0D0
7344       ecorr5=0.0d0
7345       ecorr6=0.0d0
7346 C Remove the loop below after debugging !!!
7347       do i=nnt,nct
7348         do j=1,3
7349           gradcorr(j,i)=0.0D0
7350           gradxorr(j,i)=0.0D0
7351         enddo
7352       enddo
7353 C Calculate the dipole-dipole interaction energies
7354       if (wcorr6.gt.0.0d0 .or. wturn6.gt.0.0d0) then
7355       do i=iatel_s,iatel_e+1
7356         num_conti=num_cont_hb(i)
7357         do jj=1,num_conti
7358           j=jcont_hb(jj,i)
7359 #ifdef MOMENT
7360           call dipole(i,j,jj)
7361 #endif
7362         enddo
7363       enddo
7364       endif
7365 C Calculate the local-electrostatic correlation terms
7366 c                write (iout,*) "gradcorr5 in eello5 before loop"
7367 c                do iii=1,nres
7368 c                  write (iout,'(i5,3f10.5)') 
7369 c     &             iii,(gradcorr5(jjj,iii),jjj=1,3)
7370 c                enddo
7371       do i=min0(iatel_s,iturn4_start),max0(iatel_e+1,iturn3_end+1)
7372 c        write (iout,*) "corr loop i",i
7373         i1=i+1
7374         num_conti=num_cont_hb(i)
7375         num_conti1=num_cont_hb(i+1)
7376         do jj=1,num_conti
7377           j=jcont_hb(jj,i)
7378           jp=iabs(j)
7379           do kk=1,num_conti1
7380             j1=jcont_hb(kk,i1)
7381             jp1=iabs(j1)
7382 c            write (iout,*) 'i=',i,' j=',j,' i1=',i1,' j1=',j1,
7383 c     &         ' jj=',jj,' kk=',kk
7384 c            if (j1.eq.j+1 .or. j1.eq.j-1) then
7385             if ((j.gt.0 .and. j1.gt.0 .or. j.gt.0 .and. j1.lt.0 
7386      &          .or. j.lt.0 .and. j1.gt.0) .and.
7387      &         (jp1.eq.jp+1 .or. jp1.eq.jp-1)) then
7388 C Contacts I-J and (I+1)-(J+1) or (I+1)-(J-1) occur simultaneously. 
7389 C The system gains extra energy.
7390               n_corr=n_corr+1
7391               sqd1=dsqrt(d_cont(jj,i))
7392               sqd2=dsqrt(d_cont(kk,i1))
7393               sred_geom = sqd1*sqd2
7394               IF (sred_geom.lt.cutoff_corr) THEN
7395                 call gcont(sred_geom,r0_corr,1.0D0,delt_corr,
7396      &            ekont,fprimcont)
7397 cd               write (iout,*) 'i=',i,' j=',jp,' i1=',i1,' j1=',jp1,
7398 cd     &         ' jj=',jj,' kk=',kk
7399                 fac_prim1=0.5d0*sqd2/sqd1*fprimcont
7400                 fac_prim2=0.5d0*sqd1/sqd2*fprimcont
7401                 do l=1,3
7402                   g_contij(l,1)=fac_prim1*grij_hb_cont(l,jj,i)
7403                   g_contij(l,2)=fac_prim2*grij_hb_cont(l,kk,i1)
7404                 enddo
7405                 n_corr1=n_corr1+1
7406 cd               write (iout,*) 'sred_geom=',sred_geom,
7407 cd     &          ' ekont=',ekont,' fprim=',fprimcont,
7408 cd     &          ' fac_prim1',fac_prim1,' fac_prim2',fac_prim2
7409 cd               write (iout,*) "g_contij",g_contij
7410 cd               write (iout,*) "grij_hb_cont i",grij_hb_cont(:,jj,i)
7411 cd               write (iout,*) "grij_hb_cont i1",grij_hb_cont(:,jj,i1)
7412                 call calc_eello(i,jp,i+1,jp1,jj,kk)
7413                 if (wcorr4.gt.0.0d0) 
7414      &            ecorr=ecorr+eello4(i,jp,i+1,jp1,jj,kk)
7415                   if (energy_dec.and.wcorr4.gt.0.0d0) 
7416      1                 write (iout,'(a6,4i5,0pf7.3)')
7417      2                'ecorr4',i,j,i+1,j1,eello4(i,jp,i+1,jp1,jj,kk)
7418 c                write (iout,*) "gradcorr5 before eello5"
7419 c                do iii=1,nres
7420 c                  write (iout,'(i5,3f10.5)') 
7421 c     &             iii,(gradcorr5(jjj,iii),jjj=1,3)
7422 c                enddo
7423                 if (wcorr5.gt.0.0d0)
7424      &            ecorr5=ecorr5+eello5(i,jp,i+1,jp1,jj,kk)
7425 c                write (iout,*) "gradcorr5 after eello5"
7426 c                do iii=1,nres
7427 c                  write (iout,'(i5,3f10.5)') 
7428 c     &             iii,(gradcorr5(jjj,iii),jjj=1,3)
7429 c                enddo
7430                   if (energy_dec.and.wcorr5.gt.0.0d0) 
7431      1                 write (iout,'(a6,4i5,0pf7.3)')
7432      2                'ecorr5',i,j,i+1,j1,eello5(i,jp,i+1,jp1,jj,kk)
7433 cd                write(2,*)'wcorr6',wcorr6,' wturn6',wturn6
7434 cd                write(2,*)'ijkl',i,jp,i+1,jp1 
7435                 if (wcorr6.gt.0.0d0 .and. (jp.ne.i+4 .or. jp1.ne.i+3
7436      &               .or. wturn6.eq.0.0d0))then
7437 cd                  write (iout,*) '******ecorr6: i,j,i+1,j1',i,j,i+1,j1
7438                   ecorr6=ecorr6+eello6(i,jp,i+1,jp1,jj,kk)
7439                   if (energy_dec) write (iout,'(a6,4i5,0pf7.3)')
7440      1                'ecorr6',i,j,i+1,j1,eello6(i,jp,i+1,jp1,jj,kk)
7441 cd                write (iout,*) 'ecorr',ecorr,' ecorr5=',ecorr5,
7442 cd     &            'ecorr6=',ecorr6
7443 cd                write (iout,'(4e15.5)') sred_geom,
7444 cd     &          dabs(eello4(i,jp,i+1,jp1,jj,kk)),
7445 cd     &          dabs(eello5(i,jp,i+1,jp1,jj,kk)),
7446 cd     &          dabs(eello6(i,jp,i+1,jp1,jj,kk))
7447                 else if (wturn6.gt.0.0d0
7448      &            .and. (jp.eq.i+4 .and. jp1.eq.i+3)) then
7449 cd                  write (iout,*) '******eturn6: i,j,i+1,j1',i,jip,i+1,jp1
7450                   eturn6=eturn6+eello_turn6(i,jj,kk)
7451                   if (energy_dec) write (iout,'(a6,4i5,0pf7.3)')
7452      1                 'eturn6',i,j,i+1,j1,eello_turn6(i,jj,kk)
7453 cd                  write (2,*) 'multibody_eello:eturn6',eturn6
7454                 endif
7455               ENDIF
7456 1111          continue
7457             endif
7458           enddo ! kk
7459         enddo ! jj
7460       enddo ! i
7461       do i=1,nres
7462         num_cont_hb(i)=num_cont_hb_old(i)
7463       enddo
7464 c                write (iout,*) "gradcorr5 in eello5"
7465 c                do iii=1,nres
7466 c                  write (iout,'(i5,3f10.5)') 
7467 c     &             iii,(gradcorr5(jjj,iii),jjj=1,3)
7468 c                enddo
7469       return
7470       end
7471 c------------------------------------------------------------------------------
7472       subroutine add_hb_contact_eello(ii,jj,itask)
7473       implicit real*8 (a-h,o-z)
7474       include "DIMENSIONS"
7475       include "COMMON.IOUNITS"
7476       integer max_cont
7477       integer max_dim
7478       parameter (max_cont=maxconts)
7479       parameter (max_dim=70)
7480       include "COMMON.CONTACTS"
7481       double precision zapas(max_dim,maxconts,max_fg_procs),
7482      &  zapas_recv(max_dim,maxconts,max_fg_procs)
7483       common /przechowalnia/ zapas
7484       integer i,j,ii,jj,iproc,itask(4),nn
7485 c      write (iout,*) "itask",itask
7486       do i=1,2
7487         iproc=itask(i)
7488         if (iproc.gt.0) then
7489           do j=1,num_cont_hb(ii)
7490             jjc=jcont_hb(j,ii)
7491 c            write (iout,*) "send turns i",ii," j",jj," jjc",jjc
7492             if (jjc.eq.jj) then
7493               ncont_sent(iproc)=ncont_sent(iproc)+1
7494               nn=ncont_sent(iproc)
7495               zapas(1,nn,iproc)=ii
7496               zapas(2,nn,iproc)=jjc
7497               zapas(3,nn,iproc)=d_cont(j,ii)
7498               ind=3
7499               do kk=1,3
7500                 ind=ind+1
7501                 zapas(ind,nn,iproc)=grij_hb_cont(kk,j,ii)
7502               enddo
7503               do kk=1,2
7504                 do ll=1,2
7505                   ind=ind+1
7506                   zapas(ind,nn,iproc)=a_chuj(ll,kk,j,ii)
7507                 enddo
7508               enddo
7509               do jj=1,5
7510                 do kk=1,3
7511                   do ll=1,2
7512                     do mm=1,2
7513                       ind=ind+1
7514                       zapas(ind,nn,iproc)=a_chuj_der(mm,ll,kk,jj,j,ii)
7515                     enddo
7516                   enddo
7517                 enddo
7518               enddo
7519               exit
7520             endif
7521           enddo
7522         endif
7523       enddo
7524       return
7525       end
7526 c------------------------------------------------------------------------------
7527       double precision function ehbcorr(i,j,k,l,jj,kk,coeffp,coeffm)
7528       implicit real*8 (a-h,o-z)
7529       include 'DIMENSIONS'
7530       include 'COMMON.IOUNITS'
7531       include 'COMMON.DERIV'
7532       include 'COMMON.INTERACT'
7533       include 'COMMON.CONTACTS'
7534       double precision gx(3),gx1(3)
7535       logical lprn
7536       lprn=.false.
7537       eij=facont_hb(jj,i)
7538       ekl=facont_hb(kk,k)
7539       ees0pij=ees0p(jj,i)
7540       ees0pkl=ees0p(kk,k)
7541       ees0mij=ees0m(jj,i)
7542       ees0mkl=ees0m(kk,k)
7543       ekont=eij*ekl
7544       ees=-(coeffp*ees0pij*ees0pkl+coeffm*ees0mij*ees0mkl)
7545 cd    ees=-(coeffp*ees0pkl+coeffm*ees0mkl)
7546 C Following 4 lines for diagnostics.
7547 cd    ees0pkl=0.0D0
7548 cd    ees0pij=1.0D0
7549 cd    ees0mkl=0.0D0
7550 cd    ees0mij=1.0D0
7551 c      write (iout,'(2(a,2i3,a,f10.5,a,2f10.5),a,f10.5,a,$)')
7552 c     & 'Contacts ',i,j,
7553 c     & ' eij',eij,' eesij',ees0pij,ees0mij,' and ',k,l
7554 c     & ,' fcont ',ekl,' eeskl',ees0pkl,ees0mkl,' energy=',ekont*ees,
7555 c     & 'gradcorr_long'
7556 C Calculate the multi-body contribution to energy.
7557 c      ecorr=ecorr+ekont*ees
7558 C Calculate multi-body contributions to the gradient.
7559       coeffpees0pij=coeffp*ees0pij
7560       coeffmees0mij=coeffm*ees0mij
7561       coeffpees0pkl=coeffp*ees0pkl
7562       coeffmees0mkl=coeffm*ees0mkl
7563       do ll=1,3
7564 cgrad        ghalfi=ees*ekl*gacont_hbr(ll,jj,i)
7565         gradcorr(ll,i)=gradcorr(ll,i)!+0.5d0*ghalfi
7566      &  -ekont*(coeffpees0pkl*gacontp_hb1(ll,jj,i)+
7567      &  coeffmees0mkl*gacontm_hb1(ll,jj,i))
7568         gradcorr(ll,j)=gradcorr(ll,j)!+0.5d0*ghalfi
7569      &  -ekont*(coeffpees0pkl*gacontp_hb2(ll,jj,i)+
7570      &  coeffmees0mkl*gacontm_hb2(ll,jj,i))
7571 cgrad        ghalfk=ees*eij*gacont_hbr(ll,kk,k)
7572         gradcorr(ll,k)=gradcorr(ll,k)!+0.5d0*ghalfk
7573      &  -ekont*(coeffpees0pij*gacontp_hb1(ll,kk,k)+
7574      &  coeffmees0mij*gacontm_hb1(ll,kk,k))
7575         gradcorr(ll,l)=gradcorr(ll,l)!+0.5d0*ghalfk
7576      &  -ekont*(coeffpees0pij*gacontp_hb2(ll,kk,k)+
7577      &  coeffmees0mij*gacontm_hb2(ll,kk,k))
7578         gradlongij=ees*ekl*gacont_hbr(ll,jj,i)-
7579      &     ekont*(coeffpees0pkl*gacontp_hb3(ll,jj,i)+
7580      &     coeffmees0mkl*gacontm_hb3(ll,jj,i))
7581         gradcorr_long(ll,j)=gradcorr_long(ll,j)+gradlongij
7582         gradcorr_long(ll,i)=gradcorr_long(ll,i)-gradlongij
7583         gradlongkl=ees*eij*gacont_hbr(ll,kk,k)-
7584      &     ekont*(coeffpees0pij*gacontp_hb3(ll,kk,k)+
7585      &     coeffmees0mij*gacontm_hb3(ll,kk,k))
7586         gradcorr_long(ll,l)=gradcorr_long(ll,l)+gradlongkl
7587         gradcorr_long(ll,k)=gradcorr_long(ll,k)-gradlongkl
7588 c        write (iout,'(2f10.5,2x,$)') gradlongij,gradlongkl
7589       enddo
7590 c      write (iout,*)
7591 cgrad      do m=i+1,j-1
7592 cgrad        do ll=1,3
7593 cgrad          gradcorr(ll,m)=gradcorr(ll,m)+
7594 cgrad     &     ees*ekl*gacont_hbr(ll,jj,i)-
7595 cgrad     &     ekont*(coeffp*ees0pkl*gacontp_hb3(ll,jj,i)+
7596 cgrad     &     coeffm*ees0mkl*gacontm_hb3(ll,jj,i))
7597 cgrad        enddo
7598 cgrad      enddo
7599 cgrad      do m=k+1,l-1
7600 cgrad        do ll=1,3
7601 cgrad          gradcorr(ll,m)=gradcorr(ll,m)+
7602 cgrad     &     ees*eij*gacont_hbr(ll,kk,k)-
7603 cgrad     &     ekont*(coeffp*ees0pij*gacontp_hb3(ll,kk,k)+
7604 cgrad     &     coeffm*ees0mij*gacontm_hb3(ll,kk,k))
7605 cgrad        enddo
7606 cgrad      enddo 
7607 c      write (iout,*) "ehbcorr",ekont*ees
7608       ehbcorr=ekont*ees
7609       return
7610       end
7611 #ifdef MOMENT
7612 C---------------------------------------------------------------------------
7613       subroutine dipole(i,j,jj)
7614       implicit real*8 (a-h,o-z)
7615       include 'DIMENSIONS'
7616       include 'COMMON.IOUNITS'
7617       include 'COMMON.CHAIN'
7618       include 'COMMON.FFIELD'
7619       include 'COMMON.DERIV'
7620       include 'COMMON.INTERACT'
7621       include 'COMMON.CONTACTS'
7622       include 'COMMON.TORSION'
7623       include 'COMMON.VAR'
7624       include 'COMMON.GEO'
7625       dimension dipi(2,2),dipj(2,2),dipderi(2),dipderj(2),auxvec(2),
7626      &  auxmat(2,2)
7627       iti1 = itortyp(itype(i+1))
7628       if (j.lt.nres-1) then
7629         itj1 = itortyp(itype(j+1))
7630       else
7631         itj1=ntortyp+1
7632       endif
7633       do iii=1,2
7634         dipi(iii,1)=Ub2(iii,i)
7635         dipderi(iii)=Ub2der(iii,i)
7636         dipi(iii,2)=b1(iii,iti1)
7637         dipj(iii,1)=Ub2(iii,j)
7638         dipderj(iii)=Ub2der(iii,j)
7639         dipj(iii,2)=b1(iii,itj1)
7640       enddo
7641       kkk=0
7642       do iii=1,2
7643         call matvec2(a_chuj(1,1,jj,i),dipj(1,iii),auxvec(1)) 
7644         do jjj=1,2
7645           kkk=kkk+1
7646           dip(kkk,jj,i)=scalar2(dipi(1,jjj),auxvec(1))
7647         enddo
7648       enddo
7649       do kkk=1,5
7650         do lll=1,3
7651           mmm=0
7652           do iii=1,2
7653             call matvec2(a_chuj_der(1,1,lll,kkk,jj,i),dipj(1,iii),
7654      &        auxvec(1))
7655             do jjj=1,2
7656               mmm=mmm+1
7657               dipderx(lll,kkk,mmm,jj,i)=scalar2(dipi(1,jjj),auxvec(1))
7658             enddo
7659           enddo
7660         enddo
7661       enddo
7662       call transpose2(a_chuj(1,1,jj,i),auxmat(1,1))
7663       call matvec2(auxmat(1,1),dipderi(1),auxvec(1))
7664       do iii=1,2
7665         dipderg(iii,jj,i)=scalar2(auxvec(1),dipj(1,iii))
7666       enddo
7667       call matvec2(a_chuj(1,1,jj,i),dipderj(1),auxvec(1))
7668       do iii=1,2
7669         dipderg(iii+2,jj,i)=scalar2(auxvec(1),dipi(1,iii))
7670       enddo
7671       return
7672       end
7673 #endif
7674 C---------------------------------------------------------------------------
7675       subroutine calc_eello(i,j,k,l,jj,kk)
7676
7677 C This subroutine computes matrices and vectors needed to calculate 
7678 C the fourth-, fifth-, and sixth-order local-electrostatic terms.
7679 C
7680       implicit real*8 (a-h,o-z)
7681       include 'DIMENSIONS'
7682       include 'COMMON.IOUNITS'
7683       include 'COMMON.CHAIN'
7684       include 'COMMON.DERIV'
7685       include 'COMMON.INTERACT'
7686       include 'COMMON.CONTACTS'
7687       include 'COMMON.TORSION'
7688       include 'COMMON.VAR'
7689       include 'COMMON.GEO'
7690       include 'COMMON.FFIELD'
7691       double precision aa1(2,2),aa2(2,2),aa1t(2,2),aa2t(2,2),
7692      &  aa1tder(2,2,3,5),aa2tder(2,2,3,5),auxmat(2,2)
7693       logical lprn
7694       common /kutas/ lprn
7695 cd      write (iout,*) 'calc_eello: i=',i,' j=',j,' k=',k,' l=',l,
7696 cd     & ' jj=',jj,' kk=',kk
7697 cd      if (i.ne.2 .or. j.ne.4 .or. k.ne.3 .or. l.ne.5) return
7698 cd      write (iout,*) "a_chujij",((a_chuj(iii,jjj,jj,i),iii=1,2),jjj=1,2)
7699 cd      write (iout,*) "a_chujkl",((a_chuj(iii,jjj,kk,k),iii=1,2),jjj=1,2)
7700       do iii=1,2
7701         do jjj=1,2
7702           aa1(iii,jjj)=a_chuj(iii,jjj,jj,i)
7703           aa2(iii,jjj)=a_chuj(iii,jjj,kk,k)
7704         enddo
7705       enddo
7706       call transpose2(aa1(1,1),aa1t(1,1))
7707       call transpose2(aa2(1,1),aa2t(1,1))
7708       do kkk=1,5
7709         do lll=1,3
7710           call transpose2(a_chuj_der(1,1,lll,kkk,jj,i),
7711      &      aa1tder(1,1,lll,kkk))
7712           call transpose2(a_chuj_der(1,1,lll,kkk,kk,k),
7713      &      aa2tder(1,1,lll,kkk))
7714         enddo
7715       enddo 
7716       if (l.eq.j+1) then
7717 C parallel orientation of the two CA-CA-CA frames.
7718         if (i.gt.1) then
7719           iti=itortyp(itype(i))
7720         else
7721           iti=ntortyp+1
7722         endif
7723         itk1=itortyp(itype(k+1))
7724         itj=itortyp(itype(j))
7725         if (l.lt.nres-1) then
7726           itl1=itortyp(itype(l+1))
7727         else
7728           itl1=ntortyp+1
7729         endif
7730 C A1 kernel(j+1) A2T
7731 cd        do iii=1,2
7732 cd          write (iout,'(3f10.5,5x,3f10.5)') 
7733 cd     &     (EUg(iii,jjj,k),jjj=1,2),(EUg(iii,jjj,l),jjj=1,2)
7734 cd        enddo
7735         call kernel(aa1(1,1),aa2t(1,1),a_chuj_der(1,1,1,1,jj,i),
7736      &   aa2tder(1,1,1,1),1,.false.,EUg(1,1,l),EUgder(1,1,l),
7737      &   AEA(1,1,1),AEAderg(1,1,1),AEAderx(1,1,1,1,1,1))
7738 C Following matrices are needed only for 6-th order cumulants
7739         IF (wcorr6.gt.0.0d0) THEN
7740         call kernel(aa1(1,1),aa2t(1,1),a_chuj_der(1,1,1,1,jj,i),
7741      &   aa2tder(1,1,1,1),1,.false.,EUgC(1,1,l),EUgCder(1,1,l),
7742      &   AECA(1,1,1),AECAderg(1,1,1),AECAderx(1,1,1,1,1,1))
7743         call kernel(aa1(1,1),aa2t(1,1),a_chuj_der(1,1,1,1,jj,i),
7744      &   aa2tder(1,1,1,1),2,.false.,Ug2DtEUg(1,1,l),
7745      &   Ug2DtEUgder(1,1,1,l),ADtEA(1,1,1),ADtEAderg(1,1,1,1),
7746      &   ADtEAderx(1,1,1,1,1,1))
7747         lprn=.false.
7748         call kernel(aa1(1,1),aa2t(1,1),a_chuj_der(1,1,1,1,jj,i),
7749      &   aa2tder(1,1,1,1),2,.false.,DtUg2EUg(1,1,l),
7750      &   DtUg2EUgder(1,1,1,l),ADtEA1(1,1,1),ADtEA1derg(1,1,1,1),
7751      &   ADtEA1derx(1,1,1,1,1,1))
7752         ENDIF
7753 C End 6-th order cumulants
7754 cd        lprn=.false.
7755 cd        if (lprn) then
7756 cd        write (2,*) 'In calc_eello6'
7757 cd        do iii=1,2
7758 cd          write (2,*) 'iii=',iii
7759 cd          do kkk=1,5
7760 cd            write (2,*) 'kkk=',kkk
7761 cd            do jjj=1,2
7762 cd              write (2,'(3(2f10.5),5x)') 
7763 cd     &        ((ADtEA1derx(jjj,mmm,lll,kkk,iii,1),mmm=1,2),lll=1,3)
7764 cd            enddo
7765 cd          enddo
7766 cd        enddo
7767 cd        endif
7768         call transpose2(EUgder(1,1,k),auxmat(1,1))
7769         call matmat2(auxmat(1,1),AEA(1,1,1),EAEAderg(1,1,1,1))
7770         call transpose2(EUg(1,1,k),auxmat(1,1))
7771         call matmat2(auxmat(1,1),AEA(1,1,1),EAEA(1,1,1))
7772         call matmat2(auxmat(1,1),AEAderg(1,1,1),EAEAderg(1,1,2,1))
7773         do iii=1,2
7774           do kkk=1,5
7775             do lll=1,3
7776               call matmat2(auxmat(1,1),AEAderx(1,1,lll,kkk,iii,1),
7777      &          EAEAderx(1,1,lll,kkk,iii,1))
7778             enddo
7779           enddo
7780         enddo
7781 C A1T kernel(i+1) A2
7782         call kernel(aa1t(1,1),aa2(1,1),aa1tder(1,1,1,1),
7783      &   a_chuj_der(1,1,1,1,kk,k),1,.false.,EUg(1,1,k),EUgder(1,1,k),
7784      &   AEA(1,1,2),AEAderg(1,1,2),AEAderx(1,1,1,1,1,2))
7785 C Following matrices are needed only for 6-th order cumulants
7786         IF (wcorr6.gt.0.0d0) THEN
7787         call kernel(aa1t(1,1),aa2(1,1),aa1tder(1,1,1,1),
7788      &   a_chuj_der(1,1,1,1,kk,k),1,.false.,EUgC(1,1,k),EUgCder(1,1,k),
7789      &   AECA(1,1,2),AECAderg(1,1,2),AECAderx(1,1,1,1,1,2))
7790         call kernel(aa1t(1,1),aa2(1,1),aa1tder(1,1,1,1),
7791      &   a_chuj_der(1,1,1,1,kk,k),2,.false.,Ug2DtEUg(1,1,k),
7792      &   Ug2DtEUgder(1,1,1,k),ADtEA(1,1,2),ADtEAderg(1,1,1,2),
7793      &   ADtEAderx(1,1,1,1,1,2))
7794         call kernel(aa1t(1,1),aa2(1,1),aa1tder(1,1,1,1),
7795      &   a_chuj_der(1,1,1,1,kk,k),2,.false.,DtUg2EUg(1,1,k),
7796      &   DtUg2EUgder(1,1,1,k),ADtEA1(1,1,2),ADtEA1derg(1,1,1,2),
7797      &   ADtEA1derx(1,1,1,1,1,2))
7798         ENDIF
7799 C End 6-th order cumulants
7800         call transpose2(EUgder(1,1,l),auxmat(1,1))
7801         call matmat2(auxmat(1,1),AEA(1,1,2),EAEAderg(1,1,1,2))
7802         call transpose2(EUg(1,1,l),auxmat(1,1))
7803         call matmat2(auxmat(1,1),AEA(1,1,2),EAEA(1,1,2))
7804         call matmat2(auxmat(1,1),AEAderg(1,1,2),EAEAderg(1,1,2,2))
7805         do iii=1,2
7806           do kkk=1,5
7807             do lll=1,3
7808               call matmat2(auxmat(1,1),AEAderx(1,1,lll,kkk,iii,2),
7809      &          EAEAderx(1,1,lll,kkk,iii,2))
7810             enddo
7811           enddo
7812         enddo
7813 C AEAb1 and AEAb2
7814 C Calculate the vectors and their derivatives in virtual-bond dihedral angles.
7815 C They are needed only when the fifth- or the sixth-order cumulants are
7816 C indluded.
7817         IF (wcorr5.gt.0.0d0 .or. wcorr6.gt.0.0d0) THEN
7818         call transpose2(AEA(1,1,1),auxmat(1,1))
7819         call matvec2(auxmat(1,1),b1(1,iti),AEAb1(1,1,1))
7820         call matvec2(auxmat(1,1),Ub2(1,i),AEAb2(1,1,1))
7821         call matvec2(auxmat(1,1),Ub2der(1,i),AEAb2derg(1,2,1,1))
7822         call transpose2(AEAderg(1,1,1),auxmat(1,1))
7823         call matvec2(auxmat(1,1),b1(1,iti),AEAb1derg(1,1,1))
7824         call matvec2(auxmat(1,1),Ub2(1,i),AEAb2derg(1,1,1,1))
7825         call matvec2(AEA(1,1,1),b1(1,itk1),AEAb1(1,2,1))
7826         call matvec2(AEAderg(1,1,1),b1(1,itk1),AEAb1derg(1,2,1))
7827         call matvec2(AEA(1,1,1),Ub2(1,k+1),AEAb2(1,2,1))
7828         call matvec2(AEAderg(1,1,1),Ub2(1,k+1),AEAb2derg(1,1,2,1))
7829         call matvec2(AEA(1,1,1),Ub2der(1,k+1),AEAb2derg(1,2,2,1))
7830         call transpose2(AEA(1,1,2),auxmat(1,1))
7831         call matvec2(auxmat(1,1),b1(1,itj),AEAb1(1,1,2))
7832         call matvec2(auxmat(1,1),Ub2(1,j),AEAb2(1,1,2))
7833         call matvec2(auxmat(1,1),Ub2der(1,j),AEAb2derg(1,2,1,2))
7834         call transpose2(AEAderg(1,1,2),auxmat(1,1))
7835         call matvec2(auxmat(1,1),b1(1,itj),AEAb1derg(1,1,2))
7836         call matvec2(auxmat(1,1),Ub2(1,j),AEAb2derg(1,1,1,2))
7837         call matvec2(AEA(1,1,2),b1(1,itl1),AEAb1(1,2,2))
7838         call matvec2(AEAderg(1,1,2),b1(1,itl1),AEAb1derg(1,2,2))
7839         call matvec2(AEA(1,1,2),Ub2(1,l+1),AEAb2(1,2,2))
7840         call matvec2(AEAderg(1,1,2),Ub2(1,l+1),AEAb2derg(1,1,2,2))
7841         call matvec2(AEA(1,1,2),Ub2der(1,l+1),AEAb2derg(1,2,2,2))
7842 C Calculate the Cartesian derivatives of the vectors.
7843         do iii=1,2
7844           do kkk=1,5
7845             do lll=1,3
7846               call transpose2(AEAderx(1,1,lll,kkk,iii,1),auxmat(1,1))
7847               call matvec2(auxmat(1,1),b1(1,iti),
7848      &          AEAb1derx(1,lll,kkk,iii,1,1))
7849               call matvec2(auxmat(1,1),Ub2(1,i),
7850      &          AEAb2derx(1,lll,kkk,iii,1,1))
7851               call matvec2(AEAderx(1,1,lll,kkk,iii,1),b1(1,itk1),
7852      &          AEAb1derx(1,lll,kkk,iii,2,1))
7853               call matvec2(AEAderx(1,1,lll,kkk,iii,1),Ub2(1,k+1),
7854      &          AEAb2derx(1,lll,kkk,iii,2,1))
7855               call transpose2(AEAderx(1,1,lll,kkk,iii,2),auxmat(1,1))
7856               call matvec2(auxmat(1,1),b1(1,itj),
7857      &          AEAb1derx(1,lll,kkk,iii,1,2))
7858               call matvec2(auxmat(1,1),Ub2(1,j),
7859      &          AEAb2derx(1,lll,kkk,iii,1,2))
7860               call matvec2(AEAderx(1,1,lll,kkk,iii,2),b1(1,itl1),
7861      &          AEAb1derx(1,lll,kkk,iii,2,2))
7862               call matvec2(AEAderx(1,1,lll,kkk,iii,2),Ub2(1,l+1),
7863      &          AEAb2derx(1,lll,kkk,iii,2,2))
7864             enddo
7865           enddo
7866         enddo
7867         ENDIF
7868 C End vectors
7869       else
7870 C Antiparallel orientation of the two CA-CA-CA frames.
7871         if (i.gt.1) then
7872           iti=itortyp(itype(i))
7873         else
7874           iti=ntortyp+1
7875         endif
7876         itk1=itortyp(itype(k+1))
7877         itl=itortyp(itype(l))
7878         itj=itortyp(itype(j))
7879         if (j.lt.nres-1) then
7880           itj1=itortyp(itype(j+1))
7881         else 
7882           itj1=ntortyp+1
7883         endif
7884 C A2 kernel(j-1)T A1T
7885         call kernel(aa1(1,1),aa2t(1,1),a_chuj_der(1,1,1,1,jj,i),
7886      &   aa2tder(1,1,1,1),1,.true.,EUg(1,1,j),EUgder(1,1,j),
7887      &   AEA(1,1,1),AEAderg(1,1,1),AEAderx(1,1,1,1,1,1))
7888 C Following matrices are needed only for 6-th order cumulants
7889         IF (wcorr6.gt.0.0d0 .or. (wturn6.gt.0.0d0 .and.
7890      &     j.eq.i+4 .and. l.eq.i+3)) THEN
7891         call kernel(aa1(1,1),aa2t(1,1),a_chuj_der(1,1,1,1,jj,i),
7892      &   aa2tder(1,1,1,1),1,.true.,EUgC(1,1,j),EUgCder(1,1,j),
7893      &   AECA(1,1,1),AECAderg(1,1,1),AECAderx(1,1,1,1,1,1))
7894         call kernel(aa2(1,1),aa2t(1,1),a_chuj_der(1,1,1,1,jj,i),
7895      &   aa2tder(1,1,1,1),2,.true.,Ug2DtEUg(1,1,j),
7896      &   Ug2DtEUgder(1,1,1,j),ADtEA(1,1,1),ADtEAderg(1,1,1,1),
7897      &   ADtEAderx(1,1,1,1,1,1))
7898         call kernel(aa1(1,1),aa2t(1,1),a_chuj_der(1,1,1,1,jj,i),
7899      &   aa2tder(1,1,1,1),2,.true.,DtUg2EUg(1,1,j),
7900      &   DtUg2EUgder(1,1,1,j),ADtEA1(1,1,1),ADtEA1derg(1,1,1,1),
7901      &   ADtEA1derx(1,1,1,1,1,1))
7902         ENDIF
7903 C End 6-th order cumulants
7904         call transpose2(EUgder(1,1,k),auxmat(1,1))
7905         call matmat2(auxmat(1,1),AEA(1,1,1),EAEAderg(1,1,1,1))
7906         call transpose2(EUg(1,1,k),auxmat(1,1))
7907         call matmat2(auxmat(1,1),AEA(1,1,1),EAEA(1,1,1))
7908         call matmat2(auxmat(1,1),AEAderg(1,1,1),EAEAderg(1,1,2,1))
7909         do iii=1,2
7910           do kkk=1,5
7911             do lll=1,3
7912               call matmat2(auxmat(1,1),AEAderx(1,1,lll,kkk,iii,1),
7913      &          EAEAderx(1,1,lll,kkk,iii,1))
7914             enddo
7915           enddo
7916         enddo
7917 C A2T kernel(i+1)T A1
7918         call kernel(aa2t(1,1),aa1(1,1),aa2tder(1,1,1,1),
7919      &   a_chuj_der(1,1,1,1,jj,i),1,.true.,EUg(1,1,k),EUgder(1,1,k),
7920      &   AEA(1,1,2),AEAderg(1,1,2),AEAderx(1,1,1,1,1,2))
7921 C Following matrices are needed only for 6-th order cumulants
7922         IF (wcorr6.gt.0.0d0 .or. (wturn6.gt.0.0d0 .and.
7923      &     j.eq.i+4 .and. l.eq.i+3)) THEN
7924         call kernel(aa2t(1,1),aa1(1,1),aa2tder(1,1,1,1),
7925      &   a_chuj_der(1,1,1,1,jj,i),1,.true.,EUgC(1,1,k),EUgCder(1,1,k),
7926      &   AECA(1,1,2),AECAderg(1,1,2),AECAderx(1,1,1,1,1,2))
7927         call kernel(aa2t(1,1),aa1(1,1),aa2tder(1,1,1,1),
7928      &   a_chuj_der(1,1,1,1,jj,i),2,.true.,Ug2DtEUg(1,1,k),
7929      &   Ug2DtEUgder(1,1,1,k),ADtEA(1,1,2),ADtEAderg(1,1,1,2),
7930      &   ADtEAderx(1,1,1,1,1,2))
7931         call kernel(aa2t(1,1),aa1(1,1),aa2tder(1,1,1,1),
7932      &   a_chuj_der(1,1,1,1,jj,i),2,.true.,DtUg2EUg(1,1,k),
7933      &   DtUg2EUgder(1,1,1,k),ADtEA1(1,1,2),ADtEA1derg(1,1,1,2),
7934      &   ADtEA1derx(1,1,1,1,1,2))
7935         ENDIF
7936 C End 6-th order cumulants
7937         call transpose2(EUgder(1,1,j),auxmat(1,1))
7938         call matmat2(auxmat(1,1),AEA(1,1,1),EAEAderg(1,1,2,2))
7939         call transpose2(EUg(1,1,j),auxmat(1,1))
7940         call matmat2(auxmat(1,1),AEA(1,1,2),EAEA(1,1,2))
7941         call matmat2(auxmat(1,1),AEAderg(1,1,2),EAEAderg(1,1,2,2))
7942         do iii=1,2
7943           do kkk=1,5
7944             do lll=1,3
7945               call matmat2(auxmat(1,1),AEAderx(1,1,lll,kkk,iii,2),
7946      &          EAEAderx(1,1,lll,kkk,iii,2))
7947             enddo
7948           enddo
7949         enddo
7950 C AEAb1 and AEAb2
7951 C Calculate the vectors and their derivatives in virtual-bond dihedral angles.
7952 C They are needed only when the fifth- or the sixth-order cumulants are
7953 C indluded.
7954         IF (wcorr5.gt.0.0d0 .or. wcorr6.gt.0.0d0 .or.
7955      &    (wturn6.gt.0.0d0 .and. j.eq.i+4 .and. l.eq.i+3)) THEN
7956         call transpose2(AEA(1,1,1),auxmat(1,1))
7957         call matvec2(auxmat(1,1),b1(1,iti),AEAb1(1,1,1))
7958         call matvec2(auxmat(1,1),Ub2(1,i),AEAb2(1,1,1))
7959         call matvec2(auxmat(1,1),Ub2der(1,i),AEAb2derg(1,2,1,1))
7960         call transpose2(AEAderg(1,1,1),auxmat(1,1))
7961         call matvec2(auxmat(1,1),b1(1,iti),AEAb1derg(1,1,1))
7962         call matvec2(auxmat(1,1),Ub2(1,i),AEAb2derg(1,1,1,1))
7963         call matvec2(AEA(1,1,1),b1(1,itk1),AEAb1(1,2,1))
7964         call matvec2(AEAderg(1,1,1),b1(1,itk1),AEAb1derg(1,2,1))
7965         call matvec2(AEA(1,1,1),Ub2(1,k+1),AEAb2(1,2,1))
7966         call matvec2(AEAderg(1,1,1),Ub2(1,k+1),AEAb2derg(1,1,2,1))
7967         call matvec2(AEA(1,1,1),Ub2der(1,k+1),AEAb2derg(1,2,2,1))
7968         call transpose2(AEA(1,1,2),auxmat(1,1))
7969         call matvec2(auxmat(1,1),b1(1,itj1),AEAb1(1,1,2))
7970         call matvec2(auxmat(1,1),Ub2(1,l),AEAb2(1,1,2))
7971         call matvec2(auxmat(1,1),Ub2der(1,l),AEAb2derg(1,2,1,2))
7972         call transpose2(AEAderg(1,1,2),auxmat(1,1))
7973         call matvec2(auxmat(1,1),b1(1,itl),AEAb1(1,1,2))
7974         call matvec2(auxmat(1,1),Ub2(1,l),AEAb2derg(1,1,1,2))
7975         call matvec2(AEA(1,1,2),b1(1,itj1),AEAb1(1,2,2))
7976         call matvec2(AEAderg(1,1,2),b1(1,itj1),AEAb1derg(1,2,2))
7977         call matvec2(AEA(1,1,2),Ub2(1,j),AEAb2(1,2,2))
7978         call matvec2(AEAderg(1,1,2),Ub2(1,j),AEAb2derg(1,1,2,2))
7979         call matvec2(AEA(1,1,2),Ub2der(1,j),AEAb2derg(1,2,2,2))
7980 C Calculate the Cartesian derivatives of the vectors.
7981         do iii=1,2
7982           do kkk=1,5
7983             do lll=1,3
7984               call transpose2(AEAderx(1,1,lll,kkk,iii,1),auxmat(1,1))
7985               call matvec2(auxmat(1,1),b1(1,iti),
7986      &          AEAb1derx(1,lll,kkk,iii,1,1))
7987               call matvec2(auxmat(1,1),Ub2(1,i),
7988      &          AEAb2derx(1,lll,kkk,iii,1,1))
7989               call matvec2(AEAderx(1,1,lll,kkk,iii,1),b1(1,itk1),
7990      &          AEAb1derx(1,lll,kkk,iii,2,1))
7991               call matvec2(AEAderx(1,1,lll,kkk,iii,1),Ub2(1,k+1),
7992      &          AEAb2derx(1,lll,kkk,iii,2,1))
7993               call transpose2(AEAderx(1,1,lll,kkk,iii,2),auxmat(1,1))
7994               call matvec2(auxmat(1,1),b1(1,itl),
7995      &          AEAb1derx(1,lll,kkk,iii,1,2))
7996               call matvec2(auxmat(1,1),Ub2(1,l),
7997      &          AEAb2derx(1,lll,kkk,iii,1,2))
7998               call matvec2(AEAderx(1,1,lll,kkk,iii,2),b1(1,itj1),
7999      &          AEAb1derx(1,lll,kkk,iii,2,2))
8000               call matvec2(AEAderx(1,1,lll,kkk,iii,2),Ub2(1,j),
8001      &          AEAb2derx(1,lll,kkk,iii,2,2))
8002             enddo
8003           enddo
8004         enddo
8005         ENDIF
8006 C End vectors
8007       endif
8008       return
8009       end
8010 C---------------------------------------------------------------------------
8011       subroutine kernel(aa1,aa2t,aa1derx,aa2tderx,nderg,transp,
8012      &  KK,KKderg,AKA,AKAderg,AKAderx)
8013       implicit none
8014       integer nderg
8015       logical transp
8016       double precision aa1(2,2),aa2t(2,2),aa1derx(2,2,3,5),
8017      &  aa2tderx(2,2,3,5),KK(2,2),KKderg(2,2,nderg),AKA(2,2),
8018      &  AKAderg(2,2,nderg),AKAderx(2,2,3,5,2)
8019       integer iii,kkk,lll
8020       integer jjj,mmm
8021       logical lprn
8022       common /kutas/ lprn
8023       call prodmat3(aa1(1,1),aa2t(1,1),KK(1,1),transp,AKA(1,1))
8024       do iii=1,nderg 
8025         call prodmat3(aa1(1,1),aa2t(1,1),KKderg(1,1,iii),transp,
8026      &    AKAderg(1,1,iii))
8027       enddo
8028 cd      if (lprn) write (2,*) 'In kernel'
8029       do kkk=1,5
8030 cd        if (lprn) write (2,*) 'kkk=',kkk
8031         do lll=1,3
8032           call prodmat3(aa1derx(1,1,lll,kkk),aa2t(1,1),
8033      &      KK(1,1),transp,AKAderx(1,1,lll,kkk,1))
8034 cd          if (lprn) then
8035 cd            write (2,*) 'lll=',lll
8036 cd            write (2,*) 'iii=1'
8037 cd            do jjj=1,2
8038 cd              write (2,'(3(2f10.5),5x)') 
8039 cd     &        (AKAderx(jjj,mmm,lll,kkk,1),mmm=1,2)
8040 cd            enddo
8041 cd          endif
8042           call prodmat3(aa1(1,1),aa2tderx(1,1,lll,kkk),
8043      &      KK(1,1),transp,AKAderx(1,1,lll,kkk,2))
8044 cd          if (lprn) then
8045 cd            write (2,*) 'lll=',lll
8046 cd            write (2,*) 'iii=2'
8047 cd            do jjj=1,2
8048 cd              write (2,'(3(2f10.5),5x)') 
8049 cd     &        (AKAderx(jjj,mmm,lll,kkk,2),mmm=1,2)
8050 cd            enddo
8051 cd          endif
8052         enddo
8053       enddo
8054       return
8055       end
8056 C---------------------------------------------------------------------------
8057       double precision function eello4(i,j,k,l,jj,kk)
8058       implicit real*8 (a-h,o-z)
8059       include 'DIMENSIONS'
8060       include 'COMMON.IOUNITS'
8061       include 'COMMON.CHAIN'
8062       include 'COMMON.DERIV'
8063       include 'COMMON.INTERACT'
8064       include 'COMMON.CONTACTS'
8065       include 'COMMON.TORSION'
8066       include 'COMMON.VAR'
8067       include 'COMMON.GEO'
8068       double precision pizda(2,2),ggg1(3),ggg2(3)
8069 cd      if (i.ne.1 .or. j.ne.5 .or. k.ne.2 .or.l.ne.4) then
8070 cd        eello4=0.0d0
8071 cd        return
8072 cd      endif
8073 cd      print *,'eello4:',i,j,k,l,jj,kk
8074 cd      write (2,*) 'i',i,' j',j,' k',k,' l',l
8075 cd      call checkint4(i,j,k,l,jj,kk,eel4_num)
8076 cold      eij=facont_hb(jj,i)
8077 cold      ekl=facont_hb(kk,k)
8078 cold      ekont=eij*ekl
8079       eel4=-EAEA(1,1,1)-EAEA(2,2,1)
8080 cd      eel41=-EAEA(1,1,2)-EAEA(2,2,2)
8081       gcorr_loc(k-1)=gcorr_loc(k-1)
8082      &   -ekont*(EAEAderg(1,1,1,1)+EAEAderg(2,2,1,1))
8083       if (l.eq.j+1) then
8084         gcorr_loc(l-1)=gcorr_loc(l-1)
8085      &     -ekont*(EAEAderg(1,1,2,1)+EAEAderg(2,2,2,1))
8086       else
8087         gcorr_loc(j-1)=gcorr_loc(j-1)
8088      &     -ekont*(EAEAderg(1,1,2,1)+EAEAderg(2,2,2,1))
8089       endif
8090       do iii=1,2
8091         do kkk=1,5
8092           do lll=1,3
8093             derx(lll,kkk,iii)=-EAEAderx(1,1,lll,kkk,iii,1)
8094      &                        -EAEAderx(2,2,lll,kkk,iii,1)
8095 cd            derx(lll,kkk,iii)=0.0d0
8096           enddo
8097         enddo
8098       enddo
8099 cd      gcorr_loc(l-1)=0.0d0
8100 cd      gcorr_loc(j-1)=0.0d0
8101 cd      gcorr_loc(k-1)=0.0d0
8102 cd      eel4=1.0d0
8103 cd      write (iout,*)'Contacts have occurred for peptide groups',
8104 cd     &  i,j,' fcont:',eij,' eij',' and ',k,l,
8105 cd     &  ' fcont ',ekl,' eel4=',eel4,' eel4_num',16*eel4_num
8106       if (j.lt.nres-1) then
8107         j1=j+1
8108         j2=j-1
8109       else
8110         j1=j-1
8111         j2=j-2
8112       endif
8113       if (l.lt.nres-1) then
8114         l1=l+1
8115         l2=l-1
8116       else
8117         l1=l-1
8118         l2=l-2
8119       endif
8120       do ll=1,3
8121 cgrad        ggg1(ll)=eel4*g_contij(ll,1)
8122 cgrad        ggg2(ll)=eel4*g_contij(ll,2)
8123         glongij=eel4*g_contij(ll,1)+ekont*derx(ll,1,1)
8124         glongkl=eel4*g_contij(ll,2)+ekont*derx(ll,1,2)
8125 cgrad        ghalf=0.5d0*ggg1(ll)
8126         gradcorr(ll,i)=gradcorr(ll,i)+ekont*derx(ll,2,1)
8127         gradcorr(ll,i+1)=gradcorr(ll,i+1)+ekont*derx(ll,3,1)
8128         gradcorr(ll,j)=gradcorr(ll,j)+ekont*derx(ll,4,1)
8129         gradcorr(ll,j1)=gradcorr(ll,j1)+ekont*derx(ll,5,1)
8130         gradcorr_long(ll,j)=gradcorr_long(ll,j)+glongij
8131         gradcorr_long(ll,i)=gradcorr_long(ll,i)-glongij
8132 cgrad        ghalf=0.5d0*ggg2(ll)
8133         gradcorr(ll,k)=gradcorr(ll,k)+ekont*derx(ll,2,2)
8134         gradcorr(ll,k+1)=gradcorr(ll,k+1)+ekont*derx(ll,3,2)
8135         gradcorr(ll,l)=gradcorr(ll,l)+ekont*derx(ll,4,2)
8136         gradcorr(ll,l1)=gradcorr(ll,l1)+ekont*derx(ll,5,2)
8137         gradcorr_long(ll,l)=gradcorr_long(ll,l)+glongkl
8138         gradcorr_long(ll,k)=gradcorr_long(ll,k)-glongkl
8139       enddo
8140 cgrad      do m=i+1,j-1
8141 cgrad        do ll=1,3
8142 cgrad          gradcorr(ll,m)=gradcorr(ll,m)+ggg1(ll)
8143 cgrad        enddo
8144 cgrad      enddo
8145 cgrad      do m=k+1,l-1
8146 cgrad        do ll=1,3
8147 cgrad          gradcorr(ll,m)=gradcorr(ll,m)+ggg2(ll)
8148 cgrad        enddo
8149 cgrad      enddo
8150 cgrad      do m=i+2,j2
8151 cgrad        do ll=1,3
8152 cgrad          gradcorr(ll,m)=gradcorr(ll,m)+ekont*derx(ll,1,1)
8153 cgrad        enddo
8154 cgrad      enddo
8155 cgrad      do m=k+2,l2
8156 cgrad        do ll=1,3
8157 cgrad          gradcorr(ll,m)=gradcorr(ll,m)+ekont*derx(ll,1,2)
8158 cgrad        enddo
8159 cgrad      enddo 
8160 cd      do iii=1,nres-3
8161 cd        write (2,*) iii,gcorr_loc(iii)
8162 cd      enddo
8163       eello4=ekont*eel4
8164 cd      write (2,*) 'ekont',ekont
8165 cd      write (iout,*) 'eello4',ekont*eel4
8166       return
8167       end
8168 C---------------------------------------------------------------------------
8169       double precision function eello5(i,j,k,l,jj,kk)
8170       implicit real*8 (a-h,o-z)
8171       include 'DIMENSIONS'
8172       include 'COMMON.IOUNITS'
8173       include 'COMMON.CHAIN'
8174       include 'COMMON.DERIV'
8175       include 'COMMON.INTERACT'
8176       include 'COMMON.CONTACTS'
8177       include 'COMMON.TORSION'
8178       include 'COMMON.VAR'
8179       include 'COMMON.GEO'
8180       double precision pizda(2,2),auxmat(2,2),auxmat1(2,2),vv(2)
8181       double precision ggg1(3),ggg2(3)
8182 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
8183 C                                                                              C
8184 C                            Parallel chains                                   C
8185 C                                                                              C
8186 C          o             o                   o             o                   C
8187 C         /l\           / \             \   / \           / \   /              C
8188 C        /   \         /   \             \ /   \         /   \ /               C
8189 C       j| o |l1       | o |              o| o |         | o |o                C
8190 C     \  |/k\|         |/ \|  /            |/ \|         |/ \|                 C
8191 C      \i/   \         /   \ /             /   \         /   \                 C
8192 C       o    k1             o                                                  C
8193 C         (I)          (II)                (III)          (IV)                 C
8194 C                                                                              C
8195 C      eello5_1        eello5_2            eello5_3       eello5_4             C
8196 C                                                                              C
8197 C                            Antiparallel chains                               C
8198 C                                                                              C
8199 C          o             o                   o             o                   C
8200 C         /j\           / \             \   / \           / \   /              C
8201 C        /   \         /   \             \ /   \         /   \ /               C
8202 C      j1| o |l        | o |              o| o |         | o |o                C
8203 C     \  |/k\|         |/ \|  /            |/ \|         |/ \|                 C
8204 C      \i/   \         /   \ /             /   \         /   \                 C
8205 C       o     k1            o                                                  C
8206 C         (I)          (II)                (III)          (IV)                 C
8207 C                                                                              C
8208 C      eello5_1        eello5_2            eello5_3       eello5_4             C
8209 C                                                                              C
8210 C o denotes a local interaction, vertical lines an electrostatic interaction.  C
8211 C                                                                              C
8212 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
8213 cd      if (i.ne.2 .or. j.ne.6 .or. k.ne.3 .or. l.ne.5) then
8214 cd        eello5=0.0d0
8215 cd        return
8216 cd      endif
8217 cd      write (iout,*)
8218 cd     &   'EELLO5: Contacts have occurred for peptide groups',i,j,
8219 cd     &   ' and',k,l
8220       itk=itortyp(itype(k))
8221       itl=itortyp(itype(l))
8222       itj=itortyp(itype(j))
8223       eello5_1=0.0d0
8224       eello5_2=0.0d0
8225       eello5_3=0.0d0
8226       eello5_4=0.0d0
8227 cd      call checkint5(i,j,k,l,jj,kk,eel5_1_num,eel5_2_num,
8228 cd     &   eel5_3_num,eel5_4_num)
8229       do iii=1,2
8230         do kkk=1,5
8231           do lll=1,3
8232             derx(lll,kkk,iii)=0.0d0
8233           enddo
8234         enddo
8235       enddo
8236 cd      eij=facont_hb(jj,i)
8237 cd      ekl=facont_hb(kk,k)
8238 cd      ekont=eij*ekl
8239 cd      write (iout,*)'Contacts have occurred for peptide groups',
8240 cd     &  i,j,' fcont:',eij,' eij',' and ',k,l
8241 cd      goto 1111
8242 C Contribution from the graph I.
8243 cd      write (2,*) 'AEA  ',AEA(1,1,1),AEA(2,1,1),AEA(1,2,1),AEA(2,2,1)
8244 cd      write (2,*) 'AEAb2',AEAb2(1,1,1),AEAb2(2,1,1)
8245       call transpose2(EUg(1,1,k),auxmat(1,1))
8246       call matmat2(AEA(1,1,1),auxmat(1,1),pizda(1,1))
8247       vv(1)=pizda(1,1)-pizda(2,2)
8248       vv(2)=pizda(1,2)+pizda(2,1)
8249       eello5_1=scalar2(AEAb2(1,1,1),Ub2(1,k))
8250      & +0.5d0*scalar2(vv(1),Dtobr2(1,i))
8251 C Explicit gradient in virtual-dihedral angles.
8252       if (i.gt.1) g_corr5_loc(i-1)=g_corr5_loc(i-1)
8253      & +ekont*(scalar2(AEAb2derg(1,2,1,1),Ub2(1,k))
8254      & +0.5d0*scalar2(vv(1),Dtobr2der(1,i)))
8255       call transpose2(EUgder(1,1,k),auxmat1(1,1))
8256       call matmat2(AEA(1,1,1),auxmat1(1,1),pizda(1,1))
8257       vv(1)=pizda(1,1)-pizda(2,2)
8258       vv(2)=pizda(1,2)+pizda(2,1)
8259       g_corr5_loc(k-1)=g_corr5_loc(k-1)
8260      & +ekont*(scalar2(AEAb2(1,1,1),Ub2der(1,k))
8261      & +0.5d0*scalar2(vv(1),Dtobr2(1,i)))
8262       call matmat2(AEAderg(1,1,1),auxmat(1,1),pizda(1,1))
8263       vv(1)=pizda(1,1)-pizda(2,2)
8264       vv(2)=pizda(1,2)+pizda(2,1)
8265       if (l.eq.j+1) then
8266         if (l.lt.nres-1) g_corr5_loc(l-1)=g_corr5_loc(l-1)
8267      &   +ekont*(scalar2(AEAb2derg(1,1,1,1),Ub2(1,k))
8268      &   +0.5d0*scalar2(vv(1),Dtobr2(1,i)))
8269       else
8270         if (j.lt.nres-1) g_corr5_loc(j-1)=g_corr5_loc(j-1)
8271      &   +ekont*(scalar2(AEAb2derg(1,1,1,1),Ub2(1,k))
8272      &   +0.5d0*scalar2(vv(1),Dtobr2(1,i)))
8273       endif 
8274 C Cartesian gradient
8275       do iii=1,2
8276         do kkk=1,5
8277           do lll=1,3
8278             call matmat2(AEAderx(1,1,lll,kkk,iii,1),auxmat(1,1),
8279      &        pizda(1,1))
8280             vv(1)=pizda(1,1)-pizda(2,2)
8281             vv(2)=pizda(1,2)+pizda(2,1)
8282             derx(lll,kkk,iii)=derx(lll,kkk,iii)
8283      &       +scalar2(AEAb2derx(1,lll,kkk,iii,1,1),Ub2(1,k))
8284      &       +0.5d0*scalar2(vv(1),Dtobr2(1,i))
8285           enddo
8286         enddo
8287       enddo
8288 c      goto 1112
8289 c1111  continue
8290 C Contribution from graph II 
8291       call transpose2(EE(1,1,itk),auxmat(1,1))
8292       call matmat2(auxmat(1,1),AEA(1,1,1),pizda(1,1))
8293       vv(1)=pizda(1,1)+pizda(2,2)
8294       vv(2)=pizda(2,1)-pizda(1,2)
8295       eello5_2=scalar2(AEAb1(1,2,1),b1(1,itk))
8296      & -0.5d0*scalar2(vv(1),Ctobr(1,k))
8297 C Explicit gradient in virtual-dihedral angles.
8298       g_corr5_loc(k-1)=g_corr5_loc(k-1)
8299      & -0.5d0*ekont*scalar2(vv(1),Ctobrder(1,k))
8300       call matmat2(auxmat(1,1),AEAderg(1,1,1),pizda(1,1))
8301       vv(1)=pizda(1,1)+pizda(2,2)
8302       vv(2)=pizda(2,1)-pizda(1,2)
8303       if (l.eq.j+1) then
8304         g_corr5_loc(l-1)=g_corr5_loc(l-1)
8305      &   +ekont*(scalar2(AEAb1derg(1,2,1),b1(1,itk))
8306      &   -0.5d0*scalar2(vv(1),Ctobr(1,k)))
8307       else
8308         g_corr5_loc(j-1)=g_corr5_loc(j-1)
8309      &   +ekont*(scalar2(AEAb1derg(1,2,1),b1(1,itk))
8310      &   -0.5d0*scalar2(vv(1),Ctobr(1,k)))
8311       endif
8312 C Cartesian gradient
8313       do iii=1,2
8314         do kkk=1,5
8315           do lll=1,3
8316             call matmat2(auxmat(1,1),AEAderx(1,1,lll,kkk,iii,1),
8317      &        pizda(1,1))
8318             vv(1)=pizda(1,1)+pizda(2,2)
8319             vv(2)=pizda(2,1)-pizda(1,2)
8320             derx(lll,kkk,iii)=derx(lll,kkk,iii)
8321      &       +scalar2(AEAb1derx(1,lll,kkk,iii,2,1),b1(1,itk))
8322      &       -0.5d0*scalar2(vv(1),Ctobr(1,k))
8323           enddo
8324         enddo
8325       enddo
8326 cd      goto 1112
8327 cd1111  continue
8328       if (l.eq.j+1) then
8329 cd        goto 1110
8330 C Parallel orientation
8331 C Contribution from graph III
8332         call transpose2(EUg(1,1,l),auxmat(1,1))
8333         call matmat2(AEA(1,1,2),auxmat(1,1),pizda(1,1))
8334         vv(1)=pizda(1,1)-pizda(2,2)
8335         vv(2)=pizda(1,2)+pizda(2,1)
8336         eello5_3=scalar2(AEAb2(1,1,2),Ub2(1,l))
8337      &   +0.5d0*scalar2(vv(1),Dtobr2(1,j))
8338 C Explicit gradient in virtual-dihedral angles.
8339         g_corr5_loc(j-1)=g_corr5_loc(j-1)
8340      &   +ekont*(scalar2(AEAb2derg(1,2,1,2),Ub2(1,l))
8341      &   +0.5d0*scalar2(vv(1),Dtobr2der(1,j)))
8342         call matmat2(AEAderg(1,1,2),auxmat(1,1),pizda(1,1))
8343         vv(1)=pizda(1,1)-pizda(2,2)
8344         vv(2)=pizda(1,2)+pizda(2,1)
8345         g_corr5_loc(k-1)=g_corr5_loc(k-1)
8346      &   +ekont*(scalar2(AEAb2derg(1,1,1,2),Ub2(1,l))
8347      &   +0.5d0*scalar2(vv(1),Dtobr2(1,j)))
8348         call transpose2(EUgder(1,1,l),auxmat1(1,1))
8349         call matmat2(AEA(1,1,2),auxmat1(1,1),pizda(1,1))
8350         vv(1)=pizda(1,1)-pizda(2,2)
8351         vv(2)=pizda(1,2)+pizda(2,1)
8352         g_corr5_loc(l-1)=g_corr5_loc(l-1)
8353      &   +ekont*(scalar2(AEAb2(1,1,2),Ub2der(1,l))
8354      &   +0.5d0*scalar2(vv(1),Dtobr2(1,j)))
8355 C Cartesian gradient
8356         do iii=1,2
8357           do kkk=1,5
8358             do lll=1,3
8359               call matmat2(AEAderx(1,1,lll,kkk,iii,2),auxmat(1,1),
8360      &          pizda(1,1))
8361               vv(1)=pizda(1,1)-pizda(2,2)
8362               vv(2)=pizda(1,2)+pizda(2,1)
8363               derx(lll,kkk,iii)=derx(lll,kkk,iii)
8364      &         +scalar2(AEAb2derx(1,lll,kkk,iii,1,2),Ub2(1,l))
8365      &         +0.5d0*scalar2(vv(1),Dtobr2(1,j))
8366             enddo
8367           enddo
8368         enddo
8369 cd        goto 1112
8370 C Contribution from graph IV
8371 cd1110    continue
8372         call transpose2(EE(1,1,itl),auxmat(1,1))
8373         call matmat2(auxmat(1,1),AEA(1,1,2),pizda(1,1))
8374         vv(1)=pizda(1,1)+pizda(2,2)
8375         vv(2)=pizda(2,1)-pizda(1,2)
8376         eello5_4=scalar2(AEAb1(1,2,2),b1(1,itl))
8377      &   -0.5d0*scalar2(vv(1),Ctobr(1,l))
8378 C Explicit gradient in virtual-dihedral angles.
8379         g_corr5_loc(l-1)=g_corr5_loc(l-1)
8380      &   -0.5d0*ekont*scalar2(vv(1),Ctobrder(1,l))
8381         call matmat2(auxmat(1,1),AEAderg(1,1,2),pizda(1,1))
8382         vv(1)=pizda(1,1)+pizda(2,2)
8383         vv(2)=pizda(2,1)-pizda(1,2)
8384         g_corr5_loc(k-1)=g_corr5_loc(k-1)
8385      &   +ekont*(scalar2(AEAb1derg(1,2,2),b1(1,itl))
8386      &   -0.5d0*scalar2(vv(1),Ctobr(1,l)))
8387 C Cartesian gradient
8388         do iii=1,2
8389           do kkk=1,5
8390             do lll=1,3
8391               call matmat2(auxmat(1,1),AEAderx(1,1,lll,kkk,iii,2),
8392      &          pizda(1,1))
8393               vv(1)=pizda(1,1)+pizda(2,2)
8394               vv(2)=pizda(2,1)-pizda(1,2)
8395               derx(lll,kkk,iii)=derx(lll,kkk,iii)
8396      &         +scalar2(AEAb1derx(1,lll,kkk,iii,2,2),b1(1,itl))
8397      &         -0.5d0*scalar2(vv(1),Ctobr(1,l))
8398             enddo
8399           enddo
8400         enddo
8401       else
8402 C Antiparallel orientation
8403 C Contribution from graph III
8404 c        goto 1110
8405         call transpose2(EUg(1,1,j),auxmat(1,1))
8406         call matmat2(AEA(1,1,2),auxmat(1,1),pizda(1,1))
8407         vv(1)=pizda(1,1)-pizda(2,2)
8408         vv(2)=pizda(1,2)+pizda(2,1)
8409         eello5_3=scalar2(AEAb2(1,1,2),Ub2(1,j))
8410      &   +0.5d0*scalar2(vv(1),Dtobr2(1,l))
8411 C Explicit gradient in virtual-dihedral angles.
8412         g_corr5_loc(l-1)=g_corr5_loc(l-1)
8413      &   +ekont*(scalar2(AEAb2derg(1,2,1,2),Ub2(1,j))
8414      &   +0.5d0*scalar2(vv(1),Dtobr2der(1,l)))
8415         call matmat2(AEAderg(1,1,2),auxmat(1,1),pizda(1,1))
8416         vv(1)=pizda(1,1)-pizda(2,2)
8417         vv(2)=pizda(1,2)+pizda(2,1)
8418         g_corr5_loc(k-1)=g_corr5_loc(k-1)
8419      &   +ekont*(scalar2(AEAb2derg(1,1,1,2),Ub2(1,j))
8420      &   +0.5d0*scalar2(vv(1),Dtobr2(1,l)))
8421         call transpose2(EUgder(1,1,j),auxmat1(1,1))
8422         call matmat2(AEA(1,1,2),auxmat1(1,1),pizda(1,1))
8423         vv(1)=pizda(1,1)-pizda(2,2)
8424         vv(2)=pizda(1,2)+pizda(2,1)
8425         g_corr5_loc(j-1)=g_corr5_loc(j-1)
8426      &   +ekont*(scalar2(AEAb2(1,1,2),Ub2der(1,j))
8427      &   +0.5d0*scalar2(vv(1),Dtobr2(1,l)))
8428 C Cartesian gradient
8429         do iii=1,2
8430           do kkk=1,5
8431             do lll=1,3
8432               call matmat2(AEAderx(1,1,lll,kkk,iii,2),auxmat(1,1),
8433      &          pizda(1,1))
8434               vv(1)=pizda(1,1)-pizda(2,2)
8435               vv(2)=pizda(1,2)+pizda(2,1)
8436               derx(lll,kkk,3-iii)=derx(lll,kkk,3-iii)
8437      &         +scalar2(AEAb2derx(1,lll,kkk,iii,1,2),Ub2(1,j))
8438      &         +0.5d0*scalar2(vv(1),Dtobr2(1,l))
8439             enddo
8440           enddo
8441         enddo
8442 cd        goto 1112
8443 C Contribution from graph IV
8444 1110    continue
8445         call transpose2(EE(1,1,itj),auxmat(1,1))
8446         call matmat2(auxmat(1,1),AEA(1,1,2),pizda(1,1))
8447         vv(1)=pizda(1,1)+pizda(2,2)
8448         vv(2)=pizda(2,1)-pizda(1,2)
8449         eello5_4=scalar2(AEAb1(1,2,2),b1(1,itj))
8450      &   -0.5d0*scalar2(vv(1),Ctobr(1,j))
8451 C Explicit gradient in virtual-dihedral angles.
8452         g_corr5_loc(j-1)=g_corr5_loc(j-1)
8453      &   -0.5d0*ekont*scalar2(vv(1),Ctobrder(1,j))
8454         call matmat2(auxmat(1,1),AEAderg(1,1,2),pizda(1,1))
8455         vv(1)=pizda(1,1)+pizda(2,2)
8456         vv(2)=pizda(2,1)-pizda(1,2)
8457         g_corr5_loc(k-1)=g_corr5_loc(k-1)
8458      &   +ekont*(scalar2(AEAb1derg(1,2,2),b1(1,itj))
8459      &   -0.5d0*scalar2(vv(1),Ctobr(1,j)))
8460 C Cartesian gradient
8461         do iii=1,2
8462           do kkk=1,5
8463             do lll=1,3
8464               call matmat2(auxmat(1,1),AEAderx(1,1,lll,kkk,iii,2),
8465      &          pizda(1,1))
8466               vv(1)=pizda(1,1)+pizda(2,2)
8467               vv(2)=pizda(2,1)-pizda(1,2)
8468               derx(lll,kkk,3-iii)=derx(lll,kkk,3-iii)
8469      &         +scalar2(AEAb1derx(1,lll,kkk,iii,2,2),b1(1,itj))
8470      &         -0.5d0*scalar2(vv(1),Ctobr(1,j))
8471             enddo
8472           enddo
8473         enddo
8474       endif
8475 1112  continue
8476       eel5=eello5_1+eello5_2+eello5_3+eello5_4
8477 cd      if (i.eq.2 .and. j.eq.8 .and. k.eq.3 .and. l.eq.7) then
8478 cd        write (2,*) 'ijkl',i,j,k,l
8479 cd        write (2,*) 'eello5_1',eello5_1,' eello5_2',eello5_2,
8480 cd     &     ' eello5_3',eello5_3,' eello5_4',eello5_4
8481 cd      endif
8482 cd      write(iout,*) 'eello5_1',eello5_1,' eel5_1_num',16*eel5_1_num
8483 cd      write(iout,*) 'eello5_2',eello5_2,' eel5_2_num',16*eel5_2_num
8484 cd      write(iout,*) 'eello5_3',eello5_3,' eel5_3_num',16*eel5_3_num
8485 cd      write(iout,*) 'eello5_4',eello5_4,' eel5_4_num',16*eel5_4_num
8486       if (j.lt.nres-1) then
8487         j1=j+1
8488         j2=j-1
8489       else
8490         j1=j-1
8491         j2=j-2
8492       endif
8493       if (l.lt.nres-1) then
8494         l1=l+1
8495         l2=l-1
8496       else
8497         l1=l-1
8498         l2=l-2
8499       endif
8500 cd      eij=1.0d0
8501 cd      ekl=1.0d0
8502 cd      ekont=1.0d0
8503 cd      write (2,*) 'eij',eij,' ekl',ekl,' ekont',ekont
8504 C 2/11/08 AL Gradients over DC's connecting interacting sites will be
8505 C        summed up outside the subrouine as for the other subroutines 
8506 C        handling long-range interactions. The old code is commented out
8507 C        with "cgrad" to keep track of changes.
8508       do ll=1,3
8509 cgrad        ggg1(ll)=eel5*g_contij(ll,1)
8510 cgrad        ggg2(ll)=eel5*g_contij(ll,2)
8511         gradcorr5ij=eel5*g_contij(ll,1)+ekont*derx(ll,1,1)
8512         gradcorr5kl=eel5*g_contij(ll,2)+ekont*derx(ll,1,2)
8513 c        write (iout,'(a,3i3,a,5f8.3,2i3,a,5f8.3,a,f8.3)') 
8514 c     &   "ecorr5",ll,i,j," derx",derx(ll,2,1),derx(ll,3,1),derx(ll,4,1),
8515 c     &   derx(ll,5,1),k,l," derx",derx(ll,2,2),derx(ll,3,2),
8516 c     &   derx(ll,4,2),derx(ll,5,2)," ekont",ekont
8517 c        write (iout,'(a,3i3,a,3f8.3,2i3,a,3f8.3)') 
8518 c     &   "ecorr5",ll,i,j," gradcorr5",g_contij(ll,1),derx(ll,1,1),
8519 c     &   gradcorr5ij,
8520 c     &   k,l," gradcorr5",g_contij(ll,2),derx(ll,1,2),gradcorr5kl
8521 cold        ghalf=0.5d0*eel5*ekl*gacont_hbr(ll,jj,i)
8522 cgrad        ghalf=0.5d0*ggg1(ll)
8523 cd        ghalf=0.0d0
8524         gradcorr5(ll,i)=gradcorr5(ll,i)+ekont*derx(ll,2,1)
8525         gradcorr5(ll,i+1)=gradcorr5(ll,i+1)+ekont*derx(ll,3,1)
8526         gradcorr5(ll,j)=gradcorr5(ll,j)+ekont*derx(ll,4,1)
8527         gradcorr5(ll,j1)=gradcorr5(ll,j1)+ekont*derx(ll,5,1)
8528         gradcorr5_long(ll,j)=gradcorr5_long(ll,j)+gradcorr5ij
8529         gradcorr5_long(ll,i)=gradcorr5_long(ll,i)-gradcorr5ij
8530 cold        ghalf=0.5d0*eel5*eij*gacont_hbr(ll,kk,k)
8531 cgrad        ghalf=0.5d0*ggg2(ll)
8532 cd        ghalf=0.0d0
8533         gradcorr5(ll,k)=gradcorr5(ll,k)+ghalf+ekont*derx(ll,2,2)
8534         gradcorr5(ll,k+1)=gradcorr5(ll,k+1)+ekont*derx(ll,3,2)
8535         gradcorr5(ll,l)=gradcorr5(ll,l)+ghalf+ekont*derx(ll,4,2)
8536         gradcorr5(ll,l1)=gradcorr5(ll,l1)+ekont*derx(ll,5,2)
8537         gradcorr5_long(ll,l)=gradcorr5_long(ll,l)+gradcorr5kl
8538         gradcorr5_long(ll,k)=gradcorr5_long(ll,k)-gradcorr5kl
8539       enddo
8540 cd      goto 1112
8541 cgrad      do m=i+1,j-1
8542 cgrad        do ll=1,3
8543 cold          gradcorr5(ll,m)=gradcorr5(ll,m)+eel5*ekl*gacont_hbr(ll,jj,i)
8544 cgrad          gradcorr5(ll,m)=gradcorr5(ll,m)+ggg1(ll)
8545 cgrad        enddo
8546 cgrad      enddo
8547 cgrad      do m=k+1,l-1
8548 cgrad        do ll=1,3
8549 cold          gradcorr5(ll,m)=gradcorr5(ll,m)+eel5*eij*gacont_hbr(ll,kk,k)
8550 cgrad          gradcorr5(ll,m)=gradcorr5(ll,m)+ggg2(ll)
8551 cgrad        enddo
8552 cgrad      enddo
8553 c1112  continue
8554 cgrad      do m=i+2,j2
8555 cgrad        do ll=1,3
8556 cgrad          gradcorr5(ll,m)=gradcorr5(ll,m)+ekont*derx(ll,1,1)
8557 cgrad        enddo
8558 cgrad      enddo
8559 cgrad      do m=k+2,l2
8560 cgrad        do ll=1,3
8561 cgrad          gradcorr5(ll,m)=gradcorr5(ll,m)+ekont*derx(ll,1,2)
8562 cgrad        enddo
8563 cgrad      enddo 
8564 cd      do iii=1,nres-3
8565 cd        write (2,*) iii,g_corr5_loc(iii)
8566 cd      enddo
8567       eello5=ekont*eel5
8568 cd      write (2,*) 'ekont',ekont
8569 cd      write (iout,*) 'eello5',ekont*eel5
8570       return
8571       end
8572 c--------------------------------------------------------------------------
8573       double precision function eello6(i,j,k,l,jj,kk)
8574       implicit real*8 (a-h,o-z)
8575       include 'DIMENSIONS'
8576       include 'COMMON.IOUNITS'
8577       include 'COMMON.CHAIN'
8578       include 'COMMON.DERIV'
8579       include 'COMMON.INTERACT'
8580       include 'COMMON.CONTACTS'
8581       include 'COMMON.TORSION'
8582       include 'COMMON.VAR'
8583       include 'COMMON.GEO'
8584       include 'COMMON.FFIELD'
8585       double precision ggg1(3),ggg2(3)
8586 cd      if (i.ne.1 .or. j.ne.3 .or. k.ne.2 .or. l.ne.4) then
8587 cd        eello6=0.0d0
8588 cd        return
8589 cd      endif
8590 cd      write (iout,*)
8591 cd     &   'EELLO6: Contacts have occurred for peptide groups',i,j,
8592 cd     &   ' and',k,l
8593       eello6_1=0.0d0
8594       eello6_2=0.0d0
8595       eello6_3=0.0d0
8596       eello6_4=0.0d0
8597       eello6_5=0.0d0
8598       eello6_6=0.0d0
8599 cd      call checkint6(i,j,k,l,jj,kk,eel6_1_num,eel6_2_num,
8600 cd     &   eel6_3_num,eel6_4_num,eel6_5_num,eel6_6_num)
8601       do iii=1,2
8602         do kkk=1,5
8603           do lll=1,3
8604             derx(lll,kkk,iii)=0.0d0
8605           enddo
8606         enddo
8607       enddo
8608 cd      eij=facont_hb(jj,i)
8609 cd      ekl=facont_hb(kk,k)
8610 cd      ekont=eij*ekl
8611 cd      eij=1.0d0
8612 cd      ekl=1.0d0
8613 cd      ekont=1.0d0
8614       if (l.eq.j+1) then
8615         eello6_1=eello6_graph1(i,j,k,l,1,.false.)
8616         eello6_2=eello6_graph1(j,i,l,k,2,.false.)
8617         eello6_3=eello6_graph2(i,j,k,l,jj,kk,.false.)
8618         eello6_4=eello6_graph4(i,j,k,l,jj,kk,1,.false.)
8619         eello6_5=eello6_graph4(j,i,l,k,jj,kk,2,.false.)
8620         eello6_6=eello6_graph3(i,j,k,l,jj,kk,.false.)
8621       else
8622         eello6_1=eello6_graph1(i,j,k,l,1,.false.)
8623         eello6_2=eello6_graph1(l,k,j,i,2,.true.)
8624         eello6_3=eello6_graph2(i,l,k,j,jj,kk,.true.)
8625         eello6_4=eello6_graph4(i,j,k,l,jj,kk,1,.false.)
8626         if (wturn6.eq.0.0d0 .or. j.ne.i+4) then
8627           eello6_5=eello6_graph4(l,k,j,i,kk,jj,2,.true.)
8628         else
8629           eello6_5=0.0d0
8630         endif
8631         eello6_6=eello6_graph3(i,l,k,j,jj,kk,.true.)
8632       endif
8633 C If turn contributions are considered, they will be handled separately.
8634       eel6=eello6_1+eello6_2+eello6_3+eello6_4+eello6_5+eello6_6
8635 cd      write(iout,*) 'eello6_1',eello6_1!,' eel6_1_num',16*eel6_1_num
8636 cd      write(iout,*) 'eello6_2',eello6_2!,' eel6_2_num',16*eel6_2_num
8637 cd      write(iout,*) 'eello6_3',eello6_3!,' eel6_3_num',16*eel6_3_num
8638 cd      write(iout,*) 'eello6_4',eello6_4!,' eel6_4_num',16*eel6_4_num
8639 cd      write(iout,*) 'eello6_5',eello6_5!,' eel6_5_num',16*eel6_5_num
8640 cd      write(iout,*) 'eello6_6',eello6_6!,' eel6_6_num',16*eel6_6_num
8641 cd      goto 1112
8642       if (j.lt.nres-1) then
8643         j1=j+1
8644         j2=j-1
8645       else
8646         j1=j-1
8647         j2=j-2
8648       endif
8649       if (l.lt.nres-1) then
8650         l1=l+1
8651         l2=l-1
8652       else
8653         l1=l-1
8654         l2=l-2
8655       endif
8656       do ll=1,3
8657 cgrad        ggg1(ll)=eel6*g_contij(ll,1)
8658 cgrad        ggg2(ll)=eel6*g_contij(ll,2)
8659 cold        ghalf=0.5d0*eel6*ekl*gacont_hbr(ll,jj,i)
8660 cgrad        ghalf=0.5d0*ggg1(ll)
8661 cd        ghalf=0.0d0
8662         gradcorr6ij=eel6*g_contij(ll,1)+ekont*derx(ll,1,1)
8663         gradcorr6kl=eel6*g_contij(ll,2)+ekont*derx(ll,1,2)
8664         gradcorr6(ll,i)=gradcorr6(ll,i)+ekont*derx(ll,2,1)
8665         gradcorr6(ll,i+1)=gradcorr6(ll,i+1)+ekont*derx(ll,3,1)
8666         gradcorr6(ll,j)=gradcorr6(ll,j)+ekont*derx(ll,4,1)
8667         gradcorr6(ll,j1)=gradcorr6(ll,j1)+ekont*derx(ll,5,1)
8668         gradcorr6_long(ll,j)=gradcorr6_long(ll,j)+gradcorr6ij
8669         gradcorr6_long(ll,i)=gradcorr6_long(ll,i)-gradcorr6ij
8670 cgrad        ghalf=0.5d0*ggg2(ll)
8671 cold        ghalf=0.5d0*eel6*eij*gacont_hbr(ll,kk,k)
8672 cd        ghalf=0.0d0
8673         gradcorr6(ll,k)=gradcorr6(ll,k)+ekont*derx(ll,2,2)
8674         gradcorr6(ll,k+1)=gradcorr6(ll,k+1)+ekont*derx(ll,3,2)
8675         gradcorr6(ll,l)=gradcorr6(ll,l)+ekont*derx(ll,4,2)
8676         gradcorr6(ll,l1)=gradcorr6(ll,l1)+ekont*derx(ll,5,2)
8677         gradcorr6_long(ll,l)=gradcorr6_long(ll,l)+gradcorr6kl
8678         gradcorr6_long(ll,k)=gradcorr6_long(ll,k)-gradcorr6kl
8679       enddo
8680 cd      goto 1112
8681 cgrad      do m=i+1,j-1
8682 cgrad        do ll=1,3
8683 cold          gradcorr6(ll,m)=gradcorr6(ll,m)+eel6*ekl*gacont_hbr(ll,jj,i)
8684 cgrad          gradcorr6(ll,m)=gradcorr6(ll,m)+ggg1(ll)
8685 cgrad        enddo
8686 cgrad      enddo
8687 cgrad      do m=k+1,l-1
8688 cgrad        do ll=1,3
8689 cold          gradcorr6(ll,m)=gradcorr6(ll,m)+eel6*eij*gacont_hbr(ll,kk,k)
8690 cgrad          gradcorr6(ll,m)=gradcorr6(ll,m)+ggg2(ll)
8691 cgrad        enddo
8692 cgrad      enddo
8693 cgrad1112  continue
8694 cgrad      do m=i+2,j2
8695 cgrad        do ll=1,3
8696 cgrad          gradcorr6(ll,m)=gradcorr6(ll,m)+ekont*derx(ll,1,1)
8697 cgrad        enddo
8698 cgrad      enddo
8699 cgrad      do m=k+2,l2
8700 cgrad        do ll=1,3
8701 cgrad          gradcorr6(ll,m)=gradcorr6(ll,m)+ekont*derx(ll,1,2)
8702 cgrad        enddo
8703 cgrad      enddo 
8704 cd      do iii=1,nres-3
8705 cd        write (2,*) iii,g_corr6_loc(iii)
8706 cd      enddo
8707       eello6=ekont*eel6
8708 cd      write (2,*) 'ekont',ekont
8709 cd      write (iout,*) 'eello6',ekont*eel6
8710       return
8711       end
8712 c--------------------------------------------------------------------------
8713       double precision function eello6_graph1(i,j,k,l,imat,swap)
8714       implicit real*8 (a-h,o-z)
8715       include 'DIMENSIONS'
8716       include 'COMMON.IOUNITS'
8717       include 'COMMON.CHAIN'
8718       include 'COMMON.DERIV'
8719       include 'COMMON.INTERACT'
8720       include 'COMMON.CONTACTS'
8721       include 'COMMON.TORSION'
8722       include 'COMMON.VAR'
8723       include 'COMMON.GEO'
8724       double precision vv(2),vv1(2),pizda(2,2),auxmat(2,2),pizda1(2,2)
8725       logical swap
8726       logical lprn
8727       common /kutas/ lprn
8728 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
8729 C                                              
8730 C      Parallel       Antiparallel
8731 C                                             
8732 C          o             o         
8733 C         /l\           /j\
8734 C        /   \         /   \
8735 C       /| o |         | o |\
8736 C     \ j|/k\|  /   \  |/k\|l /   
8737 C      \ /   \ /     \ /   \ /    
8738 C       o     o       o     o                
8739 C       i             i                     
8740 C
8741 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
8742       itk=itortyp(itype(k))
8743       s1= scalar2(AEAb1(1,2,imat),CUgb2(1,i))
8744       s2=-scalar2(AEAb2(1,1,imat),Ug2Db1t(1,k))
8745       s3= scalar2(AEAb2(1,1,imat),CUgb2(1,k))
8746       call transpose2(EUgC(1,1,k),auxmat(1,1))
8747       call matmat2(AEA(1,1,imat),auxmat(1,1),pizda1(1,1))
8748       vv1(1)=pizda1(1,1)-pizda1(2,2)
8749       vv1(2)=pizda1(1,2)+pizda1(2,1)
8750       s4=0.5d0*scalar2(vv1(1),Dtobr2(1,i))
8751       vv(1)=AEAb1(1,2,imat)*b1(1,itk)-AEAb1(2,2,imat)*b1(2,itk)
8752       vv(2)=AEAb1(1,2,imat)*b1(2,itk)+AEAb1(2,2,imat)*b1(1,itk)
8753       s5=scalar2(vv(1),Dtobr2(1,i))
8754 cd      write (2,*) 's1',s1,' s2',s2,' s3',s3,' s4', s4,' s5',s5
8755       eello6_graph1=-0.5d0*(s1+s2+s3+s4+s5)
8756       if (i.gt.1) g_corr6_loc(i-1)=g_corr6_loc(i-1)
8757      & -0.5d0*ekont*(scalar2(AEAb1(1,2,imat),CUgb2der(1,i))
8758      & -scalar2(AEAb2derg(1,2,1,imat),Ug2Db1t(1,k))
8759      & +scalar2(AEAb2derg(1,2,1,imat),CUgb2(1,k))
8760      & +0.5d0*scalar2(vv1(1),Dtobr2der(1,i))
8761      & +scalar2(vv(1),Dtobr2der(1,i)))
8762       call matmat2(AEAderg(1,1,imat),auxmat(1,1),pizda1(1,1))
8763       vv1(1)=pizda1(1,1)-pizda1(2,2)
8764       vv1(2)=pizda1(1,2)+pizda1(2,1)
8765       vv(1)=AEAb1derg(1,2,imat)*b1(1,itk)-AEAb1derg(2,2,imat)*b1(2,itk)
8766       vv(2)=AEAb1derg(1,2,imat)*b1(2,itk)+AEAb1derg(2,2,imat)*b1(1,itk)
8767       if (l.eq.j+1) then
8768         g_corr6_loc(l-1)=g_corr6_loc(l-1)
8769      & +ekont*(-0.5d0*(scalar2(AEAb1derg(1,2,imat),CUgb2(1,i))
8770      & -scalar2(AEAb2derg(1,1,1,imat),Ug2Db1t(1,k))
8771      & +scalar2(AEAb2derg(1,1,1,imat),CUgb2(1,k))
8772      & +0.5d0*scalar2(vv1(1),Dtobr2(1,i))+scalar2(vv(1),Dtobr2(1,i))))
8773       else
8774         g_corr6_loc(j-1)=g_corr6_loc(j-1)
8775      & +ekont*(-0.5d0*(scalar2(AEAb1derg(1,2,imat),CUgb2(1,i))
8776      & -scalar2(AEAb2derg(1,1,1,imat),Ug2Db1t(1,k))
8777      & +scalar2(AEAb2derg(1,1,1,imat),CUgb2(1,k))
8778      & +0.5d0*scalar2(vv1(1),Dtobr2(1,i))+scalar2(vv(1),Dtobr2(1,i))))
8779       endif
8780       call transpose2(EUgCder(1,1,k),auxmat(1,1))
8781       call matmat2(AEA(1,1,imat),auxmat(1,1),pizda1(1,1))
8782       vv1(1)=pizda1(1,1)-pizda1(2,2)
8783       vv1(2)=pizda1(1,2)+pizda1(2,1)
8784       if (k.gt.1) g_corr6_loc(k-1)=g_corr6_loc(k-1)
8785      & +ekont*(-0.5d0*(-scalar2(AEAb2(1,1,imat),Ug2Db1tder(1,k))
8786      & +scalar2(AEAb2(1,1,imat),CUgb2der(1,k))
8787      & +0.5d0*scalar2(vv1(1),Dtobr2(1,i))))
8788       do iii=1,2
8789         if (swap) then
8790           ind=3-iii
8791         else
8792           ind=iii
8793         endif
8794         do kkk=1,5
8795           do lll=1,3
8796             s1= scalar2(AEAb1derx(1,lll,kkk,iii,2,imat),CUgb2(1,i))
8797             s2=-scalar2(AEAb2derx(1,lll,kkk,iii,1,imat),Ug2Db1t(1,k))
8798             s3= scalar2(AEAb2derx(1,lll,kkk,iii,1,imat),CUgb2(1,k))
8799             call transpose2(EUgC(1,1,k),auxmat(1,1))
8800             call matmat2(AEAderx(1,1,lll,kkk,iii,imat),auxmat(1,1),
8801      &        pizda1(1,1))
8802             vv1(1)=pizda1(1,1)-pizda1(2,2)
8803             vv1(2)=pizda1(1,2)+pizda1(2,1)
8804             s4=0.5d0*scalar2(vv1(1),Dtobr2(1,i))
8805             vv(1)=AEAb1derx(1,lll,kkk,iii,2,imat)*b1(1,itk)
8806      &       -AEAb1derx(2,lll,kkk,iii,2,imat)*b1(2,itk)
8807             vv(2)=AEAb1derx(1,lll,kkk,iii,2,imat)*b1(2,itk)
8808      &       +AEAb1derx(2,lll,kkk,iii,2,imat)*b1(1,itk)
8809             s5=scalar2(vv(1),Dtobr2(1,i))
8810             derx(lll,kkk,ind)=derx(lll,kkk,ind)-0.5d0*(s1+s2+s3+s4+s5)
8811           enddo
8812         enddo
8813       enddo
8814       return
8815       end
8816 c----------------------------------------------------------------------------
8817       double precision function eello6_graph2(i,j,k,l,jj,kk,swap)
8818       implicit real*8 (a-h,o-z)
8819       include 'DIMENSIONS'
8820       include 'COMMON.IOUNITS'
8821       include 'COMMON.CHAIN'
8822       include 'COMMON.DERIV'
8823       include 'COMMON.INTERACT'
8824       include 'COMMON.CONTACTS'
8825       include 'COMMON.TORSION'
8826       include 'COMMON.VAR'
8827       include 'COMMON.GEO'
8828       logical swap
8829       double precision vv(2),pizda(2,2),auxmat(2,2),auxvec(2),
8830      & auxvec1(2),auxvec2(2),auxmat1(2,2)
8831       logical lprn
8832       common /kutas/ lprn
8833 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
8834 C                                                                              C
8835 C      Parallel       Antiparallel                                             C
8836 C                                                                              C
8837 C          o             o                                                     C
8838 C     \   /l\           /j\   /                                                C
8839 C      \ /   \         /   \ /                                                 C
8840 C       o| o |         | o |o                                                  C                
8841 C     \ j|/k\|      \  |/k\|l                                                  C
8842 C      \ /   \       \ /   \                                                   C
8843 C       o             o                                                        C
8844 C       i             i                                                        C 
8845 C                                                                              C           
8846 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
8847 cd      write (2,*) 'eello6_graph2: i,',i,' j',j,' k',k,' l',l
8848 C AL 7/4/01 s1 would occur in the sixth-order moment, 
8849 C           but not in a cluster cumulant
8850 #ifdef MOMENT
8851       s1=dip(1,jj,i)*dip(1,kk,k)
8852 #endif
8853       call matvec2(ADtEA1(1,1,1),Ub2(1,k),auxvec(1))
8854       s2=-0.5d0*scalar2(Ub2(1,i),auxvec(1))
8855       call matvec2(ADtEA(1,1,2),Ub2(1,l),auxvec1(1))
8856       s3=-0.5d0*scalar2(Ub2(1,j),auxvec1(1))
8857       call transpose2(EUg(1,1,k),auxmat(1,1))
8858       call matmat2(ADtEA1(1,1,1),auxmat(1,1),pizda(1,1))
8859       vv(1)=pizda(1,1)-pizda(2,2)
8860       vv(2)=pizda(1,2)+pizda(2,1)
8861       s4=-0.25d0*scalar2(vv(1),Dtobr2(1,i))
8862 cd      write (2,*) 'eello6_graph2:','s1',s1,' s2',s2,' s3',s3,' s4',s4
8863 #ifdef MOMENT
8864       eello6_graph2=-(s1+s2+s3+s4)
8865 #else
8866       eello6_graph2=-(s2+s3+s4)
8867 #endif
8868 c      eello6_graph2=-s3
8869 C Derivatives in gamma(i-1)
8870       if (i.gt.1) then
8871 #ifdef MOMENT
8872         s1=dipderg(1,jj,i)*dip(1,kk,k)
8873 #endif
8874         s2=-0.5d0*scalar2(Ub2der(1,i),auxvec(1))
8875         call matvec2(ADtEAderg(1,1,1,2),Ub2(1,l),auxvec2(1))
8876         s3=-0.5d0*scalar2(Ub2(1,j),auxvec2(1))
8877         s4=-0.25d0*scalar2(vv(1),Dtobr2der(1,i))
8878 #ifdef MOMENT
8879         g_corr6_loc(i-1)=g_corr6_loc(i-1)-ekont*(s1+s2+s3+s4)
8880 #else
8881         g_corr6_loc(i-1)=g_corr6_loc(i-1)-ekont*(s2+s3+s4)
8882 #endif
8883 c        g_corr6_loc(i-1)=g_corr6_loc(i-1)-s3
8884       endif
8885 C Derivatives in gamma(k-1)
8886 #ifdef MOMENT
8887       s1=dip(1,jj,i)*dipderg(1,kk,k)
8888 #endif
8889       call matvec2(ADtEA1(1,1,1),Ub2der(1,k),auxvec2(1))
8890       s2=-0.5d0*scalar2(Ub2(1,i),auxvec2(1))
8891       call matvec2(ADtEAderg(1,1,2,2),Ub2(1,l),auxvec2(1))
8892       s3=-0.5d0*scalar2(Ub2(1,j),auxvec2(1))
8893       call transpose2(EUgder(1,1,k),auxmat1(1,1))
8894       call matmat2(ADtEA1(1,1,1),auxmat1(1,1),pizda(1,1))
8895       vv(1)=pizda(1,1)-pizda(2,2)
8896       vv(2)=pizda(1,2)+pizda(2,1)
8897       s4=-0.25d0*scalar2(vv(1),Dtobr2(1,i))
8898 #ifdef MOMENT
8899       g_corr6_loc(k-1)=g_corr6_loc(k-1)-ekont*(s1+s2+s3+s4)
8900 #else
8901       g_corr6_loc(k-1)=g_corr6_loc(k-1)-ekont*(s2+s3+s4)
8902 #endif
8903 c      g_corr6_loc(k-1)=g_corr6_loc(k-1)-s3
8904 C Derivatives in gamma(j-1) or gamma(l-1)
8905       if (j.gt.1) then
8906 #ifdef MOMENT
8907         s1=dipderg(3,jj,i)*dip(1,kk,k) 
8908 #endif
8909         call matvec2(ADtEA1derg(1,1,1,1),Ub2(1,k),auxvec2(1))
8910         s2=-0.5d0*scalar2(Ub2(1,i),auxvec2(1))
8911         s3=-0.5d0*scalar2(Ub2der(1,j),auxvec1(1))
8912         call matmat2(ADtEA1derg(1,1,1,1),auxmat(1,1),pizda(1,1))
8913         vv(1)=pizda(1,1)-pizda(2,2)
8914         vv(2)=pizda(1,2)+pizda(2,1)
8915         s4=-0.25d0*scalar2(vv(1),Dtobr2(1,i))
8916 #ifdef MOMENT
8917         if (swap) then
8918           g_corr6_loc(l-1)=g_corr6_loc(l-1)-ekont*s1
8919         else
8920           g_corr6_loc(j-1)=g_corr6_loc(j-1)-ekont*s1
8921         endif
8922 #endif
8923         g_corr6_loc(j-1)=g_corr6_loc(j-1)-ekont*(s2+s3+s4)
8924 c        g_corr6_loc(j-1)=g_corr6_loc(j-1)-s3
8925       endif
8926 C Derivatives in gamma(l-1) or gamma(j-1)
8927       if (l.gt.1) then 
8928 #ifdef MOMENT
8929         s1=dip(1,jj,i)*dipderg(3,kk,k)
8930 #endif
8931         call matvec2(ADtEA1derg(1,1,2,1),Ub2(1,k),auxvec2(1))
8932         s2=-0.5d0*scalar2(Ub2(1,i),auxvec2(1))
8933         call matvec2(ADtEA(1,1,2),Ub2der(1,l),auxvec2(1))
8934         s3=-0.5d0*scalar2(Ub2(1,j),auxvec2(1))
8935         call matmat2(ADtEA1derg(1,1,2,1),auxmat(1,1),pizda(1,1))
8936         vv(1)=pizda(1,1)-pizda(2,2)
8937         vv(2)=pizda(1,2)+pizda(2,1)
8938         s4=-0.25d0*scalar2(vv(1),Dtobr2(1,i))
8939 #ifdef MOMENT
8940         if (swap) then
8941           g_corr6_loc(j-1)=g_corr6_loc(j-1)-ekont*s1
8942         else
8943           g_corr6_loc(l-1)=g_corr6_loc(l-1)-ekont*s1
8944         endif
8945 #endif
8946         g_corr6_loc(l-1)=g_corr6_loc(l-1)-ekont*(s2+s3+s4)
8947 c        g_corr6_loc(l-1)=g_corr6_loc(l-1)-s3
8948       endif
8949 C Cartesian derivatives.
8950       if (lprn) then
8951         write (2,*) 'In eello6_graph2'
8952         do iii=1,2
8953           write (2,*) 'iii=',iii
8954           do kkk=1,5
8955             write (2,*) 'kkk=',kkk
8956             do jjj=1,2
8957               write (2,'(3(2f10.5),5x)') 
8958      &        ((ADtEA1derx(jjj,mmm,lll,kkk,iii,1),mmm=1,2),lll=1,3)
8959             enddo
8960           enddo
8961         enddo
8962       endif
8963       do iii=1,2
8964         do kkk=1,5
8965           do lll=1,3
8966 #ifdef MOMENT
8967             if (iii.eq.1) then
8968               s1=dipderx(lll,kkk,1,jj,i)*dip(1,kk,k)
8969             else
8970               s1=dip(1,jj,i)*dipderx(lll,kkk,1,kk,k)
8971             endif
8972 #endif
8973             call matvec2(ADtEA1derx(1,1,lll,kkk,iii,1),Ub2(1,k),
8974      &        auxvec(1))
8975             s2=-0.5d0*scalar2(Ub2(1,i),auxvec(1))
8976             call matvec2(ADtEAderx(1,1,lll,kkk,iii,2),Ub2(1,l),
8977      &        auxvec(1))
8978             s3=-0.5d0*scalar2(Ub2(1,j),auxvec(1))
8979             call transpose2(EUg(1,1,k),auxmat(1,1))
8980             call matmat2(ADtEA1derx(1,1,lll,kkk,iii,1),auxmat(1,1),
8981      &        pizda(1,1))
8982             vv(1)=pizda(1,1)-pizda(2,2)
8983             vv(2)=pizda(1,2)+pizda(2,1)
8984             s4=-0.25d0*scalar2(vv(1),Dtobr2(1,i))
8985 cd            write (2,*) 's1',s1,' s2',s2,' s3',s3,' s4',s4
8986 #ifdef MOMENT
8987             derx(lll,kkk,iii)=derx(lll,kkk,iii)-(s1+s2+s4)
8988 #else
8989             derx(lll,kkk,iii)=derx(lll,kkk,iii)-(s2+s4)
8990 #endif
8991             if (swap) then
8992               derx(lll,kkk,3-iii)=derx(lll,kkk,3-iii)-s3
8993             else
8994               derx(lll,kkk,iii)=derx(lll,kkk,iii)-s3
8995             endif
8996           enddo
8997         enddo
8998       enddo
8999       return
9000       end
9001 c----------------------------------------------------------------------------
9002       double precision function eello6_graph3(i,j,k,l,jj,kk,swap)
9003       implicit real*8 (a-h,o-z)
9004       include 'DIMENSIONS'
9005       include 'COMMON.IOUNITS'
9006       include 'COMMON.CHAIN'
9007       include 'COMMON.DERIV'
9008       include 'COMMON.INTERACT'
9009       include 'COMMON.CONTACTS'
9010       include 'COMMON.TORSION'
9011       include 'COMMON.VAR'
9012       include 'COMMON.GEO'
9013       double precision vv(2),pizda(2,2),auxmat(2,2),auxvec(2)
9014       logical swap
9015 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
9016 C                                                                              C 
9017 C      Parallel       Antiparallel                                             C
9018 C                                                                              C
9019 C          o             o                                                     C 
9020 C         /l\   /   \   /j\                                                    C 
9021 C        /   \ /     \ /   \                                                   C
9022 C       /| o |o       o| o |\                                                  C
9023 C       j|/k\|  /      |/k\|l /                                                C
9024 C        /   \ /       /   \ /                                                 C
9025 C       /     o       /     o                                                  C
9026 C       i             i                                                        C
9027 C                                                                              C
9028 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
9029 C
9030 C 4/7/01 AL Component s1 was removed, because it pertains to the respective 
9031 C           energy moment and not to the cluster cumulant.
9032       iti=itortyp(itype(i))
9033       if (j.lt.nres-1) then
9034         itj1=itortyp(itype(j+1))
9035       else
9036         itj1=ntortyp+1
9037       endif
9038       itk=itortyp(itype(k))
9039       itk1=itortyp(itype(k+1))
9040       if (l.lt.nres-1) then
9041         itl1=itortyp(itype(l+1))
9042       else
9043         itl1=ntortyp+1
9044       endif
9045 #ifdef MOMENT
9046       s1=dip(4,jj,i)*dip(4,kk,k)
9047 #endif
9048       call matvec2(AECA(1,1,1),b1(1,itk1),auxvec(1))
9049       s2=0.5d0*scalar2(b1(1,itk),auxvec(1))
9050       call matvec2(AECA(1,1,2),b1(1,itl1),auxvec(1))
9051       s3=0.5d0*scalar2(b1(1,itj1),auxvec(1))
9052       call transpose2(EE(1,1,itk),auxmat(1,1))
9053       call matmat2(auxmat(1,1),AECA(1,1,1),pizda(1,1))
9054       vv(1)=pizda(1,1)+pizda(2,2)
9055       vv(2)=pizda(2,1)-pizda(1,2)
9056       s4=-0.25d0*scalar2(vv(1),Ctobr(1,k))
9057 cd      write (2,*) 'eello6_graph3:','s1',s1,' s2',s2,' s3',s3,' s4',s4,
9058 cd     & "sum",-(s2+s3+s4)
9059 #ifdef MOMENT
9060       eello6_graph3=-(s1+s2+s3+s4)
9061 #else
9062       eello6_graph3=-(s2+s3+s4)
9063 #endif
9064 c      eello6_graph3=-s4
9065 C Derivatives in gamma(k-1)
9066       call matvec2(AECAderg(1,1,2),b1(1,itl1),auxvec(1))
9067       s3=0.5d0*scalar2(b1(1,itj1),auxvec(1))
9068       s4=-0.25d0*scalar2(vv(1),Ctobrder(1,k))
9069       g_corr6_loc(k-1)=g_corr6_loc(k-1)-ekont*(s3+s4)
9070 C Derivatives in gamma(l-1)
9071       call matvec2(AECAderg(1,1,1),b1(1,itk1),auxvec(1))
9072       s2=0.5d0*scalar2(b1(1,itk),auxvec(1))
9073       call matmat2(auxmat(1,1),AECAderg(1,1,1),pizda(1,1))
9074       vv(1)=pizda(1,1)+pizda(2,2)
9075       vv(2)=pizda(2,1)-pizda(1,2)
9076       s4=-0.25d0*scalar2(vv(1),Ctobr(1,k))
9077       g_corr6_loc(l-1)=g_corr6_loc(l-1)-ekont*(s2+s4) 
9078 C Cartesian derivatives.
9079       do iii=1,2
9080         do kkk=1,5
9081           do lll=1,3
9082 #ifdef MOMENT
9083             if (iii.eq.1) then
9084               s1=dipderx(lll,kkk,4,jj,i)*dip(4,kk,k)
9085             else
9086               s1=dip(4,jj,i)*dipderx(lll,kkk,4,kk,k)
9087             endif
9088 #endif
9089             call matvec2(AECAderx(1,1,lll,kkk,iii,1),b1(1,itk1),
9090      &        auxvec(1))
9091             s2=0.5d0*scalar2(b1(1,itk),auxvec(1))
9092             call matvec2(AECAderx(1,1,lll,kkk,iii,2),b1(1,itl1),
9093      &        auxvec(1))
9094             s3=0.5d0*scalar2(b1(1,itj1),auxvec(1))
9095             call matmat2(auxmat(1,1),AECAderx(1,1,lll,kkk,iii,1),
9096      &        pizda(1,1))
9097             vv(1)=pizda(1,1)+pizda(2,2)
9098             vv(2)=pizda(2,1)-pizda(1,2)
9099             s4=-0.25d0*scalar2(vv(1),Ctobr(1,k))
9100 #ifdef MOMENT
9101             derx(lll,kkk,iii)=derx(lll,kkk,iii)-(s1+s2+s4)
9102 #else
9103             derx(lll,kkk,iii)=derx(lll,kkk,iii)-(s2+s4)
9104 #endif
9105             if (swap) then
9106               derx(lll,kkk,3-iii)=derx(lll,kkk,3-iii)-s3
9107             else
9108               derx(lll,kkk,iii)=derx(lll,kkk,iii)-s3
9109             endif
9110 c            derx(lll,kkk,iii)=derx(lll,kkk,iii)-s4
9111           enddo
9112         enddo
9113       enddo
9114       return
9115       end
9116 c----------------------------------------------------------------------------
9117       double precision function eello6_graph4(i,j,k,l,jj,kk,imat,swap)
9118       implicit real*8 (a-h,o-z)
9119       include 'DIMENSIONS'
9120       include 'COMMON.IOUNITS'
9121       include 'COMMON.CHAIN'
9122       include 'COMMON.DERIV'
9123       include 'COMMON.INTERACT'
9124       include 'COMMON.CONTACTS'
9125       include 'COMMON.TORSION'
9126       include 'COMMON.VAR'
9127       include 'COMMON.GEO'
9128       include 'COMMON.FFIELD'
9129       double precision vv(2),pizda(2,2),auxmat(2,2),auxvec(2),
9130      & auxvec1(2),auxmat1(2,2)
9131       logical swap
9132 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
9133 C                                                                              C                       
9134 C      Parallel       Antiparallel                                             C
9135 C                                                                              C
9136 C          o             o                                                     C
9137 C         /l\   /   \   /j\                                                    C
9138 C        /   \ /     \ /   \                                                   C
9139 C       /| o |o       o| o |\                                                  C
9140 C     \ j|/k\|      \  |/k\|l                                                  C
9141 C      \ /   \       \ /   \                                                   C 
9142 C       o     \       o     \                                                  C
9143 C       i             i                                                        C
9144 C                                                                              C 
9145 CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
9146 C
9147 C 4/7/01 AL Component s1 was removed, because it pertains to the respective 
9148 C           energy moment and not to the cluster cumulant.
9149 cd      write (2,*) 'eello_graph4: wturn6',wturn6
9150       iti=itortyp(itype(i))
9151       itj=itortyp(itype(j))
9152       if (j.lt.nres-1) then
9153         itj1=itortyp(itype(j+1))
9154       else
9155         itj1=ntortyp+1
9156       endif
9157       itk=itortyp(itype(k))
9158       if (k.lt.nres-1) then
9159         itk1=itortyp(itype(k+1))
9160       else
9161         itk1=ntortyp+1
9162       endif
9163       itl=itortyp(itype(l))
9164       if (l.lt.nres-1) then
9165         itl1=itortyp(itype(l+1))
9166       else
9167         itl1=ntortyp+1
9168       endif
9169 cd      write (2,*) 'eello6_graph4:','i',i,' j',j,' k',k,' l',l
9170 cd      write (2,*) 'iti',iti,' itj',itj,' itj1',itj1,' itk',itk,
9171 cd     & ' itl',itl,' itl1',itl1
9172 #ifdef MOMENT
9173       if (imat.eq.1) then
9174         s1=dip(3,jj,i)*dip(3,kk,k)
9175       else
9176         s1=dip(2,jj,j)*dip(2,kk,l)
9177       endif
9178 #endif
9179       call matvec2(AECA(1,1,imat),Ub2(1,k),auxvec(1))
9180       s2=0.5d0*scalar2(Ub2(1,i),auxvec(1))
9181       if (j.eq.l+1) then
9182         call matvec2(ADtEA1(1,1,3-imat),b1(1,itj1),auxvec1(1))
9183         s3=-0.5d0*scalar2(b1(1,itj),auxvec1(1))
9184       else
9185         call matvec2(ADtEA1(1,1,3-imat),b1(1,itl1),auxvec1(1))
9186         s3=-0.5d0*scalar2(b1(1,itl),auxvec1(1))
9187       endif
9188       call transpose2(EUg(1,1,k),auxmat(1,1))
9189       call matmat2(AECA(1,1,imat),auxmat(1,1),pizda(1,1))
9190       vv(1)=pizda(1,1)-pizda(2,2)
9191       vv(2)=pizda(2,1)+pizda(1,2)
9192       s4=0.25d0*scalar2(vv(1),Dtobr2(1,i))
9193 cd      write (2,*) 'eello6_graph4:','s1',s1,' s2',s2,' s3',s3,' s4',s4
9194 #ifdef MOMENT
9195       eello6_graph4=-(s1+s2+s3+s4)
9196 #else
9197       eello6_graph4=-(s2+s3+s4)
9198 #endif
9199 C Derivatives in gamma(i-1)
9200       if (i.gt.1) then
9201 #ifdef MOMENT
9202         if (imat.eq.1) then
9203           s1=dipderg(2,jj,i)*dip(3,kk,k)
9204         else
9205           s1=dipderg(4,jj,j)*dip(2,kk,l)
9206         endif
9207 #endif
9208         s2=0.5d0*scalar2(Ub2der(1,i),auxvec(1))
9209         if (j.eq.l+1) then
9210           call matvec2(ADtEA1derg(1,1,1,3-imat),b1(1,itj1),auxvec1(1))
9211           s3=-0.5d0*scalar2(b1(1,itj),auxvec1(1))
9212         else
9213           call matvec2(ADtEA1derg(1,1,1,3-imat),b1(1,itl1),auxvec1(1))
9214           s3=-0.5d0*scalar2(b1(1,itl),auxvec1(1))
9215         endif
9216         s4=0.25d0*scalar2(vv(1),Dtobr2der(1,i))
9217         if (wturn6.gt.0.0d0 .and. k.eq.l+4 .and. i.eq.j+2) then
9218 cd          write (2,*) 'turn6 derivatives'
9219 #ifdef MOMENT
9220           gel_loc_turn6(i-1)=gel_loc_turn6(i-1)-ekont*(s1+s2+s3+s4)
9221 #else
9222           gel_loc_turn6(i-1)=gel_loc_turn6(i-1)-ekont*(s2+s3+s4)
9223 #endif
9224         else
9225 #ifdef MOMENT
9226           g_corr6_loc(i-1)=g_corr6_loc(i-1)-ekont*(s1+s2+s3+s4)
9227 #else
9228           g_corr6_loc(i-1)=g_corr6_loc(i-1)-ekont*(s2+s3+s4)
9229 #endif
9230         endif
9231       endif
9232 C Derivatives in gamma(k-1)
9233 #ifdef MOMENT
9234       if (imat.eq.1) then
9235         s1=dip(3,jj,i)*dipderg(2,kk,k)
9236       else
9237         s1=dip(2,jj,j)*dipderg(4,kk,l)
9238       endif
9239 #endif
9240       call matvec2(AECA(1,1,imat),Ub2der(1,k),auxvec1(1))
9241       s2=0.5d0*scalar2(Ub2(1,i),auxvec1(1))
9242       if (j.eq.l+1) then
9243         call matvec2(ADtEA1derg(1,1,2,3-imat),b1(1,itj1),auxvec1(1))
9244         s3=-0.5d0*scalar2(b1(1,itj),auxvec1(1))
9245       else
9246         call matvec2(ADtEA1derg(1,1,2,3-imat),b1(1,itl1),auxvec1(1))
9247         s3=-0.5d0*scalar2(b1(1,itl),auxvec1(1))
9248       endif
9249       call transpose2(EUgder(1,1,k),auxmat1(1,1))
9250       call matmat2(AECA(1,1,imat),auxmat1(1,1),pizda(1,1))
9251       vv(1)=pizda(1,1)-pizda(2,2)
9252       vv(2)=pizda(2,1)+pizda(1,2)
9253       s4=0.25d0*scalar2(vv(1),Dtobr2(1,i))
9254       if (wturn6.gt.0.0d0 .and. k.eq.l+4 .and. i.eq.j+2) then
9255 #ifdef MOMENT
9256         gel_loc_turn6(k-1)=gel_loc_turn6(k-1)-ekont*(s1+s2+s3+s4)
9257 #else
9258         gel_loc_turn6(k-1)=gel_loc_turn6(k-1)-ekont*(s2+s3+s4)
9259 #endif
9260       else
9261 #ifdef MOMENT
9262         g_corr6_loc(k-1)=g_corr6_loc(k-1)-ekont*(s1+s2+s3+s4)
9263 #else
9264         g_corr6_loc(k-1)=g_corr6_loc(k-1)-ekont*(s2+s3+s4)
9265 #endif
9266       endif
9267 C Derivatives in gamma(j-1) or gamma(l-1)
9268       if (l.eq.j+1 .and. l.gt.1) then
9269         call matvec2(AECAderg(1,1,imat),Ub2(1,k),auxvec(1))
9270         s2=0.5d0*scalar2(Ub2(1,i),auxvec(1))
9271         call matmat2(AECAderg(1,1,imat),auxmat(1,1),pizda(1,1))
9272         vv(1)=pizda(1,1)-pizda(2,2)
9273         vv(2)=pizda(2,1)+pizda(1,2)
9274         s4=0.25d0*scalar2(vv(1),Dtobr2(1,i))
9275         g_corr6_loc(l-1)=g_corr6_loc(l-1)-ekont*(s2+s4)
9276       else if (j.gt.1) then
9277         call matvec2(AECAderg(1,1,imat),Ub2(1,k),auxvec(1))
9278         s2=0.5d0*scalar2(Ub2(1,i),auxvec(1))
9279         call matmat2(AECAderg(1,1,imat),auxmat(1,1),pizda(1,1))
9280         vv(1)=pizda(1,1)-pizda(2,2)
9281         vv(2)=pizda(2,1)+pizda(1,2)
9282         s4=0.25d0*scalar2(vv(1),Dtobr2(1,i))
9283         if (wturn6.gt.0.0d0 .and. k.eq.l+4 .and. i.eq.j+2) then
9284           gel_loc_turn6(j-1)=gel_loc_turn6(j-1)-ekont*(s2+s4)
9285         else
9286           g_corr6_loc(j-1)=g_corr6_loc(j-1)-ekont*(s2+s4)
9287         endif
9288       endif
9289 C Cartesian derivatives.
9290       do iii=1,2
9291         do kkk=1,5
9292           do lll=1,3
9293 #ifdef MOMENT
9294             if (iii.eq.1) then
9295               if (imat.eq.1) then
9296                 s1=dipderx(lll,kkk,3,jj,i)*dip(3,kk,k)
9297               else
9298                 s1=dipderx(lll,kkk,2,jj,j)*dip(2,kk,l)
9299               endif
9300             else
9301               if (imat.eq.1) then
9302                 s1=dip(3,jj,i)*dipderx(lll,kkk,3,kk,k)
9303               else
9304                 s1=dip(2,jj,j)*dipderx(lll,kkk,2,kk,l)
9305               endif
9306             endif
9307 #endif
9308             call matvec2(AECAderx(1,1,lll,kkk,iii,imat),Ub2(1,k),
9309      &        auxvec(1))
9310             s2=0.5d0*scalar2(Ub2(1,i),auxvec(1))
9311             if (j.eq.l+1) then
9312               call matvec2(ADtEA1derx(1,1,lll,kkk,iii,3-imat),
9313      &          b1(1,itj1),auxvec(1))
9314               s3=-0.5d0*scalar2(b1(1,itj),auxvec(1))
9315             else
9316               call matvec2(ADtEA1derx(1,1,lll,kkk,iii,3-imat),
9317      &          b1(1,itl1),auxvec(1))
9318               s3=-0.5d0*scalar2(b1(1,itl),auxvec(1))
9319             endif
9320             call matmat2(AECAderx(1,1,lll,kkk,iii,imat),auxmat(1,1),
9321      &        pizda(1,1))
9322             vv(1)=pizda(1,1)-pizda(2,2)
9323             vv(2)=pizda(2,1)+pizda(1,2)
9324             s4=0.25d0*scalar2(vv(1),Dtobr2(1,i))
9325             if (swap) then
9326               if (wturn6.gt.0.0d0 .and. k.eq.l+4 .and. i.eq.j+2) then
9327 #ifdef MOMENT
9328                 derx_turn(lll,kkk,3-iii)=derx_turn(lll,kkk,3-iii)
9329      &             -(s1+s2+s4)
9330 #else
9331                 derx_turn(lll,kkk,3-iii)=derx_turn(lll,kkk,3-iii)
9332      &             -(s2+s4)
9333 #endif
9334                 derx_turn(lll,kkk,iii)=derx_turn(lll,kkk,iii)-s3
9335               else
9336 #ifdef MOMENT
9337                 derx(lll,kkk,3-iii)=derx(lll,kkk,3-iii)-(s1+s2+s4)
9338 #else
9339                 derx(lll,kkk,3-iii)=derx(lll,kkk,3-iii)-(s2+s4)
9340 #endif
9341                 derx(lll,kkk,iii)=derx(lll,kkk,iii)-s3
9342               endif
9343             else
9344 #ifdef MOMENT
9345               derx(lll,kkk,iii)=derx(lll,kkk,iii)-(s1+s2+s4)
9346 #else
9347               derx(lll,kkk,iii)=derx(lll,kkk,iii)-(s2+s4)
9348 #endif
9349               if (l.eq.j+1) then
9350                 derx(lll,kkk,iii)=derx(lll,kkk,iii)-s3
9351               else 
9352                 derx(lll,kkk,3-iii)=derx(lll,kkk,3-iii)-s3
9353               endif
9354             endif 
9355           enddo
9356         enddo
9357       enddo
9358       return
9359       end
9360 c----------------------------------------------------------------------------
9361       double precision function eello_turn6(i,jj,kk)
9362       implicit real*8 (a-h,o-z)
9363       include 'DIMENSIONS'
9364       include 'COMMON.IOUNITS'
9365       include 'COMMON.CHAIN'
9366       include 'COMMON.DERIV'
9367       include 'COMMON.INTERACT'
9368       include 'COMMON.CONTACTS'
9369       include 'COMMON.TORSION'
9370       include 'COMMON.VAR'
9371       include 'COMMON.GEO'
9372       double precision vtemp1(2),vtemp2(2),vtemp3(2),vtemp4(2),
9373      &  atemp(2,2),auxmat(2,2),achuj_temp(2,2),gtemp(2,2),gvec(2),
9374      &  ggg1(3),ggg2(3)
9375       double precision vtemp1d(2),vtemp2d(2),vtemp3d(2),vtemp4d(2),
9376      &  atempd(2,2),auxmatd(2,2),achuj_tempd(2,2),gtempd(2,2),gvecd(2)
9377 C 4/7/01 AL Components s1, s8, and s13 were removed, because they pertain to
9378 C           the respective energy moment and not to the cluster cumulant.
9379       s1=0.0d0
9380       s8=0.0d0
9381       s13=0.0d0
9382 c
9383       eello_turn6=0.0d0
9384       j=i+4
9385       k=i+1
9386       l=i+3
9387       iti=itortyp(itype(i))
9388       itk=itortyp(itype(k))
9389       itk1=itortyp(itype(k+1))
9390       itl=itortyp(itype(l))
9391       itj=itortyp(itype(j))
9392 cd      write (2,*) 'itk',itk,' itk1',itk1,' itl',itl,' itj',itj
9393 cd      write (2,*) 'i',i,' k',k,' j',j,' l',l
9394 cd      if (i.ne.1 .or. j.ne.3 .or. k.ne.2 .or. l.ne.4) then
9395 cd        eello6=0.0d0
9396 cd        return
9397 cd      endif
9398 cd      write (iout,*)
9399 cd     &   'EELLO6: Contacts have occurred for peptide groups',i,j,
9400 cd     &   ' and',k,l
9401 cd      call checkint_turn6(i,jj,kk,eel_turn6_num)
9402       do iii=1,2
9403         do kkk=1,5
9404           do lll=1,3
9405             derx_turn(lll,kkk,iii)=0.0d0
9406           enddo
9407         enddo
9408       enddo
9409 cd      eij=1.0d0
9410 cd      ekl=1.0d0
9411 cd      ekont=1.0d0
9412       eello6_5=eello6_graph4(l,k,j,i,kk,jj,2,.true.)
9413 cd      eello6_5=0.0d0
9414 cd      write (2,*) 'eello6_5',eello6_5
9415 #ifdef MOMENT
9416       call transpose2(AEA(1,1,1),auxmat(1,1))
9417       call matmat2(EUg(1,1,i+1),auxmat(1,1),auxmat(1,1))
9418       ss1=scalar2(Ub2(1,i+2),b1(1,itl))
9419       s1 = (auxmat(1,1)+auxmat(2,2))*ss1
9420 #endif
9421       call matvec2(EUg(1,1,i+2),b1(1,itl),vtemp1(1))
9422       call matvec2(AEA(1,1,1),vtemp1(1),vtemp1(1))
9423       s2 = scalar2(b1(1,itk),vtemp1(1))
9424 #ifdef MOMENT
9425       call transpose2(AEA(1,1,2),atemp(1,1))
9426       call matmat2(atemp(1,1),EUg(1,1,i+4),atemp(1,1))
9427       call matvec2(Ug2(1,1,i+2),dd(1,1,itk1),vtemp2(1))
9428       s8 = -(atemp(1,1)+atemp(2,2))*scalar2(cc(1,1,itl),vtemp2(1))
9429 #endif
9430       call matmat2(EUg(1,1,i+3),AEA(1,1,2),auxmat(1,1))
9431       call matvec2(auxmat(1,1),Ub2(1,i+4),vtemp3(1))
9432       s12 = scalar2(Ub2(1,i+2),vtemp3(1))
9433 #ifdef MOMENT
9434       call transpose2(a_chuj(1,1,kk,i+1),achuj_temp(1,1))
9435       call matmat2(achuj_temp(1,1),EUg(1,1,i+2),gtemp(1,1))
9436       call matmat2(gtemp(1,1),EUg(1,1,i+3),gtemp(1,1)) 
9437       call matvec2(a_chuj(1,1,jj,i),Ub2(1,i+4),vtemp4(1)) 
9438       ss13 = scalar2(b1(1,itk),vtemp4(1))
9439       s13 = (gtemp(1,1)+gtemp(2,2))*ss13
9440 #endif
9441 c      write (2,*) 's1,s2,s8,s12,s13',s1,s2,s8,s12,s13
9442 c      s1=0.0d0
9443 c      s2=0.0d0
9444 c      s8=0.0d0
9445 c      s12=0.0d0
9446 c      s13=0.0d0
9447       eel_turn6 = eello6_5 - 0.5d0*(s1+s2+s12+s8+s13)
9448 C Derivatives in gamma(i+2)
9449       s1d =0.0d0
9450       s8d =0.0d0
9451 #ifdef MOMENT
9452       call transpose2(AEA(1,1,1),auxmatd(1,1))
9453       call matmat2(EUgder(1,1,i+1),auxmatd(1,1),auxmatd(1,1))
9454       s1d = (auxmatd(1,1)+auxmatd(2,2))*ss1
9455       call transpose2(AEAderg(1,1,2),atempd(1,1))
9456       call matmat2(atempd(1,1),EUg(1,1,i+4),atempd(1,1))
9457       s8d = -(atempd(1,1)+atempd(2,2))*scalar2(cc(1,1,itl),vtemp2(1))
9458 #endif
9459       call matmat2(EUg(1,1,i+3),AEAderg(1,1,2),auxmatd(1,1))
9460       call matvec2(auxmatd(1,1),Ub2(1,i+4),vtemp3d(1))
9461       s12d = scalar2(Ub2(1,i+2),vtemp3d(1))
9462 c      s1d=0.0d0
9463 c      s2d=0.0d0
9464 c      s8d=0.0d0
9465 c      s12d=0.0d0
9466 c      s13d=0.0d0
9467       gel_loc_turn6(i)=gel_loc_turn6(i)-0.5d0*ekont*(s1d+s8d+s12d)
9468 C Derivatives in gamma(i+3)
9469 #ifdef MOMENT
9470       call transpose2(AEA(1,1,1),auxmatd(1,1))
9471       call matmat2(EUg(1,1,i+1),auxmatd(1,1),auxmatd(1,1))
9472       ss1d=scalar2(Ub2der(1,i+2),b1(1,itl))
9473       s1d = (auxmatd(1,1)+auxmatd(2,2))*ss1d
9474 #endif
9475       call matvec2(EUgder(1,1,i+2),b1(1,itl),vtemp1d(1))
9476       call matvec2(AEA(1,1,1),vtemp1d(1),vtemp1d(1))
9477       s2d = scalar2(b1(1,itk),vtemp1d(1))
9478 #ifdef MOMENT
9479       call matvec2(Ug2der(1,1,i+2),dd(1,1,itk1),vtemp2d(1))
9480       s8d = -(atemp(1,1)+atemp(2,2))*scalar2(cc(1,1,itl),vtemp2d(1))
9481 #endif
9482       s12d = scalar2(Ub2der(1,i+2),vtemp3(1))
9483 #ifdef MOMENT
9484       call matmat2(achuj_temp(1,1),EUgder(1,1,i+2),gtempd(1,1))
9485       call matmat2(gtempd(1,1),EUg(1,1,i+3),gtempd(1,1)) 
9486       s13d = (gtempd(1,1)+gtempd(2,2))*ss13
9487 #endif
9488 c      s1d=0.0d0
9489 c      s2d=0.0d0
9490 c      s8d=0.0d0
9491 c      s12d=0.0d0
9492 c      s13d=0.0d0
9493 #ifdef MOMENT
9494       gel_loc_turn6(i+1)=gel_loc_turn6(i+1)
9495      &               -0.5d0*ekont*(s1d+s2d+s8d+s12d+s13d)
9496 #else
9497       gel_loc_turn6(i+1)=gel_loc_turn6(i+1)
9498      &               -0.5d0*ekont*(s2d+s12d)
9499 #endif
9500 C Derivatives in gamma(i+4)
9501       call matmat2(EUgder(1,1,i+3),AEA(1,1,2),auxmatd(1,1))
9502       call matvec2(auxmatd(1,1),Ub2(1,i+4),vtemp3d(1))
9503       s12d = scalar2(Ub2(1,i+2),vtemp3d(1))
9504 #ifdef MOMENT
9505       call matmat2(achuj_temp(1,1),EUg(1,1,i+2),gtempd(1,1))
9506       call matmat2(gtempd(1,1),EUgder(1,1,i+3),gtempd(1,1)) 
9507       s13d = (gtempd(1,1)+gtempd(2,2))*ss13
9508 #endif
9509 c      s1d=0.0d0
9510 c      s2d=0.0d0
9511 c      s8d=0.0d0
9512 C      s12d=0.0d0
9513 c      s13d=0.0d0
9514 #ifdef MOMENT
9515       gel_loc_turn6(i+2)=gel_loc_turn6(i+2)-0.5d0*ekont*(s12d+s13d)
9516 #else
9517       gel_loc_turn6(i+2)=gel_loc_turn6(i+2)-0.5d0*ekont*(s12d)
9518 #endif
9519 C Derivatives in gamma(i+5)
9520 #ifdef MOMENT
9521       call transpose2(AEAderg(1,1,1),auxmatd(1,1))
9522       call matmat2(EUg(1,1,i+1),auxmatd(1,1),auxmatd(1,1))
9523       s1d = (auxmatd(1,1)+auxmatd(2,2))*ss1
9524 #endif
9525       call matvec2(EUg(1,1,i+2),b1(1,itl),vtemp1d(1))
9526       call matvec2(AEAderg(1,1,1),vtemp1d(1),vtemp1d(1))
9527       s2d = scalar2(b1(1,itk),vtemp1d(1))
9528 #ifdef MOMENT
9529       call transpose2(AEA(1,1,2),atempd(1,1))
9530       call matmat2(atempd(1,1),EUgder(1,1,i+4),atempd(1,1))
9531       s8d = -(atempd(1,1)+atempd(2,2))*scalar2(cc(1,1,itl),vtemp2(1))
9532 #endif
9533       call matvec2(auxmat(1,1),Ub2der(1,i+4),vtemp3d(1))
9534       s12d = scalar2(Ub2(1,i+2),vtemp3d(1))
9535 #ifdef MOMENT
9536       call matvec2(a_chuj(1,1,jj,i),Ub2der(1,i+4),vtemp4d(1)) 
9537       ss13d = scalar2(b1(1,itk),vtemp4d(1))
9538       s13d = (gtemp(1,1)+gtemp(2,2))*ss13d
9539 #endif
9540 c      s1d=0.0d0
9541 c      s2d=0.0d0
9542 c      s8d=0.0d0
9543 c      s12d=0.0d0
9544 c      s13d=0.0d0
9545 #ifdef MOMENT
9546       gel_loc_turn6(i+3)=gel_loc_turn6(i+3)
9547      &               -0.5d0*ekont*(s1d+s2d+s8d+s12d+s13d)
9548 #else
9549       gel_loc_turn6(i+3)=gel_loc_turn6(i+3)
9550      &               -0.5d0*ekont*(s2d+s12d)
9551 #endif
9552 C Cartesian derivatives
9553       do iii=1,2
9554         do kkk=1,5
9555           do lll=1,3
9556 #ifdef MOMENT
9557             call transpose2(AEAderx(1,1,lll,kkk,iii,1),auxmatd(1,1))
9558             call matmat2(EUg(1,1,i+1),auxmatd(1,1),auxmatd(1,1))
9559             s1d = (auxmatd(1,1)+auxmatd(2,2))*ss1
9560 #endif
9561             call matvec2(EUg(1,1,i+2),b1(1,itl),vtemp1(1))
9562             call matvec2(AEAderx(1,1,lll,kkk,iii,1),vtemp1(1),
9563      &          vtemp1d(1))
9564             s2d = scalar2(b1(1,itk),vtemp1d(1))
9565 #ifdef MOMENT
9566             call transpose2(AEAderx(1,1,lll,kkk,iii,2),atempd(1,1))
9567             call matmat2(atempd(1,1),EUg(1,1,i+4),atempd(1,1))
9568             s8d = -(atempd(1,1)+atempd(2,2))*
9569      &           scalar2(cc(1,1,itl),vtemp2(1))
9570 #endif
9571             call matmat2(EUg(1,1,i+3),AEAderx(1,1,lll,kkk,iii,2),
9572      &           auxmatd(1,1))
9573             call matvec2(auxmatd(1,1),Ub2(1,i+4),vtemp3d(1))
9574             s12d = scalar2(Ub2(1,i+2),vtemp3d(1))
9575 c      s1d=0.0d0
9576 c      s2d=0.0d0
9577 c      s8d=0.0d0
9578 c      s12d=0.0d0
9579 c      s13d=0.0d0
9580 #ifdef MOMENT
9581             derx_turn(lll,kkk,iii) = derx_turn(lll,kkk,iii) 
9582      &        - 0.5d0*(s1d+s2d)
9583 #else
9584             derx_turn(lll,kkk,iii) = derx_turn(lll,kkk,iii) 
9585      &        - 0.5d0*s2d
9586 #endif
9587 #ifdef MOMENT
9588             derx_turn(lll,kkk,3-iii) = derx_turn(lll,kkk,3-iii) 
9589      &        - 0.5d0*(s8d+s12d)
9590 #else
9591             derx_turn(lll,kkk,3-iii) = derx_turn(lll,kkk,3-iii) 
9592      &        - 0.5d0*s12d
9593 #endif
9594           enddo
9595         enddo
9596       enddo
9597 #ifdef MOMENT
9598       do kkk=1,5
9599         do lll=1,3
9600           call transpose2(a_chuj_der(1,1,lll,kkk,kk,i+1),
9601      &      achuj_tempd(1,1))
9602           call matmat2(achuj_tempd(1,1),EUg(1,1,i+2),gtempd(1,1))
9603           call matmat2(gtempd(1,1),EUg(1,1,i+3),gtempd(1,1)) 
9604           s13d=(gtempd(1,1)+gtempd(2,2))*ss13
9605           derx_turn(lll,kkk,2) = derx_turn(lll,kkk,2)-0.5d0*s13d
9606           call matvec2(a_chuj_der(1,1,lll,kkk,jj,i),Ub2(1,i+4),
9607      &      vtemp4d(1)) 
9608           ss13d = scalar2(b1(1,itk),vtemp4d(1))
9609           s13d = (gtemp(1,1)+gtemp(2,2))*ss13d
9610           derx_turn(lll,kkk,1) = derx_turn(lll,kkk,1)-0.5d0*s13d
9611         enddo
9612       enddo
9613 #endif
9614 cd      write(iout,*) 'eel6_turn6',eel_turn6,' eel_turn6_num',
9615 cd     &  16*eel_turn6_num
9616 cd      goto 1112
9617       if (j.lt.nres-1) then
9618         j1=j+1
9619         j2=j-1
9620       else
9621         j1=j-1
9622         j2=j-2
9623       endif
9624       if (l.lt.nres-1) then
9625         l1=l+1
9626         l2=l-1
9627       else
9628         l1=l-1
9629         l2=l-2
9630       endif
9631       do ll=1,3
9632 cgrad        ggg1(ll)=eel_turn6*g_contij(ll,1)
9633 cgrad        ggg2(ll)=eel_turn6*g_contij(ll,2)
9634 cgrad        ghalf=0.5d0*ggg1(ll)
9635 cd        ghalf=0.0d0
9636         gturn6ij=eel_turn6*g_contij(ll,1)+ekont*derx_turn(ll,1,1)
9637         gturn6kl=eel_turn6*g_contij(ll,2)+ekont*derx_turn(ll,1,2)
9638         gcorr6_turn(ll,i)=gcorr6_turn(ll,i)!+ghalf
9639      &    +ekont*derx_turn(ll,2,1)
9640         gcorr6_turn(ll,i+1)=gcorr6_turn(ll,i+1)+ekont*derx_turn(ll,3,1)
9641         gcorr6_turn(ll,j)=gcorr6_turn(ll,j)!+ghalf
9642      &    +ekont*derx_turn(ll,4,1)
9643         gcorr6_turn(ll,j1)=gcorr6_turn(ll,j1)+ekont*derx_turn(ll,5,1)
9644         gcorr6_turn_long(ll,j)=gcorr6_turn_long(ll,j)+gturn6ij
9645         gcorr6_turn_long(ll,i)=gcorr6_turn_long(ll,i)-gturn6ij
9646 cgrad        ghalf=0.5d0*ggg2(ll)
9647 cd        ghalf=0.0d0
9648         gcorr6_turn(ll,k)=gcorr6_turn(ll,k)!+ghalf
9649      &    +ekont*derx_turn(ll,2,2)
9650         gcorr6_turn(ll,k+1)=gcorr6_turn(ll,k+1)+ekont*derx_turn(ll,3,2)
9651         gcorr6_turn(ll,l)=gcorr6_turn(ll,l)!+ghalf
9652      &    +ekont*derx_turn(ll,4,2)
9653         gcorr6_turn(ll,l1)=gcorr6_turn(ll,l1)+ekont*derx_turn(ll,5,2)
9654         gcorr6_turn_long(ll,l)=gcorr6_turn_long(ll,l)+gturn6kl
9655         gcorr6_turn_long(ll,k)=gcorr6_turn_long(ll,k)-gturn6kl
9656       enddo
9657 cd      goto 1112
9658 cgrad      do m=i+1,j-1
9659 cgrad        do ll=1,3
9660 cgrad          gcorr6_turn(ll,m)=gcorr6_turn(ll,m)+ggg1(ll)
9661 cgrad        enddo
9662 cgrad      enddo
9663 cgrad      do m=k+1,l-1
9664 cgrad        do ll=1,3
9665 cgrad          gcorr6_turn(ll,m)=gcorr6_turn(ll,m)+ggg2(ll)
9666 cgrad        enddo
9667 cgrad      enddo
9668 cgrad1112  continue
9669 cgrad      do m=i+2,j2
9670 cgrad        do ll=1,3
9671 cgrad          gcorr6_turn(ll,m)=gcorr6_turn(ll,m)+ekont*derx_turn(ll,1,1)
9672 cgrad        enddo
9673 cgrad      enddo
9674 cgrad      do m=k+2,l2
9675 cgrad        do ll=1,3
9676 cgrad          gcorr6_turn(ll,m)=gcorr6_turn(ll,m)+ekont*derx_turn(ll,1,2)
9677 cgrad        enddo
9678 cgrad      enddo 
9679 cd      do iii=1,nres-3
9680 cd        write (2,*) iii,g_corr6_loc(iii)
9681 cd      enddo
9682       eello_turn6=ekont*eel_turn6
9683 cd      write (2,*) 'ekont',ekont
9684 cd      write (2,*) 'eel_turn6',ekont*eel_turn6
9685       return
9686       end
9687
9688 C-----------------------------------------------------------------------------
9689       double precision function scalar(u,v)
9690 !DIR$ INLINEALWAYS scalar
9691 #ifndef OSF
9692 cDEC$ ATTRIBUTES FORCEINLINE::scalar
9693 #endif
9694       implicit none
9695       double precision u(3),v(3)
9696 cd      double precision sc
9697 cd      integer i
9698 cd      sc=0.0d0
9699 cd      do i=1,3
9700 cd        sc=sc+u(i)*v(i)
9701 cd      enddo
9702 cd      scalar=sc
9703
9704       scalar=u(1)*v(1)+u(2)*v(2)+u(3)*v(3)
9705       return
9706       end
9707 crc-------------------------------------------------
9708       SUBROUTINE MATVEC2(A1,V1,V2)
9709 !DIR$ INLINEALWAYS MATVEC2
9710 #ifndef OSF
9711 cDEC$ ATTRIBUTES FORCEINLINE::MATVEC2
9712 #endif
9713       implicit real*8 (a-h,o-z)
9714       include 'DIMENSIONS'
9715       DIMENSION A1(2,2),V1(2),V2(2)
9716 c      DO 1 I=1,2
9717 c        VI=0.0
9718 c        DO 3 K=1,2
9719 c    3     VI=VI+A1(I,K)*V1(K)
9720 c        Vaux(I)=VI
9721 c    1 CONTINUE
9722
9723       vaux1=a1(1,1)*v1(1)+a1(1,2)*v1(2)
9724       vaux2=a1(2,1)*v1(1)+a1(2,2)*v1(2)
9725
9726       v2(1)=vaux1
9727       v2(2)=vaux2
9728       END
9729 C---------------------------------------
9730       SUBROUTINE MATMAT2(A1,A2,A3)
9731 #ifndef OSF
9732 cDEC$ ATTRIBUTES FORCEINLINE::MATMAT2  
9733 #endif
9734       implicit real*8 (a-h,o-z)
9735       include 'DIMENSIONS'
9736       DIMENSION A1(2,2),A2(2,2),A3(2,2)
9737 c      DIMENSION AI3(2,2)
9738 c        DO  J=1,2
9739 c          A3IJ=0.0
9740 c          DO K=1,2
9741 c           A3IJ=A3IJ+A1(I,K)*A2(K,J)
9742 c          enddo
9743 c          A3(I,J)=A3IJ
9744 c       enddo
9745 c      enddo
9746
9747       ai3_11=a1(1,1)*a2(1,1)+a1(1,2)*a2(2,1)
9748       ai3_12=a1(1,1)*a2(1,2)+a1(1,2)*a2(2,2)
9749       ai3_21=a1(2,1)*a2(1,1)+a1(2,2)*a2(2,1)
9750       ai3_22=a1(2,1)*a2(1,2)+a1(2,2)*a2(2,2)
9751
9752       A3(1,1)=AI3_11
9753       A3(2,1)=AI3_21
9754       A3(1,2)=AI3_12
9755       A3(2,2)=AI3_22
9756       END
9757
9758 c-------------------------------------------------------------------------
9759       double precision function scalar2(u,v)
9760 !DIR$ INLINEALWAYS scalar2
9761       implicit none
9762       double precision u(2),v(2)
9763       double precision sc
9764       integer i
9765       scalar2=u(1)*v(1)+u(2)*v(2)
9766       return
9767       end
9768
9769 C-----------------------------------------------------------------------------
9770
9771       subroutine transpose2(a,at)
9772 !DIR$ INLINEALWAYS transpose2
9773 #ifndef OSF
9774 cDEC$ ATTRIBUTES FORCEINLINE::transpose2
9775 #endif
9776       implicit none
9777       double precision a(2,2),at(2,2)
9778       at(1,1)=a(1,1)
9779       at(1,2)=a(2,1)
9780       at(2,1)=a(1,2)
9781       at(2,2)=a(2,2)
9782       return
9783       end
9784 c--------------------------------------------------------------------------
9785       subroutine transpose(n,a,at)
9786       implicit none
9787       integer n,i,j
9788       double precision a(n,n),at(n,n)
9789       do i=1,n
9790         do j=1,n
9791           at(j,i)=a(i,j)
9792         enddo
9793       enddo
9794       return
9795       end
9796 C---------------------------------------------------------------------------
9797       subroutine prodmat3(a1,a2,kk,transp,prod)
9798 !DIR$ INLINEALWAYS prodmat3
9799 #ifndef OSF
9800 cDEC$ ATTRIBUTES FORCEINLINE::prodmat3
9801 #endif
9802       implicit none
9803       integer i,j
9804       double precision a1(2,2),a2(2,2),a2t(2,2),kk(2,2),prod(2,2)
9805       logical transp
9806 crc      double precision auxmat(2,2),prod_(2,2)
9807
9808       if (transp) then
9809 crc        call transpose2(kk(1,1),auxmat(1,1))
9810 crc        call matmat2(a1(1,1),auxmat(1,1),auxmat(1,1))
9811 crc        call matmat2(auxmat(1,1),a2(1,1),prod_(1,1)) 
9812         
9813            prod(1,1)=(a1(1,1)*kk(1,1)+a1(1,2)*kk(1,2))*a2(1,1)
9814      & +(a1(1,1)*kk(2,1)+a1(1,2)*kk(2,2))*a2(2,1)
9815            prod(1,2)=(a1(1,1)*kk(1,1)+a1(1,2)*kk(1,2))*a2(1,2)
9816      & +(a1(1,1)*kk(2,1)+a1(1,2)*kk(2,2))*a2(2,2)
9817            prod(2,1)=(a1(2,1)*kk(1,1)+a1(2,2)*kk(1,2))*a2(1,1)
9818      & +(a1(2,1)*kk(2,1)+a1(2,2)*kk(2,2))*a2(2,1)
9819            prod(2,2)=(a1(2,1)*kk(1,1)+a1(2,2)*kk(1,2))*a2(1,2)
9820      & +(a1(2,1)*kk(2,1)+a1(2,2)*kk(2,2))*a2(2,2)
9821
9822       else
9823 crc        call matmat2(a1(1,1),kk(1,1),auxmat(1,1))
9824 crc        call matmat2(auxmat(1,1),a2(1,1),prod_(1,1))
9825
9826            prod(1,1)=(a1(1,1)*kk(1,1)+a1(1,2)*kk(2,1))*a2(1,1)
9827      &  +(a1(1,1)*kk(1,2)+a1(1,2)*kk(2,2))*a2(2,1)
9828            prod(1,2)=(a1(1,1)*kk(1,1)+a1(1,2)*kk(2,1))*a2(1,2)
9829      &  +(a1(1,1)*kk(1,2)+a1(1,2)*kk(2,2))*a2(2,2)
9830            prod(2,1)=(a1(2,1)*kk(1,1)+a1(2,2)*kk(2,1))*a2(1,1)
9831      &  +(a1(2,1)*kk(1,2)+a1(2,2)*kk(2,2))*a2(2,1)
9832            prod(2,2)=(a1(2,1)*kk(1,1)+a1(2,2)*kk(2,1))*a2(1,2)
9833      &  +(a1(2,1)*kk(1,2)+a1(2,2)*kk(2,2))*a2(2,2)
9834
9835       endif
9836 c      call transpose2(a2(1,1),a2t(1,1))
9837
9838 crc      print *,transp
9839 crc      print *,((prod_(i,j),i=1,2),j=1,2)
9840 crc      print *,((prod(i,j),i=1,2),j=1,2)
9841
9842       return
9843       end
9844