Actual source code: ex3.c

  1: static char help[] = "Model Equations for Advection-Diffusion\n";

  3: /*
  4:     Page 9, Section 1.2 Model Equations for Advection-Diffusion

  6:           u_t = a u_x + d u_xx

  8:    The initial conditions used here different then in the book.

 10: */

 12: /*
 13:      Helpful runtime linear solver options:
 14:            -pc_type mg -da_refine 2 -snes_monitor -ksp_monitor -ts_view   (geometric multigrid with three levels)

 16: */

 18: /*
 19:    Include "petscts.h" so that we can use the TS ODE integrators.  Note that this file
 20:    automatically includes:
 21:      petscsys.h  - base PETSc routines
 22:      petscis.h   - index sets
 23:      petscvec.h  - vectors
 24:      petscmat.h  - matrices
 25:      petscpc.h   - preconditioners
 26:      petscksp.h  - linear solvers
 27:      petscsnes.h - nonlinear solvers
 28: */

 30: #include <petscts.h>
 31: #include <petscdm.h>
 32: #include <petscdmda.h>

 34: /*
 35:    User-defined application context - contains data needed by the
 36:    application-provided call-back routines.
 37: */
 38: typedef struct {
 39:   PetscScalar a, d; /* advection and diffusion strength */
 40:   PetscBool   upwind;
 41: } AppCtx;

 43: /*
 44:    User-defined routines
 45: */
 46: extern PetscErrorCode InitialConditions(TS, Vec, AppCtx *);
 47: extern PetscErrorCode RHSMatrixHeat(TS, PetscReal, Vec, Mat, Mat, void *);
 48: extern PetscErrorCode Solution(TS, PetscReal, Vec, AppCtx *);

 50: int main(int argc, char **argv)
 51: {
 52:   AppCtx    appctx; /* user-defined application context */
 53:   TS        ts;     /* timestepping context */
 54:   Vec       U;      /* approximate solution vector */
 55:   PetscReal dt;
 56:   DM        da;
 57:   PetscInt  M;

 59:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 60:      Initialize program and set problem parameters
 61:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 63:   PetscFunctionBeginUser;
 64:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 65:   appctx.a = 1.0;
 66:   appctx.d = 0.0;
 67:   PetscCall(PetscOptionsGetScalar(NULL, NULL, "-a", &appctx.a, NULL));
 68:   PetscCall(PetscOptionsGetScalar(NULL, NULL, "-d", &appctx.d, NULL));
 69:   appctx.upwind = PETSC_TRUE;
 70:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-upwind", &appctx.upwind, NULL));

 72:   PetscCall(DMDACreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_PERIODIC, 60, 1, 1, NULL, &da));
 73:   PetscCall(DMSetFromOptions(da));
 74:   PetscCall(DMSetUp(da));
 75:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 76:      Create vector data structures
 77:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 79:   /*
 80:      Create vector data structures for approximate and exact solutions
 81:   */
 82:   PetscCall(DMCreateGlobalVector(da, &U));

 84:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 85:      Create timestepping solver context
 86:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 88:   PetscCall(TSCreate(PETSC_COMM_WORLD, &ts));
 89:   PetscCall(TSSetDM(ts, da));

 91:   /*
 92:       For linear problems with a time-dependent f(U,t) in the equation
 93:      u_t = f(u,t), the user provides the discretized right-hand side
 94:       as a time-dependent matrix.
 95:   */
 96:   PetscCall(TSSetRHSFunction(ts, NULL, TSComputeRHSFunctionLinear, &appctx));
 97:   PetscCall(TSSetRHSJacobian(ts, NULL, NULL, RHSMatrixHeat, &appctx));
 98:   PetscCall(TSSetSolutionFunction(ts, (PetscErrorCode (*)(TS, PetscReal, Vec, void *))Solution, &appctx));

100:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
101:      Customize timestepping solver:
102:        - Set timestepping duration info
103:      Then set runtime options, which can override these defaults.
104:      For example,
105:           -ts_max_steps <maxsteps> -ts_max_time <maxtime>
106:      to override the defaults set by TSSetMaxSteps()/TSSetMaxTime().
107:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

109:   PetscCall(DMDAGetInfo(da, PETSC_IGNORE, &M, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL));
110:   dt = .48 / (M * M);
111:   PetscCall(TSSetTimeStep(ts, dt));
112:   PetscCall(TSSetMaxSteps(ts, 1000));
113:   PetscCall(TSSetMaxTime(ts, 100.0));
114:   PetscCall(TSSetExactFinalTime(ts, TS_EXACTFINALTIME_STEPOVER));
115:   PetscCall(TSSetType(ts, TSARKIMEX));
116:   PetscCall(TSSetFromOptions(ts));

118:   /*
119:      Evaluate initial conditions
120:   */
121:   PetscCall(InitialConditions(ts, U, &appctx));

123:   /*
124:      Run the timestepping solver
125:   */
126:   PetscCall(TSSolve(ts, U));

128:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
129:      Free work space.  All PETSc objects should be destroyed when they
130:      are no longer needed.
131:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

133:   PetscCall(TSDestroy(&ts));
134:   PetscCall(VecDestroy(&U));
135:   PetscCall(DMDestroy(&da));

137:   /*
138:      Always call PetscFinalize() before exiting a program.  This routine
139:        - finalizes the PETSc libraries as well as MPI
140:        - provides summary and diagnostic information if certain runtime
141:          options are chosen (e.g., -log_view).
142:   */
143:   PetscCall(PetscFinalize());
144:   return 0;
145: }
146: /* --------------------------------------------------------------------- */
147: /*
148:    InitialConditions - Computes the solution at the initial time.

150:    Input Parameter:
151:    u - uninitialized solution vector (global)
152:    appctx - user-defined application context

154:    Output Parameter:
155:    u - vector with solution at initial time (global)
156: */
157: PetscErrorCode InitialConditions(TS ts, Vec U, AppCtx *appctx)
158: {
159:   PetscScalar *u, h;
160:   PetscInt     i, mstart, mend, xm, M;
161:   DM           da;

163:   PetscFunctionBeginUser;
164:   PetscCall(TSGetDM(ts, &da));
165:   PetscCall(DMDAGetCorners(da, &mstart, NULL, NULL, &xm, NULL, NULL));
166:   PetscCall(DMDAGetInfo(da, PETSC_IGNORE, &M, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL));
167:   h    = 1.0 / M;
168:   mend = mstart + xm;
169:   /*
170:     Get a pointer to vector data.
171:     - For default PETSc vectors, VecGetArray() returns a pointer to
172:       the data array.  Otherwise, the routine is implementation dependent.
173:     - You MUST call VecRestoreArray() when you no longer need access to
174:       the array.
175:     - Note that the Fortran interface to VecGetArray() differs from the
176:       C version.  See the users manual for details.
177:   */
178:   PetscCall(DMDAVecGetArray(da, U, &u));

180:   /*
181:      We initialize the solution array by simply writing the solution
182:      directly into the array locations.  Alternatively, we could use
183:      VecSetValues() or VecSetValuesLocal().
184:   */
185:   for (i = mstart; i < mend; i++) u[i] = PetscSinScalar(PETSC_PI * i * 6. * h) + 3. * PetscSinScalar(PETSC_PI * i * 2. * h);

187:   /*
188:      Restore vector
189:   */
190:   PetscCall(DMDAVecRestoreArray(da, U, &u));
191:   PetscFunctionReturn(PETSC_SUCCESS);
192: }
193: /* --------------------------------------------------------------------- */
194: /*
195:    Solution - Computes the exact solution at a given time.

197:    Input Parameters:
198:    t - current time
199:    solution - vector in which exact solution will be computed
200:    appctx - user-defined application context

202:    Output Parameter:
203:    solution - vector with the newly computed exact solution
204: */
205: PetscErrorCode Solution(TS ts, PetscReal t, Vec U, AppCtx *appctx)
206: {
207:   PetscScalar *u, ex1, ex2, sc1, sc2, h;
208:   PetscInt     i, mstart, mend, xm, M;
209:   DM           da;

211:   PetscFunctionBeginUser;
212:   PetscCall(TSGetDM(ts, &da));
213:   PetscCall(DMDAGetCorners(da, &mstart, NULL, NULL, &xm, NULL, NULL));
214:   PetscCall(DMDAGetInfo(da, PETSC_IGNORE, &M, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL));
215:   h    = 1.0 / M;
216:   mend = mstart + xm;
217:   /*
218:      Get a pointer to vector data.
219:   */
220:   PetscCall(DMDAVecGetArray(da, U, &u));

222:   /*
223:      Simply write the solution directly into the array locations.
224:      Alternatively, we culd use VecSetValues() or VecSetValuesLocal().
225:   */
226:   ex1 = PetscExpScalar(-36. * PETSC_PI * PETSC_PI * appctx->d * t);
227:   ex2 = PetscExpScalar(-4. * PETSC_PI * PETSC_PI * appctx->d * t);
228:   sc1 = PETSC_PI * 6. * h;
229:   sc2 = PETSC_PI * 2. * h;
230:   for (i = mstart; i < mend; i++) u[i] = PetscSinScalar(sc1 * (PetscReal)i + appctx->a * PETSC_PI * 6. * t) * ex1 + 3. * PetscSinScalar(sc2 * (PetscReal)i + appctx->a * PETSC_PI * 2. * t) * ex2;

232:   /*
233:      Restore vector
234:   */
235:   PetscCall(DMDAVecRestoreArray(da, U, &u));
236:   PetscFunctionReturn(PETSC_SUCCESS);
237: }

239: /* --------------------------------------------------------------------- */
240: /*
241:    RHSMatrixHeat - User-provided routine to compute the right-hand-side
242:    matrix for the heat equation.

244:    Input Parameters:
245:    ts - the TS context
246:    t - current time
247:    global_in - global input vector
248:    dummy - optional user-defined context, as set by TSetRHSJacobian()

250:    Output Parameters:
251:    AA - Jacobian matrix
252:    BB - optionally different matrix used to construct the preconditioner

254:    Notes:
255:    Recall that MatSetValues() uses 0-based row and column numbers
256:    in Fortran as well as in C.
257: */
258: PetscErrorCode RHSMatrixHeat(TS ts, PetscReal t, Vec U, Mat AA, Mat BB, PetscCtx ctx)
259: {
260:   Mat         A      = AA;            /* Jacobian matrix */
261:   AppCtx     *appctx = (AppCtx *)ctx; /* user-defined application context */
262:   PetscInt    mstart, mend;
263:   PetscInt    i, idx[3], M, xm;
264:   PetscScalar v[3], h;
265:   DM          da;

267:   PetscFunctionBeginUser;
268:   PetscCall(TSGetDM(ts, &da));
269:   PetscCall(DMDAGetInfo(da, NULL, &M, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL));
270:   PetscCall(DMDAGetCorners(da, &mstart, NULL, NULL, &xm, NULL, NULL));
271:   h    = 1.0 / M;
272:   mend = mstart + xm;
273:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
274:      Compute entries for the locally owned part of the matrix
275:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
276:   /*
277:      Set matrix rows corresponding to boundary data
278:   */

280:   /* diffusion */
281:   v[0] = appctx->d / (h * h);
282:   v[1] = -2.0 * appctx->d / (h * h);
283:   v[2] = appctx->d / (h * h);
284:   if (!mstart) {
285:     idx[0] = M - 1;
286:     idx[1] = 0;
287:     idx[2] = 1;
288:     PetscCall(MatSetValues(A, 1, &mstart, 3, idx, v, INSERT_VALUES));
289:     mstart++;
290:   }

292:   if (mend == M) {
293:     mend--;
294:     idx[0] = M - 2;
295:     idx[1] = M - 1;
296:     idx[2] = 0;
297:     PetscCall(MatSetValues(A, 1, &mend, 3, idx, v, INSERT_VALUES));
298:   }

300:   /*
301:      Set matrix rows corresponding to interior data.  We construct the
302:      matrix one row at a time.
303:   */
304:   for (i = mstart; i < mend; i++) {
305:     idx[0] = i - 1;
306:     idx[1] = i;
307:     idx[2] = i + 1;
308:     PetscCall(MatSetValues(A, 1, &i, 3, idx, v, INSERT_VALUES));
309:   }
310:   PetscCall(MatAssemblyBegin(A, MAT_FLUSH_ASSEMBLY));
311:   PetscCall(MatAssemblyEnd(A, MAT_FLUSH_ASSEMBLY));

313:   PetscCall(DMDAGetCorners(da, &mstart, NULL, NULL, &xm, NULL, NULL));
314:   mend = mstart + xm;
315:   if (!appctx->upwind) {
316:     /* advection -- centered differencing */
317:     v[0] = -.5 * appctx->a / (h);
318:     v[1] = .5 * appctx->a / (h);
319:     if (!mstart) {
320:       idx[0] = M - 1;
321:       idx[1] = 1;
322:       PetscCall(MatSetValues(A, 1, &mstart, 2, idx, v, ADD_VALUES));
323:       mstart++;
324:     }

326:     if (mend == M) {
327:       mend--;
328:       idx[0] = M - 2;
329:       idx[1] = 0;
330:       PetscCall(MatSetValues(A, 1, &mend, 2, idx, v, ADD_VALUES));
331:     }

333:     for (i = mstart; i < mend; i++) {
334:       idx[0] = i - 1;
335:       idx[1] = i + 1;
336:       PetscCall(MatSetValues(A, 1, &i, 2, idx, v, ADD_VALUES));
337:     }
338:   } else {
339:     /* advection -- upwinding */
340:     v[0] = -appctx->a / (h);
341:     v[1] = appctx->a / (h);
342:     if (!mstart) {
343:       idx[0] = 0;
344:       idx[1] = 1;
345:       PetscCall(MatSetValues(A, 1, &mstart, 2, idx, v, ADD_VALUES));
346:       mstart++;
347:     }

349:     if (mend == M) {
350:       mend--;
351:       idx[0] = M - 1;
352:       idx[1] = 0;
353:       PetscCall(MatSetValues(A, 1, &mend, 2, idx, v, ADD_VALUES));
354:     }

356:     for (i = mstart; i < mend; i++) {
357:       idx[0] = i;
358:       idx[1] = i + 1;
359:       PetscCall(MatSetValues(A, 1, &i, 2, idx, v, ADD_VALUES));
360:     }
361:   }

363:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
364:      Complete the matrix assembly process and set some options
365:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
366:   /*
367:      Assemble matrix, using the 2-step process:
368:        MatAssemblyBegin(), MatAssemblyEnd()
369:      Computations can be done while messages are in transition
370:      by placing code between these two statements.
371:   */
372:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
373:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));

375:   /*
376:      Set and option to indicate that we will never add a new nonzero location
377:      to the matrix. If we do, it will generate an error.
378:   */
379:   PetscCall(MatSetOption(A, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
380:   PetscFunctionReturn(PETSC_SUCCESS);
381: }

383: /*TEST

385:    test:
386:       args: -pc_type mg -da_refine 2 -ts_view -ts_monitor -ts_max_time .3 -mg_levels_ksp_max_it 3
387:       requires: double
388:       filter: grep -v "total number of"

390:    test:
391:       suffix: 2
392:       args: -pc_type mg -da_refine 2 -ts_view -ts_monitor_draw_solution -ts_monitor -ts_max_time .3 -mg_levels_ksp_max_it 3
393:       requires: x
394:       output_file: output/ex3_1.out
395:       requires: double
396:       filter: grep -v "total number of"

398: TEST*/