491c676a1e42ad032e65d956bbb1539013bf0fed
[unres.git] / source / unres / src_MD-M / int_to_cart.f
1        subroutine int_to_cart
2 c--------------------------------------------------------------         
3 c  This subroutine converts the energy derivatives from internal 
4 c  coordinates to cartesian coordinates
5 c-------------------------------------------------------------
6       implicit real*8 (a-h,o-z)
7       include 'DIMENSIONS'
8       include 'COMMON.VAR'
9       include 'COMMON.CHAIN'
10       include 'COMMON.DERIV'
11       include 'COMMON.GEO'
12       include 'COMMON.LOCAL'
13       include 'COMMON.INTERACT'
14       include 'COMMON.MD'
15       include 'COMMON.IOUNITS'
16       include 'COMMON.SCCOR' 
17 c   calculating dE/ddc1     
18 C       print *,"wchodze22",ialph(2,1) 
19        if (nres.lt.3) go to 18
20        do j=1,3
21          gcart(j,1)=gcart(j,1)+gloc(1,icg)*dphi(j,1,4)
22      &     +gloc(nres-2,icg)*dtheta(j,1,3)       
23          if(itype(2).ne.10) then
24           gcart(j,1)=gcart(j,1)+gloc(ialph(2,1),icg)*dalpha(j,1,2)+
25      &    gloc(ialph(2,1)+nside,icg)*domega(j,1,2)              
26         endif
27        enddo
28 C       print *,"wchodze22",ialph(2,1)
29 c     Calculating the remainder of dE/ddc2
30        do j=1,3
31          gcart(j,2)=gcart(j,2)+gloc(1,icg)*dphi(j,2,4)+
32      &   gloc(nres-2,icg)*dtheta(j,2,3)+gloc(nres-1,icg)*dtheta(j,1,4)
33         if(itype(2).ne.10) then
34           gcart(j,2)=gcart(j,2)+gloc(ialph(2,1),icg)*dalpha(j,2,2)+
35      &    gloc(ialph(2,1)+nside,icg)*domega(j,2,2)
36         endif
37         if(itype(3).ne.10) then
38           gcart(j,2)=gcart(j,2)+gloc(ialph(3,1),icg)*dalpha(j,1,3)+
39      &    gloc(ialph(3,1)+nside,icg)*domega(j,1,3)
40         endif
41         if(nres.gt.4) then
42           gcart(j,2)=gcart(j,2)+gloc(2,icg)*dphi(j,1,5)
43         endif                   
44        enddo
45 C       print *,"wchodze22",ialph(2,1)
46 c  If there are only five residues       
47        if(nres.eq.5) then
48          do j=1,3
49            gcart(j,3)=gcart(j,3)+gloc(1,icg)*dphi(j,3,4)+gloc(2,icg)*
50      &     dphi(j,2,5)+gloc(nres-1,icg)*dtheta(j,2,4)+gloc(nres,icg)*
51      &     dtheta(j,1,5)
52          if(itype(3).ne.10) then
53            gcart(j,3)=gcart(j,3)+gloc(ialph(3,1),icg)*
54      &     dalpha(j,2,3)+gloc(ialph(3,1)+nside,icg)*domega(j,2,3)
55          endif
56          if(itype(4).ne.10) then
57            gcart(j,3)=gcart(j,3)+gloc(ialph(4,1),icg)*
58      &     dalpha(j,1,4)+gloc(ialph(4,1)+nside,icg)*domega(j,1,4)
59          endif
60         enddo
61        endif
62 c    If there are more than five residues
63       if(nres.gt.5) then                           
64 C       print *,"wchodze22",ialph(2,1)
65         do i=3,nres-3
66 C        print *,i,ialph(i,1)+nside
67          do j=1,3
68           gcart(j,i)=gcart(j,i)+gloc(i-2,icg)*dphi(j,3,i+1)
69      &    +gloc(i-1,icg)*dphi(j,2,i+2)+
70      &    gloc(i,icg)*dphi(j,1,i+3)+gloc(nres+i-4,icg)*dtheta(j,2,i+1)+
71      &    gloc(nres+i-3,icg)*dtheta(j,1,i+2)
72           if((itype(i).ne.10).and.(itype(i).ne.ntyp1)) then
73            gcart(j,i)=gcart(j,i)+gloc(ialph(i,1),icg)*dalpha(j,2,i)+
74      &     gloc(ialph(i,1)+nside,icg)*domega(j,2,i)
75           endif
76           if((itype(i+1).ne.10).and.(itype(i+1).ne.ntyp1)) then
77            gcart(j,i)=gcart(j,i)+gloc(ialph(i+1,1),icg)*dalpha(j,1,i+1)
78      &     +gloc(ialph(i+1,1)+nside,icg)*domega(j,1,i+1)
79           endif
80          enddo
81         enddo
82       endif     
83 C       print *,"wchodze22",ialph(2,1)
84
85 c  Setting dE/ddnres-2       
86       if(nres.gt.5) then
87          do j=1,3
88            gcart(j,nres-2)=gcart(j,nres-2)+gloc(nres-4,icg)*
89      &     dphi(j,3,nres-1)+gloc(nres-3,icg)*dphi(j,2,nres)
90      &     +gloc(2*nres-6,icg)*
91      &     dtheta(j,2,nres-1)+gloc(2*nres-5,icg)*dtheta(j,1,nres)
92           if(itype(nres-2).ne.10) then
93               gcart(j,nres-2)=gcart(j,nres-2)+gloc(ialph(nres-2,1),icg)*
94      &        dalpha(j,2,nres-2)+gloc(ialph(nres-2,1)+nside,icg)*
95      &        domega(j,2,nres-2)
96           endif
97           if(itype(nres-1).ne.10) then
98              gcart(j,nres-2)=gcart(j,nres-2)+gloc(ialph(nres-1,1),icg)*
99      &       dalpha(j,1,nres-1)+gloc(ialph(nres-1,1)+nside,icg)*
100      &       domega(j,1,nres-1)
101           endif
102          enddo
103       endif 
104 c  Settind dE/ddnres-1       
105        do j=1,3
106         gcart(j,nres-1)=gcart(j,nres-1)+gloc(nres-3,icg)*dphi(j,3,nres)+
107      &  gloc(2*nres-5,icg)*dtheta(j,2,nres)
108         if(itype(nres-1).ne.10) then
109           gcart(j,nres-1)=gcart(j,nres-1)+gloc(ialph(nres-1,1),icg)*
110      &    dalpha(j,2,nres-1)+gloc(ialph(nres-1,1)+nside,icg)*
111      &    domega(j,2,nres-1)
112         endif
113         enddo
114 c   The side-chain vector derivatives
115         do i=2,nres-1
116          if(itype(i).ne.10 .and. itype(i).ne.ntyp1) then        
117             do j=1,3    
118               gxcart(j,i)=gxcart(j,i)+gloc(ialph(i,1),icg)*dalpha(j,3,i)
119      &        +gloc(ialph(i,1)+nside,icg)*domega(j,3,i)
120             enddo
121          endif      
122        enddo                                                                                                                                                    
123 c----------------------------------------------------------------------
124 C INTERTYP=1 SC...Ca...Ca...Ca
125 C INTERTYP=2 Ca...Ca...Ca...SC
126 C INTERTYP=3 SC...Ca...Ca...SC
127 c   calculating dE/ddc1      
128   18   continue
129 c       do i=1,nres
130 c       gloc(i,icg)=0.0D0
131 c          write (iout,*) "poczotkoawy",i,gloc_sc(1,i,icg)
132 c       enddo
133 C       print *,"tu dochodze??"
134        if (nres.lt.2) return
135        if ((nres.lt.3).and.(itype(1).eq.10)) return
136        if ((itype(1).ne.10).and.(itype(1).ne.ntyp1)) then
137         do j=1,3
138 cc Derviative was calculated for oposite vector of side chain therefore
139 c there is "-" sign before gloc_sc
140          gxcart(j,1)=gxcart(j,1)-gloc_sc(1,0,icg)*
141      &     dtauangle(j,1,1,3)
142          gcart(j,1)=gcart(j,1)+gloc_sc(1,0,icg)*
143      &     dtauangle(j,1,2,3)
144           if ((itype(2).ne.10).and.(itype(2).ne.ntyp1)) then
145          gxcart(j,1)= gxcart(j,1)
146      &               -gloc_sc(3,0,icg)*dtauangle(j,3,1,3)
147          gcart(j,1)=gcart(j,1)+gloc_sc(3,0,icg)*
148      &       dtauangle(j,3,2,3)
149           endif
150        enddo
151        endif
152          if ((nres.ge.3).and.(itype(3).ne.10).and.(itype(3).ne.ntyp1))
153      & then
154          do j=1,3
155          gcart(j,1)=gcart(j,1)+gloc_sc(2,1,icg)*dtauangle(j,2,1,4)
156          enddo
157          endif
158 c   As potetnial DO NOT depend on omicron anlge their derivative is
159 c   ommited 
160 c     &     +gloc_sc(intertyp,nres-2,icg)*dtheta(j,1,3)  
161
162 c     Calculating the remainder of dE/ddc2
163        do j=1,3
164          if((itype(2).ne.10).and.(itype(2).ne.ntyp1)) then
165            if (itype(1).ne.10) gxcart(j,2)=gxcart(j,2)+
166      &                         gloc_sc(3,0,icg)*dtauangle(j,3,3,3)
167         if ((itype(3).ne.10).and.(nres.ge.3).and.(itype(3).ne.ntyp1))
168      &   then
169            gxcart(j,2)=gxcart(j,2)-gloc_sc(3,1,icg)*dtauangle(j,3,1,4)
170 cc                  the   - above is due to different vector direction
171            gcart(j,2)=gcart(j,2)+gloc_sc(3,1,icg)*dtauangle(j,3,2,4)
172           endif
173           if (nres.gt.3) then
174            gxcart(j,2)=gxcart(j,2)-gloc_sc(1,1,icg)*dtauangle(j,1,1,4)
175 cc                  the   - above is due to different vector direction
176            gcart(j,2)=gcart(j,2)+gloc_sc(1,1,icg)*dtauangle(j,1,2,4)
177 c          write(iout,*) gloc_sc(1,1,icg),dtauangle(j,1,2,4),"gcart"
178 c           write(iout,*) gloc_sc(1,1,icg),dtauangle(j,1,1,4),"gx"
179           endif
180          endif
181          if ((itype(1).ne.10).and.(itype(1).ne.ntyp1)) then
182           gcart(j,2)=gcart(j,2)+gloc_sc(1,0,icg)*dtauangle(j,1,3,3)
183 c           write(iout,*)  gloc_sc(1,0,icg),dtauangle(j,1,3,3)
184         endif
185          if ((itype(3).ne.10).and.(nres.ge.3)) then
186           gcart(j,2)=gcart(j,2)+gloc_sc(2,1,icg)*dtauangle(j,2,2,4)
187 c           write(iout,*) gloc_sc(2,1,icg),dtauangle(j,2,2,4)
188          endif
189          if ((itype(4).ne.10).and.(nres.ge.4)) then
190           gcart(j,2)=gcart(j,2)+gloc_sc(2,2,icg)*dtauangle(j,2,1,5)
191 c           write(iout,*) gloc_sc(2,2,icg),dtauangle(j,2,1,5)
192          endif
193
194 c      write(iout,*) gcart(j,2),itype(2),itype(1),itype(3), "gcart2"
195        enddo
196 c    If there are more than five residues
197       if(nres.ge.5) then                        
198         do i=3,nres-2
199          do j=1,3
200 c          write(iout,*) "before", gcart(j,i)
201           if ((itype(i).ne.10).and.(itype(i).ne.ntyp1)) then
202           gxcart(j,i)=gxcart(j,i)+gloc_sc(2,i-2,icg)
203      &    *dtauangle(j,2,3,i+1)
204      &    -gloc_sc(1,i-1,icg)*dtauangle(j,1,1,i+2)
205           gcart(j,i)=gcart(j,i)+gloc_sc(1,i-1,icg)
206      &    *dtauangle(j,1,2,i+2)
207 c                   write(iout,*) "new",j,i,
208 c     &  gcart(j,i),gloc_sc(1,i-1,icg),dtauangle(j,1,2,i+2)
209           if (itype(i-1).ne.10) then
210            gxcart(j,i)=gxcart(j,i)+gloc_sc(3,i-2,icg)
211      &*dtauangle(j,3,3,i+1)
212           endif
213           if (itype(i+1).ne.10) then
214            gxcart(j,i)=gxcart(j,i)-gloc_sc(3,i-1,icg)
215      &*dtauangle(j,3,1,i+2)
216            gcart(j,i)=gcart(j,i)+gloc_sc(3,i-1,icg)
217      &*dtauangle(j,3,2,i+2)
218           endif
219           endif
220           if (itype(i-1).ne.10) then
221            gcart(j,i)=gcart(j,i)+gloc_sc(1,i-2,icg)*
222      &     dtauangle(j,1,3,i+1)
223           endif
224           if (itype(i+1).ne.10) then
225            gcart(j,i)=gcart(j,i)+gloc_sc(2,i-1,icg)*
226      &     dtauangle(j,2,2,i+2)
227 c          write(iout,*) "numer",i,gloc_sc(2,i-1,icg),
228 c     &    dtauangle(j,2,2,i+2)
229           endif
230           if (itype(i+2).ne.10) then
231            gcart(j,i)=gcart(j,i)+gloc_sc(2,i,icg)*
232      &     dtauangle(j,2,1,i+3)
233           endif
234          enddo
235         enddo
236       endif     
237 c  Setting dE/ddnres-1       
238       if(nres.ge.4) then
239          do j=1,3
240          if ((itype(nres-1).ne.10).and.(itype(nres-1).ne.ntyp1)) then
241          gxcart(j,nres-1)=gxcart(j,nres-1)+gloc_sc(2,nres-3,icg)
242      &    *dtauangle(j,2,3,nres)
243 c          write (iout,*) "gxcart(nres-1)", gloc_sc(2,nres-3,icg),
244 c     &     dtauangle(j,2,3,nres), gxcart(j,nres-1)
245          if (itype(nres-2).ne.10) then
246         gxcart(j,nres-1)=gxcart(j,nres-1)+gloc_sc(3,nres-3,icg)
247      &    *dtauangle(j,3,3,nres)
248           endif
249          if ((itype(nres).ne.10).and.(itype(nres).ne.ntyp1)) then
250         gxcart(j,nres-1)=gxcart(j,nres-1)-gloc_sc(3,nres-2,icg)
251      &    *dtauangle(j,3,1,nres+1)
252         gcart(j,nres-1)=gcart(j,nres-1)+gloc_sc(3,nres-2,icg)
253      &    *dtauangle(j,3,2,nres+1)
254           endif
255          endif
256          if ((itype(nres-2).ne.10).and.(itype(nres-2).ne.ntyp1)) then
257             gcart(j,nres-1)=gcart(j,nres-1)+gloc_sc(1,nres-3,icg)*
258      &   dtauangle(j,1,3,nres)
259          endif
260           if ((itype(nres).ne.10).and.(itype(nres).ne.ntyp1)) then
261             gcart(j,nres-1)=gcart(j,nres-1)+gloc_sc(2,nres-2,icg)*
262      &     dtauangle(j,2,2,nres+1)
263 c           write (iout,*) "gcart(nres-1)", gloc_sc(2,nres-2,icg),
264 c     &     dtauangle(j,2,2,nres+1), itype(nres-1),itype(nres)
265            endif
266          enddo
267       endif
268 c  Settind dE/ddnres       
269        if ((nres.ge.3).and.(itype(nres).ne.10).and.
270      &    (itype(nres).ne.ntyp1))then
271        do j=1,3
272         gxcart(j,nres)=gxcart(j,nres)+gloc_sc(3,nres-2,icg)
273      & *dtauangle(j,3,3,nres+1)+gloc_sc(2,nres-2,icg)
274      & *dtauangle(j,2,3,nres+1)
275         enddo
276        endif
277 c   The side-chain vector derivatives
278       return
279       end       
280         
281