Actual source code: ex1.c
petsc-3.13.6 2020-09-29
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: IS labelIS;
10: const char *labelName;
11: PetscInt numLabels, l;
15: /* query the number and name of labels*/
16: DMGetNumLabels(dm, &numLabels);
17: PetscViewerASCIIPrintf(viewer, "Number of labels: %d\n", numLabels);
18: for (l = 0; l < numLabels; ++l) {
19: DMGetLabelName(dm, l, &labelName);
20: PetscViewerASCIIPrintf(viewer, "Label %d: name: %s\n", l, labelName);
21: PetscViewerASCIIPrintf(viewer, "IS of values\n");
22: DMGetLabel(dm, labelName, &label);
23: DMLabelGetValueIS(label, &labelIS);
24: PetscViewerASCIIPushTab(viewer);
25: ISView(labelIS, viewer);
26: PetscViewerASCIIPopTab(viewer);
27: ISDestroy(&labelIS);
28: PetscViewerASCIIPrintf(viewer, "\n");
29: }
30: /* Making sure that string literals work */
31: PetscViewerASCIIPrintf(viewer,"\n\nCell Set label IS\n");
32: DMGetLabel(dm, "Cell Sets", &label);
33: if (label) {
34: DMLabelGetValueIS(label, &labelIS);
35: ISView(labelIS, viewer);
36: ISDestroy(&labelIS);
37: }
38: return(0);
39: }
41: int main(int argc, char **argv)
42: {
43: DM dm, dmDist;
44: char filename[PETSC_MAX_PATH_LEN]="";
45: PetscBool interpolate = PETSC_FALSE;
48: /* initialize and get options */
49: PetscInitialize(&argc, &argv, NULL, help);if (ierr) return ierr;
50: PetscOptionsBegin(PETSC_COMM_WORLD, NULL, "DMLabel ex1 Options", "DMLabel");
51: PetscOptionsString("-i", "filename to read", "ex1.c", filename, filename, sizeof(filename), NULL);
52: PetscOptionsBool("-interpolate", "Generate intermediate mesh elements", "ex1.c", interpolate, &interpolate, NULL);
53: PetscOptionsEnd();
55: /* create and distribute DM */
56: DMPlexCreateFromFile(PETSC_COMM_WORLD, filename, interpolate, &dm);
57: DMPlexDistribute(dm, 0, NULL, &dmDist);
58: if (dmDist) {
59: DMDestroy(&dm);
60: dm = dmDist;
61: }
62: DMSetFromOptions(dm);
64: /* add custom labels to test adding/removal */
65: {
66: DMLabel label0, label1, label2, label3;
67: PetscInt p, pStart, pEnd;
68: DMPlexGetChart(dm, &pStart, &pEnd);
69: /* create label in DM and get from DM */
70: DMCreateLabel(dm, "label0");
71: DMGetLabel(dm, "label0", &label0);
72: /* alternative: create standalone label and add to DM; needs to be destroyed */
73: DMLabelCreate(PETSC_COMM_SELF, "label1", &label1);
74: DMAddLabel(dm, label1);
76: pEnd = pStart + (pEnd-pStart)/3; /* we will mark the first third of points */
77: for (p=pStart; p < pEnd; p++) {
78: DMLabelSetValue(label0, p, 1);
79: DMLabelSetValue(label1, p, 2);
80: }
81: /* duplicate label */
82: DMLabelDuplicate(label0, &label2);
83: DMLabelDuplicate(label1, &label3);
84: PetscObjectSetName((PetscObject)label2, "label2");
85: PetscObjectSetName((PetscObject)label3, "label3");
86: DMAddLabel(dm, label2);
87: DMAddLabel(dm, label3);
88: /* remove the labels in this scope */
89: DMLabelDestroy(&label1);
90: DMLabelDestroy(&label2);
91: DMLabelDestroy(&label3);
92: }
94: ViewLabels(dm, PETSC_VIEWER_STDOUT_WORLD);
96: /* remove label0 and label1 just to test manual removal; let label3 be removed automatically by DMDestroy() */
97: {
98: DMLabel label0, label1, label2;
99: DMGetLabel(dm, "label0", &label0);
100: DMGetLabel(dm, "label1", &label1);
101: if (!label0) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "label0 must not be NULL now");
102: if (!label1) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "label1 must not be NULL now");
103: DMRemoveLabel(dm, "label1", NULL);
104: DMRemoveLabel(dm, "label2", &label2);
105: DMRemoveLabelBySelf(dm, &label0, PETSC_TRUE);
106: DMGetLabel(dm, "label0", &label0);
107: DMGetLabel(dm, "label1", &label1);
108: if (label0) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "label0 must be NULL now");
109: if (label1) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "label1 must be NULL now");
110: if (!label2) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "label2 must not be NULL now");
111: DMRemoveLabelBySelf(dm, &label2, PETSC_FALSE); /* this should do nothing */
112: if (!label2) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "label2 must not be NULL now");
113: DMLabelDestroy(&label2);
114: DMGetLabel(dm, "label2", &label2);
115: if (label2) SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "label2 must be NULL now");
116: }
118: DMDestroy(&dm);
119: PetscFinalize();
120: return ierr;
121: }
123: /*TEST
125: test:
126: suffix: 0
127: args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/blockcylinder-50.exo -interpolate
128: requires: exodusii
130: TEST*/