xref: /petsc/src/dm/impls/plex/tests/ex6.c (revision 48a46eb9bd028bec07ec0f396b1a3abb43f14558)
1c4762a1bSJed Brown static char help[] = "Tests for DMLabel lookup\n\n";
2c4762a1bSJed Brown 
3c4762a1bSJed Brown #include <petscdmplex.h>
4c4762a1bSJed Brown 
5c4762a1bSJed Brown typedef struct {
6c4762a1bSJed Brown   PetscInt  debug;        /* The debugging level */
7c4762a1bSJed Brown   PetscInt  pStart, pEnd; /* The label chart */
8c4762a1bSJed Brown   PetscInt  numStrata;    /* The number of label strata */
9c4762a1bSJed Brown   PetscReal fill;         /* Percentage of label to fill */
10c4762a1bSJed Brown   PetscInt  size;         /* The number of set values */
11c4762a1bSJed Brown } AppCtx;
12c4762a1bSJed Brown 
139371c9d4SSatish Balay PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options) {
14c4762a1bSJed Brown   PetscFunctionBegin;
15c4762a1bSJed Brown   options->debug     = 0;
16c4762a1bSJed Brown   options->pStart    = 0;
17c4762a1bSJed Brown   options->pEnd      = 1000;
18c4762a1bSJed Brown   options->numStrata = 5;
19c4762a1bSJed Brown   options->fill      = 0.10;
20c4762a1bSJed Brown 
21d0609cedSBarry Smith   PetscOptionsBegin(comm, "", "Meshing Problem Options", "DMPLEX");
229566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-debug", "The debugging level", "ex6.c", options->debug, &options->debug, NULL, 0));
239566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-num_strata", "The number of label values", "ex6.c", options->numStrata, &options->numStrata, NULL, 0));
249566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-pend", "The label point limit", "ex6.c", options->pEnd, &options->pEnd, NULL, 0));
259566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-fill", "The percentage of label chart to set", "ex6.c", options->fill, &options->fill, NULL));
26d0609cedSBarry Smith   PetscOptionsEnd();
27c4762a1bSJed Brown   PetscFunctionReturn(0);
28c4762a1bSJed Brown }
29c4762a1bSJed Brown 
309371c9d4SSatish Balay PetscErrorCode TestSetup(DMLabel label, AppCtx *user) {
31c4762a1bSJed Brown   PetscRandom r;
32c4762a1bSJed Brown   PetscInt    n = (PetscInt)(user->fill * (user->pEnd - user->pStart)), i;
33c4762a1bSJed Brown 
34c4762a1bSJed Brown   PetscFunctionBegin;
359566063dSJacob Faibussowitsch   PetscCall(PetscRandomCreate(PETSC_COMM_SELF, &r));
369566063dSJacob Faibussowitsch   PetscCall(PetscRandomSetFromOptions(r)); /* -random_type <> */
379566063dSJacob Faibussowitsch   PetscCall(PetscRandomSetInterval(r, user->pStart, user->pEnd));
389566063dSJacob Faibussowitsch   PetscCall(PetscRandomSetSeed(r, 123456789L));
399566063dSJacob Faibussowitsch   PetscCall(PetscRandomSeed(r));
40c4762a1bSJed Brown   user->size = 0;
41c4762a1bSJed Brown   for (i = 0; i < n; ++i) {
42c4762a1bSJed Brown     PetscReal p;
43c4762a1bSJed Brown     PetscInt  val;
44c4762a1bSJed Brown 
459566063dSJacob Faibussowitsch     PetscCall(PetscRandomGetValueReal(r, &p));
469566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValue(label, (PetscInt)p, &val));
47c4762a1bSJed Brown     if (val < 0) {
48c4762a1bSJed Brown       ++user->size;
499566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(label, (PetscInt)p, i % user->numStrata));
50c4762a1bSJed Brown     }
51c4762a1bSJed Brown   }
529566063dSJacob Faibussowitsch   PetscCall(PetscRandomDestroy(&r));
539566063dSJacob Faibussowitsch   PetscCall(DMLabelCreateIndex(label, user->pStart, user->pEnd));
5463a3b9bcSJacob Faibussowitsch   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));
55c4762a1bSJed Brown   PetscFunctionReturn(0);
56c4762a1bSJed Brown }
57c4762a1bSJed Brown 
589371c9d4SSatish Balay PetscErrorCode TestLookup(DMLabel label, AppCtx *user) {
59c4762a1bSJed Brown   const PetscInt pStart = user->pStart;
60c4762a1bSJed Brown   const PetscInt pEnd   = user->pEnd;
61c4762a1bSJed Brown   PetscInt       p, n = 0;
62c4762a1bSJed Brown 
63c4762a1bSJed Brown   PetscFunctionBegin;
64c4762a1bSJed Brown   for (p = pStart; p < pEnd; ++p) {
65c4762a1bSJed Brown     PetscInt  val;
66c4762a1bSJed Brown     PetscBool has;
67c4762a1bSJed Brown 
689566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValue(label, p, &val));
699566063dSJacob Faibussowitsch     PetscCall(DMLabelHasPoint(label, p, &has));
701dca8a05SBarry Smith     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);
71c4762a1bSJed Brown     if (has) ++n;
72c4762a1bSJed Brown   }
7363a3b9bcSJacob Faibussowitsch   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);
74c4762a1bSJed Brown   /* Also put in timing code */
75c4762a1bSJed Brown   PetscFunctionReturn(0);
76c4762a1bSJed Brown }
77c4762a1bSJed Brown 
789371c9d4SSatish Balay PetscErrorCode TestClear(DMLabel label, AppCtx *user) {
79c4762a1bSJed Brown   PetscInt pStart = user->pStart, pEnd = user->pEnd, p;
80c4762a1bSJed Brown   PetscInt defaultValue;
81c4762a1bSJed Brown 
82c4762a1bSJed Brown   PetscFunctionBegin;
839566063dSJacob Faibussowitsch   PetscCall(DMLabelGetDefaultValue(label, &defaultValue));
84c4762a1bSJed Brown   for (p = pStart; p < pEnd; p++) {
85c4762a1bSJed Brown     PetscInt  val;
86c4762a1bSJed Brown     PetscBool hasPoint;
87c4762a1bSJed Brown 
889566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValue(label, p, &val));
89*48a46eb9SPierre Jolivet     if (val != defaultValue) PetscCall(DMLabelClearValue(label, p, val));
909566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValue(label, p, &val));
919566063dSJacob Faibussowitsch     PetscCall(DMLabelHasPoint(label, p, &hasPoint));
9263a3b9bcSJacob Faibussowitsch     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);
9363a3b9bcSJacob Faibussowitsch     PetscCheck(!hasPoint, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Label contains %" PetscInt_FMT " after clearing", p);
94c4762a1bSJed Brown   }
95c4762a1bSJed Brown   PetscFunctionReturn(0);
96c4762a1bSJed Brown }
97c4762a1bSJed Brown 
989371c9d4SSatish Balay int main(int argc, char **argv) {
99c4762a1bSJed Brown   DMLabel label;
100c4762a1bSJed Brown   AppCtx  user; /* user-defined work context */
101c4762a1bSJed Brown 
102327415f7SBarry Smith   PetscFunctionBeginUser;
1039566063dSJacob Faibussowitsch   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
1049566063dSJacob Faibussowitsch   PetscCall(ProcessOptions(PETSC_COMM_WORLD, &user));
1059566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "Test Label", &label));
1069566063dSJacob Faibussowitsch   PetscCall(TestSetup(label, &user));
1079566063dSJacob Faibussowitsch   PetscCall(TestLookup(label, &user));
1089566063dSJacob Faibussowitsch   PetscCall(TestClear(label, &user));
1099566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&label));
1109566063dSJacob Faibussowitsch   PetscCall(PetscFinalize());
111b122ec5aSJacob Faibussowitsch   return 0;
112c4762a1bSJed Brown }
113c4762a1bSJed Brown 
114c4762a1bSJed Brown /*TEST
115c4762a1bSJed Brown 
116c4762a1bSJed Brown   test:
117c4762a1bSJed Brown     suffix: 0
118c4762a1bSJed Brown     args: -malloc_dump
119c4762a1bSJed Brown   test:
120c4762a1bSJed Brown     suffix: 1
121c4762a1bSJed Brown     args: -malloc_dump -pend 10000
122c4762a1bSJed Brown   test:
123c4762a1bSJed Brown     suffix: 2
124c4762a1bSJed Brown     args: -malloc_dump -pend 10000 -fill 0.05
125c4762a1bSJed Brown   test:
126c4762a1bSJed Brown     suffix: 3
127c4762a1bSJed Brown     args: -malloc_dump -pend 10000 -fill 0.25
128c4762a1bSJed Brown 
129c4762a1bSJed Brown TEST*/
130