xref: /petsc/src/dm/impls/plex/tests/ex25.c (revision 609caa7c8c030312b00807b4f015fd827bb80932)
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 
ProcessOptions(MPI_Comm comm,AppCtx * options)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 
CreateMesh(MPI_Comm comm,AppCtx * options,DM * dm)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 */
test0(DM dm,AppCtx * options)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 */
test1(DM dm,AppCtx * options)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 */
test2(DM dm,AppCtx * options)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 
test3(DM dm,AppCtx * options)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 
test4(DM dm,AppCtx * options)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 
test5(DM dm,AppCtx * options)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 
test6(DM dm,AppCtx * options)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 
test7(DM dm,AppCtx * options)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 
test8(DM dm,AppCtx * options)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 
main(int argc,char ** argv)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     output_file: output/empty.out
248 
249     test:
250       suffix: 0
251       requires: ctetgen
252       args: -test 0
253     test:
254       suffix: 1
255       requires: ctetgen
256       args: -test 1
257     test:
258       suffix: 2
259       requires: ctetgen
260       args: -test 2
261     test:
262       suffix: 3
263       requires: ctetgen
264       args: -test 3
265     test:
266       suffix: 4
267       requires: ctetgen
268       args: -test 4
269     test:
270       suffix: 5
271       requires: ctetgen
272       args: -test 5
273     test:
274       suffix: 6
275       requires: ctetgen
276       args: -test 6
277     test:
278       suffix: 7
279       requires: ctetgen
280       args: -test 7
281     test:
282       suffix: 8
283       requires: ctetgen
284       args: -test 8
285     test:
286       suffix: 9
287       requires: ctetgen
288       nsize: 2
289       args: -test 1
290     test:
291       suffix: 10
292       requires: ctetgen
293       nsize: 2
294       args: -test 2
295     test:
296       suffix: 11
297       requires: ctetgen
298       nsize: 2
299       args: -test 3
300     test:
301       suffix: 12
302       requires: ctetgen
303       nsize: 2
304       args: -test 4
305 
306 TEST*/
307