Skip to content

Commit

Permalink
#899 fix scipy events bug
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Mar 21, 2020
1 parent a702299 commit cccd781
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
17 changes: 10 additions & 7 deletions pybamm/solvers/scipy_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _integrate(self, model, t_eval, inputs=None):
various diagnostic messages.
"""
if model.rhs_eval.form == "casadi":
if model.convert_to_format == "casadi":
inputs = casadi.vertcat(*[x for x in inputs.values()])

extra_options = {"rtol": self.rtol, "atol": self.atol}
Expand All @@ -62,12 +62,15 @@ def _integrate(self, model, t_eval, inputs=None):

# make events terminal so that the solver stops when they are reached
if model.terminate_events_eval:
events = [
lambda t, y: event(t, y, inputs)
for event in model.terminate_events_eval
]
for event in events:
event.terminal = True

def event_wrapper(event):
def event_fn(t, y):
return event(t, y, inputs)

event_fn.terminal = True
return event_fn

events = [event_wrapper(event) for event in model.terminate_events_eval]
extra_options.update({"events": events})

sol = it.solve_ivp(
Expand Down
14 changes: 12 additions & 2 deletions tests/unit/test_solvers/test_scipy_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ def test_model_solver_with_event_python(self):
var = pybamm.Variable("var", domain=domain)
model.rhs = {var: -0.1 * var}
model.initial_conditions = {var: 1}
model.events = [pybamm.Event("var=0.5", pybamm.min(var - 0.5))]
# needs to work with multiple events (to avoid bug where only last event is
# used)
model.events = [
pybamm.Event("var=0.5", pybamm.min(var - 0.5)),
pybamm.Event("var=-0.5", pybamm.min(var + 0.5)),
]
# No need to set parameters; can use base discretisation (no spatial operators)

# create discretisation
Expand Down Expand Up @@ -236,7 +241,12 @@ def test_model_solver_with_event_with_casadi(self):
var = pybamm.Variable("var", domain=domain)
model.rhs = {var: -0.1 * var}
model.initial_conditions = {var: 1}
model.events = [pybamm.Event("var=0.5", pybamm.min(var - 0.5))]
# needs to work with multiple events (to avoid bug where only last event is
# used)
model.events = [
pybamm.Event("var=0.5", pybamm.min(var - 0.5)),
pybamm.Event("var=-0.5", pybamm.min(var + 0.5)),
]
# No need to set parameters; can use base discretisation (no spatial
# operators)

Expand Down

0 comments on commit cccd781

Please sign in to comment.