-
Notifications
You must be signed in to change notification settings - Fork 63
/
thermo.py
282 lines (225 loc) · 9.4 KB
/
thermo.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
""" Core definition of a Thermo Document """
from collections import defaultdict
from typing import Dict, List, Union
from datetime import datetime
from emmet.core.utils import ValueEnum
from pydantic import BaseModel, Field
from pymatgen.analysis.phase_diagram import PhaseDiagram
from pymatgen.entries.computed_entries import ComputedEntry, ComputedStructureEntry
from emmet.core.material_property import PropertyDoc
from emmet.core.material import PropertyOrigin
from emmet.core.mpid import MPID
from emmet.core.vasp.calc_types.enums import RunType
class DecompositionProduct(BaseModel):
"""
Entry metadata for a decomposition process
"""
material_id: MPID = Field(
None,
description="The Materials Project ID for the material this decomposition points to.",
)
formula: str = Field(
None,
description="The formula of the decomposed material this material decomposes to.",
)
amount: float = Field(
None,
description="The amount of the decomposed material by formula units this this material decomposes to.",
)
class ThermoType(ValueEnum):
GGA_GGA_U = "GGA/GGA+U"
GGA_GGA_U_R2SCAN = "GGA/GGA+U/R2SCAN"
UNKNOWN = "UNKNOWN"
class ThermoDoc(PropertyDoc):
"""
A thermo entry document
"""
property_name = "thermo"
thermo_type: Union[ThermoType, RunType] = Field(
...,
description="Functional types of calculations involved in the energy mixing scheme.",
)
thermo_id: str = Field(
...,
description="Unique document ID which is composed of the Material ID and thermo data type.",
)
uncorrected_energy_per_atom: float = Field(
..., description="The total DFT energy of this material per atom in eV/atom."
)
energy_per_atom: float = Field(
...,
description="The total corrected DFT energy of this material per atom in eV/atom.",
)
energy_uncertainy_per_atom: float = Field(None, description="")
formation_energy_per_atom: float = Field(
None, description="The formation energy per atom in eV/atom."
)
energy_above_hull: float = Field(
..., description="The energy above the hull in eV/Atom."
)
is_stable: bool = Field(
False,
description="Flag for whether this material is on the hull and therefore stable.",
)
equilibrium_reaction_energy_per_atom: float = Field(
None,
description="The reaction energy of a stable entry from the neighboring equilibrium stable materials in eV."
" Also known as the inverse distance to hull.",
)
decomposes_to: List[DecompositionProduct] = Field(
None,
description="List of decomposition data for this material. Only valid for metastable or unstable material.",
)
decomposition_enthalpy: float = Field(
None,
description="Decomposition enthalpy as defined by `get_decomp_and_phase_separation_energy` in pymatgen.",
)
decomposition_enthalpy_decomposes_to: List[DecompositionProduct] = Field(
None,
description="List of decomposition data associated with the decomposition_enthalpy quantity.",
)
energy_type: str = Field(
..., description="The type of calculation this energy evaluation comes from.",
)
entry_types: List[str] = Field(
description="List of available energy types computed for this material."
)
entries: Dict[str, Union[ComputedEntry, ComputedStructureEntry]] = Field(
...,
description="List of all entries that are valid for this material."
" The keys for this dictionary are names of various calculation types.",
)
@classmethod
def from_entries(
cls,
entries: List[Union[ComputedEntry, ComputedStructureEntry]],
thermo_type: Union[ThermoType, RunType],
**kwargs
):
entries_by_comp = defaultdict(list)
for e in entries:
entries_by_comp[e.composition.reduced_formula].append(e)
# Only use lowest entry per composition to speed up QHull in Phase Diagram
reduced_entries = [
sorted(comp_entries, key=lambda e: e.energy_per_atom)[0]
for comp_entries in entries_by_comp.values()
]
pd = PhaseDiagram(reduced_entries)
docs = []
entries_by_mpid = defaultdict(list)
for e in entries:
entries_by_mpid[e.data["material_id"]].append(e)
entry_quality_scores = {"GGA": 1, "GGA+U": 2, "SCAN": 3, "R2SCAN": 4}
def _energy_eval(entry: ComputedStructureEntry):
"""
Helper function to order entries for thermo energy data selection
- Run type
- LASPH
- Energy
"""
return (
-1 * entry_quality_scores.get(entry.data["run_type"], 0),
-1 * int(entry.data.get("aspherical", False)),
entry.energy,
)
for material_id, entry_group in entries_by_mpid.items():
sorted_entries = sorted(entry_group, key=_energy_eval)
blessed_entry = sorted_entries[0]
(decomp, ehull) = pd.get_decomp_and_e_above_hull(blessed_entry)
d = {
"thermo_id": "{}_{}".format(material_id, str(thermo_type)),
"material_id": material_id,
"thermo_type": thermo_type,
"uncorrected_energy_per_atom": blessed_entry.uncorrected_energy
/ blessed_entry.composition.num_atoms,
"energy_per_atom": blessed_entry.energy
/ blessed_entry.composition.num_atoms,
"formation_energy_per_atom": pd.get_form_energy_per_atom(blessed_entry),
"energy_above_hull": ehull,
"is_stable": blessed_entry in pd.stable_entries,
}
if "last_updated" in blessed_entry.data:
d["last_updated"] = blessed_entry.data["last_updated"]
# Store different info if stable vs decomposes
if d["is_stable"]:
d[
"equilibrium_reaction_energy_per_atom"
] = pd.get_equilibrium_reaction_energy(blessed_entry)
else:
d["decomposes_to"] = [
{
"material_id": de.data["material_id"],
"formula": de.composition.formula,
"amount": amt,
}
for de, amt in decomp.items()
]
try:
decomp, energy = pd.get_decomp_and_phase_separation_energy(
blessed_entry
)
d["decomposition_enthalpy"] = energy
d["decomposition_enthalpy_decomposes_to"] = [
{
"material_id": de.data["material_id"],
"formula": de.composition.formula,
"amount": amt,
}
for de, amt in decomp.items()
]
except ValueError:
# try/except so this quantity does not take down the builder if it fails:
# it includes an optimization step that can be fragile in some instances,
# most likely failure is ValueError, "invalid value encountered in true_divide"
d["warnings"] = [
"Could not calculate decomposition enthalpy for this entry."
]
d["energy_type"] = blessed_entry.parameters.get("run_type", "Unknown")
d["entry_types"] = []
d["entries"] = {}
# Currently, each entry group contains a single entry due to how the compatability scheme works
for entry in entry_group:
d["entry_types"].append(entry.parameters.get("run_type", "Unknown"))
d["entries"][entry.parameters.get("run_type", "Unknown")] = entry
d["origins"] = [
PropertyOrigin(
name="energy",
task_id=blessed_entry.data["task_id"],
last_updated=d.get("last_updated", datetime.utcnow()),
)
]
docs.append(
ThermoDoc.from_structure(
meta_structure=blessed_entry.structure, **d, **kwargs
)
)
# Construct new phase diagram with all of the entries, not just those on the hull
pd_computed_data = pd._compute()
pd_computed_data["all_entries"] = entries
new_pd = PhaseDiagram(
entries, elements=pd.elements, computed_data=pd_computed_data
)
return docs, new_pd
class PhaseDiagramDoc(BaseModel):
"""
A phase diagram document
"""
property_name = "phase_diagram"
phase_diagram_id: str = Field(
...,
description="Phase diagram ID consisting of the chemical system and thermo type",
)
chemsys: str = Field(
..., description="Dash-delimited string of elements in the material",
)
thermo_type: Union[ThermoType, RunType] = Field(
...,
description="Functional types of calculations involved in the energy mixing scheme.",
)
phase_diagram: PhaseDiagram = Field(
..., description="Phase diagram for the chemical system.",
)
last_updated: datetime = Field(
description="Timestamp for the most recent calculation update for this property",
default_factory=datetime.utcnow,
)