xref: /petsc/src/dm/impls/plex/tests/ex26.c (revision dbbe0bcd3f3a8fbab5a45420dc06f8387e5764c6)
1 static char help[] = "Test FEM layout with DM and ExodusII storage\n\n";
2 
3 /*
4   In order to see the vectors which are being tested, use
5 
6      -ua_vec_view -s_vec_view
7 */
8 
9 #include <petsc.h>
10 #include <exodusII.h>
11 
12 #include <petsc/private/dmpleximpl.h>
13 
14 int main(int argc, char **argv) {
15   DM                dm, pdm, dmU, dmA, dmS, dmUA, dmUA2, *dmList;
16   Vec               X, U, A, S, UA, UA2;
17   IS                isU, isA, isS, isUA;
18   PetscSection      section;
19   const PetscInt    fieldU = 0;
20   const PetscInt    fieldA = 2;
21   const PetscInt    fieldS = 1;
22   const PetscInt    fieldUA[2] = {0, 2};
23   char              ifilename[PETSC_MAX_PATH_LEN], ofilename[PETSC_MAX_PATH_LEN];
24   int               exoid = -1;
25   IS                csIS;
26   const PetscInt   *csID;
27   PetscInt         *pStartDepth, *pEndDepth;
28   PetscInt          order = 1;
29   PetscInt          sdim, d, pStart, pEnd, p, numCS, set;
30   PetscMPIInt       rank, size;
31   PetscViewer       viewer;
32 
33   PetscFunctionBeginUser;
34   PetscCall(PetscInitialize(&argc, &argv,NULL, help));
35   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
36   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
37   PetscOptionsBegin(PETSC_COMM_WORLD, NULL, "FEM Layout Options", "ex26");
38   PetscCall(PetscOptionsString("-i", "Filename to read", "ex26", ifilename, ifilename, sizeof(ifilename), NULL));
39   PetscCall(PetscOptionsString("-o", "Filename to write", "ex26", ofilename, ofilename, sizeof(ofilename), NULL));
40   PetscCall(PetscOptionsBoundedInt("-order", "FEM polynomial order", "ex26", order, &order, NULL,1));
41   PetscOptionsEnd();
42   PetscCheck((order >= 1) && (order <= 2),PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "Unsupported polynomial order %" PetscInt_FMT " not in [1, 2]", order);
43 
44   /* Read the mesh from a file in any supported format */
45   PetscCall(DMPlexCreateFromFile(PETSC_COMM_WORLD, ifilename, NULL, PETSC_TRUE, &dm));
46   PetscCall(DMPlexDistributeSetDefault(dm, PETSC_FALSE));
47   PetscCall(DMSetFromOptions(dm));
48   PetscCall(DMViewFromOptions(dm, NULL, "-dm_view"));
49   PetscCall(DMGetDimension(dm, &sdim));
50 
51   /* Create the exodus result file */
52   {
53     PetscInt      numstep = 3, step;
54     char         *nodalVarName[4];
55     char         *zonalVarName[6];
56     int          *truthtable;
57     PetscInt      numNodalVar, numZonalVar, i;
58 
59     /* enable exodus debugging information */
60     ex_opts(EX_VERBOSE|EX_DEBUG);
61     /* Create the exodus file */
62     PetscCall(PetscViewerExodusIIOpen(PETSC_COMM_WORLD,ofilename,FILE_MODE_WRITE,&viewer));
63     /* The long way would be */
64     /*
65       PetscCall(PetscViewerCreate(PETSC_COMM_WORLD,&viewer));
66       PetscCall(PetscViewerSetType(viewer,PETSCVIEWEREXODUSII));
67       PetscCall(PetscViewerFileSetMode(viewer,FILE_MODE_APPEND));
68       PetscCall(PetscViewerFileSetName(viewer,ofilename));
69     */
70     /* set the mesh order */
71     PetscCall(PetscViewerExodusIISetOrder(viewer,order));
72     PetscCall(PetscViewerView(viewer,PETSC_VIEWER_STDOUT_WORLD));
73     /*
74       Notice how the exodus file is actually NOT open at this point (exoid is -1)
75       Since we are overwritting the file (mode is FILE_MODE_WRITE), we are going to have to
76       write the geometry (the DM), which can only be done on a brand new file.
77     */
78 
79     /* Save the geometry to the file, erasing all previous content */
80     PetscCall(DMView(dm,viewer));
81     PetscCall(PetscViewerView(viewer,PETSC_VIEWER_STDOUT_WORLD));
82     /*
83       Note how the exodus file is now open
84     */
85 
86     /* "Format" the exodus result file, i.e. allocate space for nodal and zonal variables */
87     switch (sdim) {
88     case 2:
89       numNodalVar = 3;
90       nodalVarName[0] = (char *) "U_x";
91       nodalVarName[1] = (char *) "U_y";
92       nodalVarName[2] = (char *) "Alpha";
93       numZonalVar = 3;
94       zonalVarName[0] = (char *) "Sigma_11";
95       zonalVarName[1] = (char *) "Sigma_22";
96       zonalVarName[2] = (char *) "Sigma_12";
97       break;
98     case 3:
99       numNodalVar = 4;
100       nodalVarName[0] = (char *) "U_x";
101       nodalVarName[1] = (char *) "U_y";
102       nodalVarName[2] = (char *) "U_z";
103       nodalVarName[3] = (char *) "Alpha";
104       numZonalVar = 6;
105       zonalVarName[0] = (char *) "Sigma_11";
106       zonalVarName[1] = (char *) "Sigma_22";
107       zonalVarName[2] = (char *) "Sigma_33";
108       zonalVarName[3] = (char *) "Sigma_23";
109       zonalVarName[4] = (char *) "Sigma_13";
110       zonalVarName[5] = (char *) "Sigma_12";
111       break;
112     default: SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No layout for dimension %" PetscInt_FMT, sdim);
113     }
114     PetscCall(PetscViewerExodusIIGetId(viewer,&exoid));
115     PetscCallExternal(ex_put_variable_param,exoid, EX_ELEM_BLOCK, numZonalVar);
116     PetscCallExternal(ex_put_variable_names,exoid, EX_ELEM_BLOCK, numZonalVar, zonalVarName);
117     PetscCallExternal(ex_put_variable_param,exoid, EX_NODAL, numNodalVar);
118     PetscCallExternal(ex_put_variable_names,exoid, EX_NODAL, numNodalVar, nodalVarName);
119     numCS = ex_inquire_int(exoid, EX_INQ_ELEM_BLK);
120 
121     /*
122       An exodusII truth table specifies which fields are saved at which time step
123       It speeds up I/O but reserving space for fieldsin the file ahead of time.
124     */
125     PetscCall(PetscMalloc1(numZonalVar * numCS, &truthtable));
126     for (i = 0; i < numZonalVar * numCS; ++i) truthtable[i] = 1;
127     PetscCallExternal(ex_put_truth_table,exoid, EX_ELEM_BLOCK, numCS, numZonalVar, truthtable);
128     PetscCall(PetscFree(truthtable));
129 
130     /* Writing time step information in the file. Note that this is currently broken in the exodus library for netcdf4 (HDF5-based) files */
131     for (step = 0; step < numstep; ++step) {
132       PetscReal time = step;
133       PetscCallExternal(ex_put_time,exoid, step+1, &time);
134     }
135   }
136 
137   /* Create the main section containing all fields */
138   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject) dm), &section));
139   PetscCall(PetscSectionSetNumFields(section, 3));
140   PetscCall(PetscSectionSetFieldName(section, fieldU, "U"));
141   PetscCall(PetscSectionSetFieldName(section, fieldA, "Alpha"));
142   PetscCall(PetscSectionSetFieldName(section, fieldS, "Sigma"));
143   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
144   PetscCall(PetscSectionSetChart(section, pStart, pEnd));
145   PetscCall(PetscMalloc2(sdim+1, &pStartDepth, sdim+1, &pEndDepth));
146   for (d = 0; d <= sdim; ++d) {PetscCall(DMPlexGetDepthStratum(dm, d, &pStartDepth[d], &pEndDepth[d]));}
147   /* Vector field U, Scalar field Alpha, Tensor field Sigma */
148   PetscCall(PetscSectionSetFieldComponents(section, fieldU, sdim));
149   PetscCall(PetscSectionSetFieldComponents(section, fieldA, 1));
150   PetscCall(PetscSectionSetFieldComponents(section, fieldS, sdim*(sdim+1)/2));
151 
152   /* Going through cell sets then cells, and setting up storage for the sections */
153   PetscCall(DMGetLabelSize(dm, "Cell Sets", &numCS));
154   PetscCall(DMGetLabelIdIS(dm, "Cell Sets", &csIS));
155   if (csIS) {PetscCall(ISGetIndices(csIS, &csID));}
156   for (set = 0; set < numCS; set++) {
157     IS                cellIS;
158     const PetscInt   *cellID;
159     PetscInt          numCells, cell, closureSize, *closureA = NULL;
160 
161     PetscCall(DMGetStratumSize(dm, "Cell Sets", csID[set], &numCells));
162     PetscCall(DMGetStratumIS(dm, "Cell Sets", csID[set], &cellIS));
163     if (numCells > 0) {
164       /* dof layout ordered by increasing height in the DAG: cell, face, edge, vertex */
165       PetscInt          dofUP1Tri[]  = {2, 0, 0};
166       PetscInt          dofAP1Tri[]  = {1, 0, 0};
167       PetscInt          dofUP2Tri[]  = {2, 2, 0};
168       PetscInt          dofAP2Tri[]  = {1, 1, 0};
169       PetscInt          dofUP1Quad[] = {2, 0, 0};
170       PetscInt          dofAP1Quad[] = {1, 0, 0};
171       PetscInt          dofUP2Quad[] = {2, 2, 2};
172       PetscInt          dofAP2Quad[] = {1, 1, 1};
173       PetscInt          dofS2D[]     = {0, 0, 3};
174       PetscInt          dofUP1Tet[]  = {3, 0, 0, 0};
175       PetscInt          dofAP1Tet[]  = {1, 0, 0, 0};
176       PetscInt          dofUP2Tet[]  = {3, 3, 0, 0};
177       PetscInt          dofAP2Tet[]  = {1, 1, 0, 0};
178       PetscInt          dofUP1Hex[]  = {3, 0, 0, 0};
179       PetscInt          dofAP1Hex[]  = {1, 0, 0, 0};
180       PetscInt          dofUP2Hex[]  = {3, 3, 3, 3};
181       PetscInt          dofAP2Hex[]  = {1, 1, 1, 1};
182       PetscInt          dofS3D[]     = {0, 0, 0, 6};
183       PetscInt         *dofU, *dofA, *dofS;
184 
185       switch (sdim) {
186       case 2: dofS = dofS2D;break;
187       case 3: dofS = dofS3D;break;
188       default: SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No layout for dimension %" PetscInt_FMT, sdim);
189       }
190 
191       /* Identify cell type based on closure size only. This works for Tri/Tet/Quad/Hex meshes
192          It will not be enough to identify more exotic elements like pyramid or prisms...  */
193       PetscCall(ISGetIndices(cellIS, &cellID));
194       PetscCall(DMPlexGetTransitiveClosure(dm, cellID[0], PETSC_TRUE, &closureSize, &closureA));
195       switch (closureSize) {
196         case 7: /* Tri */
197         if (order == 1) {
198           dofU = dofUP1Tri;
199           dofA = dofAP1Tri;
200         } else {
201           dofU = dofUP2Tri;
202           dofA = dofAP2Tri;
203         }
204         break;
205         case 9: /* Quad */
206         if (order == 1) {
207           dofU = dofUP1Quad;
208           dofA = dofAP1Quad;
209         } else {
210           dofU = dofUP2Quad;
211           dofA = dofAP2Quad;
212         }
213         break;
214         case 15: /* Tet */
215         if (order == 1) {
216           dofU = dofUP1Tet;
217           dofA = dofAP1Tet;
218         } else {
219           dofU = dofUP2Tet;
220           dofA = dofAP2Tet;
221         }
222         break;
223         case 27: /* Hex */
224         if (order == 1) {
225           dofU = dofUP1Hex;
226           dofA = dofAP1Hex;
227         } else {
228           dofU = dofUP2Hex;
229           dofA = dofAP2Hex;
230         }
231         break;
232         default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Unknown element with closure size %" PetscInt_FMT, closureSize);
233       }
234       PetscCall(DMPlexRestoreTransitiveClosure(dm, cellID[0], PETSC_TRUE, &closureSize, &closureA));
235 
236       for (cell = 0; cell < numCells; cell++) {
237         PetscInt *closure = NULL;
238 
239         PetscCall(DMPlexGetTransitiveClosure(dm, cellID[cell], PETSC_TRUE, &closureSize, &closure));
240         for (p = 0; p < closureSize; ++p) {
241           /* Find depth of p */
242           for (d = 0; d <= sdim; ++d) {
243             if ((closure[2*p] >= pStartDepth[d]) && (closure[2*p] < pEndDepth[d])) {
244               PetscCall(PetscSectionSetDof(section, closure[2*p], dofU[d]+dofA[d]+dofS[d]));
245               PetscCall(PetscSectionSetFieldDof(section, closure[2*p], fieldU, dofU[d]));
246               PetscCall(PetscSectionSetFieldDof(section, closure[2*p], fieldA, dofA[d]));
247               PetscCall(PetscSectionSetFieldDof(section, closure[2*p], fieldS, dofS[d]));
248             }
249           }
250         }
251         PetscCall(DMPlexRestoreTransitiveClosure(dm, cellID[cell], PETSC_TRUE, &closureSize, &closure));
252       }
253       PetscCall(ISRestoreIndices(cellIS, &cellID));
254       PetscCall(ISDestroy(&cellIS));
255     }
256   }
257   if (csIS) {PetscCall(ISRestoreIndices(csIS, &csID));}
258   PetscCall(ISDestroy(&csIS));
259   PetscCall(PetscSectionSetUp(section));
260   PetscCall(DMSetLocalSection(dm, section));
261   PetscCall(PetscObjectViewFromOptions((PetscObject) section, NULL, "-dm_section_view"));
262   PetscCall(PetscSectionDestroy(&section));
263 
264   {
265     PetscSF          migrationSF;
266     PetscInt         ovlp = 0;
267     PetscPartitioner part;
268 
269     PetscCall(DMSetUseNatural(dm,PETSC_TRUE));
270     PetscCall(DMPlexGetPartitioner(dm,&part));
271     PetscCall(PetscPartitionerSetFromOptions(part));
272     PetscCall(DMPlexDistribute(dm,ovlp,&migrationSF,&pdm));
273     if (!pdm) pdm = dm;
274     /* Set the migrationSF is mandatory since useNatural = PETSC_TRUE */
275     if (migrationSF) {
276       PetscCall(DMPlexSetMigrationSF(pdm, migrationSF));
277       PetscCall(PetscSFDestroy(&migrationSF));
278     }
279     PetscCall(DMViewFromOptions(pdm, NULL, "-dm_view"));
280   }
281 
282   /* Get DM and IS for each field of dm */
283   PetscCall(DMCreateSubDM(pdm, 1, &fieldU, &isU,  &dmU));
284   PetscCall(DMCreateSubDM(pdm, 1, &fieldA, &isA,  &dmA));
285   PetscCall(DMCreateSubDM(pdm, 1, &fieldS, &isS,  &dmS));
286   PetscCall(DMCreateSubDM(pdm, 2, fieldUA, &isUA, &dmUA));
287 
288   PetscCall(PetscMalloc1(2,&dmList));
289   dmList[0] = dmU;
290   dmList[1] = dmA;
291 
292   PetscCall(DMCreateSuperDM(dmList,2,NULL,&dmUA2));
293   PetscCall(PetscFree(dmList));
294 
295   PetscCall(DMGetGlobalVector(pdm,  &X));
296   PetscCall(DMGetGlobalVector(dmU,  &U));
297   PetscCall(DMGetGlobalVector(dmA,  &A));
298   PetscCall(DMGetGlobalVector(dmS,  &S));
299   PetscCall(DMGetGlobalVector(dmUA, &UA));
300   PetscCall(DMGetGlobalVector(dmUA2, &UA2));
301 
302   PetscCall(PetscObjectSetName((PetscObject) U,  "U"));
303   PetscCall(PetscObjectSetName((PetscObject) A,  "Alpha"));
304   PetscCall(PetscObjectSetName((PetscObject) S,  "Sigma"));
305   PetscCall(PetscObjectSetName((PetscObject) UA, "UAlpha"));
306   PetscCall(PetscObjectSetName((PetscObject) UA2, "UAlpha2"));
307   PetscCall(VecSet(X, -111.));
308 
309   /* Setting u to [x,y,z]  and alpha to x^2+y^2+z^2 by writing in UAlpha then restricting to U and Alpha */
310   {
311     PetscSection sectionUA;
312     Vec          UALoc;
313     PetscSection coordSection;
314     Vec          coord;
315     PetscScalar *cval, *xyz;
316     PetscInt     clSize, i, j;
317 
318     PetscCall(DMGetLocalSection(dmUA, &sectionUA));
319     PetscCall(DMGetLocalVector(dmUA, &UALoc));
320     PetscCall(VecGetArray(UALoc, &cval));
321     PetscCall(DMGetCoordinateSection(dmUA, &coordSection));
322     PetscCall(DMGetCoordinatesLocal(dmUA, &coord));
323     PetscCall(DMPlexGetChart(dmUA, &pStart, &pEnd));
324 
325     for (p = pStart; p < pEnd; ++p) {
326       PetscInt dofUA, offUA;
327 
328       PetscCall(PetscSectionGetDof(sectionUA, p, &dofUA));
329       if (dofUA > 0) {
330         xyz=NULL;
331         PetscCall(PetscSectionGetOffset(sectionUA, p, &offUA));
332         PetscCall(DMPlexVecGetClosure(dmUA, coordSection, coord, p, &clSize, &xyz));
333         cval[offUA+sdim] = 0.;
334         for (i = 0; i < sdim; ++i) {
335           cval[offUA+i] = 0;
336           for (j = 0; j < clSize/sdim; ++j) {
337             cval[offUA+i] += xyz[j*sdim+i];
338           }
339           cval[offUA+i] = cval[offUA+i] * sdim / clSize;
340           cval[offUA+sdim] += PetscSqr(cval[offUA+i]);
341         }
342         PetscCall(DMPlexVecRestoreClosure(dmUA, coordSection, coord, p, &clSize, &xyz));
343       }
344     }
345     PetscCall(VecRestoreArray(UALoc, &cval));
346     PetscCall(DMLocalToGlobalBegin(dmUA, UALoc, INSERT_VALUES, UA));
347     PetscCall(DMLocalToGlobalEnd(dmUA, UALoc, INSERT_VALUES, UA));
348     PetscCall(DMRestoreLocalVector(dmUA, &UALoc));
349 
350     /* Update X */
351     PetscCall(VecISCopy(X, isUA, SCATTER_FORWARD, UA));
352     PetscCall(VecViewFromOptions(UA, NULL, "-ua_vec_view"));
353 
354     /* Restrict to U and Alpha */
355     PetscCall(VecISCopy(X, isU, SCATTER_REVERSE, U));
356     PetscCall(VecISCopy(X, isA, SCATTER_REVERSE, A));
357 
358     /* restrict to UA2 */
359     PetscCall(VecISCopy(X, isUA, SCATTER_REVERSE, UA2));
360     PetscCall(VecViewFromOptions(UA2, NULL, "-ua2_vec_view"));
361   }
362 
363   {
364     Vec          tmpVec;
365     PetscSection coordSection;
366     Vec          coord;
367     PetscReal    norm;
368     PetscReal    time = 1.234;
369 
370     /* Writing nodal variables to ExodusII file */
371     PetscCall(DMSetOutputSequenceNumber(dmU,0,time));
372     PetscCall(DMSetOutputSequenceNumber(dmA,0,time));
373 
374     PetscCall(VecView(U, viewer));
375     PetscCall(VecView(A, viewer));
376 
377     /* Saving U and Alpha in one shot.
378        For this, we need to cheat and change the Vec's name
379        Note that in the end we write variables one component at a time,
380        so that there is no real values in doing this
381     */
382 
383     PetscCall(DMSetOutputSequenceNumber(dmUA,1,time));
384     PetscCall(DMGetGlobalVector(dmUA, &tmpVec));
385     PetscCall(VecCopy(UA, tmpVec));
386     PetscCall(PetscObjectSetName((PetscObject) tmpVec, "U"));
387     PetscCall(VecView(tmpVec, viewer));
388     /* Reading nodal variables in Exodus file */
389     PetscCall(VecSet(tmpVec, -1000.0));
390     PetscCall(VecLoad(tmpVec, viewer));
391     PetscCall(VecAXPY(UA, -1.0, tmpVec));
392     PetscCall(VecNorm(UA, NORM_INFINITY, &norm));
393     PetscCheck(norm <= PETSC_SQRT_MACHINE_EPSILON,PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "UAlpha ||Vin - Vout|| = %g", (double) norm);
394     PetscCall(DMRestoreGlobalVector(dmUA, &tmpVec));
395 
396     /* same thing with the UA2 Vec obtained from the superDM */
397     PetscCall(DMGetGlobalVector(dmUA2, &tmpVec));
398     PetscCall(VecCopy(UA2, tmpVec));
399     PetscCall(PetscObjectSetName((PetscObject) tmpVec, "U"));
400     PetscCall(DMSetOutputSequenceNumber(dmUA2,2,time));
401     PetscCall(VecView(tmpVec, viewer));
402     /* Reading nodal variables in Exodus file */
403     PetscCall(VecSet(tmpVec, -1000.0));
404     PetscCall(VecLoad(tmpVec,viewer));
405     PetscCall(VecAXPY(UA2, -1.0, tmpVec));
406     PetscCall(VecNorm(UA2, NORM_INFINITY, &norm));
407     PetscCheck(norm <= PETSC_SQRT_MACHINE_EPSILON,PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "UAlpha2 ||Vin - Vout|| = %g", (double) norm);
408     PetscCall(DMRestoreGlobalVector(dmUA2, &tmpVec));
409 
410     /* Building and saving Sigma
411        We set sigma_0 = rank (to see partitioning)
412               sigma_1 = cell set ID
413               sigma_2 = x_coordinate of the cell center of mass
414     */
415     PetscCall(DMGetCoordinateSection(dmS, &coordSection));
416     PetscCall(DMGetCoordinatesLocal(dmS, &coord));
417     PetscCall(DMGetLabelIdIS(dmS, "Cell Sets", &csIS));
418     PetscCall(DMGetLabelSize(dmS, "Cell Sets", &numCS));
419     PetscCall(ISGetIndices(csIS, &csID));
420     for (set = 0; set < numCS; ++set) {
421       /* We know that all cells in a cell set have the same type, so we can dimension cval and xyz once for each cell set */
422       IS              cellIS;
423       const PetscInt *cellID;
424       PetscInt        numCells, cell;
425       PetscScalar    *cval = NULL, *xyz  = NULL;
426       PetscInt        clSize, cdimCoord, c;
427 
428       PetscCall(DMGetStratumIS(dmS, "Cell Sets", csID[set], &cellIS));
429       PetscCall(ISGetIndices(cellIS, &cellID));
430       PetscCall(ISGetSize(cellIS, &numCells));
431       for (cell = 0; cell < numCells; cell++) {
432         PetscCall(DMPlexVecGetClosure(dmS, NULL, S, cellID[cell], &clSize, &cval));
433         PetscCall(DMPlexVecGetClosure(dmS, coordSection, coord, cellID[cell], &cdimCoord, &xyz));
434         cval[0] = rank;
435         cval[1] = csID[set];
436         cval[2] = 0.;
437         for (c = 0; c < cdimCoord/sdim; c++) cval[2] += xyz[c*sdim];
438         cval[2] = cval[2] * sdim / cdimCoord;
439         PetscCall(DMPlexVecSetClosure(dmS, NULL, S, cellID[cell], cval, INSERT_ALL_VALUES));
440       }
441       PetscCall(DMPlexVecRestoreClosure(dmS, NULL, S, cellID[0], &clSize, &cval));
442       PetscCall(DMPlexVecRestoreClosure(dmS, coordSection, coord, cellID[0], NULL, &xyz));
443       PetscCall(ISRestoreIndices(cellIS, &cellID));
444       PetscCall(ISDestroy(&cellIS));
445     }
446     PetscCall(ISRestoreIndices(csIS, &csID));
447     PetscCall(ISDestroy(&csIS));
448     PetscCall(VecViewFromOptions(S, NULL, "-s_vec_view"));
449 
450     /* Writing zonal variables in Exodus file */
451     PetscCall(DMSetOutputSequenceNumber(dmS,0,time));
452     PetscCall(VecView(S,viewer));
453 
454     /* Reading zonal variables in Exodus file */
455     PetscCall(DMGetGlobalVector(dmS, &tmpVec));
456     PetscCall(VecSet(tmpVec, -1000.0));
457     PetscCall(PetscObjectSetName((PetscObject) tmpVec, "Sigma"));
458     PetscCall(VecLoad(tmpVec,viewer));
459     PetscCall(VecAXPY(S, -1.0, tmpVec));
460     PetscCall(VecNorm(S, NORM_INFINITY, &norm));
461     PetscCheck(norm <= PETSC_SQRT_MACHINE_EPSILON,PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Sigma ||Vin - Vout|| = %g", (double) norm);
462     PetscCall(DMRestoreGlobalVector(dmS, &tmpVec));
463   }
464   PetscCall(PetscViewerDestroy(&viewer));
465 
466   PetscCall(DMRestoreGlobalVector(dmUA2, &UA2));
467   PetscCall(DMRestoreGlobalVector(dmUA, &UA));
468   PetscCall(DMRestoreGlobalVector(dmS,  &S));
469   PetscCall(DMRestoreGlobalVector(dmA,  &A));
470   PetscCall(DMRestoreGlobalVector(dmU,  &U));
471   PetscCall(DMRestoreGlobalVector(pdm,   &X));
472   PetscCall(DMDestroy(&dmU));PetscCall(ISDestroy(&isU));
473   PetscCall(DMDestroy(&dmA));PetscCall(ISDestroy(&isA));
474   PetscCall(DMDestroy(&dmS));PetscCall(ISDestroy(&isS));
475   PetscCall(DMDestroy(&dmUA));PetscCall(ISDestroy(&isUA));
476   PetscCall(DMDestroy(&dmUA2));
477   PetscCall(DMDestroy(&pdm));
478   if (size > 1) PetscCall(DMDestroy(&dm));
479   PetscCall(PetscFree2(pStartDepth, pEndDepth));
480   PetscCall(PetscFinalize());
481   return 0;
482 }
483 
484 /*TEST
485 
486   build:
487     requires: exodusii pnetcdf !complex
488   # 2D seq
489   test:
490     suffix: 0
491     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourSquareT-large.exo -o FourSquareT-large_out.exo -dm_view -petscpartitioner_type simple -order 1
492     #TODO: bug in call to NetCDF failed to complete invalid type definition in file id 65536 NetCDF: One or more variable sizes violate format constraints
493   test:
494     suffix: 1
495     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourSquareQ-large.exo -o FourSquareQ-large_out.exo -dm_view -petscpartitioner_type simple -order 1
496   test:
497     suffix: 2
498     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourSquareH-large.exo -o FourSquareH-large_out.exo -dm_view -petscpartitioner_type simple -order 1
499     #TODO: bug in call to NetCDF failed to complete invalid type definition in file id 65536 NetCDF: One or more variable sizes violate format constraints
500   test:
501     suffix: 3
502     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourSquareT-large.exo -o FourSquareT-large_out.exo -dm_view -petscpartitioner_type simple -order 2
503   test:
504     suffix: 4
505     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourSquareQ-large.exo -o FourSquareQ-large_out.exo -dm_view -petscpartitioner_type simple -order 2
506   test:
507     suffix: 5
508     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourSquareH-large.exo -o FourSquareH-large_out.exo -dm_view -petscpartitioner_type simple -order 2
509 
510   # 2D par
511   test:
512     suffix: 6
513     nsize: 2
514     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourSquareT-large.exo -o FourSquareT-large_out.exo -dm_view -petscpartitioner_type simple -order 1
515     #TODO: bug in call to NetCDF failed to complete invalid type definition in file id 65536 NetCDF: One or more variable sizes violate format constraints
516   test:
517     suffix: 7
518     nsize: 2
519     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourSquareQ-large.exo -o FourSquareQ-large_out.exo -dm_view -petscpartitioner_type simple -order 1
520   test:
521     suffix: 8
522     nsize: 2
523     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourSquareH-large.exo -o FourSquareH-large_out.exo -dm_view -petscpartitioner_type simple -order 1
524     #TODO: bug in call to NetCDF failed to complete invalid type definition in file id 65536 NetCDF: invalid dimension ID or name
525   test:
526     suffix: 9
527     nsize: 2
528     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourSquareT-large.exo -o FourSquareT-large_out.exo -dm_view -petscpartitioner_type simple -order 2
529   test:
530     suffix: 10
531     nsize: 2
532     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourSquareQ-large.exo -o FourSquareQ-large_out.exo -dm_view -petscpartitioner_type simple -order 2
533   test:
534     # Something is now broken with parallel read/write for wahtever shape H is
535     TODO: broken
536     suffix: 11
537     nsize: 2
538     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourSquareH-large.exo -o FourSquareH-large_out.exo -dm_view -petscpartitioner_type simple -order 2
539 
540   #3d seq
541   test:
542     suffix: 12
543     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourBrickHex-large.exo -o FourBrickHex-large_out.exo -dm_view -petscpartitioner_type simple -order 1
544   test:
545     suffix: 13
546     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourBrickTet-large.exo -o FourBrickTet-large_out.exo -dm_view -petscpartitioner_type simple -order 1
547     #TODO: bug in call to NetCDF failed to complete invalid type definition in file id 65536 NetCDF: One or more variable sizes violate format constraints
548   test:
549     suffix: 14
550     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourBrickHex-large.exo -o FourBrickHex-large_out.exo -dm_view -petscpartitioner_type simple -order 2
551   test:
552     suffix: 15
553     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourBrickTet-large.exo -o FourBrickTet-large_out.exo -dm_view -petscpartitioner_type simple -order 2
554     #TODO: bug in call to NetCDF failed to complete invalid type definition in file id 65536 NetCDF: One or more variable sizes violate format constraints
555   #3d par
556   test:
557     suffix: 16
558     nsize: 2
559     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourBrickHex-large.exo -o FourBrickHex-large_out.exo -dm_view -petscpartitioner_type simple -order 1
560   test:
561     suffix: 17
562     nsize: 2
563     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourBrickTet-large.exo -o FourBrickTet-large_out.exo -dm_view -petscpartitioner_type simple -order 1
564     #TODO: bug in call to NetCDF failed to complete invalid type definition in file id 65536 NetCDF: One or more variable sizes violate format constraints
565   test:
566     suffix: 18
567     nsize: 2
568     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourBrickHex-large.exo -o FourBrickHex-large_out.exo -dm_view -petscpartitioner_type simple -order 2
569   test:
570     suffix: 19
571     nsize: 2
572     args: -i ${wPETSC_DIR}/share/petsc/datafiles/meshes/FourBrickTet-large.exo -o FourBrickTet-large_out.exo -dm_view -petscpartitioner_type simple -order 2
573     #TODO: bug in call to NetCDF failed to complete invalid type definition in file id 65536 NetCDF: One or more variable sizes violate format constraints
574 
575 TEST*/
576