added/modified CMake files for all subdirs in source/unres + junkfiles cleanup
[unres.git] / source / unres / src_MD-M / gradient_p.F
1       subroutine gradient(n,x,nf,g,uiparm,urparm,ufparm)
2       implicit real*8 (a-h,o-z)
3       include 'DIMENSIONS'
4       include 'COMMON.CHAIN'
5       include 'COMMON.DERIV'
6       include 'COMMON.VAR'
7       include 'COMMON.INTERACT'
8       include 'COMMON.FFIELD'
9       include 'COMMON.MD'
10       include 'COMMON.IOUNITS'
11       external ufparm
12       integer uiparm(1)
13       double precision urparm(1)
14       dimension x(maxvar),g(maxvar)
15 c
16 c This subroutine calculates total internal coordinate gradient.
17 c Depending on the number of function evaluations, either whole energy 
18 c is evaluated beforehand, Cartesian coordinates and their derivatives in 
19 c internal coordinates are reevaluated or only the cartesian-in-internal
20 c coordinate derivatives are evaluated. The subroutine was designed to work
21 c with SUMSL.
22
23 c
24       icg=mod(nf,2)+1
25
26 cd      print *,'grad',nf,icg
27       if (nf-nfl+1) 20,30,40
28    20 call func(n,x,nf,f,uiparm,urparm,ufparm)
29 c     write (iout,*) 'grad 20'
30       if (nf.eq.0) return
31       goto 40
32    30 call var_to_geom(n,x)
33       call chainbuild 
34 c     write (iout,*) 'grad 30'
35 C
36 C Evaluate the derivatives of virtual bond lengths and SC vectors in variables.
37 C
38    40 call cartder
39 c     write (iout,*) 'grad 40'
40 c     print *,'GRADIENT: nnt=',nnt,' nct=',nct,' expon=',expon
41 C
42 C Convert the Cartesian gradient into internal-coordinate gradient.
43 C
44       ind=0
45       ind1=0
46       do i=1,nres-2
47         gthetai=0.0D0
48         gphii=0.0D0
49         do j=i+1,nres-1
50           ind=ind+1
51 c         ind=indmat(i,j)
52 c         print *,'GRAD: i=',i,' jc=',j,' ind=',ind
53           do k=1,3
54             gthetai=gthetai+dcdv(k,ind)*gradc(k,j,icg)
55           enddo
56           do k=1,3
57             gphii=gphii+dcdv(k+3,ind)*gradc(k,j,icg)
58           enddo
59         enddo
60         do j=i+1,nres-1
61           ind1=ind1+1
62 c         ind1=indmat(i,j)
63 c         print *,'GRAD: i=',i,' jx=',j,' ind1=',ind1
64           do k=1,3
65             gthetai=gthetai+dxdv(k,ind1)*gradx(k,j,icg)
66             gphii=gphii+dxdv(k+3,ind1)*gradx(k,j,icg)
67           enddo
68         enddo
69         if (i.gt.1) g(i-1)=gphii
70         if (n.gt.nphi) g(nphi+i)=gthetai
71       enddo
72       if (n.le.nphi+ntheta) goto 10
73       do i=2,nres-1
74         if (itype(i).ne.10) then
75           galphai=0.0D0
76           gomegai=0.0D0
77           do k=1,3
78             galphai=galphai+dxds(k,i)*gradx(k,i,icg)
79           enddo
80           do k=1,3
81             gomegai=gomegai+dxds(k+3,i)*gradx(k,i,icg)
82           enddo
83           g(ialph(i,1))=galphai
84           g(ialph(i,1)+nside)=gomegai
85         endif
86       enddo
87 C
88 C Add the components corresponding to local energy terms.
89 C
90    10 continue
91       do i=1,nvar
92 cd      write (iout,*) 'i=',i,'g=',g(i),' gloc=',gloc(i,icg)
93         g(i)=g(i)+gloc(i,icg)
94       enddo
95 C Uncomment following three lines for diagnostics.
96 cd    call intout
97 cd    call briefout(0,0.0d0)
98 cd    write (iout,'(i3,1pe15.5)') (k,g(k),k=1,n)
99       return
100       end
101 C-------------------------------------------------------------------------
102       subroutine grad_restr(n,x,nf,g,uiparm,urparm,ufparm)
103       implicit real*8 (a-h,o-z)
104       include 'DIMENSIONS'
105       include 'COMMON.CHAIN'
106       include 'COMMON.DERIV'
107       include 'COMMON.VAR'
108       include 'COMMON.INTERACT'
109       include 'COMMON.FFIELD'
110       include 'COMMON.IOUNITS'
111       external ufparm
112       integer uiparm(1)
113       double precision urparm(1)
114       dimension x(maxvar),g(maxvar)
115
116       icg=mod(nf,2)+1
117       if (nf-nfl+1) 20,30,40
118    20 call func_restr(n,x,nf,f,uiparm,urparm,ufparm)
119 c     write (iout,*) 'grad 20'
120       if (nf.eq.0) return
121       goto 40
122    30 continue
123 #ifdef OSF
124 c     Intercept NaNs in the coordinates
125 c      write(iout,*) (var(i),i=1,nvar)
126       x_sum=0.D0
127       do i=1,n
128         x_sum=x_sum+x(i)
129       enddo
130       if (x_sum.ne.x_sum) then
131         write(iout,*)" *** grad_restr : Found NaN in coordinates"
132         call flush(iout)
133         print *," *** grad_restr : Found NaN in coordinates"
134         return
135       endif
136 #endif
137       call var_to_geom_restr(n,x)
138       call chainbuild 
139 C
140 C Evaluate the derivatives of virtual bond lengths and SC vectors in variables.
141 C
142    40 call cartder
143 C
144 C Convert the Cartesian gradient into internal-coordinate gradient.
145 C
146
147       ig=0
148       ind=nres-2                                                                    
149       do i=2,nres-2                
150        IF (mask_phi(i+2).eq.1) THEN                                             
151         gphii=0.0D0                                                             
152         do j=i+1,nres-1                                                         
153           ind=ind+1                                 
154           do k=1,3                                                              
155             gphii=gphii+dcdv(k+3,ind)*gradc(k,j,icg)                            
156             gphii=gphii+dxdv(k+3,ind)*gradx(k,j,icg)                           
157           enddo                                                                 
158         enddo                                                                   
159         ig=ig+1
160         g(ig)=gphii
161        ELSE
162         ind=ind+nres-1-i
163        ENDIF
164       enddo                                        
165
166
167       ind=0
168       do i=1,nres-2
169        IF (mask_theta(i+2).eq.1) THEN
170         ig=ig+1
171         gthetai=0.0D0
172         do j=i+1,nres-1
173           ind=ind+1
174           do k=1,3
175             gthetai=gthetai+dcdv(k,ind)*gradc(k,j,icg)
176             gthetai=gthetai+dxdv(k,ind)*gradx(k,j,icg)
177           enddo
178         enddo
179         g(ig)=gthetai
180        ELSE
181         ind=ind+nres-1-i
182        ENDIF
183       enddo
184
185       do i=2,nres-1
186         if (itype(i).ne.10) then
187          IF (mask_side(i).eq.1) THEN
188           ig=ig+1
189           galphai=0.0D0
190           do k=1,3
191             galphai=galphai+dxds(k,i)*gradx(k,i,icg)
192           enddo
193           g(ig)=galphai
194          ENDIF
195         endif
196       enddo
197
198       
199       do i=2,nres-1
200         if (itype(i).ne.10) then
201          IF (mask_side(i).eq.1) THEN
202           ig=ig+1
203           gomegai=0.0D0
204           do k=1,3
205             gomegai=gomegai+dxds(k+3,i)*gradx(k,i,icg)
206           enddo
207           g(ig)=gomegai
208          ENDIF
209         endif
210       enddo
211
212 C
213 C Add the components corresponding to local energy terms.
214 C
215
216       ig=0
217       igall=0
218       do i=4,nres
219         igall=igall+1
220         if (mask_phi(i).eq.1) then
221           ig=ig+1
222           g(ig)=g(ig)+gloc(igall,icg)
223         endif
224       enddo
225
226       do i=3,nres
227         igall=igall+1
228         if (mask_theta(i).eq.1) then
229           ig=ig+1
230           g(ig)=g(ig)+gloc(igall,icg)
231         endif
232       enddo
233      
234       do ij=1,2
235       do i=2,nres-1
236         if (itype(i).ne.10) then
237           igall=igall+1
238           if (mask_side(i).eq.1) then
239             ig=ig+1
240             g(ig)=g(ig)+gloc(igall,icg)
241           endif
242         endif
243       enddo
244       enddo
245
246 cd      do i=1,ig
247 cd        write (iout,'(a2,i5,a3,f25.8)') 'i=',i,' g=',g(i)
248 cd      enddo
249       return
250       end
251 C-------------------------------------------------------------------------
252       subroutine cartgrad
253       implicit real*8 (a-h,o-z)
254       include 'DIMENSIONS'
255 #ifdef MPI
256       include 'mpif.h'
257 #endif
258       include 'COMMON.CHAIN'
259       include 'COMMON.DERIV'
260       include 'COMMON.VAR'
261       include 'COMMON.INTERACT'
262       include 'COMMON.FFIELD'
263       include 'COMMON.MD'
264       include 'COMMON.IOUNITS'
265       include 'COMMON.TIME1'
266 c
267 c This subrouting calculates total Cartesian coordinate gradient. 
268 c The subroutine chainbuild_cart and energy MUST be called beforehand.
269 c
270 #ifdef TIMING
271       time00=MPI_Wtime()
272 #endif
273       icg=1
274       call sum_gradient
275 #ifdef TIMING
276 #endif
277 #ifdef DEBUG
278       write (iout,*) "After sum_gradient"
279       do i=1,nres-1
280         write (iout,*) i," gradc  ",(gradc(j,i,icg),j=1,3)
281         write (iout,*) i," gradx  ",(gradx(j,i,icg),j=1,3)
282       enddo
283 #endif
284 c If performing constraint dynamics, add the gradients of the constraint energy
285       if(usampl.and.totT.gt.eq_time) then
286          do i=1,nct
287            do j=1,3
288              gradc(j,i,icg)=gradc(j,i,icg)+dudconst(j,i)+duscdiff(j,i)
289              gradx(j,i,icg)=gradx(j,i,icg)+dudxconst(j,i)+duscdiffx(j,i)
290            enddo
291          enddo
292          do i=1,nres-3
293            gloc(i,icg)=gloc(i,icg)+dugamma(i)
294          enddo
295          do i=1,nres-2
296            gloc(nphi+i,icg)=gloc(nphi+i,icg)+dutheta(i)
297          enddo
298       endif 
299 #ifdef TIMING
300       time01=MPI_Wtime()
301 #endif
302       call intcartderiv
303 #ifdef TIMING
304       time_intcartderiv=time_intcartderiv+MPI_Wtime()-time01
305 #endif
306 cd      call checkintcartgrad
307 cd      write(iout,*) 'calling int_to_cart'
308 #ifdef DEBUG
309       write (iout,*) "gcart, gxcart, gloc before int_to_cart"
310 #endif
311       do i=1,nct
312         do j=1,3
313           gcart(j,i)=gradc(j,i,icg)
314           gxcart(j,i)=gradx(j,i,icg)
315         enddo
316 #ifdef DEBUG
317         write (iout,'(i5,2(3f10.5,5x),f10.5)') i,(gcart(j,i),j=1,3),
318      &    (gxcart(j,i),j=1,3),gloc(i,icg)
319 #endif
320       enddo
321 #ifdef TIMING
322       time01=MPI_Wtime()
323 #endif
324       call int_to_cart
325 #ifdef TIMING
326       time_inttocart=time_inttocart+MPI_Wtime()-time01
327 #endif
328 #ifdef DEBUG
329       write (iout,*) "gcart and gxcart after int_to_cart"
330       do i=0,nres-1
331         write (iout,'(i5,3f10.5,5x,3f10.5)') i,(gcart(j,i),j=1,3),
332      &      (gxcart(j,i),j=1,3)
333       enddo
334 #endif
335 #ifdef TIMING
336       time_cartgrad=time_cartgrad+MPI_Wtime()-time00
337 #endif
338       return
339       end
340 C-------------------------------------------------------------------------
341       subroutine zerograd
342       implicit real*8 (a-h,o-z)
343       include 'DIMENSIONS'
344       include 'COMMON.DERIV'
345       include 'COMMON.CHAIN'
346       include 'COMMON.VAR'
347       include 'COMMON.MD'
348 C
349 C Initialize Cartesian-coordinate gradient
350 C
351       do i=1,nres
352         do j=1,3
353           gvdwx(j,i)=0.0D0
354           gradx_scp(j,i)=0.0D0
355           gvdwc(j,i)=0.0D0
356           gvdwc_scp(j,i)=0.0D0
357           gvdwc_scpp(j,i)=0.0d0
358           gelc (j,i)=0.0D0
359           gelc_long(j,i)=0.0D0
360           gradb(j,i)=0.0d0
361           gradbx(j,i)=0.0d0
362           gvdwpp(j,i)=0.0d0
363           gel_loc(j,i)=0.0d0
364           gel_loc_long(j,i)=0.0d0
365           ghpbc(j,i)=0.0D0
366           ghpbx(j,i)=0.0D0
367           gcorr3_turn(j,i)=0.0d0
368           gcorr4_turn(j,i)=0.0d0
369           gradcorr(j,i)=0.0d0
370           gradcorr_long(j,i)=0.0d0
371           gradcorr5_long(j,i)=0.0d0
372           gradcorr6_long(j,i)=0.0d0
373           gcorr6_turn_long(j,i)=0.0d0
374           gradcorr5(j,i)=0.0d0
375           gradcorr6(j,i)=0.0d0
376           gcorr6_turn(j,i)=0.0d0
377           gsccorc(j,i)=0.0d0
378           gsccorx(j,i)=0.0d0
379           gradc(j,i,icg)=0.0d0
380           gradx(j,i,icg)=0.0d0
381           gscloc(j,i)=0.0d0
382           gsclocx(j,i)=0.0d0
383         enddo
384       enddo
385 C
386 C Initialize the gradient of local energy terms.
387 C
388       do i=1,4*nres
389         gloc(i,icg)=0.0D0
390       enddo
391       do i=1,nres
392         gel_loc_loc(i)=0.0d0
393         gcorr_loc(i)=0.0d0
394         g_corr5_loc(i)=0.0d0
395         g_corr6_loc(i)=0.0d0
396         gel_loc_turn3(i)=0.0d0
397         gel_loc_turn4(i)=0.0d0
398         gel_loc_turn6(i)=0.0d0
399         gsccor_loc(i)=0.0d0
400       enddo
401 c initialize gcart and gxcart
402       do i=0,nres
403         do j=1,3
404           gcart(j,i)=0.0d0
405           gxcart(j,i)=0.0d0
406         enddo
407       enddo
408       return
409       end
410 c-------------------------------------------------------------------------
411       double precision function fdum()
412       fdum=0.0D0
413       return
414       end