nexus: add tests for qmcpack converter simulations

This commit is contained in:
Jaron Krogel 2019-11-07 11:55:36 -05:00
parent ed831da785
commit f5d04fd90d
5 changed files with 685 additions and 64 deletions

View File

@ -1526,11 +1526,47 @@ def pwscf_postprocessor_simulations():
def qmcpack_converters():
nunit('import')
def qmcpack_converter_simulations():
nunit('pw2qmcpack_import')
nunit('pw2qmcpack_minimal_init')
nunit('pw2qmcpack_check_result')
nunit('pw2qmcpack_get_result')
nunit('pw2qmcpack_incorporate_result')
nunit('pw2qmcpack_check_sim_status')
nunit('convert4qmc_import')
nunit('convert4qmc_minimal_init')
nunit('convert4qmc_check_result')
nunit('convert4qmc_get_result')
nunit('convert4qmc_incorporate_result')
nunit('convert4qmc_check_sim_status')
nunit('pyscf_to_afqmc_import')
nunit('pyscf_to_afqmc_minimal_init')
nunit('pyscf_to_afqmc_check_result')
nunit('pyscf_to_afqmc_get_result')
nunit('pyscf_to_afqmc_incorporate_result')
nunit('pyscf_to_afqmc_check_sim_status')
nunit_all()
#end def qmcpack_converters
#end def qmcpack_converter_simulations
@ -1838,7 +1874,7 @@ NexusTest( gamess_simulation )
NexusTest( pyscf_simulation )
NexusTest( quantum_package_simulation )
NexusTest( pwscf_postprocessor_simulations )
NexusTest( qmcpack_converters )
NexusTest( qmcpack_converter_simulations )
# ordered alphabetically (pytest order)
#NexusTest( basisset )
@ -1875,7 +1911,7 @@ NexusTest( qmcpack_converters )
#NexusTest( qmcpack_analyzer )
#NexusTest( qmcpack_converter_analyzers )
#NexusTest( qmcpack_converter_input )
#NexusTest( qmcpack_converters )
#NexusTest( qmcpack_converter_simulations )
#NexusTest( qmcpack_input )
#NexusTest( quantum_package_analyzer )
#NexusTest( quantum_package_input )

View File

@ -21,7 +21,6 @@ import os
from numpy import array
from generic import obj
from physical_system import PhysicalSystem
from qmcpack_converters import Pw2qmcpack
from simulation import Simulation
from pwscf_input import PwscfInput,generate_pwscf_input
from pwscf_analyzer import PwscfAnalyzer

View File

@ -47,6 +47,7 @@
import os
from generic import obj
from simulation import Simulation,SimulationInput,SimulationAnalyzer
from pwscf import Pwscf
from gamess import Gamess
from pyscf_sim import Pyscf
from quantum_package import QuantumPackage
@ -218,16 +219,13 @@ class Pw2qmcpack(Simulation):
def check_result(self,result_name,sim):
calculating_result = False
inputpp = self.input.inputpp
if result_name=='orbitals':
calculating_result = True
else:
calculating_result = False
self.error('ability to check for result '+result_name+' has not been implemented')
#end if
return calculating_result
#end def check_result
def get_result(self,result_name,sim):
result = obj()
inputpp = self.input.inputpp
@ -252,45 +250,61 @@ class Pw2qmcpack(Simulation):
return result
#end def get_result
def incorporate_result(self,result_name,result,sim):
implemented = True
if result_name=='orbitals':
pwin = sim.input.control
p2in = self.input.inputpp
pwprefix = 'pwscf'
p2prefix = 'pwscf'
pwoutdir = './'
p2outdir = './'
if 'prefix' in pwin:
pwprefix = pwin.prefix
if 'prefix' in p2in:
p2prefix = p2in.prefix
if 'outdir' in pwin:
pwoutdir = pwin.outdir
if 'outdir' in p2in:
p2outdir = p2in.outdir
if pwoutdir.startswith('./'):
pwoutdir = pwoutdir[2:]
if p2outdir.startswith('./'):
p2outdir = p2outdir[2:]
pwdir = os.path.abspath(os.path.join(sim.locdir ,pwoutdir))
p2dir = os.path.abspath(os.path.join(self.locdir,p2outdir))
errors = False
if pwdir!=p2dir:
self.error('to use orbitals, '+self.generic_identifier+' must have the same outdir as pwscf\n pwscf outdir: '+pwdir+'\n '+self.generic_identifier+' outdir: '+p2dir,exit=False)
errors = True
#end if
if pwprefix!=p2prefix:
self.error('to use orbitals, '+self.generic_identifier+' must have the same prefix as pwscf\n pwscf prefix: '+pwprefix+'\n '+self.generic_identifier+' prefix: '+p2prefix,exit=False)
errors = True
#end if
if errors:
self.error(self.generic_identifier+' cannot use orbitals from pwscf')
if isinstance(sim,Pwscf):
pwin = sim.input.control
p2in = self.input.inputpp
pwprefix = 'pwscf'
p2prefix = 'pwscf'
pwoutdir = './'
p2outdir = './'
if 'prefix' in pwin:
pwprefix = pwin.prefix
#end if
if 'prefix' in p2in:
p2prefix = p2in.prefix
#end if
if 'outdir' in pwin:
pwoutdir = pwin.outdir
#end if
if 'outdir' in p2in:
p2outdir = p2in.outdir
#end if
if pwoutdir.startswith('./'):
pwoutdir = pwoutdir[2:]
#end if
if p2outdir.startswith('./'):
p2outdir = p2outdir[2:]
#end if
pwdir = os.path.abspath(os.path.join(sim.locdir ,pwoutdir))
p2dir = os.path.abspath(os.path.join(self.locdir,p2outdir))
errors = False
if pwdir!=p2dir:
self.error('to use orbitals, '+self.generic_identifier+' must have the same outdir as pwscf\n pwscf outdir: '+pwdir+'\n '+self.generic_identifier+' outdir: '+p2dir,exit=False)
errors = True
#end if
if pwprefix!=p2prefix:
self.error('to use orbitals, '+self.generic_identifier+' must have the same prefix as pwscf\n pwscf prefix: '+pwprefix+'\n '+self.generic_identifier+' prefix: '+p2prefix,exit=False)
errors = True
#end if
if errors:
self.error(self.generic_identifier+' cannot use orbitals from pwscf')
#end if
else:
implemented = False
#end if
else:
self.error('ability to incorporate result '+result_name+' has not been implemented')
implemented = False
#end if
if not implemented:
self.error('ability to incorporate result "{0}" from {1} has not been implemented'.format(result_name,sim.__class__.__name__))
#end if
#end def incorporate_result
def check_sim_status(self):
outfile = os.path.join(self.locdir,self.outfile)
fobj = open(outfile,'r')
@ -334,11 +348,13 @@ class Pw2qmcpack(Simulation):
self.finished = files_exist and self.job.finished
#end def check_sim_status
def get_output_files(self):
output_files = []
return output_files
#end def get_output_files
def app_command(self):
return self.app_name+'<'+self.infile
#end def app_command
@ -692,9 +708,6 @@ class Convert4qmc(Simulation):
calculating_result = True
elif result_name=='particles':
calculating_result = True
else:
calculating_result = False
self.error('ability to check for result '+result_name+' has not been implemented')
#end if
return calculating_result
#end def check_result
@ -1035,14 +1048,10 @@ class PyscfToAfqmc(Simulation):
def check_result(self,result_name,sim):
calculating_result = False
input = self.input
if result_name=='wavefunction':
calculating_result = input.output is not None
calculating_result = self.input.output is not None
elif result_name=='hamiltonian':
calculating_result = input.output is not None
else:
calculating_result = False
self.error('ability to check for result '+result_name+' has not been implemented')
calculating_result = self.input.output is not None
#end if
return calculating_result
#end def check_result

View File

@ -0,0 +1,590 @@
import testing
from testing import divert_nexus,restore_nexus,clear_all_sims
from testing import failed,FailedTest
from testing import value_eq,object_eq,text_eq
#====================================================================#
# pw2qmcpack tests #
#====================================================================#
def get_pw2qmcpack_sim(**kwargs):
from machines import job
from qmcpack_converters import Pw2qmcpack,generate_pw2qmcpack
sim = generate_pw2qmcpack(
job = job(machine='ws1',cores=1),
**kwargs
)
assert(isinstance(sim,Pw2qmcpack))
return sim
#end def get_pw2qmcpack_sim
def test_pw2qmcpack_import():
from qmcpack_converters import Pw2qmcpack,generate_pw2qmcpack
#end def test_pw2qmcpack_import
def test_pw2qmcpack_minimal_init():
from machines import job
from qmcpack_converters import Pw2qmcpack,generate_pw2qmcpack
sim = generate_pw2qmcpack(
job = job(machine='ws1',cores=1),
)
assert(isinstance(sim,Pw2qmcpack))
clear_all_sims()
#end def test_pw2qmcpack_minimal_init
def test_pw2qmcpack_check_result():
sim = get_pw2qmcpack_sim()
assert(not sim.check_result('unknown',None))
assert(sim.check_result('orbitals',None))
clear_all_sims()
#end def test_pw2qmcpack_check_result
def test_pw2qmcpack_get_result():
from generic import NexusError,obj
sim = get_pw2qmcpack_sim()
try:
sim.get_result('unknown',None)
raise FailedTest
except NexusError:
None
except FailedTest:
failed()
except Exception as e:
failed(str(e))
#end try
result = sim.get_result('orbitals',None)
result_ref = obj(
h5file = './runs/pwscf_output/pwscf.pwscf.h5',
ptcl_xml = './runs/pwscf_output/pwscf.ptcl.xml',
wfs_xml = './runs/pwscf_output/pwscf.wfs.xml',
)
assert(object_eq(result,result_ref))
clear_all_sims()
#end def test_pw2qmcpack_get_result
def test_pw2qmcpack_incorporate_result():
from generic import NexusError,obj
from simulation import Simulation
from test_pwscf_simulation import pseudo_inputs,get_pwscf_sim
tpath = testing.setup_unit_test_output_directory('pwscf_simulation','test_check_result',**pseudo_inputs)
other = Simulation()
scf = get_pwscf_sim('scf')
sim = get_pw2qmcpack_sim(path='scf')
try:
sim.incorporate_result('unknown',None,other)
raise TestFailed
except NexusError:
None
except FailedTest:
failed()
except Exception as e:
failed(str(e))
#end try
sim.incorporate_result('orbitals',None,scf)
clear_all_sims()
restore_nexus()
#end def test_pw2qmcpack_incorporate_result
def test_pw2qmcpack_check_sim_status():
import os
from generic import NexusError,obj
from nexus_base import nexus_core
tpath = testing.setup_unit_test_output_directory('qmcpack_converter_simulations','test_pw2qmcpack_check_sim_status',divert=True)
nexus_core.runs = ''
sim = get_pw2qmcpack_sim()
assert(sim.locdir.rstrip('/')==tpath.rstrip('/'))
assert(not sim.finished)
assert(not sim.failed)
try:
sim.check_sim_status()
raise FailedTest
except IOError:
None
except Exception as e:
failed(str(e))
#end try
sim.create_directories()
outfile = os.path.join(sim.locdir,sim.outfile)
outfile_text = 'JOB DONE'
out = open(outfile,'w')
out.write(outfile_text)
out.close()
assert(outfile_text in open(outfile,'r').read())
prefix = sim.input.inputpp.prefix
outdir = sim.input.inputpp.outdir
outdir_path = os.path.join(tpath,outdir)
if not os.path.exists(outdir_path):
os.makedirs(outdir_path)
#end if
filenames = [prefix+'.pwscf.h5',prefix+'.ptcl.xml',prefix+'.wfs.xml']
for filename in filenames:
filepath = os.path.join(tpath,outdir,filename)
f = open(filepath,'w')
f.write('')
f.close()
assert(os.path.exists(filepath))
#end for
sim.job.finished = True
sim.check_sim_status()
assert(sim.finished)
assert(not sim.failed)
clear_all_sims()
restore_nexus()
#end def test_pw2qmcpack_check_sim_status
#====================================================================#
# convert4qmc tests #
#====================================================================#
def get_convert4qmc_sim(**kwargs):
from machines import job
from qmcpack_converters import Convert4qmc,generate_convert4qmc
sim = generate_convert4qmc(
job = job(machine='ws1',cores=1),
**kwargs
)
assert(isinstance(sim,Convert4qmc))
return sim
#end def get_convert4qmc_sim
def test_convert4qmc_import():
from qmcpack_converters import Convert4qmc,generate_convert4qmc
#end def test_convert4qmc_import
def test_convert4qmc_minimal_init():
from machines import job
from qmcpack_converters import Convert4qmc,generate_convert4qmc
sim = generate_convert4qmc(
job = job(machine='ws1',cores=1),
)
assert(isinstance(sim,Convert4qmc))
clear_all_sims()
#end def test_convert4qmc_minimal_init
def test_convert4qmc_check_result():
sim = get_convert4qmc_sim()
assert(not sim.check_result('unknown',None))
assert(sim.check_result('orbitals',None))
assert(sim.check_result('particles',None))
clear_all_sims()
#end def test_convert4qmc_check_result
def test_convert4qmc_get_result():
from generic import NexusError,obj
sim = get_convert4qmc_sim()
try:
sim.get_result('unknown',None)
raise FailedTest
except NexusError:
None
except FailedTest:
failed()
except Exception as e:
failed(str(e))
#end try
result = sim.get_result('orbitals',None)
result_ref = obj(
location = './runs/sample.wfj.xml',
)
assert(object_eq(result,result_ref))
result = sim.get_result('particles',None)
result_ref = obj(
location = './runs/sample.structure.xml',
)
assert(object_eq(result,result_ref))
clear_all_sims()
#end def test_convert4qmc_get_result
def test_convert4qmc_incorporate_result():
from generic import NexusError,obj
from simulation import Simulation
from gamess import Gamess
from pyscf_sim import Pyscf
from quantum_package import QuantumPackage
from test_gamess_simulation import get_gamess_sim
from test_pyscf_simulation import get_pyscf_sim
from test_quantum_package_simulation import get_quantum_package_sim
other = Simulation()
gms = get_gamess_sim('rhf')
Gamess.ericfmt = None
gms_result = obj(
location = './rhf/rhf.out',
mos = 0,
norbitals = 0,
outfile = './rhf/rhf.out',
scftyp = 'rohf',
vec = None,
)
pscf = get_pyscf_sim()
pscf_result = obj(
h5_file = './scf.h5',
)
qp = get_quantum_package_sim()
QuantumPackage.qprc = None
qp_result = obj(
outfile = './qp_savewf.out',
)
sim_start = get_convert4qmc_sim()
sim = sim_start.copy()
try:
sim.incorporate_result('unknown',None,other)
raise TestFailed
except NexusError:
None
except FailedTest:
failed()
except Exception as e:
failed(str(e))
#end try
# incorporate orbitals from gamess
sim = sim_start.copy()
assert(sim.input_code is None)
assert(sim.input.gamess_ascii is None)
assert(sim.job.app_command=='convert4qmc')
sim.incorporate_result('orbitals',gms_result,gms)
assert(sim.input_code=='gamess')
assert(sim.input.gamess_ascii=='../rhf/rhf.out')
assert(sim.job.app_command=='convert4qmc -gamessAscii ../rhf/rhf.out')
# incorporate orbitals from pyscf
sim = sim_start.copy()
assert(sim.input_code is None)
assert(sim.input.pyscf is None)
sim.incorporate_result('orbitals',pscf_result,pscf)
assert(sim.input_code=='pyscf')
assert(sim.input.pyscf=='../scf.h5')
# incorporate orbitals from quantum package
sim = sim_start.copy()
assert(sim.input_code is None)
assert(sim.input.qp is None)
sim.incorporate_result('orbitals',qp_result,qp)
assert(sim.input_code=='qp')
assert(sim.input.qp=='../qp_savewf.out')
clear_all_sims()
#end def test_convert4qmc_incorporate_result
def test_convert4qmc_check_sim_status():
import os
from generic import NexusError,obj
from nexus_base import nexus_core
tpath = testing.setup_unit_test_output_directory('qmcpack_converter_simulations','test_convert4qmc_check_sim_status',divert=True)
nexus_core.runs = ''
sim = get_convert4qmc_sim()
assert(sim.locdir.rstrip('/')==tpath.rstrip('/'))
assert(not sim.finished)
assert(not sim.failed)
try:
sim.check_sim_status()
raise FailedTest
except IOError:
None
except Exception as e:
failed(str(e))
#end try
sim.create_directories()
outfile = os.path.join(sim.locdir,sim.outfile)
outfile_text = 'QMCGaussianParserBase::dump'
out = open(outfile,'w')
out.write(outfile_text)
out.close()
assert(outfile_text in open(outfile,'r').read())
for filename in sim.list_output_files():
filepath = os.path.join(sim.locdir,filename)
f = open(filepath,'w')
f.write('')
f.close()
assert(os.path.exists(filepath))
#end for
sim.job.finished = True
sim.check_sim_status()
assert(sim.finished)
assert(not sim.failed)
clear_all_sims()
restore_nexus()
#end def test_convert4qmc_check_sim_status
#====================================================================#
# pyscf_to_afqmc tests #
#====================================================================#
def get_pyscf_to_afqmc_sim(**kwargs):
from machines import job
from qmcpack_converters import PyscfToAfqmc,generate_pyscf_to_afqmc
sim = generate_pyscf_to_afqmc(
job = job(machine='ws1',cores=1),
**kwargs
)
assert(isinstance(sim,PyscfToAfqmc))
return sim
#end def get_pyscf_to_afqmc_sim
def test_pyscf_to_afqmc_import():
from qmcpack_converters import PyscfToAfqmc,generate_pyscf_to_afqmc
#end def test_pyscf_to_afqmc_import
def test_pyscf_to_afqmc_minimal_init():
from machines import job
from qmcpack_converters import PyscfToAfqmc,generate_pyscf_to_afqmc
sim = generate_pyscf_to_afqmc(
job = job(machine='ws1',cores=1),
)
assert(isinstance(sim,PyscfToAfqmc))
clear_all_sims()
#end def test_pyscf_to_afqmc_minimal_init
def test_pyscf_to_afqmc_check_result():
sim = get_pyscf_to_afqmc_sim()
assert(not sim.check_result('unknown',None))
assert(not sim.check_result('wavefunction',None))
assert(not sim.check_result('hamiltonian',None))
sim.input.output = 'afqmc.h5'
assert(sim.check_result('wavefunction',None))
assert(sim.check_result('hamiltonian',None))
clear_all_sims()
#end def test_pyscf_to_afqmc_check_result
def test_pyscf_to_afqmc_get_result():
from generic import NexusError,obj
sim = get_pyscf_to_afqmc_sim()
sim.input.output = 'afqmc.h5'
try:
sim.get_result('unknown',None)
raise FailedTest
except NexusError:
None
except FailedTest:
failed()
except Exception as e:
failed(str(e))
#end try
result_ref = obj(
h5_file = './runs/afqmc.h5',
)
result = sim.get_result('wavefunction',None)
assert(object_eq(result,result_ref))
result = sim.get_result('hamiltonian',None)
assert(object_eq(result,result_ref))
clear_all_sims()
#end def test_pyscf_to_afqmc_get_result
def test_pyscf_to_afqmc_incorporate_result():
import os
from generic import NexusError,obj
from simulation import Simulation
from test_pyscf_simulation import get_pyscf_sim
other = Simulation()
scf = get_pyscf_sim()
sim = get_pyscf_to_afqmc_sim()
try:
sim.incorporate_result('unknown',None,other)
raise TestFailed
except NexusError:
None
except FailedTest:
failed()
except Exception as e:
failed(str(e))
#end try
result = obj(
chkfile = os.path.join(scf.locdir,'scf.chk'),
)
assert(sim.input.input==None)
sim.incorporate_result('wavefunction',result,scf)
assert(sim.input.input=='scf.chk')
clear_all_sims()
#end def test_pyscf_to_afqmc_incorporate_result
def test_pyscf_to_afqmc_check_sim_status():
import os
from generic import NexusError,obj
from nexus_base import nexus_core
tpath = testing.setup_unit_test_output_directory('qmcpack_converter_simulations','test_pyscf_to_afqmc_check_sim_status',divert=True)
nexus_core.runs = ''
sim = get_pyscf_to_afqmc_sim()
assert(sim.locdir.rstrip('/')==tpath.rstrip('/'))
assert(not sim.finished)
assert(not sim.failed)
try:
sim.check_sim_status()
raise FailedTest
except IOError:
None
except Exception as e:
failed(str(e))
#end try
sim.input.output = 'afqmc.h5'
sim.create_directories()
outfile = os.path.join(sim.locdir,sim.outfile)
outfile_text = '# Finished.'
out = open(outfile,'w')
out.write(outfile_text)
out.close()
assert(outfile_text in open(outfile,'r').read())
output_file = os.path.join(sim.locdir,sim.input.output)
f = open(output_file,'w')
f.write('')
f.close()
assert(os.path.exists(output_file))
sim.job.finished = True
sim.check_sim_status()
assert(sim.finished)
assert(not sim.failed)
clear_all_sims()
restore_nexus()
#end def test_pyscf_to_afqmc_check_sim_status

View File

@ -1,13 +0,0 @@
import testing
from testing import value_eq,object_eq
def test_import():
from qmcpack_converters import Pw2qmcpack
from qmcpack_converters import generate_pw2qmcpack
from qmcpack_converters import Convert4qmc
from qmcpack_converters import generate_convert4qmc
#end def test_import