Skip to content

Commit

Permalink
Added more tests for printing KS eigenvalues
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanbmorgan committed Oct 29, 2024
1 parent a363895 commit 4b2783e
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 55 deletions.
25 changes: 19 additions & 6 deletions dfttools/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,8 +969,6 @@ def _get_ks_states(self, ev_start, eigenvalues, scf_iter, n_ks_states):
eigenvalues["occupation"][scf_iter][i] = float(values[1])
eigenvalues["eigenvalue_eV"][scf_iter][i] = float(values[3])

# return eigenvalues

def get_all_ks_eigenvalues(self) -> Union[dict, Tuple[dict, dict]]:
"""
Get all Kohn-Sham eigenvalues from a calculation.
Expand All @@ -988,8 +986,10 @@ def get_all_ks_eigenvalues(self) -> Union[dict, Tuple[dict, dict]]:
Raises
------
ItemNotFoundError
the 'output_level full' keyword was not found in the calculation
ValueError
the calculation was not spin polarised
could not determine if the calculation was spin polarised
"""

# Check if the calculation was spin polarised
Expand Down Expand Up @@ -1019,9 +1019,9 @@ def get_all_ks_eigenvalues(self) -> Union[dict, Tuple[dict, dict]]:
n = 0 # Count the current SCF iteration
for i, line in enumerate(self.lines):
if target_line in line:
n += 1
# Get the KS states from this line until the next empty line
self._get_ks_states(i + 1, eigenvalues, n, n_ks_states)
n += 1

return eigenvalues

Expand Down Expand Up @@ -1083,6 +1083,8 @@ def get_final_ks_eigenvalues(self) -> Union[dict, Tuple[dict, dict]]:
------
ValueError
the calculation was not spin polarised
ValueError
the final KS states were not found in aims.out file
"""

# Check if the calculation was spin polarised
Expand All @@ -1095,11 +1097,15 @@ def get_final_ks_eigenvalues(self) -> Union[dict, Tuple[dict, dict]]:
target_line = "State Occupation Eigenvalue [Ha] Eigenvalue [eV]"

# Iterate backwards from end of aims.out to find the final KS eigenvalues
final_ev_start = None
for i, line in enumerate(reversed(self.lines)):
if target_line == line.strip():
final_ev_start = -i
break

if final_ev_start is None:
raise ValueError("Final KS states not found in aims.out file.")

if not spin_polarised:
eigenvalues = {
"state": np.zeros((1, n_ks_states), dtype=int),
Expand Down Expand Up @@ -1143,6 +1149,11 @@ def get_pert_soc_ks_eigenvalues(self) -> dict:
-------
dict
The perturbative SOC kohn-sham eigenvalues
Raises
------
ValueError
the final KS states were not found in aims.out file
"""

# Get the number of KS states
Expand All @@ -1153,15 +1164,17 @@ def get_pert_soc_ks_eigenvalues(self) -> dict:
" Eigenvalue [eV] Level Spacing [eV]"
)

print(target_line)

# Iterate backwards from end of aims.out to find the perturbative SOC
# eigenvalues
final_ev_start = None
for i, line in enumerate(reversed(self.lines)):
if target_line == line.strip():
final_ev_start = -i
break

if final_ev_start is None:
raise ValueError("Final KS states not found in aims.out file.")

eigenvalues = {
"state": np.zeros(n_ks_states, dtype=int),
"occupation": np.zeros(n_ks_states, dtype=float),
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/default_aims_calcs/1/control.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
xc pbe
output_level full
################################################################################
#
# FHI-aims code project
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/default_aims_calcs/2/control.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
xc pbe
spin collinear
default_initial_moment 1
output_level full
################################################################################
#
# FHI-aims code project
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/default_aims_calcs/3/control.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ xc pbe
spin collinear
default_initial_moment 1
include_spin_orbit non_self_consistent
output_level full
################################################################################
#
# FHI-aims code project
Expand Down
75 changes: 26 additions & 49 deletions tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ def aims_out(self, request, run_aims):
with open(f"{cwd}/test_references.yaml", "r") as references:
self.ref_data = yaml.safe_load(references)

# Set class attribute to check in xfail tests
# self._run_aims = False if run_aims is False else True

@property
def _aims_fixture_no(self) -> int:
return int(self.ao.aims_out_path.split("/")[-2])
Expand Down Expand Up @@ -161,49 +158,8 @@ def test_get_change_of_forces(self):
with pytest.raises(ValueError):
self.ao.get_change_of_forces()

def get_change_of_sum_of_eigenvalues(self):
pass

# TODO: currently a palceholder
# def test_all_output_functions(self_9):, # aims = aims_out_9

# aims.get_geometry()
# aims.get_parameters()
# aims.check_exit_normal()
# aims.get_change_of_total_energy()
# # aims.get_change_of_forces()
# aims.get_change_of_sum_of_eigenvalues()
# # aims.get_maximum_force()
# aims.get_final_energy()
# aims.get_energy_corrected()
# aims.get_total_energy_T0()
# aims.get_energy_uncorrected()
# # aims.get_energy_without_vdw()
# aims.get_HOMO_energy()
# aims.get_LUMO_energy()
# # aims.get_vdw_energy()
# aims.get_exchange_correlation_energy()
# aims.get_electrostatic_energy()
# aims.get_kinetic_energy()
# aims.get_sum_of_eigenvalues()
# aims.get_cx_potential_correction()
# aims.get_free_atom_electrostatic_energy()
# aims.get_entropy_correction()
# aims.get_hartree_energy_correction()
# # aims.get_ionic_embedding_energy()
# # aims.get_density_embedding_energy()
# # aims.get_nonlocal_embedding_energy()
# # aims.get_external_embedding_energy()
# # aims.get_forces()
# aims.check_spin_polarised()
# aims.get_conv_params()
# aims.get_n_relaxation_steps()
# aims.get_n_scf_iters()
# aims.get_i_scf_conv_acc()
# aims.get_n_initial_ks_states()
# # aims.get_all_ks_eigenvalues()# -> functionality does not work
# aims.get_final_ks_eigenvalues()
# # aims.get_pert_soc_ks_eigenvalues()# -> not great but may work if that output is there
# TODO
# def get_change_of_sum_of_eigenvalues(self):

def test_check_spin_polarised(self):
if self._aims_fixture_no in [2, 3]:
Expand Down Expand Up @@ -268,9 +224,30 @@ def compare_n_initial_ks_states():
with pytest.warns(UserWarning):
compare_n_initial_ks_states()

# TODO Setup YAML files for storing the expected values for the following tests
# def test_get_all_ks_eigenvalues(self):
def test_get_all_ks_eigenvalues(self):
if self._aims_fixture_no == 1:
assert self.ao.get_all_ks_eigenvalues() == self.ref_data["eigenvalues"]
elif self._aims_fixture_no == 2:
spin_up, spin_down = self.ao.get_all_ks_eigenvalues()
assert spin_up == self.ref_data["su_eigenvalues"]
assert spin_down == self.ref_data["sd_eigenvalues"]
else:
with pytest.raises(ValueError):
self.ao.get_all_ks_eigenvalues()

# TODO
# def get_final_ks_eigenvalues_test(self):

# def get_pert_soc_ks_eigenvalues_test(self):
def test_get_pert_soc_ks_eigenvalues(self):
if self._aims_fixture_no == 3:
assert (
self.ao.get_pert_soc_ks_eigenvalues()
== self.ref_data["pert_soc_eigenvalues"]
)
else:
with pytest.raises(ValueError):
self.ao.get_pert_soc_ks_eigenvalues()


# TODO
# class TestELSIOutput:
Loading

0 comments on commit 4b2783e

Please sign in to comment.