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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
|
from enum import Enum
from typing import Dict, List, Optional, TypeVar, Union
from pydantic import BaseModel, Field
from pymatgen.core.periodic_table import Element
from pymatgen.core.structure import Structure
from emmet.core.electronic_structure import BandstructureData, DosData
from emmet.core.material_property import PropertyDoc
from emmet.core.mpid import MPID
from emmet.core.thermo import DecompositionProduct
from emmet.core.xas import Edge, Type
T = TypeVar("T", bound="SummaryDoc")
class HasProps(Enum):
"""
Enum of possible hasprops values.
"""
materials = "materials"
thermo = "thermo"
xas = "xas"
grain_boundaries = "grain_boundaries"
chemenv = "chemenv"
electronic_structure = "electronic_structure"
absorption = "absorption"
bandstructure = "bandstructure"
dos = "dos"
magnetism = "magnetism"
elasticity = "elasticity"
dielectric = "dielectric"
piezoelectric = "piezoelectric"
surface_properties = "surface_properties"
oxi_states = "oxi_states"
provenance = "provenance"
charge_density = "charge_density"
eos = "eos"
phonon = "phonon"
insertion_electrodes = "insertion_electrodes"
substrates = "substrates"
class SummaryStats(BaseModel):
"""
Statistics about a specified SummaryDoc field.
"""
field: Optional[str] = Field(
None,
title="Field",
description="Field name corresponding to a field in SummaryDoc.",
)
num_samples: Optional[int] = Field(
None,
title="Sample",
description="The number of documents sampled to generate statistics. "
"If unspecified, statistics will be from entire database.",
)
min: Optional[float] = Field(
None,
title="Minimum",
description="The minimum value "
"of the specified field used to "
"generate statistics.",
)
max: Optional[float] = Field(
None,
title="Maximum",
description="The maximum value "
"of the specified field used to "
"generate statistics.",
)
median: Optional[float] = Field(
None, title="Median", description="The median of the field values."
)
mean: Optional[float] = Field(
None, title="Mean", description="The mean of the field values."
)
distribution: Optional[List[float]] = Field(
None,
title="Distribution",
description="List of floats specifying a kernel density "
"estimator of the distribution, equally spaced "
"between specified minimum and maximum values.",
)
class XASSearchData(BaseModel):
"""
Fields in XAS sub docs in summary
"""
edge: Optional[Edge] = Field(
None,
title="Absorption Edge",
description="The interaction edge for XAS",
)
absorbing_element: Optional[Element] = Field(
None,
description="Absorbing element.",
)
spectrum_type: Optional[Type] = Field(
None,
description="Type of XAS spectrum.",
)
class GBSearchData(BaseModel):
"""
Fields in grain boundary sub docs in summary
"""
sigma: Optional[int] = Field(
None,
description="Sigma value of the boundary.",
)
type: Optional[str] = Field(
None,
description="Grain boundary type.",
)
gb_energy: Optional[float] = Field(
None,
description="Grain boundary energy in J/m^2.",
)
rotation_angle: Optional[float] = Field(
None,
description="Rotation angle in degrees.",
)
class SummaryDoc(PropertyDoc):
"""
Summary information about materials and their properties, useful for materials
screening studies and searching.
"""
property_name: str = "summary"
# Materials
structure: Structure = Field(
...,
description="The lowest energy structure for this material.",
)
task_ids: List[MPID] = Field(
[],
title="Calculation IDs",
description="List of Calculations IDs associated with this material.",
)
# Thermo
uncorrected_energy_per_atom: Optional[float] = Field(
None,
description="The total DFT energy of this material per atom in eV/atom.",
)
energy_per_atom: Optional[float] = Field(
None,
description="The total corrected DFT energy of this material per atom in eV/atom.",
)
formation_energy_per_atom: Optional[float] = Field(
None,
description="The formation energy per atom in eV/atom.",
)
energy_above_hull: Optional[float] = Field(
None,
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: Optional[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: Optional[List[DecompositionProduct]] = Field(
None,
description="List of decomposition data for this material. Only valid for metastable or unstable material.",
)
# XAS
xas: Optional[List[XASSearchData]] = Field(
None,
description="List of xas documents.",
)
# GB
grain_boundaries: Optional[List[GBSearchData]] = Field(
None,
description="List of grain boundary documents.",
)
# Electronic Structure
band_gap: Optional[float] = Field(
None,
description="Band gap energy in eV.",
)
cbm: Optional[Union[float, Dict]] = Field(
None,
description="Conduction band minimum data.",
)
vbm: Optional[Union[float, Dict]] = Field(
None,
description="Valence band maximum data.",
)
efermi: Optional[float] = Field(
None,
description="Fermi energy in eV.",
)
is_gap_direct: Optional[bool] = Field(
None,
description="Whether the band gap is direct.",
)
is_metal: Optional[bool] = Field(
None,
description="Whether the material is a metal.",
)
es_source_calc_id: Optional[Union[MPID, int]] = Field(
None,
description="The source calculation ID for the electronic structure data.",
)
bandstructure: Optional[BandstructureData] = Field(
None,
description="Band structure data for the material.",
)
dos: Optional[DosData] = Field(
None,
description="Density of states data for the material.",
)
# DOS
dos_energy_up: Optional[float] = Field(
None,
description="Spin-up DOS band gap in eV.",
)
dos_energy_down: Optional[float] = Field(
None,
description="Spin-down DOS band gap in eV.",
)
# Magnetism
is_magnetic: Optional[bool] = Field(
None,
description="Whether the material is magnetic.",
)
ordering: Optional[str] = Field(
None,
description="Type of magnetic ordering.",
)
total_magnetization: Optional[float] = Field(
None,
description="Total magnetization in μB.",
)
total_magnetization_normalized_vol: Optional[float] = Field(
None,
description="Total magnetization normalized by volume in μB/ų.",
)
total_magnetization_normalized_formula_units: Optional[float] = Field(
None,
description="Total magnetization normalized by formula unit in μB/f.u. .",
)
num_magnetic_sites: Optional[int] = Field(
None,
description="The number of magnetic sites.",
)
num_unique_magnetic_sites: Optional[int] = Field(
None,
description="The number of unique magnetic sites.",
)
types_of_magnetic_species: Optional[List[Element]] = Field(
None,
description="Magnetic specie elements.",
)
# Elasticity
# k_voigt: Optional[float] = Field(None, description="Voigt average of the bulk modulus.")
# k_reuss: Optional[float] = Field(None, description="Reuss average of the bulk modulus in GPa.")
# k_vrh: Optional[float] = Field(None, description="Voigt-Reuss-Hill average of the bulk modulus in GPa.")
# g_voigt: Optional[float] = Field(None, description="Voigt average of the shear modulus in GPa.")
# g_reuss: Optional[float] = Field(None, description="Reuss average of the shear modulus in GPa.")
# g_vrh: Optional[float] = Field(None, description="Voigt-Reuss-Hill average of the shear modulus in GPa.")
bulk_modulus: Optional[dict] = Field(
None,
description="Voigt, Reuss, and Voigt-Reuss-Hill averages of the bulk modulus in GPa.",
)
shear_modulus: Optional[dict] = Field(
None,
description="Voigt, Reuss, and Voigt-Reuss-Hill averages of the shear modulus in GPa.",
)
universal_anisotropy: Optional[float] = Field(
None, description="Elastic anisotropy."
)
homogeneous_poisson: Optional[float] = Field(None, description="Poisson's ratio.")
# Dielectric and Piezo
e_total: Optional[float] = Field(
None,
description="Total dielectric constant.",
)
e_ionic: Optional[float] = Field(
None,
description="Ionic contribution to dielectric constant.",
)
e_electronic: Optional[float] = Field(
None,
description="Electronic contribution to dielectric constant.",
)
n: Optional[float] = Field(
None,
description="Refractive index.",
)
e_ij_max: Optional[float] = Field(
None,
description="Piezoelectric modulus.",
)
# Surface Properties
weighted_surface_energy_EV_PER_ANG2: Optional[float] = Field(
None,
description="Weighted surface energy in eV/Ų.",
)
weighted_surface_energy: Optional[float] = Field(
None,
description="Weighted surface energy in J/m².",
)
weighted_work_function: Optional[float] = Field(
None,
description="Weighted work function in eV.",
)
surface_anisotropy: Optional[float] = Field(
None,
description="Surface energy anisotropy.",
)
shape_factor: Optional[float] = Field(
None,
description="Shape factor.",
)
has_reconstructed: Optional[bool] = Field(
None,
description="Whether the material has any reconstructed surfaces.",
)
# Oxi States
possible_species: Optional[List[str]] = Field(
None,
description="Possible charged species in this material.",
)
# Has Props
has_props: Optional[Dict[str, bool]] = Field(
None,
description="List of properties that are available for a given material.",
)
# Theoretical
theoretical: bool = Field(
True,
description="Whether the material is theoretical.",
)
# External Database IDs
database_IDs: Dict[str, List[str]] = Field(
{}, description="External database IDs corresponding to this material."
)
@classmethod
def from_docs(cls, material_id: MPID, **docs: Dict[str, Dict]):
"""Converts a bunch of summary docs into a SummaryDoc"""
doc = _copy_from_doc(docs)
# Reshape document for various sub-sections
# Electronic Structure + Bandstructure + DOS
if "bandstructure" in doc:
if doc["bandstructure"] is not None and list(
filter(lambda x: x is not None, doc["bandstructure"].values())
):
doc["has_props"]["bandstructure"] = True
else:
del doc["bandstructure"]
if "dos" in doc:
if doc["dos"] is not None and list(
filter(lambda x: x is not None, doc["dos"].values())
):
doc["has_props"]["dos"] = True
else:
del doc["dos"]
if "task_id" in doc:
del doc["task_id"]
return SummaryDoc(material_id=material_id, **doc)
# Key mapping
summary_fields: Dict[str, list] = {
HasProps.materials.value: [
"nsites",
"elements",
"nelements",
"composition",
"composition_reduced",
"formula_pretty",
"formula_anonymous",
"chemsys",
"volume",
"density",
"density_atomic",
"symmetry",
"structure",
"deprecated",
"task_ids",
"builder_meta",
],
HasProps.thermo.value: [
"uncorrected_energy_per_atom",
"energy_per_atom",
"formation_energy_per_atom",
"energy_above_hull",
"is_stable",
"equilibrium_reaction_energy_per_atom",
"decomposes_to",
],
HasProps.xas.value: ["absorbing_element", "edge", "spectrum_type", "spectrum_id"],
HasProps.grain_boundaries.value: [
"gb_energy",
"sigma",
"type",
"rotation_angle",
"w_sep",
],
HasProps.electronic_structure.value: [
"band_gap",
"efermi",
"cbm",
"vbm",
"is_gap_direct",
"is_metal",
"bandstructure",
"dos",
"task_id",
],
HasProps.magnetism.value: [
"is_magnetic",
"ordering",
"total_magnetization",
"total_magnetization_normalized_vol",
"total_magnetization_normalized_formula_units",
"num_magnetic_sites",
"num_unique_magnetic_sites",
"types_of_magnetic_species",
"is_magnetic",
],
HasProps.elasticity.value: [
"bulk_modulus",
"shear_modulus",
"universal_anisotropy",
"homogeneous_poisson",
],
HasProps.dielectric.value: ["e_total", "e_ionic", "e_electronic", "n"],
HasProps.piezoelectric.value: ["e_ij_max"],
HasProps.surface_properties.value: [
"weighted_surface_energy",
"weighted_surface_energy_EV_PER_ANG2",
"shape_factor",
"surface_anisotropy",
"weighted_work_function",
"has_reconstructed",
],
HasProps.oxi_states.value: ["possible_species"],
HasProps.provenance.value: ["theoretical", "database_IDs"],
HasProps.charge_density.value: [],
HasProps.eos.value: [],
HasProps.phonon.value: [],
HasProps.absorption.value: [],
HasProps.insertion_electrodes.value: [],
HasProps.substrates.value: [],
HasProps.chemenv.value: [],
}
def _copy_from_doc(doc):
"""Helper function to copy the list of keys over from amalgamated document"""
has_props = {str(val.value): False for val in HasProps}
d = {"has_props": has_props, "origins": []}
# Complex function to grab the keys and put them in the root doc
# if the item is a list, it makes one doc per item with those corresponding keys
for doc_key in summary_fields:
sub_doc = doc.get(doc_key, None)
if isinstance(sub_doc, list) and len(sub_doc) > 0:
d["has_props"][doc_key] = True
d[doc_key] = []
for sub_item in sub_doc:
temp_doc = {
copy_key: sub_item[copy_key]
for copy_key in summary_fields[doc_key]
if copy_key in sub_item
}
d[doc_key].append(temp_doc)
elif isinstance(sub_doc, dict):
d["has_props"][doc_key] = True
if sub_doc.get("origins", None):
d["origins"].extend(sub_doc["origins"])
d.update(
{
copy_key: sub_doc[copy_key]
for copy_key in summary_fields[doc_key]
if copy_key in sub_doc
}
)
return d
|