xref: /petsc/src/dm/impls/plex/tests/ex6.c (revision a69119a591a03a9d906b29c0a4e9802e4d7c9795)
1 static char help[] = "Tests for DMLabel lookup\n\n";
2 
3 #include <petscdmplex.h>
4 
5 typedef struct {
6   PetscInt  debug;        /* The debugging level */
7   PetscInt  pStart, pEnd; /* The label chart */
8   PetscInt  numStrata;    /* The number of label strata */
9   PetscReal fill;         /* Percentage of label to fill */
10   PetscInt  size;         /* The number of set values */
11 } AppCtx;
12 
13 PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options) {
14   PetscFunctionBegin;
15   options->debug     = 0;
16   options->pStart    = 0;
17   options->pEnd      = 1000;
18   options->numStrata = 5;
19   options->fill      = 0.10;
20 
21   PetscOptionsBegin(comm, "", "Meshing Problem Options", "DMPLEX");
22   PetscCall(PetscOptionsBoundedInt("-debug", "The debugging level", "ex6.c", options->debug, &options->debug, NULL, 0));
23   PetscCall(PetscOptionsBoundedInt("-num_strata", "The number of label values", "ex6.c", options->numStrata, &options->numStrata, NULL, 0));
24   PetscCall(PetscOptionsBoundedInt("-pend", "The label point limit", "ex6.c", options->pEnd, &options->pEnd, NULL, 0));
25   PetscCall(PetscOptionsReal("-fill", "The percentage of label chart to set", "ex6.c", options->fill, &options->fill, NULL));
26   PetscOptionsEnd();
27   PetscFunctionReturn(0);
28 }
29 
30 PetscErrorCode TestSetup(DMLabel label, AppCtx *user) {
31   PetscRandom r;
32   PetscInt    n = (PetscInt)(user->fill * (user->pEnd - user->pStart)), i;
33 
34   PetscFunctionBegin;
35   PetscCall(PetscRandomCreate(PETSC_COMM_SELF, &r));
36   PetscCall(PetscRandomSetFromOptions(r)); /* -random_type <> */
37   PetscCall(PetscRandomSetInterval(r, user->pStart, user->pEnd));
38   PetscCall(PetscRandomSetSeed(r, 123456789L));
39   PetscCall(PetscRandomSeed(r));
40   user->size = 0;
41   for (i = 0; i < n; ++i) {
42     PetscReal p;
43     PetscInt  val;
44 
45     PetscCall(PetscRandomGetValueReal(r, &p));
46     PetscCall(DMLabelGetValue(label, (PetscInt)p, &val));
47     if (val < 0) {
48       ++user->size;
49       PetscCall(DMLabelSetValue(label, (PetscInt)p, i % user->numStrata));
50     }
51   }
52   PetscCall(PetscRandomDestroy(&r));
53   PetscCall(DMLabelCreateIndex(label, user->pStart, user->pEnd));
54   PetscCall(PetscPrintf(PETSC_COMM_SELF, "Created label with chart [%" PetscInt_FMT ", %" PetscInt_FMT ") and set %" PetscInt_FMT " values\n", user->pStart, user->pEnd, user->size));
55   PetscFunctionReturn(0);
56 }
57 
58 PetscErrorCode TestLookup(DMLabel label, AppCtx *user) {
59   const PetscInt pStart = user->pStart;
60   const PetscInt pEnd   = user->pEnd;
61   PetscInt       p, n = 0;
62 
63   PetscFunctionBegin;
64   for (p = pStart; p < pEnd; ++p) {
65     PetscInt  val;
66     PetscBool has;
67 
68     PetscCall(DMLabelGetValue(label, p, &val));
69     PetscCall(DMLabelHasPoint(label, p, &has));
70     PetscCheck((val < 0 || has) || (val >= 0 || has), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label value %" PetscInt_FMT " does not match contains check %" PetscInt_FMT " for point %" PetscInt_FMT, val, (PetscInt)has, p);
71     if (has) ++n;
72   }
73   PetscCheck(n == user->size, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of label points detected %" PetscInt_FMT " does not match number set %" PetscInt_FMT, n, user->size);
74   /* Also put in timing code */
75   PetscFunctionReturn(0);
76 }
77 
78 PetscErrorCode TestClear(DMLabel label, AppCtx *user) {
79   PetscInt pStart = user->pStart, pEnd = user->pEnd, p;
80   PetscInt defaultValue;
81 
82   PetscFunctionBegin;
83   PetscCall(DMLabelGetDefaultValue(label, &defaultValue));
84   for (p = pStart; p < pEnd; p++) {
85     PetscInt  val;
86     PetscBool hasPoint;
87 
88     PetscCall(DMLabelGetValue(label, p, &val));
89     if (val != defaultValue) PetscCall(DMLabelClearValue(label, p, val));
90     PetscCall(DMLabelGetValue(label, p, &val));
91     PetscCall(DMLabelHasPoint(label, p, &hasPoint));
92     PetscCheck(val == defaultValue, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expected default value %" PetscInt_FMT " after clearing point %" PetscInt_FMT ", got %" PetscInt_FMT, defaultValue, p, val);
93     PetscCheck(!hasPoint, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label contains %" PetscInt_FMT " after clearing", p);
94   }
95   PetscFunctionReturn(0);
96 }
97 
98 int main(int argc, char **argv) {
99   DMLabel label;
100   AppCtx  user; /* user-defined work context */
101 
102   PetscFunctionBeginUser;
103   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
104   PetscCall(ProcessOptions(PETSC_COMM_WORLD, &user));
105   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Test Label", &label));
106   PetscCall(TestSetup(label, &user));
107   PetscCall(TestLookup(label, &user));
108   PetscCall(TestClear(label, &user));
109   PetscCall(DMLabelDestroy(&label));
110   PetscCall(PetscFinalize());
111   return 0;
112 }
113 
114 /*TEST
115 
116   test:
117     suffix: 0
118     args: -malloc_dump
119   test:
120     suffix: 1
121     args: -malloc_dump -pend 10000
122   test:
123     suffix: 2
124     args: -malloc_dump -pend 10000 -fill 0.05
125   test:
126     suffix: 3
127     args: -malloc_dump -pend 10000 -fill 0.25
128 
129 TEST*/
130