Actual source code: biharmonic3.c
petsc-3.4.2 2013-07-02
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 <petscdmda.h>
28: #include <petscts.h>
29: #include <petscdraw.h>
31: /*
32: User-defined routines
33: */
34: extern PetscErrorCode FormFunction(TS,PetscReal,Vec,Vec,Vec,void*),FormInitialSolution(DM,Vec,PetscReal);
35: typedef struct {PetscBool cahnhillard;PetscReal kappa;PetscInt energy;PetscReal tol;PetscReal theta;PetscReal theta_c;} UserCtx;
39: int main(int argc,char **argv)
40: {
41: TS ts; /* nonlinear solver */
42: Vec x,r; /* solution, residual vectors */
43: Mat J; /* Jacobian matrix */
44: PetscInt steps,Mx,maxsteps = 10000000;
46: DM da;
47: MatFDColoring matfdcoloring;
48: ISColoring iscoloring;
49: PetscReal dt;
50: PetscReal vbounds[] = {-100000,100000,-1.1,1.1};
51: PetscBool wait;
52: Vec ul,uh;
53: SNES snes;
54: UserCtx ctx;
56: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
57: Initialize program
58: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
59: PetscInitialize(&argc,&argv,(char*)0,help);
60: ctx.kappa = 1.0;
61: PetscOptionsGetReal(NULL,"-kappa",&ctx.kappa,NULL);
62: ctx.cahnhillard = PETSC_FALSE;
63: PetscOptionsGetBool(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,"-energy",&ctx.energy,NULL); */
68: PetscOptionsInt("-energy","type of energy (1=double well, 2=double obstacle, 3=logarithmic, 4=degenerate mobility and weird splitting)","",ctx.energy,&ctx.energy,NULL);
69: ctx.tol = 1.0e-8;
70: PetscOptionsGetReal(NULL,"-tol",&ctx.tol,NULL);
71: ctx.theta = .001;
72: ctx.theta_c = 1.0;
73: PetscOptionsGetReal(NULL,"-theta",&ctx.theta,NULL);
74: PetscOptionsGetReal(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, DMDA_BOUNDARY_PERIODIC, -10,2,2,NULL,&da);
80: DMDASetFieldName(da,0,"Biharmonic heat equation: w = -kappa*u_xx");
81: DMDASetFieldName(da,1,"Biharmonic heat equation: u");
82: DMDAGetInfo(da,0,&Mx,0,0,0,0,0,0,0,0,0,0,0);
83: dt = 1.0/(10.*ctx.kappa*Mx*Mx*Mx*Mx);
85: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
86: Extract global vectors from DMDA; then duplicate for remaining
87: vectors that are the same types
88: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
89: DMCreateGlobalVector(da,&x);
90: VecDuplicate(x,&r);
92: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
93: Create timestepping solver context
94: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
95: TSCreate(PETSC_COMM_WORLD,&ts);
96: TSSetDM(ts,da);
97: TSSetProblemType(ts,TS_NONLINEAR);
98: TSSetIFunction(ts,NULL,FormFunction,&ctx);
99: TSSetDuration(ts,maxsteps,.02);
100: TSSetExactFinalTime(ts,TS_EXACTFINALTIME_STEPOVER);
102: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
103: Create matrix data structure; set Jacobian evaluation routine
105: < Set Jacobian matrix data structure and default Jacobian evaluation
106: routine. User can override with:
107: -snes_mf : matrix-free Newton-Krylov method with no preconditioning
108: (unless user explicitly sets preconditioner)
109: -snes_mf_operator : form preconditioning matrix as set by the user,
110: but use matrix-free approx for Jacobian-vector
111: products within Newton-Krylov method
113: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
114: TSGetSNES(ts,&snes);
115: DMCreateColoring(da,IS_COLORING_GLOBAL,MATAIJ,&iscoloring);
116: DMCreateMatrix(da,MATAIJ,&J);
117: MatFDColoringCreate(J,iscoloring,&matfdcoloring);
118: ISColoringDestroy(&iscoloring);
119: MatFDColoringSetFunction(matfdcoloring,(PetscErrorCode (*)(void))SNESTSFormFunction,ts);
120: MatFDColoringSetFromOptions(matfdcoloring);
121: SNESSetJacobian(snes,J,J,SNESComputeJacobianDefaultColor,matfdcoloring);
123: {
124: VecDuplicate(x,&ul);
125: VecDuplicate(x,&uh);
126: VecStrideSet(ul,0,SNES_VI_NINF);
127: VecStrideSet(ul,1,-1.0);
128: VecStrideSet(uh,0,SNES_VI_INF);
129: VecStrideSet(uh,1,1.0);
130: TSVISetVariableBounds(ts,ul,uh);
131: }
133: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
134: Customize nonlinear solver
135: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
136: TSSetType(ts,TSBEULER);
138: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
139: Set initial conditions
140: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
141: FormInitialSolution(da,x,ctx.kappa);
142: TSSetInitialTimeStep(ts,0.0,dt);
143: TSSetSolution(ts,x);
145: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
146: Set runtime options
147: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
148: TSSetFromOptions(ts);
150: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
151: Solve nonlinear system
152: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
153: TSSolve(ts,x);
154: wait = PETSC_FALSE;
155: PetscOptionsGetBool(NULL,"-wait",&wait,NULL);
156: if (wait) {
157: PetscSleep(-1);
158: }
159: TSGetTimeStepNumber(ts,&steps);
161: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
162: Free work space. All PETSc objects should be destroyed when they
163: are no longer needed.
164: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
165: {
166: VecDestroy(&ul);
167: VecDestroy(&uh);
168: }
169: MatDestroy(&J);
170: MatFDColoringDestroy(&matfdcoloring);
171: VecDestroy(&x);
172: VecDestroy(&r);
173: TSDestroy(&ts);
174: DMDestroy(&da);
176: PetscFinalize();
177: return(0);
178: }
180: typedef struct {PetscScalar w,u;} Field;
181: /* ------------------------------------------------------------------- */
184: /*
185: FormFunction - Evaluates nonlinear function, F(x).
187: Input Parameters:
188: . ts - the TS context
189: . X - input vector
190: . ptr - optional user-defined context, as set by SNESSetFunction()
192: Output Parameter:
193: . F - function vector
194: */
195: PetscErrorCode FormFunction(TS ts,PetscReal ftime,Vec X,Vec Xdot,Vec F,void *ptr)
196: {
197: DM da;
199: PetscInt i,Mx,xs,xm;
200: PetscReal hx,sx;
201: PetscScalar r,l;
202: Field *x,*xdot,*f;
203: Vec localX,localXdot;
204: UserCtx *ctx = (UserCtx*)ptr;
207: TSGetDM(ts,&da);
208: DMGetLocalVector(da,&localX);
209: DMGetLocalVector(da,&localXdot);
210: DMDAGetInfo(da,PETSC_IGNORE,&Mx,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,
211: 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: DMDAVecGetArray(da,localX,&x);
230: DMDAVecGetArray(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 (x[i].u < -1.0 + 2.0*ctx->tol) f[i].w += .5*ctx->theta*(-log(ctx->tol) + log((1.0-x[i].u)/2.0)) + ctx->theta_c*x[i].u;
253: else if (x[i].u > 1.0 - 2.0*ctx->tol) f[i].w += .5*ctx->theta*(-log((1.0+x[i].u)/2.0) + log(ctx->tol)) + ctx->theta_c*x[i].u;
254: else f[i].w += .5*ctx->theta*(-log((1.0+x[i].u)/2.0) + log((1.0-x[i].u)/2.0)) + ctx->theta_c*x[i].u;
255: break;
256: case 4:
257: break;
258: }
259: }
260: f[i].u = xdot[i].u - (x[i-1].w + x[i+1].w - 2.0*x[i].w)*sx;
261: if (ctx->energy==4) {
262: f[i].u = xdot[i].u;
263: /* approximation of \grad (M(u) \grad w), where M(u) = (1-u^2) */
264: r = (1.0 - x[i+1].u*x[i+1].u)*(x[i+2].w-x[i].w)*.5/hx;
265: l = (1.0 - x[i-1].u*x[i-1].u)*(x[i].w-x[i-2].w)*.5/hx;
266: f[i].u -= (r - l)*.5/hx;
267: 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;
268: }
269: }
271: /*
272: Restore vectors
273: */
274: DMDAVecRestoreArray(da,localXdot,&xdot);
275: DMDAVecRestoreArray(da,localX,&x);
276: DMDAVecRestoreArray(da,F,&f);
277: DMRestoreLocalVector(da,&localX);
278: DMRestoreLocalVector(da,&localXdot);
279: return(0);
280: }
282: /* ------------------------------------------------------------------- */
285: PetscErrorCode FormInitialSolution(DM da,Vec X,PetscReal kappa)
286: {
288: PetscInt i,xs,xm,Mx;
289: Field *x;
290: PetscReal hx,xx,r,sx;
293: DMDAGetInfo(da,PETSC_IGNORE,&Mx,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,
294: PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);
296: hx = 1.0/(PetscReal)Mx;
297: sx = 1.0/(hx*hx);
299: /*
300: Get pointers to vector data
301: */
302: DMDAVecGetArray(da,X,&x);
304: /*
305: Get local grid boundaries
306: */
307: DMDAGetCorners(da,&xs,NULL,NULL,&xm,NULL,NULL);
309: /*
310: Compute function over the locally owned part of the grid
311: */
312: for (i=xs; i<xs+xm; i++) {
313: xx = i*hx;
314: r = PetscSqrtScalar((xx-.5)*(xx-.5));
315: if (r < .125) x[i].u = 1.0;
316: else x[i].u = -.50;
317: /* u[i] = PetscPowScalar(x - .5,4.0); */
318: }
319: 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;
321: /*
322: Restore vectors
323: */
324: DMDAVecRestoreArray(da,X,&x);
325: return(0);
326: }