Skip to content

Commit

Permalink
#1477 increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjrobins committed Aug 5, 2021
1 parent ea59d9f commit af94506
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pybamm/solvers/idaklu_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(
max_steps="deprecated",
):

if idaklu_spec is None:
if idaklu_spec is None: # pragma: no cover
raise ImportError("KLU is not installed")

super().__init__(
Expand Down Expand Up @@ -140,7 +140,7 @@ def _check_atol_type(self, atol, size):
if atol.size != size:
raise pybamm.SolverError(
"""Absolute tolerances must be either a scalar or a numpy arrray
of the same shape at y0"""
of the same shape as y0 ({})""".format(size)
)

return atol
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/test_solvers/test_idaklu_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,31 @@ def test_set_atol(self):
variable_tols = {"Porosity times concentration": 1e-3}
solver.set_atol_by_variable(variable_tols, model)

model = pybamm.BaseModel()
u = pybamm.Variable("u")
model.rhs = {u: -0.1 * u}
model.initial_conditions = {u: 1}
t_eval = np.linspace(0, 3, 100)

disc = pybamm.Discretisation()
disc.process_model(model)

# numpy array atol
atol = np.zeros(1)
solver = pybamm.IDAKLUSolver(root_method="lm", atol=atol)
solver.solve(model, t_eval)

# list atol
atol = [1]
solver = pybamm.IDAKLUSolver(root_method="lm", atol=atol)
solver.solve(model, t_eval)

# wrong size (should fail)
atol = [1, 2]
solver = pybamm.IDAKLUSolver(root_method="lm", atol=atol)
with self.assertRaisesRegex(pybamm.SolverError, 'Absolute tolerances'):
solver.solve(model, t_eval)

def test_failures(self):
# this test implements a python version of the ida Roberts
# example provided in sundials
Expand All @@ -149,6 +174,23 @@ def test_failures(self):
with self.assertRaisesRegex(pybamm.SolverError, "KLU requires the Jacobian"):
solver.solve(model, t_eval)

model = pybamm.BaseModel()
u = pybamm.Variable("u")
model.rhs = {u: -0.1 * u}
model.initial_conditions = {u: 1}

disc = pybamm.Discretisation()
disc.process_model(model)

solver = pybamm.IDAKLUSolver(root_method="lm")

# will give solver error
t_eval = np.linspace(0, -3, 100)
with self.assertRaisesRegex(
pybamm.SolverError, 't_eval must increase monotonically'
):
solver.solve(model, t_eval)

def test_dae_solver_algebraic_model(self):
model = pybamm.BaseModel()
var = pybamm.Variable("var")
Expand Down

0 comments on commit af94506

Please sign in to comment.