xref: /petsc/src/dm/impls/plex/tests/ex25.c (revision 21e3ffae2f3b73c0bd738cf6d0a809700fc04bb0)
1 static char help[]     = "Test DMCreateLocalVector_Plex, DMPlexGetCellFields and DMPlexRestoreCellFields work properly for 0 fields/cells/DS dimension\n\n";
2 static char FILENAME[] = "ex25.c";
3 
4 #include <petscdmplex.h>
5 #include <petscds.h>
6 #include <petscsnes.h>
7 
8 typedef struct {
9   PetscInt test;
10 } AppCtx;
11 
12 static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
13 {
14   PetscFunctionBegin;
15   options->test = 0;
16   PetscOptionsBegin(comm, "", "Zero-sized DMPlexGetCellFields Test Options", "DMPLEX");
17   PetscCall(PetscOptionsBoundedInt("-test", "Test to run", FILENAME, options->test, &options->test, NULL, 0));
18   PetscOptionsEnd();
19   PetscFunctionReturn(PETSC_SUCCESS);
20 }
21 
22 static PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *options, DM *dm)
23 {
24   PetscFunctionBegin;
25   PetscCall(DMCreate(comm, dm));
26   PetscCall(DMSetType(*dm, DMPLEX));
27   PetscCall(DMSetFromOptions(*dm));
28   PetscCall(DMViewFromOptions(*dm, NULL, "-dm_view"));
29   PetscFunctionReturn(PETSC_SUCCESS);
30 }
31 
32 /* no discretization is given so DMGetNumFields yields 0 */
33 static PetscErrorCode test0(DM dm, AppCtx *options)
34 {
35   Vec locX;
36 
37   PetscFunctionBegin;
38   PetscCall(DMGetLocalVector(dm, &locX));
39   PetscCall(DMRestoreLocalVector(dm, &locX));
40   PetscFunctionReturn(PETSC_SUCCESS);
41 }
42 
43 /* no discretization is given so DMGetNumFields and PetscDSGetTotalDimension yield 0 */
44 static PetscErrorCode test1(DM dm, AppCtx *options)
45 {
46   IS           cells;
47   Vec          locX, locX_t, locA;
48   PetscScalar *u, *u_t, *a;
49 
50   PetscFunctionBegin;
51   PetscCall(ISCreateStride(PETSC_COMM_SELF, 0, 0, 1, &cells));
52   PetscCall(DMGetLocalVector(dm, &locX));
53   PetscCall(DMGetLocalVector(dm, &locX_t));
54   PetscCall(DMGetLocalVector(dm, &locA));
55   PetscCall(DMPlexGetCellFields(dm, cells, locX, locX_t, locA, &u, &u_t, &a));
56   PetscCall(DMPlexRestoreCellFields(dm, cells, locX, locX_t, locA, &u, &u_t, &a));
57   PetscCall(DMRestoreLocalVector(dm, &locX));
58   PetscCall(DMRestoreLocalVector(dm, &locX_t));
59   PetscCall(DMRestoreLocalVector(dm, &locA));
60   PetscCall(ISDestroy(&cells));
61   PetscFunctionReturn(PETSC_SUCCESS);
62 }
63 
64 /* no discretization is given so DMGetNumFields and PetscDSGetTotalDimension yield 0 */
65 static PetscErrorCode test2(DM dm, AppCtx *options)
66 {
67   IS           cells;
68   Vec          locX, locX_t, locA;
69   PetscScalar *u, *u_t, *a;
70   PetscMPIInt  rank;
71 
72   PetscFunctionBegin;
73   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
74   PetscCall(ISCreateStride(PETSC_COMM_SELF, rank ? 0 : 1, 0, 1, &cells));
75   PetscCall(DMGetLocalVector(dm, &locX));
76   PetscCall(DMGetLocalVector(dm, &locX_t));
77   PetscCall(DMGetLocalVector(dm, &locA));
78   PetscCall(DMPlexGetCellFields(dm, cells, locX, locX_t, locA, &u, &u_t, &a));
79   PetscCall(DMPlexRestoreCellFields(dm, cells, locX, locX_t, locA, &u, &u_t, &a));
80   PetscCall(DMRestoreLocalVector(dm, &locX));
81   PetscCall(DMRestoreLocalVector(dm, &locX_t));
82   PetscCall(DMRestoreLocalVector(dm, &locA));
83   PetscCall(ISDestroy(&cells));
84   PetscFunctionReturn(PETSC_SUCCESS);
85 }
86 
87 static PetscErrorCode test3(DM dm, AppCtx *options)
88 {
89   PetscDS   ds;
90   PetscFE   fe;
91   PetscInt  dim;
92   PetscBool simplex;
93 
94   PetscFunctionBegin;
95   PetscCall(DMGetDimension(dm, &dim));
96   PetscCall(DMPlexIsSimplex(dm, &simplex));
97   PetscCall(DMGetDS(dm, &ds));
98   PetscCall(PetscFECreateDefault(PetscObjectComm((PetscObject)dm), dim, 1, simplex, NULL, -1, &fe));
99   PetscCall(PetscDSSetDiscretization(ds, 0, (PetscObject)fe));
100   PetscCall(PetscFEDestroy(&fe));
101   PetscCall(test1(dm, options));
102   PetscFunctionReturn(PETSC_SUCCESS);
103 }
104 
105 static PetscErrorCode test4(DM dm, AppCtx *options)
106 {
107   PetscFE   fe;
108   PetscInt  dim;
109   PetscBool simplex;
110 
111   PetscFunctionBegin;
112   PetscCall(DMGetDimension(dm, &dim));
113   PetscCall(DMPlexIsSimplex(dm, &simplex));
114   PetscCall(PetscFECreateDefault(PetscObjectComm((PetscObject)dm), dim, 1, simplex, NULL, -1, &fe));
115   PetscCall(DMSetField(dm, 0, NULL, (PetscObject)fe));
116   PetscCall(PetscFEDestroy(&fe));
117   PetscCall(DMCreateDS(dm));
118   PetscCall(test2(dm, options));
119   PetscFunctionReturn(PETSC_SUCCESS);
120 }
121 
122 static PetscErrorCode test5(DM dm, AppCtx *options)
123 {
124   IS           cells;
125   Vec          locX, locX_t, locA;
126   PetscScalar *u, *u_t, *a;
127 
128   PetscFunctionBegin;
129   locX_t = NULL;
130   locA   = NULL;
131   PetscCall(ISCreateStride(PETSC_COMM_SELF, 0, 0, 1, &cells));
132   PetscCall(DMGetLocalVector(dm, &locX));
133   PetscCall(DMPlexGetCellFields(dm, cells, locX, locX_t, locA, &u, &u_t, &a));
134   PetscCall(DMPlexRestoreCellFields(dm, cells, locX, locX_t, locA, &u, &u_t, &a));
135   PetscCall(DMRestoreLocalVector(dm, &locX));
136   PetscCall(ISDestroy(&cells));
137   PetscFunctionReturn(PETSC_SUCCESS);
138 }
139 
140 static PetscErrorCode test6(DM dm, AppCtx *options)
141 {
142   IS           cells;
143   Vec          locX, locX_t, locA;
144   PetscScalar *u, *u_t, *a;
145   PetscMPIInt  rank;
146 
147   PetscFunctionBegin;
148   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
149   locX_t = NULL;
150   locA   = NULL;
151   PetscCall(ISCreateStride(PETSC_COMM_SELF, rank ? 0 : 1, 0, 1, &cells));
152   PetscCall(DMGetLocalVector(dm, &locX));
153   PetscCall(DMPlexGetCellFields(dm, cells, locX, locX_t, locA, &u, &u_t, &a));
154   PetscCall(DMPlexRestoreCellFields(dm, cells, locX, locX_t, locA, &u, &u_t, &a));
155   PetscCall(DMRestoreLocalVector(dm, &locX));
156   PetscCall(ISDestroy(&cells));
157   PetscFunctionReturn(PETSC_SUCCESS);
158 }
159 
160 static PetscErrorCode test7(DM dm, AppCtx *options)
161 {
162   PetscFE   fe;
163   PetscInt  dim;
164   PetscBool simplex;
165 
166   PetscFunctionBegin;
167   PetscCall(DMGetDimension(dm, &dim));
168   PetscCall(DMPlexIsSimplex(dm, &simplex));
169   PetscCall(PetscFECreateDefault(PetscObjectComm((PetscObject)dm), dim, 1, simplex, NULL, -1, &fe));
170   PetscCall(DMSetField(dm, 0, NULL, (PetscObject)fe));
171   PetscCall(PetscFEDestroy(&fe));
172   PetscCall(DMCreateDS(dm));
173   PetscCall(test5(dm, options));
174   PetscFunctionReturn(PETSC_SUCCESS);
175 }
176 
177 static PetscErrorCode test8(DM dm, AppCtx *options)
178 {
179   PetscFE   fe;
180   PetscInt  dim;
181   PetscBool simplex;
182 
183   PetscFunctionBegin;
184   PetscCall(DMGetDimension(dm, &dim));
185   PetscCall(DMPlexIsSimplex(dm, &simplex));
186   PetscCall(PetscFECreateDefault(PetscObjectComm((PetscObject)dm), dim, 1, simplex, NULL, -1, &fe));
187   PetscCall(DMSetField(dm, 0, NULL, (PetscObject)fe));
188   PetscCall(PetscFEDestroy(&fe));
189   PetscCall(DMCreateDS(dm));
190   PetscCall(test6(dm, options));
191   PetscFunctionReturn(PETSC_SUCCESS);
192 }
193 
194 int main(int argc, char **argv)
195 {
196   MPI_Comm comm;
197   DM       dm;
198   AppCtx   options;
199 
200   PetscFunctionBeginUser;
201   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
202   comm = PETSC_COMM_WORLD;
203   PetscCall(ProcessOptions(comm, &options));
204   PetscCall(CreateMesh(comm, &options, &dm));
205 
206   switch (options.test) {
207   case 0:
208     PetscCall(test0(dm, &options));
209     break;
210   case 1:
211     PetscCall(test1(dm, &options));
212     break;
213   case 2:
214     PetscCall(test2(dm, &options));
215     break;
216   case 3:
217     PetscCall(test3(dm, &options));
218     break;
219   case 4:
220     PetscCall(test4(dm, &options));
221     break;
222   case 5:
223     PetscCall(test5(dm, &options));
224     break;
225   case 6:
226     PetscCall(test6(dm, &options));
227     break;
228   case 7:
229     PetscCall(test7(dm, &options));
230     break;
231   case 8:
232     PetscCall(test8(dm, &options));
233     break;
234   default:
235     SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "No such test: %" PetscInt_FMT, options.test);
236   }
237 
238   PetscCall(DMDestroy(&dm));
239   PetscCall(PetscFinalize());
240   return 0;
241 }
242 
243 /*TEST
244 
245   testset:
246     args: -dm_plex_dim 3 -dm_plex_interpolate 0
247 
248     test:
249       suffix: 0
250       requires: ctetgen
251       args: -test 0
252     test:
253       suffix: 1
254       requires: ctetgen
255       args: -test 1
256     test:
257       suffix: 2
258       requires: ctetgen
259       args: -test 2
260     test:
261       suffix: 3
262       requires: ctetgen
263       args: -test 3
264     test:
265       suffix: 4
266       requires: ctetgen
267       args: -test 4
268     test:
269       suffix: 5
270       requires: ctetgen
271       args: -test 5
272     test:
273       suffix: 6
274       requires: ctetgen
275       args: -test 6
276     test:
277       suffix: 7
278       requires: ctetgen
279       args: -test 7
280     test:
281       suffix: 8
282       requires: ctetgen
283       args: -test 8
284     test:
285       suffix: 9
286       requires: ctetgen
287       nsize: 2
288       args: -test 1
289     test:
290       suffix: 10
291       requires: ctetgen
292       nsize: 2
293       args: -test 2
294     test:
295       suffix: 11
296       requires: ctetgen
297       nsize: 2
298       args: -test 3
299     test:
300       suffix: 12
301       requires: ctetgen
302       nsize: 2
303       args: -test 4
304 
305 TEST*/
306