4#include <petsc/private/kspimpl.h>
6#include <amdis/Output.hpp>
15 inline PetscErrorCode KSPMonitorCyclic(KSP ksp, PetscInt n, PetscReal rnorm,
void* ctx)
18 msg(
"iteration {:>4}: resid {:<.6e}", n, rnorm);
23 inline PetscErrorCode KSPMonitorNoisy(KSP ksp, PetscInt n, PetscReal rnorm,
void* ctx)
26 KSPBuildResidual(ksp, NULL, NULL, &resid);
28 PetscReal truenorm = 0.0;
29 VecNorm(resid, NORM_2, &truenorm);
32 PetscReal bnorm = 0.0;
33 VecNorm(ksp->vec_rhs, NORM_2, &bnorm);
35 msg(
"iteration {:>4}: resid {:<.6e}, true resid {:<.2e}, rel. resid {:<.2e}", n, rnorm, truenorm, truenorm/bnorm);
40 inline PetscErrorCode KSPMonitorTrueResidual(KSP ksp, PetscInt n, PetscReal rnorm,
void* ctx)
42 assert(ctx !=
nullptr);
43#if (PETSC_VERSION_MINOR >= 15)
44 return ::KSPMonitorTrueResidual(ksp, n, rnorm, (PetscViewerAndFormat*)(ctx));
45#elif (PETSC_VERSION_MINOR >= 7)
46 return ::KSPMonitorTrueResidualNorm(ksp, n, rnorm, (PetscViewerAndFormat*)(ctx));
48 return ::KSPMonitorTrueResidualNorm(ksp, n, rnorm, ctx);
53 inline PetscErrorCode KSPMonitorResidual(KSP ksp, PetscInt n, PetscReal rnorm,
void* ctx)
55 assert(ctx !=
nullptr);
56#if (PETSC_VERSION_MINOR >= 15)
57 return ::KSPMonitorResidual(ksp, n, rnorm, (PetscViewerAndFormat*)(ctx));
58#elif (PETSC_VERSION_MINOR >= 7)
59 return ::KSPMonitorDefault(ksp, n, rnorm, (PetscViewerAndFormat*)(ctx));
61 return ::KSPMonitorDefault(ksp, n, rnorm, ctx);
66 template <
class Monitor>
67 inline PetscErrorCode KSPMonitorSet(KSP ksp, Monitor monitor)
69#if (PETSC_VERSION_MINOR >= 7)
70 PetscViewerAndFormat *vf;
72 ierr = ::PetscViewerAndFormatCreate(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_DEFAULT,&vf);CHKERRQ(ierr);
73 ierr = ::KSPMonitorSet(ksp,(PetscErrorCode (*)(KSP,PetscInt,PetscReal,
void*))monitor,vf,(PetscErrorCode (*)(
void**))PetscViewerAndFormatDestroy);CHKERRQ(ierr);
76 return ::KSPMonitorSet(ksp, monitor, PETSC_NULL, PETSC_NULL);
81 inline PetscErrorCode KSPGetOperators(KSP ksp, Mat *Amat, Mat *Pmat)
83#if (PETSC_VERSION_MINOR >= 5)
84 return ::KSPGetOperators(ksp, Amat, Pmat);
86 return ::KSPGetOperators(ksp, Amat, Pmat, PETSC_NULL);
91 inline PetscErrorCode KSPSetOperators(KSP ksp, Mat Amat, Mat Pmat)
93#if (PETSC_VERSION_MINOR >= 5)
94 return ::KSPSetOperators(ksp, Amat, Pmat);
96 return ::KSPSetOperators(ksp, Amat, Pmat, SAME_NONZERO_PATTERN);
101 inline PetscErrorCode KSPConvergedReasonView(KSP ksp, PetscViewer viewer)
103#if (PETSC_VERSION_MINOR >= 14)
104 return ::KSPConvergedReasonView(ksp, viewer);
106 return ::KSPReasonView(ksp, viewer);
113 inline PetscErrorCode MatCreateVecs(Mat mat, Vec *right, Vec *left)
115#if (PETSC_VERSION_MINOR >= 6)
116 return ::MatCreateVecs(mat, right, left);
118 return ::MatGetVecs(mat, right, left);
123 inline PetscErrorCode MatCreateSubMatrix(Mat mat, IS isrow, IS iscol, MatReuse cll, Mat *newmat)
125#if (PETSC_VERSION_MINOR >= 8)
126 return ::MatCreateSubMatrix(mat, isrow, iscol, cll, newmat);
128 return ::MatGetSubMatrix(mat, isrow, iscol, cll, newmat);
133 inline PetscErrorCode MatNullSpaceRemove(MatNullSpace sp, Vec vec)
135#if (PETSC_VERSION_MINOR >= 5)
136 return ::MatNullSpaceRemove(sp, vec);
138 return ::MatNullSpaceRemove(sp, vec, PETSC_NULL);
144#if (PETSC_VERSION_MINOR >= 9)
146 inline PetscErrorCode PCFactorSetMatSolverType(PC pc, MatSolverType stype)
148 return ::PCFactorSetMatSolverType(pc, stype);
152 template <
class MatSolverType>
153 inline PetscErrorCode PCFactorSetMatSolverType(PC pc, MatSolverType stype)
155 return ::PCFactorSetMatSolverPackage(pc, stype);
160 inline PetscErrorCode PetscOptionsView(PetscViewer viewer)
162#if (PETSC_VERSION_MINOR >= 7)
163 return ::PetscOptionsView(PETSC_NULL, viewer);
165 return ::PetscOptionsView(viewer);
172 inline PetscErrorCode PetscOptionsInsertString(
const char in_str[])
174#if (PETSC_VERSION_MINOR >= 7)
175 return ::PetscOptionsInsertString(PETSC_NULL, in_str);
177 return ::PetscOptionsInsertString(in_str);