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()
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()
def test_check_sim_status(): import os tpath = testing.setup_unit_test_output_directory('vasp_simulation', 'test_check_sim_status', **pseudo_inputs) sim = setup_vasp_sim(tpath) assert (sim.locdir.strip('/') == tpath.strip('/')) assert (not sim.finished) assert (not sim.input.performing_neb()) sim.check_sim_status() assert (not sim.finished) outcar_file = os.path.join(tpath, 'OUTCAR') outcar_text = 'General timing and accounting' outcar = open(outcar_file, 'w') outcar.write(outcar_text) outcar.close() assert (os.path.exists(outcar_file)) assert (outcar_text in open(outcar_file, 'r').read()) sim.check_sim_status() assert (sim.finished) assert (not sim.failed) clear_all_sims() restore_nexus()
def test_get_output_files(): import os tpath = testing.setup_unit_test_output_directory('vasp_simulation', 'test_get_output_files', **pseudo_inputs) sim = setup_vasp_sim(tpath) vfiles = 'INCAR KPOINTS POSCAR CONTCAR OUTCAR'.split() for vfile in vfiles: f = open(os.path.join(tpath, vfile), 'w') f.write('') f.close() #end for files = sim.get_output_files() assert (value_eq(files, vfiles)) for vfile in vfiles: assert (os.path.exists( os.path.join(tpath, sim.identifier + '.' + vfile))) #end for clear_all_sims() restore_nexus()
def test_incorporate_result(): import os import shutil from numpy import array from generic import obj tpath = testing.setup_unit_test_output_directory( 'vasp_simulation', 'test_incorporate_result', **pseudo_inputs) sim = setup_vasp_sim(tpath, identifier='diamond', files=True) pcfile = os.path.join(tpath, 'diamond_POSCAR') ccfile = os.path.join(tpath, sim.identifier + '.CONTCAR') assert (sim.locdir.strip('/') == tpath.strip('/')) shutil.copy2(pcfile, ccfile) assert (os.path.exists(ccfile)) result = sim.get_result('structure', None) sim2 = setup_vasp_sim(tpath) pid = id(sim2.input.poscar) sim2.incorporate_result('structure', result, None) assert (id(sim2.input.poscar) != pid) poscar_ref = obj( axes=array([[3.57, 3.57, 0.], [0., 3.57, 3.57], [3.57, 0., 3.57]], dtype=float), coord='cartesian', description=None, dynamic=None, elem=['C'], elem_count=[16], pos=array([[0., 0., 0.], [0.8925, 0.8925, 0.8925], [1.785, 1.785, 0.], [2.6775, 2.6775, 0.8925], [0., 1.785, 1.785], [0.8925, 2.6775, 2.6775], [1.785, 3.57, 1.785], [2.6775, 4.4625, 2.6775], [1.785, 0., 1.785], [2.6775, 0.8925, 2.6775], [3.57, 1.785, 1.785], [4.4625, 2.6775, 2.6775], [1.785, 1.785, 3.57], [2.6775, 2.6775, 4.4625], [3.57, 3.57, 3.57], [4.4625, 4.4625, 4.4625]], dtype=float), scale=1.0, vel=None, vel_coord=None, ) assert (object_eq(sim2.input.poscar.to_obj(), poscar_ref)) clear_all_sims() restore_nexus()
def test_get_result(): import os from generic import NexusError, obj from nexus_base import nexus_core tpath = testing.setup_unit_test_output_directory('gamess_simulation', 'test_get_result', divert=True) nexus_core.runs = '' sim = get_gamess_sim('rhf') assert (sim.locdir.rstrip('/') == os.path.join(tpath, 'rhf').rstrip('/')) sim.create_directories() sim.write_inputs() if not os.path.exists(sim.imresdir): os.makedirs(sim.imresdir) #end if analyzer = sim.analyzer_type(sim) analyzer.save(os.path.join(sim.imresdir, sim.analyzer_image)) 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='rhf/rhf.out', mos=0, norbitals=0, outfile='rhf/rhf.out', scftyp='rohf', vec=None, ) result.location = result.location.replace(tpath, '').lstrip('/') result.outfile = result.outfile.replace(tpath, '').lstrip('/') assert (object_eq(result, result_ref)) clear_all_sims() restore_nexus()
def test_check_result(): tpath = testing.setup_unit_test_output_directory('pwscf_simulation','test_check_result',**pseudo_inputs) sim = get_pwscf_sim('scf') assert(not sim.check_result('unknown',None)) assert(sim.check_result('charge_density',None)) assert(sim.check_result('restart',None)) assert(sim.check_result('orbitals',None)) assert(not sim.check_result('structure',None)) clear_all_sims() restore_nexus()
def test_check_result(): tpath = testing.setup_unit_test_output_directory('vasp_simulation', 'test_check_result', **pseudo_inputs) sim = setup_vasp_sim(tpath) assert (not sim.check_result('unknown', None)) assert (sim.check_result('structure', None)) clear_all_sims() restore_nexus()
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()
def test_incorporate_result(): from generic import obj tpath = testing.setup_unit_test_output_directory('pwscf_simulation','test_incorporate_result',**pseudo_inputs) sim = get_pwscf_sim('scf') sim_start = sim.to_obj().copy() assert(object_eq(sim.to_obj(),sim_start)) # charge density result = obj( locdir = sim.locdir, ) sim.incorporate_result('charge_density',result,None) assert(object_eq(sim.to_obj(),sim_start)) # restart sim.incorporate_result('restart',result,None) c = sim.input.control assert(c.restart_mode=='restart') del c.restart_mode assert(object_eq(sim.to_obj(),sim_start)) # structure altered_structure = sim.system.structure.copy() altered_structure.pos += 0.1 result = obj( structure = altered_structure, ) sim.incorporate_result('structure',result,None) sim_ref = sim_start.copy() pos_ref = sim_ref.system.structure.delete('pos')+0.1 sim_ref.input.atomic_positions.delete('positions') pos = sim.system.structure.delete('pos') apos = sim.input.atomic_positions.delete('positions') assert(value_eq(pos,pos_ref)) assert(value_eq(apos,pos_ref)) assert(object_eq(sim.to_obj(),sim_ref)) clear_all_sims() restore_nexus()
def test_get_result(): from generic import NexusError tpath = testing.setup_unit_test_output_directory('pwscf_simulation','test_get_result',**pseudo_inputs) sim = get_pwscf_sim('scf') 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('charge_density',None) result2 = sim.get_result('restart',None) assert(object_eq(result,result2)) result_ref = dict( locdir = 'scf', outdir = 'scf/pwscf_output', location = 'scf/pwscf_output/pwscf.save/charge-density.dat', spin_location = 'scf/pwscf_output/pwscf.save/spin-polarization.dat', ) assert(set(result.keys())==set(result_ref.keys())) for k,path in result.items(): path = path.replace(tpath,'').lstrip('/') assert(path==result_ref[k]) #end for result = sim.get_result('orbitals',None) result_ref = dict( location = 'scf/pwscf_output/pwscf.wfc1', ) assert(set(result.keys())==set(result_ref.keys())) for k,path in result.items(): path = path.replace(tpath,'').lstrip('/') assert(path==result_ref[k]) #end for clear_all_sims() restore_nexus()
def test_check_sim_status(): import os tpath = testing.setup_unit_test_output_directory('pwscf_simulation','test_check_sim_status',**pseudo_inputs) sim = get_pwscf_sim('scf') assert(sim.locdir.rstrip('/')==os.path.join(tpath,'scf').rstrip('/')) if not os.path.exists(sim.locdir): os.makedirs(sim.locdir) #end if assert(not sim.finished) try: sim.check_sim_status() except IOError: None except Exception as e: failed(str(e)) #end try assert(not sim.finished) out_path = os.path.join(tpath,'scf',sim.outfile) out_text = '' outfile = open(out_path,'w') outfile.write(out_text) outfile.close() assert(os.path.exists(out_path)) sim.check_sim_status() assert(not sim.finished) out_text = 'JOB DONE' outfile = open(out_path,'w') outfile.write(out_text) outfile.close() assert(out_text in open(out_path,'r').read()) sim.check_sim_status() assert(sim.finished) assert(not sim.failed) clear_all_sims() restore_nexus()
def test_generate(): import os from nexus_base import nexus_noncore from physical_system import generate_physical_system from vasp_input import generate_vasp_input, VaspInput tpath = testing.setup_unit_test_output_directory('vasp_input', 'test_generate', divert=True) pseudo_dir = os.path.join(tpath, 'pseudopotentials') nexus_noncore.pseudo_dir = pseudo_dir if not os.path.exists(pseudo_dir): os.makedirs(pseudo_dir) #end if open(os.path.join(pseudo_dir, 'C.POTCAR'), 'w').write(c_potcar_text) files = get_files() dia16 = generate_physical_system(structure=files['d16bulk.POSCAR'], C=4) vi = generate_vasp_input( system=dia16, pseudos=['C.POTCAR'], input_type='generic', istart=0, icharg=2, encut=450, nsw=5, ibrion=2, isif=2, kcenter='monkhorst', kgrid=(2, 2, 2), kshift=(0, 0, 0), ) assert (isinstance(vi, VaspInput)) del vi.potcar.filepath check_vs_serial_reference(vi, 'generate') restore_nexus()
def test_check_sim_status(): import os from nexus_base import nexus_core tpath = testing.setup_unit_test_output_directory('qmcpack_simulation', 'test_check_sim_status', divert=True) nexus_core.runs = '' sim = get_qmcpack_sim(identifier='qmc') assert (sim.locdir.rstrip('/') == tpath.rstrip('/')) assert (not sim.finished) assert (not sim.failed) try: sim.check_sim_status() except IOError: None #end try assert (not sim.finished) assert (not sim.failed) outfile = os.path.join(tpath, sim.outfile) out_text = 'Total Execution' out = open(outfile, 'w') out.write(out_text) out.close() assert (os.path.exists(outfile)) assert (out_text in open(outfile, 'r').read()) errfile = os.path.join(tpath, sim.errfile) err = open(errfile, 'w') err.write('') err.close() assert (os.path.exists(errfile)) sim.check_sim_status() assert (sim.finished) assert (not sim.failed) clear_all_sims() restore_nexus()
def test_get_result(): import os from generic import NexusError, obj from nexus_base import nexus_core tpath = testing.setup_unit_test_output_directory('pyscf_simulation', 'test_get_result', divert=True) nexus_core.runs = '' template_file = 'scf_template.py' template_text = 'template $chkfile' template_filepath = os.path.join(tpath, template_file) f = open(template_filepath, 'w') f.write(template_text) f.close() sim = get_pyscf_sim( prefix='scf', checkpoint='scf.chk', template=template_filepath, ) 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) assert (result.h5_file.replace(tpath, '').lstrip('/') == 'scf.h5') result = sim.get_result('wavefunction', None) assert (result.chkfile.replace(tpath, '').lstrip('/') == 'scf.chk') clear_all_sims() restore_nexus()
def test_check_sim_status(): import os from generic import NexusError, obj from nexus_base import nexus_core tpath = testing.setup_unit_test_output_directory('gamess_simulation', 'test_check_sim_status', divert=True) nexus_core.runs = '' sim = get_gamess_sim('rhf') assert (sim.locdir.rstrip('/') == os.path.join(tpath, 'rhf').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 = 'EXECUTION OF GAMESS TERMINATED NORMALLY' out = open(outfile, 'w') out.write(outfile_text) out.close() assert (outfile_text in open(outfile, 'r').read()) sim.check_sim_status() assert (sim.finished) assert (not sim.failed) clear_all_sims() restore_nexus()
def test_check_sim_status(): import os from generic import NexusError,obj from nexus_base import nexus_core tpath = testing.setup_unit_test_output_directory('quantum_package_simulation','test_check_sim_status',divert=True) nexus_core.runs = '' sim = get_quantum_package_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 = '* SCF energy' out = open(outfile,'w') out.write(outfile_text) out.close() assert(outfile_text in open(outfile,'r').read()) sim.job.finished = True sim.check_sim_status() assert(sim.finished) assert(not sim.failed) clear_all_sims() restore_nexus()
def test_get_result(): import os import shutil from numpy import array from generic import obj, NexusError tpath = testing.setup_unit_test_output_directory('vasp_simulation', 'test_get_result', **pseudo_inputs) sim = setup_vasp_sim(tpath, identifier='diamond', files=True) try: sim.get_result('unknown', None) raise FailedTest except NexusError: None except FailedTest: failed() except Exception as e: failed(str(e)) #end try pcfile = os.path.join(tpath, 'diamond_POSCAR') ccfile = os.path.join(tpath, sim.identifier + '.CONTCAR') assert (sim.locdir.strip('/') == tpath.strip('/')) shutil.copy2(pcfile, ccfile) assert (os.path.exists(ccfile)) result = sim.get_result('structure', None) result_ref = obj(structure=obj( axes=array([[3.57, 3.57, 0.], [0., 3.57, 3.57], [3.57, 0., 3.57]], dtype=float), background_charge=0, bconds=array([]), center=array([3.57, 3.57, 3.57], dtype=float), dim=3, elem=array([ 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C' ], dtype=object), folded_structure=None, frozen=None, kaxes=array([[0.87999794, 0.87999794, -0.87999794], [-0.87999794, 0.87999794, 0.87999794], [0.87999794, -0.87999794, 0.87999794]], dtype=float), kpoints=array([], dtype=float), kweights=array([], dtype=float), mag=None, pos=array([[0., 0., 0.], [0.8925, 0.8925, 0.8925], [1.785, 1.785, 0.], [2.6775, 2.6775, 0.8925], [0., 1.785, 1.785], [0.8925, 2.6775, 2.6775], [1.785, 3.57, 1.785], [2.6775, 4.4625, 2.6775], [1.785, 0., 1.785], [2.6775, 0.8925, 2.6775], [3.57, 1.785, 1.785], [4.4625, 2.6775, 2.6775], [1.785, 1.785, 3.57], [2.6775, 2.6775, 4.4625], [3.57, 3.57, 3.57], [4.4625, 4.4625, 4.4625]], dtype=float), scale=1.0, units='A', ), ) assert (check_object_eq(result, result_ref)) clear_all_sims() restore_nexus()
def test_get_result(): import os from subprocess import Popen, PIPE from generic import NexusError, obj from nexus_base import nexus_core from qmcpack_analyzer import QmcpackAnalyzer tpath = testing.setup_unit_test_output_directory('qmcpack_simulation', 'test_get_result', divert=True) nexus_core.runs = '' nexus_core.results = '' sim = get_qmcpack_sim() assert (sim.locdir.rstrip('/') == tpath.rstrip('/')) 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('cuspcorr', None) result_ref = obj( updet_cusps='updet.cuspInfo.xml', spo_dn_cusps='qmcpack.spo-dn.cuspInfo.xml', spo_up_cusps='qmcpack.spo-up.cuspInfo.xml', dndet_cusps='downdet.cuspInfo.xml', ) assert (set(result.keys()) == set(result_ref.keys())) for k, v in result.items(): assert (v.replace(tpath, '').lstrip('/') == result_ref[k]) #end for qa_files_path = testing.unit_test_file_path('qmcpack_analyzer', 'diamond_gamma/opt') command = 'rsync -a {} {}'.format(qa_files_path, tpath) process = Popen(command, shell=True, stdout=PIPE, stderr=PIPE, close_fds=True) out, err = process.communicate() opt_path = os.path.join(tpath, 'opt') opt_infile = os.path.join(opt_path, 'opt.in.xml') assert (os.path.exists(opt_infile)) qa = QmcpackAnalyzer(opt_infile, analyze=True, equilibration=5) assert (qa.results.optimization.optimal_file == 'opt.s003.opt.xml') sim.create_directories() qa.save(os.path.join(sim.imresdir, sim.analyzer_image)) result = sim.get_result('jastrow', None) result_ref = obj(opt_file='opt.s003.opt.xml', ) assert (set(result.keys()) == set(result_ref.keys())) for k, v in result.items(): assert (v.replace(tpath, '').lstrip('/') == result_ref[k]) #end for clear_all_sims() restore_nexus()
def test_sim(): import os from nexus_base import nexus_core from test_simulation_module import get_sim tpath = testing.setup_unit_test_output_directory('nxs_sim','test_sim',divert=True) nexus_core.runs = '' nexus_core.results = '' exe = testing.executable_path('nxs-sim') sim = get_sim() sim.create_directories() sim.save_image() simp_path = os.path.join(tpath,sim.imlocdir,'sim.p') assert(os.path.isfile(simp_path)) # initial simulation state command = sys.executable+' {} show {}'.format(exe,simp_path) out,err,rc = execute(command) out_ref = ''' {} setup 0 sent_files 0 submitted 0 finished 0 failed 0 got_output 0 analyzed 0 '''.format(simp_path) assert(text_eq(out,out_ref)) # final simulation state command = sys.executable+' {} complete {}'.format(exe,simp_path) out,err,rc = execute(command) command = sys.executable+' {} show {}'.format(exe,simp_path) out,err,rc = execute(command) out_ref = ''' {} setup 1 sent_files 1 submitted 1 finished 1 failed 0 got_output 1 analyzed 1 '''.format(simp_path) assert(text_eq(out,out_ref)) # intermediate simulation state 1 command = sys.executable+' {} reset {}'.format(exe,simp_path) out,err,rc = execute(command) command = sys.executable+' {} set setup sent_files submitted {}'.format(exe,simp_path) out,err,rc = execute(command) command = sys.executable+' {} show {}'.format(exe,simp_path) out,err,rc = execute(command) out_ref = ''' {} setup 1 sent_files 1 submitted 1 finished 0 failed 0 got_output 0 analyzed 0 '''.format(simp_path) # intermediate simulation state 2 command = sys.executable+' {} complete {}'.format(exe,simp_path) out,err,rc = execute(command) command = sys.executable+' {} unset got_output analyzed {}'.format(exe,simp_path) out,err,rc = execute(command) command = sys.executable+' {} show {}'.format(exe,simp_path) out,err,rc = execute(command) out_ref = ''' {} setup 1 sent_files 1 submitted 1 finished 1 failed 0 got_output 0 analyzed 0 '''.format(simp_path) assert(text_eq(out,out_ref)) clear_all_sims() restore_nexus()
def test_incorporate_result(): import os import shutil from subprocess import Popen, PIPE from numpy import array from generic import NexusError, obj from nexus_base import nexus_core from test_vasp_simulation import setup_vasp_sim as get_vasp_sim from test_vasp_simulation import pseudo_inputs as vasp_pseudo_inputs from test_qmcpack_converter_simulations import get_pw2qmcpack_sim from test_qmcpack_converter_simulations import get_convert4qmc_sim from test_qmcpack_converter_simulations import get_pyscf_to_afqmc_sim pseudo_inputs = obj(vasp_pseudo_inputs) tpath = testing.setup_unit_test_output_directory( 'qmcpack_simulation', 'test_incorporate_result', **pseudo_inputs) nexus_core.runs = '' nexus_core.results = '' # incorporate vasp structure sim = get_qmcpack_sim(identifier='qmc_vasp_structure', tiling=(2, 2, 2)) vasp_struct = get_vasp_sim(tpath, identifier='vasp_structure', files=True) assert (sim.locdir.strip('/') == tpath.strip('/')) pc_file = 'diamond_POSCAR' cc_file = vasp_struct.identifier + '.CONTCAR' shutil.copy2(os.path.join(tpath, pc_file), os.path.join(tpath, cc_file)) result = vasp_struct.get_result('structure', None) ion0 = sim.input.get('ion0') c = ion0.groups.C cp0 = c.position[0].copy() s = result.structure.copy() s.change_units('B') rp0 = s.pos[0] zero = array([0, 0, 0], dtype=float) assert (value_eq(c.position[0], cp0, atol=1e-8)) c.position[0] += 0.1 assert (not value_eq(c.position[0], cp0, atol=1e-8)) assert (not value_eq(c.position[0], rp0, atol=1e-8)) sim.incorporate_result('structure', result, vasp_struct) ion0 = sim.input.get('ion0') c = ion0.groups.C assert (value_eq(c.position[0], rp0, atol=1e-8)) # incorporate pw2qmcpack orbitals sim = get_qmcpack_sim(identifier='qmc_p2q_orbitals') p2q_orb = get_pw2qmcpack_sim(identifier='p2q_orbitals') result = p2q_orb.get_result('orbitals', None) p2q_output_path = os.path.join(tpath, 'pwscf_output') if not os.path.exists(p2q_output_path): os.makedirs(p2q_output_path) #end if p2q_h5file = os.path.join(p2q_output_path, 'pwscf.pwscf.h5') f = open(p2q_h5file, 'w') f.write('') f.close() assert (os.path.exists(p2q_h5file)) spo = sim.input.get('bspline') assert (spo.href == 'MISSING.h5') sim.incorporate_result('orbitals', result, p2q_orb) assert (spo.href == 'pwscf_output/pwscf.pwscf.h5') # incorporate convert4qmc orbitals sim = get_qmcpack_sim(identifier='qmc_c4q_orbitals') c4q_orb = get_convert4qmc_sim(identifier='c4q_orbitals') result = c4q_orb.get_result('orbitals', None) wfn_file = os.path.join(tpath, 'c4q_orbitals.wfj.xml') wfn_file2 = os.path.join(tpath, 'c4q_orbitals.orbs.h5') input = sim.input.copy() dset = input.get('determinantset') dset.href = 'orbs.h5' qs = input.simulation.qmcsystem del input.simulation input.qmcsystem = qs input.write(wfn_file) assert (os.path.exists(wfn_file)) open(wfn_file2, 'w').write('fake') assert (os.path.exists(wfn_file2)) from qmcpack_input import QmcpackInput inp = QmcpackInput(wfn_file) dset = sim.input.get('determinantset') assert ('href' not in dset) sim.incorporate_result('orbitals', result, c4q_orb) dset = sim.input.get('determinantset') assert (dset.href == 'c4q_orbitals.orbs.h5') # incorporate qmcpack jastrow sim = get_qmcpack_sim(identifier='qmc_jastrow') qa_files_path = testing.unit_test_file_path('qmcpack_analyzer', 'diamond_gamma/opt') command = 'rsync -a {} {}'.format(qa_files_path, tpath) process = Popen(command, shell=True, stdout=PIPE, stderr=PIPE, close_fds=True) out, err = process.communicate() opt_path = os.path.join(tpath, 'opt') opt_infile = os.path.join(opt_path, 'opt.in.xml') assert (os.path.exists(opt_infile)) opt_file = os.path.join(opt_path, 'opt.s004.opt.xml') assert (os.path.exists(opt_file)) result = obj(opt_file=opt_file) sim.incorporate_result('jastrow', result, sim) j = sim.input.get('jastrow') j_text = j.write().replace('"', ' " ') j_text_ref = ''' <jastrow type="Two-Body" name="J2" function="bspline" print="yes"> <correlation speciesA="u" speciesB="u" size="8" rcut="2.3851851232"> <coefficients id="uu" type="Array"> 0.2576630369 0.1796686015 0.1326653657 0.09407180823 0.06267013118 0.03899100023 0.02070235604 0.009229775746 </coefficients> </correlation> <correlation speciesA="u" speciesB="d" size="8" rcut="2.3851851232"> <coefficients id="ud" type="Array"> 0.4385891515 0.3212399072 0.2275448261 0.1558506324 0.1009589176 0.06108433554 0.03154274436 0.01389485975 </coefficients> </correlation> </jastrow> '''.replace('"', ' " ') assert (text_eq(j_text, j_text_ref)) # incorporate qmcpack wavefunction sim = get_qmcpack_sim(identifier='qmc_wavefunction') sim.incorporate_result('wavefunction', result, sim) j = sim.input.get('jastrow') j_text = j.write().replace('"', ' " ') assert (text_eq(j_text, j_text_ref)) # incorporate qmcpack wavefunction afqmc sim = get_qmcpack_sim(type='afqmc', identifier='qmc_wf_afqmc') p2a_wf = get_pyscf_to_afqmc_sim(identifier='p2a_wavefunction') result = p2a_wf.get_result('wavefunction', None) del result.xml wfn = sim.input.get('wavefunction') ham = sim.input.get('hamiltonian') assert (wfn.filename == 'MISSING.h5') assert (ham.filename == 'MISSING.h5') sim.incorporate_result('wavefunction', result, p2a_wf) assert (wfn.filename == 'p2a_wavefunction.afqmc.h5') assert (ham.filename == 'p2a_wavefunction.afqmc.h5') clear_all_sims() restore_nexus()
def test_run_project(): from generic import generic_settings from nexus_base import nexus_core from simulation import Simulation, input_template from project_manager import ProjectManager from test_simulation_module import get_test_workflow, n_test_workflows tpath = testing.setup_unit_test_output_directory('project_manager', 'test_run_project', divert=True) assert (nexus_core.mode == nexus_core.modes.stages) assert (len(nexus_core.stages) == 0) nexus_core.stages = list(nexus_core.primary_modes) nexus_core.stages_set = set(nexus_core.stages) primary_modes = ['setup', 'send_files', 'submit', 'get_output', 'analyze'] assert (value_eq(nexus_core.stages, primary_modes)) assert (value_eq(nexus_core.stages_set, set(primary_modes))) nexus_core.sleep = 0.1 log = generic_settings.devlog flags = [ 'setup', 'sent_files', 'submitted', 'finished', 'got_output', 'analyzed' ] def finished(s): f = True for flag in flags: f &= s[flag] #end for f &= isinstance(s.process_id, int) return f #end def finished def empty(s): e = True for flag in flags: e &= not s[flag] #end for e &= s.process_id is None e &= s.job.system_id is None return e #end def empty sims = [] for n in range(n_test_workflows): #for n in range(1): sims.extend(get_test_workflow(n).list()) #end for template = ''' name = "$name" a = $a ''' for s in sims: si = input_template(text=template) si.assign(name='input_name', a=1) s.input = si #end for for s in sims: assert (empty(s)) #end for pm = ProjectManager() pm.machine = sims[0].job.get_machine() pm.add_simulations(sims) #pm.write_simulation_status() pm.run_project() #pm.write_simulation_status() #print(log.contents()) for s in sims: assert (finished(s)) #end for restore_nexus() Simulation.clear_all_sims()
def test_write_simulation_status(): from generic import generic_settings from nexus_base import nexus_core from simulation import Simulation from project_manager import ProjectManager from test_simulation_module import get_test_workflow divert_nexus() log = generic_settings.devlog sims = get_test_workflow(3) for id, sim in sims.items(): sim.identifier = 'test_sim_' + id #end for pm = ProjectManager() pm.add_simulations(sims.list()) status_modes = nexus_core.status_modes def status_log(): log.reset() pm.write_simulation_status() s = log.contents() return s #end def status_log assert (nexus_core.status == status_modes.none) status_ref = ''' cascade status setup, sent_files, submitted, finished, got_output, analyzed, failed 000000 0 ------ test_sim_s11 ./runs/ 000000 0 ------ test_sim_s21 ./runs/ 000000 0 ------ test_sim_s12 ./runs/ 000000 0 ------ test_sim_s22 ./runs/ 000000 0 ------ test_sim_s3 ./runs/ 000000 0 ------ test_sim_s4 ./runs/ 000000 0 ------ test_sim_s5 ./runs/ setup, sent_files, submitted, finished, got_output, analyzed, failed ''' assert (status_log().strip() == status_ref.strip()) nexus_core.status = status_modes.standard assert (status_log().strip() == status_ref.strip()) nexus_core.status = status_modes.active status_ref = ''' cascade status setup, sent_files, submitted, finished, got_output, analyzed, failed 000000 0 ------ test_sim_s11 ./runs/ 000000 0 ------ test_sim_s12 ./runs/ setup, sent_files, submitted, finished, got_output, analyzed, failed ''' assert (status_log().strip() == status_ref.strip()) nexus_core.status = status_modes.ready assert (status_log().strip() == status_ref.strip()) nexus_core.status = status_modes.failed status_ref = ''' cascade status setup, sent_files, submitted, finished, got_output, analyzed, failed setup, sent_files, submitted, finished, got_output, analyzed, failed ''' assert (status_log().strip() == status_ref.strip()) restore_nexus() Simulation.clear_all_sims()
def test_generate(): import os from generic import obj from nexus_base import nexus_noncore from pseudopotential import Pseudopotentials from physical_system import generate_physical_system from gamess_input import generate_gamess_input ppfiles = ['H.BFD_V5Z_ANO.gms','O.BFD_V5Z.gms'] tpath = testing.setup_unit_test_output_directory( test = 'gamess_input', subtest = 'test_generate', divert = True, file_sets = { 'pseudopotentials':ppfiles } ) ppfiles_full = [os.path.join(tpath,'pseudopotentials',f) for f in ppfiles] nexus_noncore.pseudopotentials = Pseudopotentials(ppfiles_full) input_files = ['rhf.inp','cisd.inp','cas.inp'] h2o = generate_physical_system( elem = ['O','H','H'], pos = [[0.000000, 0.000000, 0.000000], [0.000000,-0.757160, 0.586260], [0.000000, 0.757160, 0.586260]], units = 'A', net_spin = 0, O = 6, H = 1, # C2v symmetry structure folded_elem = ['O','H'], folded_pos = [[0.000000, 0.000000, 0.000000], [0.000000, 0.757160, 0.586260]], ) inputs = dict() inputs['rhf.inp'] = obj( system = h2o, pseudos = ppfiles, scftyp = 'rohf', runtyp = 'energy', exetyp = 'run', ispher = 1, maxit = 200, memory = 150000000, dirscf = True, guess = 'huckel', symmetry = 'Cnv 2', ) inputs['cisd.inp'] = obj( system = h2o, pseudos = ppfiles, scftyp = 'none', cityp = 'guga', runtyp = 'energy', exetyp = 'run', ispher = 1, maxit = 200, memory = 150000000, dirscf = True, symmetry = 'Cnv 2', cidrt = obj( group = 'c2v', nfzc = 0, ndoc = 4, nalp = 0, nval = 60, nprt = 2, istsym = 1, iexcit = 2, mxnint = 500000, ), gugdia = obj( prttol = 0.001, cvgtol = 1.0e-5, itermx = 1000, ), ) inputs['cas.inp'] = obj( system = h2o, pseudos = ppfiles, scftyp = 'mcscf', runtyp = 'energy', exetyp = 'run', ispher = 1, maxit = 200, memory = 150000000, dirscf = True, symmetry = 'Cnv 2', drt = obj( group = 'c2v', nmcc = 0, ndoc = 4, nalp = 0, nval = 4, istsym = 1, mxnint = 500000, fors = True, ), mcscf = obj( cistep = 'guga', maxit = 1000, fullnr = True, acurcy = 1e-5, ), ) for infile in input_files: gi = generate_gamess_input(**inputs[infile]) check_vs_serial_reference(gi,infile) #end for restore_nexus()
def test_settings(): # test full imports import os from nexus import settings, Settings, obj from nexus_base import nexus_core, nexus_core_defaults from nexus_base import nexus_noncore, nexus_noncore_defaults from nexus_base import nexus_core_noncore, nexus_core_noncore_defaults from pseudopotential import Pseudopotentials from basisset import BasisSets from machines import Job, Workstation from project_manager import ProjectManager from gamess import Gamess from pwscf import Pwscf from quantum_package import QuantumPackage testing.check_final_state() tpath = testing.setup_unit_test_output_directory('settings', 'test_settings') # divert logging function divert_nexus() def aux_defaults(): # check that Job and ProjectManager settings are at default values assert (Job.machine is None) assert (ProjectManager.machine is None) # check that Gamess, Pwscf, and Quantum Package settings are at default values assert (Gamess.ericfmt is None) assert (Gamess.mcppath is None) assert (Pwscf.vdw_table is None) assert (QuantumPackage.qprc is None) #end def aux_defaults def check_settings_core_noncore(): nckeys_check = set([ 'command_line', 'debug', 'dependent_modes', 'emulate', 'file_locations', 'generate_only', 'graph_sims', 'indent', 'load_images', 'local_directory', 'mode', 'modes', 'monitor', 'primary_modes', 'progress_tty', 'pseudo_dir', 'pseudopotentials', 'remote_directory', 'results', 'runs', 'skip_submit', 'sleep', 'stages', 'stages_set', 'status', 'status_modes', 'status_only', 'trace', 'verbose' ]) nnckeys_check = set( ['basis_dir', 'basissets', 'pseudo_dir', 'pseudopotentials']) setkeys_check = set([ 'command_line', 'basis_dir', 'basissets', 'debug', 'dependent_modes', 'emulate', 'file_locations', 'generate_only', 'graph_sims', 'indent', 'load_images', 'local_directory', 'mode', 'modes', 'monitor', 'primary_modes', 'progress_tty', 'pseudo_dir', 'pseudopotentials', 'remote_directory', 'results', 'runs', 'skip_submit', 'sleep', 'stages', 'stages_set', 'status', 'status_modes', 'status_only', 'trace', 'verbose' ]) setkeys_allowed = setkeys_check | Settings.allowed_vars nckeys = set(nexus_core.keys()) nnckeys = set(nexus_noncore.keys()) setkeys = set(settings.keys()) assert (nckeys == nckeys_check) assert (nnckeys == nnckeys_check) assert (setkeys >= setkeys_check) assert (setkeys <= setkeys_allowed) pairs = [(settings, nexus_core), (settings, nexus_noncore), (nexus_core, nexus_noncore)] for o1, o2 in pairs: shared_keys = set(o1.keys()) & set(o2.keys()) for k in shared_keys: v1 = o1[k] v2 = o2[k] if isinstance(v1, obj): assert (object_eq(v1, v2)) else: assert (value_eq(v1, v2)) #end if #end for #end for #end check_settings_core_noncore def check_empty_settings(): settings(command_line=False, ) settings.command_line = True nexus_core.command_line = True check_settings_core_noncore() # nexus core sets basic run stages and Pseudopotentials object assert (nexus_core.stages_set == set( nexus_core_defaults.primary_modes)) assert (isinstance(nexus_core.pseudopotentials, Pseudopotentials)) assert (len(nexus_core.pseudopotentials) == 0) nexus_core.stages_set = set() nexus_core.stages = [] nexus_core.pseudopotentials = None assert (object_eq(nexus_core, nexus_core_defaults)) # nexus noncore sets Pseudopotentials and BasisSets objects assert (isinstance(nexus_noncore.pseudopotentials, Pseudopotentials)) assert (len(nexus_noncore.pseudopotentials) == 0) assert (isinstance(nexus_noncore.basissets, BasisSets)) assert (len(nexus_noncore.basissets) == 0) nnc_defaults = obj(nexus_noncore_defaults, nexus_core_noncore_defaults) nexus_noncore.pseudopotentials = None nexus_noncore.basissets = None assert (object_eq(nexus_noncore, nnc_defaults)) # other settings objects should be at default also aux_defaults() #end def_check_empty_settings # check that core settings are at default values assert (object_eq(nexus_core, nexus_core_defaults)) assert (object_eq(nexus_noncore, nexus_noncore_defaults)) assert (object_eq(nexus_core_noncore, nexus_core_noncore_defaults)) aux_defaults() # core settings remain almost at default with empty settings check_empty_settings() # check that a few basic user settings are applied appropriately cwd = os.getcwd() os.chdir(tpath) dft_pseudos = ['Ni.opt.upf', 'O.opt.upf'] qmc_pseudos = ['Ni.opt.xml', 'O.opt.xml'] pseudos = dft_pseudos + qmc_pseudos pseudo_path = './pseudopotentials' if not os.path.exists(pseudo_path): os.makedirs(pseudo_path) for file in pseudos: filepath = os.path.join(pseudo_path, file) if not os.path.exists(filepath): open(filepath, 'w').close() #end if #end for #end if settings( pseudo_dir=pseudo_path, status_only=0, generate_only=1, machine='ws16', command_line=False, ) check_settings_core_noncore() assert (nexus_core.status_only == 0) assert (nexus_core.generate_only == 1) assert (nexus_core.pseudo_dir == './pseudopotentials') assert (len(nexus_core.pseudopotentials) == 4) assert (set(nexus_core.pseudopotentials.keys()) == set(pseudos)) assert (settings.machine == 'ws16') assert (Job.machine == 'ws16') assert (isinstance(ProjectManager.machine, Workstation)) assert (ProjectManager.machine.name == 'ws16') os.chdir(cwd) # check that a new empty settings works following basic check_empty_settings() # restore logging function restore_nexus()