Actual source code: ex54.c
petsc-dev 2014-02-02
1: static char help[] = "Cahn-Hilliard-2d problem for constant mobility and triangular elements.\n\
2: Runtime options include:\n\
3: -xmin <xmin>\n\
4: -xmax <xmax>\n\
5: -ymin <ymin>\n\
6: -T <T>, where <T> is the end time for the time domain simulation\n\
7: -dt <dt>,where <dt> is the step size for the numerical integration\n\
8: -gamma <gamma>\n\
9: -theta_c <theta_c>\n\n";
11: /*
12: Run with for example: -pc_type mg -pc_mg_galerkin -T .01 -da_grid_x 65 -da_grid_y 65 -pc_mg_levels 4 -ksp_type fgmres -snes_atol 1.e-14 -mat_no_inode
13: */
15: #include <petscsnes.h>
16: #include <petscdmda.h>
18: typedef struct {
19: PetscReal dt,T; /* Time step and end time */
20: DM da;
21: Mat M; /* Jacobian matrix */
22: Mat M_0;
23: Vec q,u,work1;
24: PetscScalar gamma,theta_c; /* physics parameters */
25: PetscReal xmin,xmax,ymin,ymax;
26: PetscBool tsmonitor;
27: } AppCtx;
29: PetscErrorCode GetParams(AppCtx*);
30: PetscErrorCode SetVariableBounds(DM,Vec,Vec);
31: PetscErrorCode SetUpMatrices(AppCtx*);
32: PetscErrorCode FormFunction(SNES,Vec,Vec,void*);
33: PetscErrorCode FormJacobian(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
34: PetscErrorCode SetInitialGuess(Vec,AppCtx*);
35: PetscErrorCode Update_q(Vec,Vec,Mat,AppCtx*);
36: PetscLogEvent event_update_q;
40: int main(int argc, char **argv)
41: {
42: PetscErrorCode ierr;
43: Vec x,r; /* Solution and residual vectors */
44: SNES snes; /* Nonlinear solver context */
45: AppCtx user; /* Application context */
46: Vec xl,xu; /* Upper and lower bounds on variables */
47: Mat J;
48: PetscReal t=0.0;
49: PETSC_UNUSED PetscLogStage stage_timestep;
50: PetscInt its;
52: PetscInitialize(&argc,&argv, (char*)0, help);
54: /* Get physics and time parameters */
55: GetParams(&user);
56: /* Create a 2D DA with dof = 2 */
57: DMDACreate2d(PETSC_COMM_WORLD,DMDA_BOUNDARY_NONE,DMDA_BOUNDARY_NONE,DMDA_STENCIL_BOX,-4,-4,PETSC_DECIDE,PETSC_DECIDE,2,1,NULL,NULL,&user.da);
58: /* Set Element type (triangular) */
59: DMDASetElementType(user.da,DMDA_ELEMENT_P1);
61: /* Set x and y coordinates */
62: DMDASetUniformCoordinates(user.da,user.xmin,user.xmax,user.ymin,user.ymax,0.0,1.0);
64: /* Get global vector x from DM and duplicate vectors r,xl,xu */
65: DMCreateGlobalVector(user.da,&x);
66: VecDuplicate(x,&r);
67: VecDuplicate(x,&xl);
68: VecDuplicate(x,&xu);
69: VecDuplicate(x,&user.q);
71: /* Get Jacobian matrix structure from the da */
72: DMSetMatType(user.da,MATAIJ);
73: DMCreateMatrix(user.da,&user.M);
74: /* Form the jacobian matrix and M_0 */
75: SetUpMatrices(&user);
76: MatDuplicate(user.M,MAT_DO_NOT_COPY_VALUES,&J);
78: /* Create nonlinear solver context */
79: SNESCreate(PETSC_COMM_WORLD,&snes);
80: SNESSetDM(snes,user.da);
82: /* Set Function evaluation and jacobian evaluation routines */
83: SNESSetFunction(snes,r,FormFunction,(void*)&user);
84: SNESSetJacobian(snes,J,J,FormJacobian,(void*)&user);
86: /* Set the boundary conditions */
87: SetVariableBounds(user.da,xl,xu);
88: SNESVISetVariableBounds(snes,xl,xu);
89: SNESSetFromOptions(snes);
91: SetInitialGuess(x,&user);
92: PetscLogStageRegister("Time stepping",&stage_timestep);
93: PetscLogEventRegister("Update q",MAT_CLASSID,&event_update_q);
94: PetscLogStagePush(stage_timestep);
95: /* Begin time loop */
96: while (t < user.T) {
97: Update_q(user.q,user.u,user.M_0,&user);
98: SNESSolve(snes,NULL,x);
99: SNESGetIterationNumber(snes,&its);
100: if (user.tsmonitor) {
101: PetscPrintf(PETSC_COMM_WORLD,"SNESVI solver converged at t = %5.4f in %d iterations\n",t,its);
102: }
103: VecStrideGather(x,1,user.u,INSERT_VALUES);
104: t = t + user.dt;
105: }
106: PetscLogStagePop();
108: VecDestroy(&x);
109: VecDestroy(&r);
110: VecDestroy(&xl);
111: VecDestroy(&xu);
112: VecDestroy(&user.q);
113: VecDestroy(&user.u);
114: VecDestroy(&user.work1);
115: MatDestroy(&user.M);
116: MatDestroy(&user.M_0);
117: MatDestroy(&J);
118: DMDestroy(&user.da);
119: SNESDestroy(&snes);
120: PetscFinalize();
121: return 0;
122: }
126: PetscErrorCode Update_q(Vec q,Vec u,Mat M_0,AppCtx *user)
127: {
129: PetscScalar *q_arr,*w_arr;
130: PetscInt i,n;
133: PetscLogEventBegin(event_update_q,0,0,0,0);
134: MatMult(M_0,u,user->work1);
135: VecScale(user->work1,-1.0);
136: VecGetLocalSize(u,&n);
137: VecGetArray(q,&q_arr);
138: VecGetArray(user->work1,&w_arr);
139: for (i=0; i<n; i++) q_arr[2*i]=q_arr[2*i+1] = w_arr[i];
140: VecRestoreArray(q,&q_arr);
141: VecRestoreArray(user->work1,&w_arr);
142: PetscLogEventEnd(event_update_q,0,0,0,0);
143: return(0);
144: }
148: PetscErrorCode SetInitialGuess(Vec X,AppCtx *user)
149: {
151: PetscScalar *x,*u;
152: PetscInt n,i;
153: Vec rand;
156: /* u = -0.4 + 0.05*rand(N,1)*(rand(N,1) - 0.5) */
157: VecDuplicate(user->u,&rand);
158: VecSetRandom(rand,NULL);
159: VecCopy(rand,user->u);
160: VecShift(rand,-0.5);
161: VecPointwiseMult(user->u,user->u,rand);
162: VecDestroy(&rand);
163: VecScale(user->u,0.05);
164: VecShift(user->u,-0.4);
166: VecGetLocalSize(X,&n);
167: VecGetArray(X,&x);
168: VecGetArray(user->u,&u);
169: /* Set initial guess, only set value for 2nd dof */
170: for (i=0; i<n/2; i++) x[2*i+1] = u[i];
171: VecRestoreArray(X,&x);
172: VecRestoreArray(user->u,&u);
173: return(0);
174: }
178: PetscErrorCode FormFunction(SNES snes,Vec X,Vec F,void *ctx)
179: {
181: AppCtx *user=(AppCtx*)ctx;
184: MatMultAdd(user->M,X,user->q,F);
185: return(0);
186: }
190: PetscErrorCode FormJacobian(SNES snes,Vec X,Mat *J,Mat *B,MatStructure *flg,void *ctx)
191: {
192: PetscErrorCode ierr;
193: AppCtx *user =(AppCtx*)ctx;
194: static PetscBool copied = PETSC_FALSE;
197: /* for active set method the matrix does not get changed, so do not need to copy each time,
198: if the active set remains the same for several solves the preconditioner does not need to be rebuilt*/
199: *flg = SAME_PRECONDITIONER;
200: if (!copied) {
201: MatCopy(user->M,*J,*flg);
202: copied = PETSC_TRUE;
203: }
204: MatAssemblyBegin(*J,MAT_FINAL_ASSEMBLY);
205: MatAssemblyEnd(*J,MAT_FINAL_ASSEMBLY);
206: return(0);
207: }
210: PetscErrorCode SetVariableBounds(DM da,Vec xl,Vec xu)
211: {
213: PetscScalar ***l,***u;
214: PetscInt xs,xm,ys,ym;
215: PetscInt j,i;
218: DMDAVecGetArrayDOF(da,xl,&l);
219: DMDAVecGetArrayDOF(da,xu,&u);
221: DMDAGetCorners(da,&xs,&ys,NULL,&xm,&ym,NULL);
223: for (j=ys; j < ys+ym; j++) {
224: for (i=xs; i < xs+xm;i++) {
225: l[j][i][0] = -PETSC_INFINITY;
226: l[j][i][1] = -1.0;
227: u[j][i][0] = PETSC_INFINITY;
228: u[j][i][1] = 1.0;
229: }
230: }
232: DMDAVecRestoreArrayDOF(da,xl,&l);
233: DMDAVecRestoreArrayDOF(da,xu,&u);
234: return(0);
235: }
239: PetscErrorCode GetParams(AppCtx *user)
240: {
242: PetscBool flg;
245: /* Set default parameters */
246: user->tsmonitor = PETSC_FALSE;
247: user->xmin = 0.0; user->xmax = 1.0;
248: user->ymin = 0.0; user->ymax = 1.0;
249: user->T = 0.0002; user->dt = 0.0001;
250: user->gamma = 3.2E-4; user->theta_c = 0;
252: PetscOptionsGetBool(NULL,"-ts_monitor",&user->tsmonitor,NULL);
253: PetscOptionsGetReal(NULL,"-xmin",&user->xmin,&flg);
254: PetscOptionsGetReal(NULL,"-xmax",&user->xmax,&flg);
255: PetscOptionsGetReal(NULL,"-ymin",&user->ymin,&flg);
256: PetscOptionsGetReal(NULL,"-ymax",&user->ymax,&flg);
257: PetscOptionsGetReal(NULL,"-T",&user->T,&flg);
258: PetscOptionsGetReal(NULL,"-dt",&user->dt,&flg);
259: PetscOptionsGetScalar(NULL,"-gamma",&user->gamma,&flg);
260: PetscOptionsGetScalar(NULL,"-theta_c",&user->theta_c,&flg);
261: return(0);
262: }
264: static void Gausspoints(PetscScalar *xx,PetscScalar *yy,PetscScalar *w,PetscScalar *x,PetscScalar *y)
265: {
267: xx[0] = 2.0/3.0*x[0] + 1.0/6.0*x[1] + 1.0/6.0*x[2];
268: xx[1] = 1.0/6.0*x[0] + 2.0/3.0*x[1] + 1.0/6.0*x[2];
269: xx[2] = 1.0/6.0*x[0] + 1.0/6.0*x[1] + 2.0/3.0*x[2];
271: yy[0] = 2.0/3.0*y[0] + 1.0/6.0*y[1] + 1.0/6.0*y[2];
272: yy[1] = 1.0/6.0*y[0] + 2.0/3.0*y[1] + 1.0/6.0*y[2];
273: yy[2] = 1.0/6.0*y[0] + 1.0/6.0*y[1] + 2.0/3.0*y[2];
275: *w = PetscAbsScalar(x[0]*(y[2]-y[1]) + x[2]*(y[1]-y[0]) + x[1]*(y[0]-y[2]))/6.0;
277: }
279: static void ShapefunctionsT3(PetscScalar *phi,PetscScalar phider[][2],PetscScalar xx,PetscScalar yy,PetscScalar *x,PetscScalar *y)
280: {
281: PetscScalar area,a1,a2,a3,b1,b2,b3,c1,c2,c3,pp;
283: /* Area of the triangle */
284: area = 1.0/2.0*PetscAbsScalar(x[0]*(y[2]-y[1]) + x[2]*(y[1]-y[0]) + x[1]*(y[0]-y[2]));
286: a1 = x[1]*y[2]-x[2]*y[1]; a2 = x[2]*y[0]-x[0]*y[2]; a3 = x[0]*y[1]-x[1]*y[0];
287: b1 = y[1]-y[2]; b2 = y[2]-y[0]; b3 = y[0]-y[1];
288: c1 = x[2]-x[1]; c2 = x[0]-x[2]; c3 = x[1]-x[0];
289: pp = 1.0/(2.0*area);
291: /* shape functions */
292: phi[0] = pp*(a1 + b1*xx + c1*yy);
293: phi[1] = pp*(a2 + b2*xx + c2*yy);
294: phi[2] = pp*(a3 + b3*xx + c3*yy);
296: /* shape functions derivatives */
297: phider[0][0] = pp*b1; phider[0][1] = pp*c1;
298: phider[1][0] = pp*b2; phider[1][1] = pp*c2;
299: phider[2][0] = pp*b3; phider[2][1] = pp*c3;
301: }
305: PetscErrorCode SetUpMatrices(AppCtx *user)
306: {
307: PetscErrorCode ierr;
308: PetscInt nele,nen,i;
309: const PetscInt *ele;
310: PetscScalar dt=user->dt;
311: Vec coords;
312: const PetscScalar *_coords;
313: PetscScalar x[3],y[3],xx[3],yy[3],w;
314: PetscInt idx[3];
315: PetscScalar phi[3],phider[3][2];
316: PetscScalar eM_0[3][3],eM_2[3][3];
317: Mat M =user->M;
318: PetscScalar gamma=user->gamma,theta_c=user->theta_c;
319: PetscInt m;
320: PetscInt j,k;
321: PetscInt row,cols[6],r;
322: PetscScalar vals[6];
323: PetscInt n,rstart;
324: IS isrow,iscol;
327: /* Get ghosted coordinates */
328: DMGetCoordinatesLocal(user->da,&coords);
329: VecGetArrayRead(coords,&_coords);
331: /* Get local element info */
332: DMDAGetElements(user->da,&nele,&nen,&ele);
333: for (i=0; i < nele; i++) {
334: idx[0] = ele[3*i]; idx[1] = ele[3*i+1]; idx[2] = ele[3*i+2];
335: x[0] = _coords[2*idx[0]]; y[0] = _coords[2*idx[0]+1];
336: x[1] = _coords[2*idx[1]]; y[1] = _coords[2*idx[1]+1];
337: x[2] = _coords[2*idx[2]]; y[2] = _coords[2*idx[2]+1];
339: PetscMemzero(xx,3*sizeof(PetscScalar));
340: PetscMemzero(yy,3*sizeof(PetscScalar));
341: Gausspoints(xx,yy,&w,x,y);
343: eM_0[0][0]=eM_0[0][1]=eM_0[0][2]=0.0;
344: eM_0[1][0]=eM_0[1][1]=eM_0[1][2]=0.0;
345: eM_0[2][0]=eM_0[2][1]=eM_0[2][2]=0.0;
346: eM_2[0][0]=eM_2[0][1]=eM_2[0][2]=0.0;
347: eM_2[1][0]=eM_2[1][1]=eM_2[1][2]=0.0;
348: eM_2[2][0]=eM_2[2][1]=eM_2[2][2]=0.0;
351: for (m=0; m<3; m++) {
352: PetscMemzero(phi,3*sizeof(PetscScalar));
353: phider[0][0]=phider[0][1]=0.0;
354: phider[1][0]=phider[1][1]=0.0;
355: phider[2][0]=phider[2][1]=0.0;
357: ShapefunctionsT3(phi,phider,xx[m],yy[m],x,y);
359: for (j=0; j<3; j++) {
360: for (k=0; k<3; k++) {
361: eM_0[k][j] += phi[j]*phi[k]*w;
362: eM_2[k][j] += phider[j][0]*phider[k][0]*w + phider[j][1]*phider[k][1]*w;
363: }
364: }
365: }
367: for (r=0; r<3; r++) {
368: row = 2*idx[r];
369: cols[0] = 2*idx[0]; vals[0] = dt*eM_2[r][0];
370: cols[1] = 2*idx[0]+1; vals[1] = eM_0[r][0];
371: cols[2] = 2*idx[1]; vals[2] = dt*eM_2[r][1];
372: cols[3] = 2*idx[1]+1; vals[3] = eM_0[r][1];
373: cols[4] = 2*idx[2]; vals[4] = dt*eM_2[r][2];
374: cols[5] = 2*idx[2]+1; vals[5] = eM_0[r][2];
376: /* Insert values in matrix M for 1st dof */
377: MatSetValuesLocal(M,1,&row,6,cols,vals,ADD_VALUES);
378: row = 2*idx[r]+1;
379: cols[0] = 2*idx[0]; vals[0] = -eM_0[r][0];
380: cols[1] = 2*idx[0]+1; vals[1] = gamma*eM_2[r][0]-theta_c*eM_0[r][0];
381: cols[2] = 2*idx[1]; vals[2] = -eM_0[r][1];
382: cols[3] = 2*idx[1]+1; vals[3] = gamma*eM_2[r][1]-theta_c*eM_0[r][1];
383: cols[4] = 2*idx[2]; vals[4] = -eM_0[r][2];
384: cols[5] = 2*idx[2]+1; vals[5] = gamma*eM_2[r][2]-theta_c*eM_2[r][2];
386: /* Insert values in matrix M for 2nd dof */
387: MatSetValuesLocal(M,1,&row,6,cols,vals,ADD_VALUES);
388: }
389: }
391: DMDARestoreElements(user->da,&nele,&nen,&ele);
392: VecRestoreArrayRead(coords,&_coords);
394: MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
395: MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);
397: /* Create ISs to extract matrix M_0 from M */
399: MatGetLocalSize(M,&n,NULL);
400: MatGetOwnershipRange(M,&rstart,NULL);
401: ISCreateStride(PETSC_COMM_WORLD,n/2,rstart,2,&isrow);
402: ISCreateStride(PETSC_COMM_WORLD,n/2,rstart+1,2,&iscol);
404: /* Extract M_0 from M */
405: MatGetSubMatrix(M,isrow,iscol,MAT_INITIAL_MATRIX,&user->M_0);
406: VecCreate(PETSC_COMM_WORLD,&user->u);
407: VecSetSizes(user->u,n/2,PETSC_DECIDE);
408: VecSetFromOptions(user->u);
409: VecDuplicate(user->u,&user->work1);
410: ISDestroy(&isrow);
411: ISDestroy(&iscol);
412: return(0);
413: }