-
Notifications
You must be signed in to change notification settings - Fork 708
/
petsc_snes.templates.h
596 lines (510 loc) · 20.6 KB
/
petsc_snes.templates.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//-----------------------------------------------------------
//
// Copyright (C) 2023 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE.md at
// the top level directory of deal.II.
//
//---------------------------------------------------------------
#ifndef dealii_petsc_snes_templates_h
#define dealii_petsc_snes_templates_h
#include <deal.II/base/config.h>
#ifdef DEAL_II_WITH_PETSC
# include <deal.II/base/exceptions.h>
# include <deal.II/lac/petsc_precondition.h>
# include <deal.II/lac/petsc_snes.h>
# include <petscdm.h>
# include <petscsnes.h>
DEAL_II_NAMESPACE_OPEN
// Shorthand notation for PETSc error codes.
# define AssertPETSc(code) \
do \
{ \
PetscErrorCode ierr = (code); \
AssertThrow(ierr == 0, ExcPETScError(ierr)); \
} \
while (0)
namespace PETScWrappers
{
namespace
{
/**
* A function that calls the function object given by its first argument
* with the set of arguments following at the end. If the call returns
* regularly, the current function returns zero to indicate success. If
* the call fails with an exception, then the current function returns with
* an error code of -1. In that case, the exception thrown by `f` is
* captured and `eptr` is set to the exception. In case of success,
* `eptr` is set to `nullptr`.
*/
template <typename F, typename... Args>
int
call_and_possibly_capture_exception(const F & f,
std::exception_ptr &eptr,
Args &&...args)
{
// See whether there is already something in the exception pointer
// variable. There is no reason why this should be so, and
// we should probably bail out:
AssertThrow(eptr == nullptr, ExcInternalError());
// Call the function and if that succeeds, return zero:
try
{
f(std::forward<Args>(args)...);
eptr = nullptr;
return 0;
}
// In case of an exception, capture the exception and
// return -1:
catch (...)
{
eptr = std::current_exception();
return -1;
}
}
} // namespace
template <typename VectorType, typename PMatrixType, typename AMatrixType>
DEAL_II_CXX20_REQUIRES(
(concepts::is_dealii_petsc_vector_type<VectorType> ||
std::constructible_from<
VectorType,
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> ||
std::constructible_from<
PMatrixType,
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> ||
std::constructible_from<AMatrixType, Mat>))
NonlinearSolver<VectorType, PMatrixType, AMatrixType>::NonlinearSolver(
const NonlinearSolverData &data,
const MPI_Comm mpi_comm)
: pending_exception(nullptr)
{
AssertPETSc(SNESCreate(mpi_comm, &snes));
AssertPETSc(SNESSetApplicationContext(snes, this));
reinit(data);
}
template <typename VectorType, typename PMatrixType, typename AMatrixType>
DEAL_II_CXX20_REQUIRES(
(concepts::is_dealii_petsc_vector_type<VectorType> ||
std::constructible_from<
VectorType,
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> ||
std::constructible_from<
PMatrixType,
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> ||
std::constructible_from<AMatrixType, Mat>))
NonlinearSolver<VectorType, PMatrixType, AMatrixType>::operator SNES() const
{
return snes;
}
template <typename VectorType, typename PMatrixType, typename AMatrixType>
DEAL_II_CXX20_REQUIRES(
(concepts::is_dealii_petsc_vector_type<VectorType> ||
std::constructible_from<
VectorType,
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> ||
std::constructible_from<
PMatrixType,
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> ||
std::constructible_from<AMatrixType, Mat>))
SNES NonlinearSolver<VectorType, PMatrixType, AMatrixType>::petsc_snes()
{
return snes;
}
template <typename VectorType, typename PMatrixType, typename AMatrixType>
DEAL_II_CXX20_REQUIRES(
(concepts::is_dealii_petsc_vector_type<VectorType> ||
std::constructible_from<
VectorType,
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> ||
std::constructible_from<
PMatrixType,
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> ||
std::constructible_from<AMatrixType, Mat>))
MPI_Comm NonlinearSolver<VectorType, PMatrixType, AMatrixType>::
get_mpi_communicator() const
{
return PetscObjectComm(reinterpret_cast<PetscObject>(snes));
}
template <typename VectorType, typename PMatrixType, typename AMatrixType>
DEAL_II_CXX20_REQUIRES(
(concepts::is_dealii_petsc_vector_type<VectorType> ||
std::constructible_from<
VectorType,
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> ||
std::constructible_from<
PMatrixType,
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> ||
std::constructible_from<AMatrixType, Mat>))
NonlinearSolver<VectorType, PMatrixType, AMatrixType>::~NonlinearSolver()
{
AssertPETSc(SNESDestroy(&snes));
Assert(pending_exception == nullptr, ExcInternalError());
}
template <typename VectorType, typename PMatrixType, typename AMatrixType>
DEAL_II_CXX20_REQUIRES(
(concepts::is_dealii_petsc_vector_type<VectorType> ||
std::constructible_from<
VectorType,
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> ||
std::constructible_from<
PMatrixType,
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> ||
std::constructible_from<AMatrixType, Mat>))
void NonlinearSolver<VectorType, PMatrixType, AMatrixType>::reinit()
{
AssertPETSc(SNESReset(snes));
}
template <typename VectorType, typename PMatrixType, typename AMatrixType>
DEAL_II_CXX20_REQUIRES(
(concepts::is_dealii_petsc_vector_type<VectorType> ||
std::constructible_from<
VectorType,
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> ||
std::constructible_from<
PMatrixType,
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> ||
std::constructible_from<AMatrixType, Mat>))
void NonlinearSolver<VectorType, PMatrixType, AMatrixType>::reinit(
const NonlinearSolverData &data)
{
reinit();
// Solver type
if (data.snes_type.size())
AssertPETSc(SNESSetType(snes, data.snes_type.c_str()));
// Linesearch type
if (data.snes_linesearch_type.size())
{
SNESLineSearch linesearch;
AssertPETSc(SNESGetLineSearch(snes, &linesearch));
AssertPETSc(
SNESLineSearchSetType(linesearch, data.snes_linesearch_type.c_str()));
}
// Options prefix
if (data.options_prefix.size())
AssertPETSc(SNESSetOptionsPrefix(snes, data.options_prefix.c_str()));
// Solver tolerances
PetscReal atol = data.absolute_tolerance > 0.0 ?
data.absolute_tolerance :
static_cast<PetscReal>(PETSC_DEFAULT);
PetscReal rtol = data.relative_tolerance > 0.0 ?
data.relative_tolerance :
static_cast<PetscReal>(PETSC_DEFAULT);
PetscReal stol = data.step_tolerance > 0.0 ?
data.step_tolerance :
static_cast<PetscReal>(PETSC_DEFAULT);
// Maximum number of iterations and function evaluations.
PetscInt maxit = data.maximum_non_linear_iterations >= 0 ?
data.maximum_non_linear_iterations :
static_cast<PetscInt>(PETSC_DEFAULT);
PetscInt maxfe = data.max_n_function_evaluations >= 0 ?
data.max_n_function_evaluations :
static_cast<PetscInt>(PETSC_DEFAULT);
AssertPETSc(SNESSetTolerances(snes, atol, rtol, stol, maxit, maxfe));
}
template <typename VectorType, typename PMatrixType, typename AMatrixType>
DEAL_II_CXX20_REQUIRES(
(concepts::is_dealii_petsc_vector_type<VectorType> ||
std::constructible_from<
VectorType,
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> ||
std::constructible_from<
PMatrixType,
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> ||
std::constructible_from<AMatrixType, Mat>))
void NonlinearSolver<VectorType, PMatrixType, AMatrixType>::set_matrix(
PMatrixType &P)
{
this->A = nullptr;
this->P = &P;
}
template <typename VectorType, typename PMatrixType, typename AMatrixType>
DEAL_II_CXX20_REQUIRES(
(concepts::is_dealii_petsc_vector_type<VectorType> ||
std::constructible_from<
VectorType,
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> ||
std::constructible_from<
PMatrixType,
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> ||
std::constructible_from<AMatrixType, Mat>))
void NonlinearSolver<VectorType, PMatrixType, AMatrixType>::set_matrices(
AMatrixType &A,
PMatrixType &P)
{
this->A = &A;
this->P = &P;
}
template <typename VectorType, typename PMatrixType, typename AMatrixType>
DEAL_II_CXX20_REQUIRES(
(concepts::is_dealii_petsc_vector_type<VectorType> ||
std::constructible_from<
VectorType,
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> ||
std::constructible_from<
PMatrixType,
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> ||
std::constructible_from<AMatrixType, Mat>))
unsigned int NonlinearSolver<VectorType, PMatrixType, AMatrixType>::solve(
VectorType &x)
{
const auto snes_function =
[](SNES, Vec x, Vec f, void *ctx) -> PetscErrorCode {
PetscFunctionBeginUser;
auto user = static_cast<NonlinearSolver *>(ctx);
VectorType xdealii(x);
VectorType fdealii(f);
const int err = call_and_possibly_capture_exception(
user->residual, user->pending_exception, xdealii, fdealii);
petsc_increment_state_counter(f);
PetscFunctionReturn(err);
};
const auto snes_jacobian =
[](SNES, Vec x, Mat A, Mat P, void *ctx) -> PetscErrorCode {
PetscFunctionBeginUser;
auto user = static_cast<NonlinearSolver *>(ctx);
VectorType xdealii(x);
AMatrixType Adealii(A);
PMatrixType Pdealii(P);
const int err = call_and_possibly_capture_exception(
user->jacobian, user->pending_exception, xdealii, Adealii, Pdealii);
petsc_increment_state_counter(P);
// Handle the Jacobian-free case
// This call allows to resample the linearization point
// of the MFFD tangent operator
PetscBool flg;
AssertPETSc(PetscObjectTypeCompare((PetscObject)A, MATMFFD, &flg));
if (flg)
{
AssertPETSc(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
AssertPETSc(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
}
else
petsc_increment_state_counter(A);
PetscFunctionReturn(err);
};
const auto snes_jacobian_with_setup =
[](SNES, Vec x, Mat A, Mat P, void *ctx) -> PetscErrorCode {
PetscFunctionBeginUser;
auto user = static_cast<NonlinearSolver *>(ctx);
VectorType xdealii(x);
AMatrixType Adealii(A);
PMatrixType Pdealii(P);
user->A = &Adealii;
user->P = &Pdealii;
const int err =
call_and_possibly_capture_exception(user->setup_jacobian,
user->pending_exception,
xdealii);
petsc_increment_state_counter(P);
// Handle older versions of PETSc for which we cannot pass a MATSHELL
// matrix to DMSetMatType. This has been fixed from 3.13 on.
if (user->need_dummy_assemble)
{
AssertPETSc(MatZeroEntries(P));
AssertPETSc(MatAssemblyBegin(P, MAT_FINAL_ASSEMBLY));
AssertPETSc(MatAssemblyEnd(P, MAT_FINAL_ASSEMBLY));
}
// Handle the Jacobian-free case
// This call allows to resample the linearization point
// of the MFFD tangent operator
PetscBool flg;
AssertPETSc(PetscObjectTypeCompare((PetscObject)A, MATMFFD, &flg));
if (flg)
{
AssertPETSc(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
AssertPETSc(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
}
else
petsc_increment_state_counter(A);
PetscFunctionReturn(err);
};
const auto snes_monitor =
[](SNES snes, PetscInt it, PetscReal f, void *ctx) -> PetscErrorCode {
PetscFunctionBeginUser;
auto user = static_cast<NonlinearSolver *>(ctx);
Vec x;
AssertPETSc(SNESGetSolution(snes, &x));
VectorType xdealii(x);
const int err = call_and_possibly_capture_exception(
user->monitor, user->pending_exception, xdealii, it, f);
PetscFunctionReturn(err);
};
const auto snes_objective =
[](SNES, Vec x, PetscReal *f, void *ctx) -> PetscErrorCode {
PetscFunctionBeginUser;
auto user = static_cast<NonlinearSolver *>(ctx);
real_type v;
VectorType xdealii(x);
const int err = call_and_possibly_capture_exception(
user->energy, user->pending_exception, xdealii, v);
*f = v;
PetscFunctionReturn(err);
};
AssertThrow(residual,
StandardExceptions::ExcFunctionNotProvided("residual"));
AssertPETSc(SNESSetSolution(snes, x.petsc_vector()));
AssertPETSc(SNESSetFunction(snes, nullptr, snes_function, this));
if (energy)
AssertPETSc(SNESSetObjective(snes, snes_objective, this));
if (setup_jacobian)
{
AssertPETSc(SNESSetJacobian(snes,
A ? A->petsc_matrix() : nullptr,
P ? P->petsc_matrix() : nullptr,
snes_jacobian_with_setup,
this));
if (!A)
set_use_matrix_free(snes, true, false);
// Do not waste memory by creating a dummy AIJ matrix inside PETSc.
this->need_dummy_assemble = false;
if (!P)
{
# if DEAL_II_PETSC_VERSION_GTE(3, 13, 0)
DM dm;
AssertPETSc(SNESGetDM(snes, &dm));
AssertPETSc(DMSetMatType(dm, MATSHELL));
# else
this->need_dummy_assemble = true;
# endif
}
}
else
{
if (jacobian)
{
AssertPETSc(SNESSetJacobian(snes,
A ? A->petsc_matrix() :
(P ? P->petsc_matrix() : nullptr),
P ? P->petsc_matrix() : nullptr,
snes_jacobian,
this));
}
else
// The user did not set any Jacobian callback. PETSc default in this
// case is to use FD and thus assemble a dense operator by finite
// differencing the residual callbacks. Here instead we decide to
// use a full matrix-free approach by default. This choice can always
// be overriden from command line.
{
set_use_matrix_free(snes, false, true);
}
}
// In case solve_with_jacobian is provided, create a shell
// preconditioner wrapping the user call. The internal Krylov
// solver will apply the preconditioner only once. This choice
// can be overriden by command line and users can use any other
// Krylov method if their solve is not accurate enough.
// Using solve_with_jacobian as a preconditioner allows users
// to provide approximate solvers and possibly iterate on a matrix-free
// approximation of the tangent operator.
PreconditionShell precond(
PetscObjectComm(reinterpret_cast<PetscObject>(snes)));
if (solve_with_jacobian)
{
precond.vmult = [&](VectorBase &indst, const VectorBase &insrc) -> int {
VectorType dst(static_cast<const Vec &>(indst));
const VectorType src(static_cast<const Vec &>(insrc));
return call_and_possibly_capture_exception(solve_with_jacobian,
pending_exception,
src,
dst);
};
// Default Krylov solver (preconditioner only)
KSP ksp;
AssertPETSc(SNESGetKSP(snes, &ksp));
AssertPETSc(KSPSetType(ksp, KSPPREONLY));
AssertPETSc(KSPSetPC(ksp, precond.get_pc()));
}
// Attach user monitoring routine
if (monitor)
AssertPETSc(SNESMonitorSet(snes, snes_monitor, this, nullptr));
// Allow command line customization.
AssertPETSc(SNESSetFromOptions(snes));
// Having set everything up, now do the actual work
// and let PETSc solve the system.
// Older versions of PETSc requires the solution vector specified even
// if we specified SNESSetSolution upfront.
//
// If there is a pending exception, then one of the user callbacks
// threw an exception we didn't know how to deal with
// at the time. It is possible that PETSc managed to
// recover anyway and in that case would have returned
// a zero error code -- if so, just eat the exception and
// continue on; otherwise, just rethrow the exception
// and get outta here.
const int status = SNESSolve(snes, nullptr, x.petsc_vector());
if (pending_exception)
{
try
{
std::rethrow_exception(pending_exception);
}
catch (...)
{
pending_exception = nullptr;
if (status == 0)
/* just eat the exception */;
else
throw;
}
}
AssertPETSc(status);
// Get the number of steps taken.
PetscInt nt;
AssertPETSc(SNESGetIterationNumber(snes, &nt));
// Raise an exception if the solver has not converged
SNESConvergedReason reason;
AssertPETSc(SNESGetConvergedReason(snes, &reason));
AssertThrow(reason > 0,
ExcMessage("SNES solver did not converge after " +
std::to_string(nt) + " iterations with reason " +
SNESConvergedReasons[reason]));
// Finally return
return nt;
}
template <typename VectorType, typename PMatrixType, typename AMatrixType>
DEAL_II_CXX20_REQUIRES(
(concepts::is_dealii_petsc_vector_type<VectorType> ||
std::constructible_from<
VectorType,
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> ||
std::constructible_from<
PMatrixType,
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> ||
std::constructible_from<AMatrixType, Mat>))
unsigned int NonlinearSolver<VectorType, PMatrixType, AMatrixType>::solve(
VectorType & x,
PMatrixType &P)
{
set_matrix(P);
return solve(x);
}
template <typename VectorType, typename PMatrixType, typename AMatrixType>
DEAL_II_CXX20_REQUIRES(
(concepts::is_dealii_petsc_vector_type<VectorType> ||
std::constructible_from<
VectorType,
Vec>)&&(concepts::is_dealii_petsc_matrix_type<PMatrixType> ||
std::constructible_from<
PMatrixType,
Mat>)&&(concepts::is_dealii_petsc_matrix_type<AMatrixType> ||
std::constructible_from<AMatrixType, Mat>))
unsigned int NonlinearSolver<VectorType, PMatrixType, AMatrixType>::solve(
VectorType & x,
AMatrixType &A,
PMatrixType &P)
{
set_matrices(A, P);
return solve(x);
}
} // namespace PETScWrappers
# undef AssertPETSc
DEAL_II_NAMESPACE_CLOSE
#endif // DEAL_II_WITH_PETSC
#endif