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