deal.II version GIT relicensing-7040-g409d9ea789 2026-11-06 04:40:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
arkode.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2017 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13
14#ifndef dealii_sundials_arkode_h
15#define dealii_sundials_arkode_h
16
17#include <deal.II/base/config.h>
18
19
20#ifdef DEAL_II_WITH_SUNDIALS
21
26
27# ifdef DEAL_II_WITH_PETSC
30# endif
31# include <deal.II/lac/vector.h>
33
34# include <arkode/arkode.h>
35# include <nvector/nvector_serial.h>
36# ifdef DEAL_II_WITH_MPI
37# include <nvector/nvector_parallel.h>
38# endif
40
45
46# include <boost/signals2.hpp>
47
48# include <sundials/sundials_linearsolver.h>
49# include <sundials/sundials_math.h>
50
51# include <exception>
52# include <memory>
53
54#endif
55
57
58#ifdef DEAL_II_WITH_SUNDIALS
62namespace SUNDIALS
63{
100 template <typename VectorType = Vector<double>>
101 class ARKode
102 {
103 private:
116 template <class Fn>
118 {
119 std::function<Fn> *target;
120
121 FunctionProxy(std::function<Fn> *t = nullptr)
122 : target(t)
123 {}
124
126 operator=(std::function<Fn> *t)
127 {
128 target = t;
129 return *this;
130 }
131
133 operator=(std::function<Fn> f)
134 {
136 target,
138 "Unable to assign the function since the target is not set in the "
139 "proxy. Most probably, you explicitly provided a stepper to create "
140 "the ARKode object and then tried to bind a callback to the latter. "
141 "For such use cases, assign the user callbacks directly to the "
142 "stepper. Assignment of callbacks to the ARKode object is permitted "
143 "only if one of the constructors without a stepper parameter was "
144 "used for instantiation of the ARKode object."));
145
146 *target = std::move(f);
147 return *this;
148 }
149 };
150
151 public:
156 {
157 public:
191 "Use another constructor and ARKStepper::AdditionalData to initialize "
192 "with the ARKStepper relevant parameters instead.")
194 // Initial parameters
195 const double initial_time,
196 const double final_time,
197 const double initial_step_size,
198 const double output_period,
199 // Running parameters
200 const double minimum_step_size,
201 const unsigned int maximum_order = 5,
202 const unsigned int maximum_non_linear_iterations = 10,
203 const bool implicit_function_is_linear = false,
204 const bool implicit_function_is_time_independent = false,
205 const bool mass_is_time_independent = false,
206 const int anderson_acceleration_subspace = 3,
207 // Error parameters
208 const double absolute_tolerance = 1e-6,
209 const double relative_tolerance = 1e-5);
210
231 // Initial parameters
232 const double initial_time = 0.0,
233 const double final_time = 1.0,
234 const double initial_step_size = 1e-2,
235 const double output_period = 1e-1,
236 // Running parameters
237 const double minimum_step_size = 1e-6,
238 // Error parameters
239 const double absolute_tolerance = 1e-6,
240 const double relative_tolerance = 1e-5);
241
256 void
258
263
268
273
278
283
288
295 "Provide ARKStepper::AdditionalData::order instead.")
296 unsigned int maximum_order;
297
303
312 "instead.")
314
322 "instead.")
324
333 "instead.")
335
344 "instead.")
346
355 "instead.")
357 };
358
369
377 ARKode(const AdditionalData &data, const MPI_Comm mpi_comm);
378
391
401 const AdditionalData &data,
402 const MPI_Comm mpi_comm);
403
408
416 unsigned int
417 solve_ode(VectorType &solution);
418
445 unsigned int
446 solve_ode_incrementally(VectorType &solution,
447 const double intermediate_time,
448 const bool reset_solver = false);
449
464 void
465 reset(const double t, const double h, const VectorType &y);
466
480 void
481 resize(const double t, const double hscale, const VectorType &y);
482
499 void *
501
515 void(const double t, const VectorType &y, VectorType &explicit_f)>
517
530 FunctionProxy<void(const double t, const VectorType &y, VectorType &res)>
532
547 FunctionProxy<void(const double t, const VectorType &v, VectorType &Mv)>
549
565 FunctionProxy<void(const double t)> mass_times_setup;
566
580 FunctionProxy<void(const VectorType &v,
581 VectorType &Jv,
582 const double t,
583 const VectorType &y,
584 const VectorType &fy)>
586
599 void(const double t, const VectorType &y, const VectorType &fy)>
601
614 FunctionProxy<void(SundialsOperator<VectorType> &op,
615 SundialsPreconditioner<VectorType> &prec,
616 VectorType &x,
617 const VectorType &b,
618 double tol)>
620
632 FunctionProxy<void(SundialsOperator<VectorType> &op,
633 SundialsPreconditioner<VectorType> &prec,
634 VectorType &x,
635 const VectorType &b,
636 double tol)>
638
651 FunctionProxy<void(const double t,
652 const VectorType &y,
653 const VectorType &fy,
654 const VectorType &r,
655 VectorType &z,
656 const double gamma,
657 const double tol,
658 const int lr)>
660
672 FunctionProxy<void(const double t,
673 const VectorType &y,
674 const VectorType &fy,
675 const int jok,
676 int &jcur,
677 const double gamma)>
679
692 FunctionProxy<void(const double t,
693 const VectorType &r,
694 VectorType &z,
695 const double tol,
696 const int lr)>
698
711
726 std::function<void(const double t,
727 const VectorType &sol,
728 const unsigned int step_number)>
730
748 std::function<bool(const double t, VectorType &sol)> solver_should_restart;
749
768 std::function<double(const double t, VectorType &sol)> solver_should_resize;
769
776 std::function<VectorType &()> get_local_tolerances;
777
802 std::function<void(void *arkode_mem)> custom_setup;
803
804 private:
810 std::string,
811 << "Please provide an implementation for the function \""
812 << arg1 << "\"");
813
817 unsigned int
818 do_evolve_time(VectorType &solution,
819 ::DiscreteTime &time,
820 const bool do_reset);
821
827 void
829
834 void
836
841 void
843
847 void
849
854
860 std::unique_ptr<ARKStepper<VectorType>> ark_stepper_storage{nullptr};
861
866
867# if DEAL_II_SUNDIALS_VERSION_GTE(6, 0, 0)
873# endif
874
880
885
891 mutable std::exception_ptr pending_exception;
892
893# ifdef DEAL_II_WITH_PETSC
894# ifdef PETSC_USE_COMPLEX
895 static_assert(!std::is_same_v<VectorType, PETScWrappers::MPI::Vector>,
896 "Sundials does not support complex scalar types, "
897 "but PETSc is configured to use a complex scalar type!");
898
899 static_assert(!std::is_same_v<VectorType, PETScWrappers::MPI::BlockVector>,
900 "Sundials does not support complex scalar types, "
901 "but PETSc is configured to use a complex scalar type!");
902# endif // PETSC_USE_COMPLEX
903# endif // DEAL_II_WITH_PETSC
904 };
905
906
907
908 template <typename VectorType>
910 // Initial parameters
911 const double initial_time,
912 const double final_time,
913 const double initial_step_size,
914 const double output_period,
915 // Running parameters
916 const double minimum_step_size,
917 const unsigned int maximum_order,
918 const unsigned int maximum_non_linear_iterations,
919 const bool implicit_function_is_linear,
920 const bool implicit_function_is_time_independent,
921 const bool mass_is_time_independent,
922 const int anderson_acceleration_subspace,
923 // Error parameters
924 const double absolute_tolerance,
925 const double relative_tolerance)
926 : initial_time(initial_time)
927 , final_time(final_time)
928 , initial_step_size(initial_step_size)
929 , minimum_step_size(minimum_step_size)
930 , absolute_tolerance(absolute_tolerance)
931 , relative_tolerance(relative_tolerance)
932 , maximum_order(maximum_order)
933 , output_period(output_period)
934 , maximum_non_linear_iterations(maximum_non_linear_iterations)
935 , implicit_function_is_linear(implicit_function_is_linear)
936 , implicit_function_is_time_independent(
937 implicit_function_is_time_independent)
938 , mass_is_time_independent(mass_is_time_independent)
939 , anderson_acceleration_subspace(anderson_acceleration_subspace)
940 {}
941
942
943
944 template <typename VectorType>
946 // Initial parameters
947 const double initial_time,
948 const double final_time,
949 const double initial_step_size,
950 const double output_period,
951 // Running parameters
952 const double minimum_step_size,
953 // Error parameters
954 const double absolute_tolerance,
955 const double relative_tolerance)
956 : initial_time(initial_time)
957 , final_time(final_time)
958 , initial_step_size(initial_step_size)
959 , minimum_step_size(minimum_step_size)
960 , absolute_tolerance(absolute_tolerance)
961 , relative_tolerance(relative_tolerance)
962 , maximum_order(5)
963 , output_period(output_period)
964 , maximum_non_linear_iterations(10)
965 , implicit_function_is_linear(false)
966 , implicit_function_is_time_independent(false)
967 , mass_is_time_independent(false)
968 , anderson_acceleration_subspace(3)
969 {}
970
971
972
973 template <typename VectorType>
974 void
976 {
977 prm.add_parameter("Initial time", initial_time);
978 prm.add_parameter("Final time", final_time);
979 prm.add_parameter("Time interval between each output", output_period);
980 prm.enter_subsection("Running parameters");
981 prm.add_parameter("Initial step size", initial_step_size);
982 prm.add_parameter("Minimum step size", minimum_step_size);
983 prm.add_parameter("Maximum order of ARK", maximum_order);
984 prm.add_parameter("Maximum number of nonlinear iterations",
985 maximum_non_linear_iterations);
986 prm.add_parameter("Implicit function is linear",
987 implicit_function_is_linear);
988 prm.add_parameter("Implicit function is time independent",
989 implicit_function_is_time_independent);
990 prm.add_parameter("Mass is time independent", mass_is_time_independent);
991 prm.add_parameter("Anderson-acceleration subspace",
992 anderson_acceleration_subspace);
993 prm.leave_subsection();
994 prm.enter_subsection("Error control");
995 prm.add_parameter("Absolute error tolerance", absolute_tolerance);
996 prm.add_parameter("Relative error tolerance", relative_tolerance);
997 prm.leave_subsection();
998 }
999
1000} // namespace SUNDIALS
1001
1002#endif
1003
1005
1006
1007#endif
*  *  for(const auto &cell :triangulation.active_cell_iterators())
void add_parameter(const std::string &entry, ParameterType &parameter, 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)
AdditionalData(const double initial_time, const double final_time, const double initial_step_size, const double output_period, const double minimum_step_size, const unsigned int maximum_order=5, const unsigned int maximum_non_linear_iterations=10, const bool implicit_function_is_linear=false, const bool implicit_function_is_time_independent=false, const bool mass_is_time_independent=false, const int anderson_acceleration_subspace=3, const double absolute_tolerance=1e-6, const double relative_tolerance=1e-5)
Definition arkode.h:909
void add_parameters(ParameterHandler &prm)
Definition arkode.h:975
unsigned int maximum_non_linear_iterations
Definition arkode.h:313
std::exception_ptr pending_exception
Definition arkode.h:891
FunctionProxy< void(SundialsOperator< VectorType > &op, SundialsPreconditioner< VectorType > &prec, VectorType &x, const VectorType &b, double tol)> solve_linearized_system
Definition arkode.h:619
std::unique_ptr< ARKStepper< VectorType > > ark_stepper_storage
Definition arkode.h:860
void * get_arkode_memory() const
std::function< void(const double t, const VectorType &sol, const unsigned int step_number)> output_step
Definition arkode.h:729
std::function< VectorType &()> get_local_tolerances
Definition arkode.h:776
AdditionalData data
Definition arkode.h:853
internal::SharedSUNContext arkode_ctx
Definition arkode.h:872
ARKodeStepper< VectorType > & stepper
Definition arkode.h:865
void resize(const double t, const double hscale, const VectorType &y)
FunctionProxy< void(const double t, const VectorType &v, VectorType &Mv)> mass_times_vector
Definition arkode.h:548
MPI_Comm mpi_communicator
Definition arkode.h:879
void reset(const double t, const double h, const VectorType &y)
void initialize_context()
std::function< double(const double t, VectorType &sol)> solver_should_resize
Definition arkode.h:768
FunctionProxy< void(const VectorType &v, VectorType &Jv, const double t, const VectorType &y, const VectorType &fy)> jacobian_times_vector
Definition arkode.h:585
FunctionProxy< void(const double t, const VectorType &y, VectorType &res)> implicit_function
Definition arkode.h:531
FunctionProxy< void(SundialsOperator< VectorType > &op, SundialsPreconditioner< VectorType > &prec, VectorType &x, const VectorType &b, double tol)> solve_mass
Definition arkode.h:637
std::function< bool(const double t, VectorType &sol)> solver_should_restart
Definition arkode.h:748
unsigned int do_evolve_time(VectorType &solution, ::DiscreteTime &time, const bool do_reset)
FunctionProxy< void(const double t, const VectorType &y, const VectorType &fy, const VectorType &r, VectorType &z, const double gamma, const double tol, const int lr)> jacobian_preconditioner_solve
Definition arkode.h:659
FunctionProxy< void(const double t)> mass_times_setup
Definition arkode.h:565
void set_functions_to_trigger_an_assert()
FunctionProxy< void(const double t, const VectorType &y, VectorType &explicit_f)> explicit_function
Definition arkode.h:516
FunctionProxy< void(const double t, const VectorType &y, const VectorType &fy)> jacobian_times_setup
Definition arkode.h:600
FunctionProxy< void(const double t, const VectorType &r, VectorType &z, const double tol, const int lr)> mass_preconditioner_solve
Definition arkode.h:697
FunctionProxy< void(const double t)> mass_preconditioner_setup
Definition arkode.h:710
unsigned int solve_ode(VectorType &solution)
unsigned int solve_ode_incrementally(VectorType &solution, const double intermediate_time, const bool reset_solver=false)
double last_end_time
Definition arkode.h:884
std::function< void(void *arkode_mem)> custom_setup
Definition arkode.h:802
FunctionProxy< void(const double t, const VectorType &y, const VectorType &fy, const int jok, int &jcur, const double gamma)> jacobian_preconditioner_setup
Definition arkode.h:678
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_DEPRECATED_WITH_COMMENT(comment)
Definition config.h:295
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
static ::ExceptionBase & ExcFunctionNotProvided(std::string arg1)
#define DeclException1(Exception1, type1, outsequence)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
STL namespace.
FunctionProxy & operator=(std::function< Fn > f)
Definition arkode.h:133
std::function< Fn > * target
Definition arkode.h:119
FunctionProxy(std::function< Fn > *t=nullptr)
Definition arkode.h:121
FunctionProxy & operator=(std::function< Fn > *t)
Definition arkode.h:126