xref: /petsc/src/ksp/ksp/tutorials/ex25.c (revision d2522c19e8fa9bca20aaca277941d9a63e71db6a)
1 
2 /*
3  Partial differential equation
4 
5    d  (1 + e*sine(2*pi*k*x)) d u = 1, 0 < x < 1,
6    --                        ---
7    dx                        dx
8 with boundary conditions
9 
10    u = 0 for x = 0, x = 1
11 
12    This uses multigrid to solve the linear system
13 
14 */
15 
16 static char help[] = "Solves 1D variable coefficient Laplacian using multigrid.\n\n";
17 
18 #include <petscdm.h>
19 #include <petscdmda.h>
20 #include <petscksp.h>
21 
22 static PetscErrorCode ComputeMatrix(KSP, Mat, Mat, void *);
23 static PetscErrorCode ComputeRHS(KSP, Vec, void *);
24 
25 typedef struct {
26   PetscInt    k;
27   PetscScalar e;
28 } AppCtx;
29 
30 int main(int argc, char **argv) {
31   KSP       ksp;
32   DM        da;
33   AppCtx    user;
34   Mat       A;
35   Vec       b, b2;
36   Vec       x;
37   PetscReal nrm;
38 
39   PetscFunctionBeginUser;
40   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
41   user.k = 1;
42   user.e = .99;
43   PetscCall(PetscOptionsGetInt(NULL, 0, "-k", &user.k, 0));
44   PetscCall(PetscOptionsGetScalar(NULL, 0, "-e", &user.e, 0));
45 
46   PetscCall(KSPCreate(PETSC_COMM_WORLD, &ksp));
47   PetscCall(DMDACreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, 128, 1, 1, 0, &da));
48   PetscCall(DMSetFromOptions(da));
49   PetscCall(DMSetUp(da));
50   PetscCall(KSPSetDM(ksp, da));
51   PetscCall(KSPSetComputeRHS(ksp, ComputeRHS, &user));
52   PetscCall(KSPSetComputeOperators(ksp, ComputeMatrix, &user));
53   PetscCall(KSPSetFromOptions(ksp));
54   PetscCall(KSPSolve(ksp, NULL, NULL));
55 
56   PetscCall(KSPGetOperators(ksp, &A, NULL));
57   PetscCall(KSPGetSolution(ksp, &x));
58   PetscCall(KSPGetRhs(ksp, &b));
59   PetscCall(VecDuplicate(b, &b2));
60   PetscCall(MatMult(A, x, b2));
61   PetscCall(VecAXPY(b2, -1.0, b));
62   PetscCall(VecNorm(b2, NORM_MAX, &nrm));
63   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Residual norm %g\n", (double)nrm));
64 
65   PetscCall(VecDestroy(&b2));
66   PetscCall(KSPDestroy(&ksp));
67   PetscCall(DMDestroy(&da));
68   PetscCall(PetscFinalize());
69   return 0;
70 }
71 
72 static PetscErrorCode ComputeRHS(KSP ksp, Vec b, void *ctx) {
73   PetscInt    mx, idx[2];
74   PetscScalar h, v[2];
75   DM          da;
76 
77   PetscFunctionBeginUser;
78   PetscCall(KSPGetDM(ksp, &da));
79   PetscCall(DMDAGetInfo(da, 0, &mx, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
80   h = 1.0 / ((mx - 1));
81   PetscCall(VecSet(b, h));
82   idx[0] = 0;
83   idx[1] = mx - 1;
84   v[0] = v[1] = 0.0;
85   PetscCall(VecSetValues(b, 2, idx, v, INSERT_VALUES));
86   PetscCall(VecAssemblyBegin(b));
87   PetscCall(VecAssemblyEnd(b));
88   PetscFunctionReturn(0);
89 }
90 
91 static PetscErrorCode ComputeMatrix(KSP ksp, Mat J, Mat jac, void *ctx) {
92   AppCtx     *user = (AppCtx *)ctx;
93   PetscInt    i, mx, xm, xs;
94   PetscScalar v[3], h, xlow, xhigh;
95   MatStencil  row, col[3];
96   DM          da;
97 
98   PetscFunctionBeginUser;
99   PetscCall(KSPGetDM(ksp, &da));
100   PetscCall(DMDAGetInfo(da, 0, &mx, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
101   PetscCall(DMDAGetCorners(da, &xs, 0, 0, &xm, 0, 0));
102   h = 1.0 / (mx - 1);
103 
104   for (i = xs; i < xs + xm; i++) {
105     row.i = i;
106     if (i == 0 || i == mx - 1) {
107       v[0] = 2.0 / h;
108       PetscCall(MatSetValuesStencil(jac, 1, &row, 1, &row, v, INSERT_VALUES));
109     } else {
110       xlow     = h * (PetscReal)i - .5 * h;
111       xhigh    = xlow + h;
112       v[0]     = (-1.0 - user->e * PetscSinScalar(2.0 * PETSC_PI * user->k * xlow)) / h;
113       col[0].i = i - 1;
114       v[1]     = (2.0 + user->e * PetscSinScalar(2.0 * PETSC_PI * user->k * xlow) + user->e * PetscSinScalar(2.0 * PETSC_PI * user->k * xhigh)) / h;
115       col[1].i = row.i;
116       v[2]     = (-1.0 - user->e * PetscSinScalar(2.0 * PETSC_PI * user->k * xhigh)) / h;
117       col[2].i = i + 1;
118       PetscCall(MatSetValuesStencil(jac, 1, &row, 3, col, v, INSERT_VALUES));
119     }
120   }
121   PetscCall(MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY));
122   PetscCall(MatAssemblyEnd(jac, MAT_FINAL_ASSEMBLY));
123   PetscFunctionReturn(0);
124 }
125 
126 /*TEST
127 
128    test:
129       args: -pc_type mg -ksp_type fgmres -da_refine 2 -ksp_monitor_short -mg_levels_ksp_monitor_short -mg_levels_ksp_norm_type unpreconditioned -ksp_view -pc_mg_type full
130       requires: !single
131 
132    test:
133       suffix: 2
134       nsize: 2
135       args: -pc_type mg -ksp_type fgmres -da_refine 2 -ksp_monitor_short -mg_levels_ksp_monitor_short -mg_levels_ksp_norm_type unpreconditioned -ksp_view -pc_mg_type full
136       requires: !single
137 
138 TEST*/
139