Actual source code: ex1.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  2: static char help[] = "Solves the nonlinear system, the Bratu (SFI - solid fuel ignition) problem in a 2D rectangular domain.\n\
  3: This example also illustrates the use of matrix coloring.  Runtime options include:\n\
  4:   -par <parameter>, where <parameter> indicates the problem's nonlinearity\n\
  5:      problem SFI:  <parameter> = Bratu parameter (0 <= par <= 6.81)\n\
  6:   -mx <xg>, where <xg> = number of grid points in the x-direction\n\
  7:   -my <yg>, where <yg> = number of grid points in the y-direction\n\n";

  9: /*T
 10:    Concepts: SNES^sequential Bratu example
 11:    Processors: 1
 12: T*/

 14: /* ------------------------------------------------------------------------

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

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

 21:     with boundary conditions

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

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

 29:     The parallel version of this code is snes/examples/tutorials/ex5.c

 31:   ------------------------------------------------------------------------- */

 33: /*
 34:    Include "petscsnes.h" so that we can use SNES solvers.  Note that
 35:    this file automatically includes:
 36:      petscsys.h       - base PETSc routines   petscvec.h - vectors
 37:      petscmat.h - matrices
 38:      petscis.h     - index sets            petscksp.h - Krylov subspace methods
 39:      petscviewer.h - viewers               petscpc.h  - preconditioners
 40:      petscksp.h   - linear solvers
 41: */

 43: #include <petscsnes.h>

 45: /*
 46:    User-defined application context - contains data needed by the
 47:    application-provided call-back routines, FormJacobian() and
 48:    FormFunction().
 49: */
 50: typedef struct {
 51:   PetscReal param;              /* test problem parameter */
 52:   PetscInt  mx;                 /* Discretization in x-direction */
 53:   PetscInt  my;                 /* Discretization in y-direction */
 54: } AppCtx;

 56: /*
 57:    User-defined routines
 58: */
 59: extern PetscErrorCode FormJacobian(SNES,Vec,Mat,Mat,void*);
 60: extern PetscErrorCode FormFunction(SNES,Vec,Vec,void*);
 61: extern PetscErrorCode FormInitialGuess(AppCtx*,Vec);

 65: int main(int argc,char **argv)
 66: {
 67:   SNES           snes;                 /* nonlinear solver context */
 68:   Vec            x,r;                 /* solution, residual vectors */
 69:   Mat            J;                    /* Jacobian matrix */
 70:   AppCtx         user;                 /* user-defined application context */
 72:   PetscInt       i,its,N,hist_its[50];
 73:   PetscMPIInt    size;
 74:   PetscReal      bratu_lambda_max = 6.81,bratu_lambda_min = 0.,history[50];
 75:   MatFDColoring  fdcoloring;
 76:   PetscBool      matrix_free = PETSC_FALSE,flg,fd_coloring = PETSC_FALSE;

 78:   PetscInitialize(&argc,&argv,(char*)0,help);
 79:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 80:   if (size != 1) SETERRQ(PETSC_COMM_SELF,1,"This is a uniprocessor example only!");

 82:   /*
 83:      Initialize problem parameters
 84:   */
 85:   user.mx = 4; user.my = 4; user.param = 6.0;
 86:   PetscOptionsGetInt(NULL,NULL,"-mx",&user.mx,NULL);
 87:   PetscOptionsGetInt(NULL,NULL,"-my",&user.my,NULL);
 88:   PetscOptionsGetReal(NULL,NULL,"-par",&user.param,NULL);
 89:   if (user.param >= bratu_lambda_max || user.param <= bratu_lambda_min) SETERRQ(PETSC_COMM_SELF,1,"Lambda is out of range");
 90:   N = user.mx*user.my;

 92:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 93:      Create nonlinear solver context
 94:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 96:   SNESCreate(PETSC_COMM_WORLD,&snes);

 98:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 99:      Create vector data structures; set function evaluation routine
100:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

102:   VecCreate(PETSC_COMM_WORLD,&x);
103:   VecSetSizes(x,PETSC_DECIDE,N);
104:   VecSetFromOptions(x);
105:   VecDuplicate(x,&r);

107:   /*
108:      Set function evaluation routine and vector.  Whenever the nonlinear
109:      solver needs to evaluate the nonlinear function, it will call this
110:      routine.
111:       - Note that the final routine argument is the user-defined
112:         context that provides application-specific data for the
113:         function evaluation routine.
114:   */
115:   SNESSetFunction(snes,r,FormFunction,(void*)&user);

117:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
118:      Create matrix data structure; set Jacobian evaluation routine
119:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

121:   /*
122:      Create matrix. Here we only approximately preallocate storage space
123:      for the Jacobian.  See the users manual for a discussion of better
124:      techniques for preallocating matrix memory.
125:   */
126:   PetscOptionsGetBool(NULL,NULL,"-snes_mf",&matrix_free,NULL);
127:   if (!matrix_free) {
128:     PetscBool matrix_free_operator = PETSC_FALSE;
129:     PetscOptionsGetBool(NULL,NULL,"-snes_mf_operator",&matrix_free_operator,NULL);
130:     if (matrix_free_operator) matrix_free = PETSC_FALSE;
131:   }
132:   if (!matrix_free) {
133:     MatCreateSeqAIJ(PETSC_COMM_WORLD,N,N,5,NULL,&J);
134:   }

136:   /*
137:      This option will cause the Jacobian to be computed via finite differences
138:     efficiently using a coloring of the columns of the matrix.
139:   */
140:   PetscOptionsGetBool(NULL,NULL,"-snes_fd_coloring",&fd_coloring,NULL);
141:   if (matrix_free && fd_coloring) SETERRQ(PETSC_COMM_SELF,1,"Use only one of -snes_mf, -snes_fd_coloring options!\nYou can do -snes_mf_operator -snes_fd_coloring");

143:   if (fd_coloring) {
144:     ISColoring   iscoloring;
145:     MatColoring  mc;

147:     /*
148:       This initializes the nonzero structure of the Jacobian. This is artificial
149:       because clearly if we had a routine to compute the Jacobian we won't need
150:       to use finite differences.
151:     */
152:     FormJacobian(snes,x,J,J,&user);

154:     /*
155:        Color the matrix, i.e. determine groups of columns that share no common
156:       rows. These columns in the Jacobian can all be computed simulataneously.
157:     */
158:     MatColoringCreate(J,&mc);
159:     MatColoringSetType(mc,MATCOLORINGSL);
160:     MatColoringSetFromOptions(mc);
161:     MatColoringApply(mc,&iscoloring);
162:     MatColoringDestroy(&mc);
163:     /*
164:        Create the data structure that SNESComputeJacobianDefaultColor() uses
165:        to compute the actual Jacobians via finite differences.
166:     */
167:     MatFDColoringCreate(J,iscoloring,&fdcoloring);
168:     MatFDColoringSetFunction(fdcoloring,(PetscErrorCode (*)(void))FormFunction,&user);
169:     MatFDColoringSetFromOptions(fdcoloring);
170:     MatFDColoringSetUp(J,iscoloring,fdcoloring);
171:     /*
172:         Tell SNES to use the routine SNESComputeJacobianDefaultColor()
173:       to compute Jacobians.
174:     */
175:     SNESSetJacobian(snes,J,J,SNESComputeJacobianDefaultColor,fdcoloring);
176:     ISColoringDestroy(&iscoloring);
177:   }
178:   /*
179:      Set Jacobian matrix data structure and default Jacobian evaluation
180:      routine.  Whenever the nonlinear solver needs to compute the
181:      Jacobian matrix, it will call this routine.
182:       - Note that the final routine argument is the user-defined
183:         context that provides application-specific data for the
184:         Jacobian evaluation routine.
185:       - The user can override with:
186:          -snes_fd : default finite differencing approximation of Jacobian
187:          -snes_mf : matrix-free Newton-Krylov method with no preconditioning
188:                     (unless user explicitly sets preconditioner)
189:          -snes_mf_operator : form preconditioning matrix as set by the user,
190:                              but use matrix-free approx for Jacobian-vector
191:                              products within Newton-Krylov method
192:   */
193:   else if (!matrix_free) {
194:     SNESSetJacobian(snes,J,J,FormJacobian,(void*)&user);
195:   }

197:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
198:      Customize nonlinear solver; set runtime options
199:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

201:   /*
202:      Set runtime options (e.g., -snes_monitor -snes_rtol <rtol> -ksp_type <type>)
203:   */
204:   SNESSetFromOptions(snes);

206:   /*
207:      Set array that saves the function norms.  This array is intended
208:      when the user wants to save the convergence history for later use
209:      rather than just to view the function norms via -snes_monitor.
210:   */
211:   SNESSetConvergenceHistory(snes,history,hist_its,50,PETSC_TRUE);

213:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
214:      Evaluate initial guess; then solve nonlinear system
215:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
216:   /*
217:      Note: The user should initialize the vector, x, with the initial guess
218:      for the nonlinear solver prior to calling SNESSolve().  In particular,
219:      to employ an initial guess of zero, the user should explicitly set
220:      this vector to zero by calling VecSet().
221:   */
222:   FormInitialGuess(&user,x);
223:   SNESSolve(snes,NULL,x);
224:   SNESGetIterationNumber(snes,&its);
225:   PetscPrintf(PETSC_COMM_WORLD,"Number of SNES iterations = %D\n",its);


228:   /*
229:      Print the convergence history.  This is intended just to demonstrate
230:      use of the data attained via SNESSetConvergenceHistory().
231:   */
232:   PetscOptionsHasName(NULL,NULL,"-print_history",&flg);
233:   if (flg) {
234:     for (i=0; i<its+1; i++) {
235:       PetscPrintf(PETSC_COMM_WORLD,"iteration %D: Linear iterations %D Function norm = %g\n",i,hist_its[i],(double)history[i]);
236:     }
237:   }

239:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
240:      Free work space.  All PETSc objects should be destroyed when they
241:      are no longer needed.
242:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

244:   if (!matrix_free) {
245:     MatDestroy(&J);
246:   }
247:   if (fd_coloring) {
248:     MatFDColoringDestroy(&fdcoloring);
249:   }
250:   VecDestroy(&x);
251:   VecDestroy(&r);
252:   SNESDestroy(&snes);
253:   PetscFinalize();

255:   return 0;
256: }
257: /* ------------------------------------------------------------------- */
260: /*
261:    FormInitialGuess - Forms initial approximation.

263:    Input Parameters:
264:    user - user-defined application context
265:    X - vector

267:    Output Parameter:
268:    X - vector
269:  */
270: PetscErrorCode FormInitialGuess(AppCtx *user,Vec X)
271: {
272:   PetscInt       i,j,row,mx,my;
274:   PetscReal      lambda,temp1,temp,hx,hy;
275:   PetscScalar    *x;

277:   mx     = user->mx;
278:   my     = user->my;
279:   lambda = user->param;

281:   hx = 1.0 / (PetscReal)(mx-1);
282:   hy = 1.0 / (PetscReal)(my-1);

284:   /*
285:      Get a pointer to vector data.
286:        - For default PETSc vectors, VecGetArray() returns a pointer to
287:          the data array.  Otherwise, the routine is implementation dependent.
288:        - You MUST call VecRestoreArray() when you no longer need access to
289:          the array.
290:   */
291:   VecGetArray(X,&x);
292:   temp1 = lambda/(lambda + 1.0);
293:   for (j=0; j<my; j++) {
294:     temp = (PetscReal)(PetscMin(j,my-j-1))*hy;
295:     for (i=0; i<mx; i++) {
296:       row = i + j*mx;
297:       if (i == 0 || j == 0 || i == mx-1 || j == my-1) {
298:         x[row] = 0.0;
299:         continue;
300:       }
301:       x[row] = temp1*PetscSqrtReal(PetscMin((PetscReal)(PetscMin(i,mx-i-1))*hx,temp));
302:     }
303:   }

305:   /*
306:      Restore vector
307:   */
308:   VecRestoreArray(X,&x);
309:   return 0;
310: }
311: /* ------------------------------------------------------------------- */
314: /*
315:    FormFunction - Evaluates nonlinear function, F(x).

317:    Input Parameters:
318: .  snes - the SNES context
319: .  X - input vector
320: .  ptr - optional user-defined context, as set by SNESSetFunction()

322:    Output Parameter:
323: .  F - function vector
324:  */
325: PetscErrorCode FormFunction(SNES snes,Vec X,Vec F,void *ptr)
326: {
327:   AppCtx            *user = (AppCtx*)ptr;
328:   PetscInt          i,j,row,mx,my;
329:   PetscErrorCode    ierr;
330:   PetscReal         two = 2.0,one = 1.0,lambda,hx,hy,hxdhy,hydhx;
331:   PetscScalar       ut,ub,ul,ur,u,uxx,uyy,sc,*f;
332:   const PetscScalar *x;

334:   mx     = user->mx;
335:   my     = user->my;
336:   lambda = user->param;
337:   hx     = one / (PetscReal)(mx-1);
338:   hy     = one / (PetscReal)(my-1);
339:   sc     = hx*hy;
340:   hxdhy  = hx/hy;
341:   hydhx  = hy/hx;

343:   /*
344:      Get pointers to vector data
345:   */
346:   VecGetArrayRead(X,&x);
347:   VecGetArray(F,&f);

349:   /*
350:      Compute function
351:   */
352:   for (j=0; j<my; j++) {
353:     for (i=0; i<mx; i++) {
354:       row = i + j*mx;
355:       if (i == 0 || j == 0 || i == mx-1 || j == my-1) {
356:         f[row] = x[row];
357:         continue;
358:       }
359:       u      = x[row];
360:       ub     = x[row - mx];
361:       ul     = x[row - 1];
362:       ut     = x[row + mx];
363:       ur     = x[row + 1];
364:       uxx    = (-ur + two*u - ul)*hydhx;
365:       uyy    = (-ut + two*u - ub)*hxdhy;
366:       f[row] = uxx + uyy - sc*lambda*PetscExpScalar(u);
367:     }
368:   }

370:   /*
371:      Restore vectors
372:   */
373:   VecRestoreArrayRead(X,&x);
374:   VecRestoreArray(F,&f);
375:   return 0;
376: }
377: /* ------------------------------------------------------------------- */
380: /*
381:    FormJacobian - Evaluates Jacobian matrix.

383:    Input Parameters:
384: .  snes - the SNES context
385: .  x - input vector
386: .  ptr - optional user-defined context, as set by SNESSetJacobian()

388:    Output Parameters:
389: .  A - Jacobian matrix
390: .  B - optionally different preconditioning matrix
391: .  flag - flag indicating matrix structure
392: */
393: PetscErrorCode FormJacobian(SNES snes,Vec X,Mat J,Mat jac,void *ptr)
394: {
395:   AppCtx            *user = (AppCtx*)ptr;   /* user-defined applicatin context */
396:   PetscInt          i,j,row,mx,my,col[5];
397:   PetscErrorCode    ierr;
398:   PetscScalar       two = 2.0,one = 1.0,lambda,v[5],sc;
399:   const PetscScalar *x;
400:   PetscReal         hx,hy,hxdhy,hydhx;

402:   mx     = user->mx;
403:   my     = user->my;
404:   lambda = user->param;
405:   hx     = 1.0 / (PetscReal)(mx-1);
406:   hy     = 1.0 / (PetscReal)(my-1);
407:   sc     = hx*hy;
408:   hxdhy  = hx/hy;
409:   hydhx  = hy/hx;

411:   /*
412:      Get pointer to vector data
413:   */
414:   VecGetArrayRead(X,&x);

416:   /*
417:      Compute entries of the Jacobian
418:   */
419:   for (j=0; j<my; j++) {
420:     for (i=0; i<mx; i++) {
421:       row = i + j*mx;
422:       if (i == 0 || j == 0 || i == mx-1 || j == my-1) {
423:         MatSetValues(jac,1,&row,1,&row,&one,INSERT_VALUES);
424:         continue;
425:       }
426:       v[0] = -hxdhy; col[0] = row - mx;
427:       v[1] = -hydhx; col[1] = row - 1;
428:       v[2] = two*(hydhx + hxdhy) - sc*lambda*PetscExpScalar(x[row]); col[2] = row;
429:       v[3] = -hydhx; col[3] = row + 1;
430:       v[4] = -hxdhy; col[4] = row + mx;
431:       MatSetValues(jac,1,&row,5,col,v,INSERT_VALUES);
432:     }
433:   }

435:   /*
436:      Restore vector
437:   */
438:   VecRestoreArrayRead(X,&x);

440:   /*
441:      Assemble matrix
442:   */
443:   MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY);
444:   MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY);

446:   if (jac != J) {
447:     MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);
448:     MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);
449:   }

451:   return 0;
452: }