Actual source code: ex1.c

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

  8: /*

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

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

 15:     with boundary conditions

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

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

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

 25: */

 27: /*
 28:    Include "petscsnes.h" so that we can use SNES solvers.  Note that
 29:    this file automatically includes:
 30:      petscsys.h - base PETSc routines
 31:      petscvec.h - vectors
 32:      petscmat.h - matrices
 33:      petscis.h  - index sets
 34:      petscpc.h  - preconditioners
 35:      petscksp.h - linear solvers
 36: */

 38: #include <petscsnes.h>

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

 51: /*
 52:    User-defined routines
 53: */
 54: static PetscErrorCode FormJacobian(SNES, Vec, Mat, Mat, void *);
 55: static PetscErrorCode FormFunction(SNES, Vec, Vec, void *);
 56: static PetscErrorCode FormInitialGuess(AppCtx *, Vec);
 57: static PetscErrorCode ConvergenceTest(KSP, PetscInt, PetscReal, KSPConvergedReason *, void *);
 58: static PetscErrorCode ConvergenceDestroy(PetscCtxRt);
 59: static PetscErrorCode postcheck(SNES, Vec, Vec, Vec, PetscBool *, PetscBool *, void *);
 60: static PetscErrorCode monitor_change_deltamax(SNES, PetscInt, PetscReal, void *);

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

 76:   PetscFunctionBeginUser;
 77:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 78:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
 79:   PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "This is a uniprocessor example only!");

 81:   /*
 82:      Initialize problem parameters
 83:   */
 84:   user.mx    = 4;
 85:   user.my    = 4;
 86:   user.param = 6.0;
 87:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-mx", &user.mx, NULL));
 88:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-my", &user.my, NULL));
 89:   PetscCall(PetscOptionsGetReal(NULL, NULL, "-par", &user.param, NULL));
 90:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-pc", &pc, NULL));
 91:   PetscCheck(user.param < bratu_lambda_max && user.param > bratu_lambda_min, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Lambda is out of range");
 92:   N = user.mx * user.my;
 93:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-use_convergence_test", &use_convergence_test, NULL));
 94:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-prune_jacobian", &prunejacobian, NULL));
 95:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-null_appctx", &null_appctx, NULL));
 96:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-test_tr_deltamax", &test_tr_deltamax, NULL));

 98:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 99:      Create nonlinear solver context
100:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

102:   PetscCall(SNESCreate(PETSC_COMM_WORLD, &snes));
103:   if (pc) {
104:     PetscCall(SNESSetType(snes, SNESNEWTONTR));
105:     PetscCall(SNESNewtonTRSetPostCheck(snes, postcheck, NULL));
106:   }
107:   if (test_tr_deltamax) {
108:     PetscCall(SNESSetType(snes, SNESNEWTONTR));
109:     PetscCall(SNESMonitorSet(snes, monitor_change_deltamax, NULL, NULL));
110:   }

112:   /* Test application context handling from Python */
113:   if (!null_appctx) PetscCall(SNESSetApplicationContext(snes, (void *)&user));

115:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
116:      Create vector data structures; set function evaluation routine
117:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

119:   PetscCall(VecCreate(PETSC_COMM_WORLD, &x));
120:   PetscCall(VecSetSizes(x, PETSC_DECIDE, N));
121:   PetscCall(VecSetFromOptions(x));
122:   PetscCall(VecDuplicate(x, &r));

124:   /*
125:      Set function evaluation routine and vector.  Whenever the nonlinear
126:      solver needs to evaluate the nonlinear function, it will call this
127:      routine.
128:       - Note that the final routine argument is the user-defined
129:         context that provides application-specific data for the
130:         function evaluation routine.
131:   */
132:   PetscCall(SNESSetFunction(snes, r, FormFunction, (void *)&user));

134:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
135:      Create matrix data structure; set Jacobian evaluation routine
136:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

138:   /*
139:      Create matrix. Here we only approximately preallocate storage space
140:      for the Jacobian.  See the users manual for a discussion of better
141:      techniques for preallocating matrix memory.
142:   */
143:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-snes_mf", &matrix_free, NULL));
144:   if (!matrix_free) {
145:     PetscBool matrix_free_operator = PETSC_FALSE;
146:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-snes_mf_operator", &matrix_free_operator, NULL));
147:     if (matrix_free_operator) matrix_free = PETSC_FALSE;
148:   }
149:   if (!matrix_free) PetscCall(MatCreateSeqAIJ(PETSC_COMM_WORLD, N, N, 5, NULL, &J));

151:   /*
152:      This option will cause the Jacobian to be computed via finite differences
153:     efficiently using a coloring of the columns of the matrix.
154:   */
155:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-snes_fd_coloring", &fd_coloring, NULL));
156:   PetscCheck(!matrix_free || !fd_coloring, PETSC_COMM_WORLD, PETSC_ERR_ARG_INCOMP, "Use only one of -snes_mf, -snes_fd_coloring options! You can do -snes_mf_operator -snes_fd_coloring");

158:   if (fd_coloring) {
159:     ISColoring  iscoloring;
160:     MatColoring mc;
161:     if (prunejacobian) {
162:       /* Initialize x with random nonzero values so that the nonzeros in the Jacobian
163:          can better reflect the sparsity structure of the Jacobian. */
164:       PetscRandom rctx;
165:       PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rctx));
166:       PetscCall(PetscRandomSetInterval(rctx, 1.0, 2.0));
167:       PetscCall(VecSetRandom(x, rctx));
168:       PetscCall(PetscRandomDestroy(&rctx));
169:     }

171:     /*
172:       This initializes the nonzero structure of the Jacobian. This is artificial
173:       because clearly if we had a routine to compute the Jacobian we won't need
174:       to use finite differences.
175:     */
176:     PetscCall(FormJacobian(snes, x, J, J, &user));

178:     /*
179:        Color the matrix, i.e. determine groups of columns that share no common
180:       rows. These columns in the Jacobian can all be computed simultaneously.
181:     */
182:     PetscCall(MatColoringCreate(J, &mc));
183:     PetscCall(MatColoringSetType(mc, MATCOLORINGSL));
184:     PetscCall(MatColoringSetFromOptions(mc));
185:     PetscCall(MatColoringApply(mc, &iscoloring));
186:     PetscCall(MatColoringDestroy(&mc));
187:     /*
188:        Create the data structure that SNESComputeJacobianDefaultColor() uses
189:        to compute the actual Jacobians via finite differences.
190:     */
191:     PetscCall(MatFDColoringCreate(J, iscoloring, &fdcoloring));
192:     PetscCall(MatFDColoringSetFunction(fdcoloring, (MatFDColoringFn *)FormFunction, &user));
193:     PetscCall(MatFDColoringSetFromOptions(fdcoloring));
194:     PetscCall(MatFDColoringSetUp(J, iscoloring, fdcoloring));
195:     /*
196:         Tell SNES to use the routine SNESComputeJacobianDefaultColor()
197:       to compute Jacobians.
198:     */
199:     PetscCall(SNESSetJacobian(snes, J, J, SNESComputeJacobianDefaultColor, fdcoloring));
200:     PetscCall(ISColoringDestroy(&iscoloring));
201:     if (prunejacobian) PetscCall(SNESPruneJacobianColor(snes, J, J));
202:   }
203:   /*
204:      Set Jacobian matrix data structure and default Jacobian evaluation
205:      routine.  Whenever the nonlinear solver needs to compute the
206:      Jacobian matrix, it will call this routine.
207:       - Note that the final routine argument is the user-defined
208:         context that provides application-specific data for the
209:         Jacobian evaluation routine.
210:       - The user can override with:
211:          -snes_fd : default finite differencing approximation of Jacobian
212:          -snes_mf : matrix-free Newton-Krylov method with no preconditioning
213:                     (unless user explicitly sets preconditioner)
214:          -snes_mf_operator : form matrix used to construct the preconditioner as set by the user,
215:                              but use matrix-free approx for Jacobian-vector
216:                              products within Newton-Krylov method
217:   */
218:   else if (!matrix_free) {
219:     PetscCall(SNESSetJacobian(snes, J, J, FormJacobian, (void *)&user));
220:   }

222:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
223:      Customize nonlinear solver; set runtime options
224:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

226:   /*
227:      Set runtime options (e.g., -snes_monitor -snes_rtol <rtol> -ksp_type <type>)
228:   */
229:   PetscCall(SNESSetFromOptions(snes));

231:   /*
232:      Set array that saves the function norms.  This array is intended
233:      when the user wants to save the convergence history for later use
234:      rather than just to view the function norms via -snes_monitor.
235:   */
236:   PetscCall(SNESSetConvergenceHistory(snes, history, hist_its, 50, PETSC_TRUE));

238:   /*
239:       Add a user provided convergence test; this is to test that SNESNEWTONTR properly calls the
240:       user provided test before the specialized test. The convergence context is just an array to
241:       test that it gets properly freed at the end
242:   */
243:   if (use_convergence_test) {
244:     PetscCall(SNESGetKSP(snes, &ksp));
245:     PetscCall(PetscMalloc1(5, &testarray));
246:     PetscCall(KSPSetConvergenceTest(ksp, ConvergenceTest, testarray, ConvergenceDestroy));
247:   }

249:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
250:      Evaluate initial guess; then solve nonlinear system
251:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
252:   /*
253:      Note: The user should initialize the vector, x, with the initial guess
254:      for the nonlinear solver prior to calling SNESSolve().  In particular,
255:      to employ an initial guess of zero, the user should explicitly set
256:      this vector to zero by calling VecSet().
257:   */
258:   PetscCall(FormInitialGuess(&user, x));
259:   PetscCall(SNESSolve(snes, NULL, x));
260:   PetscCall(SNESGetIterationNumber(snes, &its));
261:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Number of SNES iterations = %" PetscInt_FMT "\n", its));

263:   /*
264:      Print the convergence history.  This is intended just to demonstrate
265:      use of the data attained via SNESSetConvergenceHistory().
266:   */
267:   PetscCall(PetscOptionsHasName(NULL, NULL, "-print_history", &flg));
268:   if (flg) {
269:     for (i = 0; i < its + 1; i++) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "iteration %" PetscInt_FMT ": Linear iterations %" PetscInt_FMT " Function norm = %g\n", i, hist_its[i], (double)history[i]));
270:   }

272:   /* Test NewtonTR API */
273:   PetscCall(SNESNewtonTRSetTolerances(snes, 1.0, 2.0, 3.0));
274:   PetscCall(SNESNewtonTRSetUpdateParameters(snes, 4.0, 5.0, 6.0, 7.0, 8.0));
275:   PetscCall(PetscObjectTypeCompare((PetscObject)snes, SNESNEWTONTR, &flg));
276:   if (flg) {
277:     PetscReal tmp[5];

279:     PetscCall(SNESNewtonTRGetTolerances(snes, &tmp[0], &tmp[1], &tmp[2]));
280:     PetscCheck(tmp[0] == 1.0, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Wrong value");
281:     PetscCheck(tmp[1] == 2.0, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Wrong value");
282:     PetscCheck(tmp[2] == 3.0, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Wrong value");
283:     PetscCall(SNESNewtonTRGetUpdateParameters(snes, &tmp[0], &tmp[1], &tmp[2], &tmp[3], &tmp[4]));
284:     PetscCheck(tmp[0] == 4.0, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Wrong value");
285:     PetscCheck(tmp[1] == 5.0, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Wrong value");
286:     PetscCheck(tmp[2] == 6.0, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Wrong value");
287:     PetscCheck(tmp[3] == 7.0, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Wrong value");
288:     PetscCheck(tmp[4] == 8.0, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Wrong value");
289:   }

291:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
292:      Free work space.  All PETSc objects should be destroyed when they
293:      are no longer needed.
294:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

296:   if (!matrix_free) PetscCall(MatDestroy(&J));
297:   if (fd_coloring) PetscCall(MatFDColoringDestroy(&fdcoloring));
298:   PetscCall(VecDestroy(&x));
299:   PetscCall(VecDestroy(&r));
300:   PetscCall(SNESDestroy(&snes));
301:   PetscCall(PetscFinalize());
302:   return 0;
303: }

305: /*
306:    FormInitialGuess - Forms initial approximation.

308:    Input Parameters:
309:    user - user-defined application context
310:    X - vector

312:    Output Parameter:
313:    X - vector
314:  */
315: PetscErrorCode FormInitialGuess(AppCtx *user, Vec X)
316: {
317:   PetscInt     i, j, row, mx, my;
318:   PetscReal    lambda, temp1, temp, hx, hy;
319:   PetscScalar *x;

321:   PetscFunctionBeginUser;
322:   mx     = user->mx;
323:   my     = user->my;
324:   lambda = user->param;

326:   hx = 1.0 / (PetscReal)(mx - 1);
327:   hy = 1.0 / (PetscReal)(my - 1);

329:   /*
330:      Get a pointer to vector data.
331:        - For default PETSc vectors, VecGetArray() returns a pointer to
332:          the data array.  Otherwise, the routine is implementation dependent.
333:        - You MUST call VecRestoreArray() when you no longer need access to
334:          the array.
335:   */
336:   PetscCall(VecGetArray(X, &x));
337:   temp1 = lambda / (lambda + 1.0);
338:   for (j = 0; j < my; j++) {
339:     temp = (PetscReal)(PetscMin(j, my - j - 1)) * hy;
340:     for (i = 0; i < mx; i++) {
341:       row = i + j * mx;
342:       if (i == 0 || j == 0 || i == mx - 1 || j == my - 1) {
343:         x[row] = 0.0;
344:         continue;
345:       }
346:       x[row] = temp1 * PetscSqrtReal(PetscMin((PetscReal)(PetscMin(i, mx - i - 1)) * hx, temp));
347:     }
348:   }

350:   /*
351:      Restore vector
352:   */
353:   PetscCall(VecRestoreArray(X, &x));
354:   PetscFunctionReturn(PETSC_SUCCESS);
355: }

357: /*
358:    FormFunction - Evaluates nonlinear function, F(x).

360:    Input Parameters:
361: .  snes - the SNES context
362: .  X - input vector
363: .  ptr - optional user-defined context, as set by SNESSetFunction()

365:    Output Parameter:
366: .  F - function vector
367:  */
368: PetscErrorCode FormFunction(SNES snes, Vec X, Vec F, void *ptr)
369: {
370:   AppCtx            *user = (AppCtx *)ptr;
371:   PetscInt           i, j, row, mx, my;
372:   PetscReal          two = 2.0, one = 1.0, lambda, hx, hy, hxdhy, hydhx;
373:   PetscScalar        ut, ub, ul, ur, u, uxx, uyy, sc, *f;
374:   const PetscScalar *x;

376:   PetscFunctionBeginUser;
377:   mx     = user->mx;
378:   my     = user->my;
379:   lambda = user->param;
380:   hx     = one / (PetscReal)(mx - 1);
381:   hy     = one / (PetscReal)(my - 1);
382:   sc     = hx * hy;
383:   hxdhy  = hx / hy;
384:   hydhx  = hy / hx;

386:   /*
387:      Get pointers to vector data
388:   */
389:   PetscCall(VecGetArrayRead(X, &x));
390:   PetscCall(VecGetArray(F, &f));

392:   /*
393:      Compute function
394:   */
395:   for (j = 0; j < my; j++) {
396:     for (i = 0; i < mx; i++) {
397:       row = i + j * mx;
398:       if (i == 0 || j == 0 || i == mx - 1 || j == my - 1) {
399:         f[row] = x[row];
400:         continue;
401:       }
402:       u      = x[row];
403:       ub     = x[row - mx];
404:       ul     = x[row - 1];
405:       ut     = x[row + mx];
406:       ur     = x[row + 1];
407:       uxx    = (-ur + two * u - ul) * hydhx;
408:       uyy    = (-ut + two * u - ub) * hxdhy;
409:       f[row] = uxx + uyy - sc * lambda * PetscExpScalar(u);
410:     }
411:   }

413:   /*
414:      Restore vectors
415:   */
416:   PetscCall(VecRestoreArrayRead(X, &x));
417:   PetscCall(VecRestoreArray(F, &f));
418:   PetscFunctionReturn(PETSC_SUCCESS);
419: }

421: /*
422:    FormJacobian - Evaluates Jacobian matrix.

424:    Input Parameters:
425: .  snes - the SNES context
426: .  x - input vector
427: .  ptr - optional user-defined context, as set by SNESSetJacobian()

429:    Output Parameters:
430: .  A - Jacobian matrix
431: .  B - optionally different matrix used to construct the preconditioner

433: */
434: PetscErrorCode FormJacobian(SNES snes, Vec X, Mat J, Mat jac, void *ptr)
435: {
436:   AppCtx            *user = (AppCtx *)ptr; /* user-defined application context */
437:   PetscInt           i, j, row, mx, my, col[5];
438:   PetscScalar        two = 2.0, one = 1.0, lambda, v[5], sc;
439:   const PetscScalar *x;
440:   PetscReal          hx, hy, hxdhy, hydhx;

442:   PetscFunctionBeginUser;
443:   mx     = user->mx;
444:   my     = user->my;
445:   lambda = user->param;
446:   hx     = 1.0 / (PetscReal)(mx - 1);
447:   hy     = 1.0 / (PetscReal)(my - 1);
448:   sc     = hx * hy;
449:   hxdhy  = hx / hy;
450:   hydhx  = hy / hx;

452:   /*
453:      Get pointer to vector data
454:   */
455:   PetscCall(VecGetArrayRead(X, &x));

457:   /*
458:      Compute entries of the Jacobian
459:   */
460:   for (j = 0; j < my; j++) {
461:     for (i = 0; i < mx; i++) {
462:       row = i + j * mx;
463:       if (i == 0 || j == 0 || i == mx - 1 || j == my - 1) {
464:         PetscCall(MatSetValues(jac, 1, &row, 1, &row, &one, INSERT_VALUES));
465:         continue;
466:       }
467:       v[0]   = -hxdhy;
468:       col[0] = row - mx;
469:       v[1]   = -hydhx;
470:       col[1] = row - 1;
471:       v[2]   = two * (hydhx + hxdhy) - sc * lambda * PetscExpScalar(x[row]);
472:       col[2] = row;
473:       v[3]   = -hydhx;
474:       col[3] = row + 1;
475:       v[4]   = -hxdhy;
476:       col[4] = row + mx;
477:       PetscCall(MatSetValues(jac, 1, &row, 5, col, v, INSERT_VALUES));
478:     }
479:   }

481:   /*
482:      Restore vector
483:   */
484:   PetscCall(VecRestoreArrayRead(X, &x));

486:   /*
487:      Assemble matrix
488:   */
489:   PetscCall(MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY));
490:   PetscCall(MatAssemblyEnd(jac, MAT_FINAL_ASSEMBLY));

492:   if (jac != J) {
493:     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
494:     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
495:   }
496:   PetscFunctionReturn(PETSC_SUCCESS);
497: }

499: PetscErrorCode ConvergenceTest(KSP ksp, PetscInt it, PetscReal nrm, KSPConvergedReason *reason, PetscCtx ctx)
500: {
501:   PetscFunctionBeginUser;
502:   *reason = KSP_CONVERGED_ITERATING;
503:   if (it > 1) {
504:     *reason = KSP_CONVERGED_ITS;
505:     PetscCall(PetscInfo(NULL, "User provided convergence test returning after 2 iterations\n"));
506:   }
507:   PetscFunctionReturn(PETSC_SUCCESS);
508: }

510: PetscErrorCode ConvergenceDestroy(PetscCtxRt ctx)
511: {
512:   PetscFunctionBeginUser;
513:   PetscCall(PetscInfo(NULL, "User provided convergence destroy called\n"));
514:   PetscCall(PetscFree(*(void **)ctx));
515:   PetscFunctionReturn(PETSC_SUCCESS);
516: }

518: PetscErrorCode postcheck(SNES snes, Vec x, Vec y, Vec w, PetscBool *changed_y, PetscBool *changed_w, PetscCtx ctx)
519: {
520:   PetscReal norm;
521:   Vec       tmp;

523:   PetscFunctionBeginUser;
524:   PetscCall(VecDuplicate(x, &tmp));
525:   PetscCall(VecWAXPY(tmp, -1.0, x, w));
526:   PetscCall(VecNorm(tmp, NORM_2, &norm));
527:   PetscCall(VecDestroy(&tmp));
528:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Norm of search step %g\n", (double)norm));
529:   PetscFunctionReturn(PETSC_SUCCESS);
530: }

532: PetscErrorCode monitor_change_deltamax(SNES snes, PetscInt it, PetscReal fnorm, PetscCtx ctx)
533: {
534:   PetscFunctionBeginUser;
535:   if (it == 0) PetscCall(SNESNewtonTRSetTolerances(snes, PETSC_CURRENT, 0.01, PETSC_CURRENT));
536:   PetscFunctionReturn(PETSC_SUCCESS);
537: }

539: /*TEST

541:    build:
542:       requires: !single

544:    test:
545:       args: -ksp_orthogonalization_cgs_refinement_type refine_always

547:    test:
548:       suffix: 2
549:       args: -snes_monitor -snes_type newtontr -ksp_orthogonalization_cgs_refinement_type refine_always

551:    test:
552:       suffix: 2_trdeltamax_change
553:       args: -snes_monitor -snes_type newtontr -ksp_orthogonalization_cgs_refinement_type refine_always -pc -test_tr_deltamax

555:    test:
556:       suffix: 2a
557:       filter: grep -i KSPConvergedDefault > /dev/null && echo "Found KSPConvergedDefault"
558:       args: -snes_monitor -snes_type newtontr -ksp_orthogonalization_cgs_refinement_type refine_always -info
559:       requires: defined(PETSC_USE_INFO)

561:    test:
562:       suffix: 2b
563:       filter: grep -i  "User provided convergence test" > /dev/null  && echo "Found User provided convergence test"
564:       args: -snes_monitor -snes_type newtontr -ksp_orthogonalization_cgs_refinement_type refine_always -use_convergence_test -info
565:       requires: defined(PETSC_USE_INFO)

567:    test:
568:       suffix: 2c
569:       args: -snes_converged_reason -snes_type newtontr -snes_tr_qn {{same different}separate output} -pc_type mat -snes_view -snes_tr_qn_mat_type lmvmdfp -snes_tr_norm_type infinity

571:    test:
572:       suffix: 3
573:       args: -snes_monitor -mat_coloring_type sl -snes_fd_coloring -mx 8 -my 11 -ksp_orthogonalization_cgs_refinement_type refine_always

575:    test:
576:       suffix: 4
577:       args: -pc -par 6.807 -snes_monitor -snes_converged_reason

579:    test:
580:       suffix: 5
581:       args: -snes_monitor -mat_coloring_type sl -snes_fd_coloring -mx 8 -my 11 -ksp_orthogonalization_cgs_refinement_type refine_always -prune_jacobian
582:       output_file: output/ex1_3.out

584:    test:
585:       suffix: 6
586:       args: -snes_monitor draw:image:testfile -viewer_view

588:    test:
589:       suffix: python
590:       requires: petsc4py
591:       args: -python -snes_type python -snes_python_type ex1.py:MySNES -snes_view -null_appctx {{0 1}separate output}
592:       localrunfiles: ex1.py

594: TEST*/