Actual source code: ex19.c

  2: static char help[] = "Nonlinear driven cavity with multigrid in 2d.\n\
  3:   \n\
  4: The 2D driven cavity problem is solved in a velocity-vorticity formulation.\n\
  5: The flow can be driven with the lid or with bouyancy or both:\n\
  6:   -lidvelocity <lid>, where <lid> = dimensionless velocity of lid\n\
  7:   -grashof <gr>, where <gr> = dimensionless temperature gradent\n\
  8:   -prandtl <pr>, where <pr> = dimensionless thermal/momentum diffusity ratio\n\
  9:   -contours : draw contour plots of solution\n\n";

 11: /*T
 12:    Concepts: SNES^solving a system of nonlinear equations (parallel multicomponent example);
 13:    Concepts: DA^using distributed arrays;
 14:    Concepts: multicomponent
 15:    Processors: n
 16: T*/

 18: /* ------------------------------------------------------------------------

 20:     We thank David E. Keyes for contributing the driven cavity discretization
 21:     within this example code.

 23:     This problem is modeled by the partial differential equation system
 24:   
 25:         - Lap(U) - Grad_y(Omega) = 0
 26:         - Lap(V) + Grad_x(Omega) = 0
 27:         - Lap(Omega) + Div([U*Omega,V*Omega]) - GR*Grad_x(T) = 0
 28:         - Lap(T) + PR*Div([U*T,V*T]) = 0

 30:     in the unit square, which is uniformly discretized in each of x and
 31:     y in this simple encoding.

 33:     No-slip, rigid-wall Dirichlet conditions are used for [U,V].
 34:     Dirichlet conditions are used for Omega, based on the definition of
 35:     vorticity: Omega = - Grad_y(U) + Grad_x(V), where along each
 36:     constant coordinate boundary, the tangential derivative is zero.
 37:     Dirichlet conditions are used for T on the left and right walls,
 38:     and insulation homogeneous Neumann conditions are used for T on
 39:     the top and bottom walls. 

 41:     A finite difference approximation with the usual 5-point stencil 
 42:     is used to discretize the boundary value problem to obtain a 
 43:     nonlinear system of equations.  Upwinding is used for the divergence
 44:     (convective) terms and central for the gradient (source) terms.
 45:     
 46:     The Jacobian can be either
 47:       * formed via finite differencing using coloring (the default), or
 48:       * applied matrix-free via the option -snes_mf 
 49:         (for larger grid problems this variant may not converge 
 50:         without a preconditioner due to ill-conditioning).

 52:   ------------------------------------------------------------------------- */

 54: /* 
 55:    Include "petscda.h" so that we can use distributed arrays (DAs).
 56:    Include "petscsnes.h" so that we can use SNES solvers.  Note that this
 57:    file automatically includes:
 58:      petsc.h       - base PETSc routines   petscvec.h - vectors
 59:      petscsys.h    - system routines       petscmat.h - matrices
 60:      petscis.h     - index sets            petscksp.h - Krylov subspace methods
 61:      petscviewer.h - viewers               petscpc.h  - preconditioners
 62:      petscksp.h   - linear solvers 
 63: */
 64:  #include petscsnes.h
 65:  #include petscda.h
 66:  #include petscdmmg.h

 68: /* 
 69:    User-defined routines and data structures
 70: */
 71: typedef struct {
 72:   PetscScalar u,v,omega,temp;
 73: } Field;


 80: typedef struct {
 81:    PassiveReal  lidvelocity,prandtl,grashof;  /* physical parameters */
 82:    PetscTruth     draw_contours;                /* flag - 1 indicates drawing contours */
 83: } AppCtx;

 87: int main(int argc,char **argv)
 88: {
 89:   DMMG           *dmmg;               /* multilevel grid structure */
 90:   AppCtx         user;                /* user-defined work context */
 91:   PetscInt       mx,my,its;
 93:   MPI_Comm       comm;
 94:   SNES           snes;
 95:   DA             da;

 97:   PetscInitialize(&argc,&argv,(char *)0,help);
 98:   comm = PETSC_COMM_WORLD;


101:   PreLoadBegin(PETSC_TRUE,"SetUp");
102:     DMMGCreate(comm,2,&user,&dmmg);

104:     /*
105:       Create distributed array multigrid object (DMMG) to manage parallel grid and vectors
106:       for principal unknowns (x) and governing residuals (f)
107:     */
108:     DACreate2d(comm,DA_NONPERIODIC,DA_STENCIL_STAR,-4,-4,PETSC_DECIDE,PETSC_DECIDE,4,1,0,0,&da);
109:     DMMGSetDM(dmmg,(DM)da);
110:     DADestroy(da);

112:     DAGetInfo(DMMGGetDA(dmmg),0,&mx,&my,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,
113:                      PETSC_IGNORE,PETSC_IGNORE);
114:     /* 
115:      Problem parameters (velocity of lid, prandtl, and grashof numbers)
116:     */
117:     user.lidvelocity = 1.0/(mx*my);
118:     user.prandtl     = 1.0;
119:     user.grashof     = 1.0;
120:     PetscOptionsGetReal(PETSC_NULL,"-lidvelocity",&user.lidvelocity,PETSC_NULL);
121:     PetscOptionsGetReal(PETSC_NULL,"-prandtl",&user.prandtl,PETSC_NULL);
122:     PetscOptionsGetReal(PETSC_NULL,"-grashof",&user.grashof,PETSC_NULL);
123:     PetscOptionsHasName(PETSC_NULL,"-contours",&user.draw_contours);

125:     DASetFieldName(DMMGGetDA(dmmg),0,"x-velocity");
126:     DASetFieldName(DMMGGetDA(dmmg),1,"y-velocity");
127:     DASetFieldName(DMMGGetDA(dmmg),2,"Omega");
128:     DASetFieldName(DMMGGetDA(dmmg),3,"temperature");

130:     /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
131:        Create user context, set problem data, create vector data structures.
132:        Also, compute the initial guess.
133:        - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

135:     /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
136:        Create nonlinear solver context

138:        Process adiC(36): FormFunctionLocal FormFunctionLocali
139:        Process blockadiC(4): FormFunctionLocali4
140:        - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
141:     DMMGSetSNESLocal(dmmg,FormFunctionLocal,0,ad_FormFunctionLocal,admf_FormFunctionLocal);
142:     DMMGSetSNESLocali(dmmg,FormFunctionLocali,0,admf_FormFunctionLocali);
143:     DMMGSetSNESLocalib(dmmg,FormFunctionLocali4,0,admfb_FormFunctionLocali4);

145:     PetscPrintf(comm,"lid velocity = %G, prandtl # = %G, grashof # = %G\n",
146:                        user.lidvelocity,user.prandtl,user.grashof);


149:     /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
150:        Solve the nonlinear system
151:        - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
152:     DMMGSetInitialGuess(dmmg,FormInitialGuess);

154:   PreLoadStage("Solve");
155:     DMMGSolve(dmmg);

157:     snes = DMMGGetSNES(dmmg);
158:     SNESGetIterationNumber(snes,&its);
159:     PetscPrintf(comm,"Number of Newton iterations = %D\n", its);

161:     /*
162:       Visualize solution
163:     */

165:     if (user.draw_contours) {
166:       VecView(DMMGGetx(dmmg),PETSC_VIEWER_DRAW_WORLD);
167:     }

169:     /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
170:        Free work space.  All PETSc objects should be destroyed when they
171:        are no longer needed.
172:        - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

174:     DMMGDestroy(dmmg);
175:   PreLoadEnd();

177:   PetscFinalize();
178:   return 0;
179: }

181: /* ------------------------------------------------------------------- */


186: /* 
187:    FormInitialGuess - Forms initial approximation.

189:    Input Parameters:
190:    user - user-defined application context
191:    X - vector

193:    Output Parameter:
194:    X - vector
195:  */
196: PetscErrorCode FormInitialGuess(DMMG dmmg,Vec X)
197: {
198:   AppCtx         *user = (AppCtx*)dmmg->user;
199:   DA             da = (DA)dmmg->dm;
200:   PetscInt       i,j,mx,xs,ys,xm,ym;
202:   PetscReal      grashof,dx;
203:   Field          **x;

205:   grashof = user->grashof;

207:   DAGetInfo(da,0,&mx,0,0,0,0,0,0,0,0,0);
208:   dx  = 1.0/(mx-1);

210:   /*
211:      Get local grid boundaries (for 2-dimensional DA):
212:        xs, ys   - starting grid indices (no ghost points)
213:        xm, ym   - widths of local grid (no ghost points)
214:   */
215:   DAGetCorners(da,&xs,&ys,PETSC_NULL,&xm,&ym,PETSC_NULL);

217:   /*
218:      Get a pointer to vector data.
219:        - For default PETSc vectors, VecGetArray() returns a pointer to
220:          the data array.  Otherwise, the routine is implementation dependent.
221:        - You MUST call VecRestoreArray() when you no longer need access to
222:          the array.
223:   */
224:   DAVecGetArray(da,X,&x);

226:   /*
227:      Compute initial guess over the locally owned part of the grid
228:      Initial condition is motionless fluid and equilibrium temperature
229:   */
230:   for (j=ys; j<ys+ym; j++) {
231:     for (i=xs; i<xs+xm; i++) {
232:       x[j][i].u     = 0.0;
233:       x[j][i].v     = 0.0;
234:       x[j][i].omega = 0.0;
235:       x[j][i].temp  = (grashof>0)*i*dx;
236:     }
237:   }

239:   /*
240:      Restore vector
241:   */
242:   DAVecRestoreArray(da,X,&x);
243:   return 0;
244: }
245: 
246: PetscErrorCode FormFunctionLocal(DALocalInfo *info,Field **x,Field **f,void *ptr)
247:  {
248:   AppCtx         *user = (AppCtx*)ptr;
250:   PetscInt       xints,xinte,yints,yinte,i,j;
251:   PetscReal      hx,hy,dhx,dhy,hxdhy,hydhx;
252:   PetscReal      grashof,prandtl,lid;
253:   PetscScalar    u,uxx,uyy,vx,vy,avx,avy,vxp,vxm,vyp,vym;

256:   grashof = user->grashof;
257:   prandtl = user->prandtl;
258:   lid     = user->lidvelocity;

260:   /* 
261:      Define mesh intervals ratios for uniform grid.
262:      [Note: FD formulae below are normalized by multiplying through by
263:      local volume element to obtain coefficients O(1) in two dimensions.]
264:   */
265:   dhx = (PetscReal)(info->mx-1);  dhy = (PetscReal)(info->my-1);
266:   hx = 1.0/dhx;                   hy = 1.0/dhy;
267:   hxdhy = hx*dhy;                 hydhx = hy*dhx;

269:   xints = info->xs; xinte = info->xs+info->xm; yints = info->ys; yinte = info->ys+info->ym;

271:   /* Test whether we are on the bottom edge of the global array */
272:   if (yints == 0) {
273:     j = 0;
274:     yints = yints + 1;
275:     /* bottom edge */
276:     for (i=info->xs; i<info->xs+info->xm; i++) {
277:       f[j][i].u     = x[j][i].u;
278:       f[j][i].v     = x[j][i].v;
279:       f[j][i].omega = x[j][i].omega + (x[j+1][i].u - x[j][i].u)*dhy;
280:       f[j][i].temp  = x[j][i].temp-x[j+1][i].temp;
281:     }
282:   }

284:   /* Test whether we are on the top edge of the global array */
285:   if (yinte == info->my) {
286:     j = info->my - 1;
287:     yinte = yinte - 1;
288:     /* top edge */
289:     for (i=info->xs; i<info->xs+info->xm; i++) {
290:         f[j][i].u     = x[j][i].u - lid;
291:         f[j][i].v     = x[j][i].v;
292:         f[j][i].omega = x[j][i].omega + (x[j][i].u - x[j-1][i].u)*dhy;
293:         f[j][i].temp  = x[j][i].temp-x[j-1][i].temp;
294:     }
295:   }

297:   /* Test whether we are on the left edge of the global array */
298:   if (xints == 0) {
299:     i = 0;
300:     xints = xints + 1;
301:     /* left edge */
302:     for (j=info->ys; j<info->ys+info->ym; j++) {
303:       f[j][i].u     = x[j][i].u;
304:       f[j][i].v     = x[j][i].v;
305:       f[j][i].omega = x[j][i].omega - (x[j][i+1].v - x[j][i].v)*dhx;
306:       f[j][i].temp  = x[j][i].temp;
307:     }
308:   }

310:   /* Test whether we are on the right edge of the global array */
311:   if (xinte == info->mx) {
312:     i = info->mx - 1;
313:     xinte = xinte - 1;
314:     /* right edge */
315:     for (j=info->ys; j<info->ys+info->ym; j++) {
316:       f[j][i].u     = x[j][i].u;
317:       f[j][i].v     = x[j][i].v;
318:       f[j][i].omega = x[j][i].omega - (x[j][i].v - x[j][i-1].v)*dhx;
319:       f[j][i].temp  = x[j][i].temp - (PetscReal)(grashof>0);
320:     }
321:   }

323:   /* Compute over the interior points */
324:   for (j=yints; j<yinte; j++) {
325:     for (i=xints; i<xinte; i++) {

327:         /*
328:           convective coefficients for upwinding
329:         */
330:         vx = x[j][i].u; avx = PetscAbsScalar(vx);
331:         vxp = .5*(vx+avx); vxm = .5*(vx-avx);
332:         vy = x[j][i].v; avy = PetscAbsScalar(vy);
333:         vyp = .5*(vy+avy); vym = .5*(vy-avy);

335:         /* U velocity */
336:         u          = x[j][i].u;
337:         uxx        = (2.0*u - x[j][i-1].u - x[j][i+1].u)*hydhx;
338:         uyy        = (2.0*u - x[j-1][i].u - x[j+1][i].u)*hxdhy;
339:         f[j][i].u  = uxx + uyy - .5*(x[j+1][i].omega-x[j-1][i].omega)*hx;

341:         /* V velocity */
342:         u          = x[j][i].v;
343:         uxx        = (2.0*u - x[j][i-1].v - x[j][i+1].v)*hydhx;
344:         uyy        = (2.0*u - x[j-1][i].v - x[j+1][i].v)*hxdhy;
345:         f[j][i].v  = uxx + uyy + .5*(x[j][i+1].omega-x[j][i-1].omega)*hy;

347:         /* Omega */
348:         u          = x[j][i].omega;
349:         uxx        = (2.0*u - x[j][i-1].omega - x[j][i+1].omega)*hydhx;
350:         uyy        = (2.0*u - x[j-1][i].omega - x[j+1][i].omega)*hxdhy;
351:         f[j][i].omega = uxx + uyy +
352:                         (vxp*(u - x[j][i-1].omega) +
353:                           vxm*(x[j][i+1].omega - u)) * hy +
354:                         (vyp*(u - x[j-1][i].omega) +
355:                           vym*(x[j+1][i].omega - u)) * hx -
356:                         .5 * grashof * (x[j][i+1].temp - x[j][i-1].temp) * hy;

358:         /* Temperature */
359:         u             = x[j][i].temp;
360:         uxx           = (2.0*u - x[j][i-1].temp - x[j][i+1].temp)*hydhx;
361:         uyy           = (2.0*u - x[j-1][i].temp - x[j+1][i].temp)*hxdhy;
362:         f[j][i].temp =  uxx + uyy  + prandtl * (
363:                         (vxp*(u - x[j][i-1].temp) +
364:                           vxm*(x[j][i+1].temp - u)) * hy +
365:                         (vyp*(u - x[j-1][i].temp) +
366:                                  vym*(x[j+1][i].temp - u)) * hx);
367:     }
368:   }

370:   /*
371:      Flop count (multiply-adds are counted as 2 operations)
372:   */
373:   PetscLogFlops(84*info->ym*info->xm);
374:   return(0);
375: }

377: /*
378:     This function that evaluates the function for a single 
379:     degree of freedom. It is used by the -dmmg_fas solver
380: */
381: PetscErrorCode FormFunctionLocali(DALocalInfo *info,MatStencil *st,Field **x,PetscScalar *f,void *ptr)
382: {
383:   AppCtx      *user = (AppCtx*)ptr;
384:   PetscInt    i,j,c;
385:   PassiveReal hx,hy,dhx,dhy,hxdhy,hydhx;
386:   PassiveReal grashof,prandtl,lid;
387:   PetscScalar u,uxx,uyy,vx,vy,avx,avy,vxp,vxm,vyp,vym;

390:   grashof = user->grashof;
391:   prandtl = user->prandtl;
392:   lid     = user->lidvelocity;

394:   /* 
395:      Define mesh intervals ratios for uniform grid.
396:      [Note: FD formulae below are normalized by multiplying through by
397:      local volume element to obtain coefficients O(1) in two dimensions.]
398:   */
399:   dhx = (PetscReal)(info->mx-1);     dhy = (PetscReal)(info->my-1);
400:   hx = 1.0/dhx;                   hy = 1.0/dhy;
401:   hxdhy = hx*dhy;                 hydhx = hy*dhx;

403:   i = st->i; j = st->j; c = st->c;

405:   /* Test whether we are on the right edge of the global array */
406:   if (i == info->mx-1) {
407:     if (c == 0)      *f = x[j][i].u;
408:     else if (c == 1) *f = x[j][i].v;
409:     else if (c == 2) *f = x[j][i].omega - (x[j][i].v - x[j][i-1].v)*dhx;
410:     else             *f = x[j][i].temp - (PetscReal)(grashof>0);

412:   /* Test whether we are on the left edge of the global array */
413:   } else if (i == 0) {
414:     if (c == 0)      *f = x[j][i].u;
415:     else if (c == 1) *f = x[j][i].v;
416:     else if (c == 2) *f = x[j][i].omega - (x[j][i+1].v - x[j][i].v)*dhx;
417:     else             *f = x[j][i].temp;

419:   /* Test whether we are on the top edge of the global array */
420:   } else if (j == info->my-1) {
421:     if (c == 0)      *f = x[j][i].u - lid;
422:     else if (c == 1) *f = x[j][i].v;
423:     else if (c == 2) *f = x[j][i].omega + (x[j][i].u - x[j-1][i].u)*dhy;
424:     else             *f = x[j][i].temp-x[j-1][i].temp;

426:   /* Test whether we are on the bottom edge of the global array */
427:   } else if (j == 0) {
428:     if (c == 0)      *f = x[j][i].u;
429:     else if (c == 1) *f = x[j][i].v;
430:     else if (c == 2) *f = x[j][i].omega + (x[j+1][i].u - x[j][i].u)*dhy;
431:     else             *f = x[j][i].temp - x[j+1][i].temp;

433:   /* Compute over the interior points */
434:   } else {
435:     /*
436:       convective coefficients for upwinding
437:     */
438:     vx = x[j][i].u; avx = PetscAbsScalar(vx);
439:     vxp = .5*(vx+avx); vxm = .5*(vx-avx);
440:     vy = x[j][i].v; avy = PetscAbsScalar(vy);
441:     vyp = .5*(vy+avy); vym = .5*(vy-avy);

443:     /* U velocity */
444:     if (c == 0) {
445:       u          = x[j][i].u;
446:       uxx        = (2.0*u - x[j][i-1].u - x[j][i+1].u)*hydhx;
447:       uyy        = (2.0*u - x[j-1][i].u - x[j+1][i].u)*hxdhy;
448:       *f         = uxx + uyy - .5*(x[j+1][i].omega-x[j-1][i].omega)*hx;

450:     /* V velocity */
451:     } else if (c == 1) {
452:       u          = x[j][i].v;
453:       uxx        = (2.0*u - x[j][i-1].v - x[j][i+1].v)*hydhx;
454:       uyy        = (2.0*u - x[j-1][i].v - x[j+1][i].v)*hxdhy;
455:       *f         = uxx + uyy + .5*(x[j][i+1].omega-x[j][i-1].omega)*hy;
456: 
457:     /* Omega */
458:     } else if (c == 2) {
459:       u          = x[j][i].omega;
460:       uxx        = (2.0*u - x[j][i-1].omega - x[j][i+1].omega)*hydhx;
461:       uyy        = (2.0*u - x[j-1][i].omega - x[j+1][i].omega)*hxdhy;
462:       *f         = uxx + uyy +
463:         (vxp*(u - x[j][i-1].omega) +
464:          vxm*(x[j][i+1].omega - u)) * hy +
465:         (vyp*(u - x[j-1][i].omega) +
466:          vym*(x[j+1][i].omega - u)) * hx -
467:          .5 * grashof * (x[j][i+1].temp - x[j][i-1].temp) * hy;
468: 
469:     /* Temperature */
470:     } else {
471:       u           = x[j][i].temp;
472:       uxx         = (2.0*u - x[j][i-1].temp - x[j][i+1].temp)*hydhx;
473:       uyy         = (2.0*u - x[j-1][i].temp - x[j+1][i].temp)*hxdhy;
474:       *f          =  uxx + uyy  + prandtl * (
475:                                              (vxp*(u - x[j][i-1].temp) +
476:                                               vxm*(x[j][i+1].temp - u)) * hy +
477:                                              (vyp*(u - x[j-1][i].temp) +
478:                                              vym*(x[j+1][i].temp - u)) * hx);
479:     }
480:   }

482:   return(0);
483: }

485: /*
486:     This function that evaluates the function for a single 
487:     grid point. It is used by the -dmmg_fas -dmmg_fas_block solver
488: */
489: PetscErrorCode FormFunctionLocali4(DALocalInfo *info,MatStencil *st,Field **x,PetscScalar *ff,void *ptr)
490: {
491:   Field       *f = (Field*)ff;
492:   AppCtx      *user = (AppCtx*)ptr;
493:   PetscInt    i,j;
494:   PassiveReal hx,hy,dhx,dhy,hxdhy,hydhx;
495:   PassiveReal grashof,prandtl,lid;
496:   PetscScalar u,uxx,uyy,vx,vy,avx,avy,vxp,vxm,vyp,vym;

499:   grashof = user->grashof;
500:   prandtl = user->prandtl;
501:   lid     = user->lidvelocity;

503:   /* 
504:      Define mesh intervals ratios for uniform grid.
505:      [Note: FD formulae below are normalized by multiplying through by
506:      local volume element to obtain coefficients O(1) in two dimensions.]
507:   */
508:   dhx = (PetscReal)(info->mx-1);     dhy = (PetscReal)(info->my-1);
509:   hx = 1.0/dhx;                   hy = 1.0/dhy;
510:   hxdhy = hx*dhy;                 hydhx = hy*dhx;

512:   i = st->i; j = st->j;

514:   /* Test whether we are on the right edge of the global array */
515:   if (i == info->mx-1) {
516:     f->u = x[j][i].u;
517:     f->v = x[j][i].v;
518:     f->omega = x[j][i].omega - (x[j][i].v - x[j][i-1].v)*dhx;
519:     f->temp = x[j][i].temp - (PetscReal)(grashof>0);

521:     /* Test whether we are on the left edge of the global array */
522:   } else if (i == 0) {
523:     f->u = x[j][i].u;
524:     f->v = x[j][i].v;
525:     f->omega = x[j][i].omega - (x[j][i+1].v - x[j][i].v)*dhx;
526:     f->temp = x[j][i].temp;
527: 
528:     /* Test whether we are on the top edge of the global array */
529:   } else if (j == info->my-1) {
530:     f->u = x[j][i].u - lid;
531:     f->v = x[j][i].v;
532:     f->omega = x[j][i].omega + (x[j][i].u - x[j-1][i].u)*dhy;
533:     f->temp = x[j][i].temp-x[j-1][i].temp;

535:     /* Test whether we are on the bottom edge of the global array */
536:   } else if (j == 0) {
537:     f->u = x[j][i].u;
538:     f->v = x[j][i].v;
539:     f->omega = x[j][i].omega + (x[j+1][i].u - x[j][i].u)*dhy;
540:     f->temp = x[j][i].temp - x[j+1][i].temp;

542:     /* Compute over the interior points */
543:   } else {
544:     /*
545:       convective coefficients for upwinding
546:     */
547:     vx = x[j][i].u; avx = PetscAbsScalar(vx);
548:     vxp = .5*(vx+avx); vxm = .5*(vx-avx);
549:     vy = x[j][i].v; avy = PetscAbsScalar(vy);
550:     vyp = .5*(vy+avy); vym = .5*(vy-avy);

552:     /* U velocity */
553:     u          = x[j][i].u;
554:     uxx        = (2.0*u - x[j][i-1].u - x[j][i+1].u)*hydhx;
555:     uyy        = (2.0*u - x[j-1][i].u - x[j+1][i].u)*hxdhy;
556:     f->u        = uxx + uyy - .5*(x[j+1][i].omega-x[j-1][i].omega)*hx;

558:     /* V velocity */
559:     u          = x[j][i].v;
560:     uxx        = (2.0*u - x[j][i-1].v - x[j][i+1].v)*hydhx;
561:     uyy        = (2.0*u - x[j-1][i].v - x[j+1][i].v)*hxdhy;
562:     f->v        = uxx + uyy + .5*(x[j][i+1].omega-x[j][i-1].omega)*hy;
563: 
564:     /* Omega */
565:     u          = x[j][i].omega;
566:     uxx        = (2.0*u - x[j][i-1].omega - x[j][i+1].omega)*hydhx;
567:     uyy        = (2.0*u - x[j-1][i].omega - x[j+1][i].omega)*hxdhy;
568:     f->omega    = uxx + uyy +
569:       (vxp*(u - x[j][i-1].omega) +
570:        vxm*(x[j][i+1].omega - u)) * hy +
571:       (vyp*(u - x[j-1][i].omega) +
572:        vym*(x[j+1][i].omega - u)) * hx -
573:        .5 * grashof * (x[j][i+1].temp - x[j][i-1].temp) * hy;
574: 
575:     /* Temperature */
576: 
577:     u           = x[j][i].temp;
578:     uxx         = (2.0*u - x[j][i-1].temp - x[j][i+1].temp)*hydhx;
579:     uyy         = (2.0*u - x[j-1][i].temp - x[j+1][i].temp)*hxdhy;
580:     f->temp     =  uxx + uyy  + prandtl * (
581:                                            (vxp*(u - x[j][i-1].temp) +
582:                                             vxm*(x[j][i+1].temp - u)) * hy +
583:                                            (vyp*(u - x[j-1][i].temp) +
584:                                             vym*(x[j+1][i].temp - u)) * hx);
585:   }
586:   return(0);
587: }