def test_closed(mo_dirpath, delete_tmp_dir): with temp_dir(delete_tmp_dir): for f in ["mos", "control"]: shutil.copy2(os.path.join(mo_dirpath, f), f) s = States.from_file() assert len(s) == 14 assert s[0].eigenvalue == pytest.approx(-13.968369531218) assert_MSONable(s) eiger_out = EigerOutput.from_file(os.path.join(mo_dirpath, "eiger")) assert eiger_out.compare_states(s) is None assert not s.is_uhf assert not s.has_hole assert s.total_electrons == 10 assert s.irreps[0] == "a1" assert s.occupations[0] == 2 assert s.spins[0] is None assert s.eigenvalues[-1] == pytest.approx(2.55164050421) assert s.irrep_indices[0] == 1 assert str(s) # test filter s = States.from_file() with pytest.raises(ValueError): s.filter_states(spin="a", irrep="a1") assert len(s.filter_states(irrep="z")) == 0 filtered = s.filter_states(irrep="a1") assert filtered[0].irrep == "a1" # test get_shells s = States.from_file() with pytest.raises(ValueError): s.get_shells(spin="a") shell = s.get_shells() assert shell.occupations[0] == 2 assert shell.states[0] == ("a1", 1)
def test_remove_gradient_in_control(self, testdir, delete_tmp_dir): with temp_dir(delete_tmp_dir): shutil.copy2( os.path.join(testdir, 'control', 'control_test-gradient'), "control") c = Control.from_file() with pytest.raises(ValueError): c.remove_last_gradient(filename=None) c.remove_last_gradient(backup_suffix="_backup") assert os.path.isfile("control_backup") g = c.gradient assert len(g.gradients) == 0 with pytest.raises(RuntimeError): c.remove_last_gradient()
def test_remove_energy_in_control(self, testdir, delete_tmp_dir): with temp_dir(delete_tmp_dir): shutil.copy2( os.path.join(testdir, 'control', 'control_test-energy'), "control") c = Control.from_file() with pytest.raises(ValueError): c.remove_last_energy(filename=None) c.remove_last_energy(backup_suffix="_backup") assert os.path.isfile("control_backup") e = c.energy assert len(e.scf) == 0 with pytest.raises(RuntimeError): c.remove_last_energy()
def test_mdgo(self, control_filepath, delete_tmp_dir): with temp_dir(delete_tmp_dir): shutil.copy2(control_filepath, "control") options = { "internal": None, "redundant": None, "cartesian": "cartesian off", "global": None } mdgo("optimize", options, backup_file="control_backup") assert os.path.isfile("control_backup") with open("control") as f: s = f.read() matches = re.findall(r"cartesian off", s, re.MULTILINE) assert matches == ["cartesian off"] matches = re.findall(r"\$redundant", s, re.MULTILINE) assert matches == []
def test_cpc(self, molecule_filepath, delete_tmp_dir): with temp_dir(delete_tmp_dir) as tmp_dir: dp = get_define_template("ridft") run_define_runner(define_opt=dp, mol_path=molecule_filepath) run_tm("ridft") process = subprocess.Popen(["cpc", "cpc_orig"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8') process.communicate() ret_code = process.wait() if ret_code: raise RuntimeError("original cpc failed with return code {}".format(ret_code)) cpc("cpc_tmio") s_orig = set(os.listdir("cpc_orig")) s_orig.remove("wherefrom") s_tmio = set(os.listdir("cpc_tmio")) assert s_orig == s_tmio
def test_gradient(self, testdir, delete_tmp_dir): with temp_dir(delete_tmp_dir): shutil.copy2( os.path.join(testdir, 'control', 'gradient_test-gradient'), 'gradient') g = Gradient.from_file() assert len(g.molecules) == 6 assert g.gradients[0, 1, 0] == pytest.approx(-0.02705094211381) assert g.energies[-1] == pytest.approx(-76.3449460148) assert g.norms[1] == pytest.approx(0.013946816251742454) assert g.max_gradients[-1] == pytest.approx(3.8028925027729e-05) assert g.n_steps == 6 assert g.last_grad_norm == pytest.approx(5.145114095400708e-05) assert g.last_grad_max == pytest.approx(3.8028925027729e-05) assert_MSONable(g) if has_matplotlib(): assert g.plot(show=False)
def test_eiger_runner(mo_dirpath, delete_tmp_dir): with mock.patch("subprocess.run") as mock_run: er = EigerRunner() with pytest.raises(ValueError): er.to_file("test") with pytest.raises(ValueError): er.get_eiger_output() with open(os.path.join(mo_dirpath, "eiger")) as f: out = f.read() m = mock.MagicMock() m.stdout = out.encode("utf-8") mock_run.return_value = m er.run() assert er.get_eiger_output() with temp_dir(delete_tmp_dir): er.to_file("out_test") assert os.path.isfile("out_test")
def test_adg(self, control_filepath, delete_tmp_dir): with temp_dir(delete_tmp_dir): shutil.copy2(control_filepath, "control") with open("control") as f: s = f.read() matches = re.findall(r"\$scfconv 6", s, re.MULTILINE) assert matches == [] adg("scfconv", "6", backup_file="control_backup") assert os.path.isfile("control_backup") with open("control") as f: s = f.read() matches = re.findall(r"\$scfconv 6", s, re.MULTILINE) assert matches == ["$scfconv 6"] with open("control_backup") as f: s = f.read() matches = re.findall(r"\$scfconv 6", s, re.MULTILINE) assert matches == []
def test_energy_scf(self, testdir, delete_tmp_dir): with temp_dir(delete_tmp_dir): shutil.copy2( os.path.join(testdir, 'control', 'energy_test-energy'), 'energy') energy = Energy.from_file() assert len(energy.scf) == 4 assert energy.n_steps == 4 assert energy.scf[0] == pytest.approx(-10.48350884610) assert energy.scfkin[2] == pytest.approx(37.86647200156) assert energy.scfpot[1] == pytest.approx(-59.35016151450) assert energy.delta_e == pytest.approx(9.99999999577) assert energy.mp2 is None assert np.all(energy.scf == energy.total) assert_MSONable(energy) if has_matplotlib(): assert energy.plot(show=False) energy_string = ' SCF SCFKIN SCFPOT\n' \ '1 -10.48350884610 39.86550149370 -50.34901033980\n' \ '2 -20.48350872544 38.86665278906 -59.35016151450\n' \ '\n' \ '3 -30.48350875500 37.86647200156 -68.34998075656\n' \ '4 -40.48350875077 36.86649728669 -77.35000603747\n' energy = Energy.from_string(energy_string) assert len(energy.scf) == 4 assert energy.n_steps == 4 assert energy.scf[0] == pytest.approx(-10.48350884610) assert energy.scfkin[2] == pytest.approx(37.86647200156) assert energy.scfpot[1] == pytest.approx(-59.35016151450) assert energy.delta_e == pytest.approx(9.99999999577) assert energy.mp2 is None assert np.all(energy.scf == energy.total) assert_MSONable(energy) if has_matplotlib(): assert energy.plot(show=False)
def test_cpc(self, control_filepath, testdir, delete_tmp_dir): with temp_dir(delete_tmp_dir): os.makedirs("orig_dir") shutil.copy2( os.path.join(testdir, 'control', 'control_test-subfiles'), os.path.join("orig_dir", "control")) shutil.copy2( os.path.join(testdir, 'control', 'gradient_test-gradient'), os.path.join("orig_dir", "gradient")) cpc("test_dir", control_dir="orig_dir") assert os.path.isfile(os.path.join("test_dir", "control")) assert os.path.isfile(os.path.join("test_dir", "gradient")) shutil.copy2( os.path.join(testdir, 'control', 'control_test-subfiles'), "control") cpc("test_dir2") assert os.path.isfile(os.path.join("test_dir2", "control"))
def test_closed(self, testdir, delete_tmp_dir): with temp_dir(delete_tmp_dir): shutil.copy2( os.path.join(testdir, 'control', 'control_test-subfiles'), "control") with pytest.raises(ValueError): Shells.from_file("alpha") s = Shells.from_file("closed") assert len(s.states) == 3 assert s.states[0] == ("a1", 1) assert s.occupations[0] == 2 assert "a1" in s.irreps assert "t2" in s.irreps assert len(s.irreps) == 2 assert s.total_electrons == 10 original = Control.from_file().sdg("closed shells") generated = s.to_datagroup() assert check_equivalent_dg(original, generated) assert_MSONable(s)
def test_distance_no_val(self, molecule, control_filepath, delete_tmp_dir): ms = MoleculeSystem(molecule) ms.add_distance(0, 1, weights=1.0, value=None) assert not ms.has_inconsistencies() with temp_dir(delete_tmp_dir) as tmp_dir: ms.to_file("coord", fmt="coord") shutil.copy2(control_filepath, "control") dr = DefineRunner(parameters=dr_parameters) assert dr.run_update_internal_coords() ms_new = MoleculeSystem.from_file("coord") assert not ms_new.has_inconsistencies() # check that intdef is present and that the value has been updated assert len(ms_new.int_def) == 1 assert ms_new.int_def[0].value == pytest.approx(2.05846, abs=1e-4) # check that in redundant the coordinate has been taken into account dg = DataGroups.from_file("coord") redundant = dg.sdg("redundant", strict=True) assert re.search(r"\d+\s+f\s+1\.0+\s+stre\s+1\s+2\s+val=\s*2\.05", redundant)
def test_kdg(self, control_filepath, delete_tmp_dir): with temp_dir(delete_tmp_dir): shutil.copy2(control_filepath, "control") with open("control") as f: s = f.read() matches = re.findall(r"\$operating system unix", s, re.MULTILINE) assert matches == ["$operating system unix"] kdg("operating system", backup_file="control_backup") assert os.path.isfile("control_backup") with open("control") as f: s = f.read() matches = re.findall(r"\$operating system unix", s, re.MULTILINE) assert matches == [] with open("control_backup") as f: s = f.read() matches = re.findall(r"\$operating system unix", s, re.MULTILINE) assert matches == ["$operating system unix"]
def test_cpc(self, testdir, delete_tmp_dir): with temp_dir(delete_tmp_dir): c_empty = Control.empty() c_empty.cpc("test_dir") assert os.path.isfile(os.path.join("test_dir", "control")) shutil.copy2( os.path.join(testdir, 'control', 'control_test-subfiles'), "control") shutil.copy2( os.path.join(testdir, 'control', 'gradient_test-gradient'), "gradient") c = Control.from_file("control") c.cpc("test_dir") c_test = Control.from_file(os.path.join("test_dir", "control")) # check that did not overwrite assert len(c_test.dg_list) == 1 assert os.path.isfile(os.path.join("test_dir", "gradient")) c.cpc("test_dir", force_overwrite=True) c_test = Control.from_file(os.path.join("test_dir", "control")) assert len(c_test.dg_list) > 1
def test_sdgo(self, control_filepath, delete_tmp_dir): with temp_dir(delete_tmp_dir): shutil.copy2(control_filepath, "control") assert sdgo("$optimize", "internal") == " off"
def test_touch_file(self, delete_tmp_dir): with temp_dir(delete_tmp_dir) as tmp_dir: touch_file("test_file") assert os.path.isfile("test_file")
def test_temp_dir(self): # test that the file is not deleted if delete=False # manually delete the file after testing with temp_dir(delete=False) as tmp_dir: assert tmp_dir == os.getcwd() shutil.rmtree(tmp_dir)
def test_sdg(self, control_filepath, delete_tmp_dir): with temp_dir(delete_tmp_dir): shutil.copy2(control_filepath, "control") assert sdg("operating system") == " unix\n"
def test_uhf(mo_dirpath, delete_tmp_dir): with temp_dir(delete_tmp_dir): for f in ["alpha", "beta", "control"]: shutil.copy2(os.path.join(mo_dirpath, f), f) s = States.from_file() assert len(s) == 28 assert s.n_states == 28 assert s[0].eigenvalue == pytest.approx(-14.457427310212) assert_MSONable(s) eiger_out = EigerOutput.from_file(os.path.join(mo_dirpath, "eiger")) eiger_wrong_val = EigerOutput.from_file( os.path.join(mo_dirpath, "eiger_wrong_val")) eiger_wrong_length = EigerOutput.from_file( os.path.join(mo_dirpath, "eiger_wrong_length")) eiger_wrong_gap = EigerOutput.from_file( os.path.join(mo_dirpath, "eiger_wrong_gap")) eiger_wrong_nel = EigerOutput.from_file( os.path.join(mo_dirpath, "eiger_wrong_nelec")) eiger_wrong_occ = EigerOutput.from_file( os.path.join(mo_dirpath, "eiger_wrong_occ")) eiger_wrong_match = EigerOutput.from_file( os.path.join(mo_dirpath, "eiger_wrong_match")) assert eiger_out.compare_states(s) is None assert "gap" in eiger_wrong_gap.compare_states(s) assert "states" in eiger_wrong_length.compare_states(s) assert "eigenvalue" in eiger_wrong_val.compare_states(s) assert "electrons" in eiger_wrong_nel.compare_states(s) assert "occupation" in eiger_wrong_occ.compare_states(s) assert "match" in eiger_wrong_match.compare_states(s) assert s.is_uhf assert not s.has_hole assert s.total_electrons == 9 assert s.irreps[0] == "a1" assert s.occupations[0] == 1 assert s.spins[0] == "a" assert s.eigenvalues[-1] == pytest.approx(2.1425788573507) assert s.irrep_indices[0] == 1 assert str(s) s.pop(0) assert len(s) == 27 s[0] = s[1] assert s[0] == s[1] s.insert(0, s[-1]) assert s[0] == s[-1] # test filter s = States.from_file() assert len(s.filter_states(irrep="z")) == 0 filtered = s.filter_states(spin="b", irrep="a1") assert filtered[0].spin == "b" assert filtered[0].irrep == "a1" # test get_shells s = States.from_file() with pytest.raises(ValueError): s.get_shells(spin=None) shell = s.get_shells(spin="a") assert shell.occupations[0] == 1 assert shell.states[0] == ("a1", 1)
def test_sdg_subfiles(testdir, delete_tmp_dir): with temp_dir(delete_tmp_dir): for fname in ['control', 'coord', 'energy', 'mos', 'basis']: shutil.copy2( os.path.join(testdir, 'control', '{}_test-subfiles'.format(fname)), fname) cc = DataGroups.from_file("control") energy_dg = cc.sdg(data_group='energy', show_from_subfile=True, raise_if_multiple_subfiles=False, raise_if_missing_subfile=False, raise_if_regular_and_subfile=False) assert energy_dg == ' SCF ' \ 'SCFKIN SCFPOT\n' \ ' 1 -40.48328750375' \ ' 39.86406749278 -80.34735499653\n' udef_bonds_dg = cc.sdg(data_group='user-defined bonds', show_from_subfile=True, raise_if_multiple_subfiles=False, raise_if_missing_subfile=False, raise_if_regular_and_subfile=False) assert udef_bonds_dg == '\n' udef_bonds_dg = cc.sdg(data_group='user-defined bonds', show_from_subfile=False, raise_if_multiple_subfiles=False, raise_if_missing_subfile=False, raise_if_regular_and_subfile=False) assert udef_bonds_dg == ' file=coord\n' redundant_dg = cc.sdg(data_group='redundant', show_from_subfile=True, raise_if_multiple_subfiles=False, raise_if_missing_subfile=False, raise_if_regular_and_subfile=False) assert redundant_dg.startswith('\n number_of_atoms' ' 5\n' ' degrees_of_freedom' ' 9\n' ' internal_coordinates' ' 9\n' ' frozen_coordinates' ' 0\n' ' 1 k 1.0000000000000 stre' ' 1 2' ' val= 2.06326\n') assert redundant_dg.endswith(' 7\n 8' ' 0.699953222 8 0\n' ' 8\n 9' ' 0.699953222 9 0\n' ' 9\n') basis_dg = cc.sdg(data_group='basis', show_from_subfile=True, raise_if_multiple_subfiles=False, raise_if_missing_subfile=False, raise_if_regular_and_subfile=False) assert basis_dg.startswith('\n*\nc SVP\n*\n') assert basis_dg.endswith(' 1 p\n' ' 0.80000000000 1.0000000000\n*\n') mos_dg = cc.sdg(data_group='scfmo', show_from_subfile=True, raise_if_multiple_subfiles=False, raise_if_missing_subfile=False, raise_if_regular_and_subfile=False) assert mos_dg.startswith(' scfconv=6 format(4d20.14)\n' ' 1 a1 ' 'eigenvalue=-.98973943603420D+01' ' nsaos=6\n') assert mos_dg.endswith('-.11000018463806D+01-.60830569311504D+00' '0.13711445998604D+010.13421520432673D+01\n' '0.53804368223874D+00-.91129252301913D+00' '0.11101532709297D+01\n') scfintunit_dg = cc.sdg(data_group='scfintunit', show_from_subfile=True, raise_if_multiple_subfiles=False, raise_if_missing_subfile=False, raise_if_regular_and_subfile=False) assert scfintunit_dg == '\n unit=30 size=35' \ ' file=twoint1\n' \ ' unit=31 size=35' \ ' file=twoint2\n' with pytest.raises(RuntimeError, match='Multiple "file=FILENAME" directives.'): cc.sdg(data_group='scfintunit', show_from_subfile=True, raise_if_multiple_subfiles=True, raise_if_missing_subfile=False, raise_if_regular_and_subfile=False) with pytest.raises(RuntimeError, match=r'Both a reference to a file ' r'\("file=FILENAME"\) and regular data ' r'blocks are in the data group.'): cc.sdg(data_group='user-defined', show_from_subfile=True, raise_if_multiple_subfiles=False, raise_if_missing_subfile=False, raise_if_regular_and_subfile=True) with pytest.raises(RuntimeError, match='File "gradient" for data group "grad" ' 'is missing.'): cc.sdg(data_group='grad', show_from_subfile=True, raise_if_multiple_subfiles=False, raise_if_missing_subfile=True, raise_if_regular_and_subfile=False) grad_db = cc.sdg(data_group='grad', show_from_subfile=True, raise_if_multiple_subfiles=False, raise_if_missing_subfile=False, raise_if_regular_and_subfile=False) assert grad_db == ' file=gradient\n' # create a gradient empty file and check that the behavior is the same # as when the file is missing touch_file("gradient") with pytest.raises(RuntimeError, match='File "gradient" for data group "grad" ' 'is missing.'): cc.sdg(data_group='grad', show_from_subfile=True, raise_if_multiple_subfiles=False, raise_if_missing_subfile=True, raise_if_regular_and_subfile=False) grad_db = cc.sdg(data_group='grad', show_from_subfile=True, raise_if_multiple_subfiles=False, raise_if_missing_subfile=False, raise_if_regular_and_subfile=False) assert grad_db == ' file=gradient\n'