| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2023 - 2024 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_petsc_ts_h |
| 16 |
#define dealii_petsc_ts_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#ifdef DEAL_II_WITH_PETSC |
| 21 |
# include <deal.II/base/mpi.h> |
| 22 |
# include <deal.II/base/observer_pointer.h> |
| 23 |
# include <deal.II/base/parameter_handler.h> |
| 24 |
|
| 25 |
# include <deal.II/lac/petsc_matrix_base.h> |
| 26 |
# include <deal.II/lac/petsc_precondition.h> |
| 27 |
# include <deal.II/lac/petsc_vector_base.h> |
| 28 |
|
| 29 |
# include <petscts.h> |
| 30 |
|
| 31 |
# include <exception> |
| 32 |
|
| 33 |
# if defined(DEAL_II_HAVE_CXX20) |
| 34 |
# include <concepts> |
| 35 |
# endif |
| 36 |
|
| 37 |
DEAL_II_NAMESPACE_OPEN |
| 38 |
|
| 39 |
namespace PETScWrappers |
| 40 |
{ |
| 41 |
/** |
| 42 |
* Additional parameters that can be passed to the TimeStepper class. |
| 43 |
*/ |
| 44 |
class TimeStepperData |
| 45 |
{ |
| 46 |
public: |
| 47 |
/** |
| 48 |
* Type that holds real-valued numbers. |
| 49 |
* |
| 50 |
* Used to represent time and norms tolerances. |
| 51 |
*/ |
| 52 |
using real_type = PetscReal; |
| 53 |
|
| 54 |
/** |
| 55 |
* Initialization parameters for TimeStepper. |
| 56 |
* |
| 57 |
* Running parameters: |
| 58 |
* |
| 59 |
* @param options_prefix The string indicating the options prefix for command line customization. |
| 60 |
* @param ts_type The string indicating the PETSc solver type. |
| 61 |
* @param initial_time Initial simulation time. |
| 62 |
* @param final_time Final simulation time. |
| 63 |
* @param initial_step_size Initial step size. |
| 64 |
* @param max_steps Maximum number of steps allowed. |
| 65 |
* @param match_step Whether or not to exactly stop at final time or step over it. |
| 66 |
* @param restart_if_remesh Whether or not to restart the step when remeshing is flagged. |
| 67 |
* |
| 68 |
* Error parameters: |
| 69 |
* |
| 70 |
* @param ts_adapt_type The string indicating the PETSc time step adaptor type. |
| 71 |
* @param minimum_step_size Minimum step size allowed. |
| 72 |
* @param maximum_step_size Maximum step size allowed. |
| 73 |
* @param absolute_tolerance Absolute error tolerance. |
| 74 |
* @param relative_tolerance Relative error tolerance. |
| 75 |
* @param ignore_algebraic_lte Ignore algebraic terms for |
| 76 |
* error computations |
| 77 |
* |
| 78 |
* Note that one between `final_time` or `max_steps` must |
| 79 |
* be specified by the user, otherwise PETSc will complain. |
| 80 |
* Adaptive time stepping is disabled by default. |
| 81 |
* Negative values indicate using PETSc's default. |
| 82 |
* |
| 83 |
* @note All parameters values specified here can be overridden by |
| 84 |
* command line choices. |
| 85 |
* |
| 86 |
* @ingroup PETScWrappers |
| 87 |
*/ |
| 88 |
TimeStepperData( |
| 89 |
// Running parameters |
| 90 |
const std::string &options_prefix = "", |
| 91 |
const std::string &ts_type = "", |
| 92 |
const real_type initial_time = 0.0, |
| 93 |
const real_type final_time = 0.0, |
| 94 |
const real_type initial_step_size = 0.0, |
| 95 |
const int max_steps = -1, |
| 96 |
const bool match_step = false, |
| 97 |
const bool restart_if_remesh = false, |
| 98 |
// Error parameters |
| 99 |
const std::string &ts_adapt_type = "none", |
| 100 |
const real_type minimum_step_size = -1.0, |
| 101 |
const real_type maximum_step_size = -1.0, |
| 102 |
const real_type absolute_tolerance = -1.0, |
| 103 |
const real_type relative_tolerance = -1.0, |
| 104 |
const bool ignore_algebraic_lte = true) |
| 105 |
: options_prefix(options_prefix) |
| 106 |
, ts_type(ts_type) |
| 107 |
, initial_time(initial_time) |
| 108 |
, final_time(final_time) |
| 109 |
, initial_step_size(initial_step_size) |
| 110 |
, max_steps(max_steps) |
| 111 |
, match_step(match_step) |
| 112 |
, restart_if_remesh(restart_if_remesh) |
| 113 |
, ts_adapt_type(ts_adapt_type) |
| 114 |
, minimum_step_size(minimum_step_size) |
| 115 |
, maximum_step_size(maximum_step_size) |
| 116 |
, absolute_tolerance(absolute_tolerance) |
| 117 |
, relative_tolerance(relative_tolerance) |
| 118 |
, ignore_algebraic_lte(ignore_algebraic_lte) |
| 119 |
{} |
| 120 |
|
| 121 |
/** |
| 122 |
* Import parameter values. |
| 123 |
*/ |
| 124 |
void |
| 125 |
add_parameters(ParameterHandler &prm); |
| 126 |
|
| 127 |
/** |
| 128 |
* Options database prefix. |
| 129 |
*/ |
| 130 |
std::string options_prefix; |
| 131 |
|
| 132 |
/** |
| 133 |
* PETSc solver type. |
| 134 |
*/ |
| 135 |
std::string ts_type; |
| 136 |
|
| 137 |
/** |
| 138 |
* Initial time for the DAE. |
| 139 |
*/ |
| 140 |
real_type initial_time; |
| 141 |
|
| 142 |
/** |
| 143 |
* Final time. |
| 144 |
*/ |
| 145 |
real_type final_time; |
| 146 |
|
| 147 |
/** |
| 148 |
* Initial step size. |
| 149 |
* |
| 150 |
* @note Non-positive values are ignored. |
| 151 |
*/ |
| 152 |
real_type initial_step_size; |
| 153 |
|
| 154 |
/** |
| 155 |
* Maximum number of steps to be taken. |
| 156 |
* |
| 157 |
* @note Negative values are ignored. |
| 158 |
*/ |
| 159 |
int max_steps; |
| 160 |
|
| 161 |
/** |
| 162 |
* Flag to indicate to stop exactly at the requested final time. |
| 163 |
*/ |
| 164 |
bool match_step; |
| 165 |
|
| 166 |
/** |
| 167 |
* Flag to indicate to restart the step if remeshing is flagged. |
| 168 |
*/ |
| 169 |
bool restart_if_remesh; |
| 170 |
|
| 171 |
/** |
| 172 |
* PETSc time step adaptor type. |
| 173 |
*/ |
| 174 |
std::string ts_adapt_type; |
| 175 |
|
| 176 |
/** |
| 177 |
* Minimum allowed step size for adaptive time stepping. |
| 178 |
* |
| 179 |
* @note Non-positive values indicate to use PETSc's default. |
| 180 |
*/ |
| 181 |
real_type minimum_step_size; |
| 182 |
|
| 183 |
/** |
| 184 |
* Maximum allowed step size for adaptive time stepping. |
| 185 |
* |
| 186 |
* @note Non-positive values indicate to use PETSc's default. |
| 187 |
*/ |
| 188 |
real_type maximum_step_size; |
| 189 |
|
| 190 |
/** |
| 191 |
* Absolute error tolerance for adaptive time stepping. |
| 192 |
* |
| 193 |
* @note Negative values indicate to use PETSc's default. |
| 194 |
*/ |
| 195 |
real_type absolute_tolerance; |
| 196 |
|
| 197 |
/** |
| 198 |
* Relative error tolerance for adaptive time stepping. |
| 199 |
* |
| 200 |
* @note Negative values indicate to use PETSc's default. |
| 201 |
*/ |
| 202 |
real_type relative_tolerance; |
| 203 |
|
| 204 |
/** |
| 205 |
* Ignore algebraic terms for local truncation error. |
| 206 |
*/ |
| 207 |
bool ignore_algebraic_lte; |
| 208 |
}; |
| 209 |
|
| 210 |
/** |
| 211 |
* Interface to the PETSc TS solver for Ordinary Differential Equations |
| 212 |
* and Differential-Algebraic Equations. The TS solver is described in the |
| 213 |
* [PETSc manual](https://petsc.org/release/manual/ts/). This class is used |
| 214 |
* and extensively discussed in step-86. |
| 215 |
* |
| 216 |
* This class supports two kinds of formulations. |
| 217 |
* The explicit formulation: |
| 218 |
* \f[ |
| 219 |
* \begin{cases} |
| 220 |
* \dot y = G(t,y)\, , \\ |
| 221 |
* y(t_0) = y_0\, , \\ |
| 222 |
* \end{cases} |
| 223 |
* \f] |
| 224 |
* and the implicit formulation: |
| 225 |
* \f[ |
| 226 |
* \begin{cases} |
| 227 |
* F(t,y,\dot y) = 0\, , \\ |
| 228 |
* y(t_0) = y_0\, . \\ |
| 229 |
* \end{cases} |
| 230 |
* \f] |
| 231 |
* |
| 232 |
* The interface to PETSc is realized by means of std::function |
| 233 |
* callbacks like in the SUNDIALS::IDA (which also solves implicit |
| 234 |
* ODES) and SUNDIALS::ARKode classes (which solves a slightly |
| 235 |
* generalized form of the explicit formulation above that also |
| 236 |
* allows for a mass matrix on the left hand side). |
| 237 |
* |
| 238 |
* TimeStepper supports any vector and matrix type having constructors and |
| 239 |
* methods: |
| 240 |
* |
| 241 |
* @code |
| 242 |
* class VectorType : public EnableObserverPointer |
| 243 |
* ... |
| 244 |
* explicit VectorType(Vec); |
| 245 |
* ... |
| 246 |
* Vec & petsc_vector(); |
| 247 |
* ... |
| 248 |
* @endcode |
| 249 |
* |
| 250 |
* @code |
| 251 |
* class MatrixType : public EnableObserverPointer |
| 252 |
* ... |
| 253 |
* explicit MatrixType(Mat); |
| 254 |
* ... |
| 255 |
* Mat & petsc_matrix(); |
| 256 |
* ... |
| 257 |
* @endcode |
| 258 |
* |
| 259 |
* In particular, the supported types are the ones that can *wrap* |
| 260 |
* PETSc's native vector and matrix classes, that are able to modify |
| 261 |
* them in place, and that can return PETSc native types when requested. |
| 262 |
* |
| 263 |
* To use explicit solvers (like for example explicit Runge-Kutta methods), |
| 264 |
* the user only needs to provide the implementation of $G$ via the |
| 265 |
* TimeStepper::explicit_function. For implicit solvers, users have also the |
| 266 |
* alternative of providing the $F$ function via |
| 267 |
* TimeStepper::implicit_function. IMEX methods are also supported by |
| 268 |
* providing both callbacks. |
| 269 |
* |
| 270 |
* The default linearization procedure of an implicit solver instantiated with |
| 271 |
* this class consists in using Jacobian-Free-Newton-Krylov; the action of |
| 272 |
* tangent matrices inside a linear solver process are approximated via |
| 273 |
* matrix-free finite-differencing of the nonlinear residual equations that |
| 274 |
* are ODE-solver specific. For details, consult the [PETSc |
| 275 |
* manual](https://petsc.org/release/manual/snes/#sec-nlmatrixfree). |
| 276 |
* |
| 277 |
* Users can also provide the implementations of the *Jacobians*. This can be |
| 278 |
* accomplished in two ways: |
| 279 |
* - PETSc style using TimeStepper::implicit_jacobian |
| 280 |
* and TimeStepper::explicit_jacobian. |
| 281 |
* - deal.II style using TimeStepper::setup_jacobian and |
| 282 |
* TimeStepper::solve_with_jacobian. |
| 283 |
* The preconditioning matrix can be specified using |
| 284 |
* TimeStepper::set_matrix(). In case both approaches are implemented, the |
| 285 |
* deal.II style will be used. |
| 286 |
* |
| 287 |
* TimeStepper::set_matrices() must be used in case the user wants to provide |
| 288 |
* the iteration matrix of the tangent system in the deal.II style approach, |
| 289 |
* thus replacing the matrix-free linearization. |
| 290 |
* |
| 291 |
* The correctness of the constructed Jacobians passed using |
| 292 |
* TimeStepper::set_matrix() can be checked using |
| 293 |
* @code |
| 294 |
* ./myApp -snes_test_jacobian |
| 295 |
* @endcode |
| 296 |
* See TimeStepper::set_matrix() and TimeStepper::set_matrices() for |
| 297 |
* additional details. |
| 298 |
* |
| 299 |
* The deal.II style approach still allows command line customization, like |
| 300 |
* for example, |
| 301 |
* @code |
| 302 |
* ./myApp -snes_type newtontr -ksp_type cg |
| 303 |
* @endcode |
| 304 |
* in case the user wants to change the default nonlinear solver to |
| 305 |
* a trust region solver and iterate on the tangent system with CG, |
| 306 |
* still using TimeStepper::solve_with_jacobian as a preconditioner. |
| 307 |
* |
| 308 |
* The PETSc style approach has instead the advantage that only the matrix |
| 309 |
* assembly procedure has to be implemented, thus allowing quicker |
| 310 |
* implementations and faster turnaround for experimenting with linear solver |
| 311 |
* preconditioning configurations via command line customizations, like for |
| 312 |
* example, |
| 313 |
* @code |
| 314 |
* ./myApp -ksp_type cg -pc_type gamg |
| 315 |
* @endcode |
| 316 |
* |
| 317 |
* @dealiiConceptRequires{(concepts::is_dealii_petsc_vector_type<VectorType> |
| 318 |
* || std::constructible_from<VectorType,Vec>) && |
| 319 |
* (concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 320 |
* std::constructible_from<PMatrixType,Mat>) && |
| 321 |
* (concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 322 |
* std::constructible_from<AMatrixType, Mat>)} |
| 323 |
* |
| 324 |
* @ingroup PETScWrappers |
| 325 |
*/ |
| 326 |
template <typename VectorType = PETScWrappers::VectorBase, |
| 327 |
typename PMatrixType = PETScWrappers::MatrixBase, |
| 328 |
typename AMatrixType = PMatrixType> |
| 329 |
# if defined(DEAL_II_HAVE_CXX20) && \ |
| 330 |
!defined(DEAL_II_DOXYGEN_DO_NOT_PARSE_REQUIRES_CLAUSES) |
| 331 |
requires((concepts::is_dealii_petsc_vector_type<VectorType> || |
| 332 |
std::constructible_from<VectorType, Vec>) && |
| 333 |
(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 334 |
std::constructible_from<PMatrixType, Mat>) && |
| 335 |
(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 336 |
std::constructible_from<AMatrixType, Mat>)) |
| 337 |
# endif |
| 338 |
class TimeStepper |
| 339 |
{ |
| 340 |
public: |
| 341 |
/** |
| 342 |
* Type that holds real-valued numbers. |
| 343 |
* |
| 344 |
* Used to represent time and norms tolerances. |
| 345 |
*/ |
| 346 |
using real_type = PetscReal; |
| 347 |
|
| 348 |
/** |
| 349 |
* Constructor. |
| 350 |
*/ |
| 351 |
TimeStepper(const TimeStepperData &data = TimeStepperData(), |
| 352 |
const MPI_Comm mpi_comm = PETSC_COMM_WORLD); |
| 353 |
|
| 354 |
/** |
| 355 |
* Destructor. |
| 356 |
*/ |
| 357 |
~TimeStepper(); |
| 358 |
|
| 359 |
/** |
| 360 |
* Conversion operator to gain access to the underlying PETSc type. If you |
| 361 |
* do this, you cut this class off some information it may need, so this |
| 362 |
* conversion operator should only be used if you know what you do. |
| 363 |
*/ |
| 364 |
operator TS() const; |
| 365 |
|
| 366 |
/** |
| 367 |
* Return the PETSc TS object. |
| 368 |
*/ |
| 369 |
TS |
| 370 |
petsc_ts(); |
| 371 |
|
| 372 |
/** |
| 373 |
* Return the underlying MPI communicator. |
| 374 |
*/ |
| 375 |
MPI_Comm |
| 376 |
get_mpi_communicator() const; |
| 377 |
|
| 378 |
/** |
| 379 |
* Reset the solver, it does not change the customization. |
| 380 |
*/ |
| 381 |
void |
| 382 |
reinit(); |
| 383 |
|
| 384 |
/** |
| 385 |
* Reset solver. |
| 386 |
* Change customization according to @p data. |
| 387 |
*/ |
| 388 |
void |
| 389 |
reinit(const TimeStepperData &data); |
| 390 |
|
| 391 |
/** |
| 392 |
* Set the preconditioning matrix only. |
| 393 |
* |
| 394 |
* When used with TimeStepper::setup_jacobian and |
| 395 |
* TimeStepper::solve_with_jacobian, PETSc will approximate the linear |
| 396 |
* system matrix-vector product using an internal matrix-free |
| 397 |
* representation. |
| 398 |
* |
| 399 |
* When used with TimeStepper::implicit_jacobian or |
| 400 |
* TimeStepper::explicit_jacobian, PETSc will use the same matrix for both |
| 401 |
* preconditioning and matrix-vector products. |
| 402 |
*/ |
| 403 |
void |
| 404 |
set_matrix(PMatrixType &P); |
| 405 |
|
| 406 |
/** |
| 407 |
* Set both the linear system matrix and the preconditioning matrix |
| 408 |
* that PETSc will use (can be the same matrix). In this case, the |
| 409 |
* Jacobian-Free-Newton-Krylov approach will not be used. |
| 410 |
*/ |
| 411 |
void |
| 412 |
set_matrices(AMatrixType &A, PMatrixType &P); |
| 413 |
|
| 414 |
/** |
| 415 |
* Return current time. |
| 416 |
*/ |
| 417 |
real_type |
| 418 |
get_time(); |
| 419 |
|
| 420 |
/** |
| 421 |
* Return current time step. |
| 422 |
*/ |
| 423 |
real_type |
| 424 |
get_time_step(); |
| 425 |
|
| 426 |
/** |
| 427 |
* Return current step number. |
| 428 |
*/ |
| 429 |
unsigned int |
| 430 |
get_step_number(); |
| 431 |
|
| 432 |
/** |
| 433 |
* Integrate the differential-algebraic equations starting from @p y. |
| 434 |
* |
| 435 |
* This function returns the final number of computed steps. |
| 436 |
* Upon returning, the @p y vector contains the solution of the DAE at |
| 437 |
* the end time. |
| 438 |
*/ |
| 439 |
unsigned int |
| 440 |
solve(VectorType &y); |
| 441 |
|
| 442 |
/** |
| 443 |
* Integrate the differential-algebraic equations starting from @p y. |
| 444 |
* |
| 445 |
* This function returns the final number of computed steps. |
| 446 |
* Upon returning, the @p y vector contains the solution of the DAE at |
| 447 |
* the end time. |
| 448 |
* |
| 449 |
* Here we also set the matrix to precondition the tangent system. |
| 450 |
*/ |
| 451 |
unsigned int |
| 452 |
solve(VectorType &y, PMatrixType &P); |
| 453 |
|
| 454 |
/** |
| 455 |
* Integrate the differential-algebraic equations starting from @p y. |
| 456 |
* |
| 457 |
* This function returns the final number of computed steps. |
| 458 |
* Upon returning, the @p y vector contains the solution of the DAE at |
| 459 |
* the end time. |
| 460 |
* |
| 461 |
* Here we also set the matrices to describe and precondition |
| 462 |
* the tangent system. |
| 463 |
*/ |
| 464 |
unsigned int |
| 465 |
solve(VectorType &y, AMatrixType &A, PMatrixType &P); |
| 466 |
|
| 467 |
/** |
| 468 |
* Callback for the computation of the implicit residual $F(t, y, \dot y)$. |
| 469 |
* |
| 470 |
* @note This variable represents a |
| 471 |
* @ref GlossUserProvidedCallBack "user provided callback". |
| 472 |
* See there for a description of how to deal with errors and other |
| 473 |
* requirements and conventions. |
| 474 |
*/ |
| 475 |
std::function<void(const real_type t, |
| 476 |
const VectorType &y, |
| 477 |
const VectorType &y_dot, |
| 478 |
VectorType &res)> |
| 479 |
implicit_function; |
| 480 |
|
| 481 |
/** |
| 482 |
* Callback for the computation of the implicit Jacobian |
| 483 |
* $\dfrac{\partial F}{\partial y} + \alpha \dfrac{\partial F}{\partial \dot |
| 484 |
* y}$. |
| 485 |
* |
| 486 |
* All implicit solvers implementations are recast to use the above |
| 487 |
* linearization. The $\alpha$ parameter is time-step and solver-type |
| 488 |
* specific. |
| 489 |
* |
| 490 |
* @note This variable represents a |
| 491 |
* @ref GlossUserProvidedCallBack "user provided callback". |
| 492 |
* See there for a description of how to deal with errors and other |
| 493 |
* requirements and conventions. |
| 494 |
*/ |
| 495 |
std::function<void(const real_type t, |
| 496 |
const VectorType &y, |
| 497 |
const VectorType &y_dot, |
| 498 |
const real_type alpha, |
| 499 |
AMatrixType &A, |
| 500 |
PMatrixType &P)> |
| 501 |
implicit_jacobian; |
| 502 |
|
| 503 |
/** |
| 504 |
* Callback for the computation of the explicit residual $G(t, y)$. |
| 505 |
* |
| 506 |
* @note This variable represents a |
| 507 |
* @ref GlossUserProvidedCallBack "user provided callback". |
| 508 |
* See there for a description of how to deal with errors and other |
| 509 |
* requirements and conventions. |
| 510 |
*/ |
| 511 |
std::function<void(const real_type t, const VectorType &y, VectorType &res)> |
| 512 |
explicit_function; |
| 513 |
|
| 514 |
/** |
| 515 |
* Callback for the computation of the explicit Jacobian $\dfrac{\partial |
| 516 |
* G}{\partial y}$. |
| 517 |
* |
| 518 |
* @note This variable represents a |
| 519 |
* @ref GlossUserProvidedCallBack "user provided callback". |
| 520 |
* See there for a description of how to deal with errors and other |
| 521 |
* requirements and conventions. |
| 522 |
*/ |
| 523 |
std::function<void(const real_type t, |
| 524 |
const VectorType &y, |
| 525 |
AMatrixType &A, |
| 526 |
PMatrixType &P)> |
| 527 |
explicit_jacobian; |
| 528 |
|
| 529 |
/** |
| 530 |
* Callback for monitoring the solution process. |
| 531 |
* |
| 532 |
* This function is called by TimeStepper at the beginning |
| 533 |
* of each time step. |
| 534 |
* |
| 535 |
* @note This variable represents a |
| 536 |
* @ref GlossUserProvidedCallBack "user provided callback". |
| 537 |
* See there for a description of how to deal with errors and other |
| 538 |
* requirements and conventions. |
| 539 |
*/ |
| 540 |
std::function<void(const real_type t, |
| 541 |
const VectorType &y, |
| 542 |
const unsigned int step_number)> |
| 543 |
monitor; |
| 544 |
|
| 545 |
/** |
| 546 |
* Callback for the set up of the Jacobian system. |
| 547 |
* |
| 548 |
* This callback gives full control to users to set up the linearized |
| 549 |
* equations |
| 550 |
* $\dfrac{\partial F}{\partial y} + \alpha \dfrac{\partial F}{\partial \dot |
| 551 |
* y}$. |
| 552 |
* |
| 553 |
* All implicit solvers implementations are recast to use the above |
| 554 |
* linearization. The $\alpha$ parameter is time-step and solver-type |
| 555 |
* specific. |
| 556 |
* |
| 557 |
* Solvers must be provided via TimeStepper::solve_with_jacobian. |
| 558 |
* |
| 559 |
* @note This variable represents a |
| 560 |
* @ref GlossUserProvidedCallBack "user provided callback". |
| 561 |
* See there for a description of how to deal with errors and other |
| 562 |
* requirements and conventions. |
| 563 |
*/ |
| 564 |
std::function<void(const real_type t, |
| 565 |
const VectorType &y, |
| 566 |
const VectorType &ydot, |
| 567 |
const real_type alpha)> |
| 568 |
setup_jacobian; |
| 569 |
|
| 570 |
/** |
| 571 |
* Callback for the solution of the tangent system set up with |
| 572 |
* TimeStepper::setup_jacobian. |
| 573 |
* |
| 574 |
* This is used as a preconditioner inside the Krylov process. |
| 575 |
* |
| 576 |
* @note This variable represents a |
| 577 |
* @ref GlossUserProvidedCallBack "user provided callback". |
| 578 |
* See there for a description of how to deal with errors and other |
| 579 |
* requirements and conventions. |
| 580 |
*/ |
| 581 |
std::function<void(const VectorType &src, VectorType &dst)> |
| 582 |
solve_with_jacobian; |
| 583 |
|
| 584 |
/** |
| 585 |
* Callback to return an index set containing the algebraic components. |
| 586 |
* Algebraic components are degrees of freedom for which the differential |
| 587 |
* equation does not provide a time derivative. This can either be because |
| 588 |
* the degree of freedom is constrained (say, because it is a hanging |
| 589 |
* node, or because it is part of Dirichlet boundary values), or because |
| 590 |
* the differential equation simply does not contain time derivatives |
| 591 |
* for a specific solution variable. An example for the latter case is |
| 592 |
* the pressure in the time dependent Stokes equations, |
| 593 |
* @f{align*}{ |
| 594 |
* \frac{\partial \mathbf u(\mathbf x,t)}{\partial t} |
| 595 |
* - \nu \Delta \mathbf u(\mathbf x,t) + \nabla p(\mathbf x,t) |
| 596 |
* &= \mathbf f(\mathbf x,t), |
| 597 |
* \\ |
| 598 |
* \nabla \cdot \mathbf u(\mathbf x,t) &= 0. |
| 599 |
* @f} |
| 600 |
* The documentation of the SUNDIALS::IDA class has an extensive |
| 601 |
* documentation of algebraic variables as part of differential-algebraic |
| 602 |
* equations. |
| 603 |
* |
| 604 |
* Implementation of this function is optional. If your equation is also |
| 605 |
* algebraic (i.e., it contains algebraic constraints, or Lagrange |
| 606 |
* multipliers), you should implement this function in order to return only |
| 607 |
* these components of your system. |
| 608 |
* |
| 609 |
* @note This variable represents a |
| 610 |
* @ref GlossUserProvidedCallBack "user provided callback". |
| 611 |
* See there for a description of how to deal with errors and other |
| 612 |
* requirements and conventions. |
| 613 |
*/ |
| 614 |
std::function<IndexSet()> algebraic_components; |
| 615 |
|
| 616 |
/** |
| 617 |
* @deprecated This callback is equivalent to `update_constrained_components`, but is |
| 618 |
* deprecated. Use `update_constrained_components` instead. |
| 619 |
*/ |
| 620 |
DEAL_II_DEPRECATED |
| 621 |
std::function<void(const real_type t, VectorType &y)> distribute; |
| 622 |
|
| 623 |
/** |
| 624 |
* Callback to set the values of constrained components to their correct |
| 625 |
* values. Constrained components are a subset of the algebraic |
| 626 |
* components discussed in the documentation of the |
| 627 |
* TimeStepper::algebraic_components callback. In practice, the constrained |
| 628 |
* components are typically hanging nodes and degrees of freedom |
| 629 |
* constrained by Dirichlet boundary conditions. |
| 630 |
* |
| 631 |
* Implementation of this function is optional. |
| 632 |
* It is called at the end of each successful stage. |
| 633 |
* The same functionality can be equivalently implemented in |
| 634 |
* TimeStepper::solve_with_jacobian. |
| 635 |
* |
| 636 |
* @note This variable represents a |
| 637 |
* @ref GlossUserProvidedCallBack "user provided callback". |
| 638 |
* See there for a description of how to deal with errors and other |
| 639 |
* requirements and conventions. |
| 640 |
*/ |
| 641 |
std::function<void(const real_type t, VectorType &y)> |
| 642 |
update_constrained_components; |
| 643 |
|
| 644 |
/** |
| 645 |
* @deprecated This callback is equivalent to `decide_and_prepare_for_remeshing` |
| 646 |
* except that it returns the decision whether or not to stop |
| 647 |
* operations via the last reference argument of the function |
| 648 |
* object instead of a plain return value. This callback is |
| 649 |
* deprecated. Use `decide_and_prepare_for_remeshing` instead. |
| 650 |
*/ |
| 651 |
DEAL_II_DEPRECATED |
| 652 |
std::function<void(const real_type t, |
| 653 |
const unsigned int step, |
| 654 |
const VectorType &y, |
| 655 |
bool &resize)> |
| 656 |
decide_for_coarsening_and_refinement; |
| 657 |
|
| 658 |
/** |
| 659 |
* A callback that returns whether or not to stop time stepping at the |
| 660 |
* current moment for mesh adaptation. If the callback returns `true`, |
| 661 |
* then the time stepper stops time integration for now, saves some state, |
| 662 |
* calls the `interpolate` callback, and then resumes time integration. |
| 663 |
* Either in the current callback or the `interpolate` callback, the |
| 664 |
* user code needs to perform the mesh refinement. |
| 665 |
* |
| 666 |
* Implementation of this function is optional. The callback |
| 667 |
* must return `true` if mesh adaption is to be performed, `false` |
| 668 |
* otherwise. |
| 669 |
* The @p y vector contains the current solution, @p t the current time, |
| 670 |
* @ step the step number. |
| 671 |
* |
| 672 |
* @note This variable represents a |
| 673 |
* @ref GlossUserProvidedCallBack "user provided callback". |
| 674 |
* See there for a description of how to deal with errors and other |
| 675 |
* requirements and conventions. |
| 676 |
*/ |
| 677 |
std::function< |
| 678 |
bool(const real_type t, const unsigned int step, const VectorType &y)> |
| 679 |
decide_and_prepare_for_remeshing; |
| 680 |
|
| 681 |
/** |
| 682 |
* @deprecated This callback is equivalent to `transfer_solution_vectors_to_new_mesh`, but is |
| 683 |
* deprecated. Use `transfer_solution_vectors_to_new_mesh` instead. |
| 684 |
*/ |
| 685 |
DEAL_II_DEPRECATED |
| 686 |
std::function<void(const std::vector<VectorType> &all_in, |
| 687 |
std::vector<VectorType> &all_out)> |
| 688 |
interpolate; |
| 689 |
|
| 690 |
/** |
| 691 |
* Callback to perform mesh adaptation and transfer solution vectors |
| 692 |
* from the old to the new mesh. |
| 693 |
* |
| 694 |
* Implementation of this function is mandatory if |
| 695 |
* TimeStepper::decide_and_prepare_for_remeshing is used. |
| 696 |
* This function must perform mesh adaption and interpolate the discrete |
| 697 |
* functions that are stored in @p all_in onto the refined and/or coarsened grid. |
| 698 |
* The output vectors must be sized correctly within this callback. |
| 699 |
* |
| 700 |
* @note This variable represents a |
| 701 |
* @ref GlossUserProvidedCallBack "user provided callback". |
| 702 |
* See there for a description of how to deal with errors and other |
| 703 |
* requirements and conventions. |
| 704 |
*/ |
| 705 |
std::function<void(const real_type t, |
| 706 |
const std::vector<VectorType> &all_in, |
| 707 |
std::vector<VectorType> &all_out)> |
| 708 |
transfer_solution_vectors_to_new_mesh; |
| 709 |
|
| 710 |
private: |
| 711 |
/** |
| 712 |
* The PETSc object. |
| 713 |
*/ |
| 714 |
TS ts; |
| 715 |
|
| 716 |
/** |
| 717 |
* Pointers to the internal PETSc matrix objects. |
| 718 |
*/ |
| 719 |
ObserverPointer<AMatrixType, TimeStepper> A; |
| 720 |
ObserverPointer<PMatrixType, TimeStepper> P; |
| 721 |
|
| 722 |
/** |
| 723 |
* Object to apply solve_with_jacobian. |
| 724 |
*/ |
| 725 |
PreconditionShell solve_with_jacobian_pc; |
| 726 |
|
| 727 |
/** |
| 728 |
* This flag is used to decide whether or not to restart the step if |
| 729 |
* remeshing has been performed |
| 730 |
*/ |
| 731 |
bool restart_if_remesh; |
| 732 |
|
| 733 |
/** |
| 734 |
* This flag is set when changing the customization and used within solve. |
| 735 |
*/ |
| 736 |
bool need_dae_tolerances; |
| 737 |
|
| 738 |
/** |
| 739 |
* This flag is used to support versions of PETSc older than 3.13. |
| 740 |
*/ |
| 741 |
bool need_dummy_assemble; |
| 742 |
|
| 743 |
/** |
| 744 |
* A pointer to any exception that may have been thrown in user-defined |
| 745 |
* call-backs and that we have to deal after the KINSOL function we call |
| 746 |
* has returned. |
| 747 |
*/ |
| 748 |
mutable std::exception_ptr pending_exception; |
| 749 |
|
| 750 |
/** |
| 751 |
* Internal data to handle recoverable errors. |
| 752 |
*/ |
| 753 |
bool error_in_function; |
| 754 |
|
| 755 |
/** |
| 756 |
* Setup callbacks. |
| 757 |
* |
| 758 |
* This function is called inside TimeStepper::solve routines and does |
| 759 |
* not need to be called by the user. It is used to reinitialize the |
| 760 |
* solver if mesh adaption has been performed. |
| 761 |
*/ |
| 762 |
void |
| 763 |
setup_callbacks(); |
| 764 |
|
| 765 |
/** |
| 766 |
* Setup algebraic constraints. |
| 767 |
* |
| 768 |
* This function is called inside TimeStepper::solve routines and does |
| 769 |
* not need to be called by the user. It is used to reinitialize the |
| 770 |
* solver if mesh adaption has been performed. |
| 771 |
*/ |
| 772 |
void |
| 773 |
setup_algebraic_constraints(const VectorType &y); |
| 774 |
}; |
| 775 |
|
| 776 |
} // namespace PETScWrappers |
| 777 |
|
| 778 |
DEAL_II_NAMESPACE_CLOSE |
| 779 |
|
| 780 |
#else |
| 781 |
|
| 782 |
// Make sure the scripts that create the C++20 module input files have |
| 783 |
// something to latch on if the preprocessor #ifdef above would |
| 784 |
// otherwise lead to an empty content of the file. |
| 785 |
DEAL_II_NAMESPACE_OPEN |
| 786 |
DEAL_II_NAMESPACE_CLOSE |
| 787 |
|
| 788 |
#endif // DEAL_II_WITH_PETSC |
| 789 |
|
| 790 |
#endif |