Skip to content

Commit

Permalink
#1082 codacy and coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Mar 27, 2021
1 parent a18a5ba commit da7c60c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# [Unreleased](https://github.com/pybamm-team/PyBaMM)

## Features

- Added "fast with events" mode for the CasADi solver, which solves a model and finds events more efficiently than "safe" mode. As of PR #1450 this feature is still being tested and "safe" mode remains the default ([#1450](https://github.com/pybamm-team/PyBaMM/pull/1450))

## Optimizations

- Improved how the CasADi solver's "safe" mode finds events ([#1450](https://github.com/pybamm-team/PyBaMM/pull/1450))

# [v0.4.0](https://github.com/pybamm-team/PyBaMM/tree/v0.4.0) - 2021-03-27

This release introduces:
Expand Down
7 changes: 3 additions & 4 deletions examples/scripts/SPMe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import pybamm
import numpy as np

# pybamm.set_logging_level("INFO")
pybamm.set_logging_level("INFO")

# load model
model = pybamm.lithium_ion.SPMe()
# model.convert_to_format = "python"
model.convert_to_format = "python"

# create geometry
geometry = model.default_geometry
Expand All @@ -28,8 +28,7 @@

# solve model for 1 hour
t_eval = np.linspace(0, 3600, 100)
solver = pybamm.ScipySolver()
solution = solver.solve(model, t_eval)
solution = model.default_solver.solve(model, t_eval)

# plot
plot = pybamm.QuickPlot(
Expand Down
18 changes: 11 additions & 7 deletions pybamm/solvers/base_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ def set_up(self, model, inputs=None, t_eval=None):
)

inputs = inputs or {}
inputs_stacked = casadi.vertcat(*[p for p in inputs.values()])

# Set model timescale
model.timescale_eval = model.timescale.evaluate(inputs=inputs)
Expand Down Expand Up @@ -380,7 +379,9 @@ def report(string):
# We only need to do this if the model is a DAE model
# see #1082
k = 20
init_sign = float(np.sign(event_eval(0, model.y0, inputs_stacked)))
init_sign = float(
np.sign(event.evaluate(0, model.y0.full(), inputs=inputs))
)
# We create a sigmoid for each event which will multiply the
# rhs. Doing * 2 - 1 ensures that when the event is crossed,
# the sigmoid is zero. Hence the rhs is zero and the solution
Expand All @@ -389,14 +390,16 @@ def report(string):
event_sigmoid = (
pybamm.sigmoid(0, init_sign * event.expression, k) * 2 - 1
)
event_casadi = process(event_sigmoid, "event", use_jacobian=False)[
0
]
event_casadi = process(
event_sigmoid, f"event_{n}", use_jacobian=False
)[0]
# use the actual casadi object as this will go into the rhs
casadi_terminate_events.append(event_casadi)
else:
# use the function call
event_eval = process(event.expression, "event", use_jacobian=False)[1]
event_eval = process(
event.expression, f"event_{n}", use_jacobian=False
)[1]
if event.event_type == pybamm.EventType.TERMINATION:
terminate_events_eval.append(event_eval)
elif event.event_type == pybamm.EventType.INTERPOLANT_EXTRAPOLATION:
Expand Down Expand Up @@ -1080,7 +1083,8 @@ def get_termination_reason(self, solution, events):
termination_event = min(final_event_values, key=final_event_values.get)

# Check that it's actually an event
if abs(final_event_values[termination_event]) > 0.1:
if abs(final_event_values[termination_event]) > 0.1: # pragma: no cover
# Hard to test this
raise pybamm.SolverError(
"Could not determine which event was triggered "
"(possibly due to NaNs)"
Expand Down

0 comments on commit da7c60c

Please sign in to comment.