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