6
6
from ase .io import write
7
7
from adis_tools .parsers import parse_pw
8
8
import matplotlib .pyplot as plt
9
+ import numpy as np
9
10
10
11
11
12
def write_input (input_dict , working_directory = "." ):
@@ -57,7 +58,7 @@ def generate_structures(structure, strain_lst):
57
58
structure_strain .cell * strain ** (1 / 3 ), scale_atoms = True
58
59
)
59
60
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 )}
61
62
62
63
63
64
def plot_energy_volume_curve (volume_lst , energy_lst ):
@@ -67,9 +68,46 @@ def plot_energy_volume_curve(volume_lst, energy_lst):
67
68
plt .savefig ("evcurve.png" )
68
69
69
70
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 ,
73
74
a = a ,
74
75
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