-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseline_optimize.py
More file actions
225 lines (161 loc) · 8.47 KB
/
baseline_optimize.py
File metadata and controls
225 lines (161 loc) · 8.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import numpy as np
from pymatgen.core import Structure
from chgnet.model.model import CHGNet
from chgnet.model import StructOptimizer
import datetime
from io import StringIO
import sys
from operator import add
from pathlib import Path
from tempfile import TemporaryDirectory
import pickle
import argparse
from pandas import DataFrame
from ase.io import read
from ase.calculators.calculator import *
from ase.geometry.cell import *
from ase.optimize.sciopt import OptimizerConvergenceError
from analyze_res import save_stats
def relax(atoms, optimizer_class, fmax, steps, relax_cell=False):
class Capturing(list):
def __enter__(self):
self._stdout = sys.stdout
sys.stdout = self._stringio = StringIO()
return self
def __exit__(self, *args):
self.extend(self._stringio.getvalue().splitlines())
del self._stringio # free up some memory
sys.stdout = self._stdout
relaxer = StructOptimizer(optimizer_class=optimizer_class, use_device='cpu')
step_list = []
e_step_list = []
time_list = []
energy_list = []
fmax_list = []
traj = {}
start_time = None
with Capturing() as output:
try:
start_t = datetime.datetime.now()
prediction = relaxer.relax(atoms.copy(), fmax=fmax, steps=steps, relax_cell=relax_cell, verbose=True, save_path="tmp.pkl")
temp_atoms = prediction['final_structure']
traj = {
"energy": prediction['trajectory'].energies,
"forces": prediction['trajectory'].forces,
"stresses": prediction['trajectory'].stresses,
"magmoms": prediction['trajectory'].magmoms,
"atom_positions": prediction['trajectory'].atom_positions,
"cell": prediction['trajectory'].cells,
"atomic_number": prediction['trajectory'].atoms.get_atomic_numbers(),
}
with TemporaryDirectory() as temp_dir:
temp_atoms.to_file(f"{temp_dir}/relaxed.cif")
new_atoms = read(f"{temp_dir}/relaxed.cif")
except (OptimizerConvergenceError, RuntimeError, IndexError) as e:
print(f"Optimizer {optimizer_class} for {atoms}, error: {e}", sys.stderr)
opt_t = (datetime.datetime.now() - start_t).total_seconds()
return failed_res_list(opt_t), None, traj
for j in range(1, len(output)):
try:
separ_str = output[j].replace(" ", " ").replace("[ ", "[").replace(" ]", "]").split()
if start_time == None:
start_time = datetime.datetime.strptime(separ_str[2], "%H:%M:%S")
energy_list.append(float(separ_str[3])/len(atoms))
fmax_list.append(float(separ_str[4]))
time_list.append((datetime.datetime.strptime(separ_str[2], "%H:%M:%S") - start_time).total_seconds())
step_list.append(j - 1)
if "[" in separ_str[1]:
e_step_list.append(int(separ_str[1].split("[")[1].split("]")[0]))
else:
e_step_list.append(step_list[-1])
except ValueError as e:
print(f"Value error in the output list: {e}", sys.stderr)
continue
return {"Step": step_list, "E_step": e_step_list, "Energy": energy_list, "Time": time_list, "Fmax": fmax_list}, new_atoms, traj
def failed_res_list(secs):
return {f"Step": [0, 1], "E_step": [0, 1], "Energy": [0, 0], "Time": [0, secs], "Fmax": [1, 1]}
def check_atoms(atoms, a_len):
if atoms == None:
return False
# if structure lost some atoms
if len(atoms.get_atomic_numbers()) < a_len:
return False
return True
def chgnet_optimization_logs(opti,
starting_structures_dir="starting_structures",
final_structures_dir="final_structures",
baselines_dir="baselines"):
chgnet = CHGNet.load()
cif_files = [f for f in os.listdir(starting_structures_dir) if os.path.isfile(f"{starting_structures_dir}/{f}") and f.endswith(".cif")]
cif_files.sort()
steps = 1000
Path(baselines_dir).mkdir(parents=True, exist_ok=True)
res_list = {"Step": [], "E_step": [], "Energy": [], "Time": [], "Fmax": []}
failures_n = 0
total_time = 0
relax_cell = False
Path(f"{final_structures_dir}").mkdir(parents=True, exist_ok=True)
for cif in cif_files:
atoms = Structure.from_file(f"{starting_structures_dir}/{cif}")
if opti == "FIRE+BFGSLineSearch":
start_time = datetime.datetime.now()
res_list_1, new_atoms, traj = relax(atoms, "FIRE", 0.1, 250, relax_cell=relax_cell)
if not check_atoms(new_atoms, len(atoms.atomic_numbers)):
opt_t = (datetime.datetime.now() - start_time).total_seconds()
res_list_1 = failed_res_list(opt_t)
else:
res_list_2, new_atoms, traj_2 = relax(new_atoms, "BFGSLineSearch", 0.05, 750, relax_cell=relax_cell)
# first check if the final structure has not lost any atoms
if not check_atoms(new_atoms, len(atoms.atomic_numbers)):
opt_t = (datetime.datetime.now() - start_time).total_seconds()
res_list_1 = failed_res_list(opt_t)
else:
fire_steps = len(res_list_1["Step"])
res_list_1["Step"].extend(map(add, res_list_2["Step"], [fire_steps] * len(res_list_2["Step"])))
res_list_1["E_step"].extend(map(add, res_list_2["E_step"], [fire_steps] * len(res_list_2["E_step"])))
res_list_1["Energy"].extend(res_list_2["Energy"])
res_list_1["Time"].extend(map(add, res_list_2["Time"], [res_list_1["Time"][-1]] * len(res_list_2["Time"])))
res_list_1["Fmax"].extend(res_list_2["Fmax"])
try:
for key in traj:
traj[key] = np.concatenate((traj[key], traj_2[key]))
except ValueError as e:
print(f"Value error in the output list: {e}")
for key in traj:
print(f"For Key: {key}, FIRE len: {len(traj[key])}, BFGS len: {len(traj_2[key])}")
print("")
quit()
total_time += (datetime.datetime.now() - start_time).total_seconds()
else:
start_time = datetime.datetime.now()
res_list_1, new_atoms, traj = relax(atoms, opti, 0.05, steps, relax_cell=relax_cell)
total_time += (datetime.datetime.now() - start_time).total_seconds()
if not check_atoms(new_atoms, len(atoms.atomic_numbers)):
opt_t = (datetime.datetime.now() - start_time).total_seconds()
res_list_1 = failed_res_list(opt_t)
if len(res_list_1["Step"]) == 2 and res_list_1["Fmax"][1] == 1:
failures_n += 1
else:
new_atoms.write(f"{final_structures_dir}/{cif}")
pickle.dump(traj, open(f"{final_structures_dir}/{cif.replace('.cif', '.pkl')}", "wb"))
for k in res_list:
res_list[k].extend(res_list_1[k])
df = DataFrame(res_list)
df.to_csv(f"{baselines_dir}/{opti}_opt_traj.csv")
save_stats(df, baselines_dir)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='The optimization of a test set with a baseline')
parser.add_argument("--method", type=str, default="BFGSLineSearch",
help="specific method to optimize, can be BFGS, BFGSLineSearch, MDMin, SciPyFminCG, FIRE, or FIRE+BFGSLineSearch")
parser.add_argument("--input_dir", type=str, default="starting_structures/SrTiO3x8",
help="input dir with starting structures")
parser.add_argument("--output_dir", type=str, default="baseline_output",
help="The output dir to save the optimized structures and the energy history")
args = parser.parse_args()
opti = args.method
starting_structures_dir = args.input_dir
final_structures_dir = str(Path(args.output_dir) / f"final_structures")
baselines_dir = args.output_dir
chgnet_optimization_logs(opti, starting_structures_dir=starting_structures_dir,
final_structures_dir=final_structures_dir,
baselines_dir=baselines_dir)