xref: /petsc/src/ts/tutorials/ex50.c (revision 48a46eb9bd028bec07ec0f396b1a3abb43f14558)
1c4762a1bSJed Brown 
2c4762a1bSJed Brown static char help[] = "Solves one dimensional Burger's equation compares with exact solution\n\n";
3c4762a1bSJed Brown 
4c4762a1bSJed Brown /*
5c4762a1bSJed Brown 
6c4762a1bSJed Brown     Not yet tested in parallel
7c4762a1bSJed Brown 
8c4762a1bSJed Brown */
9c4762a1bSJed Brown 
10c4762a1bSJed Brown /* ------------------------------------------------------------------------
11c4762a1bSJed Brown 
12c4762a1bSJed Brown    This program uses the one-dimensional Burger's equation
13c4762a1bSJed Brown        u_t = mu*u_xx - u u_x,
14c4762a1bSJed Brown    on the domain 0 <= x <= 1, with periodic boundary conditions
15c4762a1bSJed Brown 
16c4762a1bSJed Brown    The operators are discretized with the spectral element method
17c4762a1bSJed Brown 
18c4762a1bSJed Brown    See the paper PDE-CONSTRAINED OPTIMIZATION WITH SPECTRAL ELEMENTS USING PETSC AND TAO
19c4762a1bSJed Brown    by OANA MARIN, EMIL CONSTANTINESCU, AND BARRY SMITH for details on the exact solution
20c4762a1bSJed Brown    used
21c4762a1bSJed Brown 
22c4762a1bSJed Brown    See src/tao/unconstrained/tutorials/burgers_spectral.c
23c4762a1bSJed Brown 
24c4762a1bSJed Brown   ------------------------------------------------------------------------- */
25c4762a1bSJed Brown 
26c4762a1bSJed Brown #include <petscts.h>
27c4762a1bSJed Brown #include <petscdt.h>
28c4762a1bSJed Brown #include <petscdraw.h>
29c4762a1bSJed Brown #include <petscdmda.h>
30c4762a1bSJed Brown 
31c4762a1bSJed Brown /*
32c4762a1bSJed Brown    User-defined application context - contains data needed by the
33c4762a1bSJed Brown    application-provided call-back routines.
34c4762a1bSJed Brown */
35c4762a1bSJed Brown 
36c4762a1bSJed Brown typedef struct {
37c4762a1bSJed Brown   PetscInt   n;       /* number of nodes */
38c4762a1bSJed Brown   PetscReal *nodes;   /* GLL nodes */
39c4762a1bSJed Brown   PetscReal *weights; /* GLL weights */
40c4762a1bSJed Brown } PetscGLL;
41c4762a1bSJed Brown 
42c4762a1bSJed Brown typedef struct {
43c4762a1bSJed Brown   PetscInt  N;               /* grid points per elements*/
44c4762a1bSJed Brown   PetscInt  E;               /* number of elements */
45c4762a1bSJed Brown   PetscReal tol_L2, tol_max; /* error norms */
46c4762a1bSJed Brown   PetscInt  steps;           /* number of timesteps */
47c4762a1bSJed Brown   PetscReal Tend;            /* endtime */
48c4762a1bSJed Brown   PetscReal mu;              /* viscosity */
49c4762a1bSJed Brown   PetscReal L;               /* total length of domain */
50c4762a1bSJed Brown   PetscReal Le;
51c4762a1bSJed Brown   PetscReal Tadj;
52c4762a1bSJed Brown } PetscParam;
53c4762a1bSJed Brown 
54c4762a1bSJed Brown typedef struct {
55c4762a1bSJed Brown   Vec grid; /* total grid */
56c4762a1bSJed Brown   Vec curr_sol;
57c4762a1bSJed Brown } PetscData;
58c4762a1bSJed Brown 
59c4762a1bSJed Brown typedef struct {
60c4762a1bSJed Brown   Vec      grid;  /* total grid */
61c4762a1bSJed Brown   Vec      mass;  /* mass matrix for total integration */
62c4762a1bSJed Brown   Mat      stiff; /* stifness matrix */
63c4762a1bSJed Brown   Mat      keptstiff;
64c4762a1bSJed Brown   Mat      grad;
65c4762a1bSJed Brown   PetscGLL gll;
66c4762a1bSJed Brown } PetscSEMOperators;
67c4762a1bSJed Brown 
68c4762a1bSJed Brown typedef struct {
69c4762a1bSJed Brown   DM                da; /* distributed array data structure */
70c4762a1bSJed Brown   PetscSEMOperators SEMop;
71c4762a1bSJed Brown   PetscParam        param;
72c4762a1bSJed Brown   PetscData         dat;
73c4762a1bSJed Brown   TS                ts;
74c4762a1bSJed Brown   PetscReal         initial_dt;
75c4762a1bSJed Brown } AppCtx;
76c4762a1bSJed Brown 
77c4762a1bSJed Brown /*
78c4762a1bSJed Brown    User-defined routines
79c4762a1bSJed Brown */
80c4762a1bSJed Brown extern PetscErrorCode RHSMatrixLaplaciangllDM(TS, PetscReal, Vec, Mat, Mat, void *);
81c4762a1bSJed Brown extern PetscErrorCode RHSMatrixAdvectiongllDM(TS, PetscReal, Vec, Mat, Mat, void *);
82c4762a1bSJed Brown extern PetscErrorCode TrueSolution(TS, PetscReal, Vec, AppCtx *);
83c4762a1bSJed Brown extern PetscErrorCode RHSFunction(TS, PetscReal, Vec, Vec, void *);
84c4762a1bSJed Brown extern PetscErrorCode RHSJacobian(TS, PetscReal, Vec, Mat, Mat, void *);
85c4762a1bSJed Brown 
869371c9d4SSatish Balay int main(int argc, char **argv) {
87c4762a1bSJed Brown   AppCtx       appctx; /* user-defined application context */
88c4762a1bSJed Brown   PetscInt     i, xs, xm, ind, j, lenglob;
89c4762a1bSJed Brown   PetscReal    x, *wrk_ptr1, *wrk_ptr2;
90c4762a1bSJed Brown   MatNullSpace nsp;
91c4762a1bSJed Brown   PetscMPIInt  size;
92c4762a1bSJed Brown 
93c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
94c4762a1bSJed Brown      Initialize program and set problem parameters
95c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
967510d9b0SBarry Smith   PetscFunctionBeginUser;
97c4762a1bSJed Brown 
98327415f7SBarry Smith   PetscFunctionBeginUser;
999566063dSJacob Faibussowitsch   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
100c4762a1bSJed Brown 
101c4762a1bSJed Brown   /*initialize parameters */
102c4762a1bSJed Brown   appctx.param.N     = 10;   /* order of the spectral element */
103c4762a1bSJed Brown   appctx.param.E     = 10;   /* number of elements */
104c4762a1bSJed Brown   appctx.param.L     = 4.0;  /* length of the domain */
105c4762a1bSJed Brown   appctx.param.mu    = 0.01; /* diffusion coefficient */
106c4762a1bSJed Brown   appctx.initial_dt  = 5e-3;
107c4762a1bSJed Brown   appctx.param.steps = PETSC_MAX_INT;
108c4762a1bSJed Brown   appctx.param.Tend  = 4;
109c4762a1bSJed Brown 
1109566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, NULL, "-N", &appctx.param.N, NULL));
1119566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetInt(NULL, NULL, "-E", &appctx.param.E, NULL));
1129566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetReal(NULL, NULL, "-Tend", &appctx.param.Tend, NULL));
1139566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetReal(NULL, NULL, "-mu", &appctx.param.mu, NULL));
114c4762a1bSJed Brown   appctx.param.Le = appctx.param.L / appctx.param.E;
115c4762a1bSJed Brown 
1169566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
1173c633725SBarry Smith   PetscCheck((appctx.param.E % size) == 0, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "Number of elements must be divisible by number of processes");
118c4762a1bSJed Brown 
119c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
120c4762a1bSJed Brown      Create GLL data structures
121c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1229566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(appctx.param.N, &appctx.SEMop.gll.nodes, appctx.param.N, &appctx.SEMop.gll.weights));
1239566063dSJacob Faibussowitsch   PetscCall(PetscDTGaussLobattoLegendreQuadrature(appctx.param.N, PETSCGAUSSLOBATTOLEGENDRE_VIA_LINEAR_ALGEBRA, appctx.SEMop.gll.nodes, appctx.SEMop.gll.weights));
124c4762a1bSJed Brown   appctx.SEMop.gll.n = appctx.param.N;
125c4762a1bSJed Brown   lenglob            = appctx.param.E * (appctx.param.N - 1);
126c4762a1bSJed Brown 
127c4762a1bSJed Brown   /*
128c4762a1bSJed Brown      Create distributed array (DMDA) to manage parallel grid and vectors
129c4762a1bSJed Brown      and to set up the ghost point communication pattern.  There are E*(Nl-1)+1
130c4762a1bSJed Brown      total grid values spread equally among all the processors, except first and last
131c4762a1bSJed Brown   */
132c4762a1bSJed Brown 
1339566063dSJacob Faibussowitsch   PetscCall(DMDACreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_PERIODIC, lenglob, 1, 1, NULL, &appctx.da));
1349566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions(appctx.da));
1359566063dSJacob Faibussowitsch   PetscCall(DMSetUp(appctx.da));
136c4762a1bSJed Brown 
137c4762a1bSJed Brown   /*
138c4762a1bSJed Brown      Extract global and local vectors from DMDA; we use these to store the
139c4762a1bSJed Brown      approximate solution.  Then duplicate these for remaining vectors that
140c4762a1bSJed Brown      have the same types.
141c4762a1bSJed Brown   */
142c4762a1bSJed Brown 
1439566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector(appctx.da, &appctx.dat.curr_sol));
1449566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(appctx.dat.curr_sol, &appctx.SEMop.grid));
1459566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(appctx.dat.curr_sol, &appctx.SEMop.mass));
146c4762a1bSJed Brown 
1479566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(appctx.da, &xs, NULL, NULL, &xm, NULL, NULL));
1489566063dSJacob Faibussowitsch   PetscCall(DMDAVecGetArray(appctx.da, appctx.SEMop.grid, &wrk_ptr1));
1499566063dSJacob Faibussowitsch   PetscCall(DMDAVecGetArray(appctx.da, appctx.SEMop.mass, &wrk_ptr2));
150c4762a1bSJed Brown 
151c4762a1bSJed Brown   /* Compute function over the locally owned part of the grid */
152c4762a1bSJed Brown 
153c4762a1bSJed Brown   xs = xs / (appctx.param.N - 1);
154c4762a1bSJed Brown   xm = xm / (appctx.param.N - 1);
155c4762a1bSJed Brown 
156c4762a1bSJed Brown   /*
157c4762a1bSJed Brown      Build total grid and mass over entire mesh (multi-elemental)
158c4762a1bSJed Brown   */
159c4762a1bSJed Brown 
160c4762a1bSJed Brown   for (i = xs; i < xs + xm; i++) {
161c4762a1bSJed Brown     for (j = 0; j < appctx.param.N - 1; j++) {
162c4762a1bSJed Brown       x             = (appctx.param.Le / 2.0) * (appctx.SEMop.gll.nodes[j] + 1.0) + appctx.param.Le * i;
163c4762a1bSJed Brown       ind           = i * (appctx.param.N - 1) + j;
164c4762a1bSJed Brown       wrk_ptr1[ind] = x;
165c4762a1bSJed Brown       wrk_ptr2[ind] = .5 * appctx.param.Le * appctx.SEMop.gll.weights[j];
166c4762a1bSJed Brown       if (j == 0) wrk_ptr2[ind] += .5 * appctx.param.Le * appctx.SEMop.gll.weights[j];
167c4762a1bSJed Brown     }
168c4762a1bSJed Brown   }
1699566063dSJacob Faibussowitsch   PetscCall(DMDAVecRestoreArray(appctx.da, appctx.SEMop.grid, &wrk_ptr1));
1709566063dSJacob Faibussowitsch   PetscCall(DMDAVecRestoreArray(appctx.da, appctx.SEMop.mass, &wrk_ptr2));
171c4762a1bSJed Brown 
172c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
173c4762a1bSJed Brown    Create matrix data structure; set matrix evaluation routine.
174c4762a1bSJed Brown    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1759566063dSJacob Faibussowitsch   PetscCall(DMSetMatrixPreallocateOnly(appctx.da, PETSC_TRUE));
1769566063dSJacob Faibussowitsch   PetscCall(DMCreateMatrix(appctx.da, &appctx.SEMop.stiff));
1779566063dSJacob Faibussowitsch   PetscCall(DMCreateMatrix(appctx.da, &appctx.SEMop.grad));
178c4762a1bSJed Brown   /*
179c4762a1bSJed Brown    For linear problems with a time-dependent f(u,t) in the equation
180c4762a1bSJed Brown    u_t = f(u,t), the user provides the discretized right-hand-side
181c4762a1bSJed Brown    as a time-dependent matrix.
182c4762a1bSJed Brown    */
1839566063dSJacob Faibussowitsch   PetscCall(RHSMatrixLaplaciangllDM(appctx.ts, 0.0, appctx.dat.curr_sol, appctx.SEMop.stiff, appctx.SEMop.stiff, &appctx));
1849566063dSJacob Faibussowitsch   PetscCall(RHSMatrixAdvectiongllDM(appctx.ts, 0.0, appctx.dat.curr_sol, appctx.SEMop.grad, appctx.SEMop.grad, &appctx));
185c4762a1bSJed Brown   /*
186c4762a1bSJed Brown        For linear problems with a time-dependent f(u,t) in the equation
187c4762a1bSJed Brown        u_t = f(u,t), the user provides the discretized right-hand-side
188c4762a1bSJed Brown        as a time-dependent matrix.
189c4762a1bSJed Brown     */
190c4762a1bSJed Brown 
1919566063dSJacob Faibussowitsch   PetscCall(MatDuplicate(appctx.SEMop.stiff, MAT_COPY_VALUES, &appctx.SEMop.keptstiff));
192c4762a1bSJed Brown 
193c4762a1bSJed Brown   /* attach the null space to the matrix, this probably is not needed but does no harm */
1949566063dSJacob Faibussowitsch   PetscCall(MatNullSpaceCreate(PETSC_COMM_WORLD, PETSC_TRUE, 0, NULL, &nsp));
1959566063dSJacob Faibussowitsch   PetscCall(MatSetNullSpace(appctx.SEMop.stiff, nsp));
1969566063dSJacob Faibussowitsch   PetscCall(MatSetNullSpace(appctx.SEMop.keptstiff, nsp));
1979566063dSJacob Faibussowitsch   PetscCall(MatNullSpaceTest(nsp, appctx.SEMop.stiff, NULL));
1989566063dSJacob Faibussowitsch   PetscCall(MatNullSpaceDestroy(&nsp));
199c4762a1bSJed Brown   /* attach the null space to the matrix, this probably is not needed but does no harm */
2009566063dSJacob Faibussowitsch   PetscCall(MatNullSpaceCreate(PETSC_COMM_WORLD, PETSC_TRUE, 0, NULL, &nsp));
2019566063dSJacob Faibussowitsch   PetscCall(MatSetNullSpace(appctx.SEMop.grad, nsp));
2029566063dSJacob Faibussowitsch   PetscCall(MatNullSpaceTest(nsp, appctx.SEMop.grad, NULL));
2039566063dSJacob Faibussowitsch   PetscCall(MatNullSpaceDestroy(&nsp));
204c4762a1bSJed Brown 
205c4762a1bSJed Brown   /* Create the TS solver that solves the ODE and its adjoint; set its options */
2069566063dSJacob Faibussowitsch   PetscCall(TSCreate(PETSC_COMM_WORLD, &appctx.ts));
2079566063dSJacob Faibussowitsch   PetscCall(TSSetProblemType(appctx.ts, TS_NONLINEAR));
2089566063dSJacob Faibussowitsch   PetscCall(TSSetType(appctx.ts, TSRK));
2099566063dSJacob Faibussowitsch   PetscCall(TSSetDM(appctx.ts, appctx.da));
2109566063dSJacob Faibussowitsch   PetscCall(TSSetTime(appctx.ts, 0.0));
2119566063dSJacob Faibussowitsch   PetscCall(TSSetTimeStep(appctx.ts, appctx.initial_dt));
2129566063dSJacob Faibussowitsch   PetscCall(TSSetMaxSteps(appctx.ts, appctx.param.steps));
2139566063dSJacob Faibussowitsch   PetscCall(TSSetMaxTime(appctx.ts, appctx.param.Tend));
2149566063dSJacob Faibussowitsch   PetscCall(TSSetExactFinalTime(appctx.ts, TS_EXACTFINALTIME_MATCHSTEP));
2159566063dSJacob Faibussowitsch   PetscCall(TSSetTolerances(appctx.ts, 1e-7, NULL, 1e-7, NULL));
2169566063dSJacob Faibussowitsch   PetscCall(TSSetSaveTrajectory(appctx.ts));
2179566063dSJacob Faibussowitsch   PetscCall(TSSetFromOptions(appctx.ts));
2189566063dSJacob Faibussowitsch   PetscCall(TSSetRHSFunction(appctx.ts, NULL, RHSFunction, &appctx));
2199566063dSJacob Faibussowitsch   PetscCall(TSSetRHSJacobian(appctx.ts, appctx.SEMop.stiff, appctx.SEMop.stiff, RHSJacobian, &appctx));
220c4762a1bSJed Brown 
221c4762a1bSJed Brown   /* Set Initial conditions for the problem  */
2229566063dSJacob Faibussowitsch   PetscCall(TrueSolution(appctx.ts, 0, appctx.dat.curr_sol, &appctx));
223c4762a1bSJed Brown 
2249566063dSJacob Faibussowitsch   PetscCall(TSSetSolutionFunction(appctx.ts, (PetscErrorCode(*)(TS, PetscReal, Vec, void *))TrueSolution, &appctx));
2259566063dSJacob Faibussowitsch   PetscCall(TSSetTime(appctx.ts, 0.0));
2269566063dSJacob Faibussowitsch   PetscCall(TSSetStepNumber(appctx.ts, 0));
227c4762a1bSJed Brown 
2289566063dSJacob Faibussowitsch   PetscCall(TSSolve(appctx.ts, appctx.dat.curr_sol));
229c4762a1bSJed Brown 
2309566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&appctx.SEMop.stiff));
2319566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&appctx.SEMop.keptstiff));
2329566063dSJacob Faibussowitsch   PetscCall(MatDestroy(&appctx.SEMop.grad));
2339566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&appctx.SEMop.grid));
2349566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&appctx.SEMop.mass));
2359566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&appctx.dat.curr_sol));
2369566063dSJacob Faibussowitsch   PetscCall(PetscFree2(appctx.SEMop.gll.nodes, appctx.SEMop.gll.weights));
2379566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&appctx.da));
2389566063dSJacob Faibussowitsch   PetscCall(TSDestroy(&appctx.ts));
239c4762a1bSJed Brown 
240c4762a1bSJed Brown   /*
241c4762a1bSJed Brown      Always call PetscFinalize() before exiting a program.  This routine
242c4762a1bSJed Brown        - finalizes the PETSc libraries as well as MPI
243c4762a1bSJed Brown        - provides summary and diagnostic information if certain runtime
244c4762a1bSJed Brown          options are chosen (e.g., -log_summary).
245c4762a1bSJed Brown   */
2469566063dSJacob Faibussowitsch   PetscCall(PetscFinalize());
247b122ec5aSJacob Faibussowitsch   return 0;
248c4762a1bSJed Brown }
249c4762a1bSJed Brown 
250c4762a1bSJed Brown /*
251c4762a1bSJed Brown    TrueSolution() computes the true solution for the PDE
252c4762a1bSJed Brown 
253c4762a1bSJed Brown    Input Parameter:
254c4762a1bSJed Brown    u - uninitialized solution vector (global)
255c4762a1bSJed Brown    appctx - user-defined application context
256c4762a1bSJed Brown 
257c4762a1bSJed Brown    Output Parameter:
258c4762a1bSJed Brown    u - vector with solution at initial time (global)
259c4762a1bSJed Brown */
2609371c9d4SSatish Balay PetscErrorCode TrueSolution(TS ts, PetscReal t, Vec u, AppCtx *appctx) {
261c4762a1bSJed Brown   PetscScalar       *s;
262c4762a1bSJed Brown   const PetscScalar *xg;
263c4762a1bSJed Brown   PetscInt           i, xs, xn;
264c4762a1bSJed Brown 
2659566063dSJacob Faibussowitsch   PetscCall(DMDAVecGetArray(appctx->da, u, &s));
2669566063dSJacob Faibussowitsch   PetscCall(DMDAVecGetArrayRead(appctx->da, appctx->SEMop.grid, (void *)&xg));
2679566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(appctx->da, &xs, NULL, NULL, &xn, NULL, NULL));
268c4762a1bSJed Brown   for (i = xs; i < xs + xn; i++) {
269c4762a1bSJed Brown     s[i] = 2.0 * appctx->param.mu * PETSC_PI * PetscSinScalar(PETSC_PI * xg[i]) * PetscExpReal(-appctx->param.mu * PETSC_PI * PETSC_PI * t) / (2.0 + PetscCosScalar(PETSC_PI * xg[i]) * PetscExpReal(-appctx->param.mu * PETSC_PI * PETSC_PI * t));
270c4762a1bSJed Brown   }
2719566063dSJacob Faibussowitsch   PetscCall(DMDAVecRestoreArray(appctx->da, u, &s));
2729566063dSJacob Faibussowitsch   PetscCall(DMDAVecRestoreArrayRead(appctx->da, appctx->SEMop.grid, (void *)&xg));
273c4762a1bSJed Brown   return 0;
274c4762a1bSJed Brown }
275c4762a1bSJed Brown 
2769371c9d4SSatish Balay PetscErrorCode RHSFunction(TS ts, PetscReal t, Vec globalin, Vec globalout, void *ctx) {
277c4762a1bSJed Brown   AppCtx *appctx = (AppCtx *)ctx;
278c4762a1bSJed Brown 
2797510d9b0SBarry Smith   PetscFunctionBeginUser;
2809566063dSJacob Faibussowitsch   PetscCall(MatMult(appctx->SEMop.grad, globalin, globalout)); /* grad u */
2819566063dSJacob Faibussowitsch   PetscCall(VecPointwiseMult(globalout, globalin, globalout)); /* u grad u */
2829566063dSJacob Faibussowitsch   PetscCall(VecScale(globalout, -1.0));
2839566063dSJacob Faibussowitsch   PetscCall(MatMultAdd(appctx->SEMop.keptstiff, globalin, globalout, globalout));
284c4762a1bSJed Brown   PetscFunctionReturn(0);
285c4762a1bSJed Brown }
286c4762a1bSJed Brown 
287c4762a1bSJed Brown /*
288c4762a1bSJed Brown 
289c4762a1bSJed Brown       K is the discretiziation of the Laplacian
290c4762a1bSJed Brown       G is the discretization of the gradient
291c4762a1bSJed Brown 
292c4762a1bSJed Brown       Computes Jacobian of      K u + diag(u) G u   which is given by
293c4762a1bSJed Brown               K   + diag(u)G + diag(Gu)
294c4762a1bSJed Brown */
2959371c9d4SSatish Balay PetscErrorCode RHSJacobian(TS ts, PetscReal t, Vec globalin, Mat A, Mat B, void *ctx) {
296c4762a1bSJed Brown   AppCtx *appctx = (AppCtx *)ctx;
297c4762a1bSJed Brown   Vec     Gglobalin;
298c4762a1bSJed Brown 
2997510d9b0SBarry Smith   PetscFunctionBeginUser;
300c4762a1bSJed Brown   /*    A = diag(u) G */
301c4762a1bSJed Brown 
3029566063dSJacob Faibussowitsch   PetscCall(MatCopy(appctx->SEMop.grad, A, SAME_NONZERO_PATTERN));
3039566063dSJacob Faibussowitsch   PetscCall(MatDiagonalScale(A, globalin, NULL));
304c4762a1bSJed Brown 
305c4762a1bSJed Brown   /*    A  = A + diag(Gu) */
3069566063dSJacob Faibussowitsch   PetscCall(VecDuplicate(globalin, &Gglobalin));
3079566063dSJacob Faibussowitsch   PetscCall(MatMult(appctx->SEMop.grad, globalin, Gglobalin));
3089566063dSJacob Faibussowitsch   PetscCall(MatDiagonalSet(A, Gglobalin, ADD_VALUES));
3099566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&Gglobalin));
310c4762a1bSJed Brown 
311c4762a1bSJed Brown   /*   A  = K - A    */
3129566063dSJacob Faibussowitsch   PetscCall(MatScale(A, -1.0));
3139566063dSJacob Faibussowitsch   PetscCall(MatAXPY(A, 0.0, appctx->SEMop.keptstiff, SAME_NONZERO_PATTERN));
314c4762a1bSJed Brown   PetscFunctionReturn(0);
315c4762a1bSJed Brown }
316c4762a1bSJed Brown 
317c4762a1bSJed Brown /* --------------------------------------------------------------------- */
318c4762a1bSJed Brown 
319c4762a1bSJed Brown #include "petscblaslapack.h"
320c4762a1bSJed Brown /*
321c4762a1bSJed Brown      Matrix free operation of 1d Laplacian and Grad for GLL spectral elements
322c4762a1bSJed Brown */
3239371c9d4SSatish Balay PetscErrorCode MatMult_Laplacian(Mat A, Vec x, Vec y) {
324c4762a1bSJed Brown   AppCtx            *appctx;
325c4762a1bSJed Brown   PetscReal        **temp, vv;
326c4762a1bSJed Brown   PetscInt           i, j, xs, xn;
327c4762a1bSJed Brown   Vec                xlocal, ylocal;
328c4762a1bSJed Brown   const PetscScalar *xl;
329c4762a1bSJed Brown   PetscScalar       *yl;
330c4762a1bSJed Brown   PetscBLASInt       _One  = 1, n;
331c4762a1bSJed Brown   PetscScalar        _DOne = 1;
332c4762a1bSJed Brown 
3339566063dSJacob Faibussowitsch   PetscCall(MatShellGetContext(A, &appctx));
3349566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(appctx->da, &xlocal));
3359566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(appctx->da, x, INSERT_VALUES, xlocal));
3369566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(appctx->da, x, INSERT_VALUES, xlocal));
3379566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(appctx->da, &ylocal));
3389566063dSJacob Faibussowitsch   PetscCall(VecSet(ylocal, 0.0));
3399566063dSJacob Faibussowitsch   PetscCall(PetscGaussLobattoLegendreElementLaplacianCreate(appctx->SEMop.gll.n, appctx->SEMop.gll.nodes, appctx->SEMop.gll.weights, &temp));
340c4762a1bSJed Brown   for (i = 0; i < appctx->param.N; i++) {
341c4762a1bSJed Brown     vv = -appctx->param.mu * 2.0 / appctx->param.Le;
342c4762a1bSJed Brown     for (j = 0; j < appctx->param.N; j++) temp[i][j] = temp[i][j] * vv;
343c4762a1bSJed Brown   }
3449566063dSJacob Faibussowitsch   PetscCall(DMDAVecGetArrayRead(appctx->da, xlocal, (void *)&xl));
3459566063dSJacob Faibussowitsch   PetscCall(DMDAVecGetArray(appctx->da, ylocal, &yl));
3469566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(appctx->da, &xs, NULL, NULL, &xn, NULL, NULL));
3479566063dSJacob Faibussowitsch   PetscCall(PetscBLASIntCast(appctx->param.N, &n));
348*48a46eb9SPierre Jolivet   for (j = xs; j < xs + xn; j += appctx->param.N - 1) PetscCallBLAS("BLASgemv", BLASgemv_("N", &n, &n, &_DOne, &temp[0][0], &n, &xl[j], &_One, &_DOne, &yl[j], &_One));
3499566063dSJacob Faibussowitsch   PetscCall(DMDAVecRestoreArrayRead(appctx->da, xlocal, (void *)&xl));
3509566063dSJacob Faibussowitsch   PetscCall(DMDAVecRestoreArray(appctx->da, ylocal, &yl));
3519566063dSJacob Faibussowitsch   PetscCall(PetscGaussLobattoLegendreElementLaplacianDestroy(appctx->SEMop.gll.n, appctx->SEMop.gll.nodes, appctx->SEMop.gll.weights, &temp));
3529566063dSJacob Faibussowitsch   PetscCall(VecSet(y, 0.0));
3539566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(appctx->da, ylocal, ADD_VALUES, y));
3549566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(appctx->da, ylocal, ADD_VALUES, y));
3559566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(appctx->da, &xlocal));
3569566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(appctx->da, &ylocal));
3579566063dSJacob Faibussowitsch   PetscCall(VecPointwiseDivide(y, y, appctx->SEMop.mass));
358c4762a1bSJed Brown   return 0;
359c4762a1bSJed Brown }
360c4762a1bSJed Brown 
3619371c9d4SSatish Balay PetscErrorCode MatMult_Advection(Mat A, Vec x, Vec y) {
362c4762a1bSJed Brown   AppCtx            *appctx;
363c4762a1bSJed Brown   PetscReal        **temp;
364c4762a1bSJed Brown   PetscInt           j, xs, xn;
365c4762a1bSJed Brown   Vec                xlocal, ylocal;
366c4762a1bSJed Brown   const PetscScalar *xl;
367c4762a1bSJed Brown   PetscScalar       *yl;
368c4762a1bSJed Brown   PetscBLASInt       _One  = 1, n;
369c4762a1bSJed Brown   PetscScalar        _DOne = 1;
370c4762a1bSJed Brown 
3719566063dSJacob Faibussowitsch   PetscCall(MatShellGetContext(A, &appctx));
3729566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(appctx->da, &xlocal));
3739566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalBegin(appctx->da, x, INSERT_VALUES, xlocal));
3749566063dSJacob Faibussowitsch   PetscCall(DMGlobalToLocalEnd(appctx->da, x, INSERT_VALUES, xlocal));
3759566063dSJacob Faibussowitsch   PetscCall(DMGetLocalVector(appctx->da, &ylocal));
3769566063dSJacob Faibussowitsch   PetscCall(VecSet(ylocal, 0.0));
3779566063dSJacob Faibussowitsch   PetscCall(PetscGaussLobattoLegendreElementAdvectionCreate(appctx->SEMop.gll.n, appctx->SEMop.gll.nodes, appctx->SEMop.gll.weights, &temp));
3789566063dSJacob Faibussowitsch   PetscCall(DMDAVecGetArrayRead(appctx->da, xlocal, (void *)&xl));
3799566063dSJacob Faibussowitsch   PetscCall(DMDAVecGetArray(appctx->da, ylocal, &yl));
3809566063dSJacob Faibussowitsch   PetscCall(DMDAGetCorners(appctx->da, &xs, NULL, NULL, &xn, NULL, NULL));
3819566063dSJacob Faibussowitsch   PetscCall(PetscBLASIntCast(appctx->param.N, &n));
382*48a46eb9SPierre Jolivet   for (j = xs; j < xs + xn; j += appctx->param.N - 1) PetscCallBLAS("BLASgemv", BLASgemv_("N", &n, &n, &_DOne, &temp[0][0], &n, &xl[j], &_One, &_DOne, &yl[j], &_One));
3839566063dSJacob Faibussowitsch   PetscCall(DMDAVecRestoreArrayRead(appctx->da, xlocal, (void *)&xl));
3849566063dSJacob Faibussowitsch   PetscCall(DMDAVecRestoreArray(appctx->da, ylocal, &yl));
3859566063dSJacob Faibussowitsch   PetscCall(PetscGaussLobattoLegendreElementAdvectionDestroy(appctx->SEMop.gll.n, appctx->SEMop.gll.nodes, appctx->SEMop.gll.weights, &temp));
3869566063dSJacob Faibussowitsch   PetscCall(VecSet(y, 0.0));
3879566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalBegin(appctx->da, ylocal, ADD_VALUES, y));
3889566063dSJacob Faibussowitsch   PetscCall(DMLocalToGlobalEnd(appctx->da, ylocal, ADD_VALUES, y));
3899566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(appctx->da, &xlocal));
3909566063dSJacob Faibussowitsch   PetscCall(DMRestoreLocalVector(appctx->da, &ylocal));
3919566063dSJacob Faibussowitsch   PetscCall(VecPointwiseDivide(y, y, appctx->SEMop.mass));
3929566063dSJacob Faibussowitsch   PetscCall(VecScale(y, -1.0));
393c4762a1bSJed Brown   return 0;
394c4762a1bSJed Brown }
395c4762a1bSJed Brown 
396c4762a1bSJed Brown /*
397c4762a1bSJed Brown    RHSMatrixLaplacian - User-provided routine to compute the right-hand-side
398c4762a1bSJed Brown    matrix for the Laplacian operator
399c4762a1bSJed Brown 
400c4762a1bSJed Brown    Input Parameters:
401c4762a1bSJed Brown    ts - the TS context
402c4762a1bSJed Brown    t - current time  (ignored)
403c4762a1bSJed Brown    X - current solution (ignored)
404c4762a1bSJed Brown    dummy - optional user-defined context, as set by TSetRHSJacobian()
405c4762a1bSJed Brown 
406c4762a1bSJed Brown    Output Parameters:
407c4762a1bSJed Brown    AA - Jacobian matrix
408c4762a1bSJed Brown    BB - optionally different matrix from which the preconditioner is built
409c4762a1bSJed Brown    str - flag indicating matrix structure
410c4762a1bSJed Brown 
411c4762a1bSJed Brown */
4129371c9d4SSatish Balay PetscErrorCode RHSMatrixLaplaciangllDM(TS ts, PetscReal t, Vec X, Mat A, Mat BB, void *ctx) {
413c4762a1bSJed Brown   PetscReal **temp;
414c4762a1bSJed Brown   PetscReal   vv;
415c4762a1bSJed Brown   AppCtx     *appctx = (AppCtx *)ctx; /* user-defined application context */
416c4762a1bSJed Brown   PetscInt    i, xs, xn, l, j;
417c4762a1bSJed Brown   PetscInt   *rowsDM;
418c4762a1bSJed Brown   PetscBool   flg = PETSC_FALSE;
419c4762a1bSJed Brown 
4209566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(NULL, NULL, "-gll_mf", &flg, NULL));
421c4762a1bSJed Brown 
422c4762a1bSJed Brown   if (!flg) {
423c4762a1bSJed Brown     /*
424c4762a1bSJed Brown      Creates the element stiffness matrix for the given gll
425c4762a1bSJed Brown      */
4269566063dSJacob Faibussowitsch     PetscCall(PetscGaussLobattoLegendreElementLaplacianCreate(appctx->SEMop.gll.n, appctx->SEMop.gll.nodes, appctx->SEMop.gll.weights, &temp));
427a5b23f4aSJose E. Roman     /* workaround for clang analyzer warning: Division by zero */
4283c633725SBarry Smith     PetscCheck(appctx->param.N > 1, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONG, "Spectral element order should be > 1");
429c4762a1bSJed Brown 
430c4762a1bSJed Brown     /* scale by the size of the element */
431c4762a1bSJed Brown     for (i = 0; i < appctx->param.N; i++) {
432c4762a1bSJed Brown       vv = -appctx->param.mu * 2.0 / appctx->param.Le;
433c4762a1bSJed Brown       for (j = 0; j < appctx->param.N; j++) temp[i][j] = temp[i][j] * vv;
434c4762a1bSJed Brown     }
435c4762a1bSJed Brown 
4369566063dSJacob Faibussowitsch     PetscCall(MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
4379566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(appctx->da, &xs, NULL, NULL, &xn, NULL, NULL));
438c4762a1bSJed Brown 
439c4762a1bSJed Brown     xs = xs / (appctx->param.N - 1);
440c4762a1bSJed Brown     xn = xn / (appctx->param.N - 1);
441c4762a1bSJed Brown 
4429566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(appctx->param.N, &rowsDM));
443c4762a1bSJed Brown     /*
444c4762a1bSJed Brown      loop over local elements
445c4762a1bSJed Brown      */
446c4762a1bSJed Brown     for (j = xs; j < xs + xn; j++) {
4479371c9d4SSatish Balay       for (l = 0; l < appctx->param.N; l++) { rowsDM[l] = 1 + (j - xs) * (appctx->param.N - 1) + l; }
4489566063dSJacob Faibussowitsch       PetscCall(MatSetValuesLocal(A, appctx->param.N, rowsDM, appctx->param.N, rowsDM, &temp[0][0], ADD_VALUES));
449c4762a1bSJed Brown     }
4509566063dSJacob Faibussowitsch     PetscCall(PetscFree(rowsDM));
4519566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
4529566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
4539566063dSJacob Faibussowitsch     PetscCall(VecReciprocal(appctx->SEMop.mass));
4549566063dSJacob Faibussowitsch     PetscCall(MatDiagonalScale(A, appctx->SEMop.mass, 0));
4559566063dSJacob Faibussowitsch     PetscCall(VecReciprocal(appctx->SEMop.mass));
456c4762a1bSJed Brown 
4579566063dSJacob Faibussowitsch     PetscCall(PetscGaussLobattoLegendreElementLaplacianDestroy(appctx->SEMop.gll.n, appctx->SEMop.gll.nodes, appctx->SEMop.gll.weights, &temp));
458c4762a1bSJed Brown   } else {
4599566063dSJacob Faibussowitsch     PetscCall(MatSetType(A, MATSHELL));
4609566063dSJacob Faibussowitsch     PetscCall(MatSetUp(A));
4619566063dSJacob Faibussowitsch     PetscCall(MatShellSetContext(A, appctx));
4629566063dSJacob Faibussowitsch     PetscCall(MatShellSetOperation(A, MATOP_MULT, (void (*)(void))MatMult_Laplacian));
463c4762a1bSJed Brown   }
464c4762a1bSJed Brown   return 0;
465c4762a1bSJed Brown }
466c4762a1bSJed Brown 
467c4762a1bSJed Brown /*
468c4762a1bSJed Brown    RHSMatrixAdvection - User-provided routine to compute the right-hand-side
469c4762a1bSJed Brown    matrix for the Advection (gradient) operator.
470c4762a1bSJed Brown 
471c4762a1bSJed Brown    Input Parameters:
472c4762a1bSJed Brown    ts - the TS context
473c4762a1bSJed Brown    t - current time
474c4762a1bSJed Brown    global_in - global input vector
475c4762a1bSJed Brown    dummy - optional user-defined context, as set by TSetRHSJacobian()
476c4762a1bSJed Brown 
477c4762a1bSJed Brown    Output Parameters:
478c4762a1bSJed Brown    AA - Jacobian matrix
479c4762a1bSJed Brown    BB - optionally different preconditioning matrix
480c4762a1bSJed Brown    str - flag indicating matrix structure
481c4762a1bSJed Brown 
482c4762a1bSJed Brown */
4839371c9d4SSatish Balay PetscErrorCode RHSMatrixAdvectiongllDM(TS ts, PetscReal t, Vec X, Mat A, Mat BB, void *ctx) {
484c4762a1bSJed Brown   PetscReal **temp;
485c4762a1bSJed Brown   AppCtx     *appctx = (AppCtx *)ctx; /* user-defined application context */
486c4762a1bSJed Brown   PetscInt    xs, xn, l, j;
487c4762a1bSJed Brown   PetscInt   *rowsDM;
488c4762a1bSJed Brown   PetscBool   flg = PETSC_FALSE;
489c4762a1bSJed Brown 
4909566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(NULL, NULL, "-gll_mf", &flg, NULL));
491c4762a1bSJed Brown 
492c4762a1bSJed Brown   if (!flg) {
493c4762a1bSJed Brown     /*
494c4762a1bSJed Brown      Creates the advection matrix for the given gll
495c4762a1bSJed Brown      */
4969566063dSJacob Faibussowitsch     PetscCall(PetscGaussLobattoLegendreElementAdvectionCreate(appctx->SEMop.gll.n, appctx->SEMop.gll.nodes, appctx->SEMop.gll.weights, &temp));
4979566063dSJacob Faibussowitsch     PetscCall(MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
4989566063dSJacob Faibussowitsch     PetscCall(DMDAGetCorners(appctx->da, &xs, NULL, NULL, &xn, NULL, NULL));
499c4762a1bSJed Brown     xs = xs / (appctx->param.N - 1);
500c4762a1bSJed Brown     xn = xn / (appctx->param.N - 1);
501c4762a1bSJed Brown 
5029566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(appctx->param.N, &rowsDM));
503c4762a1bSJed Brown     for (j = xs; j < xs + xn; j++) {
5049371c9d4SSatish Balay       for (l = 0; l < appctx->param.N; l++) { rowsDM[l] = 1 + (j - xs) * (appctx->param.N - 1) + l; }
5059566063dSJacob Faibussowitsch       PetscCall(MatSetValuesLocal(A, appctx->param.N, rowsDM, appctx->param.N, rowsDM, &temp[0][0], ADD_VALUES));
506c4762a1bSJed Brown     }
5079566063dSJacob Faibussowitsch     PetscCall(PetscFree(rowsDM));
5089566063dSJacob Faibussowitsch     PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
5099566063dSJacob Faibussowitsch     PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
510c4762a1bSJed Brown 
5119566063dSJacob Faibussowitsch     PetscCall(VecReciprocal(appctx->SEMop.mass));
5129566063dSJacob Faibussowitsch     PetscCall(MatDiagonalScale(A, appctx->SEMop.mass, 0));
5139566063dSJacob Faibussowitsch     PetscCall(VecReciprocal(appctx->SEMop.mass));
514c4762a1bSJed Brown 
5159566063dSJacob Faibussowitsch     PetscCall(PetscGaussLobattoLegendreElementAdvectionDestroy(appctx->SEMop.gll.n, appctx->SEMop.gll.nodes, appctx->SEMop.gll.weights, &temp));
516c4762a1bSJed Brown   } else {
5179566063dSJacob Faibussowitsch     PetscCall(MatSetType(A, MATSHELL));
5189566063dSJacob Faibussowitsch     PetscCall(MatSetUp(A));
5199566063dSJacob Faibussowitsch     PetscCall(MatShellSetContext(A, appctx));
5209566063dSJacob Faibussowitsch     PetscCall(MatShellSetOperation(A, MATOP_MULT, (void (*)(void))MatMult_Advection));
521c4762a1bSJed Brown   }
522c4762a1bSJed Brown   return 0;
523c4762a1bSJed Brown }
524c4762a1bSJed Brown 
525c4762a1bSJed Brown /*TEST
526c4762a1bSJed Brown 
527c4762a1bSJed Brown     build:
528c4762a1bSJed Brown       requires: !complex
529c4762a1bSJed Brown 
530c4762a1bSJed Brown     test:
531c4762a1bSJed Brown       suffix: 1
532c4762a1bSJed Brown       requires: !single
533c4762a1bSJed Brown 
534c4762a1bSJed Brown     test:
535c4762a1bSJed Brown       suffix: 2
536c4762a1bSJed Brown       nsize: 5
537c4762a1bSJed Brown       requires: !single
538c4762a1bSJed Brown 
539c4762a1bSJed Brown     test:
540c4762a1bSJed Brown       suffix: 3
541c4762a1bSJed Brown       requires: !single
542c4762a1bSJed Brown       args: -ts_view  -ts_type beuler -gll_mf -pc_type none -ts_max_steps 5 -ts_monitor_error
543c4762a1bSJed Brown 
544c4762a1bSJed Brown     test:
545c4762a1bSJed Brown       suffix: 4
546c4762a1bSJed Brown       requires: !single
547c4762a1bSJed Brown       args: -ts_view  -ts_type beuler  -pc_type none -ts_max_steps 5 -ts_monitor_error
548c4762a1bSJed Brown 
549c4762a1bSJed Brown TEST*/
550