xref: /petsc/src/ts/tests/ex12.c (revision 3ba1676111f5c958fe6c2729b46ca4d523958bb3)
1c4762a1bSJed Brown 
2c4762a1bSJed Brown static char help[] = "Tests PetscObjectSetOptions() for TS object\n\n";
3c4762a1bSJed Brown 
4c4762a1bSJed Brown /* ------------------------------------------------------------------------
5c4762a1bSJed Brown 
6c4762a1bSJed Brown    This program solves the PDE
7c4762a1bSJed Brown 
8c4762a1bSJed Brown                u * u_xx
9c4762a1bSJed Brown          u_t = ---------
10c4762a1bSJed Brown                2*(t+1)^2
11c4762a1bSJed Brown 
12c4762a1bSJed Brown     on the domain 0 <= x <= 1, with boundary conditions
13c4762a1bSJed Brown          u(t,0) = t + 1,  u(t,1) = 2*t + 2,
14c4762a1bSJed Brown     and initial condition
15c4762a1bSJed Brown          u(0,x) = 1 + x*x.
16c4762a1bSJed Brown 
17c4762a1bSJed Brown     The exact solution is:
18c4762a1bSJed Brown          u(t,x) = (1 + x*x) * (1 + t)
19c4762a1bSJed Brown 
20c4762a1bSJed Brown     Note that since the solution is linear in time and quadratic in x,
21c4762a1bSJed Brown     the finite difference scheme actually computes the "exact" solution.
22c4762a1bSJed Brown 
23c4762a1bSJed Brown     We use by default the backward Euler method.
24c4762a1bSJed Brown 
25c4762a1bSJed Brown   ------------------------------------------------------------------------- */
26c4762a1bSJed Brown 
27c4762a1bSJed Brown /*
28c4762a1bSJed Brown    Include "petscts.h" to use the PETSc timestepping routines. Note that
29c4762a1bSJed Brown    this file automatically includes "petscsys.h" and other lower-level
30c4762a1bSJed Brown    PETSc include files.
31c4762a1bSJed Brown 
32c4762a1bSJed Brown    Include the "petscdmda.h" to allow us to use the distributed array data
33c4762a1bSJed Brown    structures to manage the parallel grid.
34c4762a1bSJed Brown */
35c4762a1bSJed Brown #include <petscts.h>
36c4762a1bSJed Brown #include <petscdm.h>
37c4762a1bSJed Brown #include <petscdmda.h>
38c4762a1bSJed Brown #include <petscdraw.h>
39c4762a1bSJed Brown 
40c4762a1bSJed Brown /*
41c4762a1bSJed Brown    User-defined application context - contains data needed by the
42c4762a1bSJed Brown    application-provided callback routines.
43c4762a1bSJed Brown */
44c4762a1bSJed Brown typedef struct {
45c4762a1bSJed Brown   MPI_Comm  comm;      /* communicator */
46c4762a1bSJed Brown   DM        da;        /* distributed array data structure */
47c4762a1bSJed Brown   Vec       localwork; /* local ghosted work vector */
48c4762a1bSJed Brown   Vec       u_local;   /* local ghosted approximate solution vector */
49c4762a1bSJed Brown   Vec       solution;  /* global exact solution vector */
50c4762a1bSJed Brown   PetscInt  m;         /* total number of grid points */
51c4762a1bSJed Brown   PetscReal h;         /* mesh width: h = 1/(m-1) */
52c4762a1bSJed Brown } AppCtx;
53c4762a1bSJed Brown 
54c4762a1bSJed Brown /*
55c4762a1bSJed Brown    User-defined routines, provided below.
56c4762a1bSJed Brown */
57c4762a1bSJed Brown extern PetscErrorCode InitialConditions(Vec, AppCtx *);
58c4762a1bSJed Brown extern PetscErrorCode RHSFunction(TS, PetscReal, Vec, Vec, void *);
59c4762a1bSJed Brown extern PetscErrorCode RHSJacobian(TS, PetscReal, Vec, Mat, Mat, void *);
60c4762a1bSJed Brown extern PetscErrorCode ExactSolution(PetscReal, Vec, AppCtx *);
61c4762a1bSJed Brown 
62d71ae5a4SJacob Faibussowitsch int main(int argc, char **argv)
63d71ae5a4SJacob Faibussowitsch {
64c4762a1bSJed Brown   AppCtx       appctx;               /* user-defined application context */
65c4762a1bSJed Brown   TS           ts;                   /* timestepping context */
66c4762a1bSJed Brown   Mat          A;                    /* Jacobian matrix data structure */
67c4762a1bSJed Brown   Vec          u;                    /* approximate solution vector */
68c4762a1bSJed Brown   PetscInt     time_steps_max = 100; /* default max timesteps */
69c4762a1bSJed Brown   PetscReal    dt;
70c4762a1bSJed Brown   PetscReal    time_total_max = 100.0; /* default max total time */
71c4762a1bSJed Brown   PetscOptions options, optionscopy;
72c4762a1bSJed Brown 
73c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
74c4762a1bSJed Brown      Initialize program and set problem parameters
75c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
76c4762a1bSJed Brown 
77327415f7SBarry Smith   PetscFunctionBeginUser;
789566063dSJacob Faibussowitsch   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
79c4762a1bSJed Brown 
809566063dSJacob Faibussowitsch   PetscCall(PetscOptionsCreate(&options));
819566063dSJacob Faibussowitsch   PetscCall(PetscOptionsSetValue(options, "-ts_monitor", "ascii"));
829566063dSJacob Faibussowitsch   PetscCall(PetscOptionsSetValue(options, "-snes_monitor", "ascii"));
839566063dSJacob Faibussowitsch   PetscCall(PetscOptionsSetValue(options, "-ksp_monitor", "ascii"));
84c4762a1bSJed Brown 
85c4762a1bSJed Brown   appctx.comm = PETSC_COMM_WORLD;
86c4762a1bSJed Brown   appctx.m    = 60;
87c4762a1bSJed Brown 
889566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(options, NULL, "-M", &appctx.m, NULL));
89c4762a1bSJed Brown 
90c4762a1bSJed Brown   appctx.h = 1.0 / (appctx.m - 1.0);
91c4762a1bSJed Brown 
92c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
93c4762a1bSJed Brown      Create vector data structures
94c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
95c4762a1bSJed Brown 
96c4762a1bSJed Brown   /*
97c4762a1bSJed Brown      Create distributed array (DMDA) to manage parallel grid and vectors
98c4762a1bSJed Brown      and to set up the ghost point communication pattern.  There are M
99c4762a1bSJed Brown      total grid values spread equally among all the processors.
100c4762a1bSJed Brown   */
1019566063dSJacob Faibussowitsch   PetscCall(DMDACreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, appctx.m, 1, 1, NULL, &appctx.da));
1029566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptions((PetscObject)appctx.da, options));
1039566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions(appctx.da));
1049566063dSJacob Faibussowitsch   PetscCall(DMSetUp(appctx.da));
105c4762a1bSJed Brown 
106c4762a1bSJed Brown   /*
107c4762a1bSJed Brown      Extract global and local vectors from DMDA; we use these to store the
108c4762a1bSJed Brown      approximate solution.  Then duplicate these for remaining vectors that
109c4762a1bSJed Brown      have the same types.
110c4762a1bSJed Brown   */
1119566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(appctx.da, &u));
1129566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(appctx.da, &appctx.u_local));
113c4762a1bSJed Brown 
114c4762a1bSJed Brown   /*
115c4762a1bSJed Brown      Create local work vector for use in evaluating right-hand-side function;
116c4762a1bSJed Brown      create global work vector for storing exact solution.
117c4762a1bSJed Brown   */
1189566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(appctx.u_local, &appctx.localwork));
1199566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(u, &appctx.solution));
120c4762a1bSJed Brown 
121c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
122c4762a1bSJed Brown      Create timestepping solver context; set callback routine for
123c4762a1bSJed Brown      right-hand-side function evaluation.
124c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
125c4762a1bSJed Brown 
1269566063dSJacob Faibussowitsch   PetscCall(TSCreate(PETSC_COMM_WORLD, &ts));
1279566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptions((PetscObject)ts, options));
1289566063dSJacob Faibussowitsch   PetscCall(TSSetProblemType(ts, TS_NONLINEAR));
1299566063dSJacob Faibussowitsch   PetscCall(TSSetRHSFunction(ts, NULL, RHSFunction, &appctx));
130c4762a1bSJed Brown 
131c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
132c4762a1bSJed Brown      For nonlinear problems, the user can provide a Jacobian evaluation
133c4762a1bSJed Brown      routine (or use a finite differencing approximation).
134c4762a1bSJed Brown 
135c4762a1bSJed Brown      Create matrix data structure; set Jacobian evaluation routine.
136c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
137c4762a1bSJed Brown 
1389566063dSJacob Faibussowitsch   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
1399566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptions((PetscObject)A, options));
1409566063dSJacob Faibussowitsch   PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, appctx.m, appctx.m));
1419566063dSJacob Faibussowitsch   PetscCall(MatSetFromOptions(A));
1429566063dSJacob Faibussowitsch   PetscCall(MatSetUp(A));
1439566063dSJacob Faibussowitsch   PetscCall(TSSetRHSJacobian(ts, A, A, RHSJacobian, &appctx));
144c4762a1bSJed Brown 
145c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
146c4762a1bSJed Brown      Set solution vector and initial timestep
147c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
148c4762a1bSJed Brown 
149c4762a1bSJed Brown   dt = appctx.h / 2.0;
1509566063dSJacob Faibussowitsch   PetscCall(TSSetTimeStep(ts, dt));
151c4762a1bSJed Brown 
152c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
153c4762a1bSJed Brown      Customize timestepping solver:
154c4762a1bSJed Brown        - Set the solution method to be the Backward Euler method.
155c4762a1bSJed Brown        - Set timestepping duration info
156c4762a1bSJed Brown      Then set runtime options, which can override these defaults.
157c4762a1bSJed Brown      For example,
158c4762a1bSJed Brown           -ts_max_steps <maxsteps> -ts_max_time <maxtime>
159c4762a1bSJed Brown      to override the defaults set by TSSetMaxSteps()/TSSetMaxTime().
160c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
161c4762a1bSJed Brown 
1629566063dSJacob Faibussowitsch   PetscCall(TSSetType(ts, TSBEULER));
1639566063dSJacob Faibussowitsch   PetscCall(TSSetMaxSteps(ts, time_steps_max));
1649566063dSJacob Faibussowitsch   PetscCall(TSSetMaxTime(ts, time_total_max));
1659566063dSJacob Faibussowitsch   PetscCall(TSSetExactFinalTime(ts, TS_EXACTFINALTIME_STEPOVER));
1669566063dSJacob Faibussowitsch   PetscCall(TSSetFromOptions(ts));
167c4762a1bSJed Brown 
168c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
169c4762a1bSJed Brown      Solve the problem
170c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
171c4762a1bSJed Brown 
172c4762a1bSJed Brown   /*
173c4762a1bSJed Brown      Evaluate initial conditions
174c4762a1bSJed Brown   */
1759566063dSJacob Faibussowitsch   PetscCall(InitialConditions(u, &appctx));
176c4762a1bSJed Brown 
177c4762a1bSJed Brown   /*
178c4762a1bSJed Brown      Run the timestepping solver
179c4762a1bSJed Brown   */
1809566063dSJacob Faibussowitsch   PetscCall(TSSolve(ts, u));
181c4762a1bSJed Brown 
182c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
183c4762a1bSJed Brown      Free work space.  All PETSc objects should be destroyed when they
184c4762a1bSJed Brown      are no longer needed.
185c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
186c4762a1bSJed Brown 
1879566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetOptions((PetscObject)ts, &optionscopy));
1883c633725SBarry Smith   PetscCheck(options == optionscopy, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "PetscObjectGetOptions() failed");
189c4762a1bSJed Brown 
1909566063dSJacob Faibussowitsch   PetscCall(TSDestroy(&ts));
1919566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&u));
1929566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&A));
1939566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&appctx.da));
1949566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&appctx.localwork));
1959566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&appctx.solution));
1969566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&appctx.u_local));
1979566063dSJacob Faibussowitsch   PetscCall(PetscOptionsDestroy(&options));
198c4762a1bSJed Brown 
199c4762a1bSJed Brown   /*
200c4762a1bSJed Brown      Always call PetscFinalize() before exiting a program.  This routine
201c4762a1bSJed Brown        - finalizes the PETSc libraries as well as MPI
202c4762a1bSJed Brown        - provides summary and diagnostic information if certain runtime
203c4762a1bSJed Brown          options are chosen (e.g., -log_view).
204c4762a1bSJed Brown   */
2059566063dSJacob Faibussowitsch   PetscCall(PetscFinalize());
206b122ec5aSJacob Faibussowitsch   return 0;
207c4762a1bSJed Brown }
208c4762a1bSJed Brown /* --------------------------------------------------------------------- */
209c4762a1bSJed Brown /*
210c4762a1bSJed Brown    InitialConditions - Computes the solution at the initial time.
211c4762a1bSJed Brown 
212c4762a1bSJed Brown    Input Parameters:
213c4762a1bSJed Brown    u - uninitialized solution vector (global)
214c4762a1bSJed Brown    appctx - user-defined application context
215c4762a1bSJed Brown 
216c4762a1bSJed Brown    Output Parameter:
217c4762a1bSJed Brown    u - vector with solution at initial time (global)
218c4762a1bSJed Brown */
219d71ae5a4SJacob Faibussowitsch PetscErrorCode InitialConditions(Vec u, AppCtx *appctx)
220d71ae5a4SJacob Faibussowitsch {
221c4762a1bSJed Brown   PetscScalar *u_localptr, h = appctx->h, x;
222c4762a1bSJed Brown   PetscInt     i, mybase, myend;
223c4762a1bSJed Brown 
224*3ba16761SJacob Faibussowitsch   PetscFunctionBeginUser;
225c4762a1bSJed Brown   /*
226c4762a1bSJed Brown      Determine starting point of each processor's range of
227c4762a1bSJed Brown      grid values.
228c4762a1bSJed Brown   */
2299566063dSJacob Faibussowitsch   PetscCall(VecGetOwnershipRange(u, &mybase, &myend));
230c4762a1bSJed Brown 
231c4762a1bSJed Brown   /*
232c4762a1bSJed Brown     Get a pointer to vector data.
233c4762a1bSJed Brown     - For default PETSc vectors, VecGetArray() returns a pointer to
234c4762a1bSJed Brown       the data array.  Otherwise, the routine is implementation dependent.
235c4762a1bSJed Brown     - You MUST call VecRestoreArray() when you no longer need access to
236c4762a1bSJed Brown       the array.
237c4762a1bSJed Brown     - Note that the Fortran interface to VecGetArray() differs from the
238c4762a1bSJed Brown       C version.  See the users manual for details.
239c4762a1bSJed Brown   */
2409566063dSJacob Faibussowitsch   PetscCall(VecGetArray(u, &u_localptr));
241c4762a1bSJed Brown 
242c4762a1bSJed Brown   /*
243c4762a1bSJed Brown      We initialize the solution array by simply writing the solution
244c4762a1bSJed Brown      directly into the array locations.  Alternatively, we could use
245c4762a1bSJed Brown      VecSetValues() or VecSetValuesLocal().
246c4762a1bSJed Brown   */
247c4762a1bSJed Brown   for (i = mybase; i < myend; i++) {
248c4762a1bSJed Brown     x                      = h * (PetscReal)i; /* current location in global grid */
249c4762a1bSJed Brown     u_localptr[i - mybase] = 1.0 + x * x;
250c4762a1bSJed Brown   }
251c4762a1bSJed Brown 
252c4762a1bSJed Brown   /*
253c4762a1bSJed Brown      Restore vector
254c4762a1bSJed Brown   */
2559566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(u, &u_localptr));
256c4762a1bSJed Brown 
257*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
258c4762a1bSJed Brown }
259c4762a1bSJed Brown /* --------------------------------------------------------------------- */
260c4762a1bSJed Brown /*
261c4762a1bSJed Brown    ExactSolution - Computes the exact solution at a given time.
262c4762a1bSJed Brown 
263c4762a1bSJed Brown    Input Parameters:
264c4762a1bSJed Brown    t - current time
265c4762a1bSJed Brown    solution - vector in which exact solution will be computed
266c4762a1bSJed Brown    appctx - user-defined application context
267c4762a1bSJed Brown 
268c4762a1bSJed Brown    Output Parameter:
269c4762a1bSJed Brown    solution - vector with the newly computed exact solution
270c4762a1bSJed Brown */
271d71ae5a4SJacob Faibussowitsch PetscErrorCode ExactSolution(PetscReal t, Vec solution, AppCtx *appctx)
272d71ae5a4SJacob Faibussowitsch {
273c4762a1bSJed Brown   PetscScalar *s_localptr, h = appctx->h, x;
274c4762a1bSJed Brown   PetscInt     i, mybase, myend;
275c4762a1bSJed Brown 
276*3ba16761SJacob Faibussowitsch   PetscFunctionBeginUser;
277c4762a1bSJed Brown   /*
278c4762a1bSJed Brown      Determine starting and ending points of each processor's
279c4762a1bSJed Brown      range of grid values
280c4762a1bSJed Brown   */
2819566063dSJacob Faibussowitsch   PetscCall(VecGetOwnershipRange(solution, &mybase, &myend));
282c4762a1bSJed Brown 
283c4762a1bSJed Brown   /*
284c4762a1bSJed Brown      Get a pointer to vector data.
285c4762a1bSJed Brown   */
2869566063dSJacob Faibussowitsch   PetscCall(VecGetArray(solution, &s_localptr));
287c4762a1bSJed Brown 
288c4762a1bSJed Brown   /*
289c4762a1bSJed Brown      Simply write the solution directly into the array locations.
290c4762a1bSJed Brown      Alternatively, we could use VecSetValues() or VecSetValuesLocal().
291c4762a1bSJed Brown   */
292c4762a1bSJed Brown   for (i = mybase; i < myend; i++) {
293c4762a1bSJed Brown     x                      = h * (PetscReal)i;
294c4762a1bSJed Brown     s_localptr[i - mybase] = (t + 1.0) * (1.0 + x * x);
295c4762a1bSJed Brown   }
296c4762a1bSJed Brown 
297c4762a1bSJed Brown   /*
298c4762a1bSJed Brown      Restore vector
299c4762a1bSJed Brown   */
3009566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(solution, &s_localptr));
301*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
302c4762a1bSJed Brown }
303c4762a1bSJed Brown /* --------------------------------------------------------------------- */
304c4762a1bSJed Brown /*
305c4762a1bSJed Brown    RHSFunction - User-provided routine that evalues the right-hand-side
306c4762a1bSJed Brown    function of the ODE.  This routine is set in the main program by
307c4762a1bSJed Brown    calling TSSetRHSFunction().  We compute:
308c4762a1bSJed Brown           global_out = F(global_in)
309c4762a1bSJed Brown 
310c4762a1bSJed Brown    Input Parameters:
311c4762a1bSJed Brown    ts         - timesteping context
312c4762a1bSJed Brown    t          - current time
313c4762a1bSJed Brown    global_in  - vector containing the current iterate
314c4762a1bSJed Brown    ctx        - (optional) user-provided context for function evaluation.
315c4762a1bSJed Brown                 In this case we use the appctx defined above.
316c4762a1bSJed Brown 
317c4762a1bSJed Brown    Output Parameter:
318c4762a1bSJed Brown    global_out - vector containing the newly evaluated function
319c4762a1bSJed Brown */
320d71ae5a4SJacob Faibussowitsch PetscErrorCode RHSFunction(TS ts, PetscReal t, Vec global_in, Vec global_out, void *ctx)
321d71ae5a4SJacob Faibussowitsch {
322c4762a1bSJed Brown   AppCtx            *appctx    = (AppCtx *)ctx;     /* user-defined application context */
323c4762a1bSJed Brown   DM                 da        = appctx->da;        /* distributed array */
324c4762a1bSJed Brown   Vec                local_in  = appctx->u_local;   /* local ghosted input vector */
325c4762a1bSJed Brown   Vec                localwork = appctx->localwork; /* local ghosted work vector */
326c4762a1bSJed Brown   PetscInt           i, localsize;
327c4762a1bSJed Brown   PetscMPIInt        rank, size;
328c4762a1bSJed Brown   PetscScalar       *copyptr, sc;
329c4762a1bSJed Brown   const PetscScalar *localptr;
330c4762a1bSJed Brown 
331*3ba16761SJacob Faibussowitsch   PetscFunctionBeginUser;
332c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
333c4762a1bSJed Brown      Get ready for local function computations
334c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
335c4762a1bSJed Brown   /*
336c4762a1bSJed Brown      Scatter ghost points to local vector, using the 2-step process
337c4762a1bSJed Brown         DMGlobalToLocalBegin(), DMGlobalToLocalEnd().
338c4762a1bSJed Brown      By placing code between these two statements, computations can be
339c4762a1bSJed Brown      done while messages are in transition.
340c4762a1bSJed Brown   */
3419566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(da, global_in, INSERT_VALUES, local_in));
3429566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(da, global_in, INSERT_VALUES, local_in));
343c4762a1bSJed Brown 
344c4762a1bSJed Brown   /*
345c4762a1bSJed Brown       Access directly the values in our local INPUT work array
346c4762a1bSJed Brown   */
3479566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(local_in, &localptr));
348c4762a1bSJed Brown 
349c4762a1bSJed Brown   /*
350c4762a1bSJed Brown       Access directly the values in our local OUTPUT work array
351c4762a1bSJed Brown   */
3529566063dSJacob Faibussowitsch   PetscCall(VecGetArray(localwork, &copyptr));
353c4762a1bSJed Brown 
354c4762a1bSJed Brown   sc = 1.0 / (appctx->h * appctx->h * 2.0 * (1.0 + t) * (1.0 + t));
355c4762a1bSJed Brown 
356c4762a1bSJed Brown   /*
357c4762a1bSJed Brown       Evaluate our function on the nodes owned by this processor
358c4762a1bSJed Brown   */
3599566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(local_in, &localsize));
360c4762a1bSJed Brown 
361c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
362c4762a1bSJed Brown      Compute entries for the locally owned part
363c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
364c4762a1bSJed Brown 
365c4762a1bSJed Brown   /*
366c4762a1bSJed Brown      Handle boundary conditions: This is done by using the boundary condition
367c4762a1bSJed Brown         u(t,boundary) = g(t,boundary)
368c4762a1bSJed Brown      for some function g. Now take the derivative with respect to t to obtain
369c4762a1bSJed Brown         u_{t}(t,boundary) = g_{t}(t,boundary)
370c4762a1bSJed Brown 
371c4762a1bSJed Brown      In our case, u(t,0) = t + 1, so that u_{t}(t,0) = 1
372c4762a1bSJed Brown              and  u(t,1) = 2t+ 2, so that u_{t}(t,1) = 2
373c4762a1bSJed Brown   */
3749566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(appctx->comm, &rank));
3759566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(appctx->comm, &size));
376dd400576SPatrick Sanan   if (rank == 0) copyptr[0] = 1.0;
377c4762a1bSJed Brown   if (rank == size - 1) copyptr[localsize - 1] = 2.0;
378c4762a1bSJed Brown 
379c4762a1bSJed Brown   /*
380c4762a1bSJed Brown      Handle the interior nodes where the PDE is replace by finite
381c4762a1bSJed Brown      difference operators.
382c4762a1bSJed Brown   */
383c4762a1bSJed Brown   for (i = 1; i < localsize - 1; i++) copyptr[i] = localptr[i] * sc * (localptr[i + 1] + localptr[i - 1] - 2.0 * localptr[i]);
384c4762a1bSJed Brown 
385c4762a1bSJed Brown   /*
386c4762a1bSJed Brown      Restore vectors
387c4762a1bSJed Brown   */
3889566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(local_in, &localptr));
3899566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(localwork, &copyptr));
390c4762a1bSJed Brown 
391c4762a1bSJed Brown   /*
392c4762a1bSJed Brown      Insert values from the local OUTPUT vector into the global
393c4762a1bSJed Brown      output vector
394c4762a1bSJed Brown   */
3959566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(da, localwork, INSERT_VALUES, global_out));
3969566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(da, localwork, INSERT_VALUES, global_out));
397c4762a1bSJed Brown 
398*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
399c4762a1bSJed Brown }
400c4762a1bSJed Brown /* --------------------------------------------------------------------- */
401c4762a1bSJed Brown /*
402c4762a1bSJed Brown    RHSJacobian - User-provided routine to compute the Jacobian of
403c4762a1bSJed Brown    the nonlinear right-hand-side function of the ODE.
404c4762a1bSJed Brown 
405c4762a1bSJed Brown    Input Parameters:
406c4762a1bSJed Brown    ts - the TS context
407c4762a1bSJed Brown    t - current time
408c4762a1bSJed Brown    global_in - global input vector
409c4762a1bSJed Brown    dummy - optional user-defined context, as set by TSetRHSJacobian()
410c4762a1bSJed Brown 
411c4762a1bSJed Brown    Output Parameters:
412c4762a1bSJed Brown    AA - Jacobian matrix
413c4762a1bSJed Brown    BB - optionally different preconditioning matrix
414c4762a1bSJed Brown    str - flag indicating matrix structure
415c4762a1bSJed Brown 
416c4762a1bSJed Brown   Notes:
417c4762a1bSJed Brown   RHSJacobian computes entries for the locally owned part of the Jacobian.
418c4762a1bSJed Brown    - Currently, all PETSc parallel matrix formats are partitioned by
419c4762a1bSJed Brown      contiguous chunks of rows across the processors.
420c4762a1bSJed Brown    - Each processor needs to insert only elements that it owns
421c4762a1bSJed Brown      locally (but any non-local elements will be sent to the
422c4762a1bSJed Brown      appropriate processor during matrix assembly).
423c4762a1bSJed Brown    - Always specify global row and columns of matrix entries when
424c4762a1bSJed Brown      using MatSetValues().
425c4762a1bSJed Brown    - Here, we set all entries for a particular row at once.
426c4762a1bSJed Brown    - Note that MatSetValues() uses 0-based row and column numbers
427c4762a1bSJed Brown      in Fortran as well as in C.
428c4762a1bSJed Brown */
429d71ae5a4SJacob Faibussowitsch PetscErrorCode RHSJacobian(TS ts, PetscReal t, Vec global_in, Mat AA, Mat BB, void *ctx)
430d71ae5a4SJacob Faibussowitsch {
431c4762a1bSJed Brown   AppCtx            *appctx   = (AppCtx *)ctx;   /* user-defined application context */
432c4762a1bSJed Brown   Vec                local_in = appctx->u_local; /* local ghosted input vector */
433c4762a1bSJed Brown   DM                 da       = appctx->da;      /* distributed array */
434c4762a1bSJed Brown   PetscScalar        v[3], sc;
435c4762a1bSJed Brown   const PetscScalar *localptr;
436c4762a1bSJed Brown   PetscInt           i, mstart, mend, mstarts, mends, idx[3], is;
437c4762a1bSJed Brown 
438*3ba16761SJacob Faibussowitsch   PetscFunctionBeginUser;
439c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
440c4762a1bSJed Brown      Get ready for local Jacobian computations
441c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
442c4762a1bSJed Brown   /*
443c4762a1bSJed Brown      Scatter ghost points to local vector, using the 2-step process
444c4762a1bSJed Brown         DMGlobalToLocalBegin(), DMGlobalToLocalEnd().
445c4762a1bSJed Brown      By placing code between these two statements, computations can be
446c4762a1bSJed Brown      done while messages are in transition.
447c4762a1bSJed Brown   */
4489566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(da, global_in, INSERT_VALUES, local_in));
4499566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(da, global_in, INSERT_VALUES, local_in));
450c4762a1bSJed Brown 
451c4762a1bSJed Brown   /*
452c4762a1bSJed Brown      Get pointer to vector data
453c4762a1bSJed Brown   */
4549566063dSJacob Faibussowitsch   PetscCall(VecGetArrayRead(local_in, &localptr));
455c4762a1bSJed Brown 
456c4762a1bSJed Brown   /*
457c4762a1bSJed Brown      Get starting and ending locally owned rows of the matrix
458c4762a1bSJed Brown   */
4599566063dSJacob Faibussowitsch   PetscCall(MatGetOwnershipRange(BB, &mstarts, &mends));
4609371c9d4SSatish Balay   mstart = mstarts;
4619371c9d4SSatish Balay   mend   = mends;
462c4762a1bSJed Brown 
463c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
464c4762a1bSJed Brown      Compute entries for the locally owned part of the Jacobian.
465c4762a1bSJed Brown       - Currently, all PETSc parallel matrix formats are partitioned by
466c4762a1bSJed Brown         contiguous chunks of rows across the processors.
467c4762a1bSJed Brown       - Each processor needs to insert only elements that it owns
468c4762a1bSJed Brown         locally (but any non-local elements will be sent to the
469c4762a1bSJed Brown         appropriate processor during matrix assembly).
470c4762a1bSJed Brown       - Here, we set all entries for a particular row at once.
471c4762a1bSJed Brown       - We can set matrix entries either using either
472c4762a1bSJed Brown         MatSetValuesLocal() or MatSetValues().
473c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
474c4762a1bSJed Brown 
475c4762a1bSJed Brown   /*
476c4762a1bSJed Brown      Set matrix rows corresponding to boundary data
477c4762a1bSJed Brown   */
478c4762a1bSJed Brown   if (mstart == 0) {
479c4762a1bSJed Brown     v[0] = 0.0;
4809566063dSJacob Faibussowitsch     PetscCall(MatSetValues(BB, 1, &mstart, 1, &mstart, v, INSERT_VALUES));
481c4762a1bSJed Brown     mstart++;
482c4762a1bSJed Brown   }
483c4762a1bSJed Brown   if (mend == appctx->m) {
484c4762a1bSJed Brown     mend--;
485c4762a1bSJed Brown     v[0] = 0.0;
4869566063dSJacob Faibussowitsch     PetscCall(MatSetValues(BB, 1, &mend, 1, &mend, v, INSERT_VALUES));
487c4762a1bSJed Brown   }
488c4762a1bSJed Brown 
489c4762a1bSJed Brown   /*
490c4762a1bSJed Brown      Set matrix rows corresponding to interior data.  We construct the
491c4762a1bSJed Brown      matrix one row at a time.
492c4762a1bSJed Brown   */
493c4762a1bSJed Brown   sc = 1.0 / (appctx->h * appctx->h * 2.0 * (1.0 + t) * (1.0 + t));
494c4762a1bSJed Brown   for (i = mstart; i < mend; i++) {
4959371c9d4SSatish Balay     idx[0] = i - 1;
4969371c9d4SSatish Balay     idx[1] = i;
4979371c9d4SSatish Balay     idx[2] = i + 1;
498c4762a1bSJed Brown     is     = i - mstart + 1;
499c4762a1bSJed Brown     v[0]   = sc * localptr[is];
500c4762a1bSJed Brown     v[1]   = sc * (localptr[is + 1] + localptr[is - 1] - 4.0 * localptr[is]);
501c4762a1bSJed Brown     v[2]   = sc * localptr[is];
5029566063dSJacob Faibussowitsch     PetscCall(MatSetValues(BB, 1, &i, 3, idx, v, INSERT_VALUES));
503c4762a1bSJed Brown   }
504c4762a1bSJed Brown 
505c4762a1bSJed Brown   /*
506c4762a1bSJed Brown      Restore vector
507c4762a1bSJed Brown   */
5089566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayRead(local_in, &localptr));
509c4762a1bSJed Brown 
510c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
511c4762a1bSJed Brown      Complete the matrix assembly process and set some options
512c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
513c4762a1bSJed Brown   /*
514c4762a1bSJed Brown      Assemble matrix, using the 2-step process:
515c4762a1bSJed Brown        MatAssemblyBegin(), MatAssemblyEnd()
516c4762a1bSJed Brown      Computations can be done while messages are in transition
517c4762a1bSJed Brown      by placing code between these two statements.
518c4762a1bSJed Brown   */
5199566063dSJacob Faibussowitsch   PetscCall(MatAssemblyBegin(BB, MAT_FINAL_ASSEMBLY));
5209566063dSJacob Faibussowitsch   PetscCall(MatAssemblyEnd(BB, MAT_FINAL_ASSEMBLY));
521c4762a1bSJed Brown   if (BB != AA) {
5229566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(AA, MAT_FINAL_ASSEMBLY));
5239566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(AA, MAT_FINAL_ASSEMBLY));
524c4762a1bSJed Brown   }
525c4762a1bSJed Brown 
526c4762a1bSJed Brown   /*
527c4762a1bSJed Brown      Set and option to indicate that we will never add a new nonzero location
528c4762a1bSJed Brown      to the matrix. If we do, it will generate an error.
529c4762a1bSJed Brown   */
5309566063dSJacob Faibussowitsch   PetscCall(MatSetOption(BB, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
531c4762a1bSJed Brown 
532*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
533c4762a1bSJed Brown }
534c4762a1bSJed Brown 
535c4762a1bSJed Brown /*TEST
536c4762a1bSJed Brown 
537c4762a1bSJed Brown     test:
538c4762a1bSJed Brown       requires: !single
539c4762a1bSJed Brown 
540c4762a1bSJed Brown TEST*/
541