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
ida.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_ida_h
15#define dealii_sundials_ida_h
16
17#include <deal.II/base/config.h>
18
19#ifdef DEAL_II_WITH_SUNDIALS
24
25# ifdef DEAL_II_WITH_PETSC
28# endif
29# include <deal.II/lac/vector.h>
31
32# ifdef DEAL_II_SUNDIALS_WITH_IDAS
33# include <idas/idas.h>
34# else
35# include <ida/ida.h>
36# endif
37
40
41# include <boost/signals2.hpp>
42
43# include <nvector/nvector_serial.h>
44# include <sundials/sundials_config.h>
45# include <sundials/sundials_math.h>
46
47# include <cmath>
48# include <limits>
49# include <memory>
50
51#endif // DEAL_II_WITH_SUNDIALS
52
54
55#ifdef DEAL_II_WITH_SUNDIALS
56namespace SUNDIALS
57{
490 template <typename VectorType = Vector<double>>
491 class IDA
492 {
493 public:
500 {
501 public:
512 {
516 none = 0,
517
526
530 use_y_dot = 2
531 };
532
591 AdditionalData( // Initial parameters
592 const double initial_time = 0.0,
593 const double final_time = 1.0,
594 const double initial_step_size = 1e-2,
595 const double output_period = 1e-1,
596 // Running parameters
597 const double minimum_step_size = 1e-15,
598 const unsigned int maximum_order = 5,
599 const unsigned int maximum_non_linear_iterations = 10,
600 const double ls_norm_factor = 0,
601 // Error parameters
602 const double absolute_tolerance = 1e-6,
603 const double relative_tolerance = 1e-5,
604 const bool ignore_algebraic_terms_for_errors = true,
605 // Initial conditions parameters
608 const unsigned int maximum_non_linear_iterations_ic = 5,
609 // Additional running and nonlinear solver parameters
610 const double maximum_step_size = 0.0,
611 const long int maximum_number_of_steps = 500,
612 const unsigned int maximum_error_test_failures = 10,
613 const unsigned int maximum_convergence_failures = 10,
614 const double nonlinear_convergence_coefficient = 0.33,
615 // Additional initial-condition correction parameters
616 const unsigned int maximum_number_of_steps_ic = 5,
617 const unsigned int maximum_number_of_jacobians_ic = 4,
618 const unsigned int maximum_number_of_backtracks_ic = 100,
619 const bool line_search_enabled_ic = true,
620 const double step_tolerance_ic =
621 std::pow(std::numeric_limits<double>::epsilon(), 2.0 / 3.0),
622 const double nonlinear_convergence_coefficient_ic = 0.0033,
623 // Additional linear solver parameters
624 const double linear_tolerance_factor = 0.05,
625 const double increment_factor = 1.0,
626 const bool linear_solution_scaling = true,
627 const double delta_cj_lsetup = 0.25,
628 // Step adaptivity parameters
629 const double eta_min = 0.5,
630 const double eta_max = 2.0,
631 const double eta_low = 0.9,
632 const double eta_min_err_fail = 0.25,
633 const double eta_conv_fail = 0.25,
634 const double eta_min_fixed = 1.0,
635 const double eta_max_fixed = 2.0,
636 const unsigned int maximum_constraint_failures = 10)
675 {}
676
719 void
721 {
722 prm.add_parameter("Initial time", initial_time);
723 prm.add_parameter("Final time", final_time);
724 prm.add_parameter("Time interval between each output", output_period);
725
726 prm.enter_subsection("Running parameters");
727 prm.add_parameter("Initial step size", initial_step_size);
728 prm.add_parameter("Minimum step size",
730 "Smallest allowed integration step.");
731 prm.add_parameter(
732 "Maximum step size",
734 "Largest allowed integration step; zero is unlimited.");
735 prm.add_parameter("Maximum number of steps",
737 "Maximum internal steps between output times.");
738 prm.add_parameter(
739 "Maximum error test failures",
741 "Stops a solve after repeated local error test rejections.");
742 prm.add_parameter(
743 "Maximum nonlinear convergence failures",
745 "Stops a solve after repeated nonlinear solver failures.");
746 prm.add_parameter("Maximum order of BDF", maximum_order);
747 prm.add_parameter("Maximum number of nonlinear iterations",
749 prm.leave_subsection();
750
751 prm.enter_subsection("Nonlinear solver");
752 prm.add_parameter(
753 "Convergence coefficient",
755 "Controls when Newton corrections count as converged.");
756 prm.leave_subsection();
757
758 prm.enter_subsection("Error control");
759 prm.add_parameter("Absolute error tolerance", absolute_tolerance);
760 prm.add_parameter("Relative error tolerance", relative_tolerance);
761 prm.add_parameter(
762 "Ignore algebraic terms for error computations",
764 "Indicate whether or not to suppress algebraic variables "
765 "in the local error test.");
766 prm.leave_subsection();
767
768 prm.enter_subsection("Initial condition correction parameters");
769 const auto correction_type_to_string =
770 [](const InitialConditionCorrection type) {
771 switch (type)
772 {
773 case none:
774 return std::string("none");
775 case use_y_diff:
776 return std::string("use_y_diff");
777 case use_y_dot:
778 return std::string("use_y_dot");
779 }
780
782 return std::string();
783 };
784 const auto set_correction_type = [](const std::string &value,
786 if (value == "none")
787 type = none;
788 else if (value == "use_y_diff")
789 type = use_y_diff;
790 else if (value == "use_y_dot")
791 type = use_y_dot;
792 else
794 };
795
796 prm.declare_entry(
797 "Correction type at initial time",
798 correction_type_to_string(ic_type),
799 Patterns::Selection("none|use_y_diff|use_y_dot"),
800 "This is one of the following three options for the "
801 "initial condition calculation. \n"
802 " none: do not try to make initial conditions consistent. \n"
803 " use_y_diff: compute the algebraic components of y and differential\n"
804 " components of y_dot, given the differential components of y. \n"
805 " This option requires that the user specifies differential and \n"
806 " algebraic components in the function differential_components().\n"
807 " use_y_dot: compute all components of y, given y_dot.");
808 prm.add_action("Correction type at initial time",
809 [this, set_correction_type](const std::string &value) {
810 set_correction_type(value, ic_type);
811 });
812 prm.declare_entry(
813 "Correction type after restart",
814 correction_type_to_string(reset_type),
815 Patterns::Selection("none|use_y_diff|use_y_dot"),
816 "This is one of the following three options for the "
817 "initial condition calculation. \n"
818 " none: do not try to make initial conditions consistent. \n"
819 " use_y_diff: compute the algebraic components of y and differential\n"
820 " components of y_dot, given the differential components of y. \n"
821 " This option requires that the user specifies differential and \n"
822 " algebraic components in the function differential_components().\n"
823 " use_y_dot: compute all components of y, given y_dot.");
824 prm.add_action("Correction type after restart",
825 [this, set_correction_type](const std::string &value) {
826 set_correction_type(value, reset_type);
827 });
828 prm.add_parameter("Maximum number of nonlinear iterations",
830 prm.add_parameter(
831 "Factor to use when converting from the integrator tolerance to the linear solver tolerance",
833 prm.add_parameter(
834 "Maximum number of steps",
836 "Bounds work while making the initial values consistent.");
837 prm.add_parameter(
838 "Maximum number of Jacobians",
840 "Bounds Jacobian refresh attempts during IC correction.");
841 prm.add_parameter("Maximum number of backtracks",
843 "Limits trial reductions in the IC line search.");
844 prm.add_parameter("Line search enabled",
846 "Enable backtracking when adjusting initial values.");
847 prm.add_parameter(
848 "Step tolerance",
850 "Treats a sufficiently small Newton correction as convergence.");
851 prm.add_parameter(
852 "Nonlinear convergence coefficient",
854 "Sets the Newton convergence test used by IDACalcIC.");
855 prm.leave_subsection();
856
857 prm.enter_subsection("Linear solver");
858 prm.add_parameter(
859 "Linear tolerance factor",
861 "Relates linear solve accuracy to the nonlinear tolerance.");
862 prm.add_parameter(
863 "Difference quotient increment factor",
865 "Scales finite-difference perturbations in Jacobian products.");
866 prm.add_parameter(
867 "Linear solution scaling",
869 "Compensates for changes in Cj between linear solves.");
870 prm.add_parameter(
871 "Cj setup difference factor",
873 "Rebuilds linear setup when Cj changes by this fraction.");
874 prm.leave_subsection();
875
876 prm.enter_subsection("Step adaptivity");
877 prm.add_parameter(
878 "Minimum step growth factor",
879 eta_min,
880 "Prevents overly aggressive reductions in step size.");
881 prm.add_parameter("Maximum step growth factor",
882 eta_max,
883 "Caps step growth after a successful step.");
884 prm.add_parameter("Low step growth factor",
885 eta_low,
886 "Applies when IDA takes a lower-order step.");
887 prm.add_parameter("Minimum growth factor after error failure",
889 "Limits step reduction after a failed error test.");
890 prm.add_parameter(
891 "Growth factor after convergence failure",
893 "Sets the next-step reduction after nonlinear failure.");
894 prm.add_parameter(
895 "Minimum fixed step growth factor",
897 "Lower step-size ratio bound for retaining the current step size.");
898 prm.add_parameter(
899 "Maximum fixed step growth factor",
901 "Upper step-size ratio bound for retaining the current step size.");
902 prm.leave_subsection();
903 prm.enter_subsection("Constraints");
904 prm.add_parameter(
905 "Maximum constraint failures",
907 "Rejects a step after this many constraint violations.");
908 prm.leave_subsection();
909 }
910
915
920
925
930
934 double maximum_step_size = 0.0;
935
943
948
953
958
963
968
973
978
983 std::pow(std::numeric_limits<double>::epsilon(), 2.0 / 3.0);
984
989
995
999 double increment_factor = 1.0;
1000
1006
1011 double delta_cj_lsetup = 0.25;
1012
1017 double eta_min = 0.5;
1018
1023 double eta_max = 2.0;
1024
1029 double eta_low = 0.9;
1030
1035 double eta_min_err_fail = 0.25;
1036
1041 double eta_conv_fail = 0.25;
1042
1049 double eta_min_fixed = 1.0;
1050
1057 double eta_max_fixed = 2.0;
1058
1064
1069
1074
1078 unsigned int maximum_order;
1079
1084
1089
1105
1117
1122
1127
1133 };
1134
1178
1186 IDA(const AdditionalData &data, const MPI_Comm mpi_comm);
1187
1191 ~IDA();
1192
1197 unsigned int
1198 solve_dae(VectorType &solution, VectorType &solution_dot);
1199
1221 void
1222 reset(const double t, const double h, VectorType &y, VectorType &yp);
1223
1229 std::function<void(VectorType &)> reinit_vector;
1230
1241 std::function<void(const double t,
1242 const VectorType &y,
1243 const VectorType &y_dot,
1244 VectorType &res)>
1246
1277 std::function<void(const double t,
1278 const VectorType &y,
1279 const VectorType &y_dot,
1280 const double alpha)>
1282
1324 std::function<
1325 void(const VectorType &rhs, VectorType &dst, const double tolerance)>
1327
1342 std::function<void(const double t,
1343 const VectorType &sol,
1344 const VectorType &sol_dot,
1345 const unsigned int step_number)>
1347
1371 std::function<bool(const double t, VectorType &sol, VectorType &sol_dot)>
1373
1391
1400 std::function<VectorType &()> get_local_tolerances;
1401
1412 std::function<VectorType &()> get_constraint_vector;
1413
1419 void *
1420 get_ida_memory() const;
1421
1431 std::function<void(void *ida_mem)> custom_setup;
1432
1433 private:
1439 std::string,
1440 << "Please provide an implementation for the function \""
1441 << arg1 << "\"");
1442
1448 void
1450
1455
1459 void *ida_mem;
1460
1461# if DEAL_II_SUNDIALS_VERSION_GTE(6, 0, 0)
1466# endif
1467
1473
1478
1484 mutable std::exception_ptr pending_exception;
1485
1486# ifdef DEAL_II_WITH_PETSC
1487# ifdef PETSC_USE_COMPLEX
1488 static_assert(!std::is_same_v<VectorType, PETScWrappers::MPI::Vector>,
1489 "Sundials does not support complex scalar types, "
1490 "but PETSc is configured to use a complex scalar type!");
1491
1492 static_assert(!std::is_same_v<VectorType, PETScWrappers::MPI::BlockVector>,
1493 "Sundials does not support complex scalar types, "
1494 "but PETSc is configured to use a complex scalar type!");
1495# endif // PETSC_USE_COMPLEX
1496# endif // DEAL_II_WITH_PETSC
1497 };
1498
1503 int,
1504 << "One of SUNDIALS IDA's internal functions "
1505 << "returned an error code: " << arg1
1506 << ". Please consult SUNDIALS manual.");
1507
1508} // namespace SUNDIALS
1509
1510#endif // DEAL_II_WITH_SUNDIALS
1511
1513
1514#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 enter_subsection(const std::string &subsection, const bool create_path_if_needed=true)
void add_action(const std::string &entry, const std::function< void(const std::string &value)> &action, const bool execute_action=true)
void declare_entry(const std::string &entry, const std::string &default_value, const Patterns::PatternBase &pattern=Patterns::Anything(), const std::string &documentation="", const bool has_to_be_set=false)
double nonlinear_convergence_coefficient_ic
Definition ida.h:988
unsigned int maximum_number_of_steps_ic
Definition ida.h:962
unsigned int maximum_number_of_backtracks_ic
Definition ida.h:972
InitialConditionCorrection ic_type
Definition ida.h:1104
void add_parameters(ParameterHandler &prm)
Definition ida.h:720
unsigned int maximum_convergence_failures
Definition ida.h:952
double nonlinear_convergence_coefficient
Definition ida.h:957
unsigned int maximum_error_test_failures
Definition ida.h:947
unsigned int maximum_constraint_failures
Definition ida.h:1063
AdditionalData(const double initial_time=0.0, const double final_time=1.0, const double initial_step_size=1e-2, const double output_period=1e-1, const double minimum_step_size=1e-15, const unsigned int maximum_order=5, const unsigned int maximum_non_linear_iterations=10, const double ls_norm_factor=0, const double absolute_tolerance=1e-6, const double relative_tolerance=1e-5, const bool ignore_algebraic_terms_for_errors=true, const InitialConditionCorrection &ic_type=use_y_diff, const InitialConditionCorrection &reset_type=use_y_diff, const unsigned int maximum_non_linear_iterations_ic=5, const double maximum_step_size=0.0, const long int maximum_number_of_steps=500, const unsigned int maximum_error_test_failures=10, const unsigned int maximum_convergence_failures=10, const double nonlinear_convergence_coefficient=0.33, const unsigned int maximum_number_of_steps_ic=5, const unsigned int maximum_number_of_jacobians_ic=4, const unsigned int maximum_number_of_backtracks_ic=100, const bool line_search_enabled_ic=true, const double step_tolerance_ic=std::pow(std::numeric_limits< double >::epsilon(), 2.0/3.0), const double nonlinear_convergence_coefficient_ic=0.0033, const double linear_tolerance_factor=0.05, const double increment_factor=1.0, const bool linear_solution_scaling=true, const double delta_cj_lsetup=0.25, const double eta_min=0.5, const double eta_max=2.0, const double eta_low=0.9, const double eta_min_err_fail=0.25, const double eta_conv_fail=0.25, const double eta_min_fixed=1.0, const double eta_max_fixed=2.0, const unsigned int maximum_constraint_failures=10)
Definition ida.h:591
long int maximum_number_of_steps
Definition ida.h:942
unsigned maximum_non_linear_iterations_ic
Definition ida.h:1121
InitialConditionCorrection reset_type
Definition ida.h:1116
unsigned int maximum_order
Definition ida.h:1078
unsigned int maximum_number_of_jacobians_ic
Definition ida.h:967
unsigned int maximum_non_linear_iterations
Definition ida.h:1126
std::function< void(const VectorType &rhs, VectorType &dst, const double tolerance)> solve_with_jacobian
Definition ida.h:1326
unsigned int solve_dae(VectorType &solution, VectorType &solution_dot)
Definition ida.cc:115
MPI_Comm mpi_communicator
Definition ida.h:1472
std::function< void(const double t, const VectorType &y, const VectorType &y_dot, const double alpha)> setup_jacobian
Definition ida.h:1281
void set_functions_to_trigger_an_assert()
Definition ida.cc:587
const AdditionalData data
Definition ida.h:1454
void reset(const double t, const double h, VectorType &y, VectorType &yp)
Definition ida.cc:198
std::function< void(const double t, const VectorType &y, const VectorType &y_dot, VectorType &res)> residual
Definition ida.h:1245
std::function< void(const double t, const VectorType &sol, const VectorType &sol_dot, const unsigned int step_number)> output_step
Definition ida.h:1346
std::function< IndexSet()> differential_components
Definition ida.h:1390
std::function< void(void *ida_mem)> custom_setup
Definition ida.h:1431
void * get_ida_memory() const
Definition ida.cc:622
std::function< VectorType &()> get_local_tolerances
Definition ida.h:1400
void * ida_mem
Definition ida.h:1459
std::function< bool(const double t, VectorType &sol, VectorType &sol_dot)> solver_should_restart
Definition ida.h:1372
GrowingVectorMemory< VectorType > mem
Definition ida.h:1477
std::function< void(VectorType &)> reinit_vector
Definition ida.h:1229
std::function< VectorType &()> get_constraint_vector
Definition ida.h:1412
SUNContext ida_ctx
Definition ida.h:1465
std::exception_ptr pending_exception
Definition ida.h:1484
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define DEAL_II_ASSERT_UNREACHABLE()
static ::ExceptionBase & ExcFunctionNotProvided(std::string arg1)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcIDAError(int arg1)
#define DeclException1(Exception1, type1, outsequence)
#define AssertThrow(cond, exc)
::VectorizedArray< Number, width > pow(const ::VectorizedArray< Number, width > &, const Number p)