| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2014 - 2025 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_time_stepping_h |
| 16 |
#define dealii_time_stepping_h |
| 17 |
|
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <deal.II/base/signaling_nan.h> |
| 22 |
|
| 23 |
#include <functional> |
| 24 |
#include <vector> |
| 25 |
|
| 26 |
DEAL_II_NAMESPACE_OPEN |
| 27 |
|
| 28 |
/** |
| 29 |
* Namespace containing the time stepping methods. |
| 30 |
*/ |
| 31 |
|
| 32 |
namespace TimeStepping |
| 33 |
{ |
| 34 |
/** |
| 35 |
* The following Runge-Kutta methods are available: |
| 36 |
* - Explicit methods (see ExplicitRungeKutta::initialize): |
| 37 |
* - FORWARD_EULER (first order) |
| 38 |
* - RK_THIRD_ORDER (third order Runge-Kutta) |
| 39 |
* - SSP_THIRD_ORDER (third order SSP Runge-Kutta) |
| 40 |
* - RK_CLASSIC_FOURTH_ORDER (classical fourth order Runge-Kutta) |
| 41 |
* - Low-storage (explicit) Runge-Kutta methods |
| 42 |
* - LOW_STORAGE_RK_STAGE3_ORDER3 (Three stages and third order) |
| 43 |
* - LOW_STORAGE_RK_STAGE5_ORDER4 (Five stages and fourth order) |
| 44 |
* - LOW_STORAGE_RK_STAGE7_ORDER4 (Seven stages and fourth order) |
| 45 |
* - LOW_STORAGE_RK_STAGE9_ORDER5 (Nine stages and fifth order) |
| 46 |
* - Implicit methods (see ImplicitRungeKutta::initialize): |
| 47 |
* - BACKWARD_EULER (first order) |
| 48 |
* - IMPLICIT_MIDPOINT (second order) |
| 49 |
* - CRANK_NICOLSON (second order) |
| 50 |
* - SDIRK_TWO_STAGES (second order) |
| 51 |
* - Embedded explicit methods (see EmbeddedExplicitRungeKutta::initialize): |
| 52 |
* - HEUN_EULER (second order) |
| 53 |
* - BOGACKI_SHAMPINE (third order) |
| 54 |
* - DOPRI (Dormand-Prince method, fifth order; this is the method used by |
| 55 |
* ode45 in MATLAB) |
| 56 |
* - FEHLBERG (fifth order) |
| 57 |
* - CASH_KARP (fifth order) |
| 58 |
*/ |
| 59 |
enum runge_kutta_method |
| 60 |
{ |
| 61 |
/** |
| 62 |
* Forward Euler method, first order. |
| 63 |
*/ |
| 64 |
FORWARD_EULER, |
| 65 |
/** |
| 66 |
* Third order Runge-Kutta method. |
| 67 |
*/ |
| 68 |
RK_THIRD_ORDER, |
| 69 |
/** |
| 70 |
* Third order Strong Stability Preserving (SSP) Runge-Kutta method |
| 71 |
* (SSP time discretizations are also called Total Variation Diminishing |
| 72 |
* (TVD) methods in the literature, see @cite gottlieb2001strong). |
| 73 |
*/ |
| 74 |
SSP_THIRD_ORDER, |
| 75 |
/** |
| 76 |
* Classical fourth order Runge-Kutta method. |
| 77 |
*/ |
| 78 |
RK_CLASSIC_FOURTH_ORDER, |
| 79 |
/** |
| 80 |
* Fifth order Runge-Kutta method. |
| 81 |
*/ |
| 82 |
RK_FIFTH_ORDER, |
| 83 |
/** |
| 84 |
* Sixth order Runge-Kutta method. |
| 85 |
*/ |
| 86 |
RK_SIXTH_ORDER, |
| 87 |
/** |
| 88 |
* Three-stage scheme of order three by Kennedy et al. |
| 89 |
* @cite KennedyCarpenterLewis2000. Its stability region is |
| 90 |
* significantly smaller than the higher order schemes, but due to three |
| 91 |
* stages only, it is very competitive in terms of the work per stage. |
| 92 |
*/ |
| 93 |
LOW_STORAGE_RK_STAGE3_ORDER3, |
| 94 |
/** |
| 95 |
* Five-stage scheme of order four, |
| 96 |
* defined in the paper by Kennedy et al. @cite KennedyCarpenterLewis2000. |
| 97 |
*/ |
| 98 |
LOW_STORAGE_RK_STAGE5_ORDER4, |
| 99 |
/** |
| 100 |
* Seven-stage scheme of order four defined in the paper by Tselios and |
| 101 |
* Simos @cite TseliosSimos2007. |
| 102 |
*/ |
| 103 |
LOW_STORAGE_RK_STAGE7_ORDER4, |
| 104 |
/** |
| 105 |
* Nine-stage scheme of order five |
| 106 |
* defined in the paper by Kennedy et al. @cite KennedyCarpenterLewis2000. |
| 107 |
*/ |
| 108 |
LOW_STORAGE_RK_STAGE9_ORDER5, |
| 109 |
/** |
| 110 |
* Backward Euler method, first order. |
| 111 |
*/ |
| 112 |
BACKWARD_EULER, |
| 113 |
/** |
| 114 |
* Implicit midpoint method, second order. |
| 115 |
*/ |
| 116 |
IMPLICIT_MIDPOINT, |
| 117 |
/** |
| 118 |
* Crank-Nicolson method, second order. |
| 119 |
*/ |
| 120 |
CRANK_NICOLSON, |
| 121 |
/** |
| 122 |
* Two stage SDIRK method (short for "singly diagonally implicit |
| 123 |
* Runge-Kutta"), second order. |
| 124 |
*/ |
| 125 |
SDIRK_TWO_STAGES, |
| 126 |
/** |
| 127 |
* Heun's method (improved Euler's method), second order. |
| 128 |
*/ |
| 129 |
HEUN_EULER, |
| 130 |
/** |
| 131 |
* Bogacki–Shampine method, third-order. |
| 132 |
*/ |
| 133 |
BOGACKI_SHAMPINE, |
| 134 |
/** |
| 135 |
* Dormand-Prince method, fifth order; this is the method used by |
| 136 |
* ode45 in MATLAB. |
| 137 |
*/ |
| 138 |
DOPRI, |
| 139 |
/** |
| 140 |
* Fehlberg method, fifth order. |
| 141 |
*/ |
| 142 |
FEHLBERG, |
| 143 |
/** |
| 144 |
* Cash–Karp method, fifth order. |
| 145 |
*/ |
| 146 |
CASH_KARP, |
| 147 |
/** |
| 148 |
* Invalid. |
| 149 |
*/ |
| 150 |
invalid |
| 151 |
}; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
/** |
| 156 |
* Reason for exiting evolve_one_time_step when using an embedded method: |
| 157 |
* DELTA_T, MIN_DELTA_T, MAX_DELTA_T. |
| 158 |
*/ |
| 159 |
enum embedded_runge_kutta_time_step |
| 160 |
{ |
| 161 |
/** |
| 162 |
* The time step is in the valid range. |
| 163 |
*/ |
| 164 |
DELTA_T, |
| 165 |
/** |
| 166 |
* The time step was increased to the minimum acceptable time step. |
| 167 |
*/ |
| 168 |
MIN_DELTA_T, |
| 169 |
/** |
| 170 |
* The time step was reduced to the maximum acceptable time step. |
| 171 |
*/ |
| 172 |
MAX_DELTA_T |
| 173 |
}; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
/** |
| 178 |
* Abstract class for time stepping methods. These methods assume that the |
| 179 |
* equation has the form: $ \frac{\partial y}{\partial t} = f(t,y) $. |
| 180 |
*/ |
| 181 |
template <typename VectorType> |
| 182 |
class TimeStepping |
| 183 |
{ |
| 184 |
public: |
| 185 |
/** |
| 186 |
* Virtual destructor. |
| 187 |
*/ |
| 188 |
virtual ~TimeStepping() = default; |
| 189 |
|
| 190 |
/** |
| 191 |
* Purely virtual function. This function is used to advance from time @p |
| 192 |
* t to t+ @p delta_t. @p F is a vector of functions $ f(t,y) $ that |
| 193 |
* should be integrated, the input parameters are the time t and the |
| 194 |
* vector y and the output is value of f at this point. @p J_inverse is a |
| 195 |
* vector functions that compute the inverse of the Jacobians associated |
| 196 |
* to the implicit problems. The input parameters are the time, $ \tau $, |
| 197 |
* and a vector. The output is the value of function at this point. This |
| 198 |
* function returns the time at the end of the time step. |
| 199 |
*/ |
| 200 |
virtual double |
| 201 |
evolve_one_time_step( |
| 202 |
std::vector<std::function<VectorType(const double, const VectorType &)>> |
| 203 |
&F, |
| 204 |
std::vector<std::function< |
| 205 |
VectorType(const double, const double, const VectorType &)>> &J_inverse, |
| 206 |
double t, |
| 207 |
double delta_t, |
| 208 |
VectorType &y) = 0; |
| 209 |
|
| 210 |
/** |
| 211 |
* Empty structure used to store information. |
| 212 |
*/ |
| 213 |
struct Status |
| 214 |
{}; |
| 215 |
|
| 216 |
/** |
| 217 |
* Purely virtual function that return Status. |
| 218 |
*/ |
| 219 |
virtual const Status & |
| 220 |
get_status() const = 0; |
| 221 |
}; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
/** |
| 226 |
* Base class for the Runge-Kutta method |
| 227 |
*/ |
| 228 |
template <typename VectorType> |
| 229 |
class RungeKutta : public TimeStepping<VectorType> |
| 230 |
{ |
| 231 |
public: |
| 232 |
/** |
| 233 |
* Virtual destructor. |
| 234 |
*/ |
| 235 |
virtual ~RungeKutta() override = default; |
| 236 |
|
| 237 |
/** |
| 238 |
* Purely virtual method used to initialize the Runge-Kutta method. |
| 239 |
*/ |
| 240 |
virtual void |
| 241 |
initialize(const runge_kutta_method method) = 0; |
| 242 |
|
| 243 |
/** |
| 244 |
* This function is used to advance from time @p t to t+ @p delta_t. @p F |
| 245 |
* is a vector of functions $ f(t,y) $ that should be integrated, the |
| 246 |
* input parameters are the time t and the vector y and the output is |
| 247 |
* value of f at this point. @p J_inverse is a vector functions that |
| 248 |
* compute the inverse of the Jacobians associated to the implicit |
| 249 |
* problems. The input parameters are the time, $ \tau $, and a vector. |
| 250 |
* The output is the value of function at this point. This function |
| 251 |
* returns the time at the end of the time step. When using Runge-Kutta |
| 252 |
* methods, @p F and @p J_inverse can only contain one element. |
| 253 |
*/ |
| 254 |
double |
| 255 |
evolve_one_time_step( |
| 256 |
std::vector<std::function<VectorType(const double, const VectorType &)>> |
| 257 |
&F, |
| 258 |
std::vector<std::function< |
| 259 |
VectorType(const double, const double, const VectorType &)>> &J_inverse, |
| 260 |
double t, |
| 261 |
double delta_t, |
| 262 |
VectorType &y) override; |
| 263 |
|
| 264 |
/** |
| 265 |
* Purely virtual function. This function is used to advance from time @p |
| 266 |
* t to t+ @p delta_t. @p f is the function $ f(t,y) $ that should be |
| 267 |
* integrated, the input parameters are the time t and the vector y and |
| 268 |
* the output is value of f at this point. @p id_minus_tau_J_inverse is a |
| 269 |
* function that computes $ inv(I-\tau J)$ where $ I $ is the identity |
| 270 |
* matrix, $ \tau $ is given, and $ J $ is the Jacobian $ \frac{\partial |
| 271 |
* f}{\partial y} $. The input parameters are the time, $ \tau $, and a |
| 272 |
* vector. The output is the value of function at this point. |
| 273 |
* evolve_one_time_step returns the time at the end of the time step. |
| 274 |
*/ |
| 275 |
virtual double |
| 276 |
evolve_one_time_step( |
| 277 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 278 |
const std::function< |
| 279 |
VectorType(const double, const double, const VectorType &)> |
| 280 |
&id_minus_tau_J_inverse, |
| 281 |
double t, |
| 282 |
double delta_t, |
| 283 |
VectorType &y) = 0; |
| 284 |
|
| 285 |
protected: |
| 286 |
/** |
| 287 |
* Number of stages of the Runge-Kutta method. |
| 288 |
*/ |
| 289 |
unsigned int n_stages; |
| 290 |
|
| 291 |
/** |
| 292 |
* Butcher tableau coefficients. |
| 293 |
*/ |
| 294 |
std::vector<double> b; |
| 295 |
|
| 296 |
/** |
| 297 |
* Butcher tableau coefficients. |
| 298 |
*/ |
| 299 |
std::vector<double> c; |
| 300 |
|
| 301 |
/** |
| 302 |
* Butcher tableau coefficients. |
| 303 |
*/ |
| 304 |
std::vector<std::vector<double>> a; |
| 305 |
}; |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
/** |
| 310 |
* ExplicitRungeKutta is derived from RungeKutta and implement the explicit |
| 311 |
* methods. |
| 312 |
*/ |
| 313 |
template <typename VectorType> |
| 314 |
class ExplicitRungeKutta : public RungeKutta<VectorType> |
| 315 |
{ |
| 316 |
public: |
| 317 |
using RungeKutta<VectorType>::evolve_one_time_step; |
| 318 |
|
| 319 |
/** |
| 320 |
* Default constructor. This constructor creates an object for which |
| 321 |
* you will want to call <code>initialize(runge_kutta_method)</code> |
| 322 |
* before it can be used. |
| 323 |
*/ |
| 324 |
ExplicitRungeKutta() = default; |
| 325 |
|
| 326 |
/** |
| 327 |
* Constructor. This function calls initialize(runge_kutta_method). |
| 328 |
*/ |
| 329 |
ExplicitRungeKutta(const runge_kutta_method method); |
| 330 |
|
| 331 |
/** |
| 332 |
* Initialize the explicit Runge-Kutta method. |
| 333 |
*/ |
| 334 |
void |
| 335 |
initialize(const runge_kutta_method method) override; |
| 336 |
|
| 337 |
/** |
| 338 |
* This function is used to advance from time @p t to t+ @p delta_t. @p f |
| 339 |
* is the function $ f(t,y) $ that should be integrated, the input |
| 340 |
* parameters are the time t and the vector y and the output is value of f |
| 341 |
* at this point. @p id_minus_tau_J_inverse is a function that computes $ |
| 342 |
* inv(I-\tau J)$ where $ I $ is the identity matrix, $ \tau $ is given, |
| 343 |
* and $ J $ is the Jacobian $ \frac{\partial f}{\partial y} $. The input |
| 344 |
* parameter are the time, $ \tau $, and a vector. The output is the value |
| 345 |
* of function at this point. evolve_one_time_step returns the time at the |
| 346 |
* end of the time step. |
| 347 |
* |
| 348 |
* @note @p id_minus_tau_J_inverse is ignored since the method is explicit. |
| 349 |
*/ |
| 350 |
double |
| 351 |
evolve_one_time_step( |
| 352 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 353 |
const std::function< |
| 354 |
VectorType(const double, const double, const VectorType &)> |
| 355 |
&id_minus_tau_J_inverse, |
| 356 |
double t, |
| 357 |
double delta_t, |
| 358 |
VectorType &y) override; |
| 359 |
|
| 360 |
/** |
| 361 |
* This function is used to advance from time @p t to t+ @p delta_t. This |
| 362 |
* function is similar to the one derived from RungeKutta, but does not |
| 363 |
* required id_minus_tau_J_inverse because it is not used for explicit |
| 364 |
* methods. evolve_one_time_step returns the time at the end of the time |
| 365 |
* step. |
| 366 |
*/ |
| 367 |
double |
| 368 |
evolve_one_time_step( |
| 369 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 370 |
double t, |
| 371 |
double delta_t, |
| 372 |
VectorType &y); |
| 373 |
|
| 374 |
/** |
| 375 |
* This structure stores the name of the method used. |
| 376 |
*/ |
| 377 |
struct Status : public TimeStepping<VectorType>::Status |
| 378 |
{ |
| 379 |
Status() |
| 380 |
: method(invalid) |
| 381 |
{} |
| 382 |
|
| 383 |
runge_kutta_method method; |
| 384 |
}; |
| 385 |
|
| 386 |
/** |
| 387 |
* Return the status of the current object. |
| 388 |
*/ |
| 389 |
const Status & |
| 390 |
get_status() const override; |
| 391 |
|
| 392 |
private: |
| 393 |
/** |
| 394 |
* Compute the different stages needed. |
| 395 |
*/ |
| 396 |
void |
| 397 |
compute_stages( |
| 398 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 399 |
const double t, |
| 400 |
const double delta_t, |
| 401 |
const VectorType &y, |
| 402 |
std::vector<VectorType> &f_stages) const; |
| 403 |
|
| 404 |
/** |
| 405 |
* Status structure of the object. |
| 406 |
*/ |
| 407 |
Status status; |
| 408 |
}; |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
/** |
| 413 |
* The LowStorageRungeKutta class is derived from RungeKutta and implements a |
| 414 |
* specific class of explicit methods. The main advantages of low-storage |
| 415 |
* methods are the reduced memory consumption and the reduced memory access. |
| 416 |
*/ |
| 417 |
template <typename VectorType> |
| 418 |
class LowStorageRungeKutta : public RungeKutta<VectorType> |
| 419 |
{ |
| 420 |
public: |
| 421 |
using RungeKutta<VectorType>::evolve_one_time_step; |
| 422 |
|
| 423 |
/** |
| 424 |
* Default constructor. This constructor creates an object for which |
| 425 |
* you will want to call <code>initialize(runge_kutta_method)</code> |
| 426 |
* before it can be used. |
| 427 |
*/ |
| 428 |
LowStorageRungeKutta() = default; |
| 429 |
|
| 430 |
/** |
| 431 |
* Constructor. This function calls initialize(runge_kutta_method). |
| 432 |
*/ |
| 433 |
LowStorageRungeKutta(const runge_kutta_method method); |
| 434 |
|
| 435 |
/** |
| 436 |
* Initialize the explicit Runge-Kutta method. |
| 437 |
*/ |
| 438 |
void |
| 439 |
initialize(const runge_kutta_method method) override; |
| 440 |
|
| 441 |
/** |
| 442 |
* This function is used to advance from time @p t to t+ @p delta_t. @p f |
| 443 |
* is the function $ f(t,y) $ that should be integrated, the input |
| 444 |
* parameters are the time t and the vector y and the output is value of f |
| 445 |
* at this point. @p id_minus_tau_J_inverse is a function that computes $ |
| 446 |
* inv(I-\tau J)$ where $ I $ is the identity matrix, $ \tau $ is given, |
| 447 |
* and $ J $ is the Jacobian $ \frac{\partial f}{\partial y} $. The input |
| 448 |
* parameters are the time, $ \tau $, and a vector. The output is the value |
| 449 |
* of function at this point. evolve_one_time_step returns the time at the |
| 450 |
* end of the time step. |
| 451 |
*/ |
| 452 |
double |
| 453 |
evolve_one_time_step( |
| 454 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 455 |
const std::function< |
| 456 |
VectorType(const double, const double, const VectorType &)> |
| 457 |
&id_minus_tau_J_inverse, |
| 458 |
double t, |
| 459 |
double delta_t, |
| 460 |
VectorType &y) override; |
| 461 |
|
| 462 |
/** |
| 463 |
* This function is used to advance from time @p t to t+ @p delta_t. This |
| 464 |
* function is similar to the one derived from RungeKutta, but does not |
| 465 |
* required id_minus_tau_J_inverse because it is not used for explicit |
| 466 |
* methods. evolve_one_time_step returns the time at the end of the time |
| 467 |
* step. Note that vec_ki holds the evaluation of the differential operator, |
| 468 |
* and vec_ri holds the right-hand side for the differential operator |
| 469 |
* application. |
| 470 |
*/ |
| 471 |
double |
| 472 |
evolve_one_time_step( |
| 473 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 474 |
double t, |
| 475 |
double delta_t, |
| 476 |
VectorType &solution, |
| 477 |
VectorType &vec_ri, |
| 478 |
VectorType &vec_ki); |
| 479 |
|
| 480 |
/** |
| 481 |
* Get the coefficients of the scheme. |
| 482 |
* Note that here vector @p a is not the conventional definition in terms of a |
| 483 |
* Butcher tableau but merely one of the sub-diagonals. More details can be |
| 484 |
* found in step-67 and the references therein. |
| 485 |
*/ |
| 486 |
void |
| 487 |
get_coefficients(std::vector<double> &a, |
| 488 |
std::vector<double> &b, |
| 489 |
std::vector<double> &c) const; |
| 490 |
|
| 491 |
/** |
| 492 |
* This structure stores the name of the method used. |
| 493 |
*/ |
| 494 |
struct Status : public TimeStepping<VectorType>::Status |
| 495 |
{ |
| 496 |
Status() |
| 497 |
: method(invalid) |
| 498 |
{} |
| 499 |
|
| 500 |
runge_kutta_method method; |
| 501 |
}; |
| 502 |
|
| 503 |
/** |
| 504 |
* Return the status of the current object. |
| 505 |
*/ |
| 506 |
const Status & |
| 507 |
get_status() const override; |
| 508 |
|
| 509 |
private: |
| 510 |
/** |
| 511 |
* Compute one stage of low storage rk. |
| 512 |
*/ |
| 513 |
void |
| 514 |
compute_one_stage( |
| 515 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 516 |
const double t, |
| 517 |
const double factor_solution, |
| 518 |
const double factor_ai, |
| 519 |
const VectorType ¤t_ri, |
| 520 |
VectorType &vec_ki, |
| 521 |
VectorType &solution, |
| 522 |
VectorType &next_ri) const; |
| 523 |
|
| 524 |
/** |
| 525 |
* Status structure of the object. |
| 526 |
*/ |
| 527 |
Status status; |
| 528 |
}; |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
/** |
| 533 |
* This class is derived from RungeKutta and implement the implicit methods. |
| 534 |
* This class works only for Diagonal Implicit Runge-Kutta (DIRK) methods. |
| 535 |
*/ |
| 536 |
template <typename VectorType> |
| 537 |
class ImplicitRungeKutta : public RungeKutta<VectorType> |
| 538 |
{ |
| 539 |
public: |
| 540 |
using RungeKutta<VectorType>::evolve_one_time_step; |
| 541 |
|
| 542 |
/** |
| 543 |
* Default constructor. initialize(runge_kutta_method) and |
| 544 |
* set_newton_solver_parameters(unsigned int,double) need to be called |
| 545 |
* before the object can be used. |
| 546 |
*/ |
| 547 |
ImplicitRungeKutta() = default; |
| 548 |
|
| 549 |
/** |
| 550 |
* Constructor. This function calls initialize(runge_kutta_method) and |
| 551 |
* initialize the maximum number of iterations and the tolerance of the |
| 552 |
* Newton solver. |
| 553 |
*/ |
| 554 |
ImplicitRungeKutta(const runge_kutta_method method, |
| 555 |
const unsigned int max_it = 100, |
| 556 |
const double tolerance = 1e-6); |
| 557 |
|
| 558 |
/** |
| 559 |
* Initialize the implicit Runge-Kutta method. |
| 560 |
*/ |
| 561 |
void |
| 562 |
initialize(const runge_kutta_method method) override; |
| 563 |
|
| 564 |
/** |
| 565 |
* This function is used to advance from time @p t to t+ @p delta_t. @p f |
| 566 |
* is the function $ f(t,y) $ that should be integrated, the input |
| 567 |
* parameters are the time t and the vector y and the output is value of f |
| 568 |
* at this point. @p id_minus_tau_J_inverse is a function that computes $ |
| 569 |
* (I-\tau J)^{-1}$ where $ I $ is the identity matrix, $ \tau $ is given, |
| 570 |
* and $ J $ is the Jacobian $ \frac{\partial f}{\partial y} $. The input |
| 571 |
* parameters this function receives are the time, $ \tau $, and a vector. |
| 572 |
* The output is the value of function at this point. evolve_one_time_step |
| 573 |
* returns the time at the end of the time step. |
| 574 |
*/ |
| 575 |
double |
| 576 |
evolve_one_time_step( |
| 577 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 578 |
const std::function< |
| 579 |
VectorType(const double, const double, const VectorType &)> |
| 580 |
&id_minus_tau_J_inverse, |
| 581 |
double t, |
| 582 |
double delta_t, |
| 583 |
VectorType &y) override; |
| 584 |
|
| 585 |
/** |
| 586 |
* Set the maximum number of iterations and the tolerance used by the |
| 587 |
* Newton solver. |
| 588 |
*/ |
| 589 |
void |
| 590 |
set_newton_solver_parameters(const unsigned int max_it, |
| 591 |
const double tolerance); |
| 592 |
|
| 593 |
/** |
| 594 |
* Structure that stores the name of the method, the number of Newton |
| 595 |
* iterations and the norm of the residual when exiting the Newton solver. |
| 596 |
*/ |
| 597 |
struct Status : public TimeStepping<VectorType>::Status |
| 598 |
{ |
| 599 |
Status() |
| 600 |
: method(invalid) |
| 601 |
, n_iterations(numbers::invalid_unsigned_int) |
| 602 |
, norm_residual(numbers::signaling_nan<double>()) |
| 603 |
{} |
| 604 |
|
| 605 |
runge_kutta_method method; |
| 606 |
unsigned int n_iterations; |
| 607 |
double norm_residual; |
| 608 |
}; |
| 609 |
|
| 610 |
/** |
| 611 |
* Return the status of the current object. |
| 612 |
*/ |
| 613 |
const Status & |
| 614 |
get_status() const override; |
| 615 |
|
| 616 |
private: |
| 617 |
/** |
| 618 |
* Compute the different stages needed. |
| 619 |
*/ |
| 620 |
void |
| 621 |
compute_stages( |
| 622 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 623 |
const std::function< |
| 624 |
VectorType(const double, const double, const VectorType &)> |
| 625 |
&id_minus_tau_J_inverse, |
| 626 |
double t, |
| 627 |
double delta_t, |
| 628 |
VectorType &y, |
| 629 |
std::vector<VectorType> &f_stages); |
| 630 |
|
| 631 |
/** |
| 632 |
* Newton solver used for the implicit stages. |
| 633 |
*/ |
| 634 |
void |
| 635 |
newton_solve( |
| 636 |
const std::function<void(const VectorType &, VectorType &)> &get_residual, |
| 637 |
const std::function<VectorType(const VectorType &)> |
| 638 |
&id_minus_tau_J_inverse, |
| 639 |
VectorType &y); |
| 640 |
|
| 641 |
/** |
| 642 |
* Compute the residual needed by the Newton solver. |
| 643 |
*/ |
| 644 |
void |
| 645 |
compute_residual( |
| 646 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 647 |
double t, |
| 648 |
double delta_t, |
| 649 |
const VectorType &new_y, |
| 650 |
const VectorType &y, |
| 651 |
VectorType &tendency, |
| 652 |
VectorType &residual) const; |
| 653 |
|
| 654 |
/** |
| 655 |
* Maximum number of iterations of the Newton solver. |
| 656 |
*/ |
| 657 |
unsigned int max_it; |
| 658 |
|
| 659 |
/** |
| 660 |
* Tolerance of the Newton solver. |
| 661 |
*/ |
| 662 |
double tolerance; |
| 663 |
|
| 664 |
/** |
| 665 |
* Status structure of the object. |
| 666 |
*/ |
| 667 |
Status status; |
| 668 |
}; |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
/** |
| 673 |
* This class is derived from RungeKutta and implements embedded explicit |
| 674 |
* methods. |
| 675 |
*/ |
| 676 |
template <typename VectorType> |
| 677 |
class EmbeddedExplicitRungeKutta : public RungeKutta<VectorType> |
| 678 |
{ |
| 679 |
public: |
| 680 |
using RungeKutta<VectorType>::evolve_one_time_step; |
| 681 |
|
| 682 |
/** |
| 683 |
* Default constructor. initialize(runge_kutta_method) and |
| 684 |
* set_time_adaptation_parameters(double, double, double, double, double, |
| 685 |
* double) need to be called before the object can be used. |
| 686 |
*/ |
| 687 |
EmbeddedExplicitRungeKutta() = default; |
| 688 |
|
| 689 |
/** |
| 690 |
* Constructor. This function calls initialize(runge_kutta_method) and |
| 691 |
* initialize the parameters needed for time adaptation. |
| 692 |
*/ |
| 693 |
EmbeddedExplicitRungeKutta(const runge_kutta_method method, |
| 694 |
const double coarsen_param = 1.2, |
| 695 |
const double refine_param = 0.8, |
| 696 |
const double min_delta = 1e-14, |
| 697 |
const double max_delta = 1e100, |
| 698 |
const double refine_tol = 1e-8, |
| 699 |
const double coarsen_tol = 1e-12); |
| 700 |
|
| 701 |
/** |
| 702 |
* Destructor. |
| 703 |
*/ |
| 704 |
~EmbeddedExplicitRungeKutta() override |
| 705 |
{ |
| 706 |
free_memory(); |
| 707 |
} |
| 708 |
|
| 709 |
/** |
| 710 |
* If necessary, deallocate memory allocated by the object. |
| 711 |
*/ |
| 712 |
void |
| 713 |
free_memory(); |
| 714 |
|
| 715 |
/** |
| 716 |
* Initialize the embedded explicit Runge-Kutta method. |
| 717 |
*/ |
| 718 |
void |
| 719 |
initialize(const runge_kutta_method method) override; |
| 720 |
|
| 721 |
/** |
| 722 |
* This function is used to advance from time @p t to t+ @p delta_t. @p f |
| 723 |
* is the function $ f(t,y) $ that should be integrated, the input |
| 724 |
* parameters are the time t and the vector y and the output is value of f |
| 725 |
* at this point. @p id_minus_tau_J_inverse is a function that computes $ |
| 726 |
* inv(I-\tau J)$ where $ I $ is the identity matrix, $ \tau $ is given, |
| 727 |
* and $ J $ is the Jacobian $ \frac{\partial f}{\partial y} $. The input |
| 728 |
* parameters are the time, $ \tau $, and a vector. The output is the |
| 729 |
* value of function at this point. evolve_one_time_step returns the time |
| 730 |
* at the end of the time step. |
| 731 |
* |
| 732 |
* @note @p id_minus_tau_J_inverse is ignored since the method is explicit. |
| 733 |
*/ |
| 734 |
double |
| 735 |
evolve_one_time_step( |
| 736 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 737 |
const std::function< |
| 738 |
VectorType(const double, const double, const VectorType &)> |
| 739 |
&id_minus_tau_J_inverse, |
| 740 |
double t, |
| 741 |
double delta_t, |
| 742 |
VectorType &y) override; |
| 743 |
|
| 744 |
/** |
| 745 |
* This function is used to advance from time @p t to t+ @p delta_t. This |
| 746 |
* function is similar to the one derived from TimeStepping, but does not |
| 747 |
* required id_minus_tau_J_inverse because it is not used for explicit |
| 748 |
* methods. evolve_one_time_step returns the time at the end of the time |
| 749 |
* step. |
| 750 |
*/ |
| 751 |
double |
| 752 |
evolve_one_time_step( |
| 753 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 754 |
double t, |
| 755 |
double delta_t, |
| 756 |
VectorType &y); |
| 757 |
|
| 758 |
/** |
| 759 |
* Set the parameters necessary for the time adaptation. |
| 760 |
*/ |
| 761 |
void |
| 762 |
set_time_adaptation_parameters(const double coarsen_param, |
| 763 |
const double refine_param, |
| 764 |
const double min_delta, |
| 765 |
const double max_delta, |
| 766 |
const double refine_tol, |
| 767 |
const double coarsen_tol); |
| 768 |
|
| 769 |
/** |
| 770 |
* Structure that stores the name of the method, the reason to exit |
| 771 |
* evolve_one_time_step, the number of iteration inside n_iterations, a |
| 772 |
* guess of what the next time step should be, and an estimate of the norm |
| 773 |
* of the error. |
| 774 |
*/ |
| 775 |
struct Status : public TimeStepping<VectorType>::Status |
| 776 |
{ |
| 777 |
Status() |
| 778 |
: method(invalid) |
| 779 |
{} |
| 780 |
|
| 781 |
runge_kutta_method method; |
| 782 |
embedded_runge_kutta_time_step exit_delta_t; |
| 783 |
unsigned int n_iterations; |
| 784 |
double delta_t_guess; |
| 785 |
double error_norm; |
| 786 |
}; |
| 787 |
|
| 788 |
/** |
| 789 |
* Return the status of the current object. |
| 790 |
*/ |
| 791 |
const Status & |
| 792 |
get_status() const override; |
| 793 |
|
| 794 |
private: |
| 795 |
/** |
| 796 |
* Compute the different stages needed. |
| 797 |
*/ |
| 798 |
void |
| 799 |
compute_stages( |
| 800 |
const std::function<VectorType(const double, const VectorType &)> &f, |
| 801 |
const double t, |
| 802 |
const double delta_t, |
| 803 |
const VectorType &y, |
| 804 |
std::vector<VectorType> &f_stages); |
| 805 |
|
| 806 |
/** |
| 807 |
* This parameter is the factor (>1) by which the time step is multiplied |
| 808 |
* when the time stepping can be coarsen. |
| 809 |
*/ |
| 810 |
double coarsen_param; |
| 811 |
|
| 812 |
/** |
| 813 |
* This parameter is the factor (<1) by which the time step is multiplied |
| 814 |
* when the time stepping must be refined. |
| 815 |
*/ |
| 816 |
double refine_param; |
| 817 |
|
| 818 |
/** |
| 819 |
* Smallest time step allowed. |
| 820 |
*/ |
| 821 |
double min_delta_t; |
| 822 |
|
| 823 |
/** |
| 824 |
* Largest time step allowed. |
| 825 |
*/ |
| 826 |
double max_delta_t; |
| 827 |
|
| 828 |
/** |
| 829 |
* Refinement tolerance: if the error estimate is larger than refine_tol, |
| 830 |
* the time step is refined. |
| 831 |
*/ |
| 832 |
double refine_tol; |
| 833 |
|
| 834 |
/** |
| 835 |
* Coarsening tolerance: if the error estimate is smaller than coarse_tol, |
| 836 |
* the time step is coarsen. |
| 837 |
*/ |
| 838 |
double coarsen_tol; |
| 839 |
|
| 840 |
/** |
| 841 |
* If the flag is true, the last stage is the same as the first stage and |
| 842 |
* one evaluation of f can be saved. |
| 843 |
*/ |
| 844 |
bool last_same_as_first = false; |
| 845 |
|
| 846 |
/** |
| 847 |
* Butcher tableau coefficients. |
| 848 |
*/ |
| 849 |
std::vector<double> b1; |
| 850 |
|
| 851 |
/** |
| 852 |
* Butcher tableau coefficients. |
| 853 |
*/ |
| 854 |
std::vector<double> b2; |
| 855 |
|
| 856 |
/** |
| 857 |
* If the last_same_as_first flag is set to true, the last stage is saved |
| 858 |
* and reused as the first stage of the next time step. |
| 859 |
*/ |
| 860 |
VectorType *last_stage = nullptr; |
| 861 |
|
| 862 |
/** |
| 863 |
* Status structure of the object. |
| 864 |
*/ |
| 865 |
Status status; |
| 866 |
}; |
| 867 |
} // namespace TimeStepping |
| 868 |
|
| 869 |
DEAL_II_NAMESPACE_CLOSE |
| 870 |
|
| 871 |
#endif |