-
Notifications
You must be signed in to change notification settings - Fork 62
/
electrode.py
236 lines (189 loc) · 7.22 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
from datetime import datetime
from typing import Dict, List, Union
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 import Composition, Structure
from pymatgen.core.periodic_table import Element
from pymatgen.entries.computed_entries import ComputedEntry
from emmet.core.mpid import MPID
from emmet.core.utils import jsanitize
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: float = 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."
)
id_charge: Union[MPID, int, None] = Field(
None, description="The material-id of the charged structure."
)
id_discharge: Union[MPID, int, None] = Field(
None, description="The material-id of the charged structure."
)
class InsertionElectrodeDoc(InsertionVoltagePairDoc):
"""
Insertion electrode
"""
battery_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 = Field(
None,
description="The chemical compositions of the host framework",
)
material_ids: List[Union[MPID]] = Field(
None,
description="The ids of all structures that matched to the present host lattice, regardless of stability. The stable entries can be found in the adjacent pairs.",
)
electrode_object: Dict = Field(
None,
description="The pymatgen electrode object",
)
# 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: Union[MPID, int],
host_structure: Structure,
) -> Union["InsertionElectrodeDoc", None]:
try:
ie = InsertionElectrode.from_entries(
entries=grouped_entries,
working_ion_entry=working_ion_entry,
strip_structures=True,
)
except IndexError:
return None
d = ie.get_summary_dict()
d["material_ids"] = d["stable_material_ids"] + d["unstable_material_ids"]
d["num_steps"] = d.pop("nsteps", None)
d["last_updated"] = datetime.utcnow()
return cls(
battery_id=task_id,
host_structure=host_structure.as_dict(),
framework=Composition(d["framework_formula"]),
electrode_object=ie.as_dict(),
**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):
battery_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: Union[MPID, int],
):
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)