xref: /petsc/src/dm/impls/plex/tests/ex6.c (revision 5f80ce2ab25dff0f4601e710601cbbcecf323266)
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");CHKERRQ(ierr);
25   CHKERRQ(PetscOptionsBoundedInt("-debug", "The debugging level", "ex6.c", options->debug, &options->debug, NULL,0));
26   CHKERRQ(PetscOptionsBoundedInt("-num_strata", "The number of label values", "ex6.c", options->numStrata, &options->numStrata, NULL,0));
27   CHKERRQ(PetscOptionsBoundedInt("-pend", "The label point limit", "ex6.c", options->pEnd, &options->pEnd, NULL,0));
28   CHKERRQ(PetscOptionsReal("-fill", "The percentage of label chart to set", "ex6.c", options->fill, &options->fill, NULL));
29   ierr = PetscOptionsEnd();CHKERRQ(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   CHKERRQ(PetscRandomCreate(PETSC_COMM_SELF, &r));
40   CHKERRQ(PetscRandomSetFromOptions(r));/* -random_type <> */
41   CHKERRQ(PetscRandomSetInterval(r, user->pStart, user->pEnd));
42   CHKERRQ(PetscRandomSetSeed(r, 123456789L));
43   CHKERRQ(PetscRandomSeed(r));
44   user->size = 0;
45   for (i = 0; i < n; ++i) {
46     PetscReal p;
47     PetscInt  val;
48 
49     CHKERRQ(PetscRandomGetValueReal(r, &p));
50     CHKERRQ(DMLabelGetValue(label, (PetscInt) p, &val));
51     if (val < 0) {
52       ++user->size;
53       CHKERRQ(DMLabelSetValue(label, (PetscInt) p, i % user->numStrata));
54     }
55   }
56   CHKERRQ(PetscRandomDestroy(&r));
57   CHKERRQ(DMLabelCreateIndex(label, user->pStart, user->pEnd));
58   CHKERRQ(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     CHKERRQ(DMLabelGetValue(label, p, &val));
74     CHKERRQ(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   PetscCheckFalse(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   CHKERRQ(DMLabelGetDefaultValue(label,&defaultValue));
90   for (p = pStart; p < pEnd; p++) {
91     PetscInt  val;
92     PetscBool hasPoint;
93 
94     CHKERRQ(DMLabelGetValue(label,p,&val));
95     if (val != defaultValue) {
96       CHKERRQ(DMLabelClearValue(label,p,val));
97     }
98     CHKERRQ(DMLabelGetValue(label,p,&val));
99     CHKERRQ(DMLabelHasPoint(label,p,&hasPoint));
100     PetscCheckFalse(val != defaultValue,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expected default value %D after clearing point %D, got %D",defaultValue,p,val);
101     PetscCheckFalse(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   PetscErrorCode ierr;
111 
112   ierr = PetscInitialize(&argc, &argv, NULL,help);if (ierr) return ierr;
113   CHKERRQ(ProcessOptions(PETSC_COMM_WORLD, &user));
114   CHKERRQ(DMLabelCreate(PETSC_COMM_SELF, "Test Label", &label));
115   CHKERRQ(TestSetup(label, &user));
116   CHKERRQ(TestLookup(label, &user));
117   CHKERRQ(TestClear(label,&user));
118   CHKERRQ(DMLabelDestroy(&label));
119   ierr = PetscFinalize();
120   return ierr;
121 }
122 
123 /*TEST
124 
125   test:
126     suffix: 0
127     args: -malloc_dump
128   test:
129     suffix: 1
130     args: -malloc_dump -pend 10000
131   test:
132     suffix: 2
133     args: -malloc_dump -pend 10000 -fill 0.05
134   test:
135     suffix: 3
136     args: -malloc_dump -pend 10000 -fill 0.25
137 
138 TEST*/
139