| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2023 - 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_petsc_ts_templates_h |
| 16 |
#define dealii_petsc_ts_templates_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#ifdef DEAL_II_WITH_PETSC |
| 21 |
# include <deal.II/base/exceptions.h> |
| 22 |
# include <deal.II/base/mpi_stub.h> |
| 23 |
|
| 24 |
# include <deal.II/lac/petsc_precondition.h> |
| 25 |
# include <deal.II/lac/petsc_ts.h> |
| 26 |
|
| 27 |
# include <petscdm.h> |
| 28 |
# include <petscerror.h> |
| 29 |
# include <petscts.h> |
| 30 |
|
| 31 |
DEAL_II_NAMESPACE_OPEN |
| 32 |
|
| 33 |
// Shorthand notation for PETSc error codes. |
| 34 |
// This is used in deal.II code to raise exceptions |
| 35 |
# define AssertPETSc(code) \ |
| 36 |
do \ |
| 37 |
{ \ |
| 38 |
PetscErrorCode ierr = (code); \ |
| 39 |
AssertThrow(ierr == 0, ExcPETScError(ierr)); \ |
| 40 |
} \ |
| 41 |
while (false) |
| 42 |
|
| 43 |
// Macro to wrap PETSc inside callbacks. |
| 44 |
// This is used to raise "PETSc" exceptions, i.e. |
| 45 |
// start a cascade of errors inside PETSc |
| 46 |
# ifndef PetscCall |
| 47 |
# define PetscCall(code) \ |
| 48 |
do \ |
| 49 |
{ \ |
| 50 |
PetscErrorCode ierr = (code); \ |
| 51 |
CHKERRQ(ierr); \ |
| 52 |
} \ |
| 53 |
while (false) |
| 54 |
# define undefPetscCall |
| 55 |
# endif |
| 56 |
|
| 57 |
namespace PETScWrappers |
| 58 |
{ |
| 59 |
/** |
| 60 |
* A function that calls the function object given by its first argument |
| 61 |
* with the set of arguments following at the end. If the call returns |
| 62 |
* regularly, the current function returns zero to indicate success. If |
| 63 |
* the call fails with an exception, then the current function returns with |
| 64 |
* an error code of -1. In that case, the exception thrown by @p f is |
| 65 |
* captured and @p eptr is set to the exception. In case of success, |
| 66 |
* @p eptr is set to `nullptr`. |
| 67 |
* |
| 68 |
* If the user callback fails with a recoverable exception, then (i) if |
| 69 |
* @p recoverable_action is set, execute it, eat the exception, and return |
| 70 |
* zero; (ii) if @p recoverable_action is an empty function object, store the |
| 71 |
* exception and return -1. |
| 72 |
*/ |
| 73 |
template <typename F, typename... Args> |
| 74 |
int |
| 75 |
call_and_possibly_capture_ts_exception( |
| 76 |
const F &f, |
| 77 |
std::exception_ptr &eptr, |
| 78 |
const std::function<void()> &recoverable_action, |
| 79 |
Args &&...args) |
| 80 |
{ |
| 81 |
// See whether there is already something in the exception pointer |
| 82 |
// variable. There is no reason why this should be so, and |
| 83 |
// we should probably bail out: |
| 84 |
AssertThrow(eptr == nullptr, ExcInternalError()); |
| 85 |
|
| 86 |
// Call the function and if that succeeds, return zero: |
| 87 |
try |
| 88 |
{ |
| 89 |
f(std::forward<Args>(args)...); |
| 90 |
eptr = nullptr; |
| 91 |
return 0; |
| 92 |
} |
| 93 |
// In case of a recoverable exception call the action if present: |
| 94 |
catch (const RecoverableUserCallbackError &) |
| 95 |
{ |
| 96 |
if (recoverable_action) |
| 97 |
{ |
| 98 |
// recover and eat exception |
| 99 |
recoverable_action(); |
| 100 |
eptr = nullptr; |
| 101 |
return 0; |
| 102 |
} |
| 103 |
else |
| 104 |
{ |
| 105 |
// No action provided. |
| 106 |
// capture exception and return -1 |
| 107 |
eptr = std::current_exception(); |
| 108 |
return -1; |
| 109 |
} |
| 110 |
} |
| 111 |
// In case of an exception, capture the exception and |
| 112 |
// return -1: |
| 113 |
catch (...) |
| 114 |
{ |
| 115 |
eptr = std::current_exception(); |
| 116 |
return -1; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 123 |
DEAL_II_CXX20_REQUIRES( |
| 124 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 125 |
std::constructible_from< |
| 126 |
VectorType, |
| 127 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 128 |
std::constructible_from< |
| 129 |
PMatrixType, |
| 130 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 131 |
std::constructible_from<AMatrixType, Mat>)) |
| 132 |
TimeStepper<VectorType, PMatrixType, AMatrixType>::TimeStepper( |
| 133 |
const TimeStepperData &data, |
| 134 |
const MPI_Comm mpi_comm) |
| 135 |
: solve_with_jacobian_pc(mpi_comm) |
| 136 |
, pending_exception(nullptr) |
| 137 |
{ |
| 138 |
AssertPETSc(TSCreate(mpi_comm, &ts)); |
| 139 |
AssertPETSc(TSSetApplicationContext(ts, this)); |
| 140 |
reinit(data); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 146 |
DEAL_II_CXX20_REQUIRES( |
| 147 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 148 |
std::constructible_from< |
| 149 |
VectorType, |
| 150 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 151 |
std::constructible_from< |
| 152 |
PMatrixType, |
| 153 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 154 |
std::constructible_from<AMatrixType, Mat>)) |
| 155 |
TimeStepper<VectorType, PMatrixType, AMatrixType>::~TimeStepper() |
| 156 |
{ |
| 157 |
AssertPETSc(PetscObjectComposeFunction((PetscObject)ts, |
| 158 |
"__dealii_ts_resize_setup__", |
| 159 |
nullptr)); |
| 160 |
AssertPETSc(PetscObjectComposeFunction((PetscObject)ts, |
| 161 |
"__dealii_ts_resize_transfer__", |
| 162 |
nullptr)); |
| 163 |
AssertPETSc(TSDestroy(&ts)); |
| 164 |
|
| 165 |
Assert(pending_exception == nullptr, ExcInternalError()); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 171 |
DEAL_II_CXX20_REQUIRES( |
| 172 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 173 |
std::constructible_from< |
| 174 |
VectorType, |
| 175 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 176 |
std::constructible_from< |
| 177 |
PMatrixType, |
| 178 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 179 |
std::constructible_from<AMatrixType, Mat>)) |
| 180 |
TimeStepper<VectorType, PMatrixType, AMatrixType>::operator TS() const |
| 181 |
{ |
| 182 |
return ts; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 188 |
DEAL_II_CXX20_REQUIRES( |
| 189 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 190 |
std::constructible_from< |
| 191 |
VectorType, |
| 192 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 193 |
std::constructible_from< |
| 194 |
PMatrixType, |
| 195 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 196 |
std::constructible_from<AMatrixType, Mat>)) |
| 197 |
TS TimeStepper<VectorType, PMatrixType, AMatrixType>::petsc_ts() |
| 198 |
{ |
| 199 |
return ts; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 205 |
DEAL_II_CXX20_REQUIRES( |
| 206 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 207 |
std::constructible_from< |
| 208 |
VectorType, |
| 209 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 210 |
std::constructible_from< |
| 211 |
PMatrixType, |
| 212 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 213 |
std::constructible_from<AMatrixType, Mat>)) |
| 214 |
inline MPI_Comm |
| 215 |
TimeStepper<VectorType, PMatrixType, AMatrixType>::get_mpi_communicator() |
| 216 |
const |
| 217 |
{ |
| 218 |
return PetscObjectComm(reinterpret_cast<PetscObject>(ts)); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 224 |
DEAL_II_CXX20_REQUIRES( |
| 225 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 226 |
std::constructible_from< |
| 227 |
VectorType, |
| 228 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 229 |
std::constructible_from< |
| 230 |
PMatrixType, |
| 231 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 232 |
std::constructible_from<AMatrixType, Mat>)) |
| 233 |
typename TimeStepper<VectorType, PMatrixType, AMatrixType>::real_type |
| 234 |
TimeStepper<VectorType, PMatrixType, AMatrixType>::get_time() |
| 235 |
{ |
| 236 |
PetscReal t; |
| 237 |
AssertPETSc(TSGetTime(ts, &t)); |
| 238 |
return t; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 244 |
DEAL_II_CXX20_REQUIRES( |
| 245 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 246 |
std::constructible_from< |
| 247 |
VectorType, |
| 248 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 249 |
std::constructible_from< |
| 250 |
PMatrixType, |
| 251 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 252 |
std::constructible_from<AMatrixType, Mat>)) |
| 253 |
unsigned int TimeStepper<VectorType, PMatrixType, AMatrixType>:: |
| 254 |
get_step_number() |
| 255 |
{ |
| 256 |
return ts_get_step_number(ts); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 262 |
DEAL_II_CXX20_REQUIRES( |
| 263 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 264 |
std::constructible_from< |
| 265 |
VectorType, |
| 266 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 267 |
std::constructible_from< |
| 268 |
PMatrixType, |
| 269 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 270 |
std::constructible_from<AMatrixType, Mat>)) |
| 271 |
typename TimeStepper<VectorType, PMatrixType, AMatrixType>::real_type |
| 272 |
TimeStepper<VectorType, PMatrixType, AMatrixType>::get_time_step() |
| 273 |
{ |
| 274 |
PetscReal dt; |
| 275 |
|
| 276 |
AssertPETSc(TSGetTimeStep(ts, &dt)); |
| 277 |
return dt; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 283 |
DEAL_II_CXX20_REQUIRES( |
| 284 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 285 |
std::constructible_from< |
| 286 |
VectorType, |
| 287 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 288 |
std::constructible_from< |
| 289 |
PMatrixType, |
| 290 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 291 |
std::constructible_from<AMatrixType, Mat>)) |
| 292 |
void TimeStepper<VectorType, PMatrixType, AMatrixType>::reinit() |
| 293 |
{ |
| 294 |
AssertPETSc(TSReset(ts)); |
| 295 |
|
| 296 |
// By default we always allow recovery from failed nonlinear solves |
| 297 |
AssertPETSc(TSSetMaxSNESFailures(ts, -1)); |
| 298 |
|
| 299 |
// By default we do not want PETSc to return a nonzero error code from |
| 300 |
// TSSolve |
| 301 |
AssertPETSc(TSSetErrorIfStepFails(ts, PETSC_FALSE)); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 307 |
DEAL_II_CXX20_REQUIRES( |
| 308 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 309 |
std::constructible_from< |
| 310 |
VectorType, |
| 311 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 312 |
std::constructible_from< |
| 313 |
PMatrixType, |
| 314 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 315 |
std::constructible_from<AMatrixType, Mat>)) |
| 316 |
void TimeStepper<VectorType, PMatrixType, AMatrixType>::reinit( |
| 317 |
const TimeStepperData &data) |
| 318 |
{ |
| 319 |
reinit(); |
| 320 |
|
| 321 |
// Solver type |
| 322 |
if (data.ts_type.size()) |
| 323 |
AssertPETSc(TSSetType(ts, data.ts_type.c_str())); |
| 324 |
|
| 325 |
// Options prefix |
| 326 |
if (data.options_prefix.size()) |
| 327 |
AssertPETSc(TSSetOptionsPrefix(ts, data.options_prefix.c_str())); |
| 328 |
|
| 329 |
// Time and steps limits |
| 330 |
AssertPETSc(TSSetTime(ts, data.initial_time)); |
| 331 |
if (data.final_time > data.initial_time) |
| 332 |
ts_set_max_time(ts, data.final_time); |
| 333 |
if (data.initial_step_size > 0.0) |
| 334 |
AssertPETSc(TSSetTimeStep(ts, data.initial_step_size)); |
| 335 |
if (data.max_steps >= 0) |
| 336 |
ts_set_max_steps(ts, data.max_steps); |
| 337 |
|
| 338 |
// Decide how to end the integration. Either stepover the final time or |
| 339 |
// match it. |
| 340 |
AssertPETSc(TSSetExactFinalTime(ts, |
| 341 |
data.match_step ? |
| 342 |
TS_EXACTFINALTIME_MATCHSTEP : |
| 343 |
TS_EXACTFINALTIME_STEPOVER)); |
| 344 |
|
| 345 |
// Store the boolean to be passed to TSSetResize. |
| 346 |
this->restart_if_remesh = data.restart_if_remesh; |
| 347 |
|
| 348 |
// Adaptive tolerances |
| 349 |
const PetscReal atol = data.absolute_tolerance > 0.0 ? |
| 350 |
data.absolute_tolerance : |
| 351 |
static_cast<PetscReal>(PETSC_DEFAULT); |
| 352 |
const PetscReal rtol = data.relative_tolerance > 0.0 ? |
| 353 |
data.relative_tolerance : |
| 354 |
static_cast<PetscReal>(PETSC_DEFAULT); |
| 355 |
AssertPETSc(TSSetTolerances(ts, atol, nullptr, rtol, nullptr)); |
| 356 |
|
| 357 |
// At this point we do not know the problem size so we cannot |
| 358 |
// set variable tolerances for differential and algebratic equations |
| 359 |
// Store this value and use it during solve. |
| 360 |
this->need_dae_tolerances = data.ignore_algebraic_lte; |
| 361 |
|
| 362 |
// Adaptive time stepping |
| 363 |
TSAdapt tsadapt; |
| 364 |
AssertPETSc(TSGetAdapt(ts, &tsadapt)); |
| 365 |
AssertPETSc(TSAdaptSetType(tsadapt, data.ts_adapt_type.c_str())); |
| 366 |
|
| 367 |
// As of 3.19, PETSc does not propagate options prefixes to the |
| 368 |
// adaptors. |
| 369 |
if (data.options_prefix.size()) |
| 370 |
AssertPETSc( |
| 371 |
TSAdaptSetOptionsPrefix(tsadapt, data.options_prefix.c_str())); |
| 372 |
|
| 373 |
// Time step limits |
| 374 |
const PetscReal hmin = data.minimum_step_size > 0.0 ? |
| 375 |
data.minimum_step_size : |
| 376 |
static_cast<PetscReal>(PETSC_DEFAULT); |
| 377 |
const PetscReal hmax = data.maximum_step_size > 0.0 ? |
| 378 |
data.maximum_step_size : |
| 379 |
static_cast<PetscReal>(PETSC_DEFAULT); |
| 380 |
AssertPETSc(TSAdaptSetStepLimits(tsadapt, hmin, hmax)); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 386 |
DEAL_II_CXX20_REQUIRES( |
| 387 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 388 |
std::constructible_from< |
| 389 |
VectorType, |
| 390 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 391 |
std::constructible_from< |
| 392 |
PMatrixType, |
| 393 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 394 |
std::constructible_from<AMatrixType, Mat>)) |
| 395 |
void TimeStepper<VectorType, PMatrixType, AMatrixType>::set_matrix( |
| 396 |
PMatrixType &P) |
| 397 |
{ |
| 398 |
this->A = nullptr; |
| 399 |
this->P = &P; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 405 |
DEAL_II_CXX20_REQUIRES( |
| 406 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 407 |
std::constructible_from< |
| 408 |
VectorType, |
| 409 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 410 |
std::constructible_from< |
| 411 |
PMatrixType, |
| 412 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 413 |
std::constructible_from<AMatrixType, Mat>)) |
| 414 |
void TimeStepper<VectorType, PMatrixType, AMatrixType>::set_matrices( |
| 415 |
AMatrixType &A, |
| 416 |
PMatrixType &P) |
| 417 |
{ |
| 418 |
this->A = &A; |
| 419 |
this->P = &P; |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 425 |
DEAL_II_CXX20_REQUIRES( |
| 426 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 427 |
std::constructible_from< |
| 428 |
VectorType, |
| 429 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 430 |
std::constructible_from< |
| 431 |
PMatrixType, |
| 432 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 433 |
std::constructible_from<AMatrixType, Mat>)) |
| 434 |
void TimeStepper<VectorType, PMatrixType, AMatrixType>::setup_callbacks() |
| 435 |
{ |
| 436 |
const auto ts_resize_setup = |
| 437 |
[](TS ts, PetscInt it, PetscReal t, Vec x, PetscBool *flg, void *ctx) |
| 438 |
-> PetscErrorCode { |
| 439 |
PetscFunctionBeginUser; |
| 440 |
auto user = static_cast<TimeStepper *>(ctx); |
| 441 |
|
| 442 |
VectorType xdealii(x); |
| 443 |
bool resize = false; |
| 444 |
|
| 445 |
const int lineno = __LINE__; |
| 446 |
const int err = call_and_possibly_capture_ts_exception( |
| 447 |
(user->decide_and_prepare_for_remeshing ? |
| 448 |
[&](const real_type t, |
| 449 |
const unsigned int step, |
| 450 |
const VectorType &y, |
| 451 |
bool &resize) { |
| 452 |
resize = user->decide_and_prepare_for_remeshing(t, step, y); |
| 453 |
} : |
| 454 |
user->decide_for_coarsening_and_refinement), |
| 455 |
user->pending_exception, |
| 456 |
{}, |
| 457 |
t, |
| 458 |
it, |
| 459 |
xdealii, |
| 460 |
resize); |
| 461 |
*flg = resize ? PETSC_TRUE : PETSC_FALSE; |
| 462 |
if (err) |
| 463 |
return PetscError( |
| 464 |
PetscObjectComm((PetscObject)ts), |
| 465 |
lineno + 1, |
| 466 |
"decide_and_prepare_for_remeshing", |
| 467 |
__FILE__, |
| 468 |
PETSC_ERR_LIB, |
| 469 |
PETSC_ERROR_INITIAL, |
| 470 |
"Failure in ts_resize_setup from dealii::PETScWrappers::TimeStepper"); |
| 471 |
|
| 472 |
PetscFunctionReturn(PETSC_SUCCESS); |
| 473 |
}; |
| 474 |
|
| 475 |
const auto ts_resize_transfer = [](TS ts, |
| 476 |
PetscInt nv, |
| 477 |
Vec vin[], |
| 478 |
Vec vout[], |
| 479 |
void *ctx) -> PetscErrorCode { |
| 480 |
PetscFunctionBeginUser; |
| 481 |
auto user = static_cast<TimeStepper *>(ctx); |
| 482 |
|
| 483 |
std::vector<VectorType> all_in, all_out; |
| 484 |
for (PetscInt i = 0; i < nv; i++) |
| 485 |
{ |
| 486 |
all_in.push_back(VectorType(vin[i])); |
| 487 |
} |
| 488 |
|
| 489 |
const int lineno = __LINE__; |
| 490 |
const int err = call_and_possibly_capture_ts_exception( |
| 491 |
(user->transfer_solution_vectors_to_new_mesh ? |
| 492 |
// If we can, call the new callback |
| 493 |
user->transfer_solution_vectors_to_new_mesh : |
| 494 |
// otherwise, use the old one where we just ignore the time: |
| 495 |
[user](const real_type /*t*/, |
| 496 |
const std::vector<VectorType> &all_in, |
| 497 |
std::vector<VectorType> &all_out) { |
| 498 |
user->interpolate(all_in, all_out); |
| 499 |
}), |
| 500 |
user->pending_exception, |
| 501 |
{}, |
| 502 |
user->get_time(), |
| 503 |
all_in, |
| 504 |
all_out); |
| 505 |
if (err) |
| 506 |
return PetscError( |
| 507 |
PetscObjectComm((PetscObject)ts), |
| 508 |
lineno + 1, |
| 509 |
"interpolate", |
| 510 |
__FILE__, |
| 511 |
PETSC_ERR_LIB, |
| 512 |
PETSC_ERROR_INITIAL, |
| 513 |
"Failure in ts_resize_transfer from dealii::PETScWrappers::TimeStepper"); |
| 514 |
|
| 515 |
if (all_out.size() != all_in.size()) |
| 516 |
SETERRQ(PetscObjectComm((PetscObject)ts), |
| 517 |
PETSC_ERR_LIB, |
| 518 |
"Broken all_out"); |
| 519 |
|
| 520 |
for (PetscInt i = 0; i < nv; i++) |
| 521 |
{ |
| 522 |
vout[i] = all_out[i].petsc_vector(); |
| 523 |
PetscCall(PetscObjectReference((PetscObject)vout[i])); |
| 524 |
} |
| 525 |
|
| 526 |
try |
| 527 |
{ |
| 528 |
user->setup_callbacks(); |
| 529 |
} |
| 530 |
catch (...) |
| 531 |
{ |
| 532 |
SETERRQ(PetscObjectComm((PetscObject)ts), |
| 533 |
PETSC_ERR_LIB, |
| 534 |
"Exception in setup_callbacks"); |
| 535 |
} |
| 536 |
|
| 537 |
try |
| 538 |
{ |
| 539 |
user->setup_algebraic_constraints(all_out[0]); |
| 540 |
} |
| 541 |
catch (...) |
| 542 |
{ |
| 543 |
SETERRQ(PetscObjectComm((PetscObject)ts), |
| 544 |
PETSC_ERR_LIB, |
| 545 |
"Exception in setup_algebraic_constraints"); |
| 546 |
} |
| 547 |
PetscFunctionReturn(PETSC_SUCCESS); |
| 548 |
}; |
| 549 |
|
| 550 |
const auto ts_poststep_amr = [](TS ts) -> PetscErrorCode { |
| 551 |
PetscFunctionBeginUser; |
| 552 |
void *ctx; |
| 553 |
PetscCall(TSGetApplicationContext(ts, &ctx)); |
| 554 |
auto user = static_cast<TimeStepper *>(ctx); |
| 555 |
|
| 556 |
using transfer_setup_fn = |
| 557 |
PetscErrorCode (*)(TS, PetscInt, PetscReal, Vec, PetscBool *, void *); |
| 558 |
using transfer_fn = |
| 559 |
PetscErrorCode (*)(TS, PetscInt, Vec[], Vec[], void *); |
| 560 |
transfer_setup_fn transfer_setup; |
| 561 |
transfer_fn transfer; |
| 562 |
|
| 563 |
AssertPETSc(PetscObjectQueryFunction((PetscObject)ts, |
| 564 |
"__dealii_ts_resize_setup__", |
| 565 |
&transfer_setup)); |
| 566 |
AssertPETSc(PetscObjectQueryFunction((PetscObject)ts, |
| 567 |
"__dealii_ts_resize_transfer__", |
| 568 |
&transfer)); |
| 569 |
|
| 570 |
PetscBool resize = PETSC_FALSE; |
| 571 |
Vec x; |
| 572 |
PetscCall(TSGetSolution(ts, &x)); |
| 573 |
PetscCall(PetscObjectReference((PetscObject)x)); |
| 574 |
PetscCall(transfer_setup( |
| 575 |
ts, user->get_step_number(), user->get_time(), x, &resize, user)); |
| 576 |
|
| 577 |
if (resize) |
| 578 |
{ |
| 579 |
Vec new_x; |
| 580 |
|
| 581 |
user->reinit(); |
| 582 |
|
| 583 |
// Reinitialize DM. Currently it is not possible to do with public API |
| 584 |
ts_reset_dm(ts); |
| 585 |
|
| 586 |
PetscCall(transfer(ts, 1, &x, &new_x, user)); |
| 587 |
PetscCall(TSSetSolution(ts, new_x)); |
| 588 |
PetscCall(VecDestroy(&new_x)); |
| 589 |
} |
| 590 |
# if DEAL_II_PETSC_VERSION_LT(3, 17, 0) |
| 591 |
// Older versions of PETSc assume that the user does not |
| 592 |
// change the solution vector during TSPostStep |
| 593 |
// We "fix" it by taking a reference to the object and |
| 594 |
// increment its state so that the time stepper is restarted |
| 595 |
// properly. |
| 596 |
AssertPETSc(PetscObjectCompose((PetscObject)ts, |
| 597 |
"__dealii_ts_resize_bug__", |
| 598 |
(PetscObject)x)); |
| 599 |
petsc_increment_state_counter(x); |
| 600 |
# endif |
| 601 |
PetscCall(VecDestroy(&x)); |
| 602 |
PetscFunctionReturn(PETSC_SUCCESS); |
| 603 |
}; |
| 604 |
|
| 605 |
const auto ts_prestage = [](TS ts, PetscReal) -> PetscErrorCode { |
| 606 |
PetscFunctionBeginUser; |
| 607 |
void *ctx; |
| 608 |
PetscCall(TSGetApplicationContext(ts, &ctx)); |
| 609 |
auto user = static_cast<TimeStepper *>(ctx); |
| 610 |
user->error_in_function = false; |
| 611 |
|
| 612 |
// only in case we are using an implicit solver |
| 613 |
if (ts_has_snes(ts)) |
| 614 |
{ |
| 615 |
SNES snes; |
| 616 |
PetscCall(TSGetSNES(ts, &snes)); |
| 617 |
snes_reset_domain_flags(snes); |
| 618 |
} |
| 619 |
PetscFunctionReturn(PETSC_SUCCESS); |
| 620 |
}; |
| 621 |
|
| 622 |
const auto ts_poststage = |
| 623 |
[](TS ts, PetscReal t, PetscInt i, Vec *xx) -> PetscErrorCode { |
| 624 |
PetscFunctionBeginUser; |
| 625 |
void *ctx; |
| 626 |
PetscCall(TSGetApplicationContext(ts, &ctx)); |
| 627 |
auto user = static_cast<TimeStepper *>(ctx); |
| 628 |
|
| 629 |
VectorType xdealii(xx[i]); |
| 630 |
|
| 631 |
const int lineno = __LINE__; |
| 632 |
const int err = call_and_possibly_capture_ts_exception( |
| 633 |
// Call the user-provided callback. Use the new name if used, |
| 634 |
// or the legacy name otherwise. |
| 635 |
(user->update_constrained_components ? |
| 636 |
user->update_constrained_components : |
| 637 |
user->distribute), |
| 638 |
user->pending_exception, |
| 639 |
[user, ts]() -> void { |
| 640 |
user->error_in_function = true; |
| 641 |
|
| 642 |
SNES snes; |
| 643 |
AssertPETSc(TSGetSNES(ts, &snes)); |
| 644 |
AssertPETSc(SNESSetFunctionDomainError(snes)); |
| 645 |
}, |
| 646 |
t, |
| 647 |
xdealii); |
| 648 |
if (err) |
| 649 |
return PetscError( |
| 650 |
PetscObjectComm((PetscObject)ts), |
| 651 |
lineno + 1, |
| 652 |
"update_constrained_components", |
| 653 |
__FILE__, |
| 654 |
PETSC_ERR_LIB, |
| 655 |
PETSC_ERROR_INITIAL, |
| 656 |
"Failure in ts_poststage from dealii::PETScWrappers::TimeStepper"); |
| 657 |
petsc_increment_state_counter(xx[i]); |
| 658 |
PetscFunctionReturn(PETSC_SUCCESS); |
| 659 |
}; |
| 660 |
|
| 661 |
const auto ts_functiondomainerror = |
| 662 |
[](TS ts, PetscReal, Vec, PetscBool *accept) -> PetscErrorCode { |
| 663 |
PetscFunctionBeginUser; |
| 664 |
void *ctx; |
| 665 |
AssertPETSc(TSGetApplicationContext(ts, &ctx)); |
| 666 |
auto user = static_cast<TimeStepper *>(ctx); |
| 667 |
*accept = user->error_in_function ? PETSC_FALSE : PETSC_TRUE; |
| 668 |
PetscFunctionReturn(PETSC_SUCCESS); |
| 669 |
}; |
| 670 |
|
| 671 |
const auto ts_ifunction = |
| 672 |
[](TS ts, PetscReal t, Vec x, Vec xdot, Vec f, void *ctx) |
| 673 |
-> PetscErrorCode { |
| 674 |
PetscFunctionBeginUser; |
| 675 |
auto user = static_cast<TimeStepper *>(ctx); |
| 676 |
|
| 677 |
VectorType xdealii(x); |
| 678 |
VectorType xdotdealii(xdot); |
| 679 |
VectorType fdealii(f); |
| 680 |
|
| 681 |
const int lineno = __LINE__; |
| 682 |
const int err = call_and_possibly_capture_ts_exception( |
| 683 |
user->implicit_function, |
| 684 |
user->pending_exception, |
| 685 |
[user, ts]() -> void { |
| 686 |
user->error_in_function = true; |
| 687 |
|
| 688 |
SNES snes; |
| 689 |
AssertPETSc(TSGetSNES(ts, &snes)); |
| 690 |
AssertPETSc(SNESSetFunctionDomainError(snes)); |
| 691 |
}, |
| 692 |
t, |
| 693 |
xdealii, |
| 694 |
xdotdealii, |
| 695 |
fdealii); |
| 696 |
if (err) |
| 697 |
return PetscError( |
| 698 |
PetscObjectComm((PetscObject)ts), |
| 699 |
lineno + 1, |
| 700 |
"implicit_function", |
| 701 |
__FILE__, |
| 702 |
PETSC_ERR_LIB, |
| 703 |
PETSC_ERROR_INITIAL, |
| 704 |
"Failure in ts_ifunction from dealii::PETScWrappers::TimeStepper"); |
| 705 |
petsc_increment_state_counter(f); |
| 706 |
PetscFunctionReturn(PETSC_SUCCESS); |
| 707 |
}; |
| 708 |
|
| 709 |
const auto ts_ijacobian = [](TS ts, |
| 710 |
PetscReal t, |
| 711 |
Vec x, |
| 712 |
Vec xdot, |
| 713 |
PetscReal s, |
| 714 |
Mat A, |
| 715 |
Mat P, |
| 716 |
void *ctx) -> PetscErrorCode { |
| 717 |
PetscFunctionBeginUser; |
| 718 |
auto user = static_cast<TimeStepper *>(ctx); |
| 719 |
|
| 720 |
VectorType xdealii(x); |
| 721 |
VectorType xdotdealii(xdot); |
| 722 |
AMatrixType Adealii(A); |
| 723 |
PMatrixType Pdealii(P); |
| 724 |
|
| 725 |
const int lineno = __LINE__; |
| 726 |
const int err = call_and_possibly_capture_ts_exception( |
| 727 |
user->implicit_jacobian, |
| 728 |
user->pending_exception, |
| 729 |
[ts]() -> void { |
| 730 |
SNES snes; |
| 731 |
AssertPETSc(TSGetSNES(ts, &snes)); |
| 732 |
snes_set_jacobian_domain_error(snes); |
| 733 |
}, |
| 734 |
t, |
| 735 |
xdealii, |
| 736 |
xdotdealii, |
| 737 |
s, |
| 738 |
Adealii, |
| 739 |
Pdealii); |
| 740 |
if (err) |
| 741 |
return PetscError( |
| 742 |
PetscObjectComm((PetscObject)ts), |
| 743 |
lineno + 1, |
| 744 |
"implicit_jacobian", |
| 745 |
__FILE__, |
| 746 |
PETSC_ERR_LIB, |
| 747 |
PETSC_ERROR_INITIAL, |
| 748 |
"Failure in ts_ijacobian from dealii::PETScWrappers::TimeStepper"); |
| 749 |
petsc_increment_state_counter(P); |
| 750 |
|
| 751 |
// Handle the Jacobian-free case |
| 752 |
// This call allow to resample the linearization point |
| 753 |
// of the MFFD tangent operator |
| 754 |
PetscBool flg; |
| 755 |
PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMFFD, &flg)); |
| 756 |
if (flg) |
| 757 |
{ |
| 758 |
PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); |
| 759 |
PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); |
| 760 |
} |
| 761 |
else |
| 762 |
petsc_increment_state_counter(A); |
| 763 |
|
| 764 |
PetscFunctionReturn(PETSC_SUCCESS); |
| 765 |
}; |
| 766 |
|
| 767 |
const auto ts_ijacobian_with_setup = [](TS ts, |
| 768 |
PetscReal t, |
| 769 |
Vec x, |
| 770 |
Vec xdot, |
| 771 |
PetscReal s, |
| 772 |
Mat A, |
| 773 |
Mat P, |
| 774 |
void *ctx) -> PetscErrorCode { |
| 775 |
PetscFunctionBeginUser; |
| 776 |
auto user = static_cast<TimeStepper *>(ctx); |
| 777 |
|
| 778 |
VectorType xdealii(x); |
| 779 |
VectorType xdotdealii(xdot); |
| 780 |
|
| 781 |
const int lineno = __LINE__; |
| 782 |
const int err = call_and_possibly_capture_ts_exception( |
| 783 |
user->setup_jacobian, |
| 784 |
user->pending_exception, |
| 785 |
[ts]() -> void { |
| 786 |
SNES snes; |
| 787 |
AssertPETSc(TSGetSNES(ts, &snes)); |
| 788 |
snes_set_jacobian_domain_error(snes); |
| 789 |
}, |
| 790 |
t, |
| 791 |
xdealii, |
| 792 |
xdotdealii, |
| 793 |
s); |
| 794 |
if (err) |
| 795 |
return PetscError( |
| 796 |
PetscObjectComm((PetscObject)ts), |
| 797 |
lineno + 1, |
| 798 |
"setup_jacobian", |
| 799 |
__FILE__, |
| 800 |
PETSC_ERR_LIB, |
| 801 |
PETSC_ERROR_INITIAL, |
| 802 |
"Failure in ts_ijacobian_with_setup from dealii::PETScWrappers::TimeStepper"); |
| 803 |
// The MatCopy calls below are 99% of the times dummy calls. |
| 804 |
// They are only used in case we get different Mats then the one we passed |
| 805 |
// to TSSetIJacobian. |
| 806 |
if (user->P) |
| 807 |
PetscCall(MatCopy(user->P->petsc_matrix(), P, SAME_NONZERO_PATTERN)); |
| 808 |
if (user->A) |
| 809 |
PetscCall(MatCopy(user->A->petsc_matrix(), A, SAME_NONZERO_PATTERN)); |
| 810 |
petsc_increment_state_counter(P); |
| 811 |
|
| 812 |
// Handle older versions of PETSc for which we cannot pass a MATSHELL |
| 813 |
// matrix to DMSetMatType. This has been fixed from 3.13 on. |
| 814 |
// What we need to do for older version of PETSc is instead to have |
| 815 |
// a zero matrix with all diagonal entries present. |
| 816 |
// We use PETSC_MACHINE_EPSILON because some versions of PETSc skip |
| 817 |
// MatShift if we pass 0.0 |
| 818 |
if (user->need_dummy_assemble) |
| 819 |
{ |
| 820 |
PetscCall(MatZeroEntries(P)); |
| 821 |
PetscCall(MatAssemblyBegin(P, MAT_FINAL_ASSEMBLY)); |
| 822 |
PetscCall(MatAssemblyEnd(P, MAT_FINAL_ASSEMBLY)); |
| 823 |
PetscCall(MatShift(P, PETSC_MACHINE_EPSILON)); |
| 824 |
} |
| 825 |
user->need_dummy_assemble = false; |
| 826 |
|
| 827 |
// Handle the Jacobian-free case |
| 828 |
// This call allows to resample the linearization point |
| 829 |
// of the MFFD tangent operator |
| 830 |
PetscBool flg; |
| 831 |
PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMFFD, &flg)); |
| 832 |
if (flg) |
| 833 |
{ |
| 834 |
PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); |
| 835 |
PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); |
| 836 |
} |
| 837 |
else |
| 838 |
petsc_increment_state_counter(A); |
| 839 |
|
| 840 |
PetscFunctionReturn(PETSC_SUCCESS); |
| 841 |
}; |
| 842 |
|
| 843 |
const auto ts_rhsfunction = |
| 844 |
[](TS ts, PetscReal t, Vec x, Vec f, void *ctx) -> PetscErrorCode { |
| 845 |
PetscFunctionBeginUser; |
| 846 |
auto user = static_cast<TimeStepper *>(ctx); |
| 847 |
|
| 848 |
VectorType xdealii(x); |
| 849 |
VectorType fdealii(f); |
| 850 |
|
| 851 |
const int lineno = __LINE__; |
| 852 |
const int err = call_and_possibly_capture_ts_exception( |
| 853 |
user->explicit_function, |
| 854 |
user->pending_exception, |
| 855 |
[user, ts]() -> void { |
| 856 |
user->error_in_function = true; |
| 857 |
|
| 858 |
if (ts_has_snes(ts)) |
| 859 |
{ |
| 860 |
SNES snes; |
| 861 |
AssertPETSc(TSGetSNES(ts, &snes)); |
| 862 |
AssertPETSc(SNESSetFunctionDomainError(snes)); |
| 863 |
} |
| 864 |
}, |
| 865 |
t, |
| 866 |
xdealii, |
| 867 |
fdealii); |
| 868 |
if (err) |
| 869 |
return PetscError( |
| 870 |
PetscObjectComm((PetscObject)ts), |
| 871 |
lineno + 1, |
| 872 |
"explicit_function", |
| 873 |
__FILE__, |
| 874 |
PETSC_ERR_LIB, |
| 875 |
PETSC_ERROR_INITIAL, |
| 876 |
"Failure in ts_rhsfunction from dealii::PETScWrappers::TimeStepper"); |
| 877 |
petsc_increment_state_counter(f); |
| 878 |
PetscFunctionReturn(PETSC_SUCCESS); |
| 879 |
}; |
| 880 |
|
| 881 |
const auto ts_rhsjacobian = |
| 882 |
[](TS ts, PetscReal t, Vec x, Mat A, Mat P, void *ctx) -> PetscErrorCode { |
| 883 |
PetscFunctionBeginUser; |
| 884 |
auto user = static_cast<TimeStepper *>(ctx); |
| 885 |
|
| 886 |
VectorType xdealii(x); |
| 887 |
AMatrixType Adealii(A); |
| 888 |
PMatrixType Pdealii(P); |
| 889 |
|
| 890 |
const int lineno = __LINE__; |
| 891 |
const int err = call_and_possibly_capture_ts_exception( |
| 892 |
user->explicit_jacobian, |
| 893 |
user->pending_exception, |
| 894 |
[ts]() -> void { |
| 895 |
SNES snes; |
| 896 |
AssertPETSc(TSGetSNES(ts, &snes)); |
| 897 |
snes_set_jacobian_domain_error(snes); |
| 898 |
}, |
| 899 |
t, |
| 900 |
xdealii, |
| 901 |
Adealii, |
| 902 |
Pdealii); |
| 903 |
if (err) |
| 904 |
return PetscError( |
| 905 |
PetscObjectComm((PetscObject)ts), |
| 906 |
lineno + 1, |
| 907 |
"explicit_jacobian", |
| 908 |
__FILE__, |
| 909 |
PETSC_ERR_LIB, |
| 910 |
PETSC_ERROR_INITIAL, |
| 911 |
"Failure in ts_rhsjacobian from dealii::PETScWrappers::TimeStepper"); |
| 912 |
petsc_increment_state_counter(P); |
| 913 |
|
| 914 |
// Handle the Jacobian-free case |
| 915 |
// This call allow to resample the linearization point |
| 916 |
// of the MFFD tangent operator |
| 917 |
PetscBool flg; |
| 918 |
PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMFFD, &flg)); |
| 919 |
if (flg) |
| 920 |
{ |
| 921 |
PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); |
| 922 |
PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); |
| 923 |
} |
| 924 |
else |
| 925 |
petsc_increment_state_counter(A); |
| 926 |
|
| 927 |
PetscFunctionReturn(PETSC_SUCCESS); |
| 928 |
}; |
| 929 |
|
| 930 |
const auto ts_monitor = |
| 931 |
[](TS ts, PetscInt it, PetscReal t, Vec x, void *ctx) -> PetscErrorCode { |
| 932 |
PetscFunctionBeginUser; |
| 933 |
auto user = static_cast<TimeStepper *>(ctx); |
| 934 |
|
| 935 |
VectorType xdealii(x); |
| 936 |
|
| 937 |
const int lineno = __LINE__; |
| 938 |
const int err = call_and_possibly_capture_ts_exception( |
| 939 |
user->monitor, user->pending_exception, {}, t, xdealii, it); |
| 940 |
if (err) |
| 941 |
return PetscError( |
| 942 |
PetscObjectComm((PetscObject)ts), |
| 943 |
lineno + 1, |
| 944 |
"monitor", |
| 945 |
__FILE__, |
| 946 |
PETSC_ERR_LIB, |
| 947 |
PETSC_ERROR_INITIAL, |
| 948 |
"Failure in ts_monitor from dealii::PETScWrappers::TimeStepper"); |
| 949 |
PetscFunctionReturn(PETSC_SUCCESS); |
| 950 |
}; |
| 951 |
|
| 952 |
AssertThrow(explicit_function || implicit_function, |
| 953 |
StandardExceptions::ExcFunctionNotProvided( |
| 954 |
"explicit_function || implicit_function")); |
| 955 |
|
| 956 |
// Handle recoverable errors. |
| 957 |
this->error_in_function = false; |
| 958 |
AssertPETSc(TSSetPreStage(ts, ts_prestage)); |
| 959 |
AssertPETSc(TSSetFunctionDomainError(ts, ts_functiondomainerror)); |
| 960 |
|
| 961 |
// Problem description. |
| 962 |
if (explicit_function) |
| 963 |
AssertPETSc(TSSetRHSFunction(ts, nullptr, ts_rhsfunction, this)); |
| 964 |
if (implicit_function) |
| 965 |
AssertPETSc(TSSetIFunction(ts, nullptr, ts_ifunction, this)); |
| 966 |
|
| 967 |
// Handle Jacobians. |
| 968 |
this->need_dummy_assemble = false; |
| 969 |
if (setup_jacobian) |
| 970 |
{ |
| 971 |
AssertPETSc(TSSetIJacobian(ts, |
| 972 |
A ? A->petsc_matrix() : nullptr, |
| 973 |
P ? P->petsc_matrix() : nullptr, |
| 974 |
ts_ijacobian_with_setup, |
| 975 |
this)); |
| 976 |
|
| 977 |
// Tell PETSc to set up a MFFD operator for the linear system matrix |
| 978 |
if (!A) |
| 979 |
set_use_matrix_free(ts, true, false); |
| 980 |
|
| 981 |
// Do not waste memory by creating a dummy AIJ matrix inside PETSc. |
| 982 |
if (!P) |
| 983 |
{ |
| 984 |
# if DEAL_II_PETSC_VERSION_GTE(3, 13, 0) |
| 985 |
DM dm; |
| 986 |
SNES snes; |
| 987 |
AssertPETSc(TSGetSNES(ts, &snes)); |
| 988 |
AssertPETSc(SNESGetDM(snes, &dm)); |
| 989 |
AssertPETSc(DMSetMatType(dm, MATSHELL)); |
| 990 |
# else |
| 991 |
this->need_dummy_assemble = true; |
| 992 |
# endif |
| 993 |
} |
| 994 |
} |
| 995 |
else |
| 996 |
{ |
| 997 |
if (explicit_jacobian) |
| 998 |
{ |
| 999 |
AssertPETSc(TSSetRHSJacobian(ts, |
| 1000 |
A ? A->petsc_matrix() : |
| 1001 |
(P ? P->petsc_matrix() : nullptr), |
| 1002 |
P ? P->petsc_matrix() : nullptr, |
| 1003 |
ts_rhsjacobian, |
| 1004 |
this)); |
| 1005 |
} |
| 1006 |
|
| 1007 |
if (implicit_jacobian) |
| 1008 |
{ |
| 1009 |
AssertPETSc(TSSetIJacobian(ts, |
| 1010 |
A ? A->petsc_matrix() : |
| 1011 |
(P ? P->petsc_matrix() : nullptr), |
| 1012 |
P ? P->petsc_matrix() : nullptr, |
| 1013 |
ts_ijacobian, |
| 1014 |
this)); |
| 1015 |
} |
| 1016 |
|
| 1017 |
// The user did not set any Jacobian callback. PETSc default in this |
| 1018 |
// case is to use FD and thus assemble a dense operator by finite |
| 1019 |
// differencing the residual callbacks. Here instead we decide to |
| 1020 |
// use a full matrix-free approach by default. This choice can always |
| 1021 |
// be overridden from command line. |
| 1022 |
if (!explicit_jacobian && !implicit_jacobian) |
| 1023 |
{ |
| 1024 |
set_use_matrix_free(ts, false, true); |
| 1025 |
} |
| 1026 |
} |
| 1027 |
|
| 1028 |
// In case solve_with_jacobian is provided, create a shell |
| 1029 |
// preconditioner wrapping the user call. The default internal Krylov |
| 1030 |
// solver only applies the preconditioner. This choice |
| 1031 |
// can be overridden by command line and users can use any other |
| 1032 |
// Krylov method if their solve is not accurate enough. |
| 1033 |
// Using solve_with_jacobian as a preconditioner allows users |
| 1034 |
// to provide approximate solvers and possibly iterate on a matrix-free |
| 1035 |
// approximation of the tangent operator. |
| 1036 |
if (solve_with_jacobian) |
| 1037 |
{ |
| 1038 |
solve_with_jacobian_pc.vmult = [&](VectorBase &indst, |
| 1039 |
const VectorBase &insrc) -> void { |
| 1040 |
VectorType dst(static_cast<const Vec &>(indst)); |
| 1041 |
const VectorType src(static_cast<const Vec &>(insrc)); |
| 1042 |
solve_with_jacobian(src, dst); |
| 1043 |
}; |
| 1044 |
|
| 1045 |
// Default Krylov solver (preconditioner only) |
| 1046 |
SNES snes; |
| 1047 |
KSP ksp; |
| 1048 |
AssertPETSc(TSGetSNES(ts, &snes)); |
| 1049 |
AssertPETSc(SNESGetKSP(snes, &ksp)); |
| 1050 |
AssertPETSc(KSPSetType(ksp, KSPPREONLY)); |
| 1051 |
AssertPETSc(KSPSetPC(ksp, solve_with_jacobian_pc.get_pc())); |
| 1052 |
} |
| 1053 |
|
| 1054 |
if (distribute || update_constrained_components) |
| 1055 |
{ |
| 1056 |
if (update_constrained_components) |
| 1057 |
Assert(!distribute, |
| 1058 |
ExcMessage( |
| 1059 |
"The 'distribute' callback name of the TimeStepper " |
| 1060 |
"class is deprecated. If you are setting the equivalent " |
| 1061 |
"'update_constrained_components' callback, you cannot also " |
| 1062 |
"set the 'distribute' callback.")); |
| 1063 |
AssertPETSc(TSSetPostStage(ts, ts_poststage)); |
| 1064 |
} |
| 1065 |
|
| 1066 |
// Attach user monitoring routine. |
| 1067 |
if (monitor) |
| 1068 |
AssertPETSc(TSMonitorSet(ts, ts_monitor, this, nullptr)); |
| 1069 |
|
| 1070 |
// Handle AMR. |
| 1071 |
if (decide_and_prepare_for_remeshing || |
| 1072 |
decide_for_coarsening_and_refinement) |
| 1073 |
{ |
| 1074 |
if (decide_and_prepare_for_remeshing) |
| 1075 |
Assert( |
| 1076 |
!decide_for_coarsening_and_refinement, |
| 1077 |
ExcMessage( |
| 1078 |
"The 'decide_for_coarsening_and_refinement' callback name " |
| 1079 |
"of the TimeStepper class is deprecated. If you are setting " |
| 1080 |
"the equivalent 'decide_and_prepare_for_remeshing' callback, you " |
| 1081 |
"cannot also set the 'decide_for_coarsening_and_refinement' " |
| 1082 |
"callback.")); |
| 1083 |
|
| 1084 |
// If we have the decide_and_prepare_for_remeshing callback |
| 1085 |
// set, then we also need to have the callback for actually |
| 1086 |
// transferring the solution: |
| 1087 |
AssertThrow(interpolate || transfer_solution_vectors_to_new_mesh, |
| 1088 |
StandardExceptions::ExcFunctionNotProvided( |
| 1089 |
"transfer_solution_vectors_to_new_mesh")); |
| 1090 |
|
| 1091 |
if (transfer_solution_vectors_to_new_mesh) |
| 1092 |
Assert( |
| 1093 |
!interpolate, |
| 1094 |
ExcMessage( |
| 1095 |
"The 'interpolate' callback name " |
| 1096 |
"of the TimeStepper class is deprecated. If you are setting " |
| 1097 |
"the equivalent 'transfer_solution_vectors_to_new_mesh' callback, you " |
| 1098 |
"cannot also set the 'interpolate' " |
| 1099 |
"callback.")); |
| 1100 |
|
| 1101 |
# if DEAL_II_PETSC_VERSION_GTE(3, 21, 0) |
| 1102 |
(void)ts_poststep_amr; |
| 1103 |
AssertPETSc(TSSetResize(ts, |
| 1104 |
static_cast<PetscBool>(restart_if_remesh), |
| 1105 |
ts_resize_setup, |
| 1106 |
ts_resize_transfer, |
| 1107 |
this)); |
| 1108 |
# else |
| 1109 |
AssertThrow(!restart_if_remesh, |
| 1110 |
ExcMessage( |
| 1111 |
"Restart with remesh supported from PETSc 3.21.")); |
| 1112 |
# if DEAL_II_PETSC_VERSION_GTE(3, 20, 0) |
| 1113 |
(void)ts_poststep_amr; |
| 1114 |
AssertPETSc(TSSetResize(ts, ts_resize_setup, ts_resize_transfer, this)); |
| 1115 |
# else |
| 1116 |
AssertPETSc(PetscObjectComposeFunction( |
| 1117 |
(PetscObject)ts, |
| 1118 |
"__dealii_ts_resize_setup__", |
| 1119 |
static_cast<PetscErrorCode (*)( |
| 1120 |
TS, PetscInt, PetscReal, Vec, PetscBool *, void *)>( |
| 1121 |
ts_resize_setup))); |
| 1122 |
AssertPETSc(PetscObjectComposeFunction( |
| 1123 |
(PetscObject)ts, |
| 1124 |
"__dealii_ts_resize_transfer__", |
| 1125 |
static_cast<PetscErrorCode (*)(TS, PetscInt, Vec[], Vec[], void *)>( |
| 1126 |
ts_resize_transfer))); |
| 1127 |
AssertPETSc(TSSetPostStep(ts, ts_poststep_amr)); |
| 1128 |
# endif |
| 1129 |
# endif |
| 1130 |
} |
| 1131 |
|
| 1132 |
// Allow command line customization. |
| 1133 |
AssertPETSc(TSSetFromOptions(ts)); |
| 1134 |
|
| 1135 |
// By default PETSc does not check for Jacobian errors in optimized |
| 1136 |
// mode. Here we do it unconditionally. |
| 1137 |
# if DEAL_II_PETSC_VERSION_GTE(3, 11, 0) |
| 1138 |
{ |
| 1139 |
SNES snes; |
| 1140 |
AssertPETSc(TSGetSNES(ts, &snes)); |
| 1141 |
AssertPETSc(SNESSetCheckJacobianDomainError(snes, PETSC_TRUE)); |
| 1142 |
} |
| 1143 |
# endif |
| 1144 |
} |
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 1149 |
DEAL_II_CXX20_REQUIRES( |
| 1150 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 1151 |
std::constructible_from< |
| 1152 |
VectorType, |
| 1153 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 1154 |
std::constructible_from< |
| 1155 |
PMatrixType, |
| 1156 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 1157 |
std::constructible_from<AMatrixType, Mat>)) |
| 1158 |
void TimeStepper<VectorType, PMatrixType, AMatrixType>:: |
| 1159 |
setup_algebraic_constraints(const VectorType &y) |
| 1160 |
{ |
| 1161 |
if (algebraic_components && need_dae_tolerances) |
| 1162 |
{ |
| 1163 |
PetscReal atol, rtol; |
| 1164 |
AssertPETSc(TSGetTolerances(ts, &atol, nullptr, &rtol, nullptr)); |
| 1165 |
|
| 1166 |
// Fill up vectors with individual tolerances, using -1 for the |
| 1167 |
// algebraic variables. |
| 1168 |
// We need to input them with the same vector type of our solution |
| 1169 |
// but we are not sure that the user's VectorType supports operator[] |
| 1170 |
// We thus first create standard vectors that we know support the |
| 1171 |
// operator, and then copy the values into the user's type operator. |
| 1172 |
Vec av, rv; |
| 1173 |
Vec py = const_cast<VectorType &>(y).petsc_vector(); |
| 1174 |
AssertPETSc(VecDuplicate(py, &av)); |
| 1175 |
AssertPETSc(VecDuplicate(py, &rv)); |
| 1176 |
|
| 1177 |
// Standard vectors |
| 1178 |
Vec avT, rvT; |
| 1179 |
PetscInt n, N; |
| 1180 |
AssertPETSc(VecGetLocalSize(py, &n)); |
| 1181 |
AssertPETSc(VecGetSize(py, &N)); |
| 1182 |
AssertPETSc(VecCreateMPI(this->get_mpi_communicator(), n, N, &avT)); |
| 1183 |
AssertPETSc(VecDuplicate(avT, &rvT)); |
| 1184 |
|
| 1185 |
// Fill-up the vectors |
| 1186 |
VectorBase avdealii(avT); |
| 1187 |
VectorBase rvdealii(rvT); |
| 1188 |
avdealii = atol; |
| 1189 |
rvdealii = rtol; |
| 1190 |
for (auto i : algebraic_components()) |
| 1191 |
{ |
| 1192 |
avdealii[i] = -1.0; |
| 1193 |
rvdealii[i] = -1.0; |
| 1194 |
} |
| 1195 |
avdealii.compress(VectorOperation::insert); |
| 1196 |
rvdealii.compress(VectorOperation::insert); |
| 1197 |
|
| 1198 |
// Copy, set and destroy |
| 1199 |
AssertPETSc(VecCopy(avT, av)); |
| 1200 |
AssertPETSc(VecCopy(rvT, rv)); |
| 1201 |
AssertPETSc(TSSetTolerances(ts, atol, av, rtol, rv)); |
| 1202 |
AssertPETSc(VecDestroy(&av)); |
| 1203 |
AssertPETSc(VecDestroy(&rv)); |
| 1204 |
AssertPETSc(VecDestroy(&avT)); |
| 1205 |
AssertPETSc(VecDestroy(&rvT)); |
| 1206 |
} |
| 1207 |
} |
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 1212 |
DEAL_II_CXX20_REQUIRES( |
| 1213 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 1214 |
std::constructible_from< |
| 1215 |
VectorType, |
| 1216 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 1217 |
std::constructible_from< |
| 1218 |
PMatrixType, |
| 1219 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 1220 |
std::constructible_from<AMatrixType, Mat>)) |
| 1221 |
unsigned int TimeStepper<VectorType, PMatrixType, AMatrixType>::solve( |
| 1222 |
VectorType &y) |
| 1223 |
{ |
| 1224 |
// Set up PETSc data structure. |
| 1225 |
setup_callbacks(); |
| 1226 |
|
| 1227 |
// Set solution vector. |
| 1228 |
AssertPETSc(TSSetSolution(ts, y.petsc_vector())); |
| 1229 |
|
| 1230 |
// Setup internal data structures, including the internal nonlinear |
| 1231 |
// solver SNES if using an implicit solver. |
| 1232 |
AssertPETSc(TSSetUp(ts)); |
| 1233 |
|
| 1234 |
// Handle algebraic components. This must be done when we know the layout |
| 1235 |
// of the solution vector. |
| 1236 |
setup_algebraic_constraints(y); |
| 1237 |
|
| 1238 |
// Having set everything up, now do the actual work |
| 1239 |
// and let PETSc do the time stepping. If there is |
| 1240 |
// a pending exception, then one of the user callbacks |
| 1241 |
// threw an exception we didn't know how to deal with |
| 1242 |
// at the time. It is possible that PETSc managed to |
| 1243 |
// recover anyway and in that case would have returned |
| 1244 |
// a zero error code -- if so, just eat the exception and |
| 1245 |
// continue on; otherwise, just rethrow the exception |
| 1246 |
// and get outta here. |
| 1247 |
const PetscErrorCode status = TSSolve(ts, nullptr); |
| 1248 |
if (pending_exception) |
| 1249 |
{ |
| 1250 |
try |
| 1251 |
{ |
| 1252 |
std::rethrow_exception(pending_exception); |
| 1253 |
} |
| 1254 |
catch (...) |
| 1255 |
{ |
| 1256 |
pending_exception = nullptr; |
| 1257 |
if (status == 0) |
| 1258 |
/* just eat the exception */; |
| 1259 |
else |
| 1260 |
throw; |
| 1261 |
} |
| 1262 |
} |
| 1263 |
AssertPETSc(status); |
| 1264 |
|
| 1265 |
// Get the number of steps taken. |
| 1266 |
auto nt = ts_get_step_number(ts); |
| 1267 |
|
| 1268 |
// Raise an exception if the solver has not converged |
| 1269 |
TSConvergedReason reason; |
| 1270 |
AssertPETSc(TSGetConvergedReason(ts, &reason)); |
| 1271 |
AssertThrow(reason > 0, |
| 1272 |
ExcMessage("TS solver did not converge after " + |
| 1273 |
std::to_string(nt) + " iterations with reason " + |
| 1274 |
TSConvergedReasons[reason])); |
| 1275 |
|
| 1276 |
// Finally return |
| 1277 |
return nt; |
| 1278 |
} |
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 1283 |
DEAL_II_CXX20_REQUIRES( |
| 1284 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 1285 |
std::constructible_from< |
| 1286 |
VectorType, |
| 1287 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 1288 |
std::constructible_from< |
| 1289 |
PMatrixType, |
| 1290 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 1291 |
std::constructible_from<AMatrixType, Mat>)) |
| 1292 |
unsigned int TimeStepper<VectorType, PMatrixType, AMatrixType>::solve( |
| 1293 |
VectorType &y, |
| 1294 |
PMatrixType &P) |
| 1295 |
{ |
| 1296 |
set_matrix(P); |
| 1297 |
return solve(y); |
| 1298 |
} |
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 1303 |
DEAL_II_CXX20_REQUIRES( |
| 1304 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 1305 |
std::constructible_from< |
| 1306 |
VectorType, |
| 1307 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 1308 |
std::constructible_from< |
| 1309 |
PMatrixType, |
| 1310 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 1311 |
std::constructible_from<AMatrixType, Mat>)) |
| 1312 |
unsigned int TimeStepper<VectorType, PMatrixType, AMatrixType>::solve( |
| 1313 |
VectorType &y, |
| 1314 |
AMatrixType &A, |
| 1315 |
PMatrixType &P) |
| 1316 |
{ |
| 1317 |
set_matrices(A, P); |
| 1318 |
return solve(y); |
| 1319 |
} |
| 1320 |
|
| 1321 |
} // namespace PETScWrappers |
| 1322 |
|
| 1323 |
# undef AssertPETSc |
| 1324 |
# if defined(undefPetscCall) |
| 1325 |
# undef PetscCall |
| 1326 |
# undef undefPetscCall |
| 1327 |
# endif |
| 1328 |
|
| 1329 |
DEAL_II_NAMESPACE_CLOSE |
| 1330 |
|
| 1331 |
#else |
| 1332 |
|
| 1333 |
// Make sure the scripts that create the C++20 module input files have |
| 1334 |
// something to latch on if the preprocessor #ifdef above would |
| 1335 |
// otherwise lead to an empty content of the file. |
| 1336 |
DEAL_II_NAMESPACE_OPEN |
| 1337 |
DEAL_II_NAMESPACE_CLOSE |
| 1338 |
|
| 1339 |
#endif // DEAL_II_WITH_PETSC |
| 1340 |
|
| 1341 |
#endif |