        PROGRAM A22cgGS1
C CCC for ALA potential  CCCCCCCCCCCC
C*********************************************************
C   for Alanine-dipeptide 
C    --  energy + gradient from Gaussian03 
c  cccccccccccccccccccccccccccccccccccccccccccccc
C W.Quapp  09.06.2006 NEU for RGF=Newton Trajectory    c
C GS nach: Baron Peters et al. JCP 120 (2004)7877-7886 c 
C     and: WQ, JCP 122 (2005) 174106 
c script version - modular structure of program        c
cccccccccccccccccccccccccccccccccccccccccccccccccccccccc
C    eps:  tolerance
C    L :   chain length, maximum 52 
C          are to defined by the user
C          nodes: L-2 
C    N :   dimension of the problem
C          here N=66 given by the problem  
C    Y(LA,N) are the chain nodes, LA=1,..,L
cccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c  NDIM, L, und NDIM1 in cgGS3 are to put in every program: 1,2,3,5 
      Integer NDIM,L
      PARAMETER(NDIM=66,L=32)
      REAL*8  Y(L,NDIM),Yb(NDIM),Ye(NDIM),DiStart
      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) 
      Logical DiLog
c 
      Data CharAtoms/'h','c','h','c','h','o','n','c','h','c','h','c',
     1               'o','n','c','h','h','h','h','h','h','h'/
      Data Natoms/1,6,1,6,1,8,7,6,1,6,1,6,8,7,6,1,1,1,1,1,1,1/
cc
c neusortiert:
c      Data CharAtoms/'C','C','N','C','C','N','C','O','C','O','H','H',
c     1               'H','H','H','H','H','H','H','H','H','H'/
c      Data  Natoms/6,6,7,6,6,7,6,8,6,8,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='ala22start.weg')        
cccccccccccccccccccccccccccccccccccccccc
c 8,81: minima input  min1, min2 
cccccccccccccccccccccccccccccccccccccccccc  
         OPEN(8,FILE='startWEG.dat')
         OPEN(81,FILE='endeWEG.dat')
cccc z-mat coordinates ccccccccccccccccccc
        OPEN(83,FILE='startWEG.zmat')
        OPEN(84,FILE='endeWEG.zmat')
        OPEN(85,FILE='ala22current.zmat')
        OPEN(89,FILE='ala22chainint.zmat')
cccccccccccccccccccccccccccccccccccccccc                                        
c 9: all points of chain
       OPEN(9,FILE='ala22chain.dat') 
       OPEN(11,FILE='ala22param.dat') 
c 13 actual point X of GS iteration
       OPEN(13,FILE='ala22point.dat') 
c       OPEN(14,FILE='ala22ener.dat') 
c       OPEN(15,FILE='ala22grad.dat')
        OPEN(21,FILE='ala22iflag.dat')
        OPEN(22,FILE='ala22cgplFi.dat')
c for gaussian data: 
       OPEN(33,FILE='ala22poig.dat')
       OPEN(35,FILE='ala22Distart.dat')
c for molden file:
       OPEN(38,FILE='ala22chain.mol',status='unknown',access='append')
       OPEN(44,FILE='ala22proto.txt',status='unknown',access='append')
C Constants           
        LA=1
        N=NDIM
        N3=N/3
        N6=N-6
        eps=0.9D-2
        ITall=0
c        If(ITall.gt.0) goto 1111
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c        WRITE(6,*) 'give eps !! now ' 
c        Read(5,*) eps
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      rewind 21
      write(21,*) '0 '
      rewind 22
      write(22,*) ' F  0   0 '
      PRINT*,'GrowingString from C7ax to LDsp for 22 atoms ' 
      write(44,*)' GrowingString from C7ax to LDsp for 22 atoms ALA'
      write(44,*)' Nodes L = ' ,L,'  threshold eps = ',eps 
c  Start - point : use z-matrix coordinates *.zmat
      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 (135^o may be changed still)
      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 first "circle" between 2 Minima by 'linear' combination
c                                    of angles, and so on... 
      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 ccc current.zmat for first predictor
      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)
cc 
 22   FORMAT(3H { , 21(F13.9, 3H , ), F13.9, 3H }, )
 21   FORMAT(3H { , 21(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
C
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
CCC      Second part, Cartesian coordinates *.dat
ccc      may be not used if z-mat predictor is used
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
         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   up to now Node 0, LA=1 of start point:
      LA=1
      rewind 13 
      WRITE(13,50)(Y(LA,I),I=1,N)
cc
      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,*) '  ' 
      WRITE(6,*)  L, LA,  EPS, ITall, N 
      WRITE(44,*) L, LA,  EPS, ITall, N
cc
ccc  LA=2 is to put if next predictor step is not necessary
ccc  only conversation of current.zmat into gaussian input 
ccc  outside in molden4.4, and SymtoNum.f and cmout
ccc      LA=2
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
C     For step normalization in the corrector
      Distart=0.0d0
      Do 717, I=1,N
717   Distart=Distart+(Y(2,I)-Y(1,I))**2
      Distart=Dsqrt(Distart)
      DiLog=True
      rewind 35
      Write(35,*) Distart, DiLog
      Write(44,*)'Distance of Predictor Point  ', Distart
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      rewind 11
      I=1
      J=0
      WRITE(6,*)  L, LA,  EPS, I, J
      WRITE(11,*) L, LA,  EPS, I, J
      WRITE(44,*) L, LA,  EPS, I, J
      WRITE(44,*) ' Start ENDE  '
      close(44)
      WRITE(6,*)
      WRITE(6,*) ' Startprogramm A22cgGS1: ENDE, compute first point'
      Stop
      End 
