Actual source code: ex14.c

petsc-3.5.4 2015-05-23
Report Typos and Errors
  2: static char help[] = "Solves a nonlinear system in parallel with a user-defined Newton method.\n\
  3: Uses KSP to solve the linearized Newton sytems.  This solver\n\
  4: is a very simplistic inexact Newton method.  The intent of this code is to\n\
  5: demonstrate the repeated solution of linear sytems with the same nonzero pattern.\n\
  6: \n\
  7: This is NOT the recommended approach for solving nonlinear problems with PETSc!\n\
  8: We urge users to employ the SNES component for solving nonlinear problems whenever\n\
  9: possible, as it offers many advantages over coding nonlinear solvers independently.\n\
 10: \n\
 11: We solve the  Bratu (SFI - solid fuel ignition) problem in a 2D rectangular\n\
 12: domain, using distributed arrays (DMDAs) to partition the parallel grid.\n\
 13: The command line options include:\n\
 14:   -par <parameter>, where <parameter> indicates the problem's nonlinearity\n\
 15:      problem SFI:  <parameter> = Bratu parameter (0 <= par <= 6.81)\n\
 16:   -mx <xg>, where <xg> = number of grid points in the x-direction\n\
 17:   -my <yg>, where <yg> = number of grid points in the y-direction\n\
 18:   -Nx <npx>, where <npx> = number of processors in the x-direction\n\
 19:   -Ny <npy>, where <npy> = number of processors in the y-direction\n\n";

 21: /*T
 22:    Concepts: KSP^writing a user-defined nonlinear solver (parallel Bratu example);
 23:    Concepts: DMDA^using distributed arrays;
 24:    Processors: n
 25: T*/

 27: /* ------------------------------------------------------------------------

 29:     Solid Fuel Ignition (SFI) problem.  This problem is modeled by
 30:     the partial differential equation

 32:             -Laplacian u - lambda*exp(u) = 0,  0 < x,y < 1,

 34:     with boundary conditions

 36:              u = 0  for  x = 0, x = 1, y = 0, y = 1.

 38:     A finite difference approximation with the usual 5-point stencil
 39:     is used to discretize the boundary value problem to obtain a nonlinear
 40:     system of equations.

 42:     The SNES version of this problem is:  snes/examples/tutorials/ex5.c
 43:     We urge users to employ the SNES component for solving nonlinear
 44:     problems whenever possible, as it offers many advantages over coding
 45:     nonlinear solvers independently.

 47:   ------------------------------------------------------------------------- */

 49: /*
 50:    Include "petscdmda.h" so that we can use distributed arrays (DMDAs).
 51:    Include "petscksp.h" so that we can use KSP solvers.  Note that this
 52:    file automatically includes:
 53:      petscsys.h       - base PETSc routines   petscvec.h - vectors
 54:      petscmat.h - matrices
 55:      petscis.h     - index sets            petscksp.h - Krylov subspace methods
 56:      petscviewer.h - viewers               petscpc.h  - preconditioners
 57: */
 58: #include <petscdm.h>
 59: #include <petscdmda.h>
 60: #include <petscksp.h>

 62: /*
 63:    User-defined application context - contains data needed by the
 64:    application-provided call-back routines, ComputeJacobian() and
 65:    ComputeFunction().
 66: */
 67: typedef struct {
 68:   PetscReal param;             /* test problem parameter */
 69:   PetscInt  mx,my;             /* discretization in x,y directions */
 70:   Vec       localX,localF;    /* ghosted local vector */
 71:   DM        da;                /* distributed array data structure */
 72: } AppCtx;

 74: /*
 75:    User-defined routines
 76: */
 77: extern PetscErrorCode ComputeFunction(AppCtx*,Vec,Vec),FormInitialGuess(AppCtx*,Vec);
 78: extern PetscErrorCode ComputeJacobian(AppCtx*,Vec,Mat);

 82: int main(int argc,char **argv)
 83: {
 84:   /* -------------- Data to define application problem ---------------- */
 85:   MPI_Comm       comm;                /* communicator */
 86:   KSP            ksp;                /* linear solver */
 87:   Vec            X,Y,F;             /* solution, update, residual vectors */
 88:   Mat            J;                   /* Jacobian matrix */
 89:   AppCtx         user;                /* user-defined work context */
 90:   PetscInt       Nx,Ny;              /* number of preocessors in x- and y- directions */
 91:   PetscMPIInt    size;                /* number of processors */
 92:   PetscReal      bratu_lambda_max = 6.81,bratu_lambda_min = 0.;
 93:   PetscInt       m,N;

 96:   /* --------------- Data to define nonlinear solver -------------- */
 97:   PetscReal    rtol = 1.e-8;          /* relative convergence tolerance */
 98:   PetscReal    xtol = 1.e-8;          /* step convergence tolerance */
 99:   PetscReal    ttol;                  /* convergence tolerance */
100:   PetscReal    fnorm,ynorm,xnorm;     /* various vector norms */
101:   PetscInt     max_nonlin_its = 10;   /* maximum number of iterations for nonlinear solver */
102:   PetscInt     max_functions  = 50;   /* maximum number of function evaluations */
103:   PetscInt     lin_its;               /* number of linear solver iterations for each step */
104:   PetscInt     i;                     /* nonlinear solve iteration number */
105:   PetscBool    no_output = PETSC_FALSE;             /* flag indicating whether to surpress output */

107:   PetscInitialize(&argc,&argv,(char*)0,help);
108:   comm = PETSC_COMM_WORLD;
109:   PetscOptionsGetBool(NULL,"-no_output",&no_output,NULL);

111:   /*
112:      Initialize problem parameters
113:   */
114:   user.mx = 4; user.my = 4; user.param = 6.0;

116:   PetscOptionsGetInt(NULL,"-mx",&user.mx,NULL);
117:   PetscOptionsGetInt(NULL,"-my",&user.my,NULL);
118:   PetscOptionsGetReal(NULL,"-par",&user.param,NULL);
119:   if (user.param >= bratu_lambda_max || user.param <= bratu_lambda_min) SETERRQ(PETSC_COMM_WORLD,1,"Lambda is out of range");
120:   N = user.mx*user.my;

122:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
123:      Create linear solver context
124:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

126:   KSPCreate(comm,&ksp);

128:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
129:      Create vector data structures
130:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

132:   /*
133:      Create distributed array (DMDA) to manage parallel grid and vectors
134:   */
135:   MPI_Comm_size(comm,&size);
136:   Nx   = PETSC_DECIDE; Ny = PETSC_DECIDE;
137:   PetscOptionsGetInt(NULL,"-Nx",&Nx,NULL);
138:   PetscOptionsGetInt(NULL,"-Ny",&Ny,NULL);
139:   if (Nx*Ny != size && (Nx != PETSC_DECIDE || Ny != PETSC_DECIDE)) SETERRQ(PETSC_COMM_WORLD,1,"Incompatible number of processors:  Nx * Ny != size");
140:   DMDACreate2d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,user.mx,user.my,Nx,Ny,1,1,NULL,NULL,&user.da);

142:   /*
143:      Extract global and local vectors from DMDA; then duplicate for remaining
144:      vectors that are the same types
145:   */
146:   DMCreateGlobalVector(user.da,&X);
147:   DMCreateLocalVector(user.da,&user.localX);
148:   VecDuplicate(X,&F);
149:   VecDuplicate(X,&Y);
150:   VecDuplicate(user.localX,&user.localF);


153:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
154:      Create matrix data structure for Jacobian
155:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
156:   /*
157:      Note:  For the parallel case, vectors and matrices MUST be partitioned
158:      accordingly.  When using distributed arrays (DMDAs) to create vectors,
159:      the DMDAs determine the problem partitioning.  We must explicitly
160:      specify the local matrix dimensions upon its creation for compatibility
161:      with the vector distribution.  Thus, the generic MatCreate() routine
162:      is NOT sufficient when working with distributed arrays.

164:      Note: Here we only approximately preallocate storage space for the
165:      Jacobian.  See the users manual for a discussion of better techniques
166:      for preallocating matrix memory.
167:   */
168:   if (size == 1) {
169:     MatCreateSeqAIJ(comm,N,N,5,NULL,&J);
170:   } else {
171:     VecGetLocalSize(X,&m);
172:     MatCreateAIJ(comm,m,m,N,N,5,NULL,3,NULL,&J);
173:   }

175:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
176:      Customize linear solver; set runtime options
177:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

179:   /*
180:      Set runtime options (e.g.,-ksp_monitor -ksp_rtol <rtol> -ksp_type <type>)
181:   */
182:   KSPSetFromOptions(ksp);

184:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
185:      Evaluate initial guess
186:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

188:   FormInitialGuess(&user,X);
189:   ComputeFunction(&user,X,F);   /* Compute F(X)    */
190:   VecNorm(F,NORM_2,&fnorm);     /* fnorm = || F || */
191:   ttol = fnorm*rtol;
192:   if (!no_output) PetscPrintf(comm,"Initial function norm = %g\n",(double)fnorm);

194:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
195:      Solve nonlinear system with a user-defined method
196:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

198:   /*
199:       This solver is a very simplistic inexact Newton method, with no
200:       no damping strategies or bells and whistles. The intent of this code
201:       is  merely to demonstrate the repeated solution with KSP of linear
202:       sytems with the same nonzero structure.

204:       This is NOT the recommended approach for solving nonlinear problems
205:       with PETSc!  We urge users to employ the SNES component for solving
206:       nonlinear problems whenever possible with application codes, as it
207:       offers many advantages over coding nonlinear solvers independently.
208:    */

210:   for (i=0; i<max_nonlin_its; i++) {

212:     /*
213:         Compute the Jacobian matrix.  
214:      */
215:     ComputeJacobian(&user,X,J);

217:     /*
218:         Solve J Y = F, where J is the Jacobian matrix.
219:           - First, set the KSP linear operators.  Here the matrix that
220:             defines the linear system also serves as the preconditioning
221:             matrix.
222:           - Then solve the Newton system.
223:      */
224:     KSPSetOperators(ksp,J,J);
225:     KSPSolve(ksp,F,Y);
226:     KSPGetIterationNumber(ksp,&lin_its);

228:     /*
229:        Compute updated iterate
230:      */
231:     VecNorm(Y,NORM_2,&ynorm);       /* ynorm = || Y || */
232:     VecAYPX(Y,-1.0,X);              /* Y <- X - Y      */
233:     VecCopy(Y,X);                   /* X <- Y          */
234:     VecNorm(X,NORM_2,&xnorm);       /* xnorm = || X || */
235:     if (!no_output) {
236:       PetscPrintf(comm,"   linear solve iterations = %D, xnorm=%g, ynorm=%g\n",lin_its,(double)xnorm,(double)ynorm);
237:     }

239:     /*
240:        Evaluate new nonlinear function
241:      */
242:     ComputeFunction(&user,X,F);     /* Compute F(X)    */
243:     VecNorm(F,NORM_2,&fnorm);       /* fnorm = || F || */
244:     if (!no_output) {
245:       PetscPrintf(comm,"Iteration %D, function norm = %g\n",i+1,(double)fnorm);
246:     }

248:     /*
249:        Test for convergence
250:      */
251:     if (fnorm <= ttol) {
252:       if (!no_output) {
253:         PetscPrintf(comm,"Converged due to function norm %g < %g (relative tolerance)\n",(double)fnorm,(double)ttol);
254:       }
255:       break;
256:     }
257:     if (ynorm < xtol*(xnorm)) {
258:       if (!no_output) {
259:         PetscPrintf(comm,"Converged due to small update length: %g < %g * %g\n",(double)ynorm,(double)xtol,(double)xnorm);
260:       }
261:       break;
262:     }
263:     if (i > max_functions) {
264:       if (!no_output) {
265:         PetscPrintf(comm,"Exceeded maximum number of function evaluations: %D > %D\n",i,max_functions);
266:       }
267:       break;
268:     }
269:   }
270:   PetscPrintf(comm,"Number of SNES iterations = %D\n",i+1);

272:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
273:      Free work space.  All PETSc objects should be destroyed when they
274:      are no longer needed.
275:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

277:   MatDestroy(&J);           VecDestroy(&Y);
278:   VecDestroy(&user.localX); VecDestroy(&X);
279:   VecDestroy(&user.localF); VecDestroy(&F);
280:   KSPDestroy(&ksp);  DMDestroy(&user.da);
281:   PetscFinalize();

283:   return 0;
284: }
285: /* ------------------------------------------------------------------- */
288: /*
289:    FormInitialGuess - Forms initial approximation.

291:    Input Parameters:
292:    user - user-defined application context
293:    X - vector

295:    Output Parameter:
296:    X - vector
297:  */
298: PetscErrorCode FormInitialGuess(AppCtx *user,Vec X)
299: {
300:   PetscInt    i,j,row,mx,my,ierr,xs,ys,xm,ym,gxm,gym,gxs,gys;
301:   PetscReal   one = 1.0,lambda,temp1,temp,hx,hy;
302:   PetscScalar *x;
303:   Vec         localX = user->localX;

305:   mx    = user->mx;            my = user->my;            lambda = user->param;
306:   hx    = one/(PetscReal)(mx-1);  hy = one/(PetscReal)(my-1);
307:   temp1 = lambda/(lambda + one);

309:   /*
310:      Get a pointer to vector data.
311:        - For default PETSc vectors, VecGetArray() returns a pointer to
312:          the data array.  Otherwise, the routine is implementation dependent.
313:        - You MUST call VecRestoreArray() when you no longer need access to
314:          the array.
315:   */
316:   VecGetArray(localX,&x);

318:   /*
319:      Get local grid boundaries (for 2-dimensional DMDA):
320:        xs, ys   - starting grid indices (no ghost points)
321:        xm, ym   - widths of local grid (no ghost points)
322:        gxs, gys - starting grid indices (including ghost points)
323:        gxm, gym - widths of local grid (including ghost points)
324:   */
325:   DMDAGetCorners(user->da,&xs,&ys,NULL,&xm,&ym,NULL);
326:   DMDAGetGhostCorners(user->da,&gxs,&gys,NULL,&gxm,&gym,NULL);

328:   /*
329:      Compute initial guess over the locally owned part of the grid
330:   */
331:   for (j=ys; j<ys+ym; j++) {
332:     temp = (PetscReal)(PetscMin(j,my-j-1))*hy;
333:     for (i=xs; i<xs+xm; i++) {
334:       row = i - gxs + (j - gys)*gxm;
335:       if (i == 0 || j == 0 || i == mx-1 || j == my-1) {
336:         x[row] = 0.0;
337:         continue;
338:       }
339:       x[row] = temp1*PetscSqrtReal(PetscMin((PetscReal)(PetscMin(i,mx-i-1))*hx,temp));
340:     }
341:   }

343:   /*
344:      Restore vector
345:   */
346:   VecRestoreArray(localX,&x);

348:   /*
349:      Insert values into global vector
350:   */
351:   DMLocalToGlobalBegin(user->da,localX,INSERT_VALUES,X);
352:   DMLocalToGlobalEnd(user->da,localX,INSERT_VALUES,X);
353:   return 0;
354: }
355: /* ------------------------------------------------------------------- */
358: /*
359:    ComputeFunction - Evaluates nonlinear function, F(x).

361:    Input Parameters:
362: .  X - input vector
363: .  user - user-defined application context

365:    Output Parameter:
366: .  F - function vector
367:  */
368: PetscErrorCode ComputeFunction(AppCtx *user,Vec X,Vec F)
369: {
371:   PetscInt       i,j,row,mx,my,xs,ys,xm,ym,gxs,gys,gxm,gym;
372:   PetscReal      two = 2.0,one = 1.0,lambda,hx,hy,hxdhy,hydhx,sc;
373:   PetscScalar    u,uxx,uyy,*x,*f;
374:   Vec            localX = user->localX,localF = user->localF;

376:   mx = user->mx;            my = user->my;            lambda = user->param;
377:   hx = one/(PetscReal)(mx-1);  hy = one/(PetscReal)(my-1);
378:   sc = hx*hy*lambda;        hxdhy = hx/hy;            hydhx = hy/hx;

380:   /*
381:      Scatter ghost points to local vector, using the 2-step process
382:         DMGlobalToLocalBegin(), DMGlobalToLocalEnd().
383:      By placing code between these two statements, computations can be
384:      done while messages are in transition.
385:   */
386:   DMGlobalToLocalBegin(user->da,X,INSERT_VALUES,localX);
387:   DMGlobalToLocalEnd(user->da,X,INSERT_VALUES,localX);

389:   /*
390:      Get pointers to vector data
391:   */
392:   VecGetArray(localX,&x);
393:   VecGetArray(localF,&f);

395:   /*
396:      Get local grid boundaries
397:   */
398:   DMDAGetCorners(user->da,&xs,&ys,NULL,&xm,&ym,NULL);
399:   DMDAGetGhostCorners(user->da,&gxs,&gys,NULL,&gxm,&gym,NULL);

401:   /*
402:      Compute function over the locally owned part of the grid
403:   */
404:   for (j=ys; j<ys+ym; j++) {
405:     row = (j - gys)*gxm + xs - gxs - 1;
406:     for (i=xs; i<xs+xm; i++) {
407:       row++;
408:       if (i == 0 || j == 0 || i == mx-1 || j == my-1) {
409:         f[row] = x[row];
410:         continue;
411:       }
412:       u      = x[row];
413:       uxx    = (two*u - x[row-1] - x[row+1])*hydhx;
414:       uyy    = (two*u - x[row-gxm] - x[row+gxm])*hxdhy;
415:       f[row] = uxx + uyy - sc*PetscExpScalar(u);
416:     }
417:   }

419:   /*
420:      Restore vectors
421:   */
422:   VecRestoreArray(localX,&x);
423:   VecRestoreArray(localF,&f);

425:   /*
426:      Insert values into global vector
427:   */
428:   DMLocalToGlobalBegin(user->da,localF,INSERT_VALUES,F);
429:   DMLocalToGlobalEnd(user->da,localF,INSERT_VALUES,F);
430:   PetscLogFlops(11.0*ym*xm);
431:   return 0;
432: }
433: /* ------------------------------------------------------------------- */
436: /*
437:    ComputeJacobian - Evaluates Jacobian matrix.

439:    Input Parameters:
440: .  x - input vector
441: .  user - user-defined application context

443:    Output Parameters:
444: .  jac - Jacobian matrix
445: .  flag - flag indicating matrix structure

447:    Notes:
448:    Due to grid point reordering with DMDAs, we must always work
449:    with the local grid points, and then transform them to the new
450:    global numbering with the "ltog" mapping 
451:    We cannot work directly with the global numbers for the original
452:    uniprocessor grid!
453: */
454: PetscErrorCode ComputeJacobian(AppCtx *user,Vec X,Mat jac)
455: {
456:   PetscErrorCode         ierr;
457:   Vec                    localX = user->localX;   /* local vector */
458:   const PetscInt         *ltog;                   /* local-to-global mapping */
459:   PetscInt               i,j,row,mx,my,col[5];
460:   PetscInt               xs,ys,xm,ym,gxs,gys,gxm,gym,grow;
461:   PetscScalar            two = 2.0,one = 1.0,lambda,v[5],hx,hy,hxdhy,hydhx,sc,*x;
462:   ISLocalToGlobalMapping ltogm;

464:   mx = user->mx;            my = user->my;            lambda = user->param;
465:   hx = one/(PetscReal)(mx-1);  hy = one/(PetscReal)(my-1);
466:   sc = hx*hy;               hxdhy = hx/hy;            hydhx = hy/hx;

468:   /*
469:      Scatter ghost points to local vector, using the 2-step process
470:         DMGlobalToLocalBegin(), DMGlobalToLocalEnd().
471:      By placing code between these two statements, computations can be
472:      done while messages are in transition.
473:   */
474:   DMGlobalToLocalBegin(user->da,X,INSERT_VALUES,localX);
475:   DMGlobalToLocalEnd(user->da,X,INSERT_VALUES,localX);

477:   /*
478:      Get pointer to vector data
479:   */
480:   VecGetArray(localX,&x);

482:   /*
483:      Get local grid boundaries
484:   */
485:   DMDAGetCorners(user->da,&xs,&ys,NULL,&xm,&ym,NULL);
486:   DMDAGetGhostCorners(user->da,&gxs,&gys,NULL,&gxm,&gym,NULL);

488:   /*
489:      Get the global node numbers for all local nodes, including ghost points
490:   */
491:   DMGetLocalToGlobalMapping(user->da,&ltogm);
492:   ISLocalToGlobalMappingGetIndices(ltogm,&ltog);

494:   /*
495:      Compute entries for the locally owned part of the Jacobian.
496:       - Currently, all PETSc parallel matrix formats are partitioned by
497:         contiguous chunks of rows across the processors. The "grow"
498:         parameter computed below specifies the global row number
499:         corresponding to each local grid point.
500:       - Each processor needs to insert only elements that it owns
501:         locally (but any non-local elements will be sent to the
502:         appropriate processor during matrix assembly).
503:       - Always specify global row and columns of matrix entries.
504:       - Here, we set all entries for a particular row at once.
505:   */
506:   for (j=ys; j<ys+ym; j++) {
507:     row = (j - gys)*gxm + xs - gxs - 1;
508:     for (i=xs; i<xs+xm; i++) {
509:       row++;
510:       grow = ltog[row];
511:       /* boundary points */
512:       if (i == 0 || j == 0 || i == mx-1 || j == my-1) {
513:         MatSetValues(jac,1,&grow,1,&grow,&one,INSERT_VALUES);
514:         continue;
515:       }
516:       /* interior grid points */
517:       v[0] = -hxdhy; col[0] = ltog[row - gxm];
518:       v[1] = -hydhx; col[1] = ltog[row - 1];
519:       v[2] = two*(hydhx + hxdhy) - sc*lambda*PetscExpScalar(x[row]); col[2] = grow;
520:       v[3] = -hydhx; col[3] = ltog[row + 1];
521:       v[4] = -hxdhy; col[4] = ltog[row + gxm];
522:       MatSetValues(jac,1,&grow,5,col,v,INSERT_VALUES);
523:     }
524:   }
525:   ISLocalToGlobalMappingRestoreIndices(ltogm,&ltog);

527:   /*
528:      Assemble matrix, using the 2-step process:
529:        MatAssemblyBegin(), MatAssemblyEnd().
530:      By placing code between these two statements, computations can be
531:      done while messages are in transition.
532:   */
533:   MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);
534:   VecRestoreArray(localX,&x);
535:   MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);

537:   return 0;
538: }