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