''' CASTEP Geometry Optimisation of water molecule '''

from chemsh       import *
from chemsh.utils import testutils
from datetime import datetime 
from chemsh.data import unitconvert

begin=datetime.now()

water = Fragment(coords='water_opt.cjson')

castep_obj = CASTEP(frag=water,
functional='lda',maxiter=60,cut_off_energy='150 eV',
electronic_minimizer='sd')

opt = Opt(theory=castep_obj, algorithm='lbfgs',maxcycle=100,
trust_radius='energy')
          
opt.run()

ecalc = opt.result.energy[0]

end=datetime.now()
print(f'Total elapsed time={(end-begin)}') 
print(f'Optimised Energy:{ecalc} Hartree')
print(f'Optimised Energy:{ecalc*unitconvert.Ha2eV} eV')

# Reference from castep geom optimisation
eref = -15.261330956
testutils.validate(ecalc, eref)
