Skip to content

Commit

Permalink
added check for input params in symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishab87 committed Feb 20, 2025
1 parent f5c8986 commit 435ad80
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 44 deletions.
29 changes: 9 additions & 20 deletions src/pybamm/experiment/step/base_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,17 +394,9 @@ def value_based_charge_or_discharge(self):
Determine whether the step is a charge or discharge step based on the value of the
step. If an operator is provided, the step direction is not used, so we return None.
"""
if isinstance(self.value, pybamm.InputParameter):
return None
elif callable(self.value):
is_dynamic = _check_callable(self.value)
if is_dynamic:
if isinstance(self.value, pybamm.Symbol):
if _check_input_params(self.value):
return None
else:
raise ValueError(
"Cannot determine charge or discharge direction for a function"
)
elif isinstance(self.value, pybamm.Symbol):
inpt = {"start time": 0}
init_curr = self.value.evaluate(t=0, inputs=inpt).flatten()[0]
else:
Expand Down Expand Up @@ -609,21 +601,18 @@ def _parse_termination(term_str, value):
remaining = remaining.replace(" ", "")
typ, val = _convert_electric(remaining)
if (
isinstance(value, pybamm.InputParameter) or _check_callable(value)
isinstance(value, pybamm.Symbol) and _check_input_params(value)
) and operator is None:
raise ValueError(
"Termination must include an operator when using InputParameter."
)
return operator, typ, val


def _check_callable(func):
def _check_input_params(value):
"""Check if self.value is a function of input parameters"""
if not callable(func):
return False
result = func(0)
init_val = result
if isinstance(init_val, pybamm.Symbol) or not np.isfinite(init_val):
return True
else:
return False
leaves = value.post_order(filter=lambda node: len(node.children) == 0)
contains_input_parameter = any(
isinstance(leaf, pybamm.InputParameter) for leaf in leaves
)
return contains_input_parameter
28 changes: 5 additions & 23 deletions tests/unit/test_experiments/test_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,30 +243,12 @@ def test_current_step_raises_error_without_operator_with_input_parameters(self):
pybamm.step.current(pybamm.InputParameter("I_app"), termination="2.5 V")

def test_value_function_with_input_parameter(self):
def f(t):
return pybamm.Symbol("I_app") * (t + 1)

step = pybamm.step.current(
pybamm.InputParameter("I_app"), termination="< 2.5 V"
)
step.value = f
I_coeff = pybamm.InputParameter("I_coeff")
t = pybamm.t
expr = I_coeff * t
step = pybamm.step.current(expr, termination="< 2.5V")

direction = step.value_based_charge_or_discharge()

assert direction is None, (
"Expected direction to be None when step.value is a function of an input parameter."
"Expected direction to be None when the expression depends on an InputParameter."
)

def test_value_function_without_input_parameter(self):
def f(t):
return t + 1

step = pybamm.step.current(
pybamm.InputParameter("I_app"), termination="< 2.5 V"
)
step.value = f
with pytest.raises(
ValueError,
match="Cannot determine charge or discharge direction for a function",
):
step.value_based_charge_or_discharge()
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def test_run_experiment_drive_cycle(self):
(
pybamm.step.current(drive_cycle, temperature="35oC"),
pybamm.step.voltage(drive_cycle),
pybamm.step.power(drive_cycle, termination="3 V"),
pybamm.step.power(drive_cycle, termination="< 3 V"),
)
],
)
Expand Down

0 comments on commit 435ad80

Please sign in to comment.