deal.II version GIT relicensing-6750-g1dc21bc838 2026-09-15 17:20: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{
98 template <typename VectorType = Vector<double>>
99 class ARKode
100 {
101 private:
114 template <class Fn>
116 {
117 std::function<Fn> *target;
118
119 FunctionProxy(std::function<Fn> *t = nullptr)
120 : target(t)
121 {}
122
124 operator=(std::function<Fn> *t)
125 {
126 target = t;
127 return *this;
128 }
129
131 operator=(std::function<Fn> f)
132 {
134 target,
136 "Unable to assign the function since the target is not set in the "
137 "proxy. Most probably, you explicitly provided a stepper to create "
138 "the ARKode object and then tried to bind a callback to the latter. "
139 "For such use cases, assign the user callbacks directly to the "
140 "stepper. Assignment of callbacks to the ARKode object is permitted "
141 "only if one of the constructors without a stepper parameter was "
142 "used for instantiation of the ARKode object."));
143
144 *target = std::move(f);
145 return *this;
146 }
147 };
148
149 public:
154 {
155 public:
189 "Use another constructor and ARKStepper::AdditionalData to initialize "
190 "with the ARKStepper relevant parameters instead.")
192 // Initial parameters
193 const double initial_time,
194 const double final_time,
195 const double initial_step_size,
196 const double output_period,
197 // Running parameters
198 const double minimum_step_size,
199 const unsigned int maximum_order = 5,
200 const unsigned int maximum_non_linear_iterations = 10,
201 const bool implicit_function_is_linear = false,
202 const bool implicit_function_is_time_independent = false,
203 const bool mass_is_time_independent = false,
204 const int anderson_acceleration_subspace = 3,
205 // Error parameters
206 const double absolute_tolerance = 1e-6,
207 const double relative_tolerance = 1e-5);
208
229 // Initial parameters
230 const double initial_time = 0.0,
231 const double final_time = 1.0,
232 const double initial_step_size = 1e-2,
233 const double output_period = 1e-1,
234 // Running parameters
235 const double minimum_step_size = 1e-6,
236 // Error parameters
237 const double absolute_tolerance = 1e-6,
238 const double relative_tolerance = 1e-5);
239
254 void
256
261
266
271
276
281
286
293 "Provide ARKStepper::AdditionalData::order instead.")
294 unsigned int maximum_order;
295
301
310 "instead.")
312
320 "instead.")
322
331 "instead.")
333
342 "instead.")
344
353 "instead.")
355 };
356
367
375 ARKode(const AdditionalData &data, const MPI_Comm mpi_comm);
376
389
399 const AdditionalData &data,
400 const MPI_Comm mpi_comm);
401
406
414 unsigned int
415 solve_ode(VectorType &solution);
416
443 unsigned int
444 solve_ode_incrementally(VectorType &solution,
445 const double intermediate_time,
446 const bool reset_solver = false);
447
462 void
463 reset(const double t, const double h, const VectorType &y);
464
481 void *
483
497 void(const double t, const VectorType &y, VectorType &explicit_f)>
499
512 FunctionProxy<void(const double t, const VectorType &y, VectorType &res)>
514
529 FunctionProxy<void(const double t, const VectorType &v, VectorType &Mv)>
531
547 FunctionProxy<void(const double t)> mass_times_setup;
548
562 FunctionProxy<void(const VectorType &v,
563 VectorType &Jv,
564 const double t,
565 const VectorType &y,
566 const VectorType &fy)>
568
581 void(const double t, const VectorType &y, const VectorType &fy)>
583
596 FunctionProxy<void(SundialsOperator<VectorType> &op,
597 SundialsPreconditioner<VectorType> &prec,
598 VectorType &x,
599 const VectorType &b,
600 double tol)>
602
614 FunctionProxy<void(SundialsOperator<VectorType> &op,
615 SundialsPreconditioner<VectorType> &prec,
616 VectorType &x,
617 const VectorType &b,
618 double tol)>
620
633 FunctionProxy<void(const double t,
634 const VectorType &y,
635 const VectorType &fy,
636 const VectorType &r,
637 VectorType &z,
638 const double gamma,
639 const double tol,
640 const int lr)>
642
654 FunctionProxy<void(const double t,
655 const VectorType &y,
656 const VectorType &fy,
657 const int jok,
658 int &jcur,
659 const double gamma)>
661
674 FunctionProxy<void(const double t,
675 const VectorType &r,
676 VectorType &z,
677 const double tol,
678 const int lr)>
680
693
708 std::function<void(const double t,
709 const VectorType &sol,
710 const unsigned int step_number)>
712
730 std::function<bool(const double t, VectorType &sol)> solver_should_restart;
731
738 std::function<VectorType &()> get_local_tolerances;
739
764 std::function<void(void *arkode_mem)> custom_setup;
765
766 private:
772 std::string,
773 << "Please provide an implementation for the function \""
774 << arg1 << "\"");
775
779 unsigned int
780 do_evolve_time(VectorType &solution,
781 ::DiscreteTime &time,
782 const bool do_reset);
783
789 void
791
796 void
798
803
809 std::unique_ptr<ARKStepper<VectorType>> ark_stepper_storage{nullptr};
810
815
816# if DEAL_II_SUNDIALS_VERSION_GTE(6, 0, 0)
821# endif
822
828
833
839 mutable std::exception_ptr pending_exception;
840
841# ifdef DEAL_II_WITH_PETSC
842# ifdef PETSC_USE_COMPLEX
843 static_assert(!std::is_same_v<VectorType, PETScWrappers::MPI::Vector>,
844 "Sundials does not support complex scalar types, "
845 "but PETSc is configured to use a complex scalar type!");
846
847 static_assert(!std::is_same_v<VectorType, PETScWrappers::MPI::BlockVector>,
848 "Sundials does not support complex scalar types, "
849 "but PETSc is configured to use a complex scalar type!");
850# endif // PETSC_USE_COMPLEX
851# endif // DEAL_II_WITH_PETSC
852 };
853
854
855
856 template <typename VectorType>
858 // Initial parameters
859 const double initial_time,
860 const double final_time,
861 const double initial_step_size,
862 const double output_period,
863 // Running parameters
864 const double minimum_step_size,
865 const unsigned int maximum_order,
866 const unsigned int maximum_non_linear_iterations,
867 const bool implicit_function_is_linear,
868 const bool implicit_function_is_time_independent,
869 const bool mass_is_time_independent,
870 const int anderson_acceleration_subspace,
871 // Error parameters
872 const double absolute_tolerance,
873 const double relative_tolerance)
874 : initial_time(initial_time)
875 , final_time(final_time)
876 , initial_step_size(initial_step_size)
877 , minimum_step_size(minimum_step_size)
878 , absolute_tolerance(absolute_tolerance)
879 , relative_tolerance(relative_tolerance)
880 , maximum_order(maximum_order)
881 , output_period(output_period)
882 , maximum_non_linear_iterations(maximum_non_linear_iterations)
883 , implicit_function_is_linear(implicit_function_is_linear)
884 , implicit_function_is_time_independent(
885 implicit_function_is_time_independent)
886 , mass_is_time_independent(mass_is_time_independent)
887 , anderson_acceleration_subspace(anderson_acceleration_subspace)
888 {}
889
890
891
892 template <typename VectorType>
894 // Initial parameters
895 const double initial_time,
896 const double final_time,
897 const double initial_step_size,
898 const double output_period,
899 // Running parameters
900 const double minimum_step_size,
901 // Error parameters
902 const double absolute_tolerance,
903 const double relative_tolerance)
904 : initial_time(initial_time)
905 , final_time(final_time)
906 , initial_step_size(initial_step_size)
907 , minimum_step_size(minimum_step_size)
908 , absolute_tolerance(absolute_tolerance)
909 , relative_tolerance(relative_tolerance)
910 , maximum_order(5)
911 , output_period(output_period)
912 , maximum_non_linear_iterations(10)
913 , implicit_function_is_linear(false)
914 , implicit_function_is_time_independent(false)
915 , mass_is_time_independent(false)
916 , anderson_acceleration_subspace(3)
917 {}
918
919
920
921 template <typename VectorType>
922 void
924 {
925 prm.add_parameter("Initial time", initial_time);
926 prm.add_parameter("Final time", final_time);
927 prm.add_parameter("Time interval between each output", output_period);
928 prm.enter_subsection("Running parameters");
929 prm.add_parameter("Initial step size", initial_step_size);
930 prm.add_parameter("Minimum step size", minimum_step_size);
931 prm.add_parameter("Maximum order of ARK", maximum_order);
932 prm.add_parameter("Maximum number of nonlinear iterations",
933 maximum_non_linear_iterations);
934 prm.add_parameter("Implicit function is linear",
935 implicit_function_is_linear);
936 prm.add_parameter("Implicit function is time independent",
937 implicit_function_is_time_independent);
938 prm.add_parameter("Mass is time independent", mass_is_time_independent);
939 prm.add_parameter("Anderson-acceleration subspace",
940 anderson_acceleration_subspace);
941 prm.leave_subsection();
942 prm.enter_subsection("Error control");
943 prm.add_parameter("Absolute error tolerance", absolute_tolerance);
944 prm.add_parameter("Relative error tolerance", relative_tolerance);
945 prm.leave_subsection();
946 }
947
948} // namespace SUNDIALS
949
950#endif
951
953
954
955#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:857
void add_parameters(ParameterHandler &prm)
Definition arkode.h:923
unsigned int maximum_non_linear_iterations
Definition arkode.h:311
std::exception_ptr pending_exception
Definition arkode.h:839
FunctionProxy< void(SundialsOperator< VectorType > &op, SundialsPreconditioner< VectorType > &prec, VectorType &x, const VectorType &b, double tol)> solve_linearized_system
Definition arkode.h:601
std::unique_ptr< ARKStepper< VectorType > > ark_stepper_storage
Definition arkode.h:809
void * get_arkode_memory() const
std::function< void(const double t, const VectorType &sol, const unsigned int step_number)> output_step
Definition arkode.h:711
std::function< VectorType &()> get_local_tolerances
Definition arkode.h:738
AdditionalData data
Definition arkode.h:802
ARKodeStepper< VectorType > & stepper
Definition arkode.h:814
FunctionProxy< void(const double t, const VectorType &v, VectorType &Mv)> mass_times_vector
Definition arkode.h:530
MPI_Comm mpi_communicator
Definition arkode.h:827
void reset(const double t, const double h, const VectorType &y)
SUNContext arkode_ctx
Definition arkode.h:820
void initialize_context()
FunctionProxy< void(const VectorType &v, VectorType &Jv, const double t, const VectorType &y, const VectorType &fy)> jacobian_times_vector
Definition arkode.h:567
FunctionProxy< void(const double t, const VectorType &y, VectorType &res)> implicit_function
Definition arkode.h:513
FunctionProxy< void(SundialsOperator< VectorType > &op, SundialsPreconditioner< VectorType > &prec, VectorType &x, const VectorType &b, double tol)> solve_mass
Definition arkode.h:619
std::function< bool(const double t, VectorType &sol)> solver_should_restart
Definition arkode.h:730
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:641
FunctionProxy< void(const double t)> mass_times_setup
Definition arkode.h:547
void set_functions_to_trigger_an_assert()
FunctionProxy< void(const double t, const VectorType &y, VectorType &explicit_f)> explicit_function
Definition arkode.h:498
FunctionProxy< void(const double t, const VectorType &y, const VectorType &fy)> jacobian_times_setup
Definition arkode.h:582
FunctionProxy< void(const double t, const VectorType &r, VectorType &z, const double tol, const int lr)> mass_preconditioner_solve
Definition arkode.h:679
FunctionProxy< void(const double t)> mass_preconditioner_setup
Definition arkode.h:692
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:832
std::function< void(void *arkode_mem)> custom_setup
Definition arkode.h:764
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:660
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_DEPRECATED_WITH_COMMENT(comment)
Definition config.h:294
#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:131
std::function< Fn > * target
Definition arkode.h:117
FunctionProxy(std::function< Fn > *t=nullptr)
Definition arkode.h:119
FunctionProxy & operator=(std::function< Fn > *t)
Definition arkode.h:124