17 #include <deal.II/sundials/arkode.h> 18 #include <deal.II/base/config.h> 20 #ifdef DEAL_II_WITH_SUNDIALS 22 #include <deal.II/base/utilities.h> 23 #include <deal.II/lac/block_vector.h> 24 #ifdef DEAL_II_WITH_TRILINOS 25 #include <deal.II/lac/trilinos_parallel_block_vector.h> 26 #include <deal.II/lac/trilinos_vector.h> 28 #ifdef DEAL_II_WITH_PETSC 29 #include <deal.II/lac/petsc_parallel_block_vector.h> 30 #include <deal.II/lac/petsc_parallel_vector.h> 32 #include <deal.II/base/utilities.h> 33 #include <deal.II/sundials/copy.h> 35 #include <sundials/sundials_config.h> 39 #include <arkode/arkode_impl.h> 42 const auto &SundialsARKode = ARKode;
44 DEAL_II_NAMESPACE_OPEN
53 template<
typename VectorType>
54 int t_arkode_explicit_function(realtype tt,
59 ARKode<VectorType> &solver = *
static_cast<ARKode<VectorType> *
>(user_data);
63 solver.reinit_vector(*src_yy);
66 solver.reinit_vector(*dst_yp);
70 int err = solver.explicit_function(tt, *src_yy, *dst_yp);
79 template<
typename VectorType>
80 int t_arkode_implicit_function(realtype tt,
85 ARKode<VectorType> &solver = *
static_cast<ARKode<VectorType> *
>(user_data);
89 solver.reinit_vector(*src_yy);
92 solver.reinit_vector(*dst_yp);
96 int err = solver.implicit_function(tt, *src_yy, *dst_yp);
105 template<
typename VectorType>
106 int t_arkode_setup_jacobian(ARKodeMem arkode_mem,
110 booleantype *jcurPtr,
115 ARKode<VectorType> &solver = *
static_cast<ARKode<VectorType> *
>(arkode_mem->ark_user_data);
119 solver.reinit_vector(*src_ypred);
122 solver.reinit_vector(*src_fpred);
124 copy(*src_ypred, ypred);
125 copy(*src_fpred, fpred);
127 int err = solver.setup_jacobian(convfail,
129 arkode_mem->ark_gamma,
139 template<
typename VectorType>
140 int t_arkode_solve_jacobian(ARKodeMem arkode_mem,
142 #
if DEAL_II_SUNDIALS_VERSION_LT(3,0,0)
148 ARKode<VectorType> &solver = *
static_cast<ARKode<VectorType> *
>(arkode_mem->ark_user_data);
152 solver.reinit_vector(*src);
155 solver.reinit_vector(*src_ycur);
158 solver.reinit_vector(*src_fcur);
161 solver.reinit_vector(*dst);
164 copy(*src_ycur, ycur);
165 copy(*src_fcur, fcur);
167 int err = solver.solve_jacobian_system(arkode_mem->ark_tn,
168 arkode_mem->ark_gamma,
169 *src_ycur, *src_fcur,
178 template<
typename VectorType>
179 int t_arkode_setup_mass(ARKodeMem arkode_mem,
184 ARKode<VectorType> &solver = *
static_cast<ARKode<VectorType> *
>(arkode_mem->ark_user_data);
185 int err = solver.setup_mass(arkode_mem->ark_tn);
191 template<
typename VectorType>
192 int t_arkode_solve_mass(ARKodeMem arkode_mem,
193 #
if DEAL_II_SUNDIALS_VERSION_LT(3,0,0)
201 ARKode<VectorType> &solver = *
static_cast<ARKode<VectorType> *
>(arkode_mem->ark_user_data);
205 solver.reinit_vector(*src);
208 solver.reinit_vector(*dst);
212 int err = solver.solve_mass_system(*src,*dst);
219 template <
typename VectorType>
221 const MPI_Comm mpi_comm) :
228 Utilities::MPI::duplicate_communicator(mpi_comm))
233 template <
typename VectorType>
237 ARKodeFree(&arkode_mem);
238 #ifdef DEAL_II_WITH_MPI 241 const int ierr = MPI_Comm_free(&communicator);
250 template <
typename VectorType>
254 unsigned int system_size = solution.size();
256 double t = data.initial_time;
257 double h = data.initial_step_size;
258 unsigned int step_number = 0;
266 #ifdef DEAL_II_WITH_MPI 269 const IndexSet is = solution.locally_owned_elements();
270 const size_t local_system_size = is.
n_elements();
272 yy = N_VNew_Parallel(communicator,
276 abs_tolls = N_VNew_Parallel(communicator,
285 yy = N_VNew_Serial(system_size);
286 abs_tolls = N_VNew_Serial(system_size);
288 reset(data.initial_time,
289 data.initial_step_size,
292 double next_time = data.initial_time;
294 output_step( 0, solution, 0);
296 while (t<data.final_time)
299 next_time += data.output_period;
301 status = SundialsARKode(arkode_mem, next_time, yy, &t, ARK_NORMAL);
303 AssertARKode(status);
305 status = ARKodeGetLastStep(arkode_mem, &h);
306 AssertARKode(status);
310 while (solver_should_restart(t, solution))
311 reset(t, h, solution);
316 output_step(t, solution, step_number);
320 #ifdef DEAL_II_WITH_MPI 323 N_VDestroy_Parallel(yy);
324 N_VDestroy_Parallel(abs_tolls);
329 N_VDestroy_Serial(yy);
330 N_VDestroy_Serial(abs_tolls);
336 template <
typename VectorType>
338 const double ¤t_time_step,
339 const VectorType &solution)
342 unsigned int system_size;
345 ARKodeFree(&arkode_mem);
347 arkode_mem = ARKodeCreate();
352 #ifdef DEAL_II_WITH_MPI 355 N_VDestroy_Parallel(yy);
356 N_VDestroy_Parallel(abs_tolls);
361 N_VDestroy_Serial(yy);
362 N_VDestroy_Serial(abs_tolls);
368 system_size = solution.size();
369 #ifdef DEAL_II_WITH_MPI 372 const IndexSet is = solution.locally_owned_elements();
373 const size_t local_system_size = is.
n_elements();
375 yy = N_VNew_Parallel(communicator,
379 abs_tolls = N_VNew_Parallel(communicator,
387 yy = N_VNew_Serial(system_size);
388 abs_tolls = N_VNew_Serial(system_size);
393 Assert(explicit_function || implicit_function,
394 ExcFunctionNotProvided(
"explicit_function || implicit_function"));
396 status = ARKodeInit(arkode_mem,
397 explicit_function ? &t_arkode_explicit_function<VectorType> :
nullptr,
398 implicit_function ? &t_arkode_implicit_function<VectorType> :
nullptr,
400 AssertARKode(status);
402 if (get_local_tolerances)
404 copy(abs_tolls, get_local_tolerances());
405 status = ARKodeSVtolerances(arkode_mem, data.relative_tolerance, abs_tolls);
406 AssertARKode(status);
410 status = ARKodeSStolerances(arkode_mem, data.relative_tolerance, data.absolute_tolerance);
411 AssertARKode(status);
414 status = ARKodeSetInitStep(arkode_mem, current_time_step);
415 AssertARKode(status);
417 status = ARKodeSetUserData(arkode_mem, (
void *)
this);
418 AssertARKode(status);
420 status = ARKodeSetStopTime(arkode_mem, data.final_time);
421 AssertARKode(status);
423 status = ARKodeSetMaxNonlinIters(arkode_mem, data.maximum_non_linear_iterations);
424 AssertARKode(status);
427 ARKodeMem ARKode_mem = (ARKodeMem) arkode_mem;
429 if (solve_jacobian_system)
431 status = ARKodeSetNewton(arkode_mem);
432 AssertARKode(status);
433 if (data.implicit_function_is_linear)
435 status = ARKodeSetLinear(arkode_mem,
436 data.implicit_function_is_time_independent ? 0 : 1);
437 AssertARKode(status);
441 ARKode_mem->ark_lsolve = t_arkode_solve_jacobian<VectorType>;
444 ARKode_mem->ark_lsetup = t_arkode_setup_jacobian<VectorType>;
445 #if DEAL_II_SUNDIALS_VERSION_LT(3,0,0) 446 ARKode_mem->ark_setupNonNull =
true;
452 status = ARKodeSetFixedPoint(arkode_mem, data.maximum_non_linear_iterations);
453 AssertARKode(status);
457 if (solve_mass_system)
459 ARKode_mem->ark_msolve = t_arkode_solve_mass<VectorType>;
463 ARKode_mem->ark_msetup = t_arkode_setup_mass<VectorType>;
464 #if DEAL_II_SUNDIALS_VERSION_LT(3,0,0) 465 ARKode_mem->ark_MassSetupNonNull =
true;
470 status = ARKodeSetOrder(arkode_mem, data.maximum_order);
471 AssertARKode(status);
474 template<
typename VectorType>
478 reinit_vector = [](VectorType &)
480 AssertThrow(
false, ExcFunctionNotProvided(
"reinit_vector"));
483 solver_should_restart = [](
const double,
493 #ifdef DEAL_II_WITH_MPI 495 #ifdef DEAL_II_WITH_TRILINOS 498 #endif // DEAL_II_WITH_TRILINOS 500 #ifdef DEAL_II_WITH_PETSC 501 #ifndef PETSC_USE_COMPLEX 504 #endif // PETSC_USE_COMPLEX 505 #endif // DEAL_II_WITH_PETSC 507 #endif //DEAL_II_WITH_MPI 511 DEAL_II_NAMESPACE_CLOSE
#define AssertNothrow(cond, exc)
#define AssertThrow(cond, exc)
void reset(const double &t, const double &h, const VectorType &y)
#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()