-
Notifications
You must be signed in to change notification settings - Fork 62
/
electrode.py
260 lines (206 loc) · 7.96 KB
/
electrode.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
from datetime import datetime
from typing import Dict, List
from monty.json import MontyDecoder
from pydantic import BaseModel, Field, validator
from pymatgen.apps.battery.battery_abc import AbstractElectrode
from pymatgen.apps.battery.conversion_battery import ConversionElectrode
from pymatgen.apps.battery.insertion_battery import InsertionElectrode
from pymatgen.core.periodic_table import Element
from pymatgen.entries.computed_entries import ComputedEntry
from emmet.stubs import Composition, Structure
class VoltagePairDoc(BaseModel):
"""
Data for individual voltage steps.
Note: Each voltage step is represented as a sub_electrode (ConversionElectrode/InsertionElectrode)
object to gain access to some basic statistics about the voltage step
"""
max_delta_volume: str = Field(
None,
description="Volume changes in % for a particular voltage step using: "
"max(charge, discharge) / min(charge, discharge) - 1",
)
average_voltage: float = Field(
None,
description="The average voltage in V for a particular voltage step.",
)
capacity_grav: float = Field(None, description="Gravimetric capacity in mAh/g.")
capacity_vol: float = Field(None, description="Volumetric capacity in mAh/cc.")
energy_grav: float = Field(
None, description="Gravimetric energy (Specific energy) in Wh/kg."
)
energy_vol: float = Field(
None, description="Volumetric energy (Energy Density) in Wh/l."
)
fracA_charge: float = Field(
None, description="Atomic fraction of the working ion in the charged state."
)
fracA_discharge: float = Field(
None, description="Atomic fraction of the working ion in the discharged state."
)
@classmethod
def from_sub_electrode(cls, sub_electrode: AbstractElectrode, **kwargs):
"""
Convert A pymatgen electrode object to a document
"""
return cls(**sub_electrode.get_summary_dict(), **kwargs)
class InsertionVoltagePairDoc(VoltagePairDoc):
"""
Features specific to insertion electrode
"""
stability_charge: float = Field(
None, description="The energy above hull of the charged material."
)
stability_discharge: float = Field(
None, description="The energy above hull of the discharged material."
)
class InsertionElectrodeDoc(InsertionVoltagePairDoc):
"""
Insertion electrode
"""
task_id: str = Field(None, description="The id for this battery document.")
framework_formula: str = Field(
None, description="The id for this battery document."
)
host_structure: Structure = Field(
None,
description="Host structure (structure without the working ion)",
)
adj_pairs: List[InsertionVoltagePairDoc] = Field(
None,
description="Returns all the Voltage Steps",
)
working_ion: Element = Field(
None,
description="The working ion as an Element object",
)
num_steps: float = Field(
None,
description="The number of distinct voltage steps in from fully charge to "
"discharge based on the stable intermediate states",
)
max_voltage_step: float = Field(
None, description="Maximum absolute difference in adjacent voltage steps"
)
last_updated: datetime = Field(
None,
description="Timestamp for the most recent calculation for this Material document",
)
framework: Composition
# Make sure that the datetime field is properly formatted
@validator("last_updated", pre=True)
def last_updated_dict_ok(cls, v):
return MontyDecoder().process_decoded(v)
@classmethod
def from_entries(
cls,
grouped_entries: List[ComputedEntry],
working_ion_entry: ComputedEntry,
task_id: str,
host_structure: Structure,
):
ie = InsertionElectrode.from_entries(
entries=grouped_entries, working_ion_entry=working_ion_entry
)
d = ie.get_summary_dict()
d["num_steps"] = d.pop("nsteps", None)
d["last_updated"] = datetime.utcnow()
return cls(
task_id=task_id,
host_structure=host_structure.as_dict(),
framework=Composition(d["framework_formula"]),
**d
)
class ConversionVoltagePairDoc(VoltagePairDoc):
"""
Features specific to conversion electrode
"""
reactions: List[str] = Field(
None,
description="The reaction(s) the characterizes that particular voltage step.",
)
class ConversionElectrodeDoc(ConversionVoltagePairDoc):
task_id: str = Field(None, description="The id for this battery document.")
adj_pairs: List[ConversionVoltagePairDoc] = Field(
None,
description="Returns all the adjacent Voltage Steps",
)
working_ion: Element = Field(
None,
description="The working ion as an Element object",
)
num_steps: float = Field(
None,
description="The number of distinct voltage steps in from fully charge to "
"discharge based on the stable intermediate states",
)
max_voltage_step: float = Field(
None, description="Maximum absolute difference in adjacent voltage steps"
)
last_updated: datetime = Field(
None,
description="Timestamp for the most recent calculation for this Material document",
)
# Make sure that the datetime field is properly formatted
@validator("last_updated", pre=True)
def last_updated_dict_ok(cls, v):
return MontyDecoder().process_decoded(v)
@classmethod
def from_composition_and_entries(
cls,
composition: Composition,
entries: List[ComputedEntry],
working_ion_symbol: str,
task_id: str,
):
ce = ConversionElectrode.from_composition_and_entries(
comp=composition,
entries_in_chemsys=entries,
working_ion_symbol=working_ion_symbol,
)
d = ce.get_summary_dict()
d["num_steps"] = d.pop("nsteps", None)
d["last_updated"] = datetime.utcnow()
return cls(task_id=task_id, framework=Composition(d["framework_formula"]), **d)
class StructureGroupDoc(BaseModel):
"""
Document model for the intermediate structure matching database used to build the insertion electrode documents.
"""
task_id: str = Field(
None,
description="The combined task_id of the grouped document is given by the numerically smallest task id "
"followed by '_Li' or whichever working atom is considered the working ion during grouping.",
)
structure_matched: bool = Field(
None,
description="True if the structures in this group has been matched to each other. This is False for groups "
"that contain all the left over structures with the same framework.",
)
has_distinct_compositions: bool = Field(
None,
description="True if multiple working ion fractions are available in the group, which means a voltage "
"step exits.",
)
grouped_task_ids: List[str] = Field(
None,
description="The ids of the materials that have been grouped by the structure matcher.",
)
entry_data: Dict = Field(
None,
description="Dictionary keyed by the task_id, contains the 'composition' and 'volume' of each material.",
)
framework_formula: str = Field(
None, description="The formula of the host framework."
)
working_ion: Element = Field(None, description="The working ion")
chemsys: str = Field(
None,
description="The chemsys this group belongs to. Always includes the working ion",
)
last_updated: datetime = Field(
None,
description="Timestamp for the most recent calculation for this Material document",
)
# Make sure that the datetime field is properly formatted
@validator("last_updated", pre=True)
def last_updated_dict_ok(cls, v):
return MontyDecoder().process_decoded(v)