Actual source code: ex5.c


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

  9: /*
 10:    Concepts: TS^time-dependent linear problems
 11:    Concepts: TS^heat equation
 12:    Concepts: TS^diffusion equation
 13:    Processors: 1
 14: */

 16: /* ------------------------------------------------------------------------

 18:    This program solves the one-dimensional heat equation (also called the
 19:    diffusion equation),
 20:        u_t = u_xx,
 21:    on the domain 0 <= x <= 1, with the boundary conditions
 22:        u(t,0) = 1, u(t,1) = 1,
 23:    and the initial condition
 24:        u(0,x) = cos(6*pi*x) + 3*cos(2*pi*x).
 25:    This is a linear, second-order, parabolic equation.

 27:    We discretize the right-hand side using finite differences with
 28:    uniform grid spacing h:
 29:        u_xx = (u_{i+1} - 2u_{i} + u_{i-1})/(h^2)
 30:    We then demonstrate time evolution using the various TS methods by
 31:    running the program via
 32:        ex3 -ts_type <timestepping solver>

 34:    We compare the approximate solution with the exact solution, given by
 35:        u_exact(x,t) = exp(-36*pi*pi*t) * cos(6*pi*x) +
 36:                       3*exp(-4*pi*pi*t) * cos(2*pi*x)

 38:    Notes:
 39:    This code demonstrates the TS solver interface to two variants of
 40:    linear problems, u_t = f(u,t), namely
 41:      - time-dependent f:   f(u,t) is a function of t
 42:      - time-independent f: f(u,t) is simply just f(u)

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

 46:   ------------------------------------------------------------------------- */

 48: /*
 49:    Include "petscts.h" so that we can use TS solvers.  Note that this file
 50:    automatically includes:
 51:      petscsys.h       - base PETSc routines   petscvec.h  - vectors
 52:      petscmat.h  - matrices
 53:      petscis.h     - index sets            petscksp.h  - Krylov subspace methods
 54:      petscviewer.h - viewers               petscpc.h   - preconditioners
 55:      petscksp.h   - linear solvers        petscsnes.h - nonlinear solvers
 56: */
 57: #include <petscts.h>
 58: #include <petscdraw.h>

 60: /*
 61:    User-defined application context - contains data needed by the
 62:    application-provided call-back routines.
 63: */
 64: typedef struct {
 65:   Vec         solution;          /* global exact solution vector */
 66:   PetscInt    m;                      /* total number of grid points */
 67:   PetscReal   h;                 /* mesh width h = 1/(m-1) */
 68:   PetscBool   debug;             /* flag (1 indicates activation of debugging printouts) */
 69:   PetscViewer viewer1,viewer2;  /* viewers for the solution and error */
 70:   PetscReal   norm_2,norm_max;  /* error norms */
 71: } AppCtx;

 73: /*
 74:    User-defined routines
 75: */
 76: extern PetscErrorCode InitialConditions(Vec,AppCtx*);
 77: extern PetscErrorCode RHSMatrixHeat(TS,PetscReal,Vec,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:   PetscBool      flg;
 93:   PetscReal      dt,ftime;

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

 99:   PetscInitialize(&argc,&argv,(char*)0,help);
100:   MPI_Comm_size(PETSC_COMM_WORLD,&size);

103:   m               = 60;
104:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
105:   PetscOptionsHasName(NULL,NULL,"-debug",&appctx.debug);
106:   appctx.m        = m;
107:   appctx.h        = 1.0/(m-1.0);
108:   appctx.norm_2   = 0.0;
109:   appctx.norm_max = 0.0;

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

113:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
114:      Create vector data structures
115:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

117:   /*
118:      Create vector data structures for approximate and exact solutions
119:   */
120:   VecCreateSeq(PETSC_COMM_SELF,m,&u);
121:   VecDuplicate(u,&appctx.solution);

123:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
124:      Set up displays to show graphs of the solution and error
125:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

127:   PetscViewerDrawOpen(PETSC_COMM_SELF,0,"",80,380,400,160,&appctx.viewer1);
128:   PetscViewerDrawGetDraw(appctx.viewer1,0,&draw);
129:   PetscDrawSetDoubleBuffer(draw);
130:   PetscViewerDrawOpen(PETSC_COMM_SELF,0,"",80,0,400,160,&appctx.viewer2);
131:   PetscViewerDrawGetDraw(appctx.viewer2,0,&draw);
132:   PetscDrawSetDoubleBuffer(draw);

134:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
135:      Create timestepping solver context
136:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

138:   TSCreate(PETSC_COMM_SELF,&ts);
139:   TSSetProblemType(ts,TS_LINEAR);

141:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
142:      Set optional user-defined monitoring routine
143:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

145:   TSMonitorSet(ts,Monitor,&appctx,NULL);

147:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

149:      Create matrix data structure; set matrix evaluation routine.
150:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

152:   MatCreate(PETSC_COMM_SELF,&A);
153:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m,m);
154:   MatSetFromOptions(A);
155:   MatSetUp(A);

157:   PetscOptionsHasName(NULL,NULL,"-time_dependent_rhs",&flg);
158:   if (flg) {
159:     /*
160:        For linear problems with a time-dependent f(u,t) in the equation
161:        u_t = f(u,t), the user provides the discretized right-hand-side
162:        as a time-dependent matrix.
163:     */
164:     TSSetRHSFunction(ts,NULL,TSComputeRHSFunctionLinear,&appctx);
165:     TSSetRHSJacobian(ts,A,A,RHSMatrixHeat,&appctx);
166:   } else {
167:     /*
168:        For linear problems with a time-independent f(u) in the equation
169:        u_t = f(u), the user provides the discretized right-hand-side
170:        as a matrix only once, and then sets a null matrix evaluation
171:        routine.
172:     */
173:     RHSMatrixHeat(ts,0.0,u,A,A,&appctx);
174:     TSSetRHSFunction(ts,NULL,TSComputeRHSFunctionLinear,&appctx);
175:     TSSetRHSJacobian(ts,A,A,TSComputeRHSJacobianConstant,&appctx);
176:   }

178:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
179:      Set solution vector and initial timestep
180:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

182:   dt   = appctx.h*appctx.h/2.0;
183:   TSSetTimeStep(ts,dt);
184:   TSSetSolution(ts,u);

186:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
187:      Customize timestepping solver:
188:        - Set the solution method to be the Backward Euler method.
189:        - Set timestepping duration info
190:      Then set runtime options, which can override these defaults.
191:      For example,
192:           -ts_max_steps <maxsteps> -ts_max_time <maxtime>
193:      to override the defaults set by TSSetMaxSteps()/TSSetMaxTime().
194:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

196:   TSSetMaxSteps(ts,time_steps_max);
197:   TSSetMaxTime(ts,time_total_max);
198:   TSSetExactFinalTime(ts,TS_EXACTFINALTIME_STEPOVER);
199:   TSSetFromOptions(ts);

201:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
202:      Solve the problem
203:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

205:   /*
206:      Evaluate initial conditions
207:   */
208:   InitialConditions(u,&appctx);

210:   /*
211:      Run the timestepping solver
212:   */
213:   TSSolve(ts,u);
214:   TSGetSolveTime(ts,&ftime);
215:   TSGetStepNumber(ts,&steps);

217:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
218:      View timestepping solver info
219:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

221:   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));
222:   TSView(ts,PETSC_VIEWER_STDOUT_SELF);

224:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
225:      Free work space.  All PETSc objects should be destroyed when they
226:      are no longer needed.
227:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

229:   TSDestroy(&ts);
230:   MatDestroy(&A);
231:   VecDestroy(&u);
232:   PetscViewerDestroy(&appctx.viewer1);
233:   PetscViewerDestroy(&appctx.viewer2);
234:   VecDestroy(&appctx.solution);

236:   /*
237:      Always call PetscFinalize() before exiting a program.  This routine
238:        - finalizes the PETSc libraries as well as MPI
239:        - provides summary and diagnostic information if certain runtime
240:          options are chosen (e.g., -log_view).
241:   */
242:   PetscFinalize();
243:   return 0;
244: }
245: /* --------------------------------------------------------------------- */
246: /*
247:    InitialConditions - Computes the solution at the initial time.

249:    Input Parameter:
250:    u - uninitialized solution vector (global)
251:    appctx - user-defined application context

253:    Output Parameter:
254:    u - vector with solution at initial time (global)
255: */
256: PetscErrorCode InitialConditions(Vec u,AppCtx *appctx)
257: {
258:   PetscScalar    *u_localptr,h = appctx->h;
259:   PetscInt       i;

261:   /*
262:     Get a pointer to vector data.
263:     - For default PETSc vectors, VecGetArray() returns a pointer to
264:       the data array.  Otherwise, the routine is implementation dependent.
265:     - You MUST call VecRestoreArray() when you no longer need access to
266:       the array.
267:     - Note that the Fortran interface to VecGetArray() differs from the
268:       C version.  See the users manual for details.
269:   */
270:   VecGetArray(u,&u_localptr);

272:   /*
273:      We initialize the solution array by simply writing the solution
274:      directly into the array locations.  Alternatively, we could use
275:      VecSetValues() or VecSetValuesLocal().
276:   */
277:   for (i=0; i<appctx->m; i++) u_localptr[i] = PetscCosScalar(PETSC_PI*i*6.*h) + 3.*PetscCosScalar(PETSC_PI*i*2.*h);

279:   /*
280:      Restore vector
281:   */
282:   VecRestoreArray(u,&u_localptr);

284:   /*
285:      Print debugging information if desired
286:   */
287:   if (appctx->debug) {
288:     printf("initial guess vector\n");
289:     VecView(u,PETSC_VIEWER_STDOUT_SELF);
290:   }

292:   return 0;
293: }
294: /* --------------------------------------------------------------------- */
295: /*
296:    ExactSolution - Computes the exact solution at a given time.

298:    Input Parameters:
299:    t - current time
300:    solution - vector in which exact solution will be computed
301:    appctx - user-defined application context

303:    Output Parameter:
304:    solution - vector with the newly computed exact solution
305: */
306: PetscErrorCode ExactSolution(PetscReal t,Vec solution,AppCtx *appctx)
307: {
308:   PetscScalar    *s_localptr,h = appctx->h,ex1,ex2,sc1,sc2,tc = t;
309:   PetscInt       i;

311:   /*
312:      Get a pointer to vector data.
313:   */
314:   VecGetArray(solution,&s_localptr);

316:   /*
317:      Simply write the solution directly into the array locations.
318:      Alternatively, we culd use VecSetValues() or VecSetValuesLocal().
319:   */
320:   ex1 = PetscExpScalar(-36.*PETSC_PI*PETSC_PI*tc); ex2 = PetscExpScalar(-4.*PETSC_PI*PETSC_PI*tc);
321:   sc1 = PETSC_PI*6.*h;                 sc2 = PETSC_PI*2.*h;
322:   for (i=0; i<appctx->m; i++) s_localptr[i] = PetscCosScalar(sc1*(PetscReal)i)*ex1 + 3.*PetscCosScalar(sc2*(PetscReal)i)*ex2;

324:   /*
325:      Restore vector
326:   */
327:   VecRestoreArray(solution,&s_localptr);
328:   return 0;
329: }
330: /* --------------------------------------------------------------------- */
331: /*
332:    Monitor - User-provided routine to monitor the solution computed at
333:    each timestep.  This example plots the solution and computes the
334:    error in two different norms.

336:    Input Parameters:
337:    ts     - the timestep context
338:    step   - the count of the current step (with 0 meaning the
339:              initial condition)
340:    time   - the current time
341:    u      - the solution at this timestep
342:    ctx    - the user-provided context for this monitoring routine.
343:             In this case we use the application context which contains
344:             information about the problem size, workspace and the exact
345:             solution.
346: */
347: PetscErrorCode Monitor(TS ts,PetscInt step,PetscReal time,Vec u,void *ctx)
348: {
349:   AppCtx         *appctx = (AppCtx*) ctx;   /* user-defined application context */
350:   PetscReal      norm_2,norm_max;

352:   /*
353:      View a graph of the current iterate
354:   */
355:   VecView(u,appctx->viewer2);

357:   /*
358:      Compute the exact solution
359:   */
360:   ExactSolution(time,appctx->solution,appctx);

362:   /*
363:      Print debugging information if desired
364:   */
365:   if (appctx->debug) {
366:     printf("Computed solution vector\n");
367:     VecView(u,PETSC_VIEWER_STDOUT_SELF);
368:     printf("Exact solution vector\n");
369:     VecView(appctx->solution,PETSC_VIEWER_STDOUT_SELF);
370:   }

372:   /*
373:      Compute the 2-norm and max-norm of the error
374:   */
375:   VecAXPY(appctx->solution,-1.0,u);
376:   VecNorm(appctx->solution,NORM_2,&norm_2);
377:   norm_2 = PetscSqrtReal(appctx->h)*norm_2;
378:   VecNorm(appctx->solution,NORM_MAX,&norm_max);
379:   if (norm_2   < 1e-14) norm_2   = 0;
380:   if (norm_max < 1e-14) norm_max = 0;

382:   PetscPrintf(PETSC_COMM_WORLD,"Timestep %D: time = %g, 2-norm error = %g, max norm error = %g\n",step,(double)time,(double)norm_2,(double)norm_max);
383:   appctx->norm_2   += norm_2;
384:   appctx->norm_max += norm_max;

386:   /*
387:      View a graph of the error
388:   */
389:   VecView(appctx->solution,appctx->viewer1);

391:   /*
392:      Print debugging information if desired
393:   */
394:   if (appctx->debug) {
395:     printf("Error vector\n");
396:     VecView(appctx->solution,PETSC_VIEWER_STDOUT_SELF);
397:   }

399:   return 0;
400: }
401: /* --------------------------------------------------------------------- */
402: /*
403:    RHSMatrixHeat - User-provided routine to compute the right-hand-side
404:    matrix for the heat equation.

406:    Input Parameters:
407:    ts - the TS context
408:    t - current time
409:    global_in - global input vector
410:    dummy - optional user-defined context, as set by TSetRHSJacobian()

412:    Output Parameters:
413:    AA - Jacobian matrix
414:    BB - optionally different preconditioning matrix
415:    str - flag indicating matrix structure

417:   Notes:
418:   Recall that MatSetValues() uses 0-based row and column numbers
419:   in Fortran as well as in C.
420: */
421: PetscErrorCode RHSMatrixHeat(TS ts,PetscReal t,Vec X,Mat AA,Mat BB,void *ctx)
422: {
423:   Mat            A       = AA;                /* Jacobian matrix */
424:   AppCtx         *appctx = (AppCtx*)ctx;     /* user-defined application context */
425:   PetscInt       mstart  = 0;
426:   PetscInt       mend    = appctx->m;
427:   PetscInt       i,idx[3];
428:   PetscScalar    v[3],stwo = -2./(appctx->h*appctx->h),sone = -.5*stwo;

430:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
431:      Compute entries for the locally owned part of the matrix
432:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
433:   /*
434:      Set matrix rows corresponding to boundary data
435:   */

437:   mstart = 0;
438:   v[0]   = 1.0;
439:   MatSetValues(A,1,&mstart,1,&mstart,v,INSERT_VALUES);
440:   mstart++;

442:   mend--;
443:   v[0] = 1.0;
444:   MatSetValues(A,1,&mend,1,&mend,v,INSERT_VALUES);

446:   /*
447:      Set matrix rows corresponding to interior data.  We construct the
448:      matrix one row at a time.
449:   */
450:   v[0] = sone; v[1] = stwo; v[2] = sone;
451:   for (i=mstart; i<mend; i++) {
452:     idx[0] = i-1; idx[1] = i; idx[2] = i+1;
453:     MatSetValues(A,1,&i,3,idx,v,INSERT_VALUES);
454:   }

456:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
457:      Complete the matrix assembly process and set some options
458:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
459:   /*
460:      Assemble matrix, using the 2-step process:
461:        MatAssemblyBegin(), MatAssemblyEnd()
462:      Computations can be done while messages are in transition
463:      by placing code between these two statements.
464:   */
465:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
466:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

468:   /*
469:      Set and option to indicate that we will never add a new nonzero location
470:      to the matrix. If we do, it will generate an error.
471:   */
472:   MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);

474:   return 0;
475: }

477: /*TEST

479:     test:
480:       requires: x

482:     test:
483:       suffix: nox
484:       args: -nox

486: TEST*/