Skip to content

Commit

Permalink
Moved STO validator from schema to new file validators.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ikorotkin committed Sep 12, 2023
1 parent 7a54d44 commit 19d3dcf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 40 deletions.
1 change: 1 addition & 0 deletions bpx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .interpolated_table import InterpolatedTable
from .expression_parser import ExpressionParser
from .function import Function
from .validators import check_sto_limits
from .schema import BPX
from .parsers import parse_bpx_str, parse_bpx_obj, parse_bpx_file
from .utilities import get_electrode_stoichiometries, get_electrode_concentrations
41 changes: 1 addition & 40 deletions bpx/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pydantic import BaseModel, Field, Extra, root_validator

from bpx import Function, InterpolatedTable
from bpx import Function, InterpolatedTable, check_sto_limits

from warnings import warn

Expand Down Expand Up @@ -296,45 +296,6 @@ class Experiment(ExtraBaseModel):
)


# Validates that the STO limits subbed into the OCPs give the correct voltage limits.
# Works if both OCPs are defined as functions.
# Blended electrodes are not supported.
# This is a reusable validator to be used for both DFN/SPMe and SPM parameter sets.
# https://docs.pydantic.dev/latest/usage/validators/#root-validators
def check_sto_limits(cls, values):
try:
ocp_n = values.get("negative_electrode").ocp.to_python_function()
ocp_p = values.get("positive_electrode").ocp.to_python_function()
except AttributeError:
# OCPs defined as interpolated tables; do nothing
return values

sto_n_min = values.get("negative_electrode").minimum_stoichiometry
sto_n_max = values.get("negative_electrode").maximum_stoichiometry
sto_p_min = values.get("positive_electrode").minimum_stoichiometry
sto_p_max = values.get("positive_electrode").maximum_stoichiometry
V_min = values.get("cell").lower_voltage_cutoff
V_max = values.get("cell").upper_voltage_cutoff

# Checks the maximum voltage estimated from STO
V_max_sto = ocp_p(sto_p_min) - ocp_n(sto_n_max)
if V_max_sto > V_max:
warn(
f"The maximum voltage computed from the STO limits ({V_max_sto} V) "
f"is higher than the maximum allowed voltage ({V_max} V)"
)

# Checks the minimum voltage estimated from STO
V_min_sto = ocp_p(sto_p_max) - ocp_n(sto_n_min)
if V_min_sto < V_min:
warn(
f"The minimum voltage computed from the STO limits ({V_min_sto} V) "
f"is lower than the minimum allowed voltage ({V_min} V)"
)

return values


class Parameterisation(ExtraBaseModel):
cell: Cell = Field(
alias="Cell",
Expand Down
42 changes: 42 additions & 0 deletions bpx/validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from warnings import warn


def check_sto_limits(cls, values):
"""
Validates that the STO limits subbed into the OCPs give the correct voltage limits.
Works if both OCPs are defined as functions.
Blended electrodes are not supported.
This is a reusable validator to be used for both DFN/SPMe and SPM parameter sets.
"""

try:
ocp_n = values.get("negative_electrode").ocp.to_python_function()
ocp_p = values.get("positive_electrode").ocp.to_python_function()
except AttributeError:
# OCPs defined as interpolated tables or one of the electrodes is blended; do nothing
return values

sto_n_min = values.get("negative_electrode").minimum_stoichiometry
sto_n_max = values.get("negative_electrode").maximum_stoichiometry
sto_p_min = values.get("positive_electrode").minimum_stoichiometry
sto_p_max = values.get("positive_electrode").maximum_stoichiometry
V_min = values.get("cell").lower_voltage_cutoff
V_max = values.get("cell").upper_voltage_cutoff

# Checks the maximum voltage estimated from STO
V_max_sto = ocp_p(sto_p_min) - ocp_n(sto_n_max)
if V_max_sto > V_max:
warn(
f"The maximum voltage computed from the STO limits ({V_max_sto} V) "
f"is higher than the upper voltage cut-off ({V_max} V)"
)

# Checks the minimum voltage estimated from STO
V_min_sto = ocp_p(sto_p_max) - ocp_n(sto_n_min)
if V_min_sto < V_min:
warn(
f"The minimum voltage computed from the STO limits ({V_min_sto} V) "
f"is less than the lower voltage cut-off ({V_min} V)"
)

return values

0 comments on commit 19d3dcf

Please sign in to comment.