Actual source code: ex1.c
1: static char help[] = "One-Shot Multigrid for Parameter Estimation Problem for the Poisson Equation.\n\
2: Using the Interior Point Method.\n\n\n";
4: /*F
5: We are solving the parameter estimation problem for the Laplacian. We will ask to minimize a Lagrangian
6: function over $a$ and $u$, given by
7: \begin{align}
8: L(u, a, \lambda) = \frac{1}{2} || Qu - d ||^2 + \frac{1}{2} || L (a - a_r) ||^2 + \lambda F(u; a)
9: \end{align}
10: where $Q$ is a sampling operator, $L$ is a regularization operator, $F$ defines the PDE.
12: Currently, we have perfect information, meaning $Q = I$, and then we need no regularization, $L = I$. We
13: also give the exact control for the reference $a_r$.
15: The PDE will be the Laplace equation with homogeneous boundary conditions
16: \begin{align}
17: -nabla \cdot a \nabla u = f
18: \end{align}
20: F*/
22: #include <petsc.h>
23: #include <petscfe.h>
25: typedef enum {
26: RUN_FULL,
27: RUN_TEST
28: } RunType;
30: typedef struct {
31: RunType runType; /* Whether to run tests, or solve the full problem */
32: } AppCtx;
34: static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
35: {
36: const char *runTypes[2] = {"full", "test"};
37: PetscInt run;
39: PetscFunctionBeginUser;
40: options->runType = RUN_FULL;
41: PetscOptionsBegin(comm, "", "Inverse Problem Options", "DMPLEX");
42: run = options->runType;
43: PetscCall(PetscOptionsEList("-run_type", "The run type", "ex1.c", runTypes, 2, runTypes[options->runType], &run, NULL));
44: options->runType = (RunType)run;
45: PetscOptionsEnd();
46: PetscFunctionReturn(PETSC_SUCCESS);
47: }
49: static PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm)
50: {
51: PetscFunctionBeginUser;
52: PetscCall(DMCreate(comm, dm));
53: PetscCall(DMSetType(*dm, DMPLEX));
54: PetscCall(DMSetFromOptions(*dm));
55: PetscCall(DMViewFromOptions(*dm, NULL, "-dm_view"));
56: PetscFunctionReturn(PETSC_SUCCESS);
57: }
59: /* u - (x^2 + y^2) */
60: void f0_u(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
61: {
62: f0[0] = u[0] - (x[0] * x[0] + x[1] * x[1]);
63: }
64: /* a \nabla\lambda */
65: void f1_u(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[])
66: {
67: PetscInt d;
68: for (d = 0; d < dim; ++d) f1[d] = u[1] * u_x[dim * 2 + d];
69: }
70: /* I */
71: void g0_uu(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
72: {
73: g0[0] = 1.0;
74: }
75: /* \nabla */
76: void g2_ua(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g2[])
77: {
78: PetscInt d;
79: for (d = 0; d < dim; ++d) g2[d] = u_x[dim * 2 + d];
80: }
81: /* a */
82: void g3_ul(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g3[])
83: {
84: PetscInt d;
85: for (d = 0; d < dim; ++d) g3[d * dim + d] = u[1];
86: }
87: /* a - (x + y) */
88: void f0_a(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
89: {
90: f0[0] = u[1] - (x[0] + x[1]);
91: }
92: /* \lambda \nabla u */
93: void f1_a(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[])
94: {
95: PetscInt d;
96: for (d = 0; d < dim; ++d) f1[d] = u[2] * u_x[d];
97: }
98: /* I */
99: void g0_aa(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
100: {
101: g0[0] = 1.0;
102: }
103: /* 6 (x + y) */
104: void f0_l(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
105: {
106: f0[0] = 6.0 * (x[0] + x[1]);
107: }
108: /* a \nabla u */
109: void f1_l(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[])
110: {
111: for (PetscInt d = 0; d < dim; ++d) f1[d] = u[1] * u_x[d];
112: }
113: /* \nabla u */
114: void g2_la(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g2[])
115: {
116: for (PetscInt d = 0; d < dim; ++d) g2[d] = u_x[d];
117: }
118: /* a */
119: void g3_lu(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g3[])
120: {
121: for (PetscInt d = 0; d < dim; ++d) g3[d * dim + d] = u[1];
122: }
124: /*
125: In 2D for Dirichlet conditions with a variable coefficient, we use exact solution:
127: u = x^2 + y^2
128: f = 6 (x + y)
129: kappa(a) = a = (x + y)
131: so that
133: -\div \kappa(a) \grad u + f = -6 (x + y) + 6 (x + y) = 0
134: */
135: PetscErrorCode quadratic_u_2d(PetscInt dim, PetscReal t, const PetscReal x[], PetscInt Nf, PetscScalar *u, PetscCtx ctx)
136: {
137: *u = x[0] * x[0] + x[1] * x[1];
138: return PETSC_SUCCESS;
139: }
140: PetscErrorCode linear_a_2d(PetscInt dim, PetscReal t, const PetscReal x[], PetscInt Nf, PetscScalar *a, PetscCtx ctx)
141: {
142: *a = x[0] + x[1];
143: return PETSC_SUCCESS;
144: }
145: PetscErrorCode zero(PetscInt dim, PetscReal t, const PetscReal x[], PetscInt Nf, PetscScalar *l, PetscCtx ctx)
146: {
147: *l = 0.0;
148: return PETSC_SUCCESS;
149: }
151: PetscErrorCode SetupProblem(DM dm, AppCtx *user)
152: {
153: PetscDS ds;
154: DMLabel label;
155: const PetscInt id = 1;
157: PetscFunctionBeginUser;
158: PetscCall(DMGetDS(dm, &ds));
159: PetscCall(PetscDSSetResidual(ds, 0, f0_u, f1_u));
160: PetscCall(PetscDSSetResidual(ds, 1, f0_a, f1_a));
161: PetscCall(PetscDSSetResidual(ds, 2, f0_l, f1_l));
162: PetscCall(PetscDSSetJacobian(ds, 0, 0, g0_uu, NULL, NULL, NULL));
163: PetscCall(PetscDSSetJacobian(ds, 0, 1, NULL, NULL, g2_ua, NULL));
164: PetscCall(PetscDSSetJacobian(ds, 0, 2, NULL, NULL, NULL, g3_ul));
165: PetscCall(PetscDSSetJacobian(ds, 1, 1, g0_aa, NULL, NULL, NULL));
166: PetscCall(PetscDSSetJacobian(ds, 2, 1, NULL, NULL, g2_la, NULL));
167: PetscCall(PetscDSSetJacobian(ds, 2, 0, NULL, NULL, NULL, g3_lu));
169: PetscCall(PetscDSSetExactSolution(ds, 0, quadratic_u_2d, NULL));
170: PetscCall(PetscDSSetExactSolution(ds, 1, linear_a_2d, NULL));
171: PetscCall(PetscDSSetExactSolution(ds, 2, zero, NULL));
172: PetscCall(DMGetLabel(dm, "marker", &label));
173: PetscCall(DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, 1, &id, 0, 0, NULL, (PetscVoidFn *)quadratic_u_2d, NULL, user, NULL));
174: PetscCall(DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, 1, &id, 1, 0, NULL, (PetscVoidFn *)linear_a_2d, NULL, user, NULL));
175: PetscCall(DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, 1, &id, 2, 0, NULL, (PetscVoidFn *)zero, NULL, user, NULL));
176: PetscFunctionReturn(PETSC_SUCCESS);
177: }
179: PetscErrorCode SetupDiscretization(DM dm, AppCtx *user)
180: {
181: DM cdm = dm;
182: const PetscInt dim = 2;
183: PetscFE fe[3];
184: MPI_Comm comm;
186: PetscFunctionBeginUser;
187: /* Create finite element */
188: PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
189: PetscCall(PetscFECreateDefault(comm, dim, 1, PETSC_TRUE, "potential_", -1, &fe[0]));
190: PetscCall(PetscObjectSetName((PetscObject)fe[0], "potential"));
191: PetscCall(PetscFECreateDefault(comm, dim, 1, PETSC_TRUE, "conductivity_", -1, &fe[1]));
192: PetscCall(PetscObjectSetName((PetscObject)fe[1], "conductivity"));
193: PetscCall(PetscFECopyQuadrature(fe[0], fe[1]));
194: PetscCall(PetscFECreateDefault(comm, dim, 1, PETSC_TRUE, "multiplier_", -1, &fe[2]));
195: PetscCall(PetscObjectSetName((PetscObject)fe[2], "multiplier"));
196: PetscCall(PetscFECopyQuadrature(fe[0], fe[2]));
197: /* Set discretization and boundary conditions for each mesh */
198: for (PetscInt f = 0; f < 3; ++f) PetscCall(DMSetField(dm, f, NULL, (PetscObject)fe[f]));
199: PetscCall(DMCreateDS(dm));
200: PetscCall(SetupProblem(dm, user));
201: while (cdm) {
202: PetscCall(DMCopyDisc(dm, cdm));
203: PetscCall(DMGetCoarseDM(cdm, &cdm));
204: }
205: for (PetscInt f = 0; f < 3; ++f) PetscCall(PetscFEDestroy(&fe[f]));
206: PetscFunctionReturn(PETSC_SUCCESS);
207: }
209: int main(int argc, char **argv)
210: {
211: DM dm;
212: SNES snes;
213: Vec u, r;
214: AppCtx user;
216: PetscFunctionBeginUser;
217: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
218: PetscCall(ProcessOptions(PETSC_COMM_WORLD, &user));
219: PetscCall(SNESCreate(PETSC_COMM_WORLD, &snes));
220: PetscCall(CreateMesh(PETSC_COMM_WORLD, &user, &dm));
221: PetscCall(SNESSetDM(snes, dm));
222: PetscCall(SetupDiscretization(dm, &user));
224: PetscCall(DMCreateGlobalVector(dm, &u));
225: PetscCall(PetscObjectSetName((PetscObject)u, "solution"));
226: PetscCall(VecDuplicate(u, &r));
227: PetscCall(DMPlexSetSNESLocalFEM(dm, PETSC_FALSE, &user));
228: PetscCall(SNESSetFromOptions(snes));
230: PetscCall(DMSNESCheckFromOptions(snes, u));
231: if (user.runType == RUN_FULL) {
232: PetscDS ds;
233: PetscErrorCode (*exactFuncs[3])(PetscInt dim, PetscReal t, const PetscReal x[], PetscInt Nf, PetscScalar *u, PetscCtx ctx);
234: PetscErrorCode (*initialGuess[3])(PetscInt dim, PetscReal t, const PetscReal x[], PetscInt Nf, PetscScalar u[], PetscCtx ctx);
235: PetscReal error;
237: PetscCall(DMGetDS(dm, &ds));
238: PetscCall(PetscDSGetExactSolution(ds, 0, &exactFuncs[0], NULL));
239: PetscCall(PetscDSGetExactSolution(ds, 1, &exactFuncs[1], NULL));
240: PetscCall(PetscDSGetExactSolution(ds, 2, &exactFuncs[2], NULL));
241: initialGuess[0] = zero;
242: initialGuess[1] = zero;
243: initialGuess[2] = zero;
244: PetscCall(DMProjectFunction(dm, 0.0, initialGuess, NULL, INSERT_VALUES, u));
245: PetscCall(VecViewFromOptions(u, NULL, "-initial_vec_view"));
246: PetscCall(DMComputeL2Diff(dm, 0.0, exactFuncs, NULL, u, &error));
247: if (error < 1.0e-11) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Initial L_2 Error: < 1.0e-11\n"));
248: else PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Initial L_2 Error: %g\n", (double)error));
249: PetscCall(SNESSolve(snes, NULL, u));
250: PetscCall(DMComputeL2Diff(dm, 0.0, exactFuncs, NULL, u, &error));
251: if (error < 1.0e-11) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Final L_2 Error: < 1.0e-11\n"));
252: else PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Final L_2 Error: %g\n", (double)error));
253: }
254: PetscCall(VecViewFromOptions(u, NULL, "-sol_vec_view"));
256: PetscCall(VecDestroy(&u));
257: PetscCall(VecDestroy(&r));
258: PetscCall(SNESDestroy(&snes));
259: PetscCall(DMDestroy(&dm));
260: PetscCall(PetscFinalize());
261: return 0;
262: }
264: /*TEST
266: build:
267: requires: !complex
269: test:
270: suffix: 0
271: requires: triangle
272: args: -run_type test -dmsnes_check -potential_petscspace_degree 2 -conductivity_petscspace_degree 1 -multiplier_petscspace_degree 2
274: test:
275: suffix: 1
276: requires: triangle
277: args: -potential_petscspace_degree 2 -conductivity_petscspace_degree 1 -multiplier_petscspace_degree 2 -snes_monitor -pc_type fieldsplit -pc_fieldsplit_0_fields 0,1 -pc_fieldsplit_1_fields 2 -pc_fieldsplit_type schur -pc_fieldsplit_schur_factorization_type full -pc_fieldsplit_schur_precondition selfp -fieldsplit_0_pc_type lu -fieldsplit_multiplier_ksp_rtol 1.0e-10 -fieldsplit_multiplier_pc_type lu -sol_vec_view
279: TEST*/