Skip to content

Commit

Permalink
#2217 add rest of iterative solvers
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjrobins committed Sep 12, 2022
1 parent 7134ecb commit 2c1163b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
32 changes: 32 additions & 0 deletions pybamm/solvers/c_solvers/idaklu/casadi_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,38 @@ CasadiSolver::CasadiSolver(np_array atol_np, double rel_tol,
LS = SUNLinSol_SPBCGS(yy, precon_type, options.linsol_max_iterations);
#endif
}
else if (options.linear_solver == "SUNLinSol_SPFGMR")
{
DEBUG("\tsetting SUNLinSol_SPFGMR_linear solver");
#if SUNDIALS_VERSION_MAJOR >= 6
LS = SUNLinSol_SPFGMR(yy, precon_type, options.linsol_max_iterations,
sunctx);
#else
LS = SUNLinSol_SPFGMR(yy, precon_type, options.linsol_max_iterations);
#endif
}
else if (options.linear_solver == "SUNLinSol_SPGMR")
{
DEBUG("\tsetting SUNLinSol_SPGMR solver");
#if SUNDIALS_VERSION_MAJOR >= 6
LS = SUNLinSol_SPGMR(yy, precon_type, options.linsol_max_iterations,
sunctx);
#else
LS = SUNLinSol_SPGMR(yy, precon_type, options.linsol_max_iterations);
#endif
}
else if (options.linear_solver == "SUNLinSol_SPTFQMR")
{
DEBUG("\tsetting SUNLinSol_SPGMR solver");
#if SUNDIALS_VERSION_MAJOR >= 6
LS = SUNLinSol_SPTFQMR(yy, precon_type, options.linsol_max_iterations,
sunctx);
#else
LS = SUNLinSol_SPTFQMR(yy, precon_type, options.linsol_max_iterations);
#endif
}



IDASetLinearSolver(ida_mem, LS, J);

Expand Down
4 changes: 4 additions & 0 deletions pybamm/solvers/c_solvers/idaklu/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#include <sunlinsol/sunlinsol_dense.h> /* access to dense linear solver */
#include <sunlinsol/sunlinsol_lapackdense.h> /* access to lapack linear solver */
#include <sunlinsol/sunlinsol_spbcgs.h> /* access to spbcgs iterative linear solver */
#include <sunlinsol/sunlinsol_spfgmr.h>
#include <sunlinsol/sunlinsol_spgmr.h>
#include <sunlinsol/sunlinsol_sptfqmr.h>

#include <sunmatrix/sunmatrix_sparse.h> /* access to sparse SUNMatrix */
#include <sunmatrix/sunmatrix_dense.h> /* access to dense SUNMatrix */

Expand Down
5 changes: 4 additions & 1 deletion pybamm/solvers/c_solvers/idaklu/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ Options::Options(py::dict options)
else if (linear_solver == "SUNLinSol_KLU" && jacobian == "sparse")
{
}
else if (linear_solver == "SUNLinSol_SPBCGS" &&
else if ((linear_solver == "SUNLinSol_SPBCGS" ||
linear_solver == "SUNLinSol_SPFGMR" ||
linear_solver == "SUNLinSol_SPGMR" ||
linear_solver == "SUNLinSol_SPTFQMR") &&
(jacobian == "sparse" || jacobian == "matrix-free"))
{
using_iterative_solver = true;
Expand Down
3 changes: 2 additions & 1 deletion pybamm/solvers/idaklu_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class IDAKLUSolver(pybamm.BaseSolver):
linear_solver: "SUNLinSol_KLU", # name of sundials linear solver to use
# options are: "SUNLinSol_KLU",
# "SUNLinSol_Dense", "SUNLinSol_LapackDense"
# "SUNLinSol_SPBCGS"
# "SUNLinSol_SPBCGS", "SUNLinSol_SPFGMR",
# "SUNLinSol_SPGMR", "SUNLinSol_SPTFQMR",
preconditioner: "BBDP", # preconditioner for iterative solvers,
# can be "none", "BBDP"
linsol_max_iterations: 5, # for iterative linear solvers, max number of
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/test_solvers/test_idaklu_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,8 @@ def test_options(self):
for jacobian in ["none", "dense", "sparse", "matrix-free", "garbage"]:
for linear_solver in [
"SUNLinSol_SPBCGS", "SUNLinSol_Dense", "SUNLinSol_LapackDense",
"SUNLinSol_KLU", "garbage"
"SUNLinSol_KLU", "SUNLinSol_SPFGMR", "SUNLinSol_SPGMR",
"SUNLinSol_SPTFQMR", "garbage"
]:
for precon in ["none", "BBDP"]:
options = {
Expand All @@ -426,7 +427,7 @@ def test_options(self):
}
solver = pybamm.IDAKLUSolver(options=options)
soln = solver.solve(model, t_eval)
np.testing.assert_array_almost_equal(soln.y, soln_base.y)
np.testing.assert_array_almost_equal(soln.y, soln_base.y, 5)



Expand Down

0 comments on commit 2c1163b

Please sign in to comment.