Skip to content

Commit af94506

Browse files
committed
#1477 increase coverage
1 parent ea59d9f commit af94506

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

pybamm/solvers/idaklu_solver.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __init__(
5353
max_steps="deprecated",
5454
):
5555

56-
if idaklu_spec is None:
56+
if idaklu_spec is None: # pragma: no cover
5757
raise ImportError("KLU is not installed")
5858

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

146146
return atol

tests/unit/test_solvers/test_idaklu_solver.py

+42
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,31 @@ def test_set_atol(self):
130130
variable_tols = {"Porosity times concentration": 1e-3}
131131
solver.set_atol_by_variable(variable_tols, model)
132132

133+
model = pybamm.BaseModel()
134+
u = pybamm.Variable("u")
135+
model.rhs = {u: -0.1 * u}
136+
model.initial_conditions = {u: 1}
137+
t_eval = np.linspace(0, 3, 100)
138+
139+
disc = pybamm.Discretisation()
140+
disc.process_model(model)
141+
142+
# numpy array atol
143+
atol = np.zeros(1)
144+
solver = pybamm.IDAKLUSolver(root_method="lm", atol=atol)
145+
solver.solve(model, t_eval)
146+
147+
# list atol
148+
atol = [1]
149+
solver = pybamm.IDAKLUSolver(root_method="lm", atol=atol)
150+
solver.solve(model, t_eval)
151+
152+
# wrong size (should fail)
153+
atol = [1, 2]
154+
solver = pybamm.IDAKLUSolver(root_method="lm", atol=atol)
155+
with self.assertRaisesRegex(pybamm.SolverError, 'Absolute tolerances'):
156+
solver.solve(model, t_eval)
157+
133158
def test_failures(self):
134159
# this test implements a python version of the ida Roberts
135160
# example provided in sundials
@@ -149,6 +174,23 @@ def test_failures(self):
149174
with self.assertRaisesRegex(pybamm.SolverError, "KLU requires the Jacobian"):
150175
solver.solve(model, t_eval)
151176

177+
model = pybamm.BaseModel()
178+
u = pybamm.Variable("u")
179+
model.rhs = {u: -0.1 * u}
180+
model.initial_conditions = {u: 1}
181+
182+
disc = pybamm.Discretisation()
183+
disc.process_model(model)
184+
185+
solver = pybamm.IDAKLUSolver(root_method="lm")
186+
187+
# will give solver error
188+
t_eval = np.linspace(0, -3, 100)
189+
with self.assertRaisesRegex(
190+
pybamm.SolverError, 't_eval must increase monotonically'
191+
):
192+
solver.solve(model, t_eval)
193+
152194
def test_dae_solver_algebraic_model(self):
153195
model = pybamm.BaseModel()
154196
var = pybamm.Variable("var")

0 commit comments

Comments
 (0)