def test_symlink_pseudopotentials(): """ symlinks pseudopotentials into data/ directory and compares to original pseudopotential files. Obviously they'll be the same but this makes sure the file operations work """ with tools_for_tests.TemporaryDirectory() as tmp_dir: # create object to test pp_path_list = [test_inputs_dir+'/PAW.Si', test_inputs_dir+'/PAW.Ge'] argvf_template_path = '' crystal_template_path = '' correct_argvf = '' correct_crystal = '' pos = [] gcut = -1 testrun = eval_pp.DftRun(pp_path_list, argvf_template_path, crystal_template_path, pos, gcut) # call _symlink_pseudopotentials method os.mkdir('data') testrun._symlink_pseudopotentials() # check pseudopotential files have been copied to data/ with open('data/PAW.Si') as f1, open(pp_path_list[0]) as f2: assert f1.read() == f2.read() with open('data/PAW.Ge') as f1, open(pp_path_list[1]) as f2: assert f1.read() == f2.read()
def test_eval_pp_main_no_converge(): """ raises NoCutoffConvergence if there is no gcut convergence """ test_inputs_dir = os.path.join(main_test_inputs_dir, 'eval_pp_main_test') with pytest.raises(eval_pp.NoCutoffConvergence): with tools_for_tests.TemporaryDirectory() as tmp_dir: # set up a mock work directory: shutil.copy(os.path.join('..', 'calc_nflops'), os.getcwd()) shutil.copy( os.path.join(test_inputs_dir, 'configurations.in.example'), 'configurations.in') shutil.copy( os.path.join(test_inputs_dir, 'allelectron_forces.dat.example'), 'allelectron_forces.dat') os.mkdir('workdir.example') os.chdir('workdir.example') shutil.copy(os.path.join(test_inputs_dir, 'argvf.template'), 'argvf.template') shutil.copy(os.path.join(test_inputs_dir, 'crystal.template'), 'crystal.template') shutil.copy(os.path.join(test_inputs_dir, 'PAW.Si'), 'PAW.Si') shutil.copy(os.path.join(test_inputs_dir, 'PAW.Ge'), 'PAW.Ge') # run eval_pp gcuts = [20., 30., 40.] energy_tol = 1.e-100 # set impossible tolerance so it doesn't converge objectives = eval_pp.main(['Si', 'Ge'], gcuts, energy_tol)
def test_get_dft_results_at_gcut_socorrofail(): """ Reads energies and forces correctly frm socorro diaryf files for every run at corrent gcut """ with tools_for_tests.TemporaryDirectory() as tmp_dir: # set up two mock dft runs main_pp_path_list = [] main_argvf_template_path = '' main_crystal_template_path = '' pos = [] gcut = -1 run1 = eval_pp.DftRun(main_pp_path_list, main_argvf_template_path, main_crystal_template_path, pos, gcut) mock_run_dir_1 = 'dir_1' os.mkdir(mock_run_dir_1) run1.run_dir = mock_run_dir_1 run2 = eval_pp.DftRun(main_pp_path_list, main_argvf_template_path, main_crystal_template_path, pos, gcut) mock_run_dir_2 = 'dir_2' os.mkdir(mock_run_dir_2) run2.run_dir = mock_run_dir_2 # move mock diaryf files to run dirs shutil.copy(os.path.join(test_inputs_dir, 'diaryf.test_get_dft_results_1'), os.path.join(mock_run_dir_1, 'diaryf')) shutil.copy(os.path.join(test_inputs_dir, 'diaryf.test_get_dft_results_none'), os.path.join(mock_run_dir_2, 'diaryf')) with pytest.raises(eval_pp.SocorroFail): _,_ = eval_pp.get_dft_results_at_gcut([run1, run2])
def test_create_all_pseudopotentials(): """ Run in a directory with multiple atompaw input files, builds each PAW in its own directory. Compares created PAW to a correct pseudopotential. Assumes atompaw v4 right now. I can generalize this later if needed, or just check that atompaw is called and not that the output is correct. This test is fragile because output can easily change between atompaw versions. """ with tools_for_tests.TemporaryDirectory() as tmp_dir: shutil.copy(os.path.join(test_inputs_dir, 'Si.in'), os.getcwd()) shutil.copy(os.path.join(test_inputs_dir, 'Ge.in'), os.getcwd()) element_list = ['Si', 'Ge'] is_successful = analysis_driver.create_all_pseudopotentials( element_list) assert is_successful Si_file_correct = os.path.join(test_inputs_dir, 'PAW.Si.correct') with open(Si_file_correct) as f1, open('PAW.Si') as f2: assert f1.read()[0:50] == f2.read()[0:50] Ge_file_correct = os.path.join(test_inputs_dir, 'PAW.Ge.correct') with open(Ge_file_correct) as f1, open('PAW.Ge') as f2: assert f1.read()[0:50] == f2.read()[0:50]
def test_run_socorro(): """ Set up socorro input files, run socorro, check force/energy results note: this might fail if socorro versions change because different versions could give different reults even with the same input, especially since total E is relative. Could I mock socorro instead? Alternatively, I could run socorro twice, the second time with pre- setup input files, and compare results """ inputs = test_inputs_dir + '/SiGe_single_run' with tools_for_tests.TemporaryDirectory() as tmp_dir: # create object to test pp_path_list = [inputs + '/PAW.Si', inputs + '/PAW.Ge'] argvf_template_path = inputs + '/argvf.template' crystal_template_path = inputs + '/crystal.template' pos = [[0.0, 0, '0.1'], [0.24, 0.25, 0.26]] testrun = eval_pp.DftRun(pp_path_list, argvf_template_path, crystal_template_path, pos, 10.) testrun.setup_files() with open('socorro.log', 'w') as fout: p = testrun.run_socorro(fout) p.wait() #assert force and energy reults correct_energy = -312.593586340 correct_forces = np.array( [0.246519, 0.247743, 0.243064, -0.246519, -0.247743, -0.243064]) assert np.isclose(testrun.read_energy(), correct_energy) assert np.isclose(testrun.read_forces(), correct_forces, atol=1.e-5).all()
def test_analysis_driver_main_Si_noconverge(): """ For this test, the silicon inputs are bad so atompaw does not converge, and the analysis driver returns 100s for both objectives """ test_inputs_dir = os.path.join(main_test_inputs_dir, 'analysis_driver_main_Si_noconverge') with tools_for_tests.TemporaryDirectory() as tmp_dir: # set up a mock work directory: shutil.copy(os.path.join('..', 'calc_nflops'), os.getcwd()) shutil.copy(os.path.join(test_inputs_dir, 'opal.in'), 'opal.in') shutil.copy(os.path.join(test_inputs_dir, 'configurations.in.example'), 'configurations.in') shutil.copy( os.path.join(test_inputs_dir, 'allelectron_forces.dat.example'), 'allelectron_forces.dat') os.mkdir('workdir.example') os.chdir('workdir.example') shutil.copy(os.path.join(test_inputs_dir, 'argvf.template'), 'argvf.template') shutil.copy(os.path.join(test_inputs_dir, 'crystal.template'), 'crystal.template') shutil.copy(os.path.join(test_inputs_dir, 'Si.in.template'), os.getcwd()) shutil.copy(os.path.join(test_inputs_dir, 'Ge.in.template'), os.getcwd()) shutil.copy(os.path.join(test_inputs_dir, 'params'), os.getcwd()) # run analysis driver analysis_driver.main() with open('results') as fin: assert fin.readlines() == [ ' 1.0000000000000000E+02 accu\n', ' 1.0000000000000000E+02 work\n' ]
def test_analysis_driver_main_nogcut_converge(): """ returns proper obectives of 95 when no gcut convergence sets impossible energy tolerance in opal.in """ test_inputs_dir = os.path.join(main_test_inputs_dir, 'analysis_driver_main_nogcut_converge') with tools_for_tests.TemporaryDirectory() as tmp_dir: # set up a mock work directory: shutil.copy(os.path.join('..', 'calc_nflops'), os.getcwd()) shutil.copy(os.path.join(test_inputs_dir, 'opal.in'), 'opal.in') shutil.copy(os.path.join(test_inputs_dir, 'configurations.in.example'), 'configurations.in') shutil.copy( os.path.join(test_inputs_dir, 'allelectron_forces.dat.example'), 'allelectron_forces.dat') os.mkdir('workdir.example') os.chdir('workdir.example') shutil.copy(os.path.join(test_inputs_dir, 'argvf.template'), 'argvf.template') shutil.copy(os.path.join(test_inputs_dir, 'crystal.template'), 'crystal.template') shutil.copy(os.path.join(test_inputs_dir, 'Si.in.template'), os.getcwd()) shutil.copy(os.path.join(test_inputs_dir, 'Ge.in.template'), os.getcwd()) shutil.copy(os.path.join(test_inputs_dir, 'params'), os.getcwd()) # run analysis driver analysis_driver.main() with open('results') as fin: assert fin.readlines() == [ ' 9.5000000000000000E+01 accu\n', ' 9.5000000000000000E+01 work\n' ]
def test_setup_files(): """ test: setup_dir method makes files for socorro run Expected behavior: calling setup_dir method will create correctly preprocessed argvf file in current directory, directory named data/ and correctly preprocessed data/crystal file. """ correct_argvf = test_inputs_dir+'/argvf.example1' correct_crystal = test_inputs_dir+'/crystal.example1' correct_Si = test_inputs_dir+'/PAW.Si' correct_Ge = test_inputs_dir+'/PAW.Ge' with tools_for_tests.TemporaryDirectory() as tmp_dir: # create object to test pp_path_list = [test_inputs_dir+'/PAW.Si', test_inputs_dir+'/PAW.Ge'] argvf_template_path = test_inputs_dir+'/argvf.template.example1' crystal_template_path = test_inputs_dir+'/crystal.template.example1' pos = [[0.0, 0, '0.1'], [0.5, 0.6, 0.7]] testrun = eval_pp.DftRun(pp_path_list, argvf_template_path, crystal_template_path, pos, 30.) testrun.setup_files() # compare preprocessed files with correct file examples with open('argvf') as f1, open(correct_argvf) as f2: assert f1.read() == f2.read() with open('data/crystal') as f1, open(correct_crystal) as f2: assert map(str.split, f1.readlines()) == map(str.split, f2.readlines()) with open('data/PAW.Si') as f1, open(correct_Si) as f2: assert f1.read() == f2.read() with open('data/PAW.Ge') as f1, open(correct_Ge) as f2: assert f1.read() == f2.read()
def test_analysis_driver_main_success(): """ """ test_inputs_dir = os.path.join(main_test_inputs_dir, 'analysis_driver_main_success') with tools_for_tests.TemporaryDirectory() as tmp_dir: # set up a mock work directory: shutil.copy(os.path.join('..', 'calc_nflops'), os.getcwd()) shutil.copy(os.path.join(test_inputs_dir, 'opal.in'), 'opal.in') shutil.copy(os.path.join(test_inputs_dir, 'configurations.in.example'), 'configurations.in') shutil.copy( os.path.join(test_inputs_dir, 'allelectron_forces.dat.example'), 'allelectron_forces.dat') os.mkdir('workdir.example') os.chdir('workdir.example') shutil.copy(os.path.join(test_inputs_dir, 'argvf.template'), 'argvf.template') shutil.copy(os.path.join(test_inputs_dir, 'crystal.template'), 'crystal.template') shutil.copy(os.path.join(test_inputs_dir, 'Si.in.template'), os.getcwd()) shutil.copy(os.path.join(test_inputs_dir, 'Ge.in.template'), os.getcwd()) shutil.copy(os.path.join(test_inputs_dir, 'params'), os.getcwd()) # run analysis driver analysis_driver.main() with open('results') as fin: assert fin.readlines() == [ ' 7.6992177462473416E-02 accu\n', ' 8.7573645819723784E-03 work\n' ]
def test_create_all_pseudopotentials_fail(): with tools_for_tests.TemporaryDirectory() as tmp_dir: shutil.copy(os.path.join(test_inputs_dir, 'Si.in.wontconverge'), 'Si.in') shutil.copy(os.path.join(test_inputs_dir, 'Ge.in'), os.getcwd()) element_list = ['Si', 'Ge'] is_successful = analysis_driver.create_all_pseudopotentials( element_list) assert is_successful is False
def test_get_energy_none(): """ should return None because no cell energy found in socorro output """ with tools_for_tests.TemporaryDirectory() as tmp_dir: # set up fake dft_run pp_path_list = [] argvf_template_path = '' crystal_template_path = '' pos = [] gcut = -1 testrun = eval_pp.DftRun(pp_path_list, argvf_template_path, crystal_template_path, pos, gcut) energy_in = testrun.read_energy(test_inputs_dir+'/diaryf.test_get_energy_none') assert energy_in is None
def test_get_energy(): """ Read energy from example socorro ouput and check value is correct. Run inside temporary directory. """ with tools_for_tests.TemporaryDirectory() as tmp_dir: # set up fake dft_run pp_path_list = [] argvf_template_path = '' crystal_template_path = '' pos = [] gcut = -1 testrun = eval_pp.DftRun(pp_path_list, argvf_template_path, crystal_template_path, pos, gcut) energy_in = testrun.read_energy(test_inputs_dir+'/diaryf.test_get_energy') assert isclose(energy_in, -738.821147137)
def test_get_forces_none(): """ Read forces from example socorro ouput and check value is correct. Run inside temporary directory. """ with tools_for_tests.TemporaryDirectory() as tmp_dir: # set up fake dft_run pp_path_list = [] argvf_template_path = '' crystal_template_path = '' pos = [] gcut = -1 testrun = eval_pp.DftRun(pp_path_list, argvf_template_path, crystal_template_path, pos, gcut) forces_in = testrun.read_forces(test_inputs_dir+'/diaryf.test_get_forces_none') assert forces_in is None
def test_get_forces(): """ Read forces from example socorro ouput and check value is correct. Run inside temporary directory. """ with tools_for_tests.TemporaryDirectory() as tmp_dir: # set up fake dft_run pp_path_list = [] argvf_template_path = '' crystal_template_path = '' pos = [] gcut = -1 testrun = eval_pp.DftRun(pp_path_list, argvf_template_path, crystal_template_path, pos, gcut) forces_in = testrun.read_forces(test_inputs_dir+'/diaryf.test_get_forces') correct_forces = np.array([0.007170, -0.015092, -0.069756, -0.007170, 0.015092, 0.069756]) assert np.isclose(forces_in, correct_forces, rtol=1e-9, atol=0.0).all()
def test_preprocess_pseudopotential_input_files(): """ Test preprocessing files with dprepro and related file management. """ with tools_for_tests.TemporaryDirectory() as tmp_dir: # preprocess files element_list = ['Si', 'Ge'] shutil.copyfile(os.path.join(test_inputs_dir, 'params.example'), os.path.join(os.getcwd(), 'params')) analysis_driver.preprocess_pseudopotential_input_files( element_list, test_inputs_dir) # compare with correct ouptut correct_Si_file = os.path.join(test_inputs_dir, 'Si.in.correct') with open(correct_Si_file) as f1, open('Si.in') as f2: assert f1.readlines() == f2.readlines() correct_Ge_file = os.path.join(test_inputs_dir, 'Ge.in.correct') with open(correct_Ge_file) as f1, open('Ge.in') as f2: assert f1.readlines() == f2.readlines()
def test_run_atompaw(): """ create single pseudopotential in current directory Assumes atompaw v4 right now. I can generalize this later if needed, or just check that atompaw is called and not that the output is correct. This test is fragile because output can easily change between atompaw versions. """ with tools_for_tests.TemporaryDirectory() as tmp_dir: input_file_name = os.path.join(test_inputs_dir, 'Si.in.example') analysis_driver.run_atompaw(input_file_name) correct_file = os.path.join(test_inputs_dir, 'Si.SOCORRO.atomicdata.correct') with open(correct_file) as f1, open('Si.SOCORRO.atomicdata') as f2: a = f1.read() b = f2.read() assert a[0:50] == b[ 0:50] # for some reason pytest hangs with the full text
def test_eval_pp_main(): """ This should converge at gcut=40 and then return objectives: accu = 0.12408939054384546 work = 0.009064640532217023 the "correct" accuracy objectives could depend on the socorro build, and the work objective may depend on some other things such as parallelization. """ test_inputs_dir = os.path.join(main_test_inputs_dir, 'eval_pp_main_test') with tools_for_tests.TemporaryDirectory() as tmp_dir: # set up a mock work directory: shutil.copy(os.path.join('..', 'calc_nflops'), os.getcwd()) shutil.copy(os.path.join(test_inputs_dir, 'configurations.in.example'), 'configurations.in') shutil.copy( os.path.join(test_inputs_dir, 'allelectron_forces.dat.example'), 'allelectron_forces.dat') os.mkdir('workdir.example') os.chdir('workdir.example') shutil.copy(os.path.join(test_inputs_dir, 'argvf.template'), 'argvf.template') shutil.copy(os.path.join(test_inputs_dir, 'crystal.template'), 'crystal.template') shutil.copy(os.path.join(test_inputs_dir, 'PAW.Si'), 'PAW.Si') shutil.copy(os.path.join(test_inputs_dir, 'PAW.Ge'), 'PAW.Ge') # run eval_pp gcuts = [20., 30., 40., 50.] energy_tol = 3.e-3 objectives = eval_pp.main(['Si', 'Ge'], gcuts, energy_tol) assert np.isclose(objectives['accu'], 0.12408939054384546, rtol=0., atol=0.0002) assert np.isclose(objectives['work'], 0.009064640532217023, rtol=0., atol=0.000001)
def test_get_dft_results_at_gcut(): """ Reads energies and forces correctly frm socorro diaryf files for every run at corrent gcut """ with tools_for_tests.TemporaryDirectory() as tmp_dir: # set up two mock dft runs main_pp_path_list = [] main_argvf_template_path = '' main_crystal_template_path = '' pos = [] gcut = -1 run1 = eval_pp.DftRun(main_pp_path_list, main_argvf_template_path, main_crystal_template_path, pos, gcut) mock_run_dir_1 = 'dir_1' os.mkdir(mock_run_dir_1) run1.run_dir = mock_run_dir_1 run2 = eval_pp.DftRun(main_pp_path_list, main_argvf_template_path, main_crystal_template_path, pos, gcut) mock_run_dir_2 = 'dir_2' os.mkdir(mock_run_dir_2) run2.run_dir = mock_run_dir_2 # move mock diaryf files to run dirs shutil.copy(os.path.join(test_inputs_dir, 'diaryf.test_get_dft_results_1'), os.path.join(mock_run_dir_1, 'diaryf')) shutil.copy(os.path.join(test_inputs_dir, 'diaryf.test_get_dft_results_2'), os.path.join(mock_run_dir_2, 'diaryf')) dft_results = eval_pp.get_dft_results_at_gcut([run1, run2]) correct_energy_list = [-20000., -738.821147137] correct_forces_1 = np.array([0.1, -20.0, -0.08, -1000., 0.3, 0.9]) correct_forces_2 = np.array([0.007170, -0.015092, -0.069756, -0.007170, 0.015092, 0.069756]) assert np.isclose(dft_results['energies'], correct_energy_list).all() assert np.isclose(dft_results['forces'][0], correct_forces_1).all() assert np.isclose(dft_results['forces'][1], correct_forces_2).all()
def test_position_sweep(): """ Four instances of socorro run in parallel, results correct Sets up four DftRun instances with different atomic coordinates. The socorro runs should run simulataneously in different threads. note: this might fail if socorro versions change because different versions could give different reults even with the same input, especially since total E is relative. Could I mock socorro instead? Alternatively, I could run socorro twice, the second time with pre- setup input files, and compare results """ inputs = test_inputs_dir + '/SiGe_single_run' with tools_for_tests.TemporaryDirectory() as tmp_dir: positions = [[[0.0, 0.0, 0.1], [0.24, 0.25, 0.26]], [[0.0, 0.0, 0.0], [0.50, 0.50, 0.50]], [[0.0, 0.0, 0.0], [0.25, 0.25, 0.25]], [[0.1, 0.0, 0.0], [0.49, 0.50, 0.51]]] # create objects to test pp_path_list = [inputs + '/PAW.Si', inputs + '/PAW.Ge'] argvf_template_path = inputs + '/argvf.template' crystal_template_path = inputs + '/crystal.template' testruns = [] for pos in positions: testruns.append( eval_pp.DftRun(pp_path_list, argvf_template_path, crystal_template_path, pos, 10.)) # run position_sweep eval_pp.position_sweep(testruns) # get results from socorro runs energies = [] forces = [] for run in testruns: # get results and append to results array os.chdir(run.run_dir) energies.append(run.read_energy()) forces.append(run.read_forces()) os.chdir(tmp_dir) print energies print forces # correct results correct_en = [ -315.52682125, -314.613501131, -315.33638574, -314.797573776 ] correct_forces = [ np.array([ 0.337666, 0.218549, 0.321221, -0.337666, -0.218549, -0.321221 ]), np.array([0., -0., -0., -0., 0., 0.]), np.array([-0., 0., -0., 0., 0., 0.]), np.array( [0.29504, 0.341381, 0.008144, -0.29504, -0.341381, -0.008144]) ] assert np.isclose(energies, correct_en).all() assert np.isclose(forces, correct_forces, atol=1.e-5).all()