-
Notifications
You must be signed in to change notification settings - Fork 838
/
chempot_diagram.py
440 lines (358 loc) · 15.5 KB
/
chempot_diagram.py
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# coding: utf-8
# Copyright (c) Pymatgen Development Team.
# Distributed under the terms of the MIT License.
"""
This module implements the construction and plotting of chemical potential diagrams
from a list of entries within a chemical system containing 3 or more elements. The
chemical potential diagram is the mathematical dual to the traditional compositional
phase diagram.
For more information, please reference the paper below:
Todd, Paul K., McDermott, M.J., et al. “Selectivity in yttrium manganese oxide
synthesis via local chemical potentials in hyperdimensional phase space.”
ArXiv:2104.05986 [Cond-Mat], Apr. 2021. arXiv.org, http://arxiv.org/abs/2104.05986.
"""
import json
import os
from itertools import groupby
from typing import Dict, List, Optional, Tuple, Union
import numpy as np
import plotly.express as px
from monty.json import MSONable
from plotly.graph_objects import Scatter3d, Mesh3d, Figure
from scipy.spatial import ConvexHull, HalfspaceIntersection
from scipy.spatial.qhull import QhullError
from pymatgen.analysis.phase_diagram import PDPlotter, PhaseDiagram, PDEntry
from pymatgen.core.composition import Composition, Element
from pymatgen.entries.computed_entries import ComputedEntry
from pymatgen.util.coord import Simplex
with open(os.path.join(os.path.dirname(__file__), "..", "util", "plotly_chempot_layouts.json")) as f:
plotly_layouts = json.load(f)
class ChemicalPotentialDiagram(MSONable):
"""
The chemical potential diagram is the mathematical dual to the
traditional compositional phase diagram. To create the diagram, convex
minimization is performed in E vs. μ space by taking the lower convex envelope of
hyperplanes. Accordingly, "points" on the compositional phase diagram
become N-dimensional convex polytopes (domains) in chemical potential space.
For more information, please reference the paper below:
Todd, Paul K., McDermott, M.J., et al. “Selectivity in yttrium manganese oxide
synthesis via local chemical potentials in hyperdimensional Phase Space.”
ArXiv:2104.05986 [Cond-Mat], Apr. 2021. arXiv.org, http://arxiv.org/abs/2104.05986.
"""
def __init__(
self,
entries: List[PDEntry],
limits: Optional[Dict[Element, float]] = None,
default_limit: Optional[float] = -20.0,
):
"""
Args:
entries: List of PDEntry-like objects containing a composition and energy
limits: Chemical potential value chosen for bordering elemental
hyperplanes; these constrain the space over which the domains are
calculated
default_limit (float): Default minimum chemical potential limit for
unspecified elemental limits
"""
self.entries = sorted(entries, key=lambda e: e.composition.reduced_composition)
self.limits = limits
self.default_limit = default_limit
self.elements = list(sorted({els for e in self.entries for els in e.composition.elements}))
self.dim = len(self.elements)
self._min_entries, self.el_refs = self._get_min_entries_and_el_refs(entries)
self._entry_dict = {e.composition.reduced_formula: e for e in self._min_entries}
self._border_hyperplanes = self._get_border_hyperplanes()
self._hyperplanes, self._hyperplane_entries = self._get_hyperplanes_and_entries()
if self.dim < 3:
raise ValueError("ChemicalPotentialDiagram currently requires phase diagrams " "with 3 or more elements!")
if len(self.el_refs) != self.dim:
missing = set(self.elements).difference(self.el_refs.keys())
raise ValueError(f"There are no entries for the terminal elements: {missing}")
def get_plot(
self,
elements: Optional[List[Union[Element, str]]] = None,
label_stable: Optional[bool] = True,
formulas_to_draw: Optional[List[str]] = None,
draw_formula_meshes: Optional[bool] = True,
draw_formula_lines: Optional[bool] = True,
formula_colors: Optional[List[str]] = px.colors.qualitative.Dark2,
) -> Scatter3d:
"""
Args:
elements:
label_stable:
formulas_to_draw:
draw_formula_meshes:
draw_formula_lines:
formula_colors:
Returns:
"""
if not elements:
elements = self.elements[:3] # type: ignore
if not formulas_to_draw:
formulas_to_draw = []
elems = [Element(e) for e in elements] # type: ignore
elem_indices = [self.elements.index(e) for e in elems]
domains = self.domains.copy()
draw_domains = {}
draw_comps = [Composition(formula).reduced_composition for formula in formulas_to_draw]
annotations = []
for formula, points in domains.items():
entry = self.entry_dict[formula]
points_3d = np.array(points[:, elem_indices])
contains_target_elems = set(entry.composition.elements).issubset(elems)
if formulas_to_draw:
if entry.composition.reduced_composition in draw_comps:
domains[formula] = None
draw_domains[formula] = points_3d
if contains_target_elems:
domains[formula] = points_3d
else:
continue
if not contains_target_elems:
domains[formula] = None
continue
simplices, ann_loc = self._get_domain_simplices_and_ann_loc(points_3d)
ann_formula = formula
if hasattr(entry, "original_entry"):
ann_formula = entry.original_entry.composition.reduced_formula
annotation = self._get_annotation(ann_loc, ann_formula)
annotations.append(annotation)
domains[formula] = simplices
layout = plotly_layouts["default_layout_3d"].copy()
layout["scene"].update(self._get_axis_layout_dict(elements))
layout["scene"]["annotations"] = None
if label_stable:
layout["scene"].update({"annotations": annotations})
layout["scene_camera"] = dict(eye=dict(x=0, y=0, z=2.0), projection=dict(type="orthographic"))
data = self._get_domain_lines(domains)
if draw_formula_lines:
data.extend(self._get_formula_lines(draw_domains, formula_colors))
if draw_formula_meshes:
data.extend(self._get_formula_meshes(draw_domains, formula_colors))
figure = Figure(data, layout)
return figure
def _get_domains(self):
hyperplanes = self._hyperplanes
border_hyperplanes = self._border_hyperplanes
entries = self._hyperplane_entries
hs_hyperplanes = np.vstack([hyperplanes, border_hyperplanes])
interior_point = np.average(self.lims, axis=1).tolist()
hs_int = HalfspaceIntersection(hs_hyperplanes, np.array(interior_point))
domains = {entry.composition.reduced_formula: [] for entry in entries}
for intersection, facet in zip(hs_int.intersections, hs_int.dual_facets):
for v in facet:
if v < len(entries):
this_entry = entries[v]
formula = this_entry.composition.reduced_formula
domains[formula].append(intersection)
return {k: np.array(v) for k, v in domains.items() if v}
def _get_border_hyperplanes(self):
border_hyperplanes = np.array(([[0] * (self.dim + 1)] * (2 * self.dim)))
for idx, limit in enumerate(self.lims):
border_hyperplanes[2 * idx, idx] = -1
border_hyperplanes[2 * idx, -1] = limit[0]
border_hyperplanes[(2 * idx) + 1, idx] = 1
border_hyperplanes[(2 * idx) + 1, -1] = limit[1]
return border_hyperplanes
def _get_hyperplanes_and_entries(self):
data = np.array(
[
[e.composition.get_atomic_fraction(el) for el in self.elements] + [e.energy_per_atom]
for e in self._min_entries
]
)
vec = [self.el_refs[el].energy_per_atom for el in self.elements] + [-1]
form_e = -np.dot(data, vec)
inds = np.where(form_e < -PhaseDiagram.formation_energy_tol)[0].tolist()
inds.extend([self._min_entries.index(el) for el in self.el_refs.values()])
hyperplanes = data[inds]
hyperplanes[:, -1] = hyperplanes[:, -1] * -1
hyperplane_entries = [self._min_entries[i] for i in inds]
return hyperplanes, hyperplane_entries
@staticmethod
def _get_min_entries_and_el_refs(entries):
el_refs = {}
min_entries = []
for c, g in groupby(entries, key=lambda e: e.composition.reduced_composition):
g = list(g)
min_entry = min(g, key=lambda e: e.energy_per_atom)
if c.is_element:
el_refs[c.elements[0]] = min_entry
min_entries.append(min_entry)
return min_entries, el_refs
@staticmethod
def _get_domain_simplices_and_ann_loc(points_3d):
try:
domain = ConvexHull(points_3d)
ann_loc = np.mean(points_3d.T, axis=1)
except QhullError:
points_2d, v, w = simple_pca(points_3d, k=2)
domain = ConvexHull(points_2d)
centroid_2d = get_centroid_2d(points_2d[domain.vertices])
ann_loc = centroid_2d @ w.T + np.mean(points_3d.T, axis=1) # recover orig 3D coords from eigenvectors
simplices = [Simplex(points_3d[indices]) for indices in domain.simplices]
return simplices, ann_loc
@staticmethod
def _get_formula_meshes(draw_domains, formula_colors) -> List[Mesh3d]:
meshes = []
for idx, (formula, coords) in enumerate(draw_domains.items()):
points_3d = coords[:, :3]
mesh = Mesh3d(
x=points_3d[:, 0],
y=points_3d[:, 1],
z=points_3d[:, 2],
alphahull=0,
showlegend=True,
lighting=dict(fresnel=1.0),
color=formula_colors[idx],
name=f"{formula} (mesh)",
opacity=0.13,
)
meshes.append(mesh)
return meshes
@staticmethod
def _get_formula_lines(draw_domains, formula_colors) -> List[Scatter3d]:
lines = []
for idx, (formula, coords) in enumerate(draw_domains.items()):
points_3d = coords[:, :3]
domain = ConvexHull(points_3d[:, :-1])
simplexes = [Simplex(points_3d[indices]) for indices in domain.simplices]
x, y, z = [], [], []
for s in simplexes:
x.extend(s.coords[:, 0].tolist() + [None])
y.extend(s.coords[:, 1].tolist() + [None])
z.extend(s.coords[:, 2].tolist() + [None])
line = Scatter3d(
x=x,
y=y,
z=z,
mode="lines",
line={"width": 8, "color": formula_colors[idx]},
opacity=1.0,
name=f"{formula} (lines)",
)
lines.append(line)
return lines
@staticmethod
def _get_domain_lines(domains) -> List[Scatter3d]:
x, y, z = [], [], []
for phase, simplexes in domains.items():
if simplexes:
for s in simplexes:
x.extend(s.coords[:, 0].tolist() + [None])
y.extend(s.coords[:, 1].tolist() + [None])
z.extend(s.coords[:, 2].tolist() + [None])
lines = [
Scatter3d(
x=x,
y=y,
z=z,
mode="lines",
line=dict(color="black", width=4.5),
showlegend=False,
)
]
return lines
@property
def domains(self) -> Dict[str, np.array]:
"""
Mapping of formulas to array of domain boundary points
"""
return self._get_domains()
@property
def lims(self):
""" Returns array of limits used in constructing hyperplanes"""
lims = np.array([[self.default_limit, 0]] * self.dim)
for idx, elem in enumerate(self.elements):
if self.limits and elem in self.limits:
lims[idx, :] = self.limits[elem]
return lims
@property
def entry_dict(self) -> Dict[str, ComputedEntry]:
"""Mapping between reduced formula and ComputedEntry"""
return self._entry_dict
@property
def hyperplanes(self) -> np.array:
"""Returns array of hyperplane data"""
return self._hyperplanes
@property
def hyperplane_entries(self) -> List[PDEntry]:
"""Returns list of entries corresponding to hyperplanes"""
return self._hyperplane_entries
@property
def border_hyperplanes(self) -> np.array:
"""Returns bordering hyperplanes"""
return self._border_hyperplanes
@property
def chemical_system(self) -> str:
"""Returns the chemical system (A-B-C-...) of diagram object"""
return "-".join(sorted([e.symbol for e in self.elements]))
@staticmethod
def _get_chempot_axis_title(element) -> str:
return f"μ<sub>{str(element)}</sub> - μ<sub>" f"{str(element)}</sub><sup>o</sup> (eV)"
@staticmethod
def _get_annotation(ann_loc, formula) -> Dict[str, Union[str, float]]:
formula = PDPlotter._htmlize_formula(formula)
annotation = plotly_layouts["default_annotation_layout"].copy()
annotation.update(
{
"x": ann_loc[0],
"y": ann_loc[1],
"z": ann_loc[2],
"text": formula,
}
)
return annotation
def _get_axis_layout_dict(self, elements):
axes = ["xaxis", "yaxis", "zaxis"]
axes_layout = {}
for ax, el in zip(axes, elements):
layout = plotly_layouts["default_axis_layout"].copy()
layout["title"] = self._get_chempot_axis_title(el)
axes_layout[ax] = layout
return axes_layout
def __repr__(self):
return f"ChemicalPotentialDiagram for {self.chemical_system} with {len(self.entries)} " f"entries"
def simple_pca(data: np.array, k: int = 2) -> Tuple[np.array, np.array, np.array]:
"""
A barebones implementation of principal component analysis (PCA) used in the
ChemicalPotentialDiagram class for plotting.
Args:
data: array of observations
k: Number of principal components returned
Returns:
tuple: Projected data, eigenvalues, eigenvectors
"""
data = data - np.mean(data.T, axis=1) # centering the data
cov = np.cov(data.T) # calculating covariance matrix
v, w = np.linalg.eig(cov) # performing eigendecomposition
idx = v.argsort()[::-1] # sorting the components
v = v[idx]
w = w[:, idx]
scores = data.dot(w[:, :k])
return scores, v[:k], w[:, :k]
def get_centroid_2d(vertices: np.array) -> np.array:
"""
A barebones implementation of the formula for calculating the centroid of a 2D
polygon.
**NOTE**: vertices must be ordered circumfrentially!
Args:
vertices:
Returns:
"""
n = len(vertices)
cx = 0
cy = 0
a = 0
for i in range(0, n - 1):
xi = vertices[i, 0]
yi = vertices[i, 1]
xi_p = vertices[i + 1, 0]
yi_p = vertices[i + 1, 1]
common_term = xi * yi_p - xi_p * yi
cx += (xi + xi_p) * common_term
cy += (yi + yi_p) * common_term
a += common_term
prefactor = 0.5 / (6 * a)
return np.array([prefactor * cx, prefactor * cy])