| 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_snes_templates_h |
| 16 |
#define dealii_petsc_snes_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_snes.h> |
| 26 |
|
| 27 |
# include <petscdm.h> |
| 28 |
# include <petscerror.h> |
| 29 |
# include <petscsnes.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_snes_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 |
NonlinearSolver<VectorType, PMatrixType, AMatrixType>::NonlinearSolver( |
| 133 |
const NonlinearSolverData &data, |
| 134 |
const MPI_Comm mpi_comm) |
| 135 |
: pending_exception(nullptr) |
| 136 |
{ |
| 137 |
AssertPETSc(SNESCreate(mpi_comm, &snes)); |
| 138 |
AssertPETSc(SNESSetApplicationContext(snes, this)); |
| 139 |
reinit(data); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 145 |
DEAL_II_CXX20_REQUIRES( |
| 146 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 147 |
std::constructible_from< |
| 148 |
VectorType, |
| 149 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 150 |
std::constructible_from< |
| 151 |
PMatrixType, |
| 152 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 153 |
std::constructible_from<AMatrixType, Mat>)) |
| 154 |
NonlinearSolver<VectorType, PMatrixType, AMatrixType>::operator SNES() const |
| 155 |
{ |
| 156 |
return snes; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 162 |
DEAL_II_CXX20_REQUIRES( |
| 163 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 164 |
std::constructible_from< |
| 165 |
VectorType, |
| 166 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 167 |
std::constructible_from< |
| 168 |
PMatrixType, |
| 169 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 170 |
std::constructible_from<AMatrixType, Mat>)) |
| 171 |
SNES NonlinearSolver<VectorType, PMatrixType, AMatrixType>::petsc_snes() |
| 172 |
{ |
| 173 |
return snes; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 179 |
DEAL_II_CXX20_REQUIRES( |
| 180 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 181 |
std::constructible_from< |
| 182 |
VectorType, |
| 183 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 184 |
std::constructible_from< |
| 185 |
PMatrixType, |
| 186 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 187 |
std::constructible_from<AMatrixType, Mat>)) |
| 188 |
inline MPI_Comm NonlinearSolver<VectorType, PMatrixType, AMatrixType>:: |
| 189 |
get_mpi_communicator() const |
| 190 |
{ |
| 191 |
return PetscObjectComm(reinterpret_cast<PetscObject>(snes)); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 197 |
DEAL_II_CXX20_REQUIRES( |
| 198 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 199 |
std::constructible_from< |
| 200 |
VectorType, |
| 201 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 202 |
std::constructible_from< |
| 203 |
PMatrixType, |
| 204 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 205 |
std::constructible_from<AMatrixType, Mat>)) |
| 206 |
NonlinearSolver<VectorType, PMatrixType, AMatrixType>::~NonlinearSolver() |
| 207 |
{ |
| 208 |
AssertPETSc(SNESDestroy(&snes)); |
| 209 |
|
| 210 |
Assert(pending_exception == nullptr, ExcInternalError()); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 216 |
DEAL_II_CXX20_REQUIRES( |
| 217 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 218 |
std::constructible_from< |
| 219 |
VectorType, |
| 220 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 221 |
std::constructible_from< |
| 222 |
PMatrixType, |
| 223 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 224 |
std::constructible_from<AMatrixType, Mat>)) |
| 225 |
void NonlinearSolver<VectorType, PMatrixType, AMatrixType>::reinit() |
| 226 |
{ |
| 227 |
AssertPETSc(SNESReset(snes)); |
| 228 |
// By default PETSc does not check for Jacobian errors in optimized |
| 229 |
// mode. Here we do it unconditionally. |
| 230 |
# if DEAL_II_PETSC_VERSION_GTE(3, 11, 0) |
| 231 |
AssertPETSc(SNESSetCheckJacobianDomainError(snes, PETSC_TRUE)); |
| 232 |
# endif |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 238 |
DEAL_II_CXX20_REQUIRES( |
| 239 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 240 |
std::constructible_from< |
| 241 |
VectorType, |
| 242 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 243 |
std::constructible_from< |
| 244 |
PMatrixType, |
| 245 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 246 |
std::constructible_from<AMatrixType, Mat>)) |
| 247 |
void NonlinearSolver<VectorType, PMatrixType, AMatrixType>::reinit( |
| 248 |
const NonlinearSolverData &data) |
| 249 |
{ |
| 250 |
reinit(); |
| 251 |
|
| 252 |
// Solver type |
| 253 |
if (data.snes_type.size()) |
| 254 |
AssertPETSc(SNESSetType(snes, data.snes_type.c_str())); |
| 255 |
|
| 256 |
// Linesearch type |
| 257 |
if (data.snes_linesearch_type.size()) |
| 258 |
{ |
| 259 |
SNESLineSearch linesearch; |
| 260 |
AssertPETSc(SNESGetLineSearch(snes, &linesearch)); |
| 261 |
AssertPETSc( |
| 262 |
SNESLineSearchSetType(linesearch, data.snes_linesearch_type.c_str())); |
| 263 |
} |
| 264 |
|
| 265 |
// Options prefix |
| 266 |
if (data.options_prefix.size()) |
| 267 |
AssertPETSc(SNESSetOptionsPrefix(snes, data.options_prefix.c_str())); |
| 268 |
|
| 269 |
// Solver tolerances |
| 270 |
PetscReal atol = data.absolute_tolerance > 0.0 ? |
| 271 |
data.absolute_tolerance : |
| 272 |
static_cast<PetscReal>(PETSC_DEFAULT); |
| 273 |
PetscReal rtol = data.relative_tolerance > 0.0 ? |
| 274 |
data.relative_tolerance : |
| 275 |
static_cast<PetscReal>(PETSC_DEFAULT); |
| 276 |
PetscReal stol = data.step_tolerance > 0.0 ? |
| 277 |
data.step_tolerance : |
| 278 |
static_cast<PetscReal>(PETSC_DEFAULT); |
| 279 |
|
| 280 |
// Maximum number of iterations and function evaluations. |
| 281 |
PetscInt maxit = data.maximum_non_linear_iterations >= 0 ? |
| 282 |
data.maximum_non_linear_iterations : |
| 283 |
static_cast<PetscInt>(PETSC_DEFAULT); |
| 284 |
PetscInt maxfe = data.max_n_function_evaluations >= 0 ? |
| 285 |
data.max_n_function_evaluations : |
| 286 |
static_cast<PetscInt>(PETSC_DEFAULT); |
| 287 |
AssertPETSc(SNESSetTolerances(snes, atol, rtol, stol, maxit, maxfe)); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 293 |
DEAL_II_CXX20_REQUIRES( |
| 294 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 295 |
std::constructible_from< |
| 296 |
VectorType, |
| 297 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 298 |
std::constructible_from< |
| 299 |
PMatrixType, |
| 300 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 301 |
std::constructible_from<AMatrixType, Mat>)) |
| 302 |
void NonlinearSolver<VectorType, PMatrixType, AMatrixType>::set_matrix( |
| 303 |
PMatrixType &P) |
| 304 |
{ |
| 305 |
this->A = nullptr; |
| 306 |
this->P = &P; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 312 |
DEAL_II_CXX20_REQUIRES( |
| 313 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 314 |
std::constructible_from< |
| 315 |
VectorType, |
| 316 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 317 |
std::constructible_from< |
| 318 |
PMatrixType, |
| 319 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 320 |
std::constructible_from<AMatrixType, Mat>)) |
| 321 |
void NonlinearSolver<VectorType, PMatrixType, AMatrixType>::set_matrices( |
| 322 |
AMatrixType &A, |
| 323 |
PMatrixType &P) |
| 324 |
{ |
| 325 |
this->A = &A; |
| 326 |
this->P = &P; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 332 |
DEAL_II_CXX20_REQUIRES( |
| 333 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 334 |
std::constructible_from< |
| 335 |
VectorType, |
| 336 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 337 |
std::constructible_from< |
| 338 |
PMatrixType, |
| 339 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 340 |
std::constructible_from<AMatrixType, Mat>)) |
| 341 |
unsigned int NonlinearSolver<VectorType, PMatrixType, AMatrixType>::solve( |
| 342 |
VectorType &x) |
| 343 |
{ |
| 344 |
const auto snes_function = |
| 345 |
[](SNES snes, Vec x, Vec f, void *ctx) -> PetscErrorCode { |
| 346 |
PetscFunctionBeginUser; |
| 347 |
auto user = static_cast<NonlinearSolver *>(ctx); |
| 348 |
|
| 349 |
VectorType xdealii(x); |
| 350 |
VectorType fdealii(f); |
| 351 |
const int lineno = __LINE__; |
| 352 |
const int err = call_and_possibly_capture_snes_exception( |
| 353 |
user->residual, |
| 354 |
user->pending_exception, |
| 355 |
[snes]() -> void { AssertPETSc(SNESSetFunctionDomainError(snes)); }, |
| 356 |
xdealii, |
| 357 |
fdealii); |
| 358 |
if (err) |
| 359 |
return PetscError( |
| 360 |
PetscObjectComm((PetscObject)snes), |
| 361 |
lineno + 1, |
| 362 |
"residual", |
| 363 |
__FILE__, |
| 364 |
PETSC_ERR_LIB, |
| 365 |
PETSC_ERROR_INITIAL, |
| 366 |
"Failure in snes_function from dealii::PETScWrappers::NonlinearSolver"); |
| 367 |
petsc_increment_state_counter(f); |
| 368 |
PetscFunctionReturn(PETSC_SUCCESS); |
| 369 |
}; |
| 370 |
|
| 371 |
const auto snes_jacobian = |
| 372 |
[](SNES snes, Vec x, Mat A, Mat P, void *ctx) -> PetscErrorCode { |
| 373 |
PetscFunctionBeginUser; |
| 374 |
auto user = static_cast<NonlinearSolver *>(ctx); |
| 375 |
|
| 376 |
VectorType xdealii(x); |
| 377 |
AMatrixType Adealii(A); |
| 378 |
PMatrixType Pdealii(P); |
| 379 |
|
| 380 |
const int lineno = __LINE__; |
| 381 |
const int err = call_and_possibly_capture_snes_exception( |
| 382 |
user->jacobian, |
| 383 |
user->pending_exception, |
| 384 |
[snes]() -> void { snes_set_jacobian_domain_error(snes); }, |
| 385 |
xdealii, |
| 386 |
Adealii, |
| 387 |
Pdealii); |
| 388 |
if (err) |
| 389 |
return PetscError( |
| 390 |
PetscObjectComm((PetscObject)snes), |
| 391 |
lineno + 1, |
| 392 |
"jacobian", |
| 393 |
__FILE__, |
| 394 |
PETSC_ERR_LIB, |
| 395 |
PETSC_ERROR_INITIAL, |
| 396 |
"Failure in snes_jacobian from dealii::PETScWrappers::NonlinearSolver"); |
| 397 |
petsc_increment_state_counter(P); |
| 398 |
|
| 399 |
// Handle the Jacobian-free case |
| 400 |
// This call allows to resample the linearization point |
| 401 |
// of the MFFD tangent operator |
| 402 |
PetscBool flg; |
| 403 |
PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMFFD, &flg)); |
| 404 |
if (flg) |
| 405 |
{ |
| 406 |
PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); |
| 407 |
PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); |
| 408 |
} |
| 409 |
else |
| 410 |
petsc_increment_state_counter(A); |
| 411 |
|
| 412 |
PetscFunctionReturn(PETSC_SUCCESS); |
| 413 |
}; |
| 414 |
|
| 415 |
const auto snes_jacobian_with_setup = |
| 416 |
[](SNES snes, Vec x, Mat A, Mat P, void *ctx) -> PetscErrorCode { |
| 417 |
PetscFunctionBeginUser; |
| 418 |
auto user = static_cast<NonlinearSolver *>(ctx); |
| 419 |
|
| 420 |
VectorType xdealii(x); |
| 421 |
|
| 422 |
const int lineno = __LINE__; |
| 423 |
const int err = call_and_possibly_capture_snes_exception( |
| 424 |
user->setup_jacobian, |
| 425 |
user->pending_exception, |
| 426 |
[snes]() -> void { snes_set_jacobian_domain_error(snes); }, |
| 427 |
xdealii); |
| 428 |
if (err) |
| 429 |
return PetscError( |
| 430 |
PetscObjectComm((PetscObject)snes), |
| 431 |
lineno + 1, |
| 432 |
"setup_jacobian", |
| 433 |
__FILE__, |
| 434 |
PETSC_ERR_LIB, |
| 435 |
PETSC_ERROR_INITIAL, |
| 436 |
"Failure in snes_jacobian_with_setup from dealii::PETScWrappers::NonlinearSolver"); |
| 437 |
// The MatCopy calls below are 99% of the times dummy calls. |
| 438 |
// They are only used in case we get different Mats then the one we passed |
| 439 |
// to SNESSetJacobian. |
| 440 |
if (user->P) |
| 441 |
PetscCall(MatCopy(user->P->petsc_matrix(), P, SAME_NONZERO_PATTERN)); |
| 442 |
if (user->A) |
| 443 |
PetscCall(MatCopy(user->A->petsc_matrix(), A, SAME_NONZERO_PATTERN)); |
| 444 |
petsc_increment_state_counter(P); |
| 445 |
|
| 446 |
// Handle older versions of PETSc for which we cannot pass a MATSHELL |
| 447 |
// matrix to DMSetMatType. This has been fixed from 3.13 on. |
| 448 |
if (user->need_dummy_assemble) |
| 449 |
{ |
| 450 |
PetscCall(MatZeroEntries(P)); |
| 451 |
PetscCall(MatAssemblyBegin(P, MAT_FINAL_ASSEMBLY)); |
| 452 |
PetscCall(MatAssemblyEnd(P, MAT_FINAL_ASSEMBLY)); |
| 453 |
} |
| 454 |
user->need_dummy_assemble = false; |
| 455 |
|
| 456 |
// Handle the Jacobian-free case |
| 457 |
// This call allows to resample the linearization point |
| 458 |
// of the MFFD tangent operator |
| 459 |
PetscBool flg; |
| 460 |
PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMFFD, &flg)); |
| 461 |
if (flg) |
| 462 |
{ |
| 463 |
PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); |
| 464 |
PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); |
| 465 |
} |
| 466 |
else |
| 467 |
petsc_increment_state_counter(A); |
| 468 |
PetscFunctionReturn(PETSC_SUCCESS); |
| 469 |
}; |
| 470 |
|
| 471 |
const auto snes_monitor = |
| 472 |
[](SNES snes, PetscInt it, PetscReal f, void *ctx) -> PetscErrorCode { |
| 473 |
PetscFunctionBeginUser; |
| 474 |
auto user = static_cast<NonlinearSolver *>(ctx); |
| 475 |
|
| 476 |
Vec x; |
| 477 |
PetscCall(SNESGetSolution(snes, &x)); |
| 478 |
VectorType xdealii(x); |
| 479 |
const int lineno = __LINE__; |
| 480 |
const int err = call_and_possibly_capture_snes_exception( |
| 481 |
user->monitor, user->pending_exception, {}, xdealii, it, f); |
| 482 |
if (err) |
| 483 |
return PetscError( |
| 484 |
PetscObjectComm((PetscObject)snes), |
| 485 |
lineno + 1, |
| 486 |
"monitor", |
| 487 |
__FILE__, |
| 488 |
PETSC_ERR_LIB, |
| 489 |
PETSC_ERROR_INITIAL, |
| 490 |
"Failure in snes_monitor from dealii::PETScWrappers::NonlinearSolver"); |
| 491 |
PetscFunctionReturn(PETSC_SUCCESS); |
| 492 |
}; |
| 493 |
|
| 494 |
const auto snes_objective = |
| 495 |
[](SNES snes, Vec x, PetscReal *f, void *ctx) -> PetscErrorCode { |
| 496 |
PetscFunctionBeginUser; |
| 497 |
auto user = static_cast<NonlinearSolver *>(ctx); |
| 498 |
|
| 499 |
real_type v; |
| 500 |
VectorType xdealii(x); |
| 501 |
const int lineno = __LINE__; |
| 502 |
const int err = call_and_possibly_capture_snes_exception( |
| 503 |
user->energy, |
| 504 |
user->pending_exception, |
| 505 |
[snes, &v]() -> void { |
| 506 |
AssertPETSc(SNESSetFunctionDomainError(snes)); |
| 507 |
v = std::numeric_limits<real_type>::quiet_NaN(); |
| 508 |
}, |
| 509 |
xdealii, |
| 510 |
v); |
| 511 |
if (err) |
| 512 |
return PetscError( |
| 513 |
PetscObjectComm((PetscObject)snes), |
| 514 |
lineno + 1, |
| 515 |
"energy", |
| 516 |
__FILE__, |
| 517 |
PETSC_ERR_LIB, |
| 518 |
PETSC_ERROR_INITIAL, |
| 519 |
"Failure in snes_objective from dealii::PETScWrappers::NonlinearSolver"); |
| 520 |
*f = v; |
| 521 |
PetscFunctionReturn(PETSC_SUCCESS); |
| 522 |
}; |
| 523 |
|
| 524 |
AssertThrow(residual, |
| 525 |
StandardExceptions::ExcFunctionNotProvided("residual")); |
| 526 |
|
| 527 |
AssertPETSc(SNESSetSolution(snes, x.petsc_vector())); |
| 528 |
|
| 529 |
AssertPETSc(SNESSetFunction(snes, nullptr, snes_function, this)); |
| 530 |
|
| 531 |
if (energy) |
| 532 |
AssertPETSc(SNESSetObjective(snes, snes_objective, this)); |
| 533 |
|
| 534 |
this->need_dummy_assemble = false; |
| 535 |
if (setup_jacobian) |
| 536 |
{ |
| 537 |
AssertPETSc(SNESSetJacobian(snes, |
| 538 |
A ? A->petsc_matrix() : nullptr, |
| 539 |
P ? P->petsc_matrix() : nullptr, |
| 540 |
snes_jacobian_with_setup, |
| 541 |
this)); |
| 542 |
|
| 543 |
// Tell PETSc to set up a MFFD operator for the linear system matrix |
| 544 |
if (!A) |
| 545 |
set_use_matrix_free(snes, true, false); |
| 546 |
|
| 547 |
// Do not waste memory by creating a dummy AIJ matrix inside PETSc. |
| 548 |
if (!P) |
| 549 |
{ |
| 550 |
# if DEAL_II_PETSC_VERSION_GTE(3, 13, 0) |
| 551 |
DM dm; |
| 552 |
AssertPETSc(SNESGetDM(snes, &dm)); |
| 553 |
AssertPETSc(DMSetMatType(dm, MATSHELL)); |
| 554 |
# else |
| 555 |
this->need_dummy_assemble = true; |
| 556 |
# endif |
| 557 |
} |
| 558 |
} |
| 559 |
else |
| 560 |
{ |
| 561 |
if (jacobian) |
| 562 |
{ |
| 563 |
AssertPETSc(SNESSetJacobian(snes, |
| 564 |
A ? A->petsc_matrix() : |
| 565 |
(P ? P->petsc_matrix() : nullptr), |
| 566 |
P ? P->petsc_matrix() : nullptr, |
| 567 |
snes_jacobian, |
| 568 |
this)); |
| 569 |
} |
| 570 |
else |
| 571 |
// The user did not set any Jacobian callback. PETSc default in this |
| 572 |
// case is to use FD and thus assemble a dense operator by finite |
| 573 |
// differencing the residual callbacks. Here instead we decide to |
| 574 |
// use a full matrix-free approach by default. This choice can always |
| 575 |
// be overridden from command line. |
| 576 |
{ |
| 577 |
set_use_matrix_free(snes, false, true); |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
// In case solve_with_jacobian is provided, create a shell |
| 582 |
// preconditioner wrapping the user call. The default internal Krylov |
| 583 |
// solver only applies the preconditioner. This choice |
| 584 |
// can be overridden by command line and users can use any other |
| 585 |
// Krylov method if their solve is not accurate enough. |
| 586 |
// Using solve_with_jacobian as a preconditioner allows users |
| 587 |
// to provide approximate solvers and possibly iterate on a matrix-free |
| 588 |
// approximation of the tangent operator. |
| 589 |
PreconditionShell precond( |
| 590 |
PetscObjectComm(reinterpret_cast<PetscObject>(snes))); |
| 591 |
if (solve_with_jacobian) |
| 592 |
{ |
| 593 |
precond.vmult = [&](VectorBase &indst, |
| 594 |
const VectorBase &insrc) -> void { |
| 595 |
VectorType dst(static_cast<const Vec &>(indst)); |
| 596 |
const VectorType src(static_cast<const Vec &>(insrc)); |
| 597 |
solve_with_jacobian(src, dst); |
| 598 |
}; |
| 599 |
|
| 600 |
// Default Krylov solver (preconditioner only) |
| 601 |
KSP ksp; |
| 602 |
AssertPETSc(SNESGetKSP(snes, &ksp)); |
| 603 |
AssertPETSc(KSPSetType(ksp, KSPPREONLY)); |
| 604 |
AssertPETSc(KSPSetPC(ksp, precond.get_pc())); |
| 605 |
} |
| 606 |
|
| 607 |
// Attach user monitoring routine |
| 608 |
if (monitor) |
| 609 |
AssertPETSc(SNESMonitorSet(snes, snes_monitor, this, nullptr)); |
| 610 |
|
| 611 |
// Allow command line customization. |
| 612 |
AssertPETSc(SNESSetFromOptions(snes)); |
| 613 |
|
| 614 |
// Having set everything up, now do the actual work |
| 615 |
// and let PETSc solve the system. |
| 616 |
// Older versions of PETSc requires the solution vector specified even |
| 617 |
// if we specified SNESSetSolution upfront. |
| 618 |
// |
| 619 |
// If there is a pending exception, then one of the user callbacks |
| 620 |
// threw an exception we didn't know how to deal with |
| 621 |
// at the time. It is possible that PETSc managed to |
| 622 |
// recover anyway and in that case would have returned |
| 623 |
// a zero error code -- if so, just eat the exception and |
| 624 |
// continue on; otherwise, just rethrow the exception |
| 625 |
// and get outta here. |
| 626 |
const PetscErrorCode status = SNESSolve(snes, nullptr, x.petsc_vector()); |
| 627 |
if (pending_exception) |
| 628 |
{ |
| 629 |
try |
| 630 |
{ |
| 631 |
std::rethrow_exception(pending_exception); |
| 632 |
} |
| 633 |
catch (...) |
| 634 |
{ |
| 635 |
pending_exception = nullptr; |
| 636 |
if (status == 0) |
| 637 |
/* just eat the exception */; |
| 638 |
else |
| 639 |
throw; |
| 640 |
} |
| 641 |
} |
| 642 |
AssertPETSc(status); |
| 643 |
|
| 644 |
// Get the number of steps taken. |
| 645 |
PetscInt n_iterations; |
| 646 |
AssertPETSc(SNESGetIterationNumber(snes, &n_iterations)); |
| 647 |
|
| 648 |
// Raise an exception if the solver has not converged |
| 649 |
SNESConvergedReason reason; |
| 650 |
AssertPETSc(SNESGetConvergedReason(snes, &reason)); |
| 651 |
AssertThrow(reason > 0, |
| 652 |
ExcMessage("SNES solver did not converge after " + |
| 653 |
std::to_string(n_iterations) + |
| 654 |
" iterations with reason " + |
| 655 |
SNESConvergedReasons[reason])); |
| 656 |
|
| 657 |
// Finally return |
| 658 |
return n_iterations; |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 664 |
DEAL_II_CXX20_REQUIRES( |
| 665 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 666 |
std::constructible_from< |
| 667 |
VectorType, |
| 668 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 669 |
std::constructible_from< |
| 670 |
PMatrixType, |
| 671 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 672 |
std::constructible_from<AMatrixType, Mat>)) |
| 673 |
unsigned int NonlinearSolver<VectorType, PMatrixType, AMatrixType>::solve( |
| 674 |
VectorType &x, |
| 675 |
PMatrixType &P) |
| 676 |
{ |
| 677 |
set_matrix(P); |
| 678 |
return solve(x); |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
template <typename VectorType, typename PMatrixType, typename AMatrixType> |
| 684 |
DEAL_II_CXX20_REQUIRES( |
| 685 |
(concepts::is_dealii_petsc_vector_type<VectorType> || |
| 686 |
std::constructible_from< |
| 687 |
VectorType, |
| 688 |
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> || |
| 689 |
std::constructible_from< |
| 690 |
PMatrixType, |
| 691 |
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> || |
| 692 |
std::constructible_from<AMatrixType, Mat>)) |
| 693 |
unsigned int NonlinearSolver<VectorType, PMatrixType, AMatrixType>::solve( |
| 694 |
VectorType &x, |
| 695 |
AMatrixType &A, |
| 696 |
PMatrixType &P) |
| 697 |
{ |
| 698 |
set_matrices(A, P); |
| 699 |
return solve(x); |
| 700 |
} |
| 701 |
|
| 702 |
} // namespace PETScWrappers |
| 703 |
|
| 704 |
# undef AssertPETSc |
| 705 |
# if defined(undefPetscCall) |
| 706 |
# undef PetscCall |
| 707 |
# undef undefPetscCall |
| 708 |
# endif |
| 709 |
|
| 710 |
DEAL_II_NAMESPACE_CLOSE |
| 711 |
|
| 712 |
#else |
| 713 |
|
| 714 |
// Make sure the scripts that create the C++20 module input files have |
| 715 |
// something to latch on if the preprocessor #ifdef above would |
| 716 |
// otherwise lead to an empty content of the file. |
| 717 |
DEAL_II_NAMESPACE_OPEN |
| 718 |
DEAL_II_NAMESPACE_CLOSE |
| 719 |
|
| 720 |
#endif // DEAL_II_WITH_PETSC |
| 721 |
|
| 722 |
#endif |