Skip to content

Commit c96decc

Browse files
committedFeb 20, 2020
#804 add root_tol as an option
1 parent d884e44 commit c96decc

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed
 

‎pybamm/solvers/base_solver.py

+15-11
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ def set_up(self, model, inputs=None):
131131
if self.ode_solver is True:
132132
self.root_method = None
133133
if (
134-
isinstance(self, pybamm.CasadiSolver)
135-
or self.root_method == "casadi"
134+
isinstance(self, pybamm.CasadiSolver) or self.root_method == "casadi"
136135
) and model.convert_to_format != "casadi":
137136
pybamm.logger.warning(
138137
f"Converting {model.name} to CasADi for solving with CasADi solver"
@@ -367,15 +366,20 @@ def calculate_consistent_state(self, model, time=0, y0_guess=None, inputs=None):
367366
"roots",
368367
"newton",
369368
dict(x=y_alg, p=u, g=alg_root),
370-
{"error_on_fail": False},
371-
)
372-
y0_alg = roots(y0_alg_guess, u_stacked).full().flatten()
373-
success = True
374-
message = None
375-
# Check final output
376-
fun = model.casadi_algebraic(
377-
time, casadi.vertcat(y0_diff, y0_alg), u_stacked
369+
{"abstol": self.root_tol},
378370
)
371+
try:
372+
y0_alg = roots(y0_alg_guess, u_stacked).full().flatten()
373+
success = True
374+
message = None
375+
# Check final output
376+
fun = model.casadi_algebraic(
377+
time, casadi.vertcat(y0_diff, y0_alg), u_stacked
378+
)
379+
except RuntimeError as err:
380+
success = False
381+
message = err.args[0]
382+
fun = None
379383
else:
380384
algebraic = model.algebraic_eval
381385
jac = model.jac_algebraic_eval
@@ -553,7 +557,7 @@ def solve(self, model, t_eval, external_variables=None, inputs=None):
553557
)
554558
else:
555559
t_eval_dimensionless = np.insert(
556-
t_eval_dimensionless, dindex, [dtime - eps, dtime + eps],
560+
t_eval_dimensionless, dindex, [dtime - eps, dtime + eps]
557561
)
558562
end_indices.append(len(t_eval_dimensionless))
559563

‎tests/unit/test_solvers/test_base_solver.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def algebraic_eval(self, t, y):
7272
init_cond = solver.calculate_consistent_state(model)
7373
np.testing.assert_array_equal(init_cond, -2)
7474
# with casadi
75-
solver_with_casadi = pybamm.BaseSolver(root_method="casadi")
75+
solver_with_casadi = pybamm.BaseSolver(root_method="casadi", root_tol=1e-12)
7676
model = ScalarModel()
7777
init_cond = solver_with_casadi.calculate_consistent_state(model)
7878
np.testing.assert_array_equal(init_cond, -2)
@@ -160,7 +160,7 @@ def algebraic_eval(self, t, y):
160160
solver = pybamm.BaseSolver(root_method="casadi")
161161
with self.assertRaisesRegex(
162162
pybamm.SolverError,
163-
"Could not find consistent initial conditions: solver terminated",
163+
"Could not find consistent initial conditions: .../casadi",
164164
):
165165
solver.calculate_consistent_state(Model())
166166

0 commit comments

Comments
 (0)