xref: /petsc/src/dm/impls/plex/plex.c (revision cf9ba882c728e7d21db95f386fb8a6e155ae9c36)
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__
51f13a32a3SMatthew G. Knepley #define __FUNCT__ "VecView_Plex_Local_Draw"
52f13a32a3SMatthew G. Knepley PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
53e412dcbdSMatthew G. Knepley {
54e412dcbdSMatthew G. Knepley   DM                 dm;
55e412dcbdSMatthew G. Knepley   PetscDraw          draw, popup;
56e412dcbdSMatthew G. Knepley   DM                 cdm;
57e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
58e412dcbdSMatthew G. Knepley   Vec                coordinates;
59e412dcbdSMatthew G. Knepley   const PetscScalar *coords, *array;
60e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
61e412dcbdSMatthew G. Knepley   PetscReal          min, max;
62e412dcbdSMatthew G. Knepley   PetscBool          isnull;
63e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N, level;
64e412dcbdSMatthew G. Knepley   const char        *name;
65e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
66e412dcbdSMatthew G. Knepley 
67e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
68e412dcbdSMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
69e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
70e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
71e412dcbdSMatthew G. Knepley   ierr = DMGetCoarsenLevel(dm, &level);CHKERRQ(ierr);
72e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
73e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
74e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
75e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
76e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
77e412dcbdSMatthew G. Knepley 
78e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
79e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
80e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
81e412dcbdSMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
82e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, name);CHKERRQ(ierr);
83e412dcbdSMatthew G. Knepley 
84e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
85e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
86e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
870c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
880c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
89e412dcbdSMatthew G. Knepley   }
90e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
91e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
92e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
93e412dcbdSMatthew G. Knepley 
94e412dcbdSMatthew G. Knepley   ierr = VecMin(v, NULL, &min);CHKERRQ(ierr);
95e412dcbdSMatthew G. Knepley   ierr = VecMax(v, NULL, &max);CHKERRQ(ierr);
96e412dcbdSMatthew G. Knepley   ierr = PetscDrawGetPopup(draw, &popup);CHKERRQ(ierr);
97e412dcbdSMatthew G. Knepley   ierr = PetscDrawScalePopup(popup, min, max);CHKERRQ(ierr);
98e412dcbdSMatthew G. Knepley 
99e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(v, &array);CHKERRQ(ierr);
100e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
10199a2f7bcSMatthew G. Knepley     PetscScalar *coords = NULL, *a = NULL;
102e412dcbdSMatthew G. Knepley     PetscInt     numCoords, color;
103e412dcbdSMatthew G. Knepley 
104e412dcbdSMatthew G. Knepley     ierr = DMPlexPointGlobalRead(dm, c, array, &a);CHKERRQ(ierr);
105e412dcbdSMatthew G. Knepley     color = PetscDrawRealToColor(PetscRealPart(a[0]), min, max);
106e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
107e412dcbdSMatthew G. Knepley     switch (numCoords) {
108e412dcbdSMatthew G. Knepley     case 6:
1090c81f2a8SMatthew 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);
110e412dcbdSMatthew G. Knepley       break;
111e412dcbdSMatthew G. Knepley     case 8:
1120c81f2a8SMatthew 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);
1130c81f2a8SMatthew 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);
114e412dcbdSMatthew G. Knepley       break;
115e412dcbdSMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
116e412dcbdSMatthew G. Knepley     }
117e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
118e412dcbdSMatthew G. Knepley   }
119e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &array);CHKERRQ(ierr);
120e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
121e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
122e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
123e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
124e412dcbdSMatthew G. Knepley }
125e412dcbdSMatthew G. Knepley 
126e412dcbdSMatthew G. Knepley #undef __FUNCT__
127f13a32a3SMatthew G. Knepley #define __FUNCT__ "VecView_Plex_Local"
128f13a32a3SMatthew G. Knepley PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
129f13a32a3SMatthew G. Knepley {
130f13a32a3SMatthew G. Knepley   DM             dm;
131f13a32a3SMatthew G. Knepley   PetscBool      isvtk, ishdf5, isdraw, isseq;
132f13a32a3SMatthew G. Knepley   PetscErrorCode ierr;
133f13a32a3SMatthew G. Knepley 
134f13a32a3SMatthew G. Knepley   PetscFunctionBegin;
135f13a32a3SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
136f13a32a3SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
137f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
138f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
139f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW, &isdraw);CHKERRQ(ierr);
140f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
141f13a32a3SMatthew G. Knepley   if (isvtk || ishdf5) {
142f13a32a3SMatthew G. Knepley     PetscInt  numFields;
143f13a32a3SMatthew G. Knepley     PetscBool fem = PETSC_FALSE;
144f13a32a3SMatthew G. Knepley 
145f13a32a3SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
146f13a32a3SMatthew G. Knepley     if (numFields) {
147f13a32a3SMatthew G. Knepley       PetscObject fe;
148f13a32a3SMatthew G. Knepley 
149f13a32a3SMatthew G. Knepley       ierr = DMGetField(dm, 0, &fe);CHKERRQ(ierr);
150f13a32a3SMatthew G. Knepley       if (fe->classid == PETSCFE_CLASSID) fem = PETSC_TRUE;
151f13a32a3SMatthew G. Knepley     }
152f13a32a3SMatthew G. Knepley     if (fem) {ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, v, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);}
153f13a32a3SMatthew G. Knepley   }
154f13a32a3SMatthew G. Knepley   if (isvtk) {
155f13a32a3SMatthew G. Knepley     PetscSection            section;
156f13a32a3SMatthew G. Knepley     PetscViewerVTKFieldType ft;
157f13a32a3SMatthew G. Knepley     PetscInt                pStart, pEnd;
158f13a32a3SMatthew G. Knepley 
159f13a32a3SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
160f13a32a3SMatthew G. Knepley     ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
161f13a32a3SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) v);CHKERRQ(ierr);  /* viewer drops reference */
162f13a32a3SMatthew G. Knepley     ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, ft, (PetscObject) v);CHKERRQ(ierr);
163f13a32a3SMatthew G. Knepley   } else if (ishdf5) {
164f13a32a3SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
165f13a32a3SMatthew G. Knepley     ierr = VecView_Plex_Local_HDF5(v, viewer);CHKERRQ(ierr);
166f13a32a3SMatthew G. Knepley #else
167f13a32a3SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
168f13a32a3SMatthew G. Knepley #endif
169f13a32a3SMatthew G. Knepley   } else if (isdraw) {
170f13a32a3SMatthew G. Knepley     ierr = VecView_Plex_Local_Draw(v, viewer);CHKERRQ(ierr);
171f13a32a3SMatthew G. Knepley   } else {
172f13a32a3SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
173f13a32a3SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
174f13a32a3SMatthew G. Knepley   }
175f13a32a3SMatthew G. Knepley   PetscFunctionReturn(0);
176f13a32a3SMatthew G. Knepley }
177f13a32a3SMatthew G. Knepley 
178f13a32a3SMatthew G. Knepley #undef __FUNCT__
179552f7358SJed Brown #define __FUNCT__ "VecView_Plex"
180552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
181552f7358SJed Brown {
182552f7358SJed Brown   DM             dm;
183f13a32a3SMatthew G. Knepley   PetscReal      time = 0.0;
184e412dcbdSMatthew G. Knepley   PetscBool      isvtk, ishdf5, isdraw, isseq;
185552f7358SJed Brown   PetscErrorCode ierr;
186552f7358SJed Brown 
187552f7358SJed Brown   PetscFunctionBegin;
188552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
18982f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
190552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
19133c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
192e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW, &isdraw);CHKERRQ(ierr);
19355f2e967SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
194552f7358SJed Brown   if (isvtk) {
195552f7358SJed Brown     Vec         locv;
196552f7358SJed Brown     const char *name;
197552f7358SJed Brown 
198552f7358SJed Brown     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
199552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
200552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
201552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
202552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
203*cf9ba882SMatthew G. Knepley     ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
204*cf9ba882SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
205552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
206552f7358SJed Brown     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
207b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
208b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
209b136c2c9SMatthew G. Knepley     ierr = VecView_Plex_HDF5(v, viewer);CHKERRQ(ierr);
210b136c2c9SMatthew G. Knepley #else
211b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
212b136c2c9SMatthew G. Knepley #endif
213e412dcbdSMatthew G. Knepley   } else if (isdraw) {
214f13a32a3SMatthew G. Knepley     Vec         locv;
215f13a32a3SMatthew G. Knepley     const char *name;
216f13a32a3SMatthew G. Knepley 
217f13a32a3SMatthew G. Knepley     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
218f13a32a3SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
219f13a32a3SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
220f13a32a3SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
221f13a32a3SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
222f13a32a3SMatthew G. Knepley     ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
223f13a32a3SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
224f13a32a3SMatthew G. Knepley     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
225f13a32a3SMatthew G. Knepley     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
226552f7358SJed Brown   } else {
22755f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
22855f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
229552f7358SJed Brown   }
230552f7358SJed Brown   PetscFunctionReturn(0);
231552f7358SJed Brown }
232552f7358SJed Brown 
233552f7358SJed Brown #undef __FUNCT__
234d930f514SMatthew G. Knepley #define __FUNCT__ "VecView_Plex_Native"
235d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
236d930f514SMatthew G. Knepley {
237d930f514SMatthew G. Knepley   DM                dm;
238d930f514SMatthew G. Knepley   MPI_Comm          comm;
239d930f514SMatthew G. Knepley   PetscViewerFormat format;
240d930f514SMatthew G. Knepley   Vec               v;
241d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
242d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
243d930f514SMatthew G. Knepley 
244d930f514SMatthew G. Knepley   PetscFunctionBegin;
245d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
246d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
2472c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
248d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
249d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
250d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
251d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
252d930f514SMatthew G. Knepley     const char *vecname;
253d930f514SMatthew G. Knepley     PetscInt    n, nroots;
254d930f514SMatthew G. Knepley 
255d930f514SMatthew G. Knepley     if (dm->sfNatural) {
256ca43db0aSBarry Smith       ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
257d930f514SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
258d930f514SMatthew G. Knepley       if (n == nroots) {
259d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
260d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
261d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
262d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
263d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
264d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
265d930f514SMatthew G. Knepley     } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
266d930f514SMatthew G. Knepley   } else {
267d930f514SMatthew G. Knepley     /* we are viewing a natural DMPlex vec. */
268d930f514SMatthew G. Knepley     v = originalv;
269d930f514SMatthew G. Knepley   }
270d930f514SMatthew G. Knepley   if (ishdf5) {
271d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
272d930f514SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native(v, viewer);CHKERRQ(ierr);
273d930f514SMatthew G. Knepley #else
274d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
275d930f514SMatthew G. Knepley #endif
276d930f514SMatthew G. Knepley   } else if (isvtk) {
277d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
278d930f514SMatthew G. Knepley   } else {
279d930f514SMatthew G. Knepley     PetscBool isseq;
280d930f514SMatthew G. Knepley 
281d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
282d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
283d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
284d930f514SMatthew G. Knepley   }
285d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
286d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
287d930f514SMatthew G. Knepley }
288d930f514SMatthew G. Knepley 
289d930f514SMatthew G. Knepley #undef __FUNCT__
2902c40f234SMatthew G. Knepley #define __FUNCT__ "VecLoad_Plex_Local"
2912c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
2922c40f234SMatthew G. Knepley {
2932c40f234SMatthew G. Knepley   DM             dm;
2942c40f234SMatthew G. Knepley   PetscBool      ishdf5;
2952c40f234SMatthew G. Knepley   PetscErrorCode ierr;
2962c40f234SMatthew G. Knepley 
2972c40f234SMatthew G. Knepley   PetscFunctionBegin;
2982c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
2992c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
3002c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
3012c40f234SMatthew G. Knepley   if (ishdf5) {
3022c40f234SMatthew G. Knepley     DM          dmBC;
3032c40f234SMatthew G. Knepley     Vec         gv;
3042c40f234SMatthew G. Knepley     const char *name;
3052c40f234SMatthew G. Knepley 
3062c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
3072c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
3082c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
3092c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
3102c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
3112c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
3122c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
3132c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
3142c40f234SMatthew G. Knepley   } else {
3152c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
3162c40f234SMatthew G. Knepley   }
3172c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
3182c40f234SMatthew G. Knepley }
3192c40f234SMatthew G. Knepley 
3202c40f234SMatthew G. Knepley #undef __FUNCT__
3212c40f234SMatthew G. Knepley #define __FUNCT__ "VecLoad_Plex"
3222c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
3232c40f234SMatthew G. Knepley {
3242c40f234SMatthew G. Knepley   DM             dm;
3252c40f234SMatthew G. Knepley   PetscBool      ishdf5;
3262c40f234SMatthew G. Knepley   PetscErrorCode ierr;
3272c40f234SMatthew G. Knepley 
3282c40f234SMatthew G. Knepley   PetscFunctionBegin;
3292c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
3302c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
3312c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
3322c40f234SMatthew G. Knepley   if (ishdf5) {
333878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
334b136c2c9SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5(v, viewer);CHKERRQ(ierr);
335b136c2c9SMatthew G. Knepley #else
336b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
337878b459fSMatthew G. Knepley #endif
3382c40f234SMatthew G. Knepley   } else {
3392c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
340552f7358SJed Brown   }
341552f7358SJed Brown   PetscFunctionReturn(0);
342552f7358SJed Brown }
343552f7358SJed Brown 
344552f7358SJed Brown #undef __FUNCT__
345d930f514SMatthew G. Knepley #define __FUNCT__ "VecLoad_Plex_Native"
346d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
347d930f514SMatthew G. Knepley {
348d930f514SMatthew G. Knepley   DM                dm;
349d930f514SMatthew G. Knepley   PetscViewerFormat format;
350d930f514SMatthew G. Knepley   PetscBool         ishdf5;
351d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
352d930f514SMatthew G. Knepley 
353d930f514SMatthew G. Knepley   PetscFunctionBegin;
354d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
355d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
356d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
357d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
358d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
359d930f514SMatthew G. Knepley     if (dm->sfNatural) {
360d930f514SMatthew G. Knepley       if (ishdf5) {
361d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
362d930f514SMatthew G. Knepley         Vec         v;
363d930f514SMatthew G. Knepley         const char *vecname;
364d930f514SMatthew G. Knepley 
365d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
366d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
367d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
368d930f514SMatthew G. Knepley         ierr = VecLoad_Plex_HDF5_Native(v, viewer);CHKERRQ(ierr);
369d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
370d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
371d930f514SMatthew G. Knepley         ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
372d930f514SMatthew G. Knepley #else
373d930f514SMatthew G. Knepley         SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
374d930f514SMatthew G. Knepley #endif
375d930f514SMatthew G. Knepley       } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
376d930f514SMatthew G. Knepley     }
377d930f514SMatthew G. Knepley   }
378d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
379d930f514SMatthew G. Knepley }
380d930f514SMatthew G. Knepley 
381d930f514SMatthew G. Knepley #undef __FUNCT__
382731e8ddeSMatthew G. Knepley #define __FUNCT__ "DMPlexView_Ascii_Geometry"
383731e8ddeSMatthew G. Knepley PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
384731e8ddeSMatthew G. Knepley {
385731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
386731e8ddeSMatthew G. Knepley   Vec                coordinates;
387731e8ddeSMatthew G. Knepley   DMLabel            depthLabel;
388731e8ddeSMatthew G. Knepley   const char        *name[4];
389731e8ddeSMatthew G. Knepley   const PetscScalar *a;
390731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
391731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
392731e8ddeSMatthew G. Knepley 
393731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
394731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
395731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
396731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
397731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
398731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
399731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
400731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
401731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
402731e8ddeSMatthew G. Knepley   name[1]     = "edge";
403731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
404731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
405731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
406731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
407731e8ddeSMatthew G. Knepley     PetscInt  closureSize, cl;
408731e8ddeSMatthew G. Knepley 
409f347f43bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D:\n", c);CHKERRQ(ierr);
410731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
411731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
412731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
413731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
414731e8ddeSMatthew G. Knepley 
415731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
416731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
417731e8ddeSMatthew G. Knepley       if (!dof) continue;
418731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
419731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
420f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
421731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
422731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
423731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
424731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
42541477eefSMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
426731e8ddeSMatthew G. Knepley         }
427731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
428731e8ddeSMatthew G. Knepley       }
429731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
430731e8ddeSMatthew G. Knepley     }
431731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
432731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
433731e8ddeSMatthew G. Knepley   }
434731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
435731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
436731e8ddeSMatthew G. Knepley }
437731e8ddeSMatthew G. Knepley 
438731e8ddeSMatthew G. Knepley #undef __FUNCT__
439552f7358SJed Brown #define __FUNCT__ "DMPlexView_Ascii"
440552f7358SJed Brown PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
441552f7358SJed Brown {
442552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
443552f7358SJed Brown   DM                cdm;
444552f7358SJed Brown   DMLabel           markers;
445552f7358SJed Brown   PetscSection      coordSection;
446552f7358SJed Brown   Vec               coordinates;
447552f7358SJed Brown   PetscViewerFormat format;
448552f7358SJed Brown   PetscErrorCode    ierr;
449552f7358SJed Brown 
450552f7358SJed Brown   PetscFunctionBegin;
451552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
452552f7358SJed Brown   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
453552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
454552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
455552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
456552f7358SJed Brown     const char *name;
457552f7358SJed Brown     PetscInt    maxConeSize, maxSupportSize;
458552f7358SJed Brown     PetscInt    pStart, pEnd, p;
459552f7358SJed Brown     PetscMPIInt rank, size;
460552f7358SJed Brown 
46182f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
46282f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
463552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
464552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
465552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
466552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "Mesh '%s':\n", name);CHKERRQ(ierr);
467552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "orientation is missing\n", name);CHKERRQ(ierr);
468552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "cap --> base:\n", name);CHKERRQ(ierr);
4694d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
4704d4c343aSBarry Smith     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max sizes cone: %D support: %D\n", rank,maxConeSize, maxSupportSize);CHKERRQ(ierr);
471552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
472552f7358SJed Brown       PetscInt dof, off, s;
473552f7358SJed Brown 
474552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
475552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
476552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
477e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
478552f7358SJed Brown       }
479552f7358SJed Brown     }
480552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
481552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "base <-- cap:\n", name);CHKERRQ(ierr);
482552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
483552f7358SJed Brown       PetscInt dof, off, c;
484552f7358SJed Brown 
485552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
486552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
487552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
488e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
489552f7358SJed Brown       }
490552f7358SJed Brown     }
491552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4924d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
4930298fd71SBarry Smith     ierr = PetscSectionGetChart(coordSection, &pStart, NULL);CHKERRQ(ierr);
494552f7358SJed Brown     if (pStart >= 0) {ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);}
495c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
496552f7358SJed Brown     ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);
497552f7358SJed Brown     if (size > 1) {
498552f7358SJed Brown       PetscSF sf;
499552f7358SJed Brown 
500552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
501552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
502552f7358SJed Brown     }
503552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
504552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
5050588280cSMatthew G. Knepley     const char  *name, *color;
5060588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
5070588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
508552f7358SJed Brown     PetscReal    scale         = 2.0;
5090588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
5100588280cSMatthew G. Knepley     double       tcoords[3];
511552f7358SJed Brown     PetscScalar *coords;
5120588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
513552f7358SJed Brown     PetscMPIInt  rank, size;
5140588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
515552f7358SJed Brown 
5160588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
517552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
518c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
5190588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
5200588280cSMatthew G. Knepley     numColors  = 10;
5210588280cSMatthew G. Knepley     numLColors = 10;
5220588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
523c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
524c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
525c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
5260588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
527c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
5280588280cSMatthew G. Knepley     if (!useColors) {
5290588280cSMatthew G. Knepley       numColors = 3;
5300588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
5310588280cSMatthew G. Knepley     }
532c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
5330588280cSMatthew G. Knepley     if (!useColors) {
5340588280cSMatthew G. Knepley       numLColors = 4;
5350588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
5360588280cSMatthew G. Knepley     }
53782f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
53882f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
539552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
540770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
5410588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
542552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
543552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
544552f7358SJed Brown \\usetikzlibrary{arrows}\n\
5450588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
5460588280cSMatthew G. Knepley     if (size > 1) {
547f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
548770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
549770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
550770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
551770b213bSMatthew G Knepley         } else if (p > 0) {
552770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
553770b213bSMatthew G Knepley         }
554770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
555770b213bSMatthew G Knepley       }
5560588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
5570588280cSMatthew G. Knepley     }
5580588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", 1.0);CHKERRQ(ierr);
559552f7358SJed Brown     /* Plot vertices */
560552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
561552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
5624d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
563552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
564552f7358SJed Brown       PetscInt  off, dof, d;
5650588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
566552f7358SJed Brown 
567552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
568552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
5690588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
570f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
5710588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
5720588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
5730588280cSMatthew G. Knepley         tcoords[d] = PetscAbsReal(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
5740588280cSMatthew G. Knepley       }
5750588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
5760588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
577552f7358SJed Brown       for (d = 0; d < dof; ++d) {
578552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
5790588280cSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]);CHKERRQ(ierr);
580552f7358SJed Brown       }
5810588280cSMatthew G. Knepley       color = colors[rank%numColors];
5820588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
5830588280cSMatthew G. Knepley         PetscInt val;
584c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
5850588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
5860588280cSMatthew G. Knepley       }
5870588280cSMatthew G. Knepley       if (useNumbers) {
588e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
5890588280cSMatthew G. Knepley       } else {
590e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
5910588280cSMatthew G. Knepley       }
592552f7358SJed Brown     }
593552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
594552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
595552f7358SJed Brown     /* Plot edges */
5960588280cSMatthew G. Knepley     if (depth > 1) {ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);}
5970588280cSMatthew G. Knepley     if (dim < 3 && useNumbers) {
598552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
599552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
600552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
601552f7358SJed Brown         const PetscInt *cone;
602552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
603552f7358SJed Brown 
604552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
605f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
606552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
607552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
608552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
609552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
610552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
611552f7358SJed Brown         for (d = 0; d < dof; ++d) {
6120588280cSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
6130588280cSMatthew G. Knepley           tcoords[d] = PetscAbsReal(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
614552f7358SJed Brown         }
6150588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
6160588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
6170588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
6180588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
619f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
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], v, &val);CHKERRQ(ierr);
6250750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
6260588280cSMatthew G. Knepley         }
627e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
628552f7358SJed Brown       }
629552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
630552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
631552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
6320588280cSMatthew G. Knepley     }
633552f7358SJed Brown     /* Plot cells */
6340588280cSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
6350588280cSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
6360588280cSMatthew G. Knepley         const PetscInt *cone;
6370588280cSMatthew G. Knepley 
6380588280cSMatthew G. Knepley         color = colors[rank%numColors];
6390588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
6400588280cSMatthew G. Knepley           PetscInt val;
641c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
6420588280cSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
6430588280cSMatthew G. Knepley         }
6440588280cSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
645e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
6460588280cSMatthew G. Knepley       }
6470588280cSMatthew G. Knepley     } else {
648552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
649552f7358SJed Brown       for (c = cStart; c < cEnd; ++c) {
6500298fd71SBarry Smith         PetscInt *closure = NULL;
651552f7358SJed Brown         PetscInt  closureSize, firstPoint = -1;
652552f7358SJed Brown 
653552f7358SJed Brown         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
654552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
655552f7358SJed Brown         for (p = 0; p < closureSize*2; p += 2) {
656552f7358SJed Brown           const PetscInt point = closure[p];
657552f7358SJed Brown 
658552f7358SJed Brown           if ((point < vStart) || (point >= vEnd)) continue;
659552f7358SJed Brown           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
660e4b003c7SBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
661552f7358SJed Brown           if (firstPoint < 0) firstPoint = point;
662552f7358SJed Brown         }
663552f7358SJed Brown         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
664e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
665552f7358SJed Brown         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
666552f7358SJed Brown       }
6670588280cSMatthew G. Knepley     }
668552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6694d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
6700588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
671770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
6720588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
6730588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
6740588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
6750588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
676552f7358SJed Brown   } else {
67782f516ccSBarry Smith     MPI_Comm    comm;
678834065abSMatthew G. Knepley     PetscInt   *sizes, *hybsizes;
679834065abSMatthew G. Knepley     PetscInt    locDepth, depth, dim, d, pMax[4];
680552f7358SJed Brown     PetscInt    pStart, pEnd, p;
681a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
6821143a3c0SMatthew G. Knepley     const char *name;
683552f7358SJed Brown     PetscMPIInt size;
684552f7358SJed Brown 
68582f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
686552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
687c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6885f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
6891143a3c0SMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimensions:\n", name, dim);CHKERRQ(ierr);}
6901143a3c0SMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimensions:\n", dim);CHKERRQ(ierr);}
691552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
692b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
693bb81ee50SMatthew G. Knepley     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, &pMax[1], &pMax[0]);CHKERRQ(ierr);
694dcca6d9dSJed Brown     ierr = PetscMalloc2(size,&sizes,size,&hybsizes);CHKERRQ(ierr);
695552f7358SJed Brown     if (depth == 1) {
696552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
697552f7358SJed Brown       pEnd = pEnd - pStart;
698552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
699f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);CHKERRQ(ierr);
700552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
701552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
702552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
703552f7358SJed Brown       pEnd = pEnd - pStart;
704552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
705552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
706552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
707552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
708552f7358SJed Brown     } else {
709cbb7f117SMark Adams       PetscMPIInt rank;
710cbb7f117SMark Adams       ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
711552f7358SJed Brown       for (d = 0; d <= dim; d++) {
712552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
713834065abSMatthew G. Knepley         pEnd    -= pStart;
714834065abSMatthew G. Knepley         pMax[d] -= pStart;
715552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
716834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
717552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
718834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
719cbb7f117SMark Adams           if (!rank) {
720834065abSMatthew G. Knepley             if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
721834065abSMatthew G. Knepley             else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
722834065abSMatthew G. Knepley           }
723cbb7f117SMark Adams         }
724552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
725552f7358SJed Brown       }
726552f7358SJed Brown     }
727834065abSMatthew G. Knepley     ierr = PetscFree2(sizes,hybsizes);CHKERRQ(ierr);
728c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
729a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
730a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
731a57dd577SMatthew G Knepley       DMLabel         label;
732a57dd577SMatthew G Knepley       const char     *name;
733a57dd577SMatthew G Knepley       IS              valueIS;
734a57dd577SMatthew G Knepley       const PetscInt *values;
735a57dd577SMatthew G Knepley       PetscInt        numValues, v;
736a57dd577SMatthew G Knepley 
737c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
738c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
739a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
740f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata of sizes (", name, numValues);CHKERRQ(ierr);
741a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
742a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
743120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
744a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
745a57dd577SMatthew G Knepley         PetscInt size;
746a57dd577SMatthew G Knepley 
747a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
748a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
749f347f43bSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer, "%D", size);CHKERRQ(ierr);
750a57dd577SMatthew G Knepley       }
751a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
752120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
753a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
7544d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
755a57dd577SMatthew G Knepley     }
756a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
7578e7ff633SMatthew G. Knepley     if (cdm) {
7588e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
7598e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
7608e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
7618e7ff633SMatthew G. Knepley     }
762552f7358SJed Brown   }
763552f7358SJed Brown   PetscFunctionReturn(0);
764552f7358SJed Brown }
765552f7358SJed Brown 
766552f7358SJed Brown #undef __FUNCT__
767e412dcbdSMatthew G. Knepley #define __FUNCT__ "DMPlexView_Draw"
768e412dcbdSMatthew G. Knepley PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
769e412dcbdSMatthew G. Knepley {
770e412dcbdSMatthew G. Knepley   PetscDraw          draw;
771e412dcbdSMatthew G. Knepley   DM                 cdm;
772e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
773e412dcbdSMatthew G. Knepley   Vec                coordinates;
774e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
775e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
776e412dcbdSMatthew G. Knepley   PetscBool          isnull;
777e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
778e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
779e412dcbdSMatthew G. Knepley 
780e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
781e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
782e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
783e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
784e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
785e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
786e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
787e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
788e412dcbdSMatthew G. Knepley 
789e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
790e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
791e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
792e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
793e412dcbdSMatthew G. Knepley 
794e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
795e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
796e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
7970c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
7980c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
799e412dcbdSMatthew G. Knepley   }
800e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
801e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
802e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
803e412dcbdSMatthew G. Knepley 
804e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
805e412dcbdSMatthew G. Knepley     PetscScalar *coords = NULL;
806e412dcbdSMatthew G. Knepley     PetscInt     numCoords;
807e412dcbdSMatthew G. Knepley 
808e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
809e412dcbdSMatthew G. Knepley     switch (numCoords) {
810e412dcbdSMatthew G. Knepley     case 6:
8110c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8120c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8130c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
814e412dcbdSMatthew G. Knepley       break;
815e412dcbdSMatthew G. Knepley     case 8:
8160c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8170c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8180c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8190c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
820e412dcbdSMatthew G. Knepley       break;
821e412dcbdSMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
822e412dcbdSMatthew G. Knepley     }
823e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
824e412dcbdSMatthew G. Knepley   }
825e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
826e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
827e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
828e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
829e412dcbdSMatthew G. Knepley }
830e412dcbdSMatthew G. Knepley 
831e412dcbdSMatthew G. Knepley #undef __FUNCT__
832552f7358SJed Brown #define __FUNCT__ "DMView_Plex"
833552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
834552f7358SJed Brown {
835e412dcbdSMatthew G. Knepley   PetscBool      iascii, ishdf5, isvtk, isdraw;
836552f7358SJed Brown   PetscErrorCode ierr;
837552f7358SJed Brown 
838552f7358SJed Brown   PetscFunctionBegin;
839552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
840552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
841552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
842fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
843c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
844e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
845552f7358SJed Brown   if (iascii) {
846552f7358SJed Brown     ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
847c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
848c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
8497afe7537SMatthew G. Knepley     ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_VIZ);CHKERRQ(ierr);
850c6ccd67eSMatthew G. Knepley     ierr = DMPlexView_HDF5(dm, viewer);CHKERRQ(ierr);
8517afe7537SMatthew G. Knepley     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
852c6ccd67eSMatthew G. Knepley #else
853c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
854552f7358SJed Brown #endif
855e412dcbdSMatthew G. Knepley   } else if (isvtk) {
856fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
857e412dcbdSMatthew G. Knepley   } else if (isdraw) {
858e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
859fcf6c8fdSToby Isaac   }
860552f7358SJed Brown   PetscFunctionReturn(0);
861552f7358SJed Brown }
862552f7358SJed Brown 
8632c40f234SMatthew G. Knepley #undef __FUNCT__
8642c40f234SMatthew G. Knepley #define __FUNCT__ "DMLoad_Plex"
8652c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
8662c40f234SMatthew G. Knepley {
8672c40f234SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
8682c40f234SMatthew G. Knepley   PetscErrorCode ierr;
8692c40f234SMatthew G. Knepley 
8702c40f234SMatthew G. Knepley   PetscFunctionBegin;
8712c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8722c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
8732c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERBINARY, &isbinary);CHKERRQ(ierr);
8742c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
8752c40f234SMatthew G. Knepley   if (isbinary) {SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Do not yet support binary viewers");}
8762c40f234SMatthew G. Knepley   else if (ishdf5) {
8772c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
87810b7db4eSMatthew G. Knepley     ierr = DMPlexLoad_HDF5(dm, viewer);CHKERRQ(ierr);
8792c40f234SMatthew G. Knepley #else
8802c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
881552f7358SJed Brown #endif
882552f7358SJed Brown   }
883552f7358SJed Brown   PetscFunctionReturn(0);
884552f7358SJed Brown }
885552f7358SJed Brown 
8866c73c22cSMatthew G. Knepley 
8876c73c22cSMatthew G. Knepley #undef __FUNCT__
888552f7358SJed Brown #define __FUNCT__ "DMDestroy_Plex"
889552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
890552f7358SJed Brown {
891552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
892552f7358SJed Brown   PetscErrorCode ierr;
893552f7358SJed Brown 
894552f7358SJed Brown   PetscFunctionBegin;
8950d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
896552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
897552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
898552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
899552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
900be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
901552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
902552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
903d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
904d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
90577623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
906a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
907552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
908552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
909a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
910a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
911d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
912d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
913d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
914d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
915d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
916d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
917fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
918552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
919552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
920713918a9SToby Isaac   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMAdaptLabel_C",NULL);CHKERRQ(ierr);
921552f7358SJed Brown   PetscFunctionReturn(0);
922552f7358SJed Brown }
923552f7358SJed Brown 
924552f7358SJed Brown #undef __FUNCT__
925552f7358SJed Brown #define __FUNCT__ "DMCreateMatrix_Plex"
926b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
927552f7358SJed Brown {
9288d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
929acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
930552f7358SJed Brown   PetscInt               localSize;
931837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
932552f7358SJed Brown   PetscErrorCode         ierr;
933b412c318SBarry Smith   MatType                mtype;
9341428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
935552f7358SJed Brown 
936552f7358SJed Brown   PetscFunctionBegin;
937607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
938b412c318SBarry Smith   mtype = dm->mattype;
939552f7358SJed Brown   ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
940552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
941552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
94282f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
943552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
944552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
945552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
946acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
947acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
948552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
949552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
950552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
951552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
952552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
953552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
954552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
955837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
956552f7358SJed Brown   if (!isShell) {
957be36d101SStefano Zampini     PetscSection subSection;
958837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
959be36d101SStefano Zampini     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal, bsMax, bsMin, *ltogidx, lsize;
960fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
961552f7358SJed Brown 
96200140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
963be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
964be36d101SStefano Zampini       PetscSection section;
965be36d101SStefano Zampini       PetscInt     size;
966be36d101SStefano Zampini 
967be36d101SStefano Zampini       ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
968be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
969be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
970be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
971be36d101SStefano Zampini     } else {
97200140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
973be36d101SStefano Zampini     }
974552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
975be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
976a9d99c84SMatthew G. Knepley       PetscInt bdof;
977a9d99c84SMatthew G. Knepley 
978552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
979fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
9801d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
9811d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
9821d17a0a3SMatthew G. Knepley       if (dof) {
9831d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
984be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
985be36d101SStefano Zampini       }
986be36d101SStefano Zampini       if (isMatIS) {
987be36d101SStefano Zampini         PetscInt loff,c,off;
988be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
989be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
990be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
991552f7358SJed Brown       }
9922a28c762SMatthew G Knepley     }
9932a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
9942a28c762SMatthew G Knepley     bsLocal = bs;
995b2566f29SBarry Smith     ierr = MPIU_Allreduce(&bsLocal, &bsMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
9962a28c762SMatthew G Knepley     bsLocal = bs < 0 ? bsMax : bs;
997b2566f29SBarry Smith     ierr = MPIU_Allreduce(&bsLocal, &bsMin, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
998bff27382SMatthew G. Knepley     if (bsMin != bsMax) {bs = 1;}
999bff27382SMatthew G. Knepley     else                {bs = bsMax;}
10004fa26246SMatthew G. Knepley     bs   = bs < 0 ? 1 : bs;
1001be36d101SStefano Zampini     if (isMatIS) {
10024fa26246SMatthew G. Knepley       PetscInt l;
10034fa26246SMatthew G. Knepley       /* Must reduce indices by blocksize */
10044fa26246SMatthew G. Knepley       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] /= bs;
10054fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1006be36d101SStefano Zampini     }
1007be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1008be36d101SStefano Zampini     if (isMatIS) {
1009be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1010be36d101SStefano Zampini     }
10111795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
10128d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1013552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1014552f7358SJed Brown   }
1015b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1016552f7358SJed Brown   PetscFunctionReturn(0);
1017552f7358SJed Brown }
1018552f7358SJed Brown 
1019552f7358SJed Brown #undef __FUNCT__
1020be36d101SStefano Zampini #define __FUNCT__ "DMPlexGetSubdomainSection"
1021559da874SStefano Zampini /*
1022be36d101SStefano Zampini   DMPlexGetSubdomainGlobalSection - Returns the section associated with the subdomain
1023be36d101SStefano Zampini 
1024be36d101SStefano Zampini   Not collective
1025be36d101SStefano Zampini 
1026be36d101SStefano Zampini   Input Parameter:
1027be36d101SStefano Zampini . mesh - The DMPlex
1028be36d101SStefano Zampini 
1029be36d101SStefano Zampini   Output Parameters:
1030be36d101SStefano Zampini . subsection - The subdomain section
1031be36d101SStefano Zampini 
1032be36d101SStefano Zampini   Level: developer
1033be36d101SStefano Zampini 
1034be36d101SStefano Zampini .seealso:
1035559da874SStefano Zampini */
1036be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1037be36d101SStefano Zampini {
1038be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1039be36d101SStefano Zampini   PetscErrorCode ierr;
1040be36d101SStefano Zampini 
1041be36d101SStefano Zampini   PetscFunctionBegin;
1042be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1043be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1044be36d101SStefano Zampini     PetscSection section;
1045be36d101SStefano Zampini     PetscSF      sf;
1046be36d101SStefano Zampini 
1047be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
1048be36d101SStefano Zampini     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
1049be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1050be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1051be36d101SStefano Zampini   }
1052be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1053be36d101SStefano Zampini   PetscFunctionReturn(0);
1054be36d101SStefano Zampini }
1055be36d101SStefano Zampini 
1056be36d101SStefano Zampini #undef __FUNCT__
1057552f7358SJed Brown #define __FUNCT__ "DMPlexGetChart"
1058552f7358SJed Brown /*@
1059552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1060552f7358SJed Brown 
1061552f7358SJed Brown   Not collective
1062552f7358SJed Brown 
1063552f7358SJed Brown   Input Parameter:
1064552f7358SJed Brown . mesh - The DMPlex
1065552f7358SJed Brown 
1066552f7358SJed Brown   Output Parameters:
1067552f7358SJed Brown + pStart - The first mesh point
1068552f7358SJed Brown - pEnd   - The upper bound for mesh points
1069552f7358SJed Brown 
1070552f7358SJed Brown   Level: beginner
1071552f7358SJed Brown 
1072552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1073552f7358SJed Brown @*/
1074552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1075552f7358SJed Brown {
1076552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1077552f7358SJed Brown   PetscErrorCode ierr;
1078552f7358SJed Brown 
1079552f7358SJed Brown   PetscFunctionBegin;
1080552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1081552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1082552f7358SJed Brown   PetscFunctionReturn(0);
1083552f7358SJed Brown }
1084552f7358SJed Brown 
1085552f7358SJed Brown #undef __FUNCT__
1086552f7358SJed Brown #define __FUNCT__ "DMPlexSetChart"
1087552f7358SJed Brown /*@
1088552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1089552f7358SJed Brown 
1090552f7358SJed Brown   Not collective
1091552f7358SJed Brown 
1092552f7358SJed Brown   Input Parameters:
1093552f7358SJed Brown + mesh - The DMPlex
1094552f7358SJed Brown . pStart - The first mesh point
1095552f7358SJed Brown - pEnd   - The upper bound for mesh points
1096552f7358SJed Brown 
1097552f7358SJed Brown   Output Parameters:
1098552f7358SJed Brown 
1099552f7358SJed Brown   Level: beginner
1100552f7358SJed Brown 
1101552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1102552f7358SJed Brown @*/
1103552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1104552f7358SJed Brown {
1105552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1106552f7358SJed Brown   PetscErrorCode ierr;
1107552f7358SJed Brown 
1108552f7358SJed Brown   PetscFunctionBegin;
1109552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1110552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1111552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1112552f7358SJed Brown   PetscFunctionReturn(0);
1113552f7358SJed Brown }
1114552f7358SJed Brown 
1115552f7358SJed Brown #undef __FUNCT__
1116552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeSize"
1117552f7358SJed Brown /*@
1118552f7358SJed Brown   DMPlexGetConeSize - Return the number of in-edges for this point in the Sieve DAG
1119552f7358SJed Brown 
1120552f7358SJed Brown   Not collective
1121552f7358SJed Brown 
1122552f7358SJed Brown   Input Parameters:
1123552f7358SJed Brown + mesh - The DMPlex
1124552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1125552f7358SJed Brown 
1126552f7358SJed Brown   Output Parameter:
1127552f7358SJed Brown . size - The cone size for point p
1128552f7358SJed Brown 
1129552f7358SJed Brown   Level: beginner
1130552f7358SJed Brown 
1131552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1132552f7358SJed Brown @*/
1133552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1134552f7358SJed Brown {
1135552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1136552f7358SJed Brown   PetscErrorCode ierr;
1137552f7358SJed Brown 
1138552f7358SJed Brown   PetscFunctionBegin;
1139552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1140552f7358SJed Brown   PetscValidPointer(size, 3);
1141552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1142552f7358SJed Brown   PetscFunctionReturn(0);
1143552f7358SJed Brown }
1144552f7358SJed Brown 
1145552f7358SJed Brown #undef __FUNCT__
1146552f7358SJed Brown #define __FUNCT__ "DMPlexSetConeSize"
1147552f7358SJed Brown /*@
1148552f7358SJed Brown   DMPlexSetConeSize - Set the number of in-edges for this point in the Sieve DAG
1149552f7358SJed Brown 
1150552f7358SJed Brown   Not collective
1151552f7358SJed Brown 
1152552f7358SJed Brown   Input Parameters:
1153552f7358SJed Brown + mesh - The DMPlex
1154552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1155552f7358SJed Brown - size - The cone size for point p
1156552f7358SJed Brown 
1157552f7358SJed Brown   Output Parameter:
1158552f7358SJed Brown 
1159552f7358SJed Brown   Note:
1160552f7358SJed Brown   This should be called after DMPlexSetChart().
1161552f7358SJed Brown 
1162552f7358SJed Brown   Level: beginner
1163552f7358SJed Brown 
1164552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1165552f7358SJed Brown @*/
1166552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1167552f7358SJed Brown {
1168552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1169552f7358SJed Brown   PetscErrorCode ierr;
1170552f7358SJed Brown 
1171552f7358SJed Brown   PetscFunctionBegin;
1172552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1173552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
11740d644c17SKarl Rupp 
1175552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1176552f7358SJed Brown   PetscFunctionReturn(0);
1177552f7358SJed Brown }
1178552f7358SJed Brown 
1179552f7358SJed Brown #undef __FUNCT__
1180f5a469b9SMatthew G. Knepley #define __FUNCT__ "DMPlexAddConeSize"
1181f5a469b9SMatthew G. Knepley /*@
1182f5a469b9SMatthew G. Knepley   DMPlexAddConeSize - Add the given number of in-edges to this point in the Sieve DAG
1183f5a469b9SMatthew G. Knepley 
1184f5a469b9SMatthew G. Knepley   Not collective
1185f5a469b9SMatthew G. Knepley 
1186f5a469b9SMatthew G. Knepley   Input Parameters:
1187f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1188f5a469b9SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1189f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1190f5a469b9SMatthew G. Knepley 
1191f5a469b9SMatthew G. Knepley   Output Parameter:
1192f5a469b9SMatthew G. Knepley 
1193f5a469b9SMatthew G. Knepley   Note:
1194f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1195f5a469b9SMatthew G. Knepley 
1196f5a469b9SMatthew G. Knepley   Level: beginner
1197f5a469b9SMatthew G. Knepley 
1198f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1199f5a469b9SMatthew G. Knepley @*/
1200f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1201f5a469b9SMatthew G. Knepley {
1202f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1203f5a469b9SMatthew G. Knepley   PetscInt       csize;
1204f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1205f5a469b9SMatthew G. Knepley 
1206f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1207f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1208f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1209f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1210f5a469b9SMatthew G. Knepley 
1211f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1212f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1213f5a469b9SMatthew G. Knepley }
1214f5a469b9SMatthew G. Knepley 
1215f5a469b9SMatthew G. Knepley #undef __FUNCT__
1216552f7358SJed Brown #define __FUNCT__ "DMPlexGetCone"
1217552f7358SJed Brown /*@C
1218552f7358SJed Brown   DMPlexGetCone - Return the points on the in-edges for this point in the Sieve DAG
1219552f7358SJed Brown 
1220552f7358SJed Brown   Not collective
1221552f7358SJed Brown 
1222552f7358SJed Brown   Input Parameters:
1223552f7358SJed Brown + mesh - The DMPlex
1224552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1225552f7358SJed Brown 
1226552f7358SJed Brown   Output Parameter:
1227552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1228552f7358SJed Brown 
1229552f7358SJed Brown   Level: beginner
1230552f7358SJed Brown 
12313813dfbdSMatthew G Knepley   Fortran Notes:
12323813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
12333813dfbdSMatthew G Knepley   include petsc.h90 in your code.
12343813dfbdSMatthew G Knepley 
12353813dfbdSMatthew G Knepley   You must also call DMPlexRestoreCone() after you finish using the returned array.
1236552f7358SJed Brown 
1237552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart()
1238552f7358SJed Brown @*/
1239552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1240552f7358SJed Brown {
1241552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1242552f7358SJed Brown   PetscInt       off;
1243552f7358SJed Brown   PetscErrorCode ierr;
1244552f7358SJed Brown 
1245552f7358SJed Brown   PetscFunctionBegin;
1246552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1247552f7358SJed Brown   PetscValidPointer(cone, 3);
1248552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1249552f7358SJed Brown   *cone = &mesh->cones[off];
1250552f7358SJed Brown   PetscFunctionReturn(0);
1251552f7358SJed Brown }
1252552f7358SJed Brown 
1253552f7358SJed Brown #undef __FUNCT__
1254552f7358SJed Brown #define __FUNCT__ "DMPlexSetCone"
1255552f7358SJed Brown /*@
1256552f7358SJed Brown   DMPlexSetCone - Set the points on the in-edges for this point in the Sieve DAG
1257552f7358SJed Brown 
1258552f7358SJed Brown   Not collective
1259552f7358SJed Brown 
1260552f7358SJed Brown   Input Parameters:
1261552f7358SJed Brown + mesh - The DMPlex
1262552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1263552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1264552f7358SJed Brown 
1265552f7358SJed Brown   Output Parameter:
1266552f7358SJed Brown 
1267552f7358SJed Brown   Note:
1268552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1269552f7358SJed Brown 
1270552f7358SJed Brown   Level: beginner
1271552f7358SJed Brown 
1272552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1273552f7358SJed Brown @*/
1274552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1275552f7358SJed Brown {
1276552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1277552f7358SJed Brown   PetscInt       pStart, pEnd;
1278552f7358SJed Brown   PetscInt       dof, off, c;
1279552f7358SJed Brown   PetscErrorCode ierr;
1280552f7358SJed Brown 
1281552f7358SJed Brown   PetscFunctionBegin;
1282552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1283552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1284552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1285552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1286552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
128782f516ccSBarry 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);
1288552f7358SJed Brown   for (c = 0; c < dof; ++c) {
128982f516ccSBarry 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);
1290552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1291552f7358SJed Brown   }
1292552f7358SJed Brown   PetscFunctionReturn(0);
1293552f7358SJed Brown }
1294552f7358SJed Brown 
1295552f7358SJed Brown #undef __FUNCT__
1296552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeOrientation"
1297552f7358SJed Brown /*@C
1298552f7358SJed Brown   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the Sieve DAG
1299552f7358SJed Brown 
1300552f7358SJed Brown   Not collective
1301552f7358SJed Brown 
1302552f7358SJed Brown   Input Parameters:
1303552f7358SJed Brown + mesh - The DMPlex
1304552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1305552f7358SJed Brown 
1306552f7358SJed Brown   Output Parameter:
1307552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1308552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1309552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1310552f7358SJed Brown                     the index of the cone point on which to start.
1311552f7358SJed Brown 
1312552f7358SJed Brown   Level: beginner
1313552f7358SJed Brown 
13143813dfbdSMatthew G Knepley   Fortran Notes:
13153813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
13163813dfbdSMatthew G Knepley   include petsc.h90 in your code.
13173813dfbdSMatthew G Knepley 
13183813dfbdSMatthew G Knepley   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1319552f7358SJed Brown 
1320552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1321552f7358SJed Brown @*/
1322552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1323552f7358SJed Brown {
1324552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1325552f7358SJed Brown   PetscInt       off;
1326552f7358SJed Brown   PetscErrorCode ierr;
1327552f7358SJed Brown 
1328552f7358SJed Brown   PetscFunctionBegin;
1329552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1330552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1331552f7358SJed Brown   {
1332552f7358SJed Brown     PetscInt dof;
1333552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1334552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1335552f7358SJed Brown   }
1336552f7358SJed Brown #endif
1337552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
13380d644c17SKarl Rupp 
1339552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1340552f7358SJed Brown   PetscFunctionReturn(0);
1341552f7358SJed Brown }
1342552f7358SJed Brown 
1343552f7358SJed Brown #undef __FUNCT__
1344552f7358SJed Brown #define __FUNCT__ "DMPlexSetConeOrientation"
1345552f7358SJed Brown /*@
1346552f7358SJed Brown   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the Sieve DAG
1347552f7358SJed Brown 
1348552f7358SJed Brown   Not collective
1349552f7358SJed Brown 
1350552f7358SJed Brown   Input Parameters:
1351552f7358SJed Brown + mesh - The DMPlex
1352552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1353552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1354552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1355552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1356552f7358SJed Brown                     the index of the cone point on which to start.
1357552f7358SJed Brown 
1358552f7358SJed Brown   Output Parameter:
1359552f7358SJed Brown 
1360552f7358SJed Brown   Note:
1361552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1362552f7358SJed Brown 
1363552f7358SJed Brown   Level: beginner
1364552f7358SJed Brown 
1365552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1366552f7358SJed Brown @*/
1367552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
1368552f7358SJed Brown {
1369552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1370552f7358SJed Brown   PetscInt       pStart, pEnd;
1371552f7358SJed Brown   PetscInt       dof, off, c;
1372552f7358SJed Brown   PetscErrorCode ierr;
1373552f7358SJed Brown 
1374552f7358SJed Brown   PetscFunctionBegin;
1375552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1376552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1377552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1378552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
1379552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
138082f516ccSBarry 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);
1381552f7358SJed Brown   for (c = 0; c < dof; ++c) {
1382552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
1383552f7358SJed Brown 
1384552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
138582f516ccSBarry 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);
1386552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
1387552f7358SJed Brown   }
1388552f7358SJed Brown   PetscFunctionReturn(0);
1389552f7358SJed Brown }
1390552f7358SJed Brown 
1391552f7358SJed Brown #undef __FUNCT__
1392552f7358SJed Brown #define __FUNCT__ "DMPlexInsertCone"
1393552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
1394552f7358SJed Brown {
1395552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1396552f7358SJed Brown   PetscInt       pStart, pEnd;
1397552f7358SJed Brown   PetscInt       dof, off;
1398552f7358SJed Brown   PetscErrorCode ierr;
1399552f7358SJed Brown 
1400552f7358SJed Brown   PetscFunctionBegin;
1401552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1402552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
140382f516ccSBarry 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);
140482f516ccSBarry 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);
140577c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
140677c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
140777c88f5bSMatthew 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);
1408552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
1409552f7358SJed Brown   PetscFunctionReturn(0);
1410552f7358SJed Brown }
1411552f7358SJed Brown 
1412552f7358SJed Brown #undef __FUNCT__
141377c88f5bSMatthew G Knepley #define __FUNCT__ "DMPlexInsertConeOrientation"
141477c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
141577c88f5bSMatthew G Knepley {
141677c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
141777c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
141877c88f5bSMatthew G Knepley   PetscInt       dof, off;
141977c88f5bSMatthew G Knepley   PetscErrorCode ierr;
142077c88f5bSMatthew G Knepley 
142177c88f5bSMatthew G Knepley   PetscFunctionBegin;
142277c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
142377c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
142477c88f5bSMatthew 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);
142577c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
142677c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
142777c88f5bSMatthew 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);
142877c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
142977c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
143077c88f5bSMatthew G Knepley }
143177c88f5bSMatthew G Knepley 
143277c88f5bSMatthew G Knepley #undef __FUNCT__
1433552f7358SJed Brown #define __FUNCT__ "DMPlexGetSupportSize"
1434552f7358SJed Brown /*@
1435552f7358SJed Brown   DMPlexGetSupportSize - Return the number of out-edges for this point in the Sieve DAG
1436552f7358SJed Brown 
1437552f7358SJed Brown   Not collective
1438552f7358SJed Brown 
1439552f7358SJed Brown   Input Parameters:
1440552f7358SJed Brown + mesh - The DMPlex
1441552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1442552f7358SJed Brown 
1443552f7358SJed Brown   Output Parameter:
1444552f7358SJed Brown . size - The support size for point p
1445552f7358SJed Brown 
1446552f7358SJed Brown   Level: beginner
1447552f7358SJed Brown 
1448552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
1449552f7358SJed Brown @*/
1450552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
1451552f7358SJed Brown {
1452552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1453552f7358SJed Brown   PetscErrorCode ierr;
1454552f7358SJed Brown 
1455552f7358SJed Brown   PetscFunctionBegin;
1456552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1457552f7358SJed Brown   PetscValidPointer(size, 3);
1458552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
1459552f7358SJed Brown   PetscFunctionReturn(0);
1460552f7358SJed Brown }
1461552f7358SJed Brown 
1462552f7358SJed Brown #undef __FUNCT__
1463552f7358SJed Brown #define __FUNCT__ "DMPlexSetSupportSize"
1464552f7358SJed Brown /*@
1465552f7358SJed Brown   DMPlexSetSupportSize - Set the number of out-edges for this point in the Sieve DAG
1466552f7358SJed Brown 
1467552f7358SJed Brown   Not collective
1468552f7358SJed Brown 
1469552f7358SJed Brown   Input Parameters:
1470552f7358SJed Brown + mesh - The DMPlex
1471552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1472552f7358SJed Brown - size - The support size for point p
1473552f7358SJed Brown 
1474552f7358SJed Brown   Output Parameter:
1475552f7358SJed Brown 
1476552f7358SJed Brown   Note:
1477552f7358SJed Brown   This should be called after DMPlexSetChart().
1478552f7358SJed Brown 
1479552f7358SJed Brown   Level: beginner
1480552f7358SJed Brown 
1481552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
1482552f7358SJed Brown @*/
1483552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
1484552f7358SJed Brown {
1485552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1486552f7358SJed Brown   PetscErrorCode ierr;
1487552f7358SJed Brown 
1488552f7358SJed Brown   PetscFunctionBegin;
1489552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1490552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
14910d644c17SKarl Rupp 
1492552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
1493552f7358SJed Brown   PetscFunctionReturn(0);
1494552f7358SJed Brown }
1495552f7358SJed Brown 
1496552f7358SJed Brown #undef __FUNCT__
1497552f7358SJed Brown #define __FUNCT__ "DMPlexGetSupport"
1498552f7358SJed Brown /*@C
1499552f7358SJed Brown   DMPlexGetSupport - Return the points on the out-edges for this point in the Sieve DAG
1500552f7358SJed Brown 
1501552f7358SJed Brown   Not collective
1502552f7358SJed Brown 
1503552f7358SJed Brown   Input Parameters:
1504552f7358SJed Brown + mesh - The DMPlex
1505552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1506552f7358SJed Brown 
1507552f7358SJed Brown   Output Parameter:
1508552f7358SJed Brown . support - An array of points which are on the out-edges for point p
1509552f7358SJed Brown 
1510552f7358SJed Brown   Level: beginner
1511552f7358SJed Brown 
15123813dfbdSMatthew G Knepley   Fortran Notes:
15133813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
15143813dfbdSMatthew G Knepley   include petsc.h90 in your code.
15153813dfbdSMatthew G Knepley 
15163813dfbdSMatthew G Knepley   You must also call DMPlexRestoreSupport() after you finish using the returned array.
15173813dfbdSMatthew G Knepley 
1518552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1519552f7358SJed Brown @*/
1520552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
1521552f7358SJed Brown {
1522552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1523552f7358SJed Brown   PetscInt       off;
1524552f7358SJed Brown   PetscErrorCode ierr;
1525552f7358SJed Brown 
1526552f7358SJed Brown   PetscFunctionBegin;
1527552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1528552f7358SJed Brown   PetscValidPointer(support, 3);
1529552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
1530552f7358SJed Brown   *support = &mesh->supports[off];
1531552f7358SJed Brown   PetscFunctionReturn(0);
1532552f7358SJed Brown }
1533552f7358SJed Brown 
1534552f7358SJed Brown #undef __FUNCT__
1535552f7358SJed Brown #define __FUNCT__ "DMPlexSetSupport"
1536552f7358SJed Brown /*@
1537552f7358SJed Brown   DMPlexSetSupport - Set the points on the out-edges for this point in the Sieve DAG
1538552f7358SJed Brown 
1539552f7358SJed Brown   Not collective
1540552f7358SJed Brown 
1541552f7358SJed Brown   Input Parameters:
1542552f7358SJed Brown + mesh - The DMPlex
1543552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1544552f7358SJed Brown - support - An array of points which are on the in-edges for point p
1545552f7358SJed Brown 
1546552f7358SJed Brown   Output Parameter:
1547552f7358SJed Brown 
1548552f7358SJed Brown   Note:
1549552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
1550552f7358SJed Brown 
1551552f7358SJed Brown   Level: beginner
1552552f7358SJed Brown 
1553552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
1554552f7358SJed Brown @*/
1555552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
1556552f7358SJed Brown {
1557552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1558552f7358SJed Brown   PetscInt       pStart, pEnd;
1559552f7358SJed Brown   PetscInt       dof, off, c;
1560552f7358SJed Brown   PetscErrorCode ierr;
1561552f7358SJed Brown 
1562552f7358SJed Brown   PetscFunctionBegin;
1563552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1564552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1565552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1566552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
1567552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
156882f516ccSBarry 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);
1569552f7358SJed Brown   for (c = 0; c < dof; ++c) {
157082f516ccSBarry 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);
1571552f7358SJed Brown     mesh->supports[off+c] = support[c];
1572552f7358SJed Brown   }
1573552f7358SJed Brown   PetscFunctionReturn(0);
1574552f7358SJed Brown }
1575552f7358SJed Brown 
1576552f7358SJed Brown #undef __FUNCT__
1577552f7358SJed Brown #define __FUNCT__ "DMPlexInsertSupport"
1578552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
1579552f7358SJed Brown {
1580552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1581552f7358SJed Brown   PetscInt       pStart, pEnd;
1582552f7358SJed Brown   PetscInt       dof, off;
1583552f7358SJed Brown   PetscErrorCode ierr;
1584552f7358SJed Brown 
1585552f7358SJed Brown   PetscFunctionBegin;
1586552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1587552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1588552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1589552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
159082f516ccSBarry 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);
159182f516ccSBarry 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);
159282f516ccSBarry 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);
1593552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
1594552f7358SJed Brown   PetscFunctionReturn(0);
1595552f7358SJed Brown }
1596552f7358SJed Brown 
1597552f7358SJed Brown #undef __FUNCT__
1598552f7358SJed Brown #define __FUNCT__ "DMPlexGetTransitiveClosure"
1599552f7358SJed Brown /*@C
1600552f7358SJed Brown   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the Sieve DAG
1601552f7358SJed Brown 
1602552f7358SJed Brown   Not collective
1603552f7358SJed Brown 
1604552f7358SJed Brown   Input Parameters:
1605552f7358SJed Brown + mesh - The DMPlex
1606552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1607552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
16080298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
1609552f7358SJed Brown 
1610552f7358SJed Brown   Output Parameters:
1611552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
1612552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
1613552f7358SJed Brown 
1614552f7358SJed Brown   Note:
16150298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
1616552f7358SJed Brown 
16173813dfbdSMatthew G Knepley   Fortran Notes:
16183813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16193813dfbdSMatthew G Knepley   include petsc.h90 in your code.
16203813dfbdSMatthew G Knepley 
16213813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
16223813dfbdSMatthew G Knepley 
1623552f7358SJed Brown   Level: beginner
1624552f7358SJed Brown 
1625552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1626552f7358SJed Brown @*/
1627552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1628552f7358SJed Brown {
1629552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
1630552f7358SJed Brown   PetscInt       *closure, *fifo;
16310298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
1632552f7358SJed Brown   PetscInt        tmpSize, t;
1633552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
1634552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
1635552f7358SJed Brown   PetscErrorCode  ierr;
1636552f7358SJed Brown 
1637552f7358SJed Brown   PetscFunctionBegin;
1638552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1639552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1640552f7358SJed Brown   /* This is only 1-level */
1641552f7358SJed Brown   if (useCone) {
1642552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
1643552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
1644552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
1645552f7358SJed Brown   } else {
1646552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
1647552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
1648552f7358SJed Brown   }
1649bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
1650bfbcdd7aSMatthew G. Knepley     if (*points) {
1651bfbcdd7aSMatthew G. Knepley       closure = *points;
1652bfbcdd7aSMatthew G. Knepley     } else {
1653bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
1654bfbcdd7aSMatthew G. Knepley       ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1655bfbcdd7aSMatthew G. Knepley     }
1656bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
1657bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
1658bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
1659bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
1660bfbcdd7aSMatthew G. Knepley     }
1661bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
1662bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
1663bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
1664bfbcdd7aSMatthew G. Knepley   }
166524c766afSToby Isaac   {
166624c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
166724c766afSToby Isaac 
166824c766afSToby Isaac     c = mesh->maxConeSize;
166924c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
167024c766afSToby Isaac     s = mesh->maxSupportSize;
167124c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
167224c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
167324c766afSToby Isaac   }
1674bfbcdd7aSMatthew G. Knepley   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1675bfbcdd7aSMatthew G. Knepley   if (*points) {
1676bfbcdd7aSMatthew G. Knepley     closure = *points;
1677bfbcdd7aSMatthew G. Knepley   } else {
1678bfbcdd7aSMatthew G. Knepley     ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1679bfbcdd7aSMatthew G. Knepley   }
1680bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
1681552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
1682552f7358SJed Brown     const PetscInt cp = tmp[t];
1683552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
1684552f7358SJed Brown 
1685552f7358SJed Brown     closure[closureSize]   = cp;
1686552f7358SJed Brown     closure[closureSize+1] = co;
1687552f7358SJed Brown     fifo[fifoSize]         = cp;
1688552f7358SJed Brown     fifo[fifoSize+1]       = co;
1689552f7358SJed Brown   }
1690bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
1691552f7358SJed Brown   while (fifoSize - fifoStart) {
1692552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
1693552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
1694552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
1695552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
1696552f7358SJed Brown 
1697552f7358SJed Brown     if (useCone) {
1698552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
1699552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
1700552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
1701552f7358SJed Brown     } else {
1702552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
1703552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
17040298fd71SBarry Smith       tmpO = NULL;
1705552f7358SJed Brown     }
1706552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
1707552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
1708552f7358SJed Brown       const PetscInt cp = tmp[i];
17092e1b13c2SMatthew 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. */
17102e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
17112e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
17122e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
1713552f7358SJed Brown       PetscInt       c;
1714552f7358SJed Brown 
17152e1b13c2SMatthew G. Knepley       if (rev) {
17162e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
17172e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
17182e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
17192e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
17202e1b13c2SMatthew G. Knepley       }
1721552f7358SJed Brown       /* Check for duplicate */
1722552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
1723552f7358SJed Brown         if (closure[c] == cp) break;
1724552f7358SJed Brown       }
1725552f7358SJed Brown       if (c == closureSize) {
1726552f7358SJed Brown         closure[closureSize]   = cp;
1727552f7358SJed Brown         closure[closureSize+1] = co;
1728552f7358SJed Brown         fifo[fifoSize]         = cp;
1729552f7358SJed Brown         fifo[fifoSize+1]       = co;
1730552f7358SJed Brown         closureSize           += 2;
1731552f7358SJed Brown         fifoSize              += 2;
1732552f7358SJed Brown       }
1733552f7358SJed Brown     }
1734552f7358SJed Brown     fifoStart += 2;
1735552f7358SJed Brown   }
1736552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
1737552f7358SJed Brown   if (points)    *points    = closure;
1738552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1739552f7358SJed Brown   PetscFunctionReturn(0);
1740552f7358SJed Brown }
1741552f7358SJed Brown 
1742552f7358SJed Brown #undef __FUNCT__
17439bf0dad6SMatthew G. Knepley #define __FUNCT__ "DMPlexGetTransitiveClosure_Internal"
17449bf0dad6SMatthew G. Knepley /*@C
17459bf0dad6SMatthew 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
17469bf0dad6SMatthew G. Knepley 
17479bf0dad6SMatthew G. Knepley   Not collective
17489bf0dad6SMatthew G. Knepley 
17499bf0dad6SMatthew G. Knepley   Input Parameters:
17509bf0dad6SMatthew G. Knepley + mesh - The DMPlex
17519bf0dad6SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
17529bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
17539bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
17549bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
17559bf0dad6SMatthew G. Knepley 
17569bf0dad6SMatthew G. Knepley   Output Parameters:
17579bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
17589bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
17599bf0dad6SMatthew G. Knepley 
17609bf0dad6SMatthew G. Knepley   Note:
17619bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
17629bf0dad6SMatthew G. Knepley 
17639bf0dad6SMatthew G. Knepley   Fortran Notes:
17649bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
17659bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
17669bf0dad6SMatthew G. Knepley 
17679bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
17689bf0dad6SMatthew G. Knepley 
17699bf0dad6SMatthew G. Knepley   Level: beginner
17709bf0dad6SMatthew G. Knepley 
17719bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
17729bf0dad6SMatthew G. Knepley @*/
17739bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
17749bf0dad6SMatthew G. Knepley {
17759bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
17769bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
17779bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
17789bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
17799bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
17809bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
17819bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
17829bf0dad6SMatthew G. Knepley 
17839bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
17849bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17859bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
17869bf0dad6SMatthew G. Knepley   /* This is only 1-level */
17879bf0dad6SMatthew G. Knepley   if (useCone) {
17889bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
17899bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
17909bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
17919bf0dad6SMatthew G. Knepley   } else {
17929bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
17939bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
17949bf0dad6SMatthew G. Knepley   }
17959bf0dad6SMatthew G. Knepley   if (depth == 1) {
17969bf0dad6SMatthew G. Knepley     if (*points) {
17979bf0dad6SMatthew G. Knepley       closure = *points;
17989bf0dad6SMatthew G. Knepley     } else {
17999bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
18009bf0dad6SMatthew G. Knepley       ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
18019bf0dad6SMatthew G. Knepley     }
18029bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
18039bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
18049bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
18059bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
18069bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
18079bf0dad6SMatthew G. Knepley     }
18089bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
18099bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
18109bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
18119bf0dad6SMatthew G. Knepley   }
181224c766afSToby Isaac   {
181324c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
181424c766afSToby Isaac 
181524c766afSToby Isaac     c = mesh->maxConeSize;
181624c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
181724c766afSToby Isaac     s = mesh->maxSupportSize;
181824c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
181924c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
182024c766afSToby Isaac   }
18219bf0dad6SMatthew G. Knepley   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
18229bf0dad6SMatthew G. Knepley   if (*points) {
18239bf0dad6SMatthew G. Knepley     closure = *points;
18249bf0dad6SMatthew G. Knepley   } else {
18259bf0dad6SMatthew G. Knepley     ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
18269bf0dad6SMatthew G. Knepley   }
18279bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
18289bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
18299bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
18309bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
18319bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
18329bf0dad6SMatthew G. Knepley 
18339bf0dad6SMatthew G. Knepley     if (ornt < 0) {
18349bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
18359bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
183686b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
18379bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
18389bf0dad6SMatthew G. Knepley     }
18399bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
18409bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
18419bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
18429bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
18439bf0dad6SMatthew G. Knepley   }
18449bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
18459bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
18469bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
18479bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
18489bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
18499bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
18509bf0dad6SMatthew G. Knepley 
18519bf0dad6SMatthew G. Knepley     if (useCone) {
18529bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
18539bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
18549bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
18559bf0dad6SMatthew G. Knepley     } else {
18569bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
18579bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
18589bf0dad6SMatthew G. Knepley       tmpO = NULL;
18599bf0dad6SMatthew G. Knepley     }
18609bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
18619bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
18629bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
18639bf0dad6SMatthew 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. */
18649bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
18659bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
18669bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
18679bf0dad6SMatthew G. Knepley       PetscInt       c;
18689bf0dad6SMatthew G. Knepley 
18699bf0dad6SMatthew G. Knepley       if (rev) {
18709bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
18719bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
18729bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
18739bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
18749bf0dad6SMatthew G. Knepley       }
18759bf0dad6SMatthew G. Knepley       /* Check for duplicate */
18769bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
18779bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
18789bf0dad6SMatthew G. Knepley       }
18799bf0dad6SMatthew G. Knepley       if (c == closureSize) {
18809bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
18819bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
18829bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
18839bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
18849bf0dad6SMatthew G. Knepley         closureSize           += 2;
18859bf0dad6SMatthew G. Knepley         fifoSize              += 2;
18869bf0dad6SMatthew G. Knepley       }
18879bf0dad6SMatthew G. Knepley     }
18889bf0dad6SMatthew G. Knepley     fifoStart += 2;
18899bf0dad6SMatthew G. Knepley   }
18909bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
18919bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
18929bf0dad6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
18939bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
18949bf0dad6SMatthew G. Knepley }
18959bf0dad6SMatthew G. Knepley 
18969bf0dad6SMatthew G. Knepley #undef __FUNCT__
1897552f7358SJed Brown #define __FUNCT__ "DMPlexRestoreTransitiveClosure"
1898552f7358SJed Brown /*@C
1899552f7358SJed 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
1900552f7358SJed Brown 
1901552f7358SJed Brown   Not collective
1902552f7358SJed Brown 
1903552f7358SJed Brown   Input Parameters:
1904552f7358SJed Brown + mesh - The DMPlex
1905552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1906552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
1907e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
1908e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
1909552f7358SJed Brown 
1910552f7358SJed Brown   Note:
19110298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
1912552f7358SJed Brown 
19133813dfbdSMatthew G Knepley   Fortran Notes:
19143813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
19153813dfbdSMatthew G Knepley   include petsc.h90 in your code.
19163813dfbdSMatthew G Knepley 
19173813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
19183813dfbdSMatthew G Knepley 
1919552f7358SJed Brown   Level: beginner
1920552f7358SJed Brown 
1921552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1922552f7358SJed Brown @*/
1923552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1924552f7358SJed Brown {
1925552f7358SJed Brown   PetscErrorCode ierr;
1926552f7358SJed Brown 
1927552f7358SJed Brown   PetscFunctionBegin;
1928552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1929e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
1930e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
1931552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, points);CHKERRQ(ierr);
19324ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
1933552f7358SJed Brown   PetscFunctionReturn(0);
1934552f7358SJed Brown }
1935552f7358SJed Brown 
1936552f7358SJed Brown #undef __FUNCT__
1937552f7358SJed Brown #define __FUNCT__ "DMPlexGetMaxSizes"
1938552f7358SJed Brown /*@
1939552f7358SJed Brown   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the Sieve DAG
1940552f7358SJed Brown 
1941552f7358SJed Brown   Not collective
1942552f7358SJed Brown 
1943552f7358SJed Brown   Input Parameter:
1944552f7358SJed Brown . mesh - The DMPlex
1945552f7358SJed Brown 
1946552f7358SJed Brown   Output Parameters:
1947552f7358SJed Brown + maxConeSize - The maximum number of in-edges
1948552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
1949552f7358SJed Brown 
1950552f7358SJed Brown   Level: beginner
1951552f7358SJed Brown 
1952552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1953552f7358SJed Brown @*/
1954552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
1955552f7358SJed Brown {
1956552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
1957552f7358SJed Brown 
1958552f7358SJed Brown   PetscFunctionBegin;
1959552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1960552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
1961552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
1962552f7358SJed Brown   PetscFunctionReturn(0);
1963552f7358SJed Brown }
1964552f7358SJed Brown 
1965552f7358SJed Brown #undef __FUNCT__
1966552f7358SJed Brown #define __FUNCT__ "DMSetUp_Plex"
1967552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
1968552f7358SJed Brown {
1969552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1970552f7358SJed Brown   PetscInt       size;
1971552f7358SJed Brown   PetscErrorCode ierr;
1972552f7358SJed Brown 
1973552f7358SJed Brown   PetscFunctionBegin;
1974552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1975552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
1976552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
19771795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
19781795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
1979552f7358SJed Brown   if (mesh->maxSupportSize) {
1980552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
1981552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
19821795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
1983552f7358SJed Brown   }
1984552f7358SJed Brown   PetscFunctionReturn(0);
1985552f7358SJed Brown }
1986552f7358SJed Brown 
1987552f7358SJed Brown #undef __FUNCT__
1988552f7358SJed Brown #define __FUNCT__ "DMCreateSubDM_Plex"
1989552f7358SJed Brown PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1990552f7358SJed Brown {
1991552f7358SJed Brown   PetscErrorCode ierr;
1992552f7358SJed Brown 
1993552f7358SJed Brown   PetscFunctionBegin;
19944d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
19954d9407bcSMatthew G. Knepley   ierr = DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1996552f7358SJed Brown   PetscFunctionReturn(0);
1997552f7358SJed Brown }
1998552f7358SJed Brown 
1999552f7358SJed Brown #undef __FUNCT__
2000552f7358SJed Brown #define __FUNCT__ "DMPlexSymmetrize"
2001552f7358SJed Brown /*@
2002552f7358SJed Brown   DMPlexSymmetrize - Creates support (out-edge) information from cone (in-edge) inoformation
2003552f7358SJed Brown 
2004552f7358SJed Brown   Not collective
2005552f7358SJed Brown 
2006552f7358SJed Brown   Input Parameter:
2007552f7358SJed Brown . mesh - The DMPlex
2008552f7358SJed Brown 
2009552f7358SJed Brown   Output Parameter:
2010552f7358SJed Brown 
2011552f7358SJed Brown   Note:
2012552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2013552f7358SJed Brown 
2014552f7358SJed Brown   Level: beginner
2015552f7358SJed Brown 
2016552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2017552f7358SJed Brown @*/
2018552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2019552f7358SJed Brown {
2020552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2021552f7358SJed Brown   PetscInt      *offsets;
2022552f7358SJed Brown   PetscInt       supportSize;
2023552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2024552f7358SJed Brown   PetscErrorCode ierr;
2025552f7358SJed Brown 
2026552f7358SJed Brown   PetscFunctionBegin;
2027552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
202882f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
2029552f7358SJed Brown   /* Calculate support sizes */
2030552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2031552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2032552f7358SJed Brown     PetscInt dof, off, c;
2033552f7358SJed Brown 
2034552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2035552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2036552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2037552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2038552f7358SJed Brown     }
2039552f7358SJed Brown   }
2040552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2041552f7358SJed Brown     PetscInt dof;
2042552f7358SJed Brown 
2043552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
20440d644c17SKarl Rupp 
2045552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2046552f7358SJed Brown   }
2047552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2048552f7358SJed Brown   /* Calculate supports */
2049552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
20501795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
20511795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2052552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2053552f7358SJed Brown     PetscInt dof, off, c;
2054552f7358SJed Brown 
2055552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2056552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2057552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2058552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2059552f7358SJed Brown       PetscInt       offS;
2060552f7358SJed Brown 
2061552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
20620d644c17SKarl Rupp 
2063552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2064552f7358SJed Brown       ++offsets[q];
2065552f7358SJed Brown     }
2066552f7358SJed Brown   }
2067552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
2068552f7358SJed Brown   PetscFunctionReturn(0);
2069552f7358SJed Brown }
2070552f7358SJed Brown 
2071552f7358SJed Brown #undef __FUNCT__
2072552f7358SJed Brown #define __FUNCT__ "DMPlexStratify"
2073552f7358SJed Brown /*@
2074552f7358SJed Brown   DMPlexStratify - The Sieve DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
2075552f7358SJed Brown   can be illustrated by Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2076552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2077552f7358SJed Brown   the DAG.
2078552f7358SJed Brown 
2079bf4602e4SToby Isaac   Collective on dm
2080552f7358SJed Brown 
2081552f7358SJed Brown   Input Parameter:
2082552f7358SJed Brown . mesh - The DMPlex
2083552f7358SJed Brown 
2084552f7358SJed Brown   Output Parameter:
2085552f7358SJed Brown 
2086552f7358SJed Brown   Notes:
2087150b719bSJed Brown   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
2088150b719bSJed Brown   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2089c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2090150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2091552f7358SJed Brown 
2092150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2093552f7358SJed Brown 
2094552f7358SJed Brown   Level: beginner
2095552f7358SJed Brown 
2096552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
2097552f7358SJed Brown @*/
2098552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2099552f7358SJed Brown {
2100df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2101aa50250dSMatthew G. Knepley   DMLabel        label;
2102552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2103552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
2104552f7358SJed Brown   PetscErrorCode ierr;
2105552f7358SJed Brown 
2106552f7358SJed Brown   PetscFunctionBegin;
2107552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2108552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2109552f7358SJed Brown   /* Calculate depth */
2110aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2111c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2112aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2113552f7358SJed Brown   /* Initialize roots and count leaves */
2114552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2115552f7358SJed Brown     PetscInt coneSize, supportSize;
2116552f7358SJed Brown 
2117552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2118552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2119552f7358SJed Brown     if (!coneSize && supportSize) {
2120552f7358SJed Brown       ++numRoots;
2121aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2122552f7358SJed Brown     } else if (!supportSize && coneSize) {
2123552f7358SJed Brown       ++numLeaves;
2124552f7358SJed Brown     } else if (!supportSize && !coneSize) {
2125552f7358SJed Brown       /* Isolated points */
2126aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2127552f7358SJed Brown     }
2128552f7358SJed Brown   }
2129552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2130552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
2131552f7358SJed Brown       PetscInt coneSize, supportSize;
2132552f7358SJed Brown 
2133552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2134552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2135552f7358SJed Brown       if (!supportSize && coneSize) {
2136aa50250dSMatthew G. Knepley         ierr = DMLabelSetValue(label, p, 1);CHKERRQ(ierr);
2137552f7358SJed Brown       }
2138552f7358SJed Brown     }
2139552f7358SJed Brown   } else {
214074ef644bSMatthew G. Knepley     IS       pointIS;
214174ef644bSMatthew G. Knepley     PetscInt numPoints = 0, level = 0;
2142552f7358SJed Brown 
214374ef644bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
214474ef644bSMatthew G. Knepley     if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
214574ef644bSMatthew G. Knepley     while (numPoints) {
214674ef644bSMatthew G. Knepley       const PetscInt *points;
214774ef644bSMatthew G. Knepley       const PetscInt  newLevel = level+1;
214874ef644bSMatthew G. Knepley 
214974ef644bSMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
215074ef644bSMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
215174ef644bSMatthew G. Knepley         const PetscInt  point = points[p];
215274ef644bSMatthew G. Knepley         const PetscInt *support;
215374ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
215474ef644bSMatthew G. Knepley 
215574ef644bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
215674ef644bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
215774ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
215874ef644bSMatthew G. Knepley           ierr = DMLabelSetValue(label, support[s], newLevel);CHKERRQ(ierr);
2159552f7358SJed Brown         }
2160552f7358SJed Brown       }
21615edfc765SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
216274ef644bSMatthew G. Knepley       ++level;
216374ef644bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
216474ef644bSMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
216574ef644bSMatthew G. Knepley       if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
216674ef644bSMatthew G. Knepley       else         {numPoints = 0;}
216774ef644bSMatthew G. Knepley     }
216874ef644bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
216974ef644bSMatthew G. Knepley   }
2170bf4602e4SToby Isaac   { /* just in case there is an empty process */
2171bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2172bf4602e4SToby Isaac 
2173bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label,&numValues);CHKERRQ(ierr);
21744de306b1SToby Isaac     for (v = 0; v < numValues; v++) {
21754de306b1SToby Isaac       IS pointIS;
21764de306b1SToby Isaac 
21774de306b1SToby Isaac       ierr = DMLabelGetStratumIS(label, v, &pointIS);CHKERRQ(ierr);
21784de306b1SToby Isaac       if (pointIS) {
21794de306b1SToby Isaac         PetscInt  min, max, numPoints;
21804de306b1SToby Isaac         PetscInt  start;
21814de306b1SToby Isaac         PetscBool contig;
21824de306b1SToby Isaac 
21834de306b1SToby Isaac         ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
21844de306b1SToby Isaac         ierr = ISGetMinMax(pointIS, &min, &max);CHKERRQ(ierr);
21854de306b1SToby Isaac         ierr = ISContiguousLocal(pointIS,min,max+1,&start,&contig);CHKERRQ(ierr);
21864de306b1SToby Isaac         if (start == 0 && contig) {
21874de306b1SToby Isaac           ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
21884de306b1SToby Isaac           ierr = ISCreateStride(PETSC_COMM_SELF,numPoints,min,1,&pointIS);CHKERRQ(ierr);
21894de306b1SToby Isaac           ierr = DMLabelSetStratumIS(label, v, pointIS);CHKERRQ(ierr);
21904de306b1SToby Isaac         }
21914de306b1SToby Isaac       }
21924de306b1SToby Isaac       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
21934de306b1SToby Isaac     }
21946d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2195bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2196bf4602e4SToby Isaac       DMLabelAddStratum(label,v);CHKERRQ(ierr);
2197bf4602e4SToby Isaac     }
2198bf4602e4SToby Isaac   }
2199bf4602e4SToby Isaac 
2200df0420ecSMatthew G. Knepley   ierr = DMLabelGetState(label, &mesh->depthState);CHKERRQ(ierr);
2201552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2202552f7358SJed Brown   PetscFunctionReturn(0);
2203552f7358SJed Brown }
2204552f7358SJed Brown 
2205552f7358SJed Brown #undef __FUNCT__
2206552f7358SJed Brown #define __FUNCT__ "DMPlexGetJoin"
2207552f7358SJed Brown /*@C
2208552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
2209552f7358SJed Brown 
2210552f7358SJed Brown   Not Collective
2211552f7358SJed Brown 
2212552f7358SJed Brown   Input Parameters:
2213552f7358SJed Brown + dm - The DMPlex object
2214552f7358SJed Brown . numPoints - The number of input points for the join
2215552f7358SJed Brown - points - The input points
2216552f7358SJed Brown 
2217552f7358SJed Brown   Output Parameters:
2218552f7358SJed Brown + numCoveredPoints - The number of points in the join
2219552f7358SJed Brown - coveredPoints - The points in the join
2220552f7358SJed Brown 
2221552f7358SJed Brown   Level: intermediate
2222552f7358SJed Brown 
2223552f7358SJed Brown   Note: Currently, this is restricted to a single level join
2224552f7358SJed Brown 
22253813dfbdSMatthew G Knepley   Fortran Notes:
22263813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22273813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22283813dfbdSMatthew G Knepley 
22293813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
22303813dfbdSMatthew G Knepley 
2231552f7358SJed Brown .keywords: mesh
2232552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2233552f7358SJed Brown @*/
2234552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2235552f7358SJed Brown {
2236552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2237552f7358SJed Brown   PetscInt      *join[2];
2238552f7358SJed Brown   PetscInt       joinSize, i = 0;
2239552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2240552f7358SJed Brown   PetscErrorCode ierr;
2241552f7358SJed Brown 
2242552f7358SJed Brown   PetscFunctionBegin;
2243552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2244552f7358SJed Brown   PetscValidPointer(points, 2);
2245552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2246552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2247552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
2248552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
2249552f7358SJed Brown   /* Copy in support of first point */
2250552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
2251552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
2252552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
2253552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
2254552f7358SJed Brown   }
2255552f7358SJed Brown   /* Check each successive support */
2256552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2257552f7358SJed Brown     PetscInt newJoinSize = 0;
2258552f7358SJed Brown 
2259552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
2260552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
2261552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2262552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
2263552f7358SJed Brown 
2264552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
2265552f7358SJed Brown         if (point == join[i][m]) {
2266552f7358SJed Brown           join[1-i][newJoinSize++] = point;
2267552f7358SJed Brown           break;
2268552f7358SJed Brown         }
2269552f7358SJed Brown       }
2270552f7358SJed Brown     }
2271552f7358SJed Brown     joinSize = newJoinSize;
2272552f7358SJed Brown     i        = 1-i;
2273552f7358SJed Brown   }
2274552f7358SJed Brown   *numCoveredPoints = joinSize;
2275552f7358SJed Brown   *coveredPoints    = join[i];
2276552f7358SJed Brown   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
2277552f7358SJed Brown   PetscFunctionReturn(0);
2278552f7358SJed Brown }
2279552f7358SJed Brown 
2280552f7358SJed Brown #undef __FUNCT__
2281552f7358SJed Brown #define __FUNCT__ "DMPlexRestoreJoin"
2282552f7358SJed Brown /*@C
2283552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
2284552f7358SJed Brown 
2285552f7358SJed Brown   Not Collective
2286552f7358SJed Brown 
2287552f7358SJed Brown   Input Parameters:
2288552f7358SJed Brown + dm - The DMPlex object
2289552f7358SJed Brown . numPoints - The number of input points for the join
2290552f7358SJed Brown - points - The input points
2291552f7358SJed Brown 
2292552f7358SJed Brown   Output Parameters:
2293552f7358SJed Brown + numCoveredPoints - The number of points in the join
2294552f7358SJed Brown - coveredPoints - The points in the join
2295552f7358SJed Brown 
22963813dfbdSMatthew G Knepley   Fortran Notes:
22973813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22983813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22993813dfbdSMatthew G Knepley 
23003813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
23013813dfbdSMatthew G Knepley 
2302552f7358SJed Brown   Level: intermediate
2303552f7358SJed Brown 
2304552f7358SJed Brown .keywords: mesh
2305552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
2306552f7358SJed Brown @*/
2307552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2308552f7358SJed Brown {
2309552f7358SJed Brown   PetscErrorCode ierr;
2310552f7358SJed Brown 
2311552f7358SJed Brown   PetscFunctionBegin;
2312552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2313d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2314d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2315d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
2316552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
2317d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2318552f7358SJed Brown   PetscFunctionReturn(0);
2319552f7358SJed Brown }
2320552f7358SJed Brown 
2321552f7358SJed Brown #undef __FUNCT__
2322552f7358SJed Brown #define __FUNCT__ "DMPlexGetFullJoin"
2323552f7358SJed Brown /*@C
2324552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
2325552f7358SJed Brown 
2326552f7358SJed Brown   Not Collective
2327552f7358SJed Brown 
2328552f7358SJed Brown   Input Parameters:
2329552f7358SJed Brown + dm - The DMPlex object
2330552f7358SJed Brown . numPoints - The number of input points for the join
2331552f7358SJed Brown - points - The input points
2332552f7358SJed Brown 
2333552f7358SJed Brown   Output Parameters:
2334552f7358SJed Brown + numCoveredPoints - The number of points in the join
2335552f7358SJed Brown - coveredPoints - The points in the join
2336552f7358SJed Brown 
23373813dfbdSMatthew G Knepley   Fortran Notes:
23383813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
23393813dfbdSMatthew G Knepley   include petsc.h90 in your code.
23403813dfbdSMatthew G Knepley 
23413813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
23423813dfbdSMatthew G Knepley 
2343552f7358SJed Brown   Level: intermediate
2344552f7358SJed Brown 
2345552f7358SJed Brown .keywords: mesh
2346552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
2347552f7358SJed Brown @*/
2348552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2349552f7358SJed Brown {
2350552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2351552f7358SJed Brown   PetscInt      *offsets, **closures;
2352552f7358SJed Brown   PetscInt      *join[2];
2353552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
235424c766afSToby Isaac   PetscInt       p, d, c, m, ms;
2355552f7358SJed Brown   PetscErrorCode ierr;
2356552f7358SJed Brown 
2357552f7358SJed Brown   PetscFunctionBegin;
2358552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2359552f7358SJed Brown   PetscValidPointer(points, 2);
2360552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2361552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2362552f7358SJed Brown 
2363552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
23641795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
2365552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
236624c766afSToby Isaac   ms      = mesh->maxSupportSize;
236724c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
2368552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
2369552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
2370552f7358SJed Brown 
2371552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2372552f7358SJed Brown     PetscInt closureSize;
2373552f7358SJed Brown 
2374552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
23750d644c17SKarl Rupp 
2376552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
2377552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
2378552f7358SJed Brown       PetscInt pStart, pEnd, i;
2379552f7358SJed Brown 
2380552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
2381552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
2382552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2383552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
2384552f7358SJed Brown           break;
2385552f7358SJed Brown         }
2386552f7358SJed Brown       }
2387552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
2388552f7358SJed Brown     }
238982f516ccSBarry 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);
2390552f7358SJed Brown   }
2391552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
2392552f7358SJed Brown     PetscInt dof;
2393552f7358SJed Brown 
2394552f7358SJed Brown     /* Copy in support of first point */
2395552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
2396552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
2397552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
2398552f7358SJed Brown     }
2399552f7358SJed Brown     /* Check each successive cone */
2400552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
2401552f7358SJed Brown       PetscInt newJoinSize = 0;
2402552f7358SJed Brown 
2403552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
2404552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2405552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
2406552f7358SJed Brown 
2407552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
2408552f7358SJed Brown           if (point == join[i][m]) {
2409552f7358SJed Brown             join[1-i][newJoinSize++] = point;
2410552f7358SJed Brown             break;
2411552f7358SJed Brown           }
2412552f7358SJed Brown         }
2413552f7358SJed Brown       }
2414552f7358SJed Brown       joinSize = newJoinSize;
2415552f7358SJed Brown       i        = 1-i;
2416552f7358SJed Brown     }
2417552f7358SJed Brown     if (joinSize) break;
2418552f7358SJed Brown   }
2419552f7358SJed Brown   *numCoveredPoints = joinSize;
2420552f7358SJed Brown   *coveredPoints    = join[i];
2421552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
24220298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
2423552f7358SJed Brown   }
2424552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
2425552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
2426552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
2427552f7358SJed Brown   PetscFunctionReturn(0);
2428552f7358SJed Brown }
2429552f7358SJed Brown 
2430552f7358SJed Brown #undef __FUNCT__
2431552f7358SJed Brown #define __FUNCT__ "DMPlexGetMeet"
2432552f7358SJed Brown /*@C
2433552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
2434552f7358SJed Brown 
2435552f7358SJed Brown   Not Collective
2436552f7358SJed Brown 
2437552f7358SJed Brown   Input Parameters:
2438552f7358SJed Brown + dm - The DMPlex object
2439552f7358SJed Brown . numPoints - The number of input points for the meet
2440552f7358SJed Brown - points - The input points
2441552f7358SJed Brown 
2442552f7358SJed Brown   Output Parameters:
2443552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2444552f7358SJed Brown - coveredPoints - The points in the meet
2445552f7358SJed Brown 
2446552f7358SJed Brown   Level: intermediate
2447552f7358SJed Brown 
2448552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
2449552f7358SJed Brown 
24503813dfbdSMatthew G Knepley   Fortran Notes:
24513813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24523813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24533813dfbdSMatthew G Knepley 
24543813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24553813dfbdSMatthew G Knepley 
2456552f7358SJed Brown .keywords: mesh
2457552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
2458552f7358SJed Brown @*/
2459552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
2460552f7358SJed Brown {
2461552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2462552f7358SJed Brown   PetscInt      *meet[2];
2463552f7358SJed Brown   PetscInt       meetSize, i = 0;
2464552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2465552f7358SJed Brown   PetscErrorCode ierr;
2466552f7358SJed Brown 
2467552f7358SJed Brown   PetscFunctionBegin;
2468552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2469552f7358SJed Brown   PetscValidPointer(points, 2);
2470552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
2471552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
2472552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
2473552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
2474552f7358SJed Brown   /* Copy in cone of first point */
2475552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
2476552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
2477552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
2478552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
2479552f7358SJed Brown   }
2480552f7358SJed Brown   /* Check each successive cone */
2481552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2482552f7358SJed Brown     PetscInt newMeetSize = 0;
2483552f7358SJed Brown 
2484552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
2485552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
2486552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2487552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
2488552f7358SJed Brown 
2489552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
2490552f7358SJed Brown         if (point == meet[i][m]) {
2491552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
2492552f7358SJed Brown           break;
2493552f7358SJed Brown         }
2494552f7358SJed Brown       }
2495552f7358SJed Brown     }
2496552f7358SJed Brown     meetSize = newMeetSize;
2497552f7358SJed Brown     i        = 1-i;
2498552f7358SJed Brown   }
2499552f7358SJed Brown   *numCoveringPoints = meetSize;
2500552f7358SJed Brown   *coveringPoints    = meet[i];
2501552f7358SJed Brown   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
2502552f7358SJed Brown   PetscFunctionReturn(0);
2503552f7358SJed Brown }
2504552f7358SJed Brown 
2505552f7358SJed Brown #undef __FUNCT__
2506552f7358SJed Brown #define __FUNCT__ "DMPlexRestoreMeet"
2507552f7358SJed Brown /*@C
2508552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
2509552f7358SJed Brown 
2510552f7358SJed Brown   Not Collective
2511552f7358SJed Brown 
2512552f7358SJed Brown   Input Parameters:
2513552f7358SJed Brown + dm - The DMPlex object
2514552f7358SJed Brown . numPoints - The number of input points for the meet
2515552f7358SJed Brown - points - The input points
2516552f7358SJed Brown 
2517552f7358SJed Brown   Output Parameters:
2518552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2519552f7358SJed Brown - coveredPoints - The points in the meet
2520552f7358SJed Brown 
2521552f7358SJed Brown   Level: intermediate
2522552f7358SJed Brown 
25233813dfbdSMatthew G Knepley   Fortran Notes:
25243813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25253813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25263813dfbdSMatthew G Knepley 
25273813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25283813dfbdSMatthew G Knepley 
2529552f7358SJed Brown .keywords: mesh
2530552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
2531552f7358SJed Brown @*/
2532552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2533552f7358SJed Brown {
2534552f7358SJed Brown   PetscErrorCode ierr;
2535552f7358SJed Brown 
2536552f7358SJed Brown   PetscFunctionBegin;
2537552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2538d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2539d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2540d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
2541552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
2542d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2543552f7358SJed Brown   PetscFunctionReturn(0);
2544552f7358SJed Brown }
2545552f7358SJed Brown 
2546552f7358SJed Brown #undef __FUNCT__
2547552f7358SJed Brown #define __FUNCT__ "DMPlexGetFullMeet"
2548552f7358SJed Brown /*@C
2549552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
2550552f7358SJed Brown 
2551552f7358SJed Brown   Not Collective
2552552f7358SJed Brown 
2553552f7358SJed Brown   Input Parameters:
2554552f7358SJed Brown + dm - The DMPlex object
2555552f7358SJed Brown . numPoints - The number of input points for the meet
2556552f7358SJed Brown - points - The input points
2557552f7358SJed Brown 
2558552f7358SJed Brown   Output Parameters:
2559552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2560552f7358SJed Brown - coveredPoints - The points in the meet
2561552f7358SJed Brown 
2562552f7358SJed Brown   Level: intermediate
2563552f7358SJed Brown 
25643813dfbdSMatthew G Knepley   Fortran Notes:
25653813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25663813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25673813dfbdSMatthew G Knepley 
25683813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25693813dfbdSMatthew G Knepley 
2570552f7358SJed Brown .keywords: mesh
2571552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
2572552f7358SJed Brown @*/
2573552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2574552f7358SJed Brown {
2575552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2576552f7358SJed Brown   PetscInt      *offsets, **closures;
2577552f7358SJed Brown   PetscInt      *meet[2];
2578552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
257924c766afSToby Isaac   PetscInt       p, h, c, m, mc;
2580552f7358SJed Brown   PetscErrorCode ierr;
2581552f7358SJed Brown 
2582552f7358SJed Brown   PetscFunctionBegin;
2583552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2584552f7358SJed Brown   PetscValidPointer(points, 2);
2585552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2586552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2587552f7358SJed Brown 
2588552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
2589785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
2590552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
259124c766afSToby Isaac   mc      = mesh->maxConeSize;
259224c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
2593552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
2594552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
2595552f7358SJed Brown 
2596552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2597552f7358SJed Brown     PetscInt closureSize;
2598552f7358SJed Brown 
2599552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
26000d644c17SKarl Rupp 
2601552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
2602552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
2603552f7358SJed Brown       PetscInt pStart, pEnd, i;
2604552f7358SJed Brown 
2605552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
2606552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
2607552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2608552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
2609552f7358SJed Brown           break;
2610552f7358SJed Brown         }
2611552f7358SJed Brown       }
2612552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
2613552f7358SJed Brown     }
261482f516ccSBarry 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);
2615552f7358SJed Brown   }
2616552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
2617552f7358SJed Brown     PetscInt dof;
2618552f7358SJed Brown 
2619552f7358SJed Brown     /* Copy in cone of first point */
2620552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
2621552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
2622552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
2623552f7358SJed Brown     }
2624552f7358SJed Brown     /* Check each successive cone */
2625552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
2626552f7358SJed Brown       PetscInt newMeetSize = 0;
2627552f7358SJed Brown 
2628552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
2629552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2630552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
2631552f7358SJed Brown 
2632552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
2633552f7358SJed Brown           if (point == meet[i][m]) {
2634552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
2635552f7358SJed Brown             break;
2636552f7358SJed Brown           }
2637552f7358SJed Brown         }
2638552f7358SJed Brown       }
2639552f7358SJed Brown       meetSize = newMeetSize;
2640552f7358SJed Brown       i        = 1-i;
2641552f7358SJed Brown     }
2642552f7358SJed Brown     if (meetSize) break;
2643552f7358SJed Brown   }
2644552f7358SJed Brown   *numCoveredPoints = meetSize;
2645552f7358SJed Brown   *coveredPoints    = meet[i];
2646552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
26470298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
2648552f7358SJed Brown   }
2649552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
2650552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
2651552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
2652552f7358SJed Brown   PetscFunctionReturn(0);
2653552f7358SJed Brown }
2654552f7358SJed Brown 
2655552f7358SJed Brown #undef __FUNCT__
26564e3744c5SMatthew G. Knepley #define __FUNCT__ "DMPlexEqual"
26574e3744c5SMatthew G. Knepley /*@C
26584e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
26594e3744c5SMatthew G. Knepley 
26604e3744c5SMatthew G. Knepley   Not Collective
26614e3744c5SMatthew G. Knepley 
26624e3744c5SMatthew G. Knepley   Input Parameters:
26634e3744c5SMatthew G. Knepley + dmA - A DMPlex object
26644e3744c5SMatthew G. Knepley - dmB - A DMPlex object
26654e3744c5SMatthew G. Knepley 
26664e3744c5SMatthew G. Knepley   Output Parameters:
26674e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
26684e3744c5SMatthew G. Knepley 
26694e3744c5SMatthew G. Knepley   Level: intermediate
26704e3744c5SMatthew G. Knepley 
26714e3744c5SMatthew G. Knepley   Notes:
26724e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
26734e3744c5SMatthew G. Knepley 
26744e3744c5SMatthew G. Knepley .keywords: mesh
26754e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
26764e3744c5SMatthew G. Knepley @*/
26774e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
26784e3744c5SMatthew G. Knepley {
26794e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
26804e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
26814e3744c5SMatthew G. Knepley 
26824e3744c5SMatthew G. Knepley   PetscFunctionBegin;
26834e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
26844e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
26854e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
26864e3744c5SMatthew G. Knepley 
26874e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
26884e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
26894e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
26904e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
26914e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
26924e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
26934e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
26944e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
26954e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
26964e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
26974e3744c5SMatthew G. Knepley 
26984e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
26994e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
27004e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
27014e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
27024e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
27034e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
27044e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
27054e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
27064e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
27074e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
27084e3744c5SMatthew G. Knepley     }
27094e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
27104e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
27114e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
27124e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
27134e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
27144e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
27154e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
27164e3744c5SMatthew G. Knepley     }
27174e3744c5SMatthew G. Knepley   }
27184e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
27194e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
27204e3744c5SMatthew G. Knepley }
27214e3744c5SMatthew G. Knepley 
27224e3744c5SMatthew G. Knepley #undef __FUNCT__
272318ad9376SMatthew G. Knepley #define __FUNCT__ "DMPlexGetNumFaceVertices"
272418ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
2725a6dfd86eSKarl Rupp {
272682f516ccSBarry Smith   MPI_Comm       comm;
2727552f7358SJed Brown   PetscErrorCode ierr;
2728552f7358SJed Brown 
2729552f7358SJed Brown   PetscFunctionBegin;
273082f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2731552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
2732552f7358SJed Brown   switch (cellDim) {
2733552f7358SJed Brown   case 0:
2734552f7358SJed Brown     *numFaceVertices = 0;
2735552f7358SJed Brown     break;
2736552f7358SJed Brown   case 1:
2737552f7358SJed Brown     *numFaceVertices = 1;
2738552f7358SJed Brown     break;
2739552f7358SJed Brown   case 2:
2740552f7358SJed Brown     switch (numCorners) {
274119436ca2SJed Brown     case 3: /* triangle */
274219436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2743552f7358SJed Brown       break;
274419436ca2SJed Brown     case 4: /* quadrilateral */
274519436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2746552f7358SJed Brown       break;
274719436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
274819436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2749552f7358SJed Brown       break;
275019436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
275119436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2752552f7358SJed Brown       break;
2753552f7358SJed Brown     default:
2754f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2755552f7358SJed Brown     }
2756552f7358SJed Brown     break;
2757552f7358SJed Brown   case 3:
2758552f7358SJed Brown     switch (numCorners) {
275919436ca2SJed Brown     case 4: /* tetradehdron */
276019436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
2761552f7358SJed Brown       break;
276219436ca2SJed Brown     case 6: /* tet cohesive cells */
276319436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2764552f7358SJed Brown       break;
276519436ca2SJed Brown     case 8: /* hexahedron */
276619436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2767552f7358SJed Brown       break;
276819436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
276919436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2770552f7358SJed Brown       break;
277119436ca2SJed Brown     case 10: /* quadratic tetrahedron */
277219436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2773552f7358SJed Brown       break;
277419436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
277519436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2776552f7358SJed Brown       break;
277719436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
277819436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2779552f7358SJed Brown       break;
278019436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
278119436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
2782552f7358SJed Brown       break;
2783552f7358SJed Brown     default:
2784f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2785552f7358SJed Brown     }
2786552f7358SJed Brown     break;
2787552f7358SJed Brown   default:
2788f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
2789552f7358SJed Brown   }
2790552f7358SJed Brown   PetscFunctionReturn(0);
2791552f7358SJed Brown }
2792552f7358SJed Brown 
2793552f7358SJed Brown #undef __FUNCT__
2794aa50250dSMatthew G. Knepley #define __FUNCT__ "DMPlexGetDepthLabel"
2795552f7358SJed Brown /*@
2796aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
2797552f7358SJed Brown 
2798552f7358SJed Brown   Not Collective
2799552f7358SJed Brown 
2800aa50250dSMatthew G. Knepley   Input Parameter:
2801552f7358SJed Brown . dm    - The DMPlex object
2802552f7358SJed Brown 
2803aa50250dSMatthew G. Knepley   Output Parameter:
2804aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
2805552f7358SJed Brown 
2806552f7358SJed Brown   Level: developer
2807552f7358SJed Brown 
2808aa50250dSMatthew G. Knepley .keywords: mesh, points
2809aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2810aa50250dSMatthew G. Knepley @*/
2811aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
2812aa50250dSMatthew G. Knepley {
2813aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
2814aa50250dSMatthew G. Knepley 
2815aa50250dSMatthew G. Knepley   PetscFunctionBegin;
2816aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2817aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
2818c58f1c22SToby Isaac   if (!dm->depthLabel) {ierr = DMGetLabel(dm, "depth", &dm->depthLabel);CHKERRQ(ierr);}
2819c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
2820aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
2821aa50250dSMatthew G. Knepley }
2822aa50250dSMatthew G. Knepley 
2823aa50250dSMatthew G. Knepley #undef __FUNCT__
2824aa50250dSMatthew G. Knepley #define __FUNCT__ "DMPlexGetDepth"
2825aa50250dSMatthew G. Knepley /*@
2826aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
2827aa50250dSMatthew G. Knepley 
2828aa50250dSMatthew G. Knepley   Not Collective
2829aa50250dSMatthew G. Knepley 
2830aa50250dSMatthew G. Knepley   Input Parameter:
2831aa50250dSMatthew G. Knepley . dm    - The DMPlex object
2832aa50250dSMatthew G. Knepley 
2833aa50250dSMatthew G. Knepley   Output Parameter:
2834aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
2835aa50250dSMatthew G. Knepley 
2836aa50250dSMatthew G. Knepley   Level: developer
2837552f7358SJed Brown 
2838552f7358SJed Brown .keywords: mesh, points
2839aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2840552f7358SJed Brown @*/
2841552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
2842552f7358SJed Brown {
2843aa50250dSMatthew G. Knepley   DMLabel        label;
2844aa50250dSMatthew G. Knepley   PetscInt       d = 0;
2845552f7358SJed Brown   PetscErrorCode ierr;
2846552f7358SJed Brown 
2847552f7358SJed Brown   PetscFunctionBegin;
2848552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2849552f7358SJed Brown   PetscValidPointer(depth, 2);
2850aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2851aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
2852552f7358SJed Brown   *depth = d-1;
2853552f7358SJed Brown   PetscFunctionReturn(0);
2854552f7358SJed Brown }
2855552f7358SJed Brown 
2856552f7358SJed Brown #undef __FUNCT__
2857552f7358SJed Brown #define __FUNCT__ "DMPlexGetDepthStratum"
2858552f7358SJed Brown /*@
2859552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
2860552f7358SJed Brown 
2861552f7358SJed Brown   Not Collective
2862552f7358SJed Brown 
2863552f7358SJed Brown   Input Parameters:
2864552f7358SJed Brown + dm           - The DMPlex object
2865552f7358SJed Brown - stratumValue - The requested depth
2866552f7358SJed Brown 
2867552f7358SJed Brown   Output Parameters:
2868552f7358SJed Brown + start - The first point at this depth
2869552f7358SJed Brown - end   - One beyond the last point at this depth
2870552f7358SJed Brown 
2871552f7358SJed Brown   Level: developer
2872552f7358SJed Brown 
2873552f7358SJed Brown .keywords: mesh, points
2874552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
2875552f7358SJed Brown @*/
28760adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
28770adebc6cSBarry Smith {
2878aa50250dSMatthew G. Knepley   DMLabel        label;
287963d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
2880552f7358SJed Brown   PetscErrorCode ierr;
2881552f7358SJed Brown 
2882552f7358SJed Brown   PetscFunctionBegin;
2883552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
288463d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
288563d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
2886552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
28870d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
288863d1a920SMatthew G. Knepley   if (stratumValue < 0) {
288963d1a920SMatthew G. Knepley     if (start) *start = pStart;
289063d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
289163d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
2892552f7358SJed Brown   }
2893aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
289463d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
289563d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
2896552f7358SJed Brown   PetscFunctionReturn(0);
2897552f7358SJed Brown }
2898552f7358SJed Brown 
2899552f7358SJed Brown #undef __FUNCT__
2900552f7358SJed Brown #define __FUNCT__ "DMPlexGetHeightStratum"
2901552f7358SJed Brown /*@
2902552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
2903552f7358SJed Brown 
2904552f7358SJed Brown   Not Collective
2905552f7358SJed Brown 
2906552f7358SJed Brown   Input Parameters:
2907552f7358SJed Brown + dm           - The DMPlex object
2908552f7358SJed Brown - stratumValue - The requested height
2909552f7358SJed Brown 
2910552f7358SJed Brown   Output Parameters:
2911552f7358SJed Brown + start - The first point at this height
2912552f7358SJed Brown - end   - One beyond the last point at this height
2913552f7358SJed Brown 
2914552f7358SJed Brown   Level: developer
2915552f7358SJed Brown 
2916552f7358SJed Brown .keywords: mesh, points
2917552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
2918552f7358SJed Brown @*/
29190adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
29200adebc6cSBarry Smith {
2921aa50250dSMatthew G. Knepley   DMLabel        label;
292263d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
2923552f7358SJed Brown   PetscErrorCode ierr;
2924552f7358SJed Brown 
2925552f7358SJed Brown   PetscFunctionBegin;
2926552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
292763d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
292863d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
2929552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
29300d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
293163d1a920SMatthew G. Knepley   if (stratumValue < 0) {
293263d1a920SMatthew G. Knepley     if (start) *start = pStart;
293363d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
293463d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
2935552f7358SJed Brown   }
2936aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2937aa50250dSMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");CHKERRQ(ierr);
293863d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
293963d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
2940552f7358SJed Brown   PetscFunctionReturn(0);
2941552f7358SJed Brown }
2942552f7358SJed Brown 
2943552f7358SJed Brown #undef __FUNCT__
2944552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSectionInitial"
2945552f7358SJed Brown /* Set the number of dof on each point and separate by fields */
2946a6dfd86eSKarl Rupp PetscErrorCode DMPlexCreateSectionInitial(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscSection *section)
2947a6dfd86eSKarl Rupp {
2948ee55978aSMatthew G. Knepley   PetscInt      *pMax;
2949916f0f19SMatthew G. Knepley   PetscInt       depth, pStart = 0, pEnd = 0;
2950ee55978aSMatthew G. Knepley   PetscInt       Nf, p, d, dep, f;
2951fa716be8SMatthew G. Knepley   PetscBool     *isFE;
2952552f7358SJed Brown   PetscErrorCode ierr;
2953552f7358SJed Brown 
2954552f7358SJed Brown   PetscFunctionBegin;
2955fa716be8SMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
2956ee55978aSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
2957fa716be8SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
2958fa716be8SMatthew G. Knepley     PetscObject  obj;
2959fa716be8SMatthew G. Knepley     PetscClassId id;
2960fa716be8SMatthew G. Knepley 
2961ee55978aSMatthew G. Knepley     isFE[f] = PETSC_FALSE;
2962ee55978aSMatthew G. Knepley     if (f >= Nf) continue;
2963fa716be8SMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
2964fa716be8SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2965fa716be8SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
2966fa716be8SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
2967552f7358SJed Brown   }
296882f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), section);CHKERRQ(ierr);
2969552f7358SJed Brown   if (numFields > 0) {
2970552f7358SJed Brown     ierr = PetscSectionSetNumFields(*section, numFields);CHKERRQ(ierr);
2971552f7358SJed Brown     if (numComp) {
2972552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
2973552f7358SJed Brown         ierr = PetscSectionSetFieldComponents(*section, f, numComp[f]);CHKERRQ(ierr);
2974d484f18aSToby Isaac         if (isFE[f]) {
2975d484f18aSToby Isaac           PetscFE           fe;
2976d484f18aSToby Isaac           PetscDualSpace    dspace;
2977d484f18aSToby Isaac           const PetscInt    ***perms;
2978d484f18aSToby Isaac           const PetscScalar ***flips;
2979d484f18aSToby Isaac           const PetscInt    *numDof;
2980d484f18aSToby Isaac 
2981d484f18aSToby Isaac           ierr = DMGetField(dm,f,(PetscObject *) &fe);CHKERRQ(ierr);
2982d484f18aSToby Isaac           ierr = PetscFEGetDualSpace(fe,&dspace);CHKERRQ(ierr);
2983d484f18aSToby Isaac           ierr = PetscDualSpaceGetSymmetries(dspace,&perms,&flips);CHKERRQ(ierr);
2984d484f18aSToby Isaac           ierr = PetscDualSpaceGetNumDof(dspace,&numDof);CHKERRQ(ierr);
2985d484f18aSToby Isaac           if (perms || flips) {
2986d484f18aSToby Isaac             DM               K;
2987d484f18aSToby Isaac             DMLabel          depthLabel;
2988d484f18aSToby Isaac             PetscInt         depth, h;
2989d484f18aSToby Isaac             PetscSectionSym  sym;
2990d484f18aSToby Isaac 
2991d484f18aSToby Isaac             ierr = PetscDualSpaceGetDM(dspace,&K);CHKERRQ(ierr);
2992d484f18aSToby Isaac             ierr = DMPlexGetDepthLabel(dm,&depthLabel);CHKERRQ(ierr);
2993d484f18aSToby Isaac             ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
2994d484f18aSToby Isaac             ierr = PetscSectionSymCreateLabel(PetscObjectComm((PetscObject)*section),depthLabel,&sym);CHKERRQ(ierr);
2995d484f18aSToby Isaac             for (h = 0; h <= depth; h++) {
2996d484f18aSToby Isaac               PetscDualSpace    hspace;
2997d484f18aSToby Isaac               PetscInt          kStart, kEnd;
2998d484f18aSToby Isaac               PetscInt          kConeSize;
2999d484f18aSToby Isaac               const PetscInt    **perms0 = NULL;
3000d484f18aSToby Isaac               const PetscScalar **flips0 = NULL;
3001d484f18aSToby Isaac 
3002d484f18aSToby Isaac               ierr = PetscDualSpaceGetHeightSubspace(dspace,h,&hspace);CHKERRQ(ierr);
3003d484f18aSToby Isaac               ierr = DMPlexGetHeightStratum(K,h,&kStart,&kEnd);CHKERRQ(ierr);
3004d484f18aSToby Isaac               if (!hspace) continue;
3005d484f18aSToby Isaac               ierr = PetscDualSpaceGetSymmetries(hspace,&perms,&flips);CHKERRQ(ierr);
3006d484f18aSToby Isaac               if (perms) perms0 = perms[0];
3007d484f18aSToby Isaac               if (flips) flips0 = flips[0];
3008d484f18aSToby Isaac               if (!(perms0 || flips0)) continue;
3009d484f18aSToby Isaac               ierr = DMPlexGetConeSize(K,kStart,&kConeSize);CHKERRQ(ierr);
3010d484f18aSToby Isaac               if (numComp[f] == 1) {
3011d484f18aSToby Isaac                 ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numDof[depth - h],-kConeSize,kConeSize,PETSC_USE_POINTER,perms0 ? &perms0[-kConeSize] : NULL,flips0 ? &flips0[-kConeSize] : NULL);CHKERRQ(ierr);
3012d484f18aSToby Isaac               } else {
3013d484f18aSToby Isaac                 PetscInt    **fieldPerms = NULL, o;
3014d484f18aSToby Isaac                 PetscScalar **fieldFlips = NULL;
3015d484f18aSToby Isaac 
3016d484f18aSToby Isaac                 ierr = PetscCalloc1(2 * kConeSize,&fieldPerms);CHKERRQ(ierr);
3017d484f18aSToby Isaac                 ierr = PetscCalloc1(2 * kConeSize,&fieldFlips);CHKERRQ(ierr);
3018d484f18aSToby Isaac                 for (o = -kConeSize; o < kConeSize; o++) {
3019d484f18aSToby Isaac                   if (perms0 && perms0[o]) {
3020d484f18aSToby Isaac                     PetscInt r, s;
3021d484f18aSToby Isaac 
3022d484f18aSToby Isaac                     ierr = PetscMalloc1(numComp[f] * numDof[depth - h],&fieldPerms[o+kConeSize]);CHKERRQ(ierr);
3023d484f18aSToby Isaac                     for (r = 0; r < numDof[depth - h]; r++) {
3024d484f18aSToby Isaac                       for (s = 0; s < numComp[f]; s++) {
3025d484f18aSToby Isaac                         fieldPerms[o+kConeSize][r * numComp[f] + s] = numComp[f] * perms0[o][r] + s;
3026d484f18aSToby Isaac                       }
3027d484f18aSToby Isaac                     }
3028d484f18aSToby Isaac                   }
3029d484f18aSToby Isaac                   if (flips0 && flips0[o]) {
3030d484f18aSToby Isaac                     PetscInt r, s;
3031d484f18aSToby Isaac 
3032d484f18aSToby Isaac                     ierr = PetscMalloc1(numComp[f] * numDof[depth - h],&fieldFlips[o+kConeSize]);CHKERRQ(ierr);
3033d484f18aSToby Isaac                     for (r = 0; r < numDof[depth - h]; r++) {
3034d484f18aSToby Isaac                       for (s = 0; s < numComp[f]; s++) {
3035d484f18aSToby Isaac                         fieldFlips[o+kConeSize][r * numComp[f] + s] = flips0[o][r];
3036d484f18aSToby Isaac                       }
3037d484f18aSToby Isaac                     }
3038d484f18aSToby Isaac                   }
3039d484f18aSToby Isaac                 }
3040d484f18aSToby Isaac                 ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numComp[f] * numDof[depth - h],-kConeSize,kConeSize,PETSC_OWN_POINTER,(const PetscInt **) fieldPerms,(const PetscScalar **)fieldFlips);CHKERRQ(ierr);
3041d484f18aSToby Isaac               }
3042d484f18aSToby Isaac             }
3043d484f18aSToby Isaac             ierr = PetscSectionSetFieldSym(*section,f,sym);CHKERRQ(ierr);
3044d484f18aSToby Isaac             ierr = PetscSectionSymDestroy(&sym);CHKERRQ(ierr);
3045d484f18aSToby Isaac           }
3046d484f18aSToby Isaac         }
3047552f7358SJed Brown       }
3048552f7358SJed Brown     }
3049552f7358SJed Brown   }
3050552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3051552f7358SJed Brown   ierr = PetscSectionSetChart(*section, pStart, pEnd);CHKERRQ(ierr);
3052916f0f19SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
30539ac3fadcSMatthew G. Knepley   ierr = PetscMalloc1(depth+1,&pMax);CHKERRQ(ierr);
30549ac3fadcSMatthew 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);
3055916f0f19SMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
3056916f0f19SMatthew G. Knepley     d    = dim == depth ? dep : (!dep ? 0 : dim);
3057916f0f19SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dep, &pStart, &pEnd);CHKERRQ(ierr);
3058fa716be8SMatthew G. Knepley     pMax[dep] = pMax[dep] < 0 ? pEnd : pMax[dep];
3059552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
3060ee55978aSMatthew G. Knepley       PetscInt tot = 0;
3061ee55978aSMatthew G. Knepley 
3062552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3063fa716be8SMatthew G. Knepley         if (isFE[f] && p >= pMax[dep]) continue;
3064552f7358SJed Brown         ierr = PetscSectionSetFieldDof(*section, p, f, numDof[f*(dim+1)+d]);CHKERRQ(ierr);
3065ee55978aSMatthew G. Knepley         tot += numDof[f*(dim+1)+d];
3066552f7358SJed Brown       }
3067ee55978aSMatthew G. Knepley       ierr = PetscSectionSetDof(*section, p, tot);CHKERRQ(ierr);
3068552f7358SJed Brown     }
3069552f7358SJed Brown   }
30709ac3fadcSMatthew G. Knepley   ierr = PetscFree(pMax);CHKERRQ(ierr);
3071fa716be8SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
3072552f7358SJed Brown   PetscFunctionReturn(0);
3073552f7358SJed Brown }
3074552f7358SJed Brown 
3075552f7358SJed Brown #undef __FUNCT__
3076552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSectionBCDof"
3077552f7358SJed Brown /* Set the number of dof on each point and separate by fields
3078ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3079552f7358SJed Brown */
3080ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSectionBCDof(DM dm, PetscInt numBC, const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
3081a6dfd86eSKarl Rupp {
3082552f7358SJed Brown   PetscInt       numFields;
3083552f7358SJed Brown   PetscInt       bc;
3084a68b90caSToby Isaac   PetscSection   aSec;
3085552f7358SJed Brown   PetscErrorCode ierr;
3086552f7358SJed Brown 
3087552f7358SJed Brown   PetscFunctionBegin;
3088552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3089552f7358SJed Brown   for (bc = 0; bc < numBC; ++bc) {
3090552f7358SJed Brown     PetscInt        field = 0;
3091ab1d0545SMatthew G. Knepley     const PetscInt *comp;
3092552f7358SJed Brown     const PetscInt *idx;
3093ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3094552f7358SJed Brown 
30950d644c17SKarl Rupp     if (numFields) field = bcField[bc];
3096ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3097ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3098552f7358SJed Brown     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3099552f7358SJed Brown     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3100552f7358SJed Brown     for (i = 0; i < n; ++i) {
3101552f7358SJed Brown       const PetscInt p = idx[i];
310206ff1081SMatthew G. Knepley       PetscInt       numConst;
3103552f7358SJed Brown 
3104552f7358SJed Brown       if (numFields) {
3105552f7358SJed Brown         ierr = PetscSectionGetFieldDof(section, p, field, &numConst);CHKERRQ(ierr);
3106552f7358SJed Brown       } else {
3107552f7358SJed Brown         ierr = PetscSectionGetDof(section, p, &numConst);CHKERRQ(ierr);
3108552f7358SJed Brown       }
310906ff1081SMatthew G. Knepley       /* If Nc < 0, constrain every dof on the point */
311006ff1081SMatthew G. Knepley       if (Nc > 0) numConst = PetscMin(numConst, Nc);
311106ff1081SMatthew G. Knepley       if (numFields) {ierr = PetscSectionAddFieldConstraintDof(section, p, field, numConst);CHKERRQ(ierr);}
3112552f7358SJed Brown       ierr = PetscSectionAddConstraintDof(section, p, numConst);CHKERRQ(ierr);
3113552f7358SJed Brown     }
3114552f7358SJed Brown     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
311506ff1081SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3116552f7358SJed Brown   }
3117a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3118a68b90caSToby Isaac   if (aSec) {
3119a68b90caSToby Isaac     PetscInt aStart, aEnd, a;
3120a68b90caSToby Isaac 
3121a68b90caSToby Isaac     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3122a68b90caSToby Isaac     for (a = aStart; a < aEnd; a++) {
3123ab1d0545SMatthew G. Knepley       PetscInt dof, f;
3124a68b90caSToby Isaac 
3125a68b90caSToby Isaac       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3126a68b90caSToby Isaac       if (dof) {
3127a68b90caSToby Isaac         /* if there are point-to-point constraints, then all dofs are constrained */
3128a68b90caSToby Isaac         ierr = PetscSectionGetDof(section, a, &dof);CHKERRQ(ierr);
3129a68b90caSToby Isaac         ierr = PetscSectionSetConstraintDof(section, a, dof);CHKERRQ(ierr);
3130a68b90caSToby Isaac         for (f = 0; f < numFields; f++) {
3131a68b90caSToby Isaac           ierr = PetscSectionGetFieldDof(section, a, f, &dof);CHKERRQ(ierr);
3132a68b90caSToby Isaac           ierr = PetscSectionSetFieldConstraintDof(section, a, f, dof);CHKERRQ(ierr);
3133a68b90caSToby Isaac         }
3134a68b90caSToby Isaac       }
3135a68b90caSToby Isaac     }
3136a68b90caSToby Isaac   }
3137552f7358SJed Brown   PetscFunctionReturn(0);
3138552f7358SJed Brown }
3139552f7358SJed Brown 
3140552f7358SJed Brown #undef __FUNCT__
3141ab1d0545SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateSectionBCIndicesField"
3142ab1d0545SMatthew G. Knepley /* Set the constrained field indices on each point
3143ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3144ab1d0545SMatthew G. Knepley */
3145ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSectionBCIndicesField(DM dm, PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
31460adebc6cSBarry Smith {
3147ab1d0545SMatthew G. Knepley   PetscSection   aSec;
3148ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3149ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, bc, f, d;
3150552f7358SJed Brown   PetscErrorCode ierr;
3151552f7358SJed Brown 
3152552f7358SJed Brown   PetscFunctionBegin;
3153552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3154ab1d0545SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
3155ab1d0545SMatthew G. Knepley   /* Initialize all field indices to -1 */
3156552f7358SJed Brown   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3157ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3158ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3159ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3160ab1d0545SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) for (f = 0; f < numFields; ++f) {ierr = PetscSectionSetFieldConstraintIndices(section, p, f, indices);CHKERRQ(ierr);}
3161ab1d0545SMatthew G. Knepley   /* Handle BC constraints */
3162ab1d0545SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {
3163ab1d0545SMatthew G. Knepley     const PetscInt  field = bcField[bc];
3164ab1d0545SMatthew G. Knepley     const PetscInt *comp, *idx;
3165ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3166552f7358SJed Brown 
3167ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3168ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3169ab1d0545SMatthew G. Knepley     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3170ab1d0545SMatthew G. Knepley     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3171ab1d0545SMatthew G. Knepley     for (i = 0; i < n; ++i) {
3172ab1d0545SMatthew G. Knepley       const PetscInt  p = idx[i];
3173ab1d0545SMatthew G. Knepley       const PetscInt *find;
3174ab1d0545SMatthew G. Knepley       PetscInt        fcdof, c;
3175ab1d0545SMatthew G. Knepley 
3176ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintDof(section, p, field, &fcdof);CHKERRQ(ierr);
3177ab1d0545SMatthew G. Knepley       if (Nc < 0) {
3178ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) indices[d] = d;
3179552f7358SJed Brown       } else {
3180ab1d0545SMatthew G. Knepley         ierr = PetscSectionGetFieldConstraintIndices(section, p, field, &find);CHKERRQ(ierr);
3181ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) {if (find[d] < 0) break; indices[d] = find[d];}
3182ab1d0545SMatthew G. Knepley         for (c = 0; c < Nc; ++c) indices[d+c] = comp[c];
3183ab1d0545SMatthew G. Knepley         ierr = PetscSortInt(d+Nc, indices);CHKERRQ(ierr);
3184ab1d0545SMatthew G. Knepley         for (c = d+Nc; c < fcdof; ++c) indices[c] = -1;
3185552f7358SJed Brown       }
3186ab1d0545SMatthew G. Knepley       ierr = PetscSectionSetFieldConstraintIndices(section, p, field, indices);CHKERRQ(ierr);
3187552f7358SJed Brown     }
3188ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3189ab1d0545SMatthew G. Knepley     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3190552f7358SJed Brown   }
3191ab1d0545SMatthew G. Knepley   /* Handle anchors */
3192ab1d0545SMatthew G. Knepley   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3193ab1d0545SMatthew G. Knepley   if (aSec) {
3194ab1d0545SMatthew G. Knepley     PetscInt aStart, aEnd, a;
3195552f7358SJed Brown 
3196ab1d0545SMatthew G. Knepley     for (d = 0; d < maxDof; ++d) indices[d] = d;
3197ab1d0545SMatthew G. Knepley     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3198ab1d0545SMatthew G. Knepley     for (a = aStart; a < aEnd; a++) {
3199ab1d0545SMatthew G. Knepley       PetscInt dof, fdof, f;
3200ab1d0545SMatthew G. Knepley 
3201ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3202ab1d0545SMatthew G. Knepley       if (dof) {
3203ab1d0545SMatthew G. Knepley         /* if there are point-to-point constraints, then all dofs are constrained */
3204ab1d0545SMatthew G. Knepley         for (f = 0; f < numFields; f++) {
3205ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, a, f, &fdof);CHKERRQ(ierr);
3206ab1d0545SMatthew G. Knepley           ierr = PetscSectionSetFieldConstraintIndices(section, a, f, indices);CHKERRQ(ierr);
3207ab1d0545SMatthew G. Knepley         }
3208ab1d0545SMatthew G. Knepley       }
3209ab1d0545SMatthew G. Knepley     }
3210ab1d0545SMatthew G. Knepley   }
3211ab1d0545SMatthew G. Knepley   ierr = PetscFree(indices);CHKERRQ(ierr);
3212ab1d0545SMatthew G. Knepley   PetscFunctionReturn(0);
3213ab1d0545SMatthew G. Knepley }
3214ab1d0545SMatthew G. Knepley 
3215ab1d0545SMatthew G. Knepley #undef __FUNCT__
3216ab1d0545SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateSectionBCIndices"
3217ab1d0545SMatthew G. Knepley /* Set the constrained indices on each point */
3218ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSectionBCIndices(DM dm, PetscSection section)
3219ab1d0545SMatthew G. Knepley {
3220ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3221ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, f, d;
3222ab1d0545SMatthew G. Knepley   PetscErrorCode ierr;
3223ab1d0545SMatthew G. Knepley 
3224ab1d0545SMatthew G. Knepley   PetscFunctionBegin;
3225ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3226ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3227ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3228ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3229ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3230552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3231552f7358SJed Brown     PetscInt cdof, d;
3232552f7358SJed Brown 
3233552f7358SJed Brown     ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
3234552f7358SJed Brown     if (cdof) {
3235552f7358SJed Brown       if (numFields) {
3236552f7358SJed Brown         PetscInt numConst = 0, foff = 0;
3237552f7358SJed Brown 
3238552f7358SJed Brown         for (f = 0; f < numFields; ++f) {
3239ab1d0545SMatthew G. Knepley           const PetscInt *find;
3240ab1d0545SMatthew G. Knepley           PetscInt        fcdof, fdof;
3241552f7358SJed Brown 
3242552f7358SJed Brown           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
3243ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
3244ab1d0545SMatthew G. Knepley           /* Change constraint numbering from field component to local dof number */
3245ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintIndices(section, p, f, &find);CHKERRQ(ierr);
3246ab1d0545SMatthew G. Knepley           for (d = 0; d < fcdof; ++d) indices[numConst+d] = find[d] + foff;
3247ab1d0545SMatthew G. Knepley           numConst += fcdof;
3248552f7358SJed Brown           foff     += fdof;
3249552f7358SJed Brown         }
3250552f7358SJed Brown         if (cdof != numConst) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Total number of field constraints %D should be %D", numConst, cdof);
3251552f7358SJed Brown       } else {
32520d644c17SKarl Rupp         for (d = 0; d < cdof; ++d) indices[d] = d;
3253552f7358SJed Brown       }
3254552f7358SJed Brown       ierr = PetscSectionSetConstraintIndices(section, p, indices);CHKERRQ(ierr);
3255552f7358SJed Brown     }
3256552f7358SJed Brown   }
3257552f7358SJed Brown   ierr = PetscFree(indices);CHKERRQ(ierr);
3258552f7358SJed Brown   PetscFunctionReturn(0);
3259552f7358SJed Brown }
3260552f7358SJed Brown 
3261552f7358SJed Brown #undef __FUNCT__
3262552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSection"
3263552f7358SJed Brown /*@C
3264552f7358SJed Brown   DMPlexCreateSection - Create a PetscSection based upon the dof layout specification provided.
3265552f7358SJed Brown 
3266552f7358SJed Brown   Not Collective
3267552f7358SJed Brown 
3268552f7358SJed Brown   Input Parameters:
3269552f7358SJed Brown + dm        - The DMPlex object
3270552f7358SJed Brown . dim       - The spatial dimension of the problem
3271552f7358SJed Brown . numFields - The number of fields in the problem
3272552f7358SJed Brown . numComp   - An array of size numFields that holds the number of components for each field
3273552f7358SJed 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
3274552f7358SJed Brown . numBC     - The number of boundary conditions
3275552f7358SJed Brown . bcField   - An array of size numBC giving the field number for each boundry condition
3276ab1d0545SMatthew G. Knepley . bcComps   - [Optional] An array of size numBC giving an IS holding the field components to which each boundary condition applies
3277ab1d0545SMatthew G. Knepley . bcPoints  - An array of size numBC giving an IS holding the Plex points to which each boundary condition applies
3278f8f126e8SMatthew G. Knepley - perm      - Optional permutation of the chart, or NULL
3279552f7358SJed Brown 
3280552f7358SJed Brown   Output Parameter:
3281552f7358SJed Brown . section - The PetscSection object
3282552f7358SJed Brown 
3283552f7358SJed 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
3284f8f126e8SMatthew G. Knepley   number of dof for field 0 on each edge.
3285f8f126e8SMatthew G. Knepley 
3286f8f126e8SMatthew G. Knepley   The chart permutation is the same one set using PetscSectionSetPermutation()
3287552f7358SJed Brown 
3288552f7358SJed Brown   Level: developer
3289552f7358SJed Brown 
32903813dfbdSMatthew G Knepley   Fortran Notes:
32913813dfbdSMatthew G Knepley   A Fortran 90 version is available as DMPlexCreateSectionF90()
32923813dfbdSMatthew G Knepley 
3293552f7358SJed Brown .keywords: mesh, elements
3294f8f126e8SMatthew G. Knepley .seealso: DMPlexCreate(), PetscSectionCreate(), PetscSectionSetPermutation()
3295552f7358SJed Brown @*/
3296ab1d0545SMatthew 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)
3297a6dfd86eSKarl Rupp {
3298a68b90caSToby Isaac   PetscSection   aSec;
3299552f7358SJed Brown   PetscErrorCode ierr;
3300552f7358SJed Brown 
3301552f7358SJed Brown   PetscFunctionBegin;
3302552f7358SJed Brown   ierr = DMPlexCreateSectionInitial(dm, dim, numFields, numComp, numDof, section);CHKERRQ(ierr);
3303ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSectionBCDof(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3304f8f126e8SMatthew G. Knepley   if (perm) {ierr = PetscSectionSetPermutation(*section, perm);CHKERRQ(ierr);}
3305552f7358SJed Brown   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
3306a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,NULL);CHKERRQ(ierr);
3307ab1d0545SMatthew G. Knepley   if (numBC || aSec) {
3308ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndicesField(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3309ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndices(dm, *section);CHKERRQ(ierr);
3310ab1d0545SMatthew G. Knepley   }
3311ce1779c8SBarry Smith   ierr = PetscSectionViewFromOptions(*section,NULL,"-section_view");CHKERRQ(ierr);
3312552f7358SJed Brown   PetscFunctionReturn(0);
3313552f7358SJed Brown }
3314552f7358SJed Brown 
3315552f7358SJed Brown #undef __FUNCT__
3316552f7358SJed Brown #define __FUNCT__ "DMCreateCoordinateDM_Plex"
33170adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
33180adebc6cSBarry Smith {
3319efe440bfSMatthew G. Knepley   PetscSection   section, s;
3320efe440bfSMatthew G. Knepley   Mat            m;
33213e922f36SToby Isaac   PetscInt       maxHeight;
3322552f7358SJed Brown   PetscErrorCode ierr;
3323552f7358SJed Brown 
3324552f7358SJed Brown   PetscFunctionBegin;
332538221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
33263e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
33273e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
332882f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
3329552f7358SJed Brown   ierr = DMSetDefaultSection(*cdm, section);CHKERRQ(ierr);
33301d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3331efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3332efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3333efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3334efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3335efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
3336552f7358SJed Brown   PetscFunctionReturn(0);
3337552f7358SJed Brown }
3338552f7358SJed Brown 
3339552f7358SJed Brown #undef __FUNCT__
3340552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeSection"
33410adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
33420adebc6cSBarry Smith {
3343552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3344552f7358SJed Brown 
3345552f7358SJed Brown   PetscFunctionBegin;
3346552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3347552f7358SJed Brown   if (section) *section = mesh->coneSection;
3348552f7358SJed Brown   PetscFunctionReturn(0);
3349552f7358SJed Brown }
3350552f7358SJed Brown 
3351552f7358SJed Brown #undef __FUNCT__
33528cb4d582SMatthew G. Knepley #define __FUNCT__ "DMPlexGetSupportSection"
33538cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
33548cb4d582SMatthew G. Knepley {
33558cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
33568cb4d582SMatthew G. Knepley 
33578cb4d582SMatthew G. Knepley   PetscFunctionBegin;
33588cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33598cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
33608cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
33618cb4d582SMatthew G. Knepley }
33628cb4d582SMatthew G. Knepley 
33638cb4d582SMatthew G. Knepley #undef __FUNCT__
3364552f7358SJed Brown #define __FUNCT__ "DMPlexGetCones"
3365a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3366a6dfd86eSKarl Rupp {
3367552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3368552f7358SJed Brown 
3369552f7358SJed Brown   PetscFunctionBegin;
3370552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3371552f7358SJed Brown   if (cones) *cones = mesh->cones;
3372552f7358SJed Brown   PetscFunctionReturn(0);
3373552f7358SJed Brown }
3374552f7358SJed Brown 
3375552f7358SJed Brown #undef __FUNCT__
3376552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeOrientations"
3377a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3378a6dfd86eSKarl Rupp {
3379552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3380552f7358SJed Brown 
3381552f7358SJed Brown   PetscFunctionBegin;
3382552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3383552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3384552f7358SJed Brown   PetscFunctionReturn(0);
3385552f7358SJed Brown }
3386552f7358SJed Brown 
3387552f7358SJed Brown /******************************** FEM Support **********************************/
3388552f7358SJed Brown 
3389552f7358SJed Brown #undef __FUNCT__
33903194fc30SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateSpectralClosurePermutation"
33913194fc30SMatthew G. Knepley PetscErrorCode DMPlexCreateSpectralClosurePermutation(DM dm, PetscSection section)
33923194fc30SMatthew G. Knepley {
33933194fc30SMatthew G. Knepley   PetscInt      *perm;
339489eabcffSMatthew G. Knepley   PetscInt       dim, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
33953194fc30SMatthew G. Knepley   PetscErrorCode ierr;
33963194fc30SMatthew G. Knepley 
33973194fc30SMatthew G. Knepley   PetscFunctionBegin;
33983194fc30SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
33993194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
34003194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
340189eabcffSMatthew G. Knepley   if (dim <= 1) PetscFunctionReturn(0);
34023194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
34033194fc30SMatthew G. Knepley     /* An order k SEM disc has k-1 dofs on an edge */
34043194fc30SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
34053194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
34063194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
34073194fc30SMatthew G. Knepley     k = k/Nc + 1;
340889eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
34093194fc30SMatthew G. Knepley   }
34103194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
34113194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
341289eabcffSMatthew G. Knepley     switch (dim) {
341389eabcffSMatthew G. Knepley     case 2:
34143194fc30SMatthew 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} */
34153194fc30SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
34163194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
34173194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
34183194fc30SMatthew G. Knepley       k = k/Nc + 1;
34193194fc30SMatthew G. Knepley       /* The SEM order is
34203194fc30SMatthew G. Knepley 
34213194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
342289eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
34233194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
34243194fc30SMatthew G. Knepley       */
34253194fc30SMatthew G. Knepley       {
34263194fc30SMatthew G. Knepley         const PetscInt of   = 0;
34273194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
34283194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
34293194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
34303194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
34313194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
34323194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
34333194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
34343194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
34353194fc30SMatthew G. Knepley         PetscInt       o;
34363194fc30SMatthew G. Knepley 
34373194fc30SMatthew G. Knepley         /* bottom */
34383194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
34393194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
34403194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
34413194fc30SMatthew G. Knepley         /* middle */
34423194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
34433194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
34443194fc30SMatthew 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;
34453194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
34463194fc30SMatthew G. Knepley         }
34473194fc30SMatthew G. Knepley         /* top */
34483194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
34493194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
34503194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
34513194fc30SMatthew G. Knepley         foffset = offset;
34523194fc30SMatthew G. Knepley       }
345389eabcffSMatthew G. Knepley       break;
345489eabcffSMatthew G. Knepley     case 3:
345589eabcffSMatthew G. Knepley       /* The original hex closure is
345689eabcffSMatthew G. Knepley 
345789eabcffSMatthew G. Knepley          {c,
345889eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
345989eabcffSMatthew 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,
346089eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
346189eabcffSMatthew G. Knepley       */
346289eabcffSMatthew G. Knepley       ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
346389eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
346489eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
346589eabcffSMatthew G. Knepley       k = k/Nc + 1;
346689eabcffSMatthew G. Knepley       /* The SEM order is
346789eabcffSMatthew G. Knepley          Bottom Slice
346889eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
346989eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
347089eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
347189eabcffSMatthew G. Knepley 
347289eabcffSMatthew G. Knepley          Middle Slice (j)
347389eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
347489eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
347589eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
347689eabcffSMatthew G. Knepley 
347789eabcffSMatthew G. Knepley          Top Slice
347889eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
347989eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
348089eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
348189eabcffSMatthew G. Knepley       */
348289eabcffSMatthew G. Knepley       {
348389eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
348489eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
348589eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
348689eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
348789eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
348889eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
348989eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
349089eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
349189eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
349289eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
349389eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
349489eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
349589eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
349689eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
349789eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
349889eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
349989eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
350089eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
350189eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
350289eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
350389eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
350489eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
350589eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
350689eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
350789eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
350889eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
350989eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
351089eabcffSMatthew G. Knepley         PetscInt       o, n;
351189eabcffSMatthew G. Knepley 
351289eabcffSMatthew G. Knepley         /* Bottom Slice */
351389eabcffSMatthew G. Knepley         /*   bottom */
351489eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
351589eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --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] = ovbrf*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] = (oebl+i)*Nc + c + foffset;
3520316b7f87SMax 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;}
352189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
35223194fc30SMatthew G. Knepley         }
352389eabcffSMatthew G. Knepley         /*   top */
352489eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
352589eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++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] = ovbrb*Nc + c + foffset;
352789eabcffSMatthew G. Knepley 
352889eabcffSMatthew G. Knepley         /* Middle Slice */
352989eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
353089eabcffSMatthew G. Knepley           /*   bottom */
353189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
353289eabcffSMatthew 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;
353389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*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] = (ofl+i*(k-1)+j)*Nc + c + foffset;
353789eabcffSMatthew 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;
353889eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
353989eabcffSMatthew G. Knepley           }
354089eabcffSMatthew G. Knepley           /*   top */
354189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
354289eabcffSMatthew 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;
354389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
354489eabcffSMatthew G. Knepley         }
354589eabcffSMatthew G. Knepley 
354689eabcffSMatthew G. Knepley         /* Top Slice */
354789eabcffSMatthew G. Knepley         /*   bottom */
354889eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
354989eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
355089eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
355189eabcffSMatthew G. Knepley         /*   middle */
355289eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
355389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
355489eabcffSMatthew 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;
355589eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
355689eabcffSMatthew G. Knepley         }
355789eabcffSMatthew G. Knepley         /*   top */
355889eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
355989eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
356089eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
356189eabcffSMatthew G. Knepley 
356289eabcffSMatthew G. Knepley         foffset = offset;
356389eabcffSMatthew G. Knepley       }
356489eabcffSMatthew G. Knepley       break;
356589eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
356689eabcffSMatthew G. Knepley     }
356789eabcffSMatthew G. Knepley   }
356889eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
35693194fc30SMatthew G. Knepley   /* Check permutation */
35703194fc30SMatthew G. Knepley   {
35713194fc30SMatthew G. Knepley     PetscInt *check;
35723194fc30SMatthew G. Knepley 
35733194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
35743194fc30SMatthew 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]);}
35753194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
35763194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
35773194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
35783194fc30SMatthew G. Knepley   }
35791fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
35803194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
35813194fc30SMatthew G. Knepley }
35823194fc30SMatthew G. Knepley 
35833194fc30SMatthew G. Knepley #undef __FUNCT__
3584e071409bSToby Isaac #define __FUNCT__ "DMPlexGetPointDualSpaceFEM"
3585e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
3586e071409bSToby Isaac {
3587e071409bSToby Isaac   PetscDS        prob;
3588e071409bSToby Isaac   PetscInt       depth, Nf, h;
3589e071409bSToby Isaac   DMLabel        label;
3590e071409bSToby Isaac   PetscErrorCode ierr;
3591e071409bSToby Isaac 
3592e071409bSToby Isaac   PetscFunctionBeginHot;
3593e071409bSToby Isaac   prob    = dm->prob;
3594e071409bSToby Isaac   Nf      = prob->Nf;
3595e071409bSToby Isaac   label   = dm->depthLabel;
3596e071409bSToby Isaac   *dspace = NULL;
3597e071409bSToby Isaac   if (field < Nf) {
3598e071409bSToby Isaac     PetscObject disc = prob->disc[field];
3599e071409bSToby Isaac 
3600e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
3601e071409bSToby Isaac       PetscDualSpace dsp;
3602e071409bSToby Isaac 
3603e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
3604e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
3605e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
3606e071409bSToby Isaac       h    = depth - 1 - h;
3607e071409bSToby Isaac       if (h) {
3608e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
3609e071409bSToby Isaac       } else {
3610e071409bSToby Isaac         *dspace = dsp;
3611e071409bSToby Isaac       }
3612e071409bSToby Isaac     }
3613e071409bSToby Isaac   }
3614e071409bSToby Isaac   PetscFunctionReturn(0);
3615e071409bSToby Isaac }
3616e071409bSToby Isaac 
3617e071409bSToby Isaac 
3618e071409bSToby Isaac #undef __FUNCT__
36191a271a75SMatthew G. Knepley #define __FUNCT__ "DMPlexVecGetClosure_Depth1_Static"
36201a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3621a6dfd86eSKarl Rupp {
3622552f7358SJed Brown   PetscScalar    *array, *vArray;
3623d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
36241a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
3625552f7358SJed Brown   PetscErrorCode  ierr;
3626552f7358SJed Brown 
36271b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
36282a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
36295a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
36305a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
36315a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
36323f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
36339df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
36349df71ca4SMatthew G. Knepley       PetscInt dof;
3635d9917b9dSMatthew G. Knepley 
36369df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
36379df71ca4SMatthew G. Knepley       size += dof;
36389df71ca4SMatthew G. Knepley     }
36399df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
36409df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
36412a3aaacfSMatthew G. Knepley       PetscInt       dof;
36425a1bb5cfSMatthew G. Knepley 
36435a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
36442a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
36455a1bb5cfSMatthew G. Knepley       size += dof;
36465a1bb5cfSMatthew G. Knepley     }
36473f7cbbe7SMatthew G. Knepley     if (!values) {
36483f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
36493f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
36503f7cbbe7SMatthew G. Knepley     }
36515a1bb5cfSMatthew G. Knepley     ierr = DMGetWorkArray(dm, size, PETSC_SCALAR, &array);CHKERRQ(ierr);
3652982e9ed1SMatthew G. Knepley   } else {
3653982e9ed1SMatthew G. Knepley     array = *values;
3654982e9ed1SMatthew G. Knepley   }
36559df71ca4SMatthew G. Knepley   size = 0;
36565a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
36579df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
36589df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
36599df71ca4SMatthew G. Knepley     PetscScalar *varr;
3660d9917b9dSMatthew G. Knepley 
36619df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
36629df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
36639df71ca4SMatthew G. Knepley     varr = &vArray[off];
36641a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
36651a271a75SMatthew G. Knepley       array[offset] = varr[d];
36669df71ca4SMatthew G. Knepley     }
36679df71ca4SMatthew G. Knepley     size += dof;
36689df71ca4SMatthew G. Knepley   }
36699df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
36709df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
36719df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
36725a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
36735a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
36745a1bb5cfSMatthew G. Knepley 
367552ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
36765a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
36775a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
36785a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
36795a1bb5cfSMatthew G. Knepley     if (o >= 0) {
36801a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
36811a271a75SMatthew G. Knepley         array[offset] = varr[d];
36825a1bb5cfSMatthew G. Knepley       }
36835a1bb5cfSMatthew G. Knepley     } else {
36841a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
36851a271a75SMatthew G. Knepley         array[offset] = varr[d];
36865a1bb5cfSMatthew G. Knepley       }
36875a1bb5cfSMatthew G. Knepley     }
36889df71ca4SMatthew G. Knepley     size += dof;
36895a1bb5cfSMatthew G. Knepley   }
36905a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
36919df71ca4SMatthew G. Knepley   if (!*values) {
36925a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
36935a1bb5cfSMatthew G. Knepley     *values = array;
36949df71ca4SMatthew G. Knepley   } else {
36958ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
36968c312ff3SMatthew G. Knepley     *csize = size;
36979df71ca4SMatthew G. Knepley   }
36985a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
36995a1bb5cfSMatthew G. Knepley }
3700d9917b9dSMatthew G. Knepley 
3701d9917b9dSMatthew G. Knepley #undef __FUNCT__
3702923c78e0SToby Isaac #define __FUNCT__ "DMPlexGetCompressedClosure"
3703923c78e0SToby Isaac static PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3704923c78e0SToby Isaac {
3705923c78e0SToby Isaac   const PetscInt *cla;
3706923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
3707923c78e0SToby Isaac   PetscErrorCode ierr;
3708923c78e0SToby Isaac 
3709923c78e0SToby Isaac   PetscFunctionBeginHot;
3710923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
3711923c78e0SToby Isaac   if (!*clPoints) {
3712923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
3713923c78e0SToby Isaac 
3714923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3715923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
3716923c78e0SToby Isaac     /* Compress out points not in the section */
3717923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
3718923c78e0SToby Isaac       PetscInt r = pts[2*p];
3719923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
3720923c78e0SToby Isaac         pts[q*2]   = r;
3721923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
3722923c78e0SToby Isaac         ++q;
3723923c78e0SToby Isaac       }
3724923c78e0SToby Isaac     }
3725923c78e0SToby Isaac     np = q;
3726923c78e0SToby Isaac     cla = NULL;
3727923c78e0SToby Isaac   } else {
3728923c78e0SToby Isaac     PetscInt dof, off;
3729923c78e0SToby Isaac 
3730923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
3731923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
3732923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
3733923c78e0SToby Isaac     np   = dof/2;
3734923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
3735923c78e0SToby Isaac   }
3736923c78e0SToby Isaac   *numPoints = np;
3737923c78e0SToby Isaac   *points    = pts;
3738923c78e0SToby Isaac   *clp       = cla;
3739923c78e0SToby Isaac 
3740923c78e0SToby Isaac   PetscFunctionReturn(0);
3741923c78e0SToby Isaac }
3742923c78e0SToby Isaac 
3743923c78e0SToby Isaac #undef __FUNCT__
3744923c78e0SToby Isaac #define __FUNCT__ "DMPlexRestoreCompressedClosure"
3745923c78e0SToby Isaac static PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3746923c78e0SToby Isaac {
3747923c78e0SToby Isaac   PetscErrorCode ierr;
3748923c78e0SToby Isaac 
3749923c78e0SToby Isaac   PetscFunctionBeginHot;
3750923c78e0SToby Isaac   if (!*clPoints) {
3751923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
3752923c78e0SToby Isaac   } else {
3753923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
3754923c78e0SToby Isaac   }
3755923c78e0SToby Isaac   *numPoints = 0;
3756923c78e0SToby Isaac   *points    = NULL;
3757923c78e0SToby Isaac   *clSec     = NULL;
3758923c78e0SToby Isaac   *clPoints  = NULL;
3759923c78e0SToby Isaac   *clp       = NULL;
3760923c78e0SToby Isaac   PetscFunctionReturn(0);
3761923c78e0SToby Isaac }
3762923c78e0SToby Isaac 
3763923c78e0SToby Isaac #undef __FUNCT__
37641a271a75SMatthew G. Knepley #define __FUNCT__ "DMPlexVecGetClosure_Static"
376597e99dd9SToby 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[])
37661a271a75SMatthew G. Knepley {
37671a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
376897e99dd9SToby Isaac   const PetscInt    **perms = NULL;
376997e99dd9SToby Isaac   const PetscScalar **flips = NULL;
37701a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
37711a271a75SMatthew G. Knepley 
37721a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3773fe02ba77SJed Brown   *size = 0;
377497e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
377597e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
377697e99dd9SToby Isaac     const PetscInt    point = points[2*p];
377797e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
377897e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
37791a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
37801a271a75SMatthew G. Knepley     const PetscScalar *varr;
37811a271a75SMatthew G. Knepley 
37821a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
37831a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
37841a271a75SMatthew G. Knepley     varr = &vArray[off];
378597e99dd9SToby Isaac     if (clperm) {
378697e99dd9SToby Isaac       if (perm) {
378797e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
37881a271a75SMatthew G. Knepley       } else {
378997e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
379097e99dd9SToby Isaac       }
379197e99dd9SToby Isaac       if (flip) {
379297e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
379397e99dd9SToby Isaac       }
379497e99dd9SToby Isaac     } else {
379597e99dd9SToby Isaac       if (perm) {
379697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
379797e99dd9SToby Isaac       } else {
379897e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
379997e99dd9SToby Isaac       }
380097e99dd9SToby Isaac       if (flip) {
380197e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
38021a271a75SMatthew G. Knepley       }
38031a271a75SMatthew G. Knepley     }
380497e99dd9SToby Isaac     offset += dof;
380597e99dd9SToby Isaac   }
380697e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
38071a271a75SMatthew G. Knepley   *size = offset;
38081a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
38091a271a75SMatthew G. Knepley }
38101a271a75SMatthew G. Knepley 
38111a271a75SMatthew G. Knepley #undef __FUNCT__
38121a271a75SMatthew G. Knepley #define __FUNCT__ "DMPlexVecGetClosure_Fields_Static"
381397e99dd9SToby 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[])
38141a271a75SMatthew G. Knepley {
38151a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
38161a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
38171a271a75SMatthew G. Knepley 
38181a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3819fe02ba77SJed Brown   *size = 0;
38201a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
382197e99dd9SToby Isaac     PetscInt          p;
382297e99dd9SToby Isaac     const PetscInt    **perms = NULL;
382397e99dd9SToby Isaac     const PetscScalar **flips = NULL;
38241a271a75SMatthew G. Knepley 
382597e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
382697e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
382797e99dd9SToby Isaac       const PetscInt    point = points[2*p];
382897e99dd9SToby Isaac       PetscInt          fdof, foff, b;
38291a271a75SMatthew G. Knepley       const PetscScalar *varr;
383097e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
383197e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
38321a271a75SMatthew G. Knepley 
38331a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
38341a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
38351a271a75SMatthew G. Knepley       varr = &vArray[foff];
383697e99dd9SToby Isaac       if (clperm) {
383797e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
383897e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
383997e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
38401a271a75SMatthew G. Knepley       } else {
384197e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
384297e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
384397e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
38441a271a75SMatthew G. Knepley       }
384597e99dd9SToby Isaac       offset += fdof;
38461a271a75SMatthew G. Knepley     }
384797e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
38481a271a75SMatthew G. Knepley   }
38491a271a75SMatthew G. Knepley   *size = offset;
38501a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
38511a271a75SMatthew G. Knepley }
38521a271a75SMatthew G. Knepley 
38531a271a75SMatthew G. Knepley #undef __FUNCT__
3854552f7358SJed Brown #define __FUNCT__ "DMPlexVecGetClosure"
3855552f7358SJed Brown /*@C
3856552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
3857552f7358SJed Brown 
3858552f7358SJed Brown   Not collective
3859552f7358SJed Brown 
3860552f7358SJed Brown   Input Parameters:
3861552f7358SJed Brown + dm - The DM
3862552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
3863552f7358SJed Brown . v - The local vector
3864552f7358SJed Brown - point - The sieve point in the DM
3865552f7358SJed Brown 
3866552f7358SJed Brown   Output Parameters:
3867552f7358SJed Brown + csize - The number of values in the closure, or NULL
3868552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
3869552f7358SJed Brown 
3870552f7358SJed Brown   Fortran Notes:
3871552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
3872552f7358SJed Brown   include petsc.h90 in your code.
3873552f7358SJed Brown 
3874552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
3875552f7358SJed Brown 
3876552f7358SJed Brown   Level: intermediate
3877552f7358SJed Brown 
3878552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
3879552f7358SJed Brown @*/
3880552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3881552f7358SJed Brown {
3882552f7358SJed Brown   PetscSection       clSection;
3883d9917b9dSMatthew G. Knepley   IS                 clPoints;
38848a84db2dSMatthew G. Knepley   PetscScalar       *array;
38858a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
3886552f7358SJed Brown   PetscInt          *points = NULL;
38878f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
38881a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
3889552f7358SJed Brown   PetscErrorCode     ierr;
3890552f7358SJed Brown 
3891d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
3892552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3893552f7358SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
38941a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
38951a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
3896552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3897552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3898552f7358SJed Brown   if (depth == 1 && numFields < 2) {
38991a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
3900552f7358SJed Brown     PetscFunctionReturn(0);
3901552f7358SJed Brown   }
39021a271a75SMatthew G. Knepley   /* Get points */
3903923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
39041fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
39051a271a75SMatthew G. Knepley   /* Get array */
3906bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
39071a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
3908552f7358SJed Brown 
39091a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
3910552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
39111a271a75SMatthew G. Knepley       asize += dof;
3912552f7358SJed Brown     }
3913bd6c0d32SMatthew G. Knepley     if (!values) {
3914923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
39151a271a75SMatthew G. Knepley       if (csize) *csize = asize;
3916bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
3917bd6c0d32SMatthew G. Knepley     }
39181a271a75SMatthew G. Knepley     ierr = DMGetWorkArray(dm, asize, PETSC_SCALAR, &array);CHKERRQ(ierr);
3919d0f6b257SMatthew G. Knepley   } else {
3920d0f6b257SMatthew G. Knepley     array = *values;
3921d0f6b257SMatthew G. Knepley   }
39228a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
39231a271a75SMatthew G. Knepley   /* Get values */
392497e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
392597e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
39261a271a75SMatthew G. Knepley   /* Cleanup points */
3927923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
39281a271a75SMatthew G. Knepley   /* Cleanup array */
39298a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
3930d0f6b257SMatthew G. Knepley   if (!*values) {
3931552f7358SJed Brown     if (csize) *csize = size;
3932552f7358SJed Brown     *values = array;
3933d0f6b257SMatthew G. Knepley   } else {
3934f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
3935d0f6b257SMatthew G. Knepley     *csize = size;
3936d0f6b257SMatthew G. Knepley   }
3937552f7358SJed Brown   PetscFunctionReturn(0);
3938552f7358SJed Brown }
3939552f7358SJed Brown 
3940552f7358SJed Brown #undef __FUNCT__
3941552f7358SJed Brown #define __FUNCT__ "DMPlexVecRestoreClosure"
3942552f7358SJed Brown /*@C
3943552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
3944552f7358SJed Brown 
3945552f7358SJed Brown   Not collective
3946552f7358SJed Brown 
3947552f7358SJed Brown   Input Parameters:
3948552f7358SJed Brown + dm - The DM
39490298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
3950552f7358SJed Brown . v - The local vector
3951552f7358SJed Brown . point - The sieve point in the DM
39520298fd71SBarry Smith . csize - The number of values in the closure, or NULL
3953552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
3954552f7358SJed Brown 
39553813dfbdSMatthew G Knepley   Fortran Notes:
39563813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
39573813dfbdSMatthew G Knepley   include petsc.h90 in your code.
39583813dfbdSMatthew G Knepley 
39593813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
39603813dfbdSMatthew G Knepley 
3961552f7358SJed Brown   Level: intermediate
3962552f7358SJed Brown 
3963552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
3964552f7358SJed Brown @*/
39657c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3966a6dfd86eSKarl Rupp {
3967552f7358SJed Brown   PetscInt       size = 0;
3968552f7358SJed Brown   PetscErrorCode ierr;
3969552f7358SJed Brown 
3970552f7358SJed Brown   PetscFunctionBegin;
3971552f7358SJed Brown   /* Should work without recalculating size */
3972552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, size, PETSC_SCALAR, (void*) values);CHKERRQ(ierr);
3973552f7358SJed Brown   PetscFunctionReturn(0);
3974552f7358SJed Brown }
3975552f7358SJed Brown 
3976552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
3977552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
3978552f7358SJed Brown 
3979552f7358SJed Brown #undef __FUNCT__
3980552f7358SJed Brown #define __FUNCT__ "updatePoint_private"
398197e99dd9SToby 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[])
3982552f7358SJed Brown {
3983552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
3984552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
3985552f7358SJed Brown   PetscScalar    *a;
3986552f7358SJed Brown   PetscInt        off, cind = 0, k;
3987552f7358SJed Brown   PetscErrorCode  ierr;
3988552f7358SJed Brown 
3989552f7358SJed Brown   PetscFunctionBegin;
3990552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
3991552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
3992552f7358SJed Brown   a    = &array[off];
3993552f7358SJed Brown   if (!cdof || setBC) {
399497e99dd9SToby Isaac     if (clperm) {
399597e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
399697e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
3997552f7358SJed Brown     } else {
399897e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
399997e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4000552f7358SJed Brown     }
4001552f7358SJed Brown   } else {
4002552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
400397e99dd9SToby Isaac     if (clperm) {
400497e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
4005552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
400697e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4007552f7358SJed Brown         }
4008552f7358SJed Brown       } else {
4009552f7358SJed Brown         for (k = 0; k < dof; ++k) {
4010552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
401197e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
401297e99dd9SToby Isaac         }
401397e99dd9SToby Isaac       }
401497e99dd9SToby Isaac     } else {
401597e99dd9SToby Isaac       if (perm) {
401697e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
401797e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
401897e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
401997e99dd9SToby Isaac         }
402097e99dd9SToby Isaac       } else {
402197e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
402297e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
402397e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
402497e99dd9SToby Isaac         }
4025552f7358SJed Brown       }
4026552f7358SJed Brown     }
4027552f7358SJed Brown   }
4028552f7358SJed Brown   PetscFunctionReturn(0);
4029552f7358SJed Brown }
4030552f7358SJed Brown 
4031552f7358SJed Brown #undef __FUNCT__
4032a5e93ea8SMatthew G. Knepley #define __FUNCT__ "updatePointBC_private"
403397e99dd9SToby 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[])
4034a5e93ea8SMatthew G. Knepley {
4035a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
4036a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4037a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
4038a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
4039a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4040a5e93ea8SMatthew G. Knepley 
4041a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4042a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4043a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4044a5e93ea8SMatthew G. Knepley   a    = &array[off];
4045a5e93ea8SMatthew G. Knepley   if (cdof) {
4046a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
404797e99dd9SToby Isaac     if (clperm) {
404897e99dd9SToby Isaac       if (perm) {
4049a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4050a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
405197e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
405297e99dd9SToby Isaac             cind++;
4053a5e93ea8SMatthew G. Knepley           }
4054a5e93ea8SMatthew G. Knepley         }
4055a5e93ea8SMatthew G. Knepley       } else {
4056a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4057a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
405897e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
405997e99dd9SToby Isaac             cind++;
406097e99dd9SToby Isaac           }
406197e99dd9SToby Isaac         }
406297e99dd9SToby Isaac       }
406397e99dd9SToby Isaac     } else {
406497e99dd9SToby Isaac       if (perm) {
406597e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
406697e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
406797e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
406897e99dd9SToby Isaac             cind++;
406997e99dd9SToby Isaac           }
407097e99dd9SToby Isaac         }
407197e99dd9SToby Isaac       } else {
407297e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
407397e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
407497e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
407597e99dd9SToby Isaac             cind++;
407697e99dd9SToby Isaac           }
4077a5e93ea8SMatthew G. Knepley         }
4078a5e93ea8SMatthew G. Knepley       }
4079a5e93ea8SMatthew G. Knepley     }
4080a5e93ea8SMatthew G. Knepley   }
4081a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4082a5e93ea8SMatthew G. Knepley }
4083a5e93ea8SMatthew G. Knepley 
4084a5e93ea8SMatthew G. Knepley #undef __FUNCT__
4085552f7358SJed Brown #define __FUNCT__ "updatePointFields_private"
408697e99dd9SToby 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[])
4087a6dfd86eSKarl Rupp {
4088552f7358SJed Brown   PetscScalar    *a;
40891a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
40901a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
409197e99dd9SToby Isaac   PetscInt        cind = 0, b;
4092552f7358SJed Brown   PetscErrorCode  ierr;
4093552f7358SJed Brown 
4094552f7358SJed Brown   PetscFunctionBegin;
4095552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4096552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
40971a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
40981a271a75SMatthew G. Knepley   a    = &array[foff];
4099552f7358SJed Brown   if (!fcdof || setBC) {
410097e99dd9SToby Isaac     if (clperm) {
410197e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
410297e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4103552f7358SJed Brown     } else {
410497e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
410597e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4106552f7358SJed Brown     }
4107552f7358SJed Brown   } else {
4108552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
410997e99dd9SToby Isaac     if (clperm) {
411097e99dd9SToby Isaac       if (perm) {
411197e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
411297e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
411397e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4114552f7358SJed Brown         }
4115552f7358SJed Brown       } else {
411697e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
411797e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
411897e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
411997e99dd9SToby Isaac         }
412097e99dd9SToby Isaac       }
412197e99dd9SToby Isaac     } else {
412297e99dd9SToby Isaac       if (perm) {
412397e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
412497e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
412597e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
412697e99dd9SToby Isaac         }
412797e99dd9SToby Isaac       } else {
412897e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
412997e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
413097e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4131552f7358SJed Brown         }
4132552f7358SJed Brown       }
4133552f7358SJed Brown     }
4134552f7358SJed Brown   }
41351a271a75SMatthew G. Knepley   *offset += fdof;
4136552f7358SJed Brown   PetscFunctionReturn(0);
4137552f7358SJed Brown }
4138552f7358SJed Brown 
4139552f7358SJed Brown #undef __FUNCT__
4140a5e93ea8SMatthew G. Knepley #define __FUNCT__ "updatePointFieldsBC_private"
414197e99dd9SToby 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[])
4142a5e93ea8SMatthew G. Knepley {
4143a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
41441a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
41451a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
414697e99dd9SToby Isaac   PetscInt        cind = 0, b;
4147a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4148a5e93ea8SMatthew G. Knepley 
4149a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4150a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4151a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
41521a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
41531a271a75SMatthew G. Knepley   a    = &array[foff];
4154a5e93ea8SMatthew G. Knepley   if (fcdof) {
4155a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
415697e99dd9SToby Isaac     if (clperm) {
415797e99dd9SToby Isaac       if (perm) {
415897e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
415997e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
416097e99dd9SToby Isaac             fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4161a5e93ea8SMatthew G. Knepley             ++cind;
4162a5e93ea8SMatthew G. Knepley           }
4163a5e93ea8SMatthew G. Knepley         }
4164a5e93ea8SMatthew G. Knepley       } else {
416597e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
416697e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
416797e99dd9SToby Isaac             fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
416897e99dd9SToby Isaac             ++cind;
416997e99dd9SToby Isaac           }
417097e99dd9SToby Isaac         }
417197e99dd9SToby Isaac       }
417297e99dd9SToby Isaac     } else {
417397e99dd9SToby Isaac       if (perm) {
417497e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
417597e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
417697e99dd9SToby Isaac             fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
417797e99dd9SToby Isaac             ++cind;
417897e99dd9SToby Isaac           }
417997e99dd9SToby Isaac         }
418097e99dd9SToby Isaac       } else {
418197e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
418297e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
418397e99dd9SToby Isaac             fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4184a5e93ea8SMatthew G. Knepley             ++cind;
4185a5e93ea8SMatthew G. Knepley           }
4186a5e93ea8SMatthew G. Knepley         }
4187a5e93ea8SMatthew G. Knepley       }
4188a5e93ea8SMatthew G. Knepley     }
4189a5e93ea8SMatthew G. Knepley   }
41901a271a75SMatthew G. Knepley   *offset += fdof;
4191a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4192a5e93ea8SMatthew G. Knepley }
4193a5e93ea8SMatthew G. Knepley 
4194a5e93ea8SMatthew G. Knepley #undef __FUNCT__
41958f3be42fSMatthew G. Knepley #define __FUNCT__ "DMPlexVecSetClosure_Depth1_Static"
41968f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4197a6dfd86eSKarl Rupp {
4198552f7358SJed Brown   PetscScalar    *array;
41991b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
42001b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4201552f7358SJed Brown   PetscErrorCode  ierr;
4202552f7358SJed Brown 
42031b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
4204b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4205b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
4206b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4207b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
4208b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4209b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4210b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
4211b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
4212b6ebb6e6SMatthew G. Knepley 
4213b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4214b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
4215b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
4216b6ebb6e6SMatthew G. Knepley     {
4217b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4218b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
4219b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
4220b6ebb6e6SMatthew G. Knepley 
4221b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
4222b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
4223b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
4224b6ebb6e6SMatthew G. Knepley       if (!cdof) {
4225b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4226b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4227b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4228b6ebb6e6SMatthew G. Knepley           }
4229b6ebb6e6SMatthew G. Knepley         } else {
4230b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4231b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4232b6ebb6e6SMatthew G. Knepley           }
4233b6ebb6e6SMatthew G. Knepley         }
4234b6ebb6e6SMatthew G. Knepley       } else {
4235b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
4236b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4237b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4238b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4239b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4240b6ebb6e6SMatthew G. Knepley           }
4241b6ebb6e6SMatthew G. Knepley         } else {
4242b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4243b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4244b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4245b6ebb6e6SMatthew G. Knepley           }
4246b6ebb6e6SMatthew G. Knepley         }
4247b6ebb6e6SMatthew G. Knepley       }
4248b6ebb6e6SMatthew G. Knepley     }
4249b6ebb6e6SMatthew G. Knepley   }
4250b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4251b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4252b6ebb6e6SMatthew G. Knepley }
42531b406b76SMatthew G. Knepley 
42541b406b76SMatthew G. Knepley #undef __FUNCT__
42551b406b76SMatthew G. Knepley #define __FUNCT__ "DMPlexVecSetClosure"
42561b406b76SMatthew G. Knepley /*@C
42571b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
42581b406b76SMatthew G. Knepley 
42591b406b76SMatthew G. Knepley   Not collective
42601b406b76SMatthew G. Knepley 
42611b406b76SMatthew G. Knepley   Input Parameters:
42621b406b76SMatthew G. Knepley + dm - The DM
42631b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
42641b406b76SMatthew G. Knepley . v - The local vector
42651b406b76SMatthew G. Knepley . point - The sieve point in the DM
42661b406b76SMatthew G. Knepley . values - The array of values
42671b406b76SMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
42681b406b76SMatthew G. Knepley 
42691b406b76SMatthew G. Knepley   Fortran Notes:
42701b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
42711b406b76SMatthew G. Knepley 
42721b406b76SMatthew G. Knepley   Level: intermediate
42731b406b76SMatthew G. Knepley 
42741b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
42751b406b76SMatthew G. Knepley @*/
42761b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
42771b406b76SMatthew G. Knepley {
42781b406b76SMatthew G. Knepley   PetscSection    clSection;
42791b406b76SMatthew G. Knepley   IS              clPoints;
42801b406b76SMatthew G. Knepley   PetscScalar    *array;
42811b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
428297e99dd9SToby Isaac   const PetscInt *clp, *clperm;
42831a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
42841b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
42851b406b76SMatthew G. Knepley 
42861a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
42871b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42881b406b76SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
42891a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
42901a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
42911b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
42921b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
42931b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
42948f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
42951b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
42961b406b76SMatthew G. Knepley   }
42971a271a75SMatthew G. Knepley   /* Get points */
429897e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4299923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
43001a271a75SMatthew G. Knepley   /* Get array */
4301552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
43021a271a75SMatthew G. Knepley   /* Get values */
4303ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
430497e99dd9SToby Isaac     PetscInt offset = 0, f;
4305552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
430697e99dd9SToby Isaac       const PetscInt    **perms = NULL;
430797e99dd9SToby Isaac       const PetscScalar **flips = NULL;
430897e99dd9SToby Isaac 
430997e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4310552f7358SJed Brown       switch (mode) {
4311552f7358SJed Brown       case INSERT_VALUES:
431297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
431397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
431497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
431597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
431697e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4317552f7358SJed Brown         } break;
4318552f7358SJed Brown       case INSERT_ALL_VALUES:
431997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
432097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
432197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
432297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
432397e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4324552f7358SJed Brown         } break;
4325a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
432697e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
432797e99dd9SToby Isaac           const PetscInt    point = points[2*p];
432897e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
432997e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
433097e99dd9SToby Isaac           updatePointFieldsBC_private(section, point, perm, flip, f, insert, clperm, values, &offset, array);
4331a5e93ea8SMatthew G. Knepley         } break;
4332552f7358SJed Brown       case ADD_VALUES:
433397e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
433497e99dd9SToby Isaac           const PetscInt    point = points[2*p];
433597e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
433697e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
433797e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4338552f7358SJed Brown         } break;
4339552f7358SJed Brown       case ADD_ALL_VALUES:
434097e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
434197e99dd9SToby Isaac           const PetscInt    point = points[2*p];
434297e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
434397e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
434497e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4345552f7358SJed Brown         } break;
4346304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
434797e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
434897e99dd9SToby Isaac           const PetscInt    point = points[2*p];
434997e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
435097e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
435197e99dd9SToby Isaac           updatePointFieldsBC_private(section, point, perm, flip, f, add, clperm, values, &offset, array);
4352304ab55fSMatthew G. Knepley         } break;
4353552f7358SJed Brown       default:
4354f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4355552f7358SJed Brown       }
435697e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
43571a271a75SMatthew G. Knepley     }
4358552f7358SJed Brown   } else {
43591a271a75SMatthew G. Knepley     PetscInt dof, off;
436097e99dd9SToby Isaac     const PetscInt    **perms = NULL;
436197e99dd9SToby Isaac     const PetscScalar **flips = NULL;
43621a271a75SMatthew G. Knepley 
436397e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4364552f7358SJed Brown     switch (mode) {
4365552f7358SJed Brown     case INSERT_VALUES:
436697e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
436797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
436897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
436997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
437097e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
437197e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4372552f7358SJed Brown       } break;
4373552f7358SJed Brown     case INSERT_ALL_VALUES:
437497e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
437597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
437697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
437797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
437897e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
437997e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4380552f7358SJed Brown       } break;
4381a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
438297e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
438397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
438497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
438597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
438697e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
438797e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4388a5e93ea8SMatthew G. Knepley       } break;
4389552f7358SJed Brown     case ADD_VALUES:
439097e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
439197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
439297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
439397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
439497e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
439597e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4396552f7358SJed Brown       } break;
4397552f7358SJed Brown     case ADD_ALL_VALUES:
439897e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
439997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
440097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
440197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
440297e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
440397e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4404552f7358SJed Brown       } break;
4405304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
440697e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
440797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
440897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
440997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
441097e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
441197e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
4412304ab55fSMatthew G. Knepley       } break;
4413552f7358SJed Brown     default:
4414f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4415552f7358SJed Brown     }
441697e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4417552f7358SJed Brown   }
44181a271a75SMatthew G. Knepley   /* Cleanup points */
4419923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
44201a271a75SMatthew G. Knepley   /* Cleanup array */
4421552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4422552f7358SJed Brown   PetscFunctionReturn(0);
4423552f7358SJed Brown }
4424552f7358SJed Brown 
4425552f7358SJed Brown #undef __FUNCT__
4426e07394fbSMatthew G. Knepley #define __FUNCT__ "DMPlexVecSetFieldClosure_Internal"
4427e07394fbSMatthew G. Knepley PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, const PetscScalar values[], InsertMode mode)
4428e07394fbSMatthew G. Knepley {
4429e07394fbSMatthew G. Knepley   PetscSection      clSection;
4430e07394fbSMatthew G. Knepley   IS                clPoints;
4431e07394fbSMatthew G. Knepley   PetscScalar       *array;
4432e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
4433b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
4434e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
443597e99dd9SToby Isaac   PetscInt          offset = 0, f;
4436e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
4437e07394fbSMatthew G. Knepley 
4438e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
4439e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4440e07394fbSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
4441e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4442e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4443e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4444e07394fbSMatthew G. Knepley   /* Get points */
4445b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4446923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4447e07394fbSMatthew G. Knepley   /* Get array */
4448e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4449e07394fbSMatthew G. Knepley   /* Get values */
4450e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
445197e99dd9SToby Isaac     const PetscInt    **perms = NULL;
445297e99dd9SToby Isaac     const PetscScalar **flips = NULL;
445397e99dd9SToby Isaac 
4454e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
4455e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4456e07394fbSMatthew G. Knepley         PetscInt fdof;
4457e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
4458e07394fbSMatthew G. Knepley         offset += fdof;
4459e07394fbSMatthew G. Knepley       }
4460e07394fbSMatthew G. Knepley       continue;
4461e07394fbSMatthew G. Knepley     }
446297e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4463e07394fbSMatthew G. Knepley     switch (mode) {
4464e07394fbSMatthew G. Knepley     case INSERT_VALUES:
446597e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
446697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
446797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
446897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4469b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4470e07394fbSMatthew G. Knepley       } break;
4471e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
447297e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
447397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
447497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
447597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4476b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4477e07394fbSMatthew G. Knepley         } break;
4478e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
447997e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
448097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
448197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
448297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4483b04c866fSMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, insert, clperm, values, &offset, array);
4484e07394fbSMatthew G. Knepley       } break;
4485e07394fbSMatthew G. Knepley     case ADD_VALUES:
448697e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
448797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
448897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
448997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4490b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4491e07394fbSMatthew G. Knepley       } break;
4492e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
449397e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
449497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
449597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
449697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4497b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4498e07394fbSMatthew G. Knepley       } break;
4499e07394fbSMatthew G. Knepley     default:
4500f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4501e07394fbSMatthew G. Knepley     }
450297e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4503e07394fbSMatthew G. Knepley   }
4504e07394fbSMatthew G. Knepley   /* Cleanup points */
4505923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4506e07394fbSMatthew G. Knepley   /* Cleanup array */
4507e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4508e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
4509e07394fbSMatthew G. Knepley }
4510e07394fbSMatthew G. Knepley 
4511e07394fbSMatthew G. Knepley #undef __FUNCT__
4512552f7358SJed Brown #define __FUNCT__ "DMPlexPrintMatSetValues"
4513b0ecff45SMatthew G. Knepley PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
4514552f7358SJed Brown {
4515552f7358SJed Brown   PetscMPIInt    rank;
4516552f7358SJed Brown   PetscInt       i, j;
4517552f7358SJed Brown   PetscErrorCode ierr;
4518552f7358SJed Brown 
4519552f7358SJed Brown   PetscFunctionBegin;
452082f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
4521f347f43bSBarry Smith   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for sieve point %D\n", rank, point);CHKERRQ(ierr);
4522e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
4523e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
4524b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
4525b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
4526e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
4527b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
4528519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
45297eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
4530552f7358SJed Brown #else
4531b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
4532552f7358SJed Brown #endif
4533552f7358SJed Brown     }
453477a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
4535552f7358SJed Brown   }
4536552f7358SJed Brown   PetscFunctionReturn(0);
4537552f7358SJed Brown }
4538552f7358SJed Brown 
4539552f7358SJed Brown #undef __FUNCT__
4540415ce65aSMatthew G. Knepley #define __FUNCT__ "DMPlexGetIndicesPoint_Internal"
4541552f7358SJed Brown /* . off - The global offset of this point */
45424acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], PetscInt indices[])
4543a6dfd86eSKarl Rupp {
4544e6ccafaeSMatthew G Knepley   PetscInt        dof;    /* The number of unknowns on this point */
4545552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4546552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4547552f7358SJed Brown   PetscInt        cind = 0, k;
4548552f7358SJed Brown   PetscErrorCode  ierr;
4549552f7358SJed Brown 
4550552f7358SJed Brown   PetscFunctionBegin;
4551552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
4552552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4553552f7358SJed Brown   if (!cdof || setBC) {
45544acb8e1eSToby Isaac     if (perm) {
45554acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+perm[k]] = off + k;
4556552f7358SJed Brown     } else {
45574acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+k] = off + k;
4558552f7358SJed Brown     }
4559552f7358SJed Brown   } else {
4560552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
45614acb8e1eSToby Isaac     if (perm) {
45624acb8e1eSToby Isaac       for (k = 0; k < dof; ++k) {
45634acb8e1eSToby Isaac         if ((cind < cdof) && (k == cdofs[cind])) {
45644acb8e1eSToby Isaac           /* Insert check for returning constrained indices */
45654acb8e1eSToby Isaac           indices[*loff+perm[k]] = -(off+k+1);
45664acb8e1eSToby Isaac           ++cind;
45674acb8e1eSToby Isaac         } else {
45684acb8e1eSToby Isaac           indices[*loff+perm[k]] = off+k-cind;
45694acb8e1eSToby Isaac         }
45704acb8e1eSToby Isaac       }
45714acb8e1eSToby Isaac     } else {
4572552f7358SJed Brown       for (k = 0; k < dof; ++k) {
4573552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
4574552f7358SJed Brown           /* Insert check for returning constrained indices */
4575e6ccafaeSMatthew G Knepley           indices[*loff+k] = -(off+k+1);
4576552f7358SJed Brown           ++cind;
4577552f7358SJed Brown         } else {
4578e6ccafaeSMatthew G Knepley           indices[*loff+k] = off+k-cind;
4579552f7358SJed Brown         }
4580552f7358SJed Brown       }
4581552f7358SJed Brown     }
4582552f7358SJed Brown   }
4583e6ccafaeSMatthew G Knepley   *loff += dof;
4584552f7358SJed Brown   PetscFunctionReturn(0);
4585552f7358SJed Brown }
4586552f7358SJed Brown 
4587552f7358SJed Brown #undef __FUNCT__
4588415ce65aSMatthew G. Knepley #define __FUNCT__ "DMPlexGetIndicesPointFields_Internal"
4589552f7358SJed Brown /* . off - The global offset of this point */
45904acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, PetscInt indices[])
4591a6dfd86eSKarl Rupp {
4592552f7358SJed Brown   PetscInt       numFields, foff, f;
4593552f7358SJed Brown   PetscErrorCode ierr;
4594552f7358SJed Brown 
4595552f7358SJed Brown   PetscFunctionBegin;
4596552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4597552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
45984acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
4599552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
46004acb8e1eSToby Isaac     PetscInt        cind = 0, b;
46014acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
4602552f7358SJed Brown 
4603552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4604552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
4605552f7358SJed Brown     if (!cfdof || setBC) {
46064acb8e1eSToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {indices[foffs[f]+perm[b]] = off+foff+b;}}
46074acb8e1eSToby Isaac       else      {for (b = 0; b < fdof; b++) {indices[foffs[f]+     b ] = off+foff+b;}}
4608552f7358SJed Brown     } else {
4609552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
46104acb8e1eSToby Isaac       if (perm) {
46114acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
46124acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
46134acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = -(off+foff+b+1);
4614552f7358SJed Brown             ++cind;
4615552f7358SJed Brown           } else {
46164acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = off+foff+b-cind;
4617552f7358SJed Brown           }
4618552f7358SJed Brown         }
4619552f7358SJed Brown       } else {
46204acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
46214acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
46224acb8e1eSToby Isaac             indices[foffs[f]+b] = -(off+foff+b+1);
4623552f7358SJed Brown             ++cind;
4624552f7358SJed Brown           } else {
46254acb8e1eSToby Isaac             indices[foffs[f]+b] = off+foff+b-cind;
4626552f7358SJed Brown           }
4627552f7358SJed Brown         }
4628552f7358SJed Brown       }
4629552f7358SJed Brown     }
4630a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
4631552f7358SJed Brown     foffs[f] += fdof;
4632552f7358SJed Brown   }
4633552f7358SJed Brown   PetscFunctionReturn(0);
4634552f7358SJed Brown }
4635552f7358SJed Brown 
4636552f7358SJed Brown #undef __FUNCT__
4637f7c74593SToby Isaac #define __FUNCT__ "DMPlexAnchorsModifyMat"
46384acb8e1eSToby 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)
4639d3d1a6afSToby Isaac {
4640d3d1a6afSToby Isaac   Mat             cMat;
4641d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
4642d3d1a6afSToby Isaac   IS              aIS;
4643d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
4644d3d1a6afSToby Isaac   const PetscInt  *anchors;
4645e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
4646d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
4647d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
4648d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
4649d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
4650d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
4651d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
4652d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
46536ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
4654d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
4655d3d1a6afSToby Isaac   PetscErrorCode  ierr;
4656d3d1a6afSToby Isaac 
4657d3d1a6afSToby Isaac   PetscFunctionBegin;
4658d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4659d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4660d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4661d3d1a6afSToby Isaac 
4662a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
4663d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
4664d3d1a6afSToby Isaac   if (aSec) {
4665d3d1a6afSToby Isaac     ierr = PetscMemzero(newOffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
4666d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
4667d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
4668d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
4669d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
4670d3d1a6afSToby Isaac      * into the global matrix anyway) */
4671d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
4672d3d1a6afSToby Isaac       PetscInt b    = points[p];
46734b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4674d3d1a6afSToby Isaac 
46754b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
46764b2f2278SToby Isaac       if (!bSecDof) {
46774b2f2278SToby Isaac         continue;
46784b2f2278SToby Isaac       }
4679d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4680d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
4681d3d1a6afSToby Isaac       }
4682d3d1a6afSToby Isaac       if (bDof) {
4683d3d1a6afSToby Isaac         /* this point is constrained */
4684d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
4685d3d1a6afSToby Isaac         PetscInt bOff, q;
4686d3d1a6afSToby Isaac 
4687d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
4688d3d1a6afSToby Isaac         newNumPoints  += bDof;
4689d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
4690d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4691d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
4692d3d1a6afSToby Isaac           PetscInt aDof;
4693d3d1a6afSToby Isaac 
4694d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
4695d3d1a6afSToby Isaac           newNumIndices += aDof;
4696d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
4697d3d1a6afSToby Isaac             PetscInt fDof;
4698d3d1a6afSToby Isaac 
4699d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
4700d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
4701d3d1a6afSToby Isaac           }
4702d3d1a6afSToby Isaac         }
4703d3d1a6afSToby Isaac       }
4704d3d1a6afSToby Isaac       else {
4705d3d1a6afSToby Isaac         /* this point is not constrained */
4706d3d1a6afSToby Isaac         newNumPoints++;
47074b2f2278SToby Isaac         newNumIndices += bSecDof;
4708d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
4709d3d1a6afSToby Isaac           PetscInt fDof;
4710d3d1a6afSToby Isaac 
4711d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4712d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
4713d3d1a6afSToby Isaac         }
4714d3d1a6afSToby Isaac       }
4715d3d1a6afSToby Isaac     }
4716d3d1a6afSToby Isaac   }
4717d3d1a6afSToby Isaac   if (!anyConstrained) {
471872b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
471972b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
472072b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
472172b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
472272b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
4723d3d1a6afSToby Isaac     PetscFunctionReturn(0);
4724d3d1a6afSToby Isaac   }
4725d3d1a6afSToby Isaac 
47266ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
47276ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
47286ecaa68aSToby Isaac 
4729f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
4730d3d1a6afSToby Isaac 
47316ecaa68aSToby Isaac   if (!outPoints && !outValues) {
47326ecaa68aSToby Isaac     if (offsets) {
47336ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
47346ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
47356ecaa68aSToby Isaac       }
47366ecaa68aSToby Isaac     }
47376ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
47386ecaa68aSToby Isaac     PetscFunctionReturn(0);
47396ecaa68aSToby Isaac   }
47406ecaa68aSToby Isaac 
4741f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
4742d3d1a6afSToby Isaac 
4743f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
4744d3d1a6afSToby Isaac 
4745d3d1a6afSToby Isaac   /* workspaces */
4746d3d1a6afSToby Isaac   if (numFields) {
4747d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
4748d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
4749d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
4750d3d1a6afSToby Isaac     }
4751d3d1a6afSToby Isaac   }
4752d3d1a6afSToby Isaac   else {
4753d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
4754d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
4755d3d1a6afSToby Isaac   }
4756d3d1a6afSToby Isaac 
4757d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
4758d3d1a6afSToby Isaac   if (numFields) {
47594b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
47604b2f2278SToby Isaac 
4761d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4762d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
47634b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4764d3d1a6afSToby Isaac 
47654b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
47664b2f2278SToby Isaac       if (!bSecDof) {
47674b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
47684b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
47694b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
47704b2f2278SToby Isaac         }
47714b2f2278SToby Isaac         continue;
47724b2f2278SToby Isaac       }
4773d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4774d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4775d3d1a6afSToby Isaac       }
4776d3d1a6afSToby Isaac       if (bDof) {
4777d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4778d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
4779d3d1a6afSToby Isaac 
4780d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4781d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4782d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
4783d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
4784d3d1a6afSToby Isaac             PetscInt aFDof;
4785d3d1a6afSToby Isaac 
4786d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
4787d3d1a6afSToby Isaac             allFDof += aFDof;
4788d3d1a6afSToby Isaac           }
4789d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
4790d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
4791d3d1a6afSToby Isaac         }
4792d3d1a6afSToby Isaac       }
4793d3d1a6afSToby Isaac       else {
4794d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4795d3d1a6afSToby Isaac           PetscInt fDof;
4796d3d1a6afSToby Isaac 
4797d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4798d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
4799d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
4800d3d1a6afSToby Isaac         }
4801d3d1a6afSToby Isaac       }
4802d3d1a6afSToby Isaac     }
48034b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
48044b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
48054b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
4806d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
4807d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
4808d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
4809d3d1a6afSToby Isaac       }
481019f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
481119f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
4812d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
4813d3d1a6afSToby Isaac     }
4814d3d1a6afSToby Isaac   }
4815d3d1a6afSToby Isaac   else {
4816d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4817d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
48184b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4819d3d1a6afSToby Isaac 
48204b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
48214b2f2278SToby Isaac       if (!bSecDof) {
48224b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
48234b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
48244b2f2278SToby Isaac         continue;
48254b2f2278SToby Isaac       }
4826d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4827d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4828d3d1a6afSToby Isaac       }
4829d3d1a6afSToby Isaac       if (bDof) {
48304b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
4831d3d1a6afSToby Isaac 
4832d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4833d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4834d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
4835d3d1a6afSToby Isaac 
4836d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
4837d3d1a6afSToby Isaac           allDof += aDof;
4838d3d1a6afSToby Isaac         }
4839d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
48404b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
4841d3d1a6afSToby Isaac       }
4842d3d1a6afSToby Isaac       else {
48434b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
4844d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
4845d3d1a6afSToby Isaac       }
4846d3d1a6afSToby Isaac     }
4847d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
4848d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
4849d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4850d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
4851d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
4852d3d1a6afSToby Isaac     }
4853d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
4854d3d1a6afSToby Isaac   }
4855d3d1a6afSToby Isaac 
48566ecaa68aSToby Isaac   /* output arrays */
48576ecaa68aSToby Isaac   ierr = DMGetWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
48586ecaa68aSToby Isaac 
4859d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
4860d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
4861d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
4862d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
4863d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
4864d3d1a6afSToby Isaac   if (numFields) {
4865d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
4866d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4867d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
48684b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4869d3d1a6afSToby Isaac 
48704b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
48714b2f2278SToby Isaac       if (!bSecDof) {
48724b2f2278SToby Isaac         continue;
48734b2f2278SToby Isaac       }
4874d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4875d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4876d3d1a6afSToby Isaac       }
4877d3d1a6afSToby Isaac       if (bDof) {
4878d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
4879d3d1a6afSToby Isaac 
4880d3d1a6afSToby Isaac         fStart[0] = 0;
4881d3d1a6afSToby Isaac         fEnd[0]   = 0;
4882d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4883d3d1a6afSToby Isaac           PetscInt fDof;
4884d3d1a6afSToby Isaac 
4885d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
4886d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
4887d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
4888d3d1a6afSToby Isaac         }
4889d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
48904acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, indices);CHKERRQ(ierr);
4891d3d1a6afSToby Isaac 
4892d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
4893d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
4894d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4895d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
4896d3d1a6afSToby Isaac 
4897d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
4898d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
4899d3d1a6afSToby Isaac         }
4900d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4901d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4902d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
4903d3d1a6afSToby Isaac 
4904d3d1a6afSToby 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 */
4905d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
4906d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
4907302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
49084acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, newIndices);CHKERRQ(ierr);
4909d3d1a6afSToby Isaac         }
4910d3d1a6afSToby Isaac         newP += bDof;
4911d3d1a6afSToby Isaac 
49126ecaa68aSToby Isaac         if (outValues) {
4913d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
4914d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
4915d3d1a6afSToby 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);
4916d3d1a6afSToby Isaac           }
4917d3d1a6afSToby Isaac         }
49186ecaa68aSToby Isaac       }
4919d3d1a6afSToby Isaac       else {
4920d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
4921d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
4922d3d1a6afSToby Isaac         newP++;
4923d3d1a6afSToby Isaac       }
4924d3d1a6afSToby Isaac     }
4925d3d1a6afSToby Isaac   } else {
4926d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4927d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4928d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
49294b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4930d3d1a6afSToby Isaac 
49314b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
49324b2f2278SToby Isaac       if (!bSecDof) {
49334b2f2278SToby Isaac         continue;
49344b2f2278SToby Isaac       }
4935d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4936d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4937d3d1a6afSToby Isaac       }
4938d3d1a6afSToby Isaac       if (bDof) {
4939d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
4940d3d1a6afSToby Isaac 
4941d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
49424acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, indices);CHKERRQ(ierr);
4943d3d1a6afSToby Isaac 
4944d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
4945d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4946d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
4947d3d1a6afSToby Isaac 
4948d3d1a6afSToby 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 */
4949d3d1a6afSToby Isaac 
4950d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
4951d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
4952302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
49534acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, newIndices);CHKERRQ(ierr);
4954d3d1a6afSToby Isaac         }
4955d3d1a6afSToby Isaac         newP += bDof;
4956d3d1a6afSToby Isaac 
4957d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
49586ecaa68aSToby Isaac         if (outValues) {
4959d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
4960d3d1a6afSToby Isaac         }
49616ecaa68aSToby Isaac       }
4962d3d1a6afSToby Isaac       else {
4963d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
4964d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
4965d3d1a6afSToby Isaac         newP++;
4966d3d1a6afSToby Isaac       }
4967d3d1a6afSToby Isaac     }
4968d3d1a6afSToby Isaac   }
4969d3d1a6afSToby Isaac 
49706ecaa68aSToby Isaac   if (outValues) {
49716ecaa68aSToby Isaac     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
4972d3d1a6afSToby Isaac     ierr = PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));CHKERRQ(ierr);
4973d3d1a6afSToby Isaac     /* multiply constraints on the right */
4974d3d1a6afSToby Isaac     if (numFields) {
4975d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
4976d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
4977d3d1a6afSToby Isaac 
4978d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
4979d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
4980d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
4981d3d1a6afSToby Isaac           PetscInt c, r, k;
4982d3d1a6afSToby Isaac           PetscInt dof;
4983d3d1a6afSToby Isaac 
4984d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
49854b2f2278SToby Isaac           if (!dof) {
49864b2f2278SToby Isaac             continue;
49874b2f2278SToby Isaac           }
4988d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
4989d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
4990d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
4991d3d1a6afSToby Isaac 
4992d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
4993d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
4994d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
49954acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
4996d3d1a6afSToby Isaac                 }
4997d3d1a6afSToby Isaac               }
4998d3d1a6afSToby Isaac             }
4999d3d1a6afSToby Isaac           }
5000d3d1a6afSToby Isaac           else {
5001d3d1a6afSToby Isaac             /* copy this column as is */
5002d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5003d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
5004d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5005d3d1a6afSToby Isaac               }
5006d3d1a6afSToby Isaac             }
5007d3d1a6afSToby Isaac           }
5008d3d1a6afSToby Isaac           oldOff += dof;
5009d3d1a6afSToby Isaac         }
5010d3d1a6afSToby Isaac       }
5011d3d1a6afSToby Isaac     }
5012d3d1a6afSToby Isaac     else {
5013d3d1a6afSToby Isaac       PetscInt oldOff = 0;
5014d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5015d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
5016d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
5017d3d1a6afSToby Isaac         PetscInt c, r, k;
5018d3d1a6afSToby Isaac         PetscInt dof;
5019d3d1a6afSToby Isaac 
5020d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
50214b2f2278SToby Isaac         if (!dof) {
50224b2f2278SToby Isaac           continue;
50234b2f2278SToby Isaac         }
5024d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5025d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5026d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
5027d3d1a6afSToby Isaac 
5028d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5029d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
5030d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
5031d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5032d3d1a6afSToby Isaac               }
5033d3d1a6afSToby Isaac             }
5034d3d1a6afSToby Isaac           }
5035d3d1a6afSToby Isaac         }
5036d3d1a6afSToby Isaac         else {
5037d3d1a6afSToby Isaac           /* copy this column as is */
5038d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5039d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
5040d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5041d3d1a6afSToby Isaac             }
5042d3d1a6afSToby Isaac           }
5043d3d1a6afSToby Isaac         }
5044d3d1a6afSToby Isaac         oldOff += dof;
5045d3d1a6afSToby Isaac       }
5046d3d1a6afSToby Isaac     }
5047d3d1a6afSToby Isaac 
50486ecaa68aSToby Isaac     if (multiplyLeft) {
50496ecaa68aSToby Isaac       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
5050d3d1a6afSToby Isaac       ierr = PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));CHKERRQ(ierr);
5051d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
5052d3d1a6afSToby Isaac       if (numFields) {
5053d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5054d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
5055d3d1a6afSToby Isaac 
5056d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
5057d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
5058d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
5059d3d1a6afSToby Isaac             PetscInt c, r, k;
5060d3d1a6afSToby Isaac             PetscInt dof;
5061d3d1a6afSToby Isaac 
5062d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
5063d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5064d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5065d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
5066d3d1a6afSToby Isaac 
5067d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
5068d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5069d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
5070d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5071d3d1a6afSToby Isaac                   }
5072d3d1a6afSToby Isaac                 }
5073d3d1a6afSToby Isaac               }
5074d3d1a6afSToby Isaac             }
5075d3d1a6afSToby Isaac             else {
5076d3d1a6afSToby Isaac               /* copy this row as is */
5077d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
5078d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5079d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5080d3d1a6afSToby Isaac                 }
5081d3d1a6afSToby Isaac               }
5082d3d1a6afSToby Isaac             }
5083d3d1a6afSToby Isaac             oldOff += dof;
5084d3d1a6afSToby Isaac           }
5085d3d1a6afSToby Isaac         }
5086d3d1a6afSToby Isaac       }
5087d3d1a6afSToby Isaac       else {
5088d3d1a6afSToby Isaac         PetscInt oldOff = 0;
5089d3d1a6afSToby Isaac 
5090d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5091d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
5092d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5093d3d1a6afSToby Isaac           PetscInt c, r, k;
5094d3d1a6afSToby Isaac           PetscInt dof;
5095d3d1a6afSToby Isaac 
5096d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
5097d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5098d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5099d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5100d3d1a6afSToby Isaac 
5101d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5102d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5103d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5104d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5105d3d1a6afSToby Isaac                 }
5106d3d1a6afSToby Isaac               }
5107d3d1a6afSToby Isaac             }
5108d3d1a6afSToby Isaac           }
5109d3d1a6afSToby Isaac           else {
5110d3d1a6afSToby Isaac             /* copy this row as is */
51119fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5112d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5113d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5114d3d1a6afSToby Isaac               }
5115d3d1a6afSToby Isaac             }
5116d3d1a6afSToby Isaac           }
5117d3d1a6afSToby Isaac           oldOff += dof;
5118d3d1a6afSToby Isaac         }
5119d3d1a6afSToby Isaac       }
5120d3d1a6afSToby Isaac 
51216ecaa68aSToby Isaac       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
51226ecaa68aSToby Isaac     }
51236ecaa68aSToby Isaac     else {
51246ecaa68aSToby Isaac       newValues = tmpValues;
51256ecaa68aSToby Isaac     }
51266ecaa68aSToby Isaac   }
51276ecaa68aSToby Isaac 
5128d3d1a6afSToby Isaac   /* clean up */
5129d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
5130d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
51316ecaa68aSToby Isaac 
5132d3d1a6afSToby Isaac   if (numFields) {
5133d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
5134d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5135d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
5136d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5137d3d1a6afSToby Isaac     }
5138d3d1a6afSToby Isaac   }
5139d3d1a6afSToby Isaac   else {
5140d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5141d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
5142d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5143d3d1a6afSToby Isaac   }
5144d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5145d3d1a6afSToby Isaac 
5146d3d1a6afSToby Isaac   /* output */
51476ecaa68aSToby Isaac   if (outPoints) {
5148d3d1a6afSToby Isaac     *outPoints = newPoints;
51496ecaa68aSToby Isaac   }
51506ecaa68aSToby Isaac   else {
51516ecaa68aSToby Isaac     ierr = DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
51526ecaa68aSToby Isaac   }
515331620726SToby Isaac   if (outValues) {
5154d3d1a6afSToby Isaac     *outValues = newValues;
51556ecaa68aSToby Isaac   }
51566ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
5157d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
5158d3d1a6afSToby Isaac   }
5159d3d1a6afSToby Isaac   PetscFunctionReturn(0);
5160d3d1a6afSToby Isaac }
5161d3d1a6afSToby Isaac 
5162d3d1a6afSToby Isaac #undef __FUNCT__
51637773e69fSMatthew G. Knepley #define __FUNCT__ "DMPlexGetClosureIndices"
51646ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
51657773e69fSMatthew G. Knepley {
51667773e69fSMatthew G. Knepley   PetscSection    clSection;
51677773e69fSMatthew G. Knepley   IS              clPoints;
51687773e69fSMatthew G. Knepley   const PetscInt *clp;
51694acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
51707773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
51717773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
51727773e69fSMatthew G. Knepley   PetscInt        offsets[32];
51737773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
51747773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
51757773e69fSMatthew G. Knepley 
51767773e69fSMatthew G. Knepley   PetscFunctionBegin;
51777773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51787773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
51797773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
51807773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
51817773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
51827773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
51837773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
51847773e69fSMatthew G. Knepley   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
51857773e69fSMatthew G. Knepley   /* Get points in closure */
5186923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
51877773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
51887773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
51897773e69fSMatthew G. Knepley     PetscInt dof, fdof;
51907773e69fSMatthew G. Knepley 
51917773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
51927773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
51937773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
51947773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
51957773e69fSMatthew G. Knepley     }
51967773e69fSMatthew G. Knepley     Nind += dof;
51977773e69fSMatthew G. Knepley   }
51987773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
51998ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
52004acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
52014acb8e1eSToby Isaac   /* Get dual space symmetries */
52024acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
52034acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52044acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52054acb8e1eSToby Isaac   }
52067773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
52077773e69fSMatthew G. Knepley   {
52084acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
52097773e69fSMatthew G. Knepley     if (numPointsNew) {
52104acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
52114acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52124acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52134acb8e1eSToby Isaac       }
52144acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
52154acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
52164acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
52174acb8e1eSToby Isaac       }
5218923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
52197773e69fSMatthew G. Knepley       numPoints = numPointsNew;
52207773e69fSMatthew G. Knepley       Nind      = NindNew;
52217773e69fSMatthew G. Knepley       points    = pointsNew;
52227773e69fSMatthew G. Knepley     }
52237773e69fSMatthew G. Knepley   }
52247773e69fSMatthew G. Knepley   /* Calculate indices */
52257773e69fSMatthew G. Knepley   ierr = DMGetWorkArray(dm, Nind, PETSC_INT, indices);CHKERRQ(ierr);
52267773e69fSMatthew G. Knepley   if (Nf) {
52276ecaa68aSToby Isaac     if (outOffsets) {
52286ecaa68aSToby Isaac       PetscInt f;
52296ecaa68aSToby Isaac 
523046bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
52316ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
52326ecaa68aSToby Isaac       }
52336ecaa68aSToby Isaac     }
52344acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
52354acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
52364acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, *indices);
52377773e69fSMatthew G. Knepley     }
52387773e69fSMatthew G. Knepley   } else {
52394acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
52404acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
52414acb8e1eSToby Isaac 
52424acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
52434acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, *indices);
52447773e69fSMatthew G. Knepley     }
52457773e69fSMatthew G. Knepley   }
52467773e69fSMatthew G. Knepley   /* Cleanup points */
52474acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
52484acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52494acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52504acb8e1eSToby Isaac   }
52517773e69fSMatthew G. Knepley   if (numPointsNew) {
52527773e69fSMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, PETSC_INT, &pointsNew);CHKERRQ(ierr);
52537773e69fSMatthew G. Knepley   } else {
5254923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
52557773e69fSMatthew G. Knepley   }
52567773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
52577773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
52587773e69fSMatthew G. Knepley }
52597773e69fSMatthew G. Knepley 
52607773e69fSMatthew G. Knepley #undef __FUNCT__
52617773e69fSMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreClosureIndices"
526246bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
52637773e69fSMatthew G. Knepley {
52647773e69fSMatthew G. Knepley   PetscErrorCode ierr;
52657773e69fSMatthew G. Knepley 
52667773e69fSMatthew G. Knepley   PetscFunctionBegin;
52677773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52687773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
52697773e69fSMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, indices);CHKERRQ(ierr);
52707773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
52717773e69fSMatthew G. Knepley }
52727773e69fSMatthew G. Knepley 
52737773e69fSMatthew G. Knepley #undef __FUNCT__
5274552f7358SJed Brown #define __FUNCT__ "DMPlexMatSetClosure"
52757f5d1fdeSMatthew G. Knepley /*@C
52767f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
52777f5d1fdeSMatthew G. Knepley 
52787f5d1fdeSMatthew G. Knepley   Not collective
52797f5d1fdeSMatthew G. Knepley 
52807f5d1fdeSMatthew G. Knepley   Input Parameters:
52817f5d1fdeSMatthew G. Knepley + dm - The DM
5282ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5283ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
52847f5d1fdeSMatthew G. Knepley . A - The matrix
52857f5d1fdeSMatthew G. Knepley . point - The sieve point in the DM
52867f5d1fdeSMatthew G. Knepley . values - The array of values
52877f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
52887f5d1fdeSMatthew G. Knepley 
52897f5d1fdeSMatthew G. Knepley   Fortran Notes:
52907f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
52917f5d1fdeSMatthew G. Knepley 
52927f5d1fdeSMatthew G. Knepley   Level: intermediate
52937f5d1fdeSMatthew G. Knepley 
52947f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
52957f5d1fdeSMatthew G. Knepley @*/
52967c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5297552f7358SJed Brown {
5298552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
52991b406b76SMatthew G. Knepley   PetscSection        clSection;
53001b406b76SMatthew G. Knepley   IS                  clPoints;
5301d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
53021b406b76SMatthew G. Knepley   const PetscInt     *clp;
5303552f7358SJed Brown   PetscInt           *indices;
5304552f7358SJed Brown   PetscInt            offsets[32];
53054acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
53064acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
5307923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
53084acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
5309d3d1a6afSToby Isaac   PetscScalar        *newValues;
5310552f7358SJed Brown   PetscErrorCode      ierr;
5311552f7358SJed Brown 
5312552f7358SJed Brown   PetscFunctionBegin;
5313552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5314ebd6d717SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
53153dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5316ebd6d717SJed Brown   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
53173dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
53183dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5319552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
532082f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5321552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5322923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5323552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5324552f7358SJed Brown     PetscInt fdof;
5325552f7358SJed Brown 
5326552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5327552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5328552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5329552f7358SJed Brown       offsets[f+1] += fdof;
5330552f7358SJed Brown     }
5331552f7358SJed Brown     numIndices += dof;
5332552f7358SJed Brown   }
53330d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
53340d644c17SKarl Rupp 
53358ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
53364acb8e1eSToby Isaac   /* Get symmetries */
53374acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
53384acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
53394acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
53404acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
53414acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
53424acb8e1eSToby Isaac 
53434acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
53444acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
53454acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
53464acb8e1eSToby Isaac 
53474acb8e1eSToby Isaac         if (!numFields) {
53484acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
53494acb8e1eSToby Isaac         } else {
53504acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
53514acb8e1eSToby Isaac         }
53524acb8e1eSToby Isaac         if (flip) {
53534acb8e1eSToby Isaac           PetscInt i, j, k;
53544acb8e1eSToby Isaac 
53554acb8e1eSToby Isaac           if (!valCopy) {
53564acb8e1eSToby Isaac             ierr = DMGetWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
53574acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
53584acb8e1eSToby Isaac             values = valCopy;
53594acb8e1eSToby Isaac           }
53604acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
5361775f7be2SToby Isaac             PetscScalar fval = flip[i];
53624acb8e1eSToby Isaac 
53634acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
53644acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
53654acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
53664acb8e1eSToby Isaac             }
53674acb8e1eSToby Isaac           }
53684acb8e1eSToby Isaac         }
53694acb8e1eSToby Isaac         foffset += fdof;
53704acb8e1eSToby Isaac       }
53714acb8e1eSToby Isaac     }
53724acb8e1eSToby Isaac   }
53734acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
5374d3d1a6afSToby Isaac   if (newNumPoints) {
53754acb8e1eSToby Isaac     if (valCopy) {
53764acb8e1eSToby Isaac       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
53774acb8e1eSToby Isaac     }
53784acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
53794acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
53804acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
53814acb8e1eSToby Isaac     }
53824acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
53834acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
53844acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
53854acb8e1eSToby Isaac     }
5386923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5387d3d1a6afSToby Isaac     numPoints  = newNumPoints;
5388d3d1a6afSToby Isaac     numIndices = newNumIndices;
5389d3d1a6afSToby Isaac     points     = newPoints;
5390d3d1a6afSToby Isaac     values     = newValues;
5391d3d1a6afSToby Isaac   }
5392552f7358SJed Brown   ierr = DMGetWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5393552f7358SJed Brown   if (numFields) {
53944acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
53954acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
53964acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, indices);
5397552f7358SJed Brown     }
5398552f7358SJed Brown   } else {
53994acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
54004acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
54014acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
54024acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, indices);
5403552f7358SJed Brown     }
5404552f7358SJed Brown   }
5405b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
5406552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
5407ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
5408ab1d0545SMatthew G. Knepley     PetscInt i;
5409ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
54108ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
5411ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5412ab1d0545SMatthew G. Knepley   }
5413552f7358SJed Brown   if (ierr) {
5414552f7358SJed Brown     PetscMPIInt    rank;
5415552f7358SJed Brown     PetscErrorCode ierr2;
5416552f7358SJed Brown 
541782f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5418e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5419b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
54206a415b8fSMatthew G Knepley     ierr2 = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr2);
5421552f7358SJed Brown     CHKERRQ(ierr);
5422552f7358SJed Brown   }
54234acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
54244acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54254acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54264acb8e1eSToby Isaac   }
5427d3d1a6afSToby Isaac   if (newNumPoints) {
5428d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
5429d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
5430d3d1a6afSToby Isaac   }
5431d3d1a6afSToby Isaac   else {
54324acb8e1eSToby Isaac     if (valCopy) {
54334acb8e1eSToby Isaac       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
54344acb8e1eSToby Isaac     }
5435923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5436d3d1a6afSToby Isaac   }
5437552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5438552f7358SJed Brown   PetscFunctionReturn(0);
5439552f7358SJed Brown }
5440552f7358SJed Brown 
5441552f7358SJed Brown #undef __FUNCT__
5442de41b84cSMatthew G. Knepley #define __FUNCT__ "DMPlexMatSetClosureRefined"
5443de41b84cSMatthew 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)
5444de41b84cSMatthew G. Knepley {
5445de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
5446de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
5447de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
5448de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
5449de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
5450de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
54514ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
5452de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
5453de41b84cSMatthew G. Knepley 
5454de41b84cSMatthew G. Knepley   PetscFunctionBegin;
5455de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
5456de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
5457de41b84cSMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
5458de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
5459de41b84cSMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
5460de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
5461de41b84cSMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
5462de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
5463de41b84cSMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
5464de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
5465de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
5466de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
5467de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5468de41b84cSMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5469de41b84cSMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5470de41b84cSMatthew G. Knepley   /* Column indices */
5471de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
54724ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
5473de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
5474de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
5475de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
5476de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
5477de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
5478de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
5479de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
5480de41b84cSMatthew G. Knepley       ++q;
5481de41b84cSMatthew G. Knepley     }
5482de41b84cSMatthew G. Knepley   }
5483de41b84cSMatthew G. Knepley   numCPoints = q;
5484de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
5485de41b84cSMatthew G. Knepley     PetscInt fdof;
5486de41b84cSMatthew G. Knepley 
5487de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
54884ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5489de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5490de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
5491de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
5492de41b84cSMatthew G. Knepley     }
5493de41b84cSMatthew G. Knepley     numCIndices += dof;
5494de41b84cSMatthew G. Knepley   }
5495de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
5496de41b84cSMatthew G. Knepley   /* Row indices */
5497de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
5498de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
54991ba5f902SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
5500de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
5501de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
5502de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5503de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
5504de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
5505de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
5506de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
55074ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
55084ca5e9f5SMatthew G. Knepley         if (!dof) continue;
55094ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
55104ca5e9f5SMatthew G. Knepley         if (s < q) continue;
5511de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
5512de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
5513de41b84cSMatthew G. Knepley         ++q;
5514de41b84cSMatthew G. Knepley       }
5515de41b84cSMatthew G. Knepley     }
5516de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5517de41b84cSMatthew G. Knepley   }
5518de41b84cSMatthew G. Knepley   numFPoints = q;
5519de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
5520de41b84cSMatthew G. Knepley     PetscInt fdof;
5521de41b84cSMatthew G. Knepley 
5522de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
55234ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5524de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5525de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
5526de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
5527de41b84cSMatthew G. Knepley     }
5528de41b84cSMatthew G. Knepley     numFIndices += dof;
5529de41b84cSMatthew G. Knepley   }
5530de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
5531de41b84cSMatthew G. Knepley 
55328ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
55338ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
5534de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
5535de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
5536de41b84cSMatthew G. Knepley   if (numFields) {
55374acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
55384acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
55394acb8e1eSToby Isaac 
55404acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
55414acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
55424acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5543de41b84cSMatthew G. Knepley     }
55444acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
55454acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
55464acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
55474acb8e1eSToby Isaac     }
55484acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
55494acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
55504acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
55514acb8e1eSToby Isaac     }
55524acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
55534acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
55544acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5555de41b84cSMatthew G. Knepley     }
5556de41b84cSMatthew G. Knepley   } else {
55574acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
55584acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
55594acb8e1eSToby Isaac 
55604acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
55614acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
55624acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
55634acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
55644acb8e1eSToby Isaac 
55654acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
55664acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);CHKERRQ(ierr);
5567de41b84cSMatthew G. Knepley     }
55684acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
55694acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
55704acb8e1eSToby Isaac 
55714acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
55724acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);CHKERRQ(ierr);
5573de41b84cSMatthew G. Knepley     }
55744acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
55754acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
5576de41b84cSMatthew G. Knepley   }
5577de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
55784acb8e1eSToby Isaac   /* TODO: flips */
5579de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
5580de41b84cSMatthew G. Knepley   if (ierr) {
5581de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
5582de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
5583de41b84cSMatthew G. Knepley 
5584de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5585e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5586de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
5587de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr2);
5588de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr2);
5589de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
5590de41b84cSMatthew G. Knepley   }
5591549a8adaSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
5592de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
5593de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
5594de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
5595de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
5596de41b84cSMatthew G. Knepley }
5597de41b84cSMatthew G. Knepley 
5598de41b84cSMatthew G. Knepley #undef __FUNCT__
55997c927364SMatthew G. Knepley #define __FUNCT__ "DMPlexMatGetClosureIndicesRefined"
56007c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
56017c927364SMatthew G. Knepley {
56027c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
56037c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
56047c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
56057c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
56067c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
56077c927364SMatthew G. Knepley   PetscErrorCode ierr;
56087c927364SMatthew G. Knepley 
56097c927364SMatthew G. Knepley   PetscFunctionBegin;
56107c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
56117c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
56127c927364SMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
56137c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
56147c927364SMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
56157c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
56167c927364SMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
56177c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
56187c927364SMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
56197c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
56207c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
56217c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
56227c927364SMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
56237c927364SMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
56247c927364SMatthew G. Knepley   /* Column indices */
56257c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
56267c927364SMatthew G. Knepley   maxFPoints = numCPoints;
56277c927364SMatthew G. Knepley   /* Compress out points not in the section */
56287c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
56297c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
56307c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
56317c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
56327c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
56337c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
56347c927364SMatthew G. Knepley       ++q;
56357c927364SMatthew G. Knepley     }
56367c927364SMatthew G. Knepley   }
56377c927364SMatthew G. Knepley   numCPoints = q;
56387c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
56397c927364SMatthew G. Knepley     PetscInt fdof;
56407c927364SMatthew G. Knepley 
56417c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
56427c927364SMatthew G. Knepley     if (!dof) continue;
56437c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
56447c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
56457c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
56467c927364SMatthew G. Knepley     }
56477c927364SMatthew G. Knepley     numCIndices += dof;
56487c927364SMatthew G. Knepley   }
56497c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
56507c927364SMatthew G. Knepley   /* Row indices */
56517c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
56527c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
56537c927364SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
56547c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
56557c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
56567c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
56577c927364SMatthew G. Knepley     /* Compress out points not in the section */
56587c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
56597c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
56607c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
56617c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
56627c927364SMatthew G. Knepley         if (!dof) continue;
56637c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
56647c927364SMatthew G. Knepley         if (s < q) continue;
56657c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
56667c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
56677c927364SMatthew G. Knepley         ++q;
56687c927364SMatthew G. Knepley       }
56697c927364SMatthew G. Knepley     }
56707c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
56717c927364SMatthew G. Knepley   }
56727c927364SMatthew G. Knepley   numFPoints = q;
56737c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
56747c927364SMatthew G. Knepley     PetscInt fdof;
56757c927364SMatthew G. Knepley 
56767c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
56777c927364SMatthew G. Knepley     if (!dof) continue;
56787c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
56797c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
56807c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
56817c927364SMatthew G. Knepley     }
56827c927364SMatthew G. Knepley     numFIndices += dof;
56837c927364SMatthew G. Knepley   }
56847c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
56857c927364SMatthew G. Knepley 
56868ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
56878ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
56887c927364SMatthew G. Knepley   if (numFields) {
56894acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
56904acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
56914acb8e1eSToby Isaac 
56924acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
56934acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
56944acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
56957c927364SMatthew G. Knepley     }
56964acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
56974acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
56984acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
56994acb8e1eSToby Isaac     }
57004acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
57014acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
57024acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
57034acb8e1eSToby Isaac     }
57044acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
57054acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
57064acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
57077c927364SMatthew G. Knepley     }
57087c927364SMatthew G. Knepley   } else {
57094acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
57104acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
57114acb8e1eSToby Isaac 
57124acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
57134acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
57144acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
57154acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
57164acb8e1eSToby Isaac 
57174acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
57184acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);
57197c927364SMatthew G. Knepley     }
57204acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
57214acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
57224acb8e1eSToby Isaac 
57234acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
57244acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);
57257c927364SMatthew G. Knepley     }
57264acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
57274acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
57287c927364SMatthew G. Knepley   }
57297c927364SMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
57307c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
57317c927364SMatthew G. Knepley   PetscFunctionReturn(0);
57327c927364SMatthew G. Knepley }
57337c927364SMatthew G. Knepley 
57347c927364SMatthew G. Knepley #undef __FUNCT__
5735770b213bSMatthew G Knepley #define __FUNCT__ "DMPlexGetHybridBounds"
5736f96281f8SMatthew G. Knepley /*@
5737f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
5738f96281f8SMatthew G. Knepley 
5739f96281f8SMatthew G. Knepley   Input Parameter:
5740f96281f8SMatthew G. Knepley . dm - The DMPlex object
5741f96281f8SMatthew G. Knepley 
5742f96281f8SMatthew G. Knepley   Output Parameters:
5743f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
5744dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5745dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5746dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5747f96281f8SMatthew G. Knepley 
5748f96281f8SMatthew G. Knepley   Level: developer
5749f96281f8SMatthew G. Knepley 
5750dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
5751f96281f8SMatthew G. Knepley @*/
5752770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
5753552f7358SJed Brown {
5754552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5755770b213bSMatthew G Knepley   PetscInt       dim;
5756770b213bSMatthew G Knepley   PetscErrorCode ierr;
5757552f7358SJed Brown 
5758552f7358SJed Brown   PetscFunctionBegin;
5759552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5760c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5761770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
5762770b213bSMatthew G Knepley   if (fMax) *fMax = mesh->hybridPointMax[dim-1];
5763770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
5764770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
5765552f7358SJed Brown   PetscFunctionReturn(0);
5766552f7358SJed Brown }
5767552f7358SJed Brown 
5768552f7358SJed Brown #undef __FUNCT__
5769770b213bSMatthew G Knepley #define __FUNCT__ "DMPlexSetHybridBounds"
5770dc5a3409SMatthew G. Knepley /*@
5771dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
5772dc5a3409SMatthew G. Knepley 
5773dc5a3409SMatthew G. Knepley   Input Parameters:
5774dc5a3409SMatthew G. Knepley . dm   - The DMPlex object
5775dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
5776dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5777dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5778dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5779dc5a3409SMatthew G. Knepley 
5780dc5a3409SMatthew G. Knepley   Level: developer
5781dc5a3409SMatthew G. Knepley 
5782dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
5783dc5a3409SMatthew G. Knepley @*/
5784770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
5785552f7358SJed Brown {
5786552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5787770b213bSMatthew G Knepley   PetscInt       dim;
5788770b213bSMatthew G Knepley   PetscErrorCode ierr;
5789552f7358SJed Brown 
5790552f7358SJed Brown   PetscFunctionBegin;
5791552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5792c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5793770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]   = cMax;
5794770b213bSMatthew G Knepley   if (fMax >= 0) mesh->hybridPointMax[dim-1] = fMax;
5795770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]     = eMax;
5796770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]     = vMax;
5797552f7358SJed Brown   PetscFunctionReturn(0);
5798552f7358SJed Brown }
5799552f7358SJed Brown 
5800552f7358SJed Brown #undef __FUNCT__
5801552f7358SJed Brown #define __FUNCT__ "DMPlexGetVTKCellHeight"
5802552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
5803552f7358SJed Brown {
5804552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5805552f7358SJed Brown 
5806552f7358SJed Brown   PetscFunctionBegin;
5807552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5808552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
5809552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
5810552f7358SJed Brown   PetscFunctionReturn(0);
5811552f7358SJed Brown }
5812552f7358SJed Brown 
5813552f7358SJed Brown #undef __FUNCT__
5814552f7358SJed Brown #define __FUNCT__ "DMPlexSetVTKCellHeight"
5815552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
5816552f7358SJed Brown {
5817552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5818552f7358SJed Brown 
5819552f7358SJed Brown   PetscFunctionBegin;
5820552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5821552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
5822552f7358SJed Brown   PetscFunctionReturn(0);
5823552f7358SJed Brown }
5824552f7358SJed Brown 
5825552f7358SJed Brown #undef __FUNCT__
5826552f7358SJed Brown #define __FUNCT__ "DMPlexCreateNumbering_Private"
5827552f7358SJed Brown /* We can easily have a form that takes an IS instead */
5828ef48cebcSMatthew G. Knepley static PetscErrorCode DMPlexCreateNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
5829552f7358SJed Brown {
5830552f7358SJed Brown   PetscSection   section, globalSection;
5831552f7358SJed Brown   PetscInt      *numbers, p;
5832552f7358SJed Brown   PetscErrorCode ierr;
5833552f7358SJed Brown 
5834552f7358SJed Brown   PetscFunctionBegin;
583582f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
5836552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
5837552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5838552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
5839552f7358SJed Brown   }
5840552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
584115b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
5842854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
5843552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5844552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
5845ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
5846ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
5847552f7358SJed Brown   }
584882f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
5849ef48cebcSMatthew G. Knepley   if (globalSize) {
5850ef48cebcSMatthew G. Knepley     PetscLayout layout;
5851ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
5852ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
5853ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
5854ef48cebcSMatthew G. Knepley   }
5855552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
5856552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
5857552f7358SJed Brown   PetscFunctionReturn(0);
5858552f7358SJed Brown }
5859552f7358SJed Brown 
5860552f7358SJed Brown #undef __FUNCT__
586181ed3555SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateCellNumbering_Internal"
586281ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
5863552f7358SJed Brown {
5864552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
5865552f7358SJed Brown   PetscErrorCode ierr;
5866552f7358SJed Brown 
5867552f7358SJed Brown   PetscFunctionBegin;
5868552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
5869552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
58700298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
587181ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
587281ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
587381ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
5874552f7358SJed Brown }
587581ed3555SMatthew G. Knepley 
587681ed3555SMatthew G. Knepley #undef __FUNCT__
587781ed3555SMatthew G. Knepley #define __FUNCT__ "DMPlexGetCellNumbering"
587881ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
587981ed3555SMatthew G. Knepley {
588081ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
588181ed3555SMatthew G. Knepley   PetscErrorCode ierr;
588281ed3555SMatthew G. Knepley 
588381ed3555SMatthew G. Knepley   PetscFunctionBegin;
588481ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
588581ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
5886552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
5887552f7358SJed Brown   PetscFunctionReturn(0);
5888552f7358SJed Brown }
5889552f7358SJed Brown 
5890552f7358SJed Brown #undef __FUNCT__
589181ed3555SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateVertexNumbering_Internal"
589281ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
589381ed3555SMatthew G. Knepley {
589481ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
589581ed3555SMatthew G. Knepley   PetscErrorCode ierr;
589681ed3555SMatthew G. Knepley 
589781ed3555SMatthew G. Knepley   PetscFunctionBegin;
589881ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
589981ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
590081ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
590181ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
590281ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
590381ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
590481ed3555SMatthew G. Knepley }
590581ed3555SMatthew G. Knepley 
590681ed3555SMatthew G. Knepley #undef __FUNCT__
5907552f7358SJed Brown #define __FUNCT__ "DMPlexGetVertexNumbering"
5908552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
5909552f7358SJed Brown {
5910552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5911552f7358SJed Brown   PetscErrorCode ierr;
5912552f7358SJed Brown 
5913552f7358SJed Brown   PetscFunctionBegin;
5914552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
591581ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
5916552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
5917552f7358SJed Brown   PetscFunctionReturn(0);
5918552f7358SJed Brown }
5919552f7358SJed Brown 
5920ef48cebcSMatthew G. Knepley #undef __FUNCT__
5921ef48cebcSMatthew G. Knepley #define __FUNCT__ "DMPlexCreatePointNumbering"
5922ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
5923ef48cebcSMatthew G. Knepley {
5924ef48cebcSMatthew G. Knepley   IS             nums[4];
5925ef48cebcSMatthew G. Knepley   PetscInt       depths[4];
5926ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
5927ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
5928ef48cebcSMatthew G. Knepley 
5929ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
5930ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5931ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
59328abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
59338abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
5934ef48cebcSMatthew G. Knepley   depths[0] = depth; depths[1] = 0;
5935ef48cebcSMatthew G. Knepley   for (d = 2; d <= depth; ++d) depths[d] = depth-d+1;
5936ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
5937ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
5938ef48cebcSMatthew G. Knepley 
5939ef48cebcSMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, depths[d], &pStart, &pEnd);CHKERRQ(ierr);
5940ef48cebcSMatthew G. Knepley     ierr = DMPlexCreateNumbering_Private(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
5941ef48cebcSMatthew G. Knepley     shift += gsize;
5942ef48cebcSMatthew G. Knepley   }
5943302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
5944ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
5945ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
5946ef48cebcSMatthew G. Knepley }
5947ef48cebcSMatthew G. Knepley 
5948ca8062c8SMatthew G. Knepley #undef __FUNCT__
5949ca8062c8SMatthew G. Knepley #define __FUNCT__ "DMPlexCheckSymmetry"
5950ca8062c8SMatthew G. Knepley /*@
5951ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
5952ca8062c8SMatthew G. Knepley 
5953ca8062c8SMatthew G. Knepley   Input Parameters:
5954ca8062c8SMatthew G. Knepley   + dm - The DMPlex object
5955ca8062c8SMatthew G. Knepley 
5956ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
5957ca8062c8SMatthew G. Knepley 
5958ca8062c8SMatthew G. Knepley   Level: developer
5959ca8062c8SMatthew G. Knepley 
59609bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSkeleton(), DMCheckFaces()
5961ca8062c8SMatthew G. Knepley @*/
5962ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
5963ca8062c8SMatthew G. Knepley {
5964ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
5965ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
5966ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
5967ca8062c8SMatthew G. Knepley   PetscInt        pStart, pEnd, p, csize, ssize;
5968ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
5969ca8062c8SMatthew G. Knepley 
5970ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
5971ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5972ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
5973ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
5974ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
5975ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
5976ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
5977ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
5978ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
5979ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
598042e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
598142e66dfaSMatthew G. Knepley       PetscInt  d;
598242e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
598342e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
598442e66dfaSMatthew G. Knepley       }
5985ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
5986ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
5987ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
5988ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
5989ca8062c8SMatthew G. Knepley       }
599042e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
59918ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
5992ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
59938ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
5994ca8062c8SMatthew G. Knepley         }
5995302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
59968ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
5997ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
59988ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
5999ca8062c8SMatthew G. Knepley         }
6000302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
60018ccfff9cSToby 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]);
60028ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
6003ca8062c8SMatthew G. Knepley       }
600442e66dfaSMatthew G. Knepley     }
6005ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
6006ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
6007ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
6008ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
6009ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
6010ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
6011ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
6012ca8062c8SMatthew G. Knepley       }
6013ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
60148ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
6015ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
60168ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
6017ca8062c8SMatthew G. Knepley         }
6018302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
60198ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
6020ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
60218ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
6022ca8062c8SMatthew G. Knepley         }
6023302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
60248ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
6025ca8062c8SMatthew G. Knepley       }
6026ca8062c8SMatthew G. Knepley     }
6027ca8062c8SMatthew G. Knepley   }
6028ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
6029ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
60308ccfff9cSToby Isaac   if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
6031ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6032ca8062c8SMatthew G. Knepley }
6033ca8062c8SMatthew G. Knepley 
6034ca8062c8SMatthew G. Knepley #undef __FUNCT__
6035ca8062c8SMatthew G. Knepley #define __FUNCT__ "DMPlexCheckSkeleton"
6036ca8062c8SMatthew G. Knepley /*@
6037ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
6038ca8062c8SMatthew G. Knepley 
6039ca8062c8SMatthew G. Knepley   Input Parameters:
6040ca8062c8SMatthew G. Knepley + dm - The DMPlex object
604158723a97SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
604258723a97SMatthew G. Knepley - cellHeight - Normally 0
6043ca8062c8SMatthew G. Knepley 
6044ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6045ca8062c8SMatthew G. Knepley 
6046ca8062c8SMatthew G. Knepley   Level: developer
6047ca8062c8SMatthew G. Knepley 
60489bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckFaces()
6049ca8062c8SMatthew G. Knepley @*/
605058723a97SMatthew G. Knepley PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscBool isSimplex, PetscInt cellHeight)
6051ca8062c8SMatthew G. Knepley {
605242363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
6053ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
6054ca8062c8SMatthew G. Knepley 
6055ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6056ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6057c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6058ca8062c8SMatthew G. Knepley   switch (dim) {
605942363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
606042363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
606142363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
6062ca8062c8SMatthew G. Knepley   default:
60638ccfff9cSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
6064ca8062c8SMatthew G. Knepley   }
606558723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
606658723a97SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
6067ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
6068ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
6069ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
607058723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
607158723a97SMatthew G. Knepley 
607258723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
607358723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
607458723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
607558723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
607658723a97SMatthew G. Knepley     }
607758723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
60788ccfff9cSToby Isaac     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
6079ca8062c8SMatthew G. Knepley   }
608042363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
608142363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
608242363296SMatthew G. Knepley 
608342363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
608442363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
608542363296SMatthew G. Knepley       const PetscInt p = closure[cl];
608642363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
608742363296SMatthew G. Knepley     }
608842363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
60898ccfff9cSToby Isaac     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
609042363296SMatthew G. Knepley   }
6091ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6092ca8062c8SMatthew G. Knepley }
60939bf0dad6SMatthew G. Knepley 
60949bf0dad6SMatthew G. Knepley #undef __FUNCT__
60959bf0dad6SMatthew G. Knepley #define __FUNCT__ "DMPlexCheckFaces"
60969bf0dad6SMatthew G. Knepley /*@
60979bf0dad6SMatthew 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
60989bf0dad6SMatthew G. Knepley 
60999bf0dad6SMatthew G. Knepley   Input Parameters:
61009bf0dad6SMatthew G. Knepley + dm - The DMPlex object
61019bf0dad6SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
61029bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
61039bf0dad6SMatthew G. Knepley 
61049bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
61059bf0dad6SMatthew G. Knepley 
61069bf0dad6SMatthew G. Knepley   Level: developer
61079bf0dad6SMatthew G. Knepley 
61089bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckSkeleton()
61099bf0dad6SMatthew G. Knepley @*/
61109bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexCheckFaces(DM dm, PetscBool isSimplex, PetscInt cellHeight)
61119bf0dad6SMatthew G. Knepley {
61123554e41dSMatthew G. Knepley   PetscInt       pMax[4];
61133554e41dSMatthew G. Knepley   PetscInt       dim, vStart, vEnd, cStart, cEnd, c, h;
61149bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
61159bf0dad6SMatthew G. Knepley 
61169bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
61179bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6118c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
61199bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
612025abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
61213554e41dSMatthew G. Knepley   for (h = cellHeight; h < dim; ++h) {
61223554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
61233554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
61249bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
61259bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
61269bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
61279bf0dad6SMatthew G. Knepley 
612825abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
61299bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
61309bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
61319bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
61329bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
61339bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
61349bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
61359bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
61369bf0dad6SMatthew G. Knepley       }
61373554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
61388ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
61399bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
61409bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
61419bf0dad6SMatthew G. Knepley 
61429bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
61439bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
61449bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
61459bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
61469bf0dad6SMatthew G. Knepley         }
61478ccfff9cSToby 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);
61489bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
61498ccfff9cSToby 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]);
61509bf0dad6SMatthew G. Knepley         }
61519bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
61529bf0dad6SMatthew G. Knepley       }
61539bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
61549bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
61559bf0dad6SMatthew G. Knepley     }
61563554e41dSMatthew G. Knepley   }
6157552f7358SJed Brown   PetscFunctionReturn(0);
6158552f7358SJed Brown }
61593913d7c8SMatthew G. Knepley 
61603913d7c8SMatthew G. Knepley #undef __FUNCT__
6161bceba477SMatthew G. Knepley #define __FUNCT__ "DMCreateInterpolation_Plex"
6162bceba477SMatthew G. Knepley /* Pointwise interpolation
6163bceba477SMatthew G. Knepley      Just code FEM for now
6164bceba477SMatthew G. Knepley      u^f = I u^c
61654ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
61664ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
61674ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
6168bceba477SMatthew G. Knepley */
6169bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
6170bceba477SMatthew G. Knepley {
6171bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
6172bceba477SMatthew G. Knepley   PetscInt       m, n;
6173a063dac3SMatthew G. Knepley   void          *ctx;
617468132eb9SMatthew G. Knepley   DM             cdm;
617568132eb9SMatthew G. Knepley   PetscBool      regular;
6176bceba477SMatthew G. Knepley   PetscErrorCode ierr;
6177bceba477SMatthew G. Knepley 
6178bceba477SMatthew G. Knepley   PetscFunctionBegin;
6179bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6180bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6181bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6182bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
618368132eb9SMatthew G. Knepley 
6184bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
6185bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6186bceba477SMatthew G. Knepley   ierr = MatSetType(*interpolation, dmCoarse->mattype);CHKERRQ(ierr);
6187a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
618868132eb9SMatthew G. Knepley 
6189a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
619068132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
619168132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
619268132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
619368132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
61945d1c2e58SMatthew G. Knepley   /* Use naive scaling */
61955d1c2e58SMatthew G. Knepley   ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
6196a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
6197a063dac3SMatthew G. Knepley }
6198bceba477SMatthew G. Knepley 
6199a063dac3SMatthew G. Knepley #undef __FUNCT__
6200a063dac3SMatthew G. Knepley #define __FUNCT__ "DMCreateInjection_Plex"
62016dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
6202a063dac3SMatthew G. Knepley {
620390748bafSMatthew G. Knepley   PetscErrorCode ierr;
62046dbf9973SLawrence Mitchell   VecScatter     ctx;
620590748bafSMatthew G. Knepley 
6206a063dac3SMatthew G. Knepley   PetscFunctionBegin;
62076dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
62086dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
62096dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
6210bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6211bceba477SMatthew G. Knepley }
6212bceba477SMatthew G. Knepley 
6213bceba477SMatthew G. Knepley #undef __FUNCT__
6214fd59a867SMatthew G. Knepley #define __FUNCT__ "DMCreateDefaultSection_Plex"
6215fd59a867SMatthew G. Knepley PetscErrorCode DMCreateDefaultSection_Plex(DM dm)
6216fd59a867SMatthew G. Knepley {
6217fd59a867SMatthew G. Knepley   PetscSection   section;
6218ab1d0545SMatthew G. Knepley   IS            *bcPoints, *bcComps;
6219ebb3236fSMatthew G. Knepley   PetscBool     *isFE;
6220286d8613SMatthew G. Knepley   PetscInt      *bcFields, *numComp, *numDof;
6221ebb3236fSMatthew G. Knepley   PetscInt       depth, dim, numBd, numBC = 0, numFields, bd, bc = 0, f;
6222ebb3236fSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
6223fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
6224fd59a867SMatthew G. Knepley 
6225fd59a867SMatthew G. Knepley   PetscFunctionBegin;
6226ebb3236fSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
6227ae71db08SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
6228ebb3236fSMatthew G. Knepley   /* FE and FV boundary conditions are handled slightly differently */
6229ebb3236fSMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
6230ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6231ebb3236fSMatthew G. Knepley     PetscObject  obj;
6232ebb3236fSMatthew G. Knepley     PetscClassId id;
6233ebb3236fSMatthew G. Knepley 
6234ebb3236fSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6235ebb3236fSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
6236ebb3236fSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
6237ebb3236fSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
62388ccfff9cSToby Isaac     else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
6239ebb3236fSMatthew G. Knepley   }
62402f900235SMatthew G. Knepley   /* Allocate boundary point storage for FEM boundaries */
6241286d8613SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
6242c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6243ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
6244ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
6245dff059c6SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob, &numBd);CHKERRQ(ierr);
6246fd59a867SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
62472f900235SMatthew G. Knepley     PetscInt                field;
6248f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6249385532a5SToby Isaac     const char             *labelName;
6250385532a5SToby Isaac     DMLabel                 label;
62512f900235SMatthew G. Knepley 
6252f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &labelName, &field, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
6253385532a5SToby Isaac     ierr = DMGetLabel(dm,labelName,&label);CHKERRQ(ierr);
6254f971fd6bSMatthew G. Knepley     if (label && isFE[field] && (type & DM_BC_ESSENTIAL)) ++numBC;
6255fd59a867SMatthew G. Knepley   }
62562f900235SMatthew G. Knepley   /* Add ghost cell boundaries for FVM */
6257ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) if (!isFE[f] && cEndInterior >= 0) ++numBC;
62586c8d74c4SMatthew G. Knepley   ierr = PetscCalloc3(numBC,&bcFields,numBC,&bcPoints,numBC,&bcComps);CHKERRQ(ierr);
6259ebb3236fSMatthew G. Knepley   /* Constrain ghost cells for FV */
6260ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6261ebb3236fSMatthew G. Knepley     PetscInt *newidx, c;
6262ebb3236fSMatthew G. Knepley 
6263ebb3236fSMatthew G. Knepley     if (isFE[f] || cEndInterior < 0) continue;
6264ebb3236fSMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cEndInterior,&newidx);CHKERRQ(ierr);
6265ebb3236fSMatthew G. Knepley     for (c = cEndInterior; c < cEnd; ++c) newidx[c-cEndInterior] = c;
6266ebb3236fSMatthew G. Knepley     bcFields[bc] = f;
6267ebb3236fSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), cEnd-cEndInterior, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6268ebb3236fSMatthew G. Knepley   }
6269ebb3236fSMatthew G. Knepley   /* Handle FEM Dirichlet boundaries */
6270ebb3236fSMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
6271fd59a867SMatthew G. Knepley     const char             *bdLabel;
6272fd59a867SMatthew G. Knepley     DMLabel                 label;
62730e0f25f2SMatthew G. Knepley     const PetscInt         *comps;
6274fd59a867SMatthew G. Knepley     const PetscInt         *values;
62750e0f25f2SMatthew G. Knepley     PetscInt                bd2, field, numComps, numValues;
6276f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6277f971fd6bSMatthew G. Knepley     PetscBool               duplicate = PETSC_FALSE;
6278fd59a867SMatthew G. Knepley 
6279f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &bdLabel, &field, &numComps, &comps, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
6280c58f1c22SToby Isaac     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
6281385532a5SToby Isaac     if (!isFE[field] || !label) continue;
6282ebb3236fSMatthew G. Knepley     /* Only want to modify label once */
62837391c1cbSMatthew G. Knepley     for (bd2 = 0; bd2 < bd; ++bd2) {
62847391c1cbSMatthew G. Knepley       const char *bdname;
6285dff059c6SToby Isaac       ierr = PetscDSGetBoundary(dm->prob, bd2, NULL, NULL, &bdname, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
62867391c1cbSMatthew G. Knepley       ierr = PetscStrcmp(bdname, bdLabel, &duplicate);CHKERRQ(ierr);
62877391c1cbSMatthew G. Knepley       if (duplicate) break;
62887391c1cbSMatthew G. Knepley     }
6289ebb3236fSMatthew G. Knepley     if (!duplicate && (isFE[field])) {
6290b0bf5782SToby Isaac       /* don't complete cells, which are just present to give orientation to the boundary */
6291092e5057SToby Isaac       ierr = DMPlexLabelComplete(dm, label);CHKERRQ(ierr);
62927391c1cbSMatthew G. Knepley     }
6293ebb3236fSMatthew G. Knepley     /* Filter out cells, if you actually want to constrain cells you need to do things by hand right now */
6294f971fd6bSMatthew G. Knepley     if (type & DM_BC_ESSENTIAL) {
629593a5981aSMatthew G. Knepley       PetscInt       *newidx;
6296ebb3236fSMatthew G. Knepley       PetscInt        n, newn = 0, p, v;
629793a5981aSMatthew G. Knepley 
6298fd59a867SMatthew G. Knepley       bcFields[bc] = field;
62990e0f25f2SMatthew G. Knepley       if (numComps) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), numComps, comps, PETSC_COPY_VALUES, &bcComps[bc]);CHKERRQ(ierr);}
6300ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6301ebb3236fSMatthew G. Knepley         IS              tmp;
6302ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6303ebb3236fSMatthew G. Knepley 
6304c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6305ebb3236fSMatthew G. Knepley         if (!tmp) continue;
630693a5981aSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
630793a5981aSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6308ebb3236fSMatthew G. Knepley         if (isFE[field]) {
630993a5981aSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) ++newn;
6310ebb3236fSMatthew G. Knepley         } else {
6311ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) ++newn;
6312ebb3236fSMatthew G. Knepley         }
631393a5981aSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
631493a5981aSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6315fd59a867SMatthew G. Knepley       }
6316ebb3236fSMatthew G. Knepley       ierr = PetscMalloc1(newn,&newidx);CHKERRQ(ierr);
6317ebb3236fSMatthew G. Knepley       newn = 0;
6318ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6319ebb3236fSMatthew G. Knepley         IS              tmp;
6320ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6321ebb3236fSMatthew G. Knepley 
6322c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6323ebb3236fSMatthew G. Knepley         if (!tmp) continue;
6324ebb3236fSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
6325ebb3236fSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6326ebb3236fSMatthew G. Knepley         if (isFE[field]) {
6327ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) newidx[newn++] = idx[p];
6328ebb3236fSMatthew G. Knepley         } else {
6329ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) newidx[newn++] = idx[p];
6330ebb3236fSMatthew G. Knepley         }
6331ebb3236fSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
6332ebb3236fSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6333ebb3236fSMatthew G. Knepley       }
6334ebb3236fSMatthew G. Knepley       ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), newn, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6335ebb3236fSMatthew G. Knepley     }
6336fd59a867SMatthew G. Knepley   }
6337fd59a867SMatthew G. Knepley   /* Handle discretization */
6338ebb3236fSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&numComp,numFields*(dim+1),&numDof);CHKERRQ(ierr);
6339fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
63400bacfa0aSMatthew G. Knepley     PetscObject obj;
63410bacfa0aSMatthew G. Knepley 
63420bacfa0aSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6343ebb3236fSMatthew G. Knepley     if (isFE[f]) {
63440bacfa0aSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
6345286d8613SMatthew G. Knepley       const PetscInt *numFieldDof;
6346fd59a867SMatthew G. Knepley       PetscInt        d;
6347fd59a867SMatthew G. Knepley 
6348fd59a867SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
6349286d8613SMatthew G. Knepley       ierr = PetscFEGetNumDof(fe, &numFieldDof);CHKERRQ(ierr);
6350286d8613SMatthew G. Knepley       for (d = 0; d < dim+1; ++d) numDof[f*(dim+1)+d] = numFieldDof[d];
6351ebb3236fSMatthew G. Knepley     } else {
63520bacfa0aSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
63530bacfa0aSMatthew G. Knepley 
63540bacfa0aSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
63550bacfa0aSMatthew G. Knepley       numDof[f*(dim+1)+dim] = numComp[f];
6356ebb3236fSMatthew G. Knepley     }
6357fd59a867SMatthew G. Knepley   }
6358286d8613SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6359286d8613SMatthew G. Knepley     PetscInt d;
6360286d8613SMatthew G. Knepley     for (d = 1; d < dim; ++d) {
6361286d8613SMatthew 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.");
6362286d8613SMatthew G. Knepley     }
6363286d8613SMatthew G. Knepley   }
6364ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSection(dm, dim, numFields, numComp, numDof, numBC, bcFields, bcComps, bcPoints, NULL, &section);CHKERRQ(ierr);
6365fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6366fd59a867SMatthew G. Knepley     PetscFE     fe;
6367fd59a867SMatthew G. Knepley     const char *name;
636818abd763SMatthew G. Knepley 
636918abd763SMatthew G. Knepley     ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
6370fd59a867SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
6371fd59a867SMatthew G. Knepley     ierr = PetscSectionSetFieldName(section, f, name);CHKERRQ(ierr);
6372fd59a867SMatthew G. Knepley   }
6373fd59a867SMatthew G. Knepley   ierr = DMSetDefaultSection(dm, section);CHKERRQ(ierr);
6374fd59a867SMatthew G. Knepley   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
63750e0f25f2SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {ierr = ISDestroy(&bcPoints[bc]);CHKERRQ(ierr);ierr = ISDestroy(&bcComps[bc]);CHKERRQ(ierr);}
63760e0f25f2SMatthew G. Knepley   ierr = PetscFree3(bcFields,bcPoints,bcComps);CHKERRQ(ierr);
6377286d8613SMatthew G. Knepley   ierr = PetscFree2(numComp,numDof);CHKERRQ(ierr);
6378ebb3236fSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
6379bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6380bceba477SMatthew G. Knepley }
6381bceba477SMatthew G. Knepley 
6382bceba477SMatthew G. Knepley #undef __FUNCT__
63830aef6b92SMatthew G. Knepley #define __FUNCT__ "DMPlexGetRegularRefinement"
63840aef6b92SMatthew G. Knepley /*@
63850aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
63860aef6b92SMatthew G. Knepley 
63870aef6b92SMatthew G. Knepley   Input Parameter:
63880aef6b92SMatthew G. Knepley . dm - The DMPlex object
63890aef6b92SMatthew G. Knepley 
63900aef6b92SMatthew G. Knepley   Output Parameter:
63910aef6b92SMatthew G. Knepley . regular - The flag
63920aef6b92SMatthew G. Knepley 
63930aef6b92SMatthew G. Knepley   Level: intermediate
63940aef6b92SMatthew G. Knepley 
63950aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
63960aef6b92SMatthew G. Knepley @*/
63970aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
63980aef6b92SMatthew G. Knepley {
63990aef6b92SMatthew G. Knepley   PetscFunctionBegin;
64000aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64010aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
64020aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
64030aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
64040aef6b92SMatthew G. Knepley }
64050aef6b92SMatthew G. Knepley 
64060aef6b92SMatthew G. Knepley #undef __FUNCT__
64070aef6b92SMatthew G. Knepley #define __FUNCT__ "DMPlexSetRegularRefinement"
64080aef6b92SMatthew G. Knepley /*@
64090aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
64100aef6b92SMatthew G. Knepley 
64110aef6b92SMatthew G. Knepley   Input Parameters:
64120aef6b92SMatthew G. Knepley + dm - The DMPlex object
64130aef6b92SMatthew G. Knepley - regular - The flag
64140aef6b92SMatthew G. Knepley 
64150aef6b92SMatthew G. Knepley   Level: intermediate
64160aef6b92SMatthew G. Knepley 
64170aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
64180aef6b92SMatthew G. Knepley @*/
64190aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
64200aef6b92SMatthew G. Knepley {
64210aef6b92SMatthew G. Knepley   PetscFunctionBegin;
64220aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64230aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
64240aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
64250aef6b92SMatthew G. Knepley }
64260aef6b92SMatthew G. Knepley 
6427f7c74593SToby Isaac /* anchors */
6428a68b90caSToby Isaac #undef __FUNCT__
6429a17985deSToby Isaac #define __FUNCT__ "DMPlexGetAnchors"
6430a68b90caSToby Isaac /*@
6431f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
6432f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
6433a68b90caSToby Isaac 
6434e228b242SToby Isaac   not collective
6435a68b90caSToby Isaac 
6436a68b90caSToby Isaac   Input Parameters:
6437a68b90caSToby Isaac . dm - The DMPlex object
6438a68b90caSToby Isaac 
6439a68b90caSToby Isaac   Output Parameters:
6440a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
6441a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
6442a68b90caSToby Isaac 
6443a68b90caSToby Isaac 
6444a68b90caSToby Isaac   Level: intermediate
6445a68b90caSToby Isaac 
6446f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
6447a68b90caSToby Isaac @*/
6448a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
6449a68b90caSToby Isaac {
6450a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
645141e6d900SToby Isaac   PetscErrorCode ierr;
6452a68b90caSToby Isaac 
6453a68b90caSToby Isaac   PetscFunctionBegin;
6454a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
645541e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
6456a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
6457a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
6458a68b90caSToby Isaac   PetscFunctionReturn(0);
6459a68b90caSToby Isaac }
6460a68b90caSToby Isaac 
6461a68b90caSToby Isaac #undef __FUNCT__
6462a17985deSToby Isaac #define __FUNCT__ "DMPlexSetAnchors"
6463a68b90caSToby Isaac /*@
6464f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
6465f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
6466a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
6467a68b90caSToby Isaac 
6468a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
6469f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
6470a68b90caSToby Isaac 
6471e228b242SToby Isaac   collective on dm
6472a68b90caSToby Isaac 
6473a68b90caSToby Isaac   Input Parameters:
6474a68b90caSToby Isaac + dm - The DMPlex object
6475e228b242SToby 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).
6476e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
6477a68b90caSToby Isaac 
6478a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
6479a68b90caSToby Isaac 
6480a68b90caSToby Isaac   Level: intermediate
6481a68b90caSToby Isaac 
6482f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
6483a68b90caSToby Isaac @*/
6484a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
6485a68b90caSToby Isaac {
6486a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6487e228b242SToby Isaac   PetscMPIInt    result;
6488a68b90caSToby Isaac   PetscErrorCode ierr;
6489a68b90caSToby Isaac 
6490a68b90caSToby Isaac   PetscFunctionBegin;
6491a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6492e228b242SToby Isaac   if (anchorSection) {
6493e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
6494e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
6495e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
6496e228b242SToby Isaac   }
6497e228b242SToby Isaac   if (anchorIS) {
6498e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
6499e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
6500e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
6501e228b242SToby Isaac   }
6502a68b90caSToby Isaac 
6503a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
6504a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
6505a68b90caSToby Isaac   plex->anchorSection = anchorSection;
6506a68b90caSToby Isaac 
6507a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
6508a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
6509a68b90caSToby Isaac   plex->anchorIS = anchorIS;
6510a68b90caSToby Isaac 
6511a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
6512a68b90caSToby Isaac   if (anchorIS && anchorSection) {
6513a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
6514a68b90caSToby Isaac     const PetscInt *anchors;
6515a68b90caSToby Isaac 
6516a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
6517a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
6518a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
6519a68b90caSToby Isaac     for (a = 0; a < size; a++) {
6520a68b90caSToby Isaac       PetscInt p;
6521a68b90caSToby Isaac 
6522a68b90caSToby Isaac       p = anchors[a];
6523a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
6524a68b90caSToby Isaac         PetscInt dof;
6525a68b90caSToby Isaac 
6526a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6527a68b90caSToby Isaac         if (dof) {
6528a68b90caSToby Isaac           PetscErrorCode ierr2;
6529a68b90caSToby Isaac 
6530a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
65318ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
6532a68b90caSToby Isaac         }
6533a68b90caSToby Isaac       }
6534a68b90caSToby Isaac     }
6535a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
6536a68b90caSToby Isaac   }
6537a68b90caSToby Isaac #endif
6538f7c74593SToby Isaac   /* reset the generic constraints */
6539f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
6540a68b90caSToby Isaac   PetscFunctionReturn(0);
6541a68b90caSToby Isaac }
6542a68b90caSToby Isaac 
6543a68b90caSToby Isaac #undef __FUNCT__
6544f7c74593SToby Isaac #define __FUNCT__ "DMPlexCreateConstraintSection_Anchors"
6545f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
6546a68b90caSToby Isaac {
6547f7c74593SToby Isaac   PetscSection anchorSection;
65486995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
6549a68b90caSToby Isaac   PetscErrorCode ierr;
6550a68b90caSToby Isaac 
6551a68b90caSToby Isaac   PetscFunctionBegin;
6552a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6553a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
6554e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
6555a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
65566995de1eSToby Isaac   if (numFields) {
6557719ab38cSToby Isaac     PetscInt f;
6558a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
6559719ab38cSToby Isaac 
6560719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
6561719ab38cSToby Isaac       PetscInt numComp;
6562719ab38cSToby Isaac 
6563719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
6564719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
6565719ab38cSToby Isaac     }
65666995de1eSToby Isaac   }
6567a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
65686995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
65696995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
65706995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
65716995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
6572a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
6573a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
6574a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6575a68b90caSToby Isaac     if (dof) {
6576a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
6577a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
6578a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
6579a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
6580a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
6581a68b90caSToby Isaac       }
6582a68b90caSToby Isaac     }
6583a68b90caSToby Isaac   }
6584a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
6585a68b90caSToby Isaac   PetscFunctionReturn(0);
6586a68b90caSToby Isaac }
6587a68b90caSToby Isaac 
6588a68b90caSToby Isaac #undef __FUNCT__
6589f7c74593SToby Isaac #define __FUNCT__ "DMPlexCreateConstraintMatrix_Anchors"
6590f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
6591a68b90caSToby Isaac {
6592f7c74593SToby Isaac   PetscSection aSec;
65930ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
65940ac89760SToby Isaac   const PetscInt *anchors;
65950ac89760SToby Isaac   PetscInt numFields, f;
659666ad2231SToby Isaac   IS aIS;
65970ac89760SToby Isaac   PetscErrorCode ierr;
65980ac89760SToby Isaac 
65990ac89760SToby Isaac   PetscFunctionBegin;
66000ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66010ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
66020ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
66030ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
66040ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
6605302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
6606a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
660766ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
66086995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
66096995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
66100ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
66110ac89760SToby Isaac   i[0] = 0;
66120ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
66130ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
6614f19733c5SToby Isaac     PetscInt rDof, rOff, r;
6615f19733c5SToby Isaac 
6616f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
6617f19733c5SToby Isaac     if (!rDof) continue;
6618f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
66190ac89760SToby Isaac     if (numFields) {
66200ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
66210ac89760SToby Isaac         annz = 0;
6622f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
6623f19733c5SToby Isaac           a = anchors[rOff + r];
66240ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
66250ac89760SToby Isaac           annz += aDof;
66260ac89760SToby Isaac         }
66270ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
66280ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
66290ac89760SToby Isaac         for (q = 0; q < dof; q++) {
66300ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
66310ac89760SToby Isaac         }
66320ac89760SToby Isaac       }
66330ac89760SToby Isaac     }
66340ac89760SToby Isaac     else {
66350ac89760SToby Isaac       annz = 0;
66360ac89760SToby Isaac       for (q = 0; q < dof; q++) {
66370ac89760SToby Isaac         a = anchors[off + q];
66380ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
66390ac89760SToby Isaac         annz += aDof;
66400ac89760SToby Isaac       }
66410ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
66420ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
66430ac89760SToby Isaac       for (q = 0; q < dof; q++) {
66440ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
66450ac89760SToby Isaac       }
66460ac89760SToby Isaac     }
66470ac89760SToby Isaac   }
66480ac89760SToby Isaac   nnz = i[m];
66490ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
66500ac89760SToby Isaac   offset = 0;
66510ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
66520ac89760SToby Isaac     if (numFields) {
66530ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
66540ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
66550ac89760SToby Isaac         for (q = 0; q < dof; q++) {
66560ac89760SToby Isaac           PetscInt rDof, rOff, r;
66570ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
66580ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
66590ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
66600ac89760SToby Isaac             PetscInt s;
66610ac89760SToby Isaac 
66620ac89760SToby Isaac             a = anchors[rOff + r];
66630ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
66640ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
66650ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
66660ac89760SToby Isaac               j[offset++] = aOff + s;
66670ac89760SToby Isaac             }
66680ac89760SToby Isaac           }
66690ac89760SToby Isaac         }
66700ac89760SToby Isaac       }
66710ac89760SToby Isaac     }
66720ac89760SToby Isaac     else {
66730ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
66740ac89760SToby Isaac       for (q = 0; q < dof; q++) {
66750ac89760SToby Isaac         PetscInt rDof, rOff, r;
66760ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
66770ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
66780ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
66790ac89760SToby Isaac           PetscInt s;
66800ac89760SToby Isaac 
66810ac89760SToby Isaac           a = anchors[rOff + r];
66820ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
66830ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
66840ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
66850ac89760SToby Isaac             j[offset++] = aOff + s;
66860ac89760SToby Isaac           }
66870ac89760SToby Isaac         }
66880ac89760SToby Isaac       }
66890ac89760SToby Isaac     }
66900ac89760SToby Isaac   }
66910ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
669225570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
669325570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
669466ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
66950ac89760SToby Isaac   PetscFunctionReturn(0);
66960ac89760SToby Isaac }
66970ac89760SToby Isaac 
66980ac89760SToby Isaac #undef __FUNCT__
669966ad2231SToby Isaac #define __FUNCT__ "DMCreateDefaultConstraints_Plex"
670066ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
670166ad2231SToby Isaac {
6702f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6703f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
670466ad2231SToby Isaac   Mat            cMat;
670566ad2231SToby Isaac   PetscErrorCode ierr;
670666ad2231SToby Isaac 
670766ad2231SToby Isaac   PetscFunctionBegin;
670866ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6709a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
671066ad2231SToby Isaac   if (anchorSection) {
6711e228b242SToby Isaac     PetscDS  ds;
6712e228b242SToby Isaac     PetscInt nf;
6713e228b242SToby Isaac 
6714f7c74593SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
6715f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
6716f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
6717e228b242SToby Isaac     ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
6718302440fdSBarry Smith     ierr = PetscDSGetNumFields(ds,&nf);CHKERRQ(ierr);
6719e228b242SToby Isaac     if (nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
672066ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
672166ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
672266ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
672366ad2231SToby Isaac   }
672466ad2231SToby Isaac   PetscFunctionReturn(0);
672566ad2231SToby Isaac }
6726