xref: /petsc/src/dm/impls/plex/tests/ex25.c (revision ebead697dbf761eb322f829370bbe90b3bd93fa3)
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(0);
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(0);
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(0);
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(0);
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(0);
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(0);
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(0);
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(0);
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(0);
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(0);
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(0);
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: PetscCall(test0(dm, &options)); break;
208     case 1: PetscCall(test1(dm, &options)); break;
209     case 2: PetscCall(test2(dm, &options)); break;
210     case 3: PetscCall(test3(dm, &options)); break;
211     case 4: PetscCall(test4(dm, &options)); break;
212     case 5: PetscCall(test5(dm, &options)); break;
213     case 6: PetscCall(test6(dm, &options)); break;
214     case 7: PetscCall(test7(dm, &options)); break;
215     case 8: PetscCall(test8(dm, &options)); break;
216     default: SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "No such test: %" PetscInt_FMT, options.test);
217   }
218 
219   PetscCall(DMDestroy(&dm));
220   PetscCall(PetscFinalize());
221   return 0;
222 }
223 
224 /*TEST
225 
226   testset:
227     args: -dm_plex_dim 3 -dm_plex_interpolate 0
228 
229     test:
230       suffix: 0
231       requires: ctetgen
232       args: -test 0
233     test:
234       suffix: 1
235       requires: ctetgen
236       args: -test 1
237     test:
238       suffix: 2
239       requires: ctetgen
240       args: -test 2
241     test:
242       suffix: 3
243       requires: ctetgen
244       args: -test 3
245     test:
246       suffix: 4
247       requires: ctetgen
248       args: -test 4
249     test:
250       suffix: 5
251       requires: ctetgen
252       args: -test 5
253     test:
254       suffix: 6
255       requires: ctetgen
256       args: -test 6
257     test:
258       suffix: 7
259       requires: ctetgen
260       args: -test 7
261     test:
262       suffix: 8
263       requires: ctetgen
264       args: -test 8
265     test:
266       suffix: 9
267       requires: ctetgen
268       nsize: 2
269       args: -test 1
270     test:
271       suffix: 10
272       requires: ctetgen
273       nsize: 2
274       args: -test 2
275     test:
276       suffix: 11
277       requires: ctetgen
278       nsize: 2
279       args: -test 3
280     test:
281       suffix: 12
282       requires: ctetgen
283       nsize: 2
284       args: -test 4
285 
286 TEST*/
287