# CASTEP DFT single point energy

import chemsh
from chemsh       import *
from chemsh.utils import testutils
from datetime import datetime
begin=datetime.now()

si = Fragment(coords="Si2.cjson")

# default functional is 'lda'
castep_obj = CASTEP(frag=si,cut_off_energy="150 eV",elec_energy_tol="1.0e-8 eV",
grid_scale=2, functional='lda',symmetry_generate= False)

sp = SP(theory=castep_obj, gradients=True)

sp.run(dryrun=False)

ecalc = sp.result.energy[0]
gradients = sp.result.gradients

print(f"Final energy is {ecalc}")

print(f"Gradients: \n{gradients}\n")

end=datetime.now()
print(f'Total elapsed time={(end-begin)}')
# Test result from a stand-alone castep run, with the same geometry
eref = -10.537645080995
# NB 01/12/2023
# Due to variations in the generated pseudopotential (.usp file) for Si 
# from machine to machine (despite using same compilers and libaries), 
# a more relaxed tolerance has to be used for now.
testutils.validate(ecalc, eref, tol=1.0e-5)
