Skip to content

Commit

Permalink
Fix minor things (#57)
Browse files Browse the repository at this point in the history
* Fix get_n_grid_points in Variables

Changed to private _check_negative_values in ElectronicBandGap

Added get_model_system_ref in Outputs

* Fix setup
  • Loading branch information
JosePizarro3 authored Apr 19, 2024
1 parent 7273bf5 commit d9542f8
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 19 deletions.
3 changes: 2 additions & 1 deletion nomad.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ plugins:
# We only include our schema here. Without the explicit include, all plugins will be
# loaded. Many build in plugins require more dependencies. Install nomad-lab[parsing]
# to make all default plugins work.
include: 'schemas/nomad_simulations'
include:
- 'schemas/nomad_simulations'
options:
schemas/nomad_simulations:
python_package: nomad_simulations
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ authors = [
license = { text = "Apache-2.0" }
requires-python = ">=3.9"
dependencies = [
"nomad-lab>=1.2.0",
"nomad-lab>=1.2.2dev578",
"matid>=2.0.0.dev2",
]

Expand Down
26 changes: 25 additions & 1 deletion src/nomad_simulations/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
from .model_system import ModelSystem
from .physical_property import PhysicalProperty
from .numerical_settings import SelfConsistency
from .properties import ElectronicBandGap
from .properties import (
ElectronicBandGap,
)


class Outputs(ArchiveSection):
Expand Down Expand Up @@ -62,8 +64,12 @@ class Outputs(ArchiveSection):
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# List of properties
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

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
Expand All @@ -84,9 +90,27 @@ def extract_spin_polarized_property(self, property_name: str) -> list:
spin_polarized_properties.append(prop)
return spin_polarized_properties

def set_model_system_ref(self) -> Optional[ModelSystem]:
"""
Set the reference to the last ModelSystem if this is not set in the output. This is only
valid if there is only one ModelSystem in the parent section.
Returns:
(Optional[ModelSystem]): The reference to the last ModelSystem.
"""
if self.m_parent is not None:
model_systems = self.m_parent.model_system
if model_systems is not None and len(model_systems) == 1:
return model_systems[-1]
return None

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

# Set ref to the last ModelSystem if this is not set in the output
if self.model_system_ref is None:
self.model_system_ref = self.set_model_system_ref()


class SCFOutputs(Outputs):
"""
Expand Down
2 changes: 1 addition & 1 deletion src/nomad_simulations/physical_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def variables_shape(self) -> Optional[list]:
(list): The shape of the variables over which the physical property varies.
"""
if self.variables is not None:
return [v.get_n_grid_points(v.grid_points, logger) for v in self.variables]
return [v.get_n_grid_points(logger) for v in self.variables]
return []

@property
Expand Down
5 changes: 2 additions & 3 deletions src/nomad_simulations/properties/band_gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class ElectronicBandGap(PhysicalProperty):

iri = 'http://fairmat-nfdi.eu/taxonomy/ElectronicBandGap'

# ? can `type` change character depending on the `variables`?
type = Quantity(
type=MEnum('direct', 'indirect'),
description="""
Expand Down Expand Up @@ -82,7 +81,7 @@ def __init__(
self.name = self.m_def.name
self.rank = []

def check_negative_values(self, logger: BoundLogger) -> Optional[pint.Quantity]:
def _check_negative_values(self, logger: BoundLogger) -> Optional[pint.Quantity]:
"""
Checks if the electronic band gaps is negative and sets them to None if they are.
Expand Down Expand Up @@ -142,7 +141,7 @@ def normalize(self, archive, logger) -> None:
super().normalize(archive, logger)

# Checks if the `value` is negative and sets it to None if it is.
self.value = self.check_negative_values(logger)
self.value = self._check_negative_values(logger)
if self.value is None:
# ? What about deleting the class if `value` is None?
logger.error('The `value` of the electronic band gap is not stored.')
Expand Down
15 changes: 6 additions & 9 deletions src/nomad_simulations/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,35 @@ class Variables(ArchiveSection):

# grid_points_error = Quantity()

def get_n_grid_points(
self, grid_points: Optional[list], logger: BoundLogger
) -> Optional[int]:
def get_n_grid_points(self, logger: BoundLogger) -> Optional[int]:
"""
Get the number of grid points from the `grid_points` list. If `n_grid_points` is previously defined
and does not coincide with the length of `grid_points`, a warning is issued and this function re-assigns `n_grid_points`
as the length of `grid_points`.
Args:
grid_points (Optional[list]): The grid points in which the variable is discretized.
logger (BoundLogger): The logger to log messages.
Returns:
(Optional[int]): The number of grid points.
"""
if grid_points is not None and len(grid_points) > 0:
if self.grid_points is not None and len(self.grid_points) > 0:
if (
self.n_grid_points != len(grid_points)
self.n_grid_points != len(self.grid_points)
and self.n_grid_points is not None
):
logger.warning(
f'The stored `n_grid_points`, {self.n_grid_points}, does not coincide with the length of `grid_points`, '
f'{len(grid_points)}. We will re-assign `n_grid_points` as the length of `grid_points`.'
f'{len(self.grid_points)}. We will re-assign `n_grid_points` as the length of `grid_points`.'
)
return len(grid_points)
return len(self.grid_points)
return self.n_grid_points

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

# Setting `n_grid_points` if these are not defined
self.n_grid_points = self.get_n_grid_points(self.grid_points, logger)
self.n_grid_points = self.get_n_grid_points(logger)


class Temperature(Variables):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_band_gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_check_negative_values(
self, value: Union[List[float], float], result: float
):
"""
Test the `check_negative_values` method.
Test the `_check_negative_values` method.
"""
if isinstance(value, list):
electronic_band_gap = ElectronicBandGap(
Expand All @@ -67,7 +67,7 @@ def test_check_negative_values(
else:
electronic_band_gap = ElectronicBandGap()
electronic_band_gap.value = value * ureg.joule
checked_value = electronic_band_gap.check_negative_values(logger)
checked_value = electronic_band_gap._check_negative_values(logger)
if checked_value is not None:
assert np.isclose(checked_value.magnitude, result)
else:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ def test_normalize(self, n_grid_points: int, grid_points: list, result: int):
n_grid_points=n_grid_points,
grid_points=grid_points,
)
assert variable.get_n_grid_points(grid_points, logger) == result
assert variable.get_n_grid_points(logger) == result
variable.normalize(None, logger)
assert variable.n_grid_points == result

0 comments on commit d9542f8

Please sign in to comment.