1 static char help[] = "Poisson Problem with finite elements.\n\ 2 This example supports automatic convergence estimation for multilevel solvers\n\ 3 and solver adaptivity.\n\n\n"; 4 5 #include <petscdmplex.h> 6 #include <petscsnes.h> 7 #include <petscds.h> 8 #include <petscconvest.h> 9 10 /* Next steps: 11 12 - Show lowest eigenmodes using SLEPc code from my ex6 13 14 - Run CR example from Brannick's slides that looks like semicoarsening 15 - Show lowest modes 16 - Show CR convergence rate 17 - Show CR solution to show non-convergence 18 - Refine coarse grid around non-converged dofs 19 - Maybe use Barry's "more then Z% above the average" monitor to label bad dofs 20 - Mark coarse cells that contain bad dofs 21 - Run SBR on coarse grid 22 23 - Run Helmholtz example from Gander's writeup 24 25 - Run Low Mach example? 26 27 - Run subduction example? 28 */ 29 30 typedef struct { 31 PetscBool cr; /* Use compatible relaxation */ 32 } AppCtx; 33 34 static PetscErrorCode trig_u(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx) 35 { 36 PetscInt d; 37 u[0] = 0.0; 38 for (d = 0; d < dim; ++d) u[0] += PetscSinReal(2.0*PETSC_PI*x[d]); 39 return 0; 40 } 41 42 static void f0_trig_u(PetscInt dim, PetscInt Nf, PetscInt NfAux, 43 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 44 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 45 PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[]) 46 { 47 PetscInt d; 48 for (d = 0; d < dim; ++d) f0[0] += -4.0*PetscSqr(PETSC_PI)*PetscSinReal(2.0*PETSC_PI*x[d]); 49 } 50 51 static void f1_u(PetscInt dim, PetscInt Nf, PetscInt NfAux, 52 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 53 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 54 PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[]) 55 { 56 PetscInt d; 57 for (d = 0; d < dim; ++d) f1[d] = u_x[d]; 58 } 59 60 static void g3_uu(PetscInt dim, PetscInt Nf, PetscInt NfAux, 61 const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], 62 const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], 63 PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g3[]) 64 { 65 PetscInt d; 66 for (d = 0; d < dim; ++d) g3[d*dim+d] = 1.0; 67 } 68 69 static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options) 70 { 71 PetscErrorCode ierr; 72 73 PetscFunctionBeginUser; 74 options->cr = PETSC_FALSE; 75 76 ierr = PetscOptionsBegin(comm, "", "Poisson Problem Options", "DMPLEX");PetscCall(ierr); 77 PetscCall(PetscOptionsBool("-cr", "Use compatible relaxarion", "ex20.c", options->cr, &options->cr, NULL)); 78 ierr = PetscOptionsEnd();PetscCall(ierr); 79 PetscFunctionReturn(0); 80 } 81 82 static PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm) 83 { 84 PetscFunctionBeginUser; 85 PetscCall(DMCreate(comm, dm)); 86 PetscCall(DMSetType(*dm, DMPLEX)); 87 PetscCall(DMSetFromOptions(*dm)); 88 PetscCall(DMSetApplicationContext(*dm, user)); 89 PetscCall(DMViewFromOptions(*dm, NULL, "-dm_view")); 90 PetscFunctionReturn(0); 91 } 92 93 static PetscErrorCode SetupPrimalProblem(DM dm, AppCtx *user) 94 { 95 PetscDS ds; 96 DMLabel label; 97 const PetscInt id = 1; 98 99 PetscFunctionBeginUser; 100 PetscCall(DMGetDS(dm, &ds)); 101 PetscCall(DMGetLabel(dm, "marker", &label)); 102 PetscCall(PetscDSSetResidual(ds, 0, f0_trig_u, f1_u)); 103 PetscCall(PetscDSSetJacobian(ds, 0, 0, NULL, NULL, NULL, g3_uu)); 104 PetscCall(PetscDSSetExactSolution(ds, 0, trig_u, user)); 105 PetscCall(DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, 1, &id, 0, 0, NULL, (void (*)(void)) trig_u, NULL, user, NULL)); 106 PetscFunctionReturn(0); 107 } 108 109 static PetscErrorCode SetupDiscretization(DM dm, const char name[], PetscErrorCode (*setup)(DM, AppCtx *), AppCtx *user) 110 { 111 DM cdm = dm; 112 PetscFE fe; 113 DMPolytopeType ct; 114 PetscBool simplex; 115 PetscInt dim, cStart; 116 char prefix[PETSC_MAX_PATH_LEN]; 117 118 PetscFunctionBeginUser; 119 PetscCall(DMGetDimension(dm, &dim)); 120 PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL)); 121 PetscCall(DMPlexGetCellType(dm, cStart, &ct)); 122 simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE; 123 124 PetscCall(PetscSNPrintf(prefix, PETSC_MAX_PATH_LEN, "%s_", name)); 125 PetscCall(PetscFECreateDefault(PetscObjectComm((PetscObject) dm), dim, 1, simplex, name ? prefix : NULL, -1, &fe)); 126 PetscCall(PetscObjectSetName((PetscObject) fe, name)); 127 PetscCall(DMSetField(dm, 0, NULL, (PetscObject) fe)); 128 PetscCall(DMCreateDS(dm)); 129 PetscCall((*setup)(dm, user)); 130 while (cdm) { 131 PetscCall(DMCopyDisc(dm,cdm)); 132 PetscCall(DMGetCoarseDM(cdm, &cdm)); 133 } 134 PetscCall(PetscFEDestroy(&fe)); 135 PetscFunctionReturn(0); 136 } 137 138 /* 139 How to do CR in PETSc: 140 141 Loop over PCMG levels, coarse to fine: 142 Run smoother for 5 iterates 143 At each iterate, solve Inj u_f = u_c with LSQR to 1e-15 144 Suppose that e_k = c^k e_0, which means log e_k = log e_0 + k log c 145 Fit log of error to look at log c, the slope 146 Check R^2 for linearity (1 - square residual / variance) 147 Solve exactly 148 Prolong to next level 149 */ 150 151 int main(int argc, char **argv) 152 { 153 DM dm; /* Problem specification */ 154 SNES snes; /* Nonlinear solver */ 155 Vec u; /* Solutions */ 156 AppCtx user; /* User-defined work context */ 157 158 PetscCall(PetscInitialize(&argc, &argv, NULL,help)); 159 PetscCall(ProcessOptions(PETSC_COMM_WORLD, &user)); 160 /* Primal system */ 161 PetscCall(SNESCreate(PETSC_COMM_WORLD, &snes)); 162 PetscCall(CreateMesh(PETSC_COMM_WORLD, &user, &dm)); 163 PetscCall(SNESSetDM(snes, dm)); 164 PetscCall(SetupDiscretization(dm, "potential", SetupPrimalProblem, &user)); 165 PetscCall(DMCreateGlobalVector(dm, &u)); 166 PetscCall(VecSet(u, 0.0)); 167 PetscCall(PetscObjectSetName((PetscObject) u, "potential")); 168 PetscCall(DMPlexSetSNESLocalFEM(dm, &user, &user, &user)); 169 PetscCall(SNESSetFromOptions(snes)); 170 PetscCall(SNESSolve(snes, NULL, u)); 171 PetscCall(SNESGetSolution(snes, &u)); 172 PetscCall(VecViewFromOptions(u, NULL, "-potential_view")); 173 /* Cleanup */ 174 PetscCall(VecDestroy(&u)); 175 PetscCall(SNESDestroy(&snes)); 176 PetscCall(DMDestroy(&dm)); 177 PetscCall(PetscFinalize()); 178 return 0; 179 } 180 181 /*TEST 182 183 test: 184 suffix: 2d_p1_gmg_vcycle_rate 185 requires: triangle 186 args: -potential_petscspace_degree 1 -dm_plex_box_faces 2,2 -dm_refine_hierarchy 3 \ 187 -ksp_rtol 5e-10 -ksp_converged_rate -pc_type mg \ 188 -mg_levels_ksp_max_it 5 -mg_levels_ksp_norm_type preconditioned -mg_levels_ksp_converged_rate \ 189 -mg_levels_esteig_ksp_type cg \ 190 -mg_levels_esteig_ksp_max_it 10 \ 191 -mg_levels_ksp_chebyshev_esteig 0,0.05,0,1.05 \ 192 -mg_levels_pc_type jacobi 193 194 test: 195 suffix: 2d_p1_gmg_vcycle_cr 196 TODO: broken 197 # cannot MatShift() a MATNORMAL until this MatType inherits from MATSHELL, cf. https://gitlab.com/petsc/petsc/-/issues/972 198 requires: triangle 199 args: -potential_petscspace_degree 1 -dm_plex_box_faces 2,2 -dm_refine_hierarchy 3 \ 200 -ksp_rtol 5e-10 -pc_type mg -pc_mg_adapt_cr \ 201 -mg_levels_ksp_max_it 5 -mg_levels_ksp_norm_type preconditioned \ 202 -mg_levels_esteig_ksp_type cg \ 203 -mg_levels_esteig_ksp_max_it 10 \ 204 -mg_levels_ksp_chebyshev_esteig 0,0.05,0,1.05 \ 205 -mg_levels_cr_ksp_max_it 5 -mg_levels_cr_ksp_converged_rate -mg_levels_cr_ksp_converged_rate_type error 206 207 test: 208 suffix: 2d_p1_gmg_fcycle_rate 209 requires: triangle 210 args: -potential_petscspace_degree 1 -dm_plex_box_faces 2,2 -dm_refine_hierarchy 3 \ 211 -ksp_rtol 5e-10 -ksp_converged_rate -pc_type mg -pc_mg_type full \ 212 -mg_levels_ksp_max_it 5 -mg_levels_ksp_norm_type preconditioned -mg_levels_ksp_converged_rate \ 213 -mg_levels_esteig_ksp_type cg \ 214 -mg_levels_esteig_ksp_max_it 10 \ 215 -mg_levels_ksp_chebyshev_esteig 0,0.05,0,1.05 \ 216 -mg_levels_pc_type jacobi 217 test: 218 suffix: 2d_p1_gmg_vcycle_adapt_rate 219 requires: triangle bamg 220 args: -potential_petscspace_degree 1 -dm_plex_box_faces 2,2 -dm_refine_hierarchy 3 \ 221 -ksp_rtol 5e-10 -ksp_converged_rate -pc_type mg \ 222 -pc_mg_galerkin -pc_mg_adapt_interp -pc_mg_adapt_interp_coarse_space harmonic -pc_mg_adapt_interp_n 8 \ 223 -mg_levels_ksp_max_it 5 -mg_levels_ksp_norm_type preconditioned -mg_levels_ksp_converged_rate \ 224 -mg_levels_esteig_ksp_type cg \ 225 -mg_levels_esteig_ksp_max_it 10 \ 226 -mg_levels_ksp_chebyshev_esteig 0,0.05,0,1.05 \ 227 -mg_levels_pc_type jacobi 228 test: 229 suffix: 2d_p1_scalable_rate 230 requires: triangle 231 args: -potential_petscspace_degree 1 -dm_refine 3 \ 232 -ksp_type cg -ksp_rtol 1.e-11 -ksp_norm_type unpreconditioned -ksp_converged_rate \ 233 -pc_type gamg -pc_gamg_esteig_ksp_max_it 10 -pc_gamg_esteig_ksp_type cg \ 234 -pc_gamg_type agg -pc_gamg_agg_nsmooths 1 \ 235 -pc_gamg_coarse_eq_limit 1000 \ 236 -pc_gamg_square_graph 1 \ 237 -pc_gamg_threshold 0.05 \ 238 -pc_gamg_threshold_scale .0 \ 239 -mg_levels_ksp_type chebyshev -mg_levels_ksp_norm_type preconditioned -mg_levels_ksp_converged_rate \ 240 -mg_levels_ksp_max_it 5 \ 241 -matptap_via scalable 242 243 TEST*/ 244