AMDiS 2.10
The Adaptive Multi-Dimensional Simulation Toolbox
Interface.hpp
1#pragma once
2
3#include <petscksp.h>
4#include <petsc/private/kspimpl.h>
5
6#include <amdis/Output.hpp>
7
8namespace AMDiS
9{
10 namespace PETSc
11 {
12 // KSP routines
13
15 inline PetscErrorCode KSPMonitorCyclic(KSP ksp, PetscInt n, PetscReal rnorm, void* ctx)
16 {
17 if (n % 10 == 0)
18 msg("iteration {:>4}: resid {:<.6e}", n, rnorm);
19 return 0;
20 }
21
23 inline PetscErrorCode KSPMonitorNoisy(KSP ksp, PetscInt n, PetscReal rnorm, void* ctx)
24 {
25 Vec resid;
26 KSPBuildResidual(ksp, NULL, NULL, &resid);
27
28 PetscReal truenorm = 0.0;
29 VecNorm(resid, NORM_2, &truenorm);
30 VecDestroy(&resid);
31
32 PetscReal bnorm = 0.0;
33 VecNorm(ksp->vec_rhs, NORM_2, &bnorm);
34
35 msg("iteration {:>4}: resid {:<.6e}, true resid {:<.2e}, rel. resid {:<.2e}", n, rnorm, truenorm, truenorm/bnorm);
36 return 0;
37 }
38
40 inline PetscErrorCode KSPMonitorTrueResidual(KSP ksp, PetscInt n, PetscReal rnorm, void* ctx)
41 {
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));
47#else
48 return ::KSPMonitorTrueResidualNorm(ksp, n, rnorm, ctx);
49#endif
50 }
51
53 inline PetscErrorCode KSPMonitorResidual(KSP ksp, PetscInt n, PetscReal rnorm, void* ctx)
54 {
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));
60#else
61 return ::KSPMonitorDefault(ksp, n, rnorm, ctx);
62#endif
63 }
64
66 template <class Monitor>
67 inline PetscErrorCode KSPMonitorSet(KSP ksp, Monitor monitor)
68 {
69#if (PETSC_VERSION_MINOR >= 7)
70 PetscViewerAndFormat *vf;
71 PetscErrorCode ierr;
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);
74 return ierr;
75#else
76 return ::KSPMonitorSet(ksp, monitor, PETSC_NULL, PETSC_NULL);
77#endif
78 }
79
81 inline PetscErrorCode KSPGetOperators(KSP ksp, Mat *Amat, Mat *Pmat)
82 {
83#if (PETSC_VERSION_MINOR >= 5)
84 return ::KSPGetOperators(ksp, Amat, Pmat);
85#else
86 return ::KSPGetOperators(ksp, Amat, Pmat, PETSC_NULL);
87#endif
88 }
89
91 inline PetscErrorCode KSPSetOperators(KSP ksp, Mat Amat, Mat Pmat)
92 {
93#if (PETSC_VERSION_MINOR >= 5)
94 return ::KSPSetOperators(ksp, Amat, Pmat);
95#else
96 return ::KSPSetOperators(ksp, Amat, Pmat, SAME_NONZERO_PATTERN);
97#endif
98 }
99
101 inline PetscErrorCode KSPConvergedReasonView(KSP ksp, PetscViewer viewer)
102 {
103#if (PETSC_VERSION_MINOR >= 14)
104 return ::KSPConvergedReasonView(ksp, viewer);
105#else
106 return ::KSPReasonView(ksp, viewer);
107#endif
108 }
109
110 // Mat routines
111
113 inline PetscErrorCode MatCreateVecs(Mat mat, Vec *right, Vec *left)
114 {
115#if (PETSC_VERSION_MINOR >= 6)
116 return ::MatCreateVecs(mat, right, left);
117#else
118 return ::MatGetVecs(mat, right, left);
119#endif
120 }
121
123 inline PetscErrorCode MatCreateSubMatrix(Mat mat, IS isrow, IS iscol, MatReuse cll, Mat *newmat)
124 {
125#if (PETSC_VERSION_MINOR >= 8)
126 return ::MatCreateSubMatrix(mat, isrow, iscol, cll, newmat);
127#else
128 return ::MatGetSubMatrix(mat, isrow, iscol, cll, newmat);
129#endif
130 }
131
133 inline PetscErrorCode MatNullSpaceRemove(MatNullSpace sp, Vec vec)
134 {
135#if (PETSC_VERSION_MINOR >= 5)
136 return ::MatNullSpaceRemove(sp, vec);
137#else
138 return ::MatNullSpaceRemove(sp, vec, PETSC_NULL);
139#endif
140 }
141
142 // PC routines
143
144#if (PETSC_VERSION_MINOR >= 9)
146 inline PetscErrorCode PCFactorSetMatSolverType(PC pc, MatSolverType stype)
147 {
148 return ::PCFactorSetMatSolverType(pc, stype);
149 }
150#else
152 template <class MatSolverType>
153 inline PetscErrorCode PCFactorSetMatSolverType(PC pc, MatSolverType stype)
154 {
155 return ::PCFactorSetMatSolverPackage(pc, stype);
156 }
157#endif
158
160 inline PetscErrorCode PetscOptionsView(PetscViewer viewer)
161 {
162#if (PETSC_VERSION_MINOR >= 7)
163 return ::PetscOptionsView(PETSC_NULL, viewer);
164#else
165 return ::PetscOptionsView(viewer);
166#endif
167 }
168
169 // PETSc routine
170
172 inline PetscErrorCode PetscOptionsInsertString(const char in_str[])
173 {
174#if (PETSC_VERSION_MINOR >= 7)
175 return ::PetscOptionsInsertString(PETSC_NULL, in_str);
176#else
177 return ::PetscOptionsInsertString(in_str);
178#endif
179 }
180
181 } // end namespace PETSc
182} // end namespace AMDiS