-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot_enthalpy_binary_ase_objects.py
More file actions
277 lines (211 loc) · 9.5 KB
/
plot_enthalpy_binary_ase_objects.py
File metadata and controls
277 lines (211 loc) · 9.5 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 25 11:56:29 2022
@author: 7ml
"""
import os
import numpy as np
import matplotlib.pyplot as plt
import shutil
import torch
from torch_geometric.data import Data
from torch_geometric.transforms import RadiusGraph, Distance
from torch import tensor
from scipy.interpolate import griddata
from os import walk
from ase.io.vasp import read_vasp_out
plt.rcParams.update({"font.size": 20})
from utils import nsplit, flatten
from mpi4py import MPI
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
plt.rcParams.update({"font.size": 20})
pure_elements_dictionary = {'V': 23, 'Nb': 41, 'Ta': 73}
def getcolordensity(xdata, ydata):
###############################
nbin = 20
hist2d, xbins_edge, ybins_edge = np.histogram2d(
x=xdata, y=ydata, bins=[nbin, nbin]
)
xbin_cen = 0.5 * (xbins_edge[0:-1] + xbins_edge[1:])
ybin_cen = 0.5 * (ybins_edge[0:-1] + ybins_edge[1:])
BCTY, BCTX = np.meshgrid(ybin_cen, xbin_cen)
hist2d = hist2d / np.amax(hist2d)
print(np.amax(hist2d))
bctx1d = np.reshape(BCTX, len(xbin_cen) * nbin)
bcty1d = np.reshape(BCTY, len(xbin_cen) * nbin)
loc_pts = np.zeros((len(xbin_cen) * nbin, 2))
loc_pts[:, 0] = bctx1d
loc_pts[:, 1] = bcty1d
hist2d_norm = griddata(
loc_pts,
hist2d.reshape(len(xbin_cen) * nbin),
(xdata, ydata),
method="linear",
fill_value=0,
) # np.nan)
return hist2d_norm
def plot_data(source_path, elements_list):
assert len(elements_list) == 2, "Plots are supported only for binaries"
min_atom_number = min( pure_elements_dictionary[elements_list[0]], pure_elements_dictionary[elements_list[1]] )
element_selected_for_plots = elements_list[0] if min_atom_number == pure_elements_dictionary[elements_list[0]] else elements_list[1]
dataset_loc = load_raw_data(source_path)
radius = 8.0
loop = False
max_neighbours = 10000
# Compute edges
compute_edges = RadiusGraph(
r=radius,
loop=loop,
max_num_neighbors=max_neighbours,
)
"""
dataset_loc [:] = [compute_edges(data) for data in dataset_loc ]
compute_edge_lengths = Distance(norm=False, cat=True)
dataset_loc[:] = [compute_edge_lengths(data) for data in dataset_loc]
min_edge_length = float('inf')
max_edge_length = - float('inf')
for data in dataset_loc:
min_edge_length = min(min_edge_length, torch.min(data.edge_attr).item())
max_edge_length = max(max_edge_length, torch.max(data.edge_attr).item())
min_edge_length = comm.allreduce(min_edge_length, op=min)
max_edge_length = comm.allreduce(max_edge_length, op=max)
"""
xdata_loc = [sum(data.x[:, 0] == pure_elements_dictionary[element_selected_for_plots]).item() / data.num_nodes for data in dataset_loc]
formation_enthalpy_loc = [data.y[0].item() for data in dataset_loc]
assert len(xdata_loc) == len(formation_enthalpy_loc)
tuples_loc = [(composition, enthalpy) for composition, enthalpy in zip(xdata_loc,formation_enthalpy_loc)]
tuples_all = comm.gather(tuples_loc, root=0)
if rank == 0:
#print("Shortest edge length: ", min_edge_length)
#print("Longest edge length: ", max_edge_length)
tuples = flatten(tuples_all)
xdata = [elem[0] for elem in tuples]
formation_enthalpy = [elem[1] for elem in tuples]
# Rescale formation enthalpy to use meV/atom
formation_enthalpy_rescaled = [item for item in formation_enthalpy]
q25_formation_enthalpy, q75_formation_enthalpy = np.percentile(formation_enthalpy_rescaled, [25, 75])
bin_width_formation_enthalpy = 2 * (q75_formation_enthalpy - q25_formation_enthalpy) * len(formation_enthalpy_rescaled) ** (
-1 / 3)
bins_formation_enthalpy = round((max(formation_enthalpy_rescaled) - min(formation_enthalpy_rescaled)) / bin_width_formation_enthalpy)
print("Min and Maximum of Formation Energy: ", min(formation_enthalpy_rescaled[2:-1]), " - ", max(formation_enthalpy_rescaled[2:-1]))
# print("Freedman–Diaconis number of bins:", bins_formation_enthalpy)
plt.figure()
plt.hist(formation_enthalpy_rescaled, color="blue", density=False, bins=100) # density=False would make counts
plt.ylabel('Number of configurations')
plt.xlabel('Formation Energy (meV/atom)')
plt.title(elements_list[0]+elements_list[1]+' - BCC phase')
plt.draw()
plt.tight_layout()
plt.savefig('BCC_Enthalpy_Histogram_'+elements_list[0]+elements_list[1])
# plot formation enthalpu as a function of chemical composition
fig, ax = plt.subplots()
hist2d_norm = getcolordensity(xdata, formation_enthalpy_rescaled)
plt.scatter(
xdata, formation_enthalpy_rescaled, s=8, c=hist2d_norm, vmin=0, vmax=1
)
plt.clim(0, 1)
plt.colorbar()
plt.xlabel(element_selected_for_plots+" concentration")
plt.ylabel('Formation Energy (meV/atom)')
plt.title(elements_list[0]+elements_list[1])
ax.set_xticks([0.0, 0.5, 1.0])
plt.draw()
plt.tight_layout()
plt.savefig("./BCC_enthalpy_vs_concentration_" + elements_list[0]+elements_list[1] + ".png", dpi=400)
def load_raw_data(raw_data_path):
"""Loads the raw files from specified path, performs the transformation to Data objects and normalization of values.
After that the serialized data is stored to the serialized_dataset directory.
"""
dataset = []
dirs = None
if rank == 0:
dirs = [f.name for f in os.scandir(raw_data_path) if f.is_dir()]
dirs = comm.bcast(dirs, root=0)
rx = list(nsplit(range(len(dirs)), size))[rank]
for name in sorted(dirs)[rx.start:rx.stop]:
print("f Rank: ", rank, " - name: ", name, flush=True)
if name == ".DS_Store":
continue
# if the directory contains subdirectories, explore their content
if os.path.isdir(os.path.join(raw_data_path, name)):
if name == ".DS_Store":
continue
dir_name = os.path.join(raw_data_path, name)
for subname in os.listdir(dir_name):
if subname == ".DS_Store":
continue
subdir_name = os.path.join(dir_name, subname)
for subsubname in os.listdir(subdir_name):
if subsubname == ".DS_Store":
continue
subsubdir_name = os.path.join(subdir_name, subsubname)
for subsubsubname in os.listdir(subsubdir_name):
if os.path.isfile(os.path.join(subsubdir_name, subsubsubname)):
data_object = transform_input_to_data_object_base(
filepath=os.path.join(subsubdir_name, subsubsubname)
)
if not isinstance(data_object, type(None)):
dataset.append(data_object)
return dataset
def transform_input_to_data_object_base(filepath):
data_object = transform_VASP_input_to_data_object_base(
filepath=filepath
)
return data_object
def transform_VASP_input_to_data_object_base(filepath):
"""Transforms lines of strings read from the raw data EAM file to Data object and returns it.
Parameters
----------
lines:
content of data file with all the graph information
Returns
----------
Data
Data object representing structure of a graph sample.
"""
if "OUTCAR" in filepath and "0.OUTCAR" not in filepath:
try:
ase_object = read_vasp_out(filepath)
dirpath = filepath.split("OUTCAR")[0]
data_object = transform_VASP_ASE_object_to_data_object(dirpath, ase_object)
return data_object
except:
print(filepath)
else:
return None
def transform_VASP_ASE_object_to_data_object(filepath, ase_object):
# FIXME:
# this still assumes bulk modulus is specific to the CFG format.
# To deal with multiple files across formats, one should generalize this function
# by moving the reading of the .bulk file in a standalone routine.
# Morevoer, this approach assumes tha there is only one global feature to look at,
# and that this global feature is specicially retrieveable in a file with the string *bulk* inside.
data_object = Data()
data_object.supercell_size = tensor(ase_object.cell.array).float()
data_object.pos = tensor(ase_object.arrays["positions"]).float()
proton_numbers = np.expand_dims(ase_object.arrays["numbers"], axis=1)
forces = ase_object.calc.results["forces"]
stress = ase_object.calc.results["stress"]
fermi_energy = ase_object.calc.eFermi
free_energy = ase_object.calc.results["free_energy"]
energy = ase_object.calc.results["energy"]
node_feature_matrix = np.concatenate(
(proton_numbers, forces), axis=1
)
data_object.x = tensor(node_feature_matrix).float()
formation_energy_file = open(filepath + 'formation_energy.txt', 'r')
Lines = formation_energy_file.readlines()
# Strips the newline character
for line in Lines:
data_object.y = tensor([float(line.strip())])
if data_object.y > 70.0:
print(filepath)
return data_object
if __name__ == '__main__':
elements_list = ['Ta', 'V']
# source_path = './bcc_enthalpy/'+'-'.join(elements_list)
source_path = './10.13139_OLCF_2222910/bcc_' + '-'.join(elements_list)
plot_data(source_path, elements_list)