66from ase .io import write
77from adis_tools .parsers import parse_pw
88import matplotlib .pyplot as plt
9+ import numpy as np
910
1011
1112def 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
6364def 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
0 commit comments