 subroutine egrad_ext(e,geomcart,gradcart)

  ! This routines calculates the energy and the gradient of 
  ! a molecule, using an external code

!----------------------------------------------------------------------
!  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, only : Nat, renum,Order,OrderInv,AtName, Coord,ProgExe
  use Io_module

  !
  IMPLICIT NONE

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

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


  logical           :: debug

  REAL(KREAL) :: Pi

  INTEGER(KINT) :: iat, i, n3at

  !
  CHARACTER(132) :: FileIn,FileOut

  CHARACTER(VLCHARS), SAVE :: RunCommand
!  LOGICAL, SAVE :: FCopyRstrt=.False., FOrderChecked=.False.



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

  LOGICAL, EXTERNAL :: valid

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


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

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

  RunCommand=Trim(Adjustl(ProgExe))
  FileIn=Trim(CalcName) // Trim(ISuffix)
  FileOut=Trim(CalcName) // Trim(OSuffix)

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

  ! we create the input file

  OPEN(IOTMP,File=FileIn)

  WRITE(IOTMP,'(1X,I10)') NAt
  WRITE(IOTMP,'(1X,A)') Coord

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

  call system(RunCommand)

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

  OPEN(IOTMP,FILE=FileOut, STATUS='old')
  ! We first search for the forces
  READ(IOTMP,*) e
  DO I=1,Nat
     Iat=I
     IF (renum) Iat=Order(I)
     READ(IOTMP,*)  GradCart(3*Iat-2:3*Iat)
  END DO

  CLOSE(IOTMP)


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

  RETURN

!999 CONTINUE
!  if (.NOT.Ftmp) WRITE(*,*) 'We should not be here !!!!'
!  STOP
  ! ======================================================================
end subroutine egrad_ext
