1 static char help[] = "Tests adaptive refinement using DMForest, and uses HDF5.\n\n"; 2 3 #include <petscdmforest.h> 4 #include <petscdmplex.h> 5 #include <petscviewerhdf5.h> 6 7 int main (int argc, char **argv) 8 { 9 DM base, forest, plex; 10 PetscSection s; 11 PetscViewer viewer; 12 Vec g = NULL, g2 = NULL; 13 PetscReal nrm; 14 PetscBool adapt = PETSC_FALSE, userSection = PETSC_FALSE; 15 PetscInt vStart, vEnd, v, i; 16 17 PetscCall(PetscInitialize(&argc, &argv, NULL, help)); 18 PetscCall(PetscOptionsGetBool(NULL, NULL, "-adapt", &adapt, NULL)); 19 PetscCall(PetscOptionsGetBool(NULL, NULL, "-user_section", &userSection, NULL)); 20 21 /* Create a base DMPlex mesh */ 22 PetscCall(DMCreate(PETSC_COMM_WORLD, &base)); 23 PetscCall(DMSetType(base, DMPLEX)); 24 PetscCall(DMSetFromOptions(base)); 25 PetscCall(DMViewFromOptions(base, NULL, "-dm_view")); 26 27 /* Covert Plex mesh to Forest and destroy base */ 28 PetscCall(DMCreate(PETSC_COMM_WORLD, &forest)); 29 PetscCall(DMSetType(forest, DMP4EST)); 30 PetscCall(DMForestSetBaseDM(forest, base)); 31 PetscCall(DMSetUp(forest)); 32 PetscCall(DMDestroy(&base)); 33 PetscCall(DMViewFromOptions(forest, NULL, "-dm_view")); 34 35 if (adapt) { 36 /* Adaptively refine the cell 0 of the mesh */ 37 for (i = 0; i < 3; ++i) { 38 DM postforest; 39 DMLabel adaptLabel = NULL; 40 41 PetscCall(DMLabelCreate(PETSC_COMM_SELF, "adapt", &adaptLabel)); 42 PetscCall(DMLabelSetValue(adaptLabel, 0, DM_ADAPT_REFINE)); 43 PetscCall(DMForestTemplate(forest, PETSC_COMM_WORLD, &postforest)); 44 PetscCall(DMForestSetAdaptivityLabel(postforest, adaptLabel)); 45 PetscCall(DMLabelDestroy(&adaptLabel)); 46 PetscCall(DMSetUp(postforest)); 47 PetscCall(DMDestroy(&forest)); 48 forest = postforest; 49 } 50 } else { 51 /* Adaptively refine all cells of the mesh */ 52 PetscInt cStart, cEnd, c; 53 54 for (i = 0; i < 3; ++i) { 55 DM postforest; 56 DMLabel adaptLabel = NULL; 57 58 PetscCall(DMLabelCreate(PETSC_COMM_SELF, "adapt", &adaptLabel)); 59 60 PetscCall(DMForestGetCellChart(forest, &cStart, &cEnd)); 61 for (c = cStart; c < cEnd; ++c) { 62 PetscCall(DMLabelSetValue(adaptLabel, c, DM_ADAPT_REFINE)); 63 } 64 65 PetscCall(DMForestTemplate(forest, PETSC_COMM_WORLD, &postforest)); 66 PetscCall(DMForestSetAdaptivityLabel(postforest, adaptLabel)); 67 PetscCall(DMLabelDestroy(&adaptLabel)); 68 PetscCall(DMSetUp(postforest)); 69 PetscCall(DMDestroy(&forest)); 70 forest = postforest; 71 } 72 } 73 PetscCall(DMViewFromOptions(forest, NULL, "-dm_view")); 74 75 /* Setup the section*/ 76 if (userSection) { 77 PetscCall(DMConvert(forest, DMPLEX, &plex)); 78 PetscCall(DMViewFromOptions(plex, NULL, "-dm_view")); 79 PetscCall(DMPlexGetDepthStratum(plex, 0, &vStart, &vEnd)); 80 PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "Vertices [%D, %D)\n", vStart, vEnd)); 81 PetscCall(PetscSynchronizedFlush(PETSC_COMM_WORLD, NULL)); 82 PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject) forest), &s)); 83 PetscCall(PetscSectionSetNumFields(s, 1)); 84 PetscCall(PetscSectionSetChart(s, vStart, vEnd)); 85 for (v = vStart; v < vEnd; ++v) { 86 PetscCall(PetscSectionSetDof(s, v, 1)); 87 PetscCall(PetscSectionSetFieldDof(s, v, 0, 1)); 88 } 89 PetscCall(PetscSectionSetUp(s)); 90 PetscCall(DMSetLocalSection(forest, s)); 91 PetscCall(PetscObjectViewFromOptions((PetscObject) s, NULL, "-my_section_view")); 92 PetscCall(PetscSectionDestroy(&s)); 93 PetscCall(DMDestroy(&plex)); 94 } else { 95 PetscFE fe; 96 PetscInt dim; 97 98 PetscCall(DMGetDimension(forest, &dim)); 99 PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, 1, PETSC_FALSE, 1, PETSC_DETERMINE, &fe)); 100 PetscCall(DMAddField(forest, NULL, (PetscObject) fe)); 101 PetscCall(PetscFEDestroy(&fe)); 102 PetscCall(DMCreateDS(forest)); 103 } 104 105 /* Create the global vector*/ 106 PetscCall(DMCreateGlobalVector(forest, &g)); 107 PetscCall(PetscObjectSetName((PetscObject) g, "g")); 108 PetscCall(VecSet(g, 1.0)); 109 110 /* Test global to local*/ 111 Vec l; 112 PetscCall(DMCreateLocalVector(forest, &l)); 113 PetscCall(VecZeroEntries(l)); 114 PetscCall(DMGlobalToLocal(forest, g, INSERT_VALUES, l)); 115 PetscCall(VecZeroEntries(g)); 116 PetscCall(DMLocalToGlobal(forest, l, INSERT_VALUES, g)); 117 PetscCall(VecDestroy(&l)); 118 119 /* Save a vector*/ 120 PetscCall(PetscViewerHDF5Open(PETSC_COMM_WORLD, "forestHDF.h5", FILE_MODE_WRITE, &viewer)); 121 PetscCall(VecView(g, viewer)); 122 PetscCall(PetscViewerDestroy(&viewer)); 123 124 /* Load another vector to load into*/ 125 PetscCall(DMCreateGlobalVector(forest, &g2)); 126 PetscCall(PetscObjectSetName((PetscObject) g2, "g")); 127 PetscCall(VecZeroEntries(g2)); 128 129 /* Load a vector*/ 130 PetscCall(PetscViewerHDF5Open(PETSC_COMM_WORLD, "forestHDF.h5", FILE_MODE_READ, &viewer)); 131 PetscCall(VecLoad(g2, viewer)); 132 PetscCall(PetscViewerDestroy(&viewer)); 133 134 /* Check if the data is the same*/ 135 PetscCall(VecAXPY(g2, -1.0, g)); 136 PetscCall(VecNorm(g2, NORM_INFINITY, &nrm)); 137 PetscCheck(PetscAbsReal(nrm) <= PETSC_SMALL,PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Invalid difference norm %g", (double) nrm); 138 139 PetscCall(VecDestroy(&g)); 140 PetscCall(VecDestroy(&g2)); 141 PetscCall(DMDestroy(&forest)); 142 PetscCall(PetscFinalize()); 143 return 0; 144 } 145 146 /*TEST 147 148 build: 149 requires: hdf5 p4est 150 151 test: 152 suffix: 0 153 nsize: {{1 2 5}} 154 args: -adapt -dm_plex_simplex 0 -dm_plex_box_faces 2,2 155 156 TEST*/ 157