SUBROUTINE TestCart(SetCart)

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !
  ! This subroutines looks at the displacements for atoms between
  ! images. If the total displacement is less than the atomic radius
  ! then it might be a good idea to treat this atom as CART.
  !
  ! Input:
  !        SetCart : if True then Cart list is set by TestCart
  !              (Normal use : call TestCart with AutoCart as the argument)  
  !
  ! Output:
  !        None
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!----------------------------------------------------------------------
!  Copyright 2003-2014 Ecole Normale Supérieure de Lyon, 
!  Centre National de la Recherche Scientifique,
!  Université Claude Bernard Lyon 1. All rights reserved.
!
!  This work is registered with the Agency for the Protection of Programs 
!  as IDDN.FR.001.100009.000.S.P.2014.000.30625
!
!  Authors: P. Fleurat-Lessard, P. Dayal
!  Contact: optnpath@gmail.com
!
! This file is part of "Opt'n Path".
!
!  "Opt'n Path" is free software: you can redistribute it and/or modify
!  it under the terms of the GNU Affero General Public License as
!  published by the Free Software Foundation, either version 3 of the License,
!  or (at your option) any later version.
!
!  "Opt'n Path" is distributed in the hope that it will be useful,
!  but WITHOUT ANY WARRANTY; without even the implied warranty of
!
!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
!  GNU Affero General Public License for more details.
!
!  You should have received a copy of the GNU Affero General Public License
!  along with "Opt'n Path". If not, see <http://www.gnu.org/licenses/>.
!
! Contact The Office of Technology Licensing, valorisation@ens-lyon.fr,
! for commercial licensing opportunities.
!----------------------------------------------------------------------

  Use Path_module
  Use Io_module

  IMPLICIT NONE


  LOGICAL, INTENT(IN) :: SetCart

  INTEGER(KINT) :: I, J, K, Iat

  REAL(KREAL) :: d
  LOGICAL, ALLOCATABLE :: TobeCart(:) ! Nat
  LOGICAL :: Debug

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

  debug=valid('testcart')
  if (debug) WRITE(*,*) "========================= Entering TestCart ==========="

  ALLOCATE(TobeCart(Nat))

  ToBeCart=.TRUE.

  ! We put first geometry as the reference
  DO I=1,NGeomI
     DO J=I+1,NGeomI
        DO K=1,Nat
           IF (ToBeCart(K)) THEN
                Iat=Atome(K)
              d=100.*sqrt((XyzGeomI(J,1,K)-XyzGeomI(I,1,K))**2+ &
                   (XyzGeomI(J,2,K)-XyzGeomI(I,2,K))**2+ &
                   (XyzGeomI(J,3,K)-XyzGeomI(I,3,K))**2) 
              if (debug) WRITe(*,'(4(1X,I5),10(1X,F12.8))') k,i,j,iat,d,r_cov(iat),r_cov(iat)*fact,d/r_cov(iat)
              if ((d/r_cov(iat)).GE.0.5d0) ToBeCart(K)=.FALSE.
           END IF
        END DO
     END DO
  END DO

  If (Debug) THEN
     WRITE(*,*) "Atoms that might be described in CART"
     DO K=1,Nat
        IF (ToBeCart(K)) WRITE(*,'(I5)',ADVANCE="NO") K
     END DO
  END IF

  IF (SetCart) THEN
     WRITE(*,*) "AutoCART=T: Atoms that will be described in CART"
     K=1
     DO I=1,Nat
        If(ToBeCart(I)) THEN
           Cart(K)=I
           K=K+1
           WRITE(*,'(I5)',ADVANCE="NO") I
        END IF
     END DO
     Cart(K)=0
     Ncart=K-1
  END IF

  deallocate(tobecart)

if (debug) WRITE(*,*) "========================= Exiting TestCart ==========="
END SUBROUTINE TestCart


 


   
