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;

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

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

173:   ViewLabels(dm, PETSC_VIEWER_STDOUT_WORLD);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

257:     DMDestroy(&dm1);
258:   }

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

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

287: /*TEST

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

295: TEST*/