Actual source code: ex46.c

  1: static char help[] = "Surface processes in geophysics.\n\n";

  3: /*T
  4:    Concepts: SNES^parallel Surface process example
  5:    Concepts: DMDA^using distributed arrays;
  6:    Concepts: IS coloirng types;
  7:    Processors: n
  8: T*/

 10: #include <petscsnes.h>
 11: #include <petscdm.h>
 12: #include <petscdmda.h>

 14: /*
 15:    User-defined application context - contains data needed by the
 16:    application-provided call-back routines, FormJacobianLocal() and
 17:    FormFunctionLocal().
 18: */
 19: typedef struct {
 20:   PetscReal   D;  /* The diffusion coefficient */
 21:   PetscReal   K;  /* The advection coefficient */
 22:   PetscInt    m;  /* Exponent for A */
 23: } AppCtx;

 25: /*
 26:    User-defined routines
 27: */
 28: extern PetscErrorCode FormFunctionLocal(DMDALocalInfo*,PetscScalar**,PetscScalar**,AppCtx*);
 29: extern PetscErrorCode FormJacobianLocal(DMDALocalInfo*,PetscScalar**,Mat,AppCtx*);

 31: int main(int argc,char **argv)
 32: {
 33:   SNES           snes;                         /* nonlinear solver */
 34:   AppCtx         user;                         /* user-defined work context */
 35:   PetscInt       its;                          /* iterations for convergence */
 37:   DM             da;

 39:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 40:      Initialize program
 41:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 43:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;

 45:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 46:      Initialize problem parameters
 47:   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 48:   PetscOptionsBegin(PETSC_COMM_WORLD, "", "Surface Process Problem Options", "SNES");
 49:   user.D = 1.0;
 50:   PetscOptionsReal("-D", "The diffusion coefficient D", __FILE__, user.D, &user.D, NULL);
 51:   user.K = 1.0;
 52:   PetscOptionsReal("-K", "The advection coefficient K", __FILE__, user.K, &user.K, NULL);
 53:   user.m = 1;
 54:   PetscOptionsInt("-m", "The exponent for A", __FILE__, user.m, &user.m, NULL);
 55:   PetscOptionsEnd();

 57:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 58:      Create distributed array (DMDA) to manage parallel grid and vectors
 59:   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 60:   DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,4,4,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,&da);
 61:   DMSetFromOptions(da);
 62:   DMSetUp(da);
 63:   DMDASetUniformCoordinates(da, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0);
 64:   DMSetApplicationContext(da,&user);
 65:   SNESCreate(PETSC_COMM_WORLD, &snes);
 66:   SNESSetDM(snes, da);

 68:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 69:      Set local function evaluation routine
 70:   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 71:   DMDASNESSetFunctionLocal(da,INSERT_VALUES,(PetscErrorCode (*)(DMDALocalInfo*,void*,void*,void*))FormFunctionLocal,&user);

 73:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 74:      Customize solver; set runtime options
 75:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 76:   SNESSetFromOptions(snes);

 78:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 79:      Solve nonlinear system
 80:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 81:   SNESSolve(snes,0,0);
 82:   SNESGetIterationNumber(snes,&its);

 84:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 85:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 86:   PetscPrintf(PETSC_COMM_WORLD,"Number of SNES iterations = %D\n",its);

 88:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 89:      Free work space.  All PETSc objects should be destroyed when they
 90:      are no longer needed.
 91:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 93:   SNESDestroy(&snes);
 94:   DMDestroy(&da);

 96:   PetscFinalize();
 97:   return ierr;
 98: }

100: PetscScalar funcU(DMDACoor2d *coords)
101: {
102:   return coords->x + coords->y;
103: }

105: PetscScalar funcA(PetscScalar z, AppCtx *user)
106: {
107:   PetscScalar v = 1.0;
108:   PetscInt    i;

110:   for (i = 0; i < user->m; ++i) v *= z;
111:   return v;
112: }

114: PetscScalar funcADer(PetscScalar z, AppCtx *user)
115: {
116:   PetscScalar v = 1.0;
117:   PetscInt    i;

119:   for (i = 0; i < user->m-1; ++i) v *= z;
120:   return (PetscScalar)user->m*v;
121: }

123: /*
124:    FormFunctionLocal - Evaluates nonlinear function, F(x).
125: */
126: PetscErrorCode FormFunctionLocal(DMDALocalInfo *info,PetscScalar **x,PetscScalar **f,AppCtx *user)
127: {
128:   DM             coordDA;
129:   Vec            coordinates;
130:   DMDACoor2d     **coords;
131:   PetscScalar    u, ux, uy, uxx, uyy;
132:   PetscReal      D, K, hx, hy, hxdhy, hydhx;
133:   PetscInt       i,j;

137:   D     = user->D;
138:   K     = user->K;
139:   hx    = 1.0/(PetscReal)(info->mx-1);
140:   hy    = 1.0/(PetscReal)(info->my-1);
141:   hxdhy = hx/hy;
142:   hydhx = hy/hx;
143:   /*
144:      Compute function over the locally owned part of the grid
145:   */
146:   DMGetCoordinateDM(info->da, &coordDA);
147:   DMGetCoordinates(info->da, &coordinates);
148:   DMDAVecGetArray(coordDA, coordinates, &coords);
149:   for (j=info->ys; j<info->ys+info->ym; j++) {
150:     for (i=info->xs; i<info->xs+info->xm; i++) {
151:       if (i == 0 || j == 0 || i == info->mx-1 || j == info->my-1) f[j][i] = x[j][i];
152:       else {
153:         u       = x[j][i];
154:         ux      = (x[j][i+1] - x[j][i])/hx;
155:         uy      = (x[j+1][i] - x[j][i])/hy;
156:         uxx     = (2.0*u - x[j][i-1] - x[j][i+1])*hydhx;
157:         uyy     = (2.0*u - x[j-1][i] - x[j+1][i])*hxdhy;
158:         f[j][i] = D*(uxx + uyy) - (K*funcA(x[j][i], user)*PetscSqrtScalar(ux*ux + uy*uy) + funcU(&coords[j][i]))*hx*hy;
159:         if (PetscIsInfOrNanScalar(f[j][i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FP, "Invalid residual: %g", (double)PetscRealPart(f[j][i]));
160:       }
161:     }
162:   }
163:   DMDAVecRestoreArray(coordDA, coordinates, &coords);
164:   PetscLogFlops(11.0*info->ym*info->xm);
165:   return(0);
166: }

168: /*
169:    FormJacobianLocal - Evaluates Jacobian matrix.
170: */
171: PetscErrorCode FormJacobianLocal(DMDALocalInfo *info,PetscScalar **x,Mat jac,AppCtx *user)
172: {
173:   MatStencil     col[5], row;
174:   PetscScalar    D, K, A, v[5], hx, hy, hxdhy, hydhx, ux, uy;
175:   PetscReal      normGradZ;
176:   PetscInt       i, j,k;

180:   D     = user->D;
181:   K     = user->K;
182:   hx    = 1.0/(PetscReal)(info->mx-1);
183:   hy    = 1.0/(PetscReal)(info->my-1);
184:   hxdhy = hx/hy;
185:   hydhx = hy/hx;

187:   /*
188:      Compute entries for the locally owned part of the Jacobian.
189:       - Currently, all PETSc parallel matrix formats are partitioned by
190:         contiguous chunks of rows across the processors.
191:       - Each processor needs to insert only elements that it owns
192:         locally (but any non-local elements will be sent to the
193:         appropriate processor during matrix assembly).
194:       - Here, we set all entries for a particular row at once.
195:       - We can set matrix entries either using either
196:         MatSetValuesLocal() or MatSetValues(), as discussed above.
197:   */
198:   for (j=info->ys; j<info->ys+info->ym; j++) {
199:     for (i=info->xs; i<info->xs+info->xm; i++) {
200:       row.j = j; row.i = i;
201:       if (i == 0 || j == 0 || i == info->mx-1 || j == info->my-1) {
202:         /* boundary points */
203:         v[0] = 1.0;
204:         MatSetValuesStencil(jac,1,&row,1,&row,v,INSERT_VALUES);
205:       } else {
206:         /* interior grid points */
207:         ux        = (x[j][i+1] - x[j][i])/hx;
208:         uy        = (x[j+1][i] - x[j][i])/hy;
209:         normGradZ = PetscRealPart(PetscSqrtScalar(ux*ux + uy*uy));
210:         if (normGradZ < 1.0e-8) normGradZ = 1.0e-8;
211:         A = funcA(x[j][i], user);

213:         v[0] = -D*hxdhy;                                                                          col[0].j = j - 1; col[0].i = i;
214:         v[1] = -D*hydhx;                                                                          col[1].j = j;     col[1].i = i-1;
215:         v[2] = D*2.0*(hydhx + hxdhy) + K*(funcADer(x[j][i], user)*normGradZ - A/normGradZ)*hx*hy; col[2].j = row.j; col[2].i = row.i;
216:         v[3] = -D*hydhx + K*A*hx*hy/(2.0*normGradZ);                                              col[3].j = j;     col[3].i = i+1;
217:         v[4] = -D*hxdhy + K*A*hx*hy/(2.0*normGradZ);                                              col[4].j = j + 1; col[4].i = i;
218:         for (k = 0; k < 5; ++k) {
219:           if (PetscIsInfOrNanScalar(v[k])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FP, "Invalid residual: %g", (double)PetscRealPart(v[k]));
220:         }
221:         MatSetValuesStencil(jac,1,&row,5,col,v,INSERT_VALUES);
222:       }
223:     }
224:   }

226:   /*
227:      Assemble matrix, using the 2-step process:
228:        MatAssemblyBegin(), MatAssemblyEnd().
229:   */
230:   MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);
231:   MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);
232:   /*
233:      Tell the matrix we will never add a new nonzero location to the
234:      matrix. If we do, it will generate an error.
235:   */
236:   MatSetOption(jac,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
237:   return(0);
238: }

240: /*TEST

242:    test:
243:       args: -snes_view -snes_monitor_short -da_refine 1 -pc_type mg -ksp_type fgmres -pc_mg_type full -mg_levels_ksp_chebyshev_esteig 0.5,1.1

245:    test:
246:       suffix: ew_1
247:       args: -snes_monitor_short -ksp_converged_reason -da_grid_x 20 -da_grid_y 20 -snes_ksp_ew -snes_ksp_ew_version 1
248:       requires: !single

250:    test:
251:       suffix: ew_2
252:       args: -snes_monitor_short -ksp_converged_reason -da_grid_x 20 -da_grid_y 20 -snes_ksp_ew -snes_ksp_ew_version 2

254:    test:
255:       suffix: ew_3
256:       args: -snes_monitor_short -ksp_converged_reason -da_grid_x 20 -da_grid_y 20 -snes_ksp_ew -snes_ksp_ew_version 3
257:       requires: !single

259:    test:
260:       suffix: fm_rise_2
261:       args: -K 3 -m 1 -D 0.2 -snes_monitor_short -snes_type ngmres -snes_npc_side right -npc_snes_type newtonls -npc_snes_linesearch_type basic -snes_ngmres_restart_it 1 -snes_ngmres_restart_fm_rise
262:       requires: !single

264:    test:
265:       suffix: fm_rise_4
266:       args: -K 3 -m 1 -D 0.2 -snes_monitor_short -snes_type ngmres -snes_npc_side right -npc_snes_type newtonls -npc_snes_linesearch_type basic -snes_ngmres_restart_it 2 -snes_ngmres_restart_fm_rise -snes_rtol 1.e-2 -snes_max_it 5

268: TEST*/