diff --git a/src/nomad_simulations/physical_property.py b/src/nomad_simulations/physical_property.py index 9656f7e1..ab3c2b9f 100644 --- a/src/nomad_simulations/physical_property.py +++ b/src/nomad_simulations/physical_property.py @@ -16,6 +16,7 @@ # limitations under the License. # +import numpy as np from typing import Any, Optional from nomad import utils @@ -229,6 +230,11 @@ def __setattr__(self, name: str, val: Any) -> None: f'The value of the physical property {self.name} is None. Please provide a finite valid value.' ) _new_value = self._new_value + + # patch for when `val` does not have units and it is passed as a list (instead of np.array) + if isinstance(val, list): + val = np.array(val) + # non-scalar or scalar `val` try: value_shape = list(val.shape) @@ -241,7 +247,10 @@ def __setattr__(self, name: str, val: Any) -> None: f'extracted from the variables `n_grid_points` and the `shape` defined in `PhysicalProperty`.' ) _new_value.shape = self.full_shape - _new_value = val.magnitude * val.u + if hasattr(val, 'magnitude'): + _new_value = val.magnitude * val.u + else: + _new_value = val return super().__setattr__(name, _new_value) return super().__setattr__(name, val) diff --git a/src/nomad_simulations/properties/spectral_profile.py b/src/nomad_simulations/properties/spectral_profile.py index bcf5d65a..b78ef288 100644 --- a/src/nomad_simulations/properties/spectral_profile.py +++ b/src/nomad_simulations/properties/spectral_profile.py @@ -187,8 +187,12 @@ def _check_spin_polarized(self, logger: BoundLogger) -> bool: (bool): True if the simulation is spin-polarized, False otherwise. """ # TODO use this in `ElectronicBandGap` too + outputs = self.m_parent + if outputs is None: + logger.warning('Could not resolve the parent `Outputs`.') + return False model_method = get_sibling_section( - section=self, sibling_section_name='model_method', logger=logger + section=outputs, sibling_section_name='model_method', logger=logger ) return ( True @@ -412,6 +416,8 @@ def extract_band_gap(self) -> Optional[ElectronicBandGap]: band_gap.value = homo - lumo return band_gap + # TODO add extraction from `projected_dos` + def normalize(self, archive, logger) -> None: super().normalize(archive, logger) diff --git a/tests/conftest.py b/tests/conftest.py index 7fea3b83..f0dd8e07 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -21,8 +21,9 @@ from nomad.units import ureg -from nomad_simulations.outputs import ElectronicBandGap, Outputs, SCFOutputs +from nomad_simulations.outputs import Outputs, SCFOutputs from nomad_simulations.numerical_settings import SelfConsistency +from nomad_simulations.properties import ElectronicBandGap if os.getenv('_PYTEST_RAISE', '0') != '0': diff --git a/tests/test_spectral_profile.py b/tests/test_spectral_profile.py index 59f47965..ed99add7 100644 --- a/tests/test_spectral_profile.py +++ b/tests/test_spectral_profile.py @@ -28,7 +28,7 @@ ElectronicDensityOfStates, XASSpectra, ) -from nomad_simulations.variables import Temperature +from nomad_simulations.variables import Temperature, Energy2 as Energy class TestSpectralProfile: @@ -36,8 +36,17 @@ class TestSpectralProfile: Test the `SpectralProfile` class defined in `properties/spectral_profile.py`. """ - def test_negative_value(self): - spectral_profile = SpectralProfile() + def test_is_valid_spectral_profile(self): + """ + Test the `is_valid_spectral_profile` method. + """ + spectral_profile = SpectralProfile( + variables=[Energy(grid_points=[-3, -2, -1, 0, 1, 2, 3] * ureg.joule)] + ) + spectral_profile.value = [1.5, 1.2, 0, 0, 0, 0.8, 1.3] + assert spectral_profile.is_valid_spectral_profile() + spectral_profile.value = [3, 2, 0, 0, 0, -4, 1] + assert not spectral_profile.is_valid_spectral_profile() # default value inherited in other SpectralProfile classes assert spectral_profile.rank == [] @@ -52,13 +61,28 @@ def test_default_quantities(self): """ Test the default quantities assigned when creating an instance of the `ElectronicDensityOfStates` class. """ - electronic_band_gap = ElectronicDensityOfStates() + electronic_dos = ElectronicDensityOfStates() assert ( - electronic_band_gap.iri + electronic_dos.iri == 'http://fairmat-nfdi.eu/taxonomy/ElectronicDensityOfStates' ) - assert electronic_band_gap.name == 'ElectronicDensityOfStates' - assert electronic_band_gap.rank == [] + assert electronic_dos.name == 'ElectronicDensityOfStates' + assert electronic_dos.rank == [] + + def test_check_energy_variables(self): + """ + Test the `_check_energy_variables` method. + """ + electronic_dos = ElectronicDensityOfStates() + electronic_dos.variables = [ + Temperature(grid_points=[-3, -2, -1, 0, 1, 2, 3] * ureg.kelvin) + ] + assert electronic_dos._check_energy_variables(logger) is None + electronic_dos.variables.append( + Energy(grid_points=[-3, -2, -1, 0, 1, 2, 3] * ureg.joule) + ) + energies = electronic_dos._check_energy_variables(logger) + assert (energies.magnitude == np.array([-3, -2, -1, 0, 1, 2, 3])).all() class TestXASSpectra: @@ -71,7 +95,7 @@ def test_default_quantities(self): """ Test the default quantities assigned when creating an instance of the `XASSpectra` class. """ - electronic_band_gap = XASSpectra() - assert electronic_band_gap.iri is None # Add iri when available - assert electronic_band_gap.name == 'XASSpectra' - assert electronic_band_gap.rank == [] + xas_spectra = XASSpectra() + assert xas_spectra.iri is None # Add iri when available + assert xas_spectra.name == 'XASSpectra' + assert xas_spectra.rank == []