rename
[unres4.git] / source / unres / geometry.F90
1       module geometry
2 !-----------------------------------------------------------------------------
3       use io_units
4       use names
5       use math
6       use MPI_data
7       use geometry_data
8       use control_data
9       use energy_data
10       implicit none
11 !-----------------------------------------------------------------------------
12 ! commom.bounds
13 !      common /bounds/
14 !-----------------------------------------------------------------------------
15 ! commom.chain
16 !      common /chain/
17 !      common /rotmat/
18       real(kind=8),dimension(:,:,:),allocatable :: t,r !(3,3,maxres)
19 !-----------------------------------------------------------------------------
20 ! common.geo
21 !      common /geo/
22 !-----------------------------------------------------------------------------
23 ! common.locmove
24 !     Variables (set in init routine) never modified by local_move
25 !      common /loc_const/
26       integer :: init_called
27       logical :: locmove_output
28       real(kind=8) :: min_theta, max_theta
29       real(kind=8) :: dmin2,dmax2
30       real(kind=8) :: flag,small,small2
31 !     Workspace for local_move
32 !      common /loc_work/
33       integer :: a_n,b_n,res_n
34       real(kind=8),dimension(0:7) :: a_ang
35       real(kind=8),dimension(0:3) :: b_ang
36       real(kind=8),dimension(0:11) :: res_ang
37       logical,dimension(0:2,0:7) :: a_tab
38       logical,dimension(0:2,0:3) :: b_tab
39       logical,dimension(0:2,0:2,0:11) :: res_tab
40 !-----------------------------------------------------------------------------
41 !      integer,dimension(:),allocatable :: itype_pdb !(maxres) initialize in molread
42 !-----------------------------------------------------------------------------
43 !
44 !
45 !-----------------------------------------------------------------------------
46       contains
47 !-----------------------------------------------------------------------------
48 ! arcos.f
49 !-----------------------------------------------------------------------------
50       real(kind=8) function ARCOS(X)
51 !      implicit real*8 (a-h,o-z)
52 !      include 'COMMON.GEO'
53 !el local variables
54       real(kind=8) :: x
55       IF (DABS(X).LT.1.0D0) GOTO 1
56       ARCOS=PIPOL*(1.0d0-DSIGN(1.0D0,X))
57       RETURN
58     1 ARCOS=DACOS(X)
59       return
60       end function ARCOS
61 !-----------------------------------------------------------------------------
62 ! chainbuild.F
63 !-----------------------------------------------------------------------------
64       subroutine chainbuild
65
66 ! Build the virtual polypeptide chain. Side-chain centroids are moveable.
67 ! As of 2/17/95.
68 !
69 !      implicit real*8 (a-h,o-z)
70 !      include 'DIMENSIONS'
71 !      include 'COMMON.CHAIN'
72 !      include 'COMMON.LOCAL'
73 !      include 'COMMON.GEO'
74 !      include 'COMMON.VAR'
75 !      include 'COMMON.IOUNITS'
76 !      include 'COMMON.NAMES'
77 !      include 'COMMON.INTERACT'
78       logical :: lprn
79 !el local variables
80       integer :: i,j
81       real(kind=8) :: be,be1,alfai
82       integer :: nres2
83       nres2=2*nres
84 ! Set lprn=.true. for debugging
85       lprn = .false.
86 !
87 ! Define the origin and orientation of the coordinate system and locate the
88 ! first three CA's and SC(2).
89 !
90 !elwrite(iout,*)"in chainbuild"
91       call orig_frame
92 !elwrite(iout,*)"after orig_frame"
93 !
94 ! Build the alpha-carbon chain.
95 !
96       do i=4,nres
97         call locate_next_res(i)
98       enddo     
99 !elwrite(iout,*)"after locate_next_res"
100 !
101 ! First and last SC must coincide with the corresponding CA.
102 !
103       do j=1,3
104         dc(j,nres+1)=0.0D0
105         dc_norm(j,nres+1)=0.0D0
106         dc(j,nres+nres)=0.0D0
107         dc_norm(j,nres+nres)=0.0D0
108         c(j,nres+1)=c(j,1)
109         c(j,nres+nres)=c(j,nres)
110       enddo
111 !
112 ! Temporary diagnosis
113 !
114       if (lprn) then
115
116       call cartprint
117       write (iout,'(/a)') 'Recalculated internal coordinates'
118       do i=2,nres-1
119         do j=1,3
120           c(j,nres2+2)=0.5D0*(c(j,i-1)+c(j,i+1))        !maxres2=2*maxres
121         enddo
122         be=0.0D0
123         if (i.gt.3) be=rad2deg*beta(i-3,i-2,i-1,i)
124         be1=rad2deg*beta(nres+i,i,nres2+2,i+1)
125         alfai=0.0D0
126         if (i.gt.2) alfai=rad2deg*alpha(i-2,i-1,i)
127         write (iout,1212) restyp(itype(i)),i,dist(i-1,i),&
128         alfai,be,dist(nres+i,i),rad2deg*alpha(nres+i,i,nres2+2),be1
129       enddo   
130  1212 format (a3,'(',i3,')',2(f10.5,2f10.2))
131
132       endif
133
134       return
135       end subroutine chainbuild
136 !-----------------------------------------------------------------------------
137       subroutine orig_frame
138 !
139 ! Define the origin and orientation of the coordinate system and locate 
140 ! the first three atoms.
141 !
142 !      implicit real*8 (a-h,o-z)
143 !      include 'DIMENSIONS'
144 !      include 'COMMON.CHAIN'
145 !      include 'COMMON.LOCAL'
146 !      include 'COMMON.GEO'
147 !      include 'COMMON.VAR'
148 !el local variables
149       integer :: i,j
150       real(kind=8) :: cost,sint
151
152 !el      allocate(t(3,3,nres))  !(3,3,maxres) 
153 !el      allocate(r(3,3,nres))  !(3,3,maxres) 
154 !el      allocate(rt(3,3,nres)) !(3,3,maxres) 
155 !el      allocate(dc_norm(3,0:2*nres))  !(3,0:maxres2)
156 !el      allocate(prod(3,3,nres))       !(3,3,maxres) 
157
158       cost=dcos(theta(3))
159       sint=dsin(theta(3))
160       t(1,1,1)=-cost
161       t(1,2,1)=-sint 
162       t(1,3,1)= 0.0D0
163       t(2,1,1)=-sint
164       t(2,2,1)= cost
165       t(2,3,1)= 0.0D0
166       t(3,1,1)= 0.0D0
167       t(3,2,1)= 0.0D0
168       t(3,3,1)= 1.0D0
169       r(1,1,1)= 1.0D0
170       r(1,2,1)= 0.0D0
171       r(1,3,1)= 0.0D0
172       r(2,1,1)= 0.0D0
173       r(2,2,1)= 1.0D0
174       r(2,3,1)= 0.0D0
175       r(3,1,1)= 0.0D0
176       r(3,2,1)= 0.0D0
177       r(3,3,1)= 1.0D0
178       do i=1,3
179         do j=1,3
180           rt(i,j,1)=t(i,j,1)
181         enddo
182       enddo
183       do i=1,3
184         do j=1,3
185           prod(i,j,1)=0.0D0
186           prod(i,j,2)=t(i,j,1)
187         enddo
188         prod(i,i,1)=1.0D0
189       enddo   
190       c(1,1)=0.0D0
191       c(2,1)=0.0D0
192       c(3,1)=0.0D0
193       c(1,2)=vbld(2)
194       c(2,2)=0.0D0
195       c(3,2)=0.0D0
196       dc(1,0)=0.0d0
197       dc(2,0)=0.0D0
198       dc(3,0)=0.0D0
199       dc(1,1)=vbld(2)
200       dc(2,1)=0.0D0
201       dc(3,1)=0.0D0
202       dc_norm(1,0)=0.0D0
203       dc_norm(2,0)=0.0D0
204       dc_norm(3,0)=0.0D0
205       dc_norm(1,1)=1.0D0
206       dc_norm(2,1)=0.0D0
207       dc_norm(3,1)=0.0D0
208       do j=1,3
209         dc_norm(j,2)=prod(j,1,2)
210         dc(j,2)=vbld(3)*prod(j,1,2)
211         c(j,3)=c(j,2)+dc(j,2)
212       enddo
213       call locate_side_chain(2)
214       return
215       end subroutine orig_frame
216 !-----------------------------------------------------------------------------
217       subroutine locate_next_res(i)
218 !
219 ! Locate CA(i) and SC(i-1)
220 !
221 !      implicit real*8 (a-h,o-z)
222 !      include 'DIMENSIONS'
223 !      include 'COMMON.CHAIN'
224 !      include 'COMMON.LOCAL'
225 !      include 'COMMON.GEO'
226 !      include 'COMMON.VAR'
227 !      include 'COMMON.IOUNITS'
228 !      include 'COMMON.NAMES'
229 !      include 'COMMON.INTERACT'
230 !
231 ! Define the rotation matrices corresponding to CA(i)
232 !
233 !el local variables
234       integer :: i,j    
235       real(kind=8) :: theti,phii
236       real(kind=8) :: cost,sint,cosphi,sinphi
237 #ifdef OSF
238 #ifdef WHAM_RUN
239       theti=theta(i)
240       icrc=0
241       call proc_proc(theti,icrc)
242       if(icrc.eq.1)theti=100.0
243       phii=phi(i)
244       icrc=0
245       call proc_proc(phii,icrc)
246       if(icrc.eq.1)phii=180.0
247 #else
248       theti=theta(i)
249       if (theti.ne.theti) theti=100.0     
250       phii=phi(i)
251       if (phii.ne.phii) phii=180.0     
252 #endif
253 #else
254       theti=theta(i)      
255       phii=phi(i)
256 #endif
257       cost=dcos(theti)
258       sint=dsin(theti)
259       cosphi=dcos(phii)
260       sinphi=dsin(phii)
261 ! Define the matrices of the rotation about the virtual-bond valence angles
262 ! theta, T(i,j,k), virtual-bond dihedral angles gamma (miscalled PHI in this
263 ! program), R(i,j,k), and, the cumulative matrices of rotation RT
264       t(1,1,i-2)=-cost
265       t(1,2,i-2)=-sint 
266       t(1,3,i-2)= 0.0D0
267       t(2,1,i-2)=-sint
268       t(2,2,i-2)= cost
269       t(2,3,i-2)= 0.0D0
270       t(3,1,i-2)= 0.0D0
271       t(3,2,i-2)= 0.0D0
272       t(3,3,i-2)= 1.0D0
273       r(1,1,i-2)= 1.0D0
274       r(1,2,i-2)= 0.0D0
275       r(1,3,i-2)= 0.0D0
276       r(2,1,i-2)= 0.0D0
277       r(2,2,i-2)=-cosphi
278       r(2,3,i-2)= sinphi
279       r(3,1,i-2)= 0.0D0
280       r(3,2,i-2)= sinphi
281       r(3,3,i-2)= cosphi
282       rt(1,1,i-2)=-cost
283       rt(1,2,i-2)=-sint
284       rt(1,3,i-2)=0.0D0
285       rt(2,1,i-2)=sint*cosphi
286       rt(2,2,i-2)=-cost*cosphi
287       rt(2,3,i-2)=sinphi
288       rt(3,1,i-2)=-sint*sinphi
289       rt(3,2,i-2)=cost*sinphi
290       rt(3,3,i-2)=cosphi
291       call matmult(prod(1,1,i-2),rt(1,1,i-2),prod(1,1,i-1))
292       do j=1,3
293         dc_norm(j,i-1)=prod(j,1,i-1)
294         dc(j,i-1)=vbld(i)*prod(j,1,i-1)
295         c(j,i)=c(j,i-1)+dc(j,i-1)
296       enddo
297 !d    print '(2i3,2(3f10.5,5x))', i-1,i,(dc(j,i-1),j=1,3),(c(j,i),j=1,3)
298
299 ! Now calculate the coordinates of SC(i-1)
300 !
301       call locate_side_chain(i-1)
302       return
303       end subroutine locate_next_res
304 !-----------------------------------------------------------------------------
305       subroutine locate_side_chain(i)
306
307 ! Locate the side-chain centroid i, 1 < i < NRES. Put in C(*,NRES+i).
308 !
309 !      implicit real*8 (a-h,o-z)
310 !      include 'DIMENSIONS'
311 !      include 'COMMON.CHAIN'
312 !      include 'COMMON.LOCAL'
313 !      include 'COMMON.GEO'
314 !      include 'COMMON.VAR'
315 !      include 'COMMON.IOUNITS'
316 !      include 'COMMON.NAMES'
317 !      include 'COMMON.INTERACT'
318       integer :: i,j,k
319       real(kind=8),dimension(3) :: xx
320       real(kind=8) :: alphi,omegi,theta2
321       real(kind=8) :: dsci,dsci_inv,sinalphi,cosalphi,cosomegi,sinomegi
322       real(kind=8) :: xp,yp,zp,cost2,sint2,rj
323 !      dsci=dsc(itype(i))
324 !      dsci_inv=dsc_inv(itype(i))
325       dsci=vbld(i+nres)
326       dsci_inv=vbld_inv(i+nres)
327 #ifdef OSF
328       alphi=alph(i)
329       omegi=omeg(i)
330 #ifdef WHAM_RUN
331 ! detecting NaNQ
332       icrc=0
333       call proc_proc(alphi,icrc)
334       if(icrc.eq.1)alphi=100.0
335       icrc=0
336       call proc_proc(omegi,icrc)
337       if(icrc.eq.1)omegi=-100.0
338 #else
339       if (alphi.ne.alphi) alphi=100.0
340       if (omegi.ne.omegi) omegi=-100.0
341 #endif
342 #else
343       alphi=alph(i)
344       omegi=omeg(i)
345 #endif
346       cosalphi=dcos(alphi)
347       sinalphi=dsin(alphi)
348       cosomegi=dcos(omegi)
349       sinomegi=dsin(omegi) 
350       xp= dsci*cosalphi
351       yp= dsci*sinalphi*cosomegi
352       zp=-dsci*sinalphi*sinomegi
353 ! Now we have to rotate the coordinate system by 180-theta(i)/2 so as to get its
354 ! X-axis aligned with the vector DC(*,i)
355       theta2=pi-0.5D0*theta(i+1)
356       cost2=dcos(theta2)
357       sint2=dsin(theta2)
358       xx(1)= xp*cost2+yp*sint2
359       xx(2)=-xp*sint2+yp*cost2
360       xx(3)= zp
361 !d    print '(a3,i3,3f10.5,5x,3f10.5)',restyp(itype(i)),i,
362 !d   &   xp,yp,zp,(xx(k),k=1,3)
363       do j=1,3
364         xloc(j,i)=xx(j)
365       enddo
366 ! Bring the SC vectors to the common coordinate system.
367       xx(1)=xloc(1,i)
368       xx(2)=xloc(2,i)*r(2,2,i-1)+xloc(3,i)*r(2,3,i-1)
369       xx(3)=xloc(2,i)*r(3,2,i-1)+xloc(3,i)*r(3,3,i-1)
370       do j=1,3
371         xrot(j,i)=xx(j)
372       enddo
373       do j=1,3
374         rj=0.0D0
375         do k=1,3
376           rj=rj+prod(j,k,i-1)*xx(k)
377         enddo
378         dc(j,nres+i)=rj
379         dc_norm(j,nres+i)=rj*dsci_inv
380         c(j,nres+i)=c(j,i)+rj
381       enddo
382       return
383       end subroutine locate_side_chain
384 !-----------------------------------------------------------------------------
385 ! checkder_p.F
386 !-----------------------------------------------------------------------------
387       subroutine int_from_cart1(lprn)
388 !      implicit real*8 (a-h,o-z)
389 !      include 'DIMENSIONS'
390 #ifdef MPI
391       include 'mpif.h'
392       integer :: ierror
393 #endif
394 !      include 'COMMON.IOUNITS'
395 !      include 'COMMON.VAR'
396 !      include 'COMMON.CHAIN'
397 !      include 'COMMON.GEO'
398 !      include 'COMMON.INTERACT'
399 !      include 'COMMON.LOCAL'
400 !      include 'COMMON.NAMES'
401 !      include 'COMMON.SETUP'
402 !      include 'COMMON.TIME1'
403       logical :: lprn
404 !el local variables
405       integer :: i,j
406       real(kind=8) :: dnorm1,dnorm2,be
407       integer :: nres2
408       nres2=2*nres
409       if (lprn) write (iout,'(/a)') 'Recalculated internal coordinates'
410 #ifdef TIMING
411       time01=MPI_Wtime()
412 #endif
413
414 #ifdef WHAM_RUN
415       vbld(nres+1)=0.0d0
416 !write(iout,*)"geometry warring, vbld=",(vbld(i),i=1,nres+1)
417       vbld(2*nres)=0.0d0
418       vbld_inv(nres+1)=0.0d0
419       vbld_inv(2*nres)=0.0d0
420 #endif
421
422 #if defined(PARINT) && defined(MPI)
423       do i=iint_start,iint_end
424 #else
425       do i=2,nres
426 #endif
427         dnorm1=dist(i-1,i)
428         dnorm2=dist(i,i+1) 
429         do j=1,3
430           c(j,nres2+2)=0.5D0*(2*c(j,i)+(c(j,i-1)-c(j,i))/dnorm1 &
431            +(c(j,i+1)-c(j,i))/dnorm2)
432         enddo
433         be=0.0D0
434         if (i.gt.2) then
435         if (i.le.nres) phi(i+1)=beta(i-2,i-1,i,i+1)
436         if ((itype(i).ne.10).and.(itype(i-1).ne.10)) then
437          tauangle(3,i+1)=beta(i+nres-1,i-1,i,i+nres)
438         endif
439         if (itype(i-1).ne.10) then
440          tauangle(1,i+1)=beta(i-1+nres,i-1,i,i+1)
441          omicron(1,i)=alpha(i-2,i-1,i-1+nres)
442          omicron(2,i)=alpha(i-1+nres,i-1,i)
443         endif
444         if (itype(i).ne.10) then
445          tauangle(2,i+1)=beta(i-2,i-1,i,i+nres)
446         endif
447         endif
448         omeg(i)=beta(nres+i,i,nres2+2,i+1)
449         alph(i)=alpha(nres+i,i,nres2+2)
450         theta(i+1)=alpha(i-1,i,i+1)
451         vbld(i)=dist(i-1,i)
452         vbld_inv(i)=1.0d0/vbld(i)
453         vbld(nres+i)=dist(nres+i,i)
454         if (itype(i).ne.10) then
455           vbld_inv(nres+i)=1.0d0/vbld(nres+i)
456         else
457           vbld_inv(nres+i)=0.0d0
458         endif
459       enddo   
460 #if defined(PARINT) && defined(MPI)
461        if (nfgtasks1.gt.1) then
462 !d       write(iout,*) "iint_start",iint_start," iint_count",
463 !d     &   (iint_count(i),i=0,nfgtasks-1)," iint_displ",
464 !d     &   (iint_displ(i),i=0,nfgtasks-1)
465 !d       write (iout,*) "Gather vbld backbone"
466 !d       call flush(iout)
467        time00=MPI_Wtime()
468        call MPI_Allgatherv(vbld(iint_start),iint_count(fg_rank1),&
469          MPI_DOUBLE_PRECISION,vbld(1),iint_count(0),iint_displ(0),&
470          MPI_DOUBLE_PRECISION,FG_COMM1,IERR)
471 !d       write (iout,*) "Gather vbld_inv"
472 !d       call flush(iout)
473        call MPI_Allgatherv(vbld_inv(iint_start),iint_count(fg_rank1),&
474          MPI_DOUBLE_PRECISION,vbld_inv(1),iint_count(0),iint_displ(0),&
475          MPI_DOUBLE_PRECISION,FG_COMM1,IERR)
476 !d       write (iout,*) "Gather vbld side chain"
477 !d       call flush(iout)
478        call MPI_Allgatherv(vbld(iint_start+nres),iint_count(fg_rank1),&
479          MPI_DOUBLE_PRECISION,vbld(nres+1),iint_count(0),iint_displ(0),&
480          MPI_DOUBLE_PRECISION,FG_COMM1,IERR)
481 !d       write (iout,*) "Gather vbld_inv side chain"
482 !d       call flush(iout)
483        call MPI_Allgatherv(vbld_inv(iint_start+nres),&
484          iint_count(fg_rank1),MPI_DOUBLE_PRECISION,vbld_inv(nres+1),&
485          iint_count(0),iint_displ(0),MPI_DOUBLE_PRECISION,FG_COMM1,IERR)
486 !d       write (iout,*) "Gather theta"
487 !d       call flush(iout)
488        call MPI_Allgatherv(theta(iint_start+1),iint_count(fg_rank1),&
489          MPI_DOUBLE_PRECISION,theta(2),iint_count(0),iint_displ(0),&
490          MPI_DOUBLE_PRECISION,FG_COMM1,IERR)
491 !d       write (iout,*) "Gather phi"
492 !d       call flush(iout)
493        call MPI_Allgatherv(phi(iint_start+1),iint_count(fg_rank1),&
494          MPI_DOUBLE_PRECISION,phi(2),iint_count(0),iint_displ(0),&
495          MPI_DOUBLE_PRECISION,FG_COMM1,IERR)
496 #ifdef CRYST_SC
497 !d       write (iout,*) "Gather alph"
498 !d       call flush(iout)
499        call MPI_Allgatherv(alph(iint_start),iint_count(fg_rank1),&
500          MPI_DOUBLE_PRECISION,alph(1),iint_count(0),iint_displ(0),&
501          MPI_DOUBLE_PRECISION,FG_COMM1,IERR)
502 !d       write (iout,*) "Gather omeg"
503 !d       call flush(iout)
504        call MPI_Allgatherv(omeg(iint_start),iint_count(fg_rank1),&
505          MPI_DOUBLE_PRECISION,omeg(1),iint_count(0),iint_displ(0),&
506          MPI_DOUBLE_PRECISION,FG_COMM1,IERR)
507 #endif
508        time_gather=time_gather+MPI_Wtime()-time00
509       endif
510 #endif
511       do i=1,nres-1
512         do j=1,3
513 !#ifdef WHAM_RUN
514 #if defined(WHAM_RUN) || defined(CLUSTER)
515           dc(j,i)=c(j,i+1)-c(j,i)
516 #endif
517           dc_norm(j,i)=dc(j,i)*vbld_inv(i+1)
518         enddo
519       enddo
520       do i=2,nres-1
521         do j=1,3
522 !#ifdef WHAM_RUN
523 #if defined(WHAM_RUN) || defined(CLUSTER)
524           dc(j,i+nres)=c(j,i+nres)-c(j,i)
525 #endif
526           dc_norm(j,i+nres)=dc(j,i+nres)*vbld_inv(i+nres)
527         enddo
528       enddo
529       if (lprn) then
530       do i=2,nres
531        write (iout,1212) restyp(itype(i)),i,vbld(i),&
532        rad2deg*theta(i),rad2deg*phi(i),vbld(nres+i),&
533        rad2deg*alph(i),rad2deg*omeg(i)
534       enddo
535       endif
536  1212 format (a3,'(',i3,')',2(f15.10,2f10.2))
537 #ifdef TIMING
538       time_intfcart=time_intfcart+MPI_Wtime()-time01
539 #endif
540       return
541       end subroutine int_from_cart1
542 #if !defined(WHAM_RUN) && !defined(CLUSTER)
543 !-----------------------------------------------------------------------------
544 ! check_sc_distr.f
545 !-----------------------------------------------------------------------------
546       subroutine check_sc_distr
547 !      implicit real*8 (a-h,o-z)
548 !      include 'DIMENSIONS'
549 !      include 'COMMON.TIME1'
550 !      include 'COMMON.INTERACT'
551 !      include 'COMMON.NAMES'
552 !      include 'COMMON.GEO'
553 !      include 'COMMON.HEADER'
554 !      include 'COMMON.CONTROL'
555       logical :: fail
556       real(kind=8),dimension(6*nres) :: varia !(maxvar) (maxvar=6*maxres)
557       real(kind=8) :: hrtime,mintime,sectime
558       integer,parameter :: MaxSample=10000000
559       real(kind=8),parameter :: delt=1.0D0/MaxSample
560       real(kind=8),dimension(0:72,0:90) :: prob
561 !el local variables
562       integer :: it,i,j,isample,indal,indom
563       real(kind=8) :: al,om,dV
564       dV=2.0D0*5.0D0*deg2rad*deg2rad
565       print *,'dv=',dv
566       do 10 it=1,1 
567         if (it.eq.10) goto 10 
568         open (20,file=restyp(it)//'_distr.sdc',status='unknown')
569         call gen_side(it,90.0D0 * deg2rad,al,om,fail)
570         close (20)
571         goto 10
572         open (20,file=restyp(it)//'_distr1.sdc',status='unknown')
573         do i=0,90
574           do j=0,72
575             prob(j,i)=0.0D0
576           enddo
577         enddo
578         do isample=1,MaxSample
579           call gen_side(it,90.0D0 * deg2rad,al,om,fail)
580           indal=rad2deg*al/2
581           indom=(rad2deg*om+180.0D0)/5
582           prob(indom,indal)=prob(indom,indal)+delt
583         enddo
584         do i=45,90
585           do j=0,72 
586             write (20,'(2f10.3,1pd15.5)') 2*i+0.0D0,5*j-180.0D0,&
587                     prob(j,i)/dV
588           enddo
589         enddo
590    10   continue
591       return
592       end subroutine check_sc_distr
593 #endif
594 !-----------------------------------------------------------------------------
595 ! convert.f
596 !-----------------------------------------------------------------------------
597       subroutine geom_to_var(n,x)
598 !
599 ! Transfer the geometry parameters to the variable array.
600 ! The positions of variables are as follows:
601 ! 1. Virtual-bond torsional angles: 1 thru nres-3
602 ! 2. Virtual-bond valence angles: nres-2 thru 2*nres-5
603 ! 3. The polar angles alpha of local SC orientation: 2*nres-4 thru 
604 !    2*nres-4+nside
605 ! 4. The torsional angles omega of SC orientation: 2*nres-4+nside+1
606 !    thru 2*nre-4+2*nside 
607 !
608 !      implicit real*8 (a-h,o-z)
609 !      include 'DIMENSIONS'
610 !      include 'COMMON.VAR'
611 !      include 'COMMON.GEO'
612 !      include 'COMMON.CHAIN'
613       integer :: n,i
614       real(kind=8),dimension(n) :: x
615 !d    print *,'nres',nres,' nphi',nphi,' ntheta',ntheta,' nvar',nvar
616       do i=4,nres
617         x(i-3)=phi(i)
618 !d      print *,i,i-3,phi(i)
619       enddo
620       if (n.eq.nphi) return
621       do i=3,nres
622         x(i-2+nphi)=theta(i)
623 !d      print *,i,i-2+nphi,theta(i)
624       enddo
625       if (n.eq.nphi+ntheta) return
626       do i=2,nres-1
627         if (ialph(i,1).gt.0) then
628           x(ialph(i,1))=alph(i)
629           x(ialph(i,1)+nside)=omeg(i)
630 !d        print *,i,ialph(i,1),ialph(i,1)+nside,alph(i),omeg(i)
631         endif
632       enddo
633       return
634       end subroutine geom_to_var
635 !-----------------------------------------------------------------------------
636       subroutine var_to_geom(n,x)
637 !
638 ! Update geometry parameters according to the variable array.
639 !
640 !      implicit real*8 (a-h,o-z)
641 !      include 'DIMENSIONS'
642 !      include 'COMMON.VAR'
643 !      include 'COMMON.CHAIN'
644 !      include 'COMMON.GEO'
645 !      include 'COMMON.IOUNITS'
646       integer :: n,i,ii
647       real(kind=8),dimension(n) :: x
648       logical :: change !,reduce
649 !el      alph=0.0d0
650 !el      omeg=0.0d0
651 !el      phi=0.0d0
652 !el      theta=0.0d0
653
654       change=reduce(x)
655       if (n.gt.nphi+ntheta) then
656         do i=1,nside
657           ii=ialph(i,2)
658           alph(ii)=x(nphi+ntheta+i)
659           omeg(ii)=pinorm(x(nphi+ntheta+nside+i))
660 !elwrite(iout,*) "alph",ii,alph
661 !elwrite(iout,*) "omeg",ii,omeg
662         enddo      
663       endif
664       do i=4,nres
665         phi(i)=x(i-3)
666 !elwrite(iout,*) "phi",i,phi
667       enddo
668       if (n.eq.nphi) return
669       do i=3,nres
670         theta(i)=x(i-2+nphi)
671 !elwrite(iout,*) "theta",i,theta
672         if (theta(i).eq.pi) theta(i)=0.99d0*pi
673         x(i-2+nphi)=theta(i)
674       enddo
675       return
676       end subroutine var_to_geom
677 !-----------------------------------------------------------------------------
678       logical function convert_side(alphi,omegi)
679 !      implicit none
680       real(kind=8) :: alphi,omegi
681 !el      real(kind=8) :: pinorm
682 !      include 'COMMON.GEO'
683       convert_side=.false.
684 ! Apply periodicity restrictions.
685       if (alphi.gt.pi) then
686         alphi=dwapi-alphi
687         omegi=pinorm(omegi+pi)
688         convert_side=.true.
689       endif
690       return
691       end function convert_side
692 !-----------------------------------------------------------------------------
693       logical function reduce(x)
694 !
695 ! Apply periodic restrictions to variables.
696 !
697 !      implicit real*8 (a-h,o-z)
698 !      include 'DIMENSIONS'
699 !      include 'COMMON.VAR'
700 !      include 'COMMON.CHAIN'
701 !      include 'COMMON.GEO'
702       logical :: zm,zmiana      !,convert_side
703       real(kind=8),dimension(nvar) :: x
704       integer :: i,ii,iii
705       zmiana=.false.
706       do i=4,nres
707         x(i-3)=pinorm(x(i-3))
708       enddo
709       if (nvar.gt.nphi+ntheta) then
710         do i=1,nside
711           ii=nphi+ntheta+i
712           iii=ii+nside
713           x(ii)=thetnorm(x(ii))
714           x(iii)=pinorm(x(iii))
715 ! Apply periodic restrictions.
716           zm=convert_side(x(ii),x(iii))
717           zmiana=zmiana.or.zm
718         enddo      
719       endif
720       if (nvar.eq.nphi) return
721       do i=3,nres
722         ii=i-2+nphi
723         iii=i-3
724         x(ii)=dmod(x(ii),dwapi)
725 ! Apply periodic restrictions.
726         if (x(ii).gt.pi) then
727           zmiana=.true.
728           x(ii)=dwapi-x(ii)
729           if (iii.gt.0) x(iii)=pinorm(x(iii)+pi)
730           if (i.lt.nres) x(iii+1)=pinorm(x(iii+1)+pi)
731           ii=ialph(i-1,1)
732           if (ii.gt.0) then
733             x(ii)=dmod(pi-x(ii),dwapi)
734             x(ii+nside)=pinorm(-x(ii+nside))
735             zm=convert_side(x(ii),x(ii+nside))
736           endif
737         else if (x(ii).lt.-pi) then
738           zmiana=.true.
739           x(ii)=dwapi+x(ii)
740           ii=ialph(i-1,1)
741           if (ii.gt.0) then
742             x(ii)=dmod(pi-x(ii),dwapi)
743             x(ii+nside)=pinorm(-pi-x(ii+nside))
744             zm=convert_side(x(ii),x(ii+nside))
745           endif
746         else if (x(ii).lt.0.0d0) then
747           zmiana=.true.
748           x(ii)=-x(ii)
749           if (iii.gt.0) x(iii)=pinorm(x(iii)+pi)
750           if (i.lt.nres) x(iii+1)=pinorm(x(iii+1)+pi)
751           ii=ialph(i-1,1)
752           if (ii.gt.0) then
753             x(ii+nside)=pinorm(-x(ii+nside))
754             zm=convert_side(x(ii),x(ii+nside))
755           endif
756         endif 
757       enddo
758       reduce=zmiana
759       return
760       end function reduce
761 !-----------------------------------------------------------------------------
762       real(kind=8) function thetnorm(x)
763 ! This function puts x within [0,2Pi].
764       implicit none
765       real(kind=8) :: x,xx
766 !      include 'COMMON.GEO'
767       xx=dmod(x,dwapi)
768       if (xx.lt.0.0d0) xx=xx+dwapi
769       if (xx.gt.0.9999d0*pi) xx=0.9999d0*pi
770       thetnorm=xx 
771       return
772       end function thetnorm
773 #if !defined(WHAM_RUN) && !defined(CLUSTER)
774 !-----------------------------------------------------------------------------
775       subroutine var_to_geom_restr(n,xx)
776 !
777 ! Update geometry parameters according to the variable array.
778 !
779 !      implicit real*8 (a-h,o-z)
780 !      include 'DIMENSIONS'
781 !      include 'COMMON.VAR'
782 !      include 'COMMON.CHAIN'
783 !      include 'COMMON.GEO'
784 !      include 'COMMON.IOUNITS'
785       integer :: n,i,ii
786       real(kind=8),dimension(6*nres) :: x,xx !(maxvar) (maxvar=6*maxres)
787       logical :: change !,reduce
788
789       call xx2x(x,xx)
790       change=reduce(x)
791       do i=1,nside
792           ii=ialph(i,2)
793           alph(ii)=x(nphi+ntheta+i)
794           omeg(ii)=pinorm(x(nphi+ntheta+nside+i))
795       enddo      
796       do i=4,nres
797         phi(i)=x(i-3)
798       enddo
799       do i=3,nres
800         theta(i)=x(i-2+nphi)
801         if (theta(i).eq.pi) theta(i)=0.99d0*pi
802         x(i-2+nphi)=theta(i)
803       enddo
804       return
805       end subroutine var_to_geom_restr
806 !-----------------------------------------------------------------------------
807 ! gen_rand_conf.F
808 !-----------------------------------------------------------------------------
809       subroutine gen_rand_conf(nstart,*)
810 ! Generate random conformation or chain cut and regrowth.
811       use mcm_data
812 !      implicit real*8 (a-h,o-z)
813 !      include 'DIMENSIONS'
814 !      include 'COMMON.CHAIN'
815 !      include 'COMMON.LOCAL'
816 !      include 'COMMON.VAR'
817 !      include 'COMMON.INTERACT'
818 !      include 'COMMON.IOUNITS'
819 !      include 'COMMON.MCM'
820 !      include 'COMMON.GEO'
821 !      include 'COMMON.CONTROL'
822       logical :: back,fail      !overlap,
823 !el local variables
824       integer :: i,nstart,maxsi,nsi,maxnit,nit,niter
825       integer :: it1,it2,it,j
826 !d    print *,' CG Processor',me,' maxgen=',maxgen
827       maxsi=100
828 !d    write (iout,*) 'Gen_Rand_conf: nstart=',nstart
829       if (nstart.lt.5) then
830         it1=iabs(itype(2))
831         phi(4)=gen_phi(4,iabs(itype(2)),iabs(itype(3)))
832 !       write(iout,*)'phi(4)=',rad2deg*phi(4)
833         if (nstart.lt.3) theta(3)=gen_theta(iabs(itype(2)),pi,phi(4))
834 !       write(iout,*)'theta(3)=',rad2deg*theta(3) 
835         if (it1.ne.10) then
836           nsi=0
837           fail=.true.
838           do while (fail.and.nsi.le.maxsi)
839             call gen_side(it1,theta(3),alph(2),omeg(2),fail)
840             nsi=nsi+1
841           enddo
842           if (nsi.gt.maxsi) return 1
843         endif ! it1.ne.10
844         call orig_frame
845         i=4
846         nstart=4
847       else
848         i=nstart
849         nstart=max0(i,4)
850       endif
851
852       maxnit=0
853
854       nit=0
855       niter=0
856       back=.false.
857       do while (i.le.nres .and. niter.lt.maxgen)
858         if (i.lt.nstart) then
859           if(iprint.gt.1) then
860           write (iout,'(/80(1h*)/2a/80(1h*))') &
861                 'Generation procedure went down to ',&
862                 'chain beginning. Cannot continue...'
863           write (*,'(/80(1h*)/2a/80(1h*))') &
864                 'Generation procedure went down to ',&
865                 'chain beginning. Cannot continue...'
866           endif
867           return 1
868         endif
869         it1=iabs(itype(i-1))
870         it2=iabs(itype(i-2))
871         it=iabs(itype(i))
872 !       print *,'Gen_Rand_Conf: i=',i,' it=',it,' it1=',it1,' it2=',it2,
873 !    &    ' nit=',nit,' niter=',niter,' maxgen=',maxgen
874         phi(i+1)=gen_phi(i+1,it1,it)
875         if (back) then
876           phi(i)=gen_phi(i+1,it2,it1)
877 !         print *,'phi(',i,')=',phi(i)
878           theta(i-1)=gen_theta(it2,phi(i-1),phi(i))
879           if (it2.ne.10) then
880             nsi=0
881             fail=.true.
882             do while (fail.and.nsi.le.maxsi)
883               call gen_side(it2,theta(i-1),alph(i-2),omeg(i-2),fail)
884               nsi=nsi+1
885             enddo
886             if (nsi.gt.maxsi) return 1
887           endif
888           call locate_next_res(i-1)
889         endif
890         theta(i)=gen_theta(it1,phi(i),phi(i+1))
891         if (it1.ne.10) then 
892         nsi=0
893         fail=.true.
894         do while (fail.and.nsi.le.maxsi)
895           call gen_side(it1,theta(i),alph(i-1),omeg(i-1),fail)
896           nsi=nsi+1
897         enddo
898         if (nsi.gt.maxsi) return 1
899         endif
900         call locate_next_res(i)
901         if (overlap(i-1)) then
902           if (nit.lt.maxnit) then
903             back=.true.
904             nit=nit+1
905           else
906             nit=0
907             if (i.gt.3) then
908               back=.true.
909               i=i-1
910             else
911               write (iout,'(a)') &
912         'Cannot generate non-overlaping conformation. Increase MAXNIT.'
913               write (*,'(a)') &
914         'Cannot generate non-overlaping conformation. Increase MAXNIT.'
915               return 1
916             endif
917           endif
918         else
919           back=.false.
920           nit=0 
921           i=i+1
922         endif
923         niter=niter+1
924       enddo
925       if (niter.ge.maxgen) then
926         write (iout,'(a,2i5)') &
927        'Too many trials in conformation generation',niter,maxgen
928         write (*,'(a,2i5)') &
929        'Too many trials in conformation generation',niter,maxgen
930         return 1
931       endif
932       do j=1,3
933         c(j,nres+1)=c(j,1)
934         c(j,nres+nres)=c(j,nres)
935       enddo
936       return
937       end subroutine gen_rand_conf
938 !-----------------------------------------------------------------------------
939       logical function overlap(i)
940 !      implicit real*8 (a-h,o-z)
941 !      include 'DIMENSIONS'
942 !      include 'COMMON.CHAIN'
943 !      include 'COMMON.INTERACT'
944 !      include 'COMMON.FFIELD'
945       integer :: i,j,iti,itj,iteli,itelj,k
946       real(kind=8) :: redfac,rcomp
947       integer :: nres2
948       nres2=2*nres
949       data redfac /0.5D0/
950       overlap=.false.
951       iti=iabs(itype(i))
952       if (iti.gt.ntyp) return
953 ! Check for SC-SC overlaps.
954 !d    print *,'nnt=',nnt,' nct=',nct
955       do j=nnt,i-1
956         itj=iabs(itype(j))
957         if (j.lt.i-1 .or. ipot.ne.4) then
958           rcomp=sigmaii(iti,itj)
959         else 
960           rcomp=sigma(iti,itj)
961         endif
962 !d      print *,'j=',j
963         if (dist(nres+i,nres+j).lt.redfac*rcomp) then
964           overlap=.true.
965 !        print *,'overlap, SC-SC: i=',i,' j=',j,
966 !     &     ' dist=',dist(nres+i,nres+j),' rcomp=',
967 !     &     rcomp
968           return
969         endif
970       enddo
971 ! Check for overlaps between the added peptide group and the preceding
972 ! SCs.
973       iteli=itel(i)
974       do j=1,3
975 !       c(j,nres2+1)=0.5D0*(c(j,i)+c(j,i+1))
976         c(j,nres2+3)=0.5D0*(c(j,i)+c(j,i+1))
977       enddo
978       do j=nnt,i-2
979         itj=iabs(itype(j))
980 !d      print *,'overlap, p-Sc: i=',i,' j=',j,
981 !d   &         ' dist=',dist(nres+j,maxres2+1)
982         if (dist(nres+j,nres2+3).lt.4.0D0*redfac) then
983           overlap=.true.
984           return
985         endif
986       enddo
987 ! Check for overlaps between the added side chain and the preceding peptide
988 ! groups.
989       do j=1,nnt-2
990         do k=1,3
991           c(k,nres2+3)=0.5D0*(c(k,j)+c(k,j+1))
992         enddo
993 !d      print *,'overlap, SC-p: i=',i,' j=',j,
994 !d   &         ' dist=',dist(nres+i,maxres2+1)
995         if (dist(nres+i,nres2+3).lt.4.0D0*redfac) then
996           overlap=.true.
997           return
998         endif
999       enddo
1000 ! Check for p-p overlaps
1001       do j=1,3
1002         c(j,nres2+4)=0.5D0*(c(j,i)+c(j,i+1))
1003       enddo
1004       do j=nnt,i-2
1005         itelj=itel(j)
1006         do k=1,3
1007           c(k,nres2+4)=0.5D0*(c(k,j)+c(k,j+1))
1008         enddo
1009 !d      print *,'overlap, p-p: i=',i,' j=',j,
1010 !d   &         ' dist=',dist(maxres2+1,maxres2+2)
1011         if(iteli.ne.0.and.itelj.ne.0)then
1012         if (dist(nres2+3,nres2+4).lt.rpp(iteli,itelj)*redfac) then
1013           overlap=.true.
1014           return
1015         endif
1016         endif
1017       enddo
1018       return
1019       end function overlap
1020 !-----------------------------------------------------------------------------
1021       real(kind=8) function gen_phi(i,it1,it2)
1022       use random, only:ran_number
1023 !      implicit real*8 (a-h,o-z)
1024 !      include 'DIMENSIONS'
1025 !      include 'COMMON.GEO'
1026 !      include 'COMMON.BOUNDS'
1027       integer :: i,it1,it2
1028 !      gen_phi=ran_number(-pi,pi)
1029 ! 8/13/98 Generate phi using pre-defined boundaries
1030       gen_phi=ran_number(phibound(1,i),phibound(2,i))
1031       return
1032       end function gen_phi
1033 !-----------------------------------------------------------------------------
1034       real(kind=8) function gen_theta(it,gama,gama1)
1035       use random,only:binorm
1036 !      implicit real*8 (a-h,o-z)
1037 !      include 'DIMENSIONS'
1038 !      include 'COMMON.LOCAL'
1039 !      include 'COMMON.GEO'
1040       real(kind=8),dimension(2) :: y,z
1041       real(kind=8) :: theta_max,theta_min,sig,ak
1042 !el local variables
1043       integer :: j,it,k
1044       real(kind=8) :: gama,gama1,thet_pred_mean,theta_temp
1045 !     print *,'gen_theta: it=',it
1046       theta_min=0.05D0*pi
1047       theta_max=0.95D0*pi
1048       if (dabs(gama).gt.dwapi) then
1049         y(1)=dcos(gama)
1050         y(2)=dsin(gama)
1051       else
1052         y(1)=0.0D0
1053         y(2)=0.0D0
1054       endif
1055       if (dabs(gama1).gt.dwapi) then
1056         z(1)=dcos(gama1)
1057         z(2)=dsin(gama1)
1058       else
1059         z(1)=0.0D0
1060         z(2)=0.0D0
1061       endif  
1062       thet_pred_mean=a0thet(it)
1063       do k=1,2
1064         thet_pred_mean=thet_pred_mean+athet(k,it,1,1)*y(k) &
1065            +bthet(k,it,1,1)*z(k)
1066       enddo
1067       sig=polthet(3,it)
1068       do j=2,0,-1
1069         sig=sig*thet_pred_mean+polthet(j,it)
1070       enddo
1071       sig=0.5D0/(sig*sig+sigc0(it))
1072       ak=dexp(gthet(1,it)- &
1073        0.5D0*((gthet(2,it)-thet_pred_mean)/gthet(3,it))**2)
1074 !     print '(i5,5(1pe14.4))',it,(gthet(j,it),j=1,3)
1075 !     print '(5(1pe14.4))',thet_pred_mean,theta0(it),sig,sig0(it),ak
1076       theta_temp=binorm(thet_pred_mean,theta0(it),sig,sig0(it),ak) 
1077       if (theta_temp.lt.theta_min) theta_temp=theta_min
1078       if (theta_temp.gt.theta_max) theta_temp=theta_max
1079       gen_theta=theta_temp
1080 !     print '(a)','Exiting GENTHETA.'
1081       return
1082       end function gen_theta
1083 !-----------------------------------------------------------------------------
1084       subroutine gen_side(it,the,al,om,fail)
1085       use random, only:ran_number,mult_norm1
1086 !      implicit real*8 (a-h,o-z)
1087 !      include 'DIMENSIONS'
1088 !      include 'COMMON.GEO'
1089 !      include 'COMMON.LOCAL'
1090 !      include 'COMMON.SETUP'
1091 !      include 'COMMON.IOUNITS'
1092       real(kind=8) :: MaxBoxLen=10.0D0
1093       real(kind=8),dimension(3,3) :: Ap_inv,a,vec
1094       real(kind=8),dimension(:,:),allocatable :: z !(3,maxlob)
1095       real(kind=8),dimension(:),allocatable :: W1,detAp !(maxlob)
1096       real(kind=8),dimension(:),allocatable :: sumW !(0:maxlob)
1097       real(kind=8),dimension(2) :: y,cm,eig
1098       real(kind=8),dimension(2,2) :: box
1099       real(kind=8),dimension(100) :: work
1100       real(kind=8) :: eig_limit=1.0D-8
1101       real(kind=8) :: Big=10.0D0
1102       logical :: lprint,fail,lcheck
1103 !el local variables
1104       integer :: it,i,j,k,l,nlobit,ial,iom,iii,ilob
1105       real(kind=8) :: the,al,om,detApi,wart,y2,wykl,radmax
1106       real(kind=8) :: tant,zz1,W1i,radius,zk,fac,dV,sum,sum1
1107       real(kind=8) :: which_lobe
1108       lcheck=.false.
1109       lprint=.false.
1110       fail=.false.
1111       if (the.eq.0.0D0 .or. the.eq.pi) then
1112 #ifdef MPI
1113         write (*,'(a,i4,a,i3,a,1pe14.5)') &
1114        'CG Processor:',me,' Error in GenSide: it=',it,' theta=',the
1115 #else
1116 !d        write (iout,'(a,i3,a,1pe14.5)') 
1117 !d     &   'Error in GenSide: it=',it,' theta=',the
1118 #endif
1119         fail=.true.
1120         return
1121       endif
1122       tant=dtan(the-pipol)
1123       nlobit=nlob(it)
1124       allocate(z(3,nlobit))
1125       allocate(W1(nlobit))
1126       allocate(detAp(nlobit))
1127       allocate(sumW(0:nlobit))
1128       if (lprint) then
1129 #ifdef MPI
1130         print '(a,i4,a)','CG Processor:',me,' Enter Gen_Side.'
1131         write (iout,'(a,i4,a)') 'Processor:',me,' Enter Gen_Side.'
1132 #endif
1133         print *,'it=',it,' nlobit=',nlobit,' the=',the,' tant=',tant
1134         write (iout,*) 'it=',it,' nlobit=',nlobit,' the=',the,&
1135            ' tant=',tant
1136       endif
1137       do i=1,nlobit
1138        zz1=tant-censc(1,i,it)
1139         do k=1,3
1140           do l=1,3
1141             a(k,l)=gaussc(k,l,i,it)
1142           enddo
1143         enddo
1144         detApi=a(2,2)*a(3,3)-a(2,3)**2
1145         Ap_inv(2,2)=a(3,3)/detApi
1146         Ap_inv(2,3)=-a(2,3)/detApi
1147         Ap_inv(3,2)=Ap_inv(2,3)
1148         Ap_inv(3,3)=a(2,2)/detApi
1149         if (lprint) then
1150           write (*,'(/a,i2/)') 'Cluster #',i
1151           write (*,'(3(1pe14.5),5x,1pe14.5)') &
1152           ((a(l,k),l=1,3),censc(k,i,it),k=1,3)
1153           write (iout,'(/a,i2/)') 'Cluster #',i
1154           write (iout,'(3(1pe14.5),5x,1pe14.5)') &
1155           ((a(l,k),l=1,3),censc(k,i,it),k=1,3)
1156         endif
1157         W1i=0.0D0
1158         do k=2,3
1159           do l=2,3
1160             W1i=W1i+a(k,1)*a(l,1)*Ap_inv(k,l)
1161           enddo
1162         enddo
1163         W1i=a(1,1)-W1i
1164         W1(i)=dexp(bsc(i,it)-0.5D0*W1i*zz1*zz1)
1165 !        if (lprint) write(*,'(a,3(1pe15.5)/)')
1166 !     &          'detAp, W1, anormi',detApi,W1i,anormi
1167         do k=2,3
1168           zk=censc(k,i,it)
1169           do l=2,3
1170             zk=zk+zz1*Ap_inv(k,l)*a(l,1)
1171           enddo
1172           z(k,i)=zk
1173         enddo
1174         detAp(i)=dsqrt(detApi)
1175       enddo
1176
1177       if (lprint) then
1178         print *,'W1:',(w1(i),i=1,nlobit)
1179         print *,'detAp:',(detAp(i),i=1,nlobit)
1180         print *,'Z'
1181         do i=1,nlobit
1182           print '(i2,3f10.5)',i,(rad2deg*z(j,i),j=2,3)
1183         enddo
1184         write (iout,*) 'W1:',(w1(i),i=1,nlobit)
1185         write (iout,*) 'detAp:',(detAp(i),i=1,nlobit)
1186         write (iout,*) 'Z'
1187         do i=1,nlobit
1188           write (iout,'(i2,3f10.5)') i,(rad2deg*z(j,i),j=2,3)
1189         enddo
1190       endif
1191       if (lcheck) then
1192 ! Writing the distribution just to check the procedure
1193       fac=0.0D0
1194       dV=deg2rad**2*10.0D0
1195       sum=0.0D0
1196       sum1=0.0D0
1197       do i=1,nlobit
1198         fac=fac+W1(i)/detAp(i)
1199       enddo 
1200       fac=1.0D0/(2.0D0*fac*pi)
1201 !d    print *,it,'fac=',fac
1202       do ial=90,180,2
1203         y(1)=deg2rad*ial
1204         do iom=-180,180,5
1205           y(2)=deg2rad*iom
1206           wart=0.0D0
1207           do i=1,nlobit
1208             do j=2,3
1209               do k=2,3
1210                 a(j-1,k-1)=gaussc(j,k,i,it)
1211               enddo
1212             enddo
1213             y2=y(2)
1214
1215             do iii=-1,1
1216           
1217               y(2)=y2+iii*dwapi
1218
1219               wykl=0.0D0
1220               do j=1,2
1221                 do k=1,2 
1222                   wykl=wykl+a(j,k)*(y(j)-z(j+1,i))*(y(k)-z(k+1,i))
1223                 enddo
1224               enddo
1225               wart=wart+W1(i)*dexp(-0.5D0*wykl)
1226
1227             enddo
1228
1229             y(2)=y2
1230
1231           enddo
1232 !         print *,'y',y(1),y(2),' fac=',fac
1233           wart=fac*wart
1234           write (20,'(2f10.3,1pd15.5)') y(1)*rad2deg,y(2)*rad2deg,wart
1235           sum=sum+wart
1236           sum1=sum1+1.0D0
1237         enddo
1238       enddo
1239 !     print *,'it=',it,' sum=',sum*dV,' sum1=',sum1*dV
1240       return
1241       endif
1242
1243 ! Calculate the CM of the system
1244 !
1245       do i=1,nlobit
1246         W1(i)=W1(i)/detAp(i)
1247       enddo
1248       sumW(0)=0.0D0
1249       do i=1,nlobit
1250         sumW(i)=sumW(i-1)+W1(i)
1251       enddo
1252       cm(1)=z(2,1)*W1(1)
1253       cm(2)=z(3,1)*W1(1)
1254       do j=2,nlobit
1255         cm(1)=cm(1)+z(2,j)*W1(j) 
1256         cm(2)=cm(2)+W1(j)*(z(3,1)+pinorm(z(3,j)-z(3,1)))
1257       enddo
1258       cm(1)=cm(1)/sumW(nlobit)
1259       cm(2)=cm(2)/sumW(nlobit)
1260       if (cm(1).gt.Big .or. cm(1).lt.-Big .or. &
1261        cm(2).gt.Big .or. cm(2).lt.-Big) then
1262 !d        write (iout,'(a)') 
1263 !d     & 'Unexpected error in GenSide - CM coordinates too large.'
1264 !d        write (iout,'(i5,2(1pe14.5))') it,cm(1),cm(2)
1265 !d        write (*,'(a)') 
1266 !d     & 'Unexpected error in GenSide - CM coordinates too large.'
1267 !d        write (*,'(i5,2(1pe14.5))') it,cm(1),cm(2)
1268         fail=.true. 
1269         return
1270       endif
1271 !d    print *,'CM:',cm(1),cm(2)
1272 !
1273 ! Find the largest search distance from CM
1274 !
1275       radmax=0.0D0
1276       do i=1,nlobit
1277         do j=2,3
1278           do k=2,3
1279             a(j-1,k-1)=gaussc(j,k,i,it) 
1280           enddo
1281         enddo
1282 #ifdef NAG
1283         call f02faf('N','U',2,a,3,eig,work,100,ifail)
1284 #else
1285         call djacob(2,3,10000,1.0d-10,a,vec,eig)
1286 #endif
1287 #ifdef MPI
1288         if (lprint) then
1289           print *,'*************** CG Processor',me
1290           print *,'CM:',cm(1),cm(2)
1291           write (iout,*) '*************** CG Processor',me
1292           write (iout,*) 'CM:',cm(1),cm(2)
1293           print '(A,8f10.5)','Eigenvalues: ',(1.0/dsqrt(eig(k)),k=1,2)
1294           write (iout,'(A,8f10.5)') &
1295               'Eigenvalues: ',(1.0/dsqrt(eig(k)),k=1,2)
1296         endif
1297 #endif
1298         if (eig(1).lt.eig_limit) then
1299           write(iout,'(a)') &
1300            'From Mult_Norm: Eigenvalues of A are too small.'
1301           write(*,'(a)') &
1302            'From Mult_Norm: Eigenvalues of A are too small.'
1303           fail=.true.
1304           return
1305         endif
1306         radius=0.0D0
1307 !d      print *,'i=',i
1308         do j=1,2
1309           radius=radius+pinorm(z(j+1,i)-cm(j))**2
1310         enddo
1311         radius=dsqrt(radius)+3.0D0/dsqrt(eig(1))
1312         if (radius.gt.radmax) radmax=radius
1313       enddo
1314       if (radmax.gt.pi) radmax=pi
1315 !
1316 ! Determine the boundaries of the search rectangle.
1317 !
1318       if (lprint) then
1319         print '(a,4(1pe14.4))','W1: ',(W1(i),i=1,nlob(it) )
1320         print '(a,4(1pe14.4))','radmax: ',radmax
1321       endif
1322       box(1,1)=dmax1(cm(1)-radmax,0.0D0)
1323       box(2,1)=dmin1(cm(1)+radmax,pi)
1324       box(1,2)=cm(2)-radmax
1325       box(2,2)=cm(2)+radmax
1326       if (lprint) then
1327 #ifdef MPI
1328         print *,'CG Processor',me,' Array BOX:'
1329 #else
1330         print *,'Array BOX:'
1331 #endif
1332         print '(4(1pe14.4))',((box(k,j),k=1,2),j=1,2)
1333         print '(a,4(1pe14.4))','sumW: ',(sumW(i),i=0,nlob(it) )
1334 #ifdef MPI
1335         write (iout,*)'CG Processor',me,' Array BOX:'
1336 #else
1337         write (iout,*)'Array BOX:'
1338 #endif
1339         write(iout,'(4(1pe14.4))') ((box(k,j),k=1,2),j=1,2)
1340         write(iout,'(a,4(1pe14.4))')'sumW: ',(sumW(i),i=0,nlob(it) )
1341       endif
1342       if (box(1,2).lt.-MaxBoxLen .or. box(2,2).gt.MaxBoxLen) then
1343 #ifdef MPI
1344         write (iout,'(a,i4,a,3e15.5)') 'CG Processor:',me,': bad sampling box.',box(1,2),box(2,2),radmax
1345         write (*,'(a,i4,a)') 'CG Processor:',me,': bad sampling box.'
1346 #else
1347 !        write (iout,'(a)') 'Bad sampling box.'
1348 #endif
1349         fail=.true.
1350         return
1351       endif
1352       which_lobe=ran_number(0.0D0,sumW(nlobit))
1353 !     print '(a,1pe14.4)','which_lobe=',which_lobe
1354       do i=1,nlobit
1355         if (sumW(i-1).le.which_lobe .and. sumW(i).ge.which_lobe) goto 1
1356       enddo
1357     1 ilob=i
1358 !     print *,'ilob=',ilob,' nlob=',nlob(it)
1359       do i=2,3
1360         cm(i-1)=z(i,ilob)
1361         do j=2,3
1362           a(i-1,j-1)=gaussc(i,j,ilob,it)
1363         enddo
1364       enddo
1365 !d    print '(a,i4,a)','CG Processor',me,' Calling MultNorm1.'
1366       call mult_norm1(3,2,a,cm,box,y,fail)
1367       if (fail) return
1368       al=y(1)
1369       om=pinorm(y(2))
1370 !d    print *,'al=',al,' om=',om
1371 !d    stop
1372       return
1373       end subroutine gen_side
1374 !-----------------------------------------------------------------------------
1375       subroutine overlap_sc(scfail)
1376 !
1377 !     Internal and cartesian coordinates must be consistent as input,
1378 !     and will be up-to-date on return.
1379 !     At the end of this procedure, scfail is true if there are
1380 !     overlapping residues left, or false otherwise (success)
1381 !
1382 !      implicit real*8 (a-h,o-z)
1383 !      include 'DIMENSIONS'
1384 !      include 'COMMON.CHAIN'
1385 !      include 'COMMON.INTERACT'
1386 !      include 'COMMON.FFIELD'
1387 !      include 'COMMON.VAR'
1388 !      include 'COMMON.SBRIDGE'
1389 !      include 'COMMON.IOUNITS'
1390       logical :: had_overlaps,fail,scfail
1391       integer,dimension(nres) :: ioverlap !(maxres)
1392       integer :: ioverlap_last,k,maxsi,i,iti,nsi
1393       integer :: ires,j
1394
1395       had_overlaps=.false.
1396       call overlap_sc_list(ioverlap,ioverlap_last)
1397       if (ioverlap_last.gt.0) then
1398         write (iout,*) '#OVERLAPing residues ',ioverlap_last
1399         write (iout,'(20i4)') (ioverlap(k),k=1,ioverlap_last)
1400         had_overlaps=.true.
1401       endif
1402
1403       maxsi=1000
1404       do k=1,1000
1405         if (ioverlap_last.eq.0) exit
1406
1407         do ires=1,ioverlap_last 
1408           i=ioverlap(ires)
1409           iti=iabs(itype(i))
1410           if (iti.ne.10) then
1411             nsi=0
1412             fail=.true.
1413             do while (fail.and.nsi.le.maxsi)
1414               call gen_side(iti,theta(i+1),alph(i),omeg(i),fail)
1415               nsi=nsi+1
1416             enddo
1417             if(fail) goto 999
1418           endif
1419         enddo
1420
1421         call chainbuild
1422         call overlap_sc_list(ioverlap,ioverlap_last)
1423 !        write (iout,*) 'Overlaping residues ',ioverlap_last,
1424 !     &           (ioverlap(j),j=1,ioverlap_last)
1425       enddo
1426
1427       if (k.le.1000.and.ioverlap_last.eq.0) then
1428         scfail=.false.
1429         if (had_overlaps) then
1430           write (iout,*) '#OVERLAPing all corrected after ',k,&
1431                ' random generation'
1432         endif
1433       else
1434         scfail=.true.
1435         write (iout,*) '#OVERLAPing NOT all corrected ',ioverlap_last
1436         write (iout,'(20i4)') (ioverlap(j),j=1,ioverlap_last)
1437       endif
1438
1439       return
1440
1441  999  continue
1442       write (iout,'(a30,i5,a12,i4)') &
1443                      '#OVERLAP FAIL in gen_side after',maxsi,&
1444                      'iter for RES',i
1445       scfail=.true.
1446       return
1447       end subroutine overlap_sc
1448 !-----------------------------------------------------------------------------
1449       subroutine overlap_sc_list(ioverlap,ioverlap_last)
1450       use calc_data
1451 !      implicit real*8 (a-h,o-z)
1452 !      include 'DIMENSIONS'
1453 !      include 'COMMON.GEO'
1454 !      include 'COMMON.LOCAL'
1455 !      include 'COMMON.IOUNITS'
1456 !      include 'COMMON.CHAIN'
1457 !      include 'COMMON.INTERACT'
1458 !      include 'COMMON.FFIELD'
1459 !      include 'COMMON.VAR'
1460 !      include 'COMMON.CALC'
1461       logical :: fail
1462       integer,dimension(nres) :: ioverlap !(maxres)
1463       integer :: ioverlap_last
1464 !el local variables
1465       integer :: ind,iint
1466       real(kind=8) :: redfac,sig        !rrij,sigsq,
1467       integer :: itypi,itypj,itypi1
1468       real(kind=8) :: xi,yi,zi,sig0ij,rcomp,rrij,rij_shift
1469       data redfac /0.5D0/
1470
1471       ioverlap_last=0
1472 ! Check for SC-SC overlaps and mark residues
1473 !      print *,'>>overlap_sc nnt=',nnt,' nct=',nct
1474       ind=0
1475       do i=iatsc_s,iatsc_e
1476         itypi=iabs(itype(i))
1477         itypi1=iabs(itype(i+1))
1478         xi=c(1,nres+i)
1479         yi=c(2,nres+i)
1480         zi=c(3,nres+i)
1481         dxi=dc_norm(1,nres+i)
1482         dyi=dc_norm(2,nres+i)
1483         dzi=dc_norm(3,nres+i)
1484         dsci_inv=dsc_inv(itypi)
1485 !
1486        do iint=1,nint_gr(i)
1487          do j=istart(i,iint),iend(i,iint)
1488             ind=ind+1
1489             itypj=iabs(itype(j))
1490             dscj_inv=dsc_inv(itypj)
1491             sig0ij=sigma(itypi,itypj)
1492             chi1=chi(itypi,itypj)
1493             chi2=chi(itypj,itypi)
1494             chi12=chi1*chi2
1495             chip1=chip(itypi)
1496             chip2=chip(itypj)
1497             chip12=chip1*chip2
1498             alf1=alp(itypi)   
1499             alf2=alp(itypj)   
1500             alf12=0.5D0*(alf1+alf2)
1501           if (j.gt.i+1) then
1502            rcomp=sigmaii(itypi,itypj)
1503           else 
1504            rcomp=sigma(itypi,itypj)
1505           endif
1506 !         print '(2(a3,2i3),a3,2f10.5)',
1507 !     &        ' i=',i,iti,' j=',j,itj,' d=',dist(nres+i,nres+j)
1508 !     &        ,rcomp
1509             xj=c(1,nres+j)-xi
1510             yj=c(2,nres+j)-yi
1511             zj=c(3,nres+j)-zi
1512             dxj=dc_norm(1,nres+j)
1513             dyj=dc_norm(2,nres+j)
1514             dzj=dc_norm(3,nres+j)
1515             rrij=1.0D0/(xj*xj+yj*yj+zj*zj)
1516             rij=dsqrt(rrij)
1517             call sc_angular
1518             sigsq=1.0D0/sigsq
1519             sig=sig0ij*dsqrt(sigsq)
1520             rij_shift=1.0D0/rij-sig+sig0ij
1521
1522 !t          if ( 1.0/rij .lt. redfac*rcomp .or. 
1523 !t     &       rij_shift.le.0.0D0 ) then
1524             if ( rij_shift.le.0.0D0 ) then
1525 !d           write (iout,'(a,i3,a,i3,a,f10.5,a,3f10.5)')
1526 !d     &     'overlap SC-SC: i=',i,' j=',j,
1527 !d     &     ' dist=',dist(nres+i,nres+j),' rcomp=',
1528 !d     &     rcomp,1.0/rij,rij_shift
1529           ioverlap_last=ioverlap_last+1
1530           ioverlap(ioverlap_last)=i         
1531           do k=1,ioverlap_last-1
1532            if (ioverlap(k).eq.i) ioverlap_last=ioverlap_last-1
1533           enddo
1534           ioverlap_last=ioverlap_last+1
1535           ioverlap(ioverlap_last)=j         
1536           do k=1,ioverlap_last-1
1537            if (ioverlap(k).eq.j) ioverlap_last=ioverlap_last-1
1538           enddo 
1539          endif
1540         enddo
1541        enddo
1542       enddo
1543       return
1544       end subroutine overlap_sc_list
1545 #endif
1546 !-----------------------------------------------------------------------------
1547 ! energy_p_new_barrier.F
1548 !-----------------------------------------------------------------------------
1549       subroutine sc_angular
1550 ! Calculate eps1,eps2,eps3,sigma, and parts of their derivatives in om1,om2,
1551 ! om12. Called by ebp, egb, and egbv.
1552       use calc_data
1553 !      implicit none
1554 !      include 'COMMON.CALC'
1555 !      include 'COMMON.IOUNITS'
1556       erij(1)=xj*rij
1557       erij(2)=yj*rij
1558       erij(3)=zj*rij
1559       om1=dxi*erij(1)+dyi*erij(2)+dzi*erij(3)
1560       om2=dxj*erij(1)+dyj*erij(2)+dzj*erij(3)
1561       om12=dxi*dxj+dyi*dyj+dzi*dzj
1562       chiom12=chi12*om12
1563 ! Calculate eps1(om12) and its derivative in om12
1564       faceps1=1.0D0-om12*chiom12
1565       faceps1_inv=1.0D0/faceps1
1566       eps1=dsqrt(faceps1_inv)
1567 ! Following variable is eps1*deps1/dom12
1568       eps1_om12=faceps1_inv*chiom12
1569 ! diagnostics only
1570 !      faceps1_inv=om12
1571 !      eps1=om12
1572 !      eps1_om12=1.0d0
1573 !      write (iout,*) "om12",om12," eps1",eps1
1574 ! Calculate sigma(om1,om2,om12) and the derivatives of sigma**2 in om1,om2,
1575 ! and om12.
1576       om1om2=om1*om2
1577       chiom1=chi1*om1
1578       chiom2=chi2*om2
1579       facsig=om1*chiom1+om2*chiom2-2.0D0*om1om2*chiom12
1580       sigsq=1.0D0-facsig*faceps1_inv
1581       sigsq_om1=(chiom1-chiom12*om2)*faceps1_inv
1582       sigsq_om2=(chiom2-chiom12*om1)*faceps1_inv
1583       sigsq_om12=-chi12*(om1om2*faceps1-om12*facsig)*faceps1_inv**2
1584 ! diagnostics only
1585 !      sigsq=1.0d0
1586 !      sigsq_om1=0.0d0
1587 !      sigsq_om2=0.0d0
1588 !      sigsq_om12=0.0d0
1589 !      write (iout,*) "chiom1",chiom1," chiom2",chiom2," chiom12",chiom12
1590 !      write (iout,*) "faceps1",faceps1," faceps1_inv",faceps1_inv,
1591 !     &    " eps1",eps1
1592 ! Calculate eps2 and its derivatives in om1, om2, and om12.
1593       chipom1=chip1*om1
1594       chipom2=chip2*om2
1595       chipom12=chip12*om12
1596       facp=1.0D0-om12*chipom12
1597       facp_inv=1.0D0/facp
1598       facp1=om1*chipom1+om2*chipom2-2.0D0*om1om2*chipom12
1599 !      write (iout,*) "chipom1",chipom1," chipom2",chipom2,
1600 !     &  " chipom12",chipom12," facp",facp," facp_inv",facp_inv
1601 ! Following variable is the square root of eps2
1602       eps2rt=1.0D0-facp1*facp_inv
1603 ! Following three variables are the derivatives of the square root of eps
1604 ! in om1, om2, and om12.
1605       eps2rt_om1=-4.0D0*(chipom1-chipom12*om2)*facp_inv
1606       eps2rt_om2=-4.0D0*(chipom2-chipom12*om1)*facp_inv
1607       eps2rt_om12=4.0D0*chip12*(om1om2*facp-om12*facp1)*facp_inv**2 
1608 ! Evaluate the "asymmetric" factor in the VDW constant, eps3
1609       eps3rt=1.0D0-alf1*om1+alf2*om2-alf12*om12 
1610 !      write (iout,*) "eps2rt",eps2rt," eps3rt",eps3rt
1611 !      write (iout,*) "eps2rt_om1",eps2rt_om1," eps2rt_om2",eps2rt_om2,
1612 !     &  " eps2rt_om12",eps2rt_om12
1613 ! Calculate whole angle-dependent part of epsilon and contributions
1614 ! to its derivatives
1615       return
1616       end subroutine sc_angular
1617 !-----------------------------------------------------------------------------
1618 ! initialize_p.F
1619 !-----------------------------------------------------------------------------
1620       subroutine int_bounds(total_ints,lower_bound,upper_bound)
1621 !      implicit real*8 (a-h,o-z)
1622 !      include 'DIMENSIONS'
1623       include 'mpif.h'
1624 !      include 'COMMON.SETUP'
1625       integer :: total_ints,lower_bound,upper_bound,nint
1626       integer,dimension(0:nfgtasks) :: int4proc,sint4proc       !(0:max_fg_procs)
1627       integer :: i,nexcess
1628       nint=total_ints/nfgtasks
1629       do i=1,nfgtasks
1630         int4proc(i-1)=nint
1631       enddo
1632       nexcess=total_ints-nint*nfgtasks
1633       do i=1,nexcess
1634         int4proc(nfgtasks-i)=int4proc(nfgtasks-i)+1
1635       enddo
1636       lower_bound=0
1637       do i=0,fg_rank-1
1638         lower_bound=lower_bound+int4proc(i)
1639       enddo 
1640       upper_bound=lower_bound+int4proc(fg_rank)
1641       lower_bound=lower_bound+1
1642       return
1643       end subroutine int_bounds
1644 !-----------------------------------------------------------------------------
1645       subroutine int_bounds1(total_ints,lower_bound,upper_bound)
1646 !      implicit real*8 (a-h,o-z)
1647 !      include 'DIMENSIONS'
1648       include 'mpif.h'
1649 !      include 'COMMON.SETUP'
1650       integer :: total_ints,lower_bound,upper_bound,nint
1651       integer :: nexcess,i
1652       integer,dimension(0:nfgtasks) :: int4proc,sint4proc       !(0:max_fg_procs)
1653       nint=total_ints/nfgtasks1
1654       do i=1,nfgtasks1
1655         int4proc(i-1)=nint
1656       enddo
1657       nexcess=total_ints-nint*nfgtasks1
1658       do i=1,nexcess
1659         int4proc(nfgtasks1-i)=int4proc(nfgtasks1-i)+1
1660       enddo
1661       lower_bound=0
1662       do i=0,fg_rank1-1
1663         lower_bound=lower_bound+int4proc(i)
1664       enddo 
1665       upper_bound=lower_bound+int4proc(fg_rank1)
1666       lower_bound=lower_bound+1
1667       return
1668       end subroutine int_bounds1
1669 !-----------------------------------------------------------------------------
1670 ! intcartderiv.F
1671 !-----------------------------------------------------------------------------
1672       subroutine chainbuild_cart
1673 !      implicit real*8 (a-h,o-z)
1674 !      include 'DIMENSIONS'
1675       use control_data
1676 #ifdef MPI
1677       include 'mpif.h'
1678 #endif
1679 !      include 'COMMON.SETUP'
1680 !      include 'COMMON.CHAIN' 
1681 !      include 'COMMON.LOCAL'
1682 !      include 'COMMON.TIME1'
1683 !      include 'COMMON.IOUNITS'
1684       integer :: j,i,ierror,ierr
1685       real(kind=8) :: time00,time01
1686 #ifdef MPI
1687       if (nfgtasks.gt.1) then
1688 !        write (iout,*) "BCAST in chainbuild_cart"
1689 !        call flush(iout)
1690 ! Broadcast the order to build the chain and compute internal coordinates
1691 ! to the slaves. The slaves receive the order in ERGASTULUM.
1692         time00=MPI_Wtime()
1693 !      write (iout,*) "CHAINBUILD_CART: DC before BCAST"
1694 !      do i=0,nres
1695 !        write (iout,'(i3,3f10.5,5x,3f10.5)') i,(dc(j,i),j=1,3),
1696 !     &   (dc(j,i+nres),j=1,3)
1697 !      enddo 
1698         if (fg_rank.eq.0) &
1699           call MPI_Bcast(7,1,MPI_INTEGER,king,FG_COMM,IERROR)
1700         time_bcast7=time_bcast7+MPI_Wtime()-time00
1701         time01=MPI_Wtime()
1702         call MPI_Bcast(dc(1,0),6*(nres+1),MPI_DOUBLE_PRECISION,&
1703           king,FG_COMM,IERR)
1704 !      write (iout,*) "CHAINBUILD_CART: DC after BCAST"
1705 !      do i=0,nres
1706 !        write (iout,'(i3,3f10.5,5x,3f10.5)') i,(dc(j,i),j=1,3),
1707 !     &   (dc(j,i+nres),j=1,3)
1708 !      enddo 
1709 !        write (iout,*) "End BCAST in chainbuild_cart"
1710 !        call flush(iout)
1711         time_bcast=time_bcast+MPI_Wtime()-time00
1712         time_bcastc=time_bcastc+MPI_Wtime()-time01
1713       endif
1714 #endif
1715       do j=1,3
1716         c(j,1)=dc(j,0)
1717       enddo
1718       do i=2,nres
1719         do j=1,3
1720           c(j,i)=c(j,i-1)+dc(j,i-1)
1721         enddo
1722       enddo 
1723       do i=1,nres
1724         do j=1,3
1725           c(j,i+nres)=c(j,i)+dc(j,i+nres)
1726         enddo
1727       enddo
1728 !      write (iout,*) "CHAINBUILD_CART"
1729 !      call cartprint
1730       call int_from_cart1(.false.)
1731       return
1732       end subroutine chainbuild_cart
1733 !-----------------------------------------------------------------------------
1734 ! intcor.f
1735 !-----------------------------------------------------------------------------
1736       real(kind=8) function alpha(i1,i2,i3)
1737 !
1738 !  Calculates the planar angle between atoms (i1), (i2), and (i3).
1739 !
1740 !      implicit real*8 (a-h,o-z)
1741 !      include 'DIMENSIONS'
1742 !      include 'COMMON.GEO'
1743 !      include 'COMMON.CHAIN'
1744 !el local variables
1745       integer :: i1,i2,i3
1746       real(kind=8) :: x12,x23,y12,y23,z12,z23,vnorm,wnorm,scalar
1747       x12=c(1,i1)-c(1,i2)
1748       x23=c(1,i3)-c(1,i2)
1749       y12=c(2,i1)-c(2,i2)
1750       y23=c(2,i3)-c(2,i2)
1751       z12=c(3,i1)-c(3,i2)
1752       z23=c(3,i3)-c(3,i2)
1753       vnorm=dsqrt(x12*x12+y12*y12+z12*z12)
1754       wnorm=dsqrt(x23*x23+y23*y23+z23*z23)
1755       scalar=(x12*x23+y12*y23+z12*z23)/(vnorm*wnorm)
1756       alpha=arcos(scalar)
1757       return
1758       end function alpha
1759 !-----------------------------------------------------------------------------
1760       real(kind=8) function beta(i1,i2,i3,i4)
1761 !
1762 !  Calculates the dihedral angle between atoms (i1), (i2), (i3) and (i4)
1763 !
1764 !      implicit real*8 (a-h,o-z)
1765 !      include 'DIMENSIONS'
1766 !      include 'COMMON.GEO'
1767 !      include 'COMMON.CHAIN'
1768 !el local variables
1769       integer :: i1,i2,i3,i4
1770       real(kind=8) :: x12,x23,x34,y12,y23,y34,z12,z23,z34
1771       real(kind=8) :: wx,wy,wz,wnorm,vx,vy,vz,vnorm,scalar,angle
1772       real(kind=8) :: tx,ty,tz
1773       x12=c(1,i1)-c(1,i2)
1774       x23=c(1,i3)-c(1,i2)
1775       x34=c(1,i4)-c(1,i3)
1776       y12=c(2,i1)-c(2,i2)
1777       y23=c(2,i3)-c(2,i2)
1778       y34=c(2,i4)-c(2,i3)
1779       z12=c(3,i1)-c(3,i2)
1780       z23=c(3,i3)-c(3,i2)
1781       z34=c(3,i4)-c(3,i3)
1782 !d    print '(2i3,3f10.5)',i1,i2,x12,y12,z12
1783 !d    print '(2i3,3f10.5)',i2,i3,x23,y23,z23
1784 !d    print '(2i3,3f10.5)',i3,i4,x34,y34,z34
1785       wx=-y23*z34+y34*z23
1786       wy=x23*z34-z23*x34
1787       wz=-x23*y34+y23*x34
1788       wnorm=dsqrt(wx*wx+wy*wy+wz*wz)
1789       vx=y12*z23-z12*y23
1790       vy=-x12*z23+z12*x23
1791       vz=x12*y23-y12*x23
1792       vnorm=dsqrt(vx*vx+vy*vy+vz*vz)
1793       if (vnorm.gt.1.0D-13 .and. wnorm.gt.1.0D-13) then
1794       scalar=(vx*wx+vy*wy+vz*wz)/(vnorm*wnorm)
1795       if (dabs(scalar).gt.1.0D0) &
1796       scalar=0.99999999999999D0*scalar/dabs(scalar)
1797       angle=dacos(scalar)
1798 !d    print '(2i4,10f7.3)',i2,i3,vx,vy,vz,wx,wy,wz,vnorm,wnorm,
1799 !d   &scalar,angle
1800       else
1801       angle=pi
1802       endif 
1803 !     if (angle.le.0.0D0) angle=pi+angle
1804       tx=vy*wz-vz*wy
1805       ty=-vx*wz+vz*wx
1806       tz=vx*wy-vy*wx
1807       scalar=tx*x23+ty*y23+tz*z23
1808       if (scalar.lt.0.0D0) angle=-angle
1809       beta=angle
1810       return
1811       end function beta
1812 !-----------------------------------------------------------------------------
1813       real(kind=8) function dist(i1,i2)
1814 !
1815 !  Calculates the distance between atoms (i1) and (i2).
1816 !
1817 !      implicit real*8 (a-h,o-z)
1818 !      include 'DIMENSIONS'
1819 !      include 'COMMON.GEO'
1820 !      include 'COMMON.CHAIN'
1821 !el local variables
1822       integer :: i1,i2
1823       real(kind=8) :: x12,y12,z12
1824       x12=c(1,i1)-c(1,i2)
1825       y12=c(2,i1)-c(2,i2)
1826       z12=c(3,i1)-c(3,i2)
1827       dist=dsqrt(x12*x12+y12*y12+z12*z12)
1828       return
1829       end function dist
1830 #if !defined(WHAM_RUN) && !defined(CLUSTER)
1831 !-----------------------------------------------------------------------------
1832 ! local_move.f
1833 !-----------------------------------------------------------------------------
1834       subroutine local_move_init(debug)
1835 !rc      implicit none
1836
1837 !     Includes
1838 !      implicit real*8 (a-h,o-z)
1839 !      include 'DIMENSIONS'  ! Needed by COMMON.LOCAL
1840 !      include 'COMMON.GEO'  ! For pi, deg2rad
1841 !      include 'COMMON.LOCAL'  ! For vbl
1842 !      include 'COMMON.LOCMOVE'
1843
1844 !     INPUT arguments
1845       logical :: debug
1846
1847
1848 !     Determine wheter to do some debugging output
1849       locmove_output=debug
1850
1851 !     Set the init_called flag to 1
1852       init_called=1
1853
1854 !     The following are never changed
1855       min_theta=60.D0*deg2rad  ! (0,PI)
1856       max_theta=175.D0*deg2rad  ! (0,PI)
1857       dmin2=vbl*vbl*2.*(1.-cos(min_theta))
1858       dmax2=vbl*vbl*2.*(1.-cos(max_theta))
1859       flag=1.0D300
1860       small=1.0D-5
1861       small2=0.5*small*small
1862
1863 !     Not really necessary...
1864       a_n=0
1865       b_n=0
1866       res_n=0
1867
1868       return
1869       end subroutine local_move_init
1870 !-----------------------------------------------------------------------------
1871       subroutine local_move(n_start, n_end, PHImin, PHImax)
1872 !     Perform a local move between residues m and n (inclusive)
1873 !     PHImin and PHImax [0,PI] determine the size of the move
1874 !     Works on whatever structure is in the variables theta and phi,
1875 !     sidechain variables are left untouched
1876 !     The final structure is NOT minimized, but both the cartesian
1877 !     variables c and the angles are up-to-date at the end (no further
1878 !     chainbuild is required)
1879 !rc      implicit none
1880       use random,only:ran_number
1881 !     Includes
1882 !      implicit real*8 (a-h,o-z)
1883 !      include 'DIMENSIONS'
1884 !      include 'COMMON.GEO'
1885 !      include 'COMMON.CHAIN'
1886 !      include 'COMMON.VAR'
1887 !      include 'COMMON.MINIM'
1888 !      include 'COMMON.SBRIDGE'
1889 !      include 'COMMON.LOCMOVE'
1890
1891 !     External functions
1892 !EL      integer move_res
1893 !EL      external move_res
1894 !EL      double precision ran_number
1895 !EL      external ran_number
1896
1897 !     INPUT arguments
1898       integer :: n_start, n_end  ! First and last residues to move
1899       real(kind=8) :: PHImin, PHImax  ! min/max angles [0,PI]
1900
1901 !     Local variables
1902       integer :: i,j
1903       real(kind=8) :: min,max
1904       integer :: iretcode
1905
1906
1907 !     Check if local_move_init was called.  This assumes that it
1908 !     would not be 1 if not explicitely initialized
1909       if (init_called.ne.1) then
1910         write(6,*)'   ***   local_move_init not called!!!'
1911         stop
1912       endif
1913
1914 !     Quick check for crazy range
1915       if (n_start.gt.n_end .or. n_start.lt.1 .or. n_end.gt.nres) then
1916         write(6,'(a,i3,a,i3)') &
1917              '   ***   Cannot make local move between n_start = ',&
1918              n_start,' and n_end = ',n_end
1919         return
1920       endif
1921
1922 !     Take care of end residues first...
1923       if (n_start.eq.1) then
1924 !     Move residue 1 (completely random)
1925         theta(3)=ran_number(min_theta,max_theta)
1926         phi(4)=ran_number(-PI,PI)
1927         i=2
1928       else
1929         i=n_start
1930       endif
1931       if (n_end.eq.nres) then
1932 !     Move residue nres (completely random)
1933         theta(nres)=ran_number(min_theta,max_theta)
1934         phi(nres)=ran_number(-PI,PI)
1935         j=nres-1
1936       else
1937         j=n_end
1938       endif
1939
1940 !     ...then go through all other residues one by one
1941 !     Start from the two extremes and converge
1942       call chainbuild
1943       do while (i.le.j)
1944         min=PHImin
1945         max=PHImax
1946 !$$$c     Move the first two residues by less than the others
1947 !$$$        if (i-n_start.lt.3) then
1948 !$$$          if (i-n_start.eq.0) then
1949 !$$$            min=0.4*PHImin
1950 !$$$            max=0.4*PHImax
1951 !$$$          else if (i-n_start.eq.1) then
1952 !$$$            min=0.8*PHImin
1953 !$$$            max=0.8*PHImax
1954 !$$$          else if (i-n_start.eq.2) then
1955 !$$$            min=PHImin
1956 !$$$            max=PHImax
1957 !$$$          endif
1958 !$$$        endif
1959
1960 !     The actual move, on residue i
1961         iretcode=move_res(min,max,i)  ! Discard iretcode
1962         i=i+1
1963
1964         if (i.le.j) then
1965           min=PHImin
1966           max=PHImax
1967 !$$$c     Move the last two residues by less than the others
1968 !$$$          if (n_end-j.lt.3) then
1969 !$$$            if (n_end-j.eq.0) then
1970 !$$$              min=0.4*PHImin
1971 !$$$              max=0.4*PHImax
1972 !$$$            else if (n_end-j.eq.1) then
1973 !$$$              min=0.8*PHImin
1974 !$$$              max=0.8*PHImax
1975 !$$$            else if (n_end-j.eq.2) then
1976 !$$$              min=PHImin
1977 !$$$              max=PHImax
1978 !$$$            endif
1979 !$$$          endif
1980
1981 !     The actual move, on residue j
1982           iretcode=move_res(min,max,j)  ! Discard iretcode
1983           j=j-1
1984         endif
1985       enddo
1986
1987       call int_from_cart(.false.,.false.)
1988
1989       return
1990       end subroutine local_move
1991 !-----------------------------------------------------------------------------
1992       subroutine output_tabs
1993 !     Prints out the contents of a_..., b_..., res_...
1994 !      implicit none
1995
1996 !     Includes
1997 !      include 'COMMON.GEO'
1998 !      include 'COMMON.LOCMOVE'
1999
2000 !     Local variables
2001       integer :: i,j
2002
2003       write(6,*)'a_...'
2004       write(6,'(8f7.1)')(a_ang(i)*rad2deg,i=0,a_n-1)
2005       write(6,'(8(2x,3l1,2x))')((a_tab(i,j),i=0,2),j=0,a_n-1)
2006
2007       write(6,*)'b_...'
2008       write(6,'(4f7.1)')(b_ang(i)*rad2deg,i=0,b_n-1)
2009       write(6,'(4(2x,3l1,2x))')((b_tab(i,j),i=0,2),j=0,b_n-1)
2010
2011       write(6,*)'res_...'
2012       write(6,'(12f7.1)')(res_ang(i)*rad2deg,i=0,res_n-1)
2013       write(6,'(12(2x,3l1,2x))')((res_tab(0,i,j),i=0,2),j=0,res_n-1)
2014       write(6,'(12(2x,3l1,2x))')((res_tab(1,i,j),i=0,2),j=0,res_n-1)
2015       write(6,'(12(2x,3l1,2x))')((res_tab(2,i,j),i=0,2),j=0,res_n-1)
2016
2017       return
2018       end subroutine output_tabs
2019 !-----------------------------------------------------------------------------
2020       subroutine angles2tab(PHImin,PHImax,n,ang,tab)
2021 !     Only uses angles if [0,PI] (but PHImin cannot be 0.,
2022 !     and PHImax cannot be PI)
2023 !      implicit none
2024
2025 !     Includes
2026 !      include 'COMMON.GEO'
2027
2028 !     INPUT arguments
2029       real(kind=8) :: PHImin,PHImax
2030
2031 !     OUTPUT arguments
2032       integer :: n
2033       real(kind=8),dimension(0:3) :: ang
2034       logical,dimension(0:2,0:3) :: tab
2035
2036
2037       if (PHImin .eq. PHImax) then
2038 !     Special case with two 010's
2039         n = 2;
2040         ang(0) = -PHImin;
2041         ang(1) = PHImin;
2042         tab(0,0) = .false.
2043         tab(2,0) = .false.
2044         tab(0,1) = .false.
2045         tab(2,1) = .false.
2046         tab(1,0) = .true.
2047         tab(1,1) = .true.
2048       else if (PHImin .eq. PI) then
2049 !     Special case with one 010
2050         n = 1
2051         ang(0) = PI
2052         tab(0,0) = .false.
2053         tab(2,0) = .false.
2054         tab(1,0) = .true.
2055       else if (PHImax .eq. 0.) then
2056 !     Special case with one 010
2057         n = 1
2058         ang(0) = 0.
2059         tab(0,0) = .false.
2060         tab(2,0) = .false.
2061         tab(1,0) = .true.
2062       else
2063 !     Standard cases
2064         n = 0
2065         if (PHImin .gt. 0.) then
2066 !     Start of range (011)
2067           ang(n) = PHImin
2068           tab(0,n) = .false.
2069           tab(1,n) = .true.
2070           tab(2,n) = .true.
2071 !     End of range (110)
2072           ang(n+1) = -PHImin
2073           tab(0,n+1) = .true.
2074           tab(1,n+1) = .true.
2075           tab(2,n+1) = .false.
2076           n = n+2
2077         endif
2078         if (PHImax .lt. PI) then
2079 !     Start of range (011)
2080           ang(n) = -PHImax
2081           tab(0,n) = .false.
2082           tab(1,n) = .true.
2083           tab(2,n) = .true.
2084 !     End of range (110)
2085           ang(n+1) = PHImax
2086           tab(0,n+1) = .true.
2087           tab(1,n+1) = .true.
2088           tab(2,n+1) = .false.
2089           n = n+2
2090         endif
2091       endif
2092
2093       return
2094       end subroutine angles2tab
2095 !-----------------------------------------------------------------------------
2096       subroutine minmax_angles(x,y,z,r,n,ang,tab)
2097 !     When solutions do not exist, assume all angles
2098 !     are acceptable - i.e., initial geometry must be correct
2099 !      implicit none
2100
2101 !     Includes
2102 !      include 'COMMON.GEO'
2103 !      include 'COMMON.LOCMOVE'
2104
2105 !     Input arguments
2106       real(kind=8) :: x,y,z,r
2107
2108 !     Output arguments
2109       integer :: n
2110       real(kind=8),dimension(0:3) :: ang
2111       logical,dimension(0:2,0:3) :: tab
2112
2113 !     Local variables
2114       real(kind=8) :: num, denom, phi
2115       real(kind=8) :: Kmin, Kmax
2116       integer :: i
2117
2118
2119       num = x*x + y*y + z*z
2120       denom = x*x + y*y
2121       n = 0
2122       if (denom .gt. 0.) then
2123         phi = atan2(y,x)
2124         denom = 2.*r*sqrt(denom)
2125         num = num+r*r
2126         Kmin = (num - dmin2)/denom
2127         Kmax = (num - dmax2)/denom
2128
2129 !     Allowed values of K (else all angles are acceptable)
2130 !     -1 <= Kmin <  1
2131 !     -1 <  Kmax <= 1
2132         if (Kmin .gt. 1. .or. abs(Kmin-1.) .lt. small2) then
2133           Kmin = -flag
2134         else if (Kmin .lt. -1. .or. abs(Kmin+1.) .lt. small2) then
2135           Kmin = PI
2136         else
2137           Kmin = acos(Kmin)
2138         endif
2139
2140         if (Kmax .lt. -1. .or. abs(Kmax+1.) .lt. small2) then
2141           Kmax = flag
2142         else if (Kmax .gt. 1. .or. abs(Kmax-1.) .lt. small2) then
2143           Kmax = 0.
2144         else
2145           Kmax = acos(Kmax)
2146         endif
2147
2148         if (Kmax .lt. Kmin) Kmax = Kmin
2149
2150         call angles2tab(Kmin, Kmax, n, ang, tab)
2151
2152 !     Add phi and check that angles are within range (-PI,PI]
2153         do i=0,n-1
2154           ang(i) = ang(i)+phi
2155           if (ang(i) .le. -PI) then
2156             ang(i) = ang(i)+2.*PI
2157           else if (ang(i) .gt. PI) then
2158             ang(i) = ang(i)-2.*PI
2159           endif
2160         enddo
2161       endif
2162
2163       return
2164       end subroutine minmax_angles
2165 !-----------------------------------------------------------------------------
2166       subroutine construct_tab
2167 !     Take a_... and b_... values and produces the results res_...
2168 !     x_ang are assumed to be all different (diff > small)
2169 !     x_tab(1,i) must be 1 for all i (i.e., all x_ang are acceptable)
2170 !      implicit none
2171
2172 !     Includes
2173 !      include 'COMMON.LOCMOVE'
2174
2175 !     Local variables
2176       integer :: n_max,i,j,index
2177       logical :: done
2178       real(kind=8) :: phi
2179
2180
2181       n_max = a_n + b_n
2182       if (n_max .eq. 0) then
2183         res_n = 0
2184         return
2185       endif
2186
2187       do i=0,n_max-1
2188         do j=0,1
2189           res_tab(j,0,i) = .true.
2190           res_tab(j,2,i) = .true.
2191           res_tab(j,1,i) = .false.
2192         enddo
2193       enddo
2194
2195       index = 0
2196       phi = -flag
2197       done = .false.
2198       do while (.not.done)
2199         res_ang(index) = flag
2200
2201 !     Check a first...
2202         do i=0,a_n-1
2203           if ((a_ang(i)-phi).gt.small .and. &
2204                a_ang(i) .lt. res_ang(index)) then
2205 !     Found a lower angle
2206             res_ang(index) = a_ang(i)
2207 !     Copy the values from a_tab into res_tab(0,,)
2208             res_tab(0,0,index) = a_tab(0,i)
2209             res_tab(0,1,index) = a_tab(1,i)
2210             res_tab(0,2,index) = a_tab(2,i)
2211 !     Set default values for res_tab(1,,)
2212             res_tab(1,0,index) = .true.
2213             res_tab(1,1,index) = .false.
2214             res_tab(1,2,index) = .true.
2215           else if (abs(a_ang(i)-res_ang(index)).lt.small) then
2216 !     Found an equal angle (can only be equal to a b_ang)
2217             res_tab(0,0,index) = a_tab(0,i)
2218             res_tab(0,1,index) = a_tab(1,i)
2219             res_tab(0,2,index) = a_tab(2,i)
2220           endif
2221         enddo
2222 !     ...then check b
2223         do i=0,b_n-1
2224           if ((b_ang(i)-phi).gt.small .and. &
2225                b_ang(i) .lt. res_ang(index)) then
2226 !     Found a lower angle
2227             res_ang(index) = b_ang(i)
2228 !     Copy the values from b_tab into res_tab(1,,)
2229             res_tab(1,0,index) = b_tab(0,i)
2230             res_tab(1,1,index) = b_tab(1,i)
2231             res_tab(1,2,index) = b_tab(2,i)
2232 !     Set default values for res_tab(0,,)
2233             res_tab(0,0,index) = .true.
2234             res_tab(0,1,index) = .false.
2235             res_tab(0,2,index) = .true.
2236           else if (abs(b_ang(i)-res_ang(index)).lt.small) then
2237 !     Found an equal angle (can only be equal to an a_ang)
2238             res_tab(1,0,index) = b_tab(0,i)
2239             res_tab(1,1,index) = b_tab(1,i)
2240             res_tab(1,2,index) = b_tab(2,i)
2241           endif
2242         enddo
2243
2244         if (res_ang(index) .eq. flag) then
2245           res_n = index
2246           done = .true.
2247         else if (index .eq. n_max-1) then
2248           res_n = n_max
2249           done = .true.
2250         else
2251           phi = res_ang(index)  ! Store previous angle
2252           index = index+1
2253         endif
2254       enddo
2255
2256 !     Fill the gaps
2257 !     First a...
2258       index = 0
2259       if (a_n .gt. 0) then
2260         do while (.not.res_tab(0,1,index))
2261           index=index+1
2262         enddo
2263         done = res_tab(0,2,index)
2264         do i=index+1,res_n-1
2265           if (res_tab(0,1,i)) then
2266             done = res_tab(0,2,i)
2267           else
2268             res_tab(0,0,i) = done
2269             res_tab(0,1,i) = done
2270             res_tab(0,2,i) = done
2271           endif
2272         enddo
2273         done = res_tab(0,0,index)
2274         do i=index-1,0,-1
2275           if (res_tab(0,1,i)) then
2276             done = res_tab(0,0,i)
2277           else
2278             res_tab(0,0,i) = done
2279             res_tab(0,1,i) = done
2280             res_tab(0,2,i) = done
2281           endif
2282         enddo
2283       else
2284         do i=0,res_n-1
2285           res_tab(0,0,i) = .true.
2286           res_tab(0,1,i) = .true.
2287           res_tab(0,2,i) = .true.
2288         enddo
2289       endif
2290 !     ...then b
2291       index = 0
2292       if (b_n .gt. 0) then
2293         do while (.not.res_tab(1,1,index))
2294           index=index+1
2295         enddo
2296         done = res_tab(1,2,index)
2297         do i=index+1,res_n-1
2298           if (res_tab(1,1,i)) then
2299             done = res_tab(1,2,i)
2300           else
2301             res_tab(1,0,i) = done
2302             res_tab(1,1,i) = done
2303             res_tab(1,2,i) = done
2304           endif
2305         enddo
2306         done = res_tab(1,0,index)
2307         do i=index-1,0,-1
2308           if (res_tab(1,1,i)) then
2309             done = res_tab(1,0,i)
2310           else
2311             res_tab(1,0,i) = done
2312             res_tab(1,1,i) = done
2313             res_tab(1,2,i) = done
2314           endif
2315         enddo
2316       else
2317         do i=0,res_n-1
2318           res_tab(1,0,i) = .true.
2319           res_tab(1,1,i) = .true.
2320           res_tab(1,2,i) = .true.
2321         enddo
2322       endif
2323
2324 !     Finally fill the last row with AND operation
2325       do i=0,res_n-1
2326         do j=0,2
2327           res_tab(2,j,i) = (res_tab(0,j,i) .and. res_tab(1,j,i))
2328         enddo
2329       enddo
2330
2331       return
2332       end subroutine construct_tab
2333 !-----------------------------------------------------------------------------
2334       subroutine construct_ranges(phi_n,phi_start,phi_end)
2335 !     Given the data in res_..., construct a table of 
2336 !     min/max allowed angles
2337 !      implicit none
2338
2339 !     Includes
2340 !      include 'COMMON.GEO'
2341 !      include 'COMMON.LOCMOVE'
2342
2343 !     Output arguments
2344       integer :: phi_n
2345       real(kind=8),dimension(0:11) :: phi_start,phi_end
2346
2347 !     Local variables
2348       logical :: done
2349       integer :: index
2350
2351
2352       if (res_n .eq. 0) then
2353 !     Any move is allowed
2354         phi_n = 1
2355         phi_start(0) = -PI
2356         phi_end(0) = PI
2357       else
2358         phi_n = 0
2359         index = 0
2360         done = .false.
2361         do while (.not.done)
2362 !     Find start of range (01x)
2363           done = .false.
2364           do while (.not.done)
2365             if (res_tab(2,0,index).or.(.not.res_tab(2,1,index))) then
2366               index=index+1
2367             else
2368               done = .true.
2369               phi_start(phi_n) = res_ang(index)
2370             endif
2371             if (index .eq. res_n) done = .true.
2372           enddo
2373 !     If a start was found (index < res_n), find the end of range (x10)
2374 !     It may not be found without wrapping around
2375           if (index .lt. res_n) then
2376             done = .false.
2377             do while (.not.done)
2378               if ((.not.res_tab(2,1,index)).or.res_tab(2,2,index)) then
2379                 index=index+1
2380               else
2381                 done = .true.
2382               endif
2383               if (index .eq. res_n) done = .true.
2384             enddo
2385             if (index .lt. res_n) then
2386 !     Found the end of the range
2387               phi_end(phi_n) = res_ang(index)
2388               phi_n=phi_n+1
2389               index=index+1
2390               if (index .eq. res_n) then
2391                 done = .true.
2392               else
2393                 done = .false.
2394               endif
2395             else
2396 !     Need to wrap around
2397               done = .true.
2398               phi_end(phi_n) = flag
2399             endif
2400           endif
2401         enddo
2402 !     Take care of the last one if need to wrap around
2403         if (phi_end(phi_n) .eq. flag) then
2404           index = 0
2405           do while ((.not.res_tab(2,1,index)).or.res_tab(2,2,index))
2406             index=index+1
2407           enddo
2408           phi_end(phi_n) = res_ang(index) + 2.*PI
2409           phi_n=phi_n+1
2410         endif
2411       endif
2412
2413       return
2414       end subroutine construct_ranges
2415 !-----------------------------------------------------------------------------
2416       subroutine fix_no_moves(phi)
2417 !      implicit none
2418
2419 !     Includes
2420 !      include 'COMMON.GEO'
2421 !      include 'COMMON.LOCMOVE'
2422
2423 !     Output arguments
2424       real(kind=8) :: phi
2425
2426 !     Local variables
2427       integer :: index
2428       real(kind=8) :: diff,temp
2429
2430
2431 !     Look for first 01x in gammas (there MUST be at least one)
2432       diff = flag
2433       index = 0
2434       do while (res_tab(1,0,index) .or. (.not.res_tab(1,1,index)))
2435         index=index+1
2436       enddo
2437       if (res_ang(index) .le. 0.D0) then ! Make sure it's from PHImax
2438 !     Try to increase PHImax
2439         if (index .gt. 0) then
2440           phi = res_ang(index-1)
2441           diff = abs(res_ang(index) - res_ang(index-1))
2442         endif
2443 !     Look for last (corresponding) x10
2444         index = res_n - 1
2445         do while ((.not.res_tab(1,1,index)) .or. res_tab(1,2,index))
2446           index=index-1
2447         enddo
2448         if (index .lt. res_n-1) then
2449           temp = abs(res_ang(index) - res_ang(index+1))
2450           if (temp .lt. diff) then
2451             phi = res_ang(index+1)
2452             diff = temp
2453           endif
2454         endif
2455       endif
2456
2457 !     If increasing PHImax didn't work, decreasing PHImin
2458 !     will (with one exception)
2459 !     Look for first x10 (there MUST be at least one)
2460       index = 0
2461       do while ((.not.res_tab(1,1,index)) .or. res_tab(1,2,index))
2462         index=index+1
2463       enddo
2464       if (res_ang(index) .lt. 0.D0) then ! Make sure it's from PHImin
2465 !     Try to decrease PHImin
2466         if (index .lt. res_n-1) then
2467           temp = abs(res_ang(index) - res_ang(index+1))
2468           if (res_ang(index+1) .le. 0.D0 .and. temp .lt. diff) then
2469             phi = res_ang(index+1)
2470             diff = temp
2471           endif
2472         endif
2473 !     Look for last (corresponding) 01x
2474         index = res_n - 1
2475         do while (res_tab(1,0,index) .or. (.not.res_tab(1,1,index)))
2476           index=index-1
2477         enddo
2478         if (index .gt. 0) then
2479           temp = abs(res_ang(index) - res_ang(index-1))
2480           if (res_ang(index-1) .ge. 0.D0 .and. temp .lt. diff) then
2481             phi = res_ang(index-1)
2482             diff = temp
2483           endif
2484         endif
2485       endif
2486
2487 !     If it still didn't work, it must be PHImax == 0. or PHImin == PI
2488       if (diff .eq. flag) then
2489         index = 0
2490         if (res_tab(index,1,0) .or. (.not.res_tab(index,1,1)) .or. &
2491              res_tab(index,1,2)) index = res_n - 1
2492 !     This MUST work at this point
2493         if (index .eq. 0) then
2494           phi = res_ang(1)
2495         else
2496           phi = res_ang(index - 1)
2497         endif
2498       endif
2499
2500       return
2501       end subroutine fix_no_moves
2502 !-----------------------------------------------------------------------------
2503       integer function move_res(PHImin,PHImax,i_move)
2504 !     Moves residue i_move (in array c), leaving everything else fixed
2505 !     Starting geometry is not checked, it should be correct!
2506 !     R(,i_move) is the only residue that will move, but must have
2507 !     1 < i_move < nres (i.e., cannot move ends)
2508 !     Whether any output is done is controlled by locmove_output
2509 !rc      implicit none
2510       use random,only:ran_number
2511 !     Includes
2512 !      implicit real*8 (a-h,o-z)
2513 !      include 'DIMENSIONS'
2514 !      include 'COMMON.CHAIN'
2515 !      include 'COMMON.GEO'
2516 !      include 'COMMON.LOCMOVE'
2517
2518 !     External functions
2519 !EL      double precision ran_number
2520 !EL      external ran_number
2521
2522 !     Input arguments
2523       real(kind=8) :: PHImin,PHImax
2524       integer :: i_move
2525
2526 !     RETURN VALUES:
2527 !     0: move successfull
2528 !     1: Dmin or Dmax had to be modified
2529 !     2: move failed - check your input geometry
2530
2531
2532 !     Local variables
2533       real(kind=8),dimension(0:2) :: X,Y,Z,Orig
2534       real(kind=8),dimension(0:2) :: P
2535       logical :: no_moves,done
2536       integer :: index,i,j
2537       real(kind=8) :: phi,temp,radius
2538       real(kind=8),dimension(0:11) :: phi_start,phi_end
2539       integer :: phi_n
2540
2541 !     Set up the coordinate system
2542       do i=0,2
2543         Orig(i)=0.5*(c(i+1,i_move-1)+c(i+1,i_move+1)) ! Position of origin
2544       enddo
2545
2546       do i=0,2
2547         Z(i)=c(i+1,i_move+1)-c(i+1,i_move-1)
2548       enddo
2549       temp=sqrt(Z(0)*Z(0)+Z(1)*Z(1)+Z(2)*Z(2))
2550       do i=0,2
2551         Z(i)=Z(i)/temp
2552       enddo
2553
2554       do i=0,2
2555         X(i)=c(i+1,i_move)-Orig(i)
2556       enddo
2557 !     radius is the radius of the circle on which c(,i_move) can move
2558       radius=sqrt(X(0)*X(0)+X(1)*X(1)+X(2)*X(2))
2559       do i=0,2
2560         X(i)=X(i)/radius
2561       enddo
2562
2563       Y(0)=Z(1)*X(2)-X(1)*Z(2)
2564       Y(1)=X(0)*Z(2)-Z(0)*X(2)
2565       Y(2)=Z(0)*X(1)-X(0)*Z(1)
2566
2567 !     Calculate min, max angles coming from dmin, dmax to c(,i_move-2)
2568       if (i_move.gt.2) then
2569         do i=0,2
2570           P(i)=c(i+1,i_move-2)-Orig(i)
2571         enddo
2572         call minmax_angles(P(0)*X(0)+P(1)*X(1)+P(2)*X(2),&
2573              P(0)*Y(0)+P(1)*Y(1)+P(2)*Y(2),&
2574              P(0)*Z(0)+P(1)*Z(1)+P(2)*Z(2),&
2575              radius,a_n,a_ang,a_tab)
2576       else
2577         a_n=0
2578       endif
2579
2580 !     Calculate min, max angles coming from dmin, dmax to c(,i_move+2)
2581       if (i_move.lt.nres-2) then
2582         do i=0,2
2583           P(i)=c(i+1,i_move+2)-Orig(i)
2584         enddo
2585         call minmax_angles(P(0)*X(0)+P(1)*X(1)+P(2)*X(2),&
2586              P(0)*Y(0)+P(1)*Y(1)+P(2)*Y(2),&
2587              P(0)*Z(0)+P(1)*Z(1)+P(2)*Z(2),&
2588              radius,b_n,b_ang,b_tab)
2589       else
2590         b_n=0
2591       endif
2592
2593 !     Construct the resulting table for alpha and beta
2594       call construct_tab()
2595
2596       if (locmove_output) then
2597         print *,'ALPHAS & BETAS TABLE'
2598         call output_tabs()
2599       endif
2600
2601 !     Check that there is at least one possible move
2602       no_moves = .true.
2603       if (res_n .eq. 0) then
2604         no_moves = .false.
2605       else
2606         index = 0
2607         do while ((index .lt. res_n) .and. no_moves)
2608           if (res_tab(2,1,index)) no_moves = .false.
2609           index=index+1
2610         enddo
2611       endif
2612       if (no_moves) then
2613         if (locmove_output) print *,'   ***   Cannot move anywhere'
2614         move_res=2
2615         return
2616       endif
2617
2618 !     Transfer res_... into a_...
2619       a_n = 0
2620       do i=0,res_n-1
2621         if ( (res_tab(2,0,i).neqv.res_tab(2,1,i)) .or. &
2622              (res_tab(2,0,i).neqv.res_tab(2,2,i)) ) then
2623           a_ang(a_n) = res_ang(i)
2624           do j=0,2
2625             a_tab(j,a_n) = res_tab(2,j,i)
2626           enddo
2627           a_n=a_n+1
2628         endif
2629       enddo
2630
2631 !     Check that the PHI's are within [0,PI]
2632       if (PHImin .lt. 0. .or. abs(PHImin) .lt. small) PHImin = -flag
2633       if (PHImin .gt. PI .or. abs(PHImin-PI) .lt. small) PHImin = PI
2634       if (PHImax .gt. PI .or. abs(PHImax-PI) .lt. small) PHImax = flag
2635       if (PHImax .lt. 0. .or. abs(PHImax) .lt. small) PHImax = 0.
2636       if (PHImax .lt. PHImin) PHImax = PHImin
2637 !     Calculate min and max angles coming from PHImin and PHImax,
2638 !     and put them in b_...
2639       call angles2tab(PHImin, PHImax, b_n, b_ang, b_tab)
2640 !     Construct the final table
2641       call construct_tab()
2642
2643       if (locmove_output) then
2644         print *,'FINAL TABLE'
2645         call output_tabs()
2646       endif
2647
2648 !     Check that there is at least one possible move
2649       no_moves = .true.
2650       if (res_n .eq. 0) then
2651         no_moves = .false.
2652       else
2653         index = 0
2654         do while ((index .lt. res_n) .and. no_moves)
2655           if (res_tab(2,1,index)) no_moves = .false.
2656           index=index+1
2657         enddo
2658       endif
2659
2660       if (no_moves) then
2661 !     Take care of the case where no solution exists...
2662         call fix_no_moves(phi)
2663         if (locmove_output) then
2664           print *,'   ***   Had to modify PHImin or PHImax'
2665           print *,'phi: ',phi*rad2deg
2666         endif
2667         move_res=1
2668       else
2669 !     ...or calculate the solution
2670 !     Construct phi_start/phi_end arrays
2671         call construct_ranges(phi_n, phi_start, phi_end)
2672 !     Choose random angle phi in allowed range(s)
2673         temp = 0.
2674         do i=0,phi_n-1
2675           temp = temp + phi_end(i) - phi_start(i)
2676         enddo
2677         phi = ran_number(phi_start(0),phi_start(0)+temp)
2678         index = 0
2679         done = .false.
2680         do while (.not.done)
2681           if (phi .lt. phi_end(index)) then
2682             done = .true.
2683           else
2684             index=index+1
2685           endif
2686           if (index .eq. phi_n) then
2687             done = .true.
2688           else if (.not.done) then
2689             phi = phi + phi_start(index) - phi_end(index-1)
2690           endif
2691         enddo
2692         if (index.eq.phi_n) phi=phi_end(phi_n-1) ! Fix numerical errors
2693         if (phi .gt. PI) phi = phi-2.*PI
2694
2695         if (locmove_output) then
2696           print *,'ALLOWED RANGE(S)'
2697           do i=0,phi_n-1
2698             print *,phi_start(i)*rad2deg,phi_end(i)*rad2deg
2699           enddo
2700           print *,'phi: ',phi*rad2deg
2701         endif
2702         move_res=0
2703       endif
2704
2705 !     Re-use radius as temp variable
2706       temp=radius*cos(phi)
2707       radius=radius*sin(phi)
2708       do i=0,2
2709         c(i+1,i_move)=Orig(i)+temp*X(i)+radius*Y(i)
2710       enddo
2711
2712       return
2713       end function move_res
2714 !-----------------------------------------------------------------------------
2715       subroutine loc_test
2716 !rc      implicit none
2717
2718 !     Includes
2719 !      implicit real*8 (a-h,o-z)
2720 !      include 'DIMENSIONS'
2721 !      include 'COMMON.GEO'
2722 !      include 'COMMON.LOCAL'
2723 !      include 'COMMON.LOCMOVE'
2724
2725 !     External functions
2726 !EL      integer move_res
2727 !EL      external move_res
2728
2729 !     Local variables
2730       integer :: i,j,imov
2731       integer :: phi_n
2732       real(kind=8),dimension(0:11) :: phi_start,phi_end
2733       real(kind=8) :: phi
2734       real(kind=8),dimension(0:2,0:5) :: R
2735
2736       locmove_output=.true.
2737
2738 !      call angles2tab(30.*deg2rad,70.*deg2rad,a_n,a_ang,a_tab)
2739 !      call angles2tab(80.*deg2rad,130.*deg2rad,b_n,b_ang,b_tab)
2740 !      call minmax_angles(0.D0,3.8D0,0.D0,3.8D0,b_n,b_ang,b_tab)
2741 !      call construct_tab
2742 !      call output_tabs
2743
2744 !      call construct_ranges(phi_n,phi_start,phi_end)
2745 !      do i=0,phi_n-1
2746 !        print *,phi_start(i)*rad2deg,phi_end(i)*rad2deg
2747 !      enddo
2748
2749 !      call fix_no_moves(phi)
2750 !      print *,'NO MOVES FOUND, BEST PHI IS',phi*rad2deg
2751
2752       R(0,0)=0.D0
2753       R(1,0)=0.D0
2754       R(2,0)=0.D0
2755       R(0,1)=0.D0
2756       R(1,1)=-cos(28.D0*deg2rad)
2757       R(2,1)=-0.5D0-sin(28.D0*deg2rad)
2758       R(0,2)=0.D0
2759       R(1,2)=0.D0
2760       R(2,2)=-0.5D0
2761       R(0,3)=cos(30.D0*deg2rad)
2762       R(1,3)=0.D0
2763       R(2,3)=0.D0
2764       R(0,4)=0.D0
2765       R(1,4)=0.D0
2766       R(2,4)=0.5D0
2767       R(0,5)=0.D0
2768       R(1,5)=cos(26.D0*deg2rad)
2769       R(2,5)=0.5D0+sin(26.D0*deg2rad)
2770       do i=1,5
2771         do j=0,2
2772           R(j,i)=vbl*R(j,i)
2773         enddo
2774       enddo
2775 !      i=move_res(R(0,1),0.D0*deg2rad,180.D0*deg2rad)
2776       imov=2
2777       i=move_res(0.D0*deg2rad,180.D0*deg2rad,imov)
2778       print *,'RETURNED ',i
2779       print *,(R(i,3)/vbl,i=0,2)
2780
2781       return
2782       end subroutine loc_test
2783 #endif
2784 !-----------------------------------------------------------------------------
2785 ! matmult.f
2786 !-----------------------------------------------------------------------------
2787       subroutine MATMULT(A1,A2,A3)
2788 !      implicit real*8 (a-h,o-z)
2789 !      include 'DIMENSIONS'
2790 !el local variables
2791       integer :: i,j,k
2792       real(kind=8) :: A3IJ
2793
2794       real(kind=8),DIMENSION(3,3) :: A1,A2,A3
2795       real(kind=8),DIMENSION(3,3) :: AI3
2796       DO 1 I=1,3
2797         DO 2 J=1,3
2798           A3IJ=0.0
2799           DO 3 K=1,3
2800     3       A3IJ=A3IJ+A1(I,K)*A2(K,J)
2801           AI3(I,J)=A3IJ
2802     2   CONTINUE
2803     1 CONTINUE
2804       DO 4 I=1,3
2805       DO 4 J=1,3
2806     4   A3(I,J)=AI3(I,J)
2807       return
2808       end subroutine MATMULT
2809 !-----------------------------------------------------------------------------
2810 ! readpdb.F
2811 !-----------------------------------------------------------------------------
2812       subroutine int_from_cart(lside,lprn)
2813 !      implicit real*8 (a-h,o-z)
2814 !      include 'DIMENSIONS'
2815       use control_data,only:out1file
2816 #ifdef MPI
2817       include "mpif.h"
2818 #endif
2819 !      include 'COMMON.LOCAL'
2820 !      include 'COMMON.VAR'
2821 !      include 'COMMON.CHAIN'
2822 !      include 'COMMON.INTERACT'
2823 !      include 'COMMON.IOUNITS'
2824 !      include 'COMMON.GEO'
2825 !      include 'COMMON.NAMES'
2826 !      include 'COMMON.CONTROL'
2827 !      include 'COMMON.SETUP'
2828       character(len=3) :: seq,res
2829 !      character*5 atom
2830       character(len=80) :: card
2831       real(kind=8),dimension(3,20) :: sccor
2832       integer :: i,j,iti !el  rescode,
2833       logical :: lside,lprn
2834       real(kind=8) :: di,cosfac,sinfac
2835       integer :: nres2
2836       nres2=2*nres
2837
2838       if(me.eq.king.or..not.out1file)then
2839        if (lprn) then 
2840         write (iout,'(/a)') &
2841         'Internal coordinates calculated from crystal structure.'
2842         if (lside) then 
2843           write (iout,'(8a)') '  Res  ','       dvb','     Theta',&
2844        '     Gamma','    Dsc_id','       Dsc','     Alpha',&
2845        '     Beta '
2846         else 
2847           write (iout,'(4a)') '  Res  ','       dvb','     Theta',&
2848        '     Gamma'
2849         endif
2850        endif
2851       endif
2852       do i=1,nres-1
2853 !in wham      do i=1,nres
2854         iti=itype(i)
2855         if (dist(i,i+1).lt.2.0D0 .or. dist(i,i+1).gt.5.0D0) then
2856           write (iout,'(a,i4)') 'Bad Cartesians for residue',i
2857 !test          stop
2858         endif
2859 !#ifndef WHAM_RUN
2860         vbld(i+1)=dist(i,i+1)
2861         vbld_inv(i+1)=1.0d0/vbld(i+1)
2862 !#endif
2863         if (i.gt.1) theta(i+1)=alpha(i-1,i,i+1)
2864         if (i.gt.2) phi(i+1)=beta(i-2,i-1,i,i+1)
2865       enddo
2866 !el -----
2867 !#ifdef WHAM_RUN
2868 !      if (itype(1).eq.ntyp1) then
2869 !        do j=1,3
2870 !          c(j,1)=c(j,2)+(c(j,3)-c(j,4))
2871 !        enddo
2872 !      endif
2873 !      if (itype(nres).eq.ntyp1) then
2874 !        do j=1,3
2875 !          c(j,nres)=c(j,nres-1)+(c(j,nres-2)-c(j,nres-3))
2876 !        enddo
2877 !      endif
2878 !#endif
2879 !      if (unres_pdb) then
2880 !        if (itype(1).eq.21) then
2881 !          theta(3)=90.0d0*deg2rad
2882 !          phi(4)=180.0d0*deg2rad
2883 !          vbld(2)=3.8d0
2884 !          vbld_inv(2)=1.0d0/vbld(2)
2885 !        endif
2886 !        if (itype(nres).eq.21) then
2887 !          theta(nres)=90.0d0*deg2rad
2888 !          phi(nres)=180.0d0*deg2rad
2889 !          vbld(nres)=3.8d0
2890 !          vbld_inv(nres)=1.0d0/vbld(2)
2891 !        endif
2892 !      endif
2893       if (lside) then
2894         do i=2,nres-1
2895           do j=1,3
2896             c(j,nres2+2)=0.5D0*(2*c(j,i)+(c(j,i-1)-c(j,i))*vbld_inv(i) &
2897            +(c(j,i+1)-c(j,i))*vbld_inv(i+1))
2898 ! in wham            c(j,maxres2)=0.5D0*(c(j,i-1)+c(j,i+1)
2899           enddo
2900           iti=itype(i)
2901           di=dist(i,nres+i)
2902 !#ifndef WHAM_RUN
2903 ! 10/03/12 Adam: Correction for zero SC-SC bond length
2904           if (itype(i).ne.10 .and. itype(i).ne.ntyp1 .and. di.eq.0.0d0) &
2905            di=dsc(itype(i))
2906           vbld(i+nres)=di
2907           if (itype(i).ne.10) then
2908             vbld_inv(i+nres)=1.0d0/di
2909           else
2910             vbld_inv(i+nres)=0.0d0
2911           endif
2912 !#endif
2913           if (iti.ne.10) then
2914             alph(i)=alpha(nres+i,i,nres2+2)
2915             omeg(i)=beta(nres+i,i,nres2+2,i+1)
2916           endif
2917           if(me.eq.king.or..not.out1file)then
2918            if (lprn) &
2919            write (iout,'(a3,i4,7f10.3)') restyp(iti),i,vbld(i),&
2920            rad2deg*theta(i),rad2deg*phi(i),dsc(iti),vbld(nres+i),&
2921            rad2deg*alph(i),rad2deg*omeg(i)
2922           endif
2923         enddo
2924       else if (lprn) then
2925         do i=2,nres
2926           iti=itype(i)
2927           if(me.eq.king.or..not.out1file) &
2928            write (iout,'(a3,i4,7f10.3)') restyp(iti),i,dist(i,i-1),&
2929            rad2deg*theta(i),rad2deg*phi(i)
2930         enddo
2931       endif
2932       return
2933       end subroutine int_from_cart
2934 !-----------------------------------------------------------------------------
2935       subroutine sc_loc_geom(lprn)
2936 !      implicit real*8 (a-h,o-z)
2937 !      include 'DIMENSIONS'
2938       use control_data,only:out1file
2939 #ifdef MPI
2940       include "mpif.h"
2941 #endif
2942 !      include 'COMMON.LOCAL'
2943 !      include 'COMMON.VAR'
2944 !      include 'COMMON.CHAIN'
2945 !      include 'COMMON.INTERACT'
2946 !      include 'COMMON.IOUNITS'
2947 !      include 'COMMON.GEO'
2948 !      include 'COMMON.NAMES'
2949 !      include 'COMMON.CONTROL'
2950 !      include 'COMMON.SETUP'
2951       real(kind=8),dimension(3) :: x_prime,y_prime,z_prime
2952       logical :: lprn
2953 !el local variables
2954       integer :: i,j,it,iti
2955       real(kind=8) :: cosfac2,sinfac2,xx,yy,zz,cosfac,sinfac
2956       do i=1,nres-1
2957         do j=1,3
2958           dc_norm(j,i)=vbld_inv(i+1)*(c(j,i+1)-c(j,i))
2959         enddo
2960       enddo
2961       do i=2,nres-1
2962         if (itype(i).ne.10) then
2963           do j=1,3
2964             dc_norm(j,i+nres)=vbld_inv(i+nres)*(c(j,i+nres)-c(j,i))
2965           enddo
2966         else
2967           do j=1,3
2968             dc_norm(j,i+nres)=0.0d0
2969           enddo
2970         endif
2971       enddo
2972       do i=2,nres-1
2973         costtab(i+1) =dcos(theta(i+1))
2974         sinttab(i+1) =dsqrt(1-costtab(i+1)*costtab(i+1))
2975         cost2tab(i+1)=dsqrt(0.5d0*(1.0d0+costtab(i+1)))
2976         sint2tab(i+1)=dsqrt(0.5d0*(1.0d0-costtab(i+1)))
2977         cosfac2=0.5d0/(1.0d0+costtab(i+1))
2978         cosfac=dsqrt(cosfac2)
2979         sinfac2=0.5d0/(1.0d0-costtab(i+1))
2980         sinfac=dsqrt(sinfac2)
2981         it=itype(i)
2982
2983         if ((it.ne.10).and.(it.ne.ntyp1)) then
2984 !el        if (it.ne.10) then
2985 !
2986 !  Compute the axes of tghe local cartesian coordinates system; store in
2987 !   x_prime, y_prime and z_prime 
2988 !
2989         do j=1,3
2990           x_prime(j) = 0.00
2991           y_prime(j) = 0.00
2992           z_prime(j) = 0.00
2993         enddo
2994         do j = 1,3
2995           x_prime(j) = (dc_norm(j,i) - dc_norm(j,i-1))*cosfac
2996           y_prime(j) = (dc_norm(j,i) + dc_norm(j,i-1))*sinfac
2997         enddo
2998         call vecpr(x_prime,y_prime,z_prime)
2999 !
3000 ! Transform the unit vector of the ith side-chain centroid, dC_norm(*,i),
3001 ! to local coordinate system. Store in xx, yy, zz.
3002 !
3003         xx=0.0d0
3004         yy=0.0d0
3005         zz=0.0d0
3006         do j = 1,3
3007           xx = xx + x_prime(j)*dc_norm(j,i+nres)
3008           yy = yy + y_prime(j)*dc_norm(j,i+nres)
3009           zz = zz + z_prime(j)*dc_norm(j,i+nres)
3010         enddo
3011
3012         xxref(i)=xx
3013         yyref(i)=yy
3014         zzref(i)=zz
3015         else
3016         xxref(i)=0.0d0
3017         yyref(i)=0.0d0
3018         zzref(i)=0.0d0
3019         endif
3020       enddo
3021       if (lprn) then
3022         do i=2,nres
3023           iti=itype(i)
3024           if(me.eq.king.or..not.out1file) &
3025            write (iout,'(a3,i4,3f10.5)') restyp(iti),i,xxref(i),&
3026             yyref(i),zzref(i)
3027         enddo
3028       endif
3029  
3030       return
3031       end subroutine sc_loc_geom
3032 !-----------------------------------------------------------------------------
3033       subroutine sccenter(ires,nscat,sccor)
3034 !      implicit real*8 (a-h,o-z)
3035 !      include 'DIMENSIONS'
3036 !      include 'COMMON.CHAIN'
3037       integer :: i,j,ires,nscat
3038       real(kind=8),dimension(3,20) :: sccor
3039       real(kind=8) :: sccmj
3040       do j=1,3
3041         sccmj=0.0D0
3042         do i=1,nscat
3043           sccmj=sccmj+sccor(j,i) 
3044         enddo
3045         dc(j,ires)=sccmj/nscat
3046       enddo
3047       return
3048       end subroutine sccenter
3049 #if !defined(WHAM_RUN) && !defined(CLUSTER)
3050 !-----------------------------------------------------------------------------
3051       subroutine bond_regular
3052       use calc_data
3053 !      implicit real*8 (a-h,o-z)
3054 !      include 'DIMENSIONS'   
3055 !      include 'COMMON.VAR'
3056 !      include 'COMMON.LOCAL'      
3057 !      include 'COMMON.CALC'
3058 !      include 'COMMON.INTERACT'
3059 !      include 'COMMON.CHAIN'
3060       do i=1,nres-1
3061        vbld(i+1)=vbl
3062        vbld_inv(i+1)=1.0d0/vbld(i+1)
3063        vbld(i+1+nres)=dsc(itype(i+1))
3064        vbld_inv(i+1+nres)=dsc_inv(itype(i+1))
3065 !       print *,vbld(i+1),vbld(i+1+nres)
3066       enddo
3067       return
3068       end subroutine bond_regular
3069 #endif
3070 !-----------------------------------------------------------------------------
3071 ! refsys.f
3072 !-----------------------------------------------------------------------------
3073       subroutine refsys(i2,i3,i4,e1,e2,e3,fail)
3074 ! This subroutine calculates unit vectors of a local reference system
3075 ! defined by atoms (i2), (i3), and (i4). The x axis is the axis from
3076 ! atom (i3) to atom (i2), and the xy plane is the plane defined by atoms
3077 ! (i2), (i3), and (i4). z axis is directed according to the sign of the
3078 ! vector product (i3)-(i2) and (i3)-(i4). Sets fail to .true. if atoms
3079 ! (i2) and (i3) or (i3) and (i4) coincide or atoms (i2), (i3), and (i4)
3080 ! form a linear fragment. Returns vectors e1, e2, and e3.
3081 !      implicit real*8 (a-h,o-z)
3082 !      include 'DIMENSIONS'
3083       logical :: fail
3084       real(kind=8),dimension(3) :: e1,e2,e3
3085       real(kind=8),dimension(3) :: u,z
3086 !      include 'COMMON.IOUNITS'
3087 !      include 'COMMON.CHAIN'
3088       real(kind=8) :: coinc=1.0D-13,align=1.0D-13
3089 !el local variables
3090       integer :: i,i1,i2,i3,i4
3091       real(kind=8) :: v1,v2,v3,s1,s2,zi,ui,anorm
3092       fail=.false.
3093       s1=0.0
3094       s2=0.0
3095       do 1 i=1,3
3096       zi=c(i,i2)-c(i,i3)
3097       ui=c(i,i4)-c(i,i3)
3098       s1=s1+zi*zi
3099       s2=s2+ui*ui
3100       z(i)=zi
3101     1 u(i)=ui
3102       s1=sqrt(s1)
3103       s2=sqrt(s2)
3104       if (s1.gt.coinc) goto 2
3105       write (iout,1000) i2,i3,i1
3106       fail=.true.
3107 !     do 3 i=1,3
3108 !   3 c(i,i1)=0.0D0
3109       return
3110     2 if (s2.gt.coinc) goto 4
3111       write(iout,1000) i3,i4,i1
3112       fail=.true.
3113       do 5 i=1,3
3114     5 c(i,i1)=0.0D0
3115       return
3116     4 s1=1.0/s1
3117       s2=1.0/s2
3118       v1=z(2)*u(3)-z(3)*u(2)
3119       v2=z(3)*u(1)-z(1)*u(3)
3120       v3=z(1)*u(2)-z(2)*u(1)
3121       anorm=dsqrt(v1*v1+v2*v2+v3*v3)
3122       if (anorm.gt.align) goto 6
3123       write (iout,1010) i2,i3,i4,i1
3124       fail=.true.
3125 !     do 7 i=1,3
3126 !   7 c(i,i1)=0.0D0
3127       return
3128     6 anorm=1.0D0/anorm
3129       e3(1)=v1*anorm
3130       e3(2)=v2*anorm
3131       e3(3)=v3*anorm
3132       e1(1)=z(1)*s1
3133       e1(2)=z(2)*s1
3134       e1(3)=z(3)*s1
3135       e2(1)=e1(3)*e3(2)-e1(2)*e3(3)
3136       e2(2)=e1(1)*e3(3)-e1(3)*e3(1)
3137       e2(3)=e1(2)*e3(1)-e1(1)*e3(2)
3138  1000 format (/1x,' * * * Error - atoms',i4,' and',i4,' coincide.',&
3139        'coordinates of atom',i4,' are set to zero.')
3140  1010 format (/1x,' * * * Error - atoms',2(i4,2h, ),i4,' form a linear',&
3141        ' fragment. coordinates of atom',i4,' are set to zero.')
3142       return
3143       end subroutine refsys
3144 !-----------------------------------------------------------------------------
3145 ! int_to_cart.f
3146 !-----------------------------------------------------------------------------
3147       subroutine int_to_cart
3148 !--------------------------------------------------------------         
3149 !  This subroutine converts the energy derivatives from internal 
3150 !  coordinates to cartesian coordinates
3151 !-------------------------------------------------------------
3152 !      implicit real*8 (a-h,o-z)
3153 !      include 'DIMENSIONS'
3154 !      include 'COMMON.VAR'
3155 !      include 'COMMON.CHAIN'
3156 !      include 'COMMON.DERIV'
3157 !      include 'COMMON.GEO'
3158 !      include 'COMMON.LOCAL'
3159 !      include 'COMMON.INTERACT'
3160 !      include 'COMMON.MD'
3161 !      include 'COMMON.IOUNITS'
3162 !      include 'COMMON.SCCOR' 
3163 !   calculating dE/ddc1  
3164 !el local variables
3165        integer :: j,i
3166        if (nres.lt.3) go to 18
3167        do j=1,3
3168          gcart(j,1)=gcart(j,1)+gloc(1,icg)*dphi(j,1,4) &
3169            +gloc(nres-2,icg)*dtheta(j,1,3)       
3170          if(itype(2).ne.10) then
3171           gcart(j,1)=gcart(j,1)+gloc(ialph(2,1),icg)*dalpha(j,1,2)+ &
3172           gloc(ialph(2,1)+nside,icg)*domega(j,1,2)              
3173         endif
3174        enddo
3175 !     Calculating the remainder of dE/ddc2
3176        do j=1,3
3177          gcart(j,2)=gcart(j,2)+gloc(1,icg)*dphi(j,2,4)+ &
3178          gloc(nres-2,icg)*dtheta(j,2,3)+gloc(nres-1,icg)*dtheta(j,1,4)
3179         if(itype(2).ne.10) then
3180           gcart(j,2)=gcart(j,2)+gloc(ialph(2,1),icg)*dalpha(j,2,2)+ &
3181           gloc(ialph(2,1)+nside,icg)*domega(j,2,2)
3182         endif
3183         if(itype(3).ne.10) then
3184           gcart(j,2)=gcart(j,2)+gloc(ialph(3,1),icg)*dalpha(j,1,3)+ &
3185           gloc(ialph(3,1)+nside,icg)*domega(j,1,3)
3186         endif
3187         if(nres.gt.4) then
3188           gcart(j,2)=gcart(j,2)+gloc(2,icg)*dphi(j,1,5)
3189         endif                   
3190        enddo
3191 !  If there are only five residues       
3192        if(nres.eq.5) then
3193          do j=1,3
3194            gcart(j,3)=gcart(j,3)+gloc(1,icg)*dphi(j,3,4)+gloc(2,icg)* &
3195            dphi(j,2,5)+gloc(nres-1,icg)*dtheta(j,2,4)+gloc(nres,icg)* &
3196            dtheta(j,1,5)
3197          if(itype(3).ne.10) then
3198            gcart(j,3)=gcart(j,3)+gloc(ialph(3,1),icg)* &
3199            dalpha(j,2,3)+gloc(ialph(3,1)+nside,icg)*domega(j,2,3)
3200          endif
3201          if(itype(4).ne.10) then
3202            gcart(j,3)=gcart(j,3)+gloc(ialph(4,1),icg)* &
3203            dalpha(j,1,4)+gloc(ialph(4,1)+nside,icg)*domega(j,1,4)
3204          endif
3205         enddo
3206        endif
3207 !    If there are more than five residues
3208       if(nres.gt.5) then                           
3209         do i=3,nres-3
3210          do j=1,3
3211           gcart(j,i)=gcart(j,i)+gloc(i-2,icg)*dphi(j,3,i+1) &
3212           +gloc(i-1,icg)*dphi(j,2,i+2)+ &
3213           gloc(i,icg)*dphi(j,1,i+3)+gloc(nres+i-4,icg)*dtheta(j,2,i+1)+ &
3214           gloc(nres+i-3,icg)*dtheta(j,1,i+2)
3215           if(itype(i).ne.10) then
3216            gcart(j,i)=gcart(j,i)+gloc(ialph(i,1),icg)*dalpha(j,2,i)+ &
3217            gloc(ialph(i,1)+nside,icg)*domega(j,2,i)
3218           endif
3219           if(itype(i+1).ne.10) then
3220            gcart(j,i)=gcart(j,i)+gloc(ialph(i+1,1),icg)*dalpha(j,1,i+1) &
3221            +gloc(ialph(i+1,1)+nside,icg)*domega(j,1,i+1)
3222           endif
3223          enddo
3224         enddo
3225       endif     
3226 !  Setting dE/ddnres-2       
3227       if(nres.gt.5) then
3228          do j=1,3
3229            gcart(j,nres-2)=gcart(j,nres-2)+gloc(nres-4,icg)* &
3230            dphi(j,3,nres-1)+gloc(nres-3,icg)*dphi(j,2,nres) &
3231            +gloc(2*nres-6,icg)* &
3232            dtheta(j,2,nres-1)+gloc(2*nres-5,icg)*dtheta(j,1,nres)
3233           if(itype(nres-2).ne.10) then
3234               gcart(j,nres-2)=gcart(j,nres-2)+gloc(ialph(nres-2,1),icg)* &
3235               dalpha(j,2,nres-2)+gloc(ialph(nres-2,1)+nside,icg)* &
3236               domega(j,2,nres-2)
3237           endif
3238           if(itype(nres-1).ne.10) then
3239              gcart(j,nres-2)=gcart(j,nres-2)+gloc(ialph(nres-1,1),icg)* &
3240              dalpha(j,1,nres-1)+gloc(ialph(nres-1,1)+nside,icg)* &
3241              domega(j,1,nres-1)
3242           endif
3243          enddo
3244       endif 
3245 !  Settind dE/ddnres-1       
3246        do j=1,3
3247         gcart(j,nres-1)=gcart(j,nres-1)+gloc(nres-3,icg)*dphi(j,3,nres)+ &
3248         gloc(2*nres-5,icg)*dtheta(j,2,nres)
3249         if(itype(nres-1).ne.10) then
3250           gcart(j,nres-1)=gcart(j,nres-1)+gloc(ialph(nres-1,1),icg)* &
3251           dalpha(j,2,nres-1)+gloc(ialph(nres-1,1)+nside,icg)* &
3252           domega(j,2,nres-1)
3253         endif
3254         enddo
3255 !   The side-chain vector derivatives
3256         do i=2,nres-1
3257          if(itype(i).ne.10 .and. itype(i).ne.ntyp1) then        
3258             do j=1,3    
3259               gxcart(j,i)=gxcart(j,i)+gloc(ialph(i,1),icg)*dalpha(j,3,i) &
3260               +gloc(ialph(i,1)+nside,icg)*domega(j,3,i)
3261             enddo
3262          endif      
3263        enddo                                                                                                                                                    
3264 !----------------------------------------------------------------------
3265 ! INTERTYP=1 SC...Ca...Ca...Ca
3266 ! INTERTYP=2 Ca...Ca...Ca...SC
3267 ! INTERTYP=3 SC...Ca...Ca...SC
3268 !   calculating dE/ddc1      
3269   18   continue
3270 !       do i=1,nres
3271 !       gloc(i,icg)=0.0D0
3272 !          write (iout,*) "poczotkoawy",i,gloc_sc(1,i,icg)
3273 !       enddo
3274        if (nres.lt.2) return
3275        if ((nres.lt.3).and.(itype(1).eq.10)) return
3276        if ((itype(1).ne.10).and.(itype(1).ne.ntyp1)) then
3277         do j=1,3
3278 !c Derviative was calculated for oposite vector of side chain therefore
3279 ! there is "-" sign before gloc_sc
3280          gxcart(j,1)=gxcart(j,1)-gloc_sc(1,0,icg)* &
3281            dtauangle(j,1,1,3)
3282          gcart(j,1)=gcart(j,1)+gloc_sc(1,0,icg)* &
3283            dtauangle(j,1,2,3)
3284           if ((itype(2).ne.10).and.(itype(2).ne.ntyp1)) then
3285          gxcart(j,1)= gxcart(j,1) &
3286                      -gloc_sc(3,0,icg)*dtauangle(j,3,1,3)
3287          gcart(j,1)=gcart(j,1)+gloc_sc(3,0,icg)* &
3288             dtauangle(j,3,2,3)
3289           endif
3290        enddo
3291        endif
3292          if ((nres.ge.3).and.(itype(3).ne.10).and.(itype(3).ne.ntyp1)) &
3293       then
3294          do j=1,3
3295          gcart(j,1)=gcart(j,1)+gloc_sc(2,1,icg)*dtauangle(j,2,1,4)
3296          enddo
3297          endif
3298 !   As potetnial DO NOT depend on omicron anlge their derivative is
3299 !   ommited 
3300 !     &     +gloc_sc(intertyp,nres-2,icg)*dtheta(j,1,3)  
3301
3302 !     Calculating the remainder of dE/ddc2
3303        do j=1,3
3304          if((itype(2).ne.10).and.(itype(2).ne.ntyp1)) then
3305            if (itype(1).ne.10) gxcart(j,2)=gxcart(j,2)+ &
3306                                gloc_sc(3,0,icg)*dtauangle(j,3,3,3)
3307         if ((itype(3).ne.10).and.(nres.ge.3).and.(itype(3).ne.ntyp1)) &
3308          then
3309            gxcart(j,2)=gxcart(j,2)-gloc_sc(3,1,icg)*dtauangle(j,3,1,4)
3310 !c                  the   - above is due to different vector direction
3311            gcart(j,2)=gcart(j,2)+gloc_sc(3,1,icg)*dtauangle(j,3,2,4)
3312           endif
3313           if (nres.gt.3) then
3314            gxcart(j,2)=gxcart(j,2)-gloc_sc(1,1,icg)*dtauangle(j,1,1,4)
3315 !c                  the   - above is due to different vector direction
3316            gcart(j,2)=gcart(j,2)+gloc_sc(1,1,icg)*dtauangle(j,1,2,4)
3317 !          write(iout,*) gloc_sc(1,1,icg),dtauangle(j,1,2,4),"gcart"
3318 !           write(iout,*) gloc_sc(1,1,icg),dtauangle(j,1,1,4),"gx"
3319           endif
3320          endif
3321          if ((itype(1).ne.10).and.(itype(1).ne.ntyp1)) then
3322           gcart(j,2)=gcart(j,2)+gloc_sc(1,0,icg)*dtauangle(j,1,3,3)
3323 !           write(iout,*)  gloc_sc(1,0,icg),dtauangle(j,1,3,3)
3324         endif
3325          if ((itype(3).ne.10).and.(nres.ge.3)) then
3326           gcart(j,2)=gcart(j,2)+gloc_sc(2,1,icg)*dtauangle(j,2,2,4)
3327 !           write(iout,*) gloc_sc(2,1,icg),dtauangle(j,2,2,4)
3328          endif
3329          if ((itype(4).ne.10).and.(nres.ge.4)) then
3330           gcart(j,2)=gcart(j,2)+gloc_sc(2,2,icg)*dtauangle(j,2,1,5)
3331 !           write(iout,*) gloc_sc(2,2,icg),dtauangle(j,2,1,5)
3332          endif
3333
3334 !      write(iout,*) gcart(j,2),itype(2),itype(1),itype(3), "gcart2"
3335        enddo
3336 !    If there are more than five residues
3337       if(nres.ge.5) then                        
3338         do i=3,nres-2
3339          do j=1,3
3340 !          write(iout,*) "before", gcart(j,i)
3341           if ((itype(i).ne.10).and.(itype(i).ne.ntyp1)) then
3342           gxcart(j,i)=gxcart(j,i)+gloc_sc(2,i-2,icg) &
3343           *dtauangle(j,2,3,i+1) &
3344           -gloc_sc(1,i-1,icg)*dtauangle(j,1,1,i+2)
3345           gcart(j,i)=gcart(j,i)+gloc_sc(1,i-1,icg) &
3346           *dtauangle(j,1,2,i+2)
3347 !                   write(iout,*) "new",j,i,
3348 !     &  gcart(j,i),gloc_sc(1,i-1,icg),dtauangle(j,1,2,i+2)
3349           if (itype(i-1).ne.10) then
3350            gxcart(j,i)=gxcart(j,i)+gloc_sc(3,i-2,icg) &
3351       *dtauangle(j,3,3,i+1)
3352           endif
3353           if (itype(i+1).ne.10) then
3354            gxcart(j,i)=gxcart(j,i)-gloc_sc(3,i-1,icg) &
3355       *dtauangle(j,3,1,i+2)
3356            gcart(j,i)=gcart(j,i)+gloc_sc(3,i-1,icg) &
3357       *dtauangle(j,3,2,i+2)
3358           endif
3359           endif
3360           if (itype(i-1).ne.10) then
3361            gcart(j,i)=gcart(j,i)+gloc_sc(1,i-2,icg)* &
3362            dtauangle(j,1,3,i+1)
3363           endif
3364           if (itype(i+1).ne.10) then
3365            gcart(j,i)=gcart(j,i)+gloc_sc(2,i-1,icg)* &
3366            dtauangle(j,2,2,i+2)
3367 !          write(iout,*) "numer",i,gloc_sc(2,i-1,icg),
3368 !     &    dtauangle(j,2,2,i+2)
3369           endif
3370           if (itype(i+2).ne.10) then
3371            gcart(j,i)=gcart(j,i)+gloc_sc(2,i,icg)* &
3372            dtauangle(j,2,1,i+3)
3373           endif
3374          enddo
3375         enddo
3376       endif     
3377 !  Setting dE/ddnres-1       
3378       if(nres.ge.4) then
3379          do j=1,3
3380          if ((itype(nres-1).ne.10).and.(itype(nres-1).ne.ntyp1)) then
3381          gxcart(j,nres-1)=gxcart(j,nres-1)+gloc_sc(2,nres-3,icg) &
3382           *dtauangle(j,2,3,nres)
3383 !          write (iout,*) "gxcart(nres-1)", gloc_sc(2,nres-3,icg),
3384 !     &     dtauangle(j,2,3,nres), gxcart(j,nres-1)
3385          if (itype(nres-2).ne.10) then
3386         gxcart(j,nres-1)=gxcart(j,nres-1)+gloc_sc(3,nres-3,icg) &
3387           *dtauangle(j,3,3,nres)
3388           endif
3389          if ((itype(nres).ne.10).and.(itype(nres).ne.ntyp1)) then
3390         gxcart(j,nres-1)=gxcart(j,nres-1)-gloc_sc(3,nres-2,icg) &
3391           *dtauangle(j,3,1,nres+1)
3392         gcart(j,nres-1)=gcart(j,nres-1)+gloc_sc(3,nres-2,icg) &
3393           *dtauangle(j,3,2,nres+1)
3394           endif
3395          endif
3396          if ((itype(nres-2).ne.10).and.(itype(nres-2).ne.ntyp1)) then
3397             gcart(j,nres-1)=gcart(j,nres-1)+gloc_sc(1,nres-3,icg)* &
3398          dtauangle(j,1,3,nres)
3399          endif
3400           if ((itype(nres).ne.10).and.(itype(nres).ne.ntyp1)) then
3401             gcart(j,nres-1)=gcart(j,nres-1)+gloc_sc(2,nres-2,icg)* &
3402            dtauangle(j,2,2,nres+1)
3403 !           write (iout,*) "gcart(nres-1)", gloc_sc(2,nres-2,icg),
3404 !     &     dtauangle(j,2,2,nres+1), itype(nres-1),itype(nres)
3405            endif
3406          enddo
3407       endif
3408 !  Settind dE/ddnres       
3409        if ((nres.ge.3).and.(itype(nres).ne.10).and. &
3410           (itype(nres).ne.ntyp1))then
3411        do j=1,3
3412         gxcart(j,nres)=gxcart(j,nres)+gloc_sc(3,nres-2,icg) &
3413        *dtauangle(j,3,3,nres+1)+gloc_sc(2,nres-2,icg) &
3414        *dtauangle(j,2,3,nres+1)
3415         enddo
3416        endif
3417 !   The side-chain vector derivatives
3418       return
3419       end subroutine int_to_cart
3420 #if !defined(WHAM_RUN) && !defined(CLUSTER)
3421 !-----------------------------------------------------------------------------
3422 ! readrtns_CSA.F
3423 !-----------------------------------------------------------------------------
3424       subroutine gen_dist_constr
3425 ! Generate CA distance constraints.
3426 !      implicit real*8 (a-h,o-z)
3427 !      include 'DIMENSIONS'
3428 !      include 'COMMON.IOUNITS'
3429 !      include 'COMMON.GEO'
3430 !      include 'COMMON.VAR'
3431 !      include 'COMMON.INTERACT'
3432 !      include 'COMMON.LOCAL'
3433 !      include 'COMMON.NAMES'
3434 !      include 'COMMON.CHAIN'
3435 !      include 'COMMON.FFIELD'
3436 !      include 'COMMON.SBRIDGE'
3437 !      include 'COMMON.HEADER'
3438 !      include 'COMMON.CONTROL'
3439 !      include 'COMMON.DBASE'
3440 !      include 'COMMON.THREAD'
3441 !      include 'COMMON.TIME1'
3442 !      integer :: itype_pdb !(maxres)
3443 !      common /pizda/ itype_pdb(nres)
3444       character(len=2) :: iden
3445 !el local variables
3446       integer :: i,j
3447 !d      print *,'gen_dist_constr: nnt=',nnt,' nct=',nct
3448 !d      write (2,*) 'gen_dist_constr: nnt=',nnt,' nct=',nct,
3449 !d     & ' nstart_sup',nstart_sup,' nstart_seq',nstart_seq,
3450 !d     & ' nsup',nsup
3451       do i=nstart_sup,nstart_sup+nsup-1
3452 !d      write (2,*) 'i',i,' seq ',restyp(itype(i+nstart_seq-nstart_sup)),
3453 !d     &    ' seq_pdb', restyp(itype_pdb(i))
3454         do j=i+2,nstart_sup+nsup-1
3455           nhpb=nhpb+1
3456           ihpb(nhpb)=i+nstart_seq-nstart_sup
3457           jhpb(nhpb)=j+nstart_seq-nstart_sup
3458           forcon(nhpb)=weidis
3459           dhpb(nhpb)=dist(i,j)
3460         enddo
3461       enddo 
3462 !d      write (iout,'(a)') 'Distance constraints:' 
3463 !d      do i=nss+1,nhpb
3464 !d        ii=ihpb(i)
3465 !d        jj=jhpb(i)
3466 !d        iden='CA'
3467 !d        if (ii.gt.nres) then
3468 !d          iden='SC'
3469 !d          ii=ii-nres
3470 !d          jj=jj-nres
3471 !d        endif
3472 !d        write (iout,'(a,1x,a,i4,3x,a,1x,a,i4,2f10.3)') 
3473 !d     &  restyp(itype(ii)),iden,ii,restyp(itype(jj)),iden,jj,
3474 !d     &  dhpb(i),forcon(i)
3475 !d      enddo
3476 !      deallocate(itype_pdb)
3477
3478       return
3479       end subroutine gen_dist_constr
3480 #endif
3481 !-----------------------------------------------------------------------------
3482 ! cartprint.f
3483 !-----------------------------------------------------------------------------
3484       subroutine cartprint
3485
3486       use geometry_data, only: c
3487       use energy_data, only: itype
3488 !      implicit real*8 (a-h,o-z)
3489 !      include 'DIMENSIONS'
3490 !      include 'COMMON.CHAIN'
3491 !      include 'COMMON.INTERACT'
3492 !      include 'COMMON.NAMES'
3493 !      include 'COMMON.IOUNITS'
3494       integer :: i
3495
3496       write (iout,100)
3497       do i=1,nres
3498         write (iout,110) restyp(itype(i)),i,c(1,i),c(2,i),&
3499           c(3,i),c(1,nres+i),c(2,nres+i),c(3,nres+i)
3500       enddo
3501   100 format (//'              alpha-carbon coordinates       ',&
3502                 '     centroid coordinates'/ &
3503                 '       ', 6X,'X',11X,'Y',11X,'Z',&
3504                                 10X,'X',11X,'Y',11X,'Z')
3505   110 format (a,'(',i3,')',6f12.5)
3506       return
3507       end subroutine cartprint
3508 !-----------------------------------------------------------------------------
3509 !-----------------------------------------------------------------------------
3510       subroutine alloc_geo_arrays
3511 !EL Allocation of tables used by module energy
3512
3513       integer :: i,j,nres2
3514       nres2=2*nres
3515 ! commom.bounds
3516 !      common /bounds/
3517       allocate(phibound(2,nres+2)) !(2,maxres)
3518 !----------------------
3519 ! commom.chain
3520 !      common /chain/ in molread
3521 !      real(kind=8),dimension(:,:),allocatable :: c !(3,maxres2+2)
3522 !      real(kind=8),dimension(:,:),allocatable :: dc
3523       allocate(dc_old(3,0:nres2))
3524 !      if(.not.allocated(dc_norm2)) allocate(dc_norm2(3,0:nres2+2)) !(3,0:maxres2)      
3525       if(.not.allocated(dc_norm2)) then
3526         allocate(dc_norm2(3,0:nres2+2)) !(3,0:maxres2)
3527         dc_norm2(:,:)=0.d0
3528       endif
3529 !
3530 !el      if(.not.allocated(dc_norm)) 
3531 !elwrite(iout,*) "jestem w alloc geo 1"
3532       if(.not.allocated(dc_norm)) then
3533         allocate(dc_norm(3,0:nres2+2)) !(3,0:maxres2)
3534         dc_norm(:,:)=0.d0
3535       endif
3536 !elwrite(iout,*) "jestem w alloc geo 1"
3537       allocate(xloc(3,nres),xrot(3,nres))
3538 !elwrite(iout,*) "jestem w alloc geo 1"
3539       xloc(:,:)=0.0D0
3540 !elwrite(iout,*) "jestem w alloc geo 1"
3541       allocate(dc_work(6*nres)) !(MAXRES6) maxres6=6*maxres
3542 !      common /rotmat/
3543       allocate(t(3,3,nres),r(3,3,nres))
3544       allocate(prod(3,3,nres),rt(3,3,nres)) !(3,3,maxres)
3545 !      common /refstruct/
3546       if(.not.allocated(cref)) allocate(cref(3,nres2+2,maxperm)) !(3,maxres2+2,maxperm)
3547 !elwrite(iout,*) "jestem w alloc geo 2"
3548       allocate(crefjlee(3,nres2+2)) !(3,maxres2+2)
3549       if(.not.allocated(chain_rep)) allocate(chain_rep(3,nres2+2,maxsym)) !(3,maxres2+2,maxsym)
3550       if(.not.allocated(tabperm)) allocate(tabperm(maxperm,maxsym)) !(maxperm,maxsym)
3551 !      common /from_zscore/ in module.compare
3552 !----------------------
3553 ! common.local
3554 ! Inverses of the actual virtual bond lengths
3555 !      common /invlen/ in io_conf: molread or readpdb
3556 !      real(kind=8),dimension(:),allocatable :: vbld_inv !(maxres2)
3557 !----------------------
3558 ! common.var
3559 ! Store the geometric variables in the following COMMON block.
3560 !      common /var/ in readpdb or ...
3561       if(.not.allocated(theta)) allocate(theta(nres+2))
3562       if(.not.allocated(phi)) allocate(phi(nres+2))
3563       if(.not.allocated(alph)) allocate(alph(nres+2))
3564       if(.not.allocated(omeg)) allocate(omeg(nres+2))
3565       if(.not.allocated(thetaref)) allocate(thetaref(nres+2))
3566       if(.not.allocated(phiref)) allocate(phiref(nres+2))
3567       if(.not.allocated(costtab)) allocate(costtab(nres))
3568       if(.not.allocated(sinttab)) allocate(sinttab(nres))
3569       if(.not.allocated(cost2tab)) allocate(cost2tab(nres))
3570       if(.not.allocated(sint2tab)) allocate(sint2tab(nres))
3571 !      real(kind=8),dimension(:),allocatable :: vbld !(2*maxres) in io_conf: molread or readpdb
3572       allocate(omicron(2,nres+2)) !(2,maxres)
3573       allocate(tauangle(3,nres+2)) !(3,maxres)
3574 !elwrite(iout,*) "jestem w alloc geo 3"
3575       if(.not.allocated(xxtab)) allocate(xxtab(nres))
3576       if(.not.allocated(yytab)) allocate(yytab(nres))
3577       if(.not.allocated(zztab)) allocate(zztab(nres)) !(maxres)
3578       if(.not.allocated(xxref)) allocate(xxref(nres))
3579       if(.not.allocated(yyref)) allocate(yyref(nres))
3580       if(.not.allocated(zzref)) allocate(zzref(nres)) !(maxres) 
3581       allocate(ialph(nres,2)) !(maxres,2)
3582       ialph(:,1)=0
3583       ialph(:,2)=0
3584       allocate(ivar(4*nres2)) !(4*maxres2)
3585
3586 #if defined(WHAM_RUN) || defined(CLUSTER)
3587       allocate(vbld(2*nres))
3588       vbld(:)=0.d0
3589       allocate(vbld_inv(2*nres))
3590       vbld_inv(:)=0.d0
3591 #endif
3592
3593       return
3594       end subroutine alloc_geo_arrays
3595 !-----------------------------------------------------------------------------
3596 !-----------------------------------------------------------------------------
3597       end module geometry