diff --git a/exampleparser/metainfo/__init__.py b/exampleparser/metainfo/__init__.py index b256252..7effa4f 100644 --- a/exampleparser/metainfo/__init__.py +++ b/exampleparser/metainfo/__init__.py @@ -16,14 +16,10 @@ # See the License for the specific language governing permissions and # limitations under the License. # -import sys - from nomad.metainfo import Environment from nomad.metainfo.legacy import LegacyMetainfoEnvironment -import exampleparser.metainfo.example +from .example import m_package m_env = LegacyMetainfoEnvironment() -m_env.m_add_sub_section( - Environment.packages, sys.modules['exampleparser.metainfo.example'].m_package -) # type: ignore +m_env.m_add_sub_section(Environment.packages, m_package) # type: ignore diff --git a/exampleparser/metainfo/example.py b/exampleparser/metainfo/example.py index 842eaec..15e1372 100644 --- a/exampleparser/metainfo/example.py +++ b/exampleparser/metainfo/example.py @@ -16,15 +16,68 @@ # limitations under the License. # -from nomad.datamodel.metainfo.simulation.calculation import Calculation -from nomad.metainfo import Package, Quantity, Section +import numpy as np +from nomad.datamodel import ArchiveSection +from nomad.datamodel.metainfo.workflow import Workflow +from nomad.metainfo import Datetime, Package, Quantity, Reference, Section, SubSection m_package = Package(name='example_nomadmetainfo_json', description='None') -# We extend the existing common definition of -# a section "single configuration calculation" -class ExampleCalculation(Calculation): +class Model(ArchiveSection): + m_def = Section() + + n_atoms = Quantity( + type=np.int32, description="""Number of atoms in the model system.""" + ) + + labels = Quantity( + type=str, shape=['n_atoms'], description="""Labels of the atoms.""" + ) + + positions = Quantity( + type=np.float64, shape=['n_atoms'], description="""Positions of the atoms.""" + ) + + lattice = Quantity( + type=np.float64, + shape=[3, 3], + description="""Lattice vectors of the model system.""", + ) + + +class Output(ArchiveSection): + m_def = Section() + + model = Quantity( + type=Reference(Model), description="""Reference to the model system.""" + ) + + energy = Quantity( + type=np.float64, + unit='eV', + description="""Value of the total energy of the system.""", + ) + + +class Simulation(ArchiveSection): + m_def = Section() + + code_name = Quantity( + type=str, description="""Name of the code used for the simulation.""" + ) + + code_version = Quantity(type=str, description="""Version of the code.""") + + date = Quantity(type=Datetime, description="""Execution date of the simulation.""") + + model = SubSection(sub_section=Model, repeats=True) + + output = SubSection(sub_section=Output, repeats=True) + + +# We extend the existing common definition of section Workflow +class ExampleWorkflow(Workflow): # We alter the default base class behavior to add all definitions to the existing # base class instead of inheriting from the base class m_def = Section(extends_base_section=True) diff --git a/exampleparser/parser.py b/exampleparser/parser.py index 7de5802..226911d 100644 --- a/exampleparser/parser.py +++ b/exampleparser/parser.py @@ -20,16 +20,12 @@ import numpy as np from nomad.datamodel import EntryArchive -from nomad.datamodel.metainfo.simulation.calculation import ( - Calculation, - Energy, - EnergyEntry, -) -from nomad.datamodel.metainfo.simulation.run import Program, Run -from nomad.datamodel.metainfo.simulation.system import Atoms, System +from nomad.datamodel.metainfo.workflow import Workflow from nomad.parsing.file_parser import Quantity, TextParser from nomad.units import ureg as units +from .metainfo.example import Model, Output, Simulation + """ This is a hello world style example for an example parser/converter. """ @@ -50,7 +46,7 @@ def str_to_sites(string): repeats=True, ), Quantity( - Atoms.lattice_vectors, + Model.lattice, r'(?:latice|cell): \((\d)\, (\d), (\d)\)\,?\s*\((\d)\, (\d), (\d)\)\,?\s*\((\d)\, (\d), (\d)\)\,?\s*', # noqa repeats=False, ), @@ -86,28 +82,27 @@ def parse(self, mainfile: str, archive: EntryArchive, logger): mainfile_parser.mainfile = mainfile mainfile_parser.parse() - run = Run() - date = datetime.datetime.strptime(mainfile_parser.date, '%Y/%m/%d') - run.program = Program( - name='super_code', - version=mainfile_parser.get('program_version'), - compilation_datetime=date.timestamp(), + simulation = Simulation( + code_name='super_code', code_version=mainfile_parser.get('program_version') ) + date = datetime.datetime.strptime(mainfile_parser.date, '%Y/%m/%d') + simulation.date = date for calculation in mainfile_parser.get('calculation', []): - system = System(atoms=Atoms()) + model = Model() - system.atoms.lattice_vectors = calculation.get('lattice_vectors') + model.lattice = calculation.get('lattice_vectors') sites = calculation.get('sites') - system.atoms.labels = [site[0] for site in sites] - system.atoms.positions = [site[1] for site in sites] - run.system.append(system) + model.labels = [site[0] for site in sites] + model.positions = [site[1] for site in sites] + simulation.model.append(model) - calc = Calculation(energy=Energy()) - calc.system_ref = system - calc.energy.total = EnergyEntry(value=calculation.get('energy') * units.eV) + output = Output() + output.model = model + output.energy = calculation.get('energy') * units.eV magic_source = calculation.get('magic_source') if magic_source is not None: - calc.x_example_magic_value = magic_source - run.calculation.append(calc) - archive.run.append(run) + archive.workflow2 = Workflow(x_example_magic_value=magic_source) + simulation.output.append(output) + # put the simulation section into archive data + archive.data = simulation diff --git a/tests/test_parser.py b/tests/test_parser.py index ba568b0..7df75d6 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -33,7 +33,7 @@ def test_example(parser): archive = EntryArchive() parser.parse('tests/data/example.out', archive, logging) - run = archive.run[0] - assert len(run.system) == 2 - assert len(run.calculation) == 2 - assert run.calculation[0].x_example_magic_value == 42 + sim = archive.data + assert len(sim.model) == 2 + assert len(sim.output) == 2 + assert archive.workflow2.x_example_magic_value == 42