xref: /petsc/src/dm/impls/plex/tests/ex11.c (revision 2da392cc7c10228af19ad9843ce5155178acb644)
1 static char help[] = "Tests for DMLabel\n\n";
2 
3 #include <petscdmplex.h>
4 
5 static PetscErrorCode TestInsertion()
6 {
7   DMLabel        label, label2;
8   const PetscInt values[5] = {0, 3, 4, -1, 176}, N = 10000;
9   PetscInt       i, v;
10   PetscErrorCode ierr;
11 
12   PetscFunctionBegin;
13   ierr = DMLabelCreate(PETSC_COMM_SELF, "Test Label", &label);CHKERRQ(ierr);
14   ierr = DMLabelSetDefaultValue(label, -100);CHKERRQ(ierr);
15   for (i = 0; i < N; ++i) {
16     ierr = DMLabelSetValue(label, i, values[i%5]);CHKERRQ(ierr);
17   }
18   /* Test get in hash mode */
19   for (i = 0; i < N; ++i) {
20     PetscInt val;
21 
22     ierr = DMLabelGetValue(label, i, &val);CHKERRQ(ierr);
23     if (val != values[i%5]) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Value %d for point %d should be %d", val, i, values[i%5]);
24   }
25   /* Test stratum */
26   for (v = 0; v < 5; ++v) {
27     IS              stratum;
28     const PetscInt *points;
29     PetscInt        n;
30 
31     ierr = DMLabelGetStratumIS(label, values[v], &stratum);CHKERRQ(ierr);
32     if (!stratum) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Stratum %d is empty!", v);
33     ierr = ISGetIndices(stratum, &points);CHKERRQ(ierr);
34     ierr = ISGetLocalSize(stratum, &n);CHKERRQ(ierr);
35     for (i = 0; i < n; ++i) {
36       if (points[i] != i*5+v) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Point %d should be %d", points[i], i*5+v);
37     }
38     ierr = ISRestoreIndices(stratum, &points);CHKERRQ(ierr);
39     ierr = ISDestroy(&stratum);CHKERRQ(ierr);
40   }
41   /* Test get in array mode */
42   for (i = 0; i < N; ++i) {
43     PetscInt val;
44 
45     ierr = DMLabelGetValue(label, i, &val);CHKERRQ(ierr);
46     if (val != values[i%5]) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Value %d should be %d", val, values[i%5]);
47   }
48   /* Test Duplicate */
49   ierr = DMLabelDuplicate(label, &label2);CHKERRQ(ierr);
50   for (i = 0; i < N; ++i) {
51     PetscInt val;
52 
53     ierr = DMLabelGetValue(label2, i, &val);CHKERRQ(ierr);
54     if (val != values[i%5]) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Value %d should be %d", val, values[i%5]);
55   }
56   ierr = DMLabelDestroy(&label2);CHKERRQ(ierr);
57   ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
58   PetscFunctionReturn(0);
59 }
60 
61 static PetscErrorCode TestEmptyStrata(MPI_Comm comm)
62 {
63   DM               dm, dmDist;
64   PetscPartitioner part;
65   PetscInt         c0[6]  = {2,3,6,7,9,11};
66   PetscInt         c1[6]  = {4,5,7,8,10,12};
67   PetscInt         c2[4]  = {13,15,19,21};
68   PetscInt         c3[4]  = {14,16,20,22};
69   PetscInt         c4[4]  = {15,17,21,23};
70   PetscInt         c5[4]  = {16,18,22,24};
71   PetscInt         c6[4]  = {13,14,19,20};
72   PetscInt         c7[4]  = {15,16,21,22};
73   PetscInt         c8[4]  = {17,18,23,24};
74   PetscInt         c9[4]  = {13,14,15,16};
75   PetscInt         c10[4] = {15,16,17,18};
76   PetscInt         c11[4] = {19,20,21,22};
77   PetscInt         c12[4] = {21,22,23,24};
78   PetscInt         dim    = 3;
79   PetscMPIInt      rank;
80   PetscErrorCode   ierr;
81 
82   PetscFunctionBegin;
83   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
84   /* A 3D box with two adjacent cells, sharing one face and four vertices */
85   ierr = DMCreate(comm, &dm);CHKERRQ(ierr);
86   ierr = DMSetType(dm, DMPLEX);CHKERRQ(ierr);
87   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
88   if (!rank) {
89     ierr = DMPlexSetChart(dm, 0, 25);CHKERRQ(ierr);
90     ierr = DMPlexSetConeSize(dm, 0, 6);CHKERRQ(ierr);
91     ierr = DMPlexSetConeSize(dm, 1, 6);CHKERRQ(ierr);
92     ierr = DMPlexSetConeSize(dm, 2, 4);CHKERRQ(ierr);
93     ierr = DMPlexSetConeSize(dm, 3, 4);CHKERRQ(ierr);
94     ierr = DMPlexSetConeSize(dm, 4, 4);CHKERRQ(ierr);
95     ierr = DMPlexSetConeSize(dm, 5, 4);CHKERRQ(ierr);
96     ierr = DMPlexSetConeSize(dm, 6, 4);CHKERRQ(ierr);
97     ierr = DMPlexSetConeSize(dm, 7, 4);CHKERRQ(ierr);
98     ierr = DMPlexSetConeSize(dm, 8, 4);CHKERRQ(ierr);
99     ierr = DMPlexSetConeSize(dm, 9, 4);CHKERRQ(ierr);
100     ierr = DMPlexSetConeSize(dm, 10, 4);CHKERRQ(ierr);
101     ierr = DMPlexSetConeSize(dm, 11, 4);CHKERRQ(ierr);
102     ierr = DMPlexSetConeSize(dm, 12, 4);CHKERRQ(ierr);
103   }
104   ierr = DMSetUp(dm);CHKERRQ(ierr);
105   if (!rank) {
106     ierr = DMPlexSetCone(dm, 0, c0);CHKERRQ(ierr);
107     ierr = DMPlexSetCone(dm, 1, c1);CHKERRQ(ierr);
108     ierr = DMPlexSetCone(dm, 2, c2);CHKERRQ(ierr);
109     ierr = DMPlexSetCone(dm, 3, c3);CHKERRQ(ierr);
110     ierr = DMPlexSetCone(dm, 4, c4);CHKERRQ(ierr);
111     ierr = DMPlexSetCone(dm, 5, c5);CHKERRQ(ierr);
112     ierr = DMPlexSetCone(dm, 6, c6);CHKERRQ(ierr);
113     ierr = DMPlexSetCone(dm, 7, c7);CHKERRQ(ierr);
114     ierr = DMPlexSetCone(dm, 8, c8);CHKERRQ(ierr);
115     ierr = DMPlexSetCone(dm, 9, c9);CHKERRQ(ierr);
116     ierr = DMPlexSetCone(dm, 10, c10);CHKERRQ(ierr);
117     ierr = DMPlexSetCone(dm, 11, c11);CHKERRQ(ierr);
118     ierr = DMPlexSetCone(dm, 12, c12);CHKERRQ(ierr);
119   }
120   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
121   /* Create a user managed depth label, so that we can leave out edges */
122   {
123     DMLabel label;
124     PetscInt numValues, maxValues = 0, v;
125 
126     ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
127     ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
128     if (!rank) {
129       PetscInt i;
130 
131       for (i = 0; i < 25; ++i) {
132         if (i < 2)       {ierr = DMLabelSetValue(label, i, 3);CHKERRQ(ierr);}
133         else if (i < 13) {ierr = DMLabelSetValue(label, i, 2);CHKERRQ(ierr);}
134         else             {
135           if (i==13) {ierr = DMLabelAddStratum(label, 1);CHKERRQ(ierr);}
136           ierr = DMLabelSetValue(label, i, 0);CHKERRQ(ierr);
137         }
138       }
139     }
140     ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
141     ierr = MPI_Allreduce(&numValues, &maxValues, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
142     for (v = numValues; v < maxValues; ++v) {ierr = DMLabelAddStratum(label,v);CHKERRQ(ierr);}
143   }
144   {
145     DMLabel label;
146     ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
147     ierr = DMLabelView(label, PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr);
148   }
149   ierr = DMPlexGetPartitioner(dm,&part);CHKERRQ(ierr);
150   ierr = PetscPartitionerSetFromOptions(part);CHKERRQ(ierr);
151   ierr = DMPlexDistribute(dm, 1, NULL, &dmDist);CHKERRQ(ierr);
152   if (dmDist) {
153     ierr = DMDestroy(&dm);CHKERRQ(ierr);
154     dm   = dmDist;
155   }
156   {
157     DMLabel label;
158     ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
159     ierr = DMLabelView(label, PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr);
160   }
161   /* Create a cell vector */
162   {
163     Vec          v;
164     PetscSection s;
165     PetscInt     numComp[] = {1};
166     PetscInt     dof[]     = {0,0,0,1};
167     PetscInt     N;
168 
169     ierr = DMSetNumFields(dm, 1);CHKERRQ(ierr);
170     ierr = DMPlexCreateSection(dm, NULL, numComp, dof, 0, NULL, NULL, NULL, NULL, &s);CHKERRQ(ierr);
171     ierr = DMSetLocalSection(dm, s);CHKERRQ(ierr);
172     ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
173     ierr = DMCreateGlobalVector(dm, &v);CHKERRQ(ierr);
174     ierr = VecGetSize(v, &N);CHKERRQ(ierr);
175     if (N != 2) {
176       ierr = DMView(dm, PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr);
177       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "FAIL: Vector size %d != 2\n", N);
178     }
179     ierr = VecDestroy(&v);CHKERRQ(ierr);
180   }
181   ierr = DMDestroy(&dm);CHKERRQ(ierr);
182   PetscFunctionReturn(0);
183 }
184 
185 static PetscErrorCode TestDistribution(MPI_Comm comm)
186 {
187   DM               dm, dmDist;
188   PetscPartitioner part;
189   DMLabel          label;
190   char             filename[2048];
191   const char      *name    = "test label";
192   PetscInt         overlap = 0, cStart, cEnd, c;
193   PetscMPIInt      rank;
194   PetscBool        flg;
195   PetscErrorCode   ierr;
196 
197   PetscFunctionBegin;
198   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
199   ierr = PetscOptionsGetString(NULL, NULL, "-filename", filename, sizeof(filename), &flg);CHKERRQ(ierr);
200   if (!flg) PetscFunctionReturn(0);
201   ierr = PetscOptionsGetInt(NULL, NULL, "-overlap", &overlap, NULL);CHKERRQ(ierr);
202   ierr = DMPlexCreateFromFile(comm, filename, PETSC_TRUE, &dm);CHKERRQ(ierr);
203   ierr = DMSetBasicAdjacency(dm, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
204   ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
205   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
206   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
207   for (c = cStart; c < cEnd; ++c) {
208     ierr = DMLabelSetValue(label, c, c);CHKERRQ(ierr);
209   }
210   ierr = DMLabelView(label, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
211   ierr = DMPlexGetPartitioner(dm,&part);CHKERRQ(ierr);
212   ierr = PetscPartitionerSetFromOptions(part);CHKERRQ(ierr);
213   ierr = DMPlexDistribute(dm, overlap, NULL, &dmDist);CHKERRQ(ierr);
214   if (dmDist) {
215     ierr = DMDestroy(&dm);CHKERRQ(ierr);
216     dm   = dmDist;
217   }
218   ierr = DMViewFromOptions(dm, NULL, "-dm_view");CHKERRQ(ierr);
219   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
220   ierr = DMLabelView(label, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
221   ierr = DMDestroy(&dm);CHKERRQ(ierr);
222   PetscFunctionReturn(0);
223 }
224 
225 int main(int argc, char **argv)
226 {
227   PetscErrorCode ierr;
228 
229   ierr = PetscInitialize(&argc, &argv, NULL, help);if (ierr) return ierr;
230   /*ierr = ProcessOptions(PETSC_COMM_WORLD, &user);CHKERRQ(ierr);*/
231   ierr = TestInsertion();CHKERRQ(ierr);
232   ierr = TestEmptyStrata(PETSC_COMM_WORLD);CHKERRQ(ierr);
233   ierr = TestDistribution(PETSC_COMM_WORLD);CHKERRQ(ierr);
234   ierr = PetscFinalize();
235   return ierr;
236 }
237 
238 /*TEST
239 
240   test:
241     suffix: 0
242   test:
243     suffix: 1
244     nsize: 2
245     args: -petscpartitioner_type simple
246 
247   testset:
248     suffix: gmsh
249     args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/square.msh -petscpartitioner_type simple
250     test:
251       suffix: 1
252       nsize: 1
253     test:
254       suffix: 2
255       nsize: 2
256 
257   testset:
258     suffix: exodusii
259     requires: exodusii
260     args: -filename ${wPETSC_DIR}/share/petsc/datafiles/meshes/2Dgrd.exo -petscpartitioner_type simple
261     test:
262       suffix: 1
263       nsize: 1
264     test:
265       suffix: 2
266       nsize: 2
267 
268 TEST*/
269