MODULE Path_module
! This module contains most of the variables needed for the path

  use VarTypes

  IMPLICIT NONE

  SAVE

  INTEGER(KINT), PARAMETER :: NMaxL=21
  INTEGER(KINT), PARAMETER :: Max_Z=86
  INTEGER(KINT), PARAMETER :: MaxFroz=100
  REAL(KREAL), PARAMETER :: a0=0.529177249d0
  REAL(KREAL), PARAMETER :: Unit=1.d0/a0
  REAL(KREAL), PARAMETER :: au2kcal=627.509608d0
  REAL(KREAL) :: Pi

 ! Frozen contains the indices of frozen atoms
  INTEGER(KINT),ALLOCATABLE :: Frozen(:) !Nat
 ! Cart contains the indices of atoms described in cartesian coords
  INTEGER(KINT),ALLOCATABLE :: Cart(:) !Nat
! intFroz is the number of internal coord frozen
! NFroz the number of frozen atoms
! Ncart the number of  atoms described in cartesian coords
  INTEGER(KINT) :: IntFroz, NFroz, NCart
! FrozAtoms is true for a frozen atom, false else.
  LOGICAL, ALLOCATABLE :: FrozAtoms(:)  !! Nat
! Number of Primitives (mainly for Baker coordinates)
  INTEGER(KINT) :: NPrim
! Nat number of atoms
  INTEGER(KINT) :: Nat
! NGeomI number of initial geometries
! NgeomF number of final geometries
  INTEGER(KINT) :: NGeomI, NGeomF
! NCoord=3*Nat or NFree depending on the coordinate choice
  INTEGER(KINT) :: NCoord
  INTEGER(KINT) :: NMaxPtPath=1000
! IReparam is the period of reparameterization of the Path
  INTEGER(KINT) :: IReparam=1
! IReparamT is the period of reparameterization of the tangents along the path
! Be default IReparamT=IReparam
  INTEGER(KINT) :: IReparamT
! ISpline is the iteration number where spline interpolation starts to be used 
! instead of linear interpolation
  INTEGER(KINT) :: ISpline=1
! Linear: if TRUE, then we perform linear interpolation and not spline
  LOGICAL :: Linear

! FrozTol is the criterion to decide wether a frozen atom has moved between
! two initial geometries
  REAL(KREAL) :: FrozTol=1e-4

! BoxTol is the criterion to decide wether an atom has crossed the box between
! two initial geometries
  REAL(KREAL) :: BoxTol=0.5


  LOGICAL :: Freq, MW, Bohr, Renum, Hinv
! OptReac: if TRUE the reactants will be optimized. Default is FALSE
  LOGICAL :: OptReac
! OptProd: if TRUE the products will be optimized. Default is FALSE
  LOGICAL :: OptProd
! PathOnly: if TRUE, only the initial path will be constructed.
! Default is FALSE, that is OpenPath construct the path and optimize it
  LOGICAL :: PathOnly
! CalcEReac: if TRUE the reactants energy will be computed. Default is FALSE
! Not compatible with RunMode=Para
  LOGICAL :: CalcEReac
! CalEProd: if TRUE the products energy will be computed. Default is FALSE
! Not compatible with RunMode=Para
  LOGICAL :: CalcEProd
  LOGICAL :: IniHup,HupNeighbour

  REAL(KREAL) :: Fact,FTan
! IGeomRef is the index of the reference geometry, used to calculate the Zmat
! or the Baker coordinates.
  INTEGER(KINT) :: IGeomRef=-1
! For geometry optimization , OptGeom is the index of the starting geometry
  INTEGER(KINT) :: OptGeom=-1

! Optimization parameter
  INTEGER(KINT) :: MaxCyc
  REAL(KREAL) :: SThresh, Smax, SthreshRms
  REAL(KREAL) :: GThresh, GThreshRMS

! Number of steps in memory for Step_RFO_all
  INTEGER(KINT) :: NGintMax = 10

  CHARACTER(SCHARS) :: Coord='HYBRID'
  CHARACTER(SCHARS) :: Step_method='RFO'
  CHARACTER(SCHARS) :: Prog="GAUSSIAN"
  CHARACTER(VLCHARS) :: ProgExe="g03"

! PFL 06/2011: HesUpd is deprecated
! I replace it by HUpdate that is more intuitive to me
  CHARACTER(SCHARS) :: hesupd="BFGS"
! HUpdate indicates which method to use for updating the Hessian or its inverse
  CHARACTER(SCHARS) :: HUpdate="BFGS"

  REAL(KREAL), ALLOCATABLE, TARGET :: XyzGeomI(:,:,:) ! (NGeomI,3,Nat)
  REAL(KREAL), ALLOCATABLE :: XyzGeomF(:,:,:) ! (NGeomF,3,Nat)
  REAL(KREAL), ALLOCATABLE :: XyzTangent(:,:)   ! (NGeomF,3*Nat)
  REAL(KREAL), ALLOCATABLE :: IntTangent(:,:)   ! (NGeomF,Nfree=3Nat-6)
  REAL(KREAL), ALLOCATABLE :: IntCoordI(:,:) ! (NGeomI,3*Nat-6)
  ! IntCoordF: Final Internal coordinates for number of final geometries.
  ! 3*Nat-6 is the number of coordinates (NCoord) of each geometry. 
  REAL(KREAL), ALLOCATABLE :: IntCoordF(:,:) ! (NGeomF,3*Nat-6)
  REAL(KREAL), ALLOCATABLE :: Vfree(:,:) ! (Ncoord,Ncoord)
  REAL(KREAL), ALLOCATABLE :: SGeom(:) ! (NGeomF)
  REAL(KREAL), ALLOCATABLE :: Energies(:) ! NGeomF
! NCoord=3*Nat or NFree depending on the coordinate choice
  REAL(KREAL), ALLOCATABLE :: Grad(:,:) ! (NGeomF,NCoord)
  REAL(KREAL), ALLOCATABLE :: Hess(:,:,:)  !(NgeomF, N,N)
  REAL(KREAL), ALLOCATABLE :: DzDc(:,:,:,:) ! (3,Nat,3,Nat)
  REAL(KREAL), ALLOCATABLE :: BMat_BakerT(:,:) ! (3*Nat,NCoord)
  INTEGER(KINT), ALLOCATABLE :: IndZmat(:,:)   ! (Nat,5)
  INTEGER(KINT), ALLOCATABLE :: Order(:),OrderInv(:) ! Nat
  CHARACTER(10), ALLOCATABLE :: AtName(:) ! Nat
  INTEGER(KINT), ALLOCATABLE :: Atome(:) ! Nat
  REAL(KREAL), ALLOCATABLE :: MassAt(:)   ! Nat
  REAL(KREAL), ALLOCATABLE :: BTransInv(:,:) ! BTransInv (3*Nat-6,3*Nat), used for Baker case.
  REAL(KREAL), ALLOCATABLE :: BTransInvF(:,:,:) ! BTransInvF (NGeomF,3*Nat-6,3*Nat)
  ! BTransInv_local (3*Nat-6,3*Nat), used for Baker case in Opt_Geom.f90
  REAL(KREAL), ALLOCATABLE :: BTransInv_local(:,:)
  REAL(KREAL), ALLOCATABLE :: UMat(:,:) !(NPrim,3*Nat-6)
  REAL(KREAL), ALLOCATABLE :: UMat_local(:,:) !(NPrim,3*Nat-6)
  REAL(KREAL), ALLOCATABLE :: UMatF(:,:,:) !(NGeomF,NPrim,3*Nat-6)
  !REAL(KREAL), ALLOCATABLE :: IntCoordIBaker(:,:) ! (NGeomI,NCoord=3*Nat-6)
  ! IntCoordIBaker allocated in PathCreate.f90
  REAL(KREAL), ALLOCATABLE :: Xprimitive(:,:) ! Xprimitive(NgeomI,NPrim)
  REAL(KREAL), ALLOCATABLE :: Xprimitive_t(:) ! used in ConvertBakerInternal_cart.f as a temporary variable.
  REAL(KREAL), ALLOCATABLE :: XprimitiveF(:,:) ! Xprimitive(NgeomF,NPrim)
  REAL(KREAL), ALLOCATABLE :: GeomOld_all(:,:)
  REAL(KREAL), ALLOCATABLE :: GeomOld(:) ! Used in DIIS step method.
  
  LOGICAL, PARAMETER :: FRot=.TRUE., FAlign=.TRUE.
  REAL(KREAL), ALLOCATABLE :: BprimT(:,:) !(3*Nat,NPrim). This is B^prim.
  REAL(KREAL), ALLOCATABLE :: BBT(:,:) ! BBT (3*Nat-6,3*Nat-6), used for Baker case
  REAL(KREAL), ALLOCATABLE :: BBT_inv(:,:) ! BBT_inv (3*Nat-6,3*Nat-6), used for Baker case
  INTEGER(KINT) :: Symmetry_elimination
  LOGICAL :: FirstTimePathCreate
  
  Type (ListCoord), POINTER :: Coordinate, CurrentCoord 
  Type (ListCoord), POINTER :: ScanCoord

! Triggers the dynamical update of the maximum step
  LOGICAL :: DynMaxStep

! Triggers the alignment of not
  LOGICAL :: Align



!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Flags for printing
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

! Vmd is set to TRUE if user wants to use VMD to look
! at the Path.
! For now, used only for VASP

  LOGICAL :: Vmd


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Variables for VASP input/output
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  
  REAL(KREAL) :: lat_a(3), lat_b(3), lat_c(3)
  REAL(KREAL), ALLOCATABLE :: X0_vasp(:),Y0_vasp(:), Z0_vasp(:) ! nat
  REAL(KREAL) :: Latr(3,3)
  CHARACTER(LCHARS) :: V_direct
! AutoCart : true if user let PATH determines which atoms should be
! described in cartesian when COORD=MIXED
  LOGICAL :: AutoCart


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! General
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  CHARACTER(2) :: Nom(0:max_Z)=(/ ' X',' H',                   'HE',  &
                'LI','BE',            ' B',' C',' N',' O',' F','NE',  &
                'NA','MG',            'AL','SI',' P',' S','CL','AR',  &
                ' K','CA',                                            &
            'SC','TI',' V','CR','MN','FE','CO','NI','CU','ZN',        &
                                      'GA','GE','AS','SE','BR','KR',  &
                'RB','SR',                                            &
            ' Y','ZR','NB','MO','TC','RU','RH','PD','AG','CD',        &
                                      'IN','SN','SB','TE',' I','XE',  &
                'CS','BA',                                            &
            'LA',                                                     &
              'CE','PR','ND','PM','SM','EU','GD','TB','DY','HO',      &
              'ER','TM','YB','LU',                                    &
                     'HF','TA',' W','RE','OS','IR','PT','AU','HG',    &
                                      'TL','PB','BI','PO','AT','RN'/)

! This is the new table, more complete, taken from a periodic table
! some value have been changed to be consistent
  REAL(KREAL) ::  r_cov(0:max_Z)=(/ 1e-13, 37.,                93.,     &
!     2           'LI','BE',          ' B',' C',' N',' O',' F','NE',
                  123.,90.,            82., 77., 75., 73., 71., 69.,     &
!     3           'NA','MG',          'AL','SI',' P',' S','CL','AR',
                  154.,136.,          118.,111.,106.,102., 99., 97.,     &
!     4           ' K','CA',
                  203.,  174.,                                           &
!     4       'SC','TI',' V','CR','MN','FE','CO','NI','CU','ZN',
             144.,132.,122.,118.,117.,117.,116.,115.,117.,125.,          &
!     4                               'GA','GE','AS','SE','BR','KR',
                                     120.,122.,122.,117.,114.,110.,      &
!     5           'RB','SR',
                 216.,191.,                                              &
!     5       ' Y','ZR','NB','MO','TC','RU','RH','PD','AG','CD', 
              162.,145.,134.,130.,127.,125.,125.,128.,134.,148.,         &
!     5                               'IN','SN','SB','TE',' I','XE',
                                     144.,140.,143.,135.,133.,130.,      &
!     6           'CS','BA',
                 235., 198.,                                             &
!     6       'LA',
!               'CE','PR','ND','PM','SM','EU','GD','TB','DY','HO',      &
!               'ER','TM','YB','LU',                                    &
             169.,                                                       &  
             165., 165., 164., 163., 162., 185., 161., 159., 159., 158., &
             157., 156., 184.,156.,                                      &
!     6                'HF','TA',' W','RE','OS','IR','PT','AU','HG',
!     6                                 'TL','PB','BI','PO','AT','RN'/
             144., 134., 130., 126., 126., 127., 120., 134., 149.,       &
                        148., 147., 146., 146., 148., 151. /)

  REAL(KREAL) :: Mass(0:Max_Z)=(/0.0D0,1.0078D0,          4.0026D0,      &
                  7.0160D0, 9.0122D0,11.0093D0,                          &
                 12.0000D0,14.0031D0,15.9949D0,18.9984D0,19.9924D0,      &
                 22.9898D0,23.9850D0,26.9815D0,                          &
                 27.9769D0,30.9738D0,31.9721D0,34.9688D0,39.9624D0,      &
                 39.0983D0,40.08D0,                                      &
                   44.9559D0, 47.88D0, 50.9415D0, 51.996D0, 54.9380D0,   &
                   55.847D0, 58.9332D0, 58.69D0, 63.546D0, 65.39D0,      &
                 69.72D0,72.59D0,74.9216D0,78.96D0,79.904D0,83.80D0,     &
                 85.4678D0,87.62D0,88.9059D0,91.224D0,92.9064D0,         &
         95.94D0,98D0,101.07D0,102.906D0,106.42D0,107.868D0,112.41D0,    &
         114.82D0,118.71D0,121.75D0,127.60D0,126.905D0,131.29D0,         &
!     6           'CS','BA',
                 132.905D0,137.34D0,                                      &
!     6       'LA',
!               'CE','PR','ND','PM','SM','EU','GD',
!               'TB','DY','HO', 'ER','TM','YB','LU',   
            138.91D0,                                                      &
            140.12D0, 130.91D0, 144.24D0,147.D0,150.35D0, 151.96D0,157.25D0,  &
          158.924D0, 162.50D0, 164.93D0, 167.26D0,168.93D0,173.04D0,174.97D0, &
!     6                'HF','TA',' W','RE','OS','IR','PT',
!                      'AU','HG',
!     6                                 'TL','PB','BI','PO','AT','RN'/
         178.49D0, 180.95D0, 183.85D0, 186.2D0, 190.2D0, 192.2D0, 195.09D0,  &
             196.97D0, 200.59D0,                                             &
             204.37D0, 207.19D0,208.98D0,210.D0,210.D0,222.D0 /)

      END MODULE Path_module
