Skip to content

Commit

Permalink
Merge pull request #206 from munrojm/main
Browse files Browse the repository at this point in the history
Electronic Structure model and builder fixes
  • Loading branch information
Jason Munro authored Jun 21, 2021
2 parents 570f07a + ec08158 commit d8e6010
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 49 deletions.
34 changes: 22 additions & 12 deletions emmet-builders/emmet/builders/materials/electronic_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def process_item(self, mat):

dos = None
bs = {}
structures = {}

for bs_type, bs_entry in mat["bandstructure"].items():
if bs_entry.get("object", None) is not None:
Expand All @@ -135,13 +136,17 @@ def process_item(self, mat):
else None
)

structures[bs_entry["task_id"]] = bs_entry["output_structure"]

if mat["dos"]:
if mat["dos"]["object"] is not None:
self.logger.info("Processing density of states")
dos = {
mat["dos"]["task_id"]: CompleteDos.from_dict(mat["dos"]["object"])
}

structures[mat["dos"]["task_id"]] = mat["dos"]["output_structure"]

if bs:
self.logger.info(
"Processing band structure types: {}".format(
Expand All @@ -165,7 +170,7 @@ def process_item(self, mat):
else:
doc = ElectronicStructureDoc.from_bsdos(
material_id=mat[self.materials.key],
structure=structure,
structures=structures,
dos=dos,
is_gap_direct=mat["other"]["is_gap_direct"],
is_metal=mat["other"]["is_metal"],
Expand Down Expand Up @@ -239,14 +244,14 @@ def _update_materials_doc(self, mat_id):
"input.is_hubbard",
"orig_inputs.kpoints",
"input.parameters",
"input.structure",
"output.structure",
],
criteria={"task_id": str(task_id)},
)

fs_id = str(task_query["calcs_reversed"][0]["bandstructure_fs_id"])

structure = Structure.from_dict(task_query["input"]["structure"])
structure = Structure.from_dict(task_query["output"]["structure"])

kpoints = task_query["orig_inputs"]["kpoints"]
labels_dict = {
Expand Down Expand Up @@ -281,6 +286,7 @@ def _update_materials_doc(self, mat_id):
"is_hubbard": int(is_hubbard),
"nkpoints": int(nkpoints),
"updated_on": lu_dt,
"output_structure": structure,
}
)

Expand All @@ -293,6 +299,7 @@ def _update_materials_doc(self, mat_id):
"input.is_hubbard",
"orig_inputs.kpoints",
"input.parameters",
"output.structure",
],
criteria={"task_id": str(task_id)},
)
Expand All @@ -301,13 +308,11 @@ def _update_materials_doc(self, mat_id):

is_hubbard = task_query["input"]["is_hubbard"]

if (
task_query["orig_inputs"]["kpoints"]["generation_style"]
== "Monkhorst"
):
nkpoints = np.prod(
task_query["orig_inputs"]["kpoints"]["kpoints"][0], axis=0
)
structure = Structure.from_dict(task_query["output"]["structure"])

if task_query["orig_inputs"]["kpoints"]["generation_style"] == "Monkhorst":
nkpoints = np.prod(task_query["orig_inputs"]["kpoints"]["kpoints"][0], axis=0)

else:
nkpoints = task_query["orig_inputs"]["kpoints"]["nkpoints"]

Expand All @@ -322,6 +327,7 @@ def _update_materials_doc(self, mat_id):
"nkpoints": int(nkpoints),
"nedos": int(nedos),
"updated_on": lu_dt,
"output_structure": structure,
}
)

Expand All @@ -333,12 +339,12 @@ def _update_materials_doc(self, mat_id):
"input.is_hubbard",
"orig_inputs.kpoints",
"calcs_reversed",
"input.structure",
"output.structure",
],
criteria={"task_id": str(task_id)},
)

structure = Structure.from_dict(task_query["input"]["structure"])
structure = Structure.from_dict(task_query["output"]["structure"])

other_mag_ordering = CollinearMagneticStructureAnalyzer(
structure
Expand Down Expand Up @@ -408,6 +414,8 @@ def _obtain_blessed_calculations(
bs_obj["data"] if bs_obj is not None else None
)

materials_doc["bandstructure"][bs_type]["output_structure"] = sorted_bs_data[0]["output_structure"]

if dos_calcs:

sorted_dos_data = sorted(
Expand All @@ -430,6 +438,8 @@ def _obtain_blessed_calculations(
dos_obj["data"] if dos_obj is not None else None
)

materials_doc["dos"]["output_structure"] = sorted_dos_data[0]["output_structure"]

if other_calcs:

sorted_other_data = sorted(
Expand Down
26 changes: 9 additions & 17 deletions emmet-core/emmet/core/electrode.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class VoltagePairDoc(BaseModel):
)

average_voltage: float = Field(
None,
description="The average voltage in V for a particular voltage step.",
None, description="The average voltage in V for a particular voltage step."
)

capacity_grav: float = Field(None, description="Gravimetric capacity in mAh/g.")
Expand Down Expand Up @@ -101,18 +100,15 @@ class InsertionElectrodeDoc(InsertionVoltagePairDoc):
)

host_structure: Structure = Field(
None,
description="Host structure (structure without the working ion)",
None, description="Host structure (structure without the working ion)"
)

adj_pairs: List[InsertionVoltagePairDoc] = Field(
None,
description="Returns all the Voltage Steps",
None, description="Returns all the Voltage Steps"
)

working_ion: Element = Field(
None,
description="The working ion as an Element object",
None, description="The working ion as an Element object"
)

num_steps: int = Field(
Expand All @@ -131,8 +127,7 @@ class InsertionElectrodeDoc(InsertionVoltagePairDoc):
)

framework: Composition = Field(
None,
description="The chemical compositions of the host framework",
None, description="The chemical compositions of the host framework"
)

elements: List[Element] = Field(
Expand Down Expand Up @@ -162,9 +157,8 @@ class InsertionElectrodeDoc(InsertionVoltagePairDoc):
description="Anonymized representation of the formula (not including the working ion)",
)

electrode_object: Dict = Field(
None,
description="The pymatgen electrode object",
electrode_object: InsertionElectrode = Field(
None, description="The pymatgen electrode object"
)

# Make sure that the datetime field is properly formatted
Expand Down Expand Up @@ -223,13 +217,11 @@ 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",
None, description="Returns all the adjacent Voltage Steps"
)

working_ion: Element = Field(
None,
description="The working ion as an Element object",
None, description="The working ion as an Element object"
)

num_steps: int = Field(
Expand Down
17 changes: 7 additions & 10 deletions emmet-core/emmet/core/electronic_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@


class ElectronicStructureBaseData(BaseModel):
task_id: Union[MPID, int] = Field(
...,
description="The source calculation (task) ID for the electronic structure data.",
)
task_id: MPID = Field(..., description="The source calculation (task) ID for the electronic structure data.")

band_gap: float = Field(..., description="Band gap energy in eV.")

Expand Down Expand Up @@ -134,14 +131,14 @@ class ElectronicStructureDoc(PropertyDoc, ElectronicStructureSummary):
@classmethod
def from_bsdos( # type: ignore[override]
cls: Type[T],
material_id: Union[MPID, int],
dos: Dict[Union[MPID, int], CompleteDos],
material_id: MPID,
dos: Dict[MPID, CompleteDos],
is_gap_direct: bool,
is_metal: bool,
structures: Dict[Union[MPID, int], Structure] = None,
setyawan_curtarolo: Dict[Union[MPID, int], BandStructureSymmLine] = None,
hinuma: Dict[Union[MPID, int], BandStructureSymmLine] = None,
latimer_munro: Dict[Union[MPID, int], BandStructureSymmLine] = None,
structures: Dict[MPID, Structure] = None,
setyawan_curtarolo: Dict[MPID, BandStructureSymmLine] = None,
hinuma: Dict[MPID, BandStructureSymmLine] = None,
latimer_munro: Dict[MPID, BandStructureSymmLine] = None,
) -> T:
"""
Builds a electronic structure document using band structure and density of states data.
Expand Down
13 changes: 3 additions & 10 deletions emmet-core/emmet/core/xas.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ class XASDoc(SpectrumDoc):

@classmethod
def from_spectrum(
cls,
xas_spectrum: XAS,
material_id: MPID,
**kwargs,
cls, xas_spectrum: XAS, material_id: MPID, **kwargs,
):
spectrum_type = xas_spectrum.spectrum_type
el = xas_spectrum.absorbing_element
Expand Down Expand Up @@ -130,19 +127,15 @@ def from_task_docs(
# Dictionary of all site to spectra mapping
sites_to_spectra = {
index: list(group)
for index, group in groupby(
all_spectra,
key=lambda x: x.absorbing_index,
)
for index, group in groupby(all_spectra, key=lambda x: x.absorbing_index,)
}

# perform spectra merging
for site, spectra in sites_to_spectra.items():
type_to_spectra = {
index: list(group)
for index, group in groupby(
spectra,
key=lambda x: (x.edge, x.spectrum_type),
spectra, key=lambda x: (x.edge, x.spectrum_type),
)
}
# Make K-edge XAFS spectra by merging XANES + EXAFS
Expand Down

0 comments on commit d8e6010

Please sign in to comment.