Skip to content

Commit

Permalink
moved interaction setting to MDParser
Browse files Browse the repository at this point in the history
  • Loading branch information
jrudz committed Oct 31, 2023
1 parent 7a7cfd3 commit 27d395f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 105 deletions.
48 changes: 1 addition & 47 deletions atomisticparsers/gromacs/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,53 +833,7 @@ def parse_method(self):
self.logger.error('Error parsing interactions.')

interactions = self.traj_parser.get_interactions()
# TODO The section below is duplicated in the lammps and gromacs parsers. We should really move them
# to the MDAnalysis parser, but you must be careful because there are other contributions to interactions
# from other (sub)parsers
interaction_key_list = Interaction.__dict__.keys()
interaction_dict = {}
interaction_keys_remove = ['__module__', '__doc__', 'm_def']
interaction_key_list = [key for key in interaction_key_list if key not in interaction_keys_remove]
for interaction_key in interaction_key_list:
interaction_dict[interaction_key] = np.array([interaction.get(interaction_key) for interaction in interactions], dtype=object)
interaction_dict = {key: val for key, val in interaction_dict.items()}
interaction_types = np.unique(interaction_dict['type']) if interaction_dict.get('type') is not None else []
for interaction_type in interaction_types:
sec_interaction = sec_model.m_create(Interaction)
interaction_indices = np.where(interaction_dict['type'] == interaction_type)[0]
sec_interaction.type = interaction_type
sec_interaction.n_inter = len(interaction_indices)
sec_interaction.n_atoms
n_atoms = interaction_dict.get('n_atoms')[interaction_indices][0] # assuming the number of atoms per interaction type is fixed!
for key, val in interaction_dict.items():
if key == 'type':
continue
interaction_vals = val[interaction_indices]
if type(interaction_vals[0]).__name__ == 'ndarray':
interaction_vals = np.array([vals.tolist() for vals in interaction_vals], dtype=object)
if interaction_vals.all() is None:
continue
if key == 'parameters':
interaction_vals = interaction_vals.tolist()
elif key == 'n_atoms':
interaction_vals = interaction_vals[0]
try:
setattr(sec_interaction, key, interaction_vals)
except Exception:
self.logger.warning(
'Some issue trying to store ' + key + 'in Interactions section.'
' Possibly a data type problem. Ignoring these values.')

if not sec_interaction.get('n_atoms'):
sec_interaction.n_atoms = len(sec_interaction.get('atom_indices')[0]) if sec_interaction.get('atom_indices') is not None else None

# OLD VERSION WITHOUT GROUPINGS
# for interaction in interactions:
# sec_interaction = sec_model.m_create(Interaction)
# for key, val in interaction.items():
# print(key)
# print(val)
# setattr(sec_interaction, key, val)
self.parse_interactions(interactions, sec_model)

input_parameters = self.log_parser.get('input_parameters', {})
sec_force_calculations = sec_force_field.m_create(ForceCalculations)
Expand Down
59 changes: 1 addition & 58 deletions atomisticparsers/lammps/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,18 +1085,6 @@ def parse_method(self):
# Can you add the implementation here, and then we can make the MDA implementation below as a backup?
# Can you also get the charges somehow?

# This is storing the input parameters/command for the interaction at the moment, which is already stored in the "force_calculations" section
# interactions = self.log_parser.get_interactions()
# if not interactions:
# interactions = self.data_parser.get_interactions()

# for interaction in interactions:
# if not interaction[0] or interaction[1] is None or np.size(interaction[1]) == 0:
# continue
# sec_interaction = sec_model.m_create(Interaction)
# sec_interaction.type = str(interaction[0])
# sec_interaction.parameters = [[float(ai) for ai in a] for a in interaction[1]]

# parse method with MDAnalysis (should be a backup for the charges and masses...but the interactions are most easily read from the MDA universe right now)
n_atoms = self.traj_parsers.eval('get_n_atoms', 0)
if n_atoms is not None:
Expand All @@ -1108,52 +1096,7 @@ def parse_method(self):

# TODO address case types are numbered instead of giving atom labels (fix tests accordingly)
interactions = self._mdanalysistraj_parser.get_interactions()
# TODO The section below is duplicated in the lammps and gromacs parsers. We should really move them
# to the MDAnalysis parser, but you must be careful because there are other contributions to interactions
# from other (sub)parsers
interaction_key_list = Interaction.__dict__.keys()
interaction_dict = {}
interaction_keys_remove = ['__module__', '__doc__', 'm_def']
interaction_key_list = [key for key in interaction_key_list if key not in interaction_keys_remove]
for interaction_key in interaction_key_list:
interaction_dict[interaction_key] = np.array([interaction.get(interaction_key) for interaction in interactions], dtype=object)
interaction_dict = {key: val for key, val in interaction_dict.items()}
interaction_types = np.unique(interaction_dict['type']) if interaction_dict.get('type') is not None else []
for interaction_type in interaction_types:
sec_interaction = sec_model.m_create(Interaction)
interaction_indices = np.where(interaction_dict['type'] == interaction_type)[0]
sec_interaction.type = interaction_type
sec_interaction.n_inter = len(interaction_indices)
sec_interaction.n_atoms
n_atoms = interaction_dict.get('n_atoms')[interaction_indices][0] # assuming the number of atoms per interaction type is fixed!
for key, val in interaction_dict.items():
if key == 'type':
continue
interaction_vals = val[interaction_indices]
if type(interaction_vals[0]).__name__ == 'ndarray':
interaction_vals = np.array([vals.tolist() for vals in interaction_vals], dtype=object)
if interaction_vals.all() is None:
continue
if key == 'parameters':
interaction_vals = interaction_vals.tolist()
elif key == 'n_atoms':
interaction_vals = interaction_vals[0]
try:
setattr(sec_interaction, key, interaction_vals)
except Exception:
self.logger.warning(
'Some issue trying to store ' + key + 'in Interactions section.'
' Possibly a data type problem. Ignoring these values.')

if not sec_interaction.get('n_atoms'):
sec_interaction.n_atoms = len(sec_interaction.get('atom_indices')[0]) if sec_interaction.get('atom_indices') is not None else None

# OLD VERSION WITHOUT GROUPINGS
# interactions = interactions if interactions is not None else []
# for interaction in interactions:
# sec_interaction = sec_model.m_create(Interaction)
# for key, val in interaction.items():
# setattr(sec_interaction, key, val)
self.parse_interactions(interactions, sec_model)

# Force Calculation Parameters
sec_force_calculations = sec_force_field.m_create(ForceCalculations)
Expand Down
39 changes: 39 additions & 0 deletions atomisticparsers/utils/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from nomad.datamodel.metainfo.simulation.system import System
from nomad.datamodel.metainfo.simulation.calculation import Calculation
from nomad.datamodel.metainfo.simulation.workflow import MolecularDynamics
from nomad.datamodel.metainfo.simulation.method import Interaction


# TODO put this in nomad.parsing
Expand Down Expand Up @@ -200,3 +201,41 @@ def parse_md_workflow(self, data: Dict[str, Any]) -> None:
sec_workflow = MolecularDynamics()
self.parse_section(data, sec_workflow)
self.archive.workflow2 = sec_workflow

def parse_interactions(self, interactions: Dict[str, Any], sec_model: MSection) -> None:

interaction_key_list = Interaction.__dict__.keys()
interaction_dict = {}
interaction_keys_remove = ['__module__', '__doc__', 'm_def']
interaction_key_list = [key for key in interaction_key_list if key not in interaction_keys_remove]
for interaction_key in interaction_key_list:
interaction_dict[interaction_key] = np.array([interaction.get(interaction_key) for interaction in interactions], dtype=object)
interaction_dict = {key: val for key, val in interaction_dict.items()}
interaction_types = np.unique(interaction_dict['type']) if interaction_dict.get('type') is not None else []
for interaction_type in interaction_types:
sec_interaction = sec_model.m_create(Interaction)
interaction_indices = np.where(interaction_dict['type'] == interaction_type)[0]
sec_interaction.type = interaction_type
sec_interaction.n_inter = len(interaction_indices)
sec_interaction.n_atoms
for key, val in interaction_dict.items():
if key == 'type':
continue
interaction_vals = val[interaction_indices]
if type(interaction_vals[0]).__name__ == 'ndarray':
interaction_vals = np.array([vals.tolist() for vals in interaction_vals], dtype=object)
if interaction_vals.all() is None:
continue
if key == 'parameters':
interaction_vals = interaction_vals.tolist()
elif key == 'n_atoms':
interaction_vals = interaction_vals[0]
try:
setattr(sec_interaction, key, interaction_vals)
except Exception:
self.logger.warning(
'Some issue trying to store ' + key + 'in Interactions section.'
' Possibly a data type problem. Ignoring these values.')

if not sec_interaction.get('n_atoms'):
sec_interaction.n_atoms = len(sec_interaction.get('atom_indices')[0]) if sec_interaction.get('atom_indices') is not None else None

0 comments on commit 27d395f

Please sign in to comment.