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