19#ifdef DEAL_II_WITH_SUNDIALS
27# ifdef DEAL_II_TRILINOS_WITH_EPETRA
31# ifdef DEAL_II_TRILINOS_WITH_TPETRA
35# ifdef DEAL_II_WITH_PETSC
44# include <sundials/sundials_config.h>
46# if DEAL_II_SUNDIALS_VERSION_GTE(6, 0, 0)
47# include <kinsol/kinsol_ls.h>
49# include <kinsol/kinsol_direct.h>
51# include <kinsol/kinsol.h>
52# include <sunlinsol/sunlinsol_dense.h>
53# include <sunmatrix/sunmatrix_dense.h>
63#ifdef DEAL_II_WITH_SUNDIALS
67 template <
typename VectorType>
95 template <
typename VectorType>
99 static std::string strategy_str(
"newton");
102 "Choose among newton|linesearch|fixed_point|picard",
104 "newton|linesearch|fixed_point|picard"));
106 if (
value ==
"newton")
108 else if (
value ==
"linesearch")
110 else if (
value ==
"fixed_point")
112 else if (
value ==
"picard")
126 prm.
add_parameter(
"Maximum allowable scaled length of the Newton step",
128 prm.
add_parameter(
"Relative error for different quotient computation",
133 prm.
add_parameter(
"Maximum number of beta-condition failures",
142 static std::string orthogonalization_str(
"modified_gram_schmidt");
144 "Anderson QR orthogonalization",
145 orthogonalization_str,
146 "Choose among modified_gram_schmidt|inverse_compact|"
147 "classical_gram_schmidt|delayed_classical_gram_schmidt",
149 "modified_gram_schmidt|inverse_compact|classical_gram_schmidt|"
150 "delayed_classical_gram_schmidt"));
151 prm.
add_action(
"Anderson QR orthogonalization",
152 [&](
const std::string &
value) {
153 if (
value ==
"modified_gram_schmidt")
155 else if (
value ==
"inverse_compact")
157 else if (
value ==
"classical_gram_schmidt")
159 else if (
value ==
"delayed_classical_gram_schmidt")
170 template <
typename VectorType>
177 template <
typename VectorType>
195# if DEAL_II_SUNDIALS_VERSION_GTE(7, 0, 0)
202# elif DEAL_II_SUNDIALS_VERSION_GTE(6, 0, 0)
214 template <
typename VectorType>
218# if DEAL_II_SUNDIALS_VERSION_GTE(6, 0, 0)
219 const int status = SUNContext_Free(&
kinsol_ctx);
229 template <
typename VectorType>
251# if DEAL_II_SUNDIALS_VERSION_GTE(6, 0, 0)
257# if DEAL_II_SUNDIALS_VERSION_GTE(7, 0, 0)
266# elif DEAL_II_SUNDIALS_VERSION_GTE(6, 0, 0)
279 status = KINSetUserData(
kinsol_mem,
static_cast<void *
>(
this));
284 const auto make_compatible_nvector_view =
285# if DEAL_II_SUNDIALS_VERSION_LT(6, 0, 0)
300 auto u_scale = make_compatible_nvector_view(
302 auto f_scale = make_compatible_nvector_view(
305 auto solution = make_compatible_nvector_view(initial_guess_and_solution);
308 status = KINSetNumMaxIters(
kinsol_mem,
data.maximum_non_linear_iterations);
315# if DEAL_II_SUNDIALS_VERSION_GTE(6, 0, 0)
317 status = KINSetOrthAA(
kinsol_mem,
data.anderson_qr_orthogonalization);
321 data.anderson_qr_orthogonalization ==
324 "You specified an orthogonalization strategy for QR factorization "
325 "different from the default (modified Gram-Schmidt) but the installed "
326 "SUNDIALS version does not support this feature. Either choose the "
327 "default or install a SUNDIALS version >= 6.0.0."));
334 [](N_Vector yy, N_Vector FF,
void *user_data) ->
int {
356 [](N_Vector yy, N_Vector FF,
void *user_data) ->
int {
373 status = KINSetFuncNormTol(kinsol_mem, data.function_tolerance);
376 status = KINSetScaledStepTol(kinsol_mem, data.step_tolerance);
379 status = KINSetMaxSetupCalls(kinsol_mem, data.maximum_setup_calls);
382 status = KINSetNoInitSetup(kinsol_mem, data.no_init_setup);
385 status = KINSetMaxNewtonStep(kinsol_mem, data.maximum_newton_step);
388 status = KINSetMaxBetaFails(kinsol_mem, data.maximum_beta_failures);
391 status = KINSetRelErrFunc(kinsol_mem, data.dq_relative_error);
394 SUNMatrix J =
nullptr;
398 if (solve_with_jacobian)
405# if DEAL_II_SUNDIALS_VERSION_LT(6, 0, 0)
406 LS = SUNLinSolNewEmpty();
408 LS = SUNLinSolNewEmpty(kinsol_ctx);
414 return SUNLINEARSOLVER_MATRIX_ITERATIVE;
420 LS->content =
nullptr;
432 LS->ops->solve = [](SUNLinearSolver LS,
463# if DEAL_II_SUNDIALS_VERSION_LT(6, 0, 0)
464 J = SUNMatNewEmpty();
470 J->ops->getid = [](SUNMatrix ) -> SUNMatrix_ID {
471 return SUNMATRIX_CUSTOM;
474 J->ops->destroy = [](SUNMatrix A) {
477 A->content =
nullptr;
489 status = KINSetLinearSolver(kinsol_mem, LS, J);
499 status = KINSetJacFn(
518 solver.setup_jacobian, solver.pending_exception, *ycur, *fcur);
526 custom_setup(kinsol_mem);
544 status = KINSol(kinsol_mem, solution, data.strategy, u_scale, f_scale);
546 ScopeExit upon_exit([
this, &J, &LS]()
mutable {
551 KINFree(&kinsol_mem);
554 if (pending_exception)
558 std::rethrow_exception(pending_exception);
562 pending_exception =
nullptr;
570 pending_exception =
nullptr;
587 status = KINGetNumNonlinSolvIters(kinsol_mem, &nniters);
590 return static_cast<unsigned int>(nniters);
595 template <
typename VectorType>
610# ifdef DEAL_II_WITH_MPI
612# ifdef DEAL_II_TRILINOS_WITH_EPETRA
617# ifdef DEAL_II_TRILINOS_WITH_TPETRA
630# ifdef DEAL_II_WITH_PETSC
631# 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 enter_subsection(const std::string &subsection, const bool create_path_if_needed=true)
void add_action(const std::string &entry, const std::function< void(const std::string &value)> &action, const bool execute_action=true)
unsigned int maximum_setup_calls
double maximum_newton_step
SolutionStrategy strategy
unsigned int maximum_beta_failures
OrthogonalizationStrategy
@ delayed_classical_gram_schmidt
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, const OrthogonalizationStrategy anderson_qr_orthogonalization=modified_gram_schmidt)
OrthogonalizationStrategy anderson_qr_orthogonalization
void add_parameters(ParameterHandler &prm)
unsigned int maximum_non_linear_iterations
unsigned int anderson_subspace_size
double function_tolerance
std::function< Vector< double > &()> get_solution_scaling
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
std::function< void(const VectorType &rhs, VectorType &dst, const double tolerance)> solve_with_jacobian
std::function< Vector< double > &()> get_function_scaling
std::function< void(Vector< double > &)> reinit_vector
KINSOL(const AdditionalData &data=AdditionalData())
NVectorView< VectorType > make_nvector_view(VectorType &vector, SUNContext nvector_context)
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_SUNDIALS_VERSION_GTE(major, minor, patch)
#define DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_ASSERT_UNREACHABLE()
#define AssertKINSOL(code)
static ::ExceptionBase & ExcFunctionNotProvided(std::string arg1)
#define Assert(cond, exc)
static ::ExceptionBase & ExcKINSOLError(int arg1)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & RecoverableUserCallbackError()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
const bool IsBlockVector< VectorType >::value
Tpetra::Vector< Number, LO, GO, NodeType< MemorySpace > > VectorType
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)
VectorType * unwrap_nvector(N_Vector v)
const VectorType * unwrap_nvector_const(N_Vector v)