1 static char help[] = "Tests PetscSectionView()/Load() with HDF5.\n\n"; 2 3 #include <petscdmshell.h> 4 #include <petscdmplex.h> 5 #include <petscsection.h> 6 #include <petscsf.h> 7 #include <petsclayouthdf5.h> 8 9 /* Save/Load abstract sections 10 11 ===================== 12 Save on 2 processes 13 ===================== 14 15 section: 16 0 1 2 3 17 rank 0: Dof (Field 0) 2 3 5 7 18 Dof (Field 1) 1 0 0 0 19 20 0 1 2 21 rank 1: Dof (Field 0) 7 5 11 <- DoF 7 is constrained 22 Dof (Field 1) 0 0 2 23 24 sf: 25 [0] 3 <- (1, 0) 26 [1] 1 <- (0, 2) 27 28 global section (includesConstraints = PETSC_FALSE): 29 0 1 2 3 30 rank 0: Dof (Field 0) 2 3 5 -8 31 Off (Field 0) 0 3 6 -12 32 Dof (Field 1) 1 0 0 -1 33 Off (Field 1) 2 6 11 -19 34 35 0 1 2 36 rank 1: Dof (Field 0) 7 -6 11 37 Off (Field 0) 11 -7 18 38 Dof (Field 1) 0 -1 2 39 Off (Field 1) 18 -12 28 40 41 global section (includesConstraints = PETSC_TRUE): 42 0 1 2 3 43 rank 0: Dof (Field 0) 2 3 5 -8 44 Off (Field 0) 0 3 6 -12 45 Dof (Field 1) 1 0 0 -1 46 Off (Field 1) 2 6 11 -19 47 48 0 1 2 49 rank 1: Dof (Field 0) 7 -6 11 50 Off (Field 0) 11 -7 18 51 Dof (Field 1) 0 -1 2 52 Off (Field 1) 18 -12 29 53 54 ===================== 55 Load on 3 Processes 56 ===================== 57 58 (Set chartSize = 4, 0, 1 for rank 0, 1, 2, respectively) 59 60 global section (includesConstraints = PETSC_FALSE): 61 62 rank 0: Dof (Field 0) 2 3 5 7 63 Off (Field 0) 0 3 6 11 64 Dof (Field 1) 1 0 0 0 65 Off (Field 1) 2 6 11 18 66 67 rank 1: Dof (Field 0) 68 Dof (Field 1) 69 70 rank 2: Dof (Field 0) 11 71 Off (Field 0) 18 72 Dof (Field 1) 2 73 Off (Field 1) 28 74 75 global section (includesConstraints = PETSC_TRUE): 76 77 rank 0: Dof (Field 0) 2 3 5 7 78 Off (Field 0) 0 3 6 11 79 Dof (Field 1) 1 0 0 0 80 Off (Field 1) 2 6 11 18 81 82 rank 1: Dof (Field 0) 83 Dof (Field 1) 84 85 rank 2: Dof (Field 0) 11 86 Off (Field 0) 18 87 Dof (Field 1) 2 88 Off (Field 1) 29 89 */ 90 91 typedef struct { 92 char fname[PETSC_MAX_PATH_LEN]; /* Output mesh filename */ 93 PetscBool includes_constraints; /* Flag for if global section is to include constrained DoFs or not */ 94 } AppCtx; 95 96 PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options) 97 { 98 PetscErrorCode ierr; 99 100 PetscFunctionBegin; 101 options->fname[0] = '\0'; 102 options->includes_constraints = PETSC_TRUE; 103 ierr = PetscOptionsBegin(comm, "", "PetscSectionView()/Load() in HDF5 Test Options", "DMPLEX");CHKERRQ(ierr); 104 ierr = PetscOptionsString("-fname", "The output file", "ex5.c", options->fname, options->fname, sizeof(options->fname), NULL);CHKERRQ(ierr); 105 ierr = PetscOptionsBool("-includes_constraints", "Flag for if global section is to include constrained DoFs or not", "ex5.c", options->includes_constraints, &options->includes_constraints, NULL);CHKERRQ(ierr); 106 ierr = PetscOptionsEnd();CHKERRQ(ierr); 107 PetscFunctionReturn(0); 108 } 109 110 int main(int argc, char **argv) 111 { 112 MPI_Comm comm; 113 PetscMPIInt size, rank, mycolor; 114 AppCtx user; 115 PetscErrorCode ierr; 116 117 ierr = PetscInitialize(&argc, &argv, NULL, help); if (ierr) return ierr; 118 ierr = ProcessOptions(PETSC_COMM_WORLD, &user);CHKERRQ(ierr); 119 ierr = MPI_Comm_size(PETSC_COMM_WORLD, &size);CHKERRMPI(ierr); 120 ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank);CHKERRMPI(ierr); 121 if (size < 3) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "Example only works with three or more processes"); 122 123 /* Save */ 124 mycolor = (PetscMPIInt)(rank >= 2); 125 ierr = MPI_Comm_split(PETSC_COMM_WORLD, mycolor, rank, &comm);CHKERRMPI(ierr); 126 if (mycolor == 0) { 127 PetscSection section, gsection; 128 PetscSF sf; 129 PetscInt nroots = -1, nleaves = -1, *ilocal; 130 PetscSFNode *iremote; 131 PetscViewer viewer; 132 133 /* Create section */ 134 ierr = PetscSectionCreate(comm, §ion);CHKERRQ(ierr); 135 ierr = PetscSectionSetNumFields(section, 2);CHKERRQ(ierr); 136 switch (rank) { 137 case 0: 138 ierr = PetscSectionSetChart(section, 0, 4);CHKERRQ(ierr); 139 ierr = PetscSectionSetDof(section, 0, 3);CHKERRQ(ierr); 140 ierr = PetscSectionSetDof(section, 1, 3);CHKERRQ(ierr); 141 ierr = PetscSectionSetDof(section, 2, 5);CHKERRQ(ierr); 142 ierr = PetscSectionSetDof(section, 3, 7);CHKERRQ(ierr); 143 ierr = PetscSectionSetFieldDof(section, 0, 0, 2);CHKERRQ(ierr); 144 ierr = PetscSectionSetFieldDof(section, 1, 0, 3);CHKERRQ(ierr); 145 ierr = PetscSectionSetFieldDof(section, 2, 0, 5);CHKERRQ(ierr); 146 ierr = PetscSectionSetFieldDof(section, 3, 0, 7);CHKERRQ(ierr); 147 ierr = PetscSectionSetFieldDof(section, 0, 1, 1);CHKERRQ(ierr); 148 break; 149 case 1: 150 ierr = PetscSectionSetChart(section, 0, 3);CHKERRQ(ierr); 151 ierr = PetscSectionSetDof(section, 0, 7);CHKERRQ(ierr); 152 ierr = PetscSectionSetDof(section, 1, 5);CHKERRQ(ierr); 153 ierr = PetscSectionSetDof(section, 2, 13);CHKERRQ(ierr); 154 ierr = PetscSectionSetConstraintDof(section, 2, 1);CHKERRQ(ierr); 155 ierr = PetscSectionSetFieldDof(section, 0, 0, 7);CHKERRQ(ierr); 156 ierr = PetscSectionSetFieldDof(section, 1, 0, 5);CHKERRQ(ierr); 157 ierr = PetscSectionSetFieldDof(section, 2, 0, 11);CHKERRQ(ierr); 158 ierr = PetscSectionSetFieldDof(section, 2, 1, 2);CHKERRQ(ierr); 159 ierr = PetscSectionSetFieldConstraintDof(section, 2, 0, 1);CHKERRQ(ierr); 160 break; 161 } 162 ierr = PetscSectionSetUp(section);CHKERRQ(ierr); 163 if (rank == 1) 164 { 165 const PetscInt indices[] = {7}; 166 const PetscInt indices0[] = {7}; 167 168 ierr = PetscSectionSetConstraintIndices(section, 2, indices);CHKERRQ(ierr); 169 ierr = PetscSectionSetFieldConstraintIndices(section, 2, 0, indices0);CHKERRQ(ierr); 170 } 171 /* Create sf */ 172 switch (rank) { 173 case 0: 174 nroots = 4; 175 nleaves = 1; 176 ierr = PetscMalloc1(nleaves, &ilocal);CHKERRQ(ierr); 177 ierr = PetscMalloc1(nleaves, &iremote);CHKERRQ(ierr); 178 ilocal[0] = 3; 179 iremote[0].rank = 1; 180 iremote[0].index = 0; 181 break; 182 case 1: 183 nroots = 3; 184 nleaves = 1; 185 ierr = PetscMalloc1(nleaves, &ilocal);CHKERRQ(ierr); 186 ierr = PetscMalloc1(nleaves, &iremote);CHKERRQ(ierr); 187 ilocal[0] = 1; 188 iremote[0].rank = 0; 189 iremote[0].index = 2; 190 break; 191 } 192 ierr = PetscSFCreate(comm, &sf);CHKERRQ(ierr); 193 ierr = PetscSFSetGraph(sf, nroots, nleaves, ilocal, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER);CHKERRQ(ierr); 194 /* Create global section*/ 195 ierr = PetscSectionCreateGlobalSection(section, sf, user.includes_constraints, PETSC_FALSE, &gsection);CHKERRQ(ierr); 196 ierr = PetscSFDestroy(&sf);CHKERRQ(ierr); 197 /* View */ 198 ierr = PetscViewerHDF5Open(comm, user.fname, FILE_MODE_WRITE, &viewer);CHKERRQ(ierr); 199 ierr = PetscSectionView(gsection, viewer);CHKERRQ(ierr); 200 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 201 ierr = PetscObjectSetName((PetscObject)section, "Save: local section");CHKERRQ(ierr); 202 ierr = PetscSectionView(section, PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr); 203 ierr = PetscObjectSetName((PetscObject)gsection, "Save: global section");CHKERRQ(ierr); 204 ierr = PetscSectionView(gsection, PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr); 205 ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr); 206 ierr = PetscSectionDestroy(§ion);CHKERRQ(ierr); 207 } 208 ierr = MPI_Comm_free(&comm);CHKERRMPI(ierr); 209 210 /* Load */ 211 mycolor = (PetscMPIInt)(rank >= 3); 212 ierr = MPI_Comm_split(PETSC_COMM_WORLD, mycolor, rank, &comm);CHKERRMPI(ierr); 213 if (mycolor == 0) { 214 PetscSection section; 215 PetscInt chartSize = -1; 216 PetscViewer viewer; 217 218 ierr = PetscSectionCreate(comm, §ion);CHKERRQ(ierr); 219 switch (rank) { 220 case 0: 221 chartSize = 4; 222 break; 223 case 1: 224 chartSize = 0; 225 break; 226 case 2: 227 chartSize = 1; 228 break; 229 } 230 ierr = PetscSectionSetChart(section, 0, chartSize);CHKERRQ(ierr); 231 ierr = PetscViewerHDF5Open(comm, user.fname, FILE_MODE_READ, &viewer);CHKERRQ(ierr); 232 ierr = PetscSectionLoad(section, viewer);CHKERRQ(ierr); 233 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 234 ierr = PetscObjectSetName((PetscObject)section, "Load: section");CHKERRQ(ierr); 235 ierr = PetscSectionView(section, PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr); 236 ierr = PetscSectionDestroy(§ion);CHKERRQ(ierr); 237 } 238 ierr = MPI_Comm_free(&comm);CHKERRMPI(ierr); 239 240 /* Finalize */ 241 ierr = PetscFinalize(); 242 return ierr; 243 } 244 245 /*TEST 246 247 build: 248 requires: hdf5 249 requires: !complex 250 testset: 251 nsize: 4 252 test: 253 suffix: 0 254 args: -fname ex5_dump.h5 -includes_constraints 0 255 test: 256 suffix: 1 257 args: -fname ex5_dump.h5 -includes_constraints 1 258 259 TEST*/ 260