Skip to content

Commit

Permalink
Merge pull request #1271 from pyiron/lammps_fto_dict
Browse files Browse the repository at this point in the history
Lammps: use to_dict() rather than to_hdf()
  • Loading branch information
jan-janssen authored Dec 28, 2023
2 parents dda4d77 + fa779a7 commit 6201dda
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .ci_support/environment-old.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies:
- pandas =2.0.3
- phonopy =2.20.0
- pint =0.18
- pyiron_base =0.6.11
- pyiron_base =0.6.16
- pylammpsmpi =0.2.7
- pyscal =2.10.4
- scikit-learn =1.2.1
Expand Down
2 changes: 1 addition & 1 deletion .ci_support/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies:
- pandas =2.1.4
- phonopy =2.21.0
- pint =0.23
- pyiron_base =0.6.15
- pyiron_base =0.6.16
- pylammpsmpi =0.2.10
- pyscal =2.10.18
- scikit-learn =1.3.2
Expand Down
28 changes: 15 additions & 13 deletions pyiron_atomistics/atomistics/job/atomistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,17 +286,12 @@ def from_hdf(self, hdf=None, group_name=None):
except ValueError:
pass

def to_hdf(self, hdf=None, group_name=None):
"""
Store the GenericJob in an HDF5 file
Args:
hdf (ProjectHDFio): HDF5 group object - optional
group_name (str): HDF5 subgroup name - optional
"""
super(AtomisticGenericJob, self).to_hdf(hdf=hdf, group_name=group_name)
with self._hdf5.open("input") as hdf5_input:
self._generic_input.to_hdf(hdf5_input)
def to_dict(self):
data_dict = super(AtomisticGenericJob, self).to_dict()
data_dict.update(
{"input/generic/" + k: v for k, v in self._generic_input.to_dict().items()}
)
return data_dict

def store_structure(self):
"""
Expand Down Expand Up @@ -801,10 +796,17 @@ def gui(self):
"""
ProjectGUI(self)

def _structure_to_hdf(self):
def _structure_to_dict(self):
if self.structure is not None and self._generic_input["structure"] == "atoms":
return {"structure/" + k: v for k, v in self.structure.to_dict().items()}
else:
return None

def _structure_to_hdf(self):
data_dict = self._structure_to_dict()
if data_dict is not None:
with self.project_hdf5.open("input") as hdf5_input:
self.structure.to_hdf(hdf5_input)
hdf5_input.write_dict_to_hdf(data_dict)

def _structure_from_hdf(self):
if (
Expand Down
12 changes: 5 additions & 7 deletions pyiron_atomistics/atomistics/structure/atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,22 +458,20 @@ def to_dict(self):
}
for el in self.species:
if isinstance(el.tags, dict):
if "new_species" not in hdf_structure.keys():
hdf_structure["new_species"] = {}
hdf_structure["new_species"][el.Abbreviation] = el.to_dict()
for k, v in el.to_dict().items():
hdf_structure["new_species/" + el.Abbreviation + "/" + k] = v
hdf_structure["species"] = [el.Abbreviation for el in self.species]
hdf_structure["indices"] = self.indices

for tag, value in self.arrays.items():
if tag in ["positions", "numbers", "indices"]:
continue
if "tags" not in hdf_structure.keys():
hdf_structure["tags"] = {}
hdf_structure["tags"][tag] = value.tolist()
hdf_structure["tags/" + tag] = value.tolist()

if self.cell is not None:
# Convert ASE cell object to numpy array before storing
hdf_structure["cell"] = {"cell": np.array(self.cell), "pbc": self.pbc}
hdf_structure["cell/cell"] = np.array(self.cell)
hdf_structure["cell/pbc"] = self.pbc

if self.has("initial_magmoms"):
hdf_structure["spins"] = self.spins
Expand Down
2 changes: 1 addition & 1 deletion pyiron_atomistics/atomistics/structure/periodic_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def to_dict(self):
self._dataset = {"Parameter": ["Parent"], "Value": [self.Parent]}
hdf_el["elementData"] = self._dataset
# "Dictionary of element tag static"
hdf_el["tagData"] = {key: self.tags[key] for key in self.tags.keys()}
hdf_el.update({"tagData/" + key: self.tags[key] for key in self.tags.keys()})
return hdf_el

def to_hdf(self, hdf):
Expand Down
104 changes: 60 additions & 44 deletions pyiron_atomistics/lammps/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,27 +450,26 @@ def collect_output(self):
"""
self.input.from_hdf(self._hdf5)
hdf_dict = self.collect_output_parser(
cwd=self.working_directory,
dump_h5_file_name="dump.h5",
dump_out_file_name="dump.out",
log_lammps_file_name="log.lammps",
hdf_dict = resolve_hierachical_dict(
data_dict=self.collect_output_parser(
cwd=self.working_directory,
dump_h5_file_name="dump.h5",
dump_out_file_name="dump.out",
log_lammps_file_name="log.lammps",
),
group_name="output",
)

# Write to hdf
with self.project_hdf5.open("output/generic") as hdf_output:
for k, v in hdf_dict["generic"].items():
hdf_output[k] = v

with self.project_hdf5.open("output/lammps") as hdf_output:
for k, v in hdf_dict["lammps"].items():
hdf_output[k] = v

if len(self.output.cells) > 0:
final_structure = self.get_structure(iteration_step=-1)
if final_structure is not None:
with self.project_hdf5.open("output") as hdf_output:
final_structure.to_hdf(hdf_output)
hdf_dict.update(
{
"output/structure/" + k: v
for k, v in final_structure.to_dict().items()
}
)
self.project_hdf5.write_dict_to_hdf(data_dict=hdf_dict)

def convergence_check(self):
if self._generic_input["calc_mode"] == "minimize":
Expand Down Expand Up @@ -722,21 +721,13 @@ def calc_vcsgc(
rotation_matrix=rotation_matrix,
)

# define hdf5 input and output
def to_hdf(self, hdf=None, group_name=None):
"""
Args:
hdf:
group_name:
Returns:
"""
super(LammpsBase, self).to_hdf(hdf=hdf, group_name=group_name)
self._structure_to_hdf()
self.input.to_hdf(self._hdf5)
def to_dict(self):
data_dict = super(LammpsBase, self).to_dict()
lammps_dict = self._structure_to_dict() | self.input.to_dict()
data_dict.update({"input/" + k: v for k, v in lammps_dict.items()})
return data_dict

# define hdf5 output
def from_hdf(self, hdf=None, group_name=None): # TODO: group_name should be removed
"""
Expand All @@ -749,7 +740,11 @@ def from_hdf(self, hdf=None, group_name=None): # TODO: group_name should be rem
"""
super(LammpsBase, self).from_hdf(hdf=hdf, group_name=group_name)
self._structure_from_hdf()
self.input.from_hdf(self._hdf5)
self.input.from_dict(
data_dict=self._hdf5.read_dict_from_hdf(
group_paths=["input", "input/control_inp", "input/potential_inp"]
)
)

def write_restart_file(self, filename="restart.out"):
"""
Expand Down Expand Up @@ -1054,30 +1049,51 @@ def _load_default_bond_params(self):
self.bond_dict["O"]["bond_type_list"] = [1]
self.bond_dict["O"]["angle_type_list"] = [1]

def from_dict(self, data_dict):
self.control.from_dict(data_dict["input"]["control_inp"])
self.potential.from_dict(data_dict["input"]["potential_inp"])
if "bond_dict" in data_dict["input"].keys():
self.bond_dict = data_dict["input"]["bond_dict"]

def to_dict(self):
return {
self.control.table_name + "/" + k: v
for k, v in self.control.to_dict().items()
} | {
self.potential.table_name + "/" + k: v
for k, v in self.potential.to_dict().items()
}

def to_hdf(self, hdf5):
"""
Args:
hdf5:
Returns:
"""
with hdf5.open("input") as hdf5_input:
self.control.to_hdf(hdf5_input)
self.potential.to_hdf(hdf5_input)
hdf5_input.write_dict_to_hdf(data_dict=self.to_dict())

def from_hdf(self, hdf5):
"""
Args:
hdf5:
Returns:
"""
with hdf5.open("input") as hdf5_input:
self.control.from_hdf(hdf5_input)
self.potential.from_hdf(hdf5_input)
if "bond_dict" in hdf5_input.list_nodes():
self.bond_dict = hdf5_input["bond_dict"]
self.from_dict(
data_dict=hdf5.read_dict_from_hdf(
["input", "input/control_inp", "input/potential_inp"]
)
)


def resolve_hierachical_dict(data_dict, group_name=""):
return_dict = {}
if len(group_name) > 0 and group_name[-1] != "/":
group_name = group_name + "/"
for k, v in data_dict.items():
if isinstance(v, dict):
for sk, sv in v.items():
return_dict[group_name + k + "/" + sk] = sv
else:
return_dict[group_name + k] = v
return return_dict
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies = [
"pandas==2.1.4",
"phonopy==2.21.0",
"pint==0.23",
"pyiron_base==0.6.15",
"pyiron_base==0.6.16",
"pylammpsmpi==0.2.10",
"scipy==1.11.4",
"scikit-learn==1.3.2",
Expand Down

0 comments on commit 6201dda

Please sign in to comment.