Actual source code: biharmonic2.c
1: static char help[] = "Solves biharmonic equation in 1d.\n";
3: /*
4: Solves the equation biharmonic equation in split form
6: w = -kappa \Delta u
7: u_t = \Delta w
8: -1 <= u <= 1
9: Periodic boundary conditions
11: Evolve the biharmonic heat equation with bounds: (same as biharmonic)
12: ---------------
13: ./biharmonic2 -ts_monitor -snes_monitor -ts_monitor_draw_solution -pc_type lu -draw_pause .1 -snes_converged_reason -ts_type beuler -da_refine 5 -draw_fields 1 -ts_time_step 9.53674e-9
15: w = -kappa \Delta u + u^3 - u
16: u_t = \Delta w
17: -1 <= u <= 1
18: Periodic boundary conditions
20: Evolve the Cahn-Hillard equations: (this fails after a few timesteps 12/17/2017)
21: ---------------
22: ./biharmonic2 -ts_monitor -snes_monitor -ts_monitor_draw_solution -pc_type lu -draw_pause .1 -snes_converged_reason -ts_type beuler -da_refine 6 -draw_fields 1 -kappa .00001 -ts_time_step 5.96046e-06 -cahn-hillard
24: */
25: #include <petscdm.h>
26: #include <petscdmda.h>
27: #include <petscts.h>
29: /*
30: User-defined routines
31: */
32: extern PetscErrorCode FormFunction(TS, PetscReal, Vec, Vec, Vec, void *), FormInitialSolution(DM, Vec, PetscReal);
33: typedef struct {
34: PetscBool cahnhillard;
35: PetscReal kappa;
36: PetscInt energy;
37: PetscReal tol;
38: PetscReal theta;
39: PetscReal theta_c;
40: } UserCtx;
42: int main(int argc, char **argv)
43: {
44: TS ts; /* nonlinear solver */
45: Vec x, r; /* solution, residual vectors */
46: Mat J; /* Jacobian matrix */
47: PetscInt steps, Mx;
48: DM da;
49: MatFDColoring matfdcoloring;
50: ISColoring iscoloring;
51: PetscReal dt;
52: PetscReal vbounds[] = {-100000, 100000, -1.1, 1.1};
53: SNES snes;
54: UserCtx ctx;
56: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
57: Initialize program
58: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
59: PetscFunctionBeginUser;
60: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
61: ctx.kappa = 1.0;
62: PetscCall(PetscOptionsGetReal(NULL, NULL, "-kappa", &ctx.kappa, NULL));
63: ctx.cahnhillard = PETSC_FALSE;
65: PetscCall(PetscOptionsGetBool(NULL, NULL, "-cahn-hillard", &ctx.cahnhillard, NULL));
66: PetscCall(PetscViewerDrawSetBounds(PETSC_VIEWER_DRAW_(PETSC_COMM_WORLD), 2, vbounds));
67: PetscCall(PetscViewerDrawResize(PETSC_VIEWER_DRAW_(PETSC_COMM_WORLD), 600, 600));
68: ctx.energy = 1;
69: /*PetscCall(PetscOptionsGetInt(NULL,NULL,"-energy",&ctx.energy,NULL));*/
70: PetscCall(PetscOptionsGetInt(NULL, NULL, "-energy", &ctx.energy, NULL));
71: ctx.tol = 1.0e-8;
72: PetscCall(PetscOptionsGetReal(NULL, NULL, "-tol", &ctx.tol, NULL));
73: ctx.theta = .001;
74: ctx.theta_c = 1.0;
75: PetscCall(PetscOptionsGetReal(NULL, NULL, "-theta", &ctx.theta, NULL));
76: PetscCall(PetscOptionsGetReal(NULL, NULL, "-theta_c", &ctx.theta_c, NULL));
78: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
79: Create distributed array (DMDA) to manage parallel grid and vectors
80: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
81: PetscCall(DMDACreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_PERIODIC, 10, 2, 2, NULL, &da));
82: PetscCall(DMSetFromOptions(da));
83: PetscCall(DMSetUp(da));
84: PetscCall(DMDASetFieldName(da, 0, "Biharmonic heat equation: w = -kappa*u_xx"));
85: PetscCall(DMDASetFieldName(da, 1, "Biharmonic heat equation: u"));
86: PetscCall(DMDAGetInfo(da, NULL, &Mx, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL));
87: dt = 1.0 / (10. * ctx.kappa * Mx * Mx * Mx * Mx);
89: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
90: Extract global vectors from DMDA; then duplicate for remaining
91: vectors that are the same types
92: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
93: PetscCall(DMCreateGlobalVector(da, &x));
94: PetscCall(VecDuplicate(x, &r));
96: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
97: Create timestepping solver context
98: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99: PetscCall(TSCreate(PETSC_COMM_WORLD, &ts));
100: PetscCall(TSSetDM(ts, da));
101: PetscCall(TSSetProblemType(ts, TS_NONLINEAR));
102: PetscCall(TSSetIFunction(ts, NULL, FormFunction, &ctx));
103: PetscCall(TSSetMaxTime(ts, .02));
104: PetscCall(TSSetExactFinalTime(ts, TS_EXACTFINALTIME_INTERPOLATE));
106: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
107: Create matrix data structure; set Jacobian evaluation routine
109: < Set Jacobian matrix data structure and default Jacobian evaluation
110: routine. User can override with:
111: -snes_mf : matrix-free Newton-Krylov method with no preconditioning
112: (unless user explicitly sets preconditioner)
113: -snes_mf_operator : form matrix used to construct the preconditioner as set by the user,
114: but use matrix-free approx for Jacobian-vector
115: products within Newton-Krylov method
117: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
118: PetscCall(TSGetSNES(ts, &snes));
119: PetscCall(DMCreateColoring(da, IS_COLORING_GLOBAL, &iscoloring));
120: PetscCall(DMSetMatType(da, MATAIJ));
121: PetscCall(DMCreateMatrix(da, &J));
122: PetscCall(MatFDColoringCreate(J, iscoloring, &matfdcoloring));
123: PetscCall(MatFDColoringSetFunction(matfdcoloring, (MatFDColoringFn *)SNESTSFormFunction, ts));
124: PetscCall(MatFDColoringSetFromOptions(matfdcoloring));
125: PetscCall(MatFDColoringSetUp(J, iscoloring, matfdcoloring));
126: PetscCall(ISColoringDestroy(&iscoloring));
127: PetscCall(SNESSetJacobian(snes, J, J, SNESComputeJacobianDefaultColor, matfdcoloring));
129: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
130: Customize nonlinear solver
131: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
132: PetscCall(TSSetType(ts, TSBEULER));
134: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
135: Set initial conditions
136: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
137: PetscCall(FormInitialSolution(da, x, ctx.kappa));
138: PetscCall(TSSetTimeStep(ts, dt));
139: PetscCall(TSSetSolution(ts, x));
141: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
142: Set runtime options
143: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
144: PetscCall(TSSetFromOptions(ts));
146: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
147: Solve nonlinear system
148: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
149: PetscCall(TSSolve(ts, x));
150: PetscCall(TSGetStepNumber(ts, &steps));
152: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
153: Free work space. All PETSc objects should be destroyed when they
154: are no longer needed.
155: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
156: PetscCall(MatDestroy(&J));
157: PetscCall(MatFDColoringDestroy(&matfdcoloring));
158: PetscCall(VecDestroy(&x));
159: PetscCall(VecDestroy(&r));
160: PetscCall(TSDestroy(&ts));
161: PetscCall(DMDestroy(&da));
163: PetscCall(PetscFinalize());
164: return 0;
165: }
167: typedef struct {
168: PetscScalar w, u;
169: } Field;
170: /* ------------------------------------------------------------------- */
171: /*
172: FormFunction - Evaluates nonlinear function, F(x).
174: Input Parameters:
175: . ts - the TS context
176: . X - input vector
177: . ptr - optional user-defined context, as set by SNESSetFunction()
179: Output Parameter:
180: . F - function vector
181: */
182: PetscErrorCode FormFunction(TS ts, PetscReal ftime, Vec X, Vec Xdot, Vec F, void *ptr)
183: {
184: DM da;
185: PetscInt i, Mx, xs, xm;
186: PetscReal hx, sx;
187: Field *x, *xdot, *f;
188: Vec localX, localXdot;
189: UserCtx *ctx = (UserCtx *)ptr;
191: PetscFunctionBegin;
192: PetscCall(TSGetDM(ts, &da));
193: PetscCall(DMGetLocalVector(da, &localX));
194: PetscCall(DMGetLocalVector(da, &localXdot));
195: PetscCall(DMDAGetInfo(da, PETSC_IGNORE, &Mx, PETSC_IGNORE, PETSC_IGNORE, PETSC_IGNORE, PETSC_IGNORE, PETSC_IGNORE, PETSC_IGNORE, PETSC_IGNORE, PETSC_IGNORE, PETSC_IGNORE, PETSC_IGNORE, PETSC_IGNORE));
197: hx = 1.0 / (PetscReal)Mx;
198: sx = 1.0 / (hx * hx);
200: /*
201: Scatter ghost points to local vector,using the 2-step process
202: DMGlobalToLocalBegin(),DMGlobalToLocalEnd().
203: By placing code between these two statements, computations can be
204: done while messages are in transition.
205: */
206: PetscCall(DMGlobalToLocalBegin(da, X, INSERT_VALUES, localX));
207: PetscCall(DMGlobalToLocalEnd(da, X, INSERT_VALUES, localX));
208: PetscCall(DMGlobalToLocalBegin(da, Xdot, INSERT_VALUES, localXdot));
209: PetscCall(DMGlobalToLocalEnd(da, Xdot, INSERT_VALUES, localXdot));
211: /*
212: Get pointers to vector data
213: */
214: PetscCall(DMDAVecGetArrayRead(da, localX, &x));
215: PetscCall(DMDAVecGetArrayRead(da, localXdot, &xdot));
216: PetscCall(DMDAVecGetArray(da, F, &f));
218: /*
219: Get local grid boundaries
220: */
221: PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &xm, NULL, NULL));
223: /*
224: Compute function over the locally owned part of the grid
225: */
226: for (i = xs; i < xs + xm; i++) {
227: f[i].w = x[i].w + ctx->kappa * (x[i - 1].u + x[i + 1].u - 2.0 * x[i].u) * sx;
228: if (ctx->cahnhillard) {
229: switch (ctx->energy) {
230: case 1: /* double well */
231: f[i].w += -x[i].u * x[i].u * x[i].u + x[i].u;
232: break;
233: case 2: /* double obstacle */
234: f[i].w += x[i].u;
235: break;
236: case 3: /* logarithmic */
237: if (PetscRealPart(x[i].u) < -1.0 + 2.0 * ctx->tol) f[i].w += .5 * ctx->theta * (-PetscLogReal(ctx->tol) + PetscLogScalar((1.0 - x[i].u) / 2.0)) + ctx->theta_c * x[i].u;
238: else if (PetscRealPart(x[i].u) > 1.0 - 2.0 * ctx->tol) f[i].w += .5 * ctx->theta * (-PetscLogScalar((1.0 + x[i].u) / 2.0) + PetscLogReal(ctx->tol)) + ctx->theta_c * x[i].u;
239: else f[i].w += .5 * ctx->theta * (-PetscLogScalar((1.0 + x[i].u) / 2.0) + PetscLogScalar((1.0 - x[i].u) / 2.0)) + ctx->theta_c * x[i].u;
240: break;
241: }
242: }
243: f[i].u = xdot[i].u - (x[i - 1].w + x[i + 1].w - 2.0 * x[i].w) * sx;
244: }
246: /*
247: Restore vectors
248: */
249: PetscCall(DMDAVecRestoreArrayRead(da, localXdot, &xdot));
250: PetscCall(DMDAVecRestoreArrayRead(da, localX, &x));
251: PetscCall(DMDAVecRestoreArray(da, F, &f));
252: PetscCall(DMRestoreLocalVector(da, &localX));
253: PetscCall(DMRestoreLocalVector(da, &localXdot));
254: PetscFunctionReturn(PETSC_SUCCESS);
255: }
257: /* ------------------------------------------------------------------- */
258: PetscErrorCode FormInitialSolution(DM da, Vec X, PetscReal kappa)
259: {
260: PetscInt i, xs, xm, Mx, xgs, xgm;
261: Field *x;
262: PetscReal hx, xx, r, sx;
263: Vec Xg;
265: PetscFunctionBegin;
266: PetscCall(DMDAGetInfo(da, PETSC_IGNORE, &Mx, PETSC_IGNORE, PETSC_IGNORE, PETSC_IGNORE, PETSC_IGNORE, PETSC_IGNORE, PETSC_IGNORE, PETSC_IGNORE, PETSC_IGNORE, PETSC_IGNORE, PETSC_IGNORE, PETSC_IGNORE));
268: hx = 1.0 / (PetscReal)Mx;
269: sx = 1.0 / (hx * hx);
271: /*
272: Get pointers to vector data
273: */
274: PetscCall(DMCreateLocalVector(da, &Xg));
275: PetscCall(DMDAVecGetArray(da, Xg, &x));
277: /*
278: Get local grid boundaries
279: */
280: PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &xm, NULL, NULL));
281: PetscCall(DMDAGetGhostCorners(da, &xgs, NULL, NULL, &xgm, NULL, NULL));
283: /*
284: Compute u function over the locally owned part of the grid including ghost points
285: */
286: for (i = xgs; i < xgs + xgm; i++) {
287: xx = i * hx;
288: r = PetscSqrtReal((xx - .5) * (xx - .5));
289: if (r < .125) x[i].u = 1.0;
290: else x[i].u = -.50;
291: /* fill in x[i].w so that valgrind doesn't detect use of uninitialized memory */
292: x[i].w = 0;
293: }
294: for (i = xs; i < xs + xm; i++) x[i].w = -kappa * (x[i - 1].u + x[i + 1].u - 2.0 * x[i].u) * sx;
296: /*
297: Restore vectors
298: */
299: PetscCall(DMDAVecRestoreArray(da, Xg, &x));
301: /* Grab only the global part of the vector */
302: PetscCall(VecSet(X, 0));
303: PetscCall(DMLocalToGlobalBegin(da, Xg, ADD_VALUES, X));
304: PetscCall(DMLocalToGlobalEnd(da, Xg, ADD_VALUES, X));
305: PetscCall(VecDestroy(&Xg));
306: PetscFunctionReturn(PETSC_SUCCESS);
307: }
309: /*TEST
311: build:
312: requires: !complex !single
314: test:
315: args: -ts_monitor -snes_monitor -pc_type lu -snes_converged_reason -ts_type beuler -da_refine 5 -ts_time_step 9.53674e-9 -ts_max_steps 50
316: requires: x
318: TEST*/