Skip to content

Commit

Permalink
New magnetic search query op
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Munro committed May 28, 2021
1 parent b118a15 commit 83747e1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 178 deletions.
195 changes: 17 additions & 178 deletions src/mp_api/routes/search/query_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class HasPropsQuery(QueryOperator):
def query(
self,
has_props: Optional[str] = Query(
None, description="Comma-delimited list of possible properties given by HasPropsEnum to search for.",
None,
description="Comma-delimited list of possible properties given by HasPropsEnum to search for.",
),
) -> STORE_PARAMS:

Expand All @@ -49,7 +50,10 @@ class MaterialIDsSearchQuery(QueryOperator):
"""

def query(
self, material_ids: Optional[str] = Query(None, description="Comma-separated list of material_ids to query on"),
self,
material_ids: Optional[str] = Query(
None, description="Comma-separated list of material_ids to query on"
),
) -> STORE_PARAMS:

crit = {}
Expand All @@ -66,7 +70,10 @@ class SearchIsStableQuery(QueryOperator):
"""

def query(
self, is_stable: Optional[bool] = Query(None, description="Whether the material is stable."),
self,
is_stable: Optional[bool] = Query(
None, description="Whether the material is stable."
),
):

crit = {}
Expand All @@ -80,198 +87,27 @@ def ensure_indexes(self):
return [("is_stable", False)]


class SearchElasticityQuery(QueryOperator):
"""
Method to generate a query for ranges of elasticity data in search docs
"""

def query(
self,
k_voigt_max: Optional[float] = Query(
None, description="Maximum value for the Voigt average of the bulk modulus in GPa.",
),
k_voigt_min: Optional[float] = Query(
None, description="Minimum value for the Voigt average of the bulk modulus in GPa.",
),
k_reuss_max: Optional[float] = Query(
None, description="Maximum value for the Reuss average of the bulk modulus in GPa.",
),
k_reuss_min: Optional[float] = Query(
None, description="Minimum value for the Reuss average of the bulk modulus in GPa.",
),
k_vrh_max: Optional[float] = Query(
None, description="Maximum value for the Voigt-Reuss-Hill average of the bulk modulus in GPa.",
),
k_vrh_min: Optional[float] = Query(
None, description="Minimum value for the Voigt-Reuss-Hill average of the bulk modulus in GPa.",
),
g_voigt_max: Optional[float] = Query(
None, description="Maximum value for the Voigt average of the shear modulus in GPa.",
),
g_voigt_min: Optional[float] = Query(
None, description="Minimum value for the Voigt average of the shear modulus in GPa.",
),
g_reuss_max: Optional[float] = Query(
None, description="Maximum value for the Reuss average of the shear modulus in GPa.",
),
g_reuss_min: Optional[float] = Query(
None, description="Minimum value for the Reuss average of the shear modulus in GPa.",
),
g_vrh_max: Optional[float] = Query(
None, description="Maximum value for the Voigt-Reuss-Hill average of the shear modulus in GPa.",
),
g_vrh_min: Optional[float] = Query(
None, description="Minimum value for the Voigt-Reuss-Hill average of the shear modulus in GPa.",
),
elastic_anisotropy_max: Optional[float] = Query(None, description="Maximum value for the elastic anisotropy.",),
elastic_anisotropy_min: Optional[float] = Query(None, description="Maximum value for the elastic anisotropy.",),
poisson_max: Optional[float] = Query(None, description="Maximum value for Poisson's ratio.",),
poisson_min: Optional[float] = Query(None, description="Minimum value for Poisson's ratio.",),
) -> STORE_PARAMS:

crit = defaultdict(dict) # type: dict

d = {
"k_voigt": [k_voigt_min, k_voigt_max],
"k_reuss": [k_reuss_min, k_reuss_max],
"k_vrh": [k_vrh_min, k_vrh_max],
"g_voigt": [g_voigt_min, g_voigt_max],
"g_reuss": [g_reuss_min, g_reuss_max],
"g_vrh": [g_vrh_min, g_vrh_max],
"universal_anisotropy": [elastic_anisotropy_min, elastic_anisotropy_max],
"homogeneous_poisson": [poisson_min, poisson_max],
}

for entry in d:
if d[entry][0]:
crit[entry]["$gte"] = d[entry][0]

if d[entry][1]:
crit[entry]["$lte"] = d[entry][1]

return {"criteria": crit}

def ensure_indexes(self):
keys = [key for key in self._keys_from_query() if "anisotropy" not in key and "poisson" not in key]

indexes = []
for key in keys:
if "_min" in key:
key = key.replace("_min", "")
indexes.append((key, False))
indexes.append(("universal_anisotropy", False))
indexes.append(("homogeneous_poisson", False))
return indexes


class SearchMagneticQuery(QueryOperator):
"""
Method to generate a query for magnetic data in search docs.
"""

def query(
self,
ordering: Optional[MagneticOrderingEnum] = Query(None, description="Magnetic ordering of the material."),
total_magnetization_max: Optional[float] = Query(
None, description="Maximum value for the total magnetization.",
),
total_magnetization_min: Optional[float] = Query(
None, description="Minimum value for the total magnetization.",
),
total_magnetization_normalized_vol_max: Optional[float] = Query(
None, description="Maximum value for the total magnetization normalized with volume.",
),
total_magnetization_normalized_vol_min: Optional[float] = Query(
None, description="Minimum value for the total magnetization normalized with volume.",
),
total_magnetization_normalized_formula_units_max: Optional[float] = Query(
None, description="Maximum value for the total magnetization normalized with formula units.",
),
total_magnetization_normalized_formula_units_min: Optional[float] = Query(
None, description="Minimum value for the total magnetization normalized with formula units.",
ordering: Optional[MagneticOrderingEnum] = Query(
None, description="Magnetic ordering of the material."
),
) -> STORE_PARAMS:

crit = defaultdict(dict) # type: dict

d = {
"total_magnetization": [total_magnetization_min, total_magnetization_max],
"total_magnetization_normalized_vol": [
total_magnetization_normalized_vol_min,
total_magnetization_normalized_vol_max,
],
"total_magnetization_normalized_formula_units": [
total_magnetization_normalized_formula_units_min,
total_magnetization_normalized_formula_units_max,
],
} # type: dict

for entry in d:
if d[entry][0]:
crit[entry]["$gte"] = d[entry][0]

if d[entry][1]:
crit[entry]["$lte"] = d[entry][1]

if ordering:
crit["ordering"] = ordering.value

return {"criteria": crit}

def ensure_indexes(self):
keys = [
"total_magnetization",
"total_magnetization_normalized_vol",
"total_magnetization_normalized_formula_units",
]
return [(key, False) for key in keys]


class SearchDielectricPiezoQuery(QueryOperator):
"""
Method to generate a query for ranges of dielectric and piezo data in search docs
"""

def query(
self,
e_total_max: Optional[float] = Query(None, description="Maximum value for the total dielectric constant.",),
e_total_min: Optional[float] = Query(None, description="Minimum value for the total dielectric constant.",),
e_ionic_max: Optional[float] = Query(None, description="Maximum value for the ionic dielectric constant.",),
e_ionic_min: Optional[float] = Query(None, description="Minimum value for the ionic dielectric constant.",),
e_static_max: Optional[float] = Query(None, description="Maximum value for the static dielectric constant.",),
e_static_min: Optional[float] = Query(None, description="Minimum value for the static dielectric constant.",),
n_max: Optional[float] = Query(None, description="Maximum value for the refractive index.",),
n_min: Optional[float] = Query(None, description="Minimum value for the refractive index.",),
piezo_modulus_max: Optional[float] = Query(
None, description="Maximum value for the piezoelectric modulus in C/m².",
),
piezo_modulus_min: Optional[float] = Query(
None, description="Minimum value for the piezoelectric modulus in C/m².",
),
) -> STORE_PARAMS:

crit = defaultdict(dict) # type: dict

d = {
"e_total": [e_total_min, e_total_max],
"e_ionic": [e_ionic_min, e_ionic_max],
"e_static": [e_static_min, e_static_max],
"n": [n_min, n_max],
"e_ij_max": [piezo_modulus_min, piezo_modulus_max],
}

for entry in d:
if d[entry][0]:
crit[entry]["$gte"] = d[entry][0]

if d[entry][1]:
crit[entry]["$lte"] = d[entry][1]

return {"criteria": crit}

def ensure_indexes(self):
keys = ["e_total", "e_ionic", "e_static", "n", "e_ij_max"]
return [(key, False) for key in keys]
return [("ordering", False)]


class SearchIsTheoreticalQuery(QueryOperator):
Expand All @@ -280,7 +116,10 @@ class SearchIsTheoreticalQuery(QueryOperator):
"""

def query(
self, theoretical: Optional[bool] = Query(None, description="Whether the material is theoretical."),
self,
theoretical: Optional[bool] = Query(
None, description="Whether the material is theoretical."
),
):

crit = {}
Expand Down
2 changes: 2 additions & 0 deletions src/mp_api/routes/search/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
HasPropsQuery,
SearchIsStableQuery,
SearchIsTheoreticalQuery,
SearchMagneticQuery,
)


Expand Down Expand Up @@ -128,6 +129,7 @@ async def generate_stats(
SymmetryQuery(),
SearchIsStableQuery(),
SearchIsTheoreticalQuery(),
SearchMagneticQuery(),
NumericQuery(model=SearchDoc, excluded_fields=["composition"]),
HasPropsQuery(),
DeprecationQuery(),
Expand Down

0 comments on commit 83747e1

Please sign in to comment.