1ccaff030SJeremy L Thompson // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at 2ccaff030SJeremy L Thompson // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights 3ccaff030SJeremy L Thompson // reserved. See files LICENSE and NOTICE for details. 4ccaff030SJeremy L Thompson // 5ccaff030SJeremy L Thompson // This file is part of CEED, a collection of benchmarks, miniapps, software 6ccaff030SJeremy L Thompson // libraries and APIs for efficient high-order finite element and spectral 7ccaff030SJeremy L Thompson // element discretizations for exascale applications. For more information and 8ccaff030SJeremy L Thompson // source code availability see http://github.com/ceed. 9ccaff030SJeremy L Thompson // 10ccaff030SJeremy L Thompson // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC, 11ccaff030SJeremy L Thompson // a collaborative effort of two U.S. Department of Energy organizations (Office 12ccaff030SJeremy L Thompson // of Science and the National Nuclear Security Administration) responsible for 13ccaff030SJeremy L Thompson // the planning and preparation of a capable exascale ecosystem, including 14ccaff030SJeremy L Thompson // software, applications, hardware, advanced system engineering and early 15ccaff030SJeremy L Thompson // testbed platforms, in support of the nation's exascale computing imperative. 16ccaff030SJeremy L Thompson 17ccaff030SJeremy L Thompson // libCEED + PETSc Example: Elasticity 18ccaff030SJeremy L Thompson // 19ccaff030SJeremy L Thompson // This example demonstrates a simple usage of libCEED with PETSc to solve 20ccaff030SJeremy L Thompson // elasticity problems. 21ccaff030SJeremy L Thompson // 22ccaff030SJeremy L Thompson // The code uses higher level communication protocols in DMPlex. 23ccaff030SJeremy L Thompson // 24ccaff030SJeremy L Thompson // Build with: 25ccaff030SJeremy L Thompson // 26ccaff030SJeremy L Thompson // make elasticity [PETSC_DIR=</path/to/petsc>] [CEED_DIR=</path/to/libceed>] 27ccaff030SJeremy L Thompson // 28ccaff030SJeremy L Thompson // Sample runs: 29ccaff030SJeremy L Thompson // 30ccaff030SJeremy L Thompson // ./elasticity -mesh [.exo file] -degree 2 -E 1 -nu 0.3 -problem linElas -forcing mms 31aee2786aSjeremylt // ./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 32b04a8a52SLeila Ghaffari // ./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 33ccaff030SJeremy L Thompson // 34ccaff030SJeremy L Thompson // Sample meshes can be found at https://github.com/jeremylt/ceedSampleMeshes 35ccaff030SJeremy L Thompson // 36da5d3694SJeremy L Thompson //TESTARGS -ceed {ceed_resource} -test -degree 3 -nu 0.3 -E 1 -dm_plex_box_faces 3,3,3 37ccaff030SJeremy L Thompson 38ccaff030SJeremy L Thompson /// @file 39ccaff030SJeremy L Thompson /// CEED elasticity example using PETSc with DMPlex 40ccaff030SJeremy L Thompson 41ccaff030SJeremy L Thompson const char help[] = "Solve solid Problems with CEED and PETSc DMPlex\n"; 42ccaff030SJeremy L Thompson 43ccaff030SJeremy L Thompson #include "elasticity.h" 44ccaff030SJeremy L Thompson 45ccaff030SJeremy L Thompson int main(int argc, char **argv) { 46ccaff030SJeremy L Thompson PetscInt ierr; 47ccaff030SJeremy L Thompson MPI_Comm comm; 48ccaff030SJeremy L Thompson // Context structs 49ccaff030SJeremy L Thompson AppCtx appCtx; // Contains problem options 50ccaff030SJeremy L Thompson Physics phys; // Contains physical constants 51f7b4142eSJeremy L Thompson Physics physSmoother = NULL; // Separate context if nuSmoother set 52ccaff030SJeremy L Thompson Units units; // Contains units scaling 53ccaff030SJeremy L Thompson // PETSc objects 54ccaff030SJeremy L Thompson PetscLogStage stageDMSetup, stageLibceedSetup, 55ccaff030SJeremy L Thompson stageSnesSetup, stageSnesSolve; 56ccaff030SJeremy L Thompson DM dmOrig; // Distributed DM to clone 57a3c02c40SJeremy L Thompson DM dmEnergy, dmDiagnostic; // DMs for postprocessing 58ccaff030SJeremy L Thompson DM *levelDMs; 59ccaff030SJeremy L Thompson Vec U, *Ug, *Uloc; // U: solution, R: residual, F: forcing 60ccaff030SJeremy L Thompson Vec R, Rloc, F, Floc; // g: global, loc: local 61e3e3df41Sjeremylt SNES snes, snesCoarse = NULL; 62ccaff030SJeremy L Thompson Mat *jacobMat, jacobMatCoarse, *prolongRestrMat; 63ccaff030SJeremy L Thompson // PETSc data 64e3e3df41Sjeremylt UserMult resCtx, jacobCoarseCtx = NULL, *jacobCtx; 65ccaff030SJeremy L Thompson FormJacobCtx formJacobCtx; 66ccaff030SJeremy L Thompson UserMultProlongRestr *prolongRestrCtx; 67ccaff030SJeremy L Thompson PCMGCycleType pcmgCycleType = PC_MG_CYCLE_V; 68ccaff030SJeremy L Thompson // libCEED objects 69ccaff030SJeremy L Thompson Ceed ceed, ceedFine = NULL; 70ccaff030SJeremy L Thompson CeedData *ceedData; 71f892e6d1Sjeremylt CeedQFunction qfRestrict = NULL, qfProlong = NULL; 72ccaff030SJeremy L Thompson // Parameters 73ccaff030SJeremy L Thompson PetscInt ncompu = 3; // 3 DoFs in 3D 7413fdad4bSJeremy L Thompson PetscInt ncompe = 1, ncompd = 5; // 1 energy output, 5 diagnostic 75ccaff030SJeremy L Thompson PetscInt numLevels = 1, fineLevel = 0; 76ccaff030SJeremy L Thompson PetscInt *Ugsz, *Ulsz, *Ulocsz; // sz: size 77*7418ca88SJeremy L Thompson PetscInt snesIts = 0, kspIts = 0; 78ccaff030SJeremy L Thompson // Timing 79ccaff030SJeremy L Thompson double startTime, elapsedTime, minTime, maxTime; 80ccaff030SJeremy L Thompson 81ccaff030SJeremy L Thompson ierr = PetscInitialize(&argc, &argv, NULL, help); 82ccaff030SJeremy L Thompson if (ierr) 83ccaff030SJeremy L Thompson return ierr; 84ccaff030SJeremy L Thompson 85ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 86ccaff030SJeremy L Thompson // Process command line options 87ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 88ccaff030SJeremy L Thompson comm = PETSC_COMM_WORLD; 89ccaff030SJeremy L Thompson 90ccaff030SJeremy L Thompson // -- Set mesh file, polynomial degree, problem type 91ccaff030SJeremy L Thompson ierr = PetscCalloc1(1, &appCtx); CHKERRQ(ierr); 92ccaff030SJeremy L Thompson ierr = ProcessCommandLineOptions(comm, appCtx); CHKERRQ(ierr); 93ccaff030SJeremy L Thompson numLevels = appCtx->numLevels; 94ccaff030SJeremy L Thompson fineLevel = numLevels - 1; 95ccaff030SJeremy L Thompson 96ccaff030SJeremy L Thompson // -- Set Poison's ratio, Young's Modulus 97ccaff030SJeremy L Thompson ierr = PetscMalloc1(1, &phys); CHKERRQ(ierr); 98ccaff030SJeremy L Thompson ierr = PetscMalloc1(1, &units); CHKERRQ(ierr); 99ccaff030SJeremy L Thompson ierr = ProcessPhysics(comm, phys, units); CHKERRQ(ierr); 100f7b4142eSJeremy L Thompson if (fabs(appCtx->nuSmoother) > 1E-14) { 101f7b4142eSJeremy L Thompson ierr = PetscMalloc1(1, &physSmoother); CHKERRQ(ierr); 102f7b4142eSJeremy L Thompson ierr = PetscMemcpy(physSmoother, phys, sizeof(*phys)); CHKERRQ(ierr); 103f7b4142eSJeremy L Thompson physSmoother->nu = appCtx->nuSmoother; 104f7b4142eSJeremy L Thompson } 105ccaff030SJeremy L Thompson 106ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 10762e9c006SJeremy L Thompson // Initalize libCEED 10862e9c006SJeremy L Thompson // --------------------------------------------------------------------------- 10962e9c006SJeremy L Thompson // Initalize backend 11062e9c006SJeremy L Thompson CeedInit(appCtx->ceedResource, &ceed); 11162e9c006SJeremy L Thompson if (appCtx->degree > 4 && appCtx->ceedResourceFine[0]) 11262e9c006SJeremy L Thompson CeedInit(appCtx->ceedResourceFine, &ceedFine); 11362e9c006SJeremy L Thompson 11462e9c006SJeremy L Thompson // Check preferred MemType 11562e9c006SJeremy L Thompson CeedMemType memTypeBackend; 11662e9c006SJeremy L Thompson CeedGetPreferredMemType(ceed, &memTypeBackend); 11762e9c006SJeremy L Thompson if (!appCtx->setMemTypeRequest) 11862e9c006SJeremy L Thompson appCtx->memTypeRequested = memTypeBackend; 11962e9c006SJeremy L Thompson else if (!appCtx->petscHaveCuda && appCtx->memTypeRequested == CEED_MEM_DEVICE) 12062e9c006SJeremy L Thompson SETERRQ1(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS, 12162e9c006SJeremy L Thompson "PETSc was not built with CUDA. " 12262e9c006SJeremy L Thompson "Requested MemType CEED_MEM_DEVICE is not supported.", NULL); 12362e9c006SJeremy L Thompson 12462e9c006SJeremy L Thompson // --------------------------------------------------------------------------- 125ccaff030SJeremy L Thompson // Setup DM 126ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 127ccaff030SJeremy L Thompson // Performance logging 128ccaff030SJeremy L Thompson ierr = PetscLogStageRegister("DM and Vector Setup Stage", &stageDMSetup); 129ccaff030SJeremy L Thompson CHKERRQ(ierr); 130ccaff030SJeremy L Thompson ierr = PetscLogStagePush(stageDMSetup); CHKERRQ(ierr); 131ccaff030SJeremy L Thompson 132ccaff030SJeremy L Thompson // -- Create distributed DM from mesh file 133ccaff030SJeremy L Thompson ierr = CreateDistributedDM(comm, appCtx, &dmOrig); CHKERRQ(ierr); 134ccaff030SJeremy L Thompson 135ccaff030SJeremy L Thompson // -- Setup DM by polynomial degree 136ccaff030SJeremy L Thompson ierr = PetscMalloc1(numLevels, &levelDMs); CHKERRQ(ierr); 137e3e3df41Sjeremylt for (PetscInt level = 0; level < numLevels; level++) { 138ccaff030SJeremy L Thompson ierr = DMClone(dmOrig, &levelDMs[level]); CHKERRQ(ierr); 13962e9c006SJeremy L Thompson if (appCtx->memTypeRequested == CEED_MEM_DEVICE) { 14062e9c006SJeremy L Thompson ierr = DMSetVecType(levelDMs[level], VECCUDA); CHKERRQ(ierr); 14162e9c006SJeremy L Thompson } 142ccaff030SJeremy L Thompson ierr = SetupDMByDegree(levelDMs[level], appCtx, appCtx->levelDegrees[level], 143a3c02c40SJeremy L Thompson PETSC_TRUE, ncompu); CHKERRQ(ierr); 144a3c02c40SJeremy L Thompson // -- Label field components for viewing 145a3c02c40SJeremy L Thompson // Empty name for conserved field (because there is only one field) 146a3c02c40SJeremy L Thompson PetscSection section; 147a3c02c40SJeremy L Thompson ierr = DMGetLocalSection(levelDMs[level], §ion); CHKERRQ(ierr); 148a3c02c40SJeremy L Thompson ierr = PetscSectionSetFieldName(section, 0, "Displacement"); CHKERRQ(ierr); 149a3c02c40SJeremy L Thompson ierr = PetscSectionSetComponentName(section, 0, 0, "DisplacementX"); 150a3c02c40SJeremy L Thompson CHKERRQ(ierr); 151a3c02c40SJeremy L Thompson ierr = PetscSectionSetComponentName(section, 0, 1, "DisplacementY"); 152a3c02c40SJeremy L Thompson CHKERRQ(ierr); 153a3c02c40SJeremy L Thompson ierr = PetscSectionSetComponentName(section, 0, 2, "DisplacementZ"); 154a3c02c40SJeremy L Thompson CHKERRQ(ierr); 155a3c02c40SJeremy L Thompson } 156a3c02c40SJeremy L Thompson 157a3c02c40SJeremy L Thompson // -- Setup postprocessing DMs 158a3c02c40SJeremy L Thompson ierr = DMClone(dmOrig, &dmEnergy); CHKERRQ(ierr); 159a3c02c40SJeremy L Thompson ierr = SetupDMByDegree(dmEnergy, appCtx, appCtx->levelDegrees[fineLevel], 1605c25879aSJeremy L Thompson PETSC_FALSE, ncompe); CHKERRQ(ierr); 161a3c02c40SJeremy L Thompson ierr = DMClone(dmOrig, &dmDiagnostic); CHKERRQ(ierr); 162a3c02c40SJeremy L Thompson ierr = SetupDMByDegree(dmDiagnostic, appCtx, appCtx->levelDegrees[fineLevel], 1638ca58ff3SJeremy L Thompson PETSC_FALSE, ncompu + ncompd); CHKERRQ(ierr); 16462e9c006SJeremy L Thompson if (appCtx->memTypeRequested == CEED_MEM_DEVICE) { 16562e9c006SJeremy L Thompson ierr = DMSetVecType(dmEnergy, VECCUDA); CHKERRQ(ierr); 16662e9c006SJeremy L Thompson ierr = DMSetVecType(dmDiagnostic, VECCUDA); CHKERRQ(ierr); 16762e9c006SJeremy L Thompson } 168a3c02c40SJeremy L Thompson { 169a3c02c40SJeremy L Thompson // -- Label field components for viewing 170a3c02c40SJeremy L Thompson // Empty name for conserved field (because there is only one field) 171a3c02c40SJeremy L Thompson PetscSection section; 172a3c02c40SJeremy L Thompson ierr = DMGetLocalSection(dmDiagnostic, §ion); CHKERRQ(ierr); 173a3c02c40SJeremy L Thompson ierr = PetscSectionSetFieldName(section, 0, "Diagnostics"); CHKERRQ(ierr); 1748ca58ff3SJeremy L Thompson ierr = PetscSectionSetComponentName(section, 0, 0, "DisplacementX"); 1758ca58ff3SJeremy L Thompson CHKERRQ(ierr); 1768ca58ff3SJeremy L Thompson ierr = PetscSectionSetComponentName(section, 0, 1, "DisplacementY"); 1778ca58ff3SJeremy L Thompson CHKERRQ(ierr); 1788ca58ff3SJeremy L Thompson ierr = PetscSectionSetComponentName(section, 0, 2, "DisplacementZ"); 1798ca58ff3SJeremy L Thompson CHKERRQ(ierr); 180379387d4SJeremy L Thompson ierr = PetscSectionSetComponentName(section, 0, 3, "Pressure"); 1818ca58ff3SJeremy L Thompson CHKERRQ(ierr); 1827ab18febSJeremy L Thompson ierr = PetscSectionSetComponentName(section, 0, 4, "VolumentricStrain"); 18313fdad4bSJeremy L Thompson CHKERRQ(ierr); 18413fdad4bSJeremy L Thompson ierr = PetscSectionSetComponentName(section, 0, 5, "TraceE2"); 18513fdad4bSJeremy L Thompson CHKERRQ(ierr); 18613fdad4bSJeremy L Thompson ierr = PetscSectionSetComponentName(section, 0, 6, "detJ"); 18713fdad4bSJeremy L Thompson CHKERRQ(ierr); 18813fdad4bSJeremy L Thompson ierr = PetscSectionSetComponentName(section, 0, 7, "StrainEnergyDensity"); 189a3c02c40SJeremy L Thompson CHKERRQ(ierr); 190ccaff030SJeremy L Thompson } 191ccaff030SJeremy L Thompson 192ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 193ccaff030SJeremy L Thompson // Setup solution and work vectors 194ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 195ccaff030SJeremy L Thompson // Allocate arrays 196ccaff030SJeremy L Thompson ierr = PetscMalloc1(numLevels, &Ug); CHKERRQ(ierr); 197ccaff030SJeremy L Thompson ierr = PetscMalloc1(numLevels, &Uloc); CHKERRQ(ierr); 198ccaff030SJeremy L Thompson ierr = PetscMalloc1(numLevels, &Ugsz); CHKERRQ(ierr); 199ccaff030SJeremy L Thompson ierr = PetscMalloc1(numLevels, &Ulsz); CHKERRQ(ierr); 200ccaff030SJeremy L Thompson ierr = PetscMalloc1(numLevels, &Ulocsz); CHKERRQ(ierr); 201ccaff030SJeremy L Thompson 202ccaff030SJeremy L Thompson // -- Setup solution vectors for each level 203e3e3df41Sjeremylt for (PetscInt level = 0; level < numLevels; level++) { 204ccaff030SJeremy L Thompson // -- Create global unknown vector U 205ccaff030SJeremy L Thompson ierr = DMCreateGlobalVector(levelDMs[level], &Ug[level]); CHKERRQ(ierr); 206ccaff030SJeremy L Thompson ierr = VecGetSize(Ug[level], &Ugsz[level]); CHKERRQ(ierr); 207ccaff030SJeremy L Thompson // Note: Local size for matShell 208ccaff030SJeremy L Thompson ierr = VecGetLocalSize(Ug[level], &Ulsz[level]); CHKERRQ(ierr); 209ccaff030SJeremy L Thompson 210ccaff030SJeremy L Thompson // -- Create local unknown vector Uloc 211ccaff030SJeremy L Thompson ierr = DMCreateLocalVector(levelDMs[level], &Uloc[level]); CHKERRQ(ierr); 212ccaff030SJeremy L Thompson // Note: local size for libCEED 213ccaff030SJeremy L Thompson ierr = VecGetSize(Uloc[level], &Ulocsz[level]); CHKERRQ(ierr); 214ccaff030SJeremy L Thompson } 215ccaff030SJeremy L Thompson 216ccaff030SJeremy L Thompson // -- Create residual and forcing vectors 217ccaff030SJeremy L Thompson ierr = VecDuplicate(Ug[fineLevel], &U); CHKERRQ(ierr); 218ccaff030SJeremy L Thompson ierr = VecDuplicate(Ug[fineLevel], &R); CHKERRQ(ierr); 219ccaff030SJeremy L Thompson ierr = VecDuplicate(Ug[fineLevel], &F); CHKERRQ(ierr); 220ccaff030SJeremy L Thompson ierr = VecDuplicate(Uloc[fineLevel], &Rloc); CHKERRQ(ierr); 221ccaff030SJeremy L Thompson ierr = VecDuplicate(Uloc[fineLevel], &Floc); CHKERRQ(ierr); 222ccaff030SJeremy L Thompson 223ccaff030SJeremy L Thompson // Performance logging 224ccaff030SJeremy L Thompson ierr = PetscLogStagePop(); 225ccaff030SJeremy L Thompson 226ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 227ccaff030SJeremy L Thompson // Set up libCEED 228ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 229ccaff030SJeremy L Thompson // Performance logging 230ccaff030SJeremy L Thompson ierr = PetscLogStageRegister("libCEED Setup Stage", &stageLibceedSetup); 231ccaff030SJeremy L Thompson CHKERRQ(ierr); 232ccaff030SJeremy L Thompson ierr = PetscLogStagePush(stageLibceedSetup); CHKERRQ(ierr); 233ccaff030SJeremy L Thompson 234ccaff030SJeremy L Thompson // -- Create libCEED local forcing vector 235ccaff030SJeremy L Thompson CeedVector forceCeed; 236ccaff030SJeremy L Thompson CeedScalar *f; 237ccaff030SJeremy L Thompson if (appCtx->forcingChoice != FORCE_NONE) { 23862e9c006SJeremy L Thompson if (appCtx->memTypeRequested == CEED_MEM_HOST) { 239ccaff030SJeremy L Thompson ierr = VecGetArray(Floc, &f); CHKERRQ(ierr); 24062e9c006SJeremy L Thompson } else { 24162e9c006SJeremy L Thompson ierr = VecCUDAGetArray(Floc, &f); CHKERRQ(ierr); 24262e9c006SJeremy L Thompson } 243ccaff030SJeremy L Thompson CeedVectorCreate(ceed, Ulocsz[fineLevel], &forceCeed); 24462e9c006SJeremy L Thompson CeedVectorSetArray(forceCeed, appCtx->memTypeRequested, CEED_USE_POINTER, f); 245ccaff030SJeremy L Thompson } 246ccaff030SJeremy L Thompson 247ccaff030SJeremy L Thompson // -- Restriction and prolongation QFunction 248ccaff030SJeremy L Thompson if (appCtx->multigridChoice != MULTIGRID_NONE) { 249ccaff030SJeremy L Thompson CeedQFunctionCreateIdentity(ceed, ncompu, CEED_EVAL_NONE, CEED_EVAL_INTERP, 250ccaff030SJeremy L Thompson &qfRestrict); 251ccaff030SJeremy L Thompson CeedQFunctionCreateIdentity(ceed, ncompu, CEED_EVAL_INTERP, CEED_EVAL_NONE, 252ccaff030SJeremy L Thompson &qfProlong); 253ccaff030SJeremy L Thompson } 254ccaff030SJeremy L Thompson 255ccaff030SJeremy L Thompson // -- Setup libCEED objects 256ccaff030SJeremy L Thompson ierr = PetscMalloc1(numLevels, &ceedData); CHKERRQ(ierr); 257ccaff030SJeremy L Thompson // ---- Setup residual evaluator and geometric information 258ccaff030SJeremy L Thompson ierr = PetscCalloc1(1, &ceedData[fineLevel]); CHKERRQ(ierr); 259ccaff030SJeremy L Thompson { 260ccaff030SJeremy L Thompson bool highOrder = (appCtx->levelDegrees[fineLevel] > 4 && ceedFine); 261ccaff030SJeremy L Thompson Ceed levelCeed = highOrder ? ceedFine : ceed; 262a3c02c40SJeremy L Thompson ierr = SetupLibceedFineLevel(levelDMs[fineLevel], dmEnergy, dmDiagnostic, 263a3c02c40SJeremy L Thompson levelCeed, appCtx, phys, ceedData, fineLevel, 264a3c02c40SJeremy L Thompson ncompu, Ugsz[fineLevel], Ulocsz[fineLevel], 265a3c02c40SJeremy L Thompson forceCeed, qfRestrict, qfProlong); 266ccaff030SJeremy L Thompson CHKERRQ(ierr); 267ccaff030SJeremy L Thompson } 268ccaff030SJeremy L Thompson // ---- Setup Jacobian evaluator and prolongation/restriction 269e3e3df41Sjeremylt for (PetscInt level = 0; level < numLevels; level++) { 270ccaff030SJeremy L Thompson if (level != fineLevel) { 271ccaff030SJeremy L Thompson ierr = PetscCalloc1(1, &ceedData[level]); CHKERRQ(ierr); 272ccaff030SJeremy L Thompson } 273ccaff030SJeremy L Thompson 2741c8142b3Sjeremylt // Note: use high order ceed, if specified and degree > 4 275ccaff030SJeremy L Thompson bool highOrder = (appCtx->levelDegrees[level] > 4 && ceedFine); 276ccaff030SJeremy L Thompson Ceed levelCeed = highOrder ? ceedFine : ceed; 277ccaff030SJeremy L Thompson ierr = SetupLibceedLevel(levelDMs[level], levelCeed, appCtx, phys, 278ccaff030SJeremy L Thompson ceedData, level, ncompu, Ugsz[level], 279ccaff030SJeremy L Thompson Ulocsz[level], forceCeed, qfRestrict, 280ccaff030SJeremy L Thompson qfProlong); CHKERRQ(ierr); 281ccaff030SJeremy L Thompson } 282ccaff030SJeremy L Thompson 283ccaff030SJeremy L Thompson // Performance logging 284ccaff030SJeremy L Thompson ierr = PetscLogStagePop(); 285ccaff030SJeremy L Thompson 286ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 287ccaff030SJeremy L Thompson // Setup global forcing vector 288ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 289ccaff030SJeremy L Thompson ierr = VecZeroEntries(F); CHKERRQ(ierr); 290ccaff030SJeremy L Thompson 291ccaff030SJeremy L Thompson if (appCtx->forcingChoice != FORCE_NONE) { 2926a6c615bSJeremy L Thompson CeedVectorTakeArray(forceCeed, appCtx->memTypeRequested, NULL); 29362e9c006SJeremy L Thompson if (appCtx->memTypeRequested == CEED_MEM_HOST) { 294ccaff030SJeremy L Thompson ierr = VecRestoreArray(Floc, &f); CHKERRQ(ierr); 29562e9c006SJeremy L Thompson } else { 29662e9c006SJeremy L Thompson ierr = VecCUDARestoreArray(Floc, &f); CHKERRQ(ierr); 29762e9c006SJeremy L Thompson } 298ccaff030SJeremy L Thompson ierr = DMLocalToGlobal(levelDMs[fineLevel], Floc, ADD_VALUES, F); 299ccaff030SJeremy L Thompson CHKERRQ(ierr); 300ccaff030SJeremy L Thompson CeedVectorDestroy(&forceCeed); 301ccaff030SJeremy L Thompson } 302ccaff030SJeremy L Thompson 303ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 304ccaff030SJeremy L Thompson // Print problem summary 305ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 306ccaff030SJeremy L Thompson if (!appCtx->testMode) { 307ccaff030SJeremy L Thompson const char *usedresource; 308ccaff030SJeremy L Thompson CeedGetResource(ceed, &usedresource); 309ccaff030SJeremy L Thompson 310ccaff030SJeremy L Thompson ierr = PetscPrintf(comm, 31117fd649aSJeremy L Thompson "\n-- Elasticity Example - libCEED + PETSc --\n" 312ccaff030SJeremy L Thompson " libCEED:\n" 31362e9c006SJeremy L Thompson " libCEED Backend : %s\n" 31462e9c006SJeremy L Thompson " libCEED Backend MemType : %s\n" 31562e9c006SJeremy L Thompson " libCEED User Requested MemType : %s\n", 31662e9c006SJeremy L Thompson usedresource, CeedMemTypes[memTypeBackend], 31762e9c006SJeremy L Thompson (appCtx->setMemTypeRequest) ? 31862e9c006SJeremy L Thompson CeedMemTypes[appCtx->memTypeRequested] : "none"); 31962e9c006SJeremy L Thompson CHKERRQ(ierr); 320ccaff030SJeremy L Thompson 321ccaff030SJeremy L Thompson if (ceedFine) { 322ccaff030SJeremy L Thompson ierr = PetscPrintf(comm, 323ccaff030SJeremy L Thompson " libCEED Backend - high order : %s\n", 324ccaff030SJeremy L Thompson appCtx->ceedResourceFine); CHKERRQ(ierr); 325ccaff030SJeremy L Thompson } 326ccaff030SJeremy L Thompson 32762e9c006SJeremy L Thompson VecType vecType; 32862e9c006SJeremy L Thompson ierr = VecGetType(U, &vecType); CHKERRQ(ierr); 32962e9c006SJeremy L Thompson ierr = PetscPrintf(comm, 33062e9c006SJeremy L Thompson " PETSc:\n" 33162e9c006SJeremy L Thompson " PETSc Vec Type : %s\n", 33262e9c006SJeremy L Thompson vecType); CHKERRQ(ierr); 33362e9c006SJeremy L Thompson 334ccaff030SJeremy L Thompson ierr = PetscPrintf(comm, 335ccaff030SJeremy L Thompson " Problem:\n" 336ccaff030SJeremy L Thompson " Problem Name : %s\n" 337ccaff030SJeremy L Thompson " Forcing Function : %s\n" 338ccaff030SJeremy L Thompson " Mesh:\n" 339ccaff030SJeremy L Thompson " File : %s\n" 340ccaff030SJeremy L Thompson " Number of 1D Basis Nodes (p) : %d\n" 341ccaff030SJeremy L Thompson " Number of 1D Quadrature Points (q) : %d\n" 342ccaff030SJeremy L Thompson " Global nodes : %D\n" 343ccaff030SJeremy L Thompson " Owned nodes : %D\n" 344ccaff030SJeremy L Thompson " DoF per node : %D\n" 345ccaff030SJeremy L Thompson " Multigrid:\n" 346ccaff030SJeremy L Thompson " Type : %s\n" 347ccaff030SJeremy L Thompson " Number of Levels : %d\n", 348ccaff030SJeremy L Thompson problemTypesForDisp[appCtx->problemChoice], 349ccaff030SJeremy L Thompson forcingTypesForDisp[appCtx->forcingChoice], 350ccaff030SJeremy L Thompson appCtx->meshFile[0] ? appCtx->meshFile : "Box Mesh", 351ccaff030SJeremy L Thompson appCtx->degree + 1, appCtx->degree + 1, 352ccaff030SJeremy L Thompson Ugsz[fineLevel]/ncompu, Ulsz[fineLevel]/ncompu, ncompu, 353f892e6d1Sjeremylt (appCtx->degree == 1 && 354f892e6d1Sjeremylt appCtx->multigridChoice != MULTIGRID_NONE) ? 355f892e6d1Sjeremylt "Algebraic multigrid" : 356ccaff030SJeremy L Thompson multigridTypesForDisp[appCtx->multigridChoice], 357f892e6d1Sjeremylt (appCtx->degree == 1 || 358f892e6d1Sjeremylt appCtx->multigridChoice == MULTIGRID_NONE) ? 359f892e6d1Sjeremylt 0 : numLevels); CHKERRQ(ierr); 360ccaff030SJeremy L Thompson 361ccaff030SJeremy L Thompson if (appCtx->multigridChoice != MULTIGRID_NONE) { 362e3e3df41Sjeremylt for (PetscInt i = 0; i < 2; i++) { 363ccaff030SJeremy L Thompson CeedInt level = i ? fineLevel : 0; 364ccaff030SJeremy L Thompson ierr = PetscPrintf(comm, 365ccaff030SJeremy L Thompson " Level %D (%s):\n" 366ccaff030SJeremy L Thompson " Number of 1D Basis Nodes (p) : %d\n" 367ccaff030SJeremy L Thompson " Global Nodes : %D\n" 368ccaff030SJeremy L Thompson " Owned Nodes : %D\n", 369ccaff030SJeremy L Thompson level, i ? "fine" : "coarse", 370ccaff030SJeremy L Thompson appCtx->levelDegrees[level] + 1, 371ccaff030SJeremy L Thompson Ugsz[level]/ncompu, Ulsz[level]/ncompu); 372ccaff030SJeremy L Thompson CHKERRQ(ierr); 373ccaff030SJeremy L Thompson } 374ccaff030SJeremy L Thompson } 375ccaff030SJeremy L Thompson } 376ccaff030SJeremy L Thompson 377ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 378ccaff030SJeremy L Thompson // Setup SNES 379ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 380ccaff030SJeremy L Thompson // Performance logging 381ccaff030SJeremy L Thompson ierr = PetscLogStageRegister("SNES Setup Stage", &stageSnesSetup); 382ccaff030SJeremy L Thompson CHKERRQ(ierr); 383ccaff030SJeremy L Thompson ierr = PetscLogStagePush(stageSnesSetup); CHKERRQ(ierr); 384ccaff030SJeremy L Thompson 385ccaff030SJeremy L Thompson // Create SNES 386ccaff030SJeremy L Thompson ierr = SNESCreate(comm, &snes); CHKERRQ(ierr); 387ccaff030SJeremy L Thompson ierr = SNESSetDM(snes, levelDMs[fineLevel]); CHKERRQ(ierr); 388ccaff030SJeremy L Thompson 389ccaff030SJeremy L Thompson // -- Jacobian evaluators 390ccaff030SJeremy L Thompson ierr = PetscMalloc1(numLevels, &jacobCtx); CHKERRQ(ierr); 391ccaff030SJeremy L Thompson ierr = PetscMalloc1(numLevels, &jacobMat); CHKERRQ(ierr); 392e3e3df41Sjeremylt for (PetscInt level = 0; level < numLevels; level++) { 393ccaff030SJeremy L Thompson // -- Jacobian context for level 394ccaff030SJeremy L Thompson ierr = PetscMalloc1(1, &jacobCtx[level]); CHKERRQ(ierr); 395ccaff030SJeremy L Thompson ierr = SetupJacobianCtx(comm, appCtx, levelDMs[level], Ug[level], 396f7b4142eSJeremy L Thompson Uloc[level], ceedData[level], ceed, phys, 397f7b4142eSJeremy L Thompson physSmoother, jacobCtx[level]); CHKERRQ(ierr); 398ccaff030SJeremy L Thompson 399ccaff030SJeremy L Thompson // -- Form Action of Jacobian on delta_u 400ccaff030SJeremy L Thompson ierr = MatCreateShell(comm, Ulsz[level], Ulsz[level], Ugsz[level], 401ccaff030SJeremy L Thompson Ugsz[level], jacobCtx[level], &jacobMat[level]); 402ccaff030SJeremy L Thompson CHKERRQ(ierr); 403ccaff030SJeremy L Thompson ierr = MatShellSetOperation(jacobMat[level], MATOP_MULT, 404ccaff030SJeremy L Thompson (void (*)(void))ApplyJacobian_Ceed); 405ccaff030SJeremy L Thompson CHKERRQ(ierr); 406ccaff030SJeremy L Thompson ierr = MatShellSetOperation(jacobMat[level], MATOP_GET_DIAGONAL, 407ccaff030SJeremy L Thompson (void(*)(void))GetDiag_Ceed); 408a3658badSJeremy L Thompson if (appCtx->memTypeRequested == CEED_MEM_DEVICE) { 409a3658badSJeremy L Thompson ierr = MatShellSetVecType(jacobMat[level], VECCUDA); CHKERRQ(ierr); 410a3658badSJeremy L Thompson } 411ccaff030SJeremy L Thompson } 412e3e3df41Sjeremylt // Note: FormJacobian updates Jacobian matrices on each level 413ccaff030SJeremy L Thompson // and assembles the Jpre matrix, if needed 414ccaff030SJeremy L Thompson ierr = PetscMalloc1(1, &formJacobCtx); CHKERRQ(ierr); 415ccaff030SJeremy L Thompson formJacobCtx->jacobCtx = jacobCtx; 416ccaff030SJeremy L Thompson formJacobCtx->numLevels = numLevels; 417ccaff030SJeremy L Thompson formJacobCtx->jacobMat = jacobMat; 418ccaff030SJeremy L Thompson 419ccaff030SJeremy L Thompson // -- Residual evaluation function 420ccaff030SJeremy L Thompson ierr = PetscMalloc1(1, &resCtx); CHKERRQ(ierr); 421ccaff030SJeremy L Thompson ierr = PetscMemcpy(resCtx, jacobCtx[fineLevel], 422ccaff030SJeremy L Thompson sizeof(*jacobCtx[fineLevel])); CHKERRQ(ierr); 423ccaff030SJeremy L Thompson resCtx->op = ceedData[fineLevel]->opApply; 424f7b4142eSJeremy L Thompson resCtx->qf = ceedData[fineLevel]->qfApply; 425ccaff030SJeremy L Thompson ierr = SNESSetFunction(snes, R, FormResidual_Ceed, resCtx); CHKERRQ(ierr); 426ccaff030SJeremy L Thompson 427ccaff030SJeremy L Thompson // -- Prolongation/Restriction evaluation 428ccaff030SJeremy L Thompson ierr = PetscMalloc1(numLevels, &prolongRestrCtx); CHKERRQ(ierr); 429ccaff030SJeremy L Thompson ierr = PetscMalloc1(numLevels, &prolongRestrMat); CHKERRQ(ierr); 430e3e3df41Sjeremylt for (PetscInt level = 1; level < numLevels; level++) { 431ccaff030SJeremy L Thompson // ---- Prolongation/restriction context for level 432ccaff030SJeremy L Thompson ierr = PetscMalloc1(1, &prolongRestrCtx[level]); CHKERRQ(ierr); 43362e9c006SJeremy L Thompson ierr = SetupProlongRestrictCtx(comm, appCtx, levelDMs[level-1], 43462e9c006SJeremy L Thompson levelDMs[level], Ug[level], Uloc[level-1], 43562e9c006SJeremy L Thompson Uloc[level], ceedData[level-1], 43662e9c006SJeremy L Thompson ceedData[level], ceed, 437ccaff030SJeremy L Thompson prolongRestrCtx[level]); CHKERRQ(ierr); 438ccaff030SJeremy L Thompson 439ccaff030SJeremy L Thompson // ---- Form Action of Jacobian on delta_u 440ccaff030SJeremy L Thompson ierr = MatCreateShell(comm, Ulsz[level], Ulsz[level-1], Ugsz[level], 441ccaff030SJeremy L Thompson Ugsz[level-1], prolongRestrCtx[level], 442ccaff030SJeremy L Thompson &prolongRestrMat[level]); CHKERRQ(ierr); 443ccaff030SJeremy L Thompson // Note: In PCMG, restriction is the transpose of prolongation 444ccaff030SJeremy L Thompson ierr = MatShellSetOperation(prolongRestrMat[level], MATOP_MULT, 445ccaff030SJeremy L Thompson (void (*)(void))Prolong_Ceed); 446ccaff030SJeremy L Thompson ierr = MatShellSetOperation(prolongRestrMat[level], MATOP_MULT_TRANSPOSE, 447ccaff030SJeremy L Thompson (void (*)(void))Restrict_Ceed); 448ccaff030SJeremy L Thompson CHKERRQ(ierr); 449a3658badSJeremy L Thompson if (appCtx->memTypeRequested == CEED_MEM_DEVICE) { 450a3658badSJeremy L Thompson ierr = MatShellSetVecType(prolongRestrMat[level], VECCUDA); CHKERRQ(ierr); 451a3658badSJeremy L Thompson } 452ccaff030SJeremy L Thompson } 453ccaff030SJeremy L Thompson 454ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 455ccaff030SJeremy L Thompson // Setup dummy SNES for AMG coarse solve 456ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 457e3e3df41Sjeremylt if (appCtx->multigridChoice != MULTIGRID_NONE) { 458e3e3df41Sjeremylt // -- Jacobian Matrix 459e3e3df41Sjeremylt ierr = DMSetMatType(levelDMs[0], MATAIJ); CHKERRQ(ierr); 460e3e3df41Sjeremylt ierr = DMCreateMatrix(levelDMs[0], &jacobMatCoarse); CHKERRQ(ierr); 461e3e3df41Sjeremylt 462e3e3df41Sjeremylt if (appCtx->degree > 1) { 463ccaff030SJeremy L Thompson ierr = SNESCreate(comm, &snesCoarse); CHKERRQ(ierr); 464ccaff030SJeremy L Thompson ierr = SNESSetDM(snesCoarse, levelDMs[0]); CHKERRQ(ierr); 465ccaff030SJeremy L Thompson ierr = SNESSetSolution(snesCoarse, Ug[0]); CHKERRQ(ierr); 466ccaff030SJeremy L Thompson 467e3e3df41Sjeremylt // -- Jacobian function 468ccaff030SJeremy L Thompson ierr = SNESSetJacobian(snesCoarse, jacobMatCoarse, jacobMatCoarse, NULL, 469ccaff030SJeremy L Thompson NULL); CHKERRQ(ierr); 470ccaff030SJeremy L Thompson 471ccaff030SJeremy L Thompson // -- Residual evaluation function 472ccaff030SJeremy L Thompson ierr = PetscMalloc1(1, &jacobCoarseCtx); CHKERRQ(ierr); 473ccaff030SJeremy L Thompson ierr = PetscMemcpy(jacobCoarseCtx, jacobCtx[0], sizeof(*jacobCtx[0])); 474ccaff030SJeremy L Thompson CHKERRQ(ierr); 475ccaff030SJeremy L Thompson ierr = SNESSetFunction(snesCoarse, Ug[0], ApplyJacobianCoarse_Ceed, 476ccaff030SJeremy L Thompson jacobCoarseCtx); CHKERRQ(ierr); 477ccaff030SJeremy L Thompson 478ccaff030SJeremy L Thompson // -- Update formJacobCtx 479ccaff030SJeremy L Thompson formJacobCtx->Ucoarse = Ug[0]; 480ccaff030SJeremy L Thompson formJacobCtx->snesCoarse = snesCoarse; 481ccaff030SJeremy L Thompson formJacobCtx->jacobMatCoarse = jacobMatCoarse; 482e3e3df41Sjeremylt } 483e3e3df41Sjeremylt } 484e3e3df41Sjeremylt 485e3e3df41Sjeremylt // Set Jacobian function 486e3e3df41Sjeremylt if (appCtx->degree > 1) { 487e3e3df41Sjeremylt ierr = SNESSetJacobian(snes, jacobMat[fineLevel], jacobMat[fineLevel], 488e3e3df41Sjeremylt FormJacobian, formJacobCtx); CHKERRQ(ierr); 489e3e3df41Sjeremylt } else { 490e3e3df41Sjeremylt ierr = SNESSetJacobian(snes, jacobMat[0], jacobMatCoarse, 491e3e3df41Sjeremylt SNESComputeJacobianDefaultColor, NULL); 492e3e3df41Sjeremylt CHKERRQ(ierr); 493e3e3df41Sjeremylt } 494ccaff030SJeremy L Thompson 495ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 496ccaff030SJeremy L Thompson // Setup KSP 497ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 498ccaff030SJeremy L Thompson { 499ccaff030SJeremy L Thompson PC pc; 500ccaff030SJeremy L Thompson KSP ksp; 501ccaff030SJeremy L Thompson 502ccaff030SJeremy L Thompson // -- KSP 503ccaff030SJeremy L Thompson ierr = SNESGetKSP(snes, &ksp); CHKERRQ(ierr); 504ccaff030SJeremy L Thompson ierr = KSPSetType(ksp, KSPCG); CHKERRQ(ierr); 505ccaff030SJeremy L Thompson ierr = KSPSetNormType(ksp, KSP_NORM_NATURAL); CHKERRQ(ierr); 506ccaff030SJeremy L Thompson ierr = KSPSetTolerances(ksp, 1e-10, PETSC_DEFAULT, PETSC_DEFAULT, 507ccaff030SJeremy L Thompson PETSC_DEFAULT); CHKERRQ(ierr); 508ccaff030SJeremy L Thompson ierr = KSPSetOptionsPrefix(ksp, "outer_"); CHKERRQ(ierr); 509ccaff030SJeremy L Thompson 510ccaff030SJeremy L Thompson // -- Preconditioning 511ccaff030SJeremy L Thompson ierr = KSPGetPC(ksp, &pc); CHKERRQ(ierr); 512ccaff030SJeremy L Thompson ierr = PCSetDM(pc, levelDMs[fineLevel]); CHKERRQ(ierr); 513ccaff030SJeremy L Thompson ierr = PCSetOptionsPrefix(pc, "outer_"); CHKERRQ(ierr); 514ccaff030SJeremy L Thompson 515ccaff030SJeremy L Thompson if (appCtx->multigridChoice == MULTIGRID_NONE) { 516ccaff030SJeremy L Thompson // ---- No Multigrid 517ccaff030SJeremy L Thompson ierr = PCSetType(pc, PCJACOBI); CHKERRQ(ierr); 518ccaff030SJeremy L Thompson ierr = PCJacobiSetType(pc, PC_JACOBI_DIAGONAL); CHKERRQ(ierr); 519f892e6d1Sjeremylt } else if (appCtx->degree == 1) { 520f892e6d1Sjeremylt // ---- AMG for degree 1 521f892e6d1Sjeremylt ierr = PCSetType(pc, PCGAMG); CHKERRQ(ierr); 522ccaff030SJeremy L Thompson } else { 523ccaff030SJeremy L Thompson // ---- PCMG 524ccaff030SJeremy L Thompson ierr = PCSetType(pc, PCMG); CHKERRQ(ierr); 525ccaff030SJeremy L Thompson 526ccaff030SJeremy L Thompson // ------ PCMG levels 527ccaff030SJeremy L Thompson ierr = PCMGSetLevels(pc, numLevels, NULL); CHKERRQ(ierr); 528e3e3df41Sjeremylt for (PetscInt level = 0; level < numLevels; level++) { 529ccaff030SJeremy L Thompson // -------- Smoother 530ccaff030SJeremy L Thompson KSP kspSmoother, kspEst; 531ccaff030SJeremy L Thompson PC pcSmoother; 532ccaff030SJeremy L Thompson 533ccaff030SJeremy L Thompson // ---------- Smoother KSP 534ccaff030SJeremy L Thompson ierr = PCMGGetSmoother(pc, level, &kspSmoother); CHKERRQ(ierr); 535ccaff030SJeremy L Thompson ierr = KSPSetDM(kspSmoother, levelDMs[level]); CHKERRQ(ierr); 536ccaff030SJeremy L Thompson ierr = KSPSetDMActive(kspSmoother, PETSC_FALSE); CHKERRQ(ierr); 537ccaff030SJeremy L Thompson 538ccaff030SJeremy L Thompson // ---------- Chebyshev options 539ccaff030SJeremy L Thompson ierr = KSPSetType(kspSmoother, KSPCHEBYSHEV); CHKERRQ(ierr); 540ccaff030SJeremy L Thompson ierr = KSPChebyshevEstEigSet(kspSmoother, 0, 0.1, 0, 1.1); 541ccaff030SJeremy L Thompson CHKERRQ(ierr); 542ccaff030SJeremy L Thompson ierr = KSPChebyshevEstEigGetKSP(kspSmoother, &kspEst); CHKERRQ(ierr); 543ccaff030SJeremy L Thompson ierr = KSPSetType(kspEst, KSPCG); CHKERRQ(ierr); 544ccaff030SJeremy L Thompson ierr = KSPChebyshevEstEigSetUseNoisy(kspSmoother, PETSC_TRUE); 545ccaff030SJeremy L Thompson CHKERRQ(ierr); 546ccaff030SJeremy L Thompson ierr = KSPSetOperators(kspSmoother, jacobMat[level], jacobMat[level]); 547ccaff030SJeremy L Thompson CHKERRQ(ierr); 548ccaff030SJeremy L Thompson 549ccaff030SJeremy L Thompson // ---------- Smoother preconditioner 550ccaff030SJeremy L Thompson ierr = KSPGetPC(kspSmoother, &pcSmoother); CHKERRQ(ierr); 551ccaff030SJeremy L Thompson ierr = PCSetType(pcSmoother, PCJACOBI); CHKERRQ(ierr); 552ccaff030SJeremy L Thompson ierr = PCJacobiSetType(pcSmoother, PC_JACOBI_DIAGONAL); CHKERRQ(ierr); 553ccaff030SJeremy L Thompson 554ccaff030SJeremy L Thompson // -------- Work vector 555ccaff030SJeremy L Thompson if (level != fineLevel) { 556ccaff030SJeremy L Thompson ierr = PCMGSetX(pc, level, Ug[level]); CHKERRQ(ierr); 557ccaff030SJeremy L Thompson } 558ccaff030SJeremy L Thompson 559ccaff030SJeremy L Thompson // -------- Level prolongation/restriction operator 560ccaff030SJeremy L Thompson if (level > 0) { 561ccaff030SJeremy L Thompson ierr = PCMGSetInterpolation(pc, level, prolongRestrMat[level]); 562ccaff030SJeremy L Thompson CHKERRQ(ierr); 563ccaff030SJeremy L Thompson ierr = PCMGSetRestriction(pc, level, prolongRestrMat[level]); 564ccaff030SJeremy L Thompson CHKERRQ(ierr); 565ccaff030SJeremy L Thompson } 566ccaff030SJeremy L Thompson } 567ccaff030SJeremy L Thompson 568ccaff030SJeremy L Thompson // ------ PCMG coarse solve 569ccaff030SJeremy L Thompson KSP kspCoarse; 570ccaff030SJeremy L Thompson PC pcCoarse; 571ccaff030SJeremy L Thompson 572ccaff030SJeremy L Thompson // -------- Coarse KSP 573ccaff030SJeremy L Thompson ierr = PCMGGetCoarseSolve(pc, &kspCoarse); CHKERRQ(ierr); 574ccaff030SJeremy L Thompson ierr = KSPSetType(kspCoarse, KSPPREONLY); CHKERRQ(ierr); 575ccaff030SJeremy L Thompson ierr = KSPSetOperators(kspCoarse, jacobMatCoarse, jacobMatCoarse); 576ccaff030SJeremy L Thompson CHKERRQ(ierr); 577ccaff030SJeremy L Thompson ierr = KSPSetOptionsPrefix(kspCoarse, "coarse_"); CHKERRQ(ierr); 578ccaff030SJeremy L Thompson 579ccaff030SJeremy L Thompson // -------- Coarse preconditioner 580ccaff030SJeremy L Thompson ierr = KSPGetPC(kspCoarse, &pcCoarse); CHKERRQ(ierr); 581ccaff030SJeremy L Thompson ierr = PCSetType(pcCoarse, PCGAMG); CHKERRQ(ierr); 582ccaff030SJeremy L Thompson ierr = PCSetOptionsPrefix(pcCoarse, "coarse_"); CHKERRQ(ierr); 583ccaff030SJeremy L Thompson 584ccaff030SJeremy L Thompson ierr = KSPSetFromOptions(kspCoarse); CHKERRQ(ierr); 585ccaff030SJeremy L Thompson ierr = PCSetFromOptions(pcCoarse); CHKERRQ(ierr); 586ccaff030SJeremy L Thompson 587ccaff030SJeremy L Thompson // ------ PCMG options 588ccaff030SJeremy L Thompson ierr = PCMGSetType(pc, PC_MG_MULTIPLICATIVE); CHKERRQ(ierr); 589ccaff030SJeremy L Thompson ierr = PCMGSetNumberSmooth(pc, 3); CHKERRQ(ierr); 590ccaff030SJeremy L Thompson ierr = PCMGSetCycleType(pc, pcmgCycleType); CHKERRQ(ierr); 591ccaff030SJeremy L Thompson } 592ccaff030SJeremy L Thompson ierr = KSPSetFromOptions(ksp); 593ccaff030SJeremy L Thompson ierr = PCSetFromOptions(pc); 594ccaff030SJeremy L Thompson } 595038d0cd7Sjeremylt { 596038d0cd7Sjeremylt // Default to critical-point (CP) line search (related to Wolfe's curvature condition) 597481a4840SJed Brown SNESLineSearch linesearch; 598481a4840SJed Brown 599481a4840SJed Brown ierr = SNESGetLineSearch(snes, &linesearch); CHKERRQ(ierr); 600481a4840SJed Brown ierr = SNESLineSearchSetType(linesearch, SNESLINESEARCHCP); CHKERRQ(ierr); 601481a4840SJed Brown } 602481a4840SJed Brown 603ccaff030SJeremy L Thompson ierr = SNESSetFromOptions(snes); CHKERRQ(ierr); 604ccaff030SJeremy L Thompson 605ccaff030SJeremy L Thompson // Performance logging 606ccaff030SJeremy L Thompson ierr = PetscLogStagePop(); 607ccaff030SJeremy L Thompson 608ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 609ccaff030SJeremy L Thompson // Set initial guess 610ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 611f81c27eaSJed Brown ierr = PetscObjectSetName((PetscObject)U, ""); CHKERRQ(ierr); 612ccaff030SJeremy L Thompson ierr = VecSet(U, 0.0); CHKERRQ(ierr); 613ccaff030SJeremy L Thompson 614ccaff030SJeremy L Thompson // View solution 615ccaff030SJeremy L Thompson if (appCtx->viewSoln) { 616ccaff030SJeremy L Thompson ierr = ViewSolution(comm, U, 0, 0.0); CHKERRQ(ierr); 617ccaff030SJeremy L Thompson } 618ccaff030SJeremy L Thompson 619ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 620ccaff030SJeremy L Thompson // Solve SNES 621ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 6225f24f028Sjeremylt PetscBool snesMonitor = PETSC_FALSE; 6235f24f028Sjeremylt ierr = PetscOptionsHasName(NULL, NULL, "-snes_monitor", &snesMonitor); 6245f24f028Sjeremylt CHKERRQ(ierr); 6255f24f028Sjeremylt 626ccaff030SJeremy L Thompson // Performance logging 627ccaff030SJeremy L Thompson ierr = PetscLogStageRegister("SNES Solve Stage", &stageSnesSolve); 628ccaff030SJeremy L Thompson CHKERRQ(ierr); 629ccaff030SJeremy L Thompson ierr = PetscLogStagePush(stageSnesSolve); CHKERRQ(ierr); 630ccaff030SJeremy L Thompson 631ccaff030SJeremy L Thompson // Timing 632ccaff030SJeremy L Thompson ierr = PetscBarrier((PetscObject)snes); CHKERRQ(ierr); 633ccaff030SJeremy L Thompson startTime = MPI_Wtime(); 634ccaff030SJeremy L Thompson 635ccaff030SJeremy L Thompson // Solve for each load increment 6365f24f028Sjeremylt PetscInt increment; 6375f24f028Sjeremylt for (increment = 1; increment <= appCtx->numIncrements; increment++) { 6385f24f028Sjeremylt // -- Log increment count 6395f24f028Sjeremylt if (snesMonitor) { 6405f24f028Sjeremylt ierr = PetscPrintf(comm, "%d Load Increment\n", increment - 1); 6415f24f028Sjeremylt CHKERRQ(ierr); 6425f24f028Sjeremylt } 6435f24f028Sjeremylt 644ccaff030SJeremy L Thompson // -- Scale the problem 645ccaff030SJeremy L Thompson PetscScalar loadIncrement = 1.0*increment / appCtx->numIncrements, 646ccaff030SJeremy L Thompson scalingFactor = loadIncrement / 647ccaff030SJeremy L Thompson (increment == 1 ? 1 : resCtx->loadIncrement); 648ccaff030SJeremy L Thompson resCtx->loadIncrement = loadIncrement; 649ccaff030SJeremy L Thompson if (appCtx->numIncrements > 1 && appCtx->forcingChoice != FORCE_NONE) { 650ccaff030SJeremy L Thompson ierr = VecScale(F, scalingFactor); CHKERRQ(ierr); 651ccaff030SJeremy L Thompson } 652ccaff030SJeremy L Thompson 653ccaff030SJeremy L Thompson // -- Solve 654ccaff030SJeremy L Thompson ierr = SNESSolve(snes, F, U); CHKERRQ(ierr); 655ccaff030SJeremy L Thompson 656ccaff030SJeremy L Thompson // -- View solution 657560e6f00SJeremy L Thompson if (appCtx->viewSoln) { 658ccaff030SJeremy L Thompson ierr = ViewSolution(comm, U, increment, loadIncrement); CHKERRQ(ierr); 659ccaff030SJeremy L Thompson } 660ccaff030SJeremy L Thompson 661ccaff030SJeremy L Thompson // -- Update SNES iteration count 662ccaff030SJeremy L Thompson PetscInt its; 663ccaff030SJeremy L Thompson ierr = SNESGetIterationNumber(snes, &its); CHKERRQ(ierr); 664ccaff030SJeremy L Thompson snesIts += its; 665*7418ca88SJeremy L Thompson ierr = SNESGetLinearSolveIterations(snes, &its); CHKERRQ(ierr); 666*7418ca88SJeremy L Thompson kspIts += its; 6673951731eSjeremylt 6683951731eSjeremylt // -- Check for divergence 6693951731eSjeremylt SNESConvergedReason reason; 6703951731eSjeremylt ierr = SNESGetConvergedReason(snes, &reason); CHKERRQ(ierr); 6713951731eSjeremylt if (reason < 0) 6723951731eSjeremylt break; 673ccaff030SJeremy L Thompson } 674ccaff030SJeremy L Thompson 675ccaff030SJeremy L Thompson // Timing 676ccaff030SJeremy L Thompson elapsedTime = MPI_Wtime() - startTime; 677ccaff030SJeremy L Thompson 678ccaff030SJeremy L Thompson // Performance logging 679ccaff030SJeremy L Thompson ierr = PetscLogStagePop(); 680ccaff030SJeremy L Thompson 681ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 682ccaff030SJeremy L Thompson // Output summary 683ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 684ccaff030SJeremy L Thompson if (!appCtx->testMode) { 685ccaff030SJeremy L Thompson // -- SNES 686ccaff030SJeremy L Thompson SNESType snesType; 687ccaff030SJeremy L Thompson SNESConvergedReason reason; 688ccaff030SJeremy L Thompson PetscReal rnorm; 689ccaff030SJeremy L Thompson ierr = SNESGetType(snes, &snesType); CHKERRQ(ierr); 690ccaff030SJeremy L Thompson ierr = SNESGetConvergedReason(snes, &reason); CHKERRQ(ierr); 691ccaff030SJeremy L Thompson ierr = SNESGetFunctionNorm(snes, &rnorm); CHKERRQ(ierr); 692ccaff030SJeremy L Thompson ierr = PetscPrintf(comm, 693ccaff030SJeremy L Thompson " SNES:\n" 694ccaff030SJeremy L Thompson " SNES Type : %s\n" 695ccaff030SJeremy L Thompson " SNES Convergence : %s\n" 696ccaff030SJeremy L Thompson " Number of Load Increments : %d\n" 6975f24f028Sjeremylt " Completed Load Increments : %d\n" 698ccaff030SJeremy L Thompson " Total SNES Iterations : %D\n" 699ccaff030SJeremy L Thompson " Final rnorm : %e\n", 700ccaff030SJeremy L Thompson snesType, SNESConvergedReasons[reason], 7015f24f028Sjeremylt appCtx->numIncrements, increment - 1, 7025f24f028Sjeremylt snesIts, (double)rnorm); CHKERRQ(ierr); 703ccaff030SJeremy L Thompson 704ccaff030SJeremy L Thompson // -- KSP 705ccaff030SJeremy L Thompson KSP ksp; 706ccaff030SJeremy L Thompson KSPType kspType; 707ccaff030SJeremy L Thompson ierr = SNESGetKSP(snes, &ksp); CHKERRQ(ierr); 708ccaff030SJeremy L Thompson ierr = KSPGetType(ksp, &kspType); CHKERRQ(ierr); 709ccaff030SJeremy L Thompson ierr = PetscPrintf(comm, 710ccaff030SJeremy L Thompson " Linear Solver:\n" 711*7418ca88SJeremy L Thompson " KSP Type : %s\n" 712*7418ca88SJeremy L Thompson " Total KSP Iterations : %D\n", 713*7418ca88SJeremy L Thompson kspType, kspIts); CHKERRQ(ierr); 714ccaff030SJeremy L Thompson 715ccaff030SJeremy L Thompson // -- PC 716ccaff030SJeremy L Thompson PC pc; 717e3e3df41Sjeremylt PCType pcType; 718ccaff030SJeremy L Thompson ierr = KSPGetPC(ksp, &pc); CHKERRQ(ierr); 719e3e3df41Sjeremylt ierr = PCGetType(pc, &pcType); CHKERRQ(ierr); 720e3e3df41Sjeremylt ierr = PetscPrintf(comm, 721e3e3df41Sjeremylt " PC Type : %s\n", 722e3e3df41Sjeremylt pcType); CHKERRQ(ierr); 723e3e3df41Sjeremylt 7242b901a5eSjeremylt if (!strcmp(pcType, PCMG)) { 725e3e3df41Sjeremylt PCMGType pcmgType; 726ccaff030SJeremy L Thompson ierr = PCMGGetType(pc, &pcmgType); CHKERRQ(ierr); 727ccaff030SJeremy L Thompson ierr = PetscPrintf(comm, 728ccaff030SJeremy L Thompson " P-Multigrid:\n" 729ccaff030SJeremy L Thompson " PCMG Type : %s\n" 730ccaff030SJeremy L Thompson " PCMG Cycle Type : %s\n", 731ccaff030SJeremy L Thompson PCMGTypes[pcmgType], 732ccaff030SJeremy L Thompson PCMGCycleTypes[pcmgCycleType]); CHKERRQ(ierr); 733ccaff030SJeremy L Thompson 734ccaff030SJeremy L Thompson // -- Coarse Solve 735ccaff030SJeremy L Thompson KSP kspCoarse; 736ccaff030SJeremy L Thompson PC pcCoarse; 737ccaff030SJeremy L Thompson PCType pcType; 738ccaff030SJeremy L Thompson 739ccaff030SJeremy L Thompson ierr = PCMGGetCoarseSolve(pc, &kspCoarse); CHKERRQ(ierr); 740ccaff030SJeremy L Thompson ierr = KSPGetType(kspCoarse, &kspType); CHKERRQ(ierr); 741ccaff030SJeremy L Thompson ierr = KSPGetPC(kspCoarse, &pcCoarse); CHKERRQ(ierr); 742ccaff030SJeremy L Thompson ierr = PCGetType(pcCoarse, &pcType); CHKERRQ(ierr); 743ccaff030SJeremy L Thompson ierr = PetscPrintf(comm, 744ccaff030SJeremy L Thompson " Coarse Solve:\n" 745ccaff030SJeremy L Thompson " KSP Type : %s\n" 746ccaff030SJeremy L Thompson " PC Type : %s\n", 747ccaff030SJeremy L Thompson kspType, pcType); CHKERRQ(ierr); 748ccaff030SJeremy L Thompson } 749ccaff030SJeremy L Thompson } 750ccaff030SJeremy L Thompson 751ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 752ccaff030SJeremy L Thompson // Compute solve time 753ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 754ccaff030SJeremy L Thompson if (!appCtx->testMode) { 755ccaff030SJeremy L Thompson ierr = MPI_Allreduce(&elapsedTime, &minTime, 1, MPI_DOUBLE, MPI_MIN, comm); 756ccaff030SJeremy L Thompson CHKERRQ(ierr); 757ccaff030SJeremy L Thompson ierr = MPI_Allreduce(&elapsedTime, &maxTime, 1, MPI_DOUBLE, MPI_MAX, comm); 758ccaff030SJeremy L Thompson CHKERRQ(ierr); 759ccaff030SJeremy L Thompson ierr = PetscPrintf(comm, 760ccaff030SJeremy L Thompson " Performance:\n" 761*7418ca88SJeremy L Thompson " SNES Solve Time : %g (%g) sec\n" 762*7418ca88SJeremy L Thompson " DoFs/Sec in SNES : %g (%g) million\n", 763*7418ca88SJeremy L Thompson maxTime, minTime, 1e-6*Ugsz[fineLevel]*kspIts/maxTime, 764*7418ca88SJeremy L Thompson 1e-6*Ugsz[fineLevel]*kspIts/minTime); CHKERRQ(ierr); 765ccaff030SJeremy L Thompson } 766ccaff030SJeremy L Thompson 767ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 768ccaff030SJeremy L Thompson // Compute error 769ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 770ccaff030SJeremy L Thompson if (appCtx->forcingChoice == FORCE_MMS) { 771ccaff030SJeremy L Thompson CeedScalar l2Error = 1., l2Unorm = 1.; 772ccaff030SJeremy L Thompson const CeedScalar *truearray; 773ccaff030SJeremy L Thompson Vec errorVec, trueVec; 774ccaff030SJeremy L Thompson 775ccaff030SJeremy L Thompson // -- Work vectors 776ccaff030SJeremy L Thompson ierr = VecDuplicate(U, &errorVec); CHKERRQ(ierr); 777ccaff030SJeremy L Thompson ierr = VecSet(errorVec, 0.0); CHKERRQ(ierr); 778ccaff030SJeremy L Thompson ierr = VecDuplicate(U, &trueVec); CHKERRQ(ierr); 779ccaff030SJeremy L Thompson ierr = VecSet(trueVec, 0.0); CHKERRQ(ierr); 780ccaff030SJeremy L Thompson 781ccaff030SJeremy L Thompson // -- Assemble global true solution vector 78262e9c006SJeremy L Thompson CeedVectorGetArrayRead(ceedData[fineLevel]->truesoln, 78362e9c006SJeremy L Thompson appCtx->memTypeRequested, &truearray); 78462e9c006SJeremy L Thompson if (appCtx->memTypeRequested == CEED_MEM_HOST) { 78562e9c006SJeremy L Thompson ierr = VecPlaceArray(resCtx->Yloc, (PetscScalar *)truearray); 78662e9c006SJeremy L Thompson CHKERRQ(ierr); 78762e9c006SJeremy L Thompson } else { 78862e9c006SJeremy L Thompson ierr = VecCUDAPlaceArray(resCtx->Yloc, (PetscScalar *)truearray); 78962e9c006SJeremy L Thompson CHKERRQ(ierr); 79062e9c006SJeremy L Thompson } 791ccaff030SJeremy L Thompson ierr = DMLocalToGlobal(resCtx->dm, resCtx->Yloc, INSERT_VALUES, trueVec); 792ccaff030SJeremy L Thompson CHKERRQ(ierr); 79362e9c006SJeremy L Thompson if (appCtx->memTypeRequested == CEED_MEM_HOST) { 794ccaff030SJeremy L Thompson ierr = VecResetArray(resCtx->Yloc); CHKERRQ(ierr); 79562e9c006SJeremy L Thompson } else { 79662e9c006SJeremy L Thompson ierr = VecCUDAResetArray(resCtx->Yloc); CHKERRQ(ierr); 79762e9c006SJeremy L Thompson } 798ccaff030SJeremy L Thompson CeedVectorRestoreArrayRead(ceedData[fineLevel]->truesoln, &truearray); 799ccaff030SJeremy L Thompson 800ccaff030SJeremy L Thompson // -- Compute L2 error 801ccaff030SJeremy L Thompson ierr = VecWAXPY(errorVec, -1.0, U, trueVec); CHKERRQ(ierr); 802ccaff030SJeremy L Thompson ierr = VecNorm(errorVec, NORM_2, &l2Error); CHKERRQ(ierr); 803ccaff030SJeremy L Thompson ierr = VecNorm(U, NORM_2, &l2Unorm); CHKERRQ(ierr); 804ccaff030SJeremy L Thompson l2Error /= l2Unorm; 805ccaff030SJeremy L Thompson 806ccaff030SJeremy L Thompson // -- Output 807ccaff030SJeremy L Thompson if (!appCtx->testMode || l2Error > 0.05) { 808ccaff030SJeremy L Thompson ierr = PetscPrintf(comm, 809ccaff030SJeremy L Thompson " L2 Error : %e\n", 810ccaff030SJeremy L Thompson l2Error); CHKERRQ(ierr); 811ccaff030SJeremy L Thompson } 812ccaff030SJeremy L Thompson 813ccaff030SJeremy L Thompson // -- Cleanup 814ccaff030SJeremy L Thompson ierr = VecDestroy(&errorVec); CHKERRQ(ierr); 815ccaff030SJeremy L Thompson ierr = VecDestroy(&trueVec); CHKERRQ(ierr); 8162d93065eSjeremylt } 8172d93065eSjeremylt 8182d93065eSjeremylt // --------------------------------------------------------------------------- 8192d93065eSjeremylt // Compute energy 8202d93065eSjeremylt // --------------------------------------------------------------------------- 8212d93065eSjeremylt if (!appCtx->testMode) { 8222d93065eSjeremylt // -- Compute L2 error 8232d93065eSjeremylt CeedScalar energy; 824a3c02c40SJeremy L Thompson ierr = ComputeStrainEnergy(dmEnergy, resCtx, ceedData[fineLevel]->opEnergy, 825a3c02c40SJeremy L Thompson U, &energy); CHKERRQ(ierr); 8262d93065eSjeremylt 8272d93065eSjeremylt // -- Output 8282d93065eSjeremylt ierr = PetscPrintf(comm, 8292d93065eSjeremylt " Strain Energy : %e\n", 8302d93065eSjeremylt energy); CHKERRQ(ierr); 831ccaff030SJeremy L Thompson } 832ccaff030SJeremy L Thompson 833ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 8345c25879aSJeremy L Thompson // Output diagnostic quantities 8355c25879aSJeremy L Thompson // --------------------------------------------------------------------------- 8365c25879aSJeremy L Thompson if (appCtx->viewSoln || appCtx->viewFinalSoln) { 8375c25879aSJeremy L Thompson // -- Setup context 8385c25879aSJeremy L Thompson UserMult diagnosticCtx; 8395c25879aSJeremy L Thompson ierr = PetscMalloc1(1, &diagnosticCtx); CHKERRQ(ierr); 8405c25879aSJeremy L Thompson ierr = PetscMemcpy(diagnosticCtx, resCtx, sizeof(*resCtx)); CHKERRQ(ierr); 8415c25879aSJeremy L Thompson diagnosticCtx->dm = dmDiagnostic; 8425c25879aSJeremy L Thompson diagnosticCtx->op = ceedData[fineLevel]->opDiagnostic; 8435c25879aSJeremy L Thompson 8445c25879aSJeremy L Thompson // -- Compute and output 8455c25879aSJeremy L Thompson ierr = ViewDiagnosticQuantities(comm, levelDMs[fineLevel], diagnosticCtx, U, 8465c25879aSJeremy L Thompson ceedData[fineLevel]->ErestrictDiagnostic); 8475c25879aSJeremy L Thompson CHKERRQ(ierr); 8485c25879aSJeremy L Thompson 8495c25879aSJeremy L Thompson // -- Cleanup 8505c25879aSJeremy L Thompson ierr = PetscFree(diagnosticCtx); CHKERRQ(ierr); 8515c25879aSJeremy L Thompson } 8525c25879aSJeremy L Thompson 8535c25879aSJeremy L Thompson // --------------------------------------------------------------------------- 854ccaff030SJeremy L Thompson // Free objects 855ccaff030SJeremy L Thompson // --------------------------------------------------------------------------- 856ccaff030SJeremy L Thompson // Data in arrays per level 857e3e3df41Sjeremylt for (PetscInt level = 0; level < numLevels; level++) { 858ccaff030SJeremy L Thompson // Vectors 859ccaff030SJeremy L Thompson ierr = VecDestroy(&Ug[level]); CHKERRQ(ierr); 860ccaff030SJeremy L Thompson ierr = VecDestroy(&Uloc[level]); CHKERRQ(ierr); 861ccaff030SJeremy L Thompson 862ccaff030SJeremy L Thompson // Jacobian matrix and data 863ccaff030SJeremy L Thompson ierr = VecDestroy(&jacobCtx[level]->Yloc); CHKERRQ(ierr); 864ccaff030SJeremy L Thompson ierr = MatDestroy(&jacobMat[level]); CHKERRQ(ierr); 865ccaff030SJeremy L Thompson ierr = PetscFree(jacobCtx[level]); CHKERRQ(ierr); 866ccaff030SJeremy L Thompson 867ccaff030SJeremy L Thompson // Prolongation/Restriction matrix and data 868ccaff030SJeremy L Thompson if (level > 0) { 869ccaff030SJeremy L Thompson ierr = VecDestroy(&prolongRestrCtx[level]->multVec); CHKERRQ(ierr); 870ccaff030SJeremy L Thompson ierr = PetscFree(prolongRestrCtx[level]); CHKERRQ(ierr); 871ccaff030SJeremy L Thompson ierr = MatDestroy(&prolongRestrMat[level]); CHKERRQ(ierr); 872ccaff030SJeremy L Thompson } 873ccaff030SJeremy L Thompson 874ccaff030SJeremy L Thompson // DM 875ccaff030SJeremy L Thompson ierr = DMDestroy(&levelDMs[level]); CHKERRQ(ierr); 876ccaff030SJeremy L Thompson 877ccaff030SJeremy L Thompson // libCEED objects 878ccaff030SJeremy L Thompson ierr = CeedDataDestroy(level, ceedData[level]); CHKERRQ(ierr); 879ccaff030SJeremy L Thompson } 880ccaff030SJeremy L Thompson 881ccaff030SJeremy L Thompson // Arrays 882ccaff030SJeremy L Thompson ierr = PetscFree(Ug); CHKERRQ(ierr); 883ccaff030SJeremy L Thompson ierr = PetscFree(Uloc); CHKERRQ(ierr); 884ccaff030SJeremy L Thompson ierr = PetscFree(Ugsz); CHKERRQ(ierr); 885ccaff030SJeremy L Thompson ierr = PetscFree(Ulsz); CHKERRQ(ierr); 886ccaff030SJeremy L Thompson ierr = PetscFree(Ulocsz); CHKERRQ(ierr); 887ccaff030SJeremy L Thompson ierr = PetscFree(jacobCtx); CHKERRQ(ierr); 888ccaff030SJeremy L Thompson ierr = PetscFree(jacobMat); CHKERRQ(ierr); 889ccaff030SJeremy L Thompson ierr = PetscFree(prolongRestrCtx); CHKERRQ(ierr); 890ccaff030SJeremy L Thompson ierr = PetscFree(prolongRestrMat); CHKERRQ(ierr); 891ccaff030SJeremy L Thompson ierr = PetscFree(appCtx->levelDegrees); CHKERRQ(ierr); 892ccaff030SJeremy L Thompson ierr = PetscFree(ceedData); CHKERRQ(ierr); 893ccaff030SJeremy L Thompson 894ccaff030SJeremy L Thompson // libCEED objects 895ccaff030SJeremy L Thompson CeedQFunctionDestroy(&qfRestrict); 896ccaff030SJeremy L Thompson CeedQFunctionDestroy(&qfProlong); 897ccaff030SJeremy L Thompson CeedDestroy(&ceed); 898ccaff030SJeremy L Thompson CeedDestroy(&ceedFine); 899ccaff030SJeremy L Thompson 900ccaff030SJeremy L Thompson // PETSc objects 901ccaff030SJeremy L Thompson ierr = VecDestroy(&U); CHKERRQ(ierr); 902ccaff030SJeremy L Thompson ierr = VecDestroy(&R); CHKERRQ(ierr); 903ccaff030SJeremy L Thompson ierr = VecDestroy(&Rloc); CHKERRQ(ierr); 904ccaff030SJeremy L Thompson ierr = VecDestroy(&F); CHKERRQ(ierr); 905ccaff030SJeremy L Thompson ierr = VecDestroy(&Floc); CHKERRQ(ierr); 906ccaff030SJeremy L Thompson ierr = MatDestroy(&jacobMatCoarse); CHKERRQ(ierr); 907ccaff030SJeremy L Thompson ierr = SNESDestroy(&snes); CHKERRQ(ierr); 908ccaff030SJeremy L Thompson ierr = SNESDestroy(&snesCoarse); CHKERRQ(ierr); 909ccaff030SJeremy L Thompson ierr = DMDestroy(&dmOrig); CHKERRQ(ierr); 910a3c02c40SJeremy L Thompson ierr = DMDestroy(&dmEnergy); CHKERRQ(ierr); 911a3c02c40SJeremy L Thompson ierr = DMDestroy(&dmDiagnostic); CHKERRQ(ierr); 912ccaff030SJeremy L Thompson ierr = PetscFree(levelDMs); CHKERRQ(ierr); 913ccaff030SJeremy L Thompson 914ccaff030SJeremy L Thompson // Structs 915ccaff030SJeremy L Thompson ierr = PetscFree(resCtx); CHKERRQ(ierr); 916ccaff030SJeremy L Thompson ierr = PetscFree(formJacobCtx); CHKERRQ(ierr); 917ccaff030SJeremy L Thompson ierr = PetscFree(jacobCoarseCtx); CHKERRQ(ierr); 918ccaff030SJeremy L Thompson ierr = PetscFree(appCtx); CHKERRQ(ierr); 919ccaff030SJeremy L Thompson ierr = PetscFree(phys); CHKERRQ(ierr); 920f7b4142eSJeremy L Thompson ierr = PetscFree(physSmoother); CHKERRQ(ierr); 921ccaff030SJeremy L Thompson ierr = PetscFree(units); CHKERRQ(ierr); 922ccaff030SJeremy L Thompson 923ccaff030SJeremy L Thompson return PetscFinalize(); 924ccaff030SJeremy L Thompson } 925