77from adis_tools .parsers import parse_pw
88import matplotlib .pyplot as plt
99import numpy as np
10+ from optimade .adapters .structures .ase import from_ase_atoms , get_ase_atoms
11+ from optimade .models .structures import StructureResourceAttributes , StructureResource
1012
1113
1214def write_input (input_dict , working_directory = "." ):
1315 filename = os .path .join (working_directory , "input.pwi" )
1416 os .makedirs (working_directory , exist_ok = True )
1517 write (
1618 filename = filename ,
17- images = Atoms ( ** input_dict ["structure" ]),
19+ images = json_to_ase ( atoms_json = input_dict ["structure" ]),
1820 Crystal = True ,
1921 kpts = input_dict ["kpts" ],
2022 input_data = {
@@ -31,7 +33,7 @@ def write_input(input_dict, working_directory="."):
3133def collect_output (working_directory = "." ):
3234 output = parse_pw (os .path .join (working_directory , "pwscf.xml" ))
3335 return {
34- "structure" : atoms_to_json_dict (atoms = output ["ase_structure" ]),
36+ "structure" : ase_to_json (atoms = output ["ase_structure" ]),
3537 "energy" : output ["energy" ],
3638 "volume" : output ["ase_structure" ].get_volume (),
3739 }
@@ -53,12 +55,12 @@ def calculate_qe(working_directory, input_dict):
5355def generate_structures (structure , strain_lst ):
5456 structure_lst = []
5557 for strain in strain_lst :
56- structure_strain = Atoms ( ** structure )
58+ structure_strain = json_to_ase ( atoms_json = structure )
5759 structure_strain .set_cell (
5860 structure_strain .cell * strain ** (1 / 3 ), scale_atoms = True
5961 )
6062 structure_lst .append (structure_strain )
61- return {f"s_{ i } " : atoms_to_json_dict (atoms = s ) for i , s in enumerate (structure_lst )}
63+ return {f"s_{ i } " : ase_to_json (atoms = s ) for i , s in enumerate (structure_lst )}
6264
6365
6466def plot_energy_volume_curve (volume_lst , energy_lst ):
@@ -74,40 +76,14 @@ def get_bulk_structure(element, a, cubic):
7476 a = a ,
7577 cubic = cubic ,
7678 )
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
79+ return ase_to_json (atoms = ase_atoms )
80+
81+
82+ def ase_to_json (atoms ):
83+ struct_opt = from_ase_atoms (atoms = atoms )
84+ return json .dumps (struct_opt .model_dump (mode = "json" ))
85+
86+
87+ def json_to_ase (atoms_json ):
88+ structure_restore = StructureResourceAttributes .model_validate_json (atoms_json ))
89+ return get_ase_atoms (optimade_structure = StructureResource (id = "ase" , attributes = structure_restore ))
0 commit comments