Merge branch 'homology' of mmka.chem.univ.gda.pl:unres into homology
[unres.git] / source / unres / src_CSA / convert.f
1       subroutine geom_to_var(n,x)
2 C
3 C Transfer the geometry parameters to the variable array.
4 C The positions of variables are as follows:
5 C 1. Virtual-bond torsional angles: 1 thru nres-3
6 C 2. Virtual-bond valence angles: nres-2 thru 2*nres-5
7 C 3. The polar angles alpha of local SC orientation: 2*nres-4 thru 
8 C    2*nres-4+nside
9 C 4. The torsional angles omega of SC orientation: 2*nres-4+nside+1
10 C    thru 2*nre-4+2*nside 
11 C
12       implicit real*8 (a-h,o-z)
13       include 'DIMENSIONS'
14       include 'COMMON.VAR'
15       include 'COMMON.GEO'
16       include 'COMMON.CHAIN'
17       double precision x(n)
18 cd    print *,'nres',nres,' nphi',nphi,' ntheta',ntheta,' nvar',nvar
19       do i=4,nres
20         x(i-3)=phi(i)
21 cd      print *,i,i-3,phi(i)
22       enddo
23       if (n.eq.nphi) return
24       do i=3,nres
25         x(i-2+nphi)=theta(i)
26 cd      print *,i,i-2+nphi,theta(i)
27       enddo
28       if (n.eq.nphi+ntheta) return
29       do i=2,nres-1
30         if (ialph(i,1).gt.0) then
31           x(ialph(i,1))=alph(i)
32           x(ialph(i,1)+nside)=omeg(i)
33 cd        print *,i,ialph(i,1),ialph(i,1)+nside,alph(i),omeg(i)
34         endif
35       enddo      
36       return
37       end
38 C--------------------------------------------------------------------
39       subroutine var_to_geom(n,x)
40 C
41 C Update geometry parameters according to the variable array.
42 C
43       implicit real*8 (a-h,o-z)
44       include 'DIMENSIONS'
45       include 'COMMON.VAR'
46       include 'COMMON.CHAIN'
47       include 'COMMON.GEO'
48       include 'COMMON.IOUNITS'
49       dimension x(n)
50       logical change,reduce
51       change=reduce(x)
52       if (n.gt.nphi+ntheta) then
53         do i=1,nside
54           ii=ialph(i,2)
55           alph(ii)=x(nphi+ntheta+i)
56           omeg(ii)=pinorm(x(nphi+ntheta+nside+i))
57         enddo      
58       endif
59       do i=4,nres
60         phi(i)=x(i-3)
61       enddo
62       if (n.eq.nphi) return
63       do i=3,nres
64         theta(i)=x(i-2+nphi)
65         if (theta(i).eq.pi) theta(i)=0.99d0*pi
66         x(i-2+nphi)=theta(i)
67       enddo
68       return
69       end
70 c-------------------------------------------------------------------------
71       logical function convert_side(alphi,omegi)
72       implicit none
73       double precision alphi,omegi
74       double precision pinorm
75       include 'COMMON.GEO'
76       convert_side=.false.
77 C Apply periodicity restrictions.
78       if (alphi.gt.pi) then
79         alphi=dwapi-alphi
80         omegi=pinorm(omegi+pi)
81         convert_side=.true.
82       endif
83       return
84       end
85 c-------------------------------------------------------------------------
86       logical function reduce(x)
87 C
88 C Apply periodic restrictions to variables.
89 C
90       implicit real*8 (a-h,o-z)
91       include 'DIMENSIONS'
92       include 'COMMON.VAR'
93       include 'COMMON.CHAIN'
94       include 'COMMON.GEO'
95       logical zm,zmiana,convert_side
96       dimension x(nvar)
97       zmiana=.false.
98       do i=4,nres
99         x(i-3)=pinorm(x(i-3))
100       enddo
101       if (nvar.gt.nphi+ntheta) then
102         do i=1,nside
103           ii=nphi+ntheta+i
104           iii=ii+nside
105           x(ii)=thetnorm(x(ii))
106           x(iii)=pinorm(x(iii))
107 C Apply periodic restrictions.
108           zm=convert_side(x(ii),x(iii))
109           zmiana=zmiana.or.zm
110         enddo      
111       endif
112       if (nvar.eq.nphi) return
113       do i=3,nres
114         ii=i-2+nphi
115         iii=i-3
116         x(ii)=dmod(x(ii),dwapi)
117 C Apply periodic restrictions.
118         if (x(ii).gt.pi) then
119           zmiana=.true.
120           x(ii)=dwapi-x(ii)
121           if (iii.gt.0) x(iii)=pinorm(x(iii)+pi)
122           if (i.lt.nres) x(iii+1)=pinorm(x(iii+1)+pi)
123           ii=ialph(i-1,1)
124           if (ii.gt.0) then
125             x(ii)=dmod(pi-x(ii),dwapi)
126             x(ii+nside)=pinorm(-x(ii+nside))
127             zm=convert_side(x(ii),x(ii+nside))
128           endif
129         else if (x(ii).lt.-pi) then
130           zmiana=.true.
131           x(ii)=dwapi+x(ii)
132           ii=ialph(i-1,1)
133           if (ii.gt.0) then
134             x(ii)=dmod(pi-x(ii),dwapi)
135             x(ii+nside)=pinorm(-pi-x(ii+nside))
136             zm=convert_side(x(ii),x(ii+nside))
137           endif
138         else if (x(ii).lt.0.0d0) then
139           zmiana=.true.
140           x(ii)=-x(ii)
141           if (iii.gt.0) x(iii)=pinorm(x(iii)+pi)
142           if (i.lt.nres) x(iii+1)=pinorm(x(iii+1)+pi)
143           ii=ialph(i-1,1)
144           if (ii.gt.0) then
145             x(ii+nside)=pinorm(-x(ii+nside))
146             zm=convert_side(x(ii),x(ii+nside))
147           endif
148         endif 
149       enddo
150       reduce=zmiana
151       return
152       end
153 c--------------------------------------------------------------------------
154       double precision function thetnorm(x)
155 C This function puts x within [0,2Pi].
156       implicit none
157       double precision x,xx
158       include 'COMMON.GEO'
159       xx=dmod(x,dwapi)
160       if (xx.lt.0.0d0) xx=xx+dwapi
161       if (xx.gt.0.9999d0*pi) xx=0.9999d0*pi
162       thetnorm=xx
163       return
164       end 
165 C--------------------------------------------------------------------
166       subroutine var_to_geom_restr(n,xx)
167 C
168 C Update geometry parameters according to the variable array.
169 C
170       implicit real*8 (a-h,o-z)
171       include 'DIMENSIONS'
172       include 'COMMON.VAR'
173       include 'COMMON.CHAIN'
174       include 'COMMON.GEO'
175       include 'COMMON.IOUNITS'
176       dimension x(maxvar),xx(maxvar)
177       logical change,reduce
178
179       call xx2x(x,xx)
180       change=reduce(x)
181       do i=1,nside
182           ii=ialph(i,2)
183           alph(ii)=x(nphi+ntheta+i)
184           omeg(ii)=pinorm(x(nphi+ntheta+nside+i))
185       enddo      
186       do i=4,nres
187         phi(i)=x(i-3)
188       enddo
189       do i=3,nres
190         theta(i)=x(i-2+nphi)
191         if (theta(i).eq.pi) theta(i)=0.99d0*pi
192         x(i-2+nphi)=theta(i)
193       enddo
194       return
195       end
196 c-------------------------------------------------------------------------