Actual source code: ex42.c

petsc-3.7.5 2017-01-01
Report Typos and Errors
  1: static char help[] = "Solves the incompressible, variable viscosity stokes equation in 3d using Q1Q1 elements, \n\
  2: stabilized with Bochev's polynomial projection method. Note that implementation here assumes \n\
  3: all boundaries are free-slip, i.e. zero normal flow and zero tangential stress \n\
  4:      -mx : number elements in x-direction \n\
  5:      -my : number elements in y-direction \n\
  6:      -mz : number elements in z-direction \n\
  7:      -stokes_ksp_monitor_blocks : active monitor for each component u,v,w,p \n\
  8:      -model : defines viscosity and forcing function. Choose either: 0 (isoviscous), 1 (manufactured-broken), 2 (solcx-2d), 3 (sinker) \n";

 10: /* Contributed by Dave May */

 12: #include <petscksp.h>
 13: #include <petscdmda.h>

 15: #define PROFILE_TIMING
 16: #define ASSEMBLE_LOWER_TRIANGULAR

 18: #define NSD            3 /* number of spatial dimensions */
 19: #define NODES_PER_EL   8 /* nodes per element */
 20: #define U_DOFS         3 /* degrees of freedom per velocity node */
 21: #define P_DOFS         1 /* degrees of freedom per pressure node */
 22: #define GAUSS_POINTS   8

 24: /* Gauss point based evaluation */
 25: typedef struct {
 26:   PetscScalar gp_coords[NSD*GAUSS_POINTS];
 27:   PetscScalar eta[GAUSS_POINTS];
 28:   PetscScalar fx[GAUSS_POINTS];
 29:   PetscScalar fy[GAUSS_POINTS];
 30:   PetscScalar fz[GAUSS_POINTS];
 31:   PetscScalar hc[GAUSS_POINTS];
 32: } GaussPointCoefficients;

 34: typedef struct {
 35:   PetscScalar u_dof;
 36:   PetscScalar v_dof;
 37:   PetscScalar w_dof;
 38:   PetscScalar p_dof;
 39: } StokesDOF;

 41: typedef struct _p_CellProperties *CellProperties;
 42: struct _p_CellProperties {
 43:   PetscInt               ncells;
 44:   PetscInt               mx,my,mz;
 45:   PetscInt               sex,sey,sez;
 46:   GaussPointCoefficients *gpc;
 47: };

 49: static PetscErrorCode DMDAGetElementCorners(DM da,PetscInt *sx,PetscInt *sy,PetscInt *sz,PetscInt *mx,PetscInt *my,PetscInt *mz);

 51: /* elements */
 54: PetscErrorCode CellPropertiesCreate(DM da_stokes,CellProperties *C)
 55: {
 57:   CellProperties cells;
 58:   PetscInt       mx,my,mz,sex,sey,sez;

 61:   PetscMalloc(sizeof(struct _p_CellProperties),&cells);

 63:   DMDAGetElementCorners(da_stokes,&sex,&sey,&sez,&mx,&my,&mz);

 65:   cells->mx     = mx;
 66:   cells->my     = my;
 67:   cells->mz     = mz;
 68:   cells->ncells = mx * my * mz;
 69:   cells->sex    = sex;
 70:   cells->sey    = sey;
 71:   cells->sez    = sez;

 73:   PetscMalloc1(mx*my*mz,&cells->gpc);

 75:   *C = cells;
 76:   return(0);
 77: }

 81: PetscErrorCode CellPropertiesDestroy(CellProperties *C)
 82: {
 84:   CellProperties cells;

 87:   if (!C) return(0);
 88:   cells = *C;
 89:   PetscFree(cells->gpc);
 90:   PetscFree(cells);
 91:   *C = NULL;
 92:   return(0);
 93: }

 97: PetscErrorCode CellPropertiesGetCell(CellProperties C,PetscInt II,PetscInt J,PetscInt K,GaussPointCoefficients **G)
 98: {
100:   *G = &C->gpc[(II-C->sex) + (J-C->sey)*C->mx + (K-C->sez)*C->mx*C->my];
101:   return(0);
102: }

104: /* FEM routines */
105: /*
106:  Element: Local basis function ordering
107:  1-----2
108:  |     |
109:  |     |
110:  0-----3
111:  */
112: static void ShapeFunctionQ13D_Evaluate(PetscScalar _xi[],PetscScalar Ni[])
113: {
114:   PetscReal xi   = PetscRealPart(_xi[0]);
115:   PetscReal eta  = PetscRealPart(_xi[1]);
116:   PetscReal zeta = PetscRealPart(_xi[2]);

118:   Ni[0] = 0.125 * (1.0 - xi) * (1.0 - eta) * (1.0 - zeta);
119:   Ni[1] = 0.125 * (1.0 - xi) * (1.0 + eta) * (1.0 - zeta);
120:   Ni[2] = 0.125 * (1.0 + xi) * (1.0 + eta) * (1.0 - zeta);
121:   Ni[3] = 0.125 * (1.0 + xi) * (1.0 - eta) * (1.0 - zeta);

123:   Ni[4] = 0.125 * (1.0 - xi) * (1.0 - eta) * (1.0 + zeta);
124:   Ni[5] = 0.125 * (1.0 - xi) * (1.0 + eta) * (1.0 + zeta);
125:   Ni[6] = 0.125 * (1.0 + xi) * (1.0 + eta) * (1.0 + zeta);
126:   Ni[7] = 0.125 * (1.0 + xi) * (1.0 - eta) * (1.0 + zeta);
127: }

129: static void ShapeFunctionQ13D_Evaluate_dxi(PetscScalar _xi[],PetscScalar GNi[][NODES_PER_EL])
130: {
131:   PetscReal xi   = PetscRealPart(_xi[0]);
132:   PetscReal eta  = PetscRealPart(_xi[1]);
133:   PetscReal zeta = PetscRealPart(_xi[2]);
134:   /* xi deriv */
135:   GNi[0][0] = -0.125 * (1.0 - eta) * (1.0 - zeta);
136:   GNi[0][1] = -0.125 * (1.0 + eta) * (1.0 - zeta);
137:   GNi[0][2] =  0.125 * (1.0 + eta) * (1.0 - zeta);
138:   GNi[0][3] =  0.125 * (1.0 - eta) * (1.0 - zeta);

140:   GNi[0][4] = -0.125 * (1.0 - eta) * (1.0 + zeta);
141:   GNi[0][5] = -0.125 * (1.0 + eta) * (1.0 + zeta);
142:   GNi[0][6] =  0.125 * (1.0 + eta) * (1.0 + zeta);
143:   GNi[0][7] =  0.125 * (1.0 - eta) * (1.0 + zeta);
144:   /* eta deriv */
145:   GNi[1][0] = -0.125 * (1.0 - xi) * (1.0 - zeta);
146:   GNi[1][1] =  0.125 * (1.0 - xi) * (1.0 - zeta);
147:   GNi[1][2] =  0.125 * (1.0 + xi) * (1.0 - zeta);
148:   GNi[1][3] = -0.125 * (1.0 + xi) * (1.0 - zeta);

150:   GNi[1][4] = -0.125 * (1.0 - xi) * (1.0 + zeta);
151:   GNi[1][5] =  0.125 * (1.0 - xi) * (1.0 + zeta);
152:   GNi[1][6] =  0.125 * (1.0 + xi) * (1.0 + zeta);
153:   GNi[1][7] = -0.125 * (1.0 + xi) * (1.0 + zeta);
154:   /* zeta deriv */
155:   GNi[2][0] = -0.125 * (1.0 - xi) * (1.0 - eta);
156:   GNi[2][1] = -0.125 * (1.0 - xi) * (1.0 + eta);
157:   GNi[2][2] = -0.125 * (1.0 + xi) * (1.0 + eta);
158:   GNi[2][3] = -0.125 * (1.0 + xi) * (1.0 - eta);

160:   GNi[2][4] = 0.125 * (1.0 - xi) * (1.0 - eta);
161:   GNi[2][5] = 0.125 * (1.0 - xi) * (1.0 + eta);
162:   GNi[2][6] = 0.125 * (1.0 + xi) * (1.0 + eta);
163:   GNi[2][7] = 0.125 * (1.0 + xi) * (1.0 - eta);
164: }

166: static void matrix_inverse_3x3(PetscScalar A[3][3],PetscScalar B[3][3])
167: {
168:   PetscScalar t4, t6, t8, t10, t12, t14, t17;

170:   t4  = A[2][0] * A[0][1];
171:   t6  = A[2][0] * A[0][2];
172:   t8  = A[1][0] * A[0][1];
173:   t10 = A[1][0] * A[0][2];
174:   t12 = A[0][0] * A[1][1];
175:   t14 = A[0][0] * A[1][2];
176:   t17 = 0.1e1 / (t4 * A[1][2] - t6 * A[1][1] - t8 * A[2][2] + t10 * A[2][1] + t12 * A[2][2] - t14 * A[2][1]);

178:   B[0][0] = (A[1][1] * A[2][2] - A[1][2] * A[2][1]) * t17;
179:   B[0][1] = -(A[0][1] * A[2][2] - A[0][2] * A[2][1]) * t17;
180:   B[0][2] = (A[0][1] * A[1][2] - A[0][2] * A[1][1]) * t17;
181:   B[1][0] = -(-A[2][0] * A[1][2] + A[1][0] * A[2][2]) * t17;
182:   B[1][1] = (-t6 + A[0][0] * A[2][2]) * t17;
183:   B[1][2] = -(-t10 + t14) * t17;
184:   B[2][0] = (-A[2][0] * A[1][1] + A[1][0] * A[2][1]) * t17;
185:   B[2][1] = -(-t4 + A[0][0] * A[2][1]) * t17;
186:   B[2][2] = (-t8 + t12) * t17;
187: }

189: static void ShapeFunctionQ13D_Evaluate_dx(PetscScalar GNi[][NODES_PER_EL],PetscScalar GNx[][NODES_PER_EL],PetscScalar coords[],PetscScalar *det_J)
190: {
191:   PetscScalar J00,J01,J02,J10,J11,J12,J20,J21,J22;
192:   PetscInt    n;
193:   PetscScalar iJ[3][3],JJ[3][3];

195:   J00 = J01 = J02 = 0.0;
196:   J10 = J11 = J12 = 0.0;
197:   J20 = J21 = J22 = 0.0;
198:   for (n=0; n<NODES_PER_EL; n++) {
199:     PetscScalar cx = coords[NSD*n + 0];
200:     PetscScalar cy = coords[NSD*n + 1];
201:     PetscScalar cz = coords[NSD*n + 2];

203:     /* J_ij = d(x_j) / d(xi_i) */ /* J_ij = \sum _I GNi[j][I} * x_i */
204:     J00 = J00 + GNi[0][n] * cx;   /* J_xx */
205:     J01 = J01 + GNi[0][n] * cy;   /* J_xy = dx/deta */
206:     J02 = J02 + GNi[0][n] * cz;   /* J_xz = dx/dzeta */

208:     J10 = J10 + GNi[1][n] * cx;   /* J_yx = dy/dxi */
209:     J11 = J11 + GNi[1][n] * cy;   /* J_yy */
210:     J12 = J12 + GNi[1][n] * cz;   /* J_yz */

212:     J20 = J20 + GNi[2][n] * cx;   /* J_zx */
213:     J21 = J21 + GNi[2][n] * cy;   /* J_zy */
214:     J22 = J22 + GNi[2][n] * cz;   /* J_zz */
215:   }

217:   JJ[0][0] = J00;      JJ[0][1] = J01;      JJ[0][2] = J02;
218:   JJ[1][0] = J10;      JJ[1][1] = J11;      JJ[1][2] = J12;
219:   JJ[2][0] = J20;      JJ[2][1] = J21;      JJ[2][2] = J22;

221:   matrix_inverse_3x3(JJ,iJ);

223:   *det_J = J00*J11*J22 - J00*J12*J21 - J10*J01*J22 + J10*J02*J21 + J20*J01*J12 - J20*J02*J11;

225:   for (n=0; n<NODES_PER_EL; n++) {
226:     GNx[0][n] = GNi[0][n]*iJ[0][0] + GNi[1][n]*iJ[0][1] + GNi[2][n]*iJ[0][2];
227:     GNx[1][n] = GNi[0][n]*iJ[1][0] + GNi[1][n]*iJ[1][1] + GNi[2][n]*iJ[1][2];
228:     GNx[2][n] = GNi[0][n]*iJ[2][0] + GNi[1][n]*iJ[2][1] + GNi[2][n]*iJ[2][2];
229:   }
230: }

232: static void ConstructGaussQuadrature3D(PetscInt *ngp,PetscScalar gp_xi[][NSD],PetscScalar gp_weight[])
233: {
234:   *ngp        = 8;
235:   gp_xi[0][0] = -0.57735026919; gp_xi[0][1] = -0.57735026919; gp_xi[0][2] = -0.57735026919;
236:   gp_xi[1][0] = -0.57735026919; gp_xi[1][1] =  0.57735026919; gp_xi[1][2] = -0.57735026919;
237:   gp_xi[2][0] =  0.57735026919; gp_xi[2][1] =  0.57735026919; gp_xi[2][2] = -0.57735026919;
238:   gp_xi[3][0] =  0.57735026919; gp_xi[3][1] = -0.57735026919; gp_xi[3][2] = -0.57735026919;

240:   gp_xi[4][0] = -0.57735026919; gp_xi[4][1] = -0.57735026919; gp_xi[4][2] =  0.57735026919;
241:   gp_xi[5][0] = -0.57735026919; gp_xi[5][1] =  0.57735026919; gp_xi[5][2] =  0.57735026919;
242:   gp_xi[6][0] =  0.57735026919; gp_xi[6][1] =  0.57735026919; gp_xi[6][2] =  0.57735026919;
243:   gp_xi[7][0] =  0.57735026919; gp_xi[7][1] = -0.57735026919; gp_xi[7][2] =  0.57735026919;

245:   gp_weight[0] = 1.0;
246:   gp_weight[1] = 1.0;
247:   gp_weight[2] = 1.0;
248:   gp_weight[3] = 1.0;

250:   gp_weight[4] = 1.0;
251:   gp_weight[5] = 1.0;
252:   gp_weight[6] = 1.0;
253:   gp_weight[7] = 1.0;
254: }

256: /* procs to the left claim the ghost node as their element */
259: static PetscErrorCode DMDAGetLocalElementSize(DM da,PetscInt *mxl,PetscInt *myl,PetscInt *mzl)
260: {
261:   PetscInt       m,n,p,M,N,P;
262:   PetscInt       sx,sy,sz;

266:   DMDAGetInfo(da,0,&M,&N,&P,0,0,0,0,0,0,0,0,0);
267:   DMDAGetCorners(da,&sx,&sy,&sz,&m,&n,&p);

269:   if (mxl) {
270:     *mxl = m;
271:     if ((sx+m) == M) *mxl = m-1;  /* last proc */
272:   }
273:   if (myl) {
274:     *myl = n;
275:     if ((sy+n) == N) *myl = n-1;  /* last proc */
276:   }
277:   if (mzl) {
278:     *mzl = p;
279:     if ((sz+p) == P) *mzl = p-1;  /* last proc */
280:   }
281:   return(0);
282: }

286: static PetscErrorCode DMDAGetElementCorners(DM da,PetscInt *sx,PetscInt *sy,PetscInt *sz,PetscInt *mx,PetscInt *my,PetscInt *mz)
287: {
288:   PetscInt       si,sj,sk;

292:   DMDAGetGhostCorners(da,&si,&sj,&sk,0,0,0);

294:   if (sx) {
295:     *sx = si;
296:     if (si != 0) *sx = si+1;
297:   }
298:   if (sy) {
299:     *sy = sj;
300:     if (sj != 0) *sy = sj+1;
301:   }
302:   if (sz) {
303:     *sz = sk;
304:     if (sk != 0) *sz = sk+1;
305:   }
306:   DMDAGetLocalElementSize(da,mx,my,mz);
307:   return(0);
308: }

310: /*
311:  i,j are the element indices
312:  The unknown is a vector quantity.
313:  The s[].c is used to indicate the degree of freedom.
314:  */
317: static PetscErrorCode DMDAGetElementEqnums3D_up(MatStencil s_u[],MatStencil s_p[],PetscInt i,PetscInt j,PetscInt k)
318: {
319:   PetscInt n;

322:   /* velocity */
323:   n = 0;
324:   /* node 0 */
325:   s_u[n].i = i; s_u[n].j = j; s_u[n].k = k; s_u[n].c = 0; n++; /* Vx0 */
326:   s_u[n].i = i; s_u[n].j = j; s_u[n].k = k; s_u[n].c = 1; n++; /* Vy0 */
327:   s_u[n].i = i; s_u[n].j = j; s_u[n].k = k; s_u[n].c = 2; n++; /* Vz0 */

329:   s_u[n].i = i; s_u[n].j = j+1; s_u[n].k = k; s_u[n].c = 0; n++;
330:   s_u[n].i = i; s_u[n].j = j+1; s_u[n].k = k; s_u[n].c = 1; n++;
331:   s_u[n].i = i; s_u[n].j = j+1; s_u[n].k = k; s_u[n].c = 2; n++;

333:   s_u[n].i = i+1; s_u[n].j = j+1; s_u[n].k = k; s_u[n].c = 0; n++;
334:   s_u[n].i = i+1; s_u[n].j = j+1; s_u[n].k = k; s_u[n].c = 1; n++;
335:   s_u[n].i = i+1; s_u[n].j = j+1; s_u[n].k = k; s_u[n].c = 2; n++;

337:   s_u[n].i = i+1; s_u[n].j = j; s_u[n].k = k; s_u[n].c = 0; n++;
338:   s_u[n].i = i+1; s_u[n].j = j; s_u[n].k = k; s_u[n].c = 1; n++;
339:   s_u[n].i = i+1; s_u[n].j = j; s_u[n].k = k; s_u[n].c = 2; n++;

341:   /* */
342:   s_u[n].i = i; s_u[n].j = j; s_u[n].k = k+1; s_u[n].c = 0; n++; /* Vx4 */
343:   s_u[n].i = i; s_u[n].j = j; s_u[n].k = k+1; s_u[n].c = 1; n++; /* Vy4 */
344:   s_u[n].i = i; s_u[n].j = j; s_u[n].k = k+1; s_u[n].c = 2; n++; /* Vz4 */

346:   s_u[n].i = i; s_u[n].j = j+1; s_u[n].k = k+1; s_u[n].c = 0; n++;
347:   s_u[n].i = i; s_u[n].j = j+1; s_u[n].k = k+1; s_u[n].c = 1; n++;
348:   s_u[n].i = i; s_u[n].j = j+1; s_u[n].k = k+1; s_u[n].c = 2; n++;

350:   s_u[n].i = i+1; s_u[n].j = j+1; s_u[n].k = k+1; s_u[n].c = 0; n++;
351:   s_u[n].i = i+1; s_u[n].j = j+1; s_u[n].k = k+1; s_u[n].c = 1; n++;
352:   s_u[n].i = i+1; s_u[n].j = j+1; s_u[n].k = k+1; s_u[n].c = 2; n++;

354:   s_u[n].i = i+1; s_u[n].j = j; s_u[n].k = k+1; s_u[n].c = 0; n++;
355:   s_u[n].i = i+1; s_u[n].j = j; s_u[n].k = k+1; s_u[n].c = 1; n++;
356:   s_u[n].i = i+1; s_u[n].j = j; s_u[n].k = k+1; s_u[n].c = 2; n++;

358:   /* pressure */
359:   n = 0;

361:   s_p[n].i = i;   s_p[n].j = j;   s_p[n].k = k; s_p[n].c = 3; n++; /* P0 */
362:   s_p[n].i = i;   s_p[n].j = j+1; s_p[n].k = k; s_p[n].c = 3; n++;
363:   s_p[n].i = i+1; s_p[n].j = j+1; s_p[n].k = k; s_p[n].c = 3; n++;
364:   s_p[n].i = i+1; s_p[n].j = j;   s_p[n].k = k; s_p[n].c = 3; n++;

366:   s_p[n].i = i;   s_p[n].j = j;   s_p[n].k = k+1; s_p[n].c = 3; n++; /* P0 */
367:   s_p[n].i = i;   s_p[n].j = j+1; s_p[n].k = k+1; s_p[n].c = 3; n++;
368:   s_p[n].i = i+1; s_p[n].j = j+1; s_p[n].k = k+1; s_p[n].c = 3; n++;
369:   s_p[n].i = i+1; s_p[n].j = j;   s_p[n].k = k+1; s_p[n].c = 3; n++;
370:   return(0);
371: }

375: static PetscErrorCode GetElementCoords3D(DMDACoor3d ***coords,PetscInt i,PetscInt j,PetscInt k,PetscScalar el_coord[])
376: {
378:   /* get coords for the element */
379:   el_coord[0] = coords[k][j][i].x;
380:   el_coord[1] = coords[k][j][i].y;
381:   el_coord[2] = coords[k][j][i].z;

383:   el_coord[3] = coords[k][j+1][i].x;
384:   el_coord[4] = coords[k][j+1][i].y;
385:   el_coord[5] = coords[k][j+1][i].z;

387:   el_coord[6] = coords[k][j+1][i+1].x;
388:   el_coord[7] = coords[k][j+1][i+1].y;
389:   el_coord[8] = coords[k][j+1][i+1].z;

391:   el_coord[9]  = coords[k][j][i+1].x;
392:   el_coord[10] = coords[k][j][i+1].y;
393:   el_coord[11] = coords[k][j][i+1].z;

395:   el_coord[12] = coords[k+1][j][i].x;
396:   el_coord[13] = coords[k+1][j][i].y;
397:   el_coord[14] = coords[k+1][j][i].z;

399:   el_coord[15] = coords[k+1][j+1][i].x;
400:   el_coord[16] = coords[k+1][j+1][i].y;
401:   el_coord[17] = coords[k+1][j+1][i].z;

403:   el_coord[18] = coords[k+1][j+1][i+1].x;
404:   el_coord[19] = coords[k+1][j+1][i+1].y;
405:   el_coord[20] = coords[k+1][j+1][i+1].z;

407:   el_coord[21] = coords[k+1][j][i+1].x;
408:   el_coord[22] = coords[k+1][j][i+1].y;
409:   el_coord[23] = coords[k+1][j][i+1].z;
410:   return(0);
411: }

415: static PetscErrorCode StokesDAGetNodalFields3D(StokesDOF ***field,PetscInt i,PetscInt j,PetscInt k,StokesDOF nodal_fields[])
416: {
418:   /* get the nodal fields for u */
419:   nodal_fields[0].u_dof = field[k][j][i].u_dof;
420:   nodal_fields[0].v_dof = field[k][j][i].v_dof;
421:   nodal_fields[0].w_dof = field[k][j][i].w_dof;

423:   nodal_fields[1].u_dof = field[k][j+1][i].u_dof;
424:   nodal_fields[1].v_dof = field[k][j+1][i].v_dof;
425:   nodal_fields[1].w_dof = field[k][j+1][i].w_dof;

427:   nodal_fields[2].u_dof = field[k][j+1][i+1].u_dof;
428:   nodal_fields[2].v_dof = field[k][j+1][i+1].v_dof;
429:   nodal_fields[2].w_dof = field[k][j+1][i+1].w_dof;

431:   nodal_fields[3].u_dof = field[k][j][i+1].u_dof;
432:   nodal_fields[3].v_dof = field[k][j][i+1].v_dof;
433:   nodal_fields[3].w_dof = field[k][j][i+1].w_dof;

435:   nodal_fields[4].u_dof = field[k+1][j][i].u_dof;
436:   nodal_fields[4].v_dof = field[k+1][j][i].v_dof;
437:   nodal_fields[4].w_dof = field[k+1][j][i].w_dof;

439:   nodal_fields[5].u_dof = field[k+1][j+1][i].u_dof;
440:   nodal_fields[5].v_dof = field[k+1][j+1][i].v_dof;
441:   nodal_fields[5].w_dof = field[k+1][j+1][i].w_dof;

443:   nodal_fields[6].u_dof = field[k+1][j+1][i+1].u_dof;
444:   nodal_fields[6].v_dof = field[k+1][j+1][i+1].v_dof;
445:   nodal_fields[6].w_dof = field[k+1][j+1][i+1].w_dof;

447:   nodal_fields[7].u_dof = field[k+1][j][i+1].u_dof;
448:   nodal_fields[7].v_dof = field[k+1][j][i+1].v_dof;
449:   nodal_fields[7].w_dof = field[k+1][j][i+1].w_dof;

451:   /* get the nodal fields for p */
452:   nodal_fields[0].p_dof = field[k][j][i].p_dof;
453:   nodal_fields[1].p_dof = field[k][j+1][i].p_dof;
454:   nodal_fields[2].p_dof = field[k][j+1][i+1].p_dof;
455:   nodal_fields[3].p_dof = field[k][j][i+1].p_dof;

457:   nodal_fields[4].p_dof = field[k+1][j][i].p_dof;
458:   nodal_fields[5].p_dof = field[k+1][j+1][i].p_dof;
459:   nodal_fields[6].p_dof = field[k+1][j+1][i+1].p_dof;
460:   nodal_fields[7].p_dof = field[k+1][j][i+1].p_dof;
461:   return(0);
462: }

464: static PetscInt ASS_MAP_wIwDI_uJuDJ(PetscInt wi,PetscInt wd,PetscInt w_NPE,PetscInt w_dof,PetscInt ui,PetscInt ud,PetscInt u_NPE,PetscInt u_dof)
465: {
466:   PetscInt              ij;
467:   PETSC_UNUSED PetscInt r,c,nr,nc;

469:   nr = w_NPE*w_dof;
470:   nc = u_NPE*u_dof;

472:   r = w_dof*wi+wd;
473:   c = u_dof*ui+ud;

475:   ij = r*nc+c;

477:   return ij;
478: }

482: static PetscErrorCode DMDASetValuesLocalStencil3D_ADD_VALUES(StokesDOF ***fields_F,MatStencil u_eqn[],MatStencil p_eqn[],PetscScalar Fe_u[],PetscScalar Fe_p[])
483: {
484:   PetscInt n,II,J,K;

487:   for (n = 0; n<NODES_PER_EL; n++) {
488:     II = u_eqn[NSD*n].i;
489:     J = u_eqn[NSD*n].j;
490:     K = u_eqn[NSD*n].k;

492:     fields_F[K][J][II].u_dof = fields_F[K][J][II].u_dof+Fe_u[NSD*n];

494:     II = u_eqn[NSD*n+1].i;
495:     J = u_eqn[NSD*n+1].j;
496:     K = u_eqn[NSD*n+1].k;

498:     fields_F[K][J][II].v_dof = fields_F[K][J][II].v_dof+Fe_u[NSD*n+1];

500:     II = u_eqn[NSD*n+2].i;
501:     J = u_eqn[NSD*n+2].j;
502:     K = u_eqn[NSD*n+2].k;
503:     fields_F[K][J][II].w_dof = fields_F[K][J][II].w_dof+Fe_u[NSD*n+2];

505:     II = p_eqn[n].i;
506:     J = p_eqn[n].j;
507:     K = p_eqn[n].k;

509:     fields_F[K][J][II].p_dof = fields_F[K][J][II].p_dof+Fe_p[n];

511:   }
512:   return(0);
513: }

515: static void FormStressOperatorQ13D(PetscScalar Ke[],PetscScalar coords[],PetscScalar eta[])
516: {
517:   PetscInt       ngp;
518:   PetscScalar    gp_xi[GAUSS_POINTS][NSD];
519:   PetscScalar    gp_weight[GAUSS_POINTS];
520:   PetscInt       p,i,j,k;
521:   PetscScalar    GNi_p[NSD][NODES_PER_EL],GNx_p[NSD][NODES_PER_EL];
522:   PetscScalar    J_p,tildeD[6];
523:   PetscScalar    B[6][U_DOFS*NODES_PER_EL];
524:   const PetscInt nvdof = U_DOFS*NODES_PER_EL;

526:   /* define quadrature rule */
527:   ConstructGaussQuadrature3D(&ngp,gp_xi,gp_weight);

529:   /* evaluate integral */
530:   for (p = 0; p < ngp; p++) {
531:     ShapeFunctionQ13D_Evaluate_dxi(gp_xi[p],GNi_p);
532:     ShapeFunctionQ13D_Evaluate_dx(GNi_p,GNx_p,coords,&J_p);

534:     for (i = 0; i < NODES_PER_EL; i++) {
535:       PetscScalar d_dx_i = GNx_p[0][i];
536:       PetscScalar d_dy_i = GNx_p[1][i];
537:       PetscScalar d_dz_i = GNx_p[2][i];

539:       B[0][3*i] = d_dx_i; B[0][3*i+1] = 0.0;     B[0][3*i+2] = 0.0;
540:       B[1][3*i] = 0.0;    B[1][3*i+1] = d_dy_i;  B[1][3*i+2] = 0.0;
541:       B[2][3*i] = 0.0;    B[2][3*i+1] = 0.0;     B[2][3*i+2] = d_dz_i;

543:       B[3][3*i] = d_dy_i; B[3][3*i+1] = d_dx_i;  B[3][3*i+2] = 0.0;   /* e_xy */
544:       B[4][3*i] = d_dz_i; B[4][3*i+1] = 0.0;     B[4][3*i+2] = d_dx_i; /* e_xz */
545:       B[5][3*i] = 0.0;    B[5][3*i+1] = d_dz_i;  B[5][3*i+2] = d_dy_i; /* e_yz */
546:     }


549:     tildeD[0] = 2.0*gp_weight[p]*J_p*eta[p];
550:     tildeD[1] = 2.0*gp_weight[p]*J_p*eta[p];
551:     tildeD[2] = 2.0*gp_weight[p]*J_p*eta[p];

553:     tildeD[3] =     gp_weight[p]*J_p*eta[p];
554:     tildeD[4] =     gp_weight[p]*J_p*eta[p];
555:     tildeD[5] =     gp_weight[p]*J_p*eta[p];

557:     /* form Bt tildeD B */
558:     /*
559:      Ke_ij = Bt_ik . D_kl . B_lj
560:      = B_ki . D_kl . B_lj
561:      = B_ki . D_kk . B_kj
562:      */
563:     for (i = 0; i < nvdof; i++) {
564:       for (j = i; j < nvdof; j++) {
565:         for (k = 0; k < 6; k++) {
566:           Ke[i*nvdof+j] += B[k][i]*tildeD[k]*B[k][j];
567:         }
568:       }
569:     }

571:   }
572:   /* fill lower triangular part */
573: #if defined(ASSEMBLE_LOWER_TRIANGULAR)
574:   for (i = 0; i < nvdof; i++) {
575:     for (j = i; j < nvdof; j++) {
576:       Ke[j*nvdof+i] = Ke[i*nvdof+j];
577:     }
578:   }
579: #endif
580: }

582: static void FormGradientOperatorQ13D(PetscScalar Ke[],PetscScalar coords[])
583: {
584:   PetscInt    ngp;
585:   PetscScalar gp_xi[GAUSS_POINTS][NSD];
586:   PetscScalar gp_weight[GAUSS_POINTS];
587:   PetscInt    p,i,j,di;
588:   PetscScalar Ni_p[NODES_PER_EL];
589:   PetscScalar GNi_p[NSD][NODES_PER_EL],GNx_p[NSD][NODES_PER_EL];
590:   PetscScalar J_p,fac;

592:   /* define quadrature rule */
593:   ConstructGaussQuadrature3D(&ngp,gp_xi,gp_weight);

595:   /* evaluate integral */
596:   for (p = 0; p < ngp; p++) {
597:     ShapeFunctionQ13D_Evaluate(gp_xi[p],Ni_p);
598:     ShapeFunctionQ13D_Evaluate_dxi(gp_xi[p],GNi_p);
599:     ShapeFunctionQ13D_Evaluate_dx(GNi_p,GNx_p,coords,&J_p);
600:     fac = gp_weight[p]*J_p;

602:     for (i = 0; i < NODES_PER_EL; i++) { /* u nodes */
603:       for (di = 0; di < NSD; di++) { /* u dofs */
604:         for (j = 0; j < NODES_PER_EL; j++) {  /* p nodes, p dofs = 1 (ie no loop) */
605:           PetscInt IJ;
606:           IJ = ASS_MAP_wIwDI_uJuDJ(i,di,NODES_PER_EL,3,j,0,NODES_PER_EL,1);

608:           Ke[IJ] = Ke[IJ]-GNx_p[di][i]*Ni_p[j]*fac;
609:         }
610:       }
611:     }
612:   }
613: }

615: static void FormDivergenceOperatorQ13D(PetscScalar De[],PetscScalar coords[])
616: {
617:   PetscScalar Ge[U_DOFS*NODES_PER_EL*P_DOFS*NODES_PER_EL];
618:   PetscInt    i,j;
619:   PetscInt    nr_g,nc_g;

621:   PetscMemzero(Ge,sizeof(PetscScalar)*U_DOFS*NODES_PER_EL*P_DOFS*NODES_PER_EL);
622:   FormGradientOperatorQ13D(Ge,coords);

624:   nr_g = U_DOFS*NODES_PER_EL;
625:   nc_g = P_DOFS*NODES_PER_EL;

627:   for (i = 0; i < nr_g; i++) {
628:     for (j = 0; j < nc_g; j++) {
629:       De[nr_g*j+i] = Ge[nc_g*i+j];
630:     }
631:   }
632: }

634: static void FormStabilisationOperatorQ13D(PetscScalar Ke[],PetscScalar coords[],PetscScalar eta[])
635: {
636:   PetscInt    ngp;
637:   PetscScalar gp_xi[GAUSS_POINTS][NSD];
638:   PetscScalar gp_weight[GAUSS_POINTS];
639:   PetscInt    p,i,j;
640:   PetscScalar Ni_p[NODES_PER_EL];
641:   PetscScalar GNi_p[NSD][NODES_PER_EL],GNx_p[NSD][NODES_PER_EL];
642:   PetscScalar J_p,fac,eta_avg;

644:   /* define quadrature rule */
645:   ConstructGaussQuadrature3D(&ngp,gp_xi,gp_weight);

647:   /* evaluate integral */
648:   for (p = 0; p < ngp; p++) {
649:     ShapeFunctionQ13D_Evaluate(gp_xi[p],Ni_p);
650:     ShapeFunctionQ13D_Evaluate_dxi(gp_xi[p],GNi_p);
651:     ShapeFunctionQ13D_Evaluate_dx(GNi_p,GNx_p,coords,&J_p);
652:     fac = gp_weight[p]*J_p;
653:     /*
654:      for (i = 0; i < NODES_PER_EL; i++) {
655:      for (j = i; j < NODES_PER_EL; j++) {
656:      Ke[NODES_PER_EL*i+j] -= fac*(Ni_p[i]*Ni_p[j]-0.015625);
657:      }
658:      }
659:      */

661:     for (i = 0; i < NODES_PER_EL; i++) {
662:       for (j = 0; j < NODES_PER_EL; j++) {
663:         Ke[NODES_PER_EL*i+j] -= fac*(Ni_p[i]*Ni_p[j]-0.015625);
664:       }
665:     }
666:   }

668:   /* scale */
669:   eta_avg = 0.0;
670:   for (p = 0; p < ngp; p++) eta_avg += eta[p];
671:   eta_avg = (1.0/((PetscScalar)ngp))*eta_avg;
672:   fac     = 1.0/eta_avg;

674:   /*
675:    for (i = 0; i < NODES_PER_EL; i++) {
676:    for (j = i; j < NODES_PER_EL; j++) {
677:    Ke[NODES_PER_EL*i+j] = fac*Ke[NODES_PER_EL*i+j];
678:    #if defined(ASSEMBLE_LOWER_TRIANGULAR)
679:    Ke[NODES_PER_EL*j+i] = Ke[NODES_PER_EL*i+j];
680:    #endif
681:    }
682:    }
683:    */

685:   for (i = 0; i < NODES_PER_EL; i++) {
686:     for (j = 0; j < NODES_PER_EL; j++) {
687:       Ke[NODES_PER_EL*i+j] = fac*Ke[NODES_PER_EL*i+j];
688:     }
689:   }
690: }

692: static void FormScaledMassMatrixOperatorQ13D(PetscScalar Ke[],PetscScalar coords[],PetscScalar eta[])
693: {
694:   PetscInt    ngp;
695:   PetscScalar gp_xi[GAUSS_POINTS][NSD];
696:   PetscScalar gp_weight[GAUSS_POINTS];
697:   PetscInt    p,i,j;
698:   PetscScalar Ni_p[NODES_PER_EL];
699:   PetscScalar GNi_p[NSD][NODES_PER_EL],GNx_p[NSD][NODES_PER_EL];
700:   PetscScalar J_p,fac,eta_avg;

702:   /* define quadrature rule */
703:   ConstructGaussQuadrature3D(&ngp,gp_xi,gp_weight);

705:   /* evaluate integral */
706:   for (p = 0; p < ngp; p++) {
707:     ShapeFunctionQ13D_Evaluate(gp_xi[p],Ni_p);
708:     ShapeFunctionQ13D_Evaluate_dxi(gp_xi[p],GNi_p);
709:     ShapeFunctionQ13D_Evaluate_dx(GNi_p,GNx_p,coords,&J_p);
710:     fac = gp_weight[p]*J_p;

712:     /*
713:      for (i = 0; i < NODES_PER_EL; i++) {
714:      for (j = i; j < NODES_PER_EL; j++) {
715:      Ke[NODES_PER_EL*i+j] = Ke[NODES_PER_EL*i+j]-fac*Ni_p[i]*Ni_p[j];
716:      }
717:      }
718:      */

720:     for (i = 0; i < NODES_PER_EL; i++) {
721:       for (j = 0; j < NODES_PER_EL; j++) {
722:         Ke[NODES_PER_EL*i+j] = Ke[NODES_PER_EL*i+j]-fac*Ni_p[i]*Ni_p[j];
723:       }
724:     }
725:   }

727:   /* scale */
728:   eta_avg = 0.0;
729:   for (p = 0; p < ngp; p++) eta_avg += eta[p];
730:   eta_avg = (1.0/((PetscScalar)ngp))*eta_avg;
731:   fac     = 1.0/eta_avg;
732:   /*
733:    for (i = 0; i < NODES_PER_EL; i++) {
734:    for (j = i; j < NODES_PER_EL; j++) {
735:    Ke[NODES_PER_EL*i+j] = fac*Ke[NODES_PER_EL*i+j];
736:    #if defined(ASSEMBLE_LOWER_TRIANGULAR)
737:    Ke[NODES_PER_EL*j+i] = Ke[NODES_PER_EL*i+j];
738:    #endif
739:    }
740:    }
741:    */

743:   for (i = 0; i < NODES_PER_EL; i++) {
744:     for (j = 0; j < NODES_PER_EL; j++) {
745:       Ke[NODES_PER_EL*i+j] = fac*Ke[NODES_PER_EL*i+j];
746:     }
747:   }
748: }

750: static void FormMomentumRhsQ13D(PetscScalar Fe[],PetscScalar coords[],PetscScalar fx[],PetscScalar fy[],PetscScalar fz[])
751: {
752:   PetscInt    ngp;
753:   PetscScalar gp_xi[GAUSS_POINTS][NSD];
754:   PetscScalar gp_weight[GAUSS_POINTS];
755:   PetscInt    p,i;
756:   PetscScalar Ni_p[NODES_PER_EL];
757:   PetscScalar GNi_p[NSD][NODES_PER_EL],GNx_p[NSD][NODES_PER_EL];
758:   PetscScalar J_p,fac;

760:   /* define quadrature rule */
761:   ConstructGaussQuadrature3D(&ngp,gp_xi,gp_weight);

763:   /* evaluate integral */
764:   for (p = 0; p < ngp; p++) {
765:     ShapeFunctionQ13D_Evaluate(gp_xi[p],Ni_p);
766:     ShapeFunctionQ13D_Evaluate_dxi(gp_xi[p],GNi_p);
767:     ShapeFunctionQ13D_Evaluate_dx(GNi_p,GNx_p,coords,&J_p);
768:     fac = gp_weight[p]*J_p;

770:     for (i = 0; i < NODES_PER_EL; i++) {
771:       Fe[NSD*i]   -= fac*Ni_p[i]*fx[p];
772:       Fe[NSD*i+1] -= fac*Ni_p[i]*fy[p];
773:       Fe[NSD*i+2] -= fac*Ni_p[i]*fz[p];
774:     }
775:   }
776: }

778: static void FormContinuityRhsQ13D(PetscScalar Fe[],PetscScalar coords[],PetscScalar hc[])
779: {
780:   PetscInt    ngp;
781:   PetscScalar gp_xi[GAUSS_POINTS][NSD];
782:   PetscScalar gp_weight[GAUSS_POINTS];
783:   PetscInt    p,i;
784:   PetscScalar Ni_p[NODES_PER_EL];
785:   PetscScalar GNi_p[NSD][NODES_PER_EL],GNx_p[NSD][NODES_PER_EL];
786:   PetscScalar J_p,fac;

788:   /* define quadrature rule */
789:   ConstructGaussQuadrature3D(&ngp,gp_xi,gp_weight);

791:   /* evaluate integral */
792:   for (p = 0; p < ngp; p++) {
793:     ShapeFunctionQ13D_Evaluate(gp_xi[p],Ni_p);
794:     ShapeFunctionQ13D_Evaluate_dxi(gp_xi[p],GNi_p);
795:     ShapeFunctionQ13D_Evaluate_dx(GNi_p,GNx_p,coords,&J_p);
796:     fac = gp_weight[p]*J_p;

798:     for (i = 0; i < NODES_PER_EL; i++) Fe[i] -= fac*Ni_p[i]*hc[p];
799:   }
800: }

802: #define _ZERO_ROWCOL_i(A,i) {                   \
803:     PetscInt    KK;                             \
804:     PetscScalar tmp = A[24*(i)+(i)];            \
805:     for (KK=0;KK<24;KK++) A[24*(i)+KK]=0.0;     \
806:     for (KK=0;KK<24;KK++) A[24*KK+(i)]=0.0;     \
807:     A[24*(i)+(i)] = tmp;}                       \

809: #define _ZERO_ROW_i(A,i) {                      \
810:     PetscInt KK;                                \
811:     for (KK=0;KK<8;KK++) A[8*(i)+KK]=0.0;}

813: #define _ZERO_COL_i(A,i) {                      \
814:     PetscInt KK;                                \
815:     for (KK=0;KK<8;KK++) A[24*KK+(i)]=0.0;}

819: static PetscErrorCode AssembleA_Stokes(Mat A,DM stokes_da,CellProperties cell_properties)
820: {
821:   DM                     cda;
822:   Vec                    coords;
823:   DMDACoor3d             ***_coords;
824:   MatStencil             u_eqn[NODES_PER_EL*U_DOFS];
825:   MatStencil             p_eqn[NODES_PER_EL*P_DOFS];
826:   PetscInt               sex,sey,sez,mx,my,mz;
827:   PetscInt               ei,ej,ek;
828:   PetscScalar            Ae[NODES_PER_EL*U_DOFS*NODES_PER_EL*U_DOFS];
829:   PetscScalar            Ge[NODES_PER_EL*U_DOFS*NODES_PER_EL*P_DOFS];
830:   PetscScalar            De[NODES_PER_EL*P_DOFS*NODES_PER_EL*U_DOFS];
831:   PetscScalar            Ce[NODES_PER_EL*P_DOFS*NODES_PER_EL*P_DOFS];
832:   PetscScalar            el_coords[NODES_PER_EL*NSD];
833:   GaussPointCoefficients *props;
834:   PetscScalar            *prop_eta;
835:   PetscInt               n,M,N,P;
836:   PetscErrorCode         ierr;

839:   DMDAGetInfo(stokes_da,0,&M,&N,&P,0,0,0, 0,0,0,0,0,0);
840:   /* setup for coords */
841:   DMGetCoordinateDM(stokes_da,&cda);
842:   DMGetCoordinatesLocal(stokes_da,&coords);
843:   DMDAVecGetArray(cda,coords,&_coords);

845:   DMDAGetElementCorners(stokes_da,&sex,&sey,&sez,&mx,&my,&mz);
846:   for (ek = sez; ek < sez+mz; ek++) {
847:     for (ej = sey; ej < sey+my; ej++) {
848:       for (ei = sex; ei < sex+mx; ei++) {
849:         /* get coords for the element */
850:         GetElementCoords3D(_coords,ei,ej,ek,el_coords);
851:         /* get cell properties */
852:         CellPropertiesGetCell(cell_properties,ei,ej,ek,&props);
853:         /* get coefficients for the element */
854:         prop_eta = props->eta;

856:         /* initialise element stiffness matrix */
857:         PetscMemzero(Ae,sizeof(PetscScalar)*NODES_PER_EL*U_DOFS*NODES_PER_EL*U_DOFS);
858:         PetscMemzero(Ge,sizeof(PetscScalar)*NODES_PER_EL*U_DOFS*NODES_PER_EL*P_DOFS);
859:         PetscMemzero(De,sizeof(PetscScalar)*NODES_PER_EL*P_DOFS*NODES_PER_EL*U_DOFS);
860:         PetscMemzero(Ce,sizeof(PetscScalar)*NODES_PER_EL*P_DOFS*NODES_PER_EL*P_DOFS);

862:         /* form element stiffness matrix */
863:         FormStressOperatorQ13D(Ae,el_coords,prop_eta);
864:         FormGradientOperatorQ13D(Ge,el_coords);
865:         /*#if defined(ASSEMBLE_LOWER_TRIANGULAR)*/
866:         FormDivergenceOperatorQ13D(De,el_coords);
867:         /*#endif*/
868:         FormStabilisationOperatorQ13D(Ce,el_coords,prop_eta);

870:         /* insert element matrix into global matrix */
871:         DMDAGetElementEqnums3D_up(u_eqn,p_eqn,ei,ej,ek);

873:         for (n=0; n<NODES_PER_EL; n++) {
874:           if ((u_eqn[3*n].i == 0) || (u_eqn[3*n].i == M-1)) {
875:             _ZERO_ROWCOL_i(Ae,3*n);
876:             _ZERO_ROW_i   (Ge,3*n);
877:             _ZERO_COL_i   (De,3*n);
878:           }

880:           if ((u_eqn[3*n+1].j == 0) || (u_eqn[3*n+1].j == N-1)) {
881:             _ZERO_ROWCOL_i(Ae,3*n+1);
882:             _ZERO_ROW_i   (Ge,3*n+1);
883:             _ZERO_COL_i   (De,3*n+1);
884:           }

886:           if ((u_eqn[3*n+2].k == 0) || (u_eqn[3*n+2].k == P-1)) {
887:             _ZERO_ROWCOL_i(Ae,3*n+2);
888:             _ZERO_ROW_i   (Ge,3*n+2);
889:             _ZERO_COL_i   (De,3*n+2);
890:           }
891:         }
892:         MatSetValuesStencil(A,NODES_PER_EL*U_DOFS,u_eqn,NODES_PER_EL*U_DOFS,u_eqn,Ae,ADD_VALUES);
893:         MatSetValuesStencil(A,NODES_PER_EL*U_DOFS,u_eqn,NODES_PER_EL*P_DOFS,p_eqn,Ge,ADD_VALUES);
894:         MatSetValuesStencil(A,NODES_PER_EL*P_DOFS,p_eqn,NODES_PER_EL*U_DOFS,u_eqn,De,ADD_VALUES);
895:         MatSetValuesStencil(A,NODES_PER_EL*P_DOFS,p_eqn,NODES_PER_EL*P_DOFS,p_eqn,Ce,ADD_VALUES);
896:       }
897:     }
898:   }
899:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
900:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

902:   DMDAVecRestoreArray(cda,coords,&_coords);

904:   return(0);
905: }

909: static PetscErrorCode AssembleA_PCStokes(Mat A,DM stokes_da,CellProperties cell_properties)
910: {
911:   DM                     cda;
912:   Vec                    coords;
913:   DMDACoor3d             ***_coords;
914:   MatStencil             u_eqn[NODES_PER_EL*U_DOFS];
915:   MatStencil             p_eqn[NODES_PER_EL*P_DOFS];
916:   PetscInt               sex,sey,sez,mx,my,mz;
917:   PetscInt               ei,ej,ek;
918:   PetscScalar            Ae[NODES_PER_EL*U_DOFS*NODES_PER_EL*U_DOFS];
919:   PetscScalar            Ge[NODES_PER_EL*U_DOFS*NODES_PER_EL*P_DOFS];
920:   PetscScalar            De[NODES_PER_EL*P_DOFS*NODES_PER_EL*U_DOFS];
921:   PetscScalar            Ce[NODES_PER_EL*P_DOFS*NODES_PER_EL*P_DOFS];
922:   PetscScalar            el_coords[NODES_PER_EL*NSD];
923:   GaussPointCoefficients *props;
924:   PetscScalar            *prop_eta;
925:   PetscInt               n,M,N,P;
926:   PetscErrorCode         ierr;

929:   DMDAGetInfo(stokes_da,0,&M,&N,&P,0,0,0, 0,0,0,0,0,0);
930:   /* setup for coords */
931:   DMGetCoordinateDM(stokes_da,&cda);
932:   DMGetCoordinatesLocal(stokes_da,&coords);
933:   DMDAVecGetArray(cda,coords,&_coords);

935:   DMDAGetElementCorners(stokes_da,&sex,&sey,&sez,&mx,&my,&mz);
936:   for (ek = sez; ek < sez+mz; ek++) {
937:     for (ej = sey; ej < sey+my; ej++) {
938:       for (ei = sex; ei < sex+mx; ei++) {
939:         /* get coords for the element */
940:         GetElementCoords3D(_coords,ei,ej,ek,el_coords);
941:         /* get cell properties */
942:         CellPropertiesGetCell(cell_properties,ei,ej,ek,&props);
943:         /* get coefficients for the element */
944:         prop_eta = props->eta;

946:         /* initialise element stiffness matrix */
947:         PetscMemzero(Ae,sizeof(PetscScalar)*NODES_PER_EL*U_DOFS*NODES_PER_EL*U_DOFS);
948:         PetscMemzero(Ge,sizeof(PetscScalar)*NODES_PER_EL*U_DOFS*NODES_PER_EL*P_DOFS);
949:         PetscMemzero(De,sizeof(PetscScalar)*NODES_PER_EL*P_DOFS*NODES_PER_EL*U_DOFS);
950:         PetscMemzero(Ce,sizeof(PetscScalar)*NODES_PER_EL*P_DOFS*NODES_PER_EL*P_DOFS);

952:         /* form element stiffness matrix */
953:         FormStressOperatorQ13D(Ae,el_coords,prop_eta);
954:         FormGradientOperatorQ13D(Ge,el_coords);
955:         /* FormDivergenceOperatorQ13D(De,el_coords); */
956:         FormScaledMassMatrixOperatorQ13D(Ce,el_coords,prop_eta);

958:         /* insert element matrix into global matrix */
959:         DMDAGetElementEqnums3D_up(u_eqn,p_eqn,ei,ej,ek);

961:         for (n=0; n<NODES_PER_EL; n++) {
962:           if ((u_eqn[3*n].i == 0) || (u_eqn[3*n].i == M-1)) {
963:             _ZERO_ROWCOL_i(Ae,3*n);
964:             _ZERO_ROW_i   (Ge,3*n);
965:             _ZERO_COL_i   (De,3*n);
966:           }

968:           if ((u_eqn[3*n+1].j == 0) || (u_eqn[3*n+1].j == N-1)) {
969:             _ZERO_ROWCOL_i(Ae,3*n+1);
970:             _ZERO_ROW_i   (Ge,3*n+1);
971:             _ZERO_COL_i   (De,3*n+1);
972:           }

974:           if ((u_eqn[3*n+2].k == 0) || (u_eqn[3*n+2].k == P-1)) {
975:             _ZERO_ROWCOL_i(Ae,3*n+2);
976:             _ZERO_ROW_i   (Ge,3*n+2);
977:             _ZERO_COL_i   (De,3*n+2);
978:           }
979:         }
980:         MatSetValuesStencil(A,NODES_PER_EL*U_DOFS,u_eqn,NODES_PER_EL*U_DOFS,u_eqn,Ae,ADD_VALUES);
981:         MatSetValuesStencil(A,NODES_PER_EL*U_DOFS,u_eqn,NODES_PER_EL*P_DOFS,p_eqn,Ge,ADD_VALUES);
982:         /*MatSetValuesStencil(A,NODES_PER_EL*P_DOFS,p_eqn,NODES_PER_EL*U_DOFS,u_eqn,De,ADD_VALUES);*/
983:         MatSetValuesStencil(A,NODES_PER_EL*P_DOFS,p_eqn,NODES_PER_EL*P_DOFS,p_eqn,Ce,ADD_VALUES);
984:       }
985:     }
986:   }
987:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
988:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

990:   DMDAVecRestoreArray(cda,coords,&_coords);
991:   return(0);
992: }

996: static PetscErrorCode AssembleF_Stokes(Vec F,DM stokes_da,CellProperties cell_properties)
997: {
998:   DM                     cda;
999:   Vec                    coords;
1000:   DMDACoor3d             ***_coords;
1001:   MatStencil             u_eqn[NODES_PER_EL*U_DOFS];
1002:   MatStencil             p_eqn[NODES_PER_EL*P_DOFS];
1003:   PetscInt               sex,sey,sez,mx,my,mz;
1004:   PetscInt               ei,ej,ek;
1005:   PetscScalar            Fe[NODES_PER_EL*U_DOFS];
1006:   PetscScalar            He[NODES_PER_EL*P_DOFS];
1007:   PetscScalar            el_coords[NODES_PER_EL*NSD];
1008:   GaussPointCoefficients *props;
1009:   PetscScalar            *prop_fx,*prop_fy,*prop_fz,*prop_hc;
1010:   Vec                    local_F;
1011:   StokesDOF              ***ff;
1012:   PetscInt               n,M,N,P;
1013:   PetscErrorCode         ierr;

1016:   DMDAGetInfo(stokes_da,0,&M,&N,&P,0,0,0, 0,0,0,0,0,0);
1017:   /* setup for coords */
1018:   DMGetCoordinateDM(stokes_da,&cda);
1019:   DMGetCoordinatesLocal(stokes_da,&coords);
1020:   DMDAVecGetArray(cda,coords,&_coords);

1022:   /* get acces to the vector */
1023:   DMGetLocalVector(stokes_da,&local_F);
1024:   VecZeroEntries(local_F);
1025:   DMDAVecGetArray(stokes_da,local_F,&ff);

1027:   DMDAGetElementCorners(stokes_da,&sex,&sey,&sez,&mx,&my,&mz);
1028:   for (ek = sez; ek < sez+mz; ek++) {
1029:     for (ej = sey; ej < sey+my; ej++) {
1030:       for (ei = sex; ei < sex+mx; ei++) {
1031:         /* get coords for the element */
1032:         GetElementCoords3D(_coords,ei,ej,ek,el_coords);
1033:         /* get cell properties */
1034:         CellPropertiesGetCell(cell_properties,ei,ej,ek,&props);
1035:         /* get coefficients for the element */
1036:         prop_fx = props->fx;
1037:         prop_fy = props->fy;
1038:         prop_fz = props->fz;
1039:         prop_hc = props->hc;

1041:         /* initialise element stiffness matrix */
1042:         PetscMemzero(Fe,sizeof(PetscScalar)*NODES_PER_EL*U_DOFS);
1043:         PetscMemzero(He,sizeof(PetscScalar)*NODES_PER_EL*P_DOFS);

1045:         /* form element stiffness matrix */
1046:         FormMomentumRhsQ13D(Fe,el_coords,prop_fx,prop_fy,prop_fz);
1047:         FormContinuityRhsQ13D(He,el_coords,prop_hc);

1049:         /* insert element matrix into global matrix */
1050:         DMDAGetElementEqnums3D_up(u_eqn,p_eqn,ei,ej,ek);

1052:         for (n=0; n<NODES_PER_EL; n++) {
1053:           if ((u_eqn[3*n].i == 0) || (u_eqn[3*n].i == M-1)) Fe[3*n] = 0.0;

1055:           if ((u_eqn[3*n+1].j == 0) || (u_eqn[3*n+1].j == N-1)) Fe[3*n+1] = 0.0;

1057:           if ((u_eqn[3*n+2].k == 0) || (u_eqn[3*n+2].k == P-1)) Fe[3*n+2] = 0.0;
1058:         }

1060:         DMDASetValuesLocalStencil3D_ADD_VALUES(ff,u_eqn,p_eqn,Fe,He);
1061:       }
1062:     }
1063:   }
1064:   DMDAVecRestoreArray(stokes_da,local_F,&ff);
1065:   DMLocalToGlobalBegin(stokes_da,local_F,ADD_VALUES,F);
1066:   DMLocalToGlobalEnd(stokes_da,local_F,ADD_VALUES,F);
1067:   DMRestoreLocalVector(stokes_da,&local_F);

1069:   DMDAVecRestoreArray(cda,coords,&_coords);
1070:   return(0);
1071: }

1073: static void evaluate_MS_FrankKamentski_constants(PetscReal *theta,PetscReal *MX,PetscReal *MY,PetscReal *MZ)
1074: {
1075:   *theta = 0.0;
1076:   *MX    = 2.0 * PETSC_PI;
1077:   *MY    = 2.0 * PETSC_PI;
1078:   *MZ    = 2.0 * PETSC_PI;
1079: }
1080: static void evaluate_MS_FrankKamentski(PetscReal pos[],PetscReal v[],PetscReal *p,PetscReal *eta,PetscReal Fm[],PetscReal *Fc)
1081: {
1082:   PetscReal x,y,z;
1083:   PetscReal theta,MX,MY,MZ;

1085:   evaluate_MS_FrankKamentski_constants(&theta,&MX,&MY,&MZ);
1086:   x = pos[0];
1087:   y = pos[1];
1088:   z = pos[2];
1089:   if (v) {
1090:     /*
1091:      v[0] = PetscPowRealInt(z,3)*PetscExpReal(y)*PetscSinReal(PETSC_PI*x);
1092:      v[1] = z*cos(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y);
1093:      v[2] = -(PetscPowRealInt(x,3) + PetscPowRealInt(y,3))*PetscSinReal(2.0*PETSC_PI*z);
1094:      */
1095:     /*
1096:      v[0] = PetscSinReal(PETSC_PI*x);
1097:      v[1] = PetscSinReal(PETSC_PI*y);
1098:      v[2] = PetscSinReal(PETSC_PI*z);
1099:      */
1100:     v[0] = PetscPowRealInt(z,3)*PetscExpReal(y)*PetscSinReal(PETSC_PI*x);
1101:     v[1] = z*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y);
1102:     v[2] = PetscPowRealInt(z,2)*(PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y)/2 - PETSC_PI*PetscCosReal(PETSC_PI*y)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)/2) - PETSC_PI*PetscPowRealInt(z,4)*PetscCosReal(PETSC_PI*x)*PetscExpReal(y)/4;
1103:   }
1104:   if (p) *p = PetscPowRealInt(x,2) + PetscPowRealInt(y,2) + PetscPowRealInt(z,2);
1105:   if (eta) {
1106:     /**eta = PetscExpReal(-theta*(1.0 - y - 0.1*PetscCosReal(MX*x)*PetscCosReal(MZ*z)*PetscSinReal(MY*y)));*/
1107:     *eta = 1.0;
1108:   }
1109:   if (Fm) {
1110:     /*
1111:      Fm[0] = -2*x - 2.0*PetscPowRealInt(PETSC_PI,2)*PetscPowRealInt(z,3)*PetscExpReal(y)*PetscExpReal(-theta*(1 - y - 0.1*PetscCosReal(MX*x)*PetscCosReal(MZ*z)*PetscSinReal(MY*y)))*PetscSinReal(PETSC_PI*x) - 0.2*MZ*theta*(-1.5*PetscPowRealInt(x,2)*PetscSinReal(2.0*PETSC_PI*z) + 1.5*PetscPowRealInt(z,2)*PetscExpReal(y)*PetscSinReal(PETSC_PI*x))*PetscCosReal(MX*x)*PetscExpReal(-theta*(1 - y - 0.1*PetscCosReal(MX*x)*PetscCosReal(MZ*z)*PetscSinReal(MY*y)))*PetscSinReal(MY*y)*PetscSinReal(MZ*z) - 0.2*PETSC_PI*MX*theta*PetscPowRealInt(z,3)*PetscCosReal(PETSC_PI*x)*PetscCosReal(MZ*z)*PetscExpReal(y)*PetscExpReal(-theta*(1 - y - 0.1*PetscCosReal(MX*x)*PetscCosReal(MZ*z)*PetscSinReal(MY*y)))*PetscSinReal(MX*x)*PetscSinReal(MY*y) + 2.0*(3.0*z*PetscExpReal(y)*PetscSinReal(PETSC_PI*x) - 3.0*PETSC_PI*PetscPowRealInt(x,2)*PetscCosReal(2.0*PETSC_PI*z))*PetscExpReal(-theta*(1 - y - 0.1*PetscCosReal(MX*x)*PetscCosReal(MZ*z)*PetscSinReal(MY*y))) + 2.0*(0.5*PetscPowRealInt(z,3)*PetscExpReal(y)*PetscSinReal(PETSC_PI*x) + PETSC_PI*z*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y)*PetscSinReal(2.0*PETSC_PI*x) - 1.0*z*PetscPowRealInt(PETSC_PI,2)*PetscCosReal(PETSC_PI*y)*PetscExpReal(-y)*PetscSinReal(2.0*PETSC_PI*x))*PetscExpReal(-theta*(1 - y - 0.1*PetscCosReal(MX*x)*PetscCosReal(MZ*z)*PetscSinReal(MY*y))) + 2.0*theta*(1 + 0.1*MY*PetscCosReal(MX*x)*PetscCosReal(MY*y)*PetscCosReal(MZ*z))*(0.5*PetscPowRealInt(z,3)*PetscExpReal(y)*PetscSinReal(PETSC_PI*x) - 1.0*PETSC_PI*z*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y)*PetscSinReal(2.0*PETSC_PI*x))*PetscExpReal(-theta*(1 - y - 0.1*PetscCosReal(MX*x)*PetscCosReal(MZ*z)*PetscSinReal(MY*y))) ;
1112:      Fm[1] = -2*y - 0.2*MX*theta*(0.5*PetscPowRealInt(z,3)*PetscExpReal(y)*PetscSinReal(PETSC_PI*x) - 1.0*PETSC_PI*z*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y)*PetscSinReal(2.0*PETSC_PI*x))*PetscCosReal(MZ*z)*PetscExpReal(-theta*(1 - y - 0.1*PetscCosReal(MX*x)*PetscCosReal(MZ*z)*PetscSinReal(MY*y)))*PetscSinReal(MX*x)*PetscSinReal(MY*y) - 0.2*MZ*theta*(-1.5*PetscPowRealInt(y,2)*PetscSinReal(2.0*PETSC_PI*z) + 0.5*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y))*PetscCosReal(MX*x)*PetscExpReal(-theta*(1 - y - 0.1*PetscCosReal(MX*x)*PetscCosReal(MZ*z)*PetscSinReal(MY*y)))*PetscSinReal(MY*y)*PetscSinReal(MZ*z) + 2.0*(-2.0*z*PetscPowRealInt(PETSC_PI,2)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y) + 0.5*PETSC_PI*PetscPowRealInt(z,3)*PetscCosReal(PETSC_PI*x)*PetscExpReal(y))*PetscExpReal(-theta*(1 - y - 0.1*PetscCosReal(MX*x)*PetscCosReal(MZ*z)*PetscSinReal(MY*y))) + 2.0*(z*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y) - z*PetscPowRealInt(PETSC_PI,2)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y) - 2*PETSC_PI*z*PetscCosReal(PETSC_PI*y)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y))*PetscExpReal(-theta*(1 - y - 0.1*PetscCosReal(MX*x)*PetscCosReal(MZ*z)*PetscSinReal(MY*y))) + 2.0*theta*(1 + 0.1*MY*PetscCosReal(MX*x)*PetscCosReal(MY*y)*PetscCosReal(MZ*z))*(-z*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y) + PETSC_PI*z*PetscCosReal(PETSC_PI*y)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y))*PetscExpReal(-theta*(1 - y - 0.1*PetscCosReal(MX*x)*PetscCosReal(MZ*z)*PetscSinReal(MY*y))) - 6.0*PETSC_PI*PetscPowRealInt(y,2)*PetscCosReal(2.0*PETSC_PI*z)*PetscExpReal(-theta*(1 - y - 0.1*PetscCosReal(MX*x)*PetscCosReal(MZ*z)*PetscSinReal(MY*y)));
1113:      Fm[2] = -2*z + 8.0*PetscPowRealInt(PETSC_PI,2)*(PetscPowRealInt(x,3) + PetscPowRealInt(y,3))*PetscExpReal(-theta*(1 - y - 0.1*PetscCosReal(MX*x)*PetscCosReal(MZ*z)*PetscSinReal(MY*y)))*PetscSinReal(2.0*PETSC_PI*z) - 0.2*MX*theta*(-1.5*PetscPowRealInt(x,2)*PetscSinReal(2.0*PETSC_PI*z) + 1.5*PetscPowRealInt(z,2)*PetscExpReal(y)*PetscSinReal(PETSC_PI*x))*PetscCosReal(MZ*z)*PetscExpReal(-theta*(1 - y - 0.1*PetscCosReal(MX*x)*PetscCosReal(MZ*z)*PetscSinReal(MY*y)))*PetscSinReal(MX*x)*PetscSinReal(MY*y) + 0.4*PETSC_PI*MZ*theta*(PetscPowRealInt(x,3) + PetscPowRealInt(y,3))*PetscCosReal(MX*x)*PetscCosReal(2.0*PETSC_PI*z)*PetscExpReal(-theta*(1 - y - 0.1*PetscCosReal(MX*x)*PetscCosReal(MZ*z)*PetscSinReal(MY*y)))*PetscSinReal(MY*y)*PetscSinReal(MZ*z) + 2.0*(-3.0*x*PetscSinReal(2.0*PETSC_PI*z) + 1.5*PETSC_PI*PetscPowRealInt(z,2)*PetscCosReal(PETSC_PI*x)*PetscExpReal(y))*PetscExpReal(-theta*(1 - y - 0.1*PetscCosReal(MX*x)*PetscCosReal(MZ*z)*PetscSinReal(MY*y))) + 2.0*(-3.0*y*PetscSinReal(2.0*PETSC_PI*z) - 0.5*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y) + 0.5*PETSC_PI*PetscCosReal(PETSC_PI*y)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y))*PetscExpReal(-theta*(1 - y - 0.1*PetscCosReal(MX*x)*PetscCosReal(MZ*z)*PetscSinReal(MY*y))) + 2.0*theta*(1 + 0.1*MY*PetscCosReal(MX*x)*PetscCosReal(MY*y)*PetscCosReal(MZ*z))*(-1.5*PetscPowRealInt(y,2)*PetscSinReal(2.0*PETSC_PI*z) + 0.5*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y))*PetscExpReal(-theta*(1 - y - 0.1*PetscCosReal(MX*x)*PetscCosReal(MZ*z)*PetscSinReal(MY*y)))  ;
1114:      */
1115:     /*
1116:      Fm[0]=-2*x - 2.0*PetscPowRealInt(PETSC_PI,2)*PetscSinReal(PETSC_PI*x);
1117:      Fm[1]=-2*y - 2.0*PetscPowRealInt(PETSC_PI,2)*PetscSinReal(PETSC_PI*y);
1118:      Fm[2]=-2*z - 2.0*PetscPowRealInt(PETSC_PI,2)*PetscSinReal(PETSC_PI*z);
1119:      */
1120:     /*
1121:      Fm[0] = -2*x + PetscPowRealInt(z,3)*PetscExpReal(y)*PetscSinReal(PETSC_PI*x) + 6.0*z*PetscExpReal(y)*PetscSinReal(PETSC_PI*x) - 6.0*PETSC_PI*PetscPowRealInt(x,2)*PetscCosReal(2.0*PETSC_PI*z) - 2.0*PetscPowRealInt(PETSC_PI,2)*PetscPowRealInt(z,3)*PetscExpReal(y)*PetscSinReal(PETSC_PI*x) + 2.0*PETSC_PI*z*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y)*PetscSinReal(2.0*PETSC_PI*x) - 2.0*z*PetscPowRealInt(PETSC_PI,2)*PetscCosReal(PETSC_PI*y)*PetscExpReal(-y)*PetscSinReal(2.0*PETSC_PI*x) ;
1122:      Fm[1] = -2*y - 6.0*PETSC_PI*PetscPowRealInt(y,2)*PetscCosReal(2.0*PETSC_PI*z) + 2.0*z*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y) - 6.0*z*PetscPowRealInt(PETSC_PI,2)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y) + PETSC_PI*PetscPowRealInt(z,3)*PetscCosReal(PETSC_PI*x)*PetscExpReal(y) - 4.0*PETSC_PI*z*PetscCosReal(PETSC_PI*y)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y);
1123:      Fm[2] = -2*z - 6.0*x*PetscSinReal(2.0*PETSC_PI*z) - 6.0*y*PetscSinReal(2.0*PETSC_PI*z) - PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y) + 8.0*PetscPowRealInt(PETSC_PI,2)*(PetscPowRealInt(x,3) + PetscPowRealInt(y,3))*PetscSinReal(2.0*PETSC_PI*z) + 3.0*PETSC_PI*PetscPowRealInt(z,2)*PetscCosReal(PETSC_PI*x)*PetscExpReal(y) + PETSC_PI*PetscCosReal(PETSC_PI*y)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y) ;
1124:      */
1125:     Fm[0] = -2*x + 2*z*(PetscPowRealInt(PETSC_PI,2)*PetscCosReal(PETSC_PI*y)*PetscExpReal(-y)*PetscSinReal(2.0*PETSC_PI*x) - 1.0*PETSC_PI*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y)*PetscSinReal(2.0*PETSC_PI*x)) + PetscPowRealInt(z,3)*PetscExpReal(y)*PetscSinReal(PETSC_PI*x) + 6.0*z*PetscExpReal(y)*PetscSinReal(PETSC_PI*x) - 1.0*PetscPowRealInt(PETSC_PI,2)*PetscPowRealInt(z,3)*PetscExpReal(y)*PetscSinReal(PETSC_PI*x) + 2.0*PETSC_PI*z*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y)*PetscSinReal(2.0*PETSC_PI*x) - 2.0*z*PetscPowRealInt(PETSC_PI,2)*PetscCosReal(PETSC_PI*y)*PetscExpReal(-y)*PetscSinReal(2.0*PETSC_PI*x);
1126:     Fm[1] = -2*y + 2*z*(-PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y)/2 + PetscPowRealInt(PETSC_PI,2)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y)/2 + PETSC_PI*PetscCosReal(PETSC_PI*y)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)) + 2.0*z*PetscCosReal(2.0*PETSC_PI *x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y) - 6.0*z*PetscPowRealInt(PETSC_PI,2)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y) - 4.0*PETSC_PI*z*PetscCosReal(PETSC_PI *y)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y);
1127:     Fm[2] = -2*z + PetscPowRealInt(z,2)*(-2.0*PetscPowRealInt(PETSC_PI,2)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y) + 2.0*PetscPowRealInt(PETSC_PI,3)*PetscCosReal(PETSC_PI*y)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)) + PetscPowRealInt(z,2)*(PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y)/2 - 3*PetscPowRealInt(PETSC_PI,2)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y)/2 + PetscPowRealInt(PETSC_PI,3)*PetscCosReal(PETSC_PI*y)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)/2 - 3*PETSC_PI*PetscCosReal(PETSC_PI*y)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)/2) + 1.0*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y) + 0.25*PetscPowRealInt(PETSC_PI,3)*PetscPowRealInt(z,4)*PetscCosReal(PETSC_PI*x)*PetscExpReal(y) - 0.25*PETSC_PI*PetscPowRealInt(z,4)*PetscCosReal(PETSC_PI*x)*PetscExpReal(y) - 3.0*PETSC_PI*PetscPowRealInt(z,2)*PetscCosReal(PETSC_PI*x)*PetscExpReal(y) - 1.0*PETSC_PI*PetscCosReal(PETSC_PI *y)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y);
1128:   }
1129:   if (Fc) {
1130:     /**Fc = -2.0*PETSC_PI*(PetscPowRealInt(x,3) + PetscPowRealInt(y,3))*PetscCosReal(2.0*PETSC_PI*z) - z*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y) + PETSC_PI*PetscPowRealInt(z,3)*PetscCosReal(PETSC_PI*x)*PetscExpReal(y) + PETSC_PI*z*PetscCosReal(PETSC_PI*y)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y) ;*/
1131:     /**Fc = PETSC_PI*PetscCosReal(PETSC_PI*x) + PETSC_PI*PetscCosReal(PETSC_PI*y) + PETSC_PI*PetscCosReal(PETSC_PI*z);*/
1132:     /**Fc = -2.0*PETSC_PI*(PetscPowRealInt(x,3) + PetscPowRealInt(y,3))*PetscCosReal(2.0*PETSC_PI*z) - z*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y)*PetscSinReal(PETSC_PI*y) + PETSC_PI*PetscPowRealInt(z,3)*PetscCosReal(PETSC_PI*x)*PetscExpReal(y) + PETSC_PI*z*PetscCosReal(PETSC_PI*y)*PetscCosReal(2.0*PETSC_PI*x)*PetscExpReal(-y);*/
1133:     *Fc = 0.0;
1134:   }
1135: }

1139: static PetscErrorCode DMDACreateManufacturedSolution(PetscInt mx,PetscInt my,PetscInt mz,DM *_da,Vec *_X)
1140: {
1141:   DM             da,cda;
1142:   Vec            X;
1143:   StokesDOF      ***_stokes;
1144:   Vec            coords;
1145:   DMDACoor3d     ***_coords;
1146:   PetscInt       si,sj,sk,ei,ej,ek,i,j,k;

1150:   DMDACreate3d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,
1151:                       mx+1,my+1,mz+1,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,4,1,NULL,NULL,NULL,&da);
1152:   DMDASetFieldName(da,0,"anlytic_Vx");
1153:   DMDASetFieldName(da,1,"anlytic_Vy");
1154:   DMDASetFieldName(da,2,"anlytic_Vz");
1155:   DMDASetFieldName(da,3,"analytic_P");

1157:   DMDASetUniformCoordinates(da,0.0,1.0,0.0,1.0,0.0,1.0);

1159:   DMGetCoordinatesLocal(da,&coords);
1160:   DMGetCoordinateDM(da,&cda);
1161:   DMDAVecGetArray(cda,coords,&_coords);

1163:   DMCreateGlobalVector(da,&X);
1164:   DMDAVecGetArray(da,X,&_stokes);

1166:   DMDAGetCorners(da,&si,&sj,&sk,&ei,&ej,&ek);
1167:   for (k = sk; k < sk+ek; k++) {
1168:     for (j = sj; j < sj+ej; j++) {
1169:       for (i = si; i < si+ei; i++) {
1170:         PetscReal pos[NSD],pressure,vel[NSD];

1172:         pos[0] = PetscRealPart(_coords[k][j][i].x);
1173:         pos[1] = PetscRealPart(_coords[k][j][i].y);
1174:         pos[2] = PetscRealPart(_coords[k][j][i].z);

1176:         evaluate_MS_FrankKamentski(pos,vel,&pressure,NULL,NULL,NULL);

1178:         _stokes[k][j][i].u_dof = vel[0];
1179:         _stokes[k][j][i].v_dof = vel[1];
1180:         _stokes[k][j][i].w_dof = vel[2];
1181:         _stokes[k][j][i].p_dof = pressure;
1182:       }
1183:     }
1184:   }
1185:   DMDAVecRestoreArray(da,X,&_stokes);
1186:   DMDAVecRestoreArray(cda,coords,&_coords);

1188:   *_da = da;
1189:   *_X  = X;
1190:   return(0);
1191: }

1195: static PetscErrorCode DMDAIntegrateErrors3D(DM stokes_da,Vec X,Vec X_analytic)
1196: {
1197:   DM          cda;
1198:   Vec         coords,X_analytic_local,X_local;
1199:   DMDACoor3d  ***_coords;
1200:   PetscInt    sex,sey,sez,mx,my,mz;
1201:   PetscInt    ei,ej,ek;
1202:   PetscScalar el_coords[NODES_PER_EL*NSD];
1203:   StokesDOF   ***stokes_analytic,***stokes;
1204:   StokesDOF   stokes_analytic_e[NODES_PER_EL],stokes_e[NODES_PER_EL];

1206:   PetscScalar    GNi_p[NSD][NODES_PER_EL],GNx_p[NSD][NODES_PER_EL];
1207:   PetscScalar    Ni_p[NODES_PER_EL];
1208:   PetscInt       ngp;
1209:   PetscScalar    gp_xi[GAUSS_POINTS][NSD];
1210:   PetscScalar    gp_weight[GAUSS_POINTS];
1211:   PetscInt       p,i;
1212:   PetscScalar    J_p,fac;
1213:   PetscScalar    h,p_e_L2,u_e_L2,u_e_H1,p_L2,u_L2,u_H1,tp_L2,tu_L2,tu_H1;
1214:   PetscScalar    tint_p_ms,tint_p,int_p_ms,int_p;
1215:   PetscInt       M;
1216:   PetscReal      xymin[NSD],xymax[NSD];

1220:   /* define quadrature rule */
1221:   ConstructGaussQuadrature3D(&ngp,gp_xi,gp_weight);

1223:   /* setup for coords */
1224:   DMGetCoordinateDM(stokes_da,&cda);
1225:   DMGetCoordinatesLocal(stokes_da,&coords);
1226:   DMDAVecGetArray(cda,coords,&_coords);

1228:   /* setup for analytic */
1229:   DMCreateLocalVector(stokes_da,&X_analytic_local);
1230:   DMGlobalToLocalBegin(stokes_da,X_analytic,INSERT_VALUES,X_analytic_local);
1231:   DMGlobalToLocalEnd(stokes_da,X_analytic,INSERT_VALUES,X_analytic_local);
1232:   DMDAVecGetArray(stokes_da,X_analytic_local,&stokes_analytic);

1234:   /* setup for solution */
1235:   DMCreateLocalVector(stokes_da,&X_local);
1236:   DMGlobalToLocalBegin(stokes_da,X,INSERT_VALUES,X_local);
1237:   DMGlobalToLocalEnd(stokes_da,X,INSERT_VALUES,X_local);
1238:   DMDAVecGetArray(stokes_da,X_local,&stokes);

1240:   DMDAGetInfo(stokes_da,0,&M,0,0,0,0,0,0,0,0,0,0,0);
1241:   DMDAGetBoundingBox(stokes_da,xymin,xymax);

1243:   h = (xymax[0]-xymin[0])/((PetscReal)(M-1));

1245:   tp_L2     = tu_L2 = tu_H1 = 0.0;
1246:   tint_p_ms = tint_p = 0.0;

1248:   DMDAGetElementCorners(stokes_da,&sex,&sey,&sez,&mx,&my,&mz);

1250:   for (ek = sez; ek < sez+mz; ek++) {
1251:     for (ej = sey; ej < sey+my; ej++) {
1252:       for (ei = sex; ei < sex+mx; ei++) {
1253:         /* get coords for the element */
1254:         GetElementCoords3D(_coords,ei,ej,ek,el_coords);
1255:         StokesDAGetNodalFields3D(stokes,ei,ej,ek,stokes_e);
1256:         StokesDAGetNodalFields3D(stokes_analytic,ei,ej,ek,stokes_analytic_e);

1258:         /* evaluate integral */
1259:         for (p = 0; p < ngp; p++) {
1260:           ShapeFunctionQ13D_Evaluate(gp_xi[p],Ni_p);
1261:           ShapeFunctionQ13D_Evaluate_dxi(gp_xi[p],GNi_p);
1262:           ShapeFunctionQ13D_Evaluate_dx(GNi_p,GNx_p,el_coords,&J_p);
1263:           fac = gp_weight[p]*J_p;

1265:           for (i = 0; i < NODES_PER_EL; i++) {
1266:             tint_p_ms = tint_p_ms+fac*Ni_p[i]*stokes_analytic_e[i].p_dof;
1267:             tint_p    = tint_p   +fac*Ni_p[i]*stokes_e[i].p_dof;
1268:           }
1269:         }
1270:       }
1271:     }
1272:   }
1273:   MPI_Allreduce(&tint_p_ms,&int_p_ms,1,MPIU_SCALAR,MPIU_SUM,PETSC_COMM_WORLD);
1274:   MPI_Allreduce(&tint_p,&int_p,1,MPIU_SCALAR,MPIU_SUM,PETSC_COMM_WORLD);

1276:   PetscPrintf(PETSC_COMM_WORLD,"\\int P dv %1.4e (h)  %1.4e (ms)\n",PetscRealPart(int_p),PetscRealPart(int_p_ms));

1278:   /* remove mine and add manufacture one */
1279:   DMDAVecRestoreArray(stokes_da,X_analytic_local,&stokes_analytic);
1280:   DMDAVecRestoreArray(stokes_da,X_local,&stokes);

1282:   {
1283:     PetscInt    k,L,dof;
1284:     PetscScalar *fields;
1285:     DMDAGetInfo(stokes_da,0, 0,0,0, 0,0,0, &dof,0,0,0,0,0);

1287:     VecGetLocalSize(X_local,&L);
1288:     VecGetArray(X_local,&fields);
1289:     for (k=0; k<L/dof; k++) fields[dof*k+3] = fields[dof*k+3] - int_p + int_p_ms;
1290:     VecRestoreArray(X_local,&fields);

1292:     VecGetLocalSize(X,&L);
1293:     VecGetArray(X,&fields);
1294:     for (k=0; k<L/dof; k++) fields[dof*k+3] = fields[dof*k+3] - int_p + int_p_ms;
1295:     VecRestoreArray(X,&fields);
1296:   }

1298:   DMDAVecGetArray(stokes_da,X_local,&stokes);
1299:   DMDAVecGetArray(stokes_da,X_analytic_local,&stokes_analytic);

1301:   for (ek = sez; ek < sez+mz; ek++) {
1302:     for (ej = sey; ej < sey+my; ej++) {
1303:       for (ei = sex; ei < sex+mx; ei++) {
1304:         /* get coords for the element */
1305:         GetElementCoords3D(_coords,ei,ej,ek,el_coords);
1306:         StokesDAGetNodalFields3D(stokes,ei,ej,ek,stokes_e);
1307:         StokesDAGetNodalFields3D(stokes_analytic,ei,ej,ek,stokes_analytic_e);

1309:         /* evaluate integral */
1310:         p_e_L2 = 0.0;
1311:         u_e_L2 = 0.0;
1312:         u_e_H1 = 0.0;
1313:         for (p = 0; p < ngp; p++) {
1314:           ShapeFunctionQ13D_Evaluate(gp_xi[p],Ni_p);
1315:           ShapeFunctionQ13D_Evaluate_dxi(gp_xi[p],GNi_p);
1316:           ShapeFunctionQ13D_Evaluate_dx(GNi_p,GNx_p,el_coords,&J_p);
1317:           fac = gp_weight[p]*J_p;

1319:           for (i = 0; i < NODES_PER_EL; i++) {
1320:             PetscScalar u_error,v_error,w_error;

1322:             p_e_L2 = p_e_L2+fac*Ni_p[i]*(stokes_e[i].p_dof-stokes_analytic_e[i].p_dof)*(stokes_e[i].p_dof-stokes_analytic_e[i].p_dof);

1324:             u_error = stokes_e[i].u_dof-stokes_analytic_e[i].u_dof;
1325:             v_error = stokes_e[i].v_dof-stokes_analytic_e[i].v_dof;
1326:             w_error = stokes_e[i].w_dof-stokes_analytic_e[i].w_dof;
1327:             /*
1328:              if (p==0) {
1329:              printf("p=0: %d %d %d %1.4e,%1.4e,%1.4e \n", ei,ej,ek,u_error,v_error,w_error);
1330:              }
1331:              */
1332:             u_e_L2 += fac*Ni_p[i]*(u_error*u_error+v_error*v_error+w_error*w_error);

1334:             u_e_H1 = u_e_H1+fac*(GNx_p[0][i]*u_error*GNx_p[0][i]*u_error              /* du/dx */
1335:                                  +GNx_p[1][i]*u_error*GNx_p[1][i]*u_error
1336:                                  +GNx_p[2][i]*u_error*GNx_p[2][i]*u_error
1337:                                  +GNx_p[0][i]*v_error*GNx_p[0][i]*v_error               /* dv/dx */
1338:                                  +GNx_p[1][i]*v_error*GNx_p[1][i]*v_error
1339:                                  +GNx_p[2][i]*v_error*GNx_p[2][i]*v_error
1340:                                  +GNx_p[0][i]*w_error*GNx_p[0][i]*w_error               /* dw/dx */
1341:                                  +GNx_p[1][i]*w_error*GNx_p[1][i]*w_error
1342:                                  +GNx_p[2][i]*w_error*GNx_p[2][i]*w_error);
1343:           }
1344:         }

1346:         tp_L2 += p_e_L2;
1347:         tu_L2 += u_e_L2;
1348:         tu_H1 += u_e_H1;
1349:       }
1350:     }
1351:   }
1352:   MPI_Allreduce(&tp_L2,&p_L2,1,MPIU_SCALAR,MPIU_SUM,PETSC_COMM_WORLD);
1353:   MPI_Allreduce(&tu_L2,&u_L2,1,MPIU_SCALAR,MPIU_SUM,PETSC_COMM_WORLD);
1354:   MPI_Allreduce(&tu_H1,&u_H1,1,MPIU_SCALAR,MPIU_SUM,PETSC_COMM_WORLD);
1355:   p_L2 = PetscSqrtScalar(p_L2);
1356:   u_L2 = PetscSqrtScalar(u_L2);
1357:   u_H1 = PetscSqrtScalar(u_H1);

1359:   PetscPrintf(PETSC_COMM_WORLD,"%1.4e   %1.4e   %1.4e   %1.4e \n",PetscRealPart(h),PetscRealPart(p_L2),PetscRealPart(u_L2),PetscRealPart(u_H1));


1362:   DMDAVecRestoreArray(cda,coords,&_coords);

1364:   DMDAVecRestoreArray(stokes_da,X_analytic_local,&stokes_analytic);
1365:   VecDestroy(&X_analytic_local);
1366:   DMDAVecRestoreArray(stokes_da,X_local,&stokes);
1367:   VecDestroy(&X_local);
1368:   return(0);
1369: }

1373: PetscErrorCode DAView_3DVTK_StructuredGrid_appended(DM da,Vec FIELD,const char file_prefix[])
1374: {
1375:   char           vtk_filename[PETSC_MAX_PATH_LEN];
1376:   PetscMPIInt    rank;
1377:   MPI_Comm       comm;
1378:   FILE           *vtk_fp = NULL;
1379:   PetscInt       si,sj,sk,nx,ny,nz,i;
1380:   PetscInt       f,n_fields,N;
1381:   DM             cda;
1382:   Vec            coords;
1383:   Vec            l_FIELD;
1384:   PetscScalar    *_L_FIELD;
1385:   PetscInt       memory_offset;
1386:   PetscScalar    *buffer;


1391:   /* create file name */
1392:   PetscObjectGetComm((PetscObject)da,&comm);
1393:   MPI_Comm_rank(comm,&rank);
1394:   PetscSNPrintf(vtk_filename,sizeof(vtk_filename),"subdomain-%s-p%1.4d.vts",file_prefix,rank);

1396:   /* open file and write header */
1397:   vtk_fp = fopen(vtk_filename,"w");
1398:   if (!vtk_fp) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"Cannot open file = %s \n",vtk_filename);

1400:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"<?xml version=\"1.0\"?>\n");

1402:   /* coords */
1403:   DMDAGetGhostCorners(da,&si,&sj,&sk,&nx,&ny,&nz);
1404:   N    = nx * ny * nz;

1406: #if defined(PETSC_WORDS_BIGENDIAN)
1407:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"<VTKFile type=\"StructuredGrid\" version=\"0.1\" byte_order=\"BigEndian\">\n");
1408: #else
1409:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"<VTKFile type=\"StructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">\n");
1410: #endif
1411:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"  <StructuredGrid WholeExtent=\"%D %D %D %D %D %D\">\n",si,si+nx-1,sj,sj+ny-1,sk,sk+nz-1);
1412:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"    <Piece Extent=\"%D %D %D %D %D %D\">\n",si,si+nx-1,sj,sj+ny-1,sk,sk+nz-1);

1414:   memory_offset = 0;

1416:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"      <CellData></CellData>\n");

1418:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"      <Points>\n");

1420:   /* copy coordinates */
1421:   DMGetCoordinateDM(da,&cda);
1422:   DMGetCoordinatesLocal(da,&coords);
1423:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"        <DataArray type=\"Float64\" NumberOfComponents=\"3\" format=\"appended\" offset=\"%d\" />\n",memory_offset);
1424:   memory_offset = memory_offset + sizeof(PetscInt) + sizeof(PetscScalar)*N*3;

1426:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"      </Points>\n");

1428:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"      <PointData Scalars=\" ");
1429:   DMDAGetInfo(da,0,0,0,0,0,0,0,&n_fields,0,0,0,0,0);
1430:   for (f=0; f<n_fields; f++) {
1431:     const char *field_name;
1432:     DMDAGetFieldName(da,f,&field_name);
1433:     PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"%s ",field_name);
1434:   }
1435:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"\">\n");

1437:   for (f=0; f<n_fields; f++) {
1438:     const char *field_name;

1440:     DMDAGetFieldName(da,f,&field_name);
1441:     PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"        <DataArray type=\"Float64\" Name=\"%s\" format=\"appended\" offset=\"%d\"/>\n", field_name,memory_offset);
1442:     memory_offset = memory_offset + sizeof(PetscInt) + sizeof(PetscScalar)*N;
1443:   }

1445:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"      </PointData>\n");
1446:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"    </Piece>\n");
1447:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"  </StructuredGrid>\n");

1449:   PetscMalloc1(N,&buffer);
1450:   DMGetLocalVector(da,&l_FIELD);
1451:   DMGlobalToLocalBegin(da, FIELD,INSERT_VALUES,l_FIELD);
1452:   DMGlobalToLocalEnd(da,FIELD,INSERT_VALUES,l_FIELD);
1453:   VecGetArray(l_FIELD,&_L_FIELD);

1455:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"  <AppendedData encoding=\"raw\">\n");
1456:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"_");

1458:   /* write coordinates */
1459:   {
1460:     int         length = sizeof(PetscScalar)*N*3;
1461:     PetscScalar *allcoords;

1463:     fwrite(&length,sizeof(int),1,vtk_fp);
1464:     VecGetArray(coords,&allcoords);
1465:     fwrite(allcoords,sizeof(PetscScalar),3*N,vtk_fp);
1466:     VecRestoreArray(coords,&allcoords);
1467:   }
1468:   /* write fields */
1469:   for (f=0; f<n_fields; f++) {
1470:     int length = sizeof(PetscScalar)*N;
1471:     fwrite(&length,sizeof(int),1,vtk_fp);
1472:     /* load */
1473:     for (i=0; i<N; i++) buffer[i] = _L_FIELD[n_fields*i + f];

1475:     /* write */
1476:     fwrite(buffer,sizeof(PetscScalar),N,vtk_fp);
1477:   }
1478:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"\n  </AppendedData>\n");

1480:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"</VTKFile>\n");

1482:   PetscFree(buffer);
1483:   VecRestoreArray(l_FIELD,&_L_FIELD);
1484:   DMRestoreLocalVector(da,&l_FIELD);

1486:   if (vtk_fp) {
1487:     fclose(vtk_fp);
1488:     vtk_fp = NULL;
1489:   }

1491:   return(0);
1492: }

1496: PetscErrorCode DAViewVTK_write_PieceExtend(FILE *vtk_fp,PetscInt indent_level,DM da,const char local_file_prefix[])
1497: {
1498:   PetscMPIInt    size,rank;
1499:   MPI_Comm       comm;
1500:   const PetscInt *lx,*ly,*lz;
1501:   PetscInt       M,N,P,pM,pN,pP,sum,*olx,*oly,*olz;
1502:   PetscInt       *osx,*osy,*osz,*oex,*oey,*oez;
1503:   PetscInt       i,j,k,II,stencil;

1507:   /* create file name */
1508:   PetscObjectGetComm((PetscObject)da,&comm);
1509:   MPI_Comm_size(comm,&size);
1510:   MPI_Comm_rank(comm,&rank);

1512:   DMDAGetInfo(da,0,&M,&N,&P,&pM,&pN,&pP,0,&stencil,0,0,0,0);
1513:   DMDAGetOwnershipRanges(da,&lx,&ly,&lz);

1515:   /* generate start,end list */
1516:   PetscMalloc1(pM+1,&olx);
1517:   PetscMalloc1(pN+1,&oly);
1518:   PetscMalloc1(pP+1,&olz);
1519:   sum  = 0;
1520:   for (i=0; i<pM; i++) {
1521:     olx[i] = sum;
1522:     sum    = sum + lx[i];
1523:   }
1524:   olx[pM] = sum;
1525:   sum     = 0;
1526:   for (i=0; i<pN; i++) {
1527:     oly[i] = sum;
1528:     sum    = sum + ly[i];
1529:   }
1530:   oly[pN] = sum;
1531:   sum     = 0;
1532:   for (i=0; i<pP; i++) {
1533:     olz[i] = sum;
1534:     sum    = sum + lz[i];
1535:   }
1536:   olz[pP] = sum;

1538:   PetscMalloc1(pM,&osx);
1539:   PetscMalloc1(pN,&osy);
1540:   PetscMalloc1(pP,&osz);
1541:   PetscMalloc1(pM,&oex);
1542:   PetscMalloc1(pN,&oey);
1543:   PetscMalloc1(pP,&oez);
1544:   for (i=0; i<pM; i++) {
1545:     osx[i] = olx[i] - stencil;
1546:     oex[i] = olx[i] + lx[i] + stencil;
1547:     if (osx[i]<0) osx[i]=0;
1548:     if (oex[i]>M) oex[i]=M;
1549:   }

1551:   for (i=0; i<pN; i++) {
1552:     osy[i] = oly[i] - stencil;
1553:     oey[i] = oly[i] + ly[i] + stencil;
1554:     if (osy[i]<0)osy[i]=0;
1555:     if (oey[i]>M)oey[i]=N;
1556:   }
1557:   for (i=0; i<pP; i++) {
1558:     osz[i] = olz[i] - stencil;
1559:     oez[i] = olz[i] + lz[i] + stencil;
1560:     if (osz[i]<0) osz[i]=0;
1561:     if (oez[i]>P) oez[i]=P;
1562:   }

1564:   for (k=0; k<pP; k++) {
1565:     for (j=0; j<pN; j++) {
1566:       for (i=0; i<pM; i++) {
1567:         char     name[PETSC_MAX_PATH_LEN];
1568:         PetscInt procid = i + j*pM + k*pM*pN; /* convert proc(i,j,k) to pid */
1569:         PetscSNPrintf(name,sizeof(name),"subdomain-%s-p%1.4d.vts",local_file_prefix,procid);
1570:         for (II=0; II<indent_level; II++) PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"  ");

1572:         PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"<Piece Extent=\"%d %d %d %d %d %d\"      Source=\"%s\"/>\n",
1573:                      osx[i],oex[i]-1,
1574:                      osy[j],oey[j]-1,
1575:                      osz[k],oez[k]-1,name);
1576:       }
1577:     }
1578:   }
1579:   PetscFree(olx);
1580:   PetscFree(oly);
1581:   PetscFree(olz);
1582:   PetscFree(osx);
1583:   PetscFree(osy);
1584:   PetscFree(osz);
1585:   PetscFree(oex);
1586:   PetscFree(oey);
1587:   PetscFree(oez);
1588:   return(0);
1589: }

1593: PetscErrorCode DAView_3DVTK_PStructuredGrid(DM da,const char file_prefix[],const char local_file_prefix[])
1594: {
1595:   MPI_Comm       comm;
1596:   PetscMPIInt    size,rank;
1597:   char           vtk_filename[PETSC_MAX_PATH_LEN];
1598:   FILE           *vtk_fp = NULL;
1599:   PetscInt       M,N,P,si,sj,sk,nx,ny,nz;
1600:   PetscInt       i,dofs;

1604:   /* only master generates this file */
1605:   PetscObjectGetComm((PetscObject)da,&comm);
1606:   MPI_Comm_size(comm,&size);
1607:   MPI_Comm_rank(comm,&rank);

1609:   if (rank != 0) return(0);

1611:   /* create file name */
1612:   PetscSNPrintf(vtk_filename,sizeof(vtk_filename),"%s.pvts",file_prefix);
1613:   vtk_fp = fopen(vtk_filename,"w");
1614:   if (!vtk_fp) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"Cannot open file = %s \n",vtk_filename);

1616:   /* (VTK) generate pvts header */
1617:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"<?xml version=\"1.0\"?>\n");

1619: #if defined(PETSC_WORDS_BIGENDIAN)
1620:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"<VTKFile type=\"PStructuredGrid\" version=\"0.1\" byte_order=\"BigEndian\">\n");
1621: #else
1622:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"<VTKFile type=\"PStructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">\n");
1623: #endif

1625:   /* define size of the nodal mesh based on the cell DM */
1626:   DMDAGetInfo(da,0,&M,&N,&P,0,0,0,&dofs,0,0,0,0,0);
1627:   DMDAGetGhostCorners(da,&si,&sj,&sk,&nx,&ny,&nz);
1628:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"  <PStructuredGrid GhostLevel=\"1\" WholeExtent=\"%d %d %d %d %d %d\">\n",0,M-1,0,N-1,0,P-1); /* note overlap = 1 for Q1 */

1630:   /* DUMP THE CELL REFERENCES */
1631:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"    <PCellData>\n");
1632:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"    </PCellData>\n");

1634:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"    <PPoints>\n");
1635:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"      <PDataArray type=\"Float64\" Name=\"Points\" NumberOfComponents=\"%d\"/>\n",NSD);
1636:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"    </PPoints>\n");

1638:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"    <PPointData>\n");
1639:   for (i=0; i<dofs; i++) {
1640:     const char *fieldname;
1641:     DMDAGetFieldName(da,i,&fieldname);
1642:     PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"      <PDataArray type=\"Float64\" Name=\"%s\" NumberOfComponents=\"1\"/>\n",fieldname);
1643:   }
1644:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"    </PPointData>\n");

1646:   /* write out the parallel information */
1647:   DAViewVTK_write_PieceExtend(vtk_fp,2,da,local_file_prefix);

1649:   /* close the file */
1650:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"  </PStructuredGrid>\n");
1651:   PetscFPrintf(PETSC_COMM_SELF,vtk_fp,"</VTKFile>\n");

1653:   if (vtk_fp) {
1654:     fclose(vtk_fp);
1655:     vtk_fp = NULL;
1656:   }
1657:   return(0);
1658: }

1662: PetscErrorCode DAView3DPVTS(DM da, Vec x,const char NAME[])
1663: {
1664:   char           vts_filename[PETSC_MAX_PATH_LEN];
1665:   char           pvts_filename[PETSC_MAX_PATH_LEN];

1669:   PetscSNPrintf(vts_filename,sizeof(vts_filename),"%s-mesh",NAME);
1670:   DAView_3DVTK_StructuredGrid_appended(da,x,vts_filename);

1672:   PetscSNPrintf(pvts_filename,sizeof(pvts_filename),"%s-mesh",NAME);
1673:   DAView_3DVTK_PStructuredGrid(da,pvts_filename,vts_filename);
1674:   return(0);
1675: }

1679: PetscErrorCode KSPMonitorStokesBlocks(KSP ksp,PetscInt n,PetscReal rnorm,void *dummy)
1680: {
1682:   PetscReal      norms[4];
1683:   Vec            Br,v,w;
1684:   Mat            A;

1687:   KSPGetOperators(ksp,&A,NULL);
1688:   MatCreateVecs(A,&w,&v);

1690:   KSPBuildResidual(ksp,v,w,&Br);

1692:   VecStrideNorm(Br,0,NORM_2,&norms[0]);
1693:   VecStrideNorm(Br,1,NORM_2,&norms[1]);
1694:   VecStrideNorm(Br,2,NORM_2,&norms[2]);
1695:   VecStrideNorm(Br,3,NORM_2,&norms[3]);

1697:   VecDestroy(&v);
1698:   VecDestroy(&w);

1700:   PetscPrintf(PETSC_COMM_WORLD,"%3D KSP Component U,V,W,P residual norm [ %1.12e, %1.12e, %1.12e, %1.12e ]\n",n,norms[0],norms[1],norms[2],norms[3]);
1701:   return(0);
1702: }

1706: static PetscErrorCode PCMGSetupViaCoarsen(PC pc,DM da_fine)
1707: {
1708:   PetscInt              nlevels,k;
1709:   PETSC_UNUSED PetscInt finest;
1710:   DM                    *da_list,*daclist;
1711:   Mat                   R;
1712:   PetscErrorCode        ierr;

1715:   nlevels = 1;
1716:   PetscOptionsGetInt(NULL,NULL,"-levels",&nlevels,0);

1718:   PetscMalloc1(nlevels,&da_list);
1719:   for (k=0; k<nlevels; k++) da_list[k] = NULL;
1720:   PetscMalloc1(nlevels,&daclist);
1721:   for (k=0; k<nlevels; k++) daclist[k] = NULL;

1723:   /* finest grid is nlevels - 1 */
1724:   finest     = nlevels - 1;
1725:   daclist[0] = da_fine;
1726:   PetscObjectReference((PetscObject)da_fine);
1727:   DMCoarsenHierarchy(da_fine,nlevels-1,&daclist[1]);
1728:   for (k=0; k<nlevels; k++) {
1729:     da_list[k] = daclist[nlevels-1-k];
1730:     DMDASetUniformCoordinates(da_list[k],0.0,1.0,0.0,1.0,0.0,1.0);
1731:   }

1733:   PCMGSetLevels(pc,nlevels,NULL);
1734:   PCMGSetType(pc,PC_MG_MULTIPLICATIVE);
1735:   PCMGSetGalerkin(pc,PETSC_TRUE);

1737:   for (k=1; k<nlevels; k++) {
1738:     DMCreateInterpolation(da_list[k-1],da_list[k],&R,NULL);
1739:     PCMGSetInterpolation(pc,k,R);
1740:     MatDestroy(&R);
1741:   }

1743:   /* tidy up */
1744:   for (k=0; k<nlevels; k++) {
1745:     DMDestroy(&da_list[k]);
1746:   }
1747:   PetscFree(da_list);
1748:   PetscFree(daclist);
1749:   return(0);
1750: }

1754: static PetscErrorCode solve_stokes_3d_coupled(PetscInt mx,PetscInt my,PetscInt mz)
1755: {
1756:   DM             da_Stokes;
1757:   PetscInt       u_dof,p_dof,dof,stencil_width;
1758:   Mat            A,B;
1759:   PetscInt       mxl,myl,mzl;
1760:   DM             vel_cda;
1761:   Vec            vel_coords;
1762:   PetscInt       p;
1763:   Vec            f,X;
1764:   DMDACoor3d     ***_vel_coords;
1765:   PetscInt       its;
1766:   KSP            ksp_S;
1767:   PetscInt       model_definition = 0;
1768:   PetscInt       ei,ej,ek,sex,sey,sez,Imx,Jmy,Kmz;
1769:   CellProperties cell_properties;
1770:   PetscBool      write_output = PETSC_FALSE;

1774:   /* Generate the da for velocity and pressure */
1775:   /* Num nodes in each direction is mx+1, my+1, mz+1 */
1776:   u_dof         = U_DOFS; /* Vx, Vy - velocities */
1777:   p_dof         = P_DOFS; /* p - pressure */
1778:   dof           = u_dof+p_dof;
1779:   stencil_width = 1;
1780:   DMDACreate3d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,
1781:                                mx+1,my+1,mz+1,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,dof,stencil_width,NULL,NULL,NULL,&da_Stokes);
1782:   DMDASetFieldName(da_Stokes,0,"Vx");
1783:   DMDASetFieldName(da_Stokes,1,"Vy");
1784:   DMDASetFieldName(da_Stokes,2,"Vz");
1785:   DMDASetFieldName(da_Stokes,3,"P");

1787:   /* unit box [0,1] x [0,1] x [0,1] */
1788:   DMDASetUniformCoordinates(da_Stokes,0.0,1.0,0.0,1.0,0.0,1.0);

1790:   /* local number of elements */
1791:   DMDAGetLocalElementSize(da_Stokes,&mxl,&myl,&mzl);

1793:   /* create quadrature point info for PDE coefficients */
1794:   CellPropertiesCreate(da_Stokes,&cell_properties);

1796:   /* interpolate the coordinates to quadrature points */
1797:   DMGetCoordinateDM(da_Stokes,&vel_cda);
1798:   DMGetCoordinatesLocal(da_Stokes,&vel_coords);
1799:   DMDAVecGetArray(vel_cda,vel_coords,&_vel_coords);
1800:   DMDAGetElementCorners(da_Stokes,&sex,&sey,&sez,&Imx,&Jmy,&Kmz);
1801:   for (ek = sez; ek < sez+Kmz; ek++) {
1802:     for (ej = sey; ej < sey+Jmy; ej++) {
1803:       for (ei = sex; ei < sex+Imx; ei++) {
1804:         /* get coords for the element */
1805:         PetscInt               ngp;
1806:         PetscScalar            gp_xi[GAUSS_POINTS][NSD],gp_weight[GAUSS_POINTS];
1807:         PetscScalar            el_coords[NSD*NODES_PER_EL];
1808:         GaussPointCoefficients *cell;

1810:         CellPropertiesGetCell(cell_properties,ei,ej,ek,&cell);
1811:         GetElementCoords3D(_vel_coords,ei,ej,ek,el_coords);
1812:         ConstructGaussQuadrature3D(&ngp,gp_xi,gp_weight);

1814:         for (p = 0; p < GAUSS_POINTS; p++) {
1815:           PetscScalar xi_p[NSD],Ni_p[NODES_PER_EL];
1816:           PetscScalar gp_x,gp_y,gp_z;
1817:           PetscInt    n;

1819:           xi_p[0] = gp_xi[p][0];
1820:           xi_p[1] = gp_xi[p][1];
1821:           xi_p[2] = gp_xi[p][2];
1822:           ShapeFunctionQ13D_Evaluate(xi_p,Ni_p);

1824:           gp_x = gp_y = gp_z = 0.0;
1825:           for (n = 0; n < NODES_PER_EL; n++) {
1826:             gp_x = gp_x+Ni_p[n]*el_coords[NSD*n];
1827:             gp_y = gp_y+Ni_p[n]*el_coords[NSD*n+1];
1828:             gp_z = gp_z+Ni_p[n]*el_coords[NSD*n+2];
1829:           }
1830:           cell->gp_coords[NSD*p]   = gp_x;
1831:           cell->gp_coords[NSD*p+1] = gp_y;
1832:           cell->gp_coords[NSD*p+2] = gp_z;
1833:         }
1834:       }
1835:     }
1836:   }

1838:   PetscOptionsGetInt(NULL,NULL,"-model",&model_definition,NULL);

1840:   switch (model_definition) {
1841:   case 0: /* isoviscous */
1842:     for (ek = sez; ek < sez+Kmz; ek++) {
1843:       for (ej = sey; ej < sey+Jmy; ej++) {
1844:         for (ei = sex; ei < sex+Imx; ei++) {
1845:           GaussPointCoefficients *cell;

1847:           CellPropertiesGetCell(cell_properties,ei,ej,ek,&cell);
1848:           for (p = 0; p < GAUSS_POINTS; p++) {
1849:             PetscReal coord_x = PetscRealPart(cell->gp_coords[NSD*p]);
1850:             PetscReal coord_y = PetscRealPart(cell->gp_coords[NSD*p+1]);
1851:             PetscReal coord_z = PetscRealPart(cell->gp_coords[NSD*p+2]);

1853:             cell->eta[p] = 1.0;

1855:             cell->fx[p] = 0.0*coord_x;
1856:             cell->fy[p] = -PetscSinReal(2.2*PETSC_PI*coord_y)*PetscCosReal(1.0*PETSC_PI*coord_x);
1857:             cell->fz[p] = 0.0*coord_z;
1858:             cell->hc[p] = 0.0;
1859:           }
1860:         }
1861:       }
1862:     }
1863:     break;

1865:   case 1: /* manufactured */
1866:     for (ek = sez; ek < sez+Kmz; ek++) {
1867:       for (ej = sey; ej < sey+Jmy; ej++) {
1868:         for (ei = sex; ei < sex+Imx; ei++) {
1869:           PetscReal              eta,Fm[NSD],Fc,pos[NSD];
1870:           GaussPointCoefficients *cell;

1872:           CellPropertiesGetCell(cell_properties,ei,ej,ek,&cell);
1873:           for (p = 0; p < GAUSS_POINTS; p++) {
1874:             PetscReal coord_x = PetscRealPart(cell->gp_coords[NSD*p]);
1875:             PetscReal coord_y = PetscRealPart(cell->gp_coords[NSD*p+1]);
1876:             PetscReal coord_z = PetscRealPart(cell->gp_coords[NSD*p+2]);

1878:             pos[0] = coord_x;
1879:             pos[1] = coord_y;
1880:             pos[2] = coord_z;

1882:             evaluate_MS_FrankKamentski(pos,NULL,NULL,&eta,Fm,&Fc);
1883:             cell->eta[p] = eta;

1885:             cell->fx[p] = Fm[0];
1886:             cell->fy[p] = Fm[1];
1887:             cell->fz[p] = Fm[2];
1888:             cell->hc[p] = Fc;
1889:           }
1890:         }
1891:       }
1892:     }
1893:     break;

1895:   case 2: /* solcx */
1896:     for (ek = sez; ek < sez+Kmz; ek++) {
1897:       for (ej = sey; ej < sey+Jmy; ej++) {
1898:         for (ei = sex; ei < sex+Imx; ei++) {
1899:           GaussPointCoefficients *cell;

1901:           CellPropertiesGetCell(cell_properties,ei,ej,ek,&cell);
1902:           for (p = 0; p < GAUSS_POINTS; p++) {
1903:             PetscReal coord_x = PetscRealPart(cell->gp_coords[NSD*p]);
1904:             PetscReal coord_y = PetscRealPart(cell->gp_coords[NSD*p+1]);
1905:             PetscReal coord_z = PetscRealPart(cell->gp_coords[NSD*p+2]);

1907:             cell->eta[p] = 1.0;

1909:             cell->fx[p] = 0.0;
1910:             cell->fy[p] = -PetscSinReal(3.0*PETSC_PI*coord_y)*PetscCosReal(1.0*PETSC_PI*coord_x);
1911:             cell->fz[p] = 0.0*coord_z;
1912:             cell->hc[p] = 0.0;
1913:           }
1914:         }
1915:       }
1916:     }
1917:     break;

1919:   case 3: /* sinker */
1920:     for (ek = sez; ek < sez+Kmz; ek++) {
1921:       for (ej = sey; ej < sey+Jmy; ej++) {
1922:         for (ei = sex; ei < sex+Imx; ei++) {
1923:           GaussPointCoefficients *cell;

1925:           CellPropertiesGetCell(cell_properties,ei,ej,ek,&cell);
1926:           for (p = 0; p < GAUSS_POINTS; p++) {
1927:             PetscReal xp = PetscRealPart(cell->gp_coords[NSD*p]);
1928:             PetscReal yp = PetscRealPart(cell->gp_coords[NSD*p+1]);
1929:             PetscReal zp = PetscRealPart(cell->gp_coords[NSD*p+2]);

1931:             cell->eta[p] = 1.0e-2;
1932:             cell->fx[p]  = 0.0;
1933:             cell->fy[p]  = 0.0;
1934:             cell->fz[p]  = 0.0;
1935:             cell->hc[p]  = 0.0;

1937:             if ((fabs(xp-0.5) < 0.2) &&
1938:                 (fabs(yp-0.5) < 0.2) &&
1939:                 (fabs(zp-0.5) < 0.2)) {
1940:               cell->eta[p] = 1.0;
1941:               cell->fz[p]  = 1.0;
1942:             }

1944:           }
1945:         }
1946:       }
1947:     }
1948:     break;

1950:   default:
1951:     SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"No default model is supported. Choose either -model {0,1,2,3}");
1952:     break;
1953:   }

1955:   DMDAVecRestoreArray(vel_cda,vel_coords,&_vel_coords);

1957:   /* Generate a matrix with the correct non-zero pattern of type AIJ. This will work in parallel and serial */
1958:   DMSetMatType(da_Stokes,MATAIJ);
1959:   DMCreateMatrix(da_Stokes,&A);
1960:   DMCreateMatrix(da_Stokes,&B);
1961:   DMCreateGlobalVector(da_Stokes,&X);
1962:   DMCreateGlobalVector(da_Stokes,&f);

1964:   /* assemble A11 */
1965:   MatZeroEntries(A);
1966:   MatZeroEntries(B);
1967:   VecZeroEntries(f);

1969:   AssembleA_Stokes(A,da_Stokes,cell_properties);
1970:   AssembleA_PCStokes(B,da_Stokes,cell_properties);
1971:   /* build force vector */
1972:   AssembleF_Stokes(f,da_Stokes,cell_properties);

1974:   /* SOLVE */
1975:   KSPCreate(PETSC_COMM_WORLD,&ksp_S);
1976:   KSPSetOptionsPrefix(ksp_S,"stokes_"); /* stokes */
1977:   KSPSetOperators(ksp_S,A,B);
1978:   KSPSetFromOptions(ksp_S);

1980:   {
1981:     PC             pc;
1982:     const PetscInt ufields[] = {0,1,2},pfields[] = {3};
1983:     KSPGetPC(ksp_S,&pc);
1984:     PCFieldSplitSetBlockSize(pc,4);
1985:     PCFieldSplitSetFields(pc,"u",3,ufields,ufields);
1986:     PCFieldSplitSetFields(pc,"p",1,pfields,pfields);
1987:   }

1989:   {
1990:     PC        pc;
1991:     PetscBool same = PETSC_FALSE;
1992:     KSPGetPC(ksp_S,&pc);
1993:     PetscObjectTypeCompare((PetscObject)pc,PCMG,&same);
1994:     if (same) {
1995:       PCMGSetupViaCoarsen(pc,da_Stokes);
1996:     }
1997:   }

1999:   {
2000:     PetscBool stokes_monitor = PETSC_FALSE;
2001:     PetscOptionsGetBool(NULL,NULL,"-stokes_ksp_monitor_blocks",&stokes_monitor,0);
2002:     if (stokes_monitor) {
2003:       KSPMonitorSet(ksp_S,KSPMonitorStokesBlocks,NULL,NULL);
2004:     }
2005:   }
2006:   KSPSolve(ksp_S,f,X);

2008:   PetscOptionsGetBool(NULL,NULL,"-write_pvts",&write_output,NULL);
2009:   if (write_output) {DAView3DPVTS(da_Stokes,X,"up");}
2010:   {
2011:     PetscBool flg = PETSC_FALSE;
2012:     char      filename[PETSC_MAX_PATH_LEN];
2013:     PetscOptionsGetString(NULL,NULL,"-write_binary",filename,sizeof(filename),&flg);
2014:     if (flg) {
2015:       PetscViewer viewer;
2016:       /* PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename[0]?filename:"ex42-binaryoutput",FILE_MODE_WRITE,&viewer); */
2017:       PetscViewerVTKOpen(PETSC_COMM_WORLD,"ex42.vts",FILE_MODE_WRITE,&viewer);
2018:       VecView(X,viewer);
2019:       PetscViewerDestroy(&viewer);
2020:     }
2021:   }
2022:   KSPGetIterationNumber(ksp_S,&its);

2024:   /* verify */
2025:   if (model_definition == 1) {
2026:     DM  da_Stokes_analytic;
2027:     Vec X_analytic;

2029:     DMDACreateManufacturedSolution(mx,my,mz,&da_Stokes_analytic,&X_analytic);
2030:     if (write_output) {
2031:       DAView3DPVTS(da_Stokes_analytic,X_analytic,"ms");
2032:     }
2033:     DMDAIntegrateErrors3D(da_Stokes_analytic,X,X_analytic);
2034:     if (write_output) {
2035:       DAView3DPVTS(da_Stokes,X,"up2");
2036:     }
2037:     DMDestroy(&da_Stokes_analytic);
2038:     VecDestroy(&X_analytic);
2039:   }

2041:   KSPDestroy(&ksp_S);
2042:   VecDestroy(&X);
2043:   VecDestroy(&f);
2044:   MatDestroy(&A);
2045:   MatDestroy(&B);

2047:   CellPropertiesDestroy(&cell_properties);
2048:   DMDestroy(&da_Stokes);
2049:   return(0);
2050: }

2054: int main(int argc,char **args)
2055: {
2057:   PetscInt       mx,my,mz;

2059:   PetscInitialize(&argc,&args,(char*)0,help);

2061:   mx   = my = mz = 10;
2062:   PetscOptionsGetInt(NULL,NULL,"-mx",&mx,NULL);
2063:   my   = mx; mz = mx;
2064:   PetscOptionsGetInt(NULL,NULL,"-my",&my,NULL);
2065:   PetscOptionsGetInt(NULL,NULL,"-mz",&mz,NULL);

2067:   solve_stokes_3d_coupled(mx,my,mz);

2069:   PetscFinalize();
2070:   return 0;
2071: }