xref: /petsc/src/dm/impls/plex/plex.c (revision 8a84db2dfe7416430d07f30490d8cd16f3a5bd12)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2af0996ceSBarry Smith #include <petsc/private/isimpl.h>
3e5c6e791SKarl Rupp #include <petsc/private/vecimpl.h>
40c312b8eSJed Brown #include <petscsf.h>
5e228b242SToby Isaac #include <petscds.h>
6e412dcbdSMatthew G. Knepley #include <petscdraw.h>
7552f7358SJed Brown 
8552f7358SJed Brown /* Logging support */
925afeb17SMatthew G. Knepley PetscLogEvent DMPLEX_Interpolate, PETSCPARTITIONER_Partition, DMPLEX_Distribute, DMPLEX_DistributeCones, DMPLEX_DistributeLabels, DMPLEX_DistributeSF, DMPLEX_DistributeOverlap, DMPLEX_DistributeField, DMPLEX_DistributeData, DMPLEX_Migrate, DMPLEX_InterpolateSF, DMPLEX_GlobalToNaturalBegin, DMPLEX_GlobalToNaturalEnd, DMPLEX_NaturalToGlobalBegin, DMPLEX_NaturalToGlobalEnd, DMPLEX_Stratify, DMPLEX_Preallocate, DMPLEX_ResidualFEM, DMPLEX_JacobianFEM, DMPLEX_InterpolatorFEM, DMPLEX_InjectorFEM, DMPLEX_IntegralFEM, DMPLEX_CreateGmsh;
10552f7358SJed Brown 
115a576424SJed Brown PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);
122c40f234SMatthew G. Knepley PETSC_EXTERN PetscErrorCode VecLoad_Default(Vec, PetscViewer);
13552f7358SJed Brown 
147e42fee7SMatthew G. Knepley #undef __FUNCT__
157afe7537SMatthew G. Knepley #define __FUNCT__ "DMPlexGetFieldType_Internal"
167afe7537SMatthew G. Knepley PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
177e42fee7SMatthew G. Knepley {
1880d5bdc6SMatthew G. Knepley   PetscInt       dim, pStart, pEnd, vStart, vEnd, cStart, cEnd, cEndInterior, vdof = 0, cdof = 0;
197e42fee7SMatthew G. Knepley   PetscErrorCode ierr;
207e42fee7SMatthew G. Knepley 
217e42fee7SMatthew G. Knepley   PetscFunctionBegin;
227e42fee7SMatthew G. Knepley   *ft  = PETSC_VTK_POINT_FIELD;
23c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
247e42fee7SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
257e42fee7SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2680d5bdc6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2780d5bdc6SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
287e42fee7SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
297e42fee7SMatthew G. Knepley   if (field >= 0) {
307e42fee7SMatthew G. Knepley     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, vStart, field, &vdof);CHKERRQ(ierr);}
317e42fee7SMatthew G. Knepley     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, cStart, field, &cdof);CHKERRQ(ierr);}
327e42fee7SMatthew G. Knepley   } else {
337e42fee7SMatthew G. Knepley     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetDof(section, vStart, &vdof);CHKERRQ(ierr);}
347e42fee7SMatthew G. Knepley     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetDof(section, cStart, &cdof);CHKERRQ(ierr);}
357e42fee7SMatthew G. Knepley   }
367e42fee7SMatthew G. Knepley   if (vdof) {
377e42fee7SMatthew G. Knepley     *sStart = vStart;
387e42fee7SMatthew G. Knepley     *sEnd   = vEnd;
397e42fee7SMatthew G. Knepley     if (vdof == dim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
407e42fee7SMatthew G. Knepley     else             *ft = PETSC_VTK_POINT_FIELD;
417e42fee7SMatthew G. Knepley   } else if (cdof) {
427e42fee7SMatthew G. Knepley     *sStart = cStart;
437e42fee7SMatthew G. Knepley     *sEnd   = cEnd;
447e42fee7SMatthew G. Knepley     if (cdof == dim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
457e42fee7SMatthew G. Knepley     else             *ft = PETSC_VTK_CELL_FIELD;
467e42fee7SMatthew G. Knepley   } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Could not classify input Vec for VTK");
477e42fee7SMatthew G. Knepley   PetscFunctionReturn(0);
487e42fee7SMatthew G. Knepley }
497e42fee7SMatthew G. Knepley 
50552f7358SJed Brown #undef __FUNCT__
51552f7358SJed Brown #define __FUNCT__ "VecView_Plex_Local"
52552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
53552f7358SJed Brown {
54552f7358SJed Brown   DM             dm;
55b136c2c9SMatthew G. Knepley   PetscBool      isvtk, ishdf5, isseq;
56552f7358SJed Brown   PetscErrorCode ierr;
57552f7358SJed Brown 
58552f7358SJed Brown   PetscFunctionBegin;
59552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
6082f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
61552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
62b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
63b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
64ef31f671SMatthew G. Knepley   if (isvtk || ishdf5) {
65ef31f671SMatthew G. Knepley     PetscInt  numFields;
66ef31f671SMatthew G. Knepley     PetscBool fem = PETSC_FALSE;
67ef31f671SMatthew G. Knepley 
68ef31f671SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
69ef31f671SMatthew G. Knepley     if (numFields) {
70ef31f671SMatthew G. Knepley       PetscObject fe;
71ef31f671SMatthew G. Knepley 
72ef31f671SMatthew G. Knepley       ierr = DMGetField(dm, 0, &fe);CHKERRQ(ierr);
73ef31f671SMatthew G. Knepley       if (fe->classid == PETSCFE_CLASSID) fem = PETSC_TRUE;
74ef31f671SMatthew G. Knepley     }
75bdd6f66aSToby Isaac     if (fem) {ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, v, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);}
76ef31f671SMatthew G. Knepley   }
77552f7358SJed Brown   if (isvtk) {
78552f7358SJed Brown     PetscSection            section;
79b136c2c9SMatthew G. Knepley     PetscViewerVTKFieldType ft;
80b136c2c9SMatthew G. Knepley     PetscInt                pStart, pEnd;
81552f7358SJed Brown 
82552f7358SJed Brown     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
837afe7537SMatthew G. Knepley     ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
84552f7358SJed Brown     ierr = PetscObjectReference((PetscObject) v);CHKERRQ(ierr);  /* viewer drops reference */
85552f7358SJed Brown     ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, ft, (PetscObject) v);CHKERRQ(ierr);
86b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
87b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
88b136c2c9SMatthew G. Knepley     ierr = VecView_Plex_Local_HDF5(v, viewer);CHKERRQ(ierr);
89b136c2c9SMatthew G. Knepley #else
90b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
91b136c2c9SMatthew G. Knepley #endif
92552f7358SJed Brown   } else {
9355f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
9455f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
95552f7358SJed Brown   }
96552f7358SJed Brown   PetscFunctionReturn(0);
97552f7358SJed Brown }
98552f7358SJed Brown 
99552f7358SJed Brown #undef __FUNCT__
100e412dcbdSMatthew G. Knepley #define __FUNCT__ "VecView_Plex_Draw"
101e412dcbdSMatthew G. Knepley PetscErrorCode VecView_Plex_Draw(Vec v, PetscViewer viewer)
102e412dcbdSMatthew G. Knepley {
103e412dcbdSMatthew G. Knepley   DM                 dm;
104e412dcbdSMatthew G. Knepley   PetscDraw          draw, popup;
105e412dcbdSMatthew G. Knepley   DM                 cdm;
106e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
107e412dcbdSMatthew G. Knepley   Vec                coordinates;
108e412dcbdSMatthew G. Knepley   const PetscScalar *coords, *array;
109e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
110e412dcbdSMatthew G. Knepley   PetscReal          min, max;
111e412dcbdSMatthew G. Knepley   PetscBool          isnull;
112e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N, level;
113e412dcbdSMatthew G. Knepley   const char        *name;
114e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
115e412dcbdSMatthew G. Knepley 
116e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
117e412dcbdSMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
118e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
119e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
120e412dcbdSMatthew G. Knepley   ierr = DMGetCoarsenLevel(dm, &level);CHKERRQ(ierr);
121e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
122e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
123e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
124e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
125e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
126e412dcbdSMatthew G. Knepley 
127e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
128e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
129e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
130e412dcbdSMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
131e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, name);CHKERRQ(ierr);
132e412dcbdSMatthew G. Knepley 
133e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
134e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
135e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
1360c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
1370c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
138e412dcbdSMatthew G. Knepley   }
139e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
140e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
141e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
142e412dcbdSMatthew G. Knepley 
143e412dcbdSMatthew G. Knepley   ierr = VecMin(v, NULL, &min);CHKERRQ(ierr);
144e412dcbdSMatthew G. Knepley   ierr = VecMax(v, NULL, &max);CHKERRQ(ierr);
145e412dcbdSMatthew G. Knepley   ierr = PetscDrawGetPopup(draw, &popup);CHKERRQ(ierr);
146e412dcbdSMatthew G. Knepley   ierr = PetscDrawScalePopup(popup, min, max);CHKERRQ(ierr);
147e412dcbdSMatthew G. Knepley 
148e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(v, &array);CHKERRQ(ierr);
149e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
15099a2f7bcSMatthew G. Knepley     PetscScalar *coords = NULL, *a = NULL;
151e412dcbdSMatthew G. Knepley     PetscInt     numCoords, color;
152e412dcbdSMatthew G. Knepley 
153e412dcbdSMatthew G. Knepley     ierr = DMPlexPointGlobalRead(dm, c, array, &a);CHKERRQ(ierr);
154e412dcbdSMatthew G. Knepley     color = PetscDrawRealToColor(PetscRealPart(a[0]), min, max);
155e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
156e412dcbdSMatthew G. Knepley     switch (numCoords) {
157e412dcbdSMatthew G. Knepley     case 6:
1580c81f2a8SMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), color, color, color);CHKERRQ(ierr);
159e412dcbdSMatthew G. Knepley       break;
160e412dcbdSMatthew G. Knepley     case 8:
1610c81f2a8SMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), color, color, color);CHKERRQ(ierr);
1620c81f2a8SMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), color, color, color);CHKERRQ(ierr);
163e412dcbdSMatthew G. Knepley       break;
164e412dcbdSMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
165e412dcbdSMatthew G. Knepley     }
166e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
167e412dcbdSMatthew G. Knepley   }
168e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &array);CHKERRQ(ierr);
169e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
170e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
171e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
172e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
173e412dcbdSMatthew G. Knepley }
174e412dcbdSMatthew G. Knepley 
175e412dcbdSMatthew G. Knepley #undef __FUNCT__
176552f7358SJed Brown #define __FUNCT__ "VecView_Plex"
177552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
178552f7358SJed Brown {
179552f7358SJed Brown   DM             dm;
180e412dcbdSMatthew G. Knepley   PetscBool      isvtk, ishdf5, isdraw, isseq;
181552f7358SJed Brown   PetscErrorCode ierr;
182552f7358SJed Brown 
183552f7358SJed Brown   PetscFunctionBegin;
184552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
18582f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
186552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
18733c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
188e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW, &isdraw);CHKERRQ(ierr);
18955f2e967SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
190552f7358SJed Brown   if (isvtk) {
191552f7358SJed Brown     Vec         locv;
192552f7358SJed Brown     const char *name;
193552f7358SJed Brown 
194552f7358SJed Brown     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
195552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
196552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
197552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
198552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
199552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
200552f7358SJed Brown     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
201b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
202b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
203b136c2c9SMatthew G. Knepley     ierr = VecView_Plex_HDF5(v, viewer);CHKERRQ(ierr);
204b136c2c9SMatthew G. Knepley #else
205b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
206b136c2c9SMatthew G. Knepley #endif
207e412dcbdSMatthew G. Knepley   } else if (isdraw) {
208e412dcbdSMatthew G. Knepley     ierr = VecView_Plex_Draw(v, viewer);CHKERRQ(ierr);
209552f7358SJed Brown   } else {
21055f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
21155f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
212552f7358SJed Brown   }
213552f7358SJed Brown   PetscFunctionReturn(0);
214552f7358SJed Brown }
215552f7358SJed Brown 
216552f7358SJed Brown #undef __FUNCT__
217d930f514SMatthew G. Knepley #define __FUNCT__ "VecView_Plex_Native"
218d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
219d930f514SMatthew G. Knepley {
220d930f514SMatthew G. Knepley   DM                dm;
221d930f514SMatthew G. Knepley   MPI_Comm          comm;
222d930f514SMatthew G. Knepley   PetscViewerFormat format;
223d930f514SMatthew G. Knepley   Vec               v;
224d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
225d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
226d930f514SMatthew G. Knepley 
227d930f514SMatthew G. Knepley   PetscFunctionBegin;
228d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
229d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
2302c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
231d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
232d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
233d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
234d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
235d930f514SMatthew G. Knepley     const char *vecname;
236d930f514SMatthew G. Knepley     PetscInt    n, nroots;
237d930f514SMatthew G. Knepley 
238d930f514SMatthew G. Knepley     if (dm->sfNatural) {
239ca43db0aSBarry Smith       ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
240d930f514SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
241d930f514SMatthew G. Knepley       if (n == nroots) {
242d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
243d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
244d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
245d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
246d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
247d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
248d930f514SMatthew G. Knepley     } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
249d930f514SMatthew G. Knepley   } else {
250d930f514SMatthew G. Knepley     /* we are viewing a natural DMPlex vec. */
251d930f514SMatthew G. Knepley     v = originalv;
252d930f514SMatthew G. Knepley   }
253d930f514SMatthew G. Knepley   if (ishdf5) {
254d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
255d930f514SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native(v, viewer);CHKERRQ(ierr);
256d930f514SMatthew G. Knepley #else
257d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
258d930f514SMatthew G. Knepley #endif
259d930f514SMatthew G. Knepley   } else if (isvtk) {
260d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
261d930f514SMatthew G. Knepley   } else {
262d930f514SMatthew G. Knepley     PetscBool isseq;
263d930f514SMatthew G. Knepley 
264d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
265d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
266d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
267d930f514SMatthew G. Knepley   }
268d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
269d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
270d930f514SMatthew G. Knepley }
271d930f514SMatthew G. Knepley 
272d930f514SMatthew G. Knepley #undef __FUNCT__
2732c40f234SMatthew G. Knepley #define __FUNCT__ "VecLoad_Plex_Local"
2742c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
2752c40f234SMatthew G. Knepley {
2762c40f234SMatthew G. Knepley   DM             dm;
2772c40f234SMatthew G. Knepley   PetscBool      ishdf5;
2782c40f234SMatthew G. Knepley   PetscErrorCode ierr;
2792c40f234SMatthew G. Knepley 
2802c40f234SMatthew G. Knepley   PetscFunctionBegin;
2812c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
2822c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
2832c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
2842c40f234SMatthew G. Knepley   if (ishdf5) {
2852c40f234SMatthew G. Knepley     DM          dmBC;
2862c40f234SMatthew G. Knepley     Vec         gv;
2872c40f234SMatthew G. Knepley     const char *name;
2882c40f234SMatthew G. Knepley 
2892c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
2902c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
2912c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
2922c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
2932c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
2942c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
2952c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
2962c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
2972c40f234SMatthew G. Knepley   } else {
2982c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
2992c40f234SMatthew G. Knepley   }
3002c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
3012c40f234SMatthew G. Knepley }
3022c40f234SMatthew G. Knepley 
3032c40f234SMatthew G. Knepley #undef __FUNCT__
3042c40f234SMatthew G. Knepley #define __FUNCT__ "VecLoad_Plex"
3052c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
3062c40f234SMatthew G. Knepley {
3072c40f234SMatthew G. Knepley   DM             dm;
3082c40f234SMatthew G. Knepley   PetscBool      ishdf5;
3092c40f234SMatthew G. Knepley   PetscErrorCode ierr;
3102c40f234SMatthew G. Knepley 
3112c40f234SMatthew G. Knepley   PetscFunctionBegin;
3122c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
3132c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
3142c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
3152c40f234SMatthew G. Knepley   if (ishdf5) {
316878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
317b136c2c9SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5(v, viewer);CHKERRQ(ierr);
318b136c2c9SMatthew G. Knepley #else
319b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
320878b459fSMatthew G. Knepley #endif
3212c40f234SMatthew G. Knepley   } else {
3222c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
323552f7358SJed Brown   }
324552f7358SJed Brown   PetscFunctionReturn(0);
325552f7358SJed Brown }
326552f7358SJed Brown 
327552f7358SJed Brown #undef __FUNCT__
328d930f514SMatthew G. Knepley #define __FUNCT__ "VecLoad_Plex_Native"
329d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
330d930f514SMatthew G. Knepley {
331d930f514SMatthew G. Knepley   DM                dm;
332d930f514SMatthew G. Knepley   PetscViewerFormat format;
333d930f514SMatthew G. Knepley   PetscBool         ishdf5;
334d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
335d930f514SMatthew G. Knepley 
336d930f514SMatthew G. Knepley   PetscFunctionBegin;
337d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
338d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
339d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
340d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
341d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
342d930f514SMatthew G. Knepley     if (dm->sfNatural) {
343d930f514SMatthew G. Knepley       if (ishdf5) {
344d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
345d930f514SMatthew G. Knepley         Vec         v;
346d930f514SMatthew G. Knepley         const char *vecname;
347d930f514SMatthew G. Knepley 
348d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
349d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
350d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
351d930f514SMatthew G. Knepley         ierr = VecLoad_Plex_HDF5_Native(v, viewer);CHKERRQ(ierr);
352d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
353d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
354d930f514SMatthew G. Knepley         ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
355d930f514SMatthew G. Knepley #else
356d930f514SMatthew G. Knepley         SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
357d930f514SMatthew G. Knepley #endif
358d930f514SMatthew G. Knepley       } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
359d930f514SMatthew G. Knepley     }
360d930f514SMatthew G. Knepley   }
361d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
362d930f514SMatthew G. Knepley }
363d930f514SMatthew G. Knepley 
364d930f514SMatthew G. Knepley #undef __FUNCT__
365731e8ddeSMatthew G. Knepley #define __FUNCT__ "DMPlexView_Ascii_Geometry"
366731e8ddeSMatthew G. Knepley PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
367731e8ddeSMatthew G. Knepley {
368731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
369731e8ddeSMatthew G. Knepley   Vec                coordinates;
370731e8ddeSMatthew G. Knepley   DMLabel            depthLabel;
371731e8ddeSMatthew G. Knepley   const char        *name[4];
372731e8ddeSMatthew G. Knepley   const PetscScalar *a;
373731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
374731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
375731e8ddeSMatthew G. Knepley 
376731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
377731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
378731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
379731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
380731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
381731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
382731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
383731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
384731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
385731e8ddeSMatthew G. Knepley   name[1]     = "edge";
386731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
387731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
388731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
389731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
390731e8ddeSMatthew G. Knepley     PetscInt  closureSize, cl;
391731e8ddeSMatthew G. Knepley 
392f347f43bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D:\n", c);CHKERRQ(ierr);
393731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
394731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
395731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
396731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
397731e8ddeSMatthew G. Knepley 
398731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
399731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
400731e8ddeSMatthew G. Knepley       if (!dof) continue;
401731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
402731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
403f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
404731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
405731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
406731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
407731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
40841477eefSMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
409731e8ddeSMatthew G. Knepley         }
410731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
411731e8ddeSMatthew G. Knepley       }
412731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
413731e8ddeSMatthew G. Knepley     }
414731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
415731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
416731e8ddeSMatthew G. Knepley   }
417731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
418731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
419731e8ddeSMatthew G. Knepley }
420731e8ddeSMatthew G. Knepley 
421731e8ddeSMatthew G. Knepley #undef __FUNCT__
422552f7358SJed Brown #define __FUNCT__ "DMPlexView_Ascii"
423552f7358SJed Brown PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
424552f7358SJed Brown {
425552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
426552f7358SJed Brown   DM                cdm;
427552f7358SJed Brown   DMLabel           markers;
428552f7358SJed Brown   PetscSection      coordSection;
429552f7358SJed Brown   Vec               coordinates;
430552f7358SJed Brown   PetscViewerFormat format;
431552f7358SJed Brown   PetscErrorCode    ierr;
432552f7358SJed Brown 
433552f7358SJed Brown   PetscFunctionBegin;
434552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
435552f7358SJed Brown   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
436552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
437552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
438552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
439552f7358SJed Brown     const char *name;
440552f7358SJed Brown     PetscInt    maxConeSize, maxSupportSize;
441552f7358SJed Brown     PetscInt    pStart, pEnd, p;
442552f7358SJed Brown     PetscMPIInt rank, size;
443552f7358SJed Brown 
44482f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
44582f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
446552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
447552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
448552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
449552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "Mesh '%s':\n", name);CHKERRQ(ierr);
450552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "orientation is missing\n", name);CHKERRQ(ierr);
451552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "cap --> base:\n", name);CHKERRQ(ierr);
4524d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
4534d4c343aSBarry Smith     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max sizes cone: %D support: %D\n", rank,maxConeSize, maxSupportSize);CHKERRQ(ierr);
454552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
455552f7358SJed Brown       PetscInt dof, off, s;
456552f7358SJed Brown 
457552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
458552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
459552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
460e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
461552f7358SJed Brown       }
462552f7358SJed Brown     }
463552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
464552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "base <-- cap:\n", name);CHKERRQ(ierr);
465552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
466552f7358SJed Brown       PetscInt dof, off, c;
467552f7358SJed Brown 
468552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
469552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
470552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
471e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
472552f7358SJed Brown       }
473552f7358SJed Brown     }
474552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4754d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
4760298fd71SBarry Smith     ierr = PetscSectionGetChart(coordSection, &pStart, NULL);CHKERRQ(ierr);
477552f7358SJed Brown     if (pStart >= 0) {ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);}
478c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
479552f7358SJed Brown     ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);
480552f7358SJed Brown     if (size > 1) {
481552f7358SJed Brown       PetscSF sf;
482552f7358SJed Brown 
483552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
484552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
485552f7358SJed Brown     }
486552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
487552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
4880588280cSMatthew G. Knepley     const char  *name, *color;
4890588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
4900588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
491552f7358SJed Brown     PetscReal    scale         = 2.0;
4920588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
4930588280cSMatthew G. Knepley     double       tcoords[3];
494552f7358SJed Brown     PetscScalar *coords;
4950588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
496552f7358SJed Brown     PetscMPIInt  rank, size;
4970588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
498552f7358SJed Brown 
4990588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
500552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
501c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
5020588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
5030588280cSMatthew G. Knepley     numColors  = 10;
5040588280cSMatthew G. Knepley     numLColors = 10;
5050588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
506c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
507c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
508c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
5090588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
510c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
5110588280cSMatthew G. Knepley     if (!useColors) {
5120588280cSMatthew G. Knepley       numColors = 3;
5130588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
5140588280cSMatthew G. Knepley     }
515c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
5160588280cSMatthew G. Knepley     if (!useColors) {
5170588280cSMatthew G. Knepley       numLColors = 4;
5180588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
5190588280cSMatthew G. Knepley     }
52082f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
52182f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
522552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
523770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
5240588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
525552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
526552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
527552f7358SJed Brown \\usetikzlibrary{arrows}\n\
5280588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
5290588280cSMatthew G. Knepley     if (size > 1) {
530f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
531770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
532770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
533770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
534770b213bSMatthew G Knepley         } else if (p > 0) {
535770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
536770b213bSMatthew G Knepley         }
537770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
538770b213bSMatthew G Knepley       }
5390588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
5400588280cSMatthew G. Knepley     }
5410588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", 1.0);CHKERRQ(ierr);
542552f7358SJed Brown     /* Plot vertices */
543552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
544552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
5454d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
546552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
547552f7358SJed Brown       PetscInt  off, dof, d;
5480588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
549552f7358SJed Brown 
550552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
551552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
5520588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
553f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
5540588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
5550588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
5560588280cSMatthew G. Knepley         tcoords[d] = PetscAbsReal(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
5570588280cSMatthew G. Knepley       }
5580588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
5590588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
560552f7358SJed Brown       for (d = 0; d < dof; ++d) {
561552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
5620588280cSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]);CHKERRQ(ierr);
563552f7358SJed Brown       }
5640588280cSMatthew G. Knepley       color = colors[rank%numColors];
5650588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
5660588280cSMatthew G. Knepley         PetscInt val;
567c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
5680588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
5690588280cSMatthew G. Knepley       }
5700588280cSMatthew G. Knepley       if (useNumbers) {
571e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
5720588280cSMatthew G. Knepley       } else {
573e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
5740588280cSMatthew G. Knepley       }
575552f7358SJed Brown     }
576552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
577552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
578552f7358SJed Brown     /* Plot edges */
5790588280cSMatthew G. Knepley     if (depth > 1) {ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);}
5800588280cSMatthew G. Knepley     if (dim < 3 && useNumbers) {
581552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
582552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
583552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
584552f7358SJed Brown         const PetscInt *cone;
585552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
586552f7358SJed Brown 
587552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
588f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
589552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
590552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
591552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
592552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
593552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
594552f7358SJed Brown         for (d = 0; d < dof; ++d) {
5950588280cSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
5960588280cSMatthew G. Knepley           tcoords[d] = PetscAbsReal(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
597552f7358SJed Brown         }
5980588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
5990588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
6000588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
6010588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
602f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
6030588280cSMatthew G. Knepley         }
6040588280cSMatthew G. Knepley         color = colors[rank%numColors];
6050588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
6060588280cSMatthew G. Knepley           PetscInt val;
607c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
6080750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
6090588280cSMatthew G. Knepley         }
610e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
611552f7358SJed Brown       }
612552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
613552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
614552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
6150588280cSMatthew G. Knepley     }
616552f7358SJed Brown     /* Plot cells */
6170588280cSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
6180588280cSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
6190588280cSMatthew G. Knepley         const PetscInt *cone;
6200588280cSMatthew G. Knepley 
6210588280cSMatthew G. Knepley         color = colors[rank%numColors];
6220588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
6230588280cSMatthew G. Knepley           PetscInt val;
624c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
6250588280cSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
6260588280cSMatthew G. Knepley         }
6270588280cSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
628e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
6290588280cSMatthew G. Knepley       }
6300588280cSMatthew G. Knepley     } else {
631552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
632552f7358SJed Brown       for (c = cStart; c < cEnd; ++c) {
6330298fd71SBarry Smith         PetscInt *closure = NULL;
634552f7358SJed Brown         PetscInt  closureSize, firstPoint = -1;
635552f7358SJed Brown 
636552f7358SJed Brown         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
637552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
638552f7358SJed Brown         for (p = 0; p < closureSize*2; p += 2) {
639552f7358SJed Brown           const PetscInt point = closure[p];
640552f7358SJed Brown 
641552f7358SJed Brown           if ((point < vStart) || (point >= vEnd)) continue;
642552f7358SJed Brown           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
643e4b003c7SBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
644552f7358SJed Brown           if (firstPoint < 0) firstPoint = point;
645552f7358SJed Brown         }
646552f7358SJed Brown         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
647e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
648552f7358SJed Brown         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
649552f7358SJed Brown       }
6500588280cSMatthew G. Knepley     }
651552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6524d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
6530588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
654770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
6550588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
6560588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
6570588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
6580588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
659552f7358SJed Brown   } else {
66082f516ccSBarry Smith     MPI_Comm    comm;
661834065abSMatthew G. Knepley     PetscInt   *sizes, *hybsizes;
662834065abSMatthew G. Knepley     PetscInt    locDepth, depth, dim, d, pMax[4];
663552f7358SJed Brown     PetscInt    pStart, pEnd, p;
664a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
6651143a3c0SMatthew G. Knepley     const char *name;
666552f7358SJed Brown     PetscMPIInt size;
667552f7358SJed Brown 
66882f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
669552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
670c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6715f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
6721143a3c0SMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimensions:\n", name, dim);CHKERRQ(ierr);}
6731143a3c0SMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimensions:\n", dim);CHKERRQ(ierr);}
674552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
675b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
676bb81ee50SMatthew G. Knepley     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, &pMax[1], &pMax[0]);CHKERRQ(ierr);
677dcca6d9dSJed Brown     ierr = PetscMalloc2(size,&sizes,size,&hybsizes);CHKERRQ(ierr);
678552f7358SJed Brown     if (depth == 1) {
679552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
680552f7358SJed Brown       pEnd = pEnd - pStart;
681552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
682f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);CHKERRQ(ierr);
683552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
684552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
685552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
686552f7358SJed Brown       pEnd = pEnd - pStart;
687552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
688552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
689552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
690552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
691552f7358SJed Brown     } else {
692cbb7f117SMark Adams       PetscMPIInt rank;
693cbb7f117SMark Adams       ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
694552f7358SJed Brown       for (d = 0; d <= dim; d++) {
695552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
696834065abSMatthew G. Knepley         pEnd    -= pStart;
697834065abSMatthew G. Knepley         pMax[d] -= pStart;
698552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
699834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
700552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
701834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
702cbb7f117SMark Adams           if (!rank) {
703834065abSMatthew G. Knepley             if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
704834065abSMatthew G. Knepley             else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
705834065abSMatthew G. Knepley           }
706cbb7f117SMark Adams         }
707552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
708552f7358SJed Brown       }
709552f7358SJed Brown     }
710834065abSMatthew G. Knepley     ierr = PetscFree2(sizes,hybsizes);CHKERRQ(ierr);
711c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
712a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
713a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
714a57dd577SMatthew G Knepley       DMLabel         label;
715a57dd577SMatthew G Knepley       const char     *name;
716a57dd577SMatthew G Knepley       IS              valueIS;
717a57dd577SMatthew G Knepley       const PetscInt *values;
718a57dd577SMatthew G Knepley       PetscInt        numValues, v;
719a57dd577SMatthew G Knepley 
720c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
721c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
722a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
723f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata of sizes (", name, numValues);CHKERRQ(ierr);
724a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
725a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
726120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
727a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
728a57dd577SMatthew G Knepley         PetscInt size;
729a57dd577SMatthew G Knepley 
730a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
731a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
732f347f43bSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer, "%D", size);CHKERRQ(ierr);
733a57dd577SMatthew G Knepley       }
734a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
735120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
736a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
7374d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
738a57dd577SMatthew G Knepley     }
739a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
7408e7ff633SMatthew G. Knepley     if (cdm) {
7418e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
7428e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
7438e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
7448e7ff633SMatthew G. Knepley     }
745552f7358SJed Brown   }
746552f7358SJed Brown   PetscFunctionReturn(0);
747552f7358SJed Brown }
748552f7358SJed Brown 
749552f7358SJed Brown #undef __FUNCT__
750e412dcbdSMatthew G. Knepley #define __FUNCT__ "DMPlexView_Draw"
751e412dcbdSMatthew G. Knepley PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
752e412dcbdSMatthew G. Knepley {
753e412dcbdSMatthew G. Knepley   PetscDraw          draw;
754e412dcbdSMatthew G. Knepley   DM                 cdm;
755e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
756e412dcbdSMatthew G. Knepley   Vec                coordinates;
757e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
758e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
759e412dcbdSMatthew G. Knepley   PetscBool          isnull;
760e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
761e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
762e412dcbdSMatthew G. Knepley 
763e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
764e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
765e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
766e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
767e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
768e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
769e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
770e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
771e412dcbdSMatthew G. Knepley 
772e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
773e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
774e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
775e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
776e412dcbdSMatthew G. Knepley 
777e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
778e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
779e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
7800c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
7810c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
782e412dcbdSMatthew G. Knepley   }
783e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
784e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
785e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
786e412dcbdSMatthew G. Knepley 
787e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
788e412dcbdSMatthew G. Knepley     PetscScalar *coords = NULL;
789e412dcbdSMatthew G. Knepley     PetscInt     numCoords;
790e412dcbdSMatthew G. Knepley 
791e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
792e412dcbdSMatthew G. Knepley     switch (numCoords) {
793e412dcbdSMatthew G. Knepley     case 6:
7940c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
7950c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
7960c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
797e412dcbdSMatthew G. Knepley       break;
798e412dcbdSMatthew G. Knepley     case 8:
7990c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8000c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8010c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8020c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
803e412dcbdSMatthew G. Knepley       break;
804e412dcbdSMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
805e412dcbdSMatthew G. Knepley     }
806e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
807e412dcbdSMatthew G. Knepley   }
808e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
809e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
810e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
811e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
812e412dcbdSMatthew G. Knepley }
813e412dcbdSMatthew G. Knepley 
814e412dcbdSMatthew G. Knepley #undef __FUNCT__
815552f7358SJed Brown #define __FUNCT__ "DMView_Plex"
816552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
817552f7358SJed Brown {
818e412dcbdSMatthew G. Knepley   PetscBool      iascii, ishdf5, isvtk, isdraw;
819552f7358SJed Brown   PetscErrorCode ierr;
820552f7358SJed Brown 
821552f7358SJed Brown   PetscFunctionBegin;
822552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
823552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
824552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
825fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
826c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
827e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
828552f7358SJed Brown   if (iascii) {
829552f7358SJed Brown     ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
830c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
831c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
8327afe7537SMatthew G. Knepley     ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_VIZ);CHKERRQ(ierr);
833c6ccd67eSMatthew G. Knepley     ierr = DMPlexView_HDF5(dm, viewer);CHKERRQ(ierr);
8347afe7537SMatthew G. Knepley     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
835c6ccd67eSMatthew G. Knepley #else
836c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
837552f7358SJed Brown #endif
838e412dcbdSMatthew G. Knepley   } else if (isvtk) {
839fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
840e412dcbdSMatthew G. Knepley   } else if (isdraw) {
841e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
842fcf6c8fdSToby Isaac   }
843552f7358SJed Brown   PetscFunctionReturn(0);
844552f7358SJed Brown }
845552f7358SJed Brown 
8462c40f234SMatthew G. Knepley #undef __FUNCT__
8472c40f234SMatthew G. Knepley #define __FUNCT__ "DMLoad_Plex"
8482c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
8492c40f234SMatthew G. Knepley {
8502c40f234SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
8512c40f234SMatthew G. Knepley   PetscErrorCode ierr;
8522c40f234SMatthew G. Knepley 
8532c40f234SMatthew G. Knepley   PetscFunctionBegin;
8542c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8552c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
8562c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERBINARY, &isbinary);CHKERRQ(ierr);
8572c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
8582c40f234SMatthew G. Knepley   if (isbinary) {SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Do not yet support binary viewers");}
8592c40f234SMatthew G. Knepley   else if (ishdf5) {
8602c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
86110b7db4eSMatthew G. Knepley     ierr = DMPlexLoad_HDF5(dm, viewer);CHKERRQ(ierr);
8622c40f234SMatthew G. Knepley #else
8632c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
864552f7358SJed Brown #endif
865552f7358SJed Brown   }
866552f7358SJed Brown   PetscFunctionReturn(0);
867552f7358SJed Brown }
868552f7358SJed Brown 
8696c73c22cSMatthew G. Knepley 
8706c73c22cSMatthew G. Knepley #undef __FUNCT__
871552f7358SJed Brown #define __FUNCT__ "DMDestroy_Plex"
872552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
873552f7358SJed Brown {
874552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
875552f7358SJed Brown   PetscErrorCode ierr;
876552f7358SJed Brown 
877552f7358SJed Brown   PetscFunctionBegin;
8780d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
879552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
880552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
881552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
882552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
883be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
884552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
885552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
886d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
887d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
88877623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
889a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
890552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
891552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
892a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
893a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
894d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
895d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
896d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
897d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
898d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
899d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
900fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
901552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
902552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
903713918a9SToby Isaac   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMAdaptLabel_C",NULL);CHKERRQ(ierr);
904552f7358SJed Brown   PetscFunctionReturn(0);
905552f7358SJed Brown }
906552f7358SJed Brown 
907552f7358SJed Brown #undef __FUNCT__
908552f7358SJed Brown #define __FUNCT__ "DMCreateMatrix_Plex"
909b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
910552f7358SJed Brown {
9118d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
912acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
913552f7358SJed Brown   PetscInt               localSize;
914837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
915552f7358SJed Brown   PetscErrorCode         ierr;
916b412c318SBarry Smith   MatType                mtype;
9171428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
918552f7358SJed Brown 
919552f7358SJed Brown   PetscFunctionBegin;
920607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
921b412c318SBarry Smith   mtype = dm->mattype;
922552f7358SJed Brown   ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
923552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
924552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
92582f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
926552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
927552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
928552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
929acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
930acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
931552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
932552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
933552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
934552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
935552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
936552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
937552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
938837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
939552f7358SJed Brown   if (!isShell) {
940be36d101SStefano Zampini     PetscSection subSection;
941837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
942be36d101SStefano Zampini     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal, bsMax, bsMin, *ltogidx, lsize;
943fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
944552f7358SJed Brown 
94500140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
946be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
947be36d101SStefano Zampini       PetscSection section;
948be36d101SStefano Zampini       PetscInt     size;
949be36d101SStefano Zampini 
950be36d101SStefano Zampini       ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
951be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
952be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
953be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
954be36d101SStefano Zampini     } else {
95500140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
956be36d101SStefano Zampini     }
957552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
958be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
959a9d99c84SMatthew G. Knepley       PetscInt bdof;
960a9d99c84SMatthew G. Knepley 
961552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
962fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
9631d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
9641d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
9651d17a0a3SMatthew G. Knepley       if (dof) {
9661d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
967be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
968be36d101SStefano Zampini       }
969be36d101SStefano Zampini       if (isMatIS) {
970be36d101SStefano Zampini         PetscInt loff,c,off;
971be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
972be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
973be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
974552f7358SJed Brown       }
9752a28c762SMatthew G Knepley     }
9762a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
9772a28c762SMatthew G Knepley     bsLocal = bs;
978b2566f29SBarry Smith     ierr = MPIU_Allreduce(&bsLocal, &bsMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
9792a28c762SMatthew G Knepley     bsLocal = bs < 0 ? bsMax : bs;
980b2566f29SBarry Smith     ierr = MPIU_Allreduce(&bsLocal, &bsMin, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
981bff27382SMatthew G. Knepley     if (bsMin != bsMax) {bs = 1;}
982bff27382SMatthew G. Knepley     else                {bs = bsMax;}
9834fa26246SMatthew G. Knepley     bs   = bs < 0 ? 1 : bs;
984be36d101SStefano Zampini     if (isMatIS) {
9854fa26246SMatthew G. Knepley       PetscInt l;
9864fa26246SMatthew G. Knepley       /* Must reduce indices by blocksize */
9874fa26246SMatthew G. Knepley       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] /= bs;
9884fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
989be36d101SStefano Zampini     }
990be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
991be36d101SStefano Zampini     if (isMatIS) {
992be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
993be36d101SStefano Zampini     }
9941795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
9958d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
996552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
997552f7358SJed Brown   }
998b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
999552f7358SJed Brown   PetscFunctionReturn(0);
1000552f7358SJed Brown }
1001552f7358SJed Brown 
1002552f7358SJed Brown #undef __FUNCT__
1003be36d101SStefano Zampini #define __FUNCT__ "DMPlexGetSubdomainSection"
1004559da874SStefano Zampini /*
1005be36d101SStefano Zampini   DMPlexGetSubdomainGlobalSection - Returns the section associated with the subdomain
1006be36d101SStefano Zampini 
1007be36d101SStefano Zampini   Not collective
1008be36d101SStefano Zampini 
1009be36d101SStefano Zampini   Input Parameter:
1010be36d101SStefano Zampini . mesh - The DMPlex
1011be36d101SStefano Zampini 
1012be36d101SStefano Zampini   Output Parameters:
1013be36d101SStefano Zampini . subsection - The subdomain section
1014be36d101SStefano Zampini 
1015be36d101SStefano Zampini   Level: developer
1016be36d101SStefano Zampini 
1017be36d101SStefano Zampini .seealso:
1018559da874SStefano Zampini */
1019be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1020be36d101SStefano Zampini {
1021be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1022be36d101SStefano Zampini   PetscErrorCode ierr;
1023be36d101SStefano Zampini 
1024be36d101SStefano Zampini   PetscFunctionBegin;
1025be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1026be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1027be36d101SStefano Zampini     PetscSection section;
1028be36d101SStefano Zampini     PetscSF      sf;
1029be36d101SStefano Zampini 
1030be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
1031be36d101SStefano Zampini     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
1032be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1033be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1034be36d101SStefano Zampini   }
1035be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1036be36d101SStefano Zampini   PetscFunctionReturn(0);
1037be36d101SStefano Zampini }
1038be36d101SStefano Zampini 
1039be36d101SStefano Zampini #undef __FUNCT__
1040552f7358SJed Brown #define __FUNCT__ "DMPlexGetChart"
1041552f7358SJed Brown /*@
1042552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1043552f7358SJed Brown 
1044552f7358SJed Brown   Not collective
1045552f7358SJed Brown 
1046552f7358SJed Brown   Input Parameter:
1047552f7358SJed Brown . mesh - The DMPlex
1048552f7358SJed Brown 
1049552f7358SJed Brown   Output Parameters:
1050552f7358SJed Brown + pStart - The first mesh point
1051552f7358SJed Brown - pEnd   - The upper bound for mesh points
1052552f7358SJed Brown 
1053552f7358SJed Brown   Level: beginner
1054552f7358SJed Brown 
1055552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1056552f7358SJed Brown @*/
1057552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1058552f7358SJed Brown {
1059552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1060552f7358SJed Brown   PetscErrorCode ierr;
1061552f7358SJed Brown 
1062552f7358SJed Brown   PetscFunctionBegin;
1063552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1064552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1065552f7358SJed Brown   PetscFunctionReturn(0);
1066552f7358SJed Brown }
1067552f7358SJed Brown 
1068552f7358SJed Brown #undef __FUNCT__
1069552f7358SJed Brown #define __FUNCT__ "DMPlexSetChart"
1070552f7358SJed Brown /*@
1071552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1072552f7358SJed Brown 
1073552f7358SJed Brown   Not collective
1074552f7358SJed Brown 
1075552f7358SJed Brown   Input Parameters:
1076552f7358SJed Brown + mesh - The DMPlex
1077552f7358SJed Brown . pStart - The first mesh point
1078552f7358SJed Brown - pEnd   - The upper bound for mesh points
1079552f7358SJed Brown 
1080552f7358SJed Brown   Output Parameters:
1081552f7358SJed Brown 
1082552f7358SJed Brown   Level: beginner
1083552f7358SJed Brown 
1084552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1085552f7358SJed Brown @*/
1086552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1087552f7358SJed Brown {
1088552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1089552f7358SJed Brown   PetscErrorCode ierr;
1090552f7358SJed Brown 
1091552f7358SJed Brown   PetscFunctionBegin;
1092552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1093552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1094552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1095552f7358SJed Brown   PetscFunctionReturn(0);
1096552f7358SJed Brown }
1097552f7358SJed Brown 
1098552f7358SJed Brown #undef __FUNCT__
1099552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeSize"
1100552f7358SJed Brown /*@
1101552f7358SJed Brown   DMPlexGetConeSize - Return the number of in-edges for this point in the Sieve DAG
1102552f7358SJed Brown 
1103552f7358SJed Brown   Not collective
1104552f7358SJed Brown 
1105552f7358SJed Brown   Input Parameters:
1106552f7358SJed Brown + mesh - The DMPlex
1107552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1108552f7358SJed Brown 
1109552f7358SJed Brown   Output Parameter:
1110552f7358SJed Brown . size - The cone size for point p
1111552f7358SJed Brown 
1112552f7358SJed Brown   Level: beginner
1113552f7358SJed Brown 
1114552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1115552f7358SJed Brown @*/
1116552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1117552f7358SJed Brown {
1118552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1119552f7358SJed Brown   PetscErrorCode ierr;
1120552f7358SJed Brown 
1121552f7358SJed Brown   PetscFunctionBegin;
1122552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1123552f7358SJed Brown   PetscValidPointer(size, 3);
1124552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1125552f7358SJed Brown   PetscFunctionReturn(0);
1126552f7358SJed Brown }
1127552f7358SJed Brown 
1128552f7358SJed Brown #undef __FUNCT__
1129552f7358SJed Brown #define __FUNCT__ "DMPlexSetConeSize"
1130552f7358SJed Brown /*@
1131552f7358SJed Brown   DMPlexSetConeSize - Set the number of in-edges for this point in the Sieve DAG
1132552f7358SJed Brown 
1133552f7358SJed Brown   Not collective
1134552f7358SJed Brown 
1135552f7358SJed Brown   Input Parameters:
1136552f7358SJed Brown + mesh - The DMPlex
1137552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1138552f7358SJed Brown - size - The cone size for point p
1139552f7358SJed Brown 
1140552f7358SJed Brown   Output Parameter:
1141552f7358SJed Brown 
1142552f7358SJed Brown   Note:
1143552f7358SJed Brown   This should be called after DMPlexSetChart().
1144552f7358SJed Brown 
1145552f7358SJed Brown   Level: beginner
1146552f7358SJed Brown 
1147552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1148552f7358SJed Brown @*/
1149552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1150552f7358SJed Brown {
1151552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1152552f7358SJed Brown   PetscErrorCode ierr;
1153552f7358SJed Brown 
1154552f7358SJed Brown   PetscFunctionBegin;
1155552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1156552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
11570d644c17SKarl Rupp 
1158552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1159552f7358SJed Brown   PetscFunctionReturn(0);
1160552f7358SJed Brown }
1161552f7358SJed Brown 
1162552f7358SJed Brown #undef __FUNCT__
1163f5a469b9SMatthew G. Knepley #define __FUNCT__ "DMPlexAddConeSize"
1164f5a469b9SMatthew G. Knepley /*@
1165f5a469b9SMatthew G. Knepley   DMPlexAddConeSize - Add the given number of in-edges to this point in the Sieve DAG
1166f5a469b9SMatthew G. Knepley 
1167f5a469b9SMatthew G. Knepley   Not collective
1168f5a469b9SMatthew G. Knepley 
1169f5a469b9SMatthew G. Knepley   Input Parameters:
1170f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1171f5a469b9SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1172f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1173f5a469b9SMatthew G. Knepley 
1174f5a469b9SMatthew G. Knepley   Output Parameter:
1175f5a469b9SMatthew G. Knepley 
1176f5a469b9SMatthew G. Knepley   Note:
1177f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1178f5a469b9SMatthew G. Knepley 
1179f5a469b9SMatthew G. Knepley   Level: beginner
1180f5a469b9SMatthew G. Knepley 
1181f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1182f5a469b9SMatthew G. Knepley @*/
1183f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1184f5a469b9SMatthew G. Knepley {
1185f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1186f5a469b9SMatthew G. Knepley   PetscInt       csize;
1187f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1188f5a469b9SMatthew G. Knepley 
1189f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1190f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1191f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1192f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1193f5a469b9SMatthew G. Knepley 
1194f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1195f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1196f5a469b9SMatthew G. Knepley }
1197f5a469b9SMatthew G. Knepley 
1198f5a469b9SMatthew G. Knepley #undef __FUNCT__
1199552f7358SJed Brown #define __FUNCT__ "DMPlexGetCone"
1200552f7358SJed Brown /*@C
1201552f7358SJed Brown   DMPlexGetCone - Return the points on the in-edges for this point in the Sieve DAG
1202552f7358SJed Brown 
1203552f7358SJed Brown   Not collective
1204552f7358SJed Brown 
1205552f7358SJed Brown   Input Parameters:
1206552f7358SJed Brown + mesh - The DMPlex
1207552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1208552f7358SJed Brown 
1209552f7358SJed Brown   Output Parameter:
1210552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1211552f7358SJed Brown 
1212552f7358SJed Brown   Level: beginner
1213552f7358SJed Brown 
12143813dfbdSMatthew G Knepley   Fortran Notes:
12153813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
12163813dfbdSMatthew G Knepley   include petsc.h90 in your code.
12173813dfbdSMatthew G Knepley 
12183813dfbdSMatthew G Knepley   You must also call DMPlexRestoreCone() after you finish using the returned array.
1219552f7358SJed Brown 
1220552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart()
1221552f7358SJed Brown @*/
1222552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1223552f7358SJed Brown {
1224552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1225552f7358SJed Brown   PetscInt       off;
1226552f7358SJed Brown   PetscErrorCode ierr;
1227552f7358SJed Brown 
1228552f7358SJed Brown   PetscFunctionBegin;
1229552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1230552f7358SJed Brown   PetscValidPointer(cone, 3);
1231552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1232552f7358SJed Brown   *cone = &mesh->cones[off];
1233552f7358SJed Brown   PetscFunctionReturn(0);
1234552f7358SJed Brown }
1235552f7358SJed Brown 
1236552f7358SJed Brown #undef __FUNCT__
1237552f7358SJed Brown #define __FUNCT__ "DMPlexSetCone"
1238552f7358SJed Brown /*@
1239552f7358SJed Brown   DMPlexSetCone - Set the points on the in-edges for this point in the Sieve DAG
1240552f7358SJed Brown 
1241552f7358SJed Brown   Not collective
1242552f7358SJed Brown 
1243552f7358SJed Brown   Input Parameters:
1244552f7358SJed Brown + mesh - The DMPlex
1245552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1246552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1247552f7358SJed Brown 
1248552f7358SJed Brown   Output Parameter:
1249552f7358SJed Brown 
1250552f7358SJed Brown   Note:
1251552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1252552f7358SJed Brown 
1253552f7358SJed Brown   Level: beginner
1254552f7358SJed Brown 
1255552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1256552f7358SJed Brown @*/
1257552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1258552f7358SJed Brown {
1259552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1260552f7358SJed Brown   PetscInt       pStart, pEnd;
1261552f7358SJed Brown   PetscInt       dof, off, c;
1262552f7358SJed Brown   PetscErrorCode ierr;
1263552f7358SJed Brown 
1264552f7358SJed Brown   PetscFunctionBegin;
1265552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1266552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1267552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1268552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1269552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
127082f516ccSBarry Smith   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
1271552f7358SJed Brown   for (c = 0; c < dof; ++c) {
127282f516ccSBarry Smith     if ((cone[c] < pStart) || (cone[c] >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone point %D is not in the valid range [%D, %D)", cone[c], pStart, pEnd);
1273552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1274552f7358SJed Brown   }
1275552f7358SJed Brown   PetscFunctionReturn(0);
1276552f7358SJed Brown }
1277552f7358SJed Brown 
1278552f7358SJed Brown #undef __FUNCT__
1279552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeOrientation"
1280552f7358SJed Brown /*@C
1281552f7358SJed Brown   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the Sieve DAG
1282552f7358SJed Brown 
1283552f7358SJed Brown   Not collective
1284552f7358SJed Brown 
1285552f7358SJed Brown   Input Parameters:
1286552f7358SJed Brown + mesh - The DMPlex
1287552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1288552f7358SJed Brown 
1289552f7358SJed Brown   Output Parameter:
1290552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1291552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1292552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1293552f7358SJed Brown                     the index of the cone point on which to start.
1294552f7358SJed Brown 
1295552f7358SJed Brown   Level: beginner
1296552f7358SJed Brown 
12973813dfbdSMatthew G Knepley   Fortran Notes:
12983813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
12993813dfbdSMatthew G Knepley   include petsc.h90 in your code.
13003813dfbdSMatthew G Knepley 
13013813dfbdSMatthew G Knepley   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1302552f7358SJed Brown 
1303552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1304552f7358SJed Brown @*/
1305552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1306552f7358SJed Brown {
1307552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1308552f7358SJed Brown   PetscInt       off;
1309552f7358SJed Brown   PetscErrorCode ierr;
1310552f7358SJed Brown 
1311552f7358SJed Brown   PetscFunctionBegin;
1312552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1313552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1314552f7358SJed Brown   {
1315552f7358SJed Brown     PetscInt dof;
1316552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1317552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1318552f7358SJed Brown   }
1319552f7358SJed Brown #endif
1320552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
13210d644c17SKarl Rupp 
1322552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1323552f7358SJed Brown   PetscFunctionReturn(0);
1324552f7358SJed Brown }
1325552f7358SJed Brown 
1326552f7358SJed Brown #undef __FUNCT__
1327552f7358SJed Brown #define __FUNCT__ "DMPlexSetConeOrientation"
1328552f7358SJed Brown /*@
1329552f7358SJed Brown   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the Sieve DAG
1330552f7358SJed Brown 
1331552f7358SJed Brown   Not collective
1332552f7358SJed Brown 
1333552f7358SJed Brown   Input Parameters:
1334552f7358SJed Brown + mesh - The DMPlex
1335552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1336552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1337552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1338552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1339552f7358SJed Brown                     the index of the cone point on which to start.
1340552f7358SJed Brown 
1341552f7358SJed Brown   Output Parameter:
1342552f7358SJed Brown 
1343552f7358SJed Brown   Note:
1344552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1345552f7358SJed Brown 
1346552f7358SJed Brown   Level: beginner
1347552f7358SJed Brown 
1348552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1349552f7358SJed Brown @*/
1350552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
1351552f7358SJed Brown {
1352552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1353552f7358SJed Brown   PetscInt       pStart, pEnd;
1354552f7358SJed Brown   PetscInt       dof, off, c;
1355552f7358SJed Brown   PetscErrorCode ierr;
1356552f7358SJed Brown 
1357552f7358SJed Brown   PetscFunctionBegin;
1358552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1359552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1360552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1361552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
1362552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
136382f516ccSBarry Smith   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
1364552f7358SJed Brown   for (c = 0; c < dof; ++c) {
1365552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
1366552f7358SJed Brown 
1367552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
136882f516ccSBarry Smith     if (o && ((o < -(cdof+1)) || (o >= cdof))) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone orientation %D is not in the valid range [%D. %D)", o, -(cdof+1), cdof);
1369552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
1370552f7358SJed Brown   }
1371552f7358SJed Brown   PetscFunctionReturn(0);
1372552f7358SJed Brown }
1373552f7358SJed Brown 
1374552f7358SJed Brown #undef __FUNCT__
1375552f7358SJed Brown #define __FUNCT__ "DMPlexInsertCone"
1376552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
1377552f7358SJed Brown {
1378552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1379552f7358SJed Brown   PetscInt       pStart, pEnd;
1380552f7358SJed Brown   PetscInt       dof, off;
1381552f7358SJed Brown   PetscErrorCode ierr;
1382552f7358SJed Brown 
1383552f7358SJed Brown   PetscFunctionBegin;
1384552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1385552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
138682f516ccSBarry Smith   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
138782f516ccSBarry Smith   if ((conePoint < pStart) || (conePoint >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone point %D is not in the valid range [%D, %D)", conePoint, pStart, pEnd);
138877c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
138977c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
139077c88f5bSMatthew G Knepley   if ((conePos < 0) || (conePos >= dof)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone position %D of point %D is not in the valid range [0, %D)", conePos, p, dof);
1391552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
1392552f7358SJed Brown   PetscFunctionReturn(0);
1393552f7358SJed Brown }
1394552f7358SJed Brown 
1395552f7358SJed Brown #undef __FUNCT__
139677c88f5bSMatthew G Knepley #define __FUNCT__ "DMPlexInsertConeOrientation"
139777c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
139877c88f5bSMatthew G Knepley {
139977c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
140077c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
140177c88f5bSMatthew G Knepley   PetscInt       dof, off;
140277c88f5bSMatthew G Knepley   PetscErrorCode ierr;
140377c88f5bSMatthew G Knepley 
140477c88f5bSMatthew G Knepley   PetscFunctionBegin;
140577c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
140677c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
140777c88f5bSMatthew G Knepley   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
140877c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
140977c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
141077c88f5bSMatthew G Knepley   if ((conePos < 0) || (conePos >= dof)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone position %D of point %D is not in the valid range [0, %D)", conePos, p, dof);
141177c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
141277c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
141377c88f5bSMatthew G Knepley }
141477c88f5bSMatthew G Knepley 
141577c88f5bSMatthew G Knepley #undef __FUNCT__
1416552f7358SJed Brown #define __FUNCT__ "DMPlexGetSupportSize"
1417552f7358SJed Brown /*@
1418552f7358SJed Brown   DMPlexGetSupportSize - Return the number of out-edges for this point in the Sieve DAG
1419552f7358SJed Brown 
1420552f7358SJed Brown   Not collective
1421552f7358SJed Brown 
1422552f7358SJed Brown   Input Parameters:
1423552f7358SJed Brown + mesh - The DMPlex
1424552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1425552f7358SJed Brown 
1426552f7358SJed Brown   Output Parameter:
1427552f7358SJed Brown . size - The support size for point p
1428552f7358SJed Brown 
1429552f7358SJed Brown   Level: beginner
1430552f7358SJed Brown 
1431552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
1432552f7358SJed Brown @*/
1433552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
1434552f7358SJed Brown {
1435552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1436552f7358SJed Brown   PetscErrorCode ierr;
1437552f7358SJed Brown 
1438552f7358SJed Brown   PetscFunctionBegin;
1439552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1440552f7358SJed Brown   PetscValidPointer(size, 3);
1441552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
1442552f7358SJed Brown   PetscFunctionReturn(0);
1443552f7358SJed Brown }
1444552f7358SJed Brown 
1445552f7358SJed Brown #undef __FUNCT__
1446552f7358SJed Brown #define __FUNCT__ "DMPlexSetSupportSize"
1447552f7358SJed Brown /*@
1448552f7358SJed Brown   DMPlexSetSupportSize - Set the number of out-edges for this point in the Sieve DAG
1449552f7358SJed Brown 
1450552f7358SJed Brown   Not collective
1451552f7358SJed Brown 
1452552f7358SJed Brown   Input Parameters:
1453552f7358SJed Brown + mesh - The DMPlex
1454552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1455552f7358SJed Brown - size - The support size for point p
1456552f7358SJed Brown 
1457552f7358SJed Brown   Output Parameter:
1458552f7358SJed Brown 
1459552f7358SJed Brown   Note:
1460552f7358SJed Brown   This should be called after DMPlexSetChart().
1461552f7358SJed Brown 
1462552f7358SJed Brown   Level: beginner
1463552f7358SJed Brown 
1464552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
1465552f7358SJed Brown @*/
1466552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
1467552f7358SJed Brown {
1468552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1469552f7358SJed Brown   PetscErrorCode ierr;
1470552f7358SJed Brown 
1471552f7358SJed Brown   PetscFunctionBegin;
1472552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1473552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
14740d644c17SKarl Rupp 
1475552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
1476552f7358SJed Brown   PetscFunctionReturn(0);
1477552f7358SJed Brown }
1478552f7358SJed Brown 
1479552f7358SJed Brown #undef __FUNCT__
1480552f7358SJed Brown #define __FUNCT__ "DMPlexGetSupport"
1481552f7358SJed Brown /*@C
1482552f7358SJed Brown   DMPlexGetSupport - Return the points on the out-edges for this point in the Sieve DAG
1483552f7358SJed Brown 
1484552f7358SJed Brown   Not collective
1485552f7358SJed Brown 
1486552f7358SJed Brown   Input Parameters:
1487552f7358SJed Brown + mesh - The DMPlex
1488552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1489552f7358SJed Brown 
1490552f7358SJed Brown   Output Parameter:
1491552f7358SJed Brown . support - An array of points which are on the out-edges for point p
1492552f7358SJed Brown 
1493552f7358SJed Brown   Level: beginner
1494552f7358SJed Brown 
14953813dfbdSMatthew G Knepley   Fortran Notes:
14963813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
14973813dfbdSMatthew G Knepley   include petsc.h90 in your code.
14983813dfbdSMatthew G Knepley 
14993813dfbdSMatthew G Knepley   You must also call DMPlexRestoreSupport() after you finish using the returned array.
15003813dfbdSMatthew G Knepley 
1501552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1502552f7358SJed Brown @*/
1503552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
1504552f7358SJed Brown {
1505552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1506552f7358SJed Brown   PetscInt       off;
1507552f7358SJed Brown   PetscErrorCode ierr;
1508552f7358SJed Brown 
1509552f7358SJed Brown   PetscFunctionBegin;
1510552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1511552f7358SJed Brown   PetscValidPointer(support, 3);
1512552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
1513552f7358SJed Brown   *support = &mesh->supports[off];
1514552f7358SJed Brown   PetscFunctionReturn(0);
1515552f7358SJed Brown }
1516552f7358SJed Brown 
1517552f7358SJed Brown #undef __FUNCT__
1518552f7358SJed Brown #define __FUNCT__ "DMPlexSetSupport"
1519552f7358SJed Brown /*@
1520552f7358SJed Brown   DMPlexSetSupport - Set the points on the out-edges for this point in the Sieve DAG
1521552f7358SJed Brown 
1522552f7358SJed Brown   Not collective
1523552f7358SJed Brown 
1524552f7358SJed Brown   Input Parameters:
1525552f7358SJed Brown + mesh - The DMPlex
1526552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1527552f7358SJed Brown - support - An array of points which are on the in-edges for point p
1528552f7358SJed Brown 
1529552f7358SJed Brown   Output Parameter:
1530552f7358SJed Brown 
1531552f7358SJed Brown   Note:
1532552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
1533552f7358SJed Brown 
1534552f7358SJed Brown   Level: beginner
1535552f7358SJed Brown 
1536552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
1537552f7358SJed Brown @*/
1538552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
1539552f7358SJed Brown {
1540552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1541552f7358SJed Brown   PetscInt       pStart, pEnd;
1542552f7358SJed Brown   PetscInt       dof, off, c;
1543552f7358SJed Brown   PetscErrorCode ierr;
1544552f7358SJed Brown 
1545552f7358SJed Brown   PetscFunctionBegin;
1546552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1547552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1548552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1549552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
1550552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
155182f516ccSBarry Smith   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
1552552f7358SJed Brown   for (c = 0; c < dof; ++c) {
155382f516ccSBarry Smith     if ((support[c] < pStart) || (support[c] >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support point %D is not in the valid range [%D, %D)", support[c], pStart, pEnd);
1554552f7358SJed Brown     mesh->supports[off+c] = support[c];
1555552f7358SJed Brown   }
1556552f7358SJed Brown   PetscFunctionReturn(0);
1557552f7358SJed Brown }
1558552f7358SJed Brown 
1559552f7358SJed Brown #undef __FUNCT__
1560552f7358SJed Brown #define __FUNCT__ "DMPlexInsertSupport"
1561552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
1562552f7358SJed Brown {
1563552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1564552f7358SJed Brown   PetscInt       pStart, pEnd;
1565552f7358SJed Brown   PetscInt       dof, off;
1566552f7358SJed Brown   PetscErrorCode ierr;
1567552f7358SJed Brown 
1568552f7358SJed Brown   PetscFunctionBegin;
1569552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1570552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1571552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1572552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
157382f516ccSBarry Smith   if ((p < pStart) || (p >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Mesh point %D is not in the valid range [%D, %D)", p, pStart, pEnd);
157482f516ccSBarry Smith   if ((supportPoint < pStart) || (supportPoint >= pEnd)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support point %D is not in the valid range [%D, %D)", supportPoint, pStart, pEnd);
157582f516ccSBarry Smith   if (supportPos >= dof) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Support position %D of point %D is not in the valid range [0, %D)", supportPos, p, dof);
1576552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
1577552f7358SJed Brown   PetscFunctionReturn(0);
1578552f7358SJed Brown }
1579552f7358SJed Brown 
1580552f7358SJed Brown #undef __FUNCT__
1581552f7358SJed Brown #define __FUNCT__ "DMPlexGetTransitiveClosure"
1582552f7358SJed Brown /*@C
1583552f7358SJed Brown   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the Sieve DAG
1584552f7358SJed Brown 
1585552f7358SJed Brown   Not collective
1586552f7358SJed Brown 
1587552f7358SJed Brown   Input Parameters:
1588552f7358SJed Brown + mesh - The DMPlex
1589552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1590552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
15910298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
1592552f7358SJed Brown 
1593552f7358SJed Brown   Output Parameters:
1594552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
1595552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
1596552f7358SJed Brown 
1597552f7358SJed Brown   Note:
15980298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
1599552f7358SJed Brown 
16003813dfbdSMatthew G Knepley   Fortran Notes:
16013813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16023813dfbdSMatthew G Knepley   include petsc.h90 in your code.
16033813dfbdSMatthew G Knepley 
16043813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
16053813dfbdSMatthew G Knepley 
1606552f7358SJed Brown   Level: beginner
1607552f7358SJed Brown 
1608552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1609552f7358SJed Brown @*/
1610552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1611552f7358SJed Brown {
1612552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
1613552f7358SJed Brown   PetscInt       *closure, *fifo;
16140298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
1615552f7358SJed Brown   PetscInt        tmpSize, t;
1616552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
1617552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
1618552f7358SJed Brown   PetscErrorCode  ierr;
1619552f7358SJed Brown 
1620552f7358SJed Brown   PetscFunctionBegin;
1621552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1622552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1623552f7358SJed Brown   /* This is only 1-level */
1624552f7358SJed Brown   if (useCone) {
1625552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
1626552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
1627552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
1628552f7358SJed Brown   } else {
1629552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
1630552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
1631552f7358SJed Brown   }
1632bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
1633bfbcdd7aSMatthew G. Knepley     if (*points) {
1634bfbcdd7aSMatthew G. Knepley       closure = *points;
1635bfbcdd7aSMatthew G. Knepley     } else {
1636bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
1637bfbcdd7aSMatthew G. Knepley       ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1638bfbcdd7aSMatthew G. Knepley     }
1639bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
1640bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
1641bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
1642bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
1643bfbcdd7aSMatthew G. Knepley     }
1644bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
1645bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
1646bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
1647bfbcdd7aSMatthew G. Knepley   }
164824c766afSToby Isaac   {
164924c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
165024c766afSToby Isaac 
165124c766afSToby Isaac     c = mesh->maxConeSize;
165224c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
165324c766afSToby Isaac     s = mesh->maxSupportSize;
165424c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
165524c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
165624c766afSToby Isaac   }
1657bfbcdd7aSMatthew G. Knepley   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1658bfbcdd7aSMatthew G. Knepley   if (*points) {
1659bfbcdd7aSMatthew G. Knepley     closure = *points;
1660bfbcdd7aSMatthew G. Knepley   } else {
1661bfbcdd7aSMatthew G. Knepley     ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1662bfbcdd7aSMatthew G. Knepley   }
1663bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
1664552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
1665552f7358SJed Brown     const PetscInt cp = tmp[t];
1666552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
1667552f7358SJed Brown 
1668552f7358SJed Brown     closure[closureSize]   = cp;
1669552f7358SJed Brown     closure[closureSize+1] = co;
1670552f7358SJed Brown     fifo[fifoSize]         = cp;
1671552f7358SJed Brown     fifo[fifoSize+1]       = co;
1672552f7358SJed Brown   }
1673bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
1674552f7358SJed Brown   while (fifoSize - fifoStart) {
1675552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
1676552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
1677552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
1678552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
1679552f7358SJed Brown 
1680552f7358SJed Brown     if (useCone) {
1681552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
1682552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
1683552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
1684552f7358SJed Brown     } else {
1685552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
1686552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
16870298fd71SBarry Smith       tmpO = NULL;
1688552f7358SJed Brown     }
1689552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
1690552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
1691552f7358SJed Brown       const PetscInt cp = tmp[i];
16922e1b13c2SMatthew G. Knepley       /* Must propogate orientation: When we reverse orientation, we both reverse the direction of iteration and start at the other end of the chain. */
16932e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
16942e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
16952e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
1696552f7358SJed Brown       PetscInt       c;
1697552f7358SJed Brown 
16982e1b13c2SMatthew G. Knepley       if (rev) {
16992e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
17002e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
17012e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
17022e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
17032e1b13c2SMatthew G. Knepley       }
1704552f7358SJed Brown       /* Check for duplicate */
1705552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
1706552f7358SJed Brown         if (closure[c] == cp) break;
1707552f7358SJed Brown       }
1708552f7358SJed Brown       if (c == closureSize) {
1709552f7358SJed Brown         closure[closureSize]   = cp;
1710552f7358SJed Brown         closure[closureSize+1] = co;
1711552f7358SJed Brown         fifo[fifoSize]         = cp;
1712552f7358SJed Brown         fifo[fifoSize+1]       = co;
1713552f7358SJed Brown         closureSize           += 2;
1714552f7358SJed Brown         fifoSize              += 2;
1715552f7358SJed Brown       }
1716552f7358SJed Brown     }
1717552f7358SJed Brown     fifoStart += 2;
1718552f7358SJed Brown   }
1719552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
1720552f7358SJed Brown   if (points)    *points    = closure;
1721552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1722552f7358SJed Brown   PetscFunctionReturn(0);
1723552f7358SJed Brown }
1724552f7358SJed Brown 
1725552f7358SJed Brown #undef __FUNCT__
17269bf0dad6SMatthew G. Knepley #define __FUNCT__ "DMPlexGetTransitiveClosure_Internal"
17279bf0dad6SMatthew G. Knepley /*@C
17289bf0dad6SMatthew G. Knepley   DMPlexGetTransitiveClosure_Internal - Return the points on the transitive closure of the in-edges or out-edges for this point in the Sieve DAG with a specified initial orientation
17299bf0dad6SMatthew G. Knepley 
17309bf0dad6SMatthew G. Knepley   Not collective
17319bf0dad6SMatthew G. Knepley 
17329bf0dad6SMatthew G. Knepley   Input Parameters:
17339bf0dad6SMatthew G. Knepley + mesh - The DMPlex
17349bf0dad6SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
17359bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
17369bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
17379bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
17389bf0dad6SMatthew G. Knepley 
17399bf0dad6SMatthew G. Knepley   Output Parameters:
17409bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
17419bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
17429bf0dad6SMatthew G. Knepley 
17439bf0dad6SMatthew G. Knepley   Note:
17449bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
17459bf0dad6SMatthew G. Knepley 
17469bf0dad6SMatthew G. Knepley   Fortran Notes:
17479bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
17489bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
17499bf0dad6SMatthew G. Knepley 
17509bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
17519bf0dad6SMatthew G. Knepley 
17529bf0dad6SMatthew G. Knepley   Level: beginner
17539bf0dad6SMatthew G. Knepley 
17549bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
17559bf0dad6SMatthew G. Knepley @*/
17569bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
17579bf0dad6SMatthew G. Knepley {
17589bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
17599bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
17609bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
17619bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
17629bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
17639bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
17649bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
17659bf0dad6SMatthew G. Knepley 
17669bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
17679bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17689bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
17699bf0dad6SMatthew G. Knepley   /* This is only 1-level */
17709bf0dad6SMatthew G. Knepley   if (useCone) {
17719bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
17729bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
17739bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
17749bf0dad6SMatthew G. Knepley   } else {
17759bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
17769bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
17779bf0dad6SMatthew G. Knepley   }
17789bf0dad6SMatthew G. Knepley   if (depth == 1) {
17799bf0dad6SMatthew G. Knepley     if (*points) {
17809bf0dad6SMatthew G. Knepley       closure = *points;
17819bf0dad6SMatthew G. Knepley     } else {
17829bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
17839bf0dad6SMatthew G. Knepley       ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
17849bf0dad6SMatthew G. Knepley     }
17859bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
17869bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
17879bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
17889bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
17899bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
17909bf0dad6SMatthew G. Knepley     }
17919bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
17929bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
17939bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
17949bf0dad6SMatthew G. Knepley   }
179524c766afSToby Isaac   {
179624c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
179724c766afSToby Isaac 
179824c766afSToby Isaac     c = mesh->maxConeSize;
179924c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
180024c766afSToby Isaac     s = mesh->maxSupportSize;
180124c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
180224c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
180324c766afSToby Isaac   }
18049bf0dad6SMatthew G. Knepley   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
18059bf0dad6SMatthew G. Knepley   if (*points) {
18069bf0dad6SMatthew G. Knepley     closure = *points;
18079bf0dad6SMatthew G. Knepley   } else {
18089bf0dad6SMatthew G. Knepley     ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
18099bf0dad6SMatthew G. Knepley   }
18109bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
18119bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
18129bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
18139bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
18149bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
18159bf0dad6SMatthew G. Knepley 
18169bf0dad6SMatthew G. Knepley     if (ornt < 0) {
18179bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
18189bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
181986b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
18209bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
18219bf0dad6SMatthew G. Knepley     }
18229bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
18239bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
18249bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
18259bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
18269bf0dad6SMatthew G. Knepley   }
18279bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
18289bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
18299bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
18309bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
18319bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
18329bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
18339bf0dad6SMatthew G. Knepley 
18349bf0dad6SMatthew G. Knepley     if (useCone) {
18359bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
18369bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
18379bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
18389bf0dad6SMatthew G. Knepley     } else {
18399bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
18409bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
18419bf0dad6SMatthew G. Knepley       tmpO = NULL;
18429bf0dad6SMatthew G. Knepley     }
18439bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
18449bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
18459bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
18469bf0dad6SMatthew G. Knepley       /* Must propogate orientation: When we reverse orientation, we both reverse the direction of iteration and start at the other end of the chain. */
18479bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
18489bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
18499bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
18509bf0dad6SMatthew G. Knepley       PetscInt       c;
18519bf0dad6SMatthew G. Knepley 
18529bf0dad6SMatthew G. Knepley       if (rev) {
18539bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
18549bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
18559bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
18569bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
18579bf0dad6SMatthew G. Knepley       }
18589bf0dad6SMatthew G. Knepley       /* Check for duplicate */
18599bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
18609bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
18619bf0dad6SMatthew G. Knepley       }
18629bf0dad6SMatthew G. Knepley       if (c == closureSize) {
18639bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
18649bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
18659bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
18669bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
18679bf0dad6SMatthew G. Knepley         closureSize           += 2;
18689bf0dad6SMatthew G. Knepley         fifoSize              += 2;
18699bf0dad6SMatthew G. Knepley       }
18709bf0dad6SMatthew G. Knepley     }
18719bf0dad6SMatthew G. Knepley     fifoStart += 2;
18729bf0dad6SMatthew G. Knepley   }
18739bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
18749bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
18759bf0dad6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
18769bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
18779bf0dad6SMatthew G. Knepley }
18789bf0dad6SMatthew G. Knepley 
18799bf0dad6SMatthew G. Knepley #undef __FUNCT__
1880552f7358SJed Brown #define __FUNCT__ "DMPlexRestoreTransitiveClosure"
1881552f7358SJed Brown /*@C
1882552f7358SJed Brown   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the Sieve DAG
1883552f7358SJed Brown 
1884552f7358SJed Brown   Not collective
1885552f7358SJed Brown 
1886552f7358SJed Brown   Input Parameters:
1887552f7358SJed Brown + mesh - The DMPlex
1888552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1889552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
1890e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
1891e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
1892552f7358SJed Brown 
1893552f7358SJed Brown   Note:
18940298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
1895552f7358SJed Brown 
18963813dfbdSMatthew G Knepley   Fortran Notes:
18973813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
18983813dfbdSMatthew G Knepley   include petsc.h90 in your code.
18993813dfbdSMatthew G Knepley 
19003813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
19013813dfbdSMatthew G Knepley 
1902552f7358SJed Brown   Level: beginner
1903552f7358SJed Brown 
1904552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1905552f7358SJed Brown @*/
1906552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1907552f7358SJed Brown {
1908552f7358SJed Brown   PetscErrorCode ierr;
1909552f7358SJed Brown 
1910552f7358SJed Brown   PetscFunctionBegin;
1911552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1912e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
1913e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
1914552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, points);CHKERRQ(ierr);
19154ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
1916552f7358SJed Brown   PetscFunctionReturn(0);
1917552f7358SJed Brown }
1918552f7358SJed Brown 
1919552f7358SJed Brown #undef __FUNCT__
1920552f7358SJed Brown #define __FUNCT__ "DMPlexGetMaxSizes"
1921552f7358SJed Brown /*@
1922552f7358SJed Brown   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the Sieve DAG
1923552f7358SJed Brown 
1924552f7358SJed Brown   Not collective
1925552f7358SJed Brown 
1926552f7358SJed Brown   Input Parameter:
1927552f7358SJed Brown . mesh - The DMPlex
1928552f7358SJed Brown 
1929552f7358SJed Brown   Output Parameters:
1930552f7358SJed Brown + maxConeSize - The maximum number of in-edges
1931552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
1932552f7358SJed Brown 
1933552f7358SJed Brown   Level: beginner
1934552f7358SJed Brown 
1935552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1936552f7358SJed Brown @*/
1937552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
1938552f7358SJed Brown {
1939552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
1940552f7358SJed Brown 
1941552f7358SJed Brown   PetscFunctionBegin;
1942552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1943552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
1944552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
1945552f7358SJed Brown   PetscFunctionReturn(0);
1946552f7358SJed Brown }
1947552f7358SJed Brown 
1948552f7358SJed Brown #undef __FUNCT__
1949552f7358SJed Brown #define __FUNCT__ "DMSetUp_Plex"
1950552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
1951552f7358SJed Brown {
1952552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1953552f7358SJed Brown   PetscInt       size;
1954552f7358SJed Brown   PetscErrorCode ierr;
1955552f7358SJed Brown 
1956552f7358SJed Brown   PetscFunctionBegin;
1957552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1958552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
1959552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
19601795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
19611795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
1962552f7358SJed Brown   if (mesh->maxSupportSize) {
1963552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
1964552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
19651795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
1966552f7358SJed Brown   }
1967552f7358SJed Brown   PetscFunctionReturn(0);
1968552f7358SJed Brown }
1969552f7358SJed Brown 
1970552f7358SJed Brown #undef __FUNCT__
1971552f7358SJed Brown #define __FUNCT__ "DMCreateSubDM_Plex"
1972552f7358SJed Brown PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1973552f7358SJed Brown {
1974552f7358SJed Brown   PetscErrorCode ierr;
1975552f7358SJed Brown 
1976552f7358SJed Brown   PetscFunctionBegin;
19774d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
19784d9407bcSMatthew G. Knepley   ierr = DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1979552f7358SJed Brown   PetscFunctionReturn(0);
1980552f7358SJed Brown }
1981552f7358SJed Brown 
1982552f7358SJed Brown #undef __FUNCT__
1983552f7358SJed Brown #define __FUNCT__ "DMPlexSymmetrize"
1984552f7358SJed Brown /*@
1985552f7358SJed Brown   DMPlexSymmetrize - Creates support (out-edge) information from cone (in-edge) inoformation
1986552f7358SJed Brown 
1987552f7358SJed Brown   Not collective
1988552f7358SJed Brown 
1989552f7358SJed Brown   Input Parameter:
1990552f7358SJed Brown . mesh - The DMPlex
1991552f7358SJed Brown 
1992552f7358SJed Brown   Output Parameter:
1993552f7358SJed Brown 
1994552f7358SJed Brown   Note:
1995552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
1996552f7358SJed Brown 
1997552f7358SJed Brown   Level: beginner
1998552f7358SJed Brown 
1999552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2000552f7358SJed Brown @*/
2001552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2002552f7358SJed Brown {
2003552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2004552f7358SJed Brown   PetscInt      *offsets;
2005552f7358SJed Brown   PetscInt       supportSize;
2006552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2007552f7358SJed Brown   PetscErrorCode ierr;
2008552f7358SJed Brown 
2009552f7358SJed Brown   PetscFunctionBegin;
2010552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
201182f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
2012552f7358SJed Brown   /* Calculate support sizes */
2013552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2014552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2015552f7358SJed Brown     PetscInt dof, off, c;
2016552f7358SJed Brown 
2017552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2018552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2019552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2020552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2021552f7358SJed Brown     }
2022552f7358SJed Brown   }
2023552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2024552f7358SJed Brown     PetscInt dof;
2025552f7358SJed Brown 
2026552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
20270d644c17SKarl Rupp 
2028552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2029552f7358SJed Brown   }
2030552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2031552f7358SJed Brown   /* Calculate supports */
2032552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
20331795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
20341795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2035552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2036552f7358SJed Brown     PetscInt dof, off, c;
2037552f7358SJed Brown 
2038552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2039552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2040552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2041552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2042552f7358SJed Brown       PetscInt       offS;
2043552f7358SJed Brown 
2044552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
20450d644c17SKarl Rupp 
2046552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2047552f7358SJed Brown       ++offsets[q];
2048552f7358SJed Brown     }
2049552f7358SJed Brown   }
2050552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
2051552f7358SJed Brown   PetscFunctionReturn(0);
2052552f7358SJed Brown }
2053552f7358SJed Brown 
2054552f7358SJed Brown #undef __FUNCT__
2055552f7358SJed Brown #define __FUNCT__ "DMPlexStratify"
2056552f7358SJed Brown /*@
2057552f7358SJed Brown   DMPlexStratify - The Sieve DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
2058552f7358SJed Brown   can be illustrated by Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2059552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2060552f7358SJed Brown   the DAG.
2061552f7358SJed Brown 
2062bf4602e4SToby Isaac   Collective on dm
2063552f7358SJed Brown 
2064552f7358SJed Brown   Input Parameter:
2065552f7358SJed Brown . mesh - The DMPlex
2066552f7358SJed Brown 
2067552f7358SJed Brown   Output Parameter:
2068552f7358SJed Brown 
2069552f7358SJed Brown   Notes:
2070150b719bSJed Brown   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
2071150b719bSJed Brown   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2072c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2073150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2074552f7358SJed Brown 
2075150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2076552f7358SJed Brown 
2077552f7358SJed Brown   Level: beginner
2078552f7358SJed Brown 
2079552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
2080552f7358SJed Brown @*/
2081552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2082552f7358SJed Brown {
2083df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2084aa50250dSMatthew G. Knepley   DMLabel        label;
2085552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2086552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
2087552f7358SJed Brown   PetscErrorCode ierr;
2088552f7358SJed Brown 
2089552f7358SJed Brown   PetscFunctionBegin;
2090552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2091552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2092552f7358SJed Brown   /* Calculate depth */
2093aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2094c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2095aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2096552f7358SJed Brown   /* Initialize roots and count leaves */
2097552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2098552f7358SJed Brown     PetscInt coneSize, supportSize;
2099552f7358SJed Brown 
2100552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2101552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2102552f7358SJed Brown     if (!coneSize && supportSize) {
2103552f7358SJed Brown       ++numRoots;
2104aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2105552f7358SJed Brown     } else if (!supportSize && coneSize) {
2106552f7358SJed Brown       ++numLeaves;
2107552f7358SJed Brown     } else if (!supportSize && !coneSize) {
2108552f7358SJed Brown       /* Isolated points */
2109aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2110552f7358SJed Brown     }
2111552f7358SJed Brown   }
2112552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2113552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
2114552f7358SJed Brown       PetscInt coneSize, supportSize;
2115552f7358SJed Brown 
2116552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2117552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2118552f7358SJed Brown       if (!supportSize && coneSize) {
2119aa50250dSMatthew G. Knepley         ierr = DMLabelSetValue(label, p, 1);CHKERRQ(ierr);
2120552f7358SJed Brown       }
2121552f7358SJed Brown     }
2122552f7358SJed Brown   } else {
212374ef644bSMatthew G. Knepley     IS       pointIS;
212474ef644bSMatthew G. Knepley     PetscInt numPoints = 0, level = 0;
2125552f7358SJed Brown 
212674ef644bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
212774ef644bSMatthew G. Knepley     if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
212874ef644bSMatthew G. Knepley     while (numPoints) {
212974ef644bSMatthew G. Knepley       const PetscInt *points;
213074ef644bSMatthew G. Knepley       const PetscInt  newLevel = level+1;
213174ef644bSMatthew G. Knepley 
213274ef644bSMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
213374ef644bSMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
213474ef644bSMatthew G. Knepley         const PetscInt  point = points[p];
213574ef644bSMatthew G. Knepley         const PetscInt *support;
213674ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
213774ef644bSMatthew G. Knepley 
213874ef644bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
213974ef644bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
214074ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
214174ef644bSMatthew G. Knepley           ierr = DMLabelSetValue(label, support[s], newLevel);CHKERRQ(ierr);
2142552f7358SJed Brown         }
2143552f7358SJed Brown       }
21445edfc765SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
214574ef644bSMatthew G. Knepley       ++level;
214674ef644bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
214774ef644bSMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
214874ef644bSMatthew G. Knepley       if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
214974ef644bSMatthew G. Knepley       else         {numPoints = 0;}
215074ef644bSMatthew G. Knepley     }
215174ef644bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
215274ef644bSMatthew G. Knepley   }
2153bf4602e4SToby Isaac   { /* just in case there is an empty process */
2154bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2155bf4602e4SToby Isaac 
2156bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label,&numValues);CHKERRQ(ierr);
21574de306b1SToby Isaac     for (v = 0; v < numValues; v++) {
21584de306b1SToby Isaac       IS pointIS;
21594de306b1SToby Isaac 
21604de306b1SToby Isaac       ierr = DMLabelGetStratumIS(label, v, &pointIS);CHKERRQ(ierr);
21614de306b1SToby Isaac       if (pointIS) {
21624de306b1SToby Isaac         PetscInt  min, max, numPoints;
21634de306b1SToby Isaac         PetscInt  start;
21644de306b1SToby Isaac         PetscBool contig;
21654de306b1SToby Isaac 
21664de306b1SToby Isaac         ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
21674de306b1SToby Isaac         ierr = ISGetMinMax(pointIS, &min, &max);CHKERRQ(ierr);
21684de306b1SToby Isaac         ierr = ISContiguousLocal(pointIS,min,max+1,&start,&contig);CHKERRQ(ierr);
21694de306b1SToby Isaac         if (start == 0 && contig) {
21704de306b1SToby Isaac           ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
21714de306b1SToby Isaac           ierr = ISCreateStride(PETSC_COMM_SELF,numPoints,min,1,&pointIS);CHKERRQ(ierr);
21724de306b1SToby Isaac           ierr = DMLabelSetStratumIS(label, v, pointIS);CHKERRQ(ierr);
21734de306b1SToby Isaac         }
21744de306b1SToby Isaac       }
21754de306b1SToby Isaac       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
21764de306b1SToby Isaac     }
21776d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2178bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2179bf4602e4SToby Isaac       DMLabelAddStratum(label,v);CHKERRQ(ierr);
2180bf4602e4SToby Isaac     }
2181bf4602e4SToby Isaac   }
2182bf4602e4SToby Isaac 
2183df0420ecSMatthew G. Knepley   ierr = DMLabelGetState(label, &mesh->depthState);CHKERRQ(ierr);
2184552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2185552f7358SJed Brown   PetscFunctionReturn(0);
2186552f7358SJed Brown }
2187552f7358SJed Brown 
2188552f7358SJed Brown #undef __FUNCT__
2189552f7358SJed Brown #define __FUNCT__ "DMPlexGetJoin"
2190552f7358SJed Brown /*@C
2191552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
2192552f7358SJed Brown 
2193552f7358SJed Brown   Not Collective
2194552f7358SJed Brown 
2195552f7358SJed Brown   Input Parameters:
2196552f7358SJed Brown + dm - The DMPlex object
2197552f7358SJed Brown . numPoints - The number of input points for the join
2198552f7358SJed Brown - points - The input points
2199552f7358SJed Brown 
2200552f7358SJed Brown   Output Parameters:
2201552f7358SJed Brown + numCoveredPoints - The number of points in the join
2202552f7358SJed Brown - coveredPoints - The points in the join
2203552f7358SJed Brown 
2204552f7358SJed Brown   Level: intermediate
2205552f7358SJed Brown 
2206552f7358SJed Brown   Note: Currently, this is restricted to a single level join
2207552f7358SJed Brown 
22083813dfbdSMatthew G Knepley   Fortran Notes:
22093813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22103813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22113813dfbdSMatthew G Knepley 
22123813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
22133813dfbdSMatthew G Knepley 
2214552f7358SJed Brown .keywords: mesh
2215552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2216552f7358SJed Brown @*/
2217552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2218552f7358SJed Brown {
2219552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2220552f7358SJed Brown   PetscInt      *join[2];
2221552f7358SJed Brown   PetscInt       joinSize, i = 0;
2222552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2223552f7358SJed Brown   PetscErrorCode ierr;
2224552f7358SJed Brown 
2225552f7358SJed Brown   PetscFunctionBegin;
2226552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2227552f7358SJed Brown   PetscValidPointer(points, 2);
2228552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2229552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2230552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
2231552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
2232552f7358SJed Brown   /* Copy in support of first point */
2233552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
2234552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
2235552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
2236552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
2237552f7358SJed Brown   }
2238552f7358SJed Brown   /* Check each successive support */
2239552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2240552f7358SJed Brown     PetscInt newJoinSize = 0;
2241552f7358SJed Brown 
2242552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
2243552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
2244552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2245552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
2246552f7358SJed Brown 
2247552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
2248552f7358SJed Brown         if (point == join[i][m]) {
2249552f7358SJed Brown           join[1-i][newJoinSize++] = point;
2250552f7358SJed Brown           break;
2251552f7358SJed Brown         }
2252552f7358SJed Brown       }
2253552f7358SJed Brown     }
2254552f7358SJed Brown     joinSize = newJoinSize;
2255552f7358SJed Brown     i        = 1-i;
2256552f7358SJed Brown   }
2257552f7358SJed Brown   *numCoveredPoints = joinSize;
2258552f7358SJed Brown   *coveredPoints    = join[i];
2259552f7358SJed Brown   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
2260552f7358SJed Brown   PetscFunctionReturn(0);
2261552f7358SJed Brown }
2262552f7358SJed Brown 
2263552f7358SJed Brown #undef __FUNCT__
2264552f7358SJed Brown #define __FUNCT__ "DMPlexRestoreJoin"
2265552f7358SJed Brown /*@C
2266552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
2267552f7358SJed Brown 
2268552f7358SJed Brown   Not Collective
2269552f7358SJed Brown 
2270552f7358SJed Brown   Input Parameters:
2271552f7358SJed Brown + dm - The DMPlex object
2272552f7358SJed Brown . numPoints - The number of input points for the join
2273552f7358SJed Brown - points - The input points
2274552f7358SJed Brown 
2275552f7358SJed Brown   Output Parameters:
2276552f7358SJed Brown + numCoveredPoints - The number of points in the join
2277552f7358SJed Brown - coveredPoints - The points in the join
2278552f7358SJed Brown 
22793813dfbdSMatthew G Knepley   Fortran Notes:
22803813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22813813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22823813dfbdSMatthew G Knepley 
22833813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
22843813dfbdSMatthew G Knepley 
2285552f7358SJed Brown   Level: intermediate
2286552f7358SJed Brown 
2287552f7358SJed Brown .keywords: mesh
2288552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
2289552f7358SJed Brown @*/
2290552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2291552f7358SJed Brown {
2292552f7358SJed Brown   PetscErrorCode ierr;
2293552f7358SJed Brown 
2294552f7358SJed Brown   PetscFunctionBegin;
2295552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2296d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2297d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2298d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
2299552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
2300d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2301552f7358SJed Brown   PetscFunctionReturn(0);
2302552f7358SJed Brown }
2303552f7358SJed Brown 
2304552f7358SJed Brown #undef __FUNCT__
2305552f7358SJed Brown #define __FUNCT__ "DMPlexGetFullJoin"
2306552f7358SJed Brown /*@C
2307552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
2308552f7358SJed Brown 
2309552f7358SJed Brown   Not Collective
2310552f7358SJed Brown 
2311552f7358SJed Brown   Input Parameters:
2312552f7358SJed Brown + dm - The DMPlex object
2313552f7358SJed Brown . numPoints - The number of input points for the join
2314552f7358SJed Brown - points - The input points
2315552f7358SJed Brown 
2316552f7358SJed Brown   Output Parameters:
2317552f7358SJed Brown + numCoveredPoints - The number of points in the join
2318552f7358SJed Brown - coveredPoints - The points in the join
2319552f7358SJed Brown 
23203813dfbdSMatthew G Knepley   Fortran Notes:
23213813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
23223813dfbdSMatthew G Knepley   include petsc.h90 in your code.
23233813dfbdSMatthew G Knepley 
23243813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
23253813dfbdSMatthew G Knepley 
2326552f7358SJed Brown   Level: intermediate
2327552f7358SJed Brown 
2328552f7358SJed Brown .keywords: mesh
2329552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
2330552f7358SJed Brown @*/
2331552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2332552f7358SJed Brown {
2333552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2334552f7358SJed Brown   PetscInt      *offsets, **closures;
2335552f7358SJed Brown   PetscInt      *join[2];
2336552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
233724c766afSToby Isaac   PetscInt       p, d, c, m, ms;
2338552f7358SJed Brown   PetscErrorCode ierr;
2339552f7358SJed Brown 
2340552f7358SJed Brown   PetscFunctionBegin;
2341552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2342552f7358SJed Brown   PetscValidPointer(points, 2);
2343552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2344552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2345552f7358SJed Brown 
2346552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
23471795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
2348552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
234924c766afSToby Isaac   ms      = mesh->maxSupportSize;
235024c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
2351552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
2352552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
2353552f7358SJed Brown 
2354552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2355552f7358SJed Brown     PetscInt closureSize;
2356552f7358SJed Brown 
2357552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
23580d644c17SKarl Rupp 
2359552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
2360552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
2361552f7358SJed Brown       PetscInt pStart, pEnd, i;
2362552f7358SJed Brown 
2363552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
2364552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
2365552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2366552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
2367552f7358SJed Brown           break;
2368552f7358SJed Brown         }
2369552f7358SJed Brown       }
2370552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
2371552f7358SJed Brown     }
237282f516ccSBarry Smith     if (offsets[p*(depth+2)+depth+1] != closureSize) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Total size of closure %D should be %D", offsets[p*(depth+2)+depth+1], closureSize);
2373552f7358SJed Brown   }
2374552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
2375552f7358SJed Brown     PetscInt dof;
2376552f7358SJed Brown 
2377552f7358SJed Brown     /* Copy in support of first point */
2378552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
2379552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
2380552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
2381552f7358SJed Brown     }
2382552f7358SJed Brown     /* Check each successive cone */
2383552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
2384552f7358SJed Brown       PetscInt newJoinSize = 0;
2385552f7358SJed Brown 
2386552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
2387552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2388552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
2389552f7358SJed Brown 
2390552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
2391552f7358SJed Brown           if (point == join[i][m]) {
2392552f7358SJed Brown             join[1-i][newJoinSize++] = point;
2393552f7358SJed Brown             break;
2394552f7358SJed Brown           }
2395552f7358SJed Brown         }
2396552f7358SJed Brown       }
2397552f7358SJed Brown       joinSize = newJoinSize;
2398552f7358SJed Brown       i        = 1-i;
2399552f7358SJed Brown     }
2400552f7358SJed Brown     if (joinSize) break;
2401552f7358SJed Brown   }
2402552f7358SJed Brown   *numCoveredPoints = joinSize;
2403552f7358SJed Brown   *coveredPoints    = join[i];
2404552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
24050298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
2406552f7358SJed Brown   }
2407552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
2408552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
2409552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
2410552f7358SJed Brown   PetscFunctionReturn(0);
2411552f7358SJed Brown }
2412552f7358SJed Brown 
2413552f7358SJed Brown #undef __FUNCT__
2414552f7358SJed Brown #define __FUNCT__ "DMPlexGetMeet"
2415552f7358SJed Brown /*@C
2416552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
2417552f7358SJed Brown 
2418552f7358SJed Brown   Not Collective
2419552f7358SJed Brown 
2420552f7358SJed Brown   Input Parameters:
2421552f7358SJed Brown + dm - The DMPlex object
2422552f7358SJed Brown . numPoints - The number of input points for the meet
2423552f7358SJed Brown - points - The input points
2424552f7358SJed Brown 
2425552f7358SJed Brown   Output Parameters:
2426552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2427552f7358SJed Brown - coveredPoints - The points in the meet
2428552f7358SJed Brown 
2429552f7358SJed Brown   Level: intermediate
2430552f7358SJed Brown 
2431552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
2432552f7358SJed Brown 
24333813dfbdSMatthew G Knepley   Fortran Notes:
24343813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24353813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24363813dfbdSMatthew G Knepley 
24373813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24383813dfbdSMatthew G Knepley 
2439552f7358SJed Brown .keywords: mesh
2440552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
2441552f7358SJed Brown @*/
2442552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
2443552f7358SJed Brown {
2444552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2445552f7358SJed Brown   PetscInt      *meet[2];
2446552f7358SJed Brown   PetscInt       meetSize, i = 0;
2447552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2448552f7358SJed Brown   PetscErrorCode ierr;
2449552f7358SJed Brown 
2450552f7358SJed Brown   PetscFunctionBegin;
2451552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2452552f7358SJed Brown   PetscValidPointer(points, 2);
2453552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
2454552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
2455552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
2456552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
2457552f7358SJed Brown   /* Copy in cone of first point */
2458552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
2459552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
2460552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
2461552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
2462552f7358SJed Brown   }
2463552f7358SJed Brown   /* Check each successive cone */
2464552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2465552f7358SJed Brown     PetscInt newMeetSize = 0;
2466552f7358SJed Brown 
2467552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
2468552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
2469552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2470552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
2471552f7358SJed Brown 
2472552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
2473552f7358SJed Brown         if (point == meet[i][m]) {
2474552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
2475552f7358SJed Brown           break;
2476552f7358SJed Brown         }
2477552f7358SJed Brown       }
2478552f7358SJed Brown     }
2479552f7358SJed Brown     meetSize = newMeetSize;
2480552f7358SJed Brown     i        = 1-i;
2481552f7358SJed Brown   }
2482552f7358SJed Brown   *numCoveringPoints = meetSize;
2483552f7358SJed Brown   *coveringPoints    = meet[i];
2484552f7358SJed Brown   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
2485552f7358SJed Brown   PetscFunctionReturn(0);
2486552f7358SJed Brown }
2487552f7358SJed Brown 
2488552f7358SJed Brown #undef __FUNCT__
2489552f7358SJed Brown #define __FUNCT__ "DMPlexRestoreMeet"
2490552f7358SJed Brown /*@C
2491552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
2492552f7358SJed Brown 
2493552f7358SJed Brown   Not Collective
2494552f7358SJed Brown 
2495552f7358SJed Brown   Input Parameters:
2496552f7358SJed Brown + dm - The DMPlex object
2497552f7358SJed Brown . numPoints - The number of input points for the meet
2498552f7358SJed Brown - points - The input points
2499552f7358SJed Brown 
2500552f7358SJed Brown   Output Parameters:
2501552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2502552f7358SJed Brown - coveredPoints - The points in the meet
2503552f7358SJed Brown 
2504552f7358SJed Brown   Level: intermediate
2505552f7358SJed Brown 
25063813dfbdSMatthew G Knepley   Fortran Notes:
25073813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25083813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25093813dfbdSMatthew G Knepley 
25103813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25113813dfbdSMatthew G Knepley 
2512552f7358SJed Brown .keywords: mesh
2513552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
2514552f7358SJed Brown @*/
2515552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2516552f7358SJed Brown {
2517552f7358SJed Brown   PetscErrorCode ierr;
2518552f7358SJed Brown 
2519552f7358SJed Brown   PetscFunctionBegin;
2520552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2521d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2522d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2523d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
2524552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
2525d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2526552f7358SJed Brown   PetscFunctionReturn(0);
2527552f7358SJed Brown }
2528552f7358SJed Brown 
2529552f7358SJed Brown #undef __FUNCT__
2530552f7358SJed Brown #define __FUNCT__ "DMPlexGetFullMeet"
2531552f7358SJed Brown /*@C
2532552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
2533552f7358SJed Brown 
2534552f7358SJed Brown   Not Collective
2535552f7358SJed Brown 
2536552f7358SJed Brown   Input Parameters:
2537552f7358SJed Brown + dm - The DMPlex object
2538552f7358SJed Brown . numPoints - The number of input points for the meet
2539552f7358SJed Brown - points - The input points
2540552f7358SJed Brown 
2541552f7358SJed Brown   Output Parameters:
2542552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2543552f7358SJed Brown - coveredPoints - The points in the meet
2544552f7358SJed Brown 
2545552f7358SJed Brown   Level: intermediate
2546552f7358SJed Brown 
25473813dfbdSMatthew G Knepley   Fortran Notes:
25483813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25493813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25503813dfbdSMatthew G Knepley 
25513813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25523813dfbdSMatthew G Knepley 
2553552f7358SJed Brown .keywords: mesh
2554552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
2555552f7358SJed Brown @*/
2556552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2557552f7358SJed Brown {
2558552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2559552f7358SJed Brown   PetscInt      *offsets, **closures;
2560552f7358SJed Brown   PetscInt      *meet[2];
2561552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
256224c766afSToby Isaac   PetscInt       p, h, c, m, mc;
2563552f7358SJed Brown   PetscErrorCode ierr;
2564552f7358SJed Brown 
2565552f7358SJed Brown   PetscFunctionBegin;
2566552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2567552f7358SJed Brown   PetscValidPointer(points, 2);
2568552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2569552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2570552f7358SJed Brown 
2571552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
2572785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
2573552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
257424c766afSToby Isaac   mc      = mesh->maxConeSize;
257524c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
2576552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
2577552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
2578552f7358SJed Brown 
2579552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2580552f7358SJed Brown     PetscInt closureSize;
2581552f7358SJed Brown 
2582552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
25830d644c17SKarl Rupp 
2584552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
2585552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
2586552f7358SJed Brown       PetscInt pStart, pEnd, i;
2587552f7358SJed Brown 
2588552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
2589552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
2590552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2591552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
2592552f7358SJed Brown           break;
2593552f7358SJed Brown         }
2594552f7358SJed Brown       }
2595552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
2596552f7358SJed Brown     }
259782f516ccSBarry Smith     if (offsets[p*(height+2)+height+1] != closureSize) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Total size of closure %D should be %D", offsets[p*(height+2)+height+1], closureSize);
2598552f7358SJed Brown   }
2599552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
2600552f7358SJed Brown     PetscInt dof;
2601552f7358SJed Brown 
2602552f7358SJed Brown     /* Copy in cone of first point */
2603552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
2604552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
2605552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
2606552f7358SJed Brown     }
2607552f7358SJed Brown     /* Check each successive cone */
2608552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
2609552f7358SJed Brown       PetscInt newMeetSize = 0;
2610552f7358SJed Brown 
2611552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
2612552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2613552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
2614552f7358SJed Brown 
2615552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
2616552f7358SJed Brown           if (point == meet[i][m]) {
2617552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
2618552f7358SJed Brown             break;
2619552f7358SJed Brown           }
2620552f7358SJed Brown         }
2621552f7358SJed Brown       }
2622552f7358SJed Brown       meetSize = newMeetSize;
2623552f7358SJed Brown       i        = 1-i;
2624552f7358SJed Brown     }
2625552f7358SJed Brown     if (meetSize) break;
2626552f7358SJed Brown   }
2627552f7358SJed Brown   *numCoveredPoints = meetSize;
2628552f7358SJed Brown   *coveredPoints    = meet[i];
2629552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
26300298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
2631552f7358SJed Brown   }
2632552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
2633552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
2634552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
2635552f7358SJed Brown   PetscFunctionReturn(0);
2636552f7358SJed Brown }
2637552f7358SJed Brown 
2638552f7358SJed Brown #undef __FUNCT__
26394e3744c5SMatthew G. Knepley #define __FUNCT__ "DMPlexEqual"
26404e3744c5SMatthew G. Knepley /*@C
26414e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
26424e3744c5SMatthew G. Knepley 
26434e3744c5SMatthew G. Knepley   Not Collective
26444e3744c5SMatthew G. Knepley 
26454e3744c5SMatthew G. Knepley   Input Parameters:
26464e3744c5SMatthew G. Knepley + dmA - A DMPlex object
26474e3744c5SMatthew G. Knepley - dmB - A DMPlex object
26484e3744c5SMatthew G. Knepley 
26494e3744c5SMatthew G. Knepley   Output Parameters:
26504e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
26514e3744c5SMatthew G. Knepley 
26524e3744c5SMatthew G. Knepley   Level: intermediate
26534e3744c5SMatthew G. Knepley 
26544e3744c5SMatthew G. Knepley   Notes:
26554e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
26564e3744c5SMatthew G. Knepley 
26574e3744c5SMatthew G. Knepley .keywords: mesh
26584e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
26594e3744c5SMatthew G. Knepley @*/
26604e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
26614e3744c5SMatthew G. Knepley {
26624e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
26634e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
26644e3744c5SMatthew G. Knepley 
26654e3744c5SMatthew G. Knepley   PetscFunctionBegin;
26664e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
26674e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
26684e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
26694e3744c5SMatthew G. Knepley 
26704e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
26714e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
26724e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
26734e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
26744e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
26754e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
26764e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
26774e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
26784e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
26794e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
26804e3744c5SMatthew G. Knepley 
26814e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
26824e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
26834e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
26844e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
26854e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
26864e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
26874e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
26884e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
26894e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
26904e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
26914e3744c5SMatthew G. Knepley     }
26924e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
26934e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
26944e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
26954e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
26964e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
26974e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
26984e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
26994e3744c5SMatthew G. Knepley     }
27004e3744c5SMatthew G. Knepley   }
27014e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
27024e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
27034e3744c5SMatthew G. Knepley }
27044e3744c5SMatthew G. Knepley 
27054e3744c5SMatthew G. Knepley #undef __FUNCT__
270618ad9376SMatthew G. Knepley #define __FUNCT__ "DMPlexGetNumFaceVertices"
270718ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
2708a6dfd86eSKarl Rupp {
270982f516ccSBarry Smith   MPI_Comm       comm;
2710552f7358SJed Brown   PetscErrorCode ierr;
2711552f7358SJed Brown 
2712552f7358SJed Brown   PetscFunctionBegin;
271382f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2714552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
2715552f7358SJed Brown   switch (cellDim) {
2716552f7358SJed Brown   case 0:
2717552f7358SJed Brown     *numFaceVertices = 0;
2718552f7358SJed Brown     break;
2719552f7358SJed Brown   case 1:
2720552f7358SJed Brown     *numFaceVertices = 1;
2721552f7358SJed Brown     break;
2722552f7358SJed Brown   case 2:
2723552f7358SJed Brown     switch (numCorners) {
272419436ca2SJed Brown     case 3: /* triangle */
272519436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2726552f7358SJed Brown       break;
272719436ca2SJed Brown     case 4: /* quadrilateral */
272819436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2729552f7358SJed Brown       break;
273019436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
273119436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2732552f7358SJed Brown       break;
273319436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
273419436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2735552f7358SJed Brown       break;
2736552f7358SJed Brown     default:
2737f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2738552f7358SJed Brown     }
2739552f7358SJed Brown     break;
2740552f7358SJed Brown   case 3:
2741552f7358SJed Brown     switch (numCorners) {
274219436ca2SJed Brown     case 4: /* tetradehdron */
274319436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
2744552f7358SJed Brown       break;
274519436ca2SJed Brown     case 6: /* tet cohesive cells */
274619436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2747552f7358SJed Brown       break;
274819436ca2SJed Brown     case 8: /* hexahedron */
274919436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2750552f7358SJed Brown       break;
275119436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
275219436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2753552f7358SJed Brown       break;
275419436ca2SJed Brown     case 10: /* quadratic tetrahedron */
275519436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2756552f7358SJed Brown       break;
275719436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
275819436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2759552f7358SJed Brown       break;
276019436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
276119436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2762552f7358SJed Brown       break;
276319436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
276419436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
2765552f7358SJed Brown       break;
2766552f7358SJed Brown     default:
2767f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2768552f7358SJed Brown     }
2769552f7358SJed Brown     break;
2770552f7358SJed Brown   default:
2771f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
2772552f7358SJed Brown   }
2773552f7358SJed Brown   PetscFunctionReturn(0);
2774552f7358SJed Brown }
2775552f7358SJed Brown 
2776552f7358SJed Brown #undef __FUNCT__
2777aa50250dSMatthew G. Knepley #define __FUNCT__ "DMPlexGetDepthLabel"
2778552f7358SJed Brown /*@
2779aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
2780552f7358SJed Brown 
2781552f7358SJed Brown   Not Collective
2782552f7358SJed Brown 
2783aa50250dSMatthew G. Knepley   Input Parameter:
2784552f7358SJed Brown . dm    - The DMPlex object
2785552f7358SJed Brown 
2786aa50250dSMatthew G. Knepley   Output Parameter:
2787aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
2788552f7358SJed Brown 
2789552f7358SJed Brown   Level: developer
2790552f7358SJed Brown 
2791aa50250dSMatthew G. Knepley .keywords: mesh, points
2792aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2793aa50250dSMatthew G. Knepley @*/
2794aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
2795aa50250dSMatthew G. Knepley {
2796aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
2797aa50250dSMatthew G. Knepley 
2798aa50250dSMatthew G. Knepley   PetscFunctionBegin;
2799aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2800aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
2801c58f1c22SToby Isaac   if (!dm->depthLabel) {ierr = DMGetLabel(dm, "depth", &dm->depthLabel);CHKERRQ(ierr);}
2802c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
2803aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
2804aa50250dSMatthew G. Knepley }
2805aa50250dSMatthew G. Knepley 
2806aa50250dSMatthew G. Knepley #undef __FUNCT__
2807aa50250dSMatthew G. Knepley #define __FUNCT__ "DMPlexGetDepth"
2808aa50250dSMatthew G. Knepley /*@
2809aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
2810aa50250dSMatthew G. Knepley 
2811aa50250dSMatthew G. Knepley   Not Collective
2812aa50250dSMatthew G. Knepley 
2813aa50250dSMatthew G. Knepley   Input Parameter:
2814aa50250dSMatthew G. Knepley . dm    - The DMPlex object
2815aa50250dSMatthew G. Knepley 
2816aa50250dSMatthew G. Knepley   Output Parameter:
2817aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
2818aa50250dSMatthew G. Knepley 
2819aa50250dSMatthew G. Knepley   Level: developer
2820552f7358SJed Brown 
2821552f7358SJed Brown .keywords: mesh, points
2822aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2823552f7358SJed Brown @*/
2824552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
2825552f7358SJed Brown {
2826aa50250dSMatthew G. Knepley   DMLabel        label;
2827aa50250dSMatthew G. Knepley   PetscInt       d = 0;
2828552f7358SJed Brown   PetscErrorCode ierr;
2829552f7358SJed Brown 
2830552f7358SJed Brown   PetscFunctionBegin;
2831552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2832552f7358SJed Brown   PetscValidPointer(depth, 2);
2833aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2834aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
2835552f7358SJed Brown   *depth = d-1;
2836552f7358SJed Brown   PetscFunctionReturn(0);
2837552f7358SJed Brown }
2838552f7358SJed Brown 
2839552f7358SJed Brown #undef __FUNCT__
2840552f7358SJed Brown #define __FUNCT__ "DMPlexGetDepthStratum"
2841552f7358SJed Brown /*@
2842552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
2843552f7358SJed Brown 
2844552f7358SJed Brown   Not Collective
2845552f7358SJed Brown 
2846552f7358SJed Brown   Input Parameters:
2847552f7358SJed Brown + dm           - The DMPlex object
2848552f7358SJed Brown - stratumValue - The requested depth
2849552f7358SJed Brown 
2850552f7358SJed Brown   Output Parameters:
2851552f7358SJed Brown + start - The first point at this depth
2852552f7358SJed Brown - end   - One beyond the last point at this depth
2853552f7358SJed Brown 
2854552f7358SJed Brown   Level: developer
2855552f7358SJed Brown 
2856552f7358SJed Brown .keywords: mesh, points
2857552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
2858552f7358SJed Brown @*/
28590adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
28600adebc6cSBarry Smith {
2861aa50250dSMatthew G. Knepley   DMLabel        label;
286263d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
2863552f7358SJed Brown   PetscErrorCode ierr;
2864552f7358SJed Brown 
2865552f7358SJed Brown   PetscFunctionBegin;
2866552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
286763d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
286863d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
2869552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
28700d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
287163d1a920SMatthew G. Knepley   if (stratumValue < 0) {
287263d1a920SMatthew G. Knepley     if (start) *start = pStart;
287363d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
287463d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
2875552f7358SJed Brown   }
2876aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
287763d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
287863d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
2879552f7358SJed Brown   PetscFunctionReturn(0);
2880552f7358SJed Brown }
2881552f7358SJed Brown 
2882552f7358SJed Brown #undef __FUNCT__
2883552f7358SJed Brown #define __FUNCT__ "DMPlexGetHeightStratum"
2884552f7358SJed Brown /*@
2885552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
2886552f7358SJed Brown 
2887552f7358SJed Brown   Not Collective
2888552f7358SJed Brown 
2889552f7358SJed Brown   Input Parameters:
2890552f7358SJed Brown + dm           - The DMPlex object
2891552f7358SJed Brown - stratumValue - The requested height
2892552f7358SJed Brown 
2893552f7358SJed Brown   Output Parameters:
2894552f7358SJed Brown + start - The first point at this height
2895552f7358SJed Brown - end   - One beyond the last point at this height
2896552f7358SJed Brown 
2897552f7358SJed Brown   Level: developer
2898552f7358SJed Brown 
2899552f7358SJed Brown .keywords: mesh, points
2900552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
2901552f7358SJed Brown @*/
29020adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
29030adebc6cSBarry Smith {
2904aa50250dSMatthew G. Knepley   DMLabel        label;
290563d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
2906552f7358SJed Brown   PetscErrorCode ierr;
2907552f7358SJed Brown 
2908552f7358SJed Brown   PetscFunctionBegin;
2909552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
291063d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
291163d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
2912552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
29130d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
291463d1a920SMatthew G. Knepley   if (stratumValue < 0) {
291563d1a920SMatthew G. Knepley     if (start) *start = pStart;
291663d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
291763d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
2918552f7358SJed Brown   }
2919aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2920aa50250dSMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");CHKERRQ(ierr);
292163d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
292263d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
2923552f7358SJed Brown   PetscFunctionReturn(0);
2924552f7358SJed Brown }
2925552f7358SJed Brown 
2926552f7358SJed Brown #undef __FUNCT__
2927552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSectionInitial"
2928552f7358SJed Brown /* Set the number of dof on each point and separate by fields */
2929a6dfd86eSKarl Rupp PetscErrorCode DMPlexCreateSectionInitial(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscSection *section)
2930a6dfd86eSKarl Rupp {
2931ee55978aSMatthew G. Knepley   PetscInt      *pMax;
2932916f0f19SMatthew G. Knepley   PetscInt       depth, pStart = 0, pEnd = 0;
2933ee55978aSMatthew G. Knepley   PetscInt       Nf, p, d, dep, f;
2934fa716be8SMatthew G. Knepley   PetscBool     *isFE;
2935552f7358SJed Brown   PetscErrorCode ierr;
2936552f7358SJed Brown 
2937552f7358SJed Brown   PetscFunctionBegin;
2938fa716be8SMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
2939ee55978aSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
2940fa716be8SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
2941fa716be8SMatthew G. Knepley     PetscObject  obj;
2942fa716be8SMatthew G. Knepley     PetscClassId id;
2943fa716be8SMatthew G. Knepley 
2944ee55978aSMatthew G. Knepley     isFE[f] = PETSC_FALSE;
2945ee55978aSMatthew G. Knepley     if (f >= Nf) continue;
2946fa716be8SMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
2947fa716be8SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2948fa716be8SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
2949fa716be8SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
2950552f7358SJed Brown   }
295182f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), section);CHKERRQ(ierr);
2952552f7358SJed Brown   if (numFields > 0) {
2953552f7358SJed Brown     ierr = PetscSectionSetNumFields(*section, numFields);CHKERRQ(ierr);
2954552f7358SJed Brown     if (numComp) {
2955552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
2956552f7358SJed Brown         ierr = PetscSectionSetFieldComponents(*section, f, numComp[f]);CHKERRQ(ierr);
2957d484f18aSToby Isaac         if (isFE[f]) {
2958d484f18aSToby Isaac           PetscFE           fe;
2959d484f18aSToby Isaac           PetscDualSpace    dspace;
2960d484f18aSToby Isaac           const PetscInt    ***perms;
2961d484f18aSToby Isaac           const PetscScalar ***flips;
2962d484f18aSToby Isaac           const PetscInt    *numDof;
2963d484f18aSToby Isaac 
2964d484f18aSToby Isaac           ierr = DMGetField(dm,f,(PetscObject *) &fe);CHKERRQ(ierr);
2965d484f18aSToby Isaac           ierr = PetscFEGetDualSpace(fe,&dspace);CHKERRQ(ierr);
2966d484f18aSToby Isaac           ierr = PetscDualSpaceGetSymmetries(dspace,&perms,&flips);CHKERRQ(ierr);
2967d484f18aSToby Isaac           ierr = PetscDualSpaceGetNumDof(dspace,&numDof);CHKERRQ(ierr);
2968d484f18aSToby Isaac           if (perms || flips) {
2969d484f18aSToby Isaac             DM               K;
2970d484f18aSToby Isaac             DMLabel          depthLabel;
2971d484f18aSToby Isaac             PetscInt         depth, h;
2972d484f18aSToby Isaac             PetscSectionSym  sym;
2973d484f18aSToby Isaac 
2974d484f18aSToby Isaac             ierr = PetscDualSpaceGetDM(dspace,&K);CHKERRQ(ierr);
2975d484f18aSToby Isaac             ierr = DMPlexGetDepthLabel(dm,&depthLabel);CHKERRQ(ierr);
2976d484f18aSToby Isaac             ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
2977d484f18aSToby Isaac             ierr = PetscSectionSymCreateLabel(PetscObjectComm((PetscObject)*section),depthLabel,&sym);CHKERRQ(ierr);
2978d484f18aSToby Isaac             for (h = 0; h <= depth; h++) {
2979d484f18aSToby Isaac               PetscDualSpace    hspace;
2980d484f18aSToby Isaac               PetscInt          kStart, kEnd;
2981d484f18aSToby Isaac               PetscInt          kConeSize;
2982d484f18aSToby Isaac               const PetscInt    **perms0 = NULL;
2983d484f18aSToby Isaac               const PetscScalar **flips0 = NULL;
2984d484f18aSToby Isaac 
2985d484f18aSToby Isaac               ierr = PetscDualSpaceGetHeightSubspace(dspace,h,&hspace);CHKERRQ(ierr);
2986d484f18aSToby Isaac               ierr = DMPlexGetHeightStratum(K,h,&kStart,&kEnd);CHKERRQ(ierr);
2987d484f18aSToby Isaac               if (!hspace) continue;
2988d484f18aSToby Isaac               ierr = PetscDualSpaceGetSymmetries(hspace,&perms,&flips);CHKERRQ(ierr);
2989d484f18aSToby Isaac               if (perms) perms0 = perms[0];
2990d484f18aSToby Isaac               if (flips) flips0 = flips[0];
2991d484f18aSToby Isaac               if (!(perms0 || flips0)) continue;
2992d484f18aSToby Isaac               ierr = DMPlexGetConeSize(K,kStart,&kConeSize);CHKERRQ(ierr);
2993d484f18aSToby Isaac               if (numComp[f] == 1) {
2994d484f18aSToby Isaac                 ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numDof[depth - h],-kConeSize,kConeSize,PETSC_USE_POINTER,perms0 ? &perms0[-kConeSize] : NULL,flips0 ? &flips0[-kConeSize] : NULL);CHKERRQ(ierr);
2995d484f18aSToby Isaac               } else {
2996d484f18aSToby Isaac                 PetscInt    **fieldPerms = NULL, o;
2997d484f18aSToby Isaac                 PetscScalar **fieldFlips = NULL;
2998d484f18aSToby Isaac 
2999d484f18aSToby Isaac                 ierr = PetscCalloc1(2 * kConeSize,&fieldPerms);CHKERRQ(ierr);
3000d484f18aSToby Isaac                 ierr = PetscCalloc1(2 * kConeSize,&fieldFlips);CHKERRQ(ierr);
3001d484f18aSToby Isaac                 for (o = -kConeSize; o < kConeSize; o++) {
3002d484f18aSToby Isaac                   if (perms0 && perms0[o]) {
3003d484f18aSToby Isaac                     PetscInt r, s;
3004d484f18aSToby Isaac 
3005d484f18aSToby Isaac                     ierr = PetscMalloc1(numComp[f] * numDof[depth - h],&fieldPerms[o+kConeSize]);CHKERRQ(ierr);
3006d484f18aSToby Isaac                     for (r = 0; r < numDof[depth - h]; r++) {
3007d484f18aSToby Isaac                       for (s = 0; s < numComp[f]; s++) {
3008d484f18aSToby Isaac                         fieldPerms[o+kConeSize][r * numComp[f] + s] = numComp[f] * perms0[o][r] + s;
3009d484f18aSToby Isaac                       }
3010d484f18aSToby Isaac                     }
3011d484f18aSToby Isaac                   }
3012d484f18aSToby Isaac                   if (flips0 && flips0[o]) {
3013d484f18aSToby Isaac                     PetscInt r, s;
3014d484f18aSToby Isaac 
3015d484f18aSToby Isaac                     ierr = PetscMalloc1(numComp[f] * numDof[depth - h],&fieldFlips[o+kConeSize]);CHKERRQ(ierr);
3016d484f18aSToby Isaac                     for (r = 0; r < numDof[depth - h]; r++) {
3017d484f18aSToby Isaac                       for (s = 0; s < numComp[f]; s++) {
3018d484f18aSToby Isaac                         fieldFlips[o+kConeSize][r * numComp[f] + s] = flips0[o][r];
3019d484f18aSToby Isaac                       }
3020d484f18aSToby Isaac                     }
3021d484f18aSToby Isaac                   }
3022d484f18aSToby Isaac                 }
3023d484f18aSToby Isaac                 ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numComp[f] * numDof[depth - h],-kConeSize,kConeSize,PETSC_OWN_POINTER,(const PetscInt **) fieldPerms,(const PetscScalar **)fieldFlips);CHKERRQ(ierr);
3024d484f18aSToby Isaac               }
3025d484f18aSToby Isaac             }
3026d484f18aSToby Isaac             ierr = PetscSectionSetFieldSym(*section,f,sym);CHKERRQ(ierr);
3027d484f18aSToby Isaac             ierr = PetscSectionSymDestroy(&sym);CHKERRQ(ierr);
3028d484f18aSToby Isaac           }
3029d484f18aSToby Isaac         }
3030552f7358SJed Brown       }
3031552f7358SJed Brown     }
3032552f7358SJed Brown   }
3033552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3034552f7358SJed Brown   ierr = PetscSectionSetChart(*section, pStart, pEnd);CHKERRQ(ierr);
3035916f0f19SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
30369ac3fadcSMatthew G. Knepley   ierr = PetscMalloc1(depth+1,&pMax);CHKERRQ(ierr);
30379ac3fadcSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, depth >= 0 ? &pMax[depth] : NULL, depth>1 ? &pMax[depth-1] : NULL, depth>2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr);
3038916f0f19SMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
3039916f0f19SMatthew G. Knepley     d    = dim == depth ? dep : (!dep ? 0 : dim);
3040916f0f19SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dep, &pStart, &pEnd);CHKERRQ(ierr);
3041fa716be8SMatthew G. Knepley     pMax[dep] = pMax[dep] < 0 ? pEnd : pMax[dep];
3042552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
3043ee55978aSMatthew G. Knepley       PetscInt tot = 0;
3044ee55978aSMatthew G. Knepley 
3045552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3046fa716be8SMatthew G. Knepley         if (isFE[f] && p >= pMax[dep]) continue;
3047552f7358SJed Brown         ierr = PetscSectionSetFieldDof(*section, p, f, numDof[f*(dim+1)+d]);CHKERRQ(ierr);
3048ee55978aSMatthew G. Knepley         tot += numDof[f*(dim+1)+d];
3049552f7358SJed Brown       }
3050ee55978aSMatthew G. Knepley       ierr = PetscSectionSetDof(*section, p, tot);CHKERRQ(ierr);
3051552f7358SJed Brown     }
3052552f7358SJed Brown   }
30539ac3fadcSMatthew G. Knepley   ierr = PetscFree(pMax);CHKERRQ(ierr);
3054fa716be8SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
3055552f7358SJed Brown   PetscFunctionReturn(0);
3056552f7358SJed Brown }
3057552f7358SJed Brown 
3058552f7358SJed Brown #undef __FUNCT__
3059552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSectionBCDof"
3060552f7358SJed Brown /* Set the number of dof on each point and separate by fields
3061ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3062552f7358SJed Brown */
3063ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSectionBCDof(DM dm, PetscInt numBC, const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
3064a6dfd86eSKarl Rupp {
3065552f7358SJed Brown   PetscInt       numFields;
3066552f7358SJed Brown   PetscInt       bc;
3067a68b90caSToby Isaac   PetscSection   aSec;
3068552f7358SJed Brown   PetscErrorCode ierr;
3069552f7358SJed Brown 
3070552f7358SJed Brown   PetscFunctionBegin;
3071552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3072552f7358SJed Brown   for (bc = 0; bc < numBC; ++bc) {
3073552f7358SJed Brown     PetscInt        field = 0;
3074ab1d0545SMatthew G. Knepley     const PetscInt *comp;
3075552f7358SJed Brown     const PetscInt *idx;
3076ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3077552f7358SJed Brown 
30780d644c17SKarl Rupp     if (numFields) field = bcField[bc];
3079ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3080ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3081552f7358SJed Brown     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3082552f7358SJed Brown     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3083552f7358SJed Brown     for (i = 0; i < n; ++i) {
3084552f7358SJed Brown       const PetscInt p = idx[i];
308506ff1081SMatthew G. Knepley       PetscInt       numConst;
3086552f7358SJed Brown 
3087552f7358SJed Brown       if (numFields) {
3088552f7358SJed Brown         ierr = PetscSectionGetFieldDof(section, p, field, &numConst);CHKERRQ(ierr);
3089552f7358SJed Brown       } else {
3090552f7358SJed Brown         ierr = PetscSectionGetDof(section, p, &numConst);CHKERRQ(ierr);
3091552f7358SJed Brown       }
309206ff1081SMatthew G. Knepley       /* If Nc < 0, constrain every dof on the point */
309306ff1081SMatthew G. Knepley       if (Nc > 0) numConst = PetscMin(numConst, Nc);
309406ff1081SMatthew G. Knepley       if (numFields) {ierr = PetscSectionAddFieldConstraintDof(section, p, field, numConst);CHKERRQ(ierr);}
3095552f7358SJed Brown       ierr = PetscSectionAddConstraintDof(section, p, numConst);CHKERRQ(ierr);
3096552f7358SJed Brown     }
3097552f7358SJed Brown     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
309806ff1081SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3099552f7358SJed Brown   }
3100a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3101a68b90caSToby Isaac   if (aSec) {
3102a68b90caSToby Isaac     PetscInt aStart, aEnd, a;
3103a68b90caSToby Isaac 
3104a68b90caSToby Isaac     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3105a68b90caSToby Isaac     for (a = aStart; a < aEnd; a++) {
3106ab1d0545SMatthew G. Knepley       PetscInt dof, f;
3107a68b90caSToby Isaac 
3108a68b90caSToby Isaac       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3109a68b90caSToby Isaac       if (dof) {
3110a68b90caSToby Isaac         /* if there are point-to-point constraints, then all dofs are constrained */
3111a68b90caSToby Isaac         ierr = PetscSectionGetDof(section, a, &dof);CHKERRQ(ierr);
3112a68b90caSToby Isaac         ierr = PetscSectionSetConstraintDof(section, a, dof);CHKERRQ(ierr);
3113a68b90caSToby Isaac         for (f = 0; f < numFields; f++) {
3114a68b90caSToby Isaac           ierr = PetscSectionGetFieldDof(section, a, f, &dof);CHKERRQ(ierr);
3115a68b90caSToby Isaac           ierr = PetscSectionSetFieldConstraintDof(section, a, f, dof);CHKERRQ(ierr);
3116a68b90caSToby Isaac         }
3117a68b90caSToby Isaac       }
3118a68b90caSToby Isaac     }
3119a68b90caSToby Isaac   }
3120552f7358SJed Brown   PetscFunctionReturn(0);
3121552f7358SJed Brown }
3122552f7358SJed Brown 
3123552f7358SJed Brown #undef __FUNCT__
3124ab1d0545SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateSectionBCIndicesField"
3125ab1d0545SMatthew G. Knepley /* Set the constrained field indices on each point
3126ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3127ab1d0545SMatthew G. Knepley */
3128ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSectionBCIndicesField(DM dm, PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
31290adebc6cSBarry Smith {
3130ab1d0545SMatthew G. Knepley   PetscSection   aSec;
3131ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3132ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, bc, f, d;
3133552f7358SJed Brown   PetscErrorCode ierr;
3134552f7358SJed Brown 
3135552f7358SJed Brown   PetscFunctionBegin;
3136552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3137ab1d0545SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
3138ab1d0545SMatthew G. Knepley   /* Initialize all field indices to -1 */
3139552f7358SJed Brown   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3140ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3141ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3142ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3143ab1d0545SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) for (f = 0; f < numFields; ++f) {ierr = PetscSectionSetFieldConstraintIndices(section, p, f, indices);CHKERRQ(ierr);}
3144ab1d0545SMatthew G. Knepley   /* Handle BC constraints */
3145ab1d0545SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {
3146ab1d0545SMatthew G. Knepley     const PetscInt  field = bcField[bc];
3147ab1d0545SMatthew G. Knepley     const PetscInt *comp, *idx;
3148ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3149552f7358SJed Brown 
3150ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3151ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3152ab1d0545SMatthew G. Knepley     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3153ab1d0545SMatthew G. Knepley     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3154ab1d0545SMatthew G. Knepley     for (i = 0; i < n; ++i) {
3155ab1d0545SMatthew G. Knepley       const PetscInt  p = idx[i];
3156ab1d0545SMatthew G. Knepley       const PetscInt *find;
3157ab1d0545SMatthew G. Knepley       PetscInt        fcdof, c;
3158ab1d0545SMatthew G. Knepley 
3159ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintDof(section, p, field, &fcdof);CHKERRQ(ierr);
3160ab1d0545SMatthew G. Knepley       if (Nc < 0) {
3161ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) indices[d] = d;
3162552f7358SJed Brown       } else {
3163ab1d0545SMatthew G. Knepley         ierr = PetscSectionGetFieldConstraintIndices(section, p, field, &find);CHKERRQ(ierr);
3164ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) {if (find[d] < 0) break; indices[d] = find[d];}
3165ab1d0545SMatthew G. Knepley         for (c = 0; c < Nc; ++c) indices[d+c] = comp[c];
3166ab1d0545SMatthew G. Knepley         ierr = PetscSortInt(d+Nc, indices);CHKERRQ(ierr);
3167ab1d0545SMatthew G. Knepley         for (c = d+Nc; c < fcdof; ++c) indices[c] = -1;
3168552f7358SJed Brown       }
3169ab1d0545SMatthew G. Knepley       ierr = PetscSectionSetFieldConstraintIndices(section, p, field, indices);CHKERRQ(ierr);
3170552f7358SJed Brown     }
3171ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3172ab1d0545SMatthew G. Knepley     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3173552f7358SJed Brown   }
3174ab1d0545SMatthew G. Knepley   /* Handle anchors */
3175ab1d0545SMatthew G. Knepley   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3176ab1d0545SMatthew G. Knepley   if (aSec) {
3177ab1d0545SMatthew G. Knepley     PetscInt aStart, aEnd, a;
3178552f7358SJed Brown 
3179ab1d0545SMatthew G. Knepley     for (d = 0; d < maxDof; ++d) indices[d] = d;
3180ab1d0545SMatthew G. Knepley     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3181ab1d0545SMatthew G. Knepley     for (a = aStart; a < aEnd; a++) {
3182ab1d0545SMatthew G. Knepley       PetscInt dof, fdof, f;
3183ab1d0545SMatthew G. Knepley 
3184ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3185ab1d0545SMatthew G. Knepley       if (dof) {
3186ab1d0545SMatthew G. Knepley         /* if there are point-to-point constraints, then all dofs are constrained */
3187ab1d0545SMatthew G. Knepley         for (f = 0; f < numFields; f++) {
3188ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, a, f, &fdof);CHKERRQ(ierr);
3189ab1d0545SMatthew G. Knepley           ierr = PetscSectionSetFieldConstraintIndices(section, a, f, indices);CHKERRQ(ierr);
3190ab1d0545SMatthew G. Knepley         }
3191ab1d0545SMatthew G. Knepley       }
3192ab1d0545SMatthew G. Knepley     }
3193ab1d0545SMatthew G. Knepley   }
3194ab1d0545SMatthew G. Knepley   ierr = PetscFree(indices);CHKERRQ(ierr);
3195ab1d0545SMatthew G. Knepley   PetscFunctionReturn(0);
3196ab1d0545SMatthew G. Knepley }
3197ab1d0545SMatthew G. Knepley 
3198ab1d0545SMatthew G. Knepley #undef __FUNCT__
3199ab1d0545SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateSectionBCIndices"
3200ab1d0545SMatthew G. Knepley /* Set the constrained indices on each point */
3201ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSectionBCIndices(DM dm, PetscSection section)
3202ab1d0545SMatthew G. Knepley {
3203ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3204ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, f, d;
3205ab1d0545SMatthew G. Knepley   PetscErrorCode ierr;
3206ab1d0545SMatthew G. Knepley 
3207ab1d0545SMatthew G. Knepley   PetscFunctionBegin;
3208ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3209ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3210ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3211ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3212ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3213552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3214552f7358SJed Brown     PetscInt cdof, d;
3215552f7358SJed Brown 
3216552f7358SJed Brown     ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
3217552f7358SJed Brown     if (cdof) {
3218552f7358SJed Brown       if (numFields) {
3219552f7358SJed Brown         PetscInt numConst = 0, foff = 0;
3220552f7358SJed Brown 
3221552f7358SJed Brown         for (f = 0; f < numFields; ++f) {
3222ab1d0545SMatthew G. Knepley           const PetscInt *find;
3223ab1d0545SMatthew G. Knepley           PetscInt        fcdof, fdof;
3224552f7358SJed Brown 
3225552f7358SJed Brown           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
3226ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
3227ab1d0545SMatthew G. Knepley           /* Change constraint numbering from field component to local dof number */
3228ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintIndices(section, p, f, &find);CHKERRQ(ierr);
3229ab1d0545SMatthew G. Knepley           for (d = 0; d < fcdof; ++d) indices[numConst+d] = find[d] + foff;
3230ab1d0545SMatthew G. Knepley           numConst += fcdof;
3231552f7358SJed Brown           foff     += fdof;
3232552f7358SJed Brown         }
3233552f7358SJed Brown         if (cdof != numConst) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Total number of field constraints %D should be %D", numConst, cdof);
3234552f7358SJed Brown       } else {
32350d644c17SKarl Rupp         for (d = 0; d < cdof; ++d) indices[d] = d;
3236552f7358SJed Brown       }
3237552f7358SJed Brown       ierr = PetscSectionSetConstraintIndices(section, p, indices);CHKERRQ(ierr);
3238552f7358SJed Brown     }
3239552f7358SJed Brown   }
3240552f7358SJed Brown   ierr = PetscFree(indices);CHKERRQ(ierr);
3241552f7358SJed Brown   PetscFunctionReturn(0);
3242552f7358SJed Brown }
3243552f7358SJed Brown 
3244552f7358SJed Brown #undef __FUNCT__
3245552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSection"
3246552f7358SJed Brown /*@C
3247552f7358SJed Brown   DMPlexCreateSection - Create a PetscSection based upon the dof layout specification provided.
3248552f7358SJed Brown 
3249552f7358SJed Brown   Not Collective
3250552f7358SJed Brown 
3251552f7358SJed Brown   Input Parameters:
3252552f7358SJed Brown + dm        - The DMPlex object
3253552f7358SJed Brown . dim       - The spatial dimension of the problem
3254552f7358SJed Brown . numFields - The number of fields in the problem
3255552f7358SJed Brown . numComp   - An array of size numFields that holds the number of components for each field
3256552f7358SJed Brown . numDof    - An array of size numFields*(dim+1) which holds the number of dof for each field on a mesh piece of dimension d
3257552f7358SJed Brown . numBC     - The number of boundary conditions
3258552f7358SJed Brown . bcField   - An array of size numBC giving the field number for each boundry condition
3259ab1d0545SMatthew G. Knepley . bcComps   - [Optional] An array of size numBC giving an IS holding the field components to which each boundary condition applies
3260ab1d0545SMatthew G. Knepley . bcPoints  - An array of size numBC giving an IS holding the Plex points to which each boundary condition applies
3261f8f126e8SMatthew G. Knepley - perm      - Optional permutation of the chart, or NULL
3262552f7358SJed Brown 
3263552f7358SJed Brown   Output Parameter:
3264552f7358SJed Brown . section - The PetscSection object
3265552f7358SJed Brown 
3266552f7358SJed Brown   Notes: numDof[f*(dim+1)+d] gives the number of dof for field f on sieve points of dimension d. For instance, numDof[1] is the
3267f8f126e8SMatthew G. Knepley   number of dof for field 0 on each edge.
3268f8f126e8SMatthew G. Knepley 
3269f8f126e8SMatthew G. Knepley   The chart permutation is the same one set using PetscSectionSetPermutation()
3270552f7358SJed Brown 
3271552f7358SJed Brown   Level: developer
3272552f7358SJed Brown 
32733813dfbdSMatthew G Knepley   Fortran Notes:
32743813dfbdSMatthew G Knepley   A Fortran 90 version is available as DMPlexCreateSectionF90()
32753813dfbdSMatthew G Knepley 
3276552f7358SJed Brown .keywords: mesh, elements
3277f8f126e8SMatthew G. Knepley .seealso: DMPlexCreate(), PetscSectionCreate(), PetscSectionSetPermutation()
3278552f7358SJed Brown @*/
3279ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSection(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], IS perm, PetscSection *section)
3280a6dfd86eSKarl Rupp {
3281a68b90caSToby Isaac   PetscSection   aSec;
3282552f7358SJed Brown   PetscErrorCode ierr;
3283552f7358SJed Brown 
3284552f7358SJed Brown   PetscFunctionBegin;
3285552f7358SJed Brown   ierr = DMPlexCreateSectionInitial(dm, dim, numFields, numComp, numDof, section);CHKERRQ(ierr);
3286ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSectionBCDof(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3287f8f126e8SMatthew G. Knepley   if (perm) {ierr = PetscSectionSetPermutation(*section, perm);CHKERRQ(ierr);}
3288552f7358SJed Brown   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
3289a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,NULL);CHKERRQ(ierr);
3290ab1d0545SMatthew G. Knepley   if (numBC || aSec) {
3291ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndicesField(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3292ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndices(dm, *section);CHKERRQ(ierr);
3293ab1d0545SMatthew G. Knepley   }
3294ce1779c8SBarry Smith   ierr = PetscSectionViewFromOptions(*section,NULL,"-section_view");CHKERRQ(ierr);
3295552f7358SJed Brown   PetscFunctionReturn(0);
3296552f7358SJed Brown }
3297552f7358SJed Brown 
3298552f7358SJed Brown #undef __FUNCT__
3299552f7358SJed Brown #define __FUNCT__ "DMCreateCoordinateDM_Plex"
33000adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
33010adebc6cSBarry Smith {
3302efe440bfSMatthew G. Knepley   PetscSection   section, s;
3303efe440bfSMatthew G. Knepley   Mat            m;
33043e922f36SToby Isaac   PetscInt       maxHeight;
3305552f7358SJed Brown   PetscErrorCode ierr;
3306552f7358SJed Brown 
3307552f7358SJed Brown   PetscFunctionBegin;
330838221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
33093e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
33103e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
331182f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
3312552f7358SJed Brown   ierr = DMSetDefaultSection(*cdm, section);CHKERRQ(ierr);
33131d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3314efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3315efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3316efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3317efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3318efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
3319552f7358SJed Brown   PetscFunctionReturn(0);
3320552f7358SJed Brown }
3321552f7358SJed Brown 
3322552f7358SJed Brown #undef __FUNCT__
3323552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeSection"
33240adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
33250adebc6cSBarry Smith {
3326552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3327552f7358SJed Brown 
3328552f7358SJed Brown   PetscFunctionBegin;
3329552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3330552f7358SJed Brown   if (section) *section = mesh->coneSection;
3331552f7358SJed Brown   PetscFunctionReturn(0);
3332552f7358SJed Brown }
3333552f7358SJed Brown 
3334552f7358SJed Brown #undef __FUNCT__
33358cb4d582SMatthew G. Knepley #define __FUNCT__ "DMPlexGetSupportSection"
33368cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
33378cb4d582SMatthew G. Knepley {
33388cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
33398cb4d582SMatthew G. Knepley 
33408cb4d582SMatthew G. Knepley   PetscFunctionBegin;
33418cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33428cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
33438cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
33448cb4d582SMatthew G. Knepley }
33458cb4d582SMatthew G. Knepley 
33468cb4d582SMatthew G. Knepley #undef __FUNCT__
3347552f7358SJed Brown #define __FUNCT__ "DMPlexGetCones"
3348a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3349a6dfd86eSKarl Rupp {
3350552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3351552f7358SJed Brown 
3352552f7358SJed Brown   PetscFunctionBegin;
3353552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3354552f7358SJed Brown   if (cones) *cones = mesh->cones;
3355552f7358SJed Brown   PetscFunctionReturn(0);
3356552f7358SJed Brown }
3357552f7358SJed Brown 
3358552f7358SJed Brown #undef __FUNCT__
3359552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeOrientations"
3360a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3361a6dfd86eSKarl Rupp {
3362552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3363552f7358SJed Brown 
3364552f7358SJed Brown   PetscFunctionBegin;
3365552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3366552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3367552f7358SJed Brown   PetscFunctionReturn(0);
3368552f7358SJed Brown }
3369552f7358SJed Brown 
3370552f7358SJed Brown /******************************** FEM Support **********************************/
3371552f7358SJed Brown 
3372552f7358SJed Brown #undef __FUNCT__
33733194fc30SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateSpectralClosurePermutation"
33743194fc30SMatthew G. Knepley PetscErrorCode DMPlexCreateSpectralClosurePermutation(DM dm, PetscSection section)
33753194fc30SMatthew G. Knepley {
33763194fc30SMatthew G. Knepley   PetscInt      *perm;
337789eabcffSMatthew G. Knepley   PetscInt       dim, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
33783194fc30SMatthew G. Knepley   PetscErrorCode ierr;
33793194fc30SMatthew G. Knepley 
33803194fc30SMatthew G. Knepley   PetscFunctionBegin;
33813194fc30SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
33823194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
33833194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
338489eabcffSMatthew G. Knepley   if (dim <= 1) PetscFunctionReturn(0);
33853194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
33863194fc30SMatthew G. Knepley     /* An order k SEM disc has k-1 dofs on an edge */
33873194fc30SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
33883194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
33893194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
33903194fc30SMatthew G. Knepley     k = k/Nc + 1;
339189eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
33923194fc30SMatthew G. Knepley   }
33933194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
33943194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
339589eabcffSMatthew G. Knepley     switch (dim) {
339689eabcffSMatthew G. Knepley     case 2:
33973194fc30SMatthew G. Knepley       /* The original quad closure is oriented clockwise, {f, e_b, e_r, e_t, e_l, v_lb, v_rb, v_tr, v_tl} */
33983194fc30SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
33993194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
34003194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
34013194fc30SMatthew G. Knepley       k = k/Nc + 1;
34023194fc30SMatthew G. Knepley       /* The SEM order is
34033194fc30SMatthew G. Knepley 
34043194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
340589eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
34063194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
34073194fc30SMatthew G. Knepley       */
34083194fc30SMatthew G. Knepley       {
34093194fc30SMatthew G. Knepley         const PetscInt of   = 0;
34103194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
34113194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
34123194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
34133194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
34143194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
34153194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
34163194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
34173194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
34183194fc30SMatthew G. Knepley         PetscInt       o;
34193194fc30SMatthew G. Knepley 
34203194fc30SMatthew G. Knepley         /* bottom */
34213194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
34223194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
34233194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
34243194fc30SMatthew G. Knepley         /* middle */
34253194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
34263194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
34273194fc30SMatthew G. Knepley           for (o = of+(k-1)*i; o < of+(k-1)*(i+1); ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
34283194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
34293194fc30SMatthew G. Knepley         }
34303194fc30SMatthew G. Knepley         /* top */
34313194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
34323194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
34333194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
34343194fc30SMatthew G. Knepley         foffset = offset;
34353194fc30SMatthew G. Knepley       }
343689eabcffSMatthew G. Knepley       break;
343789eabcffSMatthew G. Knepley     case 3:
343889eabcffSMatthew G. Knepley       /* The original hex closure is
343989eabcffSMatthew G. Knepley 
344089eabcffSMatthew G. Knepley          {c,
344189eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
344289eabcffSMatthew G. Knepley           e_bl, e_bb, e_br, e_bf,  e_tf, e_tr, e_tb, e_tl,  e_rf, e_lf, e_lb, e_rb,
344389eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
344489eabcffSMatthew G. Knepley       */
344589eabcffSMatthew G. Knepley       ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
344689eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
344789eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
344889eabcffSMatthew G. Knepley       k = k/Nc + 1;
344989eabcffSMatthew G. Knepley       /* The SEM order is
345089eabcffSMatthew G. Knepley          Bottom Slice
345189eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
345289eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
345389eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
345489eabcffSMatthew G. Knepley 
345589eabcffSMatthew G. Knepley          Middle Slice (j)
345689eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
345789eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
345889eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
345989eabcffSMatthew G. Knepley 
346089eabcffSMatthew G. Knepley          Top Slice
346189eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
346289eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
346389eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
346489eabcffSMatthew G. Knepley       */
346589eabcffSMatthew G. Knepley       {
346689eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
346789eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
346889eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
346989eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
347089eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
347189eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
347289eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
347389eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
347489eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
347589eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
347689eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
347789eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
347889eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
347989eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
348089eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
348189eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
348289eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
348389eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
348489eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
348589eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
348689eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
348789eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
348889eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
348989eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
349089eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
349189eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
349289eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
349389eabcffSMatthew G. Knepley         PetscInt       o, n;
349489eabcffSMatthew G. Knepley 
349589eabcffSMatthew G. Knepley         /* Bottom Slice */
349689eabcffSMatthew G. Knepley         /*   bottom */
349789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
349889eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
349989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
350089eabcffSMatthew G. Knepley         /*   middle */
350189eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
350289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
3503316b7f87SMax Rietmann           for (n = 0; n < k-1; ++n) {o = ofb+n*(k-1)+i; for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;}
350489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
35053194fc30SMatthew G. Knepley         }
350689eabcffSMatthew G. Knepley         /*   top */
350789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
350889eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
350989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
351089eabcffSMatthew G. Knepley 
351189eabcffSMatthew G. Knepley         /* Middle Slice */
351289eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
351389eabcffSMatthew G. Knepley           /*   bottom */
351489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
351589eabcffSMatthew G. Knepley           for (o = off+j*(k-1); o < off+(j+1)*(k-1); ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
351689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
351789eabcffSMatthew G. Knepley           /*   middle */
351889eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
351989eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
352089eabcffSMatthew G. Knepley             for (n = 0; n < k-1; ++n) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oc+(j*(k-1)+i)*(k-1)+n)*Nc + c + foffset;
352189eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
352289eabcffSMatthew G. Knepley           }
352389eabcffSMatthew G. Knepley           /*   top */
352489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
352589eabcffSMatthew G. Knepley           for (o = ofk+j*(k-1)+(k-2); o >= ofk+j*(k-1); --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
352689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
352789eabcffSMatthew G. Knepley         }
352889eabcffSMatthew G. Knepley 
352989eabcffSMatthew G. Knepley         /* Top Slice */
353089eabcffSMatthew G. Knepley         /*   bottom */
353189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
353289eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
353389eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
353489eabcffSMatthew G. Knepley         /*   middle */
353589eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
353689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
353789eabcffSMatthew G. Knepley           for (n = 0; n < k-1; ++n) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oft+i*(k-1)+n)*Nc + c + foffset;
353889eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
353989eabcffSMatthew G. Knepley         }
354089eabcffSMatthew G. Knepley         /*   top */
354189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
354289eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
354389eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
354489eabcffSMatthew G. Knepley 
354589eabcffSMatthew G. Knepley         foffset = offset;
354689eabcffSMatthew G. Knepley       }
354789eabcffSMatthew G. Knepley       break;
354889eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
354989eabcffSMatthew G. Knepley     }
355089eabcffSMatthew G. Knepley   }
355189eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
35523194fc30SMatthew G. Knepley   /* Check permutation */
35533194fc30SMatthew G. Knepley   {
35543194fc30SMatthew G. Knepley     PetscInt *check;
35553194fc30SMatthew G. Knepley 
35563194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
35573194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {check[i] = -1; if (perm[i] < 0 || perm[i] >= size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid permutation index p[%D] = %D", i, perm[i]);}
35583194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
35593194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
35603194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
35613194fc30SMatthew G. Knepley   }
35621fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
35633194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
35643194fc30SMatthew G. Knepley }
35653194fc30SMatthew G. Knepley 
35663194fc30SMatthew G. Knepley #undef __FUNCT__
3567e071409bSToby Isaac #define __FUNCT__ "DMPlexGetPointDualSpaceFEM"
3568e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
3569e071409bSToby Isaac {
3570e071409bSToby Isaac   PetscDS        prob;
3571e071409bSToby Isaac   PetscInt       depth, Nf, h;
3572e071409bSToby Isaac   DMLabel        label;
3573e071409bSToby Isaac   PetscErrorCode ierr;
3574e071409bSToby Isaac 
3575e071409bSToby Isaac   PetscFunctionBeginHot;
3576e071409bSToby Isaac   prob    = dm->prob;
3577e071409bSToby Isaac   Nf      = prob->Nf;
3578e071409bSToby Isaac   label   = dm->depthLabel;
3579e071409bSToby Isaac   *dspace = NULL;
3580e071409bSToby Isaac   if (field < Nf) {
3581e071409bSToby Isaac     PetscObject disc = prob->disc[field];
3582e071409bSToby Isaac 
3583e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
3584e071409bSToby Isaac       PetscDualSpace dsp;
3585e071409bSToby Isaac 
3586e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
3587e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
3588e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
3589e071409bSToby Isaac       h    = depth - 1 - h;
3590e071409bSToby Isaac       if (h) {
3591e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
3592e071409bSToby Isaac       } else {
3593e071409bSToby Isaac         *dspace = dsp;
3594e071409bSToby Isaac       }
3595e071409bSToby Isaac     }
3596e071409bSToby Isaac   }
3597e071409bSToby Isaac   PetscFunctionReturn(0);
3598e071409bSToby Isaac }
3599e071409bSToby Isaac 
3600e071409bSToby Isaac 
3601e071409bSToby Isaac #undef __FUNCT__
36021a271a75SMatthew G. Knepley #define __FUNCT__ "DMPlexVecGetClosure_Depth1_Static"
36031a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3604a6dfd86eSKarl Rupp {
3605552f7358SJed Brown   PetscScalar    *array, *vArray;
3606d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
36071a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
3608552f7358SJed Brown   PetscErrorCode  ierr;
3609552f7358SJed Brown 
36101b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
36112a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
36125a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
36135a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
36145a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
36153f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
36169df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
36179df71ca4SMatthew G. Knepley       PetscInt dof;
3618d9917b9dSMatthew G. Knepley 
36199df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
36209df71ca4SMatthew G. Knepley       size += dof;
36219df71ca4SMatthew G. Knepley     }
36229df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
36239df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
36242a3aaacfSMatthew G. Knepley       PetscInt       dof;
36255a1bb5cfSMatthew G. Knepley 
36265a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
36272a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
36285a1bb5cfSMatthew G. Knepley       size += dof;
36295a1bb5cfSMatthew G. Knepley     }
36303f7cbbe7SMatthew G. Knepley     if (!values) {
36313f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
36323f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
36333f7cbbe7SMatthew G. Knepley     }
36345a1bb5cfSMatthew G. Knepley     ierr = DMGetWorkArray(dm, size, PETSC_SCALAR, &array);CHKERRQ(ierr);
3635982e9ed1SMatthew G. Knepley   } else {
3636982e9ed1SMatthew G. Knepley     array = *values;
3637982e9ed1SMatthew G. Knepley   }
36389df71ca4SMatthew G. Knepley   size = 0;
36395a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
36409df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
36419df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
36429df71ca4SMatthew G. Knepley     PetscScalar *varr;
3643d9917b9dSMatthew G. Knepley 
36449df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
36459df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
36469df71ca4SMatthew G. Knepley     varr = &vArray[off];
36471a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
36481a271a75SMatthew G. Knepley       array[offset] = varr[d];
36499df71ca4SMatthew G. Knepley     }
36509df71ca4SMatthew G. Knepley     size += dof;
36519df71ca4SMatthew G. Knepley   }
36529df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
36539df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
36549df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
36555a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
36565a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
36575a1bb5cfSMatthew G. Knepley 
365852ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
36595a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
36605a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
36615a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
36625a1bb5cfSMatthew G. Knepley     if (o >= 0) {
36631a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
36641a271a75SMatthew G. Knepley         array[offset] = varr[d];
36655a1bb5cfSMatthew G. Knepley       }
36665a1bb5cfSMatthew G. Knepley     } else {
36671a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
36681a271a75SMatthew G. Knepley         array[offset] = varr[d];
36695a1bb5cfSMatthew G. Knepley       }
36705a1bb5cfSMatthew G. Knepley     }
36719df71ca4SMatthew G. Knepley     size += dof;
36725a1bb5cfSMatthew G. Knepley   }
36735a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
36749df71ca4SMatthew G. Knepley   if (!*values) {
36755a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
36765a1bb5cfSMatthew G. Knepley     *values = array;
36779df71ca4SMatthew G. Knepley   } else {
36788ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
36798c312ff3SMatthew G. Knepley     *csize = size;
36809df71ca4SMatthew G. Knepley   }
36815a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
36825a1bb5cfSMatthew G. Knepley }
3683d9917b9dSMatthew G. Knepley 
3684d9917b9dSMatthew G. Knepley #undef __FUNCT__
3685923c78e0SToby Isaac #define __FUNCT__ "DMPlexGetCompressedClosure"
3686923c78e0SToby Isaac static PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3687923c78e0SToby Isaac {
3688923c78e0SToby Isaac   const PetscInt *cla;
3689923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
3690923c78e0SToby Isaac   PetscErrorCode ierr;
3691923c78e0SToby Isaac 
3692923c78e0SToby Isaac   PetscFunctionBeginHot;
3693923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
3694923c78e0SToby Isaac   if (!*clPoints) {
3695923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
3696923c78e0SToby Isaac 
3697923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3698923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
3699923c78e0SToby Isaac     /* Compress out points not in the section */
3700923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
3701923c78e0SToby Isaac       PetscInt r = pts[2*p];
3702923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
3703923c78e0SToby Isaac         pts[q*2]   = r;
3704923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
3705923c78e0SToby Isaac         ++q;
3706923c78e0SToby Isaac       }
3707923c78e0SToby Isaac     }
3708923c78e0SToby Isaac     np = q;
3709923c78e0SToby Isaac     cla = NULL;
3710923c78e0SToby Isaac   } else {
3711923c78e0SToby Isaac     PetscInt dof, off;
3712923c78e0SToby Isaac 
3713923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
3714923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
3715923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
3716923c78e0SToby Isaac     np   = dof/2;
3717923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
3718923c78e0SToby Isaac   }
3719923c78e0SToby Isaac   *numPoints = np;
3720923c78e0SToby Isaac   *points    = pts;
3721923c78e0SToby Isaac   *clp       = cla;
3722923c78e0SToby Isaac 
3723923c78e0SToby Isaac   PetscFunctionReturn(0);
3724923c78e0SToby Isaac }
3725923c78e0SToby Isaac 
3726923c78e0SToby Isaac #undef __FUNCT__
3727923c78e0SToby Isaac #define __FUNCT__ "DMPlexRestoreCompressedClosure"
3728923c78e0SToby Isaac static PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3729923c78e0SToby Isaac {
3730923c78e0SToby Isaac   PetscErrorCode ierr;
3731923c78e0SToby Isaac 
3732923c78e0SToby Isaac   PetscFunctionBeginHot;
3733923c78e0SToby Isaac   if (!*clPoints) {
3734923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
3735923c78e0SToby Isaac   } else {
3736923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
3737923c78e0SToby Isaac   }
3738923c78e0SToby Isaac   *numPoints = 0;
3739923c78e0SToby Isaac   *points    = NULL;
3740923c78e0SToby Isaac   *clSec     = NULL;
3741923c78e0SToby Isaac   *clPoints  = NULL;
3742923c78e0SToby Isaac   *clp       = NULL;
3743923c78e0SToby Isaac   PetscFunctionReturn(0);
3744923c78e0SToby Isaac }
3745923c78e0SToby Isaac 
3746923c78e0SToby Isaac #undef __FUNCT__
37471a271a75SMatthew G. Knepley #define __FUNCT__ "DMPlexVecGetClosure_Static"
374897e99dd9SToby Isaac PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Static(DM dm, PetscSection section, PetscInt numPoints, const PetscInt points[], const PetscInt clperm[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
37491a271a75SMatthew G. Knepley {
37501a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
375197e99dd9SToby Isaac   const PetscInt    **perms = NULL;
375297e99dd9SToby Isaac   const PetscScalar **flips = NULL;
37531a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
37541a271a75SMatthew G. Knepley 
37551a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3756fe02ba77SJed Brown   *size = 0;
375797e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
375897e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
375997e99dd9SToby Isaac     const PetscInt    point = points[2*p];
376097e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
376197e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
37621a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
37631a271a75SMatthew G. Knepley     const PetscScalar *varr;
37641a271a75SMatthew G. Knepley 
37651a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
37661a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
37671a271a75SMatthew G. Knepley     varr = &vArray[off];
376897e99dd9SToby Isaac     if (clperm) {
376997e99dd9SToby Isaac       if (perm) {
377097e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
37711a271a75SMatthew G. Knepley       } else {
377297e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
377397e99dd9SToby Isaac       }
377497e99dd9SToby Isaac       if (flip) {
377597e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
377697e99dd9SToby Isaac       }
377797e99dd9SToby Isaac     } else {
377897e99dd9SToby Isaac       if (perm) {
377997e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
378097e99dd9SToby Isaac       } else {
378197e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
378297e99dd9SToby Isaac       }
378397e99dd9SToby Isaac       if (flip) {
378497e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
37851a271a75SMatthew G. Knepley       }
37861a271a75SMatthew G. Knepley     }
378797e99dd9SToby Isaac     offset += dof;
378897e99dd9SToby Isaac   }
378997e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
37901a271a75SMatthew G. Knepley   *size = offset;
37911a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
37921a271a75SMatthew G. Knepley }
37931a271a75SMatthew G. Knepley 
37941a271a75SMatthew G. Knepley #undef __FUNCT__
37951a271a75SMatthew G. Knepley #define __FUNCT__ "DMPlexVecGetClosure_Fields_Static"
379697e99dd9SToby Isaac PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Fields_Static(DM dm, PetscSection section, PetscInt numPoints, const PetscInt points[], PetscInt numFields, const PetscInt clperm[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
37971a271a75SMatthew G. Knepley {
37981a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
37991a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
38001a271a75SMatthew G. Knepley 
38011a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3802fe02ba77SJed Brown   *size = 0;
38031a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
380497e99dd9SToby Isaac     PetscInt          p;
380597e99dd9SToby Isaac     const PetscInt    **perms = NULL;
380697e99dd9SToby Isaac     const PetscScalar **flips = NULL;
38071a271a75SMatthew G. Knepley 
380897e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
380997e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
381097e99dd9SToby Isaac       const PetscInt    point = points[2*p];
381197e99dd9SToby Isaac       PetscInt          fdof, foff, b;
38121a271a75SMatthew G. Knepley       const PetscScalar *varr;
381397e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
381497e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
38151a271a75SMatthew G. Knepley 
38161a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
38171a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
38181a271a75SMatthew G. Knepley       varr = &vArray[foff];
381997e99dd9SToby Isaac       if (clperm) {
382097e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
382197e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
382297e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
38231a271a75SMatthew G. Knepley       } else {
382497e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
382597e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
382697e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
38271a271a75SMatthew G. Knepley       }
382897e99dd9SToby Isaac       offset += fdof;
38291a271a75SMatthew G. Knepley     }
383097e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
38311a271a75SMatthew G. Knepley   }
38321a271a75SMatthew G. Knepley   *size = offset;
38331a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
38341a271a75SMatthew G. Knepley }
38351a271a75SMatthew G. Knepley 
38361a271a75SMatthew G. Knepley #undef __FUNCT__
3837552f7358SJed Brown #define __FUNCT__ "DMPlexVecGetClosure"
3838552f7358SJed Brown /*@C
3839552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
3840552f7358SJed Brown 
3841552f7358SJed Brown   Not collective
3842552f7358SJed Brown 
3843552f7358SJed Brown   Input Parameters:
3844552f7358SJed Brown + dm - The DM
3845552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
3846552f7358SJed Brown . v - The local vector
3847552f7358SJed Brown - point - The sieve point in the DM
3848552f7358SJed Brown 
3849552f7358SJed Brown   Output Parameters:
3850552f7358SJed Brown + csize - The number of values in the closure, or NULL
3851552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
3852552f7358SJed Brown 
3853552f7358SJed Brown   Fortran Notes:
3854552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
3855552f7358SJed Brown   include petsc.h90 in your code.
3856552f7358SJed Brown 
3857552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
3858552f7358SJed Brown 
3859552f7358SJed Brown   Level: intermediate
3860552f7358SJed Brown 
3861552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
3862552f7358SJed Brown @*/
3863552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3864552f7358SJed Brown {
3865552f7358SJed Brown   PetscSection       clSection;
3866d9917b9dSMatthew G. Knepley   IS                 clPoints;
3867*8a84db2dSMatthew G. Knepley   PetscScalar       *array;
3868*8a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
3869552f7358SJed Brown   PetscInt          *points = NULL;
38708f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
38711a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
3872552f7358SJed Brown   PetscErrorCode     ierr;
3873552f7358SJed Brown 
3874d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
3875552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3876552f7358SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
38771a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
38781a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
3879552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3880552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3881552f7358SJed Brown   if (depth == 1 && numFields < 2) {
38821a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
3883552f7358SJed Brown     PetscFunctionReturn(0);
3884552f7358SJed Brown   }
38851a271a75SMatthew G. Knepley   /* Get points */
3886923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
38871fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
38881a271a75SMatthew G. Knepley   /* Get array */
3889bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
38901a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
3891552f7358SJed Brown 
38921a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
3893552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
38941a271a75SMatthew G. Knepley       asize += dof;
3895552f7358SJed Brown     }
3896bd6c0d32SMatthew G. Knepley     if (!values) {
3897923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
38981a271a75SMatthew G. Knepley       if (csize) *csize = asize;
3899bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
3900bd6c0d32SMatthew G. Knepley     }
39011a271a75SMatthew G. Knepley     ierr = DMGetWorkArray(dm, asize, PETSC_SCALAR, &array);CHKERRQ(ierr);
3902d0f6b257SMatthew G. Knepley   } else {
3903d0f6b257SMatthew G. Knepley     array = *values;
3904d0f6b257SMatthew G. Knepley   }
3905*8a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
39061a271a75SMatthew G. Knepley   /* Get values */
390797e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
390897e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
39091a271a75SMatthew G. Knepley   /* Cleanup points */
3910923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
39111a271a75SMatthew G. Knepley   /* Cleanup array */
3912*8a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
3913d0f6b257SMatthew G. Knepley   if (!*values) {
3914552f7358SJed Brown     if (csize) *csize = size;
3915552f7358SJed Brown     *values = array;
3916d0f6b257SMatthew G. Knepley   } else {
3917f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
3918d0f6b257SMatthew G. Knepley     *csize = size;
3919d0f6b257SMatthew G. Knepley   }
3920552f7358SJed Brown   PetscFunctionReturn(0);
3921552f7358SJed Brown }
3922552f7358SJed Brown 
3923552f7358SJed Brown #undef __FUNCT__
3924552f7358SJed Brown #define __FUNCT__ "DMPlexVecRestoreClosure"
3925552f7358SJed Brown /*@C
3926552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
3927552f7358SJed Brown 
3928552f7358SJed Brown   Not collective
3929552f7358SJed Brown 
3930552f7358SJed Brown   Input Parameters:
3931552f7358SJed Brown + dm - The DM
39320298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
3933552f7358SJed Brown . v - The local vector
3934552f7358SJed Brown . point - The sieve point in the DM
39350298fd71SBarry Smith . csize - The number of values in the closure, or NULL
3936552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
3937552f7358SJed Brown 
39383813dfbdSMatthew G Knepley   Fortran Notes:
39393813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
39403813dfbdSMatthew G Knepley   include petsc.h90 in your code.
39413813dfbdSMatthew G Knepley 
39423813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
39433813dfbdSMatthew G Knepley 
3944552f7358SJed Brown   Level: intermediate
3945552f7358SJed Brown 
3946552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
3947552f7358SJed Brown @*/
39487c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3949a6dfd86eSKarl Rupp {
3950552f7358SJed Brown   PetscInt       size = 0;
3951552f7358SJed Brown   PetscErrorCode ierr;
3952552f7358SJed Brown 
3953552f7358SJed Brown   PetscFunctionBegin;
3954552f7358SJed Brown   /* Should work without recalculating size */
3955552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, size, PETSC_SCALAR, (void*) values);CHKERRQ(ierr);
3956552f7358SJed Brown   PetscFunctionReturn(0);
3957552f7358SJed Brown }
3958552f7358SJed Brown 
3959552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
3960552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
3961552f7358SJed Brown 
3962552f7358SJed Brown #undef __FUNCT__
3963552f7358SJed Brown #define __FUNCT__ "updatePoint_private"
396497e99dd9SToby Isaac PETSC_STATIC_INLINE PetscErrorCode updatePoint_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar*, PetscScalar), PetscBool setBC, const PetscInt perm[], const PetscScalar flip[], const PetscInt clperm[], const PetscScalar values[], PetscInt offset, PetscScalar array[])
3965552f7358SJed Brown {
3966552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
3967552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
3968552f7358SJed Brown   PetscScalar    *a;
3969552f7358SJed Brown   PetscInt        off, cind = 0, k;
3970552f7358SJed Brown   PetscErrorCode  ierr;
3971552f7358SJed Brown 
3972552f7358SJed Brown   PetscFunctionBegin;
3973552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
3974552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
3975552f7358SJed Brown   a    = &array[off];
3976552f7358SJed Brown   if (!cdof || setBC) {
397797e99dd9SToby Isaac     if (clperm) {
397897e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
397997e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
3980552f7358SJed Brown     } else {
398197e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
398297e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
3983552f7358SJed Brown     }
3984552f7358SJed Brown   } else {
3985552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
398697e99dd9SToby Isaac     if (clperm) {
398797e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
3988552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
398997e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
3990552f7358SJed Brown         }
3991552f7358SJed Brown       } else {
3992552f7358SJed Brown         for (k = 0; k < dof; ++k) {
3993552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
399497e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
399597e99dd9SToby Isaac         }
399697e99dd9SToby Isaac       }
399797e99dd9SToby Isaac     } else {
399897e99dd9SToby Isaac       if (perm) {
399997e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
400097e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
400197e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
400297e99dd9SToby Isaac         }
400397e99dd9SToby Isaac       } else {
400497e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
400597e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
400697e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
400797e99dd9SToby Isaac         }
4008552f7358SJed Brown       }
4009552f7358SJed Brown     }
4010552f7358SJed Brown   }
4011552f7358SJed Brown   PetscFunctionReturn(0);
4012552f7358SJed Brown }
4013552f7358SJed Brown 
4014552f7358SJed Brown #undef __FUNCT__
4015a5e93ea8SMatthew G. Knepley #define __FUNCT__ "updatePointBC_private"
401697e99dd9SToby Isaac PETSC_STATIC_INLINE PetscErrorCode updatePointBC_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar*, PetscScalar), const PetscInt perm[], const PetscScalar flip[], const PetscInt clperm[], const PetscScalar values[], PetscInt offset, PetscScalar array[])
4017a5e93ea8SMatthew G. Knepley {
4018a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
4019a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4020a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
4021a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
4022a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4023a5e93ea8SMatthew G. Knepley 
4024a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4025a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4026a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4027a5e93ea8SMatthew G. Knepley   a    = &array[off];
4028a5e93ea8SMatthew G. Knepley   if (cdof) {
4029a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
403097e99dd9SToby Isaac     if (clperm) {
403197e99dd9SToby Isaac       if (perm) {
4032a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4033a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
403497e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
403597e99dd9SToby Isaac             cind++;
4036a5e93ea8SMatthew G. Knepley           }
4037a5e93ea8SMatthew G. Knepley         }
4038a5e93ea8SMatthew G. Knepley       } else {
4039a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4040a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
404197e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
404297e99dd9SToby Isaac             cind++;
404397e99dd9SToby Isaac           }
404497e99dd9SToby Isaac         }
404597e99dd9SToby Isaac       }
404697e99dd9SToby Isaac     } else {
404797e99dd9SToby Isaac       if (perm) {
404897e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
404997e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
405097e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
405197e99dd9SToby Isaac             cind++;
405297e99dd9SToby Isaac           }
405397e99dd9SToby Isaac         }
405497e99dd9SToby Isaac       } else {
405597e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
405697e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
405797e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
405897e99dd9SToby Isaac             cind++;
405997e99dd9SToby Isaac           }
4060a5e93ea8SMatthew G. Knepley         }
4061a5e93ea8SMatthew G. Knepley       }
4062a5e93ea8SMatthew G. Knepley     }
4063a5e93ea8SMatthew G. Knepley   }
4064a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4065a5e93ea8SMatthew G. Knepley }
4066a5e93ea8SMatthew G. Knepley 
4067a5e93ea8SMatthew G. Knepley #undef __FUNCT__
4068552f7358SJed Brown #define __FUNCT__ "updatePointFields_private"
406997e99dd9SToby Isaac PETSC_STATIC_INLINE PetscErrorCode updatePointFields_private(PetscSection section, PetscInt point, const PetscInt *perm, const PetscScalar *flip, PetscInt f, void (*fuse)(PetscScalar*, PetscScalar), PetscBool setBC, const PetscInt clperm[], const PetscScalar values[], PetscInt *offset, PetscScalar array[])
4070a6dfd86eSKarl Rupp {
4071552f7358SJed Brown   PetscScalar    *a;
40721a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
40731a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
407497e99dd9SToby Isaac   PetscInt        cind = 0, b;
4075552f7358SJed Brown   PetscErrorCode  ierr;
4076552f7358SJed Brown 
4077552f7358SJed Brown   PetscFunctionBegin;
4078552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4079552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
40801a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
40811a271a75SMatthew G. Knepley   a    = &array[foff];
4082552f7358SJed Brown   if (!fcdof || setBC) {
408397e99dd9SToby Isaac     if (clperm) {
408497e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
408597e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4086552f7358SJed Brown     } else {
408797e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
408897e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4089552f7358SJed Brown     }
4090552f7358SJed Brown   } else {
4091552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
409297e99dd9SToby Isaac     if (clperm) {
409397e99dd9SToby Isaac       if (perm) {
409497e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
409597e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
409697e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4097552f7358SJed Brown         }
4098552f7358SJed Brown       } else {
409997e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
410097e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
410197e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
410297e99dd9SToby Isaac         }
410397e99dd9SToby Isaac       }
410497e99dd9SToby Isaac     } else {
410597e99dd9SToby Isaac       if (perm) {
410697e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
410797e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
410897e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
410997e99dd9SToby Isaac         }
411097e99dd9SToby Isaac       } else {
411197e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
411297e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
411397e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4114552f7358SJed Brown         }
4115552f7358SJed Brown       }
4116552f7358SJed Brown     }
4117552f7358SJed Brown   }
41181a271a75SMatthew G. Knepley   *offset += fdof;
4119552f7358SJed Brown   PetscFunctionReturn(0);
4120552f7358SJed Brown }
4121552f7358SJed Brown 
4122552f7358SJed Brown #undef __FUNCT__
4123a5e93ea8SMatthew G. Knepley #define __FUNCT__ "updatePointFieldsBC_private"
412497e99dd9SToby Isaac PETSC_STATIC_INLINE PetscErrorCode updatePointFieldsBC_private(PetscSection section, PetscInt point, const PetscInt perm[], const PetscScalar flip[], PetscInt f, void (*fuse)(PetscScalar*, PetscScalar), const PetscInt clperm[], const PetscScalar values[], PetscInt *offset, PetscScalar array[])
4125a5e93ea8SMatthew G. Knepley {
4126a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
41271a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
41281a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
412997e99dd9SToby Isaac   PetscInt        cind = 0, b;
4130a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4131a5e93ea8SMatthew G. Knepley 
4132a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4133a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4134a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
41351a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
41361a271a75SMatthew G. Knepley   a    = &array[foff];
4137a5e93ea8SMatthew G. Knepley   if (fcdof) {
4138a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
413997e99dd9SToby Isaac     if (clperm) {
414097e99dd9SToby Isaac       if (perm) {
414197e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
414297e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
414397e99dd9SToby Isaac             fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4144a5e93ea8SMatthew G. Knepley             ++cind;
4145a5e93ea8SMatthew G. Knepley           }
4146a5e93ea8SMatthew G. Knepley         }
4147a5e93ea8SMatthew G. Knepley       } else {
414897e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
414997e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
415097e99dd9SToby Isaac             fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
415197e99dd9SToby Isaac             ++cind;
415297e99dd9SToby Isaac           }
415397e99dd9SToby Isaac         }
415497e99dd9SToby Isaac       }
415597e99dd9SToby Isaac     } else {
415697e99dd9SToby Isaac       if (perm) {
415797e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
415897e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
415997e99dd9SToby Isaac             fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
416097e99dd9SToby Isaac             ++cind;
416197e99dd9SToby Isaac           }
416297e99dd9SToby Isaac         }
416397e99dd9SToby Isaac       } else {
416497e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
416597e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
416697e99dd9SToby Isaac             fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4167a5e93ea8SMatthew G. Knepley             ++cind;
4168a5e93ea8SMatthew G. Knepley           }
4169a5e93ea8SMatthew G. Knepley         }
4170a5e93ea8SMatthew G. Knepley       }
4171a5e93ea8SMatthew G. Knepley     }
4172a5e93ea8SMatthew G. Knepley   }
41731a271a75SMatthew G. Knepley   *offset += fdof;
4174a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4175a5e93ea8SMatthew G. Knepley }
4176a5e93ea8SMatthew G. Knepley 
4177a5e93ea8SMatthew G. Knepley #undef __FUNCT__
41788f3be42fSMatthew G. Knepley #define __FUNCT__ "DMPlexVecSetClosure_Depth1_Static"
41798f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4180a6dfd86eSKarl Rupp {
4181552f7358SJed Brown   PetscScalar    *array;
41821b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
41831b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4184552f7358SJed Brown   PetscErrorCode  ierr;
4185552f7358SJed Brown 
41861b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
4187b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4188b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
4189b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4190b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
4191b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4192b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4193b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
4194b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
4195b6ebb6e6SMatthew G. Knepley 
4196b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4197b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
4198b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
4199b6ebb6e6SMatthew G. Knepley     {
4200b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4201b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
4202b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
4203b6ebb6e6SMatthew G. Knepley 
4204b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
4205b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
4206b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
4207b6ebb6e6SMatthew G. Knepley       if (!cdof) {
4208b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4209b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4210b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4211b6ebb6e6SMatthew G. Knepley           }
4212b6ebb6e6SMatthew G. Knepley         } else {
4213b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4214b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4215b6ebb6e6SMatthew G. Knepley           }
4216b6ebb6e6SMatthew G. Knepley         }
4217b6ebb6e6SMatthew G. Knepley       } else {
4218b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
4219b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4220b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4221b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4222b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4223b6ebb6e6SMatthew G. Knepley           }
4224b6ebb6e6SMatthew G. Knepley         } else {
4225b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4226b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4227b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4228b6ebb6e6SMatthew G. Knepley           }
4229b6ebb6e6SMatthew G. Knepley         }
4230b6ebb6e6SMatthew G. Knepley       }
4231b6ebb6e6SMatthew G. Knepley     }
4232b6ebb6e6SMatthew G. Knepley   }
4233b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4234b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4235b6ebb6e6SMatthew G. Knepley }
42361b406b76SMatthew G. Knepley 
42371b406b76SMatthew G. Knepley #undef __FUNCT__
42381b406b76SMatthew G. Knepley #define __FUNCT__ "DMPlexVecSetClosure"
42391b406b76SMatthew G. Knepley /*@C
42401b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
42411b406b76SMatthew G. Knepley 
42421b406b76SMatthew G. Knepley   Not collective
42431b406b76SMatthew G. Knepley 
42441b406b76SMatthew G. Knepley   Input Parameters:
42451b406b76SMatthew G. Knepley + dm - The DM
42461b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
42471b406b76SMatthew G. Knepley . v - The local vector
42481b406b76SMatthew G. Knepley . point - The sieve point in the DM
42491b406b76SMatthew G. Knepley . values - The array of values
42501b406b76SMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
42511b406b76SMatthew G. Knepley 
42521b406b76SMatthew G. Knepley   Fortran Notes:
42531b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
42541b406b76SMatthew G. Knepley 
42551b406b76SMatthew G. Knepley   Level: intermediate
42561b406b76SMatthew G. Knepley 
42571b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
42581b406b76SMatthew G. Knepley @*/
42591b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
42601b406b76SMatthew G. Knepley {
42611b406b76SMatthew G. Knepley   PetscSection    clSection;
42621b406b76SMatthew G. Knepley   IS              clPoints;
42631b406b76SMatthew G. Knepley   PetscScalar    *array;
42641b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
426597e99dd9SToby Isaac   const PetscInt *clp, *clperm;
42661a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
42671b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
42681b406b76SMatthew G. Knepley 
42691a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
42701b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42711b406b76SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
42721a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
42731a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
42741b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
42751b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
42761b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
42778f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
42781b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
42791b406b76SMatthew G. Knepley   }
42801a271a75SMatthew G. Knepley   /* Get points */
428197e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4282923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
42831a271a75SMatthew G. Knepley   /* Get array */
4284552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
42851a271a75SMatthew G. Knepley   /* Get values */
4286ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
428797e99dd9SToby Isaac     PetscInt offset = 0, f;
4288552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
428997e99dd9SToby Isaac       const PetscInt    **perms = NULL;
429097e99dd9SToby Isaac       const PetscScalar **flips = NULL;
429197e99dd9SToby Isaac 
429297e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4293552f7358SJed Brown       switch (mode) {
4294552f7358SJed Brown       case INSERT_VALUES:
429597e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
429697e99dd9SToby Isaac           const PetscInt    point = points[2*p];
429797e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
429897e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
429997e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4300552f7358SJed Brown         } break;
4301552f7358SJed Brown       case INSERT_ALL_VALUES:
430297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
430397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
430497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
430597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
430697e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4307552f7358SJed Brown         } break;
4308a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
430997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
431097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
431197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
431297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
431397e99dd9SToby Isaac           updatePointFieldsBC_private(section, point, perm, flip, f, insert, clperm, values, &offset, array);
4314a5e93ea8SMatthew G. Knepley         } break;
4315552f7358SJed Brown       case ADD_VALUES:
431697e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
431797e99dd9SToby Isaac           const PetscInt    point = points[2*p];
431897e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
431997e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
432097e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4321552f7358SJed Brown         } break;
4322552f7358SJed Brown       case ADD_ALL_VALUES:
432397e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
432497e99dd9SToby Isaac           const PetscInt    point = points[2*p];
432597e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
432697e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
432797e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4328552f7358SJed Brown         } break;
4329304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
433097e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
433197e99dd9SToby Isaac           const PetscInt    point = points[2*p];
433297e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
433397e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
433497e99dd9SToby Isaac           updatePointFieldsBC_private(section, point, perm, flip, f, add, clperm, values, &offset, array);
4335304ab55fSMatthew G. Knepley         } break;
4336552f7358SJed Brown       default:
4337f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4338552f7358SJed Brown       }
433997e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
43401a271a75SMatthew G. Knepley     }
4341552f7358SJed Brown   } else {
43421a271a75SMatthew G. Knepley     PetscInt dof, off;
434397e99dd9SToby Isaac     const PetscInt    **perms = NULL;
434497e99dd9SToby Isaac     const PetscScalar **flips = NULL;
43451a271a75SMatthew G. Knepley 
434697e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4347552f7358SJed Brown     switch (mode) {
4348552f7358SJed Brown     case INSERT_VALUES:
434997e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
435097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
435197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
435297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
435397e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
435497e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4355552f7358SJed Brown       } break;
4356552f7358SJed Brown     case INSERT_ALL_VALUES:
435797e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
435897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
435997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
436097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
436197e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
436297e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4363552f7358SJed Brown       } break;
4364a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
436597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
436697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
436797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
436897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
436997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
437097e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4371a5e93ea8SMatthew G. Knepley       } break;
4372552f7358SJed Brown     case ADD_VALUES:
437397e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
437497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
437597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
437697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
437797e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
437897e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4379552f7358SJed Brown       } break;
4380552f7358SJed Brown     case ADD_ALL_VALUES:
438197e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
438297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
438397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
438497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
438597e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
438697e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4387552f7358SJed Brown       } break;
4388304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
438997e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
439097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
439197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
439297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
439397e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
439497e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
4395304ab55fSMatthew G. Knepley       } break;
4396552f7358SJed Brown     default:
4397f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4398552f7358SJed Brown     }
439997e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4400552f7358SJed Brown   }
44011a271a75SMatthew G. Knepley   /* Cleanup points */
4402923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
44031a271a75SMatthew G. Knepley   /* Cleanup array */
4404552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4405552f7358SJed Brown   PetscFunctionReturn(0);
4406552f7358SJed Brown }
4407552f7358SJed Brown 
4408552f7358SJed Brown #undef __FUNCT__
4409e07394fbSMatthew G. Knepley #define __FUNCT__ "DMPlexVecSetFieldClosure_Internal"
4410e07394fbSMatthew G. Knepley PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, const PetscScalar values[], InsertMode mode)
4411e07394fbSMatthew G. Knepley {
4412e07394fbSMatthew G. Knepley   PetscSection      clSection;
4413e07394fbSMatthew G. Knepley   IS                clPoints;
4414e07394fbSMatthew G. Knepley   PetscScalar       *array;
4415e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
4416b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
4417e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
441897e99dd9SToby Isaac   PetscInt          offset = 0, f;
4419e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
4420e07394fbSMatthew G. Knepley 
4421e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
4422e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4423e07394fbSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
4424e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4425e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4426e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4427e07394fbSMatthew G. Knepley   /* Get points */
4428b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4429923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4430e07394fbSMatthew G. Knepley   /* Get array */
4431e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4432e07394fbSMatthew G. Knepley   /* Get values */
4433e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
443497e99dd9SToby Isaac     const PetscInt    **perms = NULL;
443597e99dd9SToby Isaac     const PetscScalar **flips = NULL;
443697e99dd9SToby Isaac 
4437e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
4438e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4439e07394fbSMatthew G. Knepley         PetscInt fdof;
4440e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
4441e07394fbSMatthew G. Knepley         offset += fdof;
4442e07394fbSMatthew G. Knepley       }
4443e07394fbSMatthew G. Knepley       continue;
4444e07394fbSMatthew G. Knepley     }
444597e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4446e07394fbSMatthew G. Knepley     switch (mode) {
4447e07394fbSMatthew G. Knepley     case INSERT_VALUES:
444897e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
444997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
445097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
445197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4452b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4453e07394fbSMatthew G. Knepley       } break;
4454e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
445597e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
445697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
445797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
445897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4459b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4460e07394fbSMatthew G. Knepley         } break;
4461e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
446297e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
446397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
446497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
446597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4466b04c866fSMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, insert, clperm, values, &offset, array);
4467e07394fbSMatthew G. Knepley       } break;
4468e07394fbSMatthew G. Knepley     case ADD_VALUES:
446997e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
447097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
447197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
447297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4473b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4474e07394fbSMatthew G. Knepley       } break;
4475e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
447697e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
447797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
447897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
447997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4480b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4481e07394fbSMatthew G. Knepley       } break;
4482e07394fbSMatthew G. Knepley     default:
4483f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4484e07394fbSMatthew G. Knepley     }
448597e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4486e07394fbSMatthew G. Knepley   }
4487e07394fbSMatthew G. Knepley   /* Cleanup points */
4488923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4489e07394fbSMatthew G. Knepley   /* Cleanup array */
4490e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4491e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
4492e07394fbSMatthew G. Knepley }
4493e07394fbSMatthew G. Knepley 
4494e07394fbSMatthew G. Knepley #undef __FUNCT__
4495552f7358SJed Brown #define __FUNCT__ "DMPlexPrintMatSetValues"
4496b0ecff45SMatthew G. Knepley PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
4497552f7358SJed Brown {
4498552f7358SJed Brown   PetscMPIInt    rank;
4499552f7358SJed Brown   PetscInt       i, j;
4500552f7358SJed Brown   PetscErrorCode ierr;
4501552f7358SJed Brown 
4502552f7358SJed Brown   PetscFunctionBegin;
450382f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
4504f347f43bSBarry Smith   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for sieve point %D\n", rank, point);CHKERRQ(ierr);
4505e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
4506e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
4507b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
4508b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
4509e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
4510b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
4511519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
45127eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
4513552f7358SJed Brown #else
4514b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
4515552f7358SJed Brown #endif
4516552f7358SJed Brown     }
451777a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
4518552f7358SJed Brown   }
4519552f7358SJed Brown   PetscFunctionReturn(0);
4520552f7358SJed Brown }
4521552f7358SJed Brown 
4522552f7358SJed Brown #undef __FUNCT__
4523415ce65aSMatthew G. Knepley #define __FUNCT__ "DMPlexGetIndicesPoint_Internal"
4524552f7358SJed Brown /* . off - The global offset of this point */
45254acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], PetscInt indices[])
4526a6dfd86eSKarl Rupp {
4527e6ccafaeSMatthew G Knepley   PetscInt        dof;    /* The number of unknowns on this point */
4528552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4529552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4530552f7358SJed Brown   PetscInt        cind = 0, k;
4531552f7358SJed Brown   PetscErrorCode  ierr;
4532552f7358SJed Brown 
4533552f7358SJed Brown   PetscFunctionBegin;
4534552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
4535552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4536552f7358SJed Brown   if (!cdof || setBC) {
45374acb8e1eSToby Isaac     if (perm) {
45384acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+perm[k]] = off + k;
4539552f7358SJed Brown     } else {
45404acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+k] = off + k;
4541552f7358SJed Brown     }
4542552f7358SJed Brown   } else {
4543552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
45444acb8e1eSToby Isaac     if (perm) {
45454acb8e1eSToby Isaac       for (k = 0; k < dof; ++k) {
45464acb8e1eSToby Isaac         if ((cind < cdof) && (k == cdofs[cind])) {
45474acb8e1eSToby Isaac           /* Insert check for returning constrained indices */
45484acb8e1eSToby Isaac           indices[*loff+perm[k]] = -(off+k+1);
45494acb8e1eSToby Isaac           ++cind;
45504acb8e1eSToby Isaac         } else {
45514acb8e1eSToby Isaac           indices[*loff+perm[k]] = off+k-cind;
45524acb8e1eSToby Isaac         }
45534acb8e1eSToby Isaac       }
45544acb8e1eSToby Isaac     } else {
4555552f7358SJed Brown       for (k = 0; k < dof; ++k) {
4556552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
4557552f7358SJed Brown           /* Insert check for returning constrained indices */
4558e6ccafaeSMatthew G Knepley           indices[*loff+k] = -(off+k+1);
4559552f7358SJed Brown           ++cind;
4560552f7358SJed Brown         } else {
4561e6ccafaeSMatthew G Knepley           indices[*loff+k] = off+k-cind;
4562552f7358SJed Brown         }
4563552f7358SJed Brown       }
4564552f7358SJed Brown     }
4565552f7358SJed Brown   }
4566e6ccafaeSMatthew G Knepley   *loff += dof;
4567552f7358SJed Brown   PetscFunctionReturn(0);
4568552f7358SJed Brown }
4569552f7358SJed Brown 
4570552f7358SJed Brown #undef __FUNCT__
4571415ce65aSMatthew G. Knepley #define __FUNCT__ "DMPlexGetIndicesPointFields_Internal"
4572552f7358SJed Brown /* . off - The global offset of this point */
45734acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, PetscInt indices[])
4574a6dfd86eSKarl Rupp {
4575552f7358SJed Brown   PetscInt       numFields, foff, f;
4576552f7358SJed Brown   PetscErrorCode ierr;
4577552f7358SJed Brown 
4578552f7358SJed Brown   PetscFunctionBegin;
4579552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4580552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
45814acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
4582552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
45834acb8e1eSToby Isaac     PetscInt        cind = 0, b;
45844acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
4585552f7358SJed Brown 
4586552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4587552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
4588552f7358SJed Brown     if (!cfdof || setBC) {
45894acb8e1eSToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {indices[foffs[f]+perm[b]] = off+foff+b;}}
45904acb8e1eSToby Isaac       else      {for (b = 0; b < fdof; b++) {indices[foffs[f]+     b ] = off+foff+b;}}
4591552f7358SJed Brown     } else {
4592552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
45934acb8e1eSToby Isaac       if (perm) {
45944acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
45954acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
45964acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = -(off+foff+b+1);
4597552f7358SJed Brown             ++cind;
4598552f7358SJed Brown           } else {
45994acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = off+foff+b-cind;
4600552f7358SJed Brown           }
4601552f7358SJed Brown         }
4602552f7358SJed Brown       } else {
46034acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
46044acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
46054acb8e1eSToby Isaac             indices[foffs[f]+b] = -(off+foff+b+1);
4606552f7358SJed Brown             ++cind;
4607552f7358SJed Brown           } else {
46084acb8e1eSToby Isaac             indices[foffs[f]+b] = off+foff+b-cind;
4609552f7358SJed Brown           }
4610552f7358SJed Brown         }
4611552f7358SJed Brown       }
4612552f7358SJed Brown     }
4613a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
4614552f7358SJed Brown     foffs[f] += fdof;
4615552f7358SJed Brown   }
4616552f7358SJed Brown   PetscFunctionReturn(0);
4617552f7358SJed Brown }
4618552f7358SJed Brown 
4619552f7358SJed Brown #undef __FUNCT__
4620f7c74593SToby Isaac #define __FUNCT__ "DMPlexAnchorsModifyMat"
46214acb8e1eSToby Isaac PetscErrorCode DMPlexAnchorsModifyMat(DM dm, PetscSection section, PetscInt numPoints, PetscInt numIndices, const PetscInt points[], const PetscInt ***perms, const PetscScalar values[], PetscInt *outNumPoints, PetscInt *outNumIndices, PetscInt *outPoints[], PetscScalar *outValues[], PetscInt offsets[], PetscBool multiplyLeft)
4622d3d1a6afSToby Isaac {
4623d3d1a6afSToby Isaac   Mat             cMat;
4624d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
4625d3d1a6afSToby Isaac   IS              aIS;
4626d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
4627d3d1a6afSToby Isaac   const PetscInt  *anchors;
4628e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
4629d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
4630d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
4631d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
4632d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
4633d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
4634d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
4635d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
46366ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
4637d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
4638d3d1a6afSToby Isaac   PetscErrorCode  ierr;
4639d3d1a6afSToby Isaac 
4640d3d1a6afSToby Isaac   PetscFunctionBegin;
4641d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4642d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4643d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4644d3d1a6afSToby Isaac 
4645a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
4646d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
4647d3d1a6afSToby Isaac   if (aSec) {
4648d3d1a6afSToby Isaac     ierr = PetscMemzero(newOffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
4649d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
4650d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
4651d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
4652d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
4653d3d1a6afSToby Isaac      * into the global matrix anyway) */
4654d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
4655d3d1a6afSToby Isaac       PetscInt b    = points[p];
46564b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4657d3d1a6afSToby Isaac 
46584b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
46594b2f2278SToby Isaac       if (!bSecDof) {
46604b2f2278SToby Isaac         continue;
46614b2f2278SToby Isaac       }
4662d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4663d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
4664d3d1a6afSToby Isaac       }
4665d3d1a6afSToby Isaac       if (bDof) {
4666d3d1a6afSToby Isaac         /* this point is constrained */
4667d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
4668d3d1a6afSToby Isaac         PetscInt bOff, q;
4669d3d1a6afSToby Isaac 
4670d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
4671d3d1a6afSToby Isaac         newNumPoints  += bDof;
4672d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
4673d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4674d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
4675d3d1a6afSToby Isaac           PetscInt aDof;
4676d3d1a6afSToby Isaac 
4677d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
4678d3d1a6afSToby Isaac           newNumIndices += aDof;
4679d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
4680d3d1a6afSToby Isaac             PetscInt fDof;
4681d3d1a6afSToby Isaac 
4682d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
4683d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
4684d3d1a6afSToby Isaac           }
4685d3d1a6afSToby Isaac         }
4686d3d1a6afSToby Isaac       }
4687d3d1a6afSToby Isaac       else {
4688d3d1a6afSToby Isaac         /* this point is not constrained */
4689d3d1a6afSToby Isaac         newNumPoints++;
46904b2f2278SToby Isaac         newNumIndices += bSecDof;
4691d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
4692d3d1a6afSToby Isaac           PetscInt fDof;
4693d3d1a6afSToby Isaac 
4694d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4695d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
4696d3d1a6afSToby Isaac         }
4697d3d1a6afSToby Isaac       }
4698d3d1a6afSToby Isaac     }
4699d3d1a6afSToby Isaac   }
4700d3d1a6afSToby Isaac   if (!anyConstrained) {
470172b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
470272b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
470372b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
470472b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
470572b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
4706d3d1a6afSToby Isaac     PetscFunctionReturn(0);
4707d3d1a6afSToby Isaac   }
4708d3d1a6afSToby Isaac 
47096ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
47106ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
47116ecaa68aSToby Isaac 
4712f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
4713d3d1a6afSToby Isaac 
47146ecaa68aSToby Isaac   if (!outPoints && !outValues) {
47156ecaa68aSToby Isaac     if (offsets) {
47166ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
47176ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
47186ecaa68aSToby Isaac       }
47196ecaa68aSToby Isaac     }
47206ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
47216ecaa68aSToby Isaac     PetscFunctionReturn(0);
47226ecaa68aSToby Isaac   }
47236ecaa68aSToby Isaac 
4724f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
4725d3d1a6afSToby Isaac 
4726f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
4727d3d1a6afSToby Isaac 
4728d3d1a6afSToby Isaac   /* workspaces */
4729d3d1a6afSToby Isaac   if (numFields) {
4730d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
4731d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
4732d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
4733d3d1a6afSToby Isaac     }
4734d3d1a6afSToby Isaac   }
4735d3d1a6afSToby Isaac   else {
4736d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
4737d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
4738d3d1a6afSToby Isaac   }
4739d3d1a6afSToby Isaac 
4740d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
4741d3d1a6afSToby Isaac   if (numFields) {
47424b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
47434b2f2278SToby Isaac 
4744d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4745d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
47464b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4747d3d1a6afSToby Isaac 
47484b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
47494b2f2278SToby Isaac       if (!bSecDof) {
47504b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
47514b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
47524b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
47534b2f2278SToby Isaac         }
47544b2f2278SToby Isaac         continue;
47554b2f2278SToby Isaac       }
4756d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4757d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4758d3d1a6afSToby Isaac       }
4759d3d1a6afSToby Isaac       if (bDof) {
4760d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4761d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
4762d3d1a6afSToby Isaac 
4763d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4764d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4765d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
4766d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
4767d3d1a6afSToby Isaac             PetscInt aFDof;
4768d3d1a6afSToby Isaac 
4769d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
4770d3d1a6afSToby Isaac             allFDof += aFDof;
4771d3d1a6afSToby Isaac           }
4772d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
4773d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
4774d3d1a6afSToby Isaac         }
4775d3d1a6afSToby Isaac       }
4776d3d1a6afSToby Isaac       else {
4777d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4778d3d1a6afSToby Isaac           PetscInt fDof;
4779d3d1a6afSToby Isaac 
4780d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4781d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
4782d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
4783d3d1a6afSToby Isaac         }
4784d3d1a6afSToby Isaac       }
4785d3d1a6afSToby Isaac     }
47864b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
47874b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
47884b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
4789d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
4790d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
4791d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
4792d3d1a6afSToby Isaac       }
479319f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
479419f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
4795d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
4796d3d1a6afSToby Isaac     }
4797d3d1a6afSToby Isaac   }
4798d3d1a6afSToby Isaac   else {
4799d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4800d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
48014b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4802d3d1a6afSToby Isaac 
48034b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
48044b2f2278SToby Isaac       if (!bSecDof) {
48054b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
48064b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
48074b2f2278SToby Isaac         continue;
48084b2f2278SToby Isaac       }
4809d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4810d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4811d3d1a6afSToby Isaac       }
4812d3d1a6afSToby Isaac       if (bDof) {
48134b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
4814d3d1a6afSToby Isaac 
4815d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4816d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4817d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
4818d3d1a6afSToby Isaac 
4819d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
4820d3d1a6afSToby Isaac           allDof += aDof;
4821d3d1a6afSToby Isaac         }
4822d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
48234b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
4824d3d1a6afSToby Isaac       }
4825d3d1a6afSToby Isaac       else {
48264b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
4827d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
4828d3d1a6afSToby Isaac       }
4829d3d1a6afSToby Isaac     }
4830d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
4831d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
4832d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4833d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
4834d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
4835d3d1a6afSToby Isaac     }
4836d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
4837d3d1a6afSToby Isaac   }
4838d3d1a6afSToby Isaac 
48396ecaa68aSToby Isaac   /* output arrays */
48406ecaa68aSToby Isaac   ierr = DMGetWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
48416ecaa68aSToby Isaac 
4842d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
4843d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
4844d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
4845d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
4846d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
4847d3d1a6afSToby Isaac   if (numFields) {
4848d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
4849d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4850d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
48514b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4852d3d1a6afSToby Isaac 
48534b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
48544b2f2278SToby Isaac       if (!bSecDof) {
48554b2f2278SToby Isaac         continue;
48564b2f2278SToby Isaac       }
4857d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4858d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4859d3d1a6afSToby Isaac       }
4860d3d1a6afSToby Isaac       if (bDof) {
4861d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
4862d3d1a6afSToby Isaac 
4863d3d1a6afSToby Isaac         fStart[0] = 0;
4864d3d1a6afSToby Isaac         fEnd[0]   = 0;
4865d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4866d3d1a6afSToby Isaac           PetscInt fDof;
4867d3d1a6afSToby Isaac 
4868d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
4869d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
4870d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
4871d3d1a6afSToby Isaac         }
4872d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
48734acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, indices);CHKERRQ(ierr);
4874d3d1a6afSToby Isaac 
4875d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
4876d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
4877d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4878d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
4879d3d1a6afSToby Isaac 
4880d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
4881d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
4882d3d1a6afSToby Isaac         }
4883d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4884d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4885d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
4886d3d1a6afSToby Isaac 
4887d3d1a6afSToby Isaac           /* we take the orientation of ap into account in the order that we constructed the indices above: the newly added points have no orientation */
4888d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
4889d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
4890302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
48914acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, newIndices);CHKERRQ(ierr);
4892d3d1a6afSToby Isaac         }
4893d3d1a6afSToby Isaac         newP += bDof;
4894d3d1a6afSToby Isaac 
48956ecaa68aSToby Isaac         if (outValues) {
4896d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
4897d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
4898d3d1a6afSToby Isaac             ierr = MatGetValues(cMat,fEnd[f]-fStart[f],indices + fStart[f],fAnchorEnd[f] - fAnchorStart[f],newIndices + fAnchorStart[f],pointMat[f] + pointMatOffsets[f][p]);CHKERRQ(ierr);
4899d3d1a6afSToby Isaac           }
4900d3d1a6afSToby Isaac         }
49016ecaa68aSToby Isaac       }
4902d3d1a6afSToby Isaac       else {
4903d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
4904d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
4905d3d1a6afSToby Isaac         newP++;
4906d3d1a6afSToby Isaac       }
4907d3d1a6afSToby Isaac     }
4908d3d1a6afSToby Isaac   } else {
4909d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4910d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4911d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
49124b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4913d3d1a6afSToby Isaac 
49144b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
49154b2f2278SToby Isaac       if (!bSecDof) {
49164b2f2278SToby Isaac         continue;
49174b2f2278SToby Isaac       }
4918d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4919d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4920d3d1a6afSToby Isaac       }
4921d3d1a6afSToby Isaac       if (bDof) {
4922d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
4923d3d1a6afSToby Isaac 
4924d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
49254acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, indices);CHKERRQ(ierr);
4926d3d1a6afSToby Isaac 
4927d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
4928d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4929d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
4930d3d1a6afSToby Isaac 
4931d3d1a6afSToby Isaac           /* we take the orientation of ap into account in the order that we constructed the indices above: the newly added points have no orientation */
4932d3d1a6afSToby Isaac 
4933d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
4934d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
4935302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
49364acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, newIndices);CHKERRQ(ierr);
4937d3d1a6afSToby Isaac         }
4938d3d1a6afSToby Isaac         newP += bDof;
4939d3d1a6afSToby Isaac 
4940d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
49416ecaa68aSToby Isaac         if (outValues) {
4942d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
4943d3d1a6afSToby Isaac         }
49446ecaa68aSToby Isaac       }
4945d3d1a6afSToby Isaac       else {
4946d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
4947d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
4948d3d1a6afSToby Isaac         newP++;
4949d3d1a6afSToby Isaac       }
4950d3d1a6afSToby Isaac     }
4951d3d1a6afSToby Isaac   }
4952d3d1a6afSToby Isaac 
49536ecaa68aSToby Isaac   if (outValues) {
49546ecaa68aSToby Isaac     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
4955d3d1a6afSToby Isaac     ierr = PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));CHKERRQ(ierr);
4956d3d1a6afSToby Isaac     /* multiply constraints on the right */
4957d3d1a6afSToby Isaac     if (numFields) {
4958d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
4959d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
4960d3d1a6afSToby Isaac 
4961d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
4962d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
4963d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
4964d3d1a6afSToby Isaac           PetscInt c, r, k;
4965d3d1a6afSToby Isaac           PetscInt dof;
4966d3d1a6afSToby Isaac 
4967d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
49684b2f2278SToby Isaac           if (!dof) {
49694b2f2278SToby Isaac             continue;
49704b2f2278SToby Isaac           }
4971d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
4972d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
4973d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
4974d3d1a6afSToby Isaac 
4975d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
4976d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
4977d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
49784acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
4979d3d1a6afSToby Isaac                 }
4980d3d1a6afSToby Isaac               }
4981d3d1a6afSToby Isaac             }
4982d3d1a6afSToby Isaac           }
4983d3d1a6afSToby Isaac           else {
4984d3d1a6afSToby Isaac             /* copy this column as is */
4985d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
4986d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
4987d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
4988d3d1a6afSToby Isaac               }
4989d3d1a6afSToby Isaac             }
4990d3d1a6afSToby Isaac           }
4991d3d1a6afSToby Isaac           oldOff += dof;
4992d3d1a6afSToby Isaac         }
4993d3d1a6afSToby Isaac       }
4994d3d1a6afSToby Isaac     }
4995d3d1a6afSToby Isaac     else {
4996d3d1a6afSToby Isaac       PetscInt oldOff = 0;
4997d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
4998d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
4999d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
5000d3d1a6afSToby Isaac         PetscInt c, r, k;
5001d3d1a6afSToby Isaac         PetscInt dof;
5002d3d1a6afSToby Isaac 
5003d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
50044b2f2278SToby Isaac         if (!dof) {
50054b2f2278SToby Isaac           continue;
50064b2f2278SToby Isaac         }
5007d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5008d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5009d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
5010d3d1a6afSToby Isaac 
5011d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5012d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
5013d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
5014d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5015d3d1a6afSToby Isaac               }
5016d3d1a6afSToby Isaac             }
5017d3d1a6afSToby Isaac           }
5018d3d1a6afSToby Isaac         }
5019d3d1a6afSToby Isaac         else {
5020d3d1a6afSToby Isaac           /* copy this column as is */
5021d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5022d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
5023d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5024d3d1a6afSToby Isaac             }
5025d3d1a6afSToby Isaac           }
5026d3d1a6afSToby Isaac         }
5027d3d1a6afSToby Isaac         oldOff += dof;
5028d3d1a6afSToby Isaac       }
5029d3d1a6afSToby Isaac     }
5030d3d1a6afSToby Isaac 
50316ecaa68aSToby Isaac     if (multiplyLeft) {
50326ecaa68aSToby Isaac       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
5033d3d1a6afSToby Isaac       ierr = PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));CHKERRQ(ierr);
5034d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
5035d3d1a6afSToby Isaac       if (numFields) {
5036d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5037d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
5038d3d1a6afSToby Isaac 
5039d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
5040d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
5041d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
5042d3d1a6afSToby Isaac             PetscInt c, r, k;
5043d3d1a6afSToby Isaac             PetscInt dof;
5044d3d1a6afSToby Isaac 
5045d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
5046d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5047d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5048d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
5049d3d1a6afSToby Isaac 
5050d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
5051d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5052d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
5053d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5054d3d1a6afSToby Isaac                   }
5055d3d1a6afSToby Isaac                 }
5056d3d1a6afSToby Isaac               }
5057d3d1a6afSToby Isaac             }
5058d3d1a6afSToby Isaac             else {
5059d3d1a6afSToby Isaac               /* copy this row as is */
5060d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
5061d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5062d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5063d3d1a6afSToby Isaac                 }
5064d3d1a6afSToby Isaac               }
5065d3d1a6afSToby Isaac             }
5066d3d1a6afSToby Isaac             oldOff += dof;
5067d3d1a6afSToby Isaac           }
5068d3d1a6afSToby Isaac         }
5069d3d1a6afSToby Isaac       }
5070d3d1a6afSToby Isaac       else {
5071d3d1a6afSToby Isaac         PetscInt oldOff = 0;
5072d3d1a6afSToby Isaac 
5073d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5074d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
5075d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5076d3d1a6afSToby Isaac           PetscInt c, r, k;
5077d3d1a6afSToby Isaac           PetscInt dof;
5078d3d1a6afSToby Isaac 
5079d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
5080d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5081d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5082d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5083d3d1a6afSToby Isaac 
5084d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5085d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5086d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5087d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5088d3d1a6afSToby Isaac                 }
5089d3d1a6afSToby Isaac               }
5090d3d1a6afSToby Isaac             }
5091d3d1a6afSToby Isaac           }
5092d3d1a6afSToby Isaac           else {
5093d3d1a6afSToby Isaac             /* copy this row as is */
50949fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5095d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5096d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5097d3d1a6afSToby Isaac               }
5098d3d1a6afSToby Isaac             }
5099d3d1a6afSToby Isaac           }
5100d3d1a6afSToby Isaac           oldOff += dof;
5101d3d1a6afSToby Isaac         }
5102d3d1a6afSToby Isaac       }
5103d3d1a6afSToby Isaac 
51046ecaa68aSToby Isaac       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
51056ecaa68aSToby Isaac     }
51066ecaa68aSToby Isaac     else {
51076ecaa68aSToby Isaac       newValues = tmpValues;
51086ecaa68aSToby Isaac     }
51096ecaa68aSToby Isaac   }
51106ecaa68aSToby Isaac 
5111d3d1a6afSToby Isaac   /* clean up */
5112d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
5113d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
51146ecaa68aSToby Isaac 
5115d3d1a6afSToby Isaac   if (numFields) {
5116d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
5117d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5118d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
5119d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5120d3d1a6afSToby Isaac     }
5121d3d1a6afSToby Isaac   }
5122d3d1a6afSToby Isaac   else {
5123d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5124d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
5125d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5126d3d1a6afSToby Isaac   }
5127d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5128d3d1a6afSToby Isaac 
5129d3d1a6afSToby Isaac   /* output */
51306ecaa68aSToby Isaac   if (outPoints) {
5131d3d1a6afSToby Isaac     *outPoints = newPoints;
51326ecaa68aSToby Isaac   }
51336ecaa68aSToby Isaac   else {
51346ecaa68aSToby Isaac     ierr = DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
51356ecaa68aSToby Isaac   }
513631620726SToby Isaac   if (outValues) {
5137d3d1a6afSToby Isaac     *outValues = newValues;
51386ecaa68aSToby Isaac   }
51396ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
5140d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
5141d3d1a6afSToby Isaac   }
5142d3d1a6afSToby Isaac   PetscFunctionReturn(0);
5143d3d1a6afSToby Isaac }
5144d3d1a6afSToby Isaac 
5145d3d1a6afSToby Isaac #undef __FUNCT__
51467773e69fSMatthew G. Knepley #define __FUNCT__ "DMPlexGetClosureIndices"
51476ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
51487773e69fSMatthew G. Knepley {
51497773e69fSMatthew G. Knepley   PetscSection    clSection;
51507773e69fSMatthew G. Knepley   IS              clPoints;
51517773e69fSMatthew G. Knepley   const PetscInt *clp;
51524acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
51537773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
51547773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
51557773e69fSMatthew G. Knepley   PetscInt        offsets[32];
51567773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
51577773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
51587773e69fSMatthew G. Knepley 
51597773e69fSMatthew G. Knepley   PetscFunctionBegin;
51607773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51617773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
51627773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
51637773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
51647773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
51657773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
51667773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
51677773e69fSMatthew G. Knepley   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
51687773e69fSMatthew G. Knepley   /* Get points in closure */
5169923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
51707773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
51717773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
51727773e69fSMatthew G. Knepley     PetscInt dof, fdof;
51737773e69fSMatthew G. Knepley 
51747773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
51757773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
51767773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
51777773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
51787773e69fSMatthew G. Knepley     }
51797773e69fSMatthew G. Knepley     Nind += dof;
51807773e69fSMatthew G. Knepley   }
51817773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
51828ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
51834acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
51844acb8e1eSToby Isaac   /* Get dual space symmetries */
51854acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
51864acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
51874acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
51884acb8e1eSToby Isaac   }
51897773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
51907773e69fSMatthew G. Knepley   {
51914acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
51927773e69fSMatthew G. Knepley     if (numPointsNew) {
51934acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
51944acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
51954acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
51964acb8e1eSToby Isaac       }
51974acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
51984acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
51994acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
52004acb8e1eSToby Isaac       }
5201923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
52027773e69fSMatthew G. Knepley       numPoints = numPointsNew;
52037773e69fSMatthew G. Knepley       Nind      = NindNew;
52047773e69fSMatthew G. Knepley       points    = pointsNew;
52057773e69fSMatthew G. Knepley     }
52067773e69fSMatthew G. Knepley   }
52077773e69fSMatthew G. Knepley   /* Calculate indices */
52087773e69fSMatthew G. Knepley   ierr = DMGetWorkArray(dm, Nind, PETSC_INT, indices);CHKERRQ(ierr);
52097773e69fSMatthew G. Knepley   if (Nf) {
52106ecaa68aSToby Isaac     if (outOffsets) {
52116ecaa68aSToby Isaac       PetscInt f;
52126ecaa68aSToby Isaac 
521346bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
52146ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
52156ecaa68aSToby Isaac       }
52166ecaa68aSToby Isaac     }
52174acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
52184acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
52194acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, *indices);
52207773e69fSMatthew G. Knepley     }
52217773e69fSMatthew G. Knepley   } else {
52224acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
52234acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
52244acb8e1eSToby Isaac 
52254acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
52264acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, *indices);
52277773e69fSMatthew G. Knepley     }
52287773e69fSMatthew G. Knepley   }
52297773e69fSMatthew G. Knepley   /* Cleanup points */
52304acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
52314acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52324acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52334acb8e1eSToby Isaac   }
52347773e69fSMatthew G. Knepley   if (numPointsNew) {
52357773e69fSMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, PETSC_INT, &pointsNew);CHKERRQ(ierr);
52367773e69fSMatthew G. Knepley   } else {
5237923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
52387773e69fSMatthew G. Knepley   }
52397773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
52407773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
52417773e69fSMatthew G. Knepley }
52427773e69fSMatthew G. Knepley 
52437773e69fSMatthew G. Knepley #undef __FUNCT__
52447773e69fSMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreClosureIndices"
524546bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
52467773e69fSMatthew G. Knepley {
52477773e69fSMatthew G. Knepley   PetscErrorCode ierr;
52487773e69fSMatthew G. Knepley 
52497773e69fSMatthew G. Knepley   PetscFunctionBegin;
52507773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52517773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
52527773e69fSMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, indices);CHKERRQ(ierr);
52537773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
52547773e69fSMatthew G. Knepley }
52557773e69fSMatthew G. Knepley 
52567773e69fSMatthew G. Knepley #undef __FUNCT__
5257552f7358SJed Brown #define __FUNCT__ "DMPlexMatSetClosure"
52587f5d1fdeSMatthew G. Knepley /*@C
52597f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
52607f5d1fdeSMatthew G. Knepley 
52617f5d1fdeSMatthew G. Knepley   Not collective
52627f5d1fdeSMatthew G. Knepley 
52637f5d1fdeSMatthew G. Knepley   Input Parameters:
52647f5d1fdeSMatthew G. Knepley + dm - The DM
5265ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5266ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
52677f5d1fdeSMatthew G. Knepley . A - The matrix
52687f5d1fdeSMatthew G. Knepley . point - The sieve point in the DM
52697f5d1fdeSMatthew G. Knepley . values - The array of values
52707f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
52717f5d1fdeSMatthew G. Knepley 
52727f5d1fdeSMatthew G. Knepley   Fortran Notes:
52737f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
52747f5d1fdeSMatthew G. Knepley 
52757f5d1fdeSMatthew G. Knepley   Level: intermediate
52767f5d1fdeSMatthew G. Knepley 
52777f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
52787f5d1fdeSMatthew G. Knepley @*/
52797c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5280552f7358SJed Brown {
5281552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
52821b406b76SMatthew G. Knepley   PetscSection        clSection;
52831b406b76SMatthew G. Knepley   IS                  clPoints;
5284d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
52851b406b76SMatthew G. Knepley   const PetscInt     *clp;
5286552f7358SJed Brown   PetscInt           *indices;
5287552f7358SJed Brown   PetscInt            offsets[32];
52884acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
52894acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
5290923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
52914acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
5292d3d1a6afSToby Isaac   PetscScalar        *newValues;
5293552f7358SJed Brown   PetscErrorCode      ierr;
5294552f7358SJed Brown 
5295552f7358SJed Brown   PetscFunctionBegin;
5296552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5297ebd6d717SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
52983dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5299ebd6d717SJed Brown   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
53003dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
53013dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5302552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
530382f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5304552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5305923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5306552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5307552f7358SJed Brown     PetscInt fdof;
5308552f7358SJed Brown 
5309552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5310552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5311552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5312552f7358SJed Brown       offsets[f+1] += fdof;
5313552f7358SJed Brown     }
5314552f7358SJed Brown     numIndices += dof;
5315552f7358SJed Brown   }
53160d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
53170d644c17SKarl Rupp 
53188ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
53194acb8e1eSToby Isaac   /* Get symmetries */
53204acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
53214acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
53224acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
53234acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
53244acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
53254acb8e1eSToby Isaac 
53264acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
53274acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
53284acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
53294acb8e1eSToby Isaac 
53304acb8e1eSToby Isaac         if (!numFields) {
53314acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
53324acb8e1eSToby Isaac         } else {
53334acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
53344acb8e1eSToby Isaac         }
53354acb8e1eSToby Isaac         if (flip) {
53364acb8e1eSToby Isaac           PetscInt i, j, k;
53374acb8e1eSToby Isaac 
53384acb8e1eSToby Isaac           if (!valCopy) {
53394acb8e1eSToby Isaac             ierr = DMGetWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
53404acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
53414acb8e1eSToby Isaac             values = valCopy;
53424acb8e1eSToby Isaac           }
53434acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
5344775f7be2SToby Isaac             PetscScalar fval = flip[i];
53454acb8e1eSToby Isaac 
53464acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
53474acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
53484acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
53494acb8e1eSToby Isaac             }
53504acb8e1eSToby Isaac           }
53514acb8e1eSToby Isaac         }
53524acb8e1eSToby Isaac         foffset += fdof;
53534acb8e1eSToby Isaac       }
53544acb8e1eSToby Isaac     }
53554acb8e1eSToby Isaac   }
53564acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
5357d3d1a6afSToby Isaac   if (newNumPoints) {
53584acb8e1eSToby Isaac     if (valCopy) {
53594acb8e1eSToby Isaac       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
53604acb8e1eSToby Isaac     }
53614acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
53624acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
53634acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
53644acb8e1eSToby Isaac     }
53654acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
53664acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
53674acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
53684acb8e1eSToby Isaac     }
5369923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5370d3d1a6afSToby Isaac     numPoints  = newNumPoints;
5371d3d1a6afSToby Isaac     numIndices = newNumIndices;
5372d3d1a6afSToby Isaac     points     = newPoints;
5373d3d1a6afSToby Isaac     values     = newValues;
5374d3d1a6afSToby Isaac   }
5375552f7358SJed Brown   ierr = DMGetWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5376552f7358SJed Brown   if (numFields) {
53774acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
53784acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
53794acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, indices);
5380552f7358SJed Brown     }
5381552f7358SJed Brown   } else {
53824acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
53834acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
53844acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
53854acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, indices);
5386552f7358SJed Brown     }
5387552f7358SJed Brown   }
5388b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
5389552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
5390ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
5391ab1d0545SMatthew G. Knepley     PetscInt i;
5392ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
53938ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
5394ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5395ab1d0545SMatthew G. Knepley   }
5396552f7358SJed Brown   if (ierr) {
5397552f7358SJed Brown     PetscMPIInt    rank;
5398552f7358SJed Brown     PetscErrorCode ierr2;
5399552f7358SJed Brown 
540082f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5401e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5402b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
54036a415b8fSMatthew G Knepley     ierr2 = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr2);
5404552f7358SJed Brown     CHKERRQ(ierr);
5405552f7358SJed Brown   }
54064acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
54074acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54084acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54094acb8e1eSToby Isaac   }
5410d3d1a6afSToby Isaac   if (newNumPoints) {
5411d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
5412d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
5413d3d1a6afSToby Isaac   }
5414d3d1a6afSToby Isaac   else {
54154acb8e1eSToby Isaac     if (valCopy) {
54164acb8e1eSToby Isaac       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
54174acb8e1eSToby Isaac     }
5418923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5419d3d1a6afSToby Isaac   }
5420552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5421552f7358SJed Brown   PetscFunctionReturn(0);
5422552f7358SJed Brown }
5423552f7358SJed Brown 
5424552f7358SJed Brown #undef __FUNCT__
5425de41b84cSMatthew G. Knepley #define __FUNCT__ "DMPlexMatSetClosureRefined"
5426de41b84cSMatthew G. Knepley PetscErrorCode DMPlexMatSetClosureRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5427de41b84cSMatthew G. Knepley {
5428de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
5429de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
5430de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
5431de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
5432de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
5433de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
54344ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
5435de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
5436de41b84cSMatthew G. Knepley 
5437de41b84cSMatthew G. Knepley   PetscFunctionBegin;
5438de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
5439de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
5440de41b84cSMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
5441de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
5442de41b84cSMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
5443de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
5444de41b84cSMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
5445de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
5446de41b84cSMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
5447de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
5448de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
5449de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
5450de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5451de41b84cSMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5452de41b84cSMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5453de41b84cSMatthew G. Knepley   /* Column indices */
5454de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
54554ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
5456de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
5457de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
5458de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
5459de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
5460de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
5461de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
5462de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
5463de41b84cSMatthew G. Knepley       ++q;
5464de41b84cSMatthew G. Knepley     }
5465de41b84cSMatthew G. Knepley   }
5466de41b84cSMatthew G. Knepley   numCPoints = q;
5467de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
5468de41b84cSMatthew G. Knepley     PetscInt fdof;
5469de41b84cSMatthew G. Knepley 
5470de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
54714ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5472de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5473de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
5474de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
5475de41b84cSMatthew G. Knepley     }
5476de41b84cSMatthew G. Knepley     numCIndices += dof;
5477de41b84cSMatthew G. Knepley   }
5478de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
5479de41b84cSMatthew G. Knepley   /* Row indices */
5480de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
5481de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
54821ba5f902SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
5483de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
5484de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
5485de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5486de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
5487de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
5488de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
5489de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
54904ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
54914ca5e9f5SMatthew G. Knepley         if (!dof) continue;
54924ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
54934ca5e9f5SMatthew G. Knepley         if (s < q) continue;
5494de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
5495de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
5496de41b84cSMatthew G. Knepley         ++q;
5497de41b84cSMatthew G. Knepley       }
5498de41b84cSMatthew G. Knepley     }
5499de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5500de41b84cSMatthew G. Knepley   }
5501de41b84cSMatthew G. Knepley   numFPoints = q;
5502de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
5503de41b84cSMatthew G. Knepley     PetscInt fdof;
5504de41b84cSMatthew G. Knepley 
5505de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
55064ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5507de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5508de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
5509de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
5510de41b84cSMatthew G. Knepley     }
5511de41b84cSMatthew G. Knepley     numFIndices += dof;
5512de41b84cSMatthew G. Knepley   }
5513de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
5514de41b84cSMatthew G. Knepley 
55158ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
55168ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
5517de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
5518de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
5519de41b84cSMatthew G. Knepley   if (numFields) {
55204acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
55214acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
55224acb8e1eSToby Isaac 
55234acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
55244acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
55254acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5526de41b84cSMatthew G. Knepley     }
55274acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
55284acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
55294acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
55304acb8e1eSToby Isaac     }
55314acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
55324acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
55334acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
55344acb8e1eSToby Isaac     }
55354acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
55364acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
55374acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5538de41b84cSMatthew G. Knepley     }
5539de41b84cSMatthew G. Knepley   } else {
55404acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
55414acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
55424acb8e1eSToby Isaac 
55434acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
55444acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
55454acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
55464acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
55474acb8e1eSToby Isaac 
55484acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
55494acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);CHKERRQ(ierr);
5550de41b84cSMatthew G. Knepley     }
55514acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
55524acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
55534acb8e1eSToby Isaac 
55544acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
55554acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);CHKERRQ(ierr);
5556de41b84cSMatthew G. Knepley     }
55574acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
55584acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
5559de41b84cSMatthew G. Knepley   }
5560de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
55614acb8e1eSToby Isaac   /* TODO: flips */
5562de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
5563de41b84cSMatthew G. Knepley   if (ierr) {
5564de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
5565de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
5566de41b84cSMatthew G. Knepley 
5567de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5568e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5569de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
5570de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr2);
5571de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr2);
5572de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
5573de41b84cSMatthew G. Knepley   }
5574549a8adaSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
5575de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
5576de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
5577de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
5578de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
5579de41b84cSMatthew G. Knepley }
5580de41b84cSMatthew G. Knepley 
5581de41b84cSMatthew G. Knepley #undef __FUNCT__
55827c927364SMatthew G. Knepley #define __FUNCT__ "DMPlexMatGetClosureIndicesRefined"
55837c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
55847c927364SMatthew G. Knepley {
55857c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
55867c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
55877c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
55887c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
55897c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
55907c927364SMatthew G. Knepley   PetscErrorCode ierr;
55917c927364SMatthew G. Knepley 
55927c927364SMatthew G. Knepley   PetscFunctionBegin;
55937c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
55947c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
55957c927364SMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
55967c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
55977c927364SMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
55987c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
55997c927364SMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
56007c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
56017c927364SMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
56027c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
56037c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
56047c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
56057c927364SMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
56067c927364SMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
56077c927364SMatthew G. Knepley   /* Column indices */
56087c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
56097c927364SMatthew G. Knepley   maxFPoints = numCPoints;
56107c927364SMatthew G. Knepley   /* Compress out points not in the section */
56117c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
56127c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
56137c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
56147c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
56157c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
56167c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
56177c927364SMatthew G. Knepley       ++q;
56187c927364SMatthew G. Knepley     }
56197c927364SMatthew G. Knepley   }
56207c927364SMatthew G. Knepley   numCPoints = q;
56217c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
56227c927364SMatthew G. Knepley     PetscInt fdof;
56237c927364SMatthew G. Knepley 
56247c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
56257c927364SMatthew G. Knepley     if (!dof) continue;
56267c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
56277c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
56287c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
56297c927364SMatthew G. Knepley     }
56307c927364SMatthew G. Knepley     numCIndices += dof;
56317c927364SMatthew G. Knepley   }
56327c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
56337c927364SMatthew G. Knepley   /* Row indices */
56347c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
56357c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
56367c927364SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
56377c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
56387c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
56397c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
56407c927364SMatthew G. Knepley     /* Compress out points not in the section */
56417c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
56427c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
56437c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
56447c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
56457c927364SMatthew G. Knepley         if (!dof) continue;
56467c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
56477c927364SMatthew G. Knepley         if (s < q) continue;
56487c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
56497c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
56507c927364SMatthew G. Knepley         ++q;
56517c927364SMatthew G. Knepley       }
56527c927364SMatthew G. Knepley     }
56537c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
56547c927364SMatthew G. Knepley   }
56557c927364SMatthew G. Knepley   numFPoints = q;
56567c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
56577c927364SMatthew G. Knepley     PetscInt fdof;
56587c927364SMatthew G. Knepley 
56597c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
56607c927364SMatthew G. Knepley     if (!dof) continue;
56617c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
56627c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
56637c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
56647c927364SMatthew G. Knepley     }
56657c927364SMatthew G. Knepley     numFIndices += dof;
56667c927364SMatthew G. Knepley   }
56677c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
56687c927364SMatthew G. Knepley 
56698ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
56708ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
56717c927364SMatthew G. Knepley   if (numFields) {
56724acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
56734acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
56744acb8e1eSToby Isaac 
56754acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
56764acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
56774acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
56787c927364SMatthew G. Knepley     }
56794acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
56804acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
56814acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
56824acb8e1eSToby Isaac     }
56834acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
56844acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
56854acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
56864acb8e1eSToby Isaac     }
56874acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
56884acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
56894acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
56907c927364SMatthew G. Knepley     }
56917c927364SMatthew G. Knepley   } else {
56924acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
56934acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
56944acb8e1eSToby Isaac 
56954acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
56964acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
56974acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
56984acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
56994acb8e1eSToby Isaac 
57004acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
57014acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);
57027c927364SMatthew G. Knepley     }
57034acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
57044acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
57054acb8e1eSToby Isaac 
57064acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
57074acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);
57087c927364SMatthew G. Knepley     }
57094acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
57104acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
57117c927364SMatthew G. Knepley   }
57127c927364SMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
57137c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
57147c927364SMatthew G. Knepley   PetscFunctionReturn(0);
57157c927364SMatthew G. Knepley }
57167c927364SMatthew G. Knepley 
57177c927364SMatthew G. Knepley #undef __FUNCT__
5718770b213bSMatthew G Knepley #define __FUNCT__ "DMPlexGetHybridBounds"
5719f96281f8SMatthew G. Knepley /*@
5720f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
5721f96281f8SMatthew G. Knepley 
5722f96281f8SMatthew G. Knepley   Input Parameter:
5723f96281f8SMatthew G. Knepley . dm - The DMPlex object
5724f96281f8SMatthew G. Knepley 
5725f96281f8SMatthew G. Knepley   Output Parameters:
5726f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
5727dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5728dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5729dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5730f96281f8SMatthew G. Knepley 
5731f96281f8SMatthew G. Knepley   Level: developer
5732f96281f8SMatthew G. Knepley 
5733dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
5734f96281f8SMatthew G. Knepley @*/
5735770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
5736552f7358SJed Brown {
5737552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5738770b213bSMatthew G Knepley   PetscInt       dim;
5739770b213bSMatthew G Knepley   PetscErrorCode ierr;
5740552f7358SJed Brown 
5741552f7358SJed Brown   PetscFunctionBegin;
5742552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5743c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5744770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
5745770b213bSMatthew G Knepley   if (fMax) *fMax = mesh->hybridPointMax[dim-1];
5746770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
5747770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
5748552f7358SJed Brown   PetscFunctionReturn(0);
5749552f7358SJed Brown }
5750552f7358SJed Brown 
5751552f7358SJed Brown #undef __FUNCT__
5752770b213bSMatthew G Knepley #define __FUNCT__ "DMPlexSetHybridBounds"
5753dc5a3409SMatthew G. Knepley /*@
5754dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
5755dc5a3409SMatthew G. Knepley 
5756dc5a3409SMatthew G. Knepley   Input Parameters:
5757dc5a3409SMatthew G. Knepley . dm   - The DMPlex object
5758dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
5759dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5760dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5761dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5762dc5a3409SMatthew G. Knepley 
5763dc5a3409SMatthew G. Knepley   Level: developer
5764dc5a3409SMatthew G. Knepley 
5765dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
5766dc5a3409SMatthew G. Knepley @*/
5767770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
5768552f7358SJed Brown {
5769552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5770770b213bSMatthew G Knepley   PetscInt       dim;
5771770b213bSMatthew G Knepley   PetscErrorCode ierr;
5772552f7358SJed Brown 
5773552f7358SJed Brown   PetscFunctionBegin;
5774552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5775c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5776770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]   = cMax;
5777770b213bSMatthew G Knepley   if (fMax >= 0) mesh->hybridPointMax[dim-1] = fMax;
5778770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]     = eMax;
5779770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]     = vMax;
5780552f7358SJed Brown   PetscFunctionReturn(0);
5781552f7358SJed Brown }
5782552f7358SJed Brown 
5783552f7358SJed Brown #undef __FUNCT__
5784552f7358SJed Brown #define __FUNCT__ "DMPlexGetVTKCellHeight"
5785552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
5786552f7358SJed Brown {
5787552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5788552f7358SJed Brown 
5789552f7358SJed Brown   PetscFunctionBegin;
5790552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5791552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
5792552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
5793552f7358SJed Brown   PetscFunctionReturn(0);
5794552f7358SJed Brown }
5795552f7358SJed Brown 
5796552f7358SJed Brown #undef __FUNCT__
5797552f7358SJed Brown #define __FUNCT__ "DMPlexSetVTKCellHeight"
5798552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
5799552f7358SJed Brown {
5800552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5801552f7358SJed Brown 
5802552f7358SJed Brown   PetscFunctionBegin;
5803552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5804552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
5805552f7358SJed Brown   PetscFunctionReturn(0);
5806552f7358SJed Brown }
5807552f7358SJed Brown 
5808552f7358SJed Brown #undef __FUNCT__
5809552f7358SJed Brown #define __FUNCT__ "DMPlexCreateNumbering_Private"
5810552f7358SJed Brown /* We can easily have a form that takes an IS instead */
5811ef48cebcSMatthew G. Knepley static PetscErrorCode DMPlexCreateNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
5812552f7358SJed Brown {
5813552f7358SJed Brown   PetscSection   section, globalSection;
5814552f7358SJed Brown   PetscInt      *numbers, p;
5815552f7358SJed Brown   PetscErrorCode ierr;
5816552f7358SJed Brown 
5817552f7358SJed Brown   PetscFunctionBegin;
581882f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
5819552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
5820552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5821552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
5822552f7358SJed Brown   }
5823552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
582415b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
5825854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
5826552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5827552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
5828ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
5829ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
5830552f7358SJed Brown   }
583182f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
5832ef48cebcSMatthew G. Knepley   if (globalSize) {
5833ef48cebcSMatthew G. Knepley     PetscLayout layout;
5834ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
5835ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
5836ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
5837ef48cebcSMatthew G. Knepley   }
5838552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
5839552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
5840552f7358SJed Brown   PetscFunctionReturn(0);
5841552f7358SJed Brown }
5842552f7358SJed Brown 
5843552f7358SJed Brown #undef __FUNCT__
584481ed3555SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateCellNumbering_Internal"
584581ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
5846552f7358SJed Brown {
5847552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
5848552f7358SJed Brown   PetscErrorCode ierr;
5849552f7358SJed Brown 
5850552f7358SJed Brown   PetscFunctionBegin;
5851552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
5852552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
58530298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
585481ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
585581ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
585681ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
5857552f7358SJed Brown }
585881ed3555SMatthew G. Knepley 
585981ed3555SMatthew G. Knepley #undef __FUNCT__
586081ed3555SMatthew G. Knepley #define __FUNCT__ "DMPlexGetCellNumbering"
586181ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
586281ed3555SMatthew G. Knepley {
586381ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
586481ed3555SMatthew G. Knepley   PetscErrorCode ierr;
586581ed3555SMatthew G. Knepley 
586681ed3555SMatthew G. Knepley   PetscFunctionBegin;
586781ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
586881ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
5869552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
5870552f7358SJed Brown   PetscFunctionReturn(0);
5871552f7358SJed Brown }
5872552f7358SJed Brown 
5873552f7358SJed Brown #undef __FUNCT__
587481ed3555SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateVertexNumbering_Internal"
587581ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
587681ed3555SMatthew G. Knepley {
587781ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
587881ed3555SMatthew G. Knepley   PetscErrorCode ierr;
587981ed3555SMatthew G. Knepley 
588081ed3555SMatthew G. Knepley   PetscFunctionBegin;
588181ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
588281ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
588381ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
588481ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
588581ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
588681ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
588781ed3555SMatthew G. Knepley }
588881ed3555SMatthew G. Knepley 
588981ed3555SMatthew G. Knepley #undef __FUNCT__
5890552f7358SJed Brown #define __FUNCT__ "DMPlexGetVertexNumbering"
5891552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
5892552f7358SJed Brown {
5893552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5894552f7358SJed Brown   PetscErrorCode ierr;
5895552f7358SJed Brown 
5896552f7358SJed Brown   PetscFunctionBegin;
5897552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
589881ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
5899552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
5900552f7358SJed Brown   PetscFunctionReturn(0);
5901552f7358SJed Brown }
5902552f7358SJed Brown 
5903ef48cebcSMatthew G. Knepley #undef __FUNCT__
5904ef48cebcSMatthew G. Knepley #define __FUNCT__ "DMPlexCreatePointNumbering"
5905ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
5906ef48cebcSMatthew G. Knepley {
5907ef48cebcSMatthew G. Knepley   IS             nums[4];
5908ef48cebcSMatthew G. Knepley   PetscInt       depths[4];
5909ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
5910ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
5911ef48cebcSMatthew G. Knepley 
5912ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
5913ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5914ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
59158abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
59168abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
5917ef48cebcSMatthew G. Knepley   depths[0] = depth; depths[1] = 0;
5918ef48cebcSMatthew G. Knepley   for (d = 2; d <= depth; ++d) depths[d] = depth-d+1;
5919ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
5920ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
5921ef48cebcSMatthew G. Knepley 
5922ef48cebcSMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, depths[d], &pStart, &pEnd);CHKERRQ(ierr);
5923ef48cebcSMatthew G. Knepley     ierr = DMPlexCreateNumbering_Private(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
5924ef48cebcSMatthew G. Knepley     shift += gsize;
5925ef48cebcSMatthew G. Knepley   }
5926302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
5927ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
5928ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
5929ef48cebcSMatthew G. Knepley }
5930ef48cebcSMatthew G. Knepley 
5931ca8062c8SMatthew G. Knepley #undef __FUNCT__
5932ca8062c8SMatthew G. Knepley #define __FUNCT__ "DMPlexCheckSymmetry"
5933ca8062c8SMatthew G. Knepley /*@
5934ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
5935ca8062c8SMatthew G. Knepley 
5936ca8062c8SMatthew G. Knepley   Input Parameters:
5937ca8062c8SMatthew G. Knepley   + dm - The DMPlex object
5938ca8062c8SMatthew G. Knepley 
5939ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
5940ca8062c8SMatthew G. Knepley 
5941ca8062c8SMatthew G. Knepley   Level: developer
5942ca8062c8SMatthew G. Knepley 
59439bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSkeleton(), DMCheckFaces()
5944ca8062c8SMatthew G. Knepley @*/
5945ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
5946ca8062c8SMatthew G. Knepley {
5947ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
5948ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
5949ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
5950ca8062c8SMatthew G. Knepley   PetscInt        pStart, pEnd, p, csize, ssize;
5951ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
5952ca8062c8SMatthew G. Knepley 
5953ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
5954ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5955ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
5956ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
5957ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
5958ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
5959ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
5960ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
5961ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
5962ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
596342e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
596442e66dfaSMatthew G. Knepley       PetscInt  d;
596542e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
596642e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
596742e66dfaSMatthew G. Knepley       }
5968ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
5969ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
5970ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
5971ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
5972ca8062c8SMatthew G. Knepley       }
597342e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
59748ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
5975ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
59768ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
5977ca8062c8SMatthew G. Knepley         }
5978302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
59798ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
5980ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
59818ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
5982ca8062c8SMatthew G. Knepley         }
5983302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
59848ccfff9cSToby Isaac         if (dup) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not repeatedly found in support of repeated cone point %D", p, cone[c]);
59858ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
5986ca8062c8SMatthew G. Knepley       }
598742e66dfaSMatthew G. Knepley     }
5988ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
5989ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
5990ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
5991ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
5992ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
5993ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
5994ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
5995ca8062c8SMatthew G. Knepley       }
5996ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
59978ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
5998ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
59998ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
6000ca8062c8SMatthew G. Knepley         }
6001302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
60028ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
6003ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
60048ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
6005ca8062c8SMatthew G. Knepley         }
6006302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
60078ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
6008ca8062c8SMatthew G. Knepley       }
6009ca8062c8SMatthew G. Knepley     }
6010ca8062c8SMatthew G. Knepley   }
6011ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
6012ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
60138ccfff9cSToby Isaac   if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
6014ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6015ca8062c8SMatthew G. Knepley }
6016ca8062c8SMatthew G. Knepley 
6017ca8062c8SMatthew G. Knepley #undef __FUNCT__
6018ca8062c8SMatthew G. Knepley #define __FUNCT__ "DMPlexCheckSkeleton"
6019ca8062c8SMatthew G. Knepley /*@
6020ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
6021ca8062c8SMatthew G. Knepley 
6022ca8062c8SMatthew G. Knepley   Input Parameters:
6023ca8062c8SMatthew G. Knepley + dm - The DMPlex object
602458723a97SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
602558723a97SMatthew G. Knepley - cellHeight - Normally 0
6026ca8062c8SMatthew G. Knepley 
6027ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6028ca8062c8SMatthew G. Knepley 
6029ca8062c8SMatthew G. Knepley   Level: developer
6030ca8062c8SMatthew G. Knepley 
60319bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckFaces()
6032ca8062c8SMatthew G. Knepley @*/
603358723a97SMatthew G. Knepley PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscBool isSimplex, PetscInt cellHeight)
6034ca8062c8SMatthew G. Knepley {
603542363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
6036ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
6037ca8062c8SMatthew G. Knepley 
6038ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6039ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6040c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6041ca8062c8SMatthew G. Knepley   switch (dim) {
604242363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
604342363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
604442363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
6045ca8062c8SMatthew G. Knepley   default:
60468ccfff9cSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
6047ca8062c8SMatthew G. Knepley   }
604858723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
604958723a97SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
6050ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
6051ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
6052ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
605358723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
605458723a97SMatthew G. Knepley 
605558723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
605658723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
605758723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
605858723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
605958723a97SMatthew G. Knepley     }
606058723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
60618ccfff9cSToby Isaac     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
6062ca8062c8SMatthew G. Knepley   }
606342363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
606442363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
606542363296SMatthew G. Knepley 
606642363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
606742363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
606842363296SMatthew G. Knepley       const PetscInt p = closure[cl];
606942363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
607042363296SMatthew G. Knepley     }
607142363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
60728ccfff9cSToby Isaac     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
607342363296SMatthew G. Knepley   }
6074ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6075ca8062c8SMatthew G. Knepley }
60769bf0dad6SMatthew G. Knepley 
60779bf0dad6SMatthew G. Knepley #undef __FUNCT__
60789bf0dad6SMatthew G. Knepley #define __FUNCT__ "DMPlexCheckFaces"
60799bf0dad6SMatthew G. Knepley /*@
60809bf0dad6SMatthew G. Knepley   DMPlexCheckFaces - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type
60819bf0dad6SMatthew G. Knepley 
60829bf0dad6SMatthew G. Knepley   Input Parameters:
60839bf0dad6SMatthew G. Knepley + dm - The DMPlex object
60849bf0dad6SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
60859bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
60869bf0dad6SMatthew G. Knepley 
60879bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
60889bf0dad6SMatthew G. Knepley 
60899bf0dad6SMatthew G. Knepley   Level: developer
60909bf0dad6SMatthew G. Knepley 
60919bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckSkeleton()
60929bf0dad6SMatthew G. Knepley @*/
60939bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexCheckFaces(DM dm, PetscBool isSimplex, PetscInt cellHeight)
60949bf0dad6SMatthew G. Knepley {
60953554e41dSMatthew G. Knepley   PetscInt       pMax[4];
60963554e41dSMatthew G. Knepley   PetscInt       dim, vStart, vEnd, cStart, cEnd, c, h;
60979bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
60989bf0dad6SMatthew G. Knepley 
60999bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
61009bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6101c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
61029bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
610325abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
61043554e41dSMatthew G. Knepley   for (h = cellHeight; h < dim; ++h) {
61053554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
61063554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
61079bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
61089bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
61099bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
61109bf0dad6SMatthew G. Knepley 
611125abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
61129bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
61139bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
61149bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
61159bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
61169bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
61179bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
61189bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
61199bf0dad6SMatthew G. Knepley       }
61203554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
61218ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
61229bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
61239bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
61249bf0dad6SMatthew G. Knepley 
61259bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
61269bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
61279bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
61289bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
61299bf0dad6SMatthew G. Knepley         }
61308ccfff9cSToby Isaac         if (fnumCorners != faceSize) SETERRQ5(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D (%D) of cell %D has %D vertices but should have %D", cone[f], f, c, fnumCorners, faceSize);
61319bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
61328ccfff9cSToby Isaac           if (fclosure[v] != faces[f*faceSize+v]) SETERRQ6(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D (%d) of cell %D vertex %D, %D != %D", cone[f], f, c, v, fclosure[v], faces[f*faceSize+v]);
61339bf0dad6SMatthew G. Knepley         }
61349bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
61359bf0dad6SMatthew G. Knepley       }
61369bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
61379bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
61389bf0dad6SMatthew G. Knepley     }
61393554e41dSMatthew G. Knepley   }
6140552f7358SJed Brown   PetscFunctionReturn(0);
6141552f7358SJed Brown }
61423913d7c8SMatthew G. Knepley 
61433913d7c8SMatthew G. Knepley #undef __FUNCT__
6144bceba477SMatthew G. Knepley #define __FUNCT__ "DMCreateInterpolation_Plex"
6145bceba477SMatthew G. Knepley /* Pointwise interpolation
6146bceba477SMatthew G. Knepley      Just code FEM for now
6147bceba477SMatthew G. Knepley      u^f = I u^c
61484ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
61494ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
61504ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
6151bceba477SMatthew G. Knepley */
6152bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
6153bceba477SMatthew G. Knepley {
6154bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
6155bceba477SMatthew G. Knepley   PetscInt       m, n;
6156a063dac3SMatthew G. Knepley   void          *ctx;
615768132eb9SMatthew G. Knepley   DM             cdm;
615868132eb9SMatthew G. Knepley   PetscBool      regular;
6159bceba477SMatthew G. Knepley   PetscErrorCode ierr;
6160bceba477SMatthew G. Knepley 
6161bceba477SMatthew G. Knepley   PetscFunctionBegin;
6162bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6163bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6164bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6165bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
616668132eb9SMatthew G. Knepley 
6167bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
6168bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6169bceba477SMatthew G. Knepley   ierr = MatSetType(*interpolation, dmCoarse->mattype);CHKERRQ(ierr);
6170a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
617168132eb9SMatthew G. Knepley 
6172a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
617368132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
617468132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
617568132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
617668132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
61775d1c2e58SMatthew G. Knepley   /* Use naive scaling */
61785d1c2e58SMatthew G. Knepley   ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
6179a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
6180a063dac3SMatthew G. Knepley }
6181bceba477SMatthew G. Knepley 
6182a063dac3SMatthew G. Knepley #undef __FUNCT__
6183a063dac3SMatthew G. Knepley #define __FUNCT__ "DMCreateInjection_Plex"
61846dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
6185a063dac3SMatthew G. Knepley {
618690748bafSMatthew G. Knepley   PetscErrorCode ierr;
61876dbf9973SLawrence Mitchell   VecScatter     ctx;
618890748bafSMatthew G. Knepley 
6189a063dac3SMatthew G. Knepley   PetscFunctionBegin;
61906dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
61916dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
61926dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
6193bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6194bceba477SMatthew G. Knepley }
6195bceba477SMatthew G. Knepley 
6196bceba477SMatthew G. Knepley #undef __FUNCT__
6197fd59a867SMatthew G. Knepley #define __FUNCT__ "DMCreateDefaultSection_Plex"
6198fd59a867SMatthew G. Knepley PetscErrorCode DMCreateDefaultSection_Plex(DM dm)
6199fd59a867SMatthew G. Knepley {
6200fd59a867SMatthew G. Knepley   PetscSection   section;
6201ab1d0545SMatthew G. Knepley   IS            *bcPoints, *bcComps;
6202ebb3236fSMatthew G. Knepley   PetscBool     *isFE;
6203286d8613SMatthew G. Knepley   PetscInt      *bcFields, *numComp, *numDof;
6204ebb3236fSMatthew G. Knepley   PetscInt       depth, dim, numBd, numBC = 0, numFields, bd, bc = 0, f;
6205ebb3236fSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
6206fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
6207fd59a867SMatthew G. Knepley 
6208fd59a867SMatthew G. Knepley   PetscFunctionBegin;
6209ebb3236fSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
6210ae71db08SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
6211ebb3236fSMatthew G. Knepley   /* FE and FV boundary conditions are handled slightly differently */
6212ebb3236fSMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
6213ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6214ebb3236fSMatthew G. Knepley     PetscObject  obj;
6215ebb3236fSMatthew G. Knepley     PetscClassId id;
6216ebb3236fSMatthew G. Knepley 
6217ebb3236fSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6218ebb3236fSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
6219ebb3236fSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
6220ebb3236fSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
62218ccfff9cSToby Isaac     else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
6222ebb3236fSMatthew G. Knepley   }
62232f900235SMatthew G. Knepley   /* Allocate boundary point storage for FEM boundaries */
6224286d8613SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
6225c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6226ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
6227ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
6228dff059c6SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob, &numBd);CHKERRQ(ierr);
6229fd59a867SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
62302f900235SMatthew G. Knepley     PetscInt                field;
6231f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6232385532a5SToby Isaac     const char             *labelName;
6233385532a5SToby Isaac     DMLabel                 label;
62342f900235SMatthew G. Knepley 
6235f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &labelName, &field, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
6236385532a5SToby Isaac     ierr = DMGetLabel(dm,labelName,&label);CHKERRQ(ierr);
6237f971fd6bSMatthew G. Knepley     if (label && isFE[field] && (type & DM_BC_ESSENTIAL)) ++numBC;
6238fd59a867SMatthew G. Knepley   }
62392f900235SMatthew G. Knepley   /* Add ghost cell boundaries for FVM */
6240ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) if (!isFE[f] && cEndInterior >= 0) ++numBC;
62416c8d74c4SMatthew G. Knepley   ierr = PetscCalloc3(numBC,&bcFields,numBC,&bcPoints,numBC,&bcComps);CHKERRQ(ierr);
6242ebb3236fSMatthew G. Knepley   /* Constrain ghost cells for FV */
6243ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6244ebb3236fSMatthew G. Knepley     PetscInt *newidx, c;
6245ebb3236fSMatthew G. Knepley 
6246ebb3236fSMatthew G. Knepley     if (isFE[f] || cEndInterior < 0) continue;
6247ebb3236fSMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cEndInterior,&newidx);CHKERRQ(ierr);
6248ebb3236fSMatthew G. Knepley     for (c = cEndInterior; c < cEnd; ++c) newidx[c-cEndInterior] = c;
6249ebb3236fSMatthew G. Knepley     bcFields[bc] = f;
6250ebb3236fSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), cEnd-cEndInterior, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6251ebb3236fSMatthew G. Knepley   }
6252ebb3236fSMatthew G. Knepley   /* Handle FEM Dirichlet boundaries */
6253ebb3236fSMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
6254fd59a867SMatthew G. Knepley     const char             *bdLabel;
6255fd59a867SMatthew G. Knepley     DMLabel                 label;
62560e0f25f2SMatthew G. Knepley     const PetscInt         *comps;
6257fd59a867SMatthew G. Knepley     const PetscInt         *values;
62580e0f25f2SMatthew G. Knepley     PetscInt                bd2, field, numComps, numValues;
6259f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6260f971fd6bSMatthew G. Knepley     PetscBool               duplicate = PETSC_FALSE;
6261fd59a867SMatthew G. Knepley 
6262f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &bdLabel, &field, &numComps, &comps, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
6263c58f1c22SToby Isaac     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
6264385532a5SToby Isaac     if (!isFE[field] || !label) continue;
6265ebb3236fSMatthew G. Knepley     /* Only want to modify label once */
62667391c1cbSMatthew G. Knepley     for (bd2 = 0; bd2 < bd; ++bd2) {
62677391c1cbSMatthew G. Knepley       const char *bdname;
6268dff059c6SToby Isaac       ierr = PetscDSGetBoundary(dm->prob, bd2, NULL, NULL, &bdname, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
62697391c1cbSMatthew G. Knepley       ierr = PetscStrcmp(bdname, bdLabel, &duplicate);CHKERRQ(ierr);
62707391c1cbSMatthew G. Knepley       if (duplicate) break;
62717391c1cbSMatthew G. Knepley     }
6272ebb3236fSMatthew G. Knepley     if (!duplicate && (isFE[field])) {
6273b0bf5782SToby Isaac       /* don't complete cells, which are just present to give orientation to the boundary */
6274092e5057SToby Isaac       ierr = DMPlexLabelComplete(dm, label);CHKERRQ(ierr);
62757391c1cbSMatthew G. Knepley     }
6276ebb3236fSMatthew G. Knepley     /* Filter out cells, if you actually want to constrain cells you need to do things by hand right now */
6277f971fd6bSMatthew G. Knepley     if (type & DM_BC_ESSENTIAL) {
627893a5981aSMatthew G. Knepley       PetscInt       *newidx;
6279ebb3236fSMatthew G. Knepley       PetscInt        n, newn = 0, p, v;
628093a5981aSMatthew G. Knepley 
6281fd59a867SMatthew G. Knepley       bcFields[bc] = field;
62820e0f25f2SMatthew G. Knepley       if (numComps) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), numComps, comps, PETSC_COPY_VALUES, &bcComps[bc]);CHKERRQ(ierr);}
6283ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6284ebb3236fSMatthew G. Knepley         IS              tmp;
6285ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6286ebb3236fSMatthew G. Knepley 
6287c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6288ebb3236fSMatthew G. Knepley         if (!tmp) continue;
628993a5981aSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
629093a5981aSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6291ebb3236fSMatthew G. Knepley         if (isFE[field]) {
629293a5981aSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) ++newn;
6293ebb3236fSMatthew G. Knepley         } else {
6294ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) ++newn;
6295ebb3236fSMatthew G. Knepley         }
629693a5981aSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
629793a5981aSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6298fd59a867SMatthew G. Knepley       }
6299ebb3236fSMatthew G. Knepley       ierr = PetscMalloc1(newn,&newidx);CHKERRQ(ierr);
6300ebb3236fSMatthew G. Knepley       newn = 0;
6301ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6302ebb3236fSMatthew G. Knepley         IS              tmp;
6303ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6304ebb3236fSMatthew G. Knepley 
6305c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6306ebb3236fSMatthew G. Knepley         if (!tmp) continue;
6307ebb3236fSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
6308ebb3236fSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6309ebb3236fSMatthew G. Knepley         if (isFE[field]) {
6310ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) newidx[newn++] = idx[p];
6311ebb3236fSMatthew G. Knepley         } else {
6312ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) newidx[newn++] = idx[p];
6313ebb3236fSMatthew G. Knepley         }
6314ebb3236fSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
6315ebb3236fSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6316ebb3236fSMatthew G. Knepley       }
6317ebb3236fSMatthew G. Knepley       ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), newn, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6318ebb3236fSMatthew G. Knepley     }
6319fd59a867SMatthew G. Knepley   }
6320fd59a867SMatthew G. Knepley   /* Handle discretization */
6321ebb3236fSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&numComp,numFields*(dim+1),&numDof);CHKERRQ(ierr);
6322fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
63230bacfa0aSMatthew G. Knepley     PetscObject obj;
63240bacfa0aSMatthew G. Knepley 
63250bacfa0aSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6326ebb3236fSMatthew G. Knepley     if (isFE[f]) {
63270bacfa0aSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
6328286d8613SMatthew G. Knepley       const PetscInt *numFieldDof;
6329fd59a867SMatthew G. Knepley       PetscInt        d;
6330fd59a867SMatthew G. Knepley 
6331fd59a867SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
6332286d8613SMatthew G. Knepley       ierr = PetscFEGetNumDof(fe, &numFieldDof);CHKERRQ(ierr);
6333286d8613SMatthew G. Knepley       for (d = 0; d < dim+1; ++d) numDof[f*(dim+1)+d] = numFieldDof[d];
6334ebb3236fSMatthew G. Knepley     } else {
63350bacfa0aSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
63360bacfa0aSMatthew G. Knepley 
63370bacfa0aSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
63380bacfa0aSMatthew G. Knepley       numDof[f*(dim+1)+dim] = numComp[f];
6339ebb3236fSMatthew G. Knepley     }
6340fd59a867SMatthew G. Knepley   }
6341286d8613SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6342286d8613SMatthew G. Knepley     PetscInt d;
6343286d8613SMatthew G. Knepley     for (d = 1; d < dim; ++d) {
6344286d8613SMatthew G. Knepley       if ((numDof[f*(dim+1)+d] > 0) && (depth < dim)) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Mesh must be interpolated when unknowns are specified on edges or faces.");
6345286d8613SMatthew G. Knepley     }
6346286d8613SMatthew G. Knepley   }
6347ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSection(dm, dim, numFields, numComp, numDof, numBC, bcFields, bcComps, bcPoints, NULL, &section);CHKERRQ(ierr);
6348fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6349fd59a867SMatthew G. Knepley     PetscFE     fe;
6350fd59a867SMatthew G. Knepley     const char *name;
635118abd763SMatthew G. Knepley 
635218abd763SMatthew G. Knepley     ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
6353fd59a867SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
6354fd59a867SMatthew G. Knepley     ierr = PetscSectionSetFieldName(section, f, name);CHKERRQ(ierr);
6355fd59a867SMatthew G. Knepley   }
6356fd59a867SMatthew G. Knepley   ierr = DMSetDefaultSection(dm, section);CHKERRQ(ierr);
6357fd59a867SMatthew G. Knepley   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
63580e0f25f2SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {ierr = ISDestroy(&bcPoints[bc]);CHKERRQ(ierr);ierr = ISDestroy(&bcComps[bc]);CHKERRQ(ierr);}
63590e0f25f2SMatthew G. Knepley   ierr = PetscFree3(bcFields,bcPoints,bcComps);CHKERRQ(ierr);
6360286d8613SMatthew G. Knepley   ierr = PetscFree2(numComp,numDof);CHKERRQ(ierr);
6361ebb3236fSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
6362bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6363bceba477SMatthew G. Knepley }
6364bceba477SMatthew G. Knepley 
6365bceba477SMatthew G. Knepley #undef __FUNCT__
63660aef6b92SMatthew G. Knepley #define __FUNCT__ "DMPlexGetRegularRefinement"
63670aef6b92SMatthew G. Knepley /*@
63680aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
63690aef6b92SMatthew G. Knepley 
63700aef6b92SMatthew G. Knepley   Input Parameter:
63710aef6b92SMatthew G. Knepley . dm - The DMPlex object
63720aef6b92SMatthew G. Knepley 
63730aef6b92SMatthew G. Knepley   Output Parameter:
63740aef6b92SMatthew G. Knepley . regular - The flag
63750aef6b92SMatthew G. Knepley 
63760aef6b92SMatthew G. Knepley   Level: intermediate
63770aef6b92SMatthew G. Knepley 
63780aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
63790aef6b92SMatthew G. Knepley @*/
63800aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
63810aef6b92SMatthew G. Knepley {
63820aef6b92SMatthew G. Knepley   PetscFunctionBegin;
63830aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63840aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
63850aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
63860aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
63870aef6b92SMatthew G. Knepley }
63880aef6b92SMatthew G. Knepley 
63890aef6b92SMatthew G. Knepley #undef __FUNCT__
63900aef6b92SMatthew G. Knepley #define __FUNCT__ "DMPlexSetRegularRefinement"
63910aef6b92SMatthew G. Knepley /*@
63920aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
63930aef6b92SMatthew G. Knepley 
63940aef6b92SMatthew G. Knepley   Input Parameters:
63950aef6b92SMatthew G. Knepley + dm - The DMPlex object
63960aef6b92SMatthew G. Knepley - regular - The flag
63970aef6b92SMatthew G. Knepley 
63980aef6b92SMatthew G. Knepley   Level: intermediate
63990aef6b92SMatthew G. Knepley 
64000aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
64010aef6b92SMatthew G. Knepley @*/
64020aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
64030aef6b92SMatthew G. Knepley {
64040aef6b92SMatthew G. Knepley   PetscFunctionBegin;
64050aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64060aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
64070aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
64080aef6b92SMatthew G. Knepley }
64090aef6b92SMatthew G. Knepley 
6410f7c74593SToby Isaac /* anchors */
6411a68b90caSToby Isaac #undef __FUNCT__
6412a17985deSToby Isaac #define __FUNCT__ "DMPlexGetAnchors"
6413a68b90caSToby Isaac /*@
6414f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
6415f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
6416a68b90caSToby Isaac 
6417e228b242SToby Isaac   not collective
6418a68b90caSToby Isaac 
6419a68b90caSToby Isaac   Input Parameters:
6420a68b90caSToby Isaac . dm - The DMPlex object
6421a68b90caSToby Isaac 
6422a68b90caSToby Isaac   Output Parameters:
6423a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
6424a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
6425a68b90caSToby Isaac 
6426a68b90caSToby Isaac 
6427a68b90caSToby Isaac   Level: intermediate
6428a68b90caSToby Isaac 
6429f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
6430a68b90caSToby Isaac @*/
6431a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
6432a68b90caSToby Isaac {
6433a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
643441e6d900SToby Isaac   PetscErrorCode ierr;
6435a68b90caSToby Isaac 
6436a68b90caSToby Isaac   PetscFunctionBegin;
6437a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
643841e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
6439a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
6440a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
6441a68b90caSToby Isaac   PetscFunctionReturn(0);
6442a68b90caSToby Isaac }
6443a68b90caSToby Isaac 
6444a68b90caSToby Isaac #undef __FUNCT__
6445a17985deSToby Isaac #define __FUNCT__ "DMPlexSetAnchors"
6446a68b90caSToby Isaac /*@
6447f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
6448f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
6449a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
6450a68b90caSToby Isaac 
6451a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
6452f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
6453a68b90caSToby Isaac 
6454e228b242SToby Isaac   collective on dm
6455a68b90caSToby Isaac 
6456a68b90caSToby Isaac   Input Parameters:
6457a68b90caSToby Isaac + dm - The DMPlex object
6458e228b242SToby Isaac . anchorSection - The section that describes the mapping from constrained points to the anchor points listed in anchorIS.  Must have a local communicator (PETSC_COMM_SELF or derivative).
6459e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
6460a68b90caSToby Isaac 
6461a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
6462a68b90caSToby Isaac 
6463a68b90caSToby Isaac   Level: intermediate
6464a68b90caSToby Isaac 
6465f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
6466a68b90caSToby Isaac @*/
6467a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
6468a68b90caSToby Isaac {
6469a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6470e228b242SToby Isaac   PetscMPIInt    result;
6471a68b90caSToby Isaac   PetscErrorCode ierr;
6472a68b90caSToby Isaac 
6473a68b90caSToby Isaac   PetscFunctionBegin;
6474a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6475e228b242SToby Isaac   if (anchorSection) {
6476e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
6477e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
6478e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
6479e228b242SToby Isaac   }
6480e228b242SToby Isaac   if (anchorIS) {
6481e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
6482e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
6483e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
6484e228b242SToby Isaac   }
6485a68b90caSToby Isaac 
6486a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
6487a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
6488a68b90caSToby Isaac   plex->anchorSection = anchorSection;
6489a68b90caSToby Isaac 
6490a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
6491a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
6492a68b90caSToby Isaac   plex->anchorIS = anchorIS;
6493a68b90caSToby Isaac 
6494a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
6495a68b90caSToby Isaac   if (anchorIS && anchorSection) {
6496a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
6497a68b90caSToby Isaac     const PetscInt *anchors;
6498a68b90caSToby Isaac 
6499a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
6500a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
6501a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
6502a68b90caSToby Isaac     for (a = 0; a < size; a++) {
6503a68b90caSToby Isaac       PetscInt p;
6504a68b90caSToby Isaac 
6505a68b90caSToby Isaac       p = anchors[a];
6506a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
6507a68b90caSToby Isaac         PetscInt dof;
6508a68b90caSToby Isaac 
6509a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6510a68b90caSToby Isaac         if (dof) {
6511a68b90caSToby Isaac           PetscErrorCode ierr2;
6512a68b90caSToby Isaac 
6513a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
65148ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
6515a68b90caSToby Isaac         }
6516a68b90caSToby Isaac       }
6517a68b90caSToby Isaac     }
6518a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
6519a68b90caSToby Isaac   }
6520a68b90caSToby Isaac #endif
6521f7c74593SToby Isaac   /* reset the generic constraints */
6522f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
6523a68b90caSToby Isaac   PetscFunctionReturn(0);
6524a68b90caSToby Isaac }
6525a68b90caSToby Isaac 
6526a68b90caSToby Isaac #undef __FUNCT__
6527f7c74593SToby Isaac #define __FUNCT__ "DMPlexCreateConstraintSection_Anchors"
6528f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
6529a68b90caSToby Isaac {
6530f7c74593SToby Isaac   PetscSection anchorSection;
65316995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
6532a68b90caSToby Isaac   PetscErrorCode ierr;
6533a68b90caSToby Isaac 
6534a68b90caSToby Isaac   PetscFunctionBegin;
6535a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6536a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
6537e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
6538a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
65396995de1eSToby Isaac   if (numFields) {
6540719ab38cSToby Isaac     PetscInt f;
6541a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
6542719ab38cSToby Isaac 
6543719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
6544719ab38cSToby Isaac       PetscInt numComp;
6545719ab38cSToby Isaac 
6546719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
6547719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
6548719ab38cSToby Isaac     }
65496995de1eSToby Isaac   }
6550a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
65516995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
65526995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
65536995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
65546995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
6555a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
6556a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
6557a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6558a68b90caSToby Isaac     if (dof) {
6559a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
6560a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
6561a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
6562a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
6563a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
6564a68b90caSToby Isaac       }
6565a68b90caSToby Isaac     }
6566a68b90caSToby Isaac   }
6567a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
6568a68b90caSToby Isaac   PetscFunctionReturn(0);
6569a68b90caSToby Isaac }
6570a68b90caSToby Isaac 
6571a68b90caSToby Isaac #undef __FUNCT__
6572f7c74593SToby Isaac #define __FUNCT__ "DMPlexCreateConstraintMatrix_Anchors"
6573f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
6574a68b90caSToby Isaac {
6575f7c74593SToby Isaac   PetscSection aSec;
65760ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
65770ac89760SToby Isaac   const PetscInt *anchors;
65780ac89760SToby Isaac   PetscInt numFields, f;
657966ad2231SToby Isaac   IS aIS;
65800ac89760SToby Isaac   PetscErrorCode ierr;
65810ac89760SToby Isaac 
65820ac89760SToby Isaac   PetscFunctionBegin;
65830ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65840ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
65850ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
65860ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
65870ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
6588302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
6589a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
659066ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
65916995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
65926995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
65930ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
65940ac89760SToby Isaac   i[0] = 0;
65950ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
65960ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
6597f19733c5SToby Isaac     PetscInt rDof, rOff, r;
6598f19733c5SToby Isaac 
6599f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
6600f19733c5SToby Isaac     if (!rDof) continue;
6601f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
66020ac89760SToby Isaac     if (numFields) {
66030ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
66040ac89760SToby Isaac         annz = 0;
6605f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
6606f19733c5SToby Isaac           a = anchors[rOff + r];
66070ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
66080ac89760SToby Isaac           annz += aDof;
66090ac89760SToby Isaac         }
66100ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
66110ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
66120ac89760SToby Isaac         for (q = 0; q < dof; q++) {
66130ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
66140ac89760SToby Isaac         }
66150ac89760SToby Isaac       }
66160ac89760SToby Isaac     }
66170ac89760SToby Isaac     else {
66180ac89760SToby Isaac       annz = 0;
66190ac89760SToby Isaac       for (q = 0; q < dof; q++) {
66200ac89760SToby Isaac         a = anchors[off + q];
66210ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
66220ac89760SToby Isaac         annz += aDof;
66230ac89760SToby Isaac       }
66240ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
66250ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
66260ac89760SToby Isaac       for (q = 0; q < dof; q++) {
66270ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
66280ac89760SToby Isaac       }
66290ac89760SToby Isaac     }
66300ac89760SToby Isaac   }
66310ac89760SToby Isaac   nnz = i[m];
66320ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
66330ac89760SToby Isaac   offset = 0;
66340ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
66350ac89760SToby Isaac     if (numFields) {
66360ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
66370ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
66380ac89760SToby Isaac         for (q = 0; q < dof; q++) {
66390ac89760SToby Isaac           PetscInt rDof, rOff, r;
66400ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
66410ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
66420ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
66430ac89760SToby Isaac             PetscInt s;
66440ac89760SToby Isaac 
66450ac89760SToby Isaac             a = anchors[rOff + r];
66460ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
66470ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
66480ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
66490ac89760SToby Isaac               j[offset++] = aOff + s;
66500ac89760SToby Isaac             }
66510ac89760SToby Isaac           }
66520ac89760SToby Isaac         }
66530ac89760SToby Isaac       }
66540ac89760SToby Isaac     }
66550ac89760SToby Isaac     else {
66560ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
66570ac89760SToby Isaac       for (q = 0; q < dof; q++) {
66580ac89760SToby Isaac         PetscInt rDof, rOff, r;
66590ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
66600ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
66610ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
66620ac89760SToby Isaac           PetscInt s;
66630ac89760SToby Isaac 
66640ac89760SToby Isaac           a = anchors[rOff + r];
66650ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
66660ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
66670ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
66680ac89760SToby Isaac             j[offset++] = aOff + s;
66690ac89760SToby Isaac           }
66700ac89760SToby Isaac         }
66710ac89760SToby Isaac       }
66720ac89760SToby Isaac     }
66730ac89760SToby Isaac   }
66740ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
667525570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
667625570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
667766ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
66780ac89760SToby Isaac   PetscFunctionReturn(0);
66790ac89760SToby Isaac }
66800ac89760SToby Isaac 
66810ac89760SToby Isaac #undef __FUNCT__
668266ad2231SToby Isaac #define __FUNCT__ "DMCreateDefaultConstraints_Plex"
668366ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
668466ad2231SToby Isaac {
6685f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6686f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
668766ad2231SToby Isaac   Mat            cMat;
668866ad2231SToby Isaac   PetscErrorCode ierr;
668966ad2231SToby Isaac 
669066ad2231SToby Isaac   PetscFunctionBegin;
669166ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6692a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
669366ad2231SToby Isaac   if (anchorSection) {
6694e228b242SToby Isaac     PetscDS  ds;
6695e228b242SToby Isaac     PetscInt nf;
6696e228b242SToby Isaac 
6697f7c74593SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
6698f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
6699f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
6700e228b242SToby Isaac     ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
6701302440fdSBarry Smith     ierr = PetscDSGetNumFields(ds,&nf);CHKERRQ(ierr);
6702e228b242SToby Isaac     if (nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
670366ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
670466ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
670566ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
670666ad2231SToby Isaac   }
670766ad2231SToby Isaac   PetscFunctionReturn(0);
670866ad2231SToby Isaac }
6709