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
|
from __future__ import annotations
from emmet.core.settings import EmmetSettings
from emmet.core.symmetry import CrystalSystem
from emmet.core.vasp.material import MaterialsDoc
from pymatgen.core.structure import Structure
from mp_api.client.core import BaseRester, MPRestError
from mp_api.client.core.utils import validate_ids
from mp_api.client.routes.materials import (
AbsorptionRester,
AlloysRester,
BandStructureRester,
BondsRester,
ChemenvRester,
DielectricRester,
DosRester,
ElasticityRester,
ElectrodeRester,
ElectronicStructureRester,
EOSRester,
GrainBoundaryRester,
MagnetismRester,
OxidationStatesRester,
PhononRester,
PiezoRester,
ProvenanceRester,
RobocrysRester,
SimilarityRester,
SubstratesRester,
SummaryRester,
SurfacePropertiesRester,
SynthesisRester,
TaskRester,
ThermoRester,
XASRester,
)
_EMMET_SETTINGS = EmmetSettings() # type: ignore
class MaterialsRester(BaseRester[MaterialsDoc]):
suffix = "materials/core"
document_model = MaterialsDoc # type: ignore
supports_versions = True
primary_key = "material_id"
_sub_resters = [
"eos",
"similarity",
"tasks",
"xas",
"grain_boundaries",
"substrates",
"surface_properties",
"phonon",
"elasticity",
"thermo",
"dielectric",
"piezoelectric",
"magnetism",
"summary",
"robocrys",
"synthesis",
"insertion_electrodes",
"electronic_structure",
"electronic_structure_bandstructure",
"electronic_structure_dos",
"oxidation_states",
"provenance",
"bonds",
"alloys",
"absorption",
"chemenv",
]
# Materials subresters
eos: EOSRester
materials: MaterialsRester
similarity: SimilarityRester
tasks: TaskRester
xas: XASRester
grain_boundary: GrainBoundaryRester
substrates: SubstratesRester
surface_properties: SurfacePropertiesRester
phonon: PhononRester
elasticity: ElasticityRester
thermo: ThermoRester
dielectric: DielectricRester
piezoelectric: PiezoRester
magnetism: MagnetismRester
summary: SummaryRester
robocrys: RobocrysRester
synthesis: SynthesisRester
insertion_electrodes: ElectrodeRester
electronic_structure: ElectronicStructureRester
electronic_structure_bandstructure: BandStructureRester
electronic_structure_dos: DosRester
oxidation_states: OxidationStatesRester
provenance: ProvenanceRester
bonds: BondsRester
alloys: AlloysRester
absorption: AbsorptionRester
chemenv: ChemenvRester
def __dir__(self):
return dir(MaterialsRester) + self._sub_resters
def get_structure_by_material_id(
self, material_id: str, final: bool = True
) -> Structure | list[Structure]:
"""Get a structure for a given Materials Project ID.
Arguments:
material_id (str): Materials project ID
final (bool): Whether to get the final structure, or the list of initial
(pre-relaxation) structures. Defaults to True.
Returns:
structure (Union[Structure, List[Structure]]): Pymatgen structure object or list of
pymatgen structure objects.
"""
field = "structure" if final else "initial_structures"
response = self.search(material_ids=material_id, fields=[field])
if response:
response = (
response[0].model_dump() if self.use_document_model else response[0] # type: ignore
)
return response[field] if response else response # type: ignore
def search(
self,
material_ids: str | list[str] | None = None,
chemsys: str | list[str] | None = None,
crystal_system: CrystalSystem | None = None,
density: tuple[float, float] | None = None,
deprecated: bool | None = False,
elements: list[str] | None = None,
exclude_elements: list[str] | None = None,
formula: str | list[str] | None = None,
num_elements: tuple[int, int] | None = None,
num_sites: tuple[int, int] | None = None,
spacegroup_number: int | None = None,
spacegroup_symbol: str | None = None,
task_ids: list[str] | None = None,
volume: tuple[float, float] | None = None,
num_chunks: int | None = None,
chunk_size: int = 1000,
all_fields: bool = True,
fields: list[str] | None = None,
) -> list[MaterialsDoc] | list[dict]:
"""Query core material docs using a variety of search criteria.
Arguments:
material_ids (str, List[str]): A single Material ID string or list of strings
(e.g., mp-149, [mp-149, mp-13]).
chemsys (str, List[str]): A chemical system or list of chemical systems
(e.g., Li-Fe-O, Si-*, [Si-O, Li-Fe-P]).
crystal_system (CrystalSystem): Crystal system of material.
density (Tuple[float,float]): Minimum and maximum density to consider.
deprecated (bool): Whether the material is tagged as deprecated.
elements (List[str]): A list of elements.
exclude_elements (List[str]): A list of elements to exclude.
formula (str, List[str]): A formula including anonymized formula
or wild cards (e.g., Fe2O3, ABO3, Si*). A list of chemical formulas can also be passed
(e.g., [Fe2O3, ABO3]).
num_elements (Tuple[int,int]): Minimum and maximum number of elements to consider.
num_sites (Tuple[int,int]): Minimum and maximum number of sites to consider.
spacegroup_number (int): Space group number of material.
spacegroup_symbol (str): Space group symbol of the material in international short symbol notation.
task_ids (List[str]): List of Materials Project IDs to return data for.
volume (Tuple[float,float]): Minimum and maximum volume to consider.
num_chunks (int): Maximum number of chunks of data to yield. None will yield all possible.
chunk_size (int): Number of data entries per chunk.
all_fields (bool): Whether to return all fields in the document. Defaults to True.
fields (List[str]): List of fields in MaterialsCoreDoc to return data for.
Default is material_id, last_updated, and formula_pretty if all_fields is False.
Returns:
([MaterialsDoc], [dict]) List of material documents or dictionaries.
"""
query_params = {"deprecated": deprecated} # type: dict
if material_ids:
if isinstance(material_ids, str):
material_ids = [material_ids]
query_params.update({"material_ids": ",".join(validate_ids(material_ids))})
if formula:
if isinstance(formula, str):
formula = [formula]
query_params.update({"formula": ",".join(formula)})
if chemsys:
if isinstance(chemsys, str):
chemsys = [chemsys]
query_params.update({"chemsys": ",".join(chemsys)})
if elements:
query_params.update({"elements": ",".join(elements)})
if exclude_elements:
query_params.update({"exclude_elements": ",".join(exclude_elements)})
if task_ids:
query_params.update({"task_ids": ",".join(validate_ids(task_ids))})
query_params.update(
{
"crystal_system": crystal_system,
"spacegroup_number": spacegroup_number,
"spacegroup_symbol": spacegroup_symbol,
}
)
if num_sites:
query_params.update(
{"nsites_min": num_sites[0], "nsites_max": num_sites[1]}
)
if num_elements:
if isinstance(num_elements, int):
num_elements = (num_elements, num_elements)
query_params.update(
{"nelements_min": num_elements[0], "nelements_max": num_elements[1]}
)
if volume:
query_params.update({"volume_min": volume[0], "volume_max": volume[1]})
if density:
query_params.update({"density_min": density[0], "density_max": density[1]})
query_params = {
entry: query_params[entry]
for entry in query_params
if query_params[entry] is not None
}
return super()._search(
num_chunks=num_chunks,
chunk_size=chunk_size,
all_fields=all_fields,
fields=fields,
**query_params,
)
def find_structure(
self,
filename_or_structure,
ltol=_EMMET_SETTINGS.LTOL,
stol=_EMMET_SETTINGS.STOL,
angle_tol=_EMMET_SETTINGS.ANGLE_TOL,
allow_multiple_results=False,
) -> list[str] | str:
"""Finds matching structures from the Materials Project database.
Multiple results may be returned of "similar" structures based on
distance using the pymatgen StructureMatcher algorithm, however only
a single result should match with the same spacegroup, calculated to the
default tolerances.
Args:
filename_or_structure: filename or Structure object
ltol: fractional length tolerance
stol: site tolerance
angle_tol: angle tolerance in degrees
allow_multiple_results: changes return type for either
a single material_id or list of material_ids
Returns:
A matching material_id if one is found or list of results if allow_multiple_results
is True
Raises:
MPRestError
"""
params = {"ltol": ltol, "stol": stol, "angle_tol": angle_tol, "_limit": 1}
if isinstance(filename_or_structure, str):
s = Structure.from_file(filename_or_structure)
elif isinstance(filename_or_structure, Structure):
s = filename_or_structure
else:
raise MPRestError("Provide filename or Structure object.")
results = self._post_resource(
body=s.as_dict(),
params=params,
suburl="find_structure",
use_document_model=False,
).get("data")
if len(results) > 1: # type: ignore
if not allow_multiple_results:
raise ValueError(
"Multiple matches found for this combination of tolerances, but "
"`allow_multiple_results` set to False."
)
return results # type: ignore
if results:
return results[0]["material_id"]
else:
return []
|