Skip to content

Commit

Permalink
Adjust tests and change output format for levelparser.
Browse files Browse the repository at this point in the history
  • Loading branch information
hdsassnick committed Dec 9, 2023
1 parent 13d82b4 commit 60efdb4
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 13 deletions.
4 changes: 3 additions & 1 deletion cp2k_output_tools/blocks/scf.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ def match_scf(content: str, start: int = 0, end: int = sys.maxsize) -> Optional[
conv_match = INNER_SCF_CONV_RE.search(content, start, end)
if conv_match and energy_match:
start = match.span()[1]
kwargs = {key + "_energy": float(val) for key, val in energy_match.groupdict().items() if val is not None}
kwargs = {
key + "_energy": Decimal(val) * UREG.hartree for key, val in energy_match.groupdict().items() if val is not None
}
sublevels.append(
InnerSCF(
converged="SCF run converged" in conv_match["convtxt"], nsteps=int(conv_match["nsteps"]), **kwargs, sublevels=[]
Expand Down
14 changes: 13 additions & 1 deletion tests/test_energies.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,16 @@ def test_energies():
result = next(parse_iter(fhandle.read(), matchers=[match_energies]))

assert result
assert result == {"energies": {"total force_eval": -251.687390311050706}}
assert result == {
"energies": {
"core_hamiltonian": 138.60026809219576,
"electronic_entropic": -1.256404e-08,
"fermi": 0.20382509931778,
"hartree": 343.3040142273272,
"overlap_core": 3.0088e-10,
"self_core": -656.5115154010256,
"total": -251.63989121633358,
"xc": -77.03265812258856,
"total force_eval": -251.687390311050706,
}
}
128 changes: 117 additions & 11 deletions tests/test_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,79 @@
"num_occ_orb": 1,
"num_mol_orb": 1,
"num_orb_func": 10,
"nsteps": 8,
"force_eval_energy": -1.16096052,
"inner_scf": {
"nsteps": 8,
"overlap_core_energy": 6.0512959e-07,
"self_core_energy": -2.82094792,
"core_hamiltonian_energy": 1.07883184,
"hartree_energy": 1.30392538,
"xc_energy": -0.72277042,
"total_energy": -1.16096052,
"electronic_entropic_energy": None,
"fermi_energy": None,
"dispersion_energy": None,
},
},
{
"nspin": 1,
"nelec": 2,
"num_occ_orb": 1,
"num_mol_orb": 1,
"num_orb_func": 10,
"nsteps": 4,
"force_eval_energy": -1.16118019,
"inner_scf": {
"nsteps": 4,
"overlap_core_energy": 0.0000012,
"self_core_energy": -2.82094792,
"core_hamiltonian_energy": 1.08370608,
"hartree_energy": 1.30183473,
"xc_energy": -0.72577425,
"total_energy": -1.16118019,
"electronic_entropic_energy": None,
"fermi_energy": None,
"dispersion_energy": None,
},
},
{
"nspin": 1,
"nelec": 2,
"num_occ_orb": 1,
"num_mol_orb": 1,
"num_orb_func": 10,
"nsteps": 4,
"force_eval_energy": -1.16118537,
"inner_scf": {
"nsteps": 4,
"overlap_core_energy": 1.07320985e-06,
"self_core_energy": -2.82094792,
"core_hamiltonian_energy": 1.08302262,
"hartree_energy": 1.30210002,
"xc_energy": -0.72536117,
"total_energy": -1.16118537,
"electronic_entropic_energy": None,
"fermi_energy": None,
"dispersion_energy": None,
},
},
{
"nspin": 1,
"nelec": 2,
"num_occ_orb": 1,
"num_mol_orb": 1,
"num_orb_func": 10,
"nsteps": 4,
"force_eval_energy": -1.16118537,
"inner_scf": {
"nsteps": 4,
"overlap_core_energy": 1.07320985e-06,
"self_core_energy": -2.82094792,
"core_hamiltonian_energy": 1.08302280,
"hartree_energy": 1.30210002,
"xc_energy": -0.72536117,
"total_energy": -1.16118537,
"electronic_entropic_energy": None,
"fermi_energy": None,
"dispersion_energy": None,
},
},
],
},
Expand All @@ -53,44 +97,99 @@
"num_occ_orb": 16,
"num_mol_orb": 16,
"num_orb_func": 55,
"nsteps": 10,
"force_eval_energy": -65.98706397,
"inner_scf": {
"nsteps": 10,
"overlap_core_energy": 0.0,
"self_core_energy": -138.89582544,
"core_hamiltonian_energy": 27.22305865,
"hartree_energy": 56.70225641,
"xc_energy": -11.01655328,
"total_energy": -65.98706366,
"electronic_entropic_energy": None,
"fermi_energy": None,
"dispersion_energy": None,
},
},
{
"nspin": 1,
"nelec": 32,
"num_occ_orb": 16,
"num_mol_orb": 16,
"num_orb_func": 55,
"nsteps": 9,
"force_eval_energy": -65.98706506,
"inner_scf": {
"nsteps": 9,
"overlap_core_energy": 0.0,
"self_core_energy": -138.89582544,
"core_hamiltonian_energy": 27.22275912,
"hartree_energy": 56.70220023,
"xc_energy": -11.01647284,
"total_energy": -65.98733894,
"electronic_entropic_energy": None,
"fermi_energy": None,
"dispersion_energy": None,
},
},
{
"nspin": 1,
"nelec": 32,
"num_occ_orb": 16,
"num_mol_orb": 16,
"num_orb_func": 55,
"nsteps": 5,
"force_eval_energy": -65.98710767,
"inner_scf": {
"nsteps": 5,
"overlap_core_energy": 0.0,
"self_core_energy": -138.89582544,
"core_hamiltonian_energy": 27.20749466,
"hartree_energy": 56.71104696,
"xc_energy": -11.00902476,
"total_energy": -65.98630859,
"electronic_entropic_energy": None,
"fermi_energy": None,
"dispersion_energy": None,
},
},
{
"nspin": 1,
"nelec": 32,
"num_occ_orb": 16,
"num_mol_orb": 16,
"num_orb_func": 55,
"nsteps": 4,
"force_eval_energy": -65.98710770,
"inner_scf": {
"nsteps": 4,
"overlap_core_energy": 0.0,
"self_core_energy": -138.89582544,
"core_hamiltonian_energy": 27.20670707,
"hartree_energy": 56.71083178,
"xc_energy": -11.00884866,
"total_energy": -65.98713525,
"electronic_entropic_energy": None,
"fermi_energy": None,
"dispersion_energy": None,
},
},
{
"nspin": 1,
"nelec": 32,
"num_occ_orb": 16,
"num_mol_orb": 16,
"num_orb_func": 55,
"nsteps": 1,
"force_eval_energy": -65.98710770,
"inner_scf": {
"nsteps": 1,
"overlap_core_energy": 0.0,
"self_core_energy": -138.89582544,
"core_hamiltonian_energy": 27.20676302,
"hartree_energy": 56.71080149,
"xc_energy": -11.00884677,
"total_energy": -65.98710770,
"electronic_entropic_energy": None,
"fermi_energy": None,
"dispersion_energy": None,
},
},
],
"cell_infos": [
Expand Down Expand Up @@ -149,12 +248,19 @@ def test_optimization(opt_type):
for step_scf, opt_ref in zip(opt_steps_scf + [result.levels[0].sublevels[1]], ref["opt"]):
assert step_scf.converged
assert step_scf.sublevels[0].converged
inner_scf_ref = opt_ref.pop("inner_scf")
inner_scf = step_scf.sublevels[0]
for keyw, val in inner_scf_ref.items():
if val is None:
assert getattr(inner_scf, keyw) is None
elif isinstance(val, float):
assert abs(float(getattr(inner_scf, keyw).magnitude) - val) < 1.0e-7
else:
assert getattr(inner_scf, keyw) == val
assert abs(float(step_scf.force_eval_energy.magnitude) - opt_ref.pop("force_eval_energy")) < 1.0e-7
assert step_scf.sublevels[0].nsteps == opt_ref.pop("nsteps")
for keyw, val in opt_ref.items():
assert getattr(step_scf, keyw) == val
if opt_type == "cell":
print(result.levels[0].cell_infos)
assert len(result.levels[0].cell_infos) == len(ref["cell_infos"])
for info, ref_info in zip(result.levels[0].cell_infos, ref["cell_infos"]):
assert abs(float(info.volume.magnitude) - ref_info.pop("volume")) < 1.0e-7
Expand Down

0 comments on commit 60efdb4

Please sign in to comment.