subroutine egrad_mopac(e,geomcart,gradcart)

  ! This routines calculates the energy and the gradient of 
  ! a molecule, using mopac 

  use Path_module, only : Nat, renum,Order,OrderInv,AtName, Coord, dzdc, indzmat,Nom,Atome, massat, unit,ProgExe, au2kcal
  use Io_module

  IMPLICIT NONE

  ! Energy (calculated if F300K=.F., else estimated)
  REAL(KREAL), INTENT (OUT) :: e
  ! Nb degree of freedom
  REAL(KREAL), INTENT (IN) :: geomcart(Nat,3)
  ! Gradient calculated at Geom geometry
  REAL(KREAL), INTENT (OUT) :: gradcart(3*NAt)

  ! ======================================================================

  character(LCHARS) :: LINE

  logical           :: debug
  LOGICAL, SAVE :: first=.true.

  REAL(KREAL), ALLOCATABLE :: GradTmp(:)

  REAL(KREAL) :: d, a_val, Pi

  REAL(KREAL) :: coef,x
  INTEGER(KINT) :: iat, jat, kat, i, j, k,n3at
  INTEGER(KINT) :: ITmp1,ITmp2, Idx
  INTEGER(KINT), PARAMETER :: IOLOG=12, IOCOM=11

  CHARACTER(132) :: FileIn, FileOut, FileChk

  CHARACTER(3) :: SepIn=' < ', SepOut=' > '
  CHARACTER(VLCHARS), SAVE ::  RunCommand

  ! ======================================================================

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

  ! ======================================================================

  Pi=dacos(-1.0d0)
  n3at=3*nat

  debug=valid('EGRAD')
  if (debug) WRITE(*,*) '================ Entering Egrad_Mopac ===================='

  if (first) THEN
     First=.FALSE.
     SepIn='   '
     SepOut='   '
  END IF
 
  gradcart=0.
  E=0.

  FileIn=Trim(CalcName) // "." // Trim(ISuffix)
  FileOut=Trim(CalcName) // "." // Trim(OSuffix)
  FileChk=Trim(CalcName) // ".arc"

  RunCommand=Trim(ProgExe) // SepIn // Trim(FileIn) 

  IF (DEBUG) WRITE(*,*)'RunCommand:',TRIM(RunCommand)

  ! we create the input file

  OPEN(IOTMP,File=FileIn)
  WRITE(IOTMP,'(A)') '* All comments are put at the begining of the file'
  Current => Mopac_comment
  DO WHILE (ASSOCIATED(Current%next))
     WRITE(IOTMP,'(A)') Trim(current%line)
     Current => current%next
  END DO

  Current => Mopac_root
  DO WHILE (ASSOCIATED(Current%next))
     WRITE(IOTMP,'(1X,A)') Trim(current%line)
     WRITE(*,'(1X,A)') Trim(current%line)
     Current => current%next
  END DO

  DO I=1,Nat
     If (renum) THEN
        Iat=Order(I)
        WRITE(IOTMP,'(1X,A10,3(1X,F15.8))') Trim(AtName(I)),GeomCart(Iat,:)
     ELSE
        Iat=OrderInv(I)
        WRITE(IOTMP,'(1X,A10,3(1X,F15.8))') Trim(AtName(Iat)),GeomCart(I,:)
     END IF
  END DO

     WRITE(IOTMP,'(1X,A)') Trim(Mopac_EndGeom)

  Current => Mopac_End
  DO WHILE (ASSOCIATED(Current%next))
     WRITE(IOTMP,'(1X,A)') Trim(current%line)
     Current => current%next
  END DO

  WRITE(IOTMP,*) 
  CLOSE(IOTMP)

  IF (debug) THEN
     WRITE(*,*) 'DBG EGRAD ' //  Trim(FileIn),' COORD=',TRIM(COORD)
     call system('cat ' // Trim(FileIn))
  END IF


  call system(RunCommand)

  if (debug) WRITE(*,*) 'DBG EGRAD, back from calculation'

  ! Whatever the coordinate system, we read the cartesian gradient
  ! and we convert it to Zmat or other if needed

  OPEN(IOTMP,FILE=FileOut, STATUS='old')
  ! We first search for the forces
  READ(IOTMP,'(A)') LINE

  DO WHILE (INDEX(LINE,'FINAL  POINT  AND  DERIVATIVES')==0)
     IF (INDEX(Line,'FINAL HEAT OF FORMATION').NE.0)  THEN
        Itmp1=Index(Line,"=")+1
        ITmp2=Index(Line,"KCAL")-1
        Read(Line(Itmp1:Itmp2),*) E
        E=E/au2kcal
     END IF
     READ(IOTMP,'(A)',END=999) LINE
  END DO

  if (debug) WRITE(*,*) "GradCart reading - renum=",Renum
  READ(IOTMP,'(A)') LINE
  READ(IOTMP,'(A)') LINE
  DO I=1,Nat
     Iat=I
     IF (renum) Iat=Order(I)
     DO J=1,3
        READ(IOTMP,'(A)') Line
! Gradient is on the 7th column
        DO K=1,6
           Line=AdjustL(Line)
           Idx=Index(Line," ")
           Line=AdjustL(Line(Idx+1:))
        END DO
        READ(Line,*) GradCart(3*Iat-3+j)
     END DO
     if (debug) WRITE(*,*) "I,Iat,GradCart:",I,Iat,GradCart(3*Iat-2:3*Iat)
  END DO

! Mopac gives the Forces in kcal/angstrom, so we convert it into the 
! gradient in ua/Angstrom 
  gradCart=gradCart/au2kcal

!  if (debug.AND.(COORD.EQ.'ZMAT')) THEN
!     DO WHILE (INDEX(LINE,'Internal Coordinate Forces')==0)
!        READ(IOTMP,'(A)') LINE
!!        WRITE(*,*) 'Pas bon:' // TRIM(LINE)
!     END DO
!     WRITE(*,*) TRIM(LINE)
!     DO I=1,Nat+2
!        READ(IOTMP,'(A)') LINE
!        WRITE(*,*) TRIM(LINE)
!     END DO
!  END IF

  CLOSE(IOTMP)

  if (debug) WRITE(*,*) '================  Egrad_Mopac Over ===================='

  RETURN

999 CONTINUE
  WRITE(*,*) 'We should not be here !!!!'
  STOP
  ! ======================================================================
end subroutine egrad_mopac
