def test_grep_line(self): string = """some text some more text following line""" p = Parser(string) assert p.grep_line("some more", nlines=1) == "some more text\nfollowing line" assert p.grep_line("wrong text") is None
def test_fail_all_done_check(self, delete_tmp_dir): with temp_dir(delete_tmp_dir) as tmp: with open("test.log", "wt") as f: f.write("""some text some more text following line""") with pytest.raises( ValueError, match= "The string does not contain data for a completed calculation" ): Parser.from_file("test.log")
def test_get_split_jobex_parsers(self, testdir): path = os.path.join(testdir, "outputs", "jobex", "h2o_dscf_job.last") p = Parser.from_file(path) jp = p.get_split_jobex_parsers() assert jp.exec_en == "dscf" assert jp.exec_grad == "grad" assert jp.exec_relax == "statpt" path = os.path.join(testdir, "outputs", "jobex", "no3_ridft_job.last") p = Parser.from_file(path) jp = p.get_split_jobex_parsers() assert jp.exec_en == "ridft" assert jp.exec_grad == "rdgrad" assert jp.exec_relax == "statpt"
def test_remove_last_gradient(self, molecule_filepath, delete_tmp_dir): """ Tests the remove_last_gradient method of Control. First runs twice an energy-grad-relax loop, then removes the last energy and runs escf. The output of escf contains the gs energy and it reads it from the energy file. The test checks that the value from the escf.log output corresponds to the first value of the energy and not to the one that should have been removed. """ 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") run_tm("rdgrad") run_tm("statpt") run_tm("ridft") run_tm("rdgrad") c = Control.from_file() g = c.gradient assert len(g.gradients) == 2 c.remove_last_gradient() run_tm("statpt") p = Parser.from_file("statpt.log") g_from_statp = p.relax_gradient_values assert np.allclose(g.norms[0], g_from_statp["norm_cartesian"]) assert not np.allclose(g.norms[1], g_from_statp["norm_cartesian"])
def test_remove_last_energy(self, molecule_filepath, delete_tmp_dir): """ Tests the remove_last_energy method of Control. First runs twice an energy-grad-relax loop, then removes the last energy and runs escf. The output of escf contains the gs energy and it reads it from the energy file. The test checks that the value from the escf.log output corresponds to the first value of the energy and not to the one that should have been removed. """ with temp_dir(delete_tmp_dir) as tmp_dir: dp = get_define_template("ridft_escf") run_define_runner(define_opt=dp, mol_path=molecule_filepath) for i in range(2): run_tm("ridft") run_tm("rdgrad") run_tm("statpt") c = Control.from_file() en = c.energy.total assert len(c.energy.total) == 2 c.remove_last_energy() run_tm("escf") p = Parser.from_file("escf.log") gs_en_from_escf = p.escf_gs_total_en assert gs_en_from_escf == pytest.approx(en[0]) assert not gs_en_from_escf == pytest.approx(en[1])
def parser_and_dict(request, testdir): directory = request.param[0] name = request.param[1] path = os.path.join(testdir, "outputs", directory, name) parser = Parser.from_file(path + ".log") with open(path + ".json") as f: d = json.load(f) return parser, d
def test_mod_control(self, molecule_filepath, delete_tmp_dir): with temp_dir(delete_tmp_dir) as tmp_dir: dp = get_define_template("ridft") dp["desy"] = True dp["functional"] = "b-p" run_define_runner(define_opt=dp, mol_path=molecule_filepath) assert sdgo("dft", "functional").strip() == "b-p" assert sdg("thime").strip() == "5" cdg("thime", "6") mdgo("dft", {"functional": "functional pbe"}) run_tm("ridft") p = Parser.from_file("ridft.log") assert p.integral["thime"] == 6 assert p.density_functional_data["functional_name"] == "pbe"
def test_mod_shells(self, molecule_filepath, delete_tmp_dir): with temp_dir(delete_tmp_dir) as tmp_dir: dp = get_define_template("ridft") dp["desy"] = False dp["functional"] = "b-p" run_define_runner(define_opt=dp, mol_path=molecule_filepath) s = Shells.from_file("closed") s.states.pop(-1) s.occupations.pop(-1) cdg("closed shells", s.to_datagroup()) run_tm("ridft") p = Parser.from_file("ridft.log") assert p.electrostatic_moments["charge"] == pytest.approx(2)
def generate_files(files=None, methods=None, overwrite=False): """ Helper function to generate the reference files for the test of the parser. Allows to target only specific methods and specific files. Args: files (list): list of tuples with (folder name, file name without extension), like the one in "files_list". Only the json for the files in this list will be generated. If None "files_list" will be used. methods (list): list of string with the methods of Parser for which the reference data will be generated. If None "parser_methods" will be used. overwrite (bool): if False, in case a method has already its reference value in the json file it will not be changed. If True it will be overwritten. """ if methods is None: methods = parser_methods if files is None: files = files_list for directory, name in files: path = os.path.join( os.path.split(__file__)[0], "../../testfiles", "outputs", directory, name) parser = Parser.from_file(path + ".log") json_path = path + ".json" if os.path.isfile(json_path): with open(json_path) as f: ref_data = json.load(f) else: ref_data = {} for m in methods: if m not in ref_data or overwrite: parsed_data = getattr(parser, m) ref_data[m] = parsed_data with open(json_path, "wt") as f: from monty.json import jsanitize json.dump(jsanitize(ref_data), f, indent=2)
def test_get_value(self): p = Parser("line to match 0.1 xxx") assert p.get_value("line to", -2, 0, float) == pytest.approx(0.1)