Actual source code: biharmonic2.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;
64: PetscOptionsGetBool(NULL,"-cahn-hillard",&ctx.cahnhillard,NULL);
65: PetscViewerDrawSetBounds(PETSC_VIEWER_DRAW_(PETSC_COMM_WORLD),2,vbounds);
66: PetscViewerDrawResize(PETSC_VIEWER_DRAW_(PETSC_COMM_WORLD),600,600);
67: ctx.energy = 1;
68: /*PetscOptionsGetInt(NULL,"-energy",&ctx.energy,NULL);*/
69: PetscOptionsInt("-energy","type of energy (1=double well, 2=double obstacle, 3=logarithmic)","",ctx.energy,&ctx.energy,NULL);
70: ctx.tol = 1.0e-8;
71: PetscOptionsGetReal(NULL,"-tol",&ctx.tol,NULL);
72: ctx.theta = .001;
73: ctx.theta_c = 1.0;
74: PetscOptionsGetReal(NULL,"-theta",&ctx.theta,NULL);
75: PetscOptionsGetReal(NULL,"-theta_c",&ctx.theta_c,NULL);
77: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
78: Create distributed array (DMDA) to manage parallel grid and vectors
79: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
80: DMDACreate1d(PETSC_COMM_WORLD, DMDA_BOUNDARY_PERIODIC, -10,2,2,NULL,&da);
81: DMDASetFieldName(da,0,"Biharmonic heat equation: w = -kappa*u_xx");
82: DMDASetFieldName(da,1,"Biharmonic heat equation: u");
83: DMDAGetInfo(da,0,&Mx,0,0,0,0,0,0,0,0,0,0,0);
84: dt = 1.0/(10.*ctx.kappa*Mx*Mx*Mx*Mx);
86: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
87: Extract global vectors from DMDA; then duplicate for remaining
88: vectors that are the same types
89: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
90: DMCreateGlobalVector(da,&x);
91: VecDuplicate(x,&r);
93: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
94: Create timestepping solver context
95: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
96: TSCreate(PETSC_COMM_WORLD,&ts);
97: TSSetDM(ts,da);
98: TSSetProblemType(ts,TS_NONLINEAR);
99: TSSetIFunction(ts,NULL,FormFunction,&ctx);
100: TSSetDuration(ts,maxsteps,.02);
101: TSSetExactFinalTime(ts,TS_EXACTFINALTIME_INTERPOLATE);
103: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
104: Create matrix data structure; set Jacobian evaluation routine
106: < Set Jacobian matrix data structure and default Jacobian evaluation
107: routine. User can override with:
108: -snes_mf : matrix-free Newton-Krylov method with no preconditioning
109: (unless user explicitly sets preconditioner)
110: -snes_mf_operator : form preconditioning matrix as set by the user,
111: but use matrix-free approx for Jacobian-vector
112: products within Newton-Krylov method
114: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
115: TSGetSNES(ts,&snes);
116: DMCreateColoring(da,IS_COLORING_GLOBAL,MATAIJ,&iscoloring);
117: DMCreateMatrix(da,MATAIJ,&J);
118: MatFDColoringCreate(J,iscoloring,&matfdcoloring);
119: ISColoringDestroy(&iscoloring);
120: MatFDColoringSetFunction(matfdcoloring,(PetscErrorCode (*)(void))SNESTSFormFunction,ts);
121: MatFDColoringSetFromOptions(matfdcoloring);
122: SNESSetJacobian(snes,J,J,SNESComputeJacobianDefaultColor,matfdcoloring);
124: {
125: VecDuplicate(x,&ul);
126: VecDuplicate(x,&uh);
127: VecStrideSet(ul,0,SNES_VI_NINF);
128: VecStrideSet(ul,1,-1.0);
129: VecStrideSet(uh,0,SNES_VI_INF);
130: VecStrideSet(uh,1,1.0);
131: TSVISetVariableBounds(ts,ul,uh);
132: }
134: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
135: Customize nonlinear solver
136: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
137: TSSetType(ts,TSBEULER);
139: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
140: Set initial conditions
141: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
142: FormInitialSolution(da,x,ctx.kappa);
143: TSSetInitialTimeStep(ts,0.0,dt);
144: TSSetSolution(ts,x);
146: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
147: Set runtime options
148: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
149: TSSetFromOptions(ts);
151: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
152: Solve nonlinear system
153: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
154: TSSolve(ts,x);
155: wait = PETSC_FALSE;
156: PetscOptionsGetBool(NULL,"-wait",&wait,NULL);
157: if (wait) {
158: PetscSleep(-1);
159: }
160: TSGetTimeStepNumber(ts,&steps);
162: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
163: Free work space. All PETSc objects should be destroyed when they
164: are no longer needed.
165: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
166: {
167: VecDestroy(&ul);
168: VecDestroy(&uh);
169: }
170: MatDestroy(&J);
171: MatFDColoringDestroy(&matfdcoloring);
172: VecDestroy(&x);
173: VecDestroy(&r);
174: TSDestroy(&ts);
175: DMDestroy(&da);
177: PetscFinalize();
178: return(0);
179: }
181: typedef struct {PetscScalar w,u;} Field;
182: /* ------------------------------------------------------------------- */
185: /*
186: FormFunction - Evaluates nonlinear function, F(x).
188: Input Parameters:
189: . ts - the TS context
190: . X - input vector
191: . ptr - optional user-defined context, as set by SNESSetFunction()
193: Output Parameter:
194: . F - function vector
195: */
196: PetscErrorCode FormFunction(TS ts,PetscReal ftime,Vec X,Vec Xdot,Vec F,void *ptr)
197: {
198: DM da;
200: PetscInt i,Mx,xs,xm;
201: PetscReal hx,sx;
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 (PetscRealPart(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 (PetscRealPart(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: }
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: DMDAVecRestoreArray(da,localXdot,&xdot);
265: DMDAVecRestoreArray(da,localX,&x);
266: DMDAVecRestoreArray(da,F,&f);
267: DMRestoreLocalVector(da,&localX);
268: DMRestoreLocalVector(da,&localXdot);
269: return(0);
270: }
272: /* ------------------------------------------------------------------- */
275: PetscErrorCode FormInitialSolution(DM da,Vec X,PetscReal kappa)
276: {
278: PetscInt i,xs,xm,Mx;
279: Field *x;
280: PetscReal hx,xx,r,sx;
283: DMDAGetInfo(da,PETSC_IGNORE,&Mx,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,
284: PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);
286: hx = 1.0/(PetscReal)Mx;
287: sx = 1.0/(hx*hx);
289: /*
290: Get pointers to vector data
291: */
292: DMDAVecGetArray(da,X,&x);
294: /*
295: Get local grid boundaries
296: */
297: DMDAGetCorners(da,&xs,NULL,NULL,&xm,NULL,NULL);
299: /*
300: Compute function over the locally owned part of the grid
301: */
302: for (i=xs; i<xs+xm; i++) {
303: xx = i*hx;
304: r = PetscSqrtScalar((xx-.5)*(xx-.5));
305: if (r < .125) x[i].u = 1.0;
306: else x[i].u = -.50;
307: /* u[i] = PetscPowScalar(x - .5,4.0); */
308: }
309: 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;
311: /*
312: Restore vectors
313: */
314: DMDAVecRestoreArray(da,X,&x);
315: return(0);
316: }