Actual source code: ex1.c

  1: static char help[] = "Tests DMLabel operations.\n\n";

  3: #include <petscdm.h>
  4: #include <petscdmplex.h>

  6: PetscErrorCode ViewLabels(DM dm, PetscViewer viewer)
  7: {
  8:   DMLabel     label;
  9:   const char *labelName;
 10:   PetscInt    numLabels, l;

 12:   /* query the number and name of labels*/
 13:   DMGetNumLabels(dm, &numLabels);
 14:   PetscViewerASCIIPrintf(viewer, "Number of labels: %" PetscInt_FMT "\n", numLabels);
 15:   for (l = 0; l < numLabels; ++l) {
 16:     IS labelIS, tmpIS;

 18:     DMGetLabelName(dm, l, &labelName);
 19:     PetscViewerASCIIPrintf(viewer, "Label %" PetscInt_FMT ": name: %s\n", l, labelName);
 20:     PetscViewerASCIIPrintf(viewer, "IS of values\n");
 21:     DMGetLabel(dm, labelName, &label);
 22:     DMLabelGetValueIS(label, &labelIS);
 23:     ISOnComm(labelIS, PetscObjectComm((PetscObject)viewer), PETSC_USE_POINTER, &tmpIS);
 24:     PetscViewerASCIIPushTab(viewer);
 25:     ISView(tmpIS, viewer);
 26:     PetscViewerASCIIPopTab(viewer);
 27:     ISDestroy(&tmpIS);
 28:     ISDestroy(&labelIS);
 29:     PetscViewerASCIIPrintf(viewer, "\n");
 30:   }
 31:   /* Making sure that string literals work */
 32:   PetscViewerASCIIPrintf(viewer, "\n\nCell Set label IS\n");
 33:   DMGetLabel(dm, "Cell Sets", &label);
 34:   if (label) {
 35:     IS labelIS, tmpIS;

 37:     DMLabelGetValueIS(label, &labelIS);
 38:     ISOnComm(labelIS, PetscObjectComm((PetscObject)viewer), PETSC_USE_POINTER, &tmpIS);
 39:     ISView(tmpIS, viewer);
 40:     ISDestroy(&tmpIS);
 41:     ISDestroy(&labelIS);
 42:   }
 43:   return 0;
 44: }

 46: PetscErrorCode CheckLabelsSame(DMLabel label0, DMLabel label1)
 47: {
 48:   const char *name0, *name1;
 49:   PetscBool   same;
 50:   char       *msg;

 52:   PetscObjectGetName((PetscObject)label0, &name0);
 53:   PetscObjectGetName((PetscObject)label1, &name1);
 54:   DMLabelCompare(PETSC_COMM_WORLD, label0, label1, &same, &msg);
 57:   /* Test passing NULL, must not fail */
 58:   DMLabelCompare(PETSC_COMM_WORLD, label0, label1, NULL, NULL);
 59:   PetscFree(msg);
 60:   return 0;
 61: }

 63: PetscErrorCode CheckLabelsNotSame(DMLabel label0, DMLabel label1)
 64: {
 65:   const char *name0, *name1;
 66:   PetscBool   same;
 67:   char       *msg;

 69:   PetscObjectGetName((PetscObject)label0, &name0);
 70:   PetscObjectGetName((PetscObject)label1, &name1);
 71:   DMLabelCompare(PETSC_COMM_WORLD, label0, label1, &same, &msg);
 74:   PetscPrintf(PETSC_COMM_WORLD, "Compare label \"%s\" with \"%s\": %s\n", name0, name1, msg);
 75:   PetscFree(msg);
 76:   return 0;
 77: }

 79: PetscErrorCode CheckDMLabelsSame(DM dm0, DM dm1)
 80: {
 81:   const char *name0, *name1;
 82:   PetscBool   same;
 83:   char       *msg;

 85:   PetscObjectGetName((PetscObject)dm0, &name0);
 86:   PetscObjectGetName((PetscObject)dm1, &name1);
 87:   DMCompareLabels(dm0, dm1, &same, &msg);
 90:   /* Test passing NULL, must not fail */
 91:   DMCompareLabels(dm0, dm1, NULL, NULL);
 92:   PetscFree(msg);
 93:   return 0;
 94: }

 96: PetscErrorCode CheckDMLabelsNotSame(DM dm0, DM dm1)
 97: {
 98:   const char *name0, *name1;
 99:   PetscBool   same;
100:   char       *msg;

102:   PetscObjectGetName((PetscObject)dm0, &name0);
103:   PetscObjectGetName((PetscObject)dm1, &name1);
104:   DMCompareLabels(dm0, dm1, &same, &msg);
107:   PetscPrintf(PETSC_COMM_WORLD, "Labels of DMs \"%s\" and \"%s\" differ: %s\n", name0, name1, msg);
108:   PetscFree(msg);
109:   return 0;
110: }

112: PetscErrorCode CreateMesh(const char name[], DM *newdm)
113: {
114:   DM        dm, dmDist;
115:   char      filename[PETSC_MAX_PATH_LEN] = "";
116:   PetscBool interpolate                  = PETSC_FALSE;

118:   /* initialize and get options */
119:   PetscOptionsBegin(PETSC_COMM_WORLD, NULL, "DMLabel ex1 Options", "DMLabel");
120:   PetscOptionsString("-i", "filename to read", "ex1.c", filename, filename, sizeof(filename), NULL);
121:   PetscOptionsBool("-interpolate", "Generate intermediate mesh elements", "ex1.c", interpolate, &interpolate, NULL);
122:   PetscOptionsEnd();

124:   /* create and distribute DM */
125:   DMPlexCreateFromFile(PETSC_COMM_WORLD, filename, "ex1_plex", interpolate, &dm);
126:   DMPlexDistribute(dm, 0, NULL, &dmDist);
127:   if (dmDist) {
128:     DMDestroy(&dm);
129:     dm = dmDist;
130:   }
131:   DMSetFromOptions(dm);
132:   PetscObjectSetName((PetscObject)dm, name);
133:   *newdm = dm;
134:   return 0;
135: }

137: int main(int argc, char **argv)
138: {
139:   DM dm;

142:   PetscInitialize(&argc, &argv, NULL, help);
143:   CreateMesh("plex0", &dm);
144:   /* add custom labels to test adding/removal */
145:   {
146:     DMLabel  label0, label1, label2, label3;
147:     PetscInt p, pStart, pEnd;
148:     DMPlexGetChart(dm, &pStart, &pEnd);
149:     /* create label in DM and get from DM */
150:     DMCreateLabel(dm, "label0");
151:     DMGetLabel(dm, "label0", &label0);
152:     /* alternative: create standalone label and add to DM; needs to be destroyed */
153:     DMLabelCreate(PETSC_COMM_SELF, "label1", &label1);
154:     DMAddLabel(dm, label1);

156:     pEnd = PetscMin(pEnd, pStart + 5);
157:     for (p = pStart; p < pEnd; p++) {
158:       DMLabelSetValue(label0, p, 1);
159:       DMLabelSetValue(label1, p, 2);
160:     }
161:     /* duplicate label */
162:     DMLabelDuplicate(label0, &label2);
163:     DMLabelDuplicate(label1, &label3);
164:     PetscObjectSetName((PetscObject)label2, "label2");
165:     PetscObjectSetName((PetscObject)label3, "label3");
166:     DMAddLabel(dm, label2);
167:     DMAddLabel(dm, label3);
168:     /* remove the labels in this scope */
169:     DMLabelDestroy(&label1);
170:     DMLabelDestroy(&label2);
171:     DMLabelDestroy(&label3);
172:   }

174:   ViewLabels(dm, PETSC_VIEWER_STDOUT_WORLD);

176:   /* do label perturbations and comparisons */
177:   {
178:     DMLabel  label0, label1, label2, label3;
179:     PetscInt val;
180:     PetscInt p, pStart, pEnd;

182:     DMGetLabel(dm, "label0", &label0);
183:     DMGetLabel(dm, "label1", &label1);
184:     DMGetLabel(dm, "label2", &label2);
185:     DMGetLabel(dm, "label3", &label3);

187:     CheckLabelsNotSame(label0, label1);
188:     CheckLabelsSame(label0, label2);
189:     CheckLabelsSame(label1, label3);

191:     DMLabelGetDefaultValue(label1, &val);
192:     DMLabelSetDefaultValue(label1, 333);
193:     CheckLabelsNotSame(label1, label3);
194:     DMLabelSetDefaultValue(label1, val);
195:     CheckLabelsSame(label1, label3);

197:     DMLabelGetBounds(label1, &pStart, &pEnd);

199:     for (p = pStart; p < pEnd; p++) {
200:       DMLabelGetValue(label1, p, &val);
201:       // This is weird. Perhaps we should not need to call DMLabelClearValue()
202:       DMLabelClearValue(label1, p, val);
203:       val++;
204:       DMLabelSetValue(label1, p, val);
205:     }
206:     CheckLabelsNotSame(label1, label3);
207:     for (p = pStart; p < pEnd; p++) {
208:       DMLabelGetValue(label1, p, &val);
209:       // This is weird. Perhaps we should not need to call DMLabelClearValue()
210:       DMLabelClearValue(label1, p, val);
211:       val--;
212:       DMLabelSetValue(label1, p, val);
213:     }
214:     CheckLabelsSame(label1, label3);

216:     DMLabelGetValue(label3, pEnd - 1, &val);
217:     DMLabelSetValue(label3, pEnd, val);
218:     CheckLabelsNotSame(label1, label3);
219:     // This is weird. Perhaps we should not need to call DMLabelClearValue()
220:     DMLabelClearValue(label3, pEnd, val);
221:     CheckLabelsSame(label1, label3);
222:   }

224:   {
225:     DM       dm1;
226:     DMLabel  label02, label12;
227:     PetscInt p = 0, val;

229:     CreateMesh("plex1", &dm1);
230:     CheckDMLabelsNotSame(dm, dm1);

232:     DMCopyLabels(dm, dm1, PETSC_OWN_POINTER, PETSC_FALSE, DM_COPY_LABELS_REPLACE);
233:     CheckDMLabelsSame(dm, dm1);

235:     DMCopyLabels(dm, dm1, PETSC_COPY_VALUES, PETSC_FALSE, DM_COPY_LABELS_REPLACE);
236:     DMGetLabel(dm, "label2", &label02);
237:     DMGetLabel(dm1, "label2", &label12);
238:     CheckLabelsSame(label02, label12);

240:     DMLabelGetValue(label12, p, &val);
241:     // This is weird. Perhaps we should not need to call DMLabelClearValue()
242:     DMLabelClearValue(label12, p, val);
243:     DMLabelSetValue(label12, p, val + 1);
244:     CheckLabelsNotSame(label02, label12);
245:     CheckDMLabelsNotSame(dm, dm1);

247:     // This is weird. Perhaps we should not need to call DMLabelClearValue()
248:     DMLabelClearValue(label12, p, val + 1);
249:     DMLabelSetValue(label12, p, val);
250:     CheckLabelsSame(label02, label12);
251:     CheckDMLabelsSame(dm, dm1);

253:     PetscObjectSetName((PetscObject)label12, "label12");
254:     CheckDMLabelsNotSame(dm, dm1);
255:     PetscObjectSetName((PetscObject)label12, "label2");
256:     CheckDMLabelsSame(dm, dm1);

258:     DMDestroy(&dm1);
259:   }

261:   /* remove label0 and label1 just to test manual removal; let label3 be removed automatically by DMDestroy() */
262:   {
263:     DMLabel label0, label1, label2;
264:     DMGetLabel(dm, "label0", &label0);
265:     DMGetLabel(dm, "label1", &label1);
268:     DMRemoveLabel(dm, "label1", NULL);
269:     DMRemoveLabel(dm, "label2", &label2);
270:     DMRemoveLabelBySelf(dm, &label0, PETSC_TRUE);
271:     DMGetLabel(dm, "label0", &label0);
272:     DMGetLabel(dm, "label1", &label1);
276:     DMRemoveLabelBySelf(dm, &label2, PETSC_FALSE); /* this should do nothing */
278:     DMLabelDestroy(&label2);
279:     DMGetLabel(dm, "label2", &label2);
281:   }

283:   DMDestroy(&dm);
284:   PetscFinalize();
285:   return 0;
286: }

288: /*TEST

290:   test:
291:     suffix: 0
292:     nsize: {{1 2}separate output}
293:     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/blockcylinder-50.exo -interpolate
294:     requires: exodusii

296: TEST*/