21 #ifdef DEAL_II_WITH_SUNDIALS
26 # ifdef DEAL_II_WITH_TRILINOS
30 # ifdef DEAL_II_WITH_PETSC
37 # include <arkode/arkode_impl.h>
38 # include <sundials/sundials_config.h>
54 template <
typename VectorType>
56 t_arkode_explicit_function(realtype tt,
61 ARKode<VectorType> &solver =
62 *
static_cast<ARKode<VectorType> *
>(user_data);
66 solver.reinit_vector(*src_yy);
69 solver.reinit_vector(*dst_yp);
73 int err = solver.explicit_function(tt, *src_yy, *dst_yp);
82 template <
typename VectorType>
84 t_arkode_implicit_function(realtype tt,
89 ARKode<VectorType> &solver =
90 *
static_cast<ARKode<VectorType> *
>(user_data);
94 solver.reinit_vector(*src_yy);
97 solver.reinit_vector(*dst_yp);
101 int err = solver.implicit_function(tt, *src_yy, *dst_yp);
110 template <
typename VectorType>
112 t_arkode_setup_jacobian(ARKodeMem arkode_mem,
116 booleantype *jcurPtr,
121 ARKode<VectorType> &solver =
122 *
static_cast<ARKode<VectorType> *
>(arkode_mem->ark_user_data);
126 solver.reinit_vector(*src_ypred);
129 solver.reinit_vector(*src_fpred);
131 copy(*src_ypred, ypred);
132 copy(*src_fpred, fpred);
135 bool jcurPtr_tmp =
false;
136 int err = solver.setup_jacobian(convfail,
138 arkode_mem->ark_gamma,
142 # if DEAL_II_SUNDIALS_VERSION_GTE(2, 0, 0)
143 *jcurPtr = jcurPtr_tmp ? SUNTRUE : SUNFALSE;
145 *jcurPtr = jcurPtr_tmp ? TRUE : FALSE;
153 template <
typename VectorType>
155 t_arkode_solve_jacobian(ARKodeMem arkode_mem,
163 ARKode<VectorType> &solver =
164 *
static_cast<ARKode<VectorType> *
>(arkode_mem->ark_user_data);
168 solver.reinit_vector(*src);
171 solver.reinit_vector(*src_ycur);
174 solver.reinit_vector(*src_fcur);
177 solver.reinit_vector(*dst);
180 copy(*src_ycur, ycur);
181 copy(*src_fcur, fcur);
183 int err = solver.solve_jacobian_system(arkode_mem->ark_tn,
184 arkode_mem->ark_gamma,
196 template <
typename VectorType>
198 t_arkode_setup_mass(ARKodeMem arkode_mem, N_Vector, N_Vector, N_Vector)
200 ARKode<VectorType> &solver =
201 *
static_cast<ARKode<VectorType> *
>(arkode_mem->ark_user_data);
202 int err = solver.setup_mass(arkode_mem->ark_tn);
208 template <
typename VectorType>
210 t_arkode_solve_mass(ARKodeMem arkode_mem,
219 ARKode<VectorType> &solver =
220 *
static_cast<ARKode<VectorType> *
>(arkode_mem->ark_user_data);
224 solver.reinit_vector(*src);
227 solver.reinit_vector(*dst);
231 int err = solver.solve_mass_system(*src, *dst);
238 template <
typename VectorType>
242 , arkode_mem(nullptr)
252 template <
typename VectorType>
256 ARKodeFree(&arkode_mem);
257 # ifdef DEAL_II_WITH_MPI
260 const int ierr = MPI_Comm_free(&communicator);
269 template <
typename VectorType>
273 unsigned int system_size = solution.size();
275 double t = data.initial_time;
276 double h = data.initial_step_size;
277 unsigned int step_number = 0;
285 # ifdef DEAL_II_WITH_MPI
288 const IndexSet is = solution.locally_owned_elements();
289 const std::size_t local_system_size = is.
n_elements();
291 yy = N_VNew_Parallel(communicator, local_system_size, system_size);
294 N_VNew_Parallel(communicator, local_system_size, system_size);
301 "Trying to use a serial code with a parallel vector."));
302 yy = N_VNew_Serial(system_size);
303 abs_tolls = N_VNew_Serial(system_size);
305 reset(data.initial_time, data.initial_step_size, solution);
307 double next_time = data.initial_time;
310 output_step(0, solution, 0);
312 while (t < data.final_time)
314 next_time += data.output_period;
316 status =
SundialsARKode(arkode_mem, next_time, yy, &t, ARK_NORMAL);
320 status = ARKodeGetLastStep(arkode_mem, &h);
325 while (solver_should_restart(t, solution))
326 reset(t, h, solution);
331 output_step(t, solution, step_number);
335 # ifdef DEAL_II_WITH_MPI
338 N_VDestroy_Parallel(yy);
339 N_VDestroy_Parallel(abs_tolls);
344 N_VDestroy_Serial(yy);
345 N_VDestroy_Serial(abs_tolls);
351 template <
typename VectorType>
354 const double current_time_step,
357 unsigned int system_size;
360 ARKodeFree(&arkode_mem);
362 arkode_mem = ARKodeCreate();
367 # ifdef DEAL_II_WITH_MPI
370 N_VDestroy_Parallel(yy);
371 N_VDestroy_Parallel(abs_tolls);
376 N_VDestroy_Serial(yy);
377 N_VDestroy_Serial(abs_tolls);
383 system_size = solution.size();
384 # ifdef DEAL_II_WITH_MPI
387 const IndexSet is = solution.locally_owned_elements();
388 const std::size_t local_system_size = is.
n_elements();
390 yy = N_VNew_Parallel(communicator, local_system_size, system_size);
393 N_VNew_Parallel(communicator, local_system_size, system_size);
398 yy = N_VNew_Serial(system_size);
399 abs_tolls = N_VNew_Serial(system_size);
404 Assert(explicit_function || implicit_function,
405 ExcFunctionNotProvided(
"explicit_function || implicit_function"));
409 explicit_function ? &t_arkode_explicit_function<VectorType> :
nullptr,
410 implicit_function ? &t_arkode_implicit_function<VectorType> :
nullptr,
415 if (get_local_tolerances)
417 copy(abs_tolls, get_local_tolerances());
419 ARKodeSVtolerances(arkode_mem, data.relative_tolerance, abs_tolls);
424 status = ARKodeSStolerances(arkode_mem,
425 data.relative_tolerance,
426 data.absolute_tolerance);
430 status = ARKodeSetInitStep(arkode_mem, current_time_step);
433 status = ARKodeSetUserData(arkode_mem,
this);
436 status = ARKodeSetStopTime(arkode_mem, data.final_time);
440 ARKodeSetMaxNonlinIters(arkode_mem, data.maximum_non_linear_iterations);
444 auto ARKode_mem =
static_cast<ARKodeMem
>(arkode_mem);
446 if (solve_jacobian_system)
448 status = ARKodeSetNewton(arkode_mem);
450 if (data.implicit_function_is_linear)
452 status = ARKodeSetLinear(
453 arkode_mem, data.implicit_function_is_time_independent ? 0 : 1);
458 ARKode_mem->ark_lsolve = t_arkode_solve_jacobian<VectorType>;
461 ARKode_mem->ark_lsetup = t_arkode_setup_jacobian<VectorType>;
462 # if DEAL_II_SUNDIALS_VERSION_LT(3, 0, 0)
463 ARKode_mem->ark_setupNonNull =
true;
470 ARKodeSetFixedPoint(arkode_mem, data.maximum_non_linear_iterations);
475 if (solve_mass_system)
477 ARKode_mem->ark_msolve = t_arkode_solve_mass<VectorType>;
481 ARKode_mem->ark_msetup = t_arkode_setup_mass<VectorType>;
482 # if DEAL_II_SUNDIALS_VERSION_LT(3, 0, 0)
483 ARKode_mem->ark_MassSetupNonNull =
true;
488 status = ARKodeSetOrder(arkode_mem, data.maximum_order);
492 template <
typename VectorType>
497 AssertThrow(
false, ExcFunctionNotProvided(
"reinit_vector"));
508 # ifdef DEAL_II_WITH_MPI
510 # ifdef DEAL_II_WITH_TRILINOS
513 # endif // DEAL_II_WITH_TRILINOS
515 # ifdef DEAL_II_WITH_PETSC
516 # ifndef PETSC_USE_COMPLEX
519 # endif // PETSC_USE_COMPLEX
520 # endif // DEAL_II_WITH_PETSC
522 # endif // DEAL_II_WITH_MPI