Actual source code: ex4.c
1: static char help[] = "Solves the incompressible, variable-viscosity Stokes equation in 2D or 3D, driven by buoyancy variations.\n"
2: "-dim: set dimension (2 or 3)\n"
3: "-nondimensional: replace dimensional domain and coefficients with nondimensional ones\n"
4: "-isoviscous: use constant viscosity\n"
5: "-levels: number of grids to create, by coarsening\n"
6: "-rediscretize: create operators for all grids and set up a FieldSplit/MG solver\n"
7: "-dump_solution: dump VTK files\n\n";
9: #include <petscdm.h>
10: #include <petscksp.h>
11: #include <petscdmstag.h>
12: #include <petscdmda.h>
14: /* Application context - grid-based data*/
15: typedef struct LevelCtxData_ {
16: DM dm_stokes, dm_coefficients, dm_faces;
17: Vec coeff;
18: PetscInt cells_x, cells_y, cells_z; /* redundant with DMs */
19: PetscReal hx_characteristic, hy_characteristic, hz_characteristic;
20: PetscScalar K_bound, K_cont;
21: } LevelCtxData;
22: typedef LevelCtxData *LevelCtx;
24: /* Application context - problem and grid(s) (but not solver-specific data) */
25: typedef struct CtxData_ {
26: MPI_Comm comm;
27: PetscInt dim; /* redundant with DMs */
28: PetscInt cells_x, cells_y, cells_z; /* Redundant with finest DMs */
29: PetscReal xmax, ymax, xmin, ymin, zmin, zmax;
30: PetscScalar eta1, eta2, rho1, rho2, gy, eta_characteristic;
31: PetscBool pin_pressure;
32: PetscScalar (*GetEta)(struct CtxData_ *, PetscScalar, PetscScalar, PetscScalar);
33: PetscScalar (*GetRho)(struct CtxData_ *, PetscScalar, PetscScalar, PetscScalar);
34: PetscInt n_levels;
35: LevelCtx *levels;
36: } CtxData;
37: typedef CtxData *Ctx;
39: /* Helper to pass system-creation parameters */
40: typedef struct SystemParameters_ {
41: Ctx ctx;
42: PetscInt level;
43: PetscBool include_inverse_visc, faces_only;
44: } SystemParametersData;
45: typedef SystemParametersData *SystemParameters;
47: /* Main logic */
48: static PetscErrorCode AttachNullspace(DM, Mat);
49: static PetscErrorCode CreateAuxiliaryOperator(Ctx, PetscInt, Mat *);
50: static PetscErrorCode CreateSystem(SystemParameters, Mat *, Vec *);
51: static PetscErrorCode CtxCreateAndSetFromOptions(Ctx *);
52: static PetscErrorCode CtxDestroy(Ctx *);
53: static PetscErrorCode DumpSolution(Ctx, PetscInt, Vec);
54: static PetscErrorCode OperatorInsertInverseViscosityPressureTerms(DM, DM, Vec, PetscScalar, Mat);
55: static PetscErrorCode PopulateCoefficientData(Ctx, PetscInt);
56: static PetscErrorCode SystemParametersCreate(SystemParameters *, Ctx);
57: static PetscErrorCode SystemParametersDestroy(SystemParameters *);
59: int main(int argc, char **argv)
60: {
61: Ctx ctx;
62: Mat A, *A_faces = NULL, S_hat = NULL, P = NULL;
63: Vec x, b;
64: KSP ksp;
65: DM dm_stokes, dm_coefficients;
66: PetscBool dump_solution, build_auxiliary_operator, rediscretize, custom_pc_mat;
68: PetscFunctionBeginUser;
69: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
71: /* Accept options for program behavior */
72: dump_solution = PETSC_FALSE;
73: PetscCall(PetscOptionsGetBool(NULL, NULL, "-dump_solution", &dump_solution, NULL));
74: rediscretize = PETSC_FALSE;
75: PetscCall(PetscOptionsGetBool(NULL, NULL, "-rediscretize", &rediscretize, NULL));
76: build_auxiliary_operator = rediscretize;
77: PetscCall(PetscOptionsGetBool(NULL, NULL, "-build_auxiliary_operator", &build_auxiliary_operator, NULL));
78: custom_pc_mat = PETSC_FALSE;
79: PetscCall(PetscOptionsGetBool(NULL, NULL, "-custom_pc_mat", &custom_pc_mat, NULL));
81: /* Populate application context */
82: PetscCall(CtxCreateAndSetFromOptions(&ctx));
84: /* Create two DMStag objects, corresponding to the same domain and parallel
85: decomposition ("topology"). Each defines a different set of fields on
86: the domain ("section"); the first the solution to the Stokes equations
87: (x- and y-velocities and scalar pressure), and the second holds coefficients
88: (viscosities on elements and viscosities+densities on corners/edges in 2d/3d) */
89: if (ctx->dim == 2) {
90: PetscCall(DMStagCreate2d(ctx->comm, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, ctx->cells_x, ctx->cells_y, /* Global element counts */
91: PETSC_DECIDE, PETSC_DECIDE, /* Determine parallel decomposition automatically */
92: 0, 1, 1, /* dof: 0 per vertex, 1 per edge, 1 per face/element */
93: DMSTAG_STENCIL_BOX, 1, /* elementwise stencil width */
94: NULL, NULL, &ctx->levels[ctx->n_levels - 1]->dm_stokes));
95: } else if (ctx->dim == 3) {
96: PetscCall(DMStagCreate3d(ctx->comm, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, ctx->cells_x, ctx->cells_y, ctx->cells_z, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE, 0, 0, 1, 1, /* dof: 1 per face, 1 per element */
97: DMSTAG_STENCIL_BOX, 1, NULL, NULL, NULL, &ctx->levels[ctx->n_levels - 1]->dm_stokes));
98: } else SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "Unsupported dimension: %" PetscInt_FMT, ctx->dim);
99: dm_stokes = ctx->levels[ctx->n_levels - 1]->dm_stokes;
100: PetscCall(DMSetFromOptions(dm_stokes));
101: PetscCall(DMStagGetGlobalSizes(dm_stokes, &ctx->cells_x, &ctx->cells_y, &ctx->cells_z));
102: PetscCall(DMSetUp(dm_stokes));
103: PetscCall(DMStagSetUniformCoordinatesProduct(dm_stokes, ctx->xmin, ctx->xmax, ctx->ymin, ctx->ymax, ctx->zmin, ctx->zmax));
105: if (ctx->dim == 2) PetscCall(DMStagCreateCompatibleDMStag(dm_stokes, 2, 0, 1, 0, &ctx->levels[ctx->n_levels - 1]->dm_coefficients));
106: else if (ctx->dim == 3) PetscCall(DMStagCreateCompatibleDMStag(dm_stokes, 0, 2, 0, 1, &ctx->levels[ctx->n_levels - 1]->dm_coefficients));
107: else SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "Unsupported dimension: %" PetscInt_FMT, ctx->dim);
108: dm_coefficients = ctx->levels[ctx->n_levels - 1]->dm_coefficients;
109: PetscCall(DMSetUp(dm_coefficients));
110: PetscCall(DMStagSetUniformCoordinatesProduct(dm_coefficients, ctx->xmin, ctx->xmax, ctx->ymin, ctx->ymax, ctx->zmin, ctx->zmax));
112: /* Create additional DMs by coarsening. 0 is the coarsest level */
113: for (PetscInt level = ctx->n_levels - 1; level > 0; --level) {
114: PetscCall(DMCoarsen(ctx->levels[level]->dm_stokes, MPI_COMM_NULL, &ctx->levels[level - 1]->dm_stokes));
115: PetscCall(DMCoarsen(ctx->levels[level]->dm_coefficients, MPI_COMM_NULL, &ctx->levels[level - 1]->dm_coefficients));
116: }
118: /* Compute scaling constants, knowing grid spacing */
119: ctx->eta_characteristic = PetscMin(PetscRealPart(ctx->eta1), PetscRealPart(ctx->eta2));
120: for (PetscInt level = 0; level < ctx->n_levels; ++level) {
121: PetscInt N[3];
122: PetscReal hx_avg_inv;
124: PetscCall(DMStagGetGlobalSizes(ctx->levels[level]->dm_stokes, &N[0], &N[1], &N[2]));
125: ctx->levels[level]->hx_characteristic = (ctx->xmax - ctx->xmin) / N[0];
126: ctx->levels[level]->hy_characteristic = (ctx->ymax - ctx->ymin) / N[1];
127: if (N[2]) ctx->levels[level]->hz_characteristic = (ctx->zmax - ctx->zmin) / N[2];
128: if (ctx->dim == 2) {
129: hx_avg_inv = 2.0 / (ctx->levels[level]->hx_characteristic + ctx->levels[level]->hy_characteristic);
130: } else if (ctx->dim == 3) {
131: hx_avg_inv = 3.0 / (ctx->levels[level]->hx_characteristic + ctx->levels[level]->hy_characteristic + ctx->levels[level]->hz_characteristic);
132: } else SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "Not Implemented for dimension %" PetscInt_FMT, ctx->dim);
133: ctx->levels[level]->K_cont = ctx->eta_characteristic * hx_avg_inv;
134: ctx->levels[level]->K_bound = ctx->eta_characteristic * hx_avg_inv * hx_avg_inv;
135: }
137: /* Populate coefficient data */
138: for (PetscInt level = 0; level < ctx->n_levels; ++level) PetscCall(PopulateCoefficientData(ctx, level));
140: /* Construct main system */
141: {
142: SystemParameters system_parameters;
144: PetscCall(SystemParametersCreate(&system_parameters, ctx));
145: PetscCall(CreateSystem(system_parameters, &A, &b));
146: PetscCall(SystemParametersDestroy(&system_parameters));
147: }
149: /* Attach a constant-pressure nullspace to the fine-level operator */
150: if (!ctx->pin_pressure) PetscCall(AttachNullspace(dm_stokes, A));
152: /* Set up solver */
153: PetscCall(KSPCreate(ctx->comm, &ksp));
154: PetscCall(KSPSetType(ksp, KSPFGMRES));
155: {
156: /* Default to a direct solver, if a package is available */
157: PetscMPIInt size;
159: PetscCallMPI(MPI_Comm_size(ctx->comm, &size));
160: if (PetscDefined(HAVE_SUITESPARSE) && size == 1) {
161: PC pc;
163: PetscCall(KSPGetPC(ksp, &pc));
164: PetscCall(PCSetType(pc, PCLU));
165: PetscCall(PCFactorSetMatSolverType(pc, MATSOLVERUMFPACK)); /* A default, requires SuiteSparse */
166: }
167: if (PetscDefined(HAVE_MUMPS) && size > 1) {
168: PC pc;
170: PetscCall(KSPGetPC(ksp, &pc));
171: PetscCall(PCSetType(pc, PCLU));
172: PetscCall(PCFactorSetMatSolverType(pc, MATSOLVERMUMPS)); /* A default, requires MUMPS */
173: }
174: }
176: /* Create and set a custom matrix from which the preconditioner is constructed */
177: if (custom_pc_mat) {
178: SystemParameters system_parameters;
180: PetscCall(SystemParametersCreate(&system_parameters, ctx));
181: system_parameters->include_inverse_visc = PETSC_TRUE;
182: PetscCall(CreateSystem(system_parameters, &P, NULL));
183: PetscCall(SystemParametersDestroy(&system_parameters));
184: PetscCall(KSPSetOperators(ksp, A, P));
185: } else {
186: PetscCall(KSPSetOperators(ksp, A, A));
187: }
189: PetscCall(KSPSetDM(ksp, dm_stokes));
190: PetscCall(KSPSetDMActive(ksp, KSP_DMACTIVE_ALL, PETSC_FALSE));
192: /* Finish setting up solver (can override options set above) */
193: PetscCall(KSPSetFromOptions(ksp));
195: /* Additional solver configuration that can involve setting up and CANNOT be
196: overridden from the command line */
198: /* Construct an auxiliary operator for use a Schur complement preconditioner,
199: and tell PCFieldSplit to use it (which has no effect if not using that PC) */
200: if (build_auxiliary_operator && !rediscretize) {
201: PC pc;
203: PetscCall(KSPGetPC(ksp, &pc));
204: PetscCall(CreateAuxiliaryOperator(ctx, ctx->n_levels - 1, &S_hat));
205: PetscCall(PCFieldSplitSetSchurPre(pc, PC_FIELDSPLIT_SCHUR_PRE_USER, S_hat));
206: PetscCall(KSPSetFromOptions(ksp));
207: }
209: if (rediscretize) {
210: /* Set up an ABF solver with rediscretized geometric multigrid on the velocity-velocity block */
211: PC pc, pc_faces;
212: KSP ksp_faces;
214: if (ctx->n_levels < 2) PetscCall(PetscPrintf(ctx->comm, "Warning: not using multiple levels!\n"));
216: PetscCall(KSPGetPC(ksp, &pc));
217: PetscCall(PCSetType(pc, PCFIELDSPLIT));
218: PetscCall(PCFieldSplitSetType(pc, PC_COMPOSITE_SCHUR));
219: PetscCall(PCFieldSplitSetSchurFactType(pc, PC_FIELDSPLIT_SCHUR_FACT_UPPER));
220: if (build_auxiliary_operator) {
221: PetscCall(CreateAuxiliaryOperator(ctx, ctx->n_levels - 1, &S_hat));
222: PetscCall(PCFieldSplitSetSchurPre(pc, PC_FIELDSPLIT_SCHUR_PRE_USER, S_hat));
223: }
225: /* Create rediscretized velocity-only DMs and operators */
226: PetscCall(PetscMalloc1(ctx->n_levels, &A_faces));
227: for (PetscInt level = 0; level < ctx->n_levels; ++level) {
228: if (ctx->dim == 2) {
229: PetscCall(DMStagCreateCompatibleDMStag(ctx->levels[level]->dm_stokes, 0, 1, 0, 0, &ctx->levels[level]->dm_faces));
230: } else if (ctx->dim == 3) {
231: PetscCall(DMStagCreateCompatibleDMStag(ctx->levels[level]->dm_stokes, 0, 0, 1, 0, &ctx->levels[level]->dm_faces));
232: } else SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "Not Implemented for dimension %" PetscInt_FMT, ctx->dim);
233: {
234: SystemParameters system_parameters;
236: PetscCall(SystemParametersCreate(&system_parameters, ctx));
237: system_parameters->faces_only = PETSC_TRUE;
238: system_parameters->level = level;
239: PetscCall(CreateSystem(system_parameters, &A_faces[level], NULL));
240: PetscCall(SystemParametersDestroy(&system_parameters));
241: }
242: }
244: /* Set up to populate enough to define the sub-solver */
245: PetscCall(KSPSetUp(ksp));
247: /* Set multigrid components and settings */
248: {
249: KSP *sub_ksp;
251: PetscCall(PCFieldSplitSchurGetSubKSP(pc, NULL, &sub_ksp));
252: ksp_faces = sub_ksp[0];
253: PetscCall(PetscFree(sub_ksp));
254: }
255: PetscCall(KSPSetType(ksp_faces, KSPGCR));
256: PetscCall(KSPGetPC(ksp_faces, &pc_faces));
257: PetscCall(PCSetType(pc_faces, PCMG));
258: PetscCall(PCMGSetLevels(pc_faces, ctx->n_levels, NULL));
259: for (PetscInt level = 0; level < ctx->n_levels; ++level) {
260: KSP ksp_level;
261: PC pc_level;
263: /* Smoothers */
264: PetscCall(PCMGGetSmoother(pc_faces, level, &ksp_level));
265: PetscCall(KSPGetPC(ksp_level, &pc_level));
266: PetscCall(KSPSetOperators(ksp_level, A_faces[level], A_faces[level]));
267: if (level > 0) PetscCall(PCSetType(pc_level, PCJACOBI));
269: /* Transfer Operators */
270: if (level > 0) {
271: Mat restriction, interpolation;
272: DM dm_level = ctx->levels[level]->dm_faces;
273: DM dm_coarser = ctx->levels[level - 1]->dm_faces;
275: PetscCall(DMCreateInterpolation(dm_coarser, dm_level, &interpolation, NULL));
276: PetscCall(PCMGSetInterpolation(pc_faces, level, interpolation));
277: PetscCall(MatDestroy(&interpolation));
278: PetscCall(DMCreateRestriction(dm_coarser, dm_level, &restriction));
279: PetscCall(PCMGSetRestriction(pc_faces, level, restriction));
280: PetscCall(MatDestroy(&restriction));
281: }
282: }
283: }
285: /* Solve */
286: PetscCall(VecDuplicate(b, &x));
287: PetscCall(KSPSolve(ksp, b, x));
288: {
289: KSPConvergedReason reason;
290: PetscCall(KSPGetConvergedReason(ksp, &reason));
291: PetscCheck(reason >= 0, PETSC_COMM_WORLD, PETSC_ERR_CONV_FAILED, "Linear solve failed");
292: }
294: /* Dump solution by converting to DMDAs and dumping */
295: if (dump_solution) PetscCall(DumpSolution(ctx, ctx->n_levels - 1, x));
297: /* Destroy PETSc objects and finalize */
298: PetscCall(MatDestroy(&A));
299: PetscCall(PetscFree(A));
300: PetscCall(MatDestroyMatrices(ctx->n_levels, &A_faces));
301: PetscCall(MatDestroy(&P));
302: PetscCall(VecDestroy(&x));
303: PetscCall(VecDestroy(&b));
304: PetscCall(MatDestroy(&S_hat));
305: PetscCall(KSPDestroy(&ksp));
306: PetscCall(CtxDestroy(&ctx));
307: PetscCall(PetscFinalize());
308: return 0;
309: }
311: static PetscScalar GetEta_constant(Ctx ctx, PetscScalar x, PetscScalar y, PetscScalar z)
312: {
313: (void)ctx;
314: (void)x;
315: (void)y;
316: (void)z;
317: return 1.0;
318: }
320: static PetscScalar GetRho_layers(Ctx ctx, PetscScalar x, PetscScalar y, PetscScalar z)
321: {
322: (void)y;
323: (void)z;
324: return PetscRealPart(x) < (ctx->xmax - ctx->xmin) / 2.0 ? ctx->rho1 : ctx->rho2;
325: }
327: static PetscScalar GetEta_layers(Ctx ctx, PetscScalar x, PetscScalar y, PetscScalar z)
328: {
329: (void)y;
330: (void)z;
331: return PetscRealPart(x) < (ctx->xmax - ctx->xmin) / 2.0 ? ctx->eta1 : ctx->eta2;
332: }
334: static PetscScalar GetRho_sinker_box2(Ctx ctx, PetscScalar x, PetscScalar y, PetscScalar z)
335: {
336: (void)z;
337: const PetscReal d = ctx->xmax - ctx->xmin;
338: const PetscReal xx = PetscRealPart(x) / d - 0.5;
339: const PetscReal yy = PetscRealPart(y) / d - 0.5;
340: return (xx * xx > 0.15 * 0.15 || yy * yy > 0.15 * 0.15) ? ctx->rho1 : ctx->rho2;
341: }
343: static PetscScalar GetEta_sinker_box2(Ctx ctx, PetscScalar x, PetscScalar y, PetscScalar z)
344: {
345: (void)z;
346: const PetscReal d = ctx->xmax - ctx->xmin;
347: const PetscReal xx = PetscRealPart(x) / d - 0.5;
348: const PetscReal yy = PetscRealPart(y) / d - 0.5;
349: return (xx * xx > 0.15 * 0.15 || yy * yy > 0.15 * 0.15) ? ctx->eta1 : ctx->eta2;
350: }
352: static PetscScalar GetRho_sinker_box3(Ctx ctx, PetscScalar x, PetscScalar y, PetscScalar z)
353: {
354: const PetscReal d = ctx->xmax - ctx->xmin;
355: const PetscReal xx = PetscRealPart(x) / d - 0.5;
356: const PetscReal yy = PetscRealPart(y) / d - 0.5;
357: const PetscReal zz = PetscRealPart(z) / d - 0.5;
358: const PetscReal half_width = 0.15;
359: return (PetscAbsReal(xx) > half_width || PetscAbsReal(yy) > half_width || PetscAbsReal(zz) > half_width) ? ctx->rho1 : ctx->rho2;
360: }
362: static PetscScalar GetEta_sinker_box3(Ctx ctx, PetscScalar x, PetscScalar y, PetscScalar z)
363: {
364: const PetscReal d = ctx->xmax - ctx->xmin;
365: const PetscReal xx = PetscRealPart(x) / d - 0.5;
366: const PetscReal yy = PetscRealPart(y) / d - 0.5;
367: const PetscReal zz = PetscRealPart(z) / d - 0.5;
368: const PetscReal half_width = 0.15;
369: return (PetscAbsReal(xx) > half_width || PetscAbsReal(yy) > half_width || PetscAbsReal(zz) > half_width) ? ctx->eta1 : ctx->eta2;
370: }
372: static PetscScalar GetRho_sinker_sphere3(Ctx ctx, PetscScalar x, PetscScalar y, PetscScalar z)
373: {
374: const PetscReal d = ctx->xmax - ctx->xmin;
375: const PetscReal xx = PetscRealPart(x) / d - 0.5;
376: const PetscReal yy = PetscRealPart(y) / d - 0.5;
377: const PetscReal zz = PetscRealPart(z) / d - 0.5;
378: const PetscReal half_width = 0.3;
379: return (xx * xx + yy * yy + zz * zz > half_width * half_width) ? ctx->rho1 : ctx->rho2;
380: }
382: static PetscScalar GetEta_sinker_sphere3(Ctx ctx, PetscScalar x, PetscScalar y, PetscScalar z)
383: {
384: const PetscReal d = ctx->xmax - ctx->xmin;
385: const PetscReal xx = PetscRealPart(x) / d - 0.5;
386: const PetscReal yy = PetscRealPart(y) / d - 0.5;
387: const PetscReal zz = PetscRealPart(z) / d - 0.5;
388: const PetscReal half_width = 0.3;
389: return (xx * xx + yy * yy + zz * zz > half_width * half_width) ? ctx->eta1 : ctx->eta2;
390: }
392: static PetscScalar GetEta_blob3(Ctx ctx, PetscScalar x, PetscScalar y, PetscScalar z)
393: {
394: const PetscReal d = ctx->xmax - ctx->xmin;
395: const PetscReal xx = PetscRealPart(x) / d - 0.5;
396: const PetscReal yy = PetscRealPart(y) / d - 0.5;
397: const PetscReal zz = PetscRealPart(z) / d - 0.5;
398: return ctx->eta1 + ctx->eta2 * PetscExpScalar(-20.0 * (xx * xx + yy * yy + zz * zz));
399: }
401: static PetscScalar GetRho_blob3(Ctx ctx, PetscScalar x, PetscScalar y, PetscScalar z)
402: {
403: const PetscReal d = ctx->xmax - ctx->xmin;
404: const PetscReal xx = PetscRealPart(x) / d - 0.5;
405: const PetscReal yy = PetscRealPart(y) / d - 0.5;
406: const PetscReal zz = PetscRealPart(z) / d - 0.5;
407: return ctx->rho1 + ctx->rho2 * PetscExpScalar(-20.0 * (xx * xx + yy * yy + zz * zz));
408: }
410: static PetscErrorCode LevelCtxCreate(LevelCtx *p_level_ctx)
411: {
412: LevelCtx level_ctx;
414: PetscFunctionBeginUser;
415: PetscCall(PetscMalloc1(1, p_level_ctx));
416: level_ctx = *p_level_ctx;
417: level_ctx->dm_stokes = NULL;
418: level_ctx->dm_coefficients = NULL;
419: level_ctx->dm_faces = NULL;
420: PetscFunctionReturn(PETSC_SUCCESS);
421: }
423: static PetscErrorCode LevelCtxDestroy(LevelCtx *p_level_ctx)
424: {
425: LevelCtx level_ctx;
427: PetscFunctionBeginUser;
428: level_ctx = *p_level_ctx;
429: PetscCall(DMDestroy(&level_ctx->dm_stokes));
430: PetscCall(DMDestroy(&level_ctx->dm_coefficients));
431: PetscCall(DMDestroy(&level_ctx->dm_faces));
432: PetscCall(VecDestroy(&level_ctx->coeff));
433: PetscCall(PetscFree(*p_level_ctx));
434: PetscFunctionReturn(PETSC_SUCCESS);
435: }
437: static PetscErrorCode CtxCreateAndSetFromOptions(Ctx *p_ctx)
438: {
439: Ctx ctx;
441: PetscFunctionBeginUser;
442: PetscCall(PetscMalloc1(1, p_ctx));
443: ctx = *p_ctx;
445: ctx->comm = PETSC_COMM_WORLD;
446: ctx->pin_pressure = PETSC_FALSE;
447: PetscCall(PetscOptionsGetBool(NULL, NULL, "-pin_pressure", &ctx->pin_pressure, NULL));
448: ctx->dim = 3;
449: PetscCall(PetscOptionsGetInt(NULL, NULL, "-dim", &ctx->dim, NULL));
450: if (ctx->dim <= 2) {
451: ctx->cells_x = 32;
452: } else {
453: ctx->cells_x = 16;
454: }
455: PetscCall(PetscOptionsGetInt(NULL, NULL, "-s", &ctx->cells_x, NULL)); /* shortcut. Usually, use -stag_grid_x etc. */
456: ctx->cells_z = ctx->cells_y = ctx->cells_x;
457: ctx->xmin = ctx->ymin = ctx->zmin = 0.0;
458: {
459: PetscBool nondimensional = PETSC_TRUE;
461: PetscCall(PetscOptionsGetBool(NULL, NULL, "-nondimensional", &nondimensional, NULL));
462: if (nondimensional) {
463: ctx->xmax = ctx->ymax = ctx->zmax = 1.0;
464: ctx->rho1 = 0.0;
465: ctx->rho2 = 1.0;
466: ctx->eta1 = 1.0;
467: ctx->eta2 = 1e2;
468: ctx->gy = -1.0; /* downwards */
469: } else {
470: ctx->xmax = 1e6;
471: ctx->ymax = 1.5e6;
472: ctx->zmax = 1e6;
473: ctx->rho1 = 3200;
474: ctx->rho2 = 3300;
475: ctx->eta1 = 1e20;
476: ctx->eta2 = 1e22;
477: ctx->gy = -10.0; /* downwards */
478: }
479: }
480: {
481: PetscBool isoviscous;
483: isoviscous = PETSC_FALSE;
484: PetscCall(PetscOptionsGetScalar(NULL, NULL, "-eta1", &ctx->eta1, NULL));
485: PetscCall(PetscOptionsGetBool(NULL, NULL, "-isoviscous", &isoviscous, NULL));
486: if (isoviscous) {
487: ctx->eta2 = ctx->eta1;
488: ctx->GetEta = GetEta_constant; /* override */
489: } else {
490: PetscCall(PetscOptionsGetScalar(NULL, NULL, "-eta2", &ctx->eta2, NULL));
491: }
492: }
493: {
494: char mode[1024] = "sinker";
495: PetscBool is_layers, is_blob, is_sinker_box, is_sinker_sphere;
497: PetscCall(PetscOptionsGetString(NULL, NULL, "-coefficients", mode, sizeof(mode), NULL));
498: PetscCall(PetscStrncmp(mode, "layers", sizeof(mode), &is_layers));
499: PetscCall(PetscStrncmp(mode, "sinker", sizeof(mode), &is_sinker_box));
500: if (!is_sinker_box) PetscCall(PetscStrncmp(mode, "sinker_box", sizeof(mode), &is_sinker_box));
501: PetscCall(PetscStrncmp(mode, "sinker_sphere", sizeof(mode), &is_sinker_sphere));
502: PetscCall(PetscStrncmp(mode, "blob", sizeof(mode), &is_blob));
504: if (is_layers) {
505: ctx->GetRho = GetRho_layers;
506: ctx->GetEta = GetEta_layers;
507: }
508: if (is_blob) {
509: PetscCheck(ctx->dim == 3, PETSC_COMM_WORLD, PETSC_ERR_SUP, "Not implemented for dimension %" PetscInt_FMT, ctx->dim);
510: ctx->GetRho = GetRho_blob3;
511: ctx->GetEta = GetEta_blob3;
512: }
513: if (is_sinker_box) {
514: if (ctx->dim == 2) {
515: ctx->GetRho = GetRho_sinker_box2;
516: ctx->GetEta = GetEta_sinker_box2;
517: } else if (ctx->dim == 3) {
518: ctx->GetRho = GetRho_sinker_box3;
519: ctx->GetEta = GetEta_sinker_box3;
520: } else SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "Not implemented for dimension %" PetscInt_FMT, ctx->dim);
521: }
522: if (is_sinker_sphere) {
523: PetscCheck(ctx->dim == 3, PETSC_COMM_WORLD, PETSC_ERR_SUP, "Not implemented for dimension %" PetscInt_FMT, ctx->dim);
524: ctx->GetRho = GetRho_sinker_sphere3;
525: ctx->GetEta = GetEta_sinker_sphere3;
526: }
527: }
529: /* Per-level data */
530: ctx->n_levels = 1;
531: PetscCall(PetscOptionsGetInt(NULL, NULL, "-levels", &ctx->n_levels, NULL));
532: PetscCall(PetscMalloc1(ctx->n_levels, &ctx->levels));
533: for (PetscInt i = 0; i < ctx->n_levels; ++i) PetscCall(LevelCtxCreate(&ctx->levels[i]));
534: PetscFunctionReturn(PETSC_SUCCESS);
535: }
537: static PetscErrorCode CtxDestroy(Ctx *p_ctx)
538: {
539: Ctx ctx;
541: PetscFunctionBeginUser;
542: ctx = *p_ctx;
543: for (PetscInt i = 0; i < ctx->n_levels; ++i) PetscCall(LevelCtxDestroy(&ctx->levels[i]));
544: PetscCall(PetscFree(ctx->levels));
545: PetscCall(PetscFree(*p_ctx));
546: PetscFunctionReturn(PETSC_SUCCESS);
547: }
549: static PetscErrorCode SystemParametersCreate(SystemParameters *parameters, Ctx ctx)
550: {
551: PetscFunctionBeginUser;
552: PetscCall(PetscMalloc1(1, parameters));
553: (*parameters)->ctx = ctx;
554: (*parameters)->level = ctx->n_levels - 1;
555: (*parameters)->include_inverse_visc = PETSC_FALSE;
556: (*parameters)->faces_only = PETSC_FALSE;
557: PetscFunctionReturn(PETSC_SUCCESS);
558: }
560: static PetscErrorCode SystemParametersDestroy(SystemParameters *parameters)
561: {
562: PetscFunctionBeginUser;
563: PetscCall(PetscFree(*parameters));
564: PetscFunctionReturn(PETSC_SUCCESS);
565: }
567: static PetscErrorCode CreateSystem2d(SystemParameters parameters, Mat *pA, Vec *pRhs)
568: {
569: PetscInt N[2];
570: PetscInt ex, ey, startx, starty, nx, ny;
571: Mat A;
572: Vec rhs;
573: PetscReal hx, hy, dv;
574: Vec coefficients_local;
575: PetscBool build_rhs;
576: DM dm_main, dm_coefficients;
577: PetscScalar K_cont, K_bound;
578: Ctx ctx = parameters->ctx;
579: PetscInt level = parameters->level;
581: PetscFunctionBeginUser;
582: if (parameters->faces_only) {
583: dm_main = ctx->levels[level]->dm_faces;
584: } else {
585: dm_main = ctx->levels[level]->dm_stokes;
586: }
587: dm_coefficients = ctx->levels[level]->dm_coefficients;
588: K_cont = ctx->levels[level]->K_cont;
589: K_bound = ctx->levels[level]->K_bound;
590: PetscCall(DMCreateMatrix(dm_main, pA));
591: A = *pA;
592: build_rhs = (PetscBool)(pRhs != NULL);
593: PetscCheck(!(parameters->faces_only && build_rhs), PetscObjectComm((PetscObject)dm_main), PETSC_ERR_SUP, "RHS for faces-only not supported");
594: if (build_rhs) {
595: PetscCall(DMCreateGlobalVector(dm_main, pRhs));
596: rhs = *pRhs;
597: } else {
598: rhs = NULL;
599: }
600: PetscCall(DMStagGetCorners(dm_main, &startx, &starty, NULL, &nx, &ny, NULL, NULL, NULL, NULL));
601: PetscCall(DMStagGetGlobalSizes(dm_main, &N[0], &N[1], NULL));
602: hx = ctx->levels[level]->hx_characteristic;
603: hy = ctx->levels[level]->hy_characteristic;
604: dv = hx * hy;
605: PetscCall(DMGetLocalVector(dm_coefficients, &coefficients_local));
606: PetscCall(DMGlobalToLocal(dm_coefficients, ctx->levels[level]->coeff, INSERT_VALUES, coefficients_local));
608: /* Loop over all local elements */
609: for (ey = starty; ey < starty + ny; ++ey) { /* With DMStag, always iterate x fastest, y second fastest, z slowest */
610: for (ex = startx; ex < startx + nx; ++ex) {
611: const PetscBool left_boundary = (PetscBool)(ex == 0);
612: const PetscBool right_boundary = (PetscBool)(ex == N[0] - 1);
613: const PetscBool bottom_boundary = (PetscBool)(ey == 0);
614: const PetscBool top_boundary = (PetscBool)(ey == N[1] - 1);
616: if (ey == N[1] - 1) {
617: /* Top boundary velocity Dirichlet */
618: DMStagStencil row;
619: const PetscScalar val_rhs = 0.0;
620: const PetscScalar val_A = K_bound;
622: row.i = ex;
623: row.j = ey;
624: row.loc = DMSTAG_UP;
625: row.c = 0;
626: PetscCall(DMStagMatSetValuesStencil(dm_main, A, 1, &row, 1, &row, &val_A, INSERT_VALUES));
627: if (build_rhs) PetscCall(DMStagVecSetValuesStencil(dm_main, rhs, 1, &row, &val_rhs, INSERT_VALUES));
628: }
630: if (ey == 0) {
631: /* Bottom boundary velocity Dirichlet */
632: DMStagStencil row;
633: const PetscScalar val_rhs = 0.0;
634: const PetscScalar val_A = K_bound;
636: row.i = ex;
637: row.j = ey;
638: row.loc = DMSTAG_DOWN;
639: row.c = 0;
640: PetscCall(DMStagMatSetValuesStencil(dm_main, A, 1, &row, 1, &row, &val_A, INSERT_VALUES));
641: if (build_rhs) PetscCall(DMStagVecSetValuesStencil(dm_main, rhs, 1, &row, &val_rhs, INSERT_VALUES));
642: } else {
643: /* Y-momentum equation : (u_xx + u_yy) - p_y = f^y
644: includes non-zero forcing and free-slip boundary conditions */
645: PetscInt count;
646: DMStagStencil row, col[11];
647: PetscScalar val_A[11];
648: DMStagStencil rhoPoint[2];
649: PetscScalar rho[2], val_rhs;
650: DMStagStencil etaPoint[4];
651: PetscScalar eta[4], eta_left, eta_right, eta_up, eta_down;
653: row.i = ex;
654: row.j = ey;
655: row.loc = DMSTAG_DOWN;
656: row.c = 0;
658: /* get rho values and compute rhs value*/
659: rhoPoint[0].i = ex;
660: rhoPoint[0].j = ey;
661: rhoPoint[0].loc = DMSTAG_DOWN_LEFT;
662: rhoPoint[0].c = 1;
663: rhoPoint[1].i = ex;
664: rhoPoint[1].j = ey;
665: rhoPoint[1].loc = DMSTAG_DOWN_RIGHT;
666: rhoPoint[1].c = 1;
667: PetscCall(DMStagVecGetValuesStencil(dm_coefficients, coefficients_local, 2, rhoPoint, rho));
668: val_rhs = -ctx->gy * dv * 0.5 * (rho[0] + rho[1]);
670: /* Get eta values */
671: etaPoint[0].i = ex;
672: etaPoint[0].j = ey;
673: etaPoint[0].loc = DMSTAG_DOWN_LEFT;
674: etaPoint[0].c = 0; /* Left */
675: etaPoint[1].i = ex;
676: etaPoint[1].j = ey;
677: etaPoint[1].loc = DMSTAG_DOWN_RIGHT;
678: etaPoint[1].c = 0; /* Right */
679: etaPoint[2].i = ex;
680: etaPoint[2].j = ey;
681: etaPoint[2].loc = DMSTAG_ELEMENT;
682: etaPoint[2].c = 0; /* Up */
683: etaPoint[3].i = ex;
684: etaPoint[3].j = ey - 1;
685: etaPoint[3].loc = DMSTAG_ELEMENT;
686: etaPoint[3].c = 0; /* Down */
687: PetscCall(DMStagVecGetValuesStencil(dm_coefficients, coefficients_local, 4, etaPoint, eta));
688: eta_left = eta[0];
689: eta_right = eta[1];
690: eta_up = eta[2];
691: eta_down = eta[3];
693: count = 0;
695: col[count] = row;
696: val_A[count] = -2.0 * dv * (eta_down + eta_up) / (hy * hy);
697: if (!left_boundary) val_A[count] += -1.0 * dv * eta_left / (hx * hx);
698: if (!right_boundary) val_A[count] += -1.0 * dv * eta_right / (hx * hx);
699: ++count;
701: col[count].i = ex;
702: col[count].j = ey - 1;
703: col[count].loc = DMSTAG_DOWN;
704: col[count].c = 0;
705: val_A[count] = 2.0 * dv * eta_down / (hy * hy);
706: ++count;
707: col[count].i = ex;
708: col[count].j = ey + 1;
709: col[count].loc = DMSTAG_DOWN;
710: col[count].c = 0;
711: val_A[count] = 2.0 * dv * eta_up / (hy * hy);
712: ++count;
713: if (!left_boundary) {
714: col[count].i = ex - 1;
715: col[count].j = ey;
716: col[count].loc = DMSTAG_DOWN;
717: col[count].c = 0;
718: val_A[count] = dv * eta_left / (hx * hx);
719: ++count;
720: }
721: if (!right_boundary) {
722: col[count].i = ex + 1;
723: col[count].j = ey;
724: col[count].loc = DMSTAG_DOWN;
725: col[count].c = 0;
726: val_A[count] = dv * eta_right / (hx * hx);
727: ++count;
728: }
729: col[count].i = ex;
730: col[count].j = ey - 1;
731: col[count].loc = DMSTAG_LEFT;
732: col[count].c = 0;
733: val_A[count] = dv * eta_left / (hx * hy);
734: ++count; /* down left x edge */
735: col[count].i = ex;
736: col[count].j = ey - 1;
737: col[count].loc = DMSTAG_RIGHT;
738: col[count].c = 0;
739: val_A[count] = -1.0 * dv * eta_right / (hx * hy);
740: ++count; /* down right x edge */
741: col[count].i = ex;
742: col[count].j = ey;
743: col[count].loc = DMSTAG_LEFT;
744: col[count].c = 0;
745: val_A[count] = -1.0 * dv * eta_left / (hx * hy);
746: ++count; /* up left x edge */
747: col[count].i = ex;
748: col[count].j = ey;
749: col[count].loc = DMSTAG_RIGHT;
750: col[count].c = 0;
751: val_A[count] = dv * eta_right / (hx * hy);
752: ++count; /* up right x edge */
753: if (!parameters->faces_only) {
754: col[count].i = ex;
755: col[count].j = ey - 1;
756: col[count].loc = DMSTAG_ELEMENT;
757: col[count].c = 0;
758: val_A[count] = K_cont * dv / hy;
759: ++count;
760: col[count].i = ex;
761: col[count].j = ey;
762: col[count].loc = DMSTAG_ELEMENT;
763: col[count].c = 0;
764: val_A[count] = -1.0 * K_cont * dv / hy;
765: ++count;
766: }
768: PetscCall(DMStagMatSetValuesStencil(dm_main, A, 1, &row, count, col, val_A, INSERT_VALUES));
769: if (build_rhs) PetscCall(DMStagVecSetValuesStencil(dm_main, rhs, 1, &row, &val_rhs, INSERT_VALUES));
770: }
772: if (ex == N[0] - 1) {
773: /* Right Boundary velocity Dirichlet */
774: /* Redundant in the corner */
775: DMStagStencil row;
776: const PetscScalar val_rhs = 0.0;
777: const PetscScalar val_A = K_bound;
779: row.i = ex;
780: row.j = ey;
781: row.loc = DMSTAG_RIGHT;
782: row.c = 0;
783: PetscCall(DMStagMatSetValuesStencil(dm_main, A, 1, &row, 1, &row, &val_A, INSERT_VALUES));
784: if (build_rhs) PetscCall(DMStagVecSetValuesStencil(dm_main, rhs, 1, &row, &val_rhs, INSERT_VALUES));
785: }
786: if (ex == 0) {
787: /* Left velocity Dirichlet */
788: DMStagStencil row;
789: const PetscScalar val_rhs = 0.0;
790: const PetscScalar val_A = K_bound;
792: row.i = ex;
793: row.j = ey;
794: row.loc = DMSTAG_LEFT;
795: row.c = 0;
796: PetscCall(DMStagMatSetValuesStencil(dm_main, A, 1, &row, 1, &row, &val_A, INSERT_VALUES));
797: if (build_rhs) PetscCall(DMStagVecSetValuesStencil(dm_main, rhs, 1, &row, &val_rhs, INSERT_VALUES));
798: } else {
799: /* X-momentum equation : (u_xx + u_yy) - p_x = f^x
800: zero RHS, including free-slip boundary conditions */
801: PetscInt count;
802: DMStagStencil row, col[11];
803: PetscScalar val_A[11];
804: DMStagStencil etaPoint[4];
805: PetscScalar eta[4], eta_left, eta_right, eta_up, eta_down;
806: const PetscScalar val_rhs = 0.0;
808: row.i = ex;
809: row.j = ey;
810: row.loc = DMSTAG_LEFT;
811: row.c = 0;
813: /* Get eta values */
814: etaPoint[0].i = ex - 1;
815: etaPoint[0].j = ey;
816: etaPoint[0].loc = DMSTAG_ELEMENT;
817: etaPoint[0].c = 0; /* Left */
818: etaPoint[1].i = ex;
819: etaPoint[1].j = ey;
820: etaPoint[1].loc = DMSTAG_ELEMENT;
821: etaPoint[1].c = 0; /* Right */
822: etaPoint[2].i = ex;
823: etaPoint[2].j = ey;
824: etaPoint[2].loc = DMSTAG_UP_LEFT;
825: etaPoint[2].c = 0; /* Up */
826: etaPoint[3].i = ex;
827: etaPoint[3].j = ey;
828: etaPoint[3].loc = DMSTAG_DOWN_LEFT;
829: etaPoint[3].c = 0; /* Down */
830: PetscCall(DMStagVecGetValuesStencil(dm_coefficients, coefficients_local, 4, etaPoint, eta));
831: eta_left = eta[0];
832: eta_right = eta[1];
833: eta_up = eta[2];
834: eta_down = eta[3];
836: count = 0;
837: col[count] = row;
838: val_A[count] = -2.0 * dv * (eta_left + eta_right) / (hx * hx);
839: if (!bottom_boundary) val_A[count] += -1.0 * dv * eta_down / (hy * hy);
840: if (!top_boundary) val_A[count] += -1.0 * dv * eta_up / (hy * hy);
841: ++count;
843: if (!bottom_boundary) {
844: col[count].i = ex;
845: col[count].j = ey - 1;
846: col[count].loc = DMSTAG_LEFT;
847: col[count].c = 0;
848: val_A[count] = dv * eta_down / (hy * hy);
849: ++count;
850: }
851: if (!top_boundary) {
852: col[count].i = ex;
853: col[count].j = ey + 1;
854: col[count].loc = DMSTAG_LEFT;
855: col[count].c = 0;
856: val_A[count] = dv * eta_up / (hy * hy);
857: ++count;
858: }
859: col[count].i = ex - 1;
860: col[count].j = ey;
861: col[count].loc = DMSTAG_LEFT;
862: col[count].c = 0;
863: val_A[count] = 2.0 * dv * eta_left / (hx * hx);
864: ++count;
865: col[count].i = ex + 1;
866: col[count].j = ey;
867: col[count].loc = DMSTAG_LEFT;
868: col[count].c = 0;
869: val_A[count] = 2.0 * dv * eta_right / (hx * hx);
870: ++count;
871: col[count].i = ex - 1;
872: col[count].j = ey;
873: col[count].loc = DMSTAG_DOWN;
874: col[count].c = 0;
875: val_A[count] = dv * eta_down / (hx * hy);
876: ++count; /* down left */
877: col[count].i = ex;
878: col[count].j = ey;
879: col[count].loc = DMSTAG_DOWN;
880: col[count].c = 0;
881: val_A[count] = -1.0 * dv * eta_down / (hx * hy);
882: ++count; /* down right */
883: col[count].i = ex - 1;
884: col[count].j = ey;
885: col[count].loc = DMSTAG_UP;
886: col[count].c = 0;
887: val_A[count] = -1.0 * dv * eta_up / (hx * hy);
888: ++count; /* up left */
889: col[count].i = ex;
890: col[count].j = ey;
891: col[count].loc = DMSTAG_UP;
892: col[count].c = 0;
893: val_A[count] = dv * eta_up / (hx * hy);
894: ++count; /* up right */
895: if (!parameters->faces_only) {
896: col[count].i = ex - 1;
897: col[count].j = ey;
898: col[count].loc = DMSTAG_ELEMENT;
899: col[count].c = 0;
900: val_A[count] = K_cont * dv / hx;
901: ++count;
902: col[count].i = ex;
903: col[count].j = ey;
904: col[count].loc = DMSTAG_ELEMENT;
905: col[count].c = 0;
906: val_A[count] = -1.0 * K_cont * dv / hx;
907: ++count;
908: }
910: PetscCall(DMStagMatSetValuesStencil(dm_main, A, 1, &row, count, col, val_A, INSERT_VALUES));
911: if (build_rhs) PetscCall(DMStagVecSetValuesStencil(dm_main, rhs, 1, &row, &val_rhs, INSERT_VALUES));
912: }
914: /* P equation : u_x + v_y = 0
916: Note that this includes an explicit zero on the diagonal. This is only needed for
917: direct solvers (not required if using an iterative solver and setting the constant-pressure nullspace)
919: Note: the scaling by dv is not chosen in a principled way and is likely sub-optimal
920: */
921: if (!parameters->faces_only) {
922: if (ctx->pin_pressure && ex == 0 && ey == 0) { /* Pin the first pressure node to zero, if requested */
923: DMStagStencil row;
924: const PetscScalar val_A = K_bound;
925: const PetscScalar val_rhs = 0.0;
927: row.i = ex;
928: row.j = ey;
929: row.loc = DMSTAG_ELEMENT;
930: row.c = 0;
931: PetscCall(DMStagMatSetValuesStencil(dm_main, A, 1, &row, 1, &row, &val_A, INSERT_VALUES));
932: if (build_rhs) PetscCall(DMStagVecSetValuesStencil(dm_main, rhs, 1, &row, &val_rhs, INSERT_VALUES));
933: } else {
934: DMStagStencil row, col[5];
935: PetscScalar val_A[5];
936: const PetscScalar val_rhs = 0.0;
938: row.i = ex;
939: row.j = ey;
940: row.loc = DMSTAG_ELEMENT;
941: row.c = 0;
942: col[0].i = ex;
943: col[0].j = ey;
944: col[0].loc = DMSTAG_LEFT;
945: col[0].c = 0;
946: val_A[0] = -1.0 * K_cont * dv / hx;
947: col[1].i = ex;
948: col[1].j = ey;
949: col[1].loc = DMSTAG_RIGHT;
950: col[1].c = 0;
951: val_A[1] = K_cont * dv / hx;
952: col[2].i = ex;
953: col[2].j = ey;
954: col[2].loc = DMSTAG_DOWN;
955: col[2].c = 0;
956: val_A[2] = -1.0 * K_cont * dv / hy;
957: col[3].i = ex;
958: col[3].j = ey;
959: col[3].loc = DMSTAG_UP;
960: col[3].c = 0;
961: val_A[3] = K_cont * dv / hy;
962: col[4] = row;
963: val_A[4] = 0.0;
964: PetscCall(DMStagMatSetValuesStencil(dm_main, A, 1, &row, 5, col, val_A, INSERT_VALUES));
965: if (build_rhs) PetscCall(DMStagVecSetValuesStencil(dm_main, rhs, 1, &row, &val_rhs, INSERT_VALUES));
966: }
967: }
968: }
969: }
970: PetscCall(DMRestoreLocalVector(dm_coefficients, &coefficients_local));
972: /* Add additional inverse viscosity terms (for use in building a matrix used to construct the preconditioner) */
973: if (parameters->include_inverse_visc) {
974: PetscCheck(!parameters->faces_only, PetscObjectComm((PetscObject)dm_main), PETSC_ERR_SUP, "Does not make sense with faces only");
975: PetscCall(OperatorInsertInverseViscosityPressureTerms(dm_main, dm_coefficients, ctx->levels[level]->coeff, 1.0, A));
976: }
978: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
979: if (build_rhs) PetscCall(VecAssemblyBegin(rhs));
980: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
981: if (build_rhs) PetscCall(VecAssemblyEnd(rhs));
982: PetscFunctionReturn(PETSC_SUCCESS);
983: }
985: static PetscErrorCode CreateSystem3d(SystemParameters parameters, Mat *pA, Vec *pRhs)
986: {
987: PetscInt N[3];
988: PetscInt ex, ey, ez, startx, starty, startz, nx, ny, nz;
989: Mat A;
990: PetscReal hx, hy, hz, dv;
991: PetscInt pinx, piny, pinz;
992: Vec coeff_local, rhs;
993: PetscBool build_rhs;
994: DM dm_main, dm_coefficients;
995: PetscScalar K_cont, K_bound;
996: Ctx ctx = parameters->ctx;
997: PetscInt level = parameters->level;
999: PetscFunctionBeginUser;
1000: if (parameters->faces_only) {
1001: dm_main = ctx->levels[level]->dm_faces;
1002: } else {
1003: dm_main = ctx->levels[level]->dm_stokes;
1004: }
1005: dm_coefficients = ctx->levels[level]->dm_coefficients;
1006: K_cont = ctx->levels[level]->K_cont;
1007: K_bound = ctx->levels[level]->K_bound;
1008: PetscCall(DMCreateMatrix(dm_main, pA));
1009: A = *pA;
1010: build_rhs = (PetscBool)(pRhs != NULL);
1011: if (build_rhs) {
1012: PetscCall(DMCreateGlobalVector(dm_main, pRhs));
1013: rhs = *pRhs;
1014: } else {
1015: rhs = NULL;
1016: }
1017: PetscCall(DMStagGetCorners(dm_main, &startx, &starty, &startz, &nx, &ny, &nz, NULL, NULL, NULL));
1018: PetscCall(DMStagGetGlobalSizes(dm_main, &N[0], &N[1], &N[2]));
1019: hx = ctx->levels[level]->hx_characteristic;
1020: hy = ctx->levels[level]->hy_characteristic;
1021: hz = ctx->levels[level]->hz_characteristic;
1022: dv = hx * hy * hz;
1023: PetscCall(DMGetLocalVector(dm_coefficients, &coeff_local));
1024: PetscCall(DMGlobalToLocal(dm_coefficients, ctx->levels[level]->coeff, INSERT_VALUES, coeff_local));
1026: PetscCheck(N[0] >= 2 && N[1] >= 2 && N[2] >= 2, PETSC_COMM_WORLD, PETSC_ERR_SUP, "Not implemented for less than 2 elements in any direction");
1027: pinx = 1;
1028: piny = 0;
1029: pinz = 0; /* Depends on assertion above that there are at least two element in the x direction */
1031: /* Loop over all local elements.
1033: For each element, fill 4-7 rows of the matrix, corresponding to
1034: - the pressure degree of freedom (dof), centered on the element
1035: - the 3 velocity dofs on left, bottom, and back faces of the element
1036: - velocity dof on the right, top, and front faces of the element (only on domain boundaries)
1038: */
1039: for (ez = startz; ez < startz + nz; ++ez) {
1040: for (ey = starty; ey < starty + ny; ++ey) {
1041: for (ex = startx; ex < startx + nx; ++ex) {
1042: const PetscBool left_boundary = (PetscBool)(ex == 0);
1043: const PetscBool right_boundary = (PetscBool)(ex == N[0] - 1);
1044: const PetscBool bottom_boundary = (PetscBool)(ey == 0);
1045: const PetscBool top_boundary = (PetscBool)(ey == N[1] - 1);
1046: const PetscBool back_boundary = (PetscBool)(ez == 0);
1047: const PetscBool front_boundary = (PetscBool)(ez == N[2] - 1);
1049: /* Note that below, we depend on the check above that there is never one
1050: element (globally) in a given direction. Thus, for example, an
1051: element is never both on the left and right boundary */
1053: /* X-faces - right boundary */
1054: if (right_boundary) {
1055: /* Right x-velocity Dirichlet */
1056: DMStagStencil row;
1057: const PetscScalar val_rhs = 0.0;
1058: const PetscScalar val_A = K_bound;
1060: row.i = ex;
1061: row.j = ey;
1062: row.k = ez;
1063: row.loc = DMSTAG_RIGHT;
1064: row.c = 0;
1065: PetscCall(DMStagMatSetValuesStencil(dm_main, A, 1, &row, 1, &row, &val_A, INSERT_VALUES));
1066: if (build_rhs) PetscCall(DMStagVecSetValuesStencil(dm_main, rhs, 1, &row, &val_rhs, INSERT_VALUES));
1067: }
1069: /* X faces - left*/
1070: {
1071: DMStagStencil row;
1073: row.i = ex;
1074: row.j = ey;
1075: row.k = ez;
1076: row.loc = DMSTAG_LEFT;
1077: row.c = 0;
1079: if (left_boundary) {
1080: /* Left x-velocity Dirichlet */
1081: const PetscScalar val_rhs = 0.0;
1082: const PetscScalar val_A = K_bound;
1084: PetscCall(DMStagMatSetValuesStencil(dm_main, A, 1, &row, 1, &row, &val_A, INSERT_VALUES));
1085: if (build_rhs) PetscCall(DMStagVecSetValuesStencil(dm_main, rhs, 1, &row, &val_rhs, INSERT_VALUES));
1086: } else {
1087: /* X-momentum equation */
1088: PetscInt count;
1089: DMStagStencil col[17];
1090: PetscScalar val_A[17];
1091: DMStagStencil eta_point[6];
1092: PetscScalar eta[6], eta_left, eta_right, eta_up, eta_down, eta_back, eta_front; /* relative to the left face */
1093: const PetscScalar val_rhs = 0.0;
1095: /* Get eta values */
1096: eta_point[0].i = ex - 1;
1097: eta_point[0].j = ey;
1098: eta_point[0].k = ez;
1099: eta_point[0].loc = DMSTAG_ELEMENT;
1100: eta_point[0].c = 0; /* Left */
1101: eta_point[1].i = ex;
1102: eta_point[1].j = ey;
1103: eta_point[1].k = ez;
1104: eta_point[1].loc = DMSTAG_ELEMENT;
1105: eta_point[1].c = 0; /* Right */
1106: eta_point[2].i = ex;
1107: eta_point[2].j = ey;
1108: eta_point[2].k = ez;
1109: eta_point[2].loc = DMSTAG_DOWN_LEFT;
1110: eta_point[2].c = 0; /* Down */
1111: eta_point[3].i = ex;
1112: eta_point[3].j = ey;
1113: eta_point[3].k = ez;
1114: eta_point[3].loc = DMSTAG_UP_LEFT;
1115: eta_point[3].c = 0; /* Up */
1116: eta_point[4].i = ex;
1117: eta_point[4].j = ey;
1118: eta_point[4].k = ez;
1119: eta_point[4].loc = DMSTAG_BACK_LEFT;
1120: eta_point[4].c = 0; /* Back */
1121: eta_point[5].i = ex;
1122: eta_point[5].j = ey;
1123: eta_point[5].k = ez;
1124: eta_point[5].loc = DMSTAG_FRONT_LEFT;
1125: eta_point[5].c = 0; /* Front */
1126: PetscCall(DMStagVecGetValuesStencil(dm_coefficients, coeff_local, 6, eta_point, eta));
1127: eta_left = eta[0];
1128: eta_right = eta[1];
1129: eta_down = eta[2];
1130: eta_up = eta[3];
1131: eta_back = eta[4];
1132: eta_front = eta[5];
1134: count = 0;
1136: col[count] = row;
1137: val_A[count] = -2.0 * dv * (eta_left + eta_right) / (hx * hx);
1138: if (!top_boundary) val_A[count] += -1.0 * dv * eta_up / (hy * hy);
1139: if (!bottom_boundary) val_A[count] += -1.0 * dv * eta_down / (hy * hy);
1140: if (!back_boundary) val_A[count] += -1.0 * dv * eta_back / (hz * hz);
1141: if (!front_boundary) val_A[count] += -1.0 * dv * eta_front / (hz * hz);
1142: ++count;
1144: col[count].i = ex - 1;
1145: col[count].j = ey;
1146: col[count].k = ez;
1147: col[count].loc = DMSTAG_LEFT;
1148: col[count].c = 0;
1149: val_A[count] = 2.0 * dv * eta_left / (hx * hx);
1150: ++count;
1151: col[count].i = ex + 1;
1152: col[count].j = ey;
1153: col[count].k = ez;
1154: col[count].loc = DMSTAG_LEFT;
1155: col[count].c = 0;
1156: val_A[count] = 2.0 * dv * eta_right / (hx * hx);
1157: ++count;
1158: if (!bottom_boundary) {
1159: col[count].i = ex;
1160: col[count].j = ey - 1;
1161: col[count].k = ez;
1162: col[count].loc = DMSTAG_LEFT;
1163: col[count].c = 0;
1164: val_A[count] = dv * eta_down / (hy * hy);
1165: ++count;
1166: }
1167: if (!top_boundary) {
1168: col[count].i = ex;
1169: col[count].j = ey + 1;
1170: col[count].k = ez;
1171: col[count].loc = DMSTAG_LEFT;
1172: col[count].c = 0;
1173: val_A[count] = dv * eta_up / (hy * hy);
1174: ++count;
1175: }
1176: if (!back_boundary) {
1177: col[count].i = ex;
1178: col[count].j = ey;
1179: col[count].k = ez - 1;
1180: col[count].loc = DMSTAG_LEFT;
1181: col[count].c = 0;
1182: val_A[count] = dv * eta_back / (hz * hz);
1183: ++count;
1184: }
1185: if (!front_boundary) {
1186: col[count].i = ex;
1187: col[count].j = ey;
1188: col[count].k = ez + 1;
1189: col[count].loc = DMSTAG_LEFT;
1190: col[count].c = 0;
1191: val_A[count] = dv * eta_front / (hz * hz);
1192: ++count;
1193: }
1195: col[count].i = ex - 1;
1196: col[count].j = ey;
1197: col[count].k = ez;
1198: col[count].loc = DMSTAG_DOWN;
1199: col[count].c = 0;
1200: val_A[count] = dv * eta_down / (hx * hy);
1201: ++count; /* down left */
1202: col[count].i = ex;
1203: col[count].j = ey;
1204: col[count].k = ez;
1205: col[count].loc = DMSTAG_DOWN;
1206: col[count].c = 0;
1207: val_A[count] = -1.0 * dv * eta_down / (hx * hy);
1208: ++count; /* down right */
1210: col[count].i = ex - 1;
1211: col[count].j = ey;
1212: col[count].k = ez;
1213: col[count].loc = DMSTAG_UP;
1214: col[count].c = 0;
1215: val_A[count] = -1.0 * dv * eta_up / (hx * hy);
1216: ++count; /* up left */
1217: col[count].i = ex;
1218: col[count].j = ey;
1219: col[count].k = ez;
1220: col[count].loc = DMSTAG_UP;
1221: col[count].c = 0;
1222: val_A[count] = dv * eta_up / (hx * hy);
1223: ++count; /* up right */
1225: col[count].i = ex - 1;
1226: col[count].j = ey;
1227: col[count].k = ez;
1228: col[count].loc = DMSTAG_BACK;
1229: col[count].c = 0;
1230: val_A[count] = dv * eta_back / (hx * hz);
1231: ++count; /* back left */
1232: col[count].i = ex;
1233: col[count].j = ey;
1234: col[count].k = ez;
1235: col[count].loc = DMSTAG_BACK;
1236: col[count].c = 0;
1237: val_A[count] = -1.0 * dv * eta_back / (hx * hz);
1238: ++count; /* back right */
1240: col[count].i = ex - 1;
1241: col[count].j = ey;
1242: col[count].k = ez;
1243: col[count].loc = DMSTAG_FRONT;
1244: col[count].c = 0;
1245: val_A[count] = -1.0 * dv * eta_front / (hx * hz);
1246: ++count; /* front left */
1247: col[count].i = ex;
1248: col[count].j = ey;
1249: col[count].k = ez;
1250: col[count].loc = DMSTAG_FRONT;
1251: col[count].c = 0;
1252: val_A[count] = dv * eta_front / (hx * hz);
1253: ++count; /* front right */
1255: if (!parameters->faces_only) {
1256: col[count].i = ex - 1;
1257: col[count].j = ey;
1258: col[count].k = ez;
1259: col[count].loc = DMSTAG_ELEMENT;
1260: col[count].c = 0;
1261: val_A[count] = K_cont * dv / hx;
1262: ++count;
1263: col[count].i = ex;
1264: col[count].j = ey;
1265: col[count].k = ez;
1266: col[count].loc = DMSTAG_ELEMENT;
1267: col[count].c = 0;
1268: val_A[count] = -1.0 * K_cont * dv / hx;
1269: ++count;
1270: }
1272: PetscCall(DMStagMatSetValuesStencil(dm_main, A, 1, &row, count, col, val_A, INSERT_VALUES));
1273: if (build_rhs) PetscCall(DMStagVecSetValuesStencil(dm_main, rhs, 1, &row, &val_rhs, INSERT_VALUES));
1274: }
1275: }
1277: /* Y faces - top boundary */
1278: if (top_boundary) {
1279: /* Top y-velocity Dirichlet */
1280: DMStagStencil row;
1281: const PetscScalar val_rhs = 0.0;
1282: const PetscScalar val_A = K_bound;
1284: row.i = ex;
1285: row.j = ey;
1286: row.k = ez;
1287: row.loc = DMSTAG_UP;
1288: row.c = 0;
1289: PetscCall(DMStagMatSetValuesStencil(dm_main, A, 1, &row, 1, &row, &val_A, INSERT_VALUES));
1290: if (build_rhs) PetscCall(DMStagVecSetValuesStencil(dm_main, rhs, 1, &row, &val_rhs, INSERT_VALUES));
1291: }
1293: /* Y faces - down */
1294: {
1295: DMStagStencil row;
1297: row.i = ex;
1298: row.j = ey;
1299: row.k = ez;
1300: row.loc = DMSTAG_DOWN;
1301: row.c = 0;
1303: if (bottom_boundary) {
1304: /* Bottom y-velocity Dirichlet */
1305: const PetscScalar val_rhs = 0.0;
1306: const PetscScalar val_A = K_bound;
1308: PetscCall(DMStagMatSetValuesStencil(dm_main, A, 1, &row, 1, &row, &val_A, INSERT_VALUES));
1309: if (build_rhs) PetscCall(DMStagVecSetValuesStencil(dm_main, rhs, 1, &row, &val_rhs, INSERT_VALUES));
1310: } else {
1311: /* Y-momentum equation (including non-zero forcing) */
1312: PetscInt count;
1313: DMStagStencil col[17];
1314: PetscScalar val_rhs, val_A[17];
1315: DMStagStencil eta_point[6], rho_point[4];
1316: PetscScalar eta[6], rho[4], eta_left, eta_right, eta_up, eta_down, eta_back, eta_front; /* relative to the bottom face */
1318: if (build_rhs) {
1319: /* get rho values (note .c = 1) */
1320: /* Note that we have rho at perhaps strange points (edges not corners) */
1321: rho_point[0].i = ex;
1322: rho_point[0].j = ey;
1323: rho_point[0].k = ez;
1324: rho_point[0].loc = DMSTAG_DOWN_LEFT;
1325: rho_point[0].c = 1;
1326: rho_point[1].i = ex;
1327: rho_point[1].j = ey;
1328: rho_point[1].k = ez;
1329: rho_point[1].loc = DMSTAG_DOWN_RIGHT;
1330: rho_point[1].c = 1;
1331: rho_point[2].i = ex;
1332: rho_point[2].j = ey;
1333: rho_point[2].k = ez;
1334: rho_point[2].loc = DMSTAG_BACK_DOWN;
1335: rho_point[2].c = 1;
1336: rho_point[3].i = ex;
1337: rho_point[3].j = ey;
1338: rho_point[3].k = ez;
1339: rho_point[3].loc = DMSTAG_FRONT_DOWN;
1340: rho_point[3].c = 1;
1341: PetscCall(DMStagVecGetValuesStencil(dm_coefficients, coeff_local, 4, rho_point, rho));
1343: /* Compute forcing */
1344: val_rhs = ctx->gy * dv * (0.25 * (rho[0] + rho[1] + rho[2] + rho[3]));
1345: }
1347: /* Get eta values */
1348: eta_point[0].i = ex;
1349: eta_point[0].j = ey;
1350: eta_point[0].k = ez;
1351: eta_point[0].loc = DMSTAG_DOWN_LEFT;
1352: eta_point[0].c = 0; /* Left */
1353: eta_point[1].i = ex;
1354: eta_point[1].j = ey;
1355: eta_point[1].k = ez;
1356: eta_point[1].loc = DMSTAG_DOWN_RIGHT;
1357: eta_point[1].c = 0; /* Right */
1358: eta_point[2].i = ex;
1359: eta_point[2].j = ey - 1;
1360: eta_point[2].k = ez;
1361: eta_point[2].loc = DMSTAG_ELEMENT;
1362: eta_point[2].c = 0; /* Down */
1363: eta_point[3].i = ex;
1364: eta_point[3].j = ey;
1365: eta_point[3].k = ez;
1366: eta_point[3].loc = DMSTAG_ELEMENT;
1367: eta_point[3].c = 0; /* Up */
1368: eta_point[4].i = ex;
1369: eta_point[4].j = ey;
1370: eta_point[4].k = ez;
1371: eta_point[4].loc = DMSTAG_BACK_DOWN;
1372: eta_point[4].c = 0; /* Back */
1373: eta_point[5].i = ex;
1374: eta_point[5].j = ey;
1375: eta_point[5].k = ez;
1376: eta_point[5].loc = DMSTAG_FRONT_DOWN;
1377: eta_point[5].c = 0; /* Front */
1378: PetscCall(DMStagVecGetValuesStencil(dm_coefficients, coeff_local, 6, eta_point, eta));
1379: eta_left = eta[0];
1380: eta_right = eta[1];
1381: eta_down = eta[2];
1382: eta_up = eta[3];
1383: eta_back = eta[4];
1384: eta_front = eta[5];
1386: count = 0;
1388: col[count] = row;
1389: val_A[count] = -2.0 * dv * (eta_up + eta_down) / (hy * hy);
1390: if (!left_boundary) val_A[count] += -1.0 * dv * eta_left / (hx * hx);
1391: if (!right_boundary) val_A[count] += -1.0 * dv * eta_right / (hx * hx);
1392: if (!back_boundary) val_A[count] += -1.0 * dv * eta_back / (hz * hz);
1393: if (!front_boundary) val_A[count] += -1.0 * dv * eta_front / (hz * hz);
1394: ++count;
1396: col[count].i = ex;
1397: col[count].j = ey - 1;
1398: col[count].k = ez;
1399: col[count].loc = DMSTAG_DOWN;
1400: col[count].c = 0;
1401: val_A[count] = 2.0 * dv * eta_down / (hy * hy);
1402: ++count;
1403: col[count].i = ex;
1404: col[count].j = ey + 1;
1405: col[count].k = ez;
1406: col[count].loc = DMSTAG_DOWN;
1407: col[count].c = 0;
1408: val_A[count] = 2.0 * dv * eta_up / (hy * hy);
1409: ++count;
1411: if (!left_boundary) {
1412: col[count].i = ex - 1;
1413: col[count].j = ey;
1414: col[count].k = ez;
1415: col[count].loc = DMSTAG_DOWN;
1416: col[count].c = 0;
1417: val_A[count] = dv * eta_left / (hx * hx);
1418: ++count;
1419: }
1420: if (!right_boundary) {
1421: col[count].i = ex + 1;
1422: col[count].j = ey;
1423: col[count].k = ez;
1424: col[count].loc = DMSTAG_DOWN;
1425: col[count].c = 0;
1426: val_A[count] = dv * eta_right / (hx * hx);
1427: ++count;
1428: }
1429: if (!back_boundary) {
1430: col[count].i = ex;
1431: col[count].j = ey;
1432: col[count].k = ez - 1;
1433: col[count].loc = DMSTAG_DOWN;
1434: col[count].c = 0;
1435: val_A[count] = dv * eta_back / (hz * hz);
1436: ++count;
1437: }
1438: if (!front_boundary) {
1439: col[count].i = ex;
1440: col[count].j = ey;
1441: col[count].k = ez + 1;
1442: col[count].loc = DMSTAG_DOWN;
1443: col[count].c = 0;
1444: val_A[count] = dv * eta_front / (hz * hz);
1445: ++count;
1446: }
1448: col[count].i = ex;
1449: col[count].j = ey - 1;
1450: col[count].k = ez;
1451: col[count].loc = DMSTAG_LEFT;
1452: col[count].c = 0;
1453: val_A[count] = dv * eta_left / (hx * hy);
1454: ++count; /* down left*/
1455: col[count].i = ex;
1456: col[count].j = ey;
1457: col[count].k = ez;
1458: col[count].loc = DMSTAG_LEFT;
1459: col[count].c = 0;
1460: val_A[count] = -1.0 * dv * eta_left / (hx * hy);
1461: ++count; /* up left*/
1463: col[count].i = ex;
1464: col[count].j = ey - 1;
1465: col[count].k = ez;
1466: col[count].loc = DMSTAG_RIGHT;
1467: col[count].c = 0;
1468: val_A[count] = -1.0 * dv * eta_right / (hx * hy);
1469: ++count; /* down right*/
1470: col[count].i = ex;
1471: col[count].j = ey;
1472: col[count].k = ez;
1473: col[count].loc = DMSTAG_RIGHT;
1474: col[count].c = 0;
1475: val_A[count] = dv * eta_right / (hx * hy);
1476: ++count; /* up right*/
1478: col[count].i = ex;
1479: col[count].j = ey - 1;
1480: col[count].k = ez;
1481: col[count].loc = DMSTAG_BACK;
1482: col[count].c = 0;
1483: val_A[count] = dv * eta_back / (hy * hz);
1484: ++count; /* back down */
1485: col[count].i = ex;
1486: col[count].j = ey;
1487: col[count].k = ez;
1488: col[count].loc = DMSTAG_BACK;
1489: col[count].c = 0;
1490: val_A[count] = -1.0 * dv * eta_back / (hy * hz);
1491: ++count; /* back up */
1493: col[count].i = ex;
1494: col[count].j = ey - 1;
1495: col[count].k = ez;
1496: col[count].loc = DMSTAG_FRONT;
1497: col[count].c = 0;
1498: val_A[count] = -1.0 * dv * eta_front / (hy * hz);
1499: ++count; /* front down */
1500: col[count].i = ex;
1501: col[count].j = ey;
1502: col[count].k = ez;
1503: col[count].loc = DMSTAG_FRONT;
1504: col[count].c = 0;
1505: val_A[count] = dv * eta_front / (hy * hz);
1506: ++count; /* front up */
1508: if (!parameters->faces_only) {
1509: col[count].i = ex;
1510: col[count].j = ey - 1;
1511: col[count].k = ez;
1512: col[count].loc = DMSTAG_ELEMENT;
1513: col[count].c = 0;
1514: val_A[count] = K_cont * dv / hy;
1515: ++count;
1516: col[count].i = ex;
1517: col[count].j = ey;
1518: col[count].k = ez;
1519: col[count].loc = DMSTAG_ELEMENT;
1520: col[count].c = 0;
1521: val_A[count] = -1.0 * K_cont * dv / hy;
1522: ++count;
1523: }
1525: PetscCall(DMStagMatSetValuesStencil(dm_main, A, 1, &row, count, col, val_A, INSERT_VALUES));
1526: if (build_rhs) PetscCall(DMStagVecSetValuesStencil(dm_main, rhs, 1, &row, &val_rhs, INSERT_VALUES));
1527: }
1528: }
1530: if (front_boundary) {
1531: /* Front z-velocity Dirichlet */
1532: DMStagStencil row;
1533: const PetscScalar val_rhs = 0.0;
1534: const PetscScalar val_A = K_bound;
1536: row.i = ex;
1537: row.j = ey;
1538: row.k = ez;
1539: row.loc = DMSTAG_FRONT;
1540: row.c = 0;
1541: PetscCall(DMStagMatSetValuesStencil(dm_main, A, 1, &row, 1, &row, &val_A, INSERT_VALUES));
1542: if (build_rhs) PetscCall(DMStagVecSetValuesStencil(dm_main, rhs, 1, &row, &val_rhs, INSERT_VALUES));
1543: }
1545: /* Z faces - back */
1546: {
1547: DMStagStencil row;
1549: row.i = ex;
1550: row.j = ey;
1551: row.k = ez;
1552: row.loc = DMSTAG_BACK;
1553: row.c = 0;
1555: if (back_boundary) {
1556: /* Back z-velocity Dirichlet */
1557: const PetscScalar val_rhs = 0.0;
1558: const PetscScalar val_A = K_bound;
1560: PetscCall(DMStagMatSetValuesStencil(dm_main, A, 1, &row, 1, &row, &val_A, INSERT_VALUES));
1561: if (build_rhs) PetscCall(DMStagVecSetValuesStencil(dm_main, rhs, 1, &row, &val_rhs, INSERT_VALUES));
1562: } else {
1563: /* Z-momentum equation */
1564: PetscInt count;
1565: DMStagStencil col[17];
1566: PetscScalar val_A[17];
1567: DMStagStencil eta_point[6];
1568: PetscScalar eta[6], eta_left, eta_right, eta_up, eta_down, eta_back, eta_front; /* relative to the back face */
1569: const PetscScalar val_rhs = 0.0;
1571: /* Get eta values */
1572: eta_point[0].i = ex;
1573: eta_point[0].j = ey;
1574: eta_point[0].k = ez;
1575: eta_point[0].loc = DMSTAG_BACK_LEFT;
1576: eta_point[0].c = 0; /* Left */
1577: eta_point[1].i = ex;
1578: eta_point[1].j = ey;
1579: eta_point[1].k = ez;
1580: eta_point[1].loc = DMSTAG_BACK_RIGHT;
1581: eta_point[1].c = 0; /* Right */
1582: eta_point[2].i = ex;
1583: eta_point[2].j = ey;
1584: eta_point[2].k = ez;
1585: eta_point[2].loc = DMSTAG_BACK_DOWN;
1586: eta_point[2].c = 0; /* Down */
1587: eta_point[3].i = ex;
1588: eta_point[3].j = ey;
1589: eta_point[3].k = ez;
1590: eta_point[3].loc = DMSTAG_BACK_UP;
1591: eta_point[3].c = 0; /* Up */
1592: eta_point[4].i = ex;
1593: eta_point[4].j = ey;
1594: eta_point[4].k = ez - 1;
1595: eta_point[4].loc = DMSTAG_ELEMENT;
1596: eta_point[4].c = 0; /* Back */
1597: eta_point[5].i = ex;
1598: eta_point[5].j = ey;
1599: eta_point[5].k = ez;
1600: eta_point[5].loc = DMSTAG_ELEMENT;
1601: eta_point[5].c = 0; /* Front */
1602: PetscCall(DMStagVecGetValuesStencil(dm_coefficients, coeff_local, 6, eta_point, eta));
1603: eta_left = eta[0];
1604: eta_right = eta[1];
1605: eta_down = eta[2];
1606: eta_up = eta[3];
1607: eta_back = eta[4];
1608: eta_front = eta[5];
1610: count = 0;
1612: col[count] = row;
1613: val_A[count] = -2.0 * dv * (eta_back + eta_front) / (hz * hz);
1614: if (!left_boundary) val_A[count] += -1.0 * dv * eta_left / (hx * hx);
1615: if (!right_boundary) val_A[count] += -1.0 * dv * eta_right / (hx * hx);
1616: if (!top_boundary) val_A[count] += -1.0 * dv * eta_up / (hy * hy);
1617: if (!bottom_boundary) val_A[count] += -1.0 * dv * eta_down / (hy * hy);
1618: ++count;
1620: col[count].i = ex;
1621: col[count].j = ey;
1622: col[count].k = ez - 1;
1623: col[count].loc = DMSTAG_BACK;
1624: col[count].c = 0;
1625: val_A[count] = 2.0 * dv * eta_back / (hz * hz);
1626: ++count;
1627: col[count].i = ex;
1628: col[count].j = ey;
1629: col[count].k = ez + 1;
1630: col[count].loc = DMSTAG_BACK;
1631: col[count].c = 0;
1632: val_A[count] = 2.0 * dv * eta_front / (hz * hz);
1633: ++count;
1635: if (!left_boundary) {
1636: col[count].i = ex - 1;
1637: col[count].j = ey;
1638: col[count].k = ez;
1639: col[count].loc = DMSTAG_BACK;
1640: col[count].c = 0;
1641: val_A[count] = dv * eta_left / (hx * hx);
1642: ++count;
1643: }
1644: if (!right_boundary) {
1645: col[count].i = ex + 1;
1646: col[count].j = ey;
1647: col[count].k = ez;
1648: col[count].loc = DMSTAG_BACK;
1649: col[count].c = 0;
1650: val_A[count] = dv * eta_right / (hx * hx);
1651: ++count;
1652: }
1653: if (!bottom_boundary) {
1654: col[count].i = ex;
1655: col[count].j = ey - 1;
1656: col[count].k = ez;
1657: col[count].loc = DMSTAG_BACK;
1658: col[count].c = 0;
1659: val_A[count] = dv * eta_down / (hy * hy);
1660: ++count;
1661: }
1662: if (!top_boundary) {
1663: col[count].i = ex;
1664: col[count].j = ey + 1;
1665: col[count].k = ez;
1666: col[count].loc = DMSTAG_BACK;
1667: col[count].c = 0;
1668: val_A[count] = dv * eta_up / (hy * hy);
1669: ++count;
1670: }
1672: col[count].i = ex;
1673: col[count].j = ey;
1674: col[count].k = ez - 1;
1675: col[count].loc = DMSTAG_LEFT;
1676: col[count].c = 0;
1677: val_A[count] = dv * eta_left / (hx * hz);
1678: ++count; /* back left*/
1679: col[count].i = ex;
1680: col[count].j = ey;
1681: col[count].k = ez;
1682: col[count].loc = DMSTAG_LEFT;
1683: col[count].c = 0;
1684: val_A[count] = -1.0 * dv * eta_left / (hx * hz);
1685: ++count; /* front left*/
1687: col[count].i = ex;
1688: col[count].j = ey;
1689: col[count].k = ez - 1;
1690: col[count].loc = DMSTAG_RIGHT;
1691: col[count].c = 0;
1692: val_A[count] = -1.0 * dv * eta_right / (hx * hz);
1693: ++count; /* back right */
1694: col[count].i = ex;
1695: col[count].j = ey;
1696: col[count].k = ez;
1697: col[count].loc = DMSTAG_RIGHT;
1698: col[count].c = 0;
1699: val_A[count] = dv * eta_right / (hx * hz);
1700: ++count; /* front right*/
1702: col[count].i = ex;
1703: col[count].j = ey;
1704: col[count].k = ez - 1;
1705: col[count].loc = DMSTAG_DOWN;
1706: col[count].c = 0;
1707: val_A[count] = dv * eta_down / (hy * hz);
1708: ++count; /* back down */
1709: col[count].i = ex;
1710: col[count].j = ey;
1711: col[count].k = ez;
1712: col[count].loc = DMSTAG_DOWN;
1713: col[count].c = 0;
1714: val_A[count] = -1.0 * dv * eta_down / (hy * hz);
1715: ++count; /* back down */
1717: col[count].i = ex;
1718: col[count].j = ey;
1719: col[count].k = ez - 1;
1720: col[count].loc = DMSTAG_UP;
1721: col[count].c = 0;
1722: val_A[count] = -1.0 * dv * eta_up / (hy * hz);
1723: ++count; /* back up */
1724: col[count].i = ex;
1725: col[count].j = ey;
1726: col[count].k = ez;
1727: col[count].loc = DMSTAG_UP;
1728: col[count].c = 0;
1729: val_A[count] = dv * eta_up / (hy * hz);
1730: ++count; /* back up */
1732: if (!parameters->faces_only) {
1733: col[count].i = ex;
1734: col[count].j = ey;
1735: col[count].k = ez - 1;
1736: col[count].loc = DMSTAG_ELEMENT;
1737: col[count].c = 0;
1738: val_A[count] = K_cont * dv / hz;
1739: ++count;
1740: col[count].i = ex;
1741: col[count].j = ey;
1742: col[count].k = ez;
1743: col[count].loc = DMSTAG_ELEMENT;
1744: col[count].c = 0;
1745: val_A[count] = -1.0 * K_cont * dv / hz;
1746: ++count;
1747: }
1749: PetscCall(DMStagMatSetValuesStencil(dm_main, A, 1, &row, count, col, val_A, INSERT_VALUES));
1750: if (build_rhs) PetscCall(DMStagVecSetValuesStencil(dm_main, rhs, 1, &row, &val_rhs, INSERT_VALUES));
1751: }
1752: }
1754: /* Elements */
1755: if (!parameters->faces_only) {
1756: DMStagStencil row;
1758: row.i = ex;
1759: row.j = ey;
1760: row.k = ez;
1761: row.loc = DMSTAG_ELEMENT;
1762: row.c = 0;
1764: if (ctx->pin_pressure && ex == pinx && ey == piny && ez == pinz) {
1765: /* Pin a pressure node to zero, if requested */
1766: const PetscScalar val_A = K_bound;
1767: const PetscScalar val_rhs = 0.0;
1769: PetscCall(DMStagMatSetValuesStencil(dm_main, A, 1, &row, 1, &row, &val_A, INSERT_VALUES));
1770: if (build_rhs) PetscCall(DMStagVecSetValuesStencil(dm_main, rhs, 1, &row, &val_rhs, INSERT_VALUES));
1771: } else {
1772: /* Continuity equation */
1773: /* Note that this includes an explicit zero on the diagonal. This is only needed for
1774: some direct solvers (not required if using an iterative solver and setting a constant-pressure nullspace) */
1775: /* Note: the scaling by dv is not chosen in a principled way and is likely sub-optimal */
1776: DMStagStencil col[7];
1777: PetscScalar val_A[7];
1778: const PetscScalar val_rhs = 0.0;
1780: col[0].i = ex;
1781: col[0].j = ey;
1782: col[0].k = ez;
1783: col[0].loc = DMSTAG_LEFT;
1784: col[0].c = 0;
1785: val_A[0] = -1.0 * K_cont * dv / hx;
1786: col[1].i = ex;
1787: col[1].j = ey;
1788: col[1].k = ez;
1789: col[1].loc = DMSTAG_RIGHT;
1790: col[1].c = 0;
1791: val_A[1] = K_cont * dv / hx;
1792: col[2].i = ex;
1793: col[2].j = ey;
1794: col[2].k = ez;
1795: col[2].loc = DMSTAG_DOWN;
1796: col[2].c = 0;
1797: val_A[2] = -1.0 * K_cont * dv / hy;
1798: col[3].i = ex;
1799: col[3].j = ey;
1800: col[3].k = ez;
1801: col[3].loc = DMSTAG_UP;
1802: col[3].c = 0;
1803: val_A[3] = K_cont * dv / hy;
1804: col[4].i = ex;
1805: col[4].j = ey;
1806: col[4].k = ez;
1807: col[4].loc = DMSTAG_BACK;
1808: col[4].c = 0;
1809: val_A[4] = -1.0 * K_cont * dv / hz;
1810: col[5].i = ex;
1811: col[5].j = ey;
1812: col[5].k = ez;
1813: col[5].loc = DMSTAG_FRONT;
1814: col[5].c = 0;
1815: val_A[5] = K_cont * dv / hz;
1816: col[6] = row;
1817: val_A[6] = 0.0;
1818: PetscCall(DMStagMatSetValuesStencil(dm_main, A, 1, &row, 7, col, val_A, INSERT_VALUES));
1819: if (build_rhs) PetscCall(DMStagVecSetValuesStencil(dm_main, rhs, 1, &row, &val_rhs, INSERT_VALUES));
1820: }
1821: }
1822: }
1823: }
1824: }
1825: PetscCall(DMRestoreLocalVector(dm_coefficients, &coeff_local));
1827: /* Add additional inverse viscosity terms (for use in building a matrix used to construct the preconditioner) */
1828: if (parameters->include_inverse_visc) {
1829: PetscCheck(!parameters->faces_only, PetscObjectComm((PetscObject)dm_main), PETSC_ERR_SUP, "Does not make sense with faces only");
1830: PetscCall(OperatorInsertInverseViscosityPressureTerms(dm_main, dm_coefficients, ctx->levels[level]->coeff, 1.0, A));
1831: }
1833: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
1834: if (build_rhs) PetscCall(VecAssemblyBegin(rhs));
1835: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
1836: if (build_rhs) PetscCall(VecAssemblyEnd(rhs));
1837: PetscFunctionReturn(PETSC_SUCCESS);
1838: }
1840: static PetscErrorCode CreateSystem(SystemParameters parameters, Mat *pA, Vec *pRhs)
1841: {
1842: PetscFunctionBeginUser;
1843: if (parameters->ctx->dim == 2) {
1844: PetscCall(CreateSystem2d(parameters, pA, pRhs));
1845: } else if (parameters->ctx->dim == 3) {
1846: PetscCall(CreateSystem3d(parameters, pA, pRhs));
1847: } else SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "Unsupported dimension %" PetscInt_FMT, parameters->ctx->dim);
1848: PetscFunctionReturn(PETSC_SUCCESS);
1849: }
1851: PetscErrorCode PopulateCoefficientData(Ctx ctx, PetscInt level)
1852: {
1853: PetscInt dim;
1854: PetscInt N[3];
1855: PetscInt ex, ey, ez, startx, starty, startz, nx, ny, nz;
1856: PetscInt slot_prev, slot_center;
1857: PetscInt slot_rho_downleft, slot_rho_backleft, slot_rho_backdown, slot_eta_element, slot_eta_downleft, slot_eta_backleft, slot_eta_backdown;
1858: Vec coeff_local;
1859: PetscReal **arr_coordinates_x, **arr_coordinates_y, **arr_coordinates_z;
1860: DM dm_coefficients;
1861: Vec coeff;
1863: PetscFunctionBeginUser;
1864: dm_coefficients = ctx->levels[level]->dm_coefficients;
1865: PetscCall(DMGetDimension(dm_coefficients, &dim));
1867: /* Create global coefficient vector */
1868: PetscCall(DMCreateGlobalVector(dm_coefficients, &ctx->levels[level]->coeff));
1869: coeff = ctx->levels[level]->coeff;
1871: /* Get temporary access to a local representation of the coefficient data */
1872: PetscCall(DMGetLocalVector(dm_coefficients, &coeff_local));
1873: PetscCall(DMGlobalToLocal(dm_coefficients, coeff, INSERT_VALUES, coeff_local));
1875: /* Use direct array access to coefficient and coordinate arrays, to popoulate coefficient data */
1876: PetscCall(DMStagGetGhostCorners(dm_coefficients, &startx, &starty, &startz, &nx, &ny, &nz));
1877: PetscCall(DMStagGetGlobalSizes(dm_coefficients, &N[0], &N[1], &N[2]));
1878: PetscCall(DMStagGetProductCoordinateArraysRead(dm_coefficients, &arr_coordinates_x, &arr_coordinates_y, &arr_coordinates_z));
1879: PetscCall(DMStagGetProductCoordinateLocationSlot(dm_coefficients, DMSTAG_ELEMENT, &slot_center));
1880: PetscCall(DMStagGetProductCoordinateLocationSlot(dm_coefficients, DMSTAG_LEFT, &slot_prev));
1881: PetscCall(DMStagGetLocationSlot(dm_coefficients, DMSTAG_ELEMENT, 0, &slot_eta_element));
1882: PetscCall(DMStagGetLocationSlot(dm_coefficients, DMSTAG_DOWN_LEFT, 0, &slot_eta_downleft));
1883: PetscCall(DMStagGetLocationSlot(dm_coefficients, DMSTAG_DOWN_LEFT, 1, &slot_rho_downleft));
1884: if (dim == 2) {
1885: PetscScalar ***arr_coefficients;
1887: PetscCall(DMStagVecGetArray(dm_coefficients, coeff_local, &arr_coefficients));
1888: /* Note that these ranges are with respect to the local representation */
1889: for (ey = starty; ey < starty + ny; ++ey) {
1890: for (ex = startx; ex < startx + nx; ++ex) {
1891: arr_coefficients[ey][ex][slot_eta_element] = ctx->GetEta(ctx, arr_coordinates_x[ex][slot_center], arr_coordinates_y[ey][slot_center], 0.0);
1892: arr_coefficients[ey][ex][slot_eta_downleft] = ctx->GetEta(ctx, arr_coordinates_x[ex][slot_prev], arr_coordinates_y[ey][slot_prev], 0.0);
1893: arr_coefficients[ey][ex][slot_rho_downleft] = ctx->GetRho(ctx, arr_coordinates_x[ex][slot_prev], arr_coordinates_y[ey][slot_prev], 0.0);
1894: }
1895: }
1896: PetscCall(DMStagVecRestoreArray(dm_coefficients, coeff_local, &arr_coefficients));
1897: } else if (dim == 3) {
1898: PetscScalar ****arr_coefficients;
1900: PetscCall(DMStagGetLocationSlot(dm_coefficients, DMSTAG_BACK_LEFT, 0, &slot_eta_backleft));
1901: PetscCall(DMStagGetLocationSlot(dm_coefficients, DMSTAG_BACK_LEFT, 1, &slot_rho_backleft));
1902: PetscCall(DMStagGetLocationSlot(dm_coefficients, DMSTAG_BACK_DOWN, 0, &slot_eta_backdown));
1903: PetscCall(DMStagGetLocationSlot(dm_coefficients, DMSTAG_BACK_DOWN, 1, &slot_rho_backdown));
1904: PetscCall(DMStagVecGetArray(dm_coefficients, coeff_local, &arr_coefficients));
1905: /* Note that these are with respect to the entire local representation, including ghosts */
1906: for (ez = startz; ez < startz + nz; ++ez) {
1907: for (ey = starty; ey < starty + ny; ++ey) {
1908: for (ex = startx; ex < startx + nx; ++ex) {
1909: const PetscScalar x_prev = arr_coordinates_x[ex][slot_prev];
1910: const PetscScalar y_prev = arr_coordinates_y[ey][slot_prev];
1911: const PetscScalar z_prev = arr_coordinates_z[ez][slot_prev];
1912: const PetscScalar x_center = arr_coordinates_x[ex][slot_center];
1913: const PetscScalar y_center = arr_coordinates_y[ey][slot_center];
1914: const PetscScalar z_center = arr_coordinates_z[ez][slot_center];
1916: arr_coefficients[ez][ey][ex][slot_eta_element] = ctx->GetEta(ctx, x_center, y_center, z_center);
1917: arr_coefficients[ez][ey][ex][slot_eta_downleft] = ctx->GetEta(ctx, x_prev, y_prev, z_center);
1918: arr_coefficients[ez][ey][ex][slot_rho_downleft] = ctx->GetRho(ctx, x_prev, y_prev, z_center);
1919: arr_coefficients[ez][ey][ex][slot_eta_backleft] = ctx->GetEta(ctx, x_prev, y_center, z_prev);
1920: arr_coefficients[ez][ey][ex][slot_rho_backleft] = ctx->GetRho(ctx, x_prev, y_center, z_prev);
1921: arr_coefficients[ez][ey][ex][slot_eta_backdown] = ctx->GetEta(ctx, x_center, y_prev, z_prev);
1922: arr_coefficients[ez][ey][ex][slot_rho_backdown] = ctx->GetRho(ctx, x_center, y_prev, z_prev);
1923: }
1924: }
1925: }
1926: PetscCall(DMStagVecRestoreArray(dm_coefficients, coeff_local, &arr_coefficients));
1927: } else SETERRQ(PetscObjectComm((PetscObject)dm_coefficients), PETSC_ERR_SUP, "Unsupported dimension %" PetscInt_FMT, dim);
1928: PetscCall(DMStagRestoreProductCoordinateArraysRead(dm_coefficients, &arr_coordinates_x, &arr_coordinates_y, &arr_coordinates_z));
1929: PetscCall(DMLocalToGlobal(dm_coefficients, coeff_local, INSERT_VALUES, coeff));
1930: PetscCall(DMRestoreLocalVector(dm_coefficients, &coeff_local));
1931: PetscFunctionReturn(PETSC_SUCCESS);
1932: }
1934: static PetscErrorCode CreateAuxiliaryOperator(Ctx ctx, PetscInt level, Mat *p_S_hat)
1935: {
1936: DM dm_element;
1937: Mat S_hat;
1938: DM dm_stokes, dm_coefficients;
1939: Vec coeff;
1941: PetscFunctionBeginUser;
1942: dm_stokes = ctx->levels[level]->dm_stokes;
1943: dm_coefficients = ctx->levels[level]->dm_coefficients;
1944: coeff = ctx->levels[level]->coeff;
1945: if (ctx->dim == 2) {
1946: PetscCall(DMStagCreateCompatibleDMStag(dm_stokes, 0, 0, 1, 0, &dm_element));
1947: } else if (ctx->dim == 3) {
1948: PetscCall(DMStagCreateCompatibleDMStag(dm_stokes, 0, 0, 0, 1, &dm_element));
1949: } else SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "Not implemented for dimension %" PetscInt_FMT, ctx->dim);
1950: PetscCall(DMCreateMatrix(dm_element, p_S_hat));
1951: S_hat = *p_S_hat;
1952: PetscCall(OperatorInsertInverseViscosityPressureTerms(dm_element, dm_coefficients, coeff, 1.0, S_hat));
1953: PetscCall(MatAssemblyBegin(S_hat, MAT_FINAL_ASSEMBLY));
1954: PetscCall(MatAssemblyEnd(S_hat, MAT_FINAL_ASSEMBLY));
1955: PetscCall(DMDestroy(&dm_element));
1956: PetscFunctionReturn(PETSC_SUCCESS);
1957: }
1959: static PetscErrorCode OperatorInsertInverseViscosityPressureTerms(DM dm, DM dm_coefficients, Vec coefficients, PetscScalar scale, Mat mat)
1960: {
1961: PetscInt dim, ex, ey, ez, startx, starty, startz, nx, ny, nz;
1962: Vec coeff_local;
1964: PetscFunctionBeginUser;
1965: PetscCall(DMGetDimension(dm, &dim));
1966: PetscCall(DMGetLocalVector(dm_coefficients, &coeff_local));
1967: PetscCall(DMGlobalToLocal(dm_coefficients, coefficients, INSERT_VALUES, coeff_local));
1968: PetscCall(DMStagGetCorners(dm, &startx, &starty, &startz, &nx, &ny, &nz, NULL, NULL, NULL));
1969: if (dim == 2) { /* Trick to have one loop nest */
1970: startz = 0;
1971: nz = 1;
1972: }
1973: for (ez = startz; ez < startz + nz; ++ez) {
1974: for (ey = starty; ey < starty + ny; ++ey) {
1975: for (ex = startx; ex < startx + nx; ++ex) {
1976: DMStagStencil from, to;
1977: PetscScalar val;
1979: /* component 0 on element is viscosity */
1980: from.i = ex;
1981: from.j = ey;
1982: from.k = ez;
1983: from.c = 0;
1984: from.loc = DMSTAG_ELEMENT;
1985: PetscCall(DMStagVecGetValuesStencil(dm_coefficients, coeff_local, 1, &from, &val));
1986: val = scale / val; /* inverse viscosity, scaled */
1987: to = from;
1988: PetscCall(DMStagMatSetValuesStencil(dm, mat, 1, &to, 1, &to, &val, INSERT_VALUES));
1989: }
1990: }
1991: }
1992: PetscCall(DMRestoreLocalVector(dm_coefficients, &coeff_local));
1993: /* Note that this function does not call MatAssembly{Begin,End} */
1994: PetscFunctionReturn(PETSC_SUCCESS);
1995: }
1997: /* Create a pressure-only DMStag and use it to generate a nullspace vector
1998: - Create a compatible DMStag with one dof per element (and nothing else).
1999: - Create a constant vector and normalize it
2000: - Migrate it to a vector on the original dmSol (making use of the fact
2001: that this will fill in zeros for "extra" dof)
2002: - Set the nullspace for the operator
2003: - Destroy everything (the operator keeps the references it needs) */
2004: static PetscErrorCode AttachNullspace(DM dmSol, Mat A)
2005: {
2006: DM dmPressure;
2007: Vec constantPressure, basis;
2008: PetscReal nrm;
2009: MatNullSpace matNullSpace;
2011: PetscFunctionBeginUser;
2012: PetscCall(DMStagCreateCompatibleDMStag(dmSol, 0, 0, 1, 0, &dmPressure));
2013: PetscCall(DMGetGlobalVector(dmPressure, &constantPressure));
2014: PetscCall(VecSet(constantPressure, 1.0));
2015: PetscCall(VecNorm(constantPressure, NORM_2, &nrm));
2016: PetscCall(VecScale(constantPressure, 1.0 / nrm));
2017: PetscCall(DMCreateGlobalVector(dmSol, &basis));
2018: PetscCall(DMStagMigrateVec(dmPressure, constantPressure, dmSol, basis));
2019: PetscCall(MatNullSpaceCreate(PetscObjectComm((PetscObject)dmSol), PETSC_FALSE, 1, &basis, &matNullSpace));
2020: PetscCall(VecDestroy(&basis));
2021: PetscCall(MatSetNullSpace(A, matNullSpace));
2022: PetscCall(MatNullSpaceDestroy(&matNullSpace));
2023: PetscCall(DMRestoreGlobalVector(dmPressure, &constantPressure));
2024: PetscCall(DMDestroy(&dmPressure));
2025: PetscFunctionReturn(PETSC_SUCCESS);
2026: }
2028: static PetscErrorCode DumpSolution(Ctx ctx, PetscInt level, Vec x)
2029: {
2030: DM dm_stokes, dm_coefficients;
2031: Vec coeff;
2032: DM dm_vel_avg;
2033: Vec vel_avg;
2034: DM da_vel_avg, da_p, da_eta_element;
2035: Vec vec_vel_avg, vec_p, vec_eta_element;
2036: DM da_eta_down_left, da_rho_down_left, da_eta_back_left, da_rho_back_left, da_eta_back_down, da_rho_back_down;
2037: Vec vec_eta_down_left, vec_rho_down_left, vec_eta_back_left, vec_rho_back_left, vec_eta_back_down, vec_rho_back_down;
2038: PetscInt ex, ey, ez, startx, starty, startz, nx, ny, nz;
2039: Vec stokesLocal;
2041: PetscFunctionBeginUser;
2042: dm_stokes = ctx->levels[level]->dm_stokes;
2043: dm_coefficients = ctx->levels[level]->dm_coefficients;
2044: coeff = ctx->levels[level]->coeff;
2046: /* For convenience, create a new DM and Vec which will hold averaged velocities
2047: Note that this could also be accomplished with direct array access, using
2048: DMStagVecGetArray() and related functions */
2049: if (ctx->dim == 2) {
2050: PetscCall(DMStagCreateCompatibleDMStag(dm_stokes, 0, 0, 2, 0, &dm_vel_avg)); /* 2 dof per element */
2051: } else if (ctx->dim == 3) {
2052: PetscCall(DMStagCreateCompatibleDMStag(dm_stokes, 0, 0, 0, 3, &dm_vel_avg)); /* 3 dof per element */
2053: } else SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP, "Not Implemented for dimension %" PetscInt_FMT, ctx->dim);
2054: PetscCall(DMSetUp(dm_vel_avg));
2055: PetscCall(DMStagSetUniformCoordinatesProduct(dm_vel_avg, ctx->xmin, ctx->xmax, ctx->ymin, ctx->ymax, ctx->zmin, ctx->zmax));
2056: PetscCall(DMCreateGlobalVector(dm_vel_avg, &vel_avg));
2057: PetscCall(DMGetLocalVector(dm_stokes, &stokesLocal));
2058: PetscCall(DMGlobalToLocal(dm_stokes, x, INSERT_VALUES, stokesLocal));
2059: PetscCall(DMStagGetCorners(dm_vel_avg, &startx, &starty, &startz, &nx, &ny, &nz, NULL, NULL, NULL));
2060: if (ctx->dim == 2) {
2061: for (ey = starty; ey < starty + ny; ++ey) {
2062: for (ex = startx; ex < startx + nx; ++ex) {
2063: DMStagStencil from[4], to[2];
2064: PetscScalar valFrom[4], valTo[2];
2066: from[0].i = ex;
2067: from[0].j = ey;
2068: from[0].loc = DMSTAG_UP;
2069: from[0].c = 0;
2070: from[1].i = ex;
2071: from[1].j = ey;
2072: from[1].loc = DMSTAG_DOWN;
2073: from[1].c = 0;
2074: from[2].i = ex;
2075: from[2].j = ey;
2076: from[2].loc = DMSTAG_LEFT;
2077: from[2].c = 0;
2078: from[3].i = ex;
2079: from[3].j = ey;
2080: from[3].loc = DMSTAG_RIGHT;
2081: from[3].c = 0;
2082: PetscCall(DMStagVecGetValuesStencil(dm_stokes, stokesLocal, 4, from, valFrom));
2083: to[0].i = ex;
2084: to[0].j = ey;
2085: to[0].loc = DMSTAG_ELEMENT;
2086: to[0].c = 0;
2087: valTo[0] = 0.5 * (valFrom[2] + valFrom[3]);
2088: to[1].i = ex;
2089: to[1].j = ey;
2090: to[1].loc = DMSTAG_ELEMENT;
2091: to[1].c = 1;
2092: valTo[1] = 0.5 * (valFrom[0] + valFrom[1]);
2093: PetscCall(DMStagVecSetValuesStencil(dm_vel_avg, vel_avg, 2, to, valTo, INSERT_VALUES));
2094: }
2095: }
2096: } else if (ctx->dim == 3) {
2097: for (ez = startz; ez < startz + nz; ++ez) {
2098: for (ey = starty; ey < starty + ny; ++ey) {
2099: for (ex = startx; ex < startx + nx; ++ex) {
2100: DMStagStencil from[6], to[3];
2101: PetscScalar valFrom[6], valTo[3];
2103: from[0].i = ex;
2104: from[0].j = ey;
2105: from[0].k = ez;
2106: from[0].loc = DMSTAG_UP;
2107: from[0].c = 0;
2108: from[1].i = ex;
2109: from[1].j = ey;
2110: from[1].k = ez;
2111: from[1].loc = DMSTAG_DOWN;
2112: from[1].c = 0;
2113: from[2].i = ex;
2114: from[2].j = ey;
2115: from[2].k = ez;
2116: from[2].loc = DMSTAG_LEFT;
2117: from[2].c = 0;
2118: from[3].i = ex;
2119: from[3].j = ey;
2120: from[3].k = ez;
2121: from[3].loc = DMSTAG_RIGHT;
2122: from[3].c = 0;
2123: from[4].i = ex;
2124: from[4].j = ey;
2125: from[4].k = ez;
2126: from[4].loc = DMSTAG_BACK;
2127: from[4].c = 0;
2128: from[5].i = ex;
2129: from[5].j = ey;
2130: from[5].k = ez;
2131: from[5].loc = DMSTAG_FRONT;
2132: from[5].c = 0;
2133: PetscCall(DMStagVecGetValuesStencil(dm_stokes, stokesLocal, 6, from, valFrom));
2134: to[0].i = ex;
2135: to[0].j = ey;
2136: to[0].k = ez;
2137: to[0].loc = DMSTAG_ELEMENT;
2138: to[0].c = 0;
2139: valTo[0] = 0.5 * (valFrom[2] + valFrom[3]);
2140: to[1].i = ex;
2141: to[1].j = ey;
2142: to[1].k = ez;
2143: to[1].loc = DMSTAG_ELEMENT;
2144: to[1].c = 1;
2145: valTo[1] = 0.5 * (valFrom[0] + valFrom[1]);
2146: to[2].i = ex;
2147: to[2].j = ey;
2148: to[2].k = ez;
2149: to[2].loc = DMSTAG_ELEMENT;
2150: to[2].c = 2;
2151: valTo[2] = 0.5 * (valFrom[4] + valFrom[5]);
2152: PetscCall(DMStagVecSetValuesStencil(dm_vel_avg, vel_avg, 3, to, valTo, INSERT_VALUES));
2153: }
2154: }
2155: }
2156: }
2157: PetscCall(VecAssemblyBegin(vel_avg));
2158: PetscCall(VecAssemblyEnd(vel_avg));
2159: PetscCall(DMRestoreLocalVector(dm_stokes, &stokesLocal));
2161: /* Create individual DMDAs for sub-grids of our DMStag objects. This is
2162: somewhat inefficient, but allows use of the DMDA API without re-implementing
2163: all utilities for DMStag */
2165: PetscCall(DMStagVecSplitToDMDA(dm_stokes, x, DMSTAG_ELEMENT, 0, &da_p, &vec_p));
2166: PetscCall(PetscObjectSetName((PetscObject)vec_p, "p (scaled)"));
2168: PetscCall(DMStagVecSplitToDMDA(dm_coefficients, coeff, DMSTAG_ELEMENT, 0, &da_eta_element, &vec_eta_element));
2169: PetscCall(PetscObjectSetName((PetscObject)vec_eta_element, "eta"));
2171: PetscCall(DMStagVecSplitToDMDA(dm_coefficients, coeff, DMSTAG_DOWN_LEFT, 0, &da_eta_down_left, &vec_eta_down_left));
2172: PetscCall(PetscObjectSetName((PetscObject)vec_eta_down_left, "eta"));
2174: PetscCall(DMStagVecSplitToDMDA(dm_coefficients, coeff, DMSTAG_DOWN_LEFT, 1, &da_rho_down_left, &vec_rho_down_left));
2175: PetscCall(PetscObjectSetName((PetscObject)vec_rho_down_left, "density"));
2177: if (ctx->dim == 3) {
2178: PetscCall(DMStagVecSplitToDMDA(dm_coefficients, coeff, DMSTAG_BACK_LEFT, 0, &da_eta_back_left, &vec_eta_back_left));
2179: PetscCall(PetscObjectSetName((PetscObject)vec_eta_back_left, "eta"));
2181: PetscCall(DMStagVecSplitToDMDA(dm_coefficients, coeff, DMSTAG_BACK_LEFT, 1, &da_rho_back_left, &vec_rho_back_left));
2182: PetscCall(PetscObjectSetName((PetscObject)vec_rho_back_left, "rho"));
2184: PetscCall(DMStagVecSplitToDMDA(dm_coefficients, coeff, DMSTAG_BACK_DOWN, 0, &da_eta_back_down, &vec_eta_back_down));
2185: PetscCall(PetscObjectSetName((PetscObject)vec_eta_back_down, "eta"));
2187: PetscCall(DMStagVecSplitToDMDA(dm_coefficients, coeff, DMSTAG_BACK_DOWN, 1, &da_rho_back_down, &vec_rho_back_down));
2188: PetscCall(PetscObjectSetName((PetscObject)vec_rho_back_down, "rho"));
2189: }
2191: PetscCall(DMStagVecSplitToDMDA(dm_vel_avg, vel_avg, DMSTAG_ELEMENT, -3, &da_vel_avg, &vec_vel_avg)); /* note -3 : pad with zero */
2192: PetscCall(PetscObjectSetName((PetscObject)vec_vel_avg, "Velocity (Averaged)"));
2194: /* Dump element-based fields to a .vtr file */
2195: {
2196: PetscViewer viewer;
2198: PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)da_vel_avg), "ex4_element.vtr", FILE_MODE_WRITE, &viewer));
2199: PetscCall(VecView(vec_vel_avg, viewer));
2200: PetscCall(VecView(vec_p, viewer));
2201: PetscCall(VecView(vec_eta_element, viewer));
2202: PetscCall(PetscViewerDestroy(&viewer));
2203: }
2205: /* Dump vertex- or edge-based fields to a second .vtr file */
2206: {
2207: PetscViewer viewer;
2209: PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)da_eta_down_left), "ex4_down_left.vtr", FILE_MODE_WRITE, &viewer));
2210: PetscCall(VecView(vec_eta_down_left, viewer));
2211: PetscCall(VecView(vec_rho_down_left, viewer));
2212: PetscCall(PetscViewerDestroy(&viewer));
2213: }
2214: if (ctx->dim == 3) {
2215: PetscViewer viewer;
2217: PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)da_eta_back_left), "ex4_back_left.vtr", FILE_MODE_WRITE, &viewer));
2218: PetscCall(VecView(vec_eta_back_left, viewer));
2219: PetscCall(VecView(vec_rho_back_left, viewer));
2220: PetscCall(PetscViewerDestroy(&viewer));
2221: }
2222: if (ctx->dim == 3) {
2223: PetscViewer viewer;
2225: PetscCall(PetscViewerVTKOpen(PetscObjectComm((PetscObject)da_eta_back_down), "ex4_back_down.vtr", FILE_MODE_WRITE, &viewer));
2226: PetscCall(VecView(vec_eta_back_down, viewer));
2227: PetscCall(VecView(vec_rho_back_down, viewer));
2228: PetscCall(PetscViewerDestroy(&viewer));
2229: }
2231: /* Destroy DMDAs and Vecs */
2232: PetscCall(VecDestroy(&vec_vel_avg));
2233: PetscCall(VecDestroy(&vec_p));
2234: PetscCall(VecDestroy(&vec_eta_element));
2235: PetscCall(VecDestroy(&vec_eta_down_left));
2236: if (ctx->dim == 3) {
2237: PetscCall(VecDestroy(&vec_eta_back_left));
2238: PetscCall(VecDestroy(&vec_eta_back_down));
2239: }
2240: PetscCall(VecDestroy(&vec_rho_down_left));
2241: if (ctx->dim == 3) {
2242: PetscCall(VecDestroy(&vec_rho_back_left));
2243: PetscCall(VecDestroy(&vec_rho_back_down));
2244: }
2245: PetscCall(DMDestroy(&da_vel_avg));
2246: PetscCall(DMDestroy(&da_p));
2247: PetscCall(DMDestroy(&da_eta_element));
2248: PetscCall(DMDestroy(&da_eta_down_left));
2249: if (ctx->dim == 3) {
2250: PetscCall(DMDestroy(&da_eta_back_left));
2251: PetscCall(DMDestroy(&da_eta_back_down));
2252: }
2253: PetscCall(DMDestroy(&da_rho_down_left));
2254: if (ctx->dim == 3) {
2255: PetscCall(DMDestroy(&da_rho_back_left));
2256: PetscCall(DMDestroy(&da_rho_back_down));
2257: }
2258: PetscCall(VecDestroy(&vel_avg));
2259: PetscCall(DMDestroy(&dm_vel_avg));
2260: PetscFunctionReturn(PETSC_SUCCESS);
2261: }
2263: /*TEST
2265: test:
2266: suffix: direct_umfpack
2267: requires: suitesparse !complex
2268: nsize: 1
2269: args: -dim 2 -coefficients layers -nondimensional 0 -stag_grid_x 12 -stag_grid_y 7 -pc_type lu -pc_factor_mat_solver_type umfpack -ksp_converged_reason
2271: test:
2272: suffix: direct_mumps
2273: requires: mumps !complex !single
2274: nsize: 9
2275: args: -dim 2 -coefficients layers -nondimensional 0 -stag_grid_x 13 -stag_grid_y 8 -pc_type lu -pc_factor_mat_solver_type mumps -ksp_converged_reason
2277: test:
2278: suffix: isovisc_nondim_abf_mg
2279: nsize: 1
2280: args: -dim 2 -coefficients layers -nondimensional 1 -pc_type fieldsplit -pc_fieldsplit_type schur -ksp_converged_reason -fieldsplit_element_ksp_type preonly -pc_fieldsplit_detect_saddle_point false -fieldsplit_face_pc_type mg -fieldsplit_face_pc_mg_levels 3 -stag_grid_x 24 -stag_grid_y 24 -fieldsplit_face_pc_mg_galerkin -fieldsplit_face_ksp_converged_reason -ksp_type fgmres -fieldsplit_element_pc_type none -fieldsplit_face_mg_levels_ksp_max_it 6 -pc_fieldsplit_schur_fact_type upper -isoviscous
2282: test:
2283: suffix: isovisc_nondim_abf_mg_2
2284: nsize: 1
2285: args: -dim 2 -coefficients layers -nondimensional -isoviscous -eta1 1.0 -stag_grid_x 32 -stag_grid_y 32 -ksp_type fgmres -pc_type fieldsplit -pc_fieldsplit_type schur -pc_fieldsplit_schur_fact_type upper -build_auxiliary_operator -fieldsplit_element_ksp_type preonly -fieldsplit_element_pc_type jacobi -fieldsplit_face_pc_type mg -fieldsplit_face_pc_mg_levels 3 -fieldsplit_face_pc_mg_galerkin -fieldsplit_face_mg_levels_pc_type jacobi -fieldsplit_face_mg_levels_ksp_type chebyshev -ksp_converged_reason
2287: test:
2288: suffix: nondim_abf_mg
2289: requires: suitesparse !complex
2290: nsize: 4
2291: args: -dim 2 -coefficients layers -pc_type fieldsplit -pc_fieldsplit_type schur -ksp_converged_reason -fieldsplit_element_ksp_type preonly -pc_fieldsplit_detect_saddle_point false -fieldsplit_face_pc_type mg -fieldsplit_face_pc_mg_levels 3 -fieldsplit_face_pc_mg_galerkin -fieldsplit_face_mg_coarse_pc_type redundant -fieldsplit_face_mg_coarse_redundant_pc_type lu -fieldsplit_face_mg_coarse_redundant_pc_factor_mat_solver_type umfpack -ksp_type fgmres -fieldsplit_element_pc_type none -fieldsplit_face_mg_levels_ksp_max_it 6 -pc_fieldsplit_schur_fact_type upper -nondimensional -eta1 1e-2 -eta2 1.0 -ksp_monitor -fieldsplit_face_mg_levels_pc_type jacobi -fieldsplit_face_mg_levels_ksp_type gmres -fieldsplit_element_pc_type jacobi -pc_fieldsplit_schur_precondition selfp -stag_grid_x 32 -stag_grid_y 32 -fieldsplit_face_ksp_monitor
2293: test:
2294: suffix: nondim_abf_lu
2295: requires: suitesparse !complex
2296: nsize: 1
2297: args: -dim 2 -coefficients layers -pc_type fieldsplit -pc_fieldsplit_type schur -ksp_converged_reason -fieldsplit_element_ksp_type preonly -pc_fieldsplit_detect_saddle_point false -ksp_type fgmres -fieldsplit_element_pc_type none -pc_fieldsplit_schur_fact_type upper -nondimensional -eta1 1e-2 -eta2 1.0 -isoviscous 0 -ksp_monitor -fieldsplit_element_pc_type jacobi -build_auxiliary_operator -fieldsplit_face_pc_type lu -fieldsplit_face_pc_factor_mat_solver_type umfpack -stag_grid_x 32 -stag_grid_y 32
2299: test:
2300: suffix: 3d_nondim_isovisc_abf_mg
2301: requires: !single
2302: nsize: 1
2303: args: -dim 3 -coefficients layers -isoviscous -nondimensional -build_auxiliary_operator -pc_type fieldsplit -pc_fieldsplit_type schur -ksp_converged_reason -fieldsplit_element_ksp_type preonly -pc_fieldsplit_detect_saddle_point false -fieldsplit_face_pc_type mg -fieldsplit_face_pc_mg_levels 3 -s 16 -fieldsplit_face_pc_mg_galerkin -fieldsplit_face_ksp_converged_reason -ksp_type fgmres -fieldsplit_element_pc_type none -fieldsplit_face_mg_levels_ksp_max_it 6 -pc_fieldsplit_schur_fact_type upper
2305: test:
2306: TODO: unstable across systems
2307: suffix: monolithic
2308: nsize: 1
2309: requires: double !complex
2310: args: -dim {{2 3}separate output} -s 16 -custom_pc_mat -pc_type mg -pc_mg_levels 3 -pc_mg_galerkin -mg_levels_ksp_type gmres -mg_levels_ksp_norm_type unpreconditioned -mg_levels_ksp_max_it 10 -mg_levels_pc_type jacobi -ksp_converged_reason
2312: test:
2313: suffix: 3d_nondim_isovisc_sinker_abf_mg
2314: requires: !complex !single
2315: nsize: 1
2316: args: -dim 3 -coefficients sinker -isoviscous -nondimensional -pc_type fieldsplit -pc_fieldsplit_type schur -ksp_converged_reason -fieldsplit_element_ksp_type preonly -pc_fieldsplit_detect_saddle_point false -fieldsplit_face_pc_type mg -fieldsplit_face_pc_mg_levels 3 -s 16 -fieldsplit_face_pc_mg_galerkin -fieldsplit_face_ksp_converged_reason -ksp_type fgmres -fieldsplit_element_pc_type none -fieldsplit_face_mg_levels_ksp_max_it 6 -pc_fieldsplit_schur_fact_type upper
2318: test:
2319: TODO: unstable across systems
2320: suffix: 3d_nondim_mono_mg_lamemstyle
2321: nsize: 1
2322: requires: suitesparse
2323: args: -dim 3 -coefficients layers -nondimensional -s 16 -custom_pc_mat -pc_type mg -pc_mg_galerkin -pc_mg_levels 2 -mg_levels_ksp_type richardson -mg_levels_pc_type jacobi -mg_levels_ksp_richardson_scale 0.5 -mg_levels_ksp_max_it 20 -mg_coarse_pc_type lu -mg_coarse_pc_factor_mat_solver_type umfpack -ksp_converged_reason
2325: test:
2326: suffix: 3d_isovisc_blob_cuda
2327: requires: cuda defined(PETSC_USE_LOG)
2328: nsize: 2
2329: args: -dim 3 -coefficients blob -isoviscous -s 8 -build_auxiliary_operator -fieldsplit_element_ksp_type preonly -fieldsplit_element_pc_type jacobi -fieldsplit_face_ksp_type fgmres -fieldsplit_face_mg_levels_ksp_max_it 6 -fieldsplit_face_mg_levels_ksp_norm_type none -fieldsplit_face_mg_levels_ksp_type chebyshev -fieldsplit_face_mg_levels_pc_type jacobi -fieldsplit_face_pc_mg_galerkin -fieldsplit_face_pc_mg_levels 3 -fieldsplit_face_pc_type mg -pc_fieldsplit_schur_fact_type upper -pc_fieldsplit_schur_precondition user -pc_fieldsplit_type schur -pc_type fieldsplit -dm_mat_type aijcusparse -dm_vec_type cuda -log_view -fieldsplit_face_pc_mg_log
2330: filter: awk "/MGInterp/ {print \$NF}"
2332: TEST*/