21 #ifdef DEAL_II_WITH_SUNDIALS
26 # ifdef DEAL_II_WITH_TRILINOS
30 # ifdef DEAL_II_WITH_PETSC
37 # include <sundials/sundials_config.h>
38 # if DEAL_II_SUNDIALS_VERSION_GTE(3, 0, 0)
39 # include <kinsol/kinsol_direct.h>
40 # include <sunlinsol/sunlinsol_dense.h>
41 # include <sunmatrix/sunmatrix_dense.h>
43 # include <kinsol/kinsol_dense.h>
57 template <
typename VectorType>
59 t_kinsol_function(N_Vector yy, N_Vector FF,
void *user_data)
61 KINSOL<VectorType> &solver =
62 *
static_cast<KINSOL<VectorType> *
>(user_data);
66 solver.reinit_vector(*src_yy);
69 solver.reinit_vector(*dst_FF);
75 err = solver.residual(*src_yy, *dst_FF);
76 else if (solver.iteration_function)
77 err = solver.iteration_function(*src_yy, *dst_FF);
88 template <
typename VectorType>
90 t_kinsol_setup_jacobian(KINMem kinsol_mem)
92 KINSOL<VectorType> &solver =
93 *
static_cast<KINSOL<VectorType> *
>(kinsol_mem->kin_user_data);
97 solver.reinit_vector(*src_ycur);
100 solver.reinit_vector(*src_fcur);
102 copy(*src_ycur, kinsol_mem->kin_uu);
103 copy(*src_fcur, kinsol_mem->kin_fval);
105 int err = solver.setup_jacobian(*src_ycur, *src_fcur);
111 template <
typename VectorType>
113 t_kinsol_solve_jacobian(KINMem kinsol_mem,
119 KINSOL<VectorType> &solver =
120 *
static_cast<KINSOL<VectorType> *
>(kinsol_mem->kin_user_data);
124 solver.reinit_vector(*src_ycur);
127 solver.reinit_vector(*src_fcur);
129 copy(*src_ycur, kinsol_mem->kin_uu);
130 copy(*src_fcur, kinsol_mem->kin_fval);
133 solver.reinit_vector(*src);
136 solver.reinit_vector(*dst);
140 int err = solver.solve_jacobian_system(*src_ycur, *src_fcur, *src, *dst);
143 *sJpnorm = N_VWL2Norm(
b, kinsol_mem->kin_fscale);
144 N_VProd(
b, kinsol_mem->kin_fscale,
b);
145 N_VProd(
b, kinsol_mem->kin_fscale,
b);
146 *sFdotJp = N_VDotProd(kinsol_mem->kin_fval,
b);
152 template <
typename VectorType>
156 , kinsol_mem(nullptr)
169 template <
typename VectorType>
173 KINFree(&kinsol_mem);
174 # ifdef DEAL_II_WITH_MPI
177 const int ierr = MPI_Comm_free(&communicator);
186 template <
typename VectorType>
190 unsigned int system_size = initial_guess_and_solution.size();
195 # ifdef DEAL_II_WITH_MPI
198 const IndexSet is = initial_guess_and_solution.locally_owned_elements();
199 const unsigned int local_system_size = is.
n_elements();
202 N_VNew_Parallel(communicator, local_system_size, system_size);
204 u_scale = N_VNew_Parallel(communicator, local_system_size, system_size);
205 N_VConst_Parallel(1.e0, u_scale);
207 f_scale = N_VNew_Parallel(communicator, local_system_size, system_size);
208 N_VConst_Parallel(1.e0, f_scale);
215 "Trying to use a serial code with a parallel vector."));
216 solution = N_VNew_Serial(system_size);
217 u_scale = N_VNew_Serial(system_size);
218 N_VConst_Serial(1.e0, u_scale);
219 f_scale = N_VNew_Serial(system_size);
220 N_VConst_Serial(1.e0, f_scale);
223 if (get_solution_scaling)
224 copy(u_scale, get_solution_scaling());
226 if (get_function_scaling)
227 copy(f_scale, get_function_scaling());
229 copy(solution, initial_guess_and_solution);
232 KINFree(&kinsol_mem);
234 kinsol_mem = KINCreate();
236 int status = KINInit(kinsol_mem, t_kinsol_function<VectorType>, solution);
240 status = KINSetUserData(kinsol_mem,
static_cast<void *
>(
this));
243 status = KINSetNumMaxIters(kinsol_mem, data.maximum_non_linear_iterations);
246 status = KINSetFuncNormTol(kinsol_mem, data.function_tolerance);
249 status = KINSetScaledStepTol(kinsol_mem, data.step_tolerance);
252 status = KINSetMaxSetupCalls(kinsol_mem, data.maximum_setup_calls);
255 status = KINSetNoInitSetup(kinsol_mem, data.no_init_setup);
258 status = KINSetMaxNewtonStep(kinsol_mem, data.maximum_newton_step);
261 status = KINSetMaxBetaFails(kinsol_mem, data.maximum_beta_failures);
264 status = KINSetMAA(kinsol_mem, data.anderson_subspace_size);
267 status = KINSetRelErrFunc(kinsol_mem, data.dq_relative_error);
270 # if DEAL_II_SUNDIALS_VERSION_GTE(3, 0, 0)
271 SUNMatrix J =
nullptr;
272 SUNLinearSolver LS =
nullptr;
275 if (solve_jacobian_system)
277 auto KIN_mem =
static_cast<KINMem
>(kinsol_mem);
278 KIN_mem->kin_lsolve = t_kinsol_solve_jacobian<VectorType>;
281 KIN_mem->kin_lsetup = t_kinsol_setup_jacobian<VectorType>;
282 # if DEAL_II_SUNDIALS_VERSION_LT(3, 0, 0)
283 KIN_mem->kin_setupNonNull =
true;
289 # if DEAL_II_SUNDIALS_VERSION_GTE(3, 0, 0)
290 J = SUNDenseMatrix(system_size, system_size);
291 LS = SUNDenseLinearSolver(u_scale, J);
292 status = KINDlsSetLinearSolver(kinsol_mem, LS, J);
294 status = KINDense(kinsol_mem, system_size);
299 if (data.strategy == AdditionalData::newton ||
300 data.strategy == AdditionalData::linesearch)
301 Assert(residual, ExcFunctionNotProvided(
"residual"));
303 if (data.strategy == AdditionalData::fixed_point ||
304 data.strategy == AdditionalData::picard)
305 Assert(iteration_function, ExcFunctionNotProvided(
"iteration_function"));
308 status = KINSol(kinsol_mem, solution, data.strategy, u_scale, f_scale);
311 copy(initial_guess_and_solution, solution);
314 # ifdef DEAL_II_WITH_MPI
317 N_VDestroy_Parallel(solution);
318 N_VDestroy_Parallel(u_scale);
319 N_VDestroy_Parallel(f_scale);
324 N_VDestroy_Serial(solution);
325 N_VDestroy_Serial(u_scale);
326 N_VDestroy_Serial(f_scale);
330 status = KINGetNumNonlinSolvIters(kinsol_mem, &nniters);
333 # if DEAL_II_SUNDIALS_VERSION_GTE(3, 0, 0)
337 KINFree(&kinsol_mem);
339 return static_cast<unsigned int>(nniters);
342 template <
typename VectorType>
347 AssertThrow(
false, ExcFunctionNotProvided(
"reinit_vector"));
354 # ifdef DEAL_II_WITH_MPI
356 # ifdef DEAL_II_WITH_TRILINOS
361 # ifdef DEAL_II_WITH_PETSC
362 # ifndef PETSC_USE_COMPLEX