Skip to content

Commit

Permalink
Merge pull request #1 from FAIRmat-NFDI/migrate_from_nomadcoe
Browse files Browse the repository at this point in the history
removed old parsing functions; redid outputs section; moved parsing u…
  • Loading branch information
JFRudzinski authored Oct 1, 2024
2 parents 749ee86 + afababf commit 85a7f9c
Show file tree
Hide file tree
Showing 10 changed files with 1,351 additions and 64 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ maintainers = [
{ name = "Joseph Rudzinski", email = "[email protected]" },
]
license = { file = "LICENSE" }
dependencies = ["nomad-lab@git+https://github.com/nomad-coe/nomad.git@develop", "nomad-simulations>=0.0.3"]
dependencies = ["nomad-lab@file:///home/jfrudzinski/work/soft/nomad_2024", "nomad-simulations>=0.0.3"]

[project.urls]
Repository = "https://github.com/FAIRmat-NFDI/nomad-parser-h5md"
Expand Down Expand Up @@ -109,7 +109,7 @@ where = ["src"]

[project.entry-points.'nomad.plugin']
h5md_parser_entry_point = "nomad_parser_h5md.parsers:h5md_parser_entry_point"
schema_package_entry_point = "nomad_parser_h5md.schema_packages:schema_package_entry_point"
h5md_schema_package_entry_point = "nomad_parser_h5md.schema_packages:h5md_schema_package_entry_point"



Expand Down
5 changes: 4 additions & 1 deletion src/nomad_parser_h5md/parsers/mdanalysisparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@

from nomad.units import ureg
from nomad.parsing.file_parser import FileParser
from nomad.atomutils import BeadGroup, shifted_correlation_average
from simulationworkflowschema.molecular_dynamics import (
BeadGroup,
shifted_correlation_average,
)

MOL = 6.022140857e23

Expand Down
100 changes: 73 additions & 27 deletions src/nomad_parser_h5md/parsers/mdparserutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
from runschema.method import Interaction, Model
from simulationworkflowschema import MolecularDynamics

# nomad-simulations
from nomad_simulations.schema_packages.outputs import (
TotalEnergy,
TotalForce,
TrajectoryOutputs,
)
from nomad_simulations.schema_packages.properties.energies import EnergyContribution
from nomad_simulations.schema_packages.properties.forces import ForceContribution
from nomad_simulations.schema_packages.general import Simulation
from nomad_simulations.schema_packages.atoms_state import AtomsState
from nomad_simulations.schema_packages.model_system import AtomicCell, ModelSystem


class MDParser(Parser):
def __init__(self, **kwargs) -> None:
Expand Down Expand Up @@ -120,7 +132,13 @@ def parse(self, *args, **kwargs):
self._trajectory_steps_sampled = []
super().parse(*args, **kwargs)

def parse_trajectory_step(self, data: Dict[str, Any]) -> None:
def parse_trajectory_step(
self,
data: Dict[str, Any],
simulation: Simulation,
model_system: ModelSystem = None,
atomic_cell: AtomicCell = None,
) -> None:
"""
Create a system section and write the provided data.
"""
Expand All @@ -129,43 +147,70 @@ def parse_trajectory_step(self, data: Dict[str, Any]) -> None:

if (step := data.get('step')) is not None and step not in self.trajectory_steps:
return
if model_system is None:
model_system = ModelSystem()
if atomic_cell is None:
atomic_cell = AtomicCell()

if self.archive.run:
sec_run = self.archive.run[-1]
else:
sec_run = Run()
self.archive.run.append(sec_run)
atomic_cell_dict = data.pop('atomic_cell')
atom_labels = atomic_cell_dict.pop('labels')
for label in atom_labels:
atoms_state = AtomsState(
chemical_symbol=label
) # ? how can I customize AtomsState within the parser?
atomic_cell.atoms_state.append(atoms_state)
self.parse_section(atomic_cell_dict, atomic_cell)
model_system.cell.append(atomic_cell)
self.parse_section(data, model_system)
simulation.model_system.append(model_system)

sec_system = System()
sec_run.system.append(sec_system)
self.parse_section(data, sec_system)

def parse_thermodynamics_step(self, data: Dict[str, Any]) -> None:
"""
Create a calculation section and write the provided data.
"""
def parse_output_step(
self,
data: Dict[str, Any],
simulation: Simulation,
output: TrajectoryOutputs = None,
) -> bool:
if self.archive is None:
return
return False

if (
step := data.get('step')
) is not None and step not in self.thermodynamics_steps:
return
return False

if self.archive.run:
sec_run = self.archive.run[-1]
else:
sec_run = Run()
self.archive.run.append(sec_run)
sec_calc = Calculation()
sec_run.calculation.append(sec_calc)
if output is None:
output = TrajectoryOutputs()

self.parse_section(data, sec_calc)
energy_contributions = data.get('total_energies', {}).pop('contributions', {})
force_contributions = data.get('total_forces', {}).pop('contributions', {})
self.parse_section(data, output)
try:
system_ref_index = self.trajectory_steps.index(sec_calc.step)
sec_calc.system_ref = sec_run.system[system_ref_index]
system_ref_index = self.trajectory_steps.index(output.step)
output.model_system_ref = simulation.model_system[system_ref_index]
except Exception:
pass
self.logger.warning('Could not set system reference in parsing of outputs.')

if energy_contributions:
if len(output.total_energies) == 0:
output.total_energies.append(TotalEnergy())

for energy_dict in energy_contributions:
energy = EnergyContribution() # self.energy_classes[energy_label]()
output.total_energies[-1].contributions.append(energy)
self.parse_section(energy_dict, energy)

if force_contributions:
if len(output.total_forces) == 0:
output.total_forces.append(TotalForce())

for force_dict in force_contributions:
force = ForceContribution() # self.force_classes[force_label]()
output.total_forces[-1].contributions.append(force)
self.parse_section(force_dict, force)

simulation.outputs.append(output)

return True

def parse_md_workflow(self, data: Dict[str, Any]) -> None:
"""
Expand All @@ -178,6 +223,7 @@ def parse_md_workflow(self, data: Dict[str, Any]) -> None:
self.parse_section(data, sec_workflow)
self.archive.workflow2 = sec_workflow

# TODO Adapt these interaction functions for the new schema
def parse_interactions(self, interactions: List[Dict], sec_model: MSection) -> None:
if not interactions:
return
Expand Down
Loading

0 comments on commit 85a7f9c

Please sign in to comment.