        PROGRAM a162cgGS1
C*********************************************************
C   for poly- Alanine-15 
C    --  energy + gradient from Gaussian03 
c  ccccccccccccccccccccccccccccccccccccccccccccccccccccc 
C W.Quapp 16.03.2005/07.07.2006 for RGF=Newton Trajectory
C GS nach: Baron Peters et al. JCP 120 (2004)7877-7886 c
C    and:  WQ JCP 122 (2005) 174106                    c 
c script version for modular structure of program      c
c cccccccccccccccccccccccccccccccccccccccccccccccccccccc
c                         START                        c
c cccccccccccccccccccccccccccccccccccccccccccccccccccccc
C    eps:  tolerance
C    L :   chain length, maximum 52 
C          are to defined by the user
C          nodes: L-2,  (L=1:start, L=L:finish)
C    N :   dimension of the problem
C          here N=386 given by the problem  
C    Y(LA,N) are the chain nodes, LA=1,..,L  
cccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c  NDIM, L, und NDIM1 of cgGS1 are to put in every program:1,2,3,5 
      Integer NDIM,L
      PARAMETER(NDIM=486,L=12)
      REAL*8  Y(L,NDIM),Yb(NDIM),Ye(NDIM)
      Integer N,N3,N6,J,I,LA,ITall,Natoms(NDIM/3) 
      Character*2  CharAtoms(NDIM/3)
      Character*50 zmatLines(NDIM/3+4)
      Character*7  znames(NDIM-6) 
c 
      Data CharAtoms/
     1 'C','C','N','C','C','O','C','N','C','C','O','C','N','C','C','O',
     1 'C','N','C','C','O','C','N','C','C','O','C','N','C','C','O','C',
     1 'N','C','C','O','C','N','C','C','O','C','N','C','C','O','C','N',
     1 'C','C','O','C','N','C','C','O','C','N','C','C','O','C','N','C',
     1 'C','O','C','N','C','C','O','C','N','C','C','O','C','N','C','O',
     1 'H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H',
     1 'H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H',
     1 'H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H',
     1 'H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H',
     1 'H','H','H','H','H','H','H','H','H','H','H','H','H','H','H','H',
     1 'H','H'/
c
      Data Natoms/6,6,7,6,6,8,6,7,6,6,8,6,7,6,6,8,
     * 6,7,6,6,8,6,7,6,6,8,6,7,6,6,8,6,
     * 7,6,6,8,6,7,6,6,8,6,7,6,6,8,6,7,
     * 6,6,8,6,7,6,6,8,6,7,6,6,8,6,7,6,
     * 6,8,6,7,6,6,8,6,7,6,6,8,6,7,6,8,
     * 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
     * 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
     * 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
     * 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
     * 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
     * 1,1/ 

cccccccc
c 7: all points of chain for Mma output
       OPEN(17,FILE='a162start.weg')        
cccccccccccccccccccccccccccccccccccccccc
c 8,81, and 83,84: minima input  min1, min2 
cccc Cartesians cccccccccccccccccccccccccc  
         OPEN(8,FILE='starta162.dat')
         OPEN(81,FILE='endea162.dat')
cccc z-mat coordinates ccccccccccccccccccc
         OPEN(83,FILE='starta162.zmat')
         OPEN(84,FILE='endea162.zmat')
cccccccccccccccccccccccccccccccccccccccc
         OPEN(85,FILE='a162current.zmat')
         OPEN(89,FILE='a162chainint.zmat')
cccccccccccccccccccccccccccccccccccccccc                                        
c 9: all points of chain
        OPEN(9,FILE='a162chain.dat') 
        OPEN(11,FILE='a162param.dat') 
c 13 actual point X of GS iteration
        OPEN(13,FILE='a162point.dat') 
        OPEN(21,FILE='a162iflag.dat')
        OPEN(22,FILE='a162cgplFi.dat')
c for gaussian data: 
       OPEN(33,FILE='a162poig.dat')
c for molden file:
      OPEN(38,FILE='a162chain.mol',status='unknown',access='append')
      OPEN(44,FILE='a162proto.txt',status='unknown',access='append')
C Constants          
        LA=1 
        N=NDIM
        N3=N/3
        N6=N-6
        eps=2.50d-3
        ITall=0
c        WRITE(6,*) 'give eps !! here ' 
c        Read(5,*) eps 
      rewind 21
      write(21,*) ' 0 '
      rewind 22
      write(22,*) ' F  0   0 '
c
      PRINT*,' Chain from Min1 to Min2 for 162atomic polyalanine ' 
      write(44,*)' Newton trajectory by CG+program ' 
      write(44,*)' Version2006 '
      write(44,*)'                                   for: ' 
      write(44,*)' Chain from Min1 to Min2 for 162atomic polyalanine '
      write(44,*)' Nodes = ',L-2,'      threshold eps = ',eps 

c  Start - Punkt 
      rewind 83
      Do 3 I=1,N3+1
3     READ(83,833)  zmatLines(I)
833   Format(50A)
      Do 333 I=1,N6
333   READ(83,*) znames(I),Yb(I)

      Do 4 I=1,N3+1
4     READ(84,833) zmatLines(I)  
      Do 41 I=1,N6
41    READ(84,*) znames(I),Ye(I)
c   harmonize the dihedrals:
      Do 311 I=6,N6,3      
      IF( (Yb(I)*Ye(I) .lt. 0.0d0)
     1   .and.  (Dabs(Yb(I)).gt. 135.0d0)
     1   .and.  (Dabs(Ye(I)).gt. 135.0d0) )  then
      write(44,*)' dih- ERROR in Yb(',I,') is corrected' 
          if (Yb(I).gt.0.0d0 ) then
              Yb(I)=Yb(I)-360.0d0
            else
              Yb(I)=Yb(I)+360.0d0
          endif
       ENDIF
311    continue
c
c first "circle" between the Minima by angles 'linear' combination 
      Do 12 J=1,L
      Do 11 I=1,N6
11      Y(J,I)= ((L-J)*Yb(I)+(J-1)*Ye(I))/(L-1)
12    continue

cc      rewind 89
      Do 222 J=1,L
      Do 32  I=1,N3+1
32     Write(89,833)  zmatLines(I)
       WRITE(89,*)
       Do 321 I=1,N6
321    WRITE(89,*) znames(I), Y(J,I)
       WRITE(89,*)
222     continue
c current.zmat in curvilinear coordinates for first predictor point
      Do 337 I=1,N3+1
337    Write(85,833)  zmatLines(I)
       WRITE(85,*)
      Do 327 I=1,N6
327    WRITE(85,*) znames(I), Y(2,I)
c
 22   FORMAT(3H { , 161(F13.9, 3H , ), F13.9, 3H }, )
 21   FORMAT(3H { , 161(F13.9, 3H , ), F13.9, 3H }} )
 50   FORMAT(1X,3F20.10)

      WRITE(6,*)  L, LA,  EPS, ITall, N
      WRITE(44,*) L, LA,  EPS, ITall, N

ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
CCC   Second part, Cartesian coordinates *.dat
CCC   may not be used, but the zmat predictor
1111  continue
      Read(8,*) (Yb(I),I=1,N)
      write(38,*) '       ',N3
      write(38,*)
      Do 5 J=1,N3
 5    WRITE(38,555) CharAtoms(J), (Yb(3*(J-1)+I ),I=1,3)
      
      READ(81,*) (Ye(I),I=1,N)
      Do 1 J=1,L
      Do 1 I=1,N
c   first straigt chain between 2 Minima
      Y(J,I)= ((L-J)*Yb(I)+(J-1)*Ye(I))/(L-1)
 1    continue
      rewind 9
      Do 55, J=1,L
CC55     WRITE(9,50) (Y(J,I),I=1,N)
      write(9,*) '    ',N3
      write(9,*) ' '
      Do 55 JJ=1,N3
55    WRITE(9,555) CharAtoms(JJ),(Y(J,3*(JJ-1)+I),I=1,3)
      write(9,*)
      close(9)
555   Format(1X,A3, 3F16.8)
c actual node is LA, node number is LA-1
c so, min1=Y_1, min_fin=Y_L 
C LA=1 is first point: Start MIN or SP 
      LA=1
      rewind 13 
      WRITE(13,50)(Y(LA,I),I=1,N)

      WRITE(44,*) ' first point'
      rewind 33 
      Do 7 J=1,N3 
      WRITE(44,123) Natoms(J), (Y(LA,3*(J-1)+I ),I=1,3)
 7    WRITE(33,*)   Natoms(J), (Y(LA,3*(J-1)+I ),I=1,3)
123   Format(I3,3X,3F16.8)
      WRITE(33,*) '  ' 
c     goto LA2 without Predictor
c     use a162current.zmat from Start in molden, extern
      LA=2
      rewind 11
      I=1
      J=0
      WRITE(6,*)  L, LA,  EPS, ITall, N
      WRITE(44,*) L, LA,  EPS, ITall, N
      WRITE(11,*) L, LA,  EPS, I, J  
      WRITE(44,*)
      WRITE(44,*) ' Start ENDE  '
      WRITE(6,*)
      WRITE(6,*)  ' Start a162cgGS1: ENDE '
      close(44)
      Stop
      End 
