From c9edd069ea35e67b663158062740f3ecf8cbf277 Mon Sep 17 00:00:00 2001 From: JosePizarro3 Date: Thu, 27 Jun 2024 11:48:16 +0200 Subject: [PATCH] Added model_method_ref in Outputs and testing --- .../schema_packages/outputs.py | 38 ++++++++++++--- tests/test_outputs.py | 48 +++++++++++++++++-- 2 files changed, 75 insertions(+), 11 deletions(-) diff --git a/src/nomad_simulations/schema_packages/outputs.py b/src/nomad_simulations/schema_packages/outputs.py index 2578aba2..462d3a12 100644 --- a/src/nomad_simulations/schema_packages/outputs.py +++ b/src/nomad_simulations/schema_packages/outputs.py @@ -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 ( @@ -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'), ) @@ -130,11 +138,11 @@ 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 @@ -142,13 +150,31 @@ def set_model_system_ref(self) -> Optional[ModelSystem]: 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): """ diff --git a/tests/test_outputs.py b/tests/test_outputs.py index 172e8f3d..7b06486d 100644 --- a/tests/test_outputs.py +++ b/tests/test_outputs.py @@ -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 @@ -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: