22#ifdef DEAL_II_WITH_SUNDIALS
30# ifdef DEAL_II_WITH_TRILINOS
34# ifdef DEAL_II_WITH_PETSC
43# include <sundials/sundials_config.h>
45# include <kinsol/kinsol_direct.h>
46# include <sunlinsol/sunlinsol_dense.h>
47# include <sunmatrix/sunmatrix_dense.h>
56 template <
typename VectorType>
59 const unsigned int maximum_non_linear_iterations,
60 const double function_tolerance,
61 const double step_tolerance,
62 const bool no_init_setup,
63 const unsigned int maximum_setup_calls,
64 const double maximum_newton_step,
65 const double dq_relative_error,
66 const unsigned int maximum_beta_failures,
67 const unsigned int anderson_subspace_size)
69 , maximum_non_linear_iterations(maximum_non_linear_iterations)
70 , function_tolerance(function_tolerance)
71 , step_tolerance(step_tolerance)
72 , no_init_setup(no_init_setup)
73 , maximum_setup_calls(maximum_setup_calls)
74 , maximum_newton_step(maximum_newton_step)
75 , dq_relative_error(dq_relative_error)
76 , maximum_beta_failures(maximum_beta_failures)
77 , anderson_subspace_size(anderson_subspace_size)
82 template <
typename VectorType>
86 static std::string strategy_str(
"newton");
89 "Choose among newton|linesearch|fixed_point|picard",
91 "newton|linesearch|fixed_point|picard"));
92 prm.
add_action(
"Solution strategy", [&](
const std::string &value) {
93 if (value ==
"newton")
95 else if (value ==
"linesearch")
96 strategy = linesearch;
97 else if (value ==
"fixed_point")
98 strategy = fixed_point;
99 else if (value ==
"picard")
105 maximum_non_linear_iterations);
106 prm.
add_parameter(
"Function norm stopping tolerance", function_tolerance);
107 prm.
add_parameter(
"Scaled step stopping tolerance", step_tolerance);
112 maximum_setup_calls);
113 prm.
add_parameter(
"Maximum allowable scaled length of the Newton step",
114 maximum_newton_step);
115 prm.
add_parameter(
"Relative error for different quotient computation",
120 prm.
add_parameter(
"Maximum number of beta-condition failures",
121 maximum_beta_failures);
127 anderson_subspace_size);
133 template <
typename VectorType>
140 template <
typename VectorType>
144 , mpi_communicator(mpi_comm)
145 , kinsol_mem(nullptr)
147 , kinsol_ctx(nullptr)
149 , pending_exception(nullptr)
153# if DEAL_II_SUNDIALS_VERSION_GTE(6, 0, 0)
170 template <
typename VectorType>
173 KINFree(&kinsol_mem);
174# if DEAL_II_SUNDIALS_VERSION_GTE(6, 0, 0)
175 const int status = SUNContext_Free(&kinsol_ctx);
185 template <
typename VectorType>
190 if (data.strategy == AdditionalData::fixed_point)
192 Assert(iteration_function,
198 Assert(solve_jacobian_system || solve_with_jacobian,
200 "solve_jacobian_system || solve_with_jacobian"));
207 KINFree(&kinsol_mem);
208# if DEAL_II_SUNDIALS_VERSION_GTE(6, 0, 0)
209 status = SUNContext_Free(&kinsol_ctx);
213# if DEAL_II_SUNDIALS_VERSION_LT(6, 0, 0)
214 kinsol_mem = KINCreate();
218 SUNContext_Create(mpi_communicator == MPI_COMM_SELF ?
nullptr :
223 kinsol_mem = KINCreate(kinsol_ctx);
226 status = KINSetUserData(kinsol_mem,
static_cast<void *
>(
this));
231 const auto make_compatible_nvector_view =
232# if DEAL_II_SUNDIALS_VERSION_LT(6, 0, 0)
233 [](
auto &v) {
return internal::make_nvector_view(v); };
235 [
this](
auto &v) {
return internal::make_nvector_view(v, kinsol_ctx); };
241 if (!get_function_scaling || !get_solution_scaling)
247 auto u_scale = make_compatible_nvector_view(
248 get_solution_scaling ? get_solution_scaling() : ones);
249 auto f_scale = make_compatible_nvector_view(
250 get_function_scaling ? get_function_scaling() : ones);
252 auto solution = make_compatible_nvector_view(initial_guess_and_solution);
255 status = KINSetNumMaxIters(kinsol_mem, data.maximum_non_linear_iterations);
259 status = KINSetMAA(kinsol_mem, data.anderson_subspace_size);
262 if (data.strategy == AdditionalData::fixed_point)
266 [](N_Vector yy, N_Vector FF,
void *user_data) ->
int {
270 auto src_yy = internal::unwrap_nvector_const<VectorType>(yy);
271 auto dst_FF = internal::unwrap_nvector<VectorType>(FF);
288 [](N_Vector yy, N_Vector FF,
void *user_data) ->
int {
292 auto src_yy = internal::unwrap_nvector_const<VectorType>(yy);
293 auto dst_FF = internal::unwrap_nvector<VectorType>(FF);
305 status = KINSetFuncNormTol(kinsol_mem, data.function_tolerance);
308 status = KINSetScaledStepTol(kinsol_mem, data.step_tolerance);
311 status = KINSetMaxSetupCalls(kinsol_mem, data.maximum_setup_calls);
314 status = KINSetNoInitSetup(kinsol_mem, data.no_init_setup);
317 status = KINSetMaxNewtonStep(kinsol_mem, data.maximum_newton_step);
320 status = KINSetMaxBetaFails(kinsol_mem, data.maximum_beta_failures);
323 status = KINSetRelErrFunc(kinsol_mem, data.dq_relative_error);
326 SUNMatrix J =
nullptr;
329 if (solve_jacobian_system ||
338# if DEAL_II_SUNDIALS_VERSION_LT(6, 0, 0)
339 LS = SUNLinSolNewEmpty();
341 LS = SUNLinSolNewEmpty(kinsol_ctx);
347 return SUNLINEARSOLVER_MATRIX_ITERATIVE;
353 LS->content =
nullptr;
369 realtype tol) ->
int {
373 const KINSOL<VectorType> &solver =
374 *
static_cast<const KINSOL<VectorType> *
>(LS->content);
378 if (solver.solve_with_jacobian)
380 auto src_b = internal::unwrap_nvector_const<VectorType>(b);
381 auto dst_x = internal::unwrap_nvector<VectorType>(x);
384 solver.solve_with_jacobian,
385 solver.pending_exception,
404 auto src_b = internal::unwrap_nvector_const<VectorType>(b);
405 auto dst_x = internal::unwrap_nvector<VectorType>(x);
413 solver.solve_jacobian_system,
414 solver.pending_exception,
429# if DEAL_II_SUNDIALS_VERSION_LT(6, 0, 0)
430 J = SUNMatNewEmpty();
432 J = SUNMatNewEmpty(kinsol_ctx);
436 J->ops->getid = [](SUNMatrix ) -> SUNMatrix_ID {
437 return SUNMATRIX_CUSTOM;
440 J->ops->destroy = [](SUNMatrix A) {
443 A->content =
nullptr;
455 status = KINSetLinearSolver(kinsol_mem, LS, J);
462 setup_jacobian = [](
const VectorType &,
const VectorType &) {
465 status = KINSetJacFn(
476 const KINSOL<VectorType> &solver =
477 *
static_cast<const KINSOL<VectorType> *
>(user_data);
479 auto ycur = internal::unwrap_nvector_const<VectorType>(u);
480 auto fcur = internal::unwrap_nvector<VectorType>(f);
484 solver.setup_jacobian, solver.pending_exception, *ycur, *fcur);
504 status = KINSol(kinsol_mem, solution, data.strategy, u_scale, f_scale);
506 ScopeExit upon_exit([
this, &J, &LS]()
mutable {
511 KINFree(&kinsol_mem);
514 if (pending_exception)
518 std::rethrow_exception(pending_exception);
522 pending_exception =
nullptr;
530 pending_exception =
nullptr;
537 status = KINGetNumNonlinSolvIters(kinsol_mem, &nniters);
540 return static_cast<unsigned int>(nniters);
545 template <
typename VectorType>
549 reinit_vector = [](VectorType &) {
560# ifdef DEAL_II_WITH_MPI
562# ifdef DEAL_II_WITH_TRILINOS
567# ifdef DEAL_II_WITH_PETSC
568# ifndef PETSC_USE_COMPLEX
void add_parameter(const std::string &entry, ParameterType ¶meter, const std::string &documentation="", const Patterns::PatternBase &pattern= *Patterns::Tools::Convert< ParameterType >::to_pattern(), const bool has_to_be_set=false)
void add_action(const std::string &entry, const std::function< void(const std::string &value)> &action, const bool execute_action=true)
void enter_subsection(const std::string &subsection)
AdditionalData(const SolutionStrategy &strategy=linesearch, const unsigned int maximum_non_linear_iterations=200, const double function_tolerance=0.0, const double step_tolerance=0.0, const bool no_init_setup=false, const unsigned int maximum_setup_calls=0, const double maximum_newton_step=0.0, const double dq_relative_error=0.0, const unsigned int maximum_beta_failures=0, const unsigned int anderson_subspace_size=0)
void add_parameters(ParameterHandler &prm)
void set_functions_to_trigger_an_assert()
MPI_Comm mpi_communicator
unsigned int solve(VectorType &initial_guess_and_solution)
std::function< void(const VectorType &src, VectorType &dst)> residual
std::function< void(const VectorType &src, VectorType &dst)> iteration_function
std::exception_ptr pending_exception
KINSOL(const AdditionalData &data=AdditionalData())
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_SUNDIALS_VERSION_GTE(major, minor, patch)
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcFunctionNotProvided(std::string arg1)
#define Assert(cond, exc)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & RecoverableUserCallbackError()
#define AssertThrow(cond, exc)
#define AssertKINSOL(code)
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
int call_and_possibly_capture_exception(const F &f, std::exception_ptr &eptr, Args &&...args)