xref: /petsc/src/dm/impls/plex/tests/ex47.c (revision 6e415bd26dfd0d33b8d7233bdee8a51de4844a26)
1 static char help[] = "The main goal of this code is to retrieve the original element numbers as found in the "
2                      "initial partitions (sInitialPartition)... but after the call to DMPlexDistribute";
3 
4 #include <petsc.h>
5 
6 /* Coordinates of a 2x5 rectangular mesh of quads : */
7 PetscReal sCoords2x5Mesh[18][2] = {
8   {0.00000000000000000e+00, 0.00000000000000000e+00},
9   {2.00000000000000000e+00, 0.00000000000000000e+00},
10   {0.00000000000000000e+00, 1.00000000000000000e+00},
11   {2.00000000000000000e+00, 1.00000000000000000e+00},
12   {9.99999999997387978e-01, 0.00000000000000000e+00},
13   {9.99999999997387978e-01, 1.00000000000000000e+00},
14   {0.00000000000000000e+00, 2.00000000000000011e-01},
15   {0.00000000000000000e+00, 4.00000000000000022e-01},
16   {0.00000000000000000e+00, 5.99999999999999978e-01},
17   {0.00000000000000000e+00, 8.00000000000000044e-01},
18   {2.00000000000000000e+00, 2.00000000000000011e-01},
19   {2.00000000000000000e+00, 4.00000000000000022e-01},
20   {2.00000000000000000e+00, 5.99999999999999978e-01},
21   {2.00000000000000000e+00, 8.00000000000000044e-01},
22   {9.99999999997387756e-01, 2.00000000000000011e-01},
23   {9.99999999997387978e-01, 4.00000000000000022e-01},
24   {9.99999999997387978e-01, 6.00000000000000089e-01},
25   {9.99999999997388089e-01, 8.00000000000000044e-01}
26 };
27 
28 /* Connectivity of a 2x5 rectangular mesh of quads : */
29 const PetscInt sConnectivity2x5Mesh[10][4] = {
30   {0,  4,  14, 6 },
31   {6,  14, 15, 7 },
32   {7,  15, 16, 8 },
33   {8,  16, 17, 9 },
34   {9,  17, 5,  2 },
35   {4,  1,  10, 14},
36   {14, 10, 11, 15},
37   {15, 11, 12, 16},
38   {16, 12, 13, 17},
39   {17, 13, 3,  5 }
40 };
41 
42 /* Partitions of a 2x5 rectangular mesh of quads : */
43 const PetscInt sInitialPartition2x5Mesh[2][5] = {
44   {0, 2, 4, 6, 8},
45   {1, 3, 5, 7, 9}
46 };
47 
48 const PetscInt sNLoclCells2x5Mesh = 5;
49 const PetscInt sNGlobVerts2x5Mesh = 18;
50 
51 /* Mixed mesh : quads and triangles  (4 first quads above divided into triangles*/
52 /* Connectivity of a 2x5 rectangular mesh of quads : */
53 const PetscInt sConnectivityMixedTQMesh[14][4] = {
54   {0,  4,  6,  -1},
55   {4,  14, 6,  -1},
56   {6,  14, 7,  -1},
57   {14, 15, 7,  -1},
58   {7,  15, 8,  -1},
59   {15, 16, 8,  -1},
60   {8,  16, 9,  -1},
61   {16, 17, 9,  -1},
62   {9,  17, 5,  2 },
63   {4,  1,  10, 14},
64   {14, 10, 11, 15},
65   {15, 11, 12, 16},
66   {16, 12, 13, 17},
67   {17, 13, 3,  5 }
68 };
69 
70 /* Partitions for the rectangular mesh of quads and triangles: */
71 const PetscInt sInitialPartitionMixedTQMesh[2][7] = {
72   {0, 1, 4, 5, 8, 10, 12},
73   {2, 3, 6, 7, 9, 11, 13}
74 };
75 
76 const PetscInt sNLoclCellsMixedTQMesh = 7;
77 const PetscInt sNGlobVertsMixedTQMesh = 18;
78 
79 /* Prisms mesh */
80 PetscReal sCoordsPrismsMesh[125][3] = {
81   {2.24250931694056355e-01, 0.00000000000000000e+00, 0.00000000000000000e+00},
82   {2.20660660151932697e-01, 2.87419338850266937e-01, 0.00000000000000000e+00},
83   {0.00000000000000000e+00, 0.00000000000000000e+00, 2.70243537720639027e-01},
84   {2.32445727460992402e-01, 0.00000000000000000e+00, 2.60591845015572310e-01},
85   {2.41619971105419079e-01, 2.69894910706158231e-01, 2.42844781736072490e-01},
86   {0.00000000000000000e+00, 2.46523339883120779e-01, 2.69072907562752262e-01},
87   {0.00000000000000000e+00, 0.00000000000000000e+00, 0.00000000000000000e+00},
88   {1.00000000000000000e+00, 2.75433417601945563e-01, 0.00000000000000000e+00},
89   {1.00000000000000000e+00, 0.00000000000000000e+00, 2.33748605950385602e-01},
90   {7.32445727460992457e-01, 0.00000000000000000e+00, 2.42344379130445597e-01},
91   {1.00000000000000000e+00, 2.78258478013028610e-01, 2.57379172987105553e-01},
92   {1.00000000000000000e+00, 0.00000000000000000e+00, 0.00000000000000000e+00},
93   {7.49586880891153995e-01, 1.00000000000000000e+00, 0.00000000000000000e+00},
94   {1.00000000000000000e+00, 1.00000000000000000e+00, 2.51949651266657582e-01},
95   {7.41619971105419107e-01, 7.69894910706158120e-01, 2.33697838509081768e-01},
96   {1.00000000000000000e+00, 7.78258478013028610e-01, 2.66479695645241543e-01},
97   {7.55042653233710115e-01, 1.00000000000000000e+00, 2.58019637386860512e-01},
98   {1.00000000000000000e+00, 1.00000000000000000e+00, 0.00000000000000000e+00},
99   {0.00000000000000000e+00, 7.59235710423095789e-01, 0.00000000000000000e+00},
100   {0.00000000000000000e+00, 1.00000000000000000e+00, 2.17232187874490473e-01},
101   {0.00000000000000000e+00, 7.46523339883120807e-01, 2.42567232639677999e-01},
102   {2.55042653233710115e-01, 1.00000000000000000e+00, 2.40660905690776916e-01},
103   {0.00000000000000000e+00, 1.00000000000000000e+00, 0.00000000000000000e+00},
104   {2.38934376044866809e-01, 0.00000000000000000e+00, 1.00000000000000000e+00},
105   {2.18954188589218168e-01, 2.26916038449581692e-01, 1.00000000000000000e+00},
106   {2.39787449636397643e-01, 0.00000000000000000e+00, 7.60591845015572310e-01},
107   {2.40766735324061815e-01, 2.39643260505815608e-01, 7.42844781736072490e-01},
108   {0.00000000000000000e+00, 2.57448248192627016e-01, 7.69072907562752262e-01},
109   {0.00000000000000000e+00, 0.00000000000000000e+00, 1.00000000000000000e+00},
110   {1.00000000000000000e+00, 2.38666970143638080e-01, 1.00000000000000000e+00},
111   {7.39787449636397643e-01, 0.00000000000000000e+00, 7.42344379130445597e-01},
112   {1.00000000000000000e+00, 2.59875254283874868e-01, 7.57379172987105553e-01},
113   {1.00000000000000000e+00, 0.00000000000000000e+00, 1.00000000000000000e+00},
114   {7.76318984007844159e-01, 1.00000000000000000e+00, 1.00000000000000000e+00},
115   {7.40766735324061787e-01, 7.39643260505815636e-01, 7.33697838509081768e-01},
116   {1.00000000000000000e+00, 7.59875254283874924e-01, 7.66479695645241543e-01},
117   {7.68408704792055142e-01, 1.00000000000000000e+00, 7.58019637386860512e-01},
118   {1.00000000000000000e+00, 1.00000000000000000e+00, 1.00000000000000000e+00},
119   {0.00000000000000000e+00, 7.81085527042108207e-01, 1.00000000000000000e+00},
120   {0.00000000000000000e+00, 7.57448248192627016e-01, 7.42567232639678054e-01},
121   {2.68408704792055197e-01, 1.00000000000000000e+00, 7.40660905690776916e-01},
122   {0.00000000000000000e+00, 1.00000000000000000e+00, 1.00000000000000000e+00},
123   {7.24250931694056410e-01, 0.00000000000000000e+00, 0.00000000000000000e+00},
124   {7.24250931694056410e-01, 2.75433417601945563e-01, 0.00000000000000000e+00},
125   {4.44911591845989052e-01, 2.87419338850266937e-01, 0.00000000000000000e+00},
126   {4.64891454921984804e-01, 0.00000000000000000e+00, 2.50940152310505593e-01},
127   {4.74065698566411453e-01, 2.69894910706158231e-01, 2.33193089031005774e-01},
128   {4.48501863388112709e-01, 0.00000000000000000e+00, 0.00000000000000000e+00},
129   {7.20660660151932753e-01, 7.87419338850266937e-01, 0.00000000000000000e+00},
130   {7.20660660151932753e-01, 5.62852756452212555e-01, 0.00000000000000000e+00},
131   {2.20660660151932697e-01, 5.46655049273362614e-01, 0.00000000000000000e+00},
132   {4.83239942210838158e-01, 5.39789821412316462e-01, 2.15446025751505982e-01},
133   {7.41619971105419107e-01, 5.48153388719186951e-01, 2.48227882887665785e-01},
134   {2.41619971105419079e-01, 5.16418250589278927e-01, 2.41674151578185781e-01},
135   {4.41321320303865394e-01, 5.74838677700533873e-01, 0.00000000000000000e+00},
136   {1.00000000000000000e+00, 7.75433417601945507e-01, 0.00000000000000000e+00},
137   {1.00000000000000000e+00, 5.56516956026057219e-01, 2.81009740023825560e-01},
138   {7.32445727460992457e-01, 2.78258478013028610e-01, 2.65974946167165549e-01},
139   {1.00000000000000000e+00, 5.50866835203891125e-01, 0.00000000000000000e+00},
140   {0.00000000000000000e+00, 2.59235710423095733e-01, 0.00000000000000000e+00},
141   {0.00000000000000000e+00, 4.93046679766241558e-01, 2.67902277404865552e-01},
142   {2.55042653233710115e-01, 7.46523339883120807e-01, 2.65995950455964469e-01},
143   {0.00000000000000000e+00, 5.18471420846191466e-01, 0.00000000000000000e+00},
144   {2.49586880891154023e-01, 1.00000000000000000e+00, 0.00000000000000000e+00},
145   {2.49586880891154023e-01, 7.59235710423095789e-01, 0.00000000000000000e+00},
146   {4.70247541043086748e-01, 7.87419338850266937e-01, 0.00000000000000000e+00},
147   {5.10085306467420230e-01, 1.00000000000000000e+00, 2.64089623507063387e-01},
148   {4.96662624339129222e-01, 7.69894910706158231e-01, 2.39767824629284698e-01},
149   {4.99173761782308045e-01, 1.00000000000000000e+00, 0.00000000000000000e+00},
150   {0.00000000000000000e+00, 0.00000000000000000e+00, 7.70243537720639027e-01},
151   {2.40640523227928449e-01, 0.00000000000000000e+00, 5.21183690031144620e-01},
152   {2.62579282058905461e-01, 2.52370482562049525e-01, 4.85689563472144981e-01},
153   {0.00000000000000000e+00, 2.33810969343145825e-01, 5.38145815125504523e-01},
154   {0.00000000000000000e+00, 0.00000000000000000e+00, 5.40487075441278053e-01},
155   {1.00000000000000000e+00, 0.00000000000000000e+00, 7.33748605950385602e-01},
156   {7.40640523227928504e-01, 0.00000000000000000e+00, 4.84688758260891195e-01},
157   {1.00000000000000000e+00, 2.81083538424111656e-01, 5.14758345974211107e-01},
158   {1.00000000000000000e+00, 0.00000000000000000e+00, 4.67497211900771203e-01},
159   {7.38934376044866781e-01, 0.00000000000000000e+00, 1.00000000000000000e+00},
160   {4.79574899272795285e-01, 0.00000000000000000e+00, 7.50940152310505593e-01},
161   {4.77868752089733617e-01, 0.00000000000000000e+00, 1.00000000000000000e+00},
162   {1.00000000000000000e+00, 1.00000000000000000e+00, 7.51949651266657582e-01},
163   {7.62579282058905461e-01, 7.52370482562049525e-01, 4.67395677018163536e-01},
164   {1.00000000000000000e+00, 7.81083538424111712e-01, 5.32959391290483087e-01},
165   {7.60498425576266124e-01, 1.00000000000000000e+00, 5.16039274773721024e-01},
166   {1.00000000000000000e+00, 1.00000000000000000e+00, 5.03899302533315163e-01},
167   {7.18954188589218113e-01, 7.26916038449581636e-01, 1.00000000000000000e+00},
168   {4.81533470648123629e-01, 4.79286521011631217e-01, 7.15446025751505954e-01},
169   {4.57888564634085005e-01, 2.26916038449581692e-01, 1.00000000000000000e+00},
170   {4.95273172597062383e-01, 7.26916038449581636e-01, 1.00000000000000000e+00},
171   {4.37908377178436337e-01, 4.53832076899163384e-01, 1.00000000000000000e+00},
172   {1.00000000000000000e+00, 7.38666970143638135e-01, 1.00000000000000000e+00},
173   {1.00000000000000000e+00, 5.19750508567749736e-01, 7.81009740023825616e-01},
174   {7.38934376044866781e-01, 2.38666970143638080e-01, 1.00000000000000000e+00},
175   {7.18954188589218113e-01, 4.65583008593219771e-01, 1.00000000000000000e+00},
176   {1.00000000000000000e+00, 4.77333940287276159e-01, 1.00000000000000000e+00},
177   {0.00000000000000000e+00, 1.00000000000000000e+00, 7.17232187874490501e-01},
178   {0.00000000000000000e+00, 7.33810969343145825e-01, 4.85134465279355998e-01},
179   {2.60498425576266179e-01, 1.00000000000000000e+00, 4.81321811381553832e-01},
180   {0.00000000000000000e+00, 1.00000000000000000e+00, 4.34464375748980947e-01},
181   {0.00000000000000000e+00, 2.81085527042108152e-01, 1.00000000000000000e+00},
182   {0.00000000000000000e+00, 5.14896496385254032e-01, 7.67902277404865607e-01},
183   {2.76318984007844215e-01, 7.81085527042108207e-01, 1.00000000000000000e+00},
184   {2.18954188589218168e-01, 5.08001565491689844e-01, 1.00000000000000000e+00},
185   {0.00000000000000000e+00, 5.62171054084216304e-01, 1.00000000000000000e+00},
186   {2.76318984007844215e-01, 1.00000000000000000e+00, 1.00000000000000000e+00},
187   {5.36817409584110394e-01, 1.00000000000000000e+00, 7.64089623507063331e-01},
188   {5.52637968015688430e-01, 1.00000000000000000e+00, 1.00000000000000000e+00},
189   {5.03219805286833965e-01, 2.52370482562049525e-01, 4.66386178062011547e-01},
190   {4.80554184960459430e-01, 2.39643260505815608e-01, 7.33193089031005774e-01},
191   {4.81281046455856898e-01, 0.00000000000000000e+00, 5.01880304621011186e-01},
192   {7.62579282058905461e-01, 5.33454020986161126e-01, 4.96455765775331570e-01},
193   {2.62579282058905461e-01, 4.86181451905195350e-01, 4.83348303156371562e-01},
194   {7.40766735324061787e-01, 4.99518514789690449e-01, 7.48227882887665841e-01},
195   {2.40766735324061815e-01, 4.97091508698442541e-01, 7.41674151578185725e-01},
196   {5.25158564117810922e-01, 5.04740965124099050e-01, 4.30892051503011964e-01},
197   {7.40640523227928504e-01, 2.81083538424111656e-01, 5.31949892334331098e-01},
198   {7.39787449636397643e-01, 2.59875254283874868e-01, 7.65974946167165549e-01},
199   {1.00000000000000000e+00, 5.62167076848223313e-01, 5.62019480047651121e-01},
200   {2.60498425576266179e-01, 7.33810969343145825e-01, 5.31991900911928939e-01},
201   {2.68408704792055197e-01, 7.57448248192627016e-01, 7.65995950455964469e-01},
202   {0.00000000000000000e+00, 4.67621938686291649e-01, 5.35804554809731104e-01},
203   {5.23077707635171585e-01, 7.52370482562049525e-01, 4.79535649258569396e-01},
204   {5.09175440116116929e-01, 7.39643260505815636e-01, 7.39767824629284698e-01},
205   {5.20996851152532359e-01, 1.00000000000000000e+00, 5.28179247014126774e-01}
206 };
207 
208 const PetscInt sConnectivityPrismsMesh[128][6] = {
209   /* rank 0 */
210   {11,  7,   42,  8,   10,  9  },
211   {47,  42,  43,  45,  9,   57 },
212   {8,   10,  9,   77,  76,  75 },
213   {45,  9,   57,  110, 75,  116},
214   {17,  48,  55,  13,  14,  15 },
215   {58,  55,  49,  56,  15,  52 },
216   {13,  14,  15,  85,  82,  83 },
217   {56,  15,  52,  118, 83,  111},
218   {6,   0,   1,   2,   3,   4  },
219   {54,  1,   44,  51,  4,   46 },
220   {2,   3,   4,   73,  70,  71 },
221   {51,  4,   46,  115, 71,  108},
222   {58,  49,  43,  56,  52,  57 },
223   {47,  43,  44,  45,  57,  46 },
224   {56,  52,  57,  118, 111, 116},
225   {45,  57,  46,  110, 116, 108},
226   {77,  76,  75,  74,  31,  30 },
227   {110, 75,  116, 79,  30,  117},
228   {74,  31,  30,  32,  29,  78 },
229   {79,  30,  117, 80,  78,  93 },
230   {85,  82,  83,  81,  34,  35 },
231   {118, 83,  111, 92,  35,  113},
232   {81,  34,  35,  37,  86,  91 },
233   {92,  35,  113, 95,  91,  94 },
234   {73,  70,  71,  69,  25,  26 },
235   {115, 71,  108, 87,  26,  109},
236   {69,  25,  26,  28,  23,  24 },
237   {87,  26,  109, 90,  24,  88 },
238   {118, 111, 116, 92,  113, 117},
239   {110, 116, 108, 79,  117, 109},
240   {92,  113, 117, 95,  94,  93 },
241   {79,  117, 109, 80,  93,  88 },
242   {22,  18,  63,  19,  20,  21 },
243   {68,  63,  64,  66,  21,  61 },
244   {19,  20,  21,  99,  97,  98 },
245   {66,  21,  61,  124, 98,  119},
246   {6,   1,   59,  2,   4,   5  },
247   {62,  59,  50,  60,  5,   53 },
248   {2,   4,   5,   73,  71,  72 },
249   {60,  5,   53,  121, 72,  112},
250   {17,  12,  48,  13,  16,  14 },
251   {54,  48,  65,  51,  14,  67 },
252   {13,  16,  14,  85,  84,  82 },
253   {51,  14,  67,  115, 82,  122},
254   {62,  50,  64,  60,  53,  61 },
255   {68,  64,  65,  66,  61,  67 },
256   {60,  53,  61,  121, 112, 119},
257   {66,  61,  67,  124, 119, 122},
258   {99,  97,  98,  96,  39,  40 },
259   {124, 98,  119, 106, 40,  120},
260   {96,  39,  40,  41,  38,  105},
261   {106, 40,  120, 107, 105, 102},
262   {73,  71,  72,  69,  26,  27 },
263   {121, 72,  112, 101, 27,  114},
264   {69,  26,  27,  28,  24,  100},
265   {101, 27,  114, 104, 100, 103},
266   {85,  84,  82,  81,  36,  34 },
267   {115, 82,  122, 87,  34,  123},
268   {81,  36,  34,  37,  33,  86 },
269   {87,  34,  123, 90,  86,  89 },
270   {121, 112, 119, 101, 114, 120},
271   {124, 119, 122, 106, 120, 123},
272   {101, 114, 120, 104, 103, 102},
273   {106, 120, 123, 107, 102, 89 },
274   /* rank 1 */
275   {58,  43,  7,   56,  57,  10 },
276   {7,   43,  42,  10,  57,  9  },
277   {56,  57,  10,  118, 116, 76 },
278   {10,  57,  9,   76,  116, 75 },
279   {54,  49,  48,  51,  52,  14 },
280   {48,  49,  55,  14,  52,  15 },
281   {51,  52,  14,  115, 111, 82 },
282   {14,  52,  15,  82,  111, 83 },
283   {47,  44,  0,   45,  46,  3  },
284   {0,   44,  1,   3,   46,  4  },
285   {45,  46,  3,   110, 108, 70 },
286   {3,   46,  4,   70,  108, 71 },
287   {54,  44,  49,  51,  46,  52 },
288   {49,  44,  43,  52,  46,  57 },
289   {51,  46,  52,  115, 108, 111},
290   {52,  46,  57,  111, 108, 116},
291   {118, 116, 76,  92,  117, 31 },
292   {76,  116, 75,  31,  117, 30 },
293   {92,  117, 31,  95,  93,  29 },
294   {31,  117, 30,  29,  93,  78 },
295   {115, 111, 82,  87,  113, 34 },
296   {82,  111, 83,  34,  113, 35 },
297   {87,  113, 34,  90,  94,  86 },
298   {34,  113, 35,  86,  94,  91 },
299   {110, 108, 70,  79,  109, 25 },
300   {70,  108, 71,  25,  109, 26 },
301   {79,  109, 25,  80,  88,  23 },
302   {25,  109, 26,  23,  88,  24 },
303   {115, 108, 111, 87,  109, 113},
304   {111, 108, 116, 113, 109, 117},
305   {87,  109, 113, 90,  88,  94 },
306   {113, 109, 117, 94,  88,  93 },
307   {62,  64,  18,  60,  61,  20 },
308   {18,  64,  63,  20,  61,  21 },
309   {60,  61,  20,  121, 119, 97 },
310   {20,  61,  21,  97,  119, 98 },
311   {54,  50,  1,   51,  53,  4  },
312   {1,   50,  59,  4,   53,  5  },
313   {51,  53,  4,   115, 112, 71 },
314   {4,   53,  5,   71,  112, 72 },
315   {68,  65,  12,  66,  67,  16 },
316   {12,  65,  48,  16,  67,  14 },
317   {66,  67,  16,  124, 122, 84 },
318   {16,  67,  14,  84,  122, 82 },
319   {54,  65,  50,  51,  67,  53 },
320   {50,  65,  64,  53,  67,  61 },
321   {51,  67,  53,  115, 122, 112},
322   {53,  67,  61,  112, 122, 119},
323   {121, 119, 97,  101, 120, 39 },
324   {97,  119, 98,  39,  120, 40 },
325   {101, 120, 39,  104, 102, 38 },
326   {39,  120, 40,  38,  102, 105},
327   {115, 112, 71,  87,  114, 26 },
328   {71,  112, 72,  26,  114, 27 },
329   {87,  114, 26,  90,  103, 24 },
330   {26,  114, 27,  24,  103, 100},
331   {124, 122, 84,  106, 123, 36 },
332   {84,  122, 82,  36,  123, 34 },
333   {106, 123, 36,  107, 89,  33 },
334   {36,  123, 34,  33,  89,  86 },
335   {115, 122, 112, 87,  123, 114},
336   {112, 122, 119, 114, 123, 120},
337   {87,  123, 114, 90,  89,  103},
338   {114, 123, 120, 103, 89,  102}
339 };
340 
341 /* Partitions of prisms mesh : */
342 const PetscInt sInitialPartitionPrismsMesh[2][64] = {
343   {0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63 },
344   {64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
345    96,                                                                                                                                 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127}
346 };
347 
348 const PetscInt sNLoclCellsPrismsMesh = 64;
349 const PetscInt sNGlobVertsPrismsMesh = 125;
350 
351 int main(int argc, char **argv)
352 {
353   PetscInt         Nc = 0;
354   const PetscInt  *InitPartForRank[2];
355   DM               dm, idm, ddm;
356   PetscSF          sfVert, sfMig, sfPart;
357   PetscPartitioner part;
358   PetscSection     s;
359   PetscInt        *cells, c;
360   PetscMPIInt      size, rank;
361   PetscBool        box = PETSC_FALSE, field = PETSC_FALSE, quadsmesh = PETSC_FALSE, trisquadsmesh = PETSC_FALSE, prismsmesh = PETSC_FALSE;
362 
363   PetscFunctionBeginUser;
364   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
365   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
366   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
367   PetscCheck(size == 2, PETSC_COMM_WORLD, PETSC_ERR_SUP, "This is a 2 processors example only");
368   PetscCall(PetscOptionsGetBool(NULL, NULL, "-field", &field, NULL));
369   PetscCall(PetscOptionsGetBool(NULL, NULL, "-box", &box, NULL));
370   PetscCall(PetscOptionsGetBool(NULL, NULL, "-quadsmesh", &quadsmesh, NULL));
371   PetscCall(PetscOptionsGetBool(NULL, NULL, "-trisquadsmesh", &trisquadsmesh, NULL));
372   PetscCall(PetscOptionsGetBool(NULL, NULL, "-prismsmesh", &prismsmesh, NULL));
373   PetscCheck(1 == (box ? 1 : 0) + (quadsmesh ? 1 : 0) + (trisquadsmesh ? 1 : 0) + (prismsmesh ? 1 : 0), PETSC_COMM_WORLD, PETSC_ERR_SUP, "Specify one and only one of -box, -quadsmesh or -prismsmesh");
374 
375   PetscCall(DMPlexCreate(PETSC_COMM_WORLD, &dm));
376   if (box) {
377     PetscCall(DMSetType(dm, DMPLEX));
378     PetscCall(DMSetFromOptions(dm));
379   } else {
380     if (quadsmesh) {
381       Nc                       = sNLoclCells2x5Mesh; //Same on each rank for this example...
382       PetscInt Nv              = sNGlobVerts2x5Mesh;
383       InitPartForRank[0]       = &sInitialPartition2x5Mesh[0][0];
384       InitPartForRank[1]       = &sInitialPartition2x5Mesh[1][0];
385       const PetscInt(*Conn)[4] = sConnectivity2x5Mesh;
386 
387       const PetscInt Ncor = 4;
388       const PetscInt dim  = 2;
389 
390       PetscCall(PetscMalloc1(Nc * Ncor, &cells));
391       for (c = 0; c < Nc; ++c) {
392         PetscInt cell = (InitPartForRank[rank])[c], cor;
393 
394         for (cor = 0; cor < Ncor; ++cor) cells[c * Ncor + cor] = Conn[cell][cor];
395       }
396       PetscCall(DMSetDimension(dm, dim));
397       PetscCall(DMPlexBuildFromCellListParallel(dm, Nc, PETSC_DECIDE, Nv, Ncor, cells, &sfVert, NULL));
398     } else if (trisquadsmesh) {
399       Nc                       = sNLoclCellsMixedTQMesh; //Same on each rank for this example...
400       PetscInt Nv              = sNGlobVertsMixedTQMesh;
401       InitPartForRank[0]       = &sInitialPartitionMixedTQMesh[0][0];
402       InitPartForRank[1]       = &sInitialPartitionMixedTQMesh[1][0];
403       const PetscInt(*Conn)[4] = sConnectivityMixedTQMesh;
404 
405       const PetscInt NcorMax = 4;
406       const PetscInt dim     = 2;
407 
408       /* Create a PetscSection and taking care to exclude nodes with "-1" into element connectivity: */
409       PetscSection s;
410       PetscInt     vStart = 0, vEnd = Nc;
411       PetscCall(PetscSectionCreate(PETSC_COMM_WORLD, &s));
412       PetscCall(PetscSectionSetNumFields(s, 1));
413       PetscCall(PetscSectionSetFieldComponents(s, 0, 1));
414       PetscCall(PetscSectionSetChart(s, vStart, vEnd));
415 
416       PetscCall(PetscMalloc1(Nc * NcorMax, &cells));
417       PetscInt count = 0;
418       for (c = 0; c < Nc; ++c) {
419         PetscInt cell         = (InitPartForRank[rank])[c], cor;
420         PetscInt nbElemVertex = ((-1 == Conn[cell][NcorMax - 1]) ? 3 : 4);
421         for (cor = 0; cor < nbElemVertex; ++cor) {
422           cells[count] = Conn[cell][cor];
423           ++count;
424         }
425         PetscCall(PetscSectionSetDof(s, c, nbElemVertex));
426         PetscCall(PetscSectionSetFieldDof(s, c, 0, nbElemVertex));
427       }
428       PetscCall(PetscSectionSetUp(s));
429       PetscCall(DMSetDimension(dm, dim));
430       PetscCall(DMPlexBuildFromCellSectionParallel(dm, Nc, PETSC_DECIDE, Nv, s, cells, &sfVert, NULL));
431       PetscCall(PetscSectionDestroy(&s));
432     } else if (prismsmesh) {
433       Nc                       = sNLoclCellsPrismsMesh; //Same on each rank for this example...
434       PetscInt Nv              = sNGlobVertsPrismsMesh;
435       InitPartForRank[0]       = &sInitialPartitionPrismsMesh[0][0];
436       InitPartForRank[1]       = &sInitialPartitionPrismsMesh[1][0];
437       const PetscInt(*Conn)[6] = sConnectivityPrismsMesh;
438 
439       const PetscInt Ncor = 6;
440       const PetscInt dim  = 3;
441 
442       PetscCall(PetscMalloc1(Nc * Ncor, &cells));
443       for (c = 0; c < Nc; ++c) {
444         PetscInt cell = (InitPartForRank[rank])[c], cor;
445 
446         for (cor = 0; cor < Ncor; ++cor) cells[c * Ncor + cor] = Conn[cell][cor];
447       }
448       PetscCall(DMSetDimension(dm, dim));
449       PetscCall(DMPlexBuildFromCellListParallel(dm, Nc, PETSC_DECIDE, Nv, Ncor, cells, &sfVert, NULL));
450     }
451     PetscCall(PetscSFDestroy(&sfVert));
452     PetscCall(PetscFree(cells));
453     PetscCall(DMPlexInterpolate(dm, &idm));
454     PetscCall(DMDestroy(&dm));
455     dm = idm;
456   }
457   PetscCall(DMSetUseNatural(dm, PETSC_TRUE));
458   PetscCall(DMViewFromOptions(dm, NULL, "-dm_view"));
459 
460   if (field) {
461     const PetscInt Nf         = 1;
462     const PetscInt numBC      = 0;
463     const PetscInt numComp[1] = {1};
464     PetscInt       numDof[4]  = {0, 0, 0, 0};
465     PetscInt       dim;
466 
467     PetscCall(DMGetDimension(dm, &dim));
468     numDof[dim] = 1;
469 
470     PetscCall(DMSetNumFields(dm, Nf));
471     PetscCall(DMPlexCreateSection(dm, NULL, numComp, numDof, numBC, NULL, NULL, NULL, NULL, &s));
472     PetscCall(DMSetLocalSection(dm, s));
473     PetscCall(PetscSectionView(s, PETSC_VIEWER_STDOUT_WORLD));
474     PetscCall(PetscSectionDestroy(&s));
475   }
476 
477   PetscCall(DMPlexGetPartitioner(dm, &part));
478   PetscCall(PetscPartitionerSetFromOptions(part));
479 
480   PetscCall(DMPlexDistribute(dm, 0, &sfMig, &ddm));
481   PetscCall(PetscSFView(sfMig, PETSC_VIEWER_STDOUT_WORLD));
482   PetscCall(PetscSFCreateInverseSF(sfMig, &sfPart));
483   PetscCall(PetscObjectSetName((PetscObject)sfPart, "Inverse Migration SF"));
484   PetscCall(PetscSFView(sfPart, PETSC_VIEWER_STDOUT_WORLD));
485 
486   Vec          lGlobalVec, lNatVec;
487   PetscScalar *lNatVecArray;
488 
489   {
490     PetscSection s;
491 
492     PetscCall(DMGetGlobalSection(dm, &s));
493     PetscCall(PetscSectionView(s, PETSC_VIEWER_STDOUT_WORLD));
494   }
495   PetscCall(DMGetGlobalVector(dm, &lNatVec));
496   PetscCall(PetscObjectSetName((PetscObject)lNatVec, "Natural Vector (initial partition)"));
497 
498   //Copying the initial partition into the "natural" vector:
499   PetscCall(VecZeroEntries(lNatVec));
500   PetscCall(VecGetArray(lNatVec, &lNatVecArray));
501   for (c = 0; c < Nc; ++c) lNatVecArray[c] = (InitPartForRank[rank])[c];
502   PetscCall(VecRestoreArray(lNatVec, &lNatVecArray));
503 
504   PetscCall(DMGetGlobalVector(ddm, &lGlobalVec));
505   PetscCall(PetscObjectSetName((PetscObject)lGlobalVec, "Global Vector (reordered element numbers in the petsc distributed order)"));
506   PetscCall(VecZeroEntries(lGlobalVec));
507 
508   // The call to DMPlexNaturalToGlobalBegin/End does not produce our expected result...
509   // In lGlobalVec, we expect to have:
510   /*
511    * Process [0]
512    * 2.
513    * 4.
514    * 8.
515    * 3.
516    * 9.
517    * Process [1]
518    * 1.
519    * 5.
520    * 7.
521    * 0.
522    * 6.
523    *
524    * but we obtained:
525    *
526    * Process [0]
527    * 2.
528    * 4.
529    * 8.
530    * 0.
531    * 0.
532    * Process [1]
533    * 0.
534    * 0.
535    * 0.
536    * 0.
537    * 0.
538    */
539 
540   {
541     PetscSF nsf;
542 
543     PetscCall(DMPlexGetGlobalToNaturalSF(ddm, &nsf));
544     PetscCall(PetscSFView(nsf, NULL));
545   }
546   PetscCall(DMPlexNaturalToGlobalBegin(ddm, lNatVec, lGlobalVec));
547   PetscCall(DMPlexNaturalToGlobalEnd(ddm, lNatVec, lGlobalVec));
548 
549   PetscCall(VecView(lNatVec, PETSC_VIEWER_STDOUT_WORLD));
550   PetscCall(VecView(lGlobalVec, PETSC_VIEWER_STDOUT_WORLD));
551 
552   PetscCall(DMRestoreGlobalVector(dm, &lNatVec));
553   PetscCall(DMRestoreGlobalVector(ddm, &lGlobalVec));
554 
555   const PetscBool lUseCone    = PETSC_FALSE;
556   const PetscBool lUseClosure = PETSC_TRUE;
557   PetscCall(DMSetBasicAdjacency(ddm, lUseCone, lUseClosure));
558   const PetscInt lNbCellsInOverlap = 1;
559   PetscSF        lSFMigrationOvl;
560   DM             ddm_with_overlap;
561 
562   PetscCall(DMPlexDistributeOverlap(ddm, lNbCellsInOverlap, &lSFMigrationOvl, &ddm_with_overlap));
563 
564   IS lISCellWithOvl = 0;
565   /* This is the buggy call with prisms since commit 5ae96e2b862 */
566   PetscCall(DMPlexCreateCellNumbering(ddm_with_overlap, PETSC_TRUE, &lISCellWithOvl));
567   /* Here, we can see the elements in the overlap within the IS: they are the ones with negative indices */
568   PetscCall(ISView(lISCellWithOvl, PETSC_VIEWER_STDOUT_WORLD));
569   PetscCall(ISDestroy(&lISCellWithOvl));
570 
571   PetscCall(PetscSFDestroy(&lSFMigrationOvl));
572   PetscCall(DMDestroy(&ddm_with_overlap));
573   PetscCall(PetscSFDestroy(&sfMig));
574   PetscCall(PetscSFDestroy(&sfPart));
575   PetscCall(DMDestroy(&dm));
576   PetscCall(DMDestroy(&ddm));
577   PetscCall(PetscFinalize());
578   return 0;
579 }
580 
581 /*TEST
582 
583   testset:
584     args: -field -petscpartitioner_type simple
585     nsize: 2
586 
587     test:
588       suffix: 0
589       args: -quadsmesh
590       output_file: output/ex47_0.out
591 
592     test:
593       suffix: 1
594       args: -box -dm_plex_simplex 0 -dm_plex_box_faces 2,5 -dm_distribute
595       output_file: output/ex47_1.out
596 
597     test:
598       suffix: 2
599       args: -prismsmesh
600       output_file: output/ex47_2.out
601 
602     test:
603       suffix: 3
604       args: -trisquadsmesh
605       output_file: output/ex47_3.out
606 
607 TEST*/
608