Skip to content

Commit b92f4df

Browse files
authored
Use JSON structure (#12)
1 parent 843d346 commit b92f4df

File tree

4 files changed

+700
-8
lines changed

4 files changed

+700
-8
lines changed

jobflow_to_pyiron_base_qe.ipynb

Lines changed: 323 additions & 1 deletion
Large diffs are not rendered by default.

pyiron_base_to_jobflow_qe.ipynb

Lines changed: 333 additions & 1 deletion
Large diffs are not rendered by default.

quantum_espresso_workflow.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from ase.io import write
77
from adis_tools.parsers import parse_pw
88
import matplotlib.pyplot as plt
9+
import numpy as np
910

1011

1112
def write_input(input_dict, working_directory="."):
@@ -57,7 +58,7 @@ def generate_structures(structure, strain_lst):
5758
structure_strain.cell * strain ** (1 / 3), scale_atoms=True
5859
)
5960
structure_lst.append(structure_strain)
60-
return {str(i): s.todict() for i, s in enumerate(structure_lst)}
61+
return {str(i): atoms_to_json_dict(atoms=s) for i, s in enumerate(structure_lst)}
6162

6263

6364
def plot_energy_volume_curve(volume_lst, energy_lst):
@@ -67,9 +68,46 @@ def plot_energy_volume_curve(volume_lst, energy_lst):
6768
plt.savefig("evcurve.png")
6869

6970

70-
def get_bulk_structure(name, a, cubic):
71-
return bulk(
72-
name=name,
71+
def get_bulk_structure(element, a, cubic):
72+
ase_atoms = bulk(
73+
name=element,
7374
a=a,
7475
cubic=cubic,
75-
).todict()
76+
)
77+
return atoms_to_json_dict(atoms=ase_atoms)
78+
79+
80+
def atoms_to_json_dict(atoms):
81+
"""
82+
Convert an ASE Atoms object to a fully JSON-serializable dictionary
83+
that uses only Python base data types.
84+
85+
Parameters:
86+
-----------
87+
atoms : ase.Atoms
88+
The Atoms object to convert
89+
90+
Returns:
91+
--------
92+
dict
93+
A dictionary representation using only Python base types
94+
"""
95+
# Get the dictionary representation from ASE
96+
atoms_dict = atoms.todict()
97+
98+
# Create a new dictionary with JSON-serializable values
99+
json_dict = {}
100+
101+
# Convert numpy arrays to lists
102+
for key, value in atoms_dict.items():
103+
if isinstance(value, np.ndarray):
104+
# Convert numpy boolean values to Python booleans
105+
if value.dtype == np.bool_ or value.dtype == bool:
106+
json_dict[key] = value.tolist()
107+
# Convert numpy arrays of numbers to Python lists
108+
else:
109+
json_dict[key] = value.tolist()
110+
else:
111+
json_dict[key] = value
112+
113+
return json_dict

workflow_qe.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"31": "python_workflow_definition.pyiron_base.get_list"
3535
},
3636
"edges": [
37-
{"target": 0, "targetHandle": "name", "source": 9, "sourceHandle": null},
37+
{"target": 0, "targetHandle": "element", "source": 9, "sourceHandle": null},
3838
{"target": 0, "targetHandle": "a", "source": 10, "sourceHandle": null},
3939
{"target": 0, "targetHandle": "cubic", "source": 11, "sourceHandle": null},
4040
{"target": 1, "targetHandle": "working_directory", "source": 12, "sourceHandle": null},

0 commit comments

Comments
 (0)