Actual source code: biharmonic3.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: ./biharmonic3 -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:
 21: ---------------
 22: ./biharmonic3 -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;
 64:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-cahn-hillard", &ctx.cahnhillard, NULL));
 65:   PetscCall(PetscViewerDrawSetBounds(PETSC_VIEWER_DRAW_(PETSC_COMM_WORLD), 2, vbounds));
 66:   PetscCall(PetscViewerDrawResize(PETSC_VIEWER_DRAW_(PETSC_COMM_WORLD), 600, 600));
 67:   ctx.energy = 1;
 68:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-energy", &ctx.energy, NULL));
 69:   ctx.tol = 1.0e-8;
 70:   PetscCall(PetscOptionsGetReal(NULL, NULL, "-tol", &ctx.tol, NULL));
 71:   ctx.theta   = .001;
 72:   ctx.theta_c = 1.0;
 73:   PetscCall(PetscOptionsGetReal(NULL, NULL, "-theta", &ctx.theta, NULL));
 74:   PetscCall(PetscOptionsGetReal(NULL, NULL, "-theta_c", &ctx.theta_c, NULL));

 76:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 77:      Create distributed array (DMDA) to manage parallel grid and vectors
 78:   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 79:   PetscCall(DMDACreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_PERIODIC, 10, 2, 2, NULL, &da));
 80:   PetscCall(DMSetFromOptions(da));
 81:   PetscCall(DMSetUp(da));
 82:   PetscCall(DMDASetFieldName(da, 0, "Biharmonic heat equation: w = -kappa*u_xx"));
 83:   PetscCall(DMDASetFieldName(da, 1, "Biharmonic heat equation: u"));
 84:   PetscCall(DMDAGetInfo(da, NULL, &Mx, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL));
 85:   dt = 1.0 / (10. * ctx.kappa * Mx * Mx * Mx * Mx);

 87:   /*  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 88:      Extract global vectors from DMDA; then duplicate for remaining
 89:      vectors that are the same types
 90:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 91:   PetscCall(DMCreateGlobalVector(da, &x));
 92:   PetscCall(VecDuplicate(x, &r));

 94:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 95:      Create timestepping solver context
 96:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 97:   PetscCall(TSCreate(PETSC_COMM_WORLD, &ts));
 98:   PetscCall(TSSetDM(ts, da));
 99:   PetscCall(TSSetProblemType(ts, TS_NONLINEAR));
100:   PetscCall(TSSetIFunction(ts, NULL, FormFunction, &ctx));
101:   PetscCall(TSSetMaxTime(ts, .02));
102:   PetscCall(TSSetExactFinalTime(ts, TS_EXACTFINALTIME_STEPOVER));

104:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
105:      Create matrix data structure; set Jacobian evaluation routine

107: <     Set Jacobian matrix data structure and default Jacobian evaluation
108:      routine. User can override with:
109:      -snes_mf : matrix-free Newton-Krylov method with no preconditioning
110:                 (unless user explicitly sets preconditioner)
111:      -snes_mf_operator : form matrix used to construct the preconditioner as set by the user,
112:                          but use matrix-free approx for Jacobian-vector
113:                          products within Newton-Krylov method

115:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
116:   PetscCall(TSGetSNES(ts, &snes));
117:   PetscCall(SNESSetType(snes, SNESVINEWTONRSLS));
118:   PetscCall(DMCreateColoring(da, IS_COLORING_GLOBAL, &iscoloring));
119:   PetscCall(DMSetMatType(da, MATAIJ));
120:   PetscCall(DMCreateMatrix(da, &J));
121:   PetscCall(MatFDColoringCreate(J, iscoloring, &matfdcoloring));
122:   PetscCall(MatFDColoringSetFunction(matfdcoloring, (MatFDColoringFn *)SNESTSFormFunction, ts));
123:   PetscCall(MatFDColoringSetFromOptions(matfdcoloring));
124:   PetscCall(MatFDColoringSetUp(J, iscoloring, matfdcoloring));
125:   PetscCall(ISColoringDestroy(&iscoloring));
126:   PetscCall(SNESSetJacobian(snes, J, J, SNESComputeJacobianDefaultColor, matfdcoloring));

128:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
129:      Customize nonlinear solver
130:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
131:   PetscCall(TSSetType(ts, TSBEULER));

133:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
134:      Set initial conditions
135:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
136:   PetscCall(FormInitialSolution(da, x, ctx.kappa));
137:   PetscCall(TSSetTimeStep(ts, dt));
138:   PetscCall(TSSetSolution(ts, x));

140:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
141:      Set runtime options
142:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
143:   PetscCall(TSSetFromOptions(ts));

145:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
146:      Solve nonlinear system
147:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
148:   PetscCall(TSSolve(ts, x));
149:   PetscCall(TSGetStepNumber(ts, &steps));

151:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
152:      Free work space.  All PETSc objects should be destroyed when they
153:      are no longer needed.
154:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
155:   PetscCall(MatDestroy(&J));
156:   PetscCall(MatFDColoringDestroy(&matfdcoloring));
157:   PetscCall(VecDestroy(&x));
158:   PetscCall(VecDestroy(&r));
159:   PetscCall(TSDestroy(&ts));
160:   PetscCall(DMDestroy(&da));

162:   PetscCall(PetscFinalize());
163:   return 0;
164: }

166: typedef struct {
167:   PetscScalar w, u;
168: } Field;
169: /* ------------------------------------------------------------------- */
170: /*
171:    FormFunction - Evaluates nonlinear function, F(x).

173:    Input Parameters:
174: .  ts - the TS context
175: .  X - input vector
176: .  ptr - optional user-defined context, as set by SNESSetFunction()

178:    Output Parameter:
179: .  F - function vector
180:  */
181: PetscErrorCode FormFunction(TS ts, PetscReal ftime, Vec X, Vec Xdot, Vec F, void *ptr)
182: {
183:   DM          da;
184:   PetscInt    i, Mx, xs, xm;
185:   PetscReal   hx, sx;
186:   PetscScalar r, l;
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 (x[i].u < -1.0 + 2.0 * ctx->tol) f[i].w += .5 * ctx->theta * (-PetscLogScalar(ctx->tol) + PetscLogScalar((1.0 - x[i].u) / 2.0)) + ctx->theta_c * x[i].u;
238:         else if (x[i].u > 1.0 - 2.0 * ctx->tol) f[i].w += .5 * ctx->theta * (-PetscLogScalar((1.0 + x[i].u) / 2.0) + PetscLogScalar(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:       case 4:
242:         break;
243:       }
244:     }
245:     f[i].u = xdot[i].u - (x[i - 1].w + x[i + 1].w - 2.0 * x[i].w) * sx;
246:     if (ctx->energy == 4) {
247:       f[i].u = xdot[i].u;
248:       /* approximation of \grad (M(u) \grad w), where M(u) = (1-u^2) */
249:       r = (1.0 - x[i + 1].u * x[i + 1].u) * (x[i + 2].w - x[i].w) * .5 / hx;
250:       l = (1.0 - x[i - 1].u * x[i - 1].u) * (x[i].w - x[i - 2].w) * .5 / hx;
251:       f[i].u -= (r - l) * .5 / hx;
252:       f[i].u += 2.0 * ctx->theta_c * x[i].u * (x[i + 1].u - x[i - 1].u) * (x[i + 1].u - x[i - 1].u) * .25 * sx - (ctx->theta - ctx->theta_c * (1 - x[i].u * x[i].u)) * (x[i + 1].u + x[i - 1].u - 2.0 * x[i].u) * sx;
253:     }
254:   }

256:   /*
257:      Restore vectors
258:   */
259:   PetscCall(DMDAVecRestoreArrayRead(da, localXdot, &xdot));
260:   PetscCall(DMDAVecRestoreArrayRead(da, localX, &x));
261:   PetscCall(DMDAVecRestoreArray(da, F, &f));
262:   PetscCall(DMRestoreLocalVector(da, &localX));
263:   PetscCall(DMRestoreLocalVector(da, &localXdot));
264:   PetscFunctionReturn(PETSC_SUCCESS);
265: }

267: /* ------------------------------------------------------------------- */
268: PetscErrorCode FormInitialSolution(DM da, Vec X, PetscReal kappa)
269: {
270:   PetscInt  i, xs, xm, Mx, xgs, xgm;
271:   Field    *x;
272:   PetscReal hx, xx, r, sx;
273:   Vec       Xg;

275:   PetscFunctionBegin;
276:   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));

278:   hx = 1.0 / (PetscReal)Mx;
279:   sx = 1.0 / (hx * hx);

281:   /*
282:      Get pointers to vector data
283:   */
284:   PetscCall(DMCreateLocalVector(da, &Xg));
285:   PetscCall(DMDAVecGetArray(da, Xg, &x));

287:   /*
288:      Get local grid boundaries
289:   */
290:   PetscCall(DMDAGetCorners(da, &xs, NULL, NULL, &xm, NULL, NULL));
291:   PetscCall(DMDAGetGhostCorners(da, &xgs, NULL, NULL, &xgm, NULL, NULL));

293:   /*
294:      Compute u function over the locally owned part of the grid including ghost points
295:   */
296:   for (i = xgs; i < xgs + xgm; i++) {
297:     xx = i * hx;
298:     r  = PetscSqrtReal((xx - .5) * (xx - .5));
299:     if (r < .125) x[i].u = 1.0;
300:     else x[i].u = -.50;
301:     /* fill in x[i].w so that valgrind doesn't detect use of uninitialized memory */
302:     x[i].w = 0;
303:   }
304:   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;

306:   /*
307:      Restore vectors
308:   */
309:   PetscCall(DMDAVecRestoreArray(da, Xg, &x));

311:   /* Grab only the global part of the vector */
312:   PetscCall(VecSet(X, 0));
313:   PetscCall(DMLocalToGlobalBegin(da, Xg, ADD_VALUES, X));
314:   PetscCall(DMLocalToGlobalEnd(da, Xg, ADD_VALUES, X));
315:   PetscCall(VecDestroy(&Xg));
316:   PetscFunctionReturn(PETSC_SUCCESS);
317: }

319: /*TEST

321:    build:
322:      requires: !complex !single

324:    test:
325:      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
326:      requires: x

328: TEST*/