Skip to content

Commit ec5d05f

Browse files
committed
#2217 fix test to detect exceptions
1 parent d7b203e commit ec5d05f

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

pybamm/solvers/c_solvers/idaklu/options.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ Options::Options(py::dict options)
3434
}
3535

3636
using_iterative_solver = false;
37-
if (linear_solver == "SUNLinSol_Dense" && jacobian == "dense")
37+
if (linear_solver == "SUNLinSol_Dense" && (jacobian == "dense" || jacobian == "none"))
3838
{
3939
}
40-
else if (linear_solver == "SUNLinSol_LapackDense" && jacobian == "dense")
40+
else if (linear_solver == "SUNLinSol_LapackDense" && (jacobian == "dense" || jacobian == "none"))
4141
{
4242
}
4343
else if (linear_solver == "SUNLinSol_KLU" && jacobian == "sparse")
@@ -54,25 +54,35 @@ Options::Options(py::dict options)
5454
else if (jacobian == "sparse")
5555
{
5656
throw std::domain_error(
57-
"Unknown linear solver or incompatible options. For a sparse jacobian "
57+
"Unknown linear solver or incompatible options: "
58+
"jacobian = \"" + jacobian + "\" linear solver = \"" + linear_solver +
59+
"\". For a sparse jacobian "
5860
"please use the SUNLinSol_KLU linear solver"
5961
);
6062
}
6163
else if (jacobian == "matrix-free")
6264
{
6365
throw std::domain_error(
64-
"Unknown linear solver or incompatible options. For a matrix-free jacobian "
65-
"please use one of the iterative linear solvers: \"SUNLinSol_SPBCGS\", "
66-
"\"SUNLinSol_SPFGMR\", \"SUNLinSol_SPGMR\", or \"SUNLinSol_SPTFQMR\"."
67-
);
66+
"Unknown linear solver or incompatible options. "
67+
"jacobian = \"" + jacobian + "\" linear solver = \"" + linear_solver +
68+
"\". For a matrix-free jacobian "
69+
"please use one of the iterative linear solvers: \"SUNLinSol_SPBCGS\", "
70+
"\"SUNLinSol_SPFGMR\", \"SUNLinSol_SPGMR\", or \"SUNLinSol_SPTFQMR\"."
71+
);
72+
}
73+
else if (jacobian == "none")
74+
{
75+
throw std::domain_error(
76+
"Unknown linear solver or incompatible options: "
77+
"jacobian = \"" + jacobian + "\" linear solver = \"" + linear_solver +
78+
"\". For no jacobian please use the SUNLinSol_Dense solver"
79+
);
6880
}
6981
else
7082
{
7183
throw std::domain_error(
72-
"Unknown linear solver \""s + linear_solver +
73-
"\", use one of \"SUNLinSol_KLU\", \"SUNLinSol_Dense\", "
74-
"\"SUNLinSol_LapackDense\", \"SUNLinSol_SPBCGS\", \"SUNLinSol_SPFGMR\", "
75-
"\"SUNLinSol_SPGMR\", or \"SUNLinSol_SPTFQMR\""
84+
"Unknown linear solver or incompatible options. "
85+
"jacobian = \"" + jacobian + "\" linear solver = \"" + linear_solver + "\""
7686
);
7787
}
7888

tests/unit/test_solvers/test_idaklu_solver.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,37 @@ def test_options(self):
495495
"preconditioner": precon,
496496
}
497497
solver = pybamm.IDAKLUSolver(options=options)
498-
soln = solver.solve(model, t_eval)
499-
np.testing.assert_array_almost_equal(soln.y, soln_base.y, 5)
498+
if (
499+
jacobian == "none" and (
500+
linear_solver == "SUNLinSol_Dense" or
501+
linear_solver == "SUNLinSol_LapackDense"
502+
) or
503+
jacobian == "dense" and (
504+
linear_solver == "SUNLinSol_Dense" or
505+
linear_solver == "SUNLinSol_LapackDense"
506+
) or
507+
jacobian == "sparse" and (
508+
linear_solver != "SUNLinSol_Dense" and
509+
linear_solver != "SUNLinSol_LapackDense" and
510+
linear_solver != "garbage"
511+
) or
512+
jacobian == "matrix-free" and (
513+
linear_solver != "SUNLinSol_KLU" and
514+
linear_solver != "SUNLinSol_Dense" and
515+
linear_solver != "SUNLinSol_LapackDense" and
516+
linear_solver != "garbage"
517+
)
518+
):
519+
works = True
520+
else:
521+
works = False
522+
523+
if works:
524+
soln = solver.solve(model, t_eval)
525+
np.testing.assert_array_almost_equal(soln.y, soln_base.y, 5)
526+
else:
527+
with self.assertRaises(ValueError):
528+
soln = solver.solve(model, t_eval)
500529

501530

502531
if __name__ == "__main__":

0 commit comments

Comments
 (0)