! This subroutine checks that the required step fulfills some conditions:
! v1.0
! Only valence angles are checked to remain in to 0:Pi range.

SUBROUTINE Check_step(IGeom,Coord,Nat,NCoord,GeomOld,Step)

  Use Path_module, only : pi,ncart

  IMPLICIT NONE


  INTEGER, PARAMETER :: KINT=KIND(1)
  INTEGER, PARAMETER :: KREAL=KIND(1.D0)

  INTEGER(KINT), INTENT(IN) :: Nat,NCoord,IGeom
  CHARACTER(32), INTENT(IN) :: Coord
  REAL(KREAL), INTENT(INOUT) :: Step(NCoord)
  REAL(KREAL), INTENT(IN) :: GeomOld(NCoord)

  ! As this subroutine is here for debugging, debug=T !
  LOGICAL, PARAMETER :: debug=.TRUE.
  LOGICAL :: Fini

  ! Variables
  INTEGER(KINT) :: IBeg, Idx,Iat
  REAL(KREAL) ::  Fact

  ! Parameters
  REAL(KREAL),PARAMETER :: eps=1e-8

  INTERFACE
     function valid(string) result (isValid)
       CHARACTER(*), intent(in) :: string
       logical                  :: isValid
     END function VALID
  END INTERFACE

  if (.NOT.Valid("CheckStep")) RETURN

  if (nat.LE.2) THEN
     if (debug) WRITE(*,*) "Check-Step useless for 2 or less atoms !"
     return
  END IF

  if (debug) WRITE(*,*) "=========================== Entering Check_Step ===================="

  Fact=1.d0

  SELECT CASE(COORD)
  CASE ('ZMAT')
     Idx=3
     IF (STEP(Idx).GT.Eps) THEN
        If (GeomOld(Idx)+Step(Idx).GE.Pi) THEN
           Fact=min(Fact,0.999d0*(Pi-GeomOld(Idx))/Step(Idx))
           WRITE(*,*) "WARNING: Step reduced to prevent valence angle >=180. Idx=",Idx
        END IF
        If (GeomOld(Idx)+Step(Idx).LE.0) THEN
           Fact=min(Fact,abs(0.999d0*GeomOld(Idx)/Step(Idx)))
           WRITE(*,*) "WARNING: Step reduced to prevent valence angle <=0. Idx=",Idx
        END IF
     END IF
     If (Nat.GT.3) THEN
        Idx=5
        IF (STEP(Idx).GE.Eps) THEN
           If (GeomOld(Idx)+Step(Idx).GE.Pi) THEN
              Fact=min(Fact,0.999d0*(Pi-GeomOld(Idx))/Step(Idx))
              WRITE(*,*) "WARNING: Step reduced to prevent valence angle >=180. Idx=",Idx
           END IF
           If (GeomOld(Idx)+Step(Idx).LE.0) THEN
              Fact=min(Fact,abs(0.999d0*GeomOld(Idx)/Step(Idx)))
              WRITE(*,*) "WARNING: Step reduced to prevent valence angle <=0. Idx=",Idx
           END IF
        END IF
        DO Iat=5,Nat
           Idx=Idx+3
           IF (STEP(Idx).GE.Eps) THEN
              If (GeomOld(Idx)+Step(Idx).GE.Pi) THEN
                 Fact=min(Fact,0.999d0*(Pi-GeomOld(Idx))/Step(Idx))
                 WRITE(*,*) "WARNING: Step reduced to prevent valence angle >=180. Idx=",Idx
              END IF
              If (GeomOld(Idx)+Step(Idx).LE.0) THEN
                 Fact=min(Fact,abs(0.999d0*GeomOld(Idx)/Step(Idx)))
                 WRITE(*,*) "WARNING: Step reduced to prevent valence angle <=0. Idx=",Idx      
              END IF
           END IF
        END DO
     END IF

  CASE ('MIXED')
     SELECT CASE (NCart)
     CASE (1:2)
        Idx=3
        IF (STEP(Idx).GE.Eps) THEN
           If (GeomOld(Idx)+Step(Idx).GE.Pi) THEN
              Fact=min(Fact,0.999d0*(Pi-GeomOld(Idx))/Step(Idx))
              WRITE(*,*) "WARNING: Step reduced to prevent valence angle >=180. Idx=",Idx
           END IF
           If (GeomOld(Idx)+Step(Idx).LE.0) THEN
              Fact=min(Fact,abs(0.999d0*GeomOld(Idx)/Step(Idx)))
              WRITE(*,*) "WARNING: Step reduced to prevent valence angle <=0. Idx=",Idx
           END IF
        END IF
        IF (Nat.GT.3) THEN
           Idx=5
           IF (STEP(Idx).GE.Eps) THEN
              If (GeomOld(Idx)+Step(Idx).GE.Pi) THEN
                 Fact=min(Fact,0.999d0*(Pi-GeomOld(Idx))/Step(Idx))
                 WRITE(*,*) "WARNING: Step reduced to prevent valence angle >=180. Idx=",Idx
              END IF
              If (GeomOld(Idx)+Step(Idx).LE.0) THEN
                 Fact=min(Fact,abs(0.999d0*GeomOld(Idx)/Step(Idx)))
                 WRITE(*,*) "WARNING: Step reduced to prevent valence angle <=0. Idx=",Idx
              END IF
           END IF
        END IF
        IBeg=5
     CASE (3:)
        IBeg=NCart+1
        Idx=3*Ncart+2

     END SELECT
     DO Iat=5,Nat
        IF (STEP(Idx).GE.Eps) THEN
           If (GeomOld(Idx)+Step(Idx).GE.Pi) THEN
              Fact=min(Fact,0.999d0*(Pi-GeomOld(Idx))/Step(Idx))
              WRITE(*,*) "WARNING: Step reduced to prevent valence angle >=180. Idx=",Idx
           END IF
           If (GeomOld(Idx)+Step(Idx).LE.0) THEN
              Fact=min(Fact,abs(0.999d0*GeomOld(Idx)/Step(Idx)))
              WRITE(*,*) "WARNING: Step reduced to prevent valence angle <=0. Idx=",Idx
           END IF
        END IF
        Idx=Idx+3
     END DO

  CASE ('CART','HYBRID','BAKER')
     if (debug) WRITE(*,*) "DBG Check_step:  Check useless for COORD=",TRIM(COORD)
  CASE DEFAULT
     WRITE(*,*) "COORD=",TRIM(COORD)," Not recognized in Check_step"
     STOP
  END SELECT

  If (debug) WRITE(*,*) "Check_Step: Fact=",Fact
  Step=Step*Fact

  if (debug) WRITE(*,*) "=========================== Exiting Check_Step ===================="

END SUBROUTINE Check_step
