From 19d3dcf85174ae514290bd43983c1fbb8322f9a9 Mon Sep 17 00:00:00 2001 From: Ivan Korotkin Date: Tue, 12 Sep 2023 10:05:24 +0100 Subject: [PATCH] Moved STO validator from schema to new file validators.py --- bpx/__init__.py | 1 + bpx/schema.py | 41 +---------------------------------------- bpx/validators.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 40 deletions(-) create mode 100644 bpx/validators.py diff --git a/bpx/__init__.py b/bpx/__init__.py index a4b6ea2..e7d9f3b 100644 --- a/bpx/__init__.py +++ b/bpx/__init__.py @@ -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 diff --git a/bpx/schema.py b/bpx/schema.py index 8fbb235..e12b4d2 100644 --- a/bpx/schema.py +++ b/bpx/schema.py @@ -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 @@ -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", diff --git a/bpx/validators.py b/bpx/validators.py new file mode 100644 index 0000000..b7a8aa5 --- /dev/null +++ b/bpx/validators.py @@ -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