Skip to content

Commit

Permalink
Added extract_spin_polarized_property to Outputs and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
JosePizarro3 committed Apr 15, 2024
1 parent 0c058b3 commit 1643440
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/nomad_simulations/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,29 @@ class Outputs(ArchiveSection):
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
electronic_band_gap = SubSection(sub_section=ElectronicBandGap.m_def, repeats=True)

def extract_spin_polarized_property(self, property_name: str) -> list:
"""
Extracts the spin-polarized properties if present from the property name and returns them as a list of two elements in
which each element refers to each `spin_channel`. If the return list is empty, it means that the simulation is not
spin-polarized (i.e., `spin_channel` is not defined).
Args:
property_name (str): The name of the property to be extracted.
Returns:
(list): The list of spin-polarized properties.
"""
spin_polarized_properties = []
properties = getattr(self, property_name)
for prop in properties:
if prop.spin_channel is None:
continue
spin_polarized_properties.append(prop)
return spin_polarized_properties

def normalize(self, archive, logger) -> None:
super().normalize(archive, logger)

# Resolve if the output property `is_derived` or not.
# self.is_derived = self.resolve_is_derived(self.outputs_ref)


class SCFOutputs(Outputs):
"""
Expand Down
25 changes: 25 additions & 0 deletions tests/test_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@ def test_is_scf_converged(self, threshold_change: float, result: bool):
)
assert is_scf_converged == result

def test_extract_spin_polarized_properties(self):
"""
Test the `extract_spin_polarized_property` method.
"""
outputs = Outputs()

# No spin-polarized band gap
band_gap_non_spin_polarized = ElectronicBandGap(variables=[])
band_gap_non_spin_polarized.value = 2.0 * ureg.joule
outputs.electronic_band_gap.append(band_gap_non_spin_polarized)
band_gaps = outputs.extract_spin_polarized_property('electronic_band_gap')
assert band_gaps == []

# Spin-polarized band gaps
band_gap_spin_1 = ElectronicBandGap(variables=[], spin_channel=0)
band_gap_spin_1.value = 1.0 * ureg.joule
outputs.electronic_band_gap.append(band_gap_spin_1)
band_gap_spin_2 = ElectronicBandGap(variables=[], spin_channel=1)
band_gap_spin_2.value = 1.5 * ureg.joule
outputs.electronic_band_gap.append(band_gap_spin_2)
band_gaps = outputs.extract_spin_polarized_property('electronic_band_gap')
assert len(band_gaps) == 2
assert band_gaps[0].value.magnitude == 1.0
assert band_gaps[1].value.magnitude == 1.5

@pytest.mark.parametrize(
'threshold_change, result',
[(1e-3, True), (1e-5, False)],
Expand Down

0 comments on commit 1643440

Please sign in to comment.