Skip to content

Commit

Permalink
#846 changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Mar 9, 2020
2 parents 786fefc + 25b5035 commit 6c73878
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
8 changes: 3 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# [Unreleased](https://github.com/pybamm-team/PyBaMM)
## Features

- Add ambient temperature as a function of time ([#872](https://github.com/pybamm-team/PyBaMM/pull/872))

## Features
## Features

- Added `Minimum`, `Maximum` and `Sign` operators
- Added `Minimum`, `Maximum` and `Sign` operators ([#876](https://github.com/pybamm-team/PyBaMM/pull/876))
- Added `CasadiAlgebraicSolver` for solving algebraic systems with CasADi ([#868](https://github.com/pybamm-team/PyBaMM/pull/868))
- Add ambient temperature as a function of time ([#872](https://github.com/pybamm-team/PyBaMM/pull/872))

## Bug fixes

Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/DFN_ambient_temperature.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Example showing how to load and solve the DFN
# Example showing how to solve the DFN with a varying ambient temperature
#

import pybamm
Expand Down
7 changes: 5 additions & 2 deletions pybamm/solvers/base_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,11 @@ def solve(self, model, t_eval=None, external_variables=None, inputs=None):

# t_eval can only be None if the solver is an algebraic solver. In that case
# set it to 0
if self.algebraic_solver is True and t_eval is None:
t_eval = np.array([0])
if t_eval is None:
if self.algebraic_solver is True:
t_eval = np.array([0])
else:
raise ValueError("t_eval cannot be None")

# Make sure t_eval is monotonic
if (np.diff(t_eval) < 0).any():
Expand Down
8 changes: 6 additions & 2 deletions pybamm/solvers/casadi_algebraic_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ class CasadiAlgebraicSolver(pybamm.BaseSolver):
The tolerance for the solver (default is 1e-6).
"""

def __init__(self, method="lm", tol=1e-6):
def __init__(self, method="lm", tol=1e-6, **extra_options):
super().__init__()
self.tol = tol
self.name = "CasADi algebraic solver"
self.algebraic_solver = True
self.extra_options = extra_options
pybamm.citations.register("Andersson2019")

@property
Expand Down Expand Up @@ -62,7 +63,10 @@ def _integrate(self, model, t_eval, inputs=None):

# Set up rootfinder
roots = casadi.rootfinder(
"roots", "newton", dict(x=y_sym, p=t_u_sym, g=alg), {"abstol": self.tol}
"roots",
"newton",
dict(x=y_sym, p=t_u_sym, g=alg),
{**self.extra_options, "abstol": self.tol},
)
for idx, t in enumerate(t_eval):
# Evaluate algebraic with new t and previous y0, if it's already close
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_solvers/test_base_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ def test_step_or_solve_empty_model(self):
with self.assertRaisesRegex(pybamm.ModelError, "Cannot solve empty model"):
solver.solve(model, None)

def test_t_eval_none(self):
model = pybamm.BaseModel()
v = pybamm.Variable("v")
model.rhs = {v: 1}
model.initial_conditions = {v: 1}
disc = pybamm.Discretisation()
disc.process_model(model)

solver = pybamm.BaseSolver()
with self.assertRaisesRegex(ValueError, "t_eval cannot be None"):
solver.solve(model, None)

def test_nonmonotonic_teval(self):
solver = pybamm.BaseSolver(rtol=1e-2, atol=1e-4)
model = pybamm.BaseModel()
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_solvers/test_casadi_algebraic_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ def algebraic_eval(self, t, y):
pybamm.SolverError, "Could not find acceptable solution: .../casadi",
):
solver._integrate(model, np.array([0]), {})
solver = pybamm.CasadiAlgebraicSolver(error_on_fail=False)
with self.assertRaisesRegex(
pybamm.SolverError, "Could not find acceptable solution: solver terminated",
):
solver._integrate(model, np.array([0]), {})

def test_model_solver_with_time(self):
# Create model
Expand Down

0 comments on commit 6c73878

Please sign in to comment.