Actual source code: ex3.c

  1: static char help[] = "Solves a simple time-dependent linear PDE (the heat equation).\n\
  2: Input parameters include:\n\
  3:   -m <points>, where <points> = number of grid points\n\
  4:   -time_dependent_rhs : Treat the problem as having a time-dependent right-hand side\n\
  5:   -use_ifunc          : Use IFunction/IJacobian interface\n\
  6:   -debug              : Activate debugging printouts\n\
  7:   -nox                : Deactivate x-window graphics\n\n";

  9: /* ------------------------------------------------------------------------

 11:    This program solves the one-dimensional heat equation (also called the
 12:    diffusion equation),
 13:        u_t = u_xx,
 14:    on the domain 0 <= x <= 1, with the boundary conditions
 15:        u(t,0) = 0, u(t,1) = 0,
 16:    and the initial condition
 17:        u(0,x) = sin(6*pi*x) + 3*sin(2*pi*x).
 18:    This is a linear, second-order, parabolic equation.

 20:    We discretize the right-hand side using finite differences with
 21:    uniform grid spacing h:
 22:        u_xx = (u_{i+1} - 2u_{i} + u_{i-1})/(h^2)
 23:    We then demonstrate time evolution using the various TS methods by
 24:    running the program via
 25:        ex3 -ts_type <timestepping solver>

 27:    We compare the approximate solution with the exact solution, given by
 28:        u_exact(x,t) = exp(-36*pi*pi*t) * sin(6*pi*x) +
 29:                       3*exp(-4*pi*pi*t) * sin(2*pi*x)

 31:    Notes:
 32:    This code demonstrates the TS solver interface to two variants of
 33:    linear problems, u_t = f(u,t), namely
 34:      - time-dependent f:   f(u,t) is a function of t
 35:      - time-independent f: f(u,t) is simply f(u)

 37:     The parallel version of this code is ts/tutorials/ex4.c

 39:   ------------------------------------------------------------------------- */

 41: /*
 42:    Include "petscts.h" so that we can use the TS integrators.  Note that this file
 43:    automatically includes:
 44:      petscsys.h  - base PETSc routines
 45:      petscis.h   - index sets
 46:      petscvec.h  - vectors
 47:      petscmat.h  - matrices
 48:      petscpc.h   - preconditioners
 49:      petscksp.h  - linear solvers
 50:      petscsnes.h - nonlinear solvers
 51: */

 53: #include <petscts.h>
 54: #include <petscdraw.h>

 56: /*
 57:    User-defined application context - contains data needed by the
 58:    application-provided call-back routines.
 59: */
 60: typedef struct {
 61:   Vec         solution;         /* global exact solution vector */
 62:   PetscInt    m;                /* total number of grid points */
 63:   PetscReal   h;                /* mesh width h = 1/(m-1) */
 64:   PetscBool   debug;            /* flag (1 indicates activation of debugging printouts) */
 65:   PetscViewer viewer1, viewer2; /* viewers for the solution and error */
 66:   PetscReal   norm_2, norm_max; /* error norms */
 67:   Mat         A;                /* RHS mat, used with IFunction interface */
 68:   PetscReal   oshift;           /* old shift applied, prevent to recompute the IJacobian */
 69: } AppCtx;

 71: /*
 72:    User-defined routines
 73: */
 74: extern PetscErrorCode InitialConditions(Vec, AppCtx *);
 75: extern PetscErrorCode RHSMatrixHeat(TS, PetscReal, Vec, Mat, Mat, void *);
 76: extern PetscErrorCode IFunctionHeat(TS, PetscReal, Vec, Vec, Vec, void *);
 77: extern PetscErrorCode IJacobianHeat(TS, PetscReal, Vec, Vec, PetscReal, Mat, Mat, void *);
 78: extern PetscErrorCode Monitor(TS, PetscInt, PetscReal, Vec, void *);
 79: extern PetscErrorCode ExactSolution(PetscReal, Vec, AppCtx *);

 81: int main(int argc, char **argv)
 82: {
 83:   AppCtx      appctx;                 /* user-defined application context */
 84:   TS          ts;                     /* timestepping context */
 85:   Mat         A;                      /* matrix data structure */
 86:   Vec         u;                      /* approximate solution vector */
 87:   PetscReal   time_total_max = 100.0; /* default max total time */
 88:   PetscInt    time_steps_max = 100;   /* default max timesteps */
 89:   PetscDraw   draw;                   /* drawing context */
 90:   PetscInt    steps, m;
 91:   PetscMPIInt size;
 92:   PetscReal   dt;
 93:   PetscBool   flg, flg_string;

 95:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 96:      Initialize program and set problem parameters
 97:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 99:   PetscFunctionBeginUser;
100:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
101:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
102:   PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "This is a uniprocessor example only!");

104:   m = 60;
105:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));
106:   PetscCall(PetscOptionsHasName(NULL, NULL, "-debug", &appctx.debug));
107:   flg_string = PETSC_FALSE;
108:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-test_string_viewer", &flg_string, NULL));

110:   appctx.m        = m;
111:   appctx.h        = 1.0 / (m - 1.0);
112:   appctx.norm_2   = 0.0;
113:   appctx.norm_max = 0.0;

115:   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Solving a linear TS problem on 1 processor\n"));

117:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
118:      Create vector data structures
119:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

121:   /*
122:      Create vector data structures for approximate and exact solutions
123:   */
124:   PetscCall(VecCreateSeq(PETSC_COMM_SELF, m, &u));
125:   PetscCall(VecDuplicate(u, &appctx.solution));

127:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
128:      Set up displays to show graphs of the solution and error
129:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

131:   PetscCall(PetscViewerDrawOpen(PETSC_COMM_SELF, NULL, "", 80, 380, 400, 160, &appctx.viewer1));
132:   PetscCall(PetscViewerDrawGetDraw(appctx.viewer1, 0, &draw));
133:   PetscCall(PetscDrawSetDoubleBuffer(draw));
134:   PetscCall(PetscViewerDrawOpen(PETSC_COMM_SELF, NULL, "", 80, 0, 400, 160, &appctx.viewer2));
135:   PetscCall(PetscViewerDrawGetDraw(appctx.viewer2, 0, &draw));
136:   PetscCall(PetscDrawSetDoubleBuffer(draw));

138:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
139:      Create timestepping solver context
140:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

142:   PetscCall(TSCreate(PETSC_COMM_SELF, &ts));
143:   PetscCall(TSSetProblemType(ts, TS_LINEAR));

145:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
146:      Set optional user-defined monitoring routine
147:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

149:   if (!flg_string) PetscCall(TSMonitorSet(ts, Monitor, &appctx, NULL));

151:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

153:      Create matrix data structure; set matrix evaluation routine.
154:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

156:   PetscCall(MatCreate(PETSC_COMM_SELF, &A));
157:   PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, m, m));
158:   PetscCall(MatSetFromOptions(A));
159:   PetscCall(MatSetUp(A));

161:   flg = PETSC_FALSE;
162:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-use_ifunc", &flg, NULL));
163:   if (!flg) {
164:     appctx.A = NULL;
165:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-time_dependent_rhs", &flg, NULL));
166:     if (flg) {
167:       /*
168:          For linear problems with a time-dependent f(u,t) in the equation
169:          u_t = f(u,t), the user provides the discretized right-hand side
170:          as a time-dependent matrix.
171:       */
172:       PetscCall(TSSetRHSFunction(ts, NULL, TSComputeRHSFunctionLinear, &appctx));
173:       PetscCall(TSSetRHSJacobian(ts, A, A, RHSMatrixHeat, &appctx));
174:     } else {
175:       /*
176:          For linear problems with a time-independent f(u) in the equation
177:          u_t = f(u), the user provides the discretized right-hand side
178:          as a matrix only once, and then sets the special Jacobian evaluation
179:          routine TSComputeRHSJacobianConstant() which will NOT recompute the Jacobian.
180:       */
181:       PetscCall(RHSMatrixHeat(ts, 0.0, u, A, A, &appctx));
182:       PetscCall(TSSetRHSFunction(ts, NULL, TSComputeRHSFunctionLinear, &appctx));
183:       PetscCall(TSSetRHSJacobian(ts, A, A, TSComputeRHSJacobianConstant, &appctx));
184:     }
185:   } else {
186:     Mat J;

188:     PetscCall(RHSMatrixHeat(ts, 0.0, u, A, A, &appctx));
189:     PetscCall(MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, &J));
190:     PetscCall(TSSetIFunction(ts, NULL, IFunctionHeat, &appctx));
191:     PetscCall(TSSetIJacobian(ts, J, J, IJacobianHeat, &appctx));
192:     PetscCall(MatDestroy(&J));

194:     PetscCall(PetscObjectReference((PetscObject)A));
195:     appctx.A      = A;
196:     appctx.oshift = PETSC_MIN_REAL;
197:   }
198:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
199:      Set solution vector and initial timestep
200:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

202:   dt = appctx.h * appctx.h / 2.0;
203:   PetscCall(TSSetTimeStep(ts, dt));

205:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
206:      Customize timestepping solver:
207:        - Set the solution method to be the Backward Euler method.
208:        - Set timestepping duration info
209:      Then set runtime options, which can override these defaults.
210:      For example,
211:           -ts_max_steps <maxsteps> -ts_max_time <maxtime>
212:      to override the defaults set by TSSetMaxSteps()/TSSetMaxTime().
213:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

215:   PetscCall(TSSetMaxSteps(ts, time_steps_max));
216:   PetscCall(TSSetMaxTime(ts, time_total_max));
217:   PetscCall(TSSetExactFinalTime(ts, TS_EXACTFINALTIME_STEPOVER));
218:   PetscCall(TSSetFromOptions(ts));

220:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
221:      Solve the problem
222:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

224:   /*
225:      Evaluate initial conditions
226:   */
227:   PetscCall(InitialConditions(u, &appctx));

229:   /*
230:      Run the timestepping solver
231:   */
232:   PetscCall(TSSolve(ts, u));
233:   PetscCall(TSGetStepNumber(ts, &steps));

235:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
236:      View timestepping solver info
237:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

239:   PetscCall(PetscPrintf(PETSC_COMM_SELF, "avg. error (2 norm) = %g, avg. error (max norm) = %g\n", (double)(appctx.norm_2 / steps), (double)(appctx.norm_max / steps)));
240:   if (!flg_string) {
241:     PetscCall(TSView(ts, PETSC_VIEWER_STDOUT_SELF));
242:   } else {
243:     PetscViewer stringviewer;
244:     char        string[512];
245:     const char *outstring;

247:     PetscCall(PetscViewerStringOpen(PETSC_COMM_WORLD, string, sizeof(string), &stringviewer));
248:     PetscCall(TSView(ts, stringviewer));
249:     PetscCall(PetscViewerStringGetStringRead(stringviewer, &outstring, NULL));
250:     PetscCheck((char *)outstring == (char *)string, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "String returned from viewer does not equal original string");
251:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Output from string viewer:%s\n", outstring));
252:     PetscCall(PetscViewerDestroy(&stringviewer));
253:   }

255:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
256:      Free work space.  All PETSc objects should be destroyed when they
257:      are no longer needed.
258:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

260:   PetscCall(TSDestroy(&ts));
261:   PetscCall(MatDestroy(&A));
262:   PetscCall(VecDestroy(&u));
263:   PetscCall(PetscViewerDestroy(&appctx.viewer1));
264:   PetscCall(PetscViewerDestroy(&appctx.viewer2));
265:   PetscCall(VecDestroy(&appctx.solution));
266:   PetscCall(MatDestroy(&appctx.A));

268:   /*
269:      Always call PetscFinalize() before exiting a program.  This routine
270:        - finalizes the PETSc libraries as well as MPI
271:        - provides summary and diagnostic information if certain runtime
272:          options are chosen (e.g., -log_view).
273:   */
274:   PetscCall(PetscFinalize());
275:   return 0;
276: }
277: /* --------------------------------------------------------------------- */
278: /*
279:    InitialConditions - Computes the solution at the initial time.

281:    Input Parameter:
282:    u - uninitialized solution vector (global)
283:    appctx - user-defined application context

285:    Output Parameter:
286:    u - vector with solution at initial time (global)
287: */
288: PetscErrorCode InitialConditions(Vec u, AppCtx *appctx)
289: {
290:   PetscScalar *u_localptr, h = appctx->h;
291:   PetscInt     i;

293:   PetscFunctionBeginUser;
294:   /*
295:     Get a pointer to vector data.
296:     - For default PETSc vectors, VecGetArray() returns a pointer to
297:       the data array.  Otherwise, the routine is implementation dependent.
298:     - You MUST call VecRestoreArray() when you no longer need access to
299:       the array.
300:     - Note that the Fortran interface to VecGetArray() differs from the
301:       C version.  See the users manual for details.
302:   */
303:   PetscCall(VecGetArrayWrite(u, &u_localptr));

305:   /*
306:      We initialize the solution array by simply writing the solution
307:      directly into the array locations.  Alternatively, we could use
308:      VecSetValues() or VecSetValuesLocal().
309:   */
310:   for (i = 0; i < appctx->m; i++) u_localptr[i] = PetscSinScalar(PETSC_PI * i * 6. * h) + 3. * PetscSinScalar(PETSC_PI * i * 2. * h);

312:   /*
313:      Restore vector
314:   */
315:   PetscCall(VecRestoreArrayWrite(u, &u_localptr));

317:   /*
318:      Print debugging information if desired
319:   */
320:   if (appctx->debug) {
321:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Initial guess vector\n"));
322:     PetscCall(VecView(u, PETSC_VIEWER_STDOUT_SELF));
323:   }
324:   PetscFunctionReturn(PETSC_SUCCESS);
325: }
326: /* --------------------------------------------------------------------- */
327: /*
328:    ExactSolution - Computes the exact solution at a given time.

330:    Input Parameters:
331:    t - current time
332:    solution - vector in which exact solution will be computed
333:    appctx - user-defined application context

335:    Output Parameter:
336:    solution - vector with the newly computed exact solution
337: */
338: PetscErrorCode ExactSolution(PetscReal t, Vec solution, AppCtx *appctx)
339: {
340:   PetscScalar *s_localptr, h = appctx->h, ex1, ex2, sc1, sc2, tc = t;
341:   PetscInt     i;

343:   PetscFunctionBeginUser;
344:   /*
345:      Get a pointer to vector data.
346:   */
347:   PetscCall(VecGetArrayWrite(solution, &s_localptr));

349:   /*
350:      Simply write the solution directly into the array locations.
351:      Alternatively, we culd use VecSetValues() or VecSetValuesLocal().
352:   */
353:   ex1 = PetscExpScalar(-36. * PETSC_PI * PETSC_PI * tc);
354:   ex2 = PetscExpScalar(-4. * PETSC_PI * PETSC_PI * tc);
355:   sc1 = PETSC_PI * 6. * h;
356:   sc2 = PETSC_PI * 2. * h;
357:   for (i = 0; i < appctx->m; i++) s_localptr[i] = PetscSinScalar(sc1 * (PetscReal)i) * ex1 + 3. * PetscSinScalar(sc2 * (PetscReal)i) * ex2;

359:   /*
360:      Restore vector
361:   */
362:   PetscCall(VecRestoreArrayWrite(solution, &s_localptr));
363:   PetscFunctionReturn(PETSC_SUCCESS);
364: }
365: /* --------------------------------------------------------------------- */
366: /*
367:    Monitor - User-provided routine to monitor the solution computed at
368:    each timestep.  This example plots the solution and computes the
369:    error in two different norms.

371:    This example also demonstrates changing the timestep via TSSetTimeStep().

373:    Input Parameters:
374:    ts     - the timestep context
375:    step   - the count of the current step (with 0 meaning the
376:              initial condition)
377:    time   - the current time
378:    u      - the solution at this timestep
379:    ctx    - the user-provided context for this monitoring routine.
380:             In this case we use the application context which contains
381:             information about the problem size, workspace and the exact
382:             solution.
383: */
384: PetscErrorCode Monitor(TS ts, PetscInt step, PetscReal time, Vec u, PetscCtx ctx)
385: {
386:   AppCtx   *appctx = (AppCtx *)ctx; /* user-defined application context */
387:   PetscReal norm_2, norm_max, dt, dttol;

389:   PetscFunctionBeginUser;
390:   /*
391:      View a graph of the current iterate
392:   */
393:   PetscCall(VecView(u, appctx->viewer2));

395:   /*
396:      Compute the exact solution
397:   */
398:   PetscCall(ExactSolution(time, appctx->solution, appctx));

400:   /*
401:      Print debugging information if desired
402:   */
403:   if (appctx->debug) {
404:     PetscCall(PetscPrintf(PETSC_COMM_SELF, "Computed solution vector\n"));
405:     PetscCall(VecView(u, PETSC_VIEWER_STDOUT_SELF));
406:     PetscCall(PetscPrintf(PETSC_COMM_SELF, "Exact solution vector\n"));
407:     PetscCall(VecView(appctx->solution, PETSC_VIEWER_STDOUT_SELF));
408:   }

410:   /*
411:      Compute the 2-norm and max-norm of the error
412:   */
413:   PetscCall(VecAXPY(appctx->solution, -1.0, u));
414:   PetscCall(VecNorm(appctx->solution, NORM_2, &norm_2));
415:   norm_2 = PetscSqrtReal(appctx->h) * norm_2;
416:   PetscCall(VecNorm(appctx->solution, NORM_MAX, &norm_max));

418:   PetscCall(TSGetTimeStep(ts, &dt));
419:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Timestep %3" PetscInt_FMT ": step size = %g, time = %g, 2-norm error = %g, max norm error = %g\n", step, (double)dt, (double)time, (double)norm_2, (double)norm_max));

421:   appctx->norm_2 += norm_2;
422:   appctx->norm_max += norm_max;

424:   dttol = .0001;
425:   PetscCall(PetscOptionsGetReal(NULL, NULL, "-dttol", &dttol, NULL));
426:   if (dt < dttol) {
427:     dt *= .999;
428:     PetscCall(TSSetTimeStep(ts, dt));
429:   }

431:   /*
432:      View a graph of the error
433:   */
434:   PetscCall(VecView(appctx->solution, appctx->viewer1));

436:   /*
437:      Print debugging information if desired
438:   */
439:   if (appctx->debug) {
440:     PetscCall(PetscPrintf(PETSC_COMM_SELF, "Error vector\n"));
441:     PetscCall(VecView(appctx->solution, PETSC_VIEWER_STDOUT_SELF));
442:   }
443:   PetscFunctionReturn(PETSC_SUCCESS);
444: }
445: /* --------------------------------------------------------------------- */
446: /*
447:    RHSMatrixHeat - User-provided routine to compute the right-hand-side
448:    matrix for the heat equation.

450:    Input Parameters:
451:    ts - the TS context
452:    t - current time
453:    global_in - global input vector
454:    dummy - optional user-defined context, as set by TSetRHSJacobian()

456:    Output Parameters:
457:    AA - Jacobian matrix
458:    BB - optionally different matrix used to construct the preconditioner

460:    Notes:
461:    Recall that MatSetValues() uses 0-based row and column numbers
462:    in Fortran as well as in C.
463: */
464: PetscErrorCode RHSMatrixHeat(TS ts, PetscReal t, Vec X, Mat AA, Mat BB, PetscCtx ctx)
465: {
466:   Mat         A      = AA;            /* Jacobian matrix */
467:   AppCtx     *appctx = (AppCtx *)ctx; /* user-defined application context */
468:   PetscInt    mstart = 0;
469:   PetscInt    mend   = appctx->m;
470:   PetscInt    i, idx[3];
471:   PetscScalar v[3], stwo = -2. / (appctx->h * appctx->h), sone = -.5 * stwo;

473:   PetscFunctionBeginUser;
474:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
475:      Compute entries for the locally owned part of the matrix
476:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
477:   /*
478:      Set matrix rows corresponding to boundary data
479:   */

481:   mstart = 0;
482:   v[0]   = 1.0;
483:   PetscCall(MatSetValues(A, 1, &mstart, 1, &mstart, v, INSERT_VALUES));
484:   mstart++;

486:   mend--;
487:   v[0] = 1.0;
488:   PetscCall(MatSetValues(A, 1, &mend, 1, &mend, v, INSERT_VALUES));

490:   /*
491:      Set matrix rows corresponding to interior data.  We construct the
492:      matrix one row at a time.
493:   */
494:   v[0] = sone;
495:   v[1] = stwo;
496:   v[2] = sone;
497:   for (i = mstart; i < mend; i++) {
498:     idx[0] = i - 1;
499:     idx[1] = i;
500:     idx[2] = i + 1;
501:     PetscCall(MatSetValues(A, 1, &i, 3, idx, v, INSERT_VALUES));
502:   }

504:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
505:      Complete the matrix assembly process and set some options
506:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
507:   /*
508:      Assemble matrix, using the 2-step process:
509:        MatAssemblyBegin(), MatAssemblyEnd()
510:      Computations can be done while messages are in transition
511:      by placing code between these two statements.
512:   */
513:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
514:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));

516:   /*
517:      Set and option to indicate that we will never add a new nonzero location
518:      to the matrix. If we do, it will generate an error.
519:   */
520:   PetscCall(MatSetOption(A, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
521:   PetscFunctionReturn(PETSC_SUCCESS);
522: }

524: PetscErrorCode IFunctionHeat(TS ts, PetscReal t, Vec X, Vec Xdot, Vec r, PetscCtx ctx)
525: {
526:   AppCtx *appctx = (AppCtx *)ctx; /* user-defined application context */

528:   PetscFunctionBeginUser;
529:   PetscCall(MatMult(appctx->A, X, r));
530:   PetscCall(VecAYPX(r, -1.0, Xdot));
531:   PetscFunctionReturn(PETSC_SUCCESS);
532: }

534: PetscErrorCode IJacobianHeat(TS ts, PetscReal t, Vec X, Vec Xdot, PetscReal s, Mat A, Mat B, PetscCtx ctx)
535: {
536:   AppCtx *appctx = (AppCtx *)ctx; /* user-defined application context */

538:   PetscFunctionBeginUser;
539:   if (appctx->oshift == s) PetscFunctionReturn(PETSC_SUCCESS);
540:   PetscCall(MatCopy(appctx->A, A, SAME_NONZERO_PATTERN));
541:   PetscCall(MatScale(A, -1));
542:   PetscCall(MatShift(A, s));
543:   PetscCall(MatCopy(A, B, SAME_NONZERO_PATTERN));
544:   appctx->oshift = s;
545:   PetscFunctionReturn(PETSC_SUCCESS);
546: }

548: /*TEST

550:     test:
551:       args: -nox -ts_type ssp -ts_time_step 0.0005

553:     test:
554:       suffix: 2
555:       args: -nox -ts_type ssp -ts_time_step 0.0005 -time_dependent_rhs 1

557:     test:
558:       suffix: 3
559:       args: -nox -ts_type rosw -ts_max_steps 3 -ksp_converged_reason
560:       filter: sed "s/ATOL/RTOL/g"
561:       requires: !single

563:     test:
564:       suffix: 4
565:       args: -nox -ts_type beuler -ts_max_steps 3 -ksp_converged_reason
566:       filter: sed "s/ATOL/RTOL/g"

568:     test:
569:       suffix: 5
570:       args: -nox -ts_type beuler -ts_max_steps 3 -ksp_converged_reason -time_dependent_rhs
571:       filter: sed "s/ATOL/RTOL/g"

573:     test:
574:       requires: !single
575:       suffix: pod_guess
576:       args: -nox -ts_type beuler -use_ifunc -ts_time_step 0.0005 -ksp_guess_type pod -pc_type none -ksp_converged_reason

578:     test:
579:       requires: !single
580:       suffix: pod_guess_Ainner
581:       args: -nox -ts_type beuler -use_ifunc -ts_time_step 0.0005 -ksp_guess_type pod -ksp_guess_pod_Ainner -pc_type none -ksp_converged_reason

583:     test:
584:       requires: !single
585:       suffix: fischer_guess
586:       args: -nox -ts_type beuler -use_ifunc -ts_time_step 0.0005 -ksp_guess_type fischer -pc_type none -ksp_converged_reason

588:     test:
589:       requires: !single
590:       suffix: fischer_guess_2
591:       args: -nox -ts_type beuler -use_ifunc -ts_time_step 0.0005 -ksp_guess_type fischer -ksp_guess_fischer_model 2,10 -pc_type none -ksp_converged_reason

593:     test:
594:       requires: !single
595:       suffix: fischer_guess_3
596:       args: -nox -ts_type beuler -use_ifunc -ts_time_step 0.0005 -ksp_guess_type fischer -ksp_guess_fischer_model 3,10 -pc_type none -ksp_converged_reason

598:     test:
599:       requires: !single
600:       suffix: stringview
601:       args: -nox -ts_type rosw -test_string_viewer

603:     test:
604:       requires: !single
605:       suffix: stringview_euler
606:       args: -nox -ts_type euler -test_string_viewer

608: TEST*/