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"; 3 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. 11 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$. 14 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} 19 20 F*/ 21 22 #include <petsc.h> 23 #include <petscfe.h> 24 25 typedef enum {RUN_FULL, RUN_TEST} RunType; 26 27 typedef struct { 28 RunType runType; /* Whether to run tests, or solve the full problem */ 29 } AppCtx; 30 31 static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options) 32 { 33 const char *runTypes[2] = {"full", "test"}; 34 PetscInt run; 35 36 PetscFunctionBeginUser; 37 options->runType = RUN_FULL; 38 PetscOptionsBegin(comm, "", "Inverse Problem Options", "DMPLEX"); 39 run = options->runType; 40 PetscCall(PetscOptionsEList("-run_type", "The run type", "ex1.c", runTypes, 2, runTypes[options->runType], &run, NULL)); 41 options->runType = (RunType) run; 42 PetscOptionsEnd(); 43 PetscFunctionReturn(0); 44 } 45 46 static PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm) 47 { 48 PetscFunctionBeginUser; 49 PetscCall(DMCreate(comm, dm)); 50 PetscCall(DMSetType(*dm, DMPLEX)); 51 PetscCall(DMSetFromOptions(*dm)); 52 PetscCall(DMViewFromOptions(*dm, NULL, "-dm_view")); 53 PetscFunctionReturn(0); 54 } 55 56 /* u - (x^2 + y^2) */ 57 void f0_u(PetscInt dim, PetscInt Nf, PetscInt NfAux, 58 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 59 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 60 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, 66 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 67 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 68 PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[]) 69 { 70 PetscInt d; 71 for (d = 0; d < dim; ++d) f1[d] = u[1]*u_x[dim*2+d]; 72 } 73 /* I */ 74 void g0_uu(PetscInt dim, PetscInt Nf, PetscInt NfAux, 75 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 76 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 77 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[]) 78 { 79 g0[0] = 1.0; 80 } 81 /* \nabla */ 82 void g2_ua(PetscInt dim, PetscInt Nf, PetscInt NfAux, 83 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 84 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 85 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g2[]) 86 { 87 PetscInt d; 88 for (d = 0; d < dim; ++d) g2[d] = u_x[dim*2+d]; 89 } 90 /* a */ 91 void g3_ul(PetscInt dim, PetscInt Nf, PetscInt NfAux, 92 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 93 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 94 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g3[]) 95 { 96 PetscInt d; 97 for (d = 0; d < dim; ++d) g3[d*dim+d] = u[1]; 98 } 99 /* a - (x + y) */ 100 void f0_a(PetscInt dim, PetscInt Nf, PetscInt NfAux, 101 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 102 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 103 PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[]) 104 { 105 f0[0] = u[1] - (x[0] + x[1]); 106 } 107 /* \lambda \nabla u */ 108 void f1_a(PetscInt dim, PetscInt Nf, PetscInt NfAux, 109 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 110 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 111 PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[]) 112 { 113 PetscInt d; 114 for (d = 0; d < dim; ++d) f1[d] = u[2]*u_x[d]; 115 } 116 /* I */ 117 void g0_aa(PetscInt dim, PetscInt Nf, PetscInt NfAux, 118 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 119 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 120 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[]) 121 { 122 g0[0] = 1.0; 123 } 124 /* 6 (x + y) */ 125 void f0_l(PetscInt dim, PetscInt Nf, PetscInt NfAux, 126 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 127 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 128 PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[]) 129 { 130 f0[0] = 6.0*(x[0] + x[1]); 131 } 132 /* a \nabla u */ 133 void f1_l(PetscInt dim, PetscInt Nf, PetscInt NfAux, 134 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 135 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 136 PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[]) 137 { 138 PetscInt d; 139 for (d = 0; d < dim; ++d) f1[d] = u[1]*u_x[d]; 140 } 141 /* \nabla u */ 142 void g2_la(PetscInt dim, PetscInt Nf, PetscInt NfAux, 143 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 144 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 145 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g2[]) 146 { 147 PetscInt d; 148 for (d = 0; d < dim; ++d) g2[d] = u_x[d]; 149 } 150 /* a */ 151 void g3_lu(PetscInt dim, PetscInt Nf, PetscInt NfAux, 152 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 153 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 154 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g3[]) 155 { 156 PetscInt d; 157 for (d = 0; d < dim; ++d) g3[d*dim+d] = u[1]; 158 } 159 160 /* 161 In 2D for Dirichlet conditions with a variable coefficient, we use exact solution: 162 163 u = x^2 + y^2 164 f = 6 (x + y) 165 kappa(a) = a = (x + y) 166 167 so that 168 169 -\div \kappa(a) \grad u + f = -6 (x + y) + 6 (x + y) = 0 170 */ 171 PetscErrorCode quadratic_u_2d(PetscInt dim, PetscReal t, const PetscReal x[], PetscInt Nf, PetscScalar *u, void *ctx) 172 { 173 *u = x[0]*x[0] + x[1]*x[1]; 174 return 0; 175 } 176 PetscErrorCode linear_a_2d(PetscInt dim, PetscReal t, const PetscReal x[], PetscInt Nf, PetscScalar *a, void *ctx) 177 { 178 *a = x[0] + x[1]; 179 return 0; 180 } 181 PetscErrorCode zero(PetscInt dim, PetscReal t, const PetscReal x[], PetscInt Nf, PetscScalar *l, void *ctx) 182 { 183 *l = 0.0; 184 return 0; 185 } 186 187 PetscErrorCode SetupProblem(DM dm, AppCtx *user) 188 { 189 PetscDS ds; 190 DMLabel label; 191 const PetscInt id = 1; 192 193 PetscFunctionBeginUser; 194 PetscCall(DMGetDS(dm, &ds)); 195 PetscCall(PetscDSSetResidual(ds, 0, f0_u, f1_u)); 196 PetscCall(PetscDSSetResidual(ds, 1, f0_a, f1_a)); 197 PetscCall(PetscDSSetResidual(ds, 2, f0_l, f1_l)); 198 PetscCall(PetscDSSetJacobian(ds, 0, 0, g0_uu, NULL, NULL, NULL)); 199 PetscCall(PetscDSSetJacobian(ds, 0, 1, NULL, NULL, g2_ua, NULL)); 200 PetscCall(PetscDSSetJacobian(ds, 0, 2, NULL, NULL, NULL, g3_ul)); 201 PetscCall(PetscDSSetJacobian(ds, 1, 1, g0_aa, NULL, NULL, NULL)); 202 PetscCall(PetscDSSetJacobian(ds, 2, 1, NULL, NULL, g2_la, NULL)); 203 PetscCall(PetscDSSetJacobian(ds, 2, 0, NULL, NULL, NULL, g3_lu)); 204 205 PetscCall(PetscDSSetExactSolution(ds, 0, quadratic_u_2d, NULL)); 206 PetscCall(PetscDSSetExactSolution(ds, 1, linear_a_2d, NULL)); 207 PetscCall(PetscDSSetExactSolution(ds, 2, zero, NULL)); 208 PetscCall(DMGetLabel(dm, "marker", &label)); 209 PetscCall(DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, 1, &id, 0, 0, NULL, (void (*)(void)) quadratic_u_2d, NULL, user, NULL)); 210 PetscCall(DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, 1, &id, 1, 0, NULL, (void (*)(void)) linear_a_2d, NULL, user, NULL)); 211 PetscCall(DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, 1, &id, 2, 0, NULL, (void (*)(void)) zero, NULL, user, NULL)); 212 PetscFunctionReturn(0); 213 } 214 215 PetscErrorCode SetupDiscretization(DM dm, AppCtx *user) 216 { 217 DM cdm = dm; 218 const PetscInt dim = 2; 219 PetscFE fe[3]; 220 PetscInt f; 221 MPI_Comm comm; 222 223 PetscFunctionBeginUser; 224 /* Create finite element */ 225 PetscCall(PetscObjectGetComm((PetscObject) dm, &comm)); 226 PetscCall(PetscFECreateDefault(comm, dim, 1, PETSC_TRUE, "potential_", -1, &fe[0])); 227 PetscCall(PetscObjectSetName((PetscObject) fe[0], "potential")); 228 PetscCall(PetscFECreateDefault(comm, dim, 1, PETSC_TRUE, "conductivity_", -1, &fe[1])); 229 PetscCall(PetscObjectSetName((PetscObject) fe[1], "conductivity")); 230 PetscCall(PetscFECopyQuadrature(fe[0], fe[1])); 231 PetscCall(PetscFECreateDefault(comm, dim, 1, PETSC_TRUE, "multiplier_", -1, &fe[2])); 232 PetscCall(PetscObjectSetName((PetscObject) fe[2], "multiplier")); 233 PetscCall(PetscFECopyQuadrature(fe[0], fe[2])); 234 /* Set discretization and boundary conditions for each mesh */ 235 for (f = 0; f < 3; ++f) PetscCall(DMSetField(dm, f, NULL, (PetscObject) fe[f])); 236 PetscCall(DMCreateDS(dm)); 237 PetscCall(SetupProblem(dm, user)); 238 while (cdm) { 239 PetscCall(DMCopyDisc(dm, cdm)); 240 PetscCall(DMGetCoarseDM(cdm, &cdm)); 241 } 242 for (f = 0; f < 3; ++f) PetscCall(PetscFEDestroy(&fe[f])); 243 PetscFunctionReturn(0); 244 } 245 246 int main(int argc, char **argv) 247 { 248 DM dm; 249 SNES snes; 250 Vec u, r; 251 AppCtx user; 252 253 PetscCall(PetscInitialize(&argc, &argv, NULL,help)); 254 PetscCall(ProcessOptions(PETSC_COMM_WORLD, &user)); 255 PetscCall(SNESCreate(PETSC_COMM_WORLD, &snes)); 256 PetscCall(CreateMesh(PETSC_COMM_WORLD, &user, &dm)); 257 PetscCall(SNESSetDM(snes, dm)); 258 PetscCall(SetupDiscretization(dm, &user)); 259 260 PetscCall(DMCreateGlobalVector(dm, &u)); 261 PetscCall(PetscObjectSetName((PetscObject) u, "solution")); 262 PetscCall(VecDuplicate(u, &r)); 263 PetscCall(DMPlexSetSNESLocalFEM(dm,&user,&user,&user)); 264 PetscCall(SNESSetFromOptions(snes)); 265 266 PetscCall(DMSNESCheckFromOptions(snes, u)); 267 if (user.runType == RUN_FULL) { 268 PetscDS ds; 269 PetscErrorCode (*exactFuncs[3])(PetscInt dim, PetscReal t, const PetscReal x[], PetscInt Nf, PetscScalar *u, void *ctx); 270 PetscErrorCode (*initialGuess[3])(PetscInt dim, PetscReal t, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx); 271 PetscReal error; 272 273 PetscCall(DMGetDS(dm, &ds)); 274 PetscCall(PetscDSGetExactSolution(ds, 0, &exactFuncs[0], NULL)); 275 PetscCall(PetscDSGetExactSolution(ds, 1, &exactFuncs[1], NULL)); 276 PetscCall(PetscDSGetExactSolution(ds, 2, &exactFuncs[2], NULL)); 277 initialGuess[0] = zero; 278 initialGuess[1] = zero; 279 initialGuess[2] = zero; 280 PetscCall(DMProjectFunction(dm, 0.0, initialGuess, NULL, INSERT_VALUES, u)); 281 PetscCall(VecViewFromOptions(u, NULL, "-initial_vec_view")); 282 PetscCall(DMComputeL2Diff(dm, 0.0, exactFuncs, NULL, u, &error)); 283 if (error < 1.0e-11) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Initial L_2 Error: < 1.0e-11\n")); 284 else PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Initial L_2 Error: %g\n", (double)error)); 285 PetscCall(SNESSolve(snes, NULL, u)); 286 PetscCall(DMComputeL2Diff(dm, 0.0, exactFuncs, NULL, u, &error)); 287 if (error < 1.0e-11) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Final L_2 Error: < 1.0e-11\n")); 288 else PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Final L_2 Error: %g\n", (double)error)); 289 } 290 PetscCall(VecViewFromOptions(u, NULL, "-sol_vec_view")); 291 292 PetscCall(VecDestroy(&u)); 293 PetscCall(VecDestroy(&r)); 294 PetscCall(SNESDestroy(&snes)); 295 PetscCall(DMDestroy(&dm)); 296 PetscCall(PetscFinalize()); 297 return 0; 298 } 299 300 /*TEST 301 302 build: 303 requires: !complex 304 305 test: 306 suffix: 0 307 requires: triangle 308 args: -run_type test -dmsnes_check -potential_petscspace_degree 2 -conductivity_petscspace_degree 1 -multiplier_petscspace_degree 2 309 310 test: 311 suffix: 1 312 requires: triangle 313 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 314 315 TEST*/ 316