17 #include <deal.II/base/config.h> 19 #include <deal.II/sundials/arkode.h> 21 #ifdef DEAL_II_WITH_SUNDIALS 23 # include <deal.II/base/utilities.h> 25 # include <deal.II/lac/block_vector.h> 26 # ifdef DEAL_II_WITH_TRILINOS 27 # include <deal.II/lac/trilinos_parallel_block_vector.h> 28 # include <deal.II/lac/trilinos_vector.h> 30 # ifdef DEAL_II_WITH_PETSC 31 # include <deal.II/lac/petsc_block_vector.h> 32 # include <deal.II/lac/petsc_vector.h> 34 # include <deal.II/base/utilities.h> 36 # include <deal.II/sundials/copy.h> 38 # include <arkode/arkode_impl.h> 39 # include <sundials/sundials_config.h> 45 const auto &SundialsARKode = ARKode;
47 DEAL_II_NAMESPACE_OPEN
55 template <
typename VectorType>
57 t_arkode_explicit_function(realtype tt,
62 ARKode<VectorType> &solver =
63 *
static_cast<ARKode<VectorType> *
>(user_data);
67 solver.reinit_vector(*src_yy);
70 solver.reinit_vector(*dst_yp);
74 int err = solver.explicit_function(tt, *src_yy, *dst_yp);
83 template <
typename VectorType>
85 t_arkode_implicit_function(realtype tt,
90 ARKode<VectorType> &solver =
91 *
static_cast<ARKode<VectorType> *
>(user_data);
95 solver.reinit_vector(*src_yy);
98 solver.reinit_vector(*dst_yp);
102 int err = solver.implicit_function(tt, *src_yy, *dst_yp);
111 template <
typename VectorType>
113 t_arkode_setup_jacobian(ARKodeMem arkode_mem,
117 booleantype *jcurPtr,
122 ARKode<VectorType> &solver =
123 *
static_cast<ARKode<VectorType> *
>(arkode_mem->ark_user_data);
127 solver.reinit_vector(*src_ypred);
130 solver.reinit_vector(*src_fpred);
132 copy(*src_ypred, ypred);
133 copy(*src_fpred, fpred);
136 bool jcurPtr_tmp =
false;
137 int err = solver.setup_jacobian(convfail,
139 arkode_mem->ark_gamma,
143 # if DEAL_II_SUNDIALS_VERSION_GTE(2, 0, 0) 144 *jcurPtr = jcurPtr_tmp ? SUNTRUE : SUNFALSE;
146 *jcurPtr = jcurPtr_tmp ? TRUE : FALSE;
154 template <
typename VectorType>
156 t_arkode_solve_jacobian(ARKodeMem arkode_mem,
158 #
if DEAL_II_SUNDIALS_VERSION_LT(3, 0, 0)
164 ARKode<VectorType> &solver =
165 *
static_cast<ARKode<VectorType> *
>(arkode_mem->ark_user_data);
169 solver.reinit_vector(*src);
172 solver.reinit_vector(*src_ycur);
175 solver.reinit_vector(*src_fcur);
178 solver.reinit_vector(*dst);
181 copy(*src_ycur, ycur);
182 copy(*src_fcur, fcur);
184 int err = solver.solve_jacobian_system(arkode_mem->ark_tn,
185 arkode_mem->ark_gamma,
197 template <
typename VectorType>
199 t_arkode_setup_mass(ARKodeMem arkode_mem, N_Vector, N_Vector, N_Vector)
201 ARKode<VectorType> &solver =
202 *
static_cast<ARKode<VectorType> *
>(arkode_mem->ark_user_data);
203 int err = solver.setup_mass(arkode_mem->ark_tn);
209 template <
typename VectorType>
211 t_arkode_solve_mass(ARKodeMem arkode_mem,
212 #
if DEAL_II_SUNDIALS_VERSION_LT(3, 0, 0)
220 ARKode<VectorType> &solver =
221 *
static_cast<ARKode<VectorType> *
>(arkode_mem->ark_user_data);
225 solver.reinit_vector(*src);
228 solver.reinit_vector(*dst);
232 int err = solver.solve_mass_system(*src, *dst);
239 template <
typename VectorType>
241 const MPI_Comm mpi_comm)
243 , arkode_mem(nullptr)
248 Utilities::MPI::duplicate_communicator(mpi_comm))
253 template <
typename VectorType>
257 ARKodeFree(&arkode_mem);
258 # ifdef DEAL_II_WITH_MPI 261 const int ierr = MPI_Comm_free(&communicator);
270 template <
typename VectorType>
274 unsigned int system_size = solution.size();
276 double t = data.initial_time;
277 double h = data.initial_step_size;
278 unsigned int step_number = 0;
286 # ifdef DEAL_II_WITH_MPI 289 const IndexSet is = solution.locally_owned_elements();
290 const std::size_t local_system_size = is.
n_elements();
292 yy = N_VNew_Parallel(communicator, local_system_size, system_size);
295 N_VNew_Parallel(communicator, local_system_size, system_size);
302 "Trying to use a serial code with a parallel vector."));
303 yy = N_VNew_Serial(system_size);
304 abs_tolls = N_VNew_Serial(system_size);
306 reset(data.initial_time, data.initial_step_size, solution);
308 double next_time = data.initial_time;
311 output_step(0, solution, 0);
313 while (t < data.final_time)
315 next_time += data.output_period;
317 status = SundialsARKode(arkode_mem, next_time, yy, &t, ARK_NORMAL);
319 AssertARKode(status);
321 status = ARKodeGetLastStep(arkode_mem, &h);
322 AssertARKode(status);
326 while (solver_should_restart(t, solution))
327 reset(t, h, solution);
332 output_step(t, solution, step_number);
336 # ifdef DEAL_II_WITH_MPI 339 N_VDestroy_Parallel(yy);
340 N_VDestroy_Parallel(abs_tolls);
345 N_VDestroy_Serial(yy);
346 N_VDestroy_Serial(abs_tolls);
352 template <
typename VectorType>
355 const double current_time_step,
356 const VectorType &solution)
358 unsigned int system_size;
361 ARKodeFree(&arkode_mem);
363 arkode_mem = ARKodeCreate();
368 # ifdef DEAL_II_WITH_MPI 371 N_VDestroy_Parallel(yy);
372 N_VDestroy_Parallel(abs_tolls);
377 N_VDestroy_Serial(yy);
378 N_VDestroy_Serial(abs_tolls);
384 system_size = solution.size();
385 # ifdef DEAL_II_WITH_MPI 388 const IndexSet is = solution.locally_owned_elements();
389 const std::size_t local_system_size = is.
n_elements();
391 yy = N_VNew_Parallel(communicator, local_system_size, system_size);
394 N_VNew_Parallel(communicator, local_system_size, system_size);
399 yy = N_VNew_Serial(system_size);
400 abs_tolls = N_VNew_Serial(system_size);
405 Assert(explicit_function || implicit_function,
406 ExcFunctionNotProvided(
"explicit_function || implicit_function"));
410 explicit_function ? &t_arkode_explicit_function<VectorType> :
nullptr,
411 implicit_function ? &t_arkode_implicit_function<VectorType> :
nullptr,
414 AssertARKode(status);
416 if (get_local_tolerances)
418 copy(abs_tolls, get_local_tolerances());
420 ARKodeSVtolerances(arkode_mem, data.relative_tolerance, abs_tolls);
421 AssertARKode(status);
425 status = ARKodeSStolerances(arkode_mem,
426 data.relative_tolerance,
427 data.absolute_tolerance);
428 AssertARKode(status);
431 status = ARKodeSetInitStep(arkode_mem, current_time_step);
432 AssertARKode(status);
434 status = ARKodeSetUserData(arkode_mem,
this);
435 AssertARKode(status);
437 status = ARKodeSetStopTime(arkode_mem, data.final_time);
438 AssertARKode(status);
441 ARKodeSetMaxNonlinIters(arkode_mem, data.maximum_non_linear_iterations);
442 AssertARKode(status);
445 auto ARKode_mem =
static_cast<ARKodeMem
>(arkode_mem);
447 if (solve_jacobian_system)
449 status = ARKodeSetNewton(arkode_mem);
450 AssertARKode(status);
451 if (data.implicit_function_is_linear)
453 status = ARKodeSetLinear(
454 arkode_mem, data.implicit_function_is_time_independent ? 0 : 1);
455 AssertARKode(status);
459 ARKode_mem->ark_lsolve = t_arkode_solve_jacobian<VectorType>;
462 ARKode_mem->ark_lsetup = t_arkode_setup_jacobian<VectorType>;
463 # if DEAL_II_SUNDIALS_VERSION_LT(3, 0, 0) 464 ARKode_mem->ark_setupNonNull =
true;
471 ARKodeSetFixedPoint(arkode_mem, data.maximum_non_linear_iterations);
472 AssertARKode(status);
476 if (solve_mass_system)
478 ARKode_mem->ark_msolve = t_arkode_solve_mass<VectorType>;
482 ARKode_mem->ark_msetup = t_arkode_setup_mass<VectorType>;
483 # if DEAL_II_SUNDIALS_VERSION_LT(3, 0, 0) 484 ARKode_mem->ark_MassSetupNonNull =
true;
489 status = ARKodeSetOrder(arkode_mem, data.maximum_order);
490 AssertARKode(status);
493 template <
typename VectorType>
497 reinit_vector = [](VectorType &) {
498 AssertThrow(
false, ExcFunctionNotProvided(
"reinit_vector"));
501 solver_should_restart = [](
const double, VectorType &) ->
bool {
509 # ifdef DEAL_II_WITH_MPI 511 # ifdef DEAL_II_WITH_TRILINOS 514 # endif // DEAL_II_WITH_TRILINOS 516 # ifdef DEAL_II_WITH_PETSC 517 # ifndef PETSC_USE_COMPLEX 520 # endif // PETSC_USE_COMPLEX 521 # endif // DEAL_II_WITH_PETSC 523 # endif // DEAL_II_WITH_MPI 527 DEAL_II_NAMESPACE_CLOSE
void reset(const double t, const double h, const VectorType &y)
#define AssertNothrow(cond, exc)
#define AssertThrow(cond, exc)
#define Assert(cond, exc)
unsigned int solve_ode(VectorType &solution)
void set_functions_to_trigger_an_assert()
ARKode(const AdditionalData &data=AdditionalData(), const MPI_Comm mpi_comm=MPI_COMM_WORLD)
size_type n_elements() const
static ::ExceptionBase & ExcInternalError()