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