version GIT relicensing-7278-g091132d5eb 2026-12-02 03:10: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_stepper.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) 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_stepper_h
15#define dealii_sundials_arkode_stepper_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
46
47# include <boost/signals2.hpp>
48
49# include <sundials/sundials_linearsolver.h>
50# include <sundials/sundials_math.h>
51# include <sundials/sundials_nonlinearsolver.h>
52
53# include <complex>
54# include <exception>
55# include <memory>
56# include <optional>
57# include <string>
58
59#endif // DEAL_II_WITH_SUNDIALS
60
62
63#ifdef DEAL_II_WITH_SUNDIALS
64
65namespace SUNDIALS
66{
70 template <typename VectorType>
71 class ARKode;
72
83 template <typename VectorType>
85 {
86 public:
91 friend class ARKode<VectorType>;
92
97 virtual ~ARKodeStepper() = default;
98
115 virtual void *
116 get_arkode_memory() const = 0;
117
118 private:
127 void
128 attach(std::exception_ptr &pending_exception
130 ,
131 const internal::SharedSUNContext &context
132# endif
133 )
134 {
135 Assert(pending_exception_ptr == nullptr,
136 ExcMessage("This stepper is already attached to an ARKode "
137 "object."));
138 pending_exception_ptr = &pending_exception;
139# if DEAL_II_SUNDIALS_VERSION_GTE(6, 0, 0)
140 sundials_context = context;
141# endif
142 }
143
152 virtual void
153 reinit(double t0, const VectorType &y0) = 0;
154
165 virtual void
166 resize(double t0, const VectorType &y0, double hscale) = 0;
167
168 protected:
174 template <typename Stepper>
176 {
177 Stepper *stepper = nullptr;
178 std::exception_ptr *pending_exception = nullptr;
179 };
180
188 using ARKodeMemoryPtr = std::unique_ptr<void, void (*)(void *)>;
189
197 {
198 Assert(pending_exception_ptr != nullptr,
199 ExcMessage("This stepper is not attached to an ARKode object."));
201# if DEAL_II_SUNDIALS_VERSION_GTE(6, 0, 0)
202 ,
204# endif
205 };
206 }
207
208 private:
212 std::exception_ptr *pending_exception_ptr = nullptr;
213
214# if DEAL_II_SUNDIALS_VERSION_GTE(6, 0, 0)
221# endif
222 };
223
463 template <typename VectorType = Vector<double>>
464 class ARKStepper : public ARKodeStepper<VectorType>
465 {
466 public:
471 {
472 public:
518 explicit AdditionalData(
519 const unsigned int order = 0,
520 const unsigned int maximum_non_linear_iterations = 10,
521 const bool implicit_function_is_linear = false,
522 const bool implicit_function_is_time_independent = false,
523 const bool mass_is_time_independent = false,
524 const int anderson_acceleration_subspace = 3,
525 const std::string &implicit_butcher_table = "",
526 const std::string &explicit_butcher_table = "");
527
542 void
544
553 unsigned int order;
554
560
565
571
577
583
594
605 };
606
613
614 void *
615 get_arkode_memory() const override;
616
633 std::function<
634 void(const double t, const VectorType &y, VectorType &explicit_f)>
636
653 std::function<void(const double t, const VectorType &y, VectorType &res)>
655
674 std::function<void(const double t, const VectorType &v, VectorType &Mv)>
676
711 std::function<void(const double t)> mass_times_vector_setup;
712
739 std::function<void(const VectorType &v,
740 VectorType &Jv,
741 const double t,
742 const VectorType &y,
743 const VectorType &fy)>
745
781 std::function<
782 void(const double t, const VectorType &y, const VectorType &fy)>
784
812
836
868 std::function<void(const double t,
869 const VectorType &y,
870 const VectorType &fy,
871 const VectorType &r,
872 VectorType &z,
873 const double gamma,
874 const double tol,
875 const int lr)>
877
920 std::function<void(const double t,
921 const VectorType &y,
922 const VectorType &fy,
923 const int jok,
924 int &jcur,
925 const double gamma)>
927
955 std::function<void(const double t,
956 const VectorType &r,
957 VectorType &z,
958 const double tol,
959 const int lr)>
961
986 std::function<void(const double t)> mass_preconditioner_setup;
987
1013 std::function<void(void *arkode_mem)> custom_setup;
1014
1015 private:
1017 VectorType>::template CallbackContext<ARKStepper<VectorType>>;
1018
1020
1029 void
1030 reinit(double t0, const VectorType &y0) override;
1031
1042 void
1043 resize(double t0, const VectorType &y0, double hscale) override;
1044
1052 void
1053 setup_system_solver(const VectorType &solution);
1054
1063 void
1064 setup_mass_solver(const VectorType &solution);
1065
1070
1076 std::optional<internal::LinearSolverWrapper<VectorType>> linear_solver;
1077
1082 std::optional<internal::LinearSolverWrapper<VectorType>> mass_solver;
1083
1089 std::optional<internal::SundialsLinearSolver> default_linear_solver;
1090
1095 std::optional<internal::SundialsLinearSolver> default_mass_solver;
1096
1101 std::optional<internal::SundialsNonlinearSolver> fixed_point_solver;
1102
1109
1114 };
1115
1116
1117 template <typename VectorType>
1119 const unsigned int order,
1120 const unsigned int maximum_non_linear_iterations,
1121 const bool implicit_function_is_linear,
1122 const bool implicit_function_is_time_independent,
1123 const bool mass_is_time_independent,
1124 const int anderson_acceleration_subspace,
1125 const std::string &implicit_butcher_table,
1126 const std::string &explicit_butcher_table)
1127 : order(order)
1128 , maximum_non_linear_iterations(maximum_non_linear_iterations)
1129 , implicit_function_is_linear(implicit_function_is_linear)
1130 , implicit_function_is_time_independent(
1131 implicit_function_is_time_independent)
1132 , mass_is_time_independent(mass_is_time_independent)
1133 , anderson_acceleration_subspace(anderson_acceleration_subspace)
1134 , implicit_butcher_table(implicit_butcher_table)
1135 , explicit_butcher_table(explicit_butcher_table)
1136 {}
1137
1138
1139
1140 template <typename VectorType>
1141 void
1143 {
1144 prm.add_parameter("Integration accuracy order", order);
1145 prm.add_parameter("Maximum number of nonlinear iterations",
1146 maximum_non_linear_iterations);
1147 prm.add_parameter("Implicit function is linear",
1148 implicit_function_is_linear);
1149 prm.add_parameter("Implicit function is time independent",
1150 implicit_function_is_time_independent);
1151 prm.add_parameter("Mass is time independent", mass_is_time_independent);
1152 prm.add_parameter("Anderson-acceleration subspace",
1153 anderson_acceleration_subspace);
1154 prm.add_parameter("Implicit Butcher table", implicit_butcher_table);
1155 prm.add_parameter("Explicit Butcher table", explicit_butcher_table);
1156 }
1157
1158
1228 template <typename VectorType = Vector<double>>
1229 class ERKStepper : public ARKodeStepper<VectorType>
1230 {
1231 public:
1240 {
1241 public:
1254 AdditionalData(const unsigned int order = 0,
1255 const std::string &explicit_butcher_table = "");
1256
1271 void
1272 add_parameters(ParameterHandler &prm);
1273
1282 unsigned int order;
1283
1293 };
1294
1301
1302 void *
1303 get_arkode_memory() const override;
1304
1316 std::function<
1317 void(const double t, const VectorType &y, VectorType &explicit_f)>
1319
1345 std::function<void(void *arkode_mem)> custom_setup;
1346
1347 private:
1349 VectorType>::template CallbackContext<ERKStepper<VectorType>>;
1350
1352
1361 void
1362 reinit(double t0, const VectorType &y0) override;
1363
1372 void
1373 resize(double t0, const VectorType &y0, double hscale) override;
1374
1379
1384
1389 };
1390
1391
1392 template <typename VectorType>
1394 const unsigned int order,
1395 const std::string &explicit_butcher_table)
1396 : order(order)
1397 , explicit_butcher_table(explicit_butcher_table)
1398 {}
1399
1400
1401
1402 template <typename VectorType>
1403 void
1405 {
1406 prm.add_parameter("Integration accuracy order", order);
1407 prm.add_parameter("Explicit Butcher table", explicit_butcher_table);
1408 }
1409
1410
1411# if DEAL_II_SUNDIALS_VERSION_GTE(7, 2, 0)
1412
1462 template <typename VectorType = Vector<double>>
1463 class LSRKStepperSTS : public ARKodeStepper<VectorType>
1464 {
1465 public:
1470 {
1471 public:
1508 const std::string &method_name = "",
1509 const unsigned int dom_eig_frequency = numbers::invalid_unsigned_int,
1510 const unsigned int max_num_stages = 0,
1511 const unsigned int dom_eig_estimator_max_iters = 0,
1512 const double dom_eig_estimator_rel_tol = 0,
1513 const unsigned int dom_eig_estimator_num_warmups =
1515 : method_name(method_name)
1516 , dom_eig_frequency(dom_eig_frequency)
1517 , max_num_stages(max_num_stages)
1518 , dom_eig_estimator_max_iters(dom_eig_estimator_max_iters)
1519 , dom_eig_estimator_rel_tol(dom_eig_estimator_rel_tol)
1520 , dom_eig_estimator_num_warmups(dom_eig_estimator_num_warmups)
1521 {}
1522
1528 void
1529 add_parameters(ParameterHandler &prm);
1530
1535 std::string method_name;
1536
1545 unsigned int dom_eig_frequency;
1546
1551 unsigned int max_num_stages;
1552
1561
1570
1580 };
1581
1588
1589 void *
1590 get_arkode_memory() const override;
1591
1603 std::function<
1604 void(const double t, const VectorType &y, VectorType &explicit_f)>
1606
1634 std::function<std::complex<double>(const double t,
1635 const VectorType &y,
1636 const VectorType &f)>
1638
1653 std::function<void(void *arkode_mem)> custom_setup;
1654
1655 private:
1657 VectorType>::template CallbackContext<LSRKStepperSTS<VectorType>>;
1658
1660
1669 void
1670 reinit(double t0, const VectorType &y0) override;
1671
1682 void
1683 resize(double t0, const VectorType &y0, double hscale) override;
1684
1685# if DEAL_II_SUNDIALS_VERSION_GTE(7, 5, 0)
1691 void
1693# endif
1694
1699
1700# if DEAL_II_SUNDIALS_VERSION_GTE(7, 5, 0)
1709 std::unique_ptr<void, void (*)(void *)> dom_eig_estimator;
1710# endif
1711
1716
1721 };
1722
1723
1724 template <typename VectorType>
1725 void
1727 ParameterHandler &prm)
1728 {
1729 prm.add_parameter(
1730 "Method name",
1731 method_name,
1732 "STS method name passed to LSRKStepSetSTSMethodByName. "
1733 "If empty, SUNDIALS uses the default (ARKODE_LSRK_RKC_2).");
1734 prm.add_parameter(
1735 "Dominant eigenvalue recomputation frequency",
1736 dom_eig_frequency,
1737 "Number of successful steps after which the dominant eigenvalue "
1738 "estimate is recomputed. 0 means constant dominant eigenvalue "
1739 "(no change throughout the simulation). Any negative value leaves "
1740 "this option at the SUNDIALS default of 25.");
1741 prm.add_parameter("Maximum number of stages",
1742 max_num_stages,
1743 "Maximum number of polynomial stages per time step. "
1744 "0 means no limit.");
1745 prm.add_parameter(
1746 "Dominant eigenvalue estimator maximum iterations",
1747 dom_eig_estimator_max_iters,
1748 "Maximum number of power iterations performed by the built-in "
1749 "dominant-eigenvalue estimator. Only used when no dominant eigenvalue "
1750 "function is provided.");
1751 prm.add_parameter(
1752 "Dominant eigenvalue estimator relative tolerance",
1753 dom_eig_estimator_rel_tol,
1754 "Relative convergence tolerance of the power iteration used by the "
1755 "built-in dominant-eigenvalue estimator. Only used when no dominant "
1756 "eigenvalue function is provided.");
1757 prm.add_parameter(
1758 "Dominant eigenvalue estimator warmup iterations",
1759 dom_eig_estimator_num_warmups,
1760 "Number of preprocessing warmup iterations performed once when the "
1761 "built-in dominant-eigenvalue estimator is first initialized. Only used "
1762 "when no dominant eigenvalue function is provided.");
1763 }
1764
1765
1793 template <typename VectorType = Vector<double>>
1794 class LSRKStepperSSP : public ARKodeStepper<VectorType>
1795 {
1796 public:
1801 {
1802 public:
1815 AdditionalData(const std::string &method_name = "",
1816 const unsigned int num_stages = 0)
1817 : method_name(method_name)
1818 , num_stages(num_stages)
1819 {}
1820
1826 void
1827 add_parameters(ParameterHandler &prm);
1828
1833 std::string method_name;
1834
1840 unsigned int num_stages;
1841 };
1842
1849
1850 void *
1851 get_arkode_memory() const override;
1852
1864 std::function<
1865 void(const double t, const VectorType &y, VectorType &explicit_f)>
1867
1882 std::function<void(void *arkode_mem)> custom_setup;
1883
1884 private:
1886 VectorType>::template CallbackContext<LSRKStepperSSP<VectorType>>;
1887
1889
1898 void
1899 reinit(double t0, const VectorType &y0) override;
1900
1909 void
1910 resize(double t0, const VectorType &y0, double hscale) override;
1911
1916
1921
1926 };
1927
1928
1929 template <typename VectorType>
1930 void
1932 ParameterHandler &prm)
1933 {
1934 prm.add_parameter(
1935 "Method name",
1936 method_name,
1937 "SSP method name passed to LSRKStepSetSSPMethodByName. "
1938 "If empty, SUNDIALS uses the default (ARKODE_LSRK_SSP_10_4).");
1939 prm.add_parameter("Number of SSP stages",
1940 num_stages,
1941 "Number of stages for variable-stage SSP methods "
1942 "(ARKODE_LSRK_SSP_S_2, ARKODE_LSRK_SSP_S_3). "
1943 "0 means SUNDIALS default.");
1944 }
1945
1946# endif // DEAL_II_SUNDIALS_VERSION_GTE(7, 2, 0)
1947
1948} // namespace SUNDIALS
1949
1950#endif // DEAL_II_WITH_SUNDIALS
1951
1953
1954#endif
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 add_parameters(ParameterHandler &prm)
AdditionalData(const unsigned int order=0, 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 std::string &implicit_butcher_table="", const std::string &explicit_butcher_table="")
std::optional< internal::SundialsNonlinearSolver > fixed_point_solver
std::function< void(void *arkode_mem)> custom_setup
void reinit(double t0, const VectorType &y0) override
std::optional< internal::SundialsLinearSolver > default_mass_solver
std::function< void(const double t, const VectorType &y, VectorType &res)> implicit_function
std::function< void(const double t)> mass_times_vector_setup
void setup_system_solver(const VectorType &solution)
ARKStepper(const AdditionalData &data=AdditionalData())
CallbackContext callback_ctx
void setup_mass_solver(const VectorType &solution)
typename ARKodeStepper< VectorType >::template CallbackContext< ARKStepper< VectorType > > CallbackContext
void resize(double t0, const VectorType &y0, double hscale) override
std::optional< internal::LinearSolverWrapper< VectorType > > linear_solver
std::function< void(const double t)> mass_preconditioner_setup
std::function< void(const double t, const VectorType &y, VectorType &explicit_f)> explicit_function
std::function< void(const double t, const VectorType &v, VectorType &Mv)> mass_times_vector
std::function< void(const double t, const VectorType &y, const VectorType &fy)> jacobian_times_vector_setup
std::optional< internal::LinearSolverWrapper< VectorType > > mass_solver
ARKodeMemoryPtr arkode_mem
std::function< 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
std::function< void(const double t, const VectorType &r, VectorType &z, const double tol, const int lr)> mass_preconditioner_solve
typename ARKodeStepper< VectorType >::ARKodeMemoryPtr ARKodeMemoryPtr
LinearSolveFunction< VectorType > solve_linearized_system
std::optional< internal::SundialsLinearSolver > default_linear_solver
LinearSolveFunction< VectorType > solve_mass
std::function< void(const VectorType &v, VectorType &Jv, const double t, const VectorType &y, const VectorType &fy)> jacobian_times_vector
std::function< void(const double t, const VectorType &y, const VectorType &fy, const int jok, int &jcur, const double gamma)> jacobian_preconditioner_setup
void * get_arkode_memory() const override
virtual void reinit(double t0, const VectorType &y0)=0
internal::InvocationContext invocation_context() const
virtual ~ARKodeStepper()=default
std::exception_ptr * pending_exception_ptr
std::unique_ptr< void, void(*)(void *)> ARKodeMemoryPtr
internal::SharedSUNContext sundials_context
void attach(std::exception_ptr &pending_exception, const internal::SharedSUNContext &context)
virtual void * get_arkode_memory() const =0
virtual void resize(double t0, const VectorType &y0, double hscale)=0
AdditionalData(const unsigned int order=0, const std::string &explicit_butcher_table="")
void add_parameters(ParameterHandler &prm)
CallbackContext callback_ctx
ARKodeMemoryPtr arkode_mem
void * get_arkode_memory() const override
std::function< void(void *arkode_mem)> custom_setup
void reinit(double t0, const VectorType &y0) override
ERKStepper(const AdditionalData &data=AdditionalData())
typename ARKodeStepper< VectorType >::ARKodeMemoryPtr ARKodeMemoryPtr
typename ARKodeStepper< VectorType >::template CallbackContext< ERKStepper< VectorType > > CallbackContext
std::function< void(const double t, const VectorType &y, VectorType &explicit_f)> explicit_function
void resize(double t0, const VectorType &y0, double hscale) override
void add_parameters(ParameterHandler &prm)
AdditionalData(const std::string &method_name="", const unsigned int num_stages=0)
void reinit(double t0, const VectorType &y0) override
LSRKStepperSSP(const AdditionalData &data=AdditionalData())
std::function< void(const double t, const VectorType &y, VectorType &explicit_f)> explicit_function
void * get_arkode_memory() const override
typename ARKodeStepper< VectorType >::template CallbackContext< LSRKStepperSSP< VectorType > > CallbackContext
std::function< void(void *arkode_mem)> custom_setup
void resize(double t0, const VectorType &y0, double hscale) override
typename ARKodeStepper< VectorType >::ARKodeMemoryPtr ARKodeMemoryPtr
AdditionalData(const std::string &method_name="", const unsigned int dom_eig_frequency=numbers::invalid_unsigned_int, const unsigned int max_num_stages=0, const unsigned int dom_eig_estimator_max_iters=0, const double dom_eig_estimator_rel_tol=0, const unsigned int dom_eig_estimator_num_warmups=numbers::invalid_unsigned_int)
void add_parameters(ParameterHandler &prm)
std::function< void(const double t, const VectorType &y, VectorType &explicit_f)> explicit_function
typename ARKodeStepper< VectorType >::ARKodeMemoryPtr ARKodeMemoryPtr
void reinit(double t0, const VectorType &y0) override
void resize(double t0, const VectorType &y0, double hscale) override
LSRKStepperSTS(const AdditionalData &data=AdditionalData())
std::function< std::complex< double >(const double t, const VectorType &y, const VectorType &f)> dominant_eigenvalue_function
std::function< void(void *arkode_mem)> custom_setup
void * get_arkode_memory() const override
void attach_default_dom_eig_estimator(N_Vector y, SUNContext context)
typename ARKodeStepper< VectorType >::template CallbackContext< LSRKStepperSTS< VectorType > > CallbackContext
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_SUNDIALS_VERSION_GTE(major, minor, patch)
Definition config.h:532
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define Assert(cond, exc)
static ::ExceptionBase & ExcMessage(std::string arg1)
std::vector< index_type > data
Definition mpi.cc:734
std::function< void(SundialsOperator< VectorType > &op, SundialsPreconditioner< VectorType > &prec, VectorType &x, const VectorType &b, double tol)> LinearSolveFunction
constexpr unsigned int invalid_unsigned_int
Definition types.h:228