Skip to content

Commit

Permalink
Insertion electrodes endpoint draft added
Browse files Browse the repository at this point in the history
  • Loading branch information
munrojm committed Nov 30, 2020
1 parent 827e2a3 commit ad7db58
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 11 deletions.
18 changes: 18 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
wulff_store_json = os.environ.get("WULFF_STORE", "wulff_store.json")
robocrys_store_json = os.environ.get("ROBOCRYS_STORE", "robocrys_store.json")
synth_store_json = os.environ.get("SYNTH_STORE", "synth_store.json")
insertion_electrodes_store_json = os.environ.get(
"INSERTION_ELECTRODES_STORE", "insertion_electrodes_store.json"
)
search_store_json = os.environ.get("SEARCH_STORE", "search_store.json")

bs_store_json = os.environ.get("BS_STORE", "bs_store.json")
Expand Down Expand Up @@ -181,6 +184,13 @@
collection_name="synth_descriptions",
)

insertion_electrodes_store = MongoURIStore(
uri=f"mongodb+srv://{db_uri}",
database="mp_core",
key="battery_id",
collection_name="insertion_electrodes",
)

search_store = MongoURIStore(
uri=f"mongodb+srv://{db_uri}",
database="mp_core",
Expand Down Expand Up @@ -241,6 +251,7 @@
wulff_store = loadfn(wulff_store_json)
robo_store = loadfn(robocrys_store_json)
synth_store = loadfn(synth_store_json)
insertion_electrodes_store = loadn(insertion_electrodes_store_json)
search_store = loadfn(search_store_json)

bs_store = loadfn(bs_store_json)
Expand Down Expand Up @@ -356,6 +367,13 @@

resources.update({"synthesis": synth_resource(synth_store)})

# Electrodes
from mp_api.electrodes.resources import insertion_electrodes_resource

resources.update(
{"insertion_electrodes": insertion_electrodes_resource(insertion_electrodes_store)}
)

# Search
from mp_api.search.resources import search_resource

Expand Down
Empty file.
26 changes: 15 additions & 11 deletions src/mp_api/electrodes/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from monty.json import MontyDecoder


""" Core definition of an Electrode Document """
from pymatgen.core.periodic_table import Element
from typing import Dict, List
from datetime import datetime

Expand All @@ -23,11 +21,13 @@ class VoltageStep(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.",
)

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

capacity_grav: float = Field(None, description="Gravimetric capacity in mAh/g.")
Expand Down Expand Up @@ -64,7 +64,7 @@ class InsertionVoltageStep(VoltageStep):
)


class InsertionElectrode(InsertionVoltageStep):
class InsertionElectrodeDoc(InsertionVoltageStep):

battery_id: str = Field(None, description="The id for this battery document.")

Expand All @@ -74,11 +74,13 @@ class InsertionElectrode(InsertionVoltageStep):
)

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

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

num_steps: float = Field(
Expand Down Expand Up @@ -117,11 +119,13 @@ class ConversionElectrode(ConversionVoltageStep):
battery_id: str = Field(None, description="The id for this battery document.")

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

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

num_steps: float = Field(
Expand Down
212 changes: 212 additions & 0 deletions src/mp_api/electrodes/query_operators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
from typing import Optional
from fastapi import Query
from mp_api.core.query_operator import STORE_PARAMS, QueryOperator
from pymatgen.core.periodic_table import Element

from collections import defaultdict


class VoltageStepQuery(QueryOperator):
"""
Method to generate a query for ranges of voltage step data values
"""

def query(
self,
delta_volume_max: Optional[float] = Query(
None,
description="Maximum value for the max Volume change in percent for a particular voltage step.",
),
delta_volume_min: Optional[float] = Query(
None,
description="Minimum value for the max Volume change in percent for a particular voltage step.",
),
average_voltage_max: Optional[float] = Query(
None,
description="Maximum value for the average voltage for a particular voltage step in V.",
),
average_voltage_min: Optional[float] = Query(
None,
description="Minimum value for the average voltage for a particular voltage step in V.",
),
max_voltage_max: Optional[float] = Query(
None,
description="Maximum value for the maximum voltage for a particular voltage step in V.",
),
max_voltage_min: Optional[float] = Query(
None,
description="Minimum value for the maximum voltage for a particular voltage step in V.",
),
min_voltage_max: Optional[float] = Query(
None,
description="Maximum value for the minimum voltage for a particular voltage step in V.",
),
min_voltage_min: Optional[float] = Query(
None,
description="Minimum value for the minimum voltage for a particular voltage step in V.",
),
capacity_grav_max: Optional[float] = Query(
None,
description="Maximum value for the gravimetric capacity in maH/g.",
),
capacity_grav_min: Optional[float] = Query(
None,
description="Minimum value for the gravimetric capacity in maH/g.",
),
capacity_vol_max: Optional[float] = Query(
None,
description="Maximum value for the volumetric capacity in maH/cc.",
),
capacity_vol_min: Optional[float] = Query(
None,
description="Minimum value for the volumetric capacity in maH/cc.",
),
energy_grav_max: Optional[float] = Query(
None,
description="Maximum value for the gravimetric energy (specific energy) in Wh/kg.",
),
energy_grav_min: Optional[float] = Query(
None,
description="Minimum value for the gravimetric energy (specific energy) in Wh/kg.",
),
energy_vol_max: Optional[float] = Query(
None,
description="Maximum value for the volumetric energy (energy_density) in Wh/l.",
),
energy_vol_min: Optional[float] = Query(
None,
description="Minimum value for the volumetric energy (energy_density) in Wh/l.",
),
fracA_charge_max: Optional[float] = Query(
None,
description="Maximum value for the atomic fraction of the working ion in the charged state.",
),
fracA_charge_min: Optional[float] = Query(
None,
description="Minimum value for the atomic fraction of the working ion in the charged state.",
),
fracA_discharge_max: Optional[float] = Query(
None,
description="Maximum value for the atomic fraction of the working ion in the discharged state.",
),
fracA_discharge_min: Optional[float] = Query(
None,
description="Minimum value for the atomic fraction of the working ion in the discharged state.",
),
) -> STORE_PARAMS:

crit = defaultdict(dict) # type: dict

d = {
"max_delta_volume": [delta_volume_min, delta_volume_max],
"average_voltage": [average_voltage_min, average_voltage_max],
"max_voltage": [max_voltage_min, max_voltage_max],
"min_voltage": [min_voltage_min, min_voltage_max],
"capacity_grav": [capacity_grav_min, capacity_grav_max],
"capacity_vol": [capacity_vol_min, capacity_vol_max],
"energy_grav": [energy_grav_min, energy_grav_max],
"energy_vol": [energy_vol_min, energy_vol_max],
"fracA_charge": [fracA_charge_min, fracA_charge_max],
"fracA_discharge": [fracA_discharge_min, fracA_discharge_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}


class InsertionVoltageStepQuery(QueryOperator):
"""
Method to generate a query for ranges of insertion voltage step data values
"""

def query(
self,
stability_charge_max: Optional[float] = Query(
None,
description="The maximum value of the energy above hull of the charged material.",
),
stability_charge_min: Optional[float] = Query(
None,
description="The minimum value of the energy above hull of the charged material.",
),
stability_discharge_max: Optional[float] = Query(
None,
description="The maximum value of the energy above hull of the discharged material.",
),
stability_discharge_min: Optional[float] = Query(
None,
description="The minimum value of the energy above hull of the discharged material.",
),
) -> STORE_PARAMS:

crit = defaultdict(dict) # type: dict

d = {
"stability_charge": [stability_charge_min, stability_charge_max],
"stability_discharge": [stability_discharge_min, stability_discharge_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}


class InsertionElectrodeQuery(QueryOperator):
"""
Method to generate a query for ranges of insertion electrode data values
"""

def query(
self,
working_ion: Optional[Element] = Query(
None, title="Element of the working ion"
),
num_steps_max: Optional[float] = Query(
None,
description="The maximum value of the The number of distinct voltage steps from fully charge to \
discharge based on the stable intermediate states.",
),
num_steps_min: Optional[float] = Query(
None,
description="The minimum value of the The number of distinct voltage steps from fully charge to \
discharge based on the stable intermediate states.",
),
max_voltage_step_max: Optional[float] = Query(
None,
description="The maximum value of the maximum absolute difference in adjacent voltage steps.",
),
max_voltage_step_min: Optional[float] = Query(
None,
description="The minimum value of maximum absolute difference in adjacent voltage steps.",
),
) -> STORE_PARAMS:

crit = defaultdict(dict) # type: dict

d = {
"num_steps": [num_steps_min, num_steps_max],
"max_voltage_step": [max_voltage_step_min, max_voltage_step_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]

if working_ion:
crit["working_ion"] = str(working_ion)

return {"criteria": crit}
30 changes: 30 additions & 0 deletions src/mp_api/electrodes/resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from mp_api.core.resource import Resource
from mp_api.electrodes.models import InsertionElectrodeDoc

from mp_api.core.query_operator import PaginationQuery, SortQuery, SparseFieldsQuery
from mp_api.electrodes.query_operators import (
VoltageStepQuery,
InsertionVoltageStepQuery,
InsertionElectrodeQuery,
)


def insertion_electrodes_resource(insertion_electrodes_store):
resource = Resource(
insertion_electrodes_store,
InsertionElectrodeDoc,
query_operators=[
VoltageStepQuery(),
InsertionVoltageStepQuery(),
InsertionElectrodeQuery(),
SortQuery(),
PaginationQuery(),
SparseFieldsQuery(
InsertionElectrodeDoc,
default_fields=["battery_id", "last_updated"],
),
],
tags=["Electrodes"],
)

return resource

0 comments on commit ad7db58

Please sign in to comment.