Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add-custom-module-functions-functionality #70

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion bpx/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def __modify_schema__(cls, field_schema):
def validate(cls, v: str) -> Function:
if not isinstance(v, str):
raise TypeError("string required")
if '"' in v:
return cls(v)
try:
cls.parser.parse_string(v)
except ExpressionParser.ParseException as e:
Expand All @@ -56,6 +58,12 @@ def to_python_function(self, preamble: str = None) -> Callable:
preamble += "\n\n"
arg_names = ["x"]
arg_str = ",".join(arg_names)
if '"' in self:
nested = True
else:
nested = False
if nested:
preamble += f"import {self.split('.')[0]}\n\n"
function_name = "reconstructed_function"
function_def = f"def {function_name}({arg_str}):\n"
function_body = f" return {self}"
Expand All @@ -77,4 +85,7 @@ def to_python_function(self, preamble: str = None) -> Callable:

# return the new function object
value = getattr(module, function_name)
return value
if nested:
return value(0)
else:
return value
12 changes: 9 additions & 3 deletions bpx/validators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from warnings import warn

import pybamm

def check_sto_limits(cls, values):
"""
Expand Down Expand Up @@ -27,7 +27,13 @@ def check_sto_limits(cls, values):
tol = cls.settings.tolerances["Voltage [V]"]

# Checks the maximum voltage estimated from STO
V_max_sto = ocp_p(sto_p_min) - ocp_n(sto_n_max)
negative_ocp = ocp_n(sto_n_max)
if isinstance(negative_ocp, pybamm.Scalar):
negative_ocp = negative_ocp.evaluate()
positive_ocp = ocp_p(sto_p_min)
if isinstance(positive_ocp, pybamm.Scalar):
positive_ocp = positive_ocp.evaluate()
V_max_sto = positive_ocp - negative_ocp
if V_max_sto - V_max > tol:
warn(
f"The maximum voltage computed from the STO limits ({V_max_sto} V) "
Expand All @@ -36,7 +42,7 @@ def check_sto_limits(cls, values):
)

# Checks the minimum voltage estimated from STO
V_min_sto = ocp_p(sto_p_max) - ocp_n(sto_n_min)
V_min_sto = positive_ocp - negative_ocp
if V_min_sto - V_min < -tol:
warn(
f"The minimum voltage computed from the STO limits ({V_min_sto} V) "
Expand Down