Skip to content

Commit

Permalink
Added model_method_ref in Outputs and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
JosePizarro3 committed Jun 27, 2024
1 parent 3bcaac6 commit c9edd06
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 11 deletions.
38 changes: 32 additions & 6 deletions src/nomad_simulations/schema_packages/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from structlog.stdlib import BoundLogger

from nomad_simulations.schema_packages.model_system import ModelSystem
from nomad_simulations.schema_packages.model_method import ModelMethod
from nomad_simulations.schema_packages.numerical_settings import SelfConsistency
from nomad_simulations.schema_packages.physical_property import PhysicalProperty
from nomad_simulations.schema_packages.properties import (
Expand Down Expand Up @@ -61,8 +62,15 @@ class Outputs(ArchiveSection):
model_system_ref = Quantity(
type=ModelSystem,
description="""
Reference to the `ModelSystem` section to which the output property references to and on
on which the simulation is performed.
Reference to the `ModelSystem` section in which the output physical properties were calculated.
""",
a_eln=ELNAnnotation(component='ReferenceEditQuantity'),
)

model_method_ref = Quantity(
type=ModelMethod,
description="""
Reference to the `ModelMethod` section in which the output physical properties were calculated.
""",
a_eln=ELNAnnotation(component='ReferenceEditQuantity'),
)
Expand Down Expand Up @@ -130,25 +138,43 @@ def extract_spin_polarized_property(

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.
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.
(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 set_model_method_ref(self) -> Optional[ModelMethod]:
"""
Set the reference to the last `ModelMethod` if this is not set in the output. This is only
valid if there is only one `ModelMethod` in the parent section.
Returns:
(Optional[ModelMethod]): The reference to the last `ModelMethod`.
"""
if self.m_parent is not None:
model_methods = self.m_parent.model_method
if model_methods is not None and len(model_methods) == 1:
return model_methods[-1]
return None

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

# Set ref to the last ModelSystem if this is not set in the output
# 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()

# Set ref to the last `ModelMethod` if this is not set in the output
if self.model_method_ref is None:
self.model_method_ref = self.set_model_method_ref()


class SCFOutputs(Outputs):
"""
Expand Down
48 changes: 43 additions & 5 deletions tests/test_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from nomad.datamodel import EntryArchive

from nomad_simulations.schema_packages.model_system import ModelSystem
from nomad_simulations.schema_packages.model_method import ModelMethod
from nomad_simulations.schema_packages.numerical_settings import SelfConsistency
from nomad_simulations.schema_packages.outputs import Outputs, SCFOutputs
from nomad_simulations.schema_packages.properties import ElectronicBandGap
Expand Down Expand Up @@ -135,25 +136,62 @@ def test_set_model_system_ref(self, model_system: Optional[ModelSystem]):
assert model_system_ref is None

@pytest.mark.parametrize(
'model_system',
[(None), (ModelSystem(name='example'))],
'model_method',
[(None), (ModelMethod(name='example'))],
)
def test_normalize(self, model_system: Optional[ModelSystem]):
def test_set_model_method_ref(self, model_method: Optional[ModelMethod]):
"""
Test the `set_model_method_ref` method.
Args:
model_method (Optional[ModelMethod]): The `ModelMethod` to be tested for the `model_method_ref` reference
stored in `Outputs`.
"""
outputs = Outputs()
simulation = generate_simulation(model_method=model_method, outputs=outputs)
model_method_ref = outputs.set_model_method_ref()
if model_method is not None:
assert model_method_ref == simulation.model_method[-1]
assert model_method_ref.name == 'example'
else:
assert model_method_ref is None

@pytest.mark.parametrize(
'model_system, model_method',
[
(None, None),
(ModelSystem(name='example system'), None),
(None, ModelMethod(name='example method')),
(ModelSystem(name='example system'), ModelMethod(name='example method')),
],
)
def test_normalize(
self, model_system: Optional[ModelSystem], model_method: Optional[ModelMethod]
):
"""
Test the `normalize` method.
Args:
model_system (Optional[ModelSystem]): The expected `model_system_ref` obtained after normalization and
initially stored under `Simulation.model_system[0]`.
model_method (Optional[ModelMethod]): The expected `model_method_ref` obtained after normalization and
initially stored under `Simulation.model_method[0]`.
"""
outputs = Outputs()
simulation = generate_simulation(model_system=model_system, outputs=outputs)
simulation = generate_simulation(
model_system=model_system, model_method=model_method, outputs=outputs
)
outputs.normalize(archive=EntryArchive(), logger=logger)
if model_system is not None:
assert outputs.model_system_ref == simulation.model_system[-1]
assert outputs.model_system_ref.name == 'example'
assert outputs.model_system_ref.name == 'example system'
else:
assert outputs.model_system_ref is None
if model_method is not None:
assert outputs.model_method_ref == simulation.model_method[-1]
assert outputs.model_method_ref.name == 'example method'
else:
assert outputs.model_method_ref is None


class TestSCFOutputs:
Expand Down

0 comments on commit c9edd06

Please sign in to comment.