Actual source code: ex109.c
1: static char help[] = "Tests boundary integrals on every face of a hexahedral box mesh.\n\n";
3: /*
4: For each Face Sets value, the boundary load
6: L(w) = int_face g w dS
8: is assembled with DMPlexComputeBdResidualSingle(), and compared with a 10x10 Gauss rule on the face.
9: Here
11: g = 1 + x + 2y + 3z + 4xyz
12: w = (1+x)(1+y)(1+z),
14: so the element quadrature integrates g w exactly and every face should agree to rounding error.
16: The option -face_orientation o acts with orientation o on every boundary face, to test rotated and reflected faces.
18: The option -check_jacobian instead assembles the boundary residual int_face grad u . grad v dS for u = w, and its Jacobian
19: with DMPlexComputeBdJacobianSingle(). The residual is linear in u, so it must equal the Jacobian times u. This check needs no
20: exact integral, so it also runs on meshes with non-affine cells.
21: */
23: #include <petscdmplex.h>
24: #include <petscds.h>
26: typedef struct {
27: PetscInt k; // FEM space degree
28: PetscInt o; // Orientation for faces
29: PetscBool jac; // Check the boundary residual against its Jacobian
30: } AppCtx;
32: static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
33: {
34: PetscFunctionBeginUser;
35: options->k = 1;
36: options->o = 0;
37: options->jac = PETSC_FALSE;
38: PetscOptionsBegin(comm, "", "Boundary integral options", "DMPLEX");
39: PetscCall(PetscOptionsInt("-k", "Polynomial degree of the Lagrange element", __FILE__, options->k, &options->k, NULL));
40: PetscCall(PetscOptionsInt("-face_orientation", "Orientation applied to every boundary face", __FILE__, options->o, &options->o, NULL));
41: PetscCall(PetscOptionsBool("-check_jacobian", "Check the boundary residual against its Jacobian", __FILE__, options->jac, &options->jac, NULL));
42: PetscOptionsEnd();
43: PetscFunctionReturn(PETSC_SUCCESS);
44: }
46: static PetscErrorCode CreateMesh(MPI_Comm comm, DM *dm)
47: {
48: PetscInt dim;
49: PetscBool simplex;
51: PetscFunctionBeginUser;
52: PetscCall(DMCreate(comm, dm));
53: PetscCall(DMSetType(*dm, DMPLEX));
54: PetscCall(DMSetFromOptions(*dm));
55: PetscCall(DMViewFromOptions(*dm, NULL, "-dm_view"));
57: PetscCall(DMGetDimension(*dm, &dim));
58: PetscCall(DMPlexIsSimplex(*dm, &simplex));
59: PetscCheck(dim == 3 && simplex == PETSC_FALSE, comm, PETSC_ERR_SUP, "This test requires a 3D hexahedral box mesh");
60: PetscFunctionReturn(PETSC_SUCCESS);
61: }
63: static PetscErrorCode OrientFaces(DM dm, DMLabel label, PetscInt o)
64: {
65: PetscInt fStart, fEnd;
67: PetscFunctionBeginUser;
68: if (!o) PetscFunctionReturn(PETSC_SUCCESS);
69: PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
70: for (PetscInt f = fStart; f < fEnd; ++f) {
71: PetscInt val;
73: PetscCall(DMLabelGetValue(label, f, &val));
74: if (val >= 0) PetscCall(DMPlexOrientPoint(dm, f, o));
75: }
76: PetscFunctionReturn(PETSC_SUCCESS);
77: }
79: // Quadrature of order k + 2 integrates g w exactly on every face
80: static PetscErrorCode SetupDiscretization(DM dm, AppCtx *ctx)
81: {
82: PetscFE fe;
83: DMPolytopeType ct;
84: PetscInt dim, cStart;
86: PetscFunctionBeginUser;
87: PetscCall(DMGetDimension(dm, &dim));
88: PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL));
89: PetscCall(DMPlexGetCellType(dm, cStart, &ct));
90: PetscCall(PetscFECreateLagrangeByCell(PETSC_COMM_SELF, dim, 1, ct, ctx->k, ctx->k + 2, &fe));
91: PetscCall(DMSetField(dm, 0, NULL, (PetscObject)fe));
92: PetscCall(PetscFEDestroy(&fe));
93: PetscCall(DMCreateDS(dm));
94: PetscFunctionReturn(PETSC_SUCCESS);
95: }
97: static PetscReal g_func(const PetscReal x[])
98: {
99: return 1.0 + x[0] + 2.0 * x[1] + 3.0 * x[2] + 4.0 * x[0] * x[1] * x[2];
100: }
102: static PetscErrorCode w_func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, PetscCtx ctx)
103: {
104: PetscFunctionBeginUser;
105: u[0] = (1.0 + x[0]) * (1.0 + x[1]) * (1.0 + x[2]);
106: PetscFunctionReturn(PETSC_SUCCESS);
107: }
109: static void f0_bd(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], const PetscReal n[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
110: {
111: f0[0] = g_func(x);
112: }
114: static void f1_bd_grad(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], const PetscReal n[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[])
115: {
116: for (PetscInt d = 0; d < dim; ++d) f1[d] = u_x[d];
117: }
119: static void g3_bd_identity(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, PetscReal u_tShift, const PetscReal x[], const PetscReal n[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g3[])
120: {
121: for (PetscInt d = 0; d < dim; ++d) g3[d * dim + d] = 1.0;
122: }
124: // DMPlexCreateBoxMesh() face values 1..6 are z = lower, z = upper, y = lower, y = upper, x = upper, x = lower
125: static const PetscInt faceAxis[6] = {2, 2, 1, 1, 0, 0};
126: static const PetscBool faceUpper[6] = {PETSC_FALSE, PETSC_TRUE, PETSC_FALSE, PETSC_TRUE, PETSC_TRUE, PETSC_FALSE};
127: static const char *const faceName[6] = {"z = lower", "z = upper", "y = lower", "y = upper", "x = upper", "x = lower"};
129: // Integrate g w over the face where coordinate axis is fixed at value s, using a 10x10 Gauss rule
130: static PetscErrorCode ReferenceFaceIntegral(const PetscReal lower[], const PetscReal upper[], PetscInt axis, PetscReal s, PetscReal *val)
131: {
132: const PetscInt nq = 10, a0 = (axis + 1) % 3, a1 = (axis + 2) % 3;
133: PetscReal qx[10], qw[10], sum = 0.0;
135: PetscFunctionBeginUser;
136: *val = 0.;
137: PetscCall(PetscDTGaussQuadrature(nq, -1.0, 1.0, qx, qw));
138: for (PetscInt i = 0; i < nq; ++i) {
139: for (PetscInt j = 0; j < nq; ++j) {
140: PetscReal x[3];
141: PetscScalar w;
143: x[axis] = s;
144: x[a0] = lower[a0] + 0.5 * (qx[i] + 1.0) * (upper[a0] - lower[a0]);
145: x[a1] = lower[a1] + 0.5 * (qx[j] + 1.0) * (upper[a1] - lower[a1]);
146: PetscCall(w_func(3, 0.0, x, 1, &w, NULL));
147: sum += qw[i] * qw[j] * g_func(x) * PetscRealPart(w);
148: }
149: }
150: *val = sum * 0.25 * (upper[a0] - lower[a0]) * (upper[a1] - lower[a1]);
151: PetscFunctionReturn(PETSC_SUCCESS);
152: }
154: // Integrate g against the test functions on each face, take the dot product with W, and compare with the exact integral of g w
155: static PetscErrorCode CheckFaceIntegrals(DM dm, DMLabel label, Vec W)
156: {
157: PetscWeakForm wf;
158: Vec locX, locF, F;
159: PetscReal lower[3], upper[3];
161: PetscFunctionBeginUser;
162: PetscCall(DMGetBoundingBox(dm, lower, upper));
163: PetscCall(PetscWeakFormCreate(PETSC_COMM_SELF, &wf));
164: for (PetscInt v = 1; v <= 6; ++v) PetscCall(PetscWeakFormSetIndexBdResidual(wf, label, v, 0, 0, 0, f0_bd, 0, NULL));
165: PetscCall(VecDuplicate(W, &F));
166: PetscCall(DMGetLocalVector(dm, &locX));
167: PetscCall(DMGetLocalVector(dm, &locF));
168: PetscCall(VecZeroEntries(locX));
169: for (PetscInt v = 1; v <= 6; ++v) {
170: const PetscInt axis = faceAxis[v - 1];
171: PetscFormKey key;
172: PetscScalar L;
173: PetscReal ref;
175: key.label = label;
176: key.value = v;
177: key.field = 0;
178: key.part = 0;
179: PetscCall(VecZeroEntries(locF));
180: PetscCall(DMPlexComputeBdResidualSingle(dm, wf, key, locX, NULL, 0.0, locF));
181: PetscCall(VecZeroEntries(F));
182: PetscCall(DMLocalToGlobal(dm, locF, ADD_VALUES, F));
183: PetscCall(VecDot(F, W, &L));
184: PetscCall(ReferenceFaceIntegral(lower, upper, axis, faceUpper[v - 1] == PETSC_TRUE ? upper[axis] : lower[axis], &ref));
185: if (PetscAbsReal(PetscRealPart(L) - ref) < PETSC_SMALL * PetscAbsReal(ref)) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Face %" PetscInt_FMT " (%s): boundary integral is correct\n", v, faceName[v - 1]));
186: else
187: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Face %" PetscInt_FMT " (%s): boundary integral %g differs from exact %g, relative error %.2e\n", v, faceName[v - 1], (double)PetscRealPart(L), (double)ref, (double)(PetscAbsReal(PetscRealPart(L) - ref) / PetscAbsReal(ref))));
188: }
189: PetscCall(DMRestoreLocalVector(dm, &locX));
190: PetscCall(DMRestoreLocalVector(dm, &locF));
191: PetscCall(VecDestroy(&F));
192: PetscCall(PetscWeakFormDestroy(&wf));
193: PetscFunctionReturn(PETSC_SUCCESS);
194: }
196: // Compare the boundary residual of int_face grad u . grad v dS at u = U with its Jacobian times U
197: static PetscErrorCode CheckBdJacobian(DM dm, DMLabel label, Vec U)
198: {
199: const PetscInt values[6] = {1, 2, 3, 4, 5, 6};
200: PetscWeakForm wf;
201: Mat J;
202: Vec locU, locF, F, JU;
203: PetscReal norm, err;
205: PetscFunctionBeginUser;
206: PetscCall(PetscWeakFormCreate(PETSC_COMM_SELF, &wf));
207: for (PetscInt v = 1; v <= 6; ++v) {
208: PetscCall(PetscWeakFormSetIndexBdResidual(wf, label, v, 0, 0, 0, NULL, 0, f1_bd_grad));
209: PetscCall(PetscWeakFormSetIndexBdJacobian(wf, label, v, 0, 0, 0, 0, NULL, 0, NULL, 0, NULL, 0, g3_bd_identity));
210: }
211: PetscCall(DMGetLocalVector(dm, &locU));
212: PetscCall(DMGetLocalVector(dm, &locF));
213: PetscCall(DMGlobalToLocal(dm, U, INSERT_VALUES, locU));
214: PetscCall(VecZeroEntries(locF));
215: for (PetscInt v = 1; v <= 6; ++v) {
216: PetscFormKey key;
218: key.label = label;
219: key.value = v;
220: key.field = 0;
221: key.part = 0;
222: PetscCall(DMPlexComputeBdResidualSingle(dm, wf, key, locU, NULL, 0.0, locF));
223: }
224: PetscCall(VecDuplicate(U, &F));
225: PetscCall(VecDuplicate(U, &JU));
226: PetscCall(VecZeroEntries(F));
227: PetscCall(DMLocalToGlobal(dm, locF, ADD_VALUES, F));
228: PetscCall(DMCreateMatrix(dm, &J));
229: PetscCall(DMPlexComputeBdJacobianSingle(dm, wf, label, 6, values, 0, locU, NULL, 0.0, 0.0, J, J));
230: PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
231: PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
232: PetscCall(MatMult(J, U, JU));
233: PetscCall(VecNorm(F, NORM_2, &norm));
234: PetscCall(VecAXPY(JU, -1.0, F));
235: PetscCall(VecNorm(JU, NORM_2, &err));
236: if (err < PETSC_SMALL * norm) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Boundary residual matches its Jacobian\n"));
237: else PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Boundary residual differs from its Jacobian, relative error %.2e\n", (double)(err / norm)));
238: PetscCall(MatDestroy(&J));
239: PetscCall(VecDestroy(&JU));
240: PetscCall(VecDestroy(&F));
241: PetscCall(DMRestoreLocalVector(dm, &locU));
242: PetscCall(DMRestoreLocalVector(dm, &locF));
243: PetscCall(PetscWeakFormDestroy(&wf));
244: PetscFunctionReturn(PETSC_SUCCESS);
245: }
247: int main(int argc, char **argv)
248: {
249: DM dm;
250: DMLabel label;
251: Vec W;
252: AppCtx ctx;
253: PetscErrorCode (*funcs[1])(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *) = {w_func};
255: PetscFunctionBeginUser;
256: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
257: PetscCall(ProcessOptions(PETSC_COMM_WORLD, &ctx));
258: PetscCall(CreateMesh(PETSC_COMM_WORLD, &dm));
259: PetscCall(SetupDiscretization(dm, &ctx));
260: PetscCall(DMGetLabel(dm, "Face Sets", &label));
261: PetscCall(OrientFaces(dm, label, ctx.o));
262: PetscCall(DMCreateGlobalVector(dm, &W));
263: // First project w(x) into the finite element space as W. This works because it is in Q1
264: PetscCall(DMProjectFunction(dm, 0.0, funcs, NULL, INSERT_ALL_VALUES, W));
265: if (ctx.jac == PETSC_TRUE) PetscCall(CheckBdJacobian(dm, label, W));
266: else PetscCall(CheckFaceIntegrals(dm, label, W));
267: PetscCall(VecDestroy(&W));
268: PetscCall(DMDestroy(&dm));
269: PetscCall(PetscFinalize());
270: return 0;
271: }
273: /*TEST
275: testset:
276: args: -dm_plex_dim 3 -dm_plex_simplex 0 -dm_plex_box_upper 1,2,1
278: test:
279: suffix: q1
280: args: -dm_plex_box_faces 2,2,2 -k 1
282: test:
283: suffix: q2
284: args: -dm_plex_box_faces 2,2,2 -k 2
286: test:
287: suffix: q2_orient
288: args: -dm_plex_box_faces 2,2,2 -k 2 -face_orientation {{-4 -3 -2 -1 1 2 3}separate output}
289: output_file: output/ex109_q2.out
291: test:
292: suffix: q2_flare_jacobian
293: args: -dm_plex_dim 3 -dm_plex_simplex 0 -dm_plex_box_lower 1,0,0 -dm_plex_box_upper 2,2,1 -dm_plex_box_faces 1,1,1 -dm_coord_remap -dm_coord_map flare -k 2 -check_jacobian -face_orientation {{-4 -3 -2 -1 0 1 2 3}separate output}
294: output_file: output/ex109_q2_flare_jacobian.out
296: TEST*/