Actual source code: ff2.c

  1: #include "mp.h"
  2: /*
  3:          Defines the tempature physics for a given u, v, omega
  4: */

  8: PetscErrorCode FormInitialGuessLocal2(DMDALocalInfo *info,Field2 **x,AppCtx *user)
  9: {
 10:   PetscInt       i,j;
 11:   PetscScalar    dx;

 13:   dx  = 1.0/(info->mx-1);
 14:   for (j=info->ys; j<info->ys+info->ym; j++) {
 15:     for (i=info->xs; i<info->xs+info->xm; i++) {
 16:       x[j][i].temp  = .1 + ((PetscReal)(user->grashof>0))*i*dx;
 17:     }
 18:   }
 19:   return 0;
 20: }

 22: PetscLogEvent EVENT_FORMFUNCTIONLOCAL2;

 26: /* 
 27:       
 28:      x1 contains given velocity field

 30: */
 31: PetscErrorCode FormFunctionLocal2(DMDALocalInfo *info,Field1**x1,Field2 **x,Field2 **f,void *ptr)
 32:  {
 33:   AppCtx         *user = (AppCtx*)ptr;
 34:   PetscInt       xints,xinte,yints,yinte,i,j;
 35:   PetscReal      hx,hy,dhx,dhy,hxdhy,hydhx;
 36:   PetscReal      grashof,prandtl; /* ,lid */
 37:   PetscScalar    u,uxx,uyy,vx,vy,avx,avy,vxp,vxm,vyp,vym;

 41:   PetscLogEventBegin(EVENT_FORMFUNCTIONLOCAL2,0,0,0,0);
 42:   grashof = user->grashof;
 43:   prandtl = user->prandtl;
 44:   /* lid     = user->lidvelocity; */

 46:   dhx = (PetscReal)(info->mx-1);  dhy = (PetscReal)(info->my-1);
 47:   hx = 1.0/dhx;                   hy = 1.0/dhy;
 48:   hxdhy = hx*dhy;                 hydhx = hy*dhx;

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

 52:   /* Test whether we are on the bottom edge of the global array */
 53:   if (yints == 0) {
 54:     j = 0;
 55:     yints = yints + 1;
 56:     /* bottom edge */
 57:     for (i=info->xs; i<info->xs+info->xm; i++) {
 58:       f[j][i].temp  = x[j][i].temp-x[j+1][i].temp;
 59:     }
 60:   }

 62:   /* Test whether we are on the top edge of the global array */
 63:   if (yinte == info->my) {
 64:     j = info->my - 1;
 65:     yinte = yinte - 1;
 66:     /* top edge */
 67:     for (i=info->xs; i<info->xs+info->xm; i++) {
 68:       f[j][i].temp  = x[j][i].temp-x[j-1][i].temp;
 69:     }
 70:   }

 72:   /* Test whether we are on the left edge of the global array */
 73:   if (xints == 0) {
 74:     i = 0;
 75:     xints = xints + 1;
 76:     /* left edge */
 77:     for (j=info->ys; j<info->ys+info->ym; j++) {
 78:       f[j][i].temp  = x[j][i].temp;
 79:     }
 80:   }

 82:   /* Test whether we are on the right edge of the global array */
 83:   if (xinte == info->mx) {
 84:     i = info->mx - 1;
 85:     xinte = xinte - 1;
 86:     /* right edge */
 87:     for (j=info->ys; j<info->ys+info->ym; j++) {
 88:       f[j][i].temp  = x[j][i].temp - (PetscReal)(grashof>0);
 89:     }
 90:   }

 92:   /* Compute over the interior points */
 93:   for (j=yints; j<yinte; j++) {
 94:     for (i=xints; i<xinte; i++) {
 95:       /* convective coefficients for upwinding */
 96:       if (x1) {
 97:         vx = x1[j][i].u; vy = x1[j][i].v;
 98:       } else {
 99:         vx = vy = 0;
100:       }
101:       avx = PetscAbsScalar(vx);
102:       vxp = .5*(vx+avx); vxm = .5*(vx-avx);
103:       avy = PetscAbsScalar(vy);
104:       vyp = .5*(vy+avy);
105:       vym = .5*(vy-avy);

107:       /* Temperature */
108:       u             = x[j][i].temp;
109:       uxx           = (2.0*u - x[j][i-1].temp - x[j][i+1].temp)*hydhx;
110:       uyy           = (2.0*u - x[j-1][i].temp - x[j+1][i].temp)*hxdhy;
111:       f[j][i].temp  =  uxx + uyy + prandtl * ((vxp*(u - x[j][i-1].temp) + vxm*(x[j][i+1].temp - u))*hy + (vyp*(u - x[j-1][i].temp) + vym*(x[j+1][i].temp - u))*hx);
112:     }
113:   }
114:   PetscLogEventEnd(EVENT_FORMFUNCTIONLOCAL2,0,0,0,0);
115:   return(0);
116: }