1 // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at 2 // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights 3 // reserved. See files LICENSE and NOTICE for details. 4 // 5 // This file is part of CEED, a collection of benchmarks, miniapps, software 6 // libraries and APIs for efficient high-order finite element and spectral 7 // element discretizations for exascale applications. For more information and 8 // source code availability see http://github.com/ceed. 9 // 10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC, 11 // a collaborative effort of two U.S. Department of Energy organizations (Office 12 // of Science and the National Nuclear Security Administration) responsible for 13 // the planning and preparation of a capable exascale ecosystem, including 14 // software, applications, hardware, advanced system engineering and early 15 // testbed platforms, in support of the nation's exascale computing imperative. 16 17 // libCEED + PETSc Example: Elasticity 18 // 19 // This example demonstrates a simple usage of libCEED with PETSc to solve 20 // elasticity problems. 21 // 22 // The code uses higher level communication protocols in DMPlex. 23 // 24 // Build with: 25 // 26 // make elasticity [PETSC_DIR=</path/to/petsc>] [CEED_DIR=</path/to/libceed>] 27 // 28 // Sample runs: 29 // 30 // ./elasticity -mesh [.exo file] -degree 2 -E 1 -nu 0.3 -problem linElas -forcing mms 31 // ./elasticity -mesh [.exo file] -degree 2 -E 1 -nu 0.3 -bc_clamp 998,999 -bc_clamp_998_translate 0.1,0.2,0.3 -problem hyperSS -forcing none -ceed /cpu/self 32 // ./elasticity -mesh [.exo file] -degree 2 -E 1 -nu 0.3 -bc_clamp 998,999 -bc_clamp_998 rotate 1,0,0,0.2 -problem hyperFS -forcing none -ceed /gpu/occa 33 // 34 // Sample meshes can be found at https://github.com/jeremylt/ceedSampleMeshes 35 // 36 //TESTARGS -ceed {ceed_resource} -test -degree 2 -nu 0.3 -E 1 -dm_plex_box_faces 3,3,3 37 38 /// @file 39 /// CEED elasticity example using PETSc with DMPlex 40 41 const char help[] = "Solve solid Problems with CEED and PETSc DMPlex\n"; 42 43 #include "elasticity.h" 44 45 int main(int argc, char **argv) { 46 PetscInt ierr; 47 MPI_Comm comm; 48 // Context structs 49 AppCtx appCtx; // Contains problem options 50 Physics phys; // Contains physical constants 51 Physics physSmoother = NULL; // Separate context if nuSmoother set 52 Units units; // Contains units scaling 53 // PETSc objects 54 PetscLogStage stageDMSetup, stageLibceedSetup, 55 stageSnesSetup, stageSnesSolve; 56 DM dmOrig; // Distributed DM to clone 57 DM dmEnergy, dmDiagnostic; // DMs for postprocessing 58 DM *levelDMs; 59 Vec U, *Ug, *Uloc; // U: solution, R: residual, F: forcing 60 Vec R, Rloc, F, Floc; // g: global, loc: local 61 SNES snes, snesCoarse = NULL; 62 Mat *jacobMat, jacobMatCoarse, *prolongRestrMat; 63 // PETSc data 64 UserMult resCtx, jacobCoarseCtx = NULL, *jacobCtx; 65 FormJacobCtx formJacobCtx; 66 UserMultProlongRestr *prolongRestrCtx; 67 PCMGCycleType pcmgCycleType = PC_MG_CYCLE_V; 68 // libCEED objects 69 Ceed ceed, ceedFine = NULL; 70 CeedData *ceedData; 71 CeedQFunction qfRestrict = NULL, qfProlong = NULL; 72 // Parameters 73 PetscInt ncompu = 3; // 3 DoFs in 3D 74 PetscInt ncompe = 1, ncompd = 5; // 1 energy output, 5 diagnostic 75 PetscInt numLevels = 1, fineLevel = 0; 76 PetscInt *Ugsz, *Ulsz, *Ulocsz; // sz: size 77 PetscInt snesIts = 0; 78 // Timing 79 double startTime, elapsedTime, minTime, maxTime; 80 81 ierr = PetscInitialize(&argc, &argv, NULL, help); 82 if (ierr) 83 return ierr; 84 85 // --------------------------------------------------------------------------- 86 // Process command line options 87 // --------------------------------------------------------------------------- 88 comm = PETSC_COMM_WORLD; 89 90 // -- Set mesh file, polynomial degree, problem type 91 ierr = PetscCalloc1(1, &appCtx); CHKERRQ(ierr); 92 ierr = ProcessCommandLineOptions(comm, appCtx); CHKERRQ(ierr); 93 numLevels = appCtx->numLevels; 94 fineLevel = numLevels - 1; 95 96 // -- Set Poison's ratio, Young's Modulus 97 ierr = PetscMalloc1(1, &phys); CHKERRQ(ierr); 98 ierr = PetscMalloc1(1, &units); CHKERRQ(ierr); 99 ierr = ProcessPhysics(comm, phys, units); CHKERRQ(ierr); 100 if (fabs(appCtx->nuSmoother) > 1E-14) { 101 ierr = PetscMalloc1(1, &physSmoother); CHKERRQ(ierr); 102 ierr = PetscMemcpy(physSmoother, phys, sizeof(*phys)); CHKERRQ(ierr); 103 physSmoother->nu = appCtx->nuSmoother; 104 } 105 106 // --------------------------------------------------------------------------- 107 // Setup DM 108 // --------------------------------------------------------------------------- 109 // Performance logging 110 ierr = PetscLogStageRegister("DM and Vector Setup Stage", &stageDMSetup); 111 CHKERRQ(ierr); 112 ierr = PetscLogStagePush(stageDMSetup); CHKERRQ(ierr); 113 114 // -- Create distributed DM from mesh file 115 ierr = CreateDistributedDM(comm, appCtx, &dmOrig); CHKERRQ(ierr); 116 117 // -- Setup DM by polynomial degree 118 ierr = PetscMalloc1(numLevels, &levelDMs); CHKERRQ(ierr); 119 for (PetscInt level = 0; level < numLevels; level++) { 120 ierr = DMClone(dmOrig, &levelDMs[level]); CHKERRQ(ierr); 121 ierr = SetupDMByDegree(levelDMs[level], appCtx, appCtx->levelDegrees[level], 122 PETSC_TRUE, ncompu); CHKERRQ(ierr); 123 // -- Label field components for viewing 124 // Empty name for conserved field (because there is only one field) 125 PetscSection section; 126 ierr = DMGetLocalSection(levelDMs[level], §ion); CHKERRQ(ierr); 127 ierr = PetscSectionSetFieldName(section, 0, "Displacement"); CHKERRQ(ierr); 128 ierr = PetscSectionSetComponentName(section, 0, 0, "DisplacementX"); 129 CHKERRQ(ierr); 130 ierr = PetscSectionSetComponentName(section, 0, 1, "DisplacementY"); 131 CHKERRQ(ierr); 132 ierr = PetscSectionSetComponentName(section, 0, 2, "DisplacementZ"); 133 CHKERRQ(ierr); 134 } 135 136 // -- Setup postprocessing DMs 137 ierr = DMClone(dmOrig, &dmEnergy); CHKERRQ(ierr); 138 ierr = SetupDMByDegree(dmEnergy, appCtx, appCtx->levelDegrees[fineLevel], 139 PETSC_FALSE, ncompe); CHKERRQ(ierr); 140 ierr = DMClone(dmOrig, &dmDiagnostic); CHKERRQ(ierr); 141 ierr = SetupDMByDegree(dmDiagnostic, appCtx, appCtx->levelDegrees[fineLevel], 142 PETSC_FALSE, ncompu + ncompd); CHKERRQ(ierr); 143 { 144 // -- Label field components for viewing 145 // Empty name for conserved field (because there is only one field) 146 PetscSection section; 147 ierr = DMGetLocalSection(dmDiagnostic, §ion); CHKERRQ(ierr); 148 ierr = PetscSectionSetFieldName(section, 0, "Diagnostics"); CHKERRQ(ierr); 149 ierr = PetscSectionSetComponentName(section, 0, 0, "DisplacementX"); 150 CHKERRQ(ierr); 151 ierr = PetscSectionSetComponentName(section, 0, 1, "DisplacementY"); 152 CHKERRQ(ierr); 153 ierr = PetscSectionSetComponentName(section, 0, 2, "DisplacementZ"); 154 CHKERRQ(ierr); 155 ierr = PetscSectionSetComponentName(section, 0, 3, "CondensedPressure"); 156 CHKERRQ(ierr); 157 ierr = PetscSectionSetComponentName(section, 0, 4, "TraceE"); 158 CHKERRQ(ierr); 159 ierr = PetscSectionSetComponentName(section, 0, 5, "TraceE2"); 160 CHKERRQ(ierr); 161 ierr = PetscSectionSetComponentName(section, 0, 6, "detJ"); 162 CHKERRQ(ierr); 163 ierr = PetscSectionSetComponentName(section, 0, 7, "StrainEnergyDensity"); 164 CHKERRQ(ierr); 165 } 166 167 // --------------------------------------------------------------------------- 168 // Setup solution and work vectors 169 // --------------------------------------------------------------------------- 170 // Allocate arrays 171 ierr = PetscMalloc1(numLevels, &Ug); CHKERRQ(ierr); 172 ierr = PetscMalloc1(numLevels, &Uloc); CHKERRQ(ierr); 173 ierr = PetscMalloc1(numLevels, &Ugsz); CHKERRQ(ierr); 174 ierr = PetscMalloc1(numLevels, &Ulsz); CHKERRQ(ierr); 175 ierr = PetscMalloc1(numLevels, &Ulocsz); CHKERRQ(ierr); 176 177 // -- Setup solution vectors for each level 178 for (PetscInt level = 0; level < numLevels; level++) { 179 // -- Create global unknown vector U 180 ierr = DMCreateGlobalVector(levelDMs[level], &Ug[level]); CHKERRQ(ierr); 181 ierr = VecGetSize(Ug[level], &Ugsz[level]); CHKERRQ(ierr); 182 // Note: Local size for matShell 183 ierr = VecGetLocalSize(Ug[level], &Ulsz[level]); CHKERRQ(ierr); 184 185 // -- Create local unknown vector Uloc 186 ierr = DMCreateLocalVector(levelDMs[level], &Uloc[level]); CHKERRQ(ierr); 187 // Note: local size for libCEED 188 ierr = VecGetSize(Uloc[level], &Ulocsz[level]); CHKERRQ(ierr); 189 } 190 191 // -- Create residual and forcing vectors 192 ierr = VecDuplicate(Ug[fineLevel], &U); CHKERRQ(ierr); 193 ierr = VecDuplicate(Ug[fineLevel], &R); CHKERRQ(ierr); 194 ierr = VecDuplicate(Ug[fineLevel], &F); CHKERRQ(ierr); 195 ierr = VecDuplicate(Uloc[fineLevel], &Rloc); CHKERRQ(ierr); 196 ierr = VecDuplicate(Uloc[fineLevel], &Floc); CHKERRQ(ierr); 197 198 // Performance logging 199 ierr = PetscLogStagePop(); 200 201 // --------------------------------------------------------------------------- 202 // Set up libCEED 203 // --------------------------------------------------------------------------- 204 // Performance logging 205 ierr = PetscLogStageRegister("libCEED Setup Stage", &stageLibceedSetup); 206 CHKERRQ(ierr); 207 ierr = PetscLogStagePush(stageLibceedSetup); CHKERRQ(ierr); 208 209 // Initalize backend 210 CeedInit(appCtx->ceedResource, &ceed); 211 if (appCtx->degree > 4 && appCtx->ceedResourceFine[0]) 212 CeedInit(appCtx->ceedResourceFine, &ceedFine); 213 214 // -- Create libCEED local forcing vector 215 CeedVector forceCeed; 216 CeedScalar *f; 217 if (appCtx->forcingChoice != FORCE_NONE) { 218 ierr = VecGetArray(Floc, &f); CHKERRQ(ierr); 219 CeedVectorCreate(ceed, Ulocsz[fineLevel], &forceCeed); 220 CeedVectorSetArray(forceCeed, CEED_MEM_HOST, CEED_USE_POINTER, f); 221 } 222 223 // -- Restriction and prolongation QFunction 224 if (appCtx->multigridChoice != MULTIGRID_NONE) { 225 CeedQFunctionCreateIdentity(ceed, ncompu, CEED_EVAL_NONE, CEED_EVAL_INTERP, 226 &qfRestrict); 227 CeedQFunctionCreateIdentity(ceed, ncompu, CEED_EVAL_INTERP, CEED_EVAL_NONE, 228 &qfProlong); 229 } 230 231 // -- Setup libCEED objects 232 ierr = PetscMalloc1(numLevels, &ceedData); CHKERRQ(ierr); 233 // ---- Setup residual evaluator and geometric information 234 ierr = PetscCalloc1(1, &ceedData[fineLevel]); CHKERRQ(ierr); 235 { 236 bool highOrder = (appCtx->levelDegrees[fineLevel] > 4 && ceedFine); 237 Ceed levelCeed = highOrder ? ceedFine : ceed; 238 ierr = SetupLibceedFineLevel(levelDMs[fineLevel], dmEnergy, dmDiagnostic, 239 levelCeed, appCtx, phys, ceedData, fineLevel, 240 ncompu, Ugsz[fineLevel], Ulocsz[fineLevel], 241 forceCeed, qfRestrict, qfProlong); 242 CHKERRQ(ierr); 243 } 244 // ---- Setup Jacobian evaluator and prolongation/restriction 245 for (PetscInt level = 0; level < numLevels; level++) { 246 if (level != fineLevel) { 247 ierr = PetscCalloc1(1, &ceedData[level]); CHKERRQ(ierr); 248 } 249 250 // Note: use high order ceed, if specified and degree > 4 251 bool highOrder = (appCtx->levelDegrees[level] > 4 && ceedFine); 252 Ceed levelCeed = highOrder ? ceedFine : ceed; 253 ierr = SetupLibceedLevel(levelDMs[level], levelCeed, appCtx, phys, 254 ceedData, level, ncompu, Ugsz[level], 255 Ulocsz[level], forceCeed, qfRestrict, 256 qfProlong); CHKERRQ(ierr); 257 } 258 259 // Performance logging 260 ierr = PetscLogStagePop(); 261 262 // --------------------------------------------------------------------------- 263 // Setup global forcing vector 264 // --------------------------------------------------------------------------- 265 ierr = VecZeroEntries(F); CHKERRQ(ierr); 266 267 if (appCtx->forcingChoice != FORCE_NONE) { 268 ierr = VecRestoreArray(Floc, &f); CHKERRQ(ierr); 269 ierr = DMLocalToGlobal(levelDMs[fineLevel], Floc, ADD_VALUES, F); 270 CHKERRQ(ierr); 271 CeedVectorDestroy(&forceCeed); 272 } 273 274 // --------------------------------------------------------------------------- 275 // Print problem summary 276 // --------------------------------------------------------------------------- 277 if (!appCtx->testMode) { 278 const char *usedresource; 279 CeedGetResource(ceed, &usedresource); 280 281 ierr = PetscPrintf(comm, 282 "\n-- Elasticity Example - libCEED + PETSc --\n" 283 " libCEED:\n" 284 " libCEED Backend : %s\n", 285 usedresource); CHKERRQ(ierr); 286 287 if (ceedFine) { 288 ierr = PetscPrintf(comm, 289 " libCEED Backend - high order : %s\n", 290 appCtx->ceedResourceFine); CHKERRQ(ierr); 291 } 292 293 ierr = PetscPrintf(comm, 294 " Problem:\n" 295 " Problem Name : %s\n" 296 " Forcing Function : %s\n" 297 " Mesh:\n" 298 " File : %s\n" 299 " Number of 1D Basis Nodes (p) : %d\n" 300 " Number of 1D Quadrature Points (q) : %d\n" 301 " Global nodes : %D\n" 302 " Owned nodes : %D\n" 303 " DoF per node : %D\n" 304 " Multigrid:\n" 305 " Type : %s\n" 306 " Number of Levels : %d\n", 307 problemTypesForDisp[appCtx->problemChoice], 308 forcingTypesForDisp[appCtx->forcingChoice], 309 appCtx->meshFile[0] ? appCtx->meshFile : "Box Mesh", 310 appCtx->degree + 1, appCtx->degree + 1, 311 Ugsz[fineLevel]/ncompu, Ulsz[fineLevel]/ncompu, ncompu, 312 (appCtx->degree == 1 && 313 appCtx->multigridChoice != MULTIGRID_NONE) ? 314 "Algebraic multigrid" : 315 multigridTypesForDisp[appCtx->multigridChoice], 316 (appCtx->degree == 1 || 317 appCtx->multigridChoice == MULTIGRID_NONE) ? 318 0 : numLevels); CHKERRQ(ierr); 319 320 if (appCtx->multigridChoice != MULTIGRID_NONE) { 321 for (PetscInt i = 0; i < 2; i++) { 322 CeedInt level = i ? fineLevel : 0; 323 ierr = PetscPrintf(comm, 324 " Level %D (%s):\n" 325 " Number of 1D Basis Nodes (p) : %d\n" 326 " Global Nodes : %D\n" 327 " Owned Nodes : %D\n", 328 level, i ? "fine" : "coarse", 329 appCtx->levelDegrees[level] + 1, 330 Ugsz[level]/ncompu, Ulsz[level]/ncompu); 331 CHKERRQ(ierr); 332 } 333 } 334 } 335 336 // --------------------------------------------------------------------------- 337 // Setup SNES 338 // --------------------------------------------------------------------------- 339 // Performance logging 340 ierr = PetscLogStageRegister("SNES Setup Stage", &stageSnesSetup); 341 CHKERRQ(ierr); 342 ierr = PetscLogStagePush(stageSnesSetup); CHKERRQ(ierr); 343 344 // Create SNES 345 ierr = SNESCreate(comm, &snes); CHKERRQ(ierr); 346 ierr = SNESSetDM(snes, levelDMs[fineLevel]); CHKERRQ(ierr); 347 348 // -- Jacobian evaluators 349 ierr = PetscMalloc1(numLevels, &jacobCtx); CHKERRQ(ierr); 350 ierr = PetscMalloc1(numLevels, &jacobMat); CHKERRQ(ierr); 351 for (PetscInt level = 0; level < numLevels; level++) { 352 // -- Jacobian context for level 353 ierr = PetscMalloc1(1, &jacobCtx[level]); CHKERRQ(ierr); 354 ierr = SetupJacobianCtx(comm, appCtx, levelDMs[level], Ug[level], 355 Uloc[level], ceedData[level], ceed, phys, 356 physSmoother, jacobCtx[level]); CHKERRQ(ierr); 357 358 // -- Form Action of Jacobian on delta_u 359 ierr = MatCreateShell(comm, Ulsz[level], Ulsz[level], Ugsz[level], 360 Ugsz[level], jacobCtx[level], &jacobMat[level]); 361 CHKERRQ(ierr); 362 ierr = MatShellSetOperation(jacobMat[level], MATOP_MULT, 363 (void (*)(void))ApplyJacobian_Ceed); 364 CHKERRQ(ierr); 365 ierr = MatShellSetOperation(jacobMat[level], MATOP_GET_DIAGONAL, 366 (void(*)(void))GetDiag_Ceed); 367 368 } 369 // Note: FormJacobian updates Jacobian matrices on each level 370 // and assembles the Jpre matrix, if needed 371 ierr = PetscMalloc1(1, &formJacobCtx); CHKERRQ(ierr); 372 formJacobCtx->jacobCtx = jacobCtx; 373 formJacobCtx->numLevels = numLevels; 374 formJacobCtx->jacobMat = jacobMat; 375 376 // -- Residual evaluation function 377 ierr = PetscMalloc1(1, &resCtx); CHKERRQ(ierr); 378 ierr = PetscMemcpy(resCtx, jacobCtx[fineLevel], 379 sizeof(*jacobCtx[fineLevel])); CHKERRQ(ierr); 380 resCtx->op = ceedData[fineLevel]->opApply; 381 resCtx->qf = ceedData[fineLevel]->qfApply; 382 ierr = SNESSetFunction(snes, R, FormResidual_Ceed, resCtx); CHKERRQ(ierr); 383 384 // -- Prolongation/Restriction evaluation 385 ierr = PetscMalloc1(numLevels, &prolongRestrCtx); CHKERRQ(ierr); 386 ierr = PetscMalloc1(numLevels, &prolongRestrMat); CHKERRQ(ierr); 387 for (PetscInt level = 1; level < numLevels; level++) { 388 // ---- Prolongation/restriction context for level 389 ierr = PetscMalloc1(1, &prolongRestrCtx[level]); CHKERRQ(ierr); 390 ierr = SetupProlongRestrictCtx(comm, levelDMs[level-1], levelDMs[level], 391 Ug[level], Uloc[level-1], Uloc[level], 392 ceedData[level-1], ceedData[level], ceed, 393 prolongRestrCtx[level]); CHKERRQ(ierr); 394 395 // ---- Form Action of Jacobian on delta_u 396 ierr = MatCreateShell(comm, Ulsz[level], Ulsz[level-1], Ugsz[level], 397 Ugsz[level-1], prolongRestrCtx[level], 398 &prolongRestrMat[level]); CHKERRQ(ierr); 399 // Note: In PCMG, restriction is the transpose of prolongation 400 ierr = MatShellSetOperation(prolongRestrMat[level], MATOP_MULT, 401 (void (*)(void))Prolong_Ceed); 402 ierr = MatShellSetOperation(prolongRestrMat[level], MATOP_MULT_TRANSPOSE, 403 (void (*)(void))Restrict_Ceed); 404 CHKERRQ(ierr); 405 } 406 407 // --------------------------------------------------------------------------- 408 // Setup dummy SNES for AMG coarse solve 409 // --------------------------------------------------------------------------- 410 if (appCtx->multigridChoice != MULTIGRID_NONE) { 411 // -- Jacobian Matrix 412 ierr = DMSetMatType(levelDMs[0], MATAIJ); CHKERRQ(ierr); 413 ierr = DMCreateMatrix(levelDMs[0], &jacobMatCoarse); CHKERRQ(ierr); 414 415 if (appCtx->degree > 1) { 416 ierr = SNESCreate(comm, &snesCoarse); CHKERRQ(ierr); 417 ierr = SNESSetDM(snesCoarse, levelDMs[0]); CHKERRQ(ierr); 418 ierr = SNESSetSolution(snesCoarse, Ug[0]); CHKERRQ(ierr); 419 420 // -- Jacobian function 421 ierr = SNESSetJacobian(snesCoarse, jacobMatCoarse, jacobMatCoarse, NULL, 422 NULL); CHKERRQ(ierr); 423 424 // -- Residual evaluation function 425 ierr = PetscMalloc1(1, &jacobCoarseCtx); CHKERRQ(ierr); 426 ierr = PetscMemcpy(jacobCoarseCtx, jacobCtx[0], sizeof(*jacobCtx[0])); 427 CHKERRQ(ierr); 428 ierr = SNESSetFunction(snesCoarse, Ug[0], ApplyJacobianCoarse_Ceed, 429 jacobCoarseCtx); CHKERRQ(ierr); 430 431 // -- Update formJacobCtx 432 formJacobCtx->Ucoarse = Ug[0]; 433 formJacobCtx->snesCoarse = snesCoarse; 434 formJacobCtx->jacobMatCoarse = jacobMatCoarse; 435 } 436 } 437 438 // Set Jacobian function 439 if (appCtx->degree > 1) { 440 ierr = SNESSetJacobian(snes, jacobMat[fineLevel], jacobMat[fineLevel], 441 FormJacobian, formJacobCtx); CHKERRQ(ierr); 442 } else { 443 ierr = SNESSetJacobian(snes, jacobMat[0], jacobMatCoarse, 444 SNESComputeJacobianDefaultColor, NULL); 445 CHKERRQ(ierr); 446 } 447 448 // --------------------------------------------------------------------------- 449 // Setup KSP 450 // --------------------------------------------------------------------------- 451 { 452 PC pc; 453 KSP ksp; 454 455 // -- KSP 456 ierr = SNESGetKSP(snes, &ksp); CHKERRQ(ierr); 457 ierr = KSPSetType(ksp, KSPCG); CHKERRQ(ierr); 458 ierr = KSPSetNormType(ksp, KSP_NORM_NATURAL); CHKERRQ(ierr); 459 ierr = KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, 460 PETSC_DEFAULT); CHKERRQ(ierr); 461 ierr = KSPSetOptionsPrefix(ksp, "outer_"); CHKERRQ(ierr); 462 463 // -- Preconditioning 464 ierr = KSPGetPC(ksp, &pc); CHKERRQ(ierr); 465 ierr = PCSetDM(pc, levelDMs[fineLevel]); CHKERRQ(ierr); 466 ierr = PCSetOptionsPrefix(pc, "outer_"); CHKERRQ(ierr); 467 468 if (appCtx->multigridChoice == MULTIGRID_NONE) { 469 // ---- No Multigrid 470 ierr = PCSetType(pc, PCJACOBI); CHKERRQ(ierr); 471 ierr = PCJacobiSetType(pc, PC_JACOBI_DIAGONAL); CHKERRQ(ierr); 472 } else if (appCtx->degree == 1) { 473 // ---- AMG for degree 1 474 ierr = PCSetType(pc, PCGAMG); CHKERRQ(ierr); 475 } else { 476 // ---- PCMG 477 ierr = PCSetType(pc, PCMG); CHKERRQ(ierr); 478 479 // ------ PCMG levels 480 ierr = PCMGSetLevels(pc, numLevels, NULL); CHKERRQ(ierr); 481 for (PetscInt level = 0; level < numLevels; level++) { 482 // -------- Smoother 483 KSP kspSmoother, kspEst; 484 PC pcSmoother; 485 486 // ---------- Smoother KSP 487 ierr = PCMGGetSmoother(pc, level, &kspSmoother); CHKERRQ(ierr); 488 ierr = KSPSetDM(kspSmoother, levelDMs[level]); CHKERRQ(ierr); 489 ierr = KSPSetDMActive(kspSmoother, PETSC_FALSE); CHKERRQ(ierr); 490 491 // ---------- Chebyshev options 492 ierr = KSPSetType(kspSmoother, KSPCHEBYSHEV); CHKERRQ(ierr); 493 ierr = KSPChebyshevEstEigSet(kspSmoother, 0, 0.1, 0, 1.1); 494 CHKERRQ(ierr); 495 ierr = KSPChebyshevEstEigGetKSP(kspSmoother, &kspEst); CHKERRQ(ierr); 496 ierr = KSPSetType(kspEst, KSPCG); CHKERRQ(ierr); 497 ierr = KSPChebyshevEstEigSetUseNoisy(kspSmoother, PETSC_TRUE); 498 CHKERRQ(ierr); 499 ierr = KSPSetOperators(kspSmoother, jacobMat[level], jacobMat[level]); 500 CHKERRQ(ierr); 501 502 // ---------- Smoother preconditioner 503 ierr = KSPGetPC(kspSmoother, &pcSmoother); CHKERRQ(ierr); 504 ierr = PCSetType(pcSmoother, PCJACOBI); CHKERRQ(ierr); 505 ierr = PCJacobiSetType(pcSmoother, PC_JACOBI_DIAGONAL); CHKERRQ(ierr); 506 507 // -------- Work vector 508 if (level != fineLevel) { 509 ierr = PCMGSetX(pc, level, Ug[level]); CHKERRQ(ierr); 510 } 511 512 // -------- Level prolongation/restriction operator 513 if (level > 0) { 514 ierr = PCMGSetInterpolation(pc, level, prolongRestrMat[level]); 515 CHKERRQ(ierr); 516 ierr = PCMGSetRestriction(pc, level, prolongRestrMat[level]); 517 CHKERRQ(ierr); 518 } 519 } 520 521 // ------ PCMG coarse solve 522 KSP kspCoarse; 523 PC pcCoarse; 524 525 // -------- Coarse KSP 526 ierr = PCMGGetCoarseSolve(pc, &kspCoarse); CHKERRQ(ierr); 527 ierr = KSPSetType(kspCoarse, KSPPREONLY); CHKERRQ(ierr); 528 ierr = KSPSetOperators(kspCoarse, jacobMatCoarse, jacobMatCoarse); 529 CHKERRQ(ierr); 530 ierr = KSPSetOptionsPrefix(kspCoarse, "coarse_"); CHKERRQ(ierr); 531 532 // -------- Coarse preconditioner 533 ierr = KSPGetPC(kspCoarse, &pcCoarse); CHKERRQ(ierr); 534 ierr = PCSetType(pcCoarse, PCGAMG); CHKERRQ(ierr); 535 ierr = PCSetOptionsPrefix(pcCoarse, "coarse_"); CHKERRQ(ierr); 536 537 ierr = KSPSetFromOptions(kspCoarse); CHKERRQ(ierr); 538 ierr = PCSetFromOptions(pcCoarse); CHKERRQ(ierr); 539 540 // ------ PCMG options 541 ierr = PCMGSetType(pc, PC_MG_MULTIPLICATIVE); CHKERRQ(ierr); 542 ierr = PCMGSetNumberSmooth(pc, 3); CHKERRQ(ierr); 543 ierr = PCMGSetCycleType(pc, pcmgCycleType); CHKERRQ(ierr); 544 } 545 ierr = KSPSetFromOptions(ksp); 546 ierr = PCSetFromOptions(pc); 547 } 548 { 549 // Default to critical-point (CP) line search (related to Wolfe's curvature condition) 550 SNESLineSearch linesearch; 551 552 ierr = SNESGetLineSearch(snes, &linesearch); CHKERRQ(ierr); 553 ierr = SNESLineSearchSetType(linesearch, SNESLINESEARCHCP); CHKERRQ(ierr); 554 } 555 556 ierr = SNESSetFromOptions(snes); CHKERRQ(ierr); 557 558 // Performance logging 559 ierr = PetscLogStagePop(); 560 561 // --------------------------------------------------------------------------- 562 // Set initial guess 563 // --------------------------------------------------------------------------- 564 ierr = VecSet(U, 0.0); CHKERRQ(ierr); 565 566 // View solution 567 if (appCtx->viewSoln) { 568 ierr = ViewSolution(comm, U, 0, 0.0); CHKERRQ(ierr); 569 } 570 571 // --------------------------------------------------------------------------- 572 // Solve SNES 573 // --------------------------------------------------------------------------- 574 PetscBool snesMonitor = PETSC_FALSE; 575 ierr = PetscOptionsHasName(NULL, NULL, "-snes_monitor", &snesMonitor); 576 CHKERRQ(ierr); 577 578 // Performance logging 579 ierr = PetscLogStageRegister("SNES Solve Stage", &stageSnesSolve); 580 CHKERRQ(ierr); 581 ierr = PetscLogStagePush(stageSnesSolve); CHKERRQ(ierr); 582 583 // Timing 584 ierr = PetscBarrier((PetscObject)snes); CHKERRQ(ierr); 585 startTime = MPI_Wtime(); 586 587 // Solve for each load increment 588 PetscInt increment; 589 for (increment = 1; increment <= appCtx->numIncrements; increment++) { 590 // -- Log increment count 591 if (snesMonitor) { 592 ierr = PetscPrintf(comm, "%d Load Increment\n", increment - 1); 593 CHKERRQ(ierr); 594 } 595 596 // -- Scale the problem 597 PetscScalar loadIncrement = 1.0*increment / appCtx->numIncrements, 598 scalingFactor = loadIncrement / 599 (increment == 1 ? 1 : resCtx->loadIncrement); 600 resCtx->loadIncrement = loadIncrement; 601 if (appCtx->numIncrements > 1 && appCtx->forcingChoice != FORCE_NONE) { 602 ierr = VecScale(F, scalingFactor); CHKERRQ(ierr); 603 } 604 605 // -- Solve 606 ierr = SNESSolve(snes, F, U); CHKERRQ(ierr); 607 608 // -- View solution 609 if (appCtx->viewSoln) { 610 ierr = ViewSolution(comm, U, increment, loadIncrement); CHKERRQ(ierr); 611 } 612 613 // -- Update SNES iteration count 614 PetscInt its; 615 ierr = SNESGetIterationNumber(snes, &its); CHKERRQ(ierr); 616 snesIts += its; 617 618 // -- Check for divergence 619 SNESConvergedReason reason; 620 ierr = SNESGetConvergedReason(snes, &reason); CHKERRQ(ierr); 621 if (reason < 0) 622 break; 623 } 624 625 // Timing 626 elapsedTime = MPI_Wtime() - startTime; 627 628 // Performance logging 629 ierr = PetscLogStagePop(); 630 631 // --------------------------------------------------------------------------- 632 // Output summary 633 // --------------------------------------------------------------------------- 634 if (!appCtx->testMode) { 635 // -- SNES 636 SNESType snesType; 637 SNESConvergedReason reason; 638 PetscReal rnorm; 639 ierr = SNESGetType(snes, &snesType); CHKERRQ(ierr); 640 ierr = SNESGetConvergedReason(snes, &reason); CHKERRQ(ierr); 641 ierr = SNESGetFunctionNorm(snes, &rnorm); CHKERRQ(ierr); 642 ierr = PetscPrintf(comm, 643 " SNES:\n" 644 " SNES Type : %s\n" 645 " SNES Convergence : %s\n" 646 " Number of Load Increments : %d\n" 647 " Completed Load Increments : %d\n" 648 " Total SNES Iterations : %D\n" 649 " Final rnorm : %e\n", 650 snesType, SNESConvergedReasons[reason], 651 appCtx->numIncrements, increment - 1, 652 snesIts, (double)rnorm); CHKERRQ(ierr); 653 654 // -- KSP 655 KSP ksp; 656 KSPType kspType; 657 ierr = SNESGetKSP(snes, &ksp); CHKERRQ(ierr); 658 ierr = KSPGetType(ksp, &kspType); CHKERRQ(ierr); 659 ierr = PetscPrintf(comm, 660 " Linear Solver:\n" 661 " KSP Type : %s\n", 662 kspType); CHKERRQ(ierr); 663 664 // -- PC 665 PC pc; 666 PCType pcType; 667 ierr = KSPGetPC(ksp, &pc); CHKERRQ(ierr); 668 ierr = PCGetType(pc, &pcType); CHKERRQ(ierr); 669 ierr = PetscPrintf(comm, 670 " PC Type : %s\n", 671 pcType); CHKERRQ(ierr); 672 673 if (!strcmp(pcType, PCMG)) { 674 PCMGType pcmgType; 675 ierr = PCMGGetType(pc, &pcmgType); CHKERRQ(ierr); 676 ierr = PetscPrintf(comm, 677 " P-Multigrid:\n" 678 " PCMG Type : %s\n" 679 " PCMG Cycle Type : %s\n", 680 PCMGTypes[pcmgType], 681 PCMGCycleTypes[pcmgCycleType]); CHKERRQ(ierr); 682 683 // -- Coarse Solve 684 KSP kspCoarse; 685 PC pcCoarse; 686 PCType pcType; 687 688 ierr = PCMGGetCoarseSolve(pc, &kspCoarse); CHKERRQ(ierr); 689 ierr = KSPGetType(kspCoarse, &kspType); CHKERRQ(ierr); 690 ierr = KSPGetPC(kspCoarse, &pcCoarse); CHKERRQ(ierr); 691 ierr = PCGetType(pcCoarse, &pcType); CHKERRQ(ierr); 692 ierr = PetscPrintf(comm, 693 " Coarse Solve:\n" 694 " KSP Type : %s\n" 695 " PC Type : %s\n", 696 kspType, pcType); CHKERRQ(ierr); 697 } 698 } 699 700 // --------------------------------------------------------------------------- 701 // Compute solve time 702 // --------------------------------------------------------------------------- 703 if (!appCtx->testMode) { 704 ierr = MPI_Allreduce(&elapsedTime, &minTime, 1, MPI_DOUBLE, MPI_MIN, comm); 705 CHKERRQ(ierr); 706 ierr = MPI_Allreduce(&elapsedTime, &maxTime, 1, MPI_DOUBLE, MPI_MAX, comm); 707 CHKERRQ(ierr); 708 ierr = PetscPrintf(comm, 709 " Performance:\n" 710 " SNES Solve Time : %g (%g) sec\n", 711 maxTime, minTime); CHKERRQ(ierr); 712 } 713 714 // --------------------------------------------------------------------------- 715 // Compute error 716 // --------------------------------------------------------------------------- 717 if (appCtx->forcingChoice == FORCE_MMS) { 718 CeedScalar l2Error = 1., l2Unorm = 1.; 719 const CeedScalar *truearray; 720 Vec errorVec, trueVec; 721 722 // -- Work vectors 723 ierr = VecDuplicate(U, &errorVec); CHKERRQ(ierr); 724 ierr = VecSet(errorVec, 0.0); CHKERRQ(ierr); 725 ierr = VecDuplicate(U, &trueVec); CHKERRQ(ierr); 726 ierr = VecSet(trueVec, 0.0); CHKERRQ(ierr); 727 728 // -- Assemble global true solution vector 729 CeedVectorGetArrayRead(ceedData[fineLevel]->truesoln, CEED_MEM_HOST, 730 &truearray); 731 ierr = VecPlaceArray(resCtx->Yloc, truearray); CHKERRQ(ierr); 732 ierr = DMLocalToGlobal(resCtx->dm, resCtx->Yloc, INSERT_VALUES, trueVec); 733 CHKERRQ(ierr); 734 ierr = VecResetArray(resCtx->Yloc); CHKERRQ(ierr); 735 CeedVectorRestoreArrayRead(ceedData[fineLevel]->truesoln, &truearray); 736 737 // -- Compute L2 error 738 ierr = VecWAXPY(errorVec, -1.0, U, trueVec); CHKERRQ(ierr); 739 ierr = VecNorm(errorVec, NORM_2, &l2Error); CHKERRQ(ierr); 740 ierr = VecNorm(U, NORM_2, &l2Unorm); CHKERRQ(ierr); 741 l2Error /= l2Unorm; 742 743 // -- Output 744 if (!appCtx->testMode || l2Error > 0.05) { 745 ierr = PetscPrintf(comm, 746 " L2 Error : %e\n", 747 l2Error); CHKERRQ(ierr); 748 } 749 750 // -- Cleanup 751 ierr = VecDestroy(&errorVec); CHKERRQ(ierr); 752 ierr = VecDestroy(&trueVec); CHKERRQ(ierr); 753 } 754 755 // --------------------------------------------------------------------------- 756 // Compute energy 757 // --------------------------------------------------------------------------- 758 if (!appCtx->testMode) { 759 // -- Compute L2 error 760 CeedScalar energy; 761 ierr = ComputeStrainEnergy(dmEnergy, resCtx, ceedData[fineLevel]->opEnergy, 762 U, &energy); CHKERRQ(ierr); 763 764 // -- Output 765 ierr = PetscPrintf(comm, 766 " Strain Energy : %e\n", 767 energy); CHKERRQ(ierr); 768 } 769 770 // --------------------------------------------------------------------------- 771 // Output diagnostic quantities 772 // --------------------------------------------------------------------------- 773 if (appCtx->viewSoln || appCtx->viewFinalSoln) { 774 // -- Setup context 775 UserMult diagnosticCtx; 776 ierr = PetscMalloc1(1, &diagnosticCtx); CHKERRQ(ierr); 777 ierr = PetscMemcpy(diagnosticCtx, resCtx, sizeof(*resCtx)); CHKERRQ(ierr); 778 diagnosticCtx->dm = dmDiagnostic; 779 diagnosticCtx->op = ceedData[fineLevel]->opDiagnostic; 780 781 // -- Compute and output 782 ierr = ViewDiagnosticQuantities(comm, levelDMs[fineLevel], diagnosticCtx, U, 783 ceedData[fineLevel]->ErestrictDiagnostic); 784 CHKERRQ(ierr); 785 786 // -- Cleanup 787 ierr = PetscFree(diagnosticCtx); CHKERRQ(ierr); 788 } 789 790 // --------------------------------------------------------------------------- 791 // Free objects 792 // --------------------------------------------------------------------------- 793 // Data in arrays per level 794 for (PetscInt level = 0; level < numLevels; level++) { 795 // Vectors 796 ierr = VecDestroy(&Ug[level]); CHKERRQ(ierr); 797 ierr = VecDestroy(&Uloc[level]); CHKERRQ(ierr); 798 799 // Jacobian matrix and data 800 ierr = VecDestroy(&jacobCtx[level]->Yloc); CHKERRQ(ierr); 801 ierr = MatDestroy(&jacobMat[level]); CHKERRQ(ierr); 802 ierr = PetscFree(jacobCtx[level]); CHKERRQ(ierr); 803 804 // Prolongation/Restriction matrix and data 805 if (level > 0) { 806 ierr = VecDestroy(&prolongRestrCtx[level]->multVec); CHKERRQ(ierr); 807 ierr = PetscFree(prolongRestrCtx[level]); CHKERRQ(ierr); 808 ierr = MatDestroy(&prolongRestrMat[level]); CHKERRQ(ierr); 809 } 810 811 // DM 812 ierr = DMDestroy(&levelDMs[level]); CHKERRQ(ierr); 813 814 // libCEED objects 815 ierr = CeedDataDestroy(level, ceedData[level]); CHKERRQ(ierr); 816 } 817 818 // Arrays 819 ierr = PetscFree(Ug); CHKERRQ(ierr); 820 ierr = PetscFree(Uloc); CHKERRQ(ierr); 821 ierr = PetscFree(Ugsz); CHKERRQ(ierr); 822 ierr = PetscFree(Ulsz); CHKERRQ(ierr); 823 ierr = PetscFree(Ulocsz); CHKERRQ(ierr); 824 ierr = PetscFree(jacobCtx); CHKERRQ(ierr); 825 ierr = PetscFree(jacobMat); CHKERRQ(ierr); 826 ierr = PetscFree(prolongRestrCtx); CHKERRQ(ierr); 827 ierr = PetscFree(prolongRestrMat); CHKERRQ(ierr); 828 ierr = PetscFree(appCtx->levelDegrees); CHKERRQ(ierr); 829 ierr = PetscFree(ceedData); CHKERRQ(ierr); 830 831 // libCEED objects 832 CeedQFunctionDestroy(&qfRestrict); 833 CeedQFunctionDestroy(&qfProlong); 834 CeedDestroy(&ceed); 835 CeedDestroy(&ceedFine); 836 837 // PETSc objects 838 ierr = VecDestroy(&U); CHKERRQ(ierr); 839 ierr = VecDestroy(&R); CHKERRQ(ierr); 840 ierr = VecDestroy(&Rloc); CHKERRQ(ierr); 841 ierr = VecDestroy(&F); CHKERRQ(ierr); 842 ierr = VecDestroy(&Floc); CHKERRQ(ierr); 843 ierr = MatDestroy(&jacobMatCoarse); CHKERRQ(ierr); 844 ierr = SNESDestroy(&snes); CHKERRQ(ierr); 845 ierr = SNESDestroy(&snesCoarse); CHKERRQ(ierr); 846 ierr = DMDestroy(&dmOrig); CHKERRQ(ierr); 847 ierr = DMDestroy(&dmEnergy); CHKERRQ(ierr); 848 ierr = DMDestroy(&dmDiagnostic); CHKERRQ(ierr); 849 ierr = PetscFree(levelDMs); CHKERRQ(ierr); 850 851 // Structs 852 ierr = PetscFree(resCtx); CHKERRQ(ierr); 853 ierr = PetscFree(formJacobCtx); CHKERRQ(ierr); 854 ierr = PetscFree(jacobCoarseCtx); CHKERRQ(ierr); 855 ierr = PetscFree(appCtx); CHKERRQ(ierr); 856 ierr = PetscFree(phys); CHKERRQ(ierr); 857 ierr = PetscFree(physSmoother); CHKERRQ(ierr); 858 ierr = PetscFree(units); CHKERRQ(ierr); 859 860 return PetscFinalize(); 861 } 862