Actual source code: biharmonic2.c

petsc-3.8.4 2018-03-24
Report Typos and Errors

  2: static char help[] = "Solves biharmonic equation in 1d.\n";

  4: /*
  5:   Solves the equation biharmonic equation in split form

  7:     w = -kappa \Delta u
  8:     u_t =  \Delta w
  9:     -1  <= u <= 1
 10:     Periodic boundary conditions

 12: Evolve the biharmonic heat equation with bounds:  (same as biharmonic)
 13: ---------------
 14: ./biharmonic2 -ts_monitor -snes_monitor -ts_monitor_draw_solution  -pc_type lu  -draw_pause .1 -snes_converged_reason --wait   -ts_type beuler  -da_refine 5 -draw_fields 1 -ts_dt 9.53674e-9

 16:     w = -kappa \Delta u  + u^3  - u
 17:     u_t =  \Delta w
 18:     -1  <= u <= 1
 19:     Periodic boundary conditions

 21: Evolve the Cahn-Hillard equations:
 22: ---------------
 23: ./biharmonic2 -ts_monitor -snes_monitor -ts_monitor_draw_solution  -pc_type lu  -draw_pause .1 -snes_converged_reason  --wait   -ts_type beuler    -da_refine 6 -vi  -draw_fields 1  -kappa .00001 -ts_dt 5.96046e-06 -cahn-hillard


 26: */
 27:  #include <petscdm.h>
 28:  #include <petscdmda.h>
 29:  #include <petscts.h>
 30:  #include <petscdraw.h>

 32: /*
 33:    User-defined routines
 34: */
 35: extern PetscErrorCode FormFunction(TS,PetscReal,Vec,Vec,Vec,void*),FormInitialSolution(DM,Vec,PetscReal);
 36: typedef struct {PetscBool cahnhillard;PetscReal kappa;PetscInt energy;PetscReal tol;PetscReal theta;PetscReal theta_c;} UserCtx;

 38: int main(int argc,char **argv)
 39: {
 40:   TS             ts;                           /* nonlinear solver */
 41:   Vec            x,r;                          /* solution, residual vectors */
 42:   Mat            J;                            /* Jacobian matrix */
 43:   PetscInt       steps,Mx;
 45:   DM             da;
 46:   MatFDColoring  matfdcoloring;
 47:   ISColoring     iscoloring;
 48:   PetscReal      dt;
 49:   PetscReal      vbounds[] = {-100000,100000,-1.1,1.1};
 50:   PetscBool      wait;
 51:   Vec            ul,uh;
 52:   SNES           snes;
 53:   UserCtx        ctx;

 55:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 56:      Initialize program
 57:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 58:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 59:   ctx.kappa = 1.0;
 60:   PetscOptionsGetReal(NULL,NULL,"-kappa",&ctx.kappa,NULL);
 61:   ctx.cahnhillard = PETSC_FALSE;

 63:   PetscOptionsGetBool(NULL,NULL,"-cahn-hillard",&ctx.cahnhillard,NULL);
 64:   PetscViewerDrawSetBounds(PETSC_VIEWER_DRAW_(PETSC_COMM_WORLD),2,vbounds);
 65:   PetscViewerDrawResize(PETSC_VIEWER_DRAW_(PETSC_COMM_WORLD),600,600);
 66:   ctx.energy = 1;
 67:   /*PetscOptionsGetInt(NULL,NULL,"-energy",&ctx.energy,NULL);*/
 68:   PetscOptionsGetInt(NULL,NULL,"-energy",&ctx.energy,NULL);
 69:   ctx.tol     = 1.0e-8;
 70:   PetscOptionsGetReal(NULL,NULL,"-tol",&ctx.tol,NULL);
 71:   ctx.theta   = .001;
 72:   ctx.theta_c = 1.0;
 73:   PetscOptionsGetReal(NULL,NULL,"-theta",&ctx.theta,NULL);
 74:   PetscOptionsGetReal(NULL,NULL,"-theta_c",&ctx.theta_c,NULL);

 76:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 77:      Create distributed array (DMDA) to manage parallel grid and vectors
 78:   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 79:   DMDACreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_PERIODIC, -10,2,2,NULL,&da);
 80:   DMSetFromOptions(da);
 81:   DMSetUp(da);
 82:   DMDASetFieldName(da,0,"Biharmonic heat equation: w = -kappa*u_xx");
 83:   DMDASetFieldName(da,1,"Biharmonic heat equation: u");
 84:   DMDAGetInfo(da,0,&Mx,0,0,0,0,0,0,0,0,0,0,0);
 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:   DMCreateGlobalVector(da,&x);
 92:   VecDuplicate(x,&r);

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

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 preconditioning matrix as set by the user,
112:                          but use matrix-free approx for Jacobian-vector
113:                          products within Newton-Krylov method

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

127:   {
128:     VecDuplicate(x,&ul);
129:     VecDuplicate(x,&uh);
130:     VecStrideSet(ul,0,PETSC_NINFINITY);
131:     VecStrideSet(ul,1,-1.0);
132:     VecStrideSet(uh,0,PETSC_INFINITY);
133:     VecStrideSet(uh,1,1.0);
134:     TSVISetVariableBounds(ts,ul,uh);
135:   }

137:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
138:      Customize nonlinear solver
139:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
140:   TSSetType(ts,TSBEULER);

142:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
143:      Set initial conditions
144:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
145:   FormInitialSolution(da,x,ctx.kappa);
146:   TSSetTimeStep(ts,dt);
147:   TSSetSolution(ts,x);

149:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
150:      Set runtime options
151:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
152:   TSSetFromOptions(ts);

154:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
155:      Solve nonlinear system
156:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
157:   TSSolve(ts,x);
158:   wait = PETSC_FALSE;
159:   PetscOptionsGetBool(NULL,NULL,"-wait",&wait,NULL);
160:   if (wait) {
161:     PetscSleep(-1);
162:   }
163:   TSGetStepNumber(ts,&steps);

165:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
166:      Free work space.  All PETSc objects should be destroyed when they
167:      are no longer needed.
168:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
169:   {
170:     VecDestroy(&ul);
171:     VecDestroy(&uh);
172:   }
173:   MatDestroy(&J);
174:   MatFDColoringDestroy(&matfdcoloring);
175:   VecDestroy(&x);
176:   VecDestroy(&r);
177:   TSDestroy(&ts);
178:   DMDestroy(&da);

180:   PetscFinalize();
181:   return ierr;
182: }

184: typedef struct {PetscScalar w,u;} Field;
185: /* ------------------------------------------------------------------- */
186: /*
187:    FormFunction - Evaluates nonlinear function, F(x).

189:    Input Parameters:
190: .  ts - the TS context
191: .  X - input vector
192: .  ptr - optional user-defined context, as set by SNESSetFunction()

194:    Output Parameter:
195: .  F - function vector
196:  */
197: PetscErrorCode FormFunction(TS ts,PetscReal ftime,Vec X,Vec Xdot,Vec F,void *ptr)
198: {
199:   DM             da;
201:   PetscInt       i,Mx,xs,xm;
202:   PetscReal      hx,sx;
203:   Field          *x,*xdot,*f;
204:   Vec            localX,localXdot;
205:   UserCtx        *ctx = (UserCtx*)ptr;

208:   TSGetDM(ts,&da);
209:   DMGetLocalVector(da,&localX);
210:   DMGetLocalVector(da,&localXdot);
211:   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);

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

215:   /*
216:      Scatter ghost points to local vector,using the 2-step process
217:         DMGlobalToLocalBegin(),DMGlobalToLocalEnd().
218:      By placing code between these two statements, computations can be
219:      done while messages are in transition.
220:   */
221:   DMGlobalToLocalBegin(da,X,INSERT_VALUES,localX);
222:   DMGlobalToLocalEnd(da,X,INSERT_VALUES,localX);
223:   DMGlobalToLocalBegin(da,Xdot,INSERT_VALUES,localXdot);
224:   DMGlobalToLocalEnd(da,Xdot,INSERT_VALUES,localXdot);

226:   /*
227:      Get pointers to vector data
228:   */
229:   DMDAVecGetArrayRead(da,localX,&x);
230:   DMDAVecGetArrayRead(da,localXdot,&xdot);
231:   DMDAVecGetArray(da,F,&f);

233:   /*
234:      Get local grid boundaries
235:   */
236:   DMDAGetCorners(da,&xs,NULL,NULL,&xm,NULL,NULL);

238:   /*
239:      Compute function over the locally owned part of the grid
240:   */
241:   for (i=xs; i<xs+xm; i++) {
242:     f[i].w =  x[i].w + ctx->kappa*(x[i-1].u + x[i+1].u - 2.0*x[i].u)*sx;
243:     if (ctx->cahnhillard) {
244:       switch (ctx->energy) {
245:       case 1: /* double well */
246:         f[i].w += -x[i].u*x[i].u*x[i].u + x[i].u;
247:         break;
248:       case 2: /* double obstacle */
249:         f[i].w += x[i].u;
250:         break;
251:       case 3: /* logarithmic */
252:         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;
253:         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;
254:         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;
255:         break;
256:       }
257:     }
258:     f[i].u = xdot[i].u - (x[i-1].w + x[i+1].w - 2.0*x[i].w)*sx;
259:   }

261:   /*
262:      Restore vectors
263:   */
264:   DMDAVecRestoreArrayRead(da,localXdot,&xdot);
265:   DMDAVecRestoreArrayRead(da,localX,&x);
266:   DMDAVecRestoreArray(da,F,&f);
267:   DMRestoreLocalVector(da,&localX);
268:   DMRestoreLocalVector(da,&localXdot);
269:   return(0);
270: }

272: /* ------------------------------------------------------------------- */
273: PetscErrorCode FormInitialSolution(DM da,Vec X,PetscReal kappa)
274: {
276:   PetscInt       i,xs,xm,Mx;
277:   Field          *x;
278:   PetscReal      hx,xx,r,sx;

281:   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);

283:   hx = 1.0/(PetscReal)Mx;
284:   sx = 1.0/(hx*hx);

286:   /*
287:      Get pointers to vector data
288:   */
289:   DMDAVecGetArray(da,X,&x);

291:   /*
292:      Get local grid boundaries
293:   */
294:   DMDAGetCorners(da,&xs,NULL,NULL,&xm,NULL,NULL);

296:   /*
297:      Compute function over the locally owned part of the grid
298:   */
299:   for (i=xs; i<xs+xm; i++) {
300:     xx = i*hx;
301:     r = PetscSqrtReal((xx-.5)*(xx-.5));
302:     if (r < .125) x[i].u = 1.0;
303:     else          x[i].u = -.50;
304:     /*  u[i] = PetscPowScalar(x - .5,4.0); */
305:   }
306:   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;

308:   /*
309:      Restore vectors
310:   */
311:   DMDAVecRestoreArray(da,X,&x);
312:   return(0);
313: }