xref: /petsc/src/dm/impls/plex/plex.c (revision 339e3443f6505125db007edb64eadb26c93f388d)
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};
61*339e3443SMatthew G. Knepley   PetscReal          vbound[2], time;
62*339e3443SMatthew G. Knepley   PetscBool          isnull, flg;
63*339e3443SMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N, level, step, nmax = 2;
64e412dcbdSMatthew G. Knepley   const char        *name;
65*339e3443SMatthew G. Knepley   char               title[PETSC_MAX_PATH_LEN];
66e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
67e412dcbdSMatthew G. Knepley 
68e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
69e412dcbdSMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
70e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
71e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
72e412dcbdSMatthew G. Knepley   ierr = DMGetCoarsenLevel(dm, &level);CHKERRQ(ierr);
73e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
74e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
75e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
76e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
77e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
78e412dcbdSMatthew G. Knepley 
79e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
80e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
81e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
82e412dcbdSMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
83*339e3443SMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, &step, &time);CHKERRQ(ierr);
84*339e3443SMatthew G. Knepley   ierr = PetscSNPrintf(title, sizeof(title), "%s Step: %D Time: %.4g", name, step, time);CHKERRQ(ierr);
85*339e3443SMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, title);CHKERRQ(ierr);
86e412dcbdSMatthew G. Knepley 
87e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
88e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
89e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
900c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
910c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
92e412dcbdSMatthew G. Knepley   }
93e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
94e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
95e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
96e412dcbdSMatthew G. Knepley 
97*339e3443SMatthew G. Knepley   ierr = PetscOptionsGetRealArray(NULL, v->hdr.prefix, "-vec_view_bounds", vbound, &nmax, &flg);CHKERRQ(ierr);
98*339e3443SMatthew G. Knepley   if (!flg) {
99*339e3443SMatthew G. Knepley     ierr = VecMin(v, NULL, &vbound[0]);CHKERRQ(ierr);
100*339e3443SMatthew G. Knepley     ierr = VecMax(v, NULL, &vbound[1]);CHKERRQ(ierr);
101*339e3443SMatthew G. Knepley   }
102e412dcbdSMatthew G. Knepley   ierr = PetscDrawGetPopup(draw, &popup);CHKERRQ(ierr);
103*339e3443SMatthew G. Knepley   ierr = PetscDrawScalePopup(popup, vbound[0], vbound[1]);CHKERRQ(ierr);
104e412dcbdSMatthew G. Knepley 
105e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(v, &array);CHKERRQ(ierr);
106e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
10799a2f7bcSMatthew G. Knepley     PetscScalar *coords = NULL, *a = NULL;
108*339e3443SMatthew G. Knepley     PetscInt     numCoords, color[4];
109e412dcbdSMatthew G. Knepley 
110*339e3443SMatthew G. Knepley     ierr = DMPlexPointLocalRead(dm, c, array, &a);CHKERRQ(ierr);
111*339e3443SMatthew G. Knepley     if (a) {
112*339e3443SMatthew G. Knepley       color[0] = PetscDrawRealToColor(PetscRealPart(a[0]), vbound[0], vbound[1]);
113*339e3443SMatthew G. Knepley       color[1] = color[2] = color[3] = color[0];
114*339e3443SMatthew G. Knepley     } else {
115*339e3443SMatthew G. Knepley       PetscScalar *vals = NULL;
116*339e3443SMatthew G. Knepley       PetscInt     numVals, va;
117*339e3443SMatthew G. Knepley 
118*339e3443SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, NULL, v, c, &numVals, &vals);CHKERRQ(ierr);
119*339e3443SMatthew G. Knepley       switch (numVals) {
120*339e3443SMatthew G. Knepley       case 3:
121*339e3443SMatthew G. Knepley       case 4:
122*339e3443SMatthew G. Knepley         for (va = 0; va < numVals; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va]), vbound[0], vbound[1]);
123*339e3443SMatthew G. Knepley         break;
124*339e3443SMatthew G. Knepley       default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %D cannot be handled", numVals);
125*339e3443SMatthew G. Knepley       }
126*339e3443SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, NULL, v, c, &numVals, &vals);CHKERRQ(ierr);
127*339e3443SMatthew G. Knepley     }
128e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
129e412dcbdSMatthew G. Knepley     switch (numCoords) {
130e412dcbdSMatthew G. Knepley     case 6:
131*339e3443SMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), color[0], color[1], color[2]);CHKERRQ(ierr);
132e412dcbdSMatthew G. Knepley       break;
133e412dcbdSMatthew G. Knepley     case 8:
134*339e3443SMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), color[0], color[1], color[2]);CHKERRQ(ierr);
135*339e3443SMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), color[2], color[3], color[0]);CHKERRQ(ierr);
136e412dcbdSMatthew G. Knepley       break;
137e412dcbdSMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
138e412dcbdSMatthew G. Knepley     }
139e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
140e412dcbdSMatthew G. Knepley   }
141e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &array);CHKERRQ(ierr);
142e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
143e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
144e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
145e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
146e412dcbdSMatthew G. Knepley }
147e412dcbdSMatthew G. Knepley 
148e412dcbdSMatthew G. Knepley #undef __FUNCT__
149f13a32a3SMatthew G. Knepley #define __FUNCT__ "VecView_Plex_Local"
150f13a32a3SMatthew G. Knepley PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
151f13a32a3SMatthew G. Knepley {
152f13a32a3SMatthew G. Knepley   DM             dm;
153f13a32a3SMatthew G. Knepley   PetscBool      isvtk, ishdf5, isdraw, isseq;
154f13a32a3SMatthew G. Knepley   PetscErrorCode ierr;
155f13a32a3SMatthew G. Knepley 
156f13a32a3SMatthew G. Knepley   PetscFunctionBegin;
157f13a32a3SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
158f13a32a3SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
159f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
160f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
161f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW, &isdraw);CHKERRQ(ierr);
162f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
163f13a32a3SMatthew G. Knepley   if (isvtk || ishdf5) {
164f13a32a3SMatthew G. Knepley     PetscInt  numFields;
165f13a32a3SMatthew G. Knepley     PetscBool fem = PETSC_FALSE;
166f13a32a3SMatthew G. Knepley 
167f13a32a3SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
168f13a32a3SMatthew G. Knepley     if (numFields) {
169f13a32a3SMatthew G. Knepley       PetscObject fe;
170f13a32a3SMatthew G. Knepley 
171f13a32a3SMatthew G. Knepley       ierr = DMGetField(dm, 0, &fe);CHKERRQ(ierr);
172f13a32a3SMatthew G. Knepley       if (fe->classid == PETSCFE_CLASSID) fem = PETSC_TRUE;
173f13a32a3SMatthew G. Knepley     }
174f13a32a3SMatthew G. Knepley     if (fem) {ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, v, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);}
175f13a32a3SMatthew G. Knepley   }
176f13a32a3SMatthew G. Knepley   if (isvtk) {
177f13a32a3SMatthew G. Knepley     PetscSection            section;
178f13a32a3SMatthew G. Knepley     PetscViewerVTKFieldType ft;
179f13a32a3SMatthew G. Knepley     PetscInt                pStart, pEnd;
180f13a32a3SMatthew G. Knepley 
181f13a32a3SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
182f13a32a3SMatthew G. Knepley     ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
183f13a32a3SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) v);CHKERRQ(ierr);  /* viewer drops reference */
184f13a32a3SMatthew G. Knepley     ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, ft, (PetscObject) v);CHKERRQ(ierr);
185f13a32a3SMatthew G. Knepley   } else if (ishdf5) {
186f13a32a3SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
187f13a32a3SMatthew G. Knepley     ierr = VecView_Plex_Local_HDF5(v, viewer);CHKERRQ(ierr);
188f13a32a3SMatthew G. Knepley #else
189f13a32a3SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
190f13a32a3SMatthew G. Knepley #endif
191f13a32a3SMatthew G. Knepley   } else if (isdraw) {
192f13a32a3SMatthew G. Knepley     ierr = VecView_Plex_Local_Draw(v, viewer);CHKERRQ(ierr);
193f13a32a3SMatthew G. Knepley   } else {
194f13a32a3SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
195f13a32a3SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
196f13a32a3SMatthew G. Knepley   }
197f13a32a3SMatthew G. Knepley   PetscFunctionReturn(0);
198f13a32a3SMatthew G. Knepley }
199f13a32a3SMatthew G. Knepley 
200f13a32a3SMatthew G. Knepley #undef __FUNCT__
201552f7358SJed Brown #define __FUNCT__ "VecView_Plex"
202552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
203552f7358SJed Brown {
204552f7358SJed Brown   DM             dm;
205f13a32a3SMatthew G. Knepley   PetscReal      time = 0.0;
206e412dcbdSMatthew G. Knepley   PetscBool      isvtk, ishdf5, isdraw, isseq;
207552f7358SJed Brown   PetscErrorCode ierr;
208552f7358SJed Brown 
209552f7358SJed Brown   PetscFunctionBegin;
210552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
21182f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
212552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
21333c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
214e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW, &isdraw);CHKERRQ(ierr);
21555f2e967SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
216552f7358SJed Brown   if (isvtk) {
217552f7358SJed Brown     Vec         locv;
218552f7358SJed Brown     const char *name;
219552f7358SJed Brown 
220552f7358SJed Brown     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
221552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
222552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
223552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
224552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
225cf9ba882SMatthew G. Knepley     ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
226cf9ba882SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
227552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
228552f7358SJed Brown     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
229b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
230b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
231b136c2c9SMatthew G. Knepley     ierr = VecView_Plex_HDF5(v, viewer);CHKERRQ(ierr);
232b136c2c9SMatthew G. Knepley #else
233b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
234b136c2c9SMatthew G. Knepley #endif
235e412dcbdSMatthew G. Knepley   } else if (isdraw) {
236f13a32a3SMatthew G. Knepley     Vec         locv;
237f13a32a3SMatthew G. Knepley     const char *name;
238f13a32a3SMatthew G. Knepley 
239f13a32a3SMatthew G. Knepley     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
240f13a32a3SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
241f13a32a3SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
242f13a32a3SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
243f13a32a3SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
244f13a32a3SMatthew G. Knepley     ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
245f13a32a3SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
246f13a32a3SMatthew G. Knepley     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
247f13a32a3SMatthew G. Knepley     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
248552f7358SJed Brown   } else {
24955f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
25055f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
251552f7358SJed Brown   }
252552f7358SJed Brown   PetscFunctionReturn(0);
253552f7358SJed Brown }
254552f7358SJed Brown 
255552f7358SJed Brown #undef __FUNCT__
256d930f514SMatthew G. Knepley #define __FUNCT__ "VecView_Plex_Native"
257d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
258d930f514SMatthew G. Knepley {
259d930f514SMatthew G. Knepley   DM                dm;
260d930f514SMatthew G. Knepley   MPI_Comm          comm;
261d930f514SMatthew G. Knepley   PetscViewerFormat format;
262d930f514SMatthew G. Knepley   Vec               v;
263d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
264d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
265d930f514SMatthew G. Knepley 
266d930f514SMatthew G. Knepley   PetscFunctionBegin;
267d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
268d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
2692c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
270d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
271d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
272d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
273d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
274d930f514SMatthew G. Knepley     const char *vecname;
275d930f514SMatthew G. Knepley     PetscInt    n, nroots;
276d930f514SMatthew G. Knepley 
277d930f514SMatthew G. Knepley     if (dm->sfNatural) {
278ca43db0aSBarry Smith       ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
279d930f514SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
280d930f514SMatthew G. Knepley       if (n == nroots) {
281d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
282d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
283d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
284d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
285d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
286d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
287d930f514SMatthew G. Knepley     } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
288d930f514SMatthew G. Knepley   } else {
289d930f514SMatthew G. Knepley     /* we are viewing a natural DMPlex vec. */
290d930f514SMatthew G. Knepley     v = originalv;
291d930f514SMatthew G. Knepley   }
292d930f514SMatthew G. Knepley   if (ishdf5) {
293d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
294d930f514SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native(v, viewer);CHKERRQ(ierr);
295d930f514SMatthew G. Knepley #else
296d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
297d930f514SMatthew G. Knepley #endif
298d930f514SMatthew G. Knepley   } else if (isvtk) {
299d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
300d930f514SMatthew G. Knepley   } else {
301d930f514SMatthew G. Knepley     PetscBool isseq;
302d930f514SMatthew G. Knepley 
303d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
304d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
305d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
306d930f514SMatthew G. Knepley   }
307d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
308d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
309d930f514SMatthew G. Knepley }
310d930f514SMatthew G. Knepley 
311d930f514SMatthew G. Knepley #undef __FUNCT__
3122c40f234SMatthew G. Knepley #define __FUNCT__ "VecLoad_Plex_Local"
3132c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
3142c40f234SMatthew G. Knepley {
3152c40f234SMatthew G. Knepley   DM             dm;
3162c40f234SMatthew G. Knepley   PetscBool      ishdf5;
3172c40f234SMatthew G. Knepley   PetscErrorCode ierr;
3182c40f234SMatthew G. Knepley 
3192c40f234SMatthew G. Knepley   PetscFunctionBegin;
3202c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
3212c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
3222c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
3232c40f234SMatthew G. Knepley   if (ishdf5) {
3242c40f234SMatthew G. Knepley     DM          dmBC;
3252c40f234SMatthew G. Knepley     Vec         gv;
3262c40f234SMatthew G. Knepley     const char *name;
3272c40f234SMatthew G. Knepley 
3282c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
3292c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
3302c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
3312c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
3322c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
3332c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
3342c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
3352c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
3362c40f234SMatthew G. Knepley   } else {
3372c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
3382c40f234SMatthew G. Knepley   }
3392c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
3402c40f234SMatthew G. Knepley }
3412c40f234SMatthew G. Knepley 
3422c40f234SMatthew G. Knepley #undef __FUNCT__
3432c40f234SMatthew G. Knepley #define __FUNCT__ "VecLoad_Plex"
3442c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
3452c40f234SMatthew G. Knepley {
3462c40f234SMatthew G. Knepley   DM             dm;
3472c40f234SMatthew G. Knepley   PetscBool      ishdf5;
3482c40f234SMatthew G. Knepley   PetscErrorCode ierr;
3492c40f234SMatthew G. Knepley 
3502c40f234SMatthew G. Knepley   PetscFunctionBegin;
3512c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
3522c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
3532c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
3542c40f234SMatthew G. Knepley   if (ishdf5) {
355878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
356b136c2c9SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5(v, viewer);CHKERRQ(ierr);
357b136c2c9SMatthew G. Knepley #else
358b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
359878b459fSMatthew G. Knepley #endif
3602c40f234SMatthew G. Knepley   } else {
3612c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
362552f7358SJed Brown   }
363552f7358SJed Brown   PetscFunctionReturn(0);
364552f7358SJed Brown }
365552f7358SJed Brown 
366552f7358SJed Brown #undef __FUNCT__
367d930f514SMatthew G. Knepley #define __FUNCT__ "VecLoad_Plex_Native"
368d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
369d930f514SMatthew G. Knepley {
370d930f514SMatthew G. Knepley   DM                dm;
371d930f514SMatthew G. Knepley   PetscViewerFormat format;
372d930f514SMatthew G. Knepley   PetscBool         ishdf5;
373d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
374d930f514SMatthew G. Knepley 
375d930f514SMatthew G. Knepley   PetscFunctionBegin;
376d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
377d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
378d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
379d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
380d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
381d930f514SMatthew G. Knepley     if (dm->sfNatural) {
382d930f514SMatthew G. Knepley       if (ishdf5) {
383d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
384d930f514SMatthew G. Knepley         Vec         v;
385d930f514SMatthew G. Knepley         const char *vecname;
386d930f514SMatthew G. Knepley 
387d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
388d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
389d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
390d930f514SMatthew G. Knepley         ierr = VecLoad_Plex_HDF5_Native(v, viewer);CHKERRQ(ierr);
391d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
392d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
393d930f514SMatthew G. Knepley         ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
394d930f514SMatthew G. Knepley #else
395d930f514SMatthew G. Knepley         SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
396d930f514SMatthew G. Knepley #endif
397d930f514SMatthew G. Knepley       } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
398d930f514SMatthew G. Knepley     }
399d930f514SMatthew G. Knepley   }
400d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
401d930f514SMatthew G. Knepley }
402d930f514SMatthew G. Knepley 
403d930f514SMatthew G. Knepley #undef __FUNCT__
404731e8ddeSMatthew G. Knepley #define __FUNCT__ "DMPlexView_Ascii_Geometry"
405731e8ddeSMatthew G. Knepley PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
406731e8ddeSMatthew G. Knepley {
407731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
408731e8ddeSMatthew G. Knepley   Vec                coordinates;
409731e8ddeSMatthew G. Knepley   DMLabel            depthLabel;
410731e8ddeSMatthew G. Knepley   const char        *name[4];
411731e8ddeSMatthew G. Knepley   const PetscScalar *a;
412731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
413731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
414731e8ddeSMatthew G. Knepley 
415731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
416731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
417731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
418731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
419731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
420731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
421731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
422731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
423731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
424731e8ddeSMatthew G. Knepley   name[1]     = "edge";
425731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
426731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
427731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
428731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
429731e8ddeSMatthew G. Knepley     PetscInt  closureSize, cl;
430731e8ddeSMatthew G. Knepley 
431f347f43bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D:\n", c);CHKERRQ(ierr);
432731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
433731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
434731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
435731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
436731e8ddeSMatthew G. Knepley 
437731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
438731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
439731e8ddeSMatthew G. Knepley       if (!dof) continue;
440731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
441731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
442f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
443731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
444731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
445731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
446731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
44741477eefSMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
448731e8ddeSMatthew G. Knepley         }
449731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
450731e8ddeSMatthew G. Knepley       }
451731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
452731e8ddeSMatthew G. Knepley     }
453731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
454731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
455731e8ddeSMatthew G. Knepley   }
456731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
457731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
458731e8ddeSMatthew G. Knepley }
459731e8ddeSMatthew G. Knepley 
460731e8ddeSMatthew G. Knepley #undef __FUNCT__
461552f7358SJed Brown #define __FUNCT__ "DMPlexView_Ascii"
462552f7358SJed Brown PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
463552f7358SJed Brown {
464552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
465552f7358SJed Brown   DM                cdm;
466552f7358SJed Brown   DMLabel           markers;
467552f7358SJed Brown   PetscSection      coordSection;
468552f7358SJed Brown   Vec               coordinates;
469552f7358SJed Brown   PetscViewerFormat format;
470552f7358SJed Brown   PetscErrorCode    ierr;
471552f7358SJed Brown 
472552f7358SJed Brown   PetscFunctionBegin;
473552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
474552f7358SJed Brown   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
475552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
476552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
477552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
478552f7358SJed Brown     const char *name;
479552f7358SJed Brown     PetscInt    maxConeSize, maxSupportSize;
480552f7358SJed Brown     PetscInt    pStart, pEnd, p;
481552f7358SJed Brown     PetscMPIInt rank, size;
482552f7358SJed Brown 
48382f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
48482f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
485552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
486552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
487552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
488552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "Mesh '%s':\n", name);CHKERRQ(ierr);
489552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "orientation is missing\n", name);CHKERRQ(ierr);
490552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "cap --> base:\n", name);CHKERRQ(ierr);
4914d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
4924d4c343aSBarry Smith     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max sizes cone: %D support: %D\n", rank,maxConeSize, maxSupportSize);CHKERRQ(ierr);
493552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
494552f7358SJed Brown       PetscInt dof, off, s;
495552f7358SJed Brown 
496552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
497552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
498552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
499e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
500552f7358SJed Brown       }
501552f7358SJed Brown     }
502552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
503552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "base <-- cap:\n", name);CHKERRQ(ierr);
504552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
505552f7358SJed Brown       PetscInt dof, off, c;
506552f7358SJed Brown 
507552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
508552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
509552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
510e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
511552f7358SJed Brown       }
512552f7358SJed Brown     }
513552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
5144d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
5150298fd71SBarry Smith     ierr = PetscSectionGetChart(coordSection, &pStart, NULL);CHKERRQ(ierr);
516552f7358SJed Brown     if (pStart >= 0) {ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);}
517c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
518552f7358SJed Brown     ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);
519552f7358SJed Brown     if (size > 1) {
520552f7358SJed Brown       PetscSF sf;
521552f7358SJed Brown 
522552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
523552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
524552f7358SJed Brown     }
525552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
526552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
5270588280cSMatthew G. Knepley     const char  *name, *color;
5280588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
5290588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
530552f7358SJed Brown     PetscReal    scale         = 2.0;
5310588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
5320588280cSMatthew G. Knepley     double       tcoords[3];
533552f7358SJed Brown     PetscScalar *coords;
5340588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
535552f7358SJed Brown     PetscMPIInt  rank, size;
5360588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
537552f7358SJed Brown 
5380588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
539552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
540c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
5410588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
5420588280cSMatthew G. Knepley     numColors  = 10;
5430588280cSMatthew G. Knepley     numLColors = 10;
5440588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
545c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
546c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
547c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
5480588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
549c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
5500588280cSMatthew G. Knepley     if (!useColors) {
5510588280cSMatthew G. Knepley       numColors = 3;
5520588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
5530588280cSMatthew G. Knepley     }
554c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
5550588280cSMatthew G. Knepley     if (!useColors) {
5560588280cSMatthew G. Knepley       numLColors = 4;
5570588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
5580588280cSMatthew G. Knepley     }
55982f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
56082f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
561552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
562770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
5630588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
564552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
565552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
566552f7358SJed Brown \\usetikzlibrary{arrows}\n\
5670588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
5680588280cSMatthew G. Knepley     if (size > 1) {
569f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
570770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
571770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
572770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
573770b213bSMatthew G Knepley         } else if (p > 0) {
574770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
575770b213bSMatthew G Knepley         }
576770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
577770b213bSMatthew G Knepley       }
5780588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
5790588280cSMatthew G. Knepley     }
5800588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", 1.0);CHKERRQ(ierr);
581552f7358SJed Brown     /* Plot vertices */
582552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
583552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
5844d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
585552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
586552f7358SJed Brown       PetscInt  off, dof, d;
5870588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
588552f7358SJed Brown 
589552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
590552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
5910588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
592f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
5930588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
5940588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
5950588280cSMatthew G. Knepley         tcoords[d] = PetscAbsReal(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
5960588280cSMatthew G. Knepley       }
5970588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
5980588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
599552f7358SJed Brown       for (d = 0; d < dof; ++d) {
600552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
6010588280cSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]);CHKERRQ(ierr);
602552f7358SJed Brown       }
6030588280cSMatthew G. Knepley       color = colors[rank%numColors];
6040588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
6050588280cSMatthew G. Knepley         PetscInt val;
606c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
6070588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
6080588280cSMatthew G. Knepley       }
6090588280cSMatthew G. Knepley       if (useNumbers) {
610e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
6110588280cSMatthew G. Knepley       } else {
612e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
6130588280cSMatthew G. Knepley       }
614552f7358SJed Brown     }
615552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
616552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
617552f7358SJed Brown     /* Plot edges */
6180588280cSMatthew G. Knepley     if (depth > 1) {ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);}
6190588280cSMatthew G. Knepley     if (dim < 3 && useNumbers) {
620552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
621552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
622552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
623552f7358SJed Brown         const PetscInt *cone;
624552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
625552f7358SJed Brown 
626552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
627f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
628552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
629552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
630552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
631552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
632552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
633552f7358SJed Brown         for (d = 0; d < dof; ++d) {
6340588280cSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
6350588280cSMatthew G. Knepley           tcoords[d] = PetscAbsReal(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
636552f7358SJed Brown         }
6370588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
6380588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
6390588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
6400588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
641f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
6420588280cSMatthew G. Knepley         }
6430588280cSMatthew G. Knepley         color = colors[rank%numColors];
6440588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
6450588280cSMatthew G. Knepley           PetscInt val;
646c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
6470750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
6480588280cSMatthew G. Knepley         }
649e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
650552f7358SJed Brown       }
651552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
652552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
653552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
6540588280cSMatthew G. Knepley     }
655552f7358SJed Brown     /* Plot cells */
6560588280cSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
6570588280cSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
6580588280cSMatthew G. Knepley         const PetscInt *cone;
6590588280cSMatthew G. Knepley 
6600588280cSMatthew G. Knepley         color = colors[rank%numColors];
6610588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
6620588280cSMatthew G. Knepley           PetscInt val;
663c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
6640588280cSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
6650588280cSMatthew G. Knepley         }
6660588280cSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
667e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
6680588280cSMatthew G. Knepley       }
6690588280cSMatthew G. Knepley     } else {
670552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
671552f7358SJed Brown       for (c = cStart; c < cEnd; ++c) {
6720298fd71SBarry Smith         PetscInt *closure = NULL;
673552f7358SJed Brown         PetscInt  closureSize, firstPoint = -1;
674552f7358SJed Brown 
675552f7358SJed Brown         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
676552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
677552f7358SJed Brown         for (p = 0; p < closureSize*2; p += 2) {
678552f7358SJed Brown           const PetscInt point = closure[p];
679552f7358SJed Brown 
680552f7358SJed Brown           if ((point < vStart) || (point >= vEnd)) continue;
681552f7358SJed Brown           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
682e4b003c7SBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
683552f7358SJed Brown           if (firstPoint < 0) firstPoint = point;
684552f7358SJed Brown         }
685552f7358SJed Brown         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
686e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
687552f7358SJed Brown         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
688552f7358SJed Brown       }
6890588280cSMatthew G. Knepley     }
690552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6914d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
6920588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
693770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
6940588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
6950588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
6960588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
6970588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
698552f7358SJed Brown   } else {
69982f516ccSBarry Smith     MPI_Comm    comm;
700834065abSMatthew G. Knepley     PetscInt   *sizes, *hybsizes;
701834065abSMatthew G. Knepley     PetscInt    locDepth, depth, dim, d, pMax[4];
702552f7358SJed Brown     PetscInt    pStart, pEnd, p;
703a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
7041143a3c0SMatthew G. Knepley     const char *name;
705552f7358SJed Brown     PetscMPIInt size;
706552f7358SJed Brown 
70782f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
708552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
709c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7105f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
7111143a3c0SMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimensions:\n", name, dim);CHKERRQ(ierr);}
7121143a3c0SMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimensions:\n", dim);CHKERRQ(ierr);}
713552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
714b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
715bb81ee50SMatthew G. Knepley     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, &pMax[1], &pMax[0]);CHKERRQ(ierr);
716dcca6d9dSJed Brown     ierr = PetscMalloc2(size,&sizes,size,&hybsizes);CHKERRQ(ierr);
717552f7358SJed Brown     if (depth == 1) {
718552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
719552f7358SJed Brown       pEnd = pEnd - pStart;
720552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
721f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);CHKERRQ(ierr);
722552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
723552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
724552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
725552f7358SJed Brown       pEnd = pEnd - pStart;
726552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
727552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
728552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
729552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
730552f7358SJed Brown     } else {
731cbb7f117SMark Adams       PetscMPIInt rank;
732cbb7f117SMark Adams       ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
733552f7358SJed Brown       for (d = 0; d <= dim; d++) {
734552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
735834065abSMatthew G. Knepley         pEnd    -= pStart;
736834065abSMatthew G. Knepley         pMax[d] -= pStart;
737552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
738834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
739552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
740834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
741cbb7f117SMark Adams           if (!rank) {
742834065abSMatthew G. Knepley             if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
743834065abSMatthew G. Knepley             else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
744834065abSMatthew G. Knepley           }
745cbb7f117SMark Adams         }
746552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
747552f7358SJed Brown       }
748552f7358SJed Brown     }
749834065abSMatthew G. Knepley     ierr = PetscFree2(sizes,hybsizes);CHKERRQ(ierr);
750c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
751a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
752a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
753a57dd577SMatthew G Knepley       DMLabel         label;
754a57dd577SMatthew G Knepley       const char     *name;
755a57dd577SMatthew G Knepley       IS              valueIS;
756a57dd577SMatthew G Knepley       const PetscInt *values;
757a57dd577SMatthew G Knepley       PetscInt        numValues, v;
758a57dd577SMatthew G Knepley 
759c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
760c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
761a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
762f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata of sizes (", name, numValues);CHKERRQ(ierr);
763a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
764a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
765120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
766a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
767a57dd577SMatthew G Knepley         PetscInt size;
768a57dd577SMatthew G Knepley 
769a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
770a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
771f347f43bSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer, "%D", size);CHKERRQ(ierr);
772a57dd577SMatthew G Knepley       }
773a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
774120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
775a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
7764d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
777a57dd577SMatthew G Knepley     }
778a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
7798e7ff633SMatthew G. Knepley     if (cdm) {
7808e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
7818e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
7828e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
7838e7ff633SMatthew G. Knepley     }
784552f7358SJed Brown   }
785552f7358SJed Brown   PetscFunctionReturn(0);
786552f7358SJed Brown }
787552f7358SJed Brown 
788552f7358SJed Brown #undef __FUNCT__
789e412dcbdSMatthew G. Knepley #define __FUNCT__ "DMPlexView_Draw"
790e412dcbdSMatthew G. Knepley PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
791e412dcbdSMatthew G. Knepley {
792e412dcbdSMatthew G. Knepley   PetscDraw          draw;
793e412dcbdSMatthew G. Knepley   DM                 cdm;
794e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
795e412dcbdSMatthew G. Knepley   Vec                coordinates;
796e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
797e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
798e412dcbdSMatthew G. Knepley   PetscBool          isnull;
799e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
800e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
801e412dcbdSMatthew G. Knepley 
802e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
803e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
804e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
805e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
806e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
807e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
808e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
809e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
810e412dcbdSMatthew G. Knepley 
811e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
812e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
813e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
814e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
815e412dcbdSMatthew G. Knepley 
816e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
817e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
818e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
8190c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
8200c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
821e412dcbdSMatthew G. Knepley   }
822e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
823e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
824e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
825e412dcbdSMatthew G. Knepley 
826e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
827e412dcbdSMatthew G. Knepley     PetscScalar *coords = NULL;
828e412dcbdSMatthew G. Knepley     PetscInt     numCoords;
829e412dcbdSMatthew G. Knepley 
830e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
831e412dcbdSMatthew G. Knepley     switch (numCoords) {
832e412dcbdSMatthew G. Knepley     case 6:
8330c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8340c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8350c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
836e412dcbdSMatthew G. Knepley       break;
837e412dcbdSMatthew G. Knepley     case 8:
8380c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8390c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8400c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8410c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
842e412dcbdSMatthew G. Knepley       break;
843e412dcbdSMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
844e412dcbdSMatthew G. Knepley     }
845e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
846e412dcbdSMatthew G. Knepley   }
847e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
848e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
849e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
850e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
851e412dcbdSMatthew G. Knepley }
852e412dcbdSMatthew G. Knepley 
853e412dcbdSMatthew G. Knepley #undef __FUNCT__
854552f7358SJed Brown #define __FUNCT__ "DMView_Plex"
855552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
856552f7358SJed Brown {
857e412dcbdSMatthew G. Knepley   PetscBool      iascii, ishdf5, isvtk, isdraw;
858552f7358SJed Brown   PetscErrorCode ierr;
859552f7358SJed Brown 
860552f7358SJed Brown   PetscFunctionBegin;
861552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
862552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
863552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
864fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
865c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
866e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
867552f7358SJed Brown   if (iascii) {
868552f7358SJed Brown     ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
869c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
870c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
8717afe7537SMatthew G. Knepley     ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_VIZ);CHKERRQ(ierr);
872c6ccd67eSMatthew G. Knepley     ierr = DMPlexView_HDF5(dm, viewer);CHKERRQ(ierr);
8737afe7537SMatthew G. Knepley     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
874c6ccd67eSMatthew G. Knepley #else
875c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
876552f7358SJed Brown #endif
877e412dcbdSMatthew G. Knepley   } else if (isvtk) {
878fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
879e412dcbdSMatthew G. Knepley   } else if (isdraw) {
880e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
881fcf6c8fdSToby Isaac   }
882552f7358SJed Brown   PetscFunctionReturn(0);
883552f7358SJed Brown }
884552f7358SJed Brown 
8852c40f234SMatthew G. Knepley #undef __FUNCT__
8862c40f234SMatthew G. Knepley #define __FUNCT__ "DMLoad_Plex"
8872c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
8882c40f234SMatthew G. Knepley {
8892c40f234SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
8902c40f234SMatthew G. Knepley   PetscErrorCode ierr;
8912c40f234SMatthew G. Knepley 
8922c40f234SMatthew G. Knepley   PetscFunctionBegin;
8932c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8942c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
8952c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERBINARY, &isbinary);CHKERRQ(ierr);
8962c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
8972c40f234SMatthew G. Knepley   if (isbinary) {SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Do not yet support binary viewers");}
8982c40f234SMatthew G. Knepley   else if (ishdf5) {
8992c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
90010b7db4eSMatthew G. Knepley     ierr = DMPlexLoad_HDF5(dm, viewer);CHKERRQ(ierr);
9012c40f234SMatthew G. Knepley #else
9022c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
903552f7358SJed Brown #endif
904552f7358SJed Brown   }
905552f7358SJed Brown   PetscFunctionReturn(0);
906552f7358SJed Brown }
907552f7358SJed Brown 
9086c73c22cSMatthew G. Knepley 
9096c73c22cSMatthew G. Knepley #undef __FUNCT__
910552f7358SJed Brown #define __FUNCT__ "DMDestroy_Plex"
911552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
912552f7358SJed Brown {
913552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
914552f7358SJed Brown   PetscErrorCode ierr;
915552f7358SJed Brown 
916552f7358SJed Brown   PetscFunctionBegin;
9170d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
918552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
919552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
920552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
921552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
922be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
923552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
924552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
925d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
926d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
92777623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
928a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
929552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
930552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
931a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
932a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
933d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
934d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
935d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
936d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
937d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
938d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
939fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
940552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
941552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
942713918a9SToby Isaac   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMAdaptLabel_C",NULL);CHKERRQ(ierr);
943552f7358SJed Brown   PetscFunctionReturn(0);
944552f7358SJed Brown }
945552f7358SJed Brown 
946552f7358SJed Brown #undef __FUNCT__
947552f7358SJed Brown #define __FUNCT__ "DMCreateMatrix_Plex"
948b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
949552f7358SJed Brown {
9508d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
951acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
952552f7358SJed Brown   PetscInt               localSize;
953837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
954552f7358SJed Brown   PetscErrorCode         ierr;
955b412c318SBarry Smith   MatType                mtype;
9561428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
957552f7358SJed Brown 
958552f7358SJed Brown   PetscFunctionBegin;
959607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
960b412c318SBarry Smith   mtype = dm->mattype;
961552f7358SJed Brown   ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
962552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
963552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
96482f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
965552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
966552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
967552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
968acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
969acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
970552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
971552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
972552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
973552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
974552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
975552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
976552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
977837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
978552f7358SJed Brown   if (!isShell) {
979be36d101SStefano Zampini     PetscSection subSection;
980837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
981be36d101SStefano Zampini     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal, bsMax, bsMin, *ltogidx, lsize;
982fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
983552f7358SJed Brown 
98400140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
985be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
986be36d101SStefano Zampini       PetscSection section;
987be36d101SStefano Zampini       PetscInt     size;
988be36d101SStefano Zampini 
989be36d101SStefano Zampini       ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
990be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
991be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
992be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
993be36d101SStefano Zampini     } else {
99400140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
995be36d101SStefano Zampini     }
996552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
997be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
998a9d99c84SMatthew G. Knepley       PetscInt bdof;
999a9d99c84SMatthew G. Knepley 
1000552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1001fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
10021d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
10031d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
10041d17a0a3SMatthew G. Knepley       if (dof) {
10051d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1006be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1007be36d101SStefano Zampini       }
1008be36d101SStefano Zampini       if (isMatIS) {
1009be36d101SStefano Zampini         PetscInt loff,c,off;
1010be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1011be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1012be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1013552f7358SJed Brown       }
10142a28c762SMatthew G Knepley     }
10152a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
10162a28c762SMatthew G Knepley     bsLocal = bs;
1017b2566f29SBarry Smith     ierr = MPIU_Allreduce(&bsLocal, &bsMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
10182a28c762SMatthew G Knepley     bsLocal = bs < 0 ? bsMax : bs;
1019b2566f29SBarry Smith     ierr = MPIU_Allreduce(&bsLocal, &bsMin, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1020bff27382SMatthew G. Knepley     if (bsMin != bsMax) {bs = 1;}
1021bff27382SMatthew G. Knepley     else                {bs = bsMax;}
10224fa26246SMatthew G. Knepley     bs   = bs < 0 ? 1 : bs;
1023be36d101SStefano Zampini     if (isMatIS) {
10244fa26246SMatthew G. Knepley       PetscInt l;
10254fa26246SMatthew G. Knepley       /* Must reduce indices by blocksize */
10264fa26246SMatthew G. Knepley       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] /= bs;
10274fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1028be36d101SStefano Zampini     }
1029be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1030be36d101SStefano Zampini     if (isMatIS) {
1031be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1032be36d101SStefano Zampini     }
10331795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
10348d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1035552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1036552f7358SJed Brown   }
1037b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1038552f7358SJed Brown   PetscFunctionReturn(0);
1039552f7358SJed Brown }
1040552f7358SJed Brown 
1041552f7358SJed Brown #undef __FUNCT__
1042be36d101SStefano Zampini #define __FUNCT__ "DMPlexGetSubdomainSection"
1043559da874SStefano Zampini /*
1044be36d101SStefano Zampini   DMPlexGetSubdomainGlobalSection - Returns the section associated with the subdomain
1045be36d101SStefano Zampini 
1046be36d101SStefano Zampini   Not collective
1047be36d101SStefano Zampini 
1048be36d101SStefano Zampini   Input Parameter:
1049be36d101SStefano Zampini . mesh - The DMPlex
1050be36d101SStefano Zampini 
1051be36d101SStefano Zampini   Output Parameters:
1052be36d101SStefano Zampini . subsection - The subdomain section
1053be36d101SStefano Zampini 
1054be36d101SStefano Zampini   Level: developer
1055be36d101SStefano Zampini 
1056be36d101SStefano Zampini .seealso:
1057559da874SStefano Zampini */
1058be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1059be36d101SStefano Zampini {
1060be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1061be36d101SStefano Zampini   PetscErrorCode ierr;
1062be36d101SStefano Zampini 
1063be36d101SStefano Zampini   PetscFunctionBegin;
1064be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1065be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1066be36d101SStefano Zampini     PetscSection section;
1067be36d101SStefano Zampini     PetscSF      sf;
1068be36d101SStefano Zampini 
1069be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
1070be36d101SStefano Zampini     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
1071be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1072be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1073be36d101SStefano Zampini   }
1074be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1075be36d101SStefano Zampini   PetscFunctionReturn(0);
1076be36d101SStefano Zampini }
1077be36d101SStefano Zampini 
1078be36d101SStefano Zampini #undef __FUNCT__
1079552f7358SJed Brown #define __FUNCT__ "DMPlexGetChart"
1080552f7358SJed Brown /*@
1081552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1082552f7358SJed Brown 
1083552f7358SJed Brown   Not collective
1084552f7358SJed Brown 
1085552f7358SJed Brown   Input Parameter:
1086552f7358SJed Brown . mesh - The DMPlex
1087552f7358SJed Brown 
1088552f7358SJed Brown   Output Parameters:
1089552f7358SJed Brown + pStart - The first mesh point
1090552f7358SJed Brown - pEnd   - The upper bound for mesh points
1091552f7358SJed Brown 
1092552f7358SJed Brown   Level: beginner
1093552f7358SJed Brown 
1094552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1095552f7358SJed Brown @*/
1096552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1097552f7358SJed Brown {
1098552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1099552f7358SJed Brown   PetscErrorCode ierr;
1100552f7358SJed Brown 
1101552f7358SJed Brown   PetscFunctionBegin;
1102552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1103552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1104552f7358SJed Brown   PetscFunctionReturn(0);
1105552f7358SJed Brown }
1106552f7358SJed Brown 
1107552f7358SJed Brown #undef __FUNCT__
1108552f7358SJed Brown #define __FUNCT__ "DMPlexSetChart"
1109552f7358SJed Brown /*@
1110552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1111552f7358SJed Brown 
1112552f7358SJed Brown   Not collective
1113552f7358SJed Brown 
1114552f7358SJed Brown   Input Parameters:
1115552f7358SJed Brown + mesh - The DMPlex
1116552f7358SJed Brown . pStart - The first mesh point
1117552f7358SJed Brown - pEnd   - The upper bound for mesh points
1118552f7358SJed Brown 
1119552f7358SJed Brown   Output Parameters:
1120552f7358SJed Brown 
1121552f7358SJed Brown   Level: beginner
1122552f7358SJed Brown 
1123552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1124552f7358SJed Brown @*/
1125552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1126552f7358SJed Brown {
1127552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1128552f7358SJed Brown   PetscErrorCode ierr;
1129552f7358SJed Brown 
1130552f7358SJed Brown   PetscFunctionBegin;
1131552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1132552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1133552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1134552f7358SJed Brown   PetscFunctionReturn(0);
1135552f7358SJed Brown }
1136552f7358SJed Brown 
1137552f7358SJed Brown #undef __FUNCT__
1138552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeSize"
1139552f7358SJed Brown /*@
1140552f7358SJed Brown   DMPlexGetConeSize - Return the number of in-edges for this point in the Sieve DAG
1141552f7358SJed Brown 
1142552f7358SJed Brown   Not collective
1143552f7358SJed Brown 
1144552f7358SJed Brown   Input Parameters:
1145552f7358SJed Brown + mesh - The DMPlex
1146552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1147552f7358SJed Brown 
1148552f7358SJed Brown   Output Parameter:
1149552f7358SJed Brown . size - The cone size for point p
1150552f7358SJed Brown 
1151552f7358SJed Brown   Level: beginner
1152552f7358SJed Brown 
1153552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1154552f7358SJed Brown @*/
1155552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1156552f7358SJed Brown {
1157552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1158552f7358SJed Brown   PetscErrorCode ierr;
1159552f7358SJed Brown 
1160552f7358SJed Brown   PetscFunctionBegin;
1161552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1162552f7358SJed Brown   PetscValidPointer(size, 3);
1163552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1164552f7358SJed Brown   PetscFunctionReturn(0);
1165552f7358SJed Brown }
1166552f7358SJed Brown 
1167552f7358SJed Brown #undef __FUNCT__
1168552f7358SJed Brown #define __FUNCT__ "DMPlexSetConeSize"
1169552f7358SJed Brown /*@
1170552f7358SJed Brown   DMPlexSetConeSize - Set the number of in-edges for this point in the Sieve DAG
1171552f7358SJed Brown 
1172552f7358SJed Brown   Not collective
1173552f7358SJed Brown 
1174552f7358SJed Brown   Input Parameters:
1175552f7358SJed Brown + mesh - The DMPlex
1176552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1177552f7358SJed Brown - size - The cone size for point p
1178552f7358SJed Brown 
1179552f7358SJed Brown   Output Parameter:
1180552f7358SJed Brown 
1181552f7358SJed Brown   Note:
1182552f7358SJed Brown   This should be called after DMPlexSetChart().
1183552f7358SJed Brown 
1184552f7358SJed Brown   Level: beginner
1185552f7358SJed Brown 
1186552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1187552f7358SJed Brown @*/
1188552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1189552f7358SJed Brown {
1190552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1191552f7358SJed Brown   PetscErrorCode ierr;
1192552f7358SJed Brown 
1193552f7358SJed Brown   PetscFunctionBegin;
1194552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1195552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
11960d644c17SKarl Rupp 
1197552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1198552f7358SJed Brown   PetscFunctionReturn(0);
1199552f7358SJed Brown }
1200552f7358SJed Brown 
1201552f7358SJed Brown #undef __FUNCT__
1202f5a469b9SMatthew G. Knepley #define __FUNCT__ "DMPlexAddConeSize"
1203f5a469b9SMatthew G. Knepley /*@
1204f5a469b9SMatthew G. Knepley   DMPlexAddConeSize - Add the given number of in-edges to this point in the Sieve DAG
1205f5a469b9SMatthew G. Knepley 
1206f5a469b9SMatthew G. Knepley   Not collective
1207f5a469b9SMatthew G. Knepley 
1208f5a469b9SMatthew G. Knepley   Input Parameters:
1209f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1210f5a469b9SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1211f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1212f5a469b9SMatthew G. Knepley 
1213f5a469b9SMatthew G. Knepley   Output Parameter:
1214f5a469b9SMatthew G. Knepley 
1215f5a469b9SMatthew G. Knepley   Note:
1216f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1217f5a469b9SMatthew G. Knepley 
1218f5a469b9SMatthew G. Knepley   Level: beginner
1219f5a469b9SMatthew G. Knepley 
1220f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1221f5a469b9SMatthew G. Knepley @*/
1222f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1223f5a469b9SMatthew G. Knepley {
1224f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1225f5a469b9SMatthew G. Knepley   PetscInt       csize;
1226f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1227f5a469b9SMatthew G. Knepley 
1228f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1229f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1230f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1231f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1232f5a469b9SMatthew G. Knepley 
1233f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1234f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1235f5a469b9SMatthew G. Knepley }
1236f5a469b9SMatthew G. Knepley 
1237f5a469b9SMatthew G. Knepley #undef __FUNCT__
1238552f7358SJed Brown #define __FUNCT__ "DMPlexGetCone"
1239552f7358SJed Brown /*@C
1240552f7358SJed Brown   DMPlexGetCone - Return the points on the in-edges for this point in the Sieve DAG
1241552f7358SJed Brown 
1242552f7358SJed Brown   Not collective
1243552f7358SJed Brown 
1244552f7358SJed Brown   Input Parameters:
1245552f7358SJed Brown + mesh - The DMPlex
1246552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1247552f7358SJed Brown 
1248552f7358SJed Brown   Output Parameter:
1249552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1250552f7358SJed Brown 
1251552f7358SJed Brown   Level: beginner
1252552f7358SJed Brown 
12533813dfbdSMatthew G Knepley   Fortran Notes:
12543813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
12553813dfbdSMatthew G Knepley   include petsc.h90 in your code.
12563813dfbdSMatthew G Knepley 
12573813dfbdSMatthew G Knepley   You must also call DMPlexRestoreCone() after you finish using the returned array.
1258552f7358SJed Brown 
1259552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart()
1260552f7358SJed Brown @*/
1261552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1262552f7358SJed Brown {
1263552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1264552f7358SJed Brown   PetscInt       off;
1265552f7358SJed Brown   PetscErrorCode ierr;
1266552f7358SJed Brown 
1267552f7358SJed Brown   PetscFunctionBegin;
1268552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1269552f7358SJed Brown   PetscValidPointer(cone, 3);
1270552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1271552f7358SJed Brown   *cone = &mesh->cones[off];
1272552f7358SJed Brown   PetscFunctionReturn(0);
1273552f7358SJed Brown }
1274552f7358SJed Brown 
1275552f7358SJed Brown #undef __FUNCT__
1276552f7358SJed Brown #define __FUNCT__ "DMPlexSetCone"
1277552f7358SJed Brown /*@
1278552f7358SJed Brown   DMPlexSetCone - Set the points on the in-edges for this point in the Sieve DAG
1279552f7358SJed Brown 
1280552f7358SJed Brown   Not collective
1281552f7358SJed Brown 
1282552f7358SJed Brown   Input Parameters:
1283552f7358SJed Brown + mesh - The DMPlex
1284552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1285552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1286552f7358SJed Brown 
1287552f7358SJed Brown   Output Parameter:
1288552f7358SJed Brown 
1289552f7358SJed Brown   Note:
1290552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1291552f7358SJed Brown 
1292552f7358SJed Brown   Level: beginner
1293552f7358SJed Brown 
1294552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1295552f7358SJed Brown @*/
1296552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1297552f7358SJed Brown {
1298552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1299552f7358SJed Brown   PetscInt       pStart, pEnd;
1300552f7358SJed Brown   PetscInt       dof, off, c;
1301552f7358SJed Brown   PetscErrorCode ierr;
1302552f7358SJed Brown 
1303552f7358SJed Brown   PetscFunctionBegin;
1304552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1305552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1306552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1307552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1308552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
130982f516ccSBarry 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);
1310552f7358SJed Brown   for (c = 0; c < dof; ++c) {
131182f516ccSBarry 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);
1312552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1313552f7358SJed Brown   }
1314552f7358SJed Brown   PetscFunctionReturn(0);
1315552f7358SJed Brown }
1316552f7358SJed Brown 
1317552f7358SJed Brown #undef __FUNCT__
1318552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeOrientation"
1319552f7358SJed Brown /*@C
1320552f7358SJed Brown   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the Sieve DAG
1321552f7358SJed Brown 
1322552f7358SJed Brown   Not collective
1323552f7358SJed Brown 
1324552f7358SJed Brown   Input Parameters:
1325552f7358SJed Brown + mesh - The DMPlex
1326552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1327552f7358SJed Brown 
1328552f7358SJed Brown   Output Parameter:
1329552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1330552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1331552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1332552f7358SJed Brown                     the index of the cone point on which to start.
1333552f7358SJed Brown 
1334552f7358SJed Brown   Level: beginner
1335552f7358SJed Brown 
13363813dfbdSMatthew G Knepley   Fortran Notes:
13373813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
13383813dfbdSMatthew G Knepley   include petsc.h90 in your code.
13393813dfbdSMatthew G Knepley 
13403813dfbdSMatthew G Knepley   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1341552f7358SJed Brown 
1342552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1343552f7358SJed Brown @*/
1344552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1345552f7358SJed Brown {
1346552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1347552f7358SJed Brown   PetscInt       off;
1348552f7358SJed Brown   PetscErrorCode ierr;
1349552f7358SJed Brown 
1350552f7358SJed Brown   PetscFunctionBegin;
1351552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1352552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1353552f7358SJed Brown   {
1354552f7358SJed Brown     PetscInt dof;
1355552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1356552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1357552f7358SJed Brown   }
1358552f7358SJed Brown #endif
1359552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
13600d644c17SKarl Rupp 
1361552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1362552f7358SJed Brown   PetscFunctionReturn(0);
1363552f7358SJed Brown }
1364552f7358SJed Brown 
1365552f7358SJed Brown #undef __FUNCT__
1366552f7358SJed Brown #define __FUNCT__ "DMPlexSetConeOrientation"
1367552f7358SJed Brown /*@
1368552f7358SJed Brown   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the Sieve DAG
1369552f7358SJed Brown 
1370552f7358SJed Brown   Not collective
1371552f7358SJed Brown 
1372552f7358SJed Brown   Input Parameters:
1373552f7358SJed Brown + mesh - The DMPlex
1374552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1375552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1376552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1377552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1378552f7358SJed Brown                     the index of the cone point on which to start.
1379552f7358SJed Brown 
1380552f7358SJed Brown   Output Parameter:
1381552f7358SJed Brown 
1382552f7358SJed Brown   Note:
1383552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1384552f7358SJed Brown 
1385552f7358SJed Brown   Level: beginner
1386552f7358SJed Brown 
1387552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1388552f7358SJed Brown @*/
1389552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
1390552f7358SJed Brown {
1391552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1392552f7358SJed Brown   PetscInt       pStart, pEnd;
1393552f7358SJed Brown   PetscInt       dof, off, c;
1394552f7358SJed Brown   PetscErrorCode ierr;
1395552f7358SJed Brown 
1396552f7358SJed Brown   PetscFunctionBegin;
1397552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1398552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1399552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1400552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
1401552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
140282f516ccSBarry 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);
1403552f7358SJed Brown   for (c = 0; c < dof; ++c) {
1404552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
1405552f7358SJed Brown 
1406552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
140782f516ccSBarry 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);
1408552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
1409552f7358SJed Brown   }
1410552f7358SJed Brown   PetscFunctionReturn(0);
1411552f7358SJed Brown }
1412552f7358SJed Brown 
1413552f7358SJed Brown #undef __FUNCT__
1414552f7358SJed Brown #define __FUNCT__ "DMPlexInsertCone"
1415552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
1416552f7358SJed Brown {
1417552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1418552f7358SJed Brown   PetscInt       pStart, pEnd;
1419552f7358SJed Brown   PetscInt       dof, off;
1420552f7358SJed Brown   PetscErrorCode ierr;
1421552f7358SJed Brown 
1422552f7358SJed Brown   PetscFunctionBegin;
1423552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1424552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
142582f516ccSBarry 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);
142682f516ccSBarry 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);
142777c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
142877c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
142977c88f5bSMatthew 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);
1430552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
1431552f7358SJed Brown   PetscFunctionReturn(0);
1432552f7358SJed Brown }
1433552f7358SJed Brown 
1434552f7358SJed Brown #undef __FUNCT__
143577c88f5bSMatthew G Knepley #define __FUNCT__ "DMPlexInsertConeOrientation"
143677c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
143777c88f5bSMatthew G Knepley {
143877c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
143977c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
144077c88f5bSMatthew G Knepley   PetscInt       dof, off;
144177c88f5bSMatthew G Knepley   PetscErrorCode ierr;
144277c88f5bSMatthew G Knepley 
144377c88f5bSMatthew G Knepley   PetscFunctionBegin;
144477c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
144577c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
144677c88f5bSMatthew 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);
144777c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
144877c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
144977c88f5bSMatthew 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);
145077c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
145177c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
145277c88f5bSMatthew G Knepley }
145377c88f5bSMatthew G Knepley 
145477c88f5bSMatthew G Knepley #undef __FUNCT__
1455552f7358SJed Brown #define __FUNCT__ "DMPlexGetSupportSize"
1456552f7358SJed Brown /*@
1457552f7358SJed Brown   DMPlexGetSupportSize - Return the number of out-edges for this point in the Sieve DAG
1458552f7358SJed Brown 
1459552f7358SJed Brown   Not collective
1460552f7358SJed Brown 
1461552f7358SJed Brown   Input Parameters:
1462552f7358SJed Brown + mesh - The DMPlex
1463552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1464552f7358SJed Brown 
1465552f7358SJed Brown   Output Parameter:
1466552f7358SJed Brown . size - The support size for point p
1467552f7358SJed Brown 
1468552f7358SJed Brown   Level: beginner
1469552f7358SJed Brown 
1470552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
1471552f7358SJed Brown @*/
1472552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
1473552f7358SJed Brown {
1474552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1475552f7358SJed Brown   PetscErrorCode ierr;
1476552f7358SJed Brown 
1477552f7358SJed Brown   PetscFunctionBegin;
1478552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1479552f7358SJed Brown   PetscValidPointer(size, 3);
1480552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
1481552f7358SJed Brown   PetscFunctionReturn(0);
1482552f7358SJed Brown }
1483552f7358SJed Brown 
1484552f7358SJed Brown #undef __FUNCT__
1485552f7358SJed Brown #define __FUNCT__ "DMPlexSetSupportSize"
1486552f7358SJed Brown /*@
1487552f7358SJed Brown   DMPlexSetSupportSize - Set the number of out-edges for this point in the Sieve DAG
1488552f7358SJed Brown 
1489552f7358SJed Brown   Not collective
1490552f7358SJed Brown 
1491552f7358SJed Brown   Input Parameters:
1492552f7358SJed Brown + mesh - The DMPlex
1493552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1494552f7358SJed Brown - size - The support size for point p
1495552f7358SJed Brown 
1496552f7358SJed Brown   Output Parameter:
1497552f7358SJed Brown 
1498552f7358SJed Brown   Note:
1499552f7358SJed Brown   This should be called after DMPlexSetChart().
1500552f7358SJed Brown 
1501552f7358SJed Brown   Level: beginner
1502552f7358SJed Brown 
1503552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
1504552f7358SJed Brown @*/
1505552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
1506552f7358SJed Brown {
1507552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1508552f7358SJed Brown   PetscErrorCode ierr;
1509552f7358SJed Brown 
1510552f7358SJed Brown   PetscFunctionBegin;
1511552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1512552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
15130d644c17SKarl Rupp 
1514552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
1515552f7358SJed Brown   PetscFunctionReturn(0);
1516552f7358SJed Brown }
1517552f7358SJed Brown 
1518552f7358SJed Brown #undef __FUNCT__
1519552f7358SJed Brown #define __FUNCT__ "DMPlexGetSupport"
1520552f7358SJed Brown /*@C
1521552f7358SJed Brown   DMPlexGetSupport - Return the points on the out-edges for this point in the Sieve DAG
1522552f7358SJed Brown 
1523552f7358SJed Brown   Not collective
1524552f7358SJed Brown 
1525552f7358SJed Brown   Input Parameters:
1526552f7358SJed Brown + mesh - The DMPlex
1527552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1528552f7358SJed Brown 
1529552f7358SJed Brown   Output Parameter:
1530552f7358SJed Brown . support - An array of points which are on the out-edges for point p
1531552f7358SJed Brown 
1532552f7358SJed Brown   Level: beginner
1533552f7358SJed Brown 
15343813dfbdSMatthew G Knepley   Fortran Notes:
15353813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
15363813dfbdSMatthew G Knepley   include petsc.h90 in your code.
15373813dfbdSMatthew G Knepley 
15383813dfbdSMatthew G Knepley   You must also call DMPlexRestoreSupport() after you finish using the returned array.
15393813dfbdSMatthew G Knepley 
1540552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1541552f7358SJed Brown @*/
1542552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
1543552f7358SJed Brown {
1544552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1545552f7358SJed Brown   PetscInt       off;
1546552f7358SJed Brown   PetscErrorCode ierr;
1547552f7358SJed Brown 
1548552f7358SJed Brown   PetscFunctionBegin;
1549552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1550552f7358SJed Brown   PetscValidPointer(support, 3);
1551552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
1552552f7358SJed Brown   *support = &mesh->supports[off];
1553552f7358SJed Brown   PetscFunctionReturn(0);
1554552f7358SJed Brown }
1555552f7358SJed Brown 
1556552f7358SJed Brown #undef __FUNCT__
1557552f7358SJed Brown #define __FUNCT__ "DMPlexSetSupport"
1558552f7358SJed Brown /*@
1559552f7358SJed Brown   DMPlexSetSupport - Set the points on the out-edges for this point in the Sieve DAG
1560552f7358SJed Brown 
1561552f7358SJed Brown   Not collective
1562552f7358SJed Brown 
1563552f7358SJed Brown   Input Parameters:
1564552f7358SJed Brown + mesh - The DMPlex
1565552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1566552f7358SJed Brown - support - An array of points which are on the in-edges for point p
1567552f7358SJed Brown 
1568552f7358SJed Brown   Output Parameter:
1569552f7358SJed Brown 
1570552f7358SJed Brown   Note:
1571552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
1572552f7358SJed Brown 
1573552f7358SJed Brown   Level: beginner
1574552f7358SJed Brown 
1575552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
1576552f7358SJed Brown @*/
1577552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
1578552f7358SJed Brown {
1579552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1580552f7358SJed Brown   PetscInt       pStart, pEnd;
1581552f7358SJed Brown   PetscInt       dof, off, c;
1582552f7358SJed Brown   PetscErrorCode ierr;
1583552f7358SJed Brown 
1584552f7358SJed Brown   PetscFunctionBegin;
1585552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1586552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1587552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1588552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
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);
1591552f7358SJed Brown   for (c = 0; c < dof; ++c) {
159282f516ccSBarry 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);
1593552f7358SJed Brown     mesh->supports[off+c] = support[c];
1594552f7358SJed Brown   }
1595552f7358SJed Brown   PetscFunctionReturn(0);
1596552f7358SJed Brown }
1597552f7358SJed Brown 
1598552f7358SJed Brown #undef __FUNCT__
1599552f7358SJed Brown #define __FUNCT__ "DMPlexInsertSupport"
1600552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
1601552f7358SJed Brown {
1602552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1603552f7358SJed Brown   PetscInt       pStart, pEnd;
1604552f7358SJed Brown   PetscInt       dof, off;
1605552f7358SJed Brown   PetscErrorCode ierr;
1606552f7358SJed Brown 
1607552f7358SJed Brown   PetscFunctionBegin;
1608552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1609552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1610552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1611552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
161282f516ccSBarry 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);
161382f516ccSBarry 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);
161482f516ccSBarry 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);
1615552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
1616552f7358SJed Brown   PetscFunctionReturn(0);
1617552f7358SJed Brown }
1618552f7358SJed Brown 
1619552f7358SJed Brown #undef __FUNCT__
1620552f7358SJed Brown #define __FUNCT__ "DMPlexGetTransitiveClosure"
1621552f7358SJed Brown /*@C
1622552f7358SJed Brown   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the Sieve DAG
1623552f7358SJed Brown 
1624552f7358SJed Brown   Not collective
1625552f7358SJed Brown 
1626552f7358SJed Brown   Input Parameters:
1627552f7358SJed Brown + mesh - The DMPlex
1628552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1629552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
16300298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
1631552f7358SJed Brown 
1632552f7358SJed Brown   Output Parameters:
1633552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
1634552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
1635552f7358SJed Brown 
1636552f7358SJed Brown   Note:
16370298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
1638552f7358SJed Brown 
16393813dfbdSMatthew G Knepley   Fortran Notes:
16403813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16413813dfbdSMatthew G Knepley   include petsc.h90 in your code.
16423813dfbdSMatthew G Knepley 
16433813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
16443813dfbdSMatthew G Knepley 
1645552f7358SJed Brown   Level: beginner
1646552f7358SJed Brown 
1647552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1648552f7358SJed Brown @*/
1649552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1650552f7358SJed Brown {
1651552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
1652552f7358SJed Brown   PetscInt       *closure, *fifo;
16530298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
1654552f7358SJed Brown   PetscInt        tmpSize, t;
1655552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
1656552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
1657552f7358SJed Brown   PetscErrorCode  ierr;
1658552f7358SJed Brown 
1659552f7358SJed Brown   PetscFunctionBegin;
1660552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1661552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1662552f7358SJed Brown   /* This is only 1-level */
1663552f7358SJed Brown   if (useCone) {
1664552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
1665552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
1666552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
1667552f7358SJed Brown   } else {
1668552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
1669552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
1670552f7358SJed Brown   }
1671bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
1672bfbcdd7aSMatthew G. Knepley     if (*points) {
1673bfbcdd7aSMatthew G. Knepley       closure = *points;
1674bfbcdd7aSMatthew G. Knepley     } else {
1675bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
1676bfbcdd7aSMatthew G. Knepley       ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1677bfbcdd7aSMatthew G. Knepley     }
1678bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
1679bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
1680bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
1681bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
1682bfbcdd7aSMatthew G. Knepley     }
1683bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
1684bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
1685bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
1686bfbcdd7aSMatthew G. Knepley   }
168724c766afSToby Isaac   {
168824c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
168924c766afSToby Isaac 
169024c766afSToby Isaac     c = mesh->maxConeSize;
169124c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
169224c766afSToby Isaac     s = mesh->maxSupportSize;
169324c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
169424c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
169524c766afSToby Isaac   }
1696bfbcdd7aSMatthew G. Knepley   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1697bfbcdd7aSMatthew G. Knepley   if (*points) {
1698bfbcdd7aSMatthew G. Knepley     closure = *points;
1699bfbcdd7aSMatthew G. Knepley   } else {
1700bfbcdd7aSMatthew G. Knepley     ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1701bfbcdd7aSMatthew G. Knepley   }
1702bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
1703552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
1704552f7358SJed Brown     const PetscInt cp = tmp[t];
1705552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
1706552f7358SJed Brown 
1707552f7358SJed Brown     closure[closureSize]   = cp;
1708552f7358SJed Brown     closure[closureSize+1] = co;
1709552f7358SJed Brown     fifo[fifoSize]         = cp;
1710552f7358SJed Brown     fifo[fifoSize+1]       = co;
1711552f7358SJed Brown   }
1712bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
1713552f7358SJed Brown   while (fifoSize - fifoStart) {
1714552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
1715552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
1716552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
1717552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
1718552f7358SJed Brown 
1719552f7358SJed Brown     if (useCone) {
1720552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
1721552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
1722552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
1723552f7358SJed Brown     } else {
1724552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
1725552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
17260298fd71SBarry Smith       tmpO = NULL;
1727552f7358SJed Brown     }
1728552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
1729552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
1730552f7358SJed Brown       const PetscInt cp = tmp[i];
17312e1b13c2SMatthew 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. */
17322e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
17332e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
17342e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
1735552f7358SJed Brown       PetscInt       c;
1736552f7358SJed Brown 
17372e1b13c2SMatthew G. Knepley       if (rev) {
17382e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
17392e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
17402e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
17412e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
17422e1b13c2SMatthew G. Knepley       }
1743552f7358SJed Brown       /* Check for duplicate */
1744552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
1745552f7358SJed Brown         if (closure[c] == cp) break;
1746552f7358SJed Brown       }
1747552f7358SJed Brown       if (c == closureSize) {
1748552f7358SJed Brown         closure[closureSize]   = cp;
1749552f7358SJed Brown         closure[closureSize+1] = co;
1750552f7358SJed Brown         fifo[fifoSize]         = cp;
1751552f7358SJed Brown         fifo[fifoSize+1]       = co;
1752552f7358SJed Brown         closureSize           += 2;
1753552f7358SJed Brown         fifoSize              += 2;
1754552f7358SJed Brown       }
1755552f7358SJed Brown     }
1756552f7358SJed Brown     fifoStart += 2;
1757552f7358SJed Brown   }
1758552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
1759552f7358SJed Brown   if (points)    *points    = closure;
1760552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1761552f7358SJed Brown   PetscFunctionReturn(0);
1762552f7358SJed Brown }
1763552f7358SJed Brown 
1764552f7358SJed Brown #undef __FUNCT__
17659bf0dad6SMatthew G. Knepley #define __FUNCT__ "DMPlexGetTransitiveClosure_Internal"
17669bf0dad6SMatthew G. Knepley /*@C
17679bf0dad6SMatthew 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
17689bf0dad6SMatthew G. Knepley 
17699bf0dad6SMatthew G. Knepley   Not collective
17709bf0dad6SMatthew G. Knepley 
17719bf0dad6SMatthew G. Knepley   Input Parameters:
17729bf0dad6SMatthew G. Knepley + mesh - The DMPlex
17739bf0dad6SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
17749bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
17759bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
17769bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
17779bf0dad6SMatthew G. Knepley 
17789bf0dad6SMatthew G. Knepley   Output Parameters:
17799bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
17809bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
17819bf0dad6SMatthew G. Knepley 
17829bf0dad6SMatthew G. Knepley   Note:
17839bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
17849bf0dad6SMatthew G. Knepley 
17859bf0dad6SMatthew G. Knepley   Fortran Notes:
17869bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
17879bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
17889bf0dad6SMatthew G. Knepley 
17899bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
17909bf0dad6SMatthew G. Knepley 
17919bf0dad6SMatthew G. Knepley   Level: beginner
17929bf0dad6SMatthew G. Knepley 
17939bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
17949bf0dad6SMatthew G. Knepley @*/
17959bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
17969bf0dad6SMatthew G. Knepley {
17979bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
17989bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
17999bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
18009bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
18019bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
18029bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
18039bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
18049bf0dad6SMatthew G. Knepley 
18059bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
18069bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18079bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
18089bf0dad6SMatthew G. Knepley   /* This is only 1-level */
18099bf0dad6SMatthew G. Knepley   if (useCone) {
18109bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
18119bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
18129bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
18139bf0dad6SMatthew G. Knepley   } else {
18149bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
18159bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
18169bf0dad6SMatthew G. Knepley   }
18179bf0dad6SMatthew G. Knepley   if (depth == 1) {
18189bf0dad6SMatthew G. Knepley     if (*points) {
18199bf0dad6SMatthew G. Knepley       closure = *points;
18209bf0dad6SMatthew G. Knepley     } else {
18219bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
18229bf0dad6SMatthew G. Knepley       ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
18239bf0dad6SMatthew G. Knepley     }
18249bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
18259bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
18269bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
18279bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
18289bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
18299bf0dad6SMatthew G. Knepley     }
18309bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
18319bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
18329bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
18339bf0dad6SMatthew G. Knepley   }
183424c766afSToby Isaac   {
183524c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
183624c766afSToby Isaac 
183724c766afSToby Isaac     c = mesh->maxConeSize;
183824c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
183924c766afSToby Isaac     s = mesh->maxSupportSize;
184024c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
184124c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
184224c766afSToby Isaac   }
18439bf0dad6SMatthew G. Knepley   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
18449bf0dad6SMatthew G. Knepley   if (*points) {
18459bf0dad6SMatthew G. Knepley     closure = *points;
18469bf0dad6SMatthew G. Knepley   } else {
18479bf0dad6SMatthew G. Knepley     ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
18489bf0dad6SMatthew G. Knepley   }
18499bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
18509bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
18519bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
18529bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
18539bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
18549bf0dad6SMatthew G. Knepley 
18559bf0dad6SMatthew G. Knepley     if (ornt < 0) {
18569bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
18579bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
185886b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
18599bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
18609bf0dad6SMatthew G. Knepley     }
18619bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
18629bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
18639bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
18649bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
18659bf0dad6SMatthew G. Knepley   }
18669bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
18679bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
18689bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
18699bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
18709bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
18719bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
18729bf0dad6SMatthew G. Knepley 
18739bf0dad6SMatthew G. Knepley     if (useCone) {
18749bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
18759bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
18769bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
18779bf0dad6SMatthew G. Knepley     } else {
18789bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
18799bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
18809bf0dad6SMatthew G. Knepley       tmpO = NULL;
18819bf0dad6SMatthew G. Knepley     }
18829bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
18839bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
18849bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
18859bf0dad6SMatthew 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. */
18869bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
18879bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
18889bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
18899bf0dad6SMatthew G. Knepley       PetscInt       c;
18909bf0dad6SMatthew G. Knepley 
18919bf0dad6SMatthew G. Knepley       if (rev) {
18929bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
18939bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
18949bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
18959bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
18969bf0dad6SMatthew G. Knepley       }
18979bf0dad6SMatthew G. Knepley       /* Check for duplicate */
18989bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
18999bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
19009bf0dad6SMatthew G. Knepley       }
19019bf0dad6SMatthew G. Knepley       if (c == closureSize) {
19029bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
19039bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
19049bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
19059bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
19069bf0dad6SMatthew G. Knepley         closureSize           += 2;
19079bf0dad6SMatthew G. Knepley         fifoSize              += 2;
19089bf0dad6SMatthew G. Knepley       }
19099bf0dad6SMatthew G. Knepley     }
19109bf0dad6SMatthew G. Knepley     fifoStart += 2;
19119bf0dad6SMatthew G. Knepley   }
19129bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
19139bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
19149bf0dad6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
19159bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
19169bf0dad6SMatthew G. Knepley }
19179bf0dad6SMatthew G. Knepley 
19189bf0dad6SMatthew G. Knepley #undef __FUNCT__
1919552f7358SJed Brown #define __FUNCT__ "DMPlexRestoreTransitiveClosure"
1920552f7358SJed Brown /*@C
1921552f7358SJed 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
1922552f7358SJed Brown 
1923552f7358SJed Brown   Not collective
1924552f7358SJed Brown 
1925552f7358SJed Brown   Input Parameters:
1926552f7358SJed Brown + mesh - The DMPlex
1927552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1928552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
1929e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
1930e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
1931552f7358SJed Brown 
1932552f7358SJed Brown   Note:
19330298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
1934552f7358SJed Brown 
19353813dfbdSMatthew G Knepley   Fortran Notes:
19363813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
19373813dfbdSMatthew G Knepley   include petsc.h90 in your code.
19383813dfbdSMatthew G Knepley 
19393813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
19403813dfbdSMatthew G Knepley 
1941552f7358SJed Brown   Level: beginner
1942552f7358SJed Brown 
1943552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1944552f7358SJed Brown @*/
1945552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1946552f7358SJed Brown {
1947552f7358SJed Brown   PetscErrorCode ierr;
1948552f7358SJed Brown 
1949552f7358SJed Brown   PetscFunctionBegin;
1950552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1951e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
1952e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
1953552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, points);CHKERRQ(ierr);
19544ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
1955552f7358SJed Brown   PetscFunctionReturn(0);
1956552f7358SJed Brown }
1957552f7358SJed Brown 
1958552f7358SJed Brown #undef __FUNCT__
1959552f7358SJed Brown #define __FUNCT__ "DMPlexGetMaxSizes"
1960552f7358SJed Brown /*@
1961552f7358SJed Brown   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the Sieve DAG
1962552f7358SJed Brown 
1963552f7358SJed Brown   Not collective
1964552f7358SJed Brown 
1965552f7358SJed Brown   Input Parameter:
1966552f7358SJed Brown . mesh - The DMPlex
1967552f7358SJed Brown 
1968552f7358SJed Brown   Output Parameters:
1969552f7358SJed Brown + maxConeSize - The maximum number of in-edges
1970552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
1971552f7358SJed Brown 
1972552f7358SJed Brown   Level: beginner
1973552f7358SJed Brown 
1974552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1975552f7358SJed Brown @*/
1976552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
1977552f7358SJed Brown {
1978552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
1979552f7358SJed Brown 
1980552f7358SJed Brown   PetscFunctionBegin;
1981552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1982552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
1983552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
1984552f7358SJed Brown   PetscFunctionReturn(0);
1985552f7358SJed Brown }
1986552f7358SJed Brown 
1987552f7358SJed Brown #undef __FUNCT__
1988552f7358SJed Brown #define __FUNCT__ "DMSetUp_Plex"
1989552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
1990552f7358SJed Brown {
1991552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1992552f7358SJed Brown   PetscInt       size;
1993552f7358SJed Brown   PetscErrorCode ierr;
1994552f7358SJed Brown 
1995552f7358SJed Brown   PetscFunctionBegin;
1996552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1997552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
1998552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
19991795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
20001795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2001552f7358SJed Brown   if (mesh->maxSupportSize) {
2002552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2003552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
20041795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2005552f7358SJed Brown   }
2006552f7358SJed Brown   PetscFunctionReturn(0);
2007552f7358SJed Brown }
2008552f7358SJed Brown 
2009552f7358SJed Brown #undef __FUNCT__
2010552f7358SJed Brown #define __FUNCT__ "DMCreateSubDM_Plex"
2011552f7358SJed Brown PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
2012552f7358SJed Brown {
2013552f7358SJed Brown   PetscErrorCode ierr;
2014552f7358SJed Brown 
2015552f7358SJed Brown   PetscFunctionBegin;
20164d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
20174d9407bcSMatthew G. Knepley   ierr = DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2018552f7358SJed Brown   PetscFunctionReturn(0);
2019552f7358SJed Brown }
2020552f7358SJed Brown 
2021552f7358SJed Brown #undef __FUNCT__
2022552f7358SJed Brown #define __FUNCT__ "DMPlexSymmetrize"
2023552f7358SJed Brown /*@
2024552f7358SJed Brown   DMPlexSymmetrize - Creates support (out-edge) information from cone (in-edge) inoformation
2025552f7358SJed Brown 
2026552f7358SJed Brown   Not collective
2027552f7358SJed Brown 
2028552f7358SJed Brown   Input Parameter:
2029552f7358SJed Brown . mesh - The DMPlex
2030552f7358SJed Brown 
2031552f7358SJed Brown   Output Parameter:
2032552f7358SJed Brown 
2033552f7358SJed Brown   Note:
2034552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2035552f7358SJed Brown 
2036552f7358SJed Brown   Level: beginner
2037552f7358SJed Brown 
2038552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2039552f7358SJed Brown @*/
2040552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2041552f7358SJed Brown {
2042552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2043552f7358SJed Brown   PetscInt      *offsets;
2044552f7358SJed Brown   PetscInt       supportSize;
2045552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2046552f7358SJed Brown   PetscErrorCode ierr;
2047552f7358SJed Brown 
2048552f7358SJed Brown   PetscFunctionBegin;
2049552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
205082f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
2051552f7358SJed Brown   /* Calculate support sizes */
2052552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2053552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2054552f7358SJed Brown     PetscInt dof, off, c;
2055552f7358SJed Brown 
2056552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2057552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2058552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2059552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2060552f7358SJed Brown     }
2061552f7358SJed Brown   }
2062552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2063552f7358SJed Brown     PetscInt dof;
2064552f7358SJed Brown 
2065552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
20660d644c17SKarl Rupp 
2067552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2068552f7358SJed Brown   }
2069552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2070552f7358SJed Brown   /* Calculate supports */
2071552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
20721795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
20731795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2074552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2075552f7358SJed Brown     PetscInt dof, off, c;
2076552f7358SJed Brown 
2077552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2078552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2079552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2080552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2081552f7358SJed Brown       PetscInt       offS;
2082552f7358SJed Brown 
2083552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
20840d644c17SKarl Rupp 
2085552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2086552f7358SJed Brown       ++offsets[q];
2087552f7358SJed Brown     }
2088552f7358SJed Brown   }
2089552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
2090552f7358SJed Brown   PetscFunctionReturn(0);
2091552f7358SJed Brown }
2092552f7358SJed Brown 
2093552f7358SJed Brown #undef __FUNCT__
2094552f7358SJed Brown #define __FUNCT__ "DMPlexStratify"
2095552f7358SJed Brown /*@
2096552f7358SJed Brown   DMPlexStratify - The Sieve DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
2097552f7358SJed Brown   can be illustrated by Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2098552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2099552f7358SJed Brown   the DAG.
2100552f7358SJed Brown 
2101bf4602e4SToby Isaac   Collective on dm
2102552f7358SJed Brown 
2103552f7358SJed Brown   Input Parameter:
2104552f7358SJed Brown . mesh - The DMPlex
2105552f7358SJed Brown 
2106552f7358SJed Brown   Output Parameter:
2107552f7358SJed Brown 
2108552f7358SJed Brown   Notes:
2109150b719bSJed Brown   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
2110150b719bSJed Brown   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2111c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2112150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2113552f7358SJed Brown 
2114150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2115552f7358SJed Brown 
2116552f7358SJed Brown   Level: beginner
2117552f7358SJed Brown 
2118552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
2119552f7358SJed Brown @*/
2120552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2121552f7358SJed Brown {
2122df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2123aa50250dSMatthew G. Knepley   DMLabel        label;
2124552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2125552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
2126552f7358SJed Brown   PetscErrorCode ierr;
2127552f7358SJed Brown 
2128552f7358SJed Brown   PetscFunctionBegin;
2129552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2130552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2131552f7358SJed Brown   /* Calculate depth */
2132aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2133c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2134aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2135552f7358SJed Brown   /* Initialize roots and count leaves */
2136552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2137552f7358SJed Brown     PetscInt coneSize, supportSize;
2138552f7358SJed Brown 
2139552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2140552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2141552f7358SJed Brown     if (!coneSize && supportSize) {
2142552f7358SJed Brown       ++numRoots;
2143aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2144552f7358SJed Brown     } else if (!supportSize && coneSize) {
2145552f7358SJed Brown       ++numLeaves;
2146552f7358SJed Brown     } else if (!supportSize && !coneSize) {
2147552f7358SJed Brown       /* Isolated points */
2148aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2149552f7358SJed Brown     }
2150552f7358SJed Brown   }
2151552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2152552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
2153552f7358SJed Brown       PetscInt coneSize, supportSize;
2154552f7358SJed Brown 
2155552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2156552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2157552f7358SJed Brown       if (!supportSize && coneSize) {
2158aa50250dSMatthew G. Knepley         ierr = DMLabelSetValue(label, p, 1);CHKERRQ(ierr);
2159552f7358SJed Brown       }
2160552f7358SJed Brown     }
2161552f7358SJed Brown   } else {
216274ef644bSMatthew G. Knepley     IS       pointIS;
216374ef644bSMatthew G. Knepley     PetscInt numPoints = 0, level = 0;
2164552f7358SJed Brown 
216574ef644bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
216674ef644bSMatthew G. Knepley     if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
216774ef644bSMatthew G. Knepley     while (numPoints) {
216874ef644bSMatthew G. Knepley       const PetscInt *points;
216974ef644bSMatthew G. Knepley       const PetscInt  newLevel = level+1;
217074ef644bSMatthew G. Knepley 
217174ef644bSMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
217274ef644bSMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
217374ef644bSMatthew G. Knepley         const PetscInt  point = points[p];
217474ef644bSMatthew G. Knepley         const PetscInt *support;
217574ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
217674ef644bSMatthew G. Knepley 
217774ef644bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
217874ef644bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
217974ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
218074ef644bSMatthew G. Knepley           ierr = DMLabelSetValue(label, support[s], newLevel);CHKERRQ(ierr);
2181552f7358SJed Brown         }
2182552f7358SJed Brown       }
21835edfc765SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
218474ef644bSMatthew G. Knepley       ++level;
218574ef644bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
218674ef644bSMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
218774ef644bSMatthew G. Knepley       if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
218874ef644bSMatthew G. Knepley       else         {numPoints = 0;}
218974ef644bSMatthew G. Knepley     }
219074ef644bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
219174ef644bSMatthew G. Knepley   }
2192bf4602e4SToby Isaac   { /* just in case there is an empty process */
2193bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2194bf4602e4SToby Isaac 
2195bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label,&numValues);CHKERRQ(ierr);
21964de306b1SToby Isaac     for (v = 0; v < numValues; v++) {
21974de306b1SToby Isaac       IS pointIS;
21984de306b1SToby Isaac 
21994de306b1SToby Isaac       ierr = DMLabelGetStratumIS(label, v, &pointIS);CHKERRQ(ierr);
22004de306b1SToby Isaac       if (pointIS) {
22014de306b1SToby Isaac         PetscInt  min, max, numPoints;
22024de306b1SToby Isaac         PetscInt  start;
22034de306b1SToby Isaac         PetscBool contig;
22044de306b1SToby Isaac 
22054de306b1SToby Isaac         ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
22064de306b1SToby Isaac         ierr = ISGetMinMax(pointIS, &min, &max);CHKERRQ(ierr);
22074de306b1SToby Isaac         ierr = ISContiguousLocal(pointIS,min,max+1,&start,&contig);CHKERRQ(ierr);
22084de306b1SToby Isaac         if (start == 0 && contig) {
22094de306b1SToby Isaac           ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
22104de306b1SToby Isaac           ierr = ISCreateStride(PETSC_COMM_SELF,numPoints,min,1,&pointIS);CHKERRQ(ierr);
22114de306b1SToby Isaac           ierr = DMLabelSetStratumIS(label, v, pointIS);CHKERRQ(ierr);
22124de306b1SToby Isaac         }
22134de306b1SToby Isaac       }
22144de306b1SToby Isaac       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
22154de306b1SToby Isaac     }
22166d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2217bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2218bf4602e4SToby Isaac       DMLabelAddStratum(label,v);CHKERRQ(ierr);
2219bf4602e4SToby Isaac     }
2220bf4602e4SToby Isaac   }
2221bf4602e4SToby Isaac 
2222df0420ecSMatthew G. Knepley   ierr = DMLabelGetState(label, &mesh->depthState);CHKERRQ(ierr);
2223552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2224552f7358SJed Brown   PetscFunctionReturn(0);
2225552f7358SJed Brown }
2226552f7358SJed Brown 
2227552f7358SJed Brown #undef __FUNCT__
2228552f7358SJed Brown #define __FUNCT__ "DMPlexGetJoin"
2229552f7358SJed Brown /*@C
2230552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
2231552f7358SJed Brown 
2232552f7358SJed Brown   Not Collective
2233552f7358SJed Brown 
2234552f7358SJed Brown   Input Parameters:
2235552f7358SJed Brown + dm - The DMPlex object
2236552f7358SJed Brown . numPoints - The number of input points for the join
2237552f7358SJed Brown - points - The input points
2238552f7358SJed Brown 
2239552f7358SJed Brown   Output Parameters:
2240552f7358SJed Brown + numCoveredPoints - The number of points in the join
2241552f7358SJed Brown - coveredPoints - The points in the join
2242552f7358SJed Brown 
2243552f7358SJed Brown   Level: intermediate
2244552f7358SJed Brown 
2245552f7358SJed Brown   Note: Currently, this is restricted to a single level join
2246552f7358SJed Brown 
22473813dfbdSMatthew G Knepley   Fortran Notes:
22483813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22493813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22503813dfbdSMatthew G Knepley 
22513813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
22523813dfbdSMatthew G Knepley 
2253552f7358SJed Brown .keywords: mesh
2254552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2255552f7358SJed Brown @*/
2256552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2257552f7358SJed Brown {
2258552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2259552f7358SJed Brown   PetscInt      *join[2];
2260552f7358SJed Brown   PetscInt       joinSize, i = 0;
2261552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2262552f7358SJed Brown   PetscErrorCode ierr;
2263552f7358SJed Brown 
2264552f7358SJed Brown   PetscFunctionBegin;
2265552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2266552f7358SJed Brown   PetscValidPointer(points, 2);
2267552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2268552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2269552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
2270552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
2271552f7358SJed Brown   /* Copy in support of first point */
2272552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
2273552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
2274552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
2275552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
2276552f7358SJed Brown   }
2277552f7358SJed Brown   /* Check each successive support */
2278552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2279552f7358SJed Brown     PetscInt newJoinSize = 0;
2280552f7358SJed Brown 
2281552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
2282552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
2283552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2284552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
2285552f7358SJed Brown 
2286552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
2287552f7358SJed Brown         if (point == join[i][m]) {
2288552f7358SJed Brown           join[1-i][newJoinSize++] = point;
2289552f7358SJed Brown           break;
2290552f7358SJed Brown         }
2291552f7358SJed Brown       }
2292552f7358SJed Brown     }
2293552f7358SJed Brown     joinSize = newJoinSize;
2294552f7358SJed Brown     i        = 1-i;
2295552f7358SJed Brown   }
2296552f7358SJed Brown   *numCoveredPoints = joinSize;
2297552f7358SJed Brown   *coveredPoints    = join[i];
2298552f7358SJed Brown   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
2299552f7358SJed Brown   PetscFunctionReturn(0);
2300552f7358SJed Brown }
2301552f7358SJed Brown 
2302552f7358SJed Brown #undef __FUNCT__
2303552f7358SJed Brown #define __FUNCT__ "DMPlexRestoreJoin"
2304552f7358SJed Brown /*@C
2305552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
2306552f7358SJed Brown 
2307552f7358SJed Brown   Not Collective
2308552f7358SJed Brown 
2309552f7358SJed Brown   Input Parameters:
2310552f7358SJed Brown + dm - The DMPlex object
2311552f7358SJed Brown . numPoints - The number of input points for the join
2312552f7358SJed Brown - points - The input points
2313552f7358SJed Brown 
2314552f7358SJed Brown   Output Parameters:
2315552f7358SJed Brown + numCoveredPoints - The number of points in the join
2316552f7358SJed Brown - coveredPoints - The points in the join
2317552f7358SJed Brown 
23183813dfbdSMatthew G Knepley   Fortran Notes:
23193813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
23203813dfbdSMatthew G Knepley   include petsc.h90 in your code.
23213813dfbdSMatthew G Knepley 
23223813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
23233813dfbdSMatthew G Knepley 
2324552f7358SJed Brown   Level: intermediate
2325552f7358SJed Brown 
2326552f7358SJed Brown .keywords: mesh
2327552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
2328552f7358SJed Brown @*/
2329552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2330552f7358SJed Brown {
2331552f7358SJed Brown   PetscErrorCode ierr;
2332552f7358SJed Brown 
2333552f7358SJed Brown   PetscFunctionBegin;
2334552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2335d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2336d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2337d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
2338552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
2339d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2340552f7358SJed Brown   PetscFunctionReturn(0);
2341552f7358SJed Brown }
2342552f7358SJed Brown 
2343552f7358SJed Brown #undef __FUNCT__
2344552f7358SJed Brown #define __FUNCT__ "DMPlexGetFullJoin"
2345552f7358SJed Brown /*@C
2346552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
2347552f7358SJed Brown 
2348552f7358SJed Brown   Not Collective
2349552f7358SJed Brown 
2350552f7358SJed Brown   Input Parameters:
2351552f7358SJed Brown + dm - The DMPlex object
2352552f7358SJed Brown . numPoints - The number of input points for the join
2353552f7358SJed Brown - points - The input points
2354552f7358SJed Brown 
2355552f7358SJed Brown   Output Parameters:
2356552f7358SJed Brown + numCoveredPoints - The number of points in the join
2357552f7358SJed Brown - coveredPoints - The points in the join
2358552f7358SJed Brown 
23593813dfbdSMatthew G Knepley   Fortran Notes:
23603813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
23613813dfbdSMatthew G Knepley   include petsc.h90 in your code.
23623813dfbdSMatthew G Knepley 
23633813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
23643813dfbdSMatthew G Knepley 
2365552f7358SJed Brown   Level: intermediate
2366552f7358SJed Brown 
2367552f7358SJed Brown .keywords: mesh
2368552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
2369552f7358SJed Brown @*/
2370552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2371552f7358SJed Brown {
2372552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2373552f7358SJed Brown   PetscInt      *offsets, **closures;
2374552f7358SJed Brown   PetscInt      *join[2];
2375552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
237624c766afSToby Isaac   PetscInt       p, d, c, m, ms;
2377552f7358SJed Brown   PetscErrorCode ierr;
2378552f7358SJed Brown 
2379552f7358SJed Brown   PetscFunctionBegin;
2380552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2381552f7358SJed Brown   PetscValidPointer(points, 2);
2382552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2383552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2384552f7358SJed Brown 
2385552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
23861795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
2387552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
238824c766afSToby Isaac   ms      = mesh->maxSupportSize;
238924c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
2390552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
2391552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
2392552f7358SJed Brown 
2393552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2394552f7358SJed Brown     PetscInt closureSize;
2395552f7358SJed Brown 
2396552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
23970d644c17SKarl Rupp 
2398552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
2399552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
2400552f7358SJed Brown       PetscInt pStart, pEnd, i;
2401552f7358SJed Brown 
2402552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
2403552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
2404552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2405552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
2406552f7358SJed Brown           break;
2407552f7358SJed Brown         }
2408552f7358SJed Brown       }
2409552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
2410552f7358SJed Brown     }
241182f516ccSBarry 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);
2412552f7358SJed Brown   }
2413552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
2414552f7358SJed Brown     PetscInt dof;
2415552f7358SJed Brown 
2416552f7358SJed Brown     /* Copy in support of first point */
2417552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
2418552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
2419552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
2420552f7358SJed Brown     }
2421552f7358SJed Brown     /* Check each successive cone */
2422552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
2423552f7358SJed Brown       PetscInt newJoinSize = 0;
2424552f7358SJed Brown 
2425552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
2426552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2427552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
2428552f7358SJed Brown 
2429552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
2430552f7358SJed Brown           if (point == join[i][m]) {
2431552f7358SJed Brown             join[1-i][newJoinSize++] = point;
2432552f7358SJed Brown             break;
2433552f7358SJed Brown           }
2434552f7358SJed Brown         }
2435552f7358SJed Brown       }
2436552f7358SJed Brown       joinSize = newJoinSize;
2437552f7358SJed Brown       i        = 1-i;
2438552f7358SJed Brown     }
2439552f7358SJed Brown     if (joinSize) break;
2440552f7358SJed Brown   }
2441552f7358SJed Brown   *numCoveredPoints = joinSize;
2442552f7358SJed Brown   *coveredPoints    = join[i];
2443552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
24440298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
2445552f7358SJed Brown   }
2446552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
2447552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
2448552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
2449552f7358SJed Brown   PetscFunctionReturn(0);
2450552f7358SJed Brown }
2451552f7358SJed Brown 
2452552f7358SJed Brown #undef __FUNCT__
2453552f7358SJed Brown #define __FUNCT__ "DMPlexGetMeet"
2454552f7358SJed Brown /*@C
2455552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
2456552f7358SJed Brown 
2457552f7358SJed Brown   Not Collective
2458552f7358SJed Brown 
2459552f7358SJed Brown   Input Parameters:
2460552f7358SJed Brown + dm - The DMPlex object
2461552f7358SJed Brown . numPoints - The number of input points for the meet
2462552f7358SJed Brown - points - The input points
2463552f7358SJed Brown 
2464552f7358SJed Brown   Output Parameters:
2465552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2466552f7358SJed Brown - coveredPoints - The points in the meet
2467552f7358SJed Brown 
2468552f7358SJed Brown   Level: intermediate
2469552f7358SJed Brown 
2470552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
2471552f7358SJed Brown 
24723813dfbdSMatthew G Knepley   Fortran Notes:
24733813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24743813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24753813dfbdSMatthew G Knepley 
24763813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24773813dfbdSMatthew G Knepley 
2478552f7358SJed Brown .keywords: mesh
2479552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
2480552f7358SJed Brown @*/
2481552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
2482552f7358SJed Brown {
2483552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2484552f7358SJed Brown   PetscInt      *meet[2];
2485552f7358SJed Brown   PetscInt       meetSize, i = 0;
2486552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2487552f7358SJed Brown   PetscErrorCode ierr;
2488552f7358SJed Brown 
2489552f7358SJed Brown   PetscFunctionBegin;
2490552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2491552f7358SJed Brown   PetscValidPointer(points, 2);
2492552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
2493552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
2494552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
2495552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
2496552f7358SJed Brown   /* Copy in cone of first point */
2497552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
2498552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
2499552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
2500552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
2501552f7358SJed Brown   }
2502552f7358SJed Brown   /* Check each successive cone */
2503552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2504552f7358SJed Brown     PetscInt newMeetSize = 0;
2505552f7358SJed Brown 
2506552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
2507552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
2508552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2509552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
2510552f7358SJed Brown 
2511552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
2512552f7358SJed Brown         if (point == meet[i][m]) {
2513552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
2514552f7358SJed Brown           break;
2515552f7358SJed Brown         }
2516552f7358SJed Brown       }
2517552f7358SJed Brown     }
2518552f7358SJed Brown     meetSize = newMeetSize;
2519552f7358SJed Brown     i        = 1-i;
2520552f7358SJed Brown   }
2521552f7358SJed Brown   *numCoveringPoints = meetSize;
2522552f7358SJed Brown   *coveringPoints    = meet[i];
2523552f7358SJed Brown   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
2524552f7358SJed Brown   PetscFunctionReturn(0);
2525552f7358SJed Brown }
2526552f7358SJed Brown 
2527552f7358SJed Brown #undef __FUNCT__
2528552f7358SJed Brown #define __FUNCT__ "DMPlexRestoreMeet"
2529552f7358SJed Brown /*@C
2530552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
2531552f7358SJed Brown 
2532552f7358SJed Brown   Not Collective
2533552f7358SJed Brown 
2534552f7358SJed Brown   Input Parameters:
2535552f7358SJed Brown + dm - The DMPlex object
2536552f7358SJed Brown . numPoints - The number of input points for the meet
2537552f7358SJed Brown - points - The input points
2538552f7358SJed Brown 
2539552f7358SJed Brown   Output Parameters:
2540552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2541552f7358SJed Brown - coveredPoints - The points in the meet
2542552f7358SJed Brown 
2543552f7358SJed Brown   Level: intermediate
2544552f7358SJed Brown 
25453813dfbdSMatthew G Knepley   Fortran Notes:
25463813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25473813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25483813dfbdSMatthew G Knepley 
25493813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25503813dfbdSMatthew G Knepley 
2551552f7358SJed Brown .keywords: mesh
2552552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
2553552f7358SJed Brown @*/
2554552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2555552f7358SJed Brown {
2556552f7358SJed Brown   PetscErrorCode ierr;
2557552f7358SJed Brown 
2558552f7358SJed Brown   PetscFunctionBegin;
2559552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2560d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2561d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2562d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
2563552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
2564d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2565552f7358SJed Brown   PetscFunctionReturn(0);
2566552f7358SJed Brown }
2567552f7358SJed Brown 
2568552f7358SJed Brown #undef __FUNCT__
2569552f7358SJed Brown #define __FUNCT__ "DMPlexGetFullMeet"
2570552f7358SJed Brown /*@C
2571552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
2572552f7358SJed Brown 
2573552f7358SJed Brown   Not Collective
2574552f7358SJed Brown 
2575552f7358SJed Brown   Input Parameters:
2576552f7358SJed Brown + dm - The DMPlex object
2577552f7358SJed Brown . numPoints - The number of input points for the meet
2578552f7358SJed Brown - points - The input points
2579552f7358SJed Brown 
2580552f7358SJed Brown   Output Parameters:
2581552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2582552f7358SJed Brown - coveredPoints - The points in the meet
2583552f7358SJed Brown 
2584552f7358SJed Brown   Level: intermediate
2585552f7358SJed Brown 
25863813dfbdSMatthew G Knepley   Fortran Notes:
25873813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25883813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25893813dfbdSMatthew G Knepley 
25903813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25913813dfbdSMatthew G Knepley 
2592552f7358SJed Brown .keywords: mesh
2593552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
2594552f7358SJed Brown @*/
2595552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2596552f7358SJed Brown {
2597552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2598552f7358SJed Brown   PetscInt      *offsets, **closures;
2599552f7358SJed Brown   PetscInt      *meet[2];
2600552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
260124c766afSToby Isaac   PetscInt       p, h, c, m, mc;
2602552f7358SJed Brown   PetscErrorCode ierr;
2603552f7358SJed Brown 
2604552f7358SJed Brown   PetscFunctionBegin;
2605552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2606552f7358SJed Brown   PetscValidPointer(points, 2);
2607552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2608552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2609552f7358SJed Brown 
2610552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
2611785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
2612552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
261324c766afSToby Isaac   mc      = mesh->maxConeSize;
261424c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
2615552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
2616552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
2617552f7358SJed Brown 
2618552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2619552f7358SJed Brown     PetscInt closureSize;
2620552f7358SJed Brown 
2621552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
26220d644c17SKarl Rupp 
2623552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
2624552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
2625552f7358SJed Brown       PetscInt pStart, pEnd, i;
2626552f7358SJed Brown 
2627552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
2628552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
2629552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2630552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
2631552f7358SJed Brown           break;
2632552f7358SJed Brown         }
2633552f7358SJed Brown       }
2634552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
2635552f7358SJed Brown     }
263682f516ccSBarry 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);
2637552f7358SJed Brown   }
2638552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
2639552f7358SJed Brown     PetscInt dof;
2640552f7358SJed Brown 
2641552f7358SJed Brown     /* Copy in cone of first point */
2642552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
2643552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
2644552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
2645552f7358SJed Brown     }
2646552f7358SJed Brown     /* Check each successive cone */
2647552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
2648552f7358SJed Brown       PetscInt newMeetSize = 0;
2649552f7358SJed Brown 
2650552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
2651552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2652552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
2653552f7358SJed Brown 
2654552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
2655552f7358SJed Brown           if (point == meet[i][m]) {
2656552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
2657552f7358SJed Brown             break;
2658552f7358SJed Brown           }
2659552f7358SJed Brown         }
2660552f7358SJed Brown       }
2661552f7358SJed Brown       meetSize = newMeetSize;
2662552f7358SJed Brown       i        = 1-i;
2663552f7358SJed Brown     }
2664552f7358SJed Brown     if (meetSize) break;
2665552f7358SJed Brown   }
2666552f7358SJed Brown   *numCoveredPoints = meetSize;
2667552f7358SJed Brown   *coveredPoints    = meet[i];
2668552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
26690298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
2670552f7358SJed Brown   }
2671552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
2672552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
2673552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
2674552f7358SJed Brown   PetscFunctionReturn(0);
2675552f7358SJed Brown }
2676552f7358SJed Brown 
2677552f7358SJed Brown #undef __FUNCT__
26784e3744c5SMatthew G. Knepley #define __FUNCT__ "DMPlexEqual"
26794e3744c5SMatthew G. Knepley /*@C
26804e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
26814e3744c5SMatthew G. Knepley 
26824e3744c5SMatthew G. Knepley   Not Collective
26834e3744c5SMatthew G. Knepley 
26844e3744c5SMatthew G. Knepley   Input Parameters:
26854e3744c5SMatthew G. Knepley + dmA - A DMPlex object
26864e3744c5SMatthew G. Knepley - dmB - A DMPlex object
26874e3744c5SMatthew G. Knepley 
26884e3744c5SMatthew G. Knepley   Output Parameters:
26894e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
26904e3744c5SMatthew G. Knepley 
26914e3744c5SMatthew G. Knepley   Level: intermediate
26924e3744c5SMatthew G. Knepley 
26934e3744c5SMatthew G. Knepley   Notes:
26944e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
26954e3744c5SMatthew G. Knepley 
26964e3744c5SMatthew G. Knepley .keywords: mesh
26974e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
26984e3744c5SMatthew G. Knepley @*/
26994e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
27004e3744c5SMatthew G. Knepley {
27014e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
27024e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
27034e3744c5SMatthew G. Knepley 
27044e3744c5SMatthew G. Knepley   PetscFunctionBegin;
27054e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
27064e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
27074e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
27084e3744c5SMatthew G. Knepley 
27094e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
27104e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
27114e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
27124e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
27134e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
27144e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
27154e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
27164e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
27174e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
27184e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
27194e3744c5SMatthew G. Knepley 
27204e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
27214e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
27224e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
27234e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
27244e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
27254e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
27264e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
27274e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
27284e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
27294e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
27304e3744c5SMatthew G. Knepley     }
27314e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
27324e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
27334e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
27344e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
27354e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
27364e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
27374e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
27384e3744c5SMatthew G. Knepley     }
27394e3744c5SMatthew G. Knepley   }
27404e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
27414e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
27424e3744c5SMatthew G. Knepley }
27434e3744c5SMatthew G. Knepley 
27444e3744c5SMatthew G. Knepley #undef __FUNCT__
274518ad9376SMatthew G. Knepley #define __FUNCT__ "DMPlexGetNumFaceVertices"
274618ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
2747a6dfd86eSKarl Rupp {
274882f516ccSBarry Smith   MPI_Comm       comm;
2749552f7358SJed Brown   PetscErrorCode ierr;
2750552f7358SJed Brown 
2751552f7358SJed Brown   PetscFunctionBegin;
275282f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2753552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
2754552f7358SJed Brown   switch (cellDim) {
2755552f7358SJed Brown   case 0:
2756552f7358SJed Brown     *numFaceVertices = 0;
2757552f7358SJed Brown     break;
2758552f7358SJed Brown   case 1:
2759552f7358SJed Brown     *numFaceVertices = 1;
2760552f7358SJed Brown     break;
2761552f7358SJed Brown   case 2:
2762552f7358SJed Brown     switch (numCorners) {
276319436ca2SJed Brown     case 3: /* triangle */
276419436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2765552f7358SJed Brown       break;
276619436ca2SJed Brown     case 4: /* quadrilateral */
276719436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2768552f7358SJed Brown       break;
276919436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
277019436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2771552f7358SJed Brown       break;
277219436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
277319436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2774552f7358SJed Brown       break;
2775552f7358SJed Brown     default:
2776f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2777552f7358SJed Brown     }
2778552f7358SJed Brown     break;
2779552f7358SJed Brown   case 3:
2780552f7358SJed Brown     switch (numCorners) {
278119436ca2SJed Brown     case 4: /* tetradehdron */
278219436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
2783552f7358SJed Brown       break;
278419436ca2SJed Brown     case 6: /* tet cohesive cells */
278519436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2786552f7358SJed Brown       break;
278719436ca2SJed Brown     case 8: /* hexahedron */
278819436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2789552f7358SJed Brown       break;
279019436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
279119436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2792552f7358SJed Brown       break;
279319436ca2SJed Brown     case 10: /* quadratic tetrahedron */
279419436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2795552f7358SJed Brown       break;
279619436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
279719436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2798552f7358SJed Brown       break;
279919436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
280019436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2801552f7358SJed Brown       break;
280219436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
280319436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
2804552f7358SJed Brown       break;
2805552f7358SJed Brown     default:
2806f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2807552f7358SJed Brown     }
2808552f7358SJed Brown     break;
2809552f7358SJed Brown   default:
2810f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
2811552f7358SJed Brown   }
2812552f7358SJed Brown   PetscFunctionReturn(0);
2813552f7358SJed Brown }
2814552f7358SJed Brown 
2815552f7358SJed Brown #undef __FUNCT__
2816aa50250dSMatthew G. Knepley #define __FUNCT__ "DMPlexGetDepthLabel"
2817552f7358SJed Brown /*@
2818aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
2819552f7358SJed Brown 
2820552f7358SJed Brown   Not Collective
2821552f7358SJed Brown 
2822aa50250dSMatthew G. Knepley   Input Parameter:
2823552f7358SJed Brown . dm    - The DMPlex object
2824552f7358SJed Brown 
2825aa50250dSMatthew G. Knepley   Output Parameter:
2826aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
2827552f7358SJed Brown 
2828552f7358SJed Brown   Level: developer
2829552f7358SJed Brown 
2830aa50250dSMatthew G. Knepley .keywords: mesh, points
2831aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2832aa50250dSMatthew G. Knepley @*/
2833aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
2834aa50250dSMatthew G. Knepley {
2835aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
2836aa50250dSMatthew G. Knepley 
2837aa50250dSMatthew G. Knepley   PetscFunctionBegin;
2838aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2839aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
2840c58f1c22SToby Isaac   if (!dm->depthLabel) {ierr = DMGetLabel(dm, "depth", &dm->depthLabel);CHKERRQ(ierr);}
2841c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
2842aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
2843aa50250dSMatthew G. Knepley }
2844aa50250dSMatthew G. Knepley 
2845aa50250dSMatthew G. Knepley #undef __FUNCT__
2846aa50250dSMatthew G. Knepley #define __FUNCT__ "DMPlexGetDepth"
2847aa50250dSMatthew G. Knepley /*@
2848aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
2849aa50250dSMatthew G. Knepley 
2850aa50250dSMatthew G. Knepley   Not Collective
2851aa50250dSMatthew G. Knepley 
2852aa50250dSMatthew G. Knepley   Input Parameter:
2853aa50250dSMatthew G. Knepley . dm    - The DMPlex object
2854aa50250dSMatthew G. Knepley 
2855aa50250dSMatthew G. Knepley   Output Parameter:
2856aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
2857aa50250dSMatthew G. Knepley 
2858aa50250dSMatthew G. Knepley   Level: developer
2859552f7358SJed Brown 
2860552f7358SJed Brown .keywords: mesh, points
2861aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2862552f7358SJed Brown @*/
2863552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
2864552f7358SJed Brown {
2865aa50250dSMatthew G. Knepley   DMLabel        label;
2866aa50250dSMatthew G. Knepley   PetscInt       d = 0;
2867552f7358SJed Brown   PetscErrorCode ierr;
2868552f7358SJed Brown 
2869552f7358SJed Brown   PetscFunctionBegin;
2870552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2871552f7358SJed Brown   PetscValidPointer(depth, 2);
2872aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2873aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
2874552f7358SJed Brown   *depth = d-1;
2875552f7358SJed Brown   PetscFunctionReturn(0);
2876552f7358SJed Brown }
2877552f7358SJed Brown 
2878552f7358SJed Brown #undef __FUNCT__
2879552f7358SJed Brown #define __FUNCT__ "DMPlexGetDepthStratum"
2880552f7358SJed Brown /*@
2881552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
2882552f7358SJed Brown 
2883552f7358SJed Brown   Not Collective
2884552f7358SJed Brown 
2885552f7358SJed Brown   Input Parameters:
2886552f7358SJed Brown + dm           - The DMPlex object
2887552f7358SJed Brown - stratumValue - The requested depth
2888552f7358SJed Brown 
2889552f7358SJed Brown   Output Parameters:
2890552f7358SJed Brown + start - The first point at this depth
2891552f7358SJed Brown - end   - One beyond the last point at this depth
2892552f7358SJed Brown 
2893552f7358SJed Brown   Level: developer
2894552f7358SJed Brown 
2895552f7358SJed Brown .keywords: mesh, points
2896552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
2897552f7358SJed Brown @*/
28980adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
28990adebc6cSBarry Smith {
2900aa50250dSMatthew G. Knepley   DMLabel        label;
290163d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
2902552f7358SJed Brown   PetscErrorCode ierr;
2903552f7358SJed Brown 
2904552f7358SJed Brown   PetscFunctionBegin;
2905552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
290663d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
290763d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
2908552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
29090d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
291063d1a920SMatthew G. Knepley   if (stratumValue < 0) {
291163d1a920SMatthew G. Knepley     if (start) *start = pStart;
291263d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
291363d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
2914552f7358SJed Brown   }
2915aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
291663d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
291763d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
2918552f7358SJed Brown   PetscFunctionReturn(0);
2919552f7358SJed Brown }
2920552f7358SJed Brown 
2921552f7358SJed Brown #undef __FUNCT__
2922552f7358SJed Brown #define __FUNCT__ "DMPlexGetHeightStratum"
2923552f7358SJed Brown /*@
2924552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
2925552f7358SJed Brown 
2926552f7358SJed Brown   Not Collective
2927552f7358SJed Brown 
2928552f7358SJed Brown   Input Parameters:
2929552f7358SJed Brown + dm           - The DMPlex object
2930552f7358SJed Brown - stratumValue - The requested height
2931552f7358SJed Brown 
2932552f7358SJed Brown   Output Parameters:
2933552f7358SJed Brown + start - The first point at this height
2934552f7358SJed Brown - end   - One beyond the last point at this height
2935552f7358SJed Brown 
2936552f7358SJed Brown   Level: developer
2937552f7358SJed Brown 
2938552f7358SJed Brown .keywords: mesh, points
2939552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
2940552f7358SJed Brown @*/
29410adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
29420adebc6cSBarry Smith {
2943aa50250dSMatthew G. Knepley   DMLabel        label;
294463d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
2945552f7358SJed Brown   PetscErrorCode ierr;
2946552f7358SJed Brown 
2947552f7358SJed Brown   PetscFunctionBegin;
2948552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
294963d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
295063d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
2951552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
29520d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
295363d1a920SMatthew G. Knepley   if (stratumValue < 0) {
295463d1a920SMatthew G. Knepley     if (start) *start = pStart;
295563d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
295663d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
2957552f7358SJed Brown   }
2958aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2959aa50250dSMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");CHKERRQ(ierr);
296063d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
296163d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
2962552f7358SJed Brown   PetscFunctionReturn(0);
2963552f7358SJed Brown }
2964552f7358SJed Brown 
2965552f7358SJed Brown #undef __FUNCT__
2966552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSectionInitial"
2967552f7358SJed Brown /* Set the number of dof on each point and separate by fields */
2968a6dfd86eSKarl Rupp PetscErrorCode DMPlexCreateSectionInitial(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscSection *section)
2969a6dfd86eSKarl Rupp {
2970ee55978aSMatthew G. Knepley   PetscInt      *pMax;
2971916f0f19SMatthew G. Knepley   PetscInt       depth, pStart = 0, pEnd = 0;
2972ee55978aSMatthew G. Knepley   PetscInt       Nf, p, d, dep, f;
2973fa716be8SMatthew G. Knepley   PetscBool     *isFE;
2974552f7358SJed Brown   PetscErrorCode ierr;
2975552f7358SJed Brown 
2976552f7358SJed Brown   PetscFunctionBegin;
2977fa716be8SMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
2978ee55978aSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
2979fa716be8SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
2980fa716be8SMatthew G. Knepley     PetscObject  obj;
2981fa716be8SMatthew G. Knepley     PetscClassId id;
2982fa716be8SMatthew G. Knepley 
2983ee55978aSMatthew G. Knepley     isFE[f] = PETSC_FALSE;
2984ee55978aSMatthew G. Knepley     if (f >= Nf) continue;
2985fa716be8SMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
2986fa716be8SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2987fa716be8SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
2988fa716be8SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
2989552f7358SJed Brown   }
299082f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), section);CHKERRQ(ierr);
2991552f7358SJed Brown   if (numFields > 0) {
2992552f7358SJed Brown     ierr = PetscSectionSetNumFields(*section, numFields);CHKERRQ(ierr);
2993552f7358SJed Brown     if (numComp) {
2994552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
2995552f7358SJed Brown         ierr = PetscSectionSetFieldComponents(*section, f, numComp[f]);CHKERRQ(ierr);
2996d484f18aSToby Isaac         if (isFE[f]) {
2997d484f18aSToby Isaac           PetscFE           fe;
2998d484f18aSToby Isaac           PetscDualSpace    dspace;
2999d484f18aSToby Isaac           const PetscInt    ***perms;
3000d484f18aSToby Isaac           const PetscScalar ***flips;
3001d484f18aSToby Isaac           const PetscInt    *numDof;
3002d484f18aSToby Isaac 
3003d484f18aSToby Isaac           ierr = DMGetField(dm,f,(PetscObject *) &fe);CHKERRQ(ierr);
3004d484f18aSToby Isaac           ierr = PetscFEGetDualSpace(fe,&dspace);CHKERRQ(ierr);
3005d484f18aSToby Isaac           ierr = PetscDualSpaceGetSymmetries(dspace,&perms,&flips);CHKERRQ(ierr);
3006d484f18aSToby Isaac           ierr = PetscDualSpaceGetNumDof(dspace,&numDof);CHKERRQ(ierr);
3007d484f18aSToby Isaac           if (perms || flips) {
3008d484f18aSToby Isaac             DM               K;
3009d484f18aSToby Isaac             DMLabel          depthLabel;
3010d484f18aSToby Isaac             PetscInt         depth, h;
3011d484f18aSToby Isaac             PetscSectionSym  sym;
3012d484f18aSToby Isaac 
3013d484f18aSToby Isaac             ierr = PetscDualSpaceGetDM(dspace,&K);CHKERRQ(ierr);
3014d484f18aSToby Isaac             ierr = DMPlexGetDepthLabel(dm,&depthLabel);CHKERRQ(ierr);
3015d484f18aSToby Isaac             ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
3016d484f18aSToby Isaac             ierr = PetscSectionSymCreateLabel(PetscObjectComm((PetscObject)*section),depthLabel,&sym);CHKERRQ(ierr);
3017d484f18aSToby Isaac             for (h = 0; h <= depth; h++) {
3018d484f18aSToby Isaac               PetscDualSpace    hspace;
3019d484f18aSToby Isaac               PetscInt          kStart, kEnd;
3020d484f18aSToby Isaac               PetscInt          kConeSize;
3021d484f18aSToby Isaac               const PetscInt    **perms0 = NULL;
3022d484f18aSToby Isaac               const PetscScalar **flips0 = NULL;
3023d484f18aSToby Isaac 
3024d484f18aSToby Isaac               ierr = PetscDualSpaceGetHeightSubspace(dspace,h,&hspace);CHKERRQ(ierr);
3025d484f18aSToby Isaac               ierr = DMPlexGetHeightStratum(K,h,&kStart,&kEnd);CHKERRQ(ierr);
3026d484f18aSToby Isaac               if (!hspace) continue;
3027d484f18aSToby Isaac               ierr = PetscDualSpaceGetSymmetries(hspace,&perms,&flips);CHKERRQ(ierr);
3028d484f18aSToby Isaac               if (perms) perms0 = perms[0];
3029d484f18aSToby Isaac               if (flips) flips0 = flips[0];
3030d484f18aSToby Isaac               if (!(perms0 || flips0)) continue;
3031d484f18aSToby Isaac               ierr = DMPlexGetConeSize(K,kStart,&kConeSize);CHKERRQ(ierr);
3032d484f18aSToby Isaac               if (numComp[f] == 1) {
3033d484f18aSToby Isaac                 ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numDof[depth - h],-kConeSize,kConeSize,PETSC_USE_POINTER,perms0 ? &perms0[-kConeSize] : NULL,flips0 ? &flips0[-kConeSize] : NULL);CHKERRQ(ierr);
3034d484f18aSToby Isaac               } else {
3035d484f18aSToby Isaac                 PetscInt    **fieldPerms = NULL, o;
3036d484f18aSToby Isaac                 PetscScalar **fieldFlips = NULL;
3037d484f18aSToby Isaac 
3038d484f18aSToby Isaac                 ierr = PetscCalloc1(2 * kConeSize,&fieldPerms);CHKERRQ(ierr);
3039d484f18aSToby Isaac                 ierr = PetscCalloc1(2 * kConeSize,&fieldFlips);CHKERRQ(ierr);
3040d484f18aSToby Isaac                 for (o = -kConeSize; o < kConeSize; o++) {
3041d484f18aSToby Isaac                   if (perms0 && perms0[o]) {
3042d484f18aSToby Isaac                     PetscInt r, s;
3043d484f18aSToby Isaac 
3044d484f18aSToby Isaac                     ierr = PetscMalloc1(numComp[f] * numDof[depth - h],&fieldPerms[o+kConeSize]);CHKERRQ(ierr);
3045d484f18aSToby Isaac                     for (r = 0; r < numDof[depth - h]; r++) {
3046d484f18aSToby Isaac                       for (s = 0; s < numComp[f]; s++) {
3047d484f18aSToby Isaac                         fieldPerms[o+kConeSize][r * numComp[f] + s] = numComp[f] * perms0[o][r] + s;
3048d484f18aSToby Isaac                       }
3049d484f18aSToby Isaac                     }
3050d484f18aSToby Isaac                   }
3051d484f18aSToby Isaac                   if (flips0 && flips0[o]) {
3052d484f18aSToby Isaac                     PetscInt r, s;
3053d484f18aSToby Isaac 
3054d484f18aSToby Isaac                     ierr = PetscMalloc1(numComp[f] * numDof[depth - h],&fieldFlips[o+kConeSize]);CHKERRQ(ierr);
3055d484f18aSToby Isaac                     for (r = 0; r < numDof[depth - h]; r++) {
3056d484f18aSToby Isaac                       for (s = 0; s < numComp[f]; s++) {
3057d484f18aSToby Isaac                         fieldFlips[o+kConeSize][r * numComp[f] + s] = flips0[o][r];
3058d484f18aSToby Isaac                       }
3059d484f18aSToby Isaac                     }
3060d484f18aSToby Isaac                   }
3061d484f18aSToby Isaac                 }
3062d484f18aSToby Isaac                 ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numComp[f] * numDof[depth - h],-kConeSize,kConeSize,PETSC_OWN_POINTER,(const PetscInt **) fieldPerms,(const PetscScalar **)fieldFlips);CHKERRQ(ierr);
3063d484f18aSToby Isaac               }
3064d484f18aSToby Isaac             }
3065d484f18aSToby Isaac             ierr = PetscSectionSetFieldSym(*section,f,sym);CHKERRQ(ierr);
3066d484f18aSToby Isaac             ierr = PetscSectionSymDestroy(&sym);CHKERRQ(ierr);
3067d484f18aSToby Isaac           }
3068d484f18aSToby Isaac         }
3069552f7358SJed Brown       }
3070552f7358SJed Brown     }
3071552f7358SJed Brown   }
3072552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3073552f7358SJed Brown   ierr = PetscSectionSetChart(*section, pStart, pEnd);CHKERRQ(ierr);
3074916f0f19SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
30759ac3fadcSMatthew G. Knepley   ierr = PetscMalloc1(depth+1,&pMax);CHKERRQ(ierr);
30769ac3fadcSMatthew 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);
3077916f0f19SMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
3078916f0f19SMatthew G. Knepley     d    = dim == depth ? dep : (!dep ? 0 : dim);
3079916f0f19SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dep, &pStart, &pEnd);CHKERRQ(ierr);
3080fa716be8SMatthew G. Knepley     pMax[dep] = pMax[dep] < 0 ? pEnd : pMax[dep];
3081552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
3082ee55978aSMatthew G. Knepley       PetscInt tot = 0;
3083ee55978aSMatthew G. Knepley 
3084552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3085fa716be8SMatthew G. Knepley         if (isFE[f] && p >= pMax[dep]) continue;
3086552f7358SJed Brown         ierr = PetscSectionSetFieldDof(*section, p, f, numDof[f*(dim+1)+d]);CHKERRQ(ierr);
3087ee55978aSMatthew G. Knepley         tot += numDof[f*(dim+1)+d];
3088552f7358SJed Brown       }
3089ee55978aSMatthew G. Knepley       ierr = PetscSectionSetDof(*section, p, tot);CHKERRQ(ierr);
3090552f7358SJed Brown     }
3091552f7358SJed Brown   }
30929ac3fadcSMatthew G. Knepley   ierr = PetscFree(pMax);CHKERRQ(ierr);
3093fa716be8SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
3094552f7358SJed Brown   PetscFunctionReturn(0);
3095552f7358SJed Brown }
3096552f7358SJed Brown 
3097552f7358SJed Brown #undef __FUNCT__
3098552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSectionBCDof"
3099552f7358SJed Brown /* Set the number of dof on each point and separate by fields
3100ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3101552f7358SJed Brown */
3102ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSectionBCDof(DM dm, PetscInt numBC, const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
3103a6dfd86eSKarl Rupp {
3104552f7358SJed Brown   PetscInt       numFields;
3105552f7358SJed Brown   PetscInt       bc;
3106a68b90caSToby Isaac   PetscSection   aSec;
3107552f7358SJed Brown   PetscErrorCode ierr;
3108552f7358SJed Brown 
3109552f7358SJed Brown   PetscFunctionBegin;
3110552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3111552f7358SJed Brown   for (bc = 0; bc < numBC; ++bc) {
3112552f7358SJed Brown     PetscInt        field = 0;
3113ab1d0545SMatthew G. Knepley     const PetscInt *comp;
3114552f7358SJed Brown     const PetscInt *idx;
3115ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3116552f7358SJed Brown 
31170d644c17SKarl Rupp     if (numFields) field = bcField[bc];
3118ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3119ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3120552f7358SJed Brown     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3121552f7358SJed Brown     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3122552f7358SJed Brown     for (i = 0; i < n; ++i) {
3123552f7358SJed Brown       const PetscInt p = idx[i];
312406ff1081SMatthew G. Knepley       PetscInt       numConst;
3125552f7358SJed Brown 
3126552f7358SJed Brown       if (numFields) {
3127552f7358SJed Brown         ierr = PetscSectionGetFieldDof(section, p, field, &numConst);CHKERRQ(ierr);
3128552f7358SJed Brown       } else {
3129552f7358SJed Brown         ierr = PetscSectionGetDof(section, p, &numConst);CHKERRQ(ierr);
3130552f7358SJed Brown       }
313106ff1081SMatthew G. Knepley       /* If Nc < 0, constrain every dof on the point */
313206ff1081SMatthew G. Knepley       if (Nc > 0) numConst = PetscMin(numConst, Nc);
313306ff1081SMatthew G. Knepley       if (numFields) {ierr = PetscSectionAddFieldConstraintDof(section, p, field, numConst);CHKERRQ(ierr);}
3134552f7358SJed Brown       ierr = PetscSectionAddConstraintDof(section, p, numConst);CHKERRQ(ierr);
3135552f7358SJed Brown     }
3136552f7358SJed Brown     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
313706ff1081SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3138552f7358SJed Brown   }
3139a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3140a68b90caSToby Isaac   if (aSec) {
3141a68b90caSToby Isaac     PetscInt aStart, aEnd, a;
3142a68b90caSToby Isaac 
3143a68b90caSToby Isaac     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3144a68b90caSToby Isaac     for (a = aStart; a < aEnd; a++) {
3145ab1d0545SMatthew G. Knepley       PetscInt dof, f;
3146a68b90caSToby Isaac 
3147a68b90caSToby Isaac       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3148a68b90caSToby Isaac       if (dof) {
3149a68b90caSToby Isaac         /* if there are point-to-point constraints, then all dofs are constrained */
3150a68b90caSToby Isaac         ierr = PetscSectionGetDof(section, a, &dof);CHKERRQ(ierr);
3151a68b90caSToby Isaac         ierr = PetscSectionSetConstraintDof(section, a, dof);CHKERRQ(ierr);
3152a68b90caSToby Isaac         for (f = 0; f < numFields; f++) {
3153a68b90caSToby Isaac           ierr = PetscSectionGetFieldDof(section, a, f, &dof);CHKERRQ(ierr);
3154a68b90caSToby Isaac           ierr = PetscSectionSetFieldConstraintDof(section, a, f, dof);CHKERRQ(ierr);
3155a68b90caSToby Isaac         }
3156a68b90caSToby Isaac       }
3157a68b90caSToby Isaac     }
3158a68b90caSToby Isaac   }
3159552f7358SJed Brown   PetscFunctionReturn(0);
3160552f7358SJed Brown }
3161552f7358SJed Brown 
3162552f7358SJed Brown #undef __FUNCT__
3163ab1d0545SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateSectionBCIndicesField"
3164ab1d0545SMatthew G. Knepley /* Set the constrained field indices on each point
3165ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3166ab1d0545SMatthew G. Knepley */
3167ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSectionBCIndicesField(DM dm, PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
31680adebc6cSBarry Smith {
3169ab1d0545SMatthew G. Knepley   PetscSection   aSec;
3170ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3171ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, bc, f, d;
3172552f7358SJed Brown   PetscErrorCode ierr;
3173552f7358SJed Brown 
3174552f7358SJed Brown   PetscFunctionBegin;
3175552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3176ab1d0545SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
3177ab1d0545SMatthew G. Knepley   /* Initialize all field indices to -1 */
3178552f7358SJed Brown   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3179ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3180ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3181ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3182ab1d0545SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) for (f = 0; f < numFields; ++f) {ierr = PetscSectionSetFieldConstraintIndices(section, p, f, indices);CHKERRQ(ierr);}
3183ab1d0545SMatthew G. Knepley   /* Handle BC constraints */
3184ab1d0545SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {
3185ab1d0545SMatthew G. Knepley     const PetscInt  field = bcField[bc];
3186ab1d0545SMatthew G. Knepley     const PetscInt *comp, *idx;
3187ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3188552f7358SJed Brown 
3189ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3190ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3191ab1d0545SMatthew G. Knepley     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3192ab1d0545SMatthew G. Knepley     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3193ab1d0545SMatthew G. Knepley     for (i = 0; i < n; ++i) {
3194ab1d0545SMatthew G. Knepley       const PetscInt  p = idx[i];
3195ab1d0545SMatthew G. Knepley       const PetscInt *find;
3196ab1d0545SMatthew G. Knepley       PetscInt        fcdof, c;
3197ab1d0545SMatthew G. Knepley 
3198ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintDof(section, p, field, &fcdof);CHKERRQ(ierr);
3199ab1d0545SMatthew G. Knepley       if (Nc < 0) {
3200ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) indices[d] = d;
3201552f7358SJed Brown       } else {
3202ab1d0545SMatthew G. Knepley         ierr = PetscSectionGetFieldConstraintIndices(section, p, field, &find);CHKERRQ(ierr);
3203ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) {if (find[d] < 0) break; indices[d] = find[d];}
3204ab1d0545SMatthew G. Knepley         for (c = 0; c < Nc; ++c) indices[d+c] = comp[c];
3205ab1d0545SMatthew G. Knepley         ierr = PetscSortInt(d+Nc, indices);CHKERRQ(ierr);
3206ab1d0545SMatthew G. Knepley         for (c = d+Nc; c < fcdof; ++c) indices[c] = -1;
3207552f7358SJed Brown       }
3208ab1d0545SMatthew G. Knepley       ierr = PetscSectionSetFieldConstraintIndices(section, p, field, indices);CHKERRQ(ierr);
3209552f7358SJed Brown     }
3210ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3211ab1d0545SMatthew G. Knepley     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3212552f7358SJed Brown   }
3213ab1d0545SMatthew G. Knepley   /* Handle anchors */
3214ab1d0545SMatthew G. Knepley   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3215ab1d0545SMatthew G. Knepley   if (aSec) {
3216ab1d0545SMatthew G. Knepley     PetscInt aStart, aEnd, a;
3217552f7358SJed Brown 
3218ab1d0545SMatthew G. Knepley     for (d = 0; d < maxDof; ++d) indices[d] = d;
3219ab1d0545SMatthew G. Knepley     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3220ab1d0545SMatthew G. Knepley     for (a = aStart; a < aEnd; a++) {
3221ab1d0545SMatthew G. Knepley       PetscInt dof, fdof, f;
3222ab1d0545SMatthew G. Knepley 
3223ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3224ab1d0545SMatthew G. Knepley       if (dof) {
3225ab1d0545SMatthew G. Knepley         /* if there are point-to-point constraints, then all dofs are constrained */
3226ab1d0545SMatthew G. Knepley         for (f = 0; f < numFields; f++) {
3227ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, a, f, &fdof);CHKERRQ(ierr);
3228ab1d0545SMatthew G. Knepley           ierr = PetscSectionSetFieldConstraintIndices(section, a, f, indices);CHKERRQ(ierr);
3229ab1d0545SMatthew G. Knepley         }
3230ab1d0545SMatthew G. Knepley       }
3231ab1d0545SMatthew G. Knepley     }
3232ab1d0545SMatthew G. Knepley   }
3233ab1d0545SMatthew G. Knepley   ierr = PetscFree(indices);CHKERRQ(ierr);
3234ab1d0545SMatthew G. Knepley   PetscFunctionReturn(0);
3235ab1d0545SMatthew G. Knepley }
3236ab1d0545SMatthew G. Knepley 
3237ab1d0545SMatthew G. Knepley #undef __FUNCT__
3238ab1d0545SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateSectionBCIndices"
3239ab1d0545SMatthew G. Knepley /* Set the constrained indices on each point */
3240ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSectionBCIndices(DM dm, PetscSection section)
3241ab1d0545SMatthew G. Knepley {
3242ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3243ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, f, d;
3244ab1d0545SMatthew G. Knepley   PetscErrorCode ierr;
3245ab1d0545SMatthew G. Knepley 
3246ab1d0545SMatthew G. Knepley   PetscFunctionBegin;
3247ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3248ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3249ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3250ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3251ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3252552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3253552f7358SJed Brown     PetscInt cdof, d;
3254552f7358SJed Brown 
3255552f7358SJed Brown     ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
3256552f7358SJed Brown     if (cdof) {
3257552f7358SJed Brown       if (numFields) {
3258552f7358SJed Brown         PetscInt numConst = 0, foff = 0;
3259552f7358SJed Brown 
3260552f7358SJed Brown         for (f = 0; f < numFields; ++f) {
3261ab1d0545SMatthew G. Knepley           const PetscInt *find;
3262ab1d0545SMatthew G. Knepley           PetscInt        fcdof, fdof;
3263552f7358SJed Brown 
3264552f7358SJed Brown           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
3265ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
3266ab1d0545SMatthew G. Knepley           /* Change constraint numbering from field component to local dof number */
3267ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintIndices(section, p, f, &find);CHKERRQ(ierr);
3268ab1d0545SMatthew G. Knepley           for (d = 0; d < fcdof; ++d) indices[numConst+d] = find[d] + foff;
3269ab1d0545SMatthew G. Knepley           numConst += fcdof;
3270552f7358SJed Brown           foff     += fdof;
3271552f7358SJed Brown         }
3272552f7358SJed Brown         if (cdof != numConst) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Total number of field constraints %D should be %D", numConst, cdof);
3273552f7358SJed Brown       } else {
32740d644c17SKarl Rupp         for (d = 0; d < cdof; ++d) indices[d] = d;
3275552f7358SJed Brown       }
3276552f7358SJed Brown       ierr = PetscSectionSetConstraintIndices(section, p, indices);CHKERRQ(ierr);
3277552f7358SJed Brown     }
3278552f7358SJed Brown   }
3279552f7358SJed Brown   ierr = PetscFree(indices);CHKERRQ(ierr);
3280552f7358SJed Brown   PetscFunctionReturn(0);
3281552f7358SJed Brown }
3282552f7358SJed Brown 
3283552f7358SJed Brown #undef __FUNCT__
3284552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSection"
3285552f7358SJed Brown /*@C
3286552f7358SJed Brown   DMPlexCreateSection - Create a PetscSection based upon the dof layout specification provided.
3287552f7358SJed Brown 
3288552f7358SJed Brown   Not Collective
3289552f7358SJed Brown 
3290552f7358SJed Brown   Input Parameters:
3291552f7358SJed Brown + dm        - The DMPlex object
3292552f7358SJed Brown . dim       - The spatial dimension of the problem
3293552f7358SJed Brown . numFields - The number of fields in the problem
3294552f7358SJed Brown . numComp   - An array of size numFields that holds the number of components for each field
3295552f7358SJed 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
3296552f7358SJed Brown . numBC     - The number of boundary conditions
3297552f7358SJed Brown . bcField   - An array of size numBC giving the field number for each boundry condition
3298ab1d0545SMatthew G. Knepley . bcComps   - [Optional] An array of size numBC giving an IS holding the field components to which each boundary condition applies
3299ab1d0545SMatthew G. Knepley . bcPoints  - An array of size numBC giving an IS holding the Plex points to which each boundary condition applies
3300f8f126e8SMatthew G. Knepley - perm      - Optional permutation of the chart, or NULL
3301552f7358SJed Brown 
3302552f7358SJed Brown   Output Parameter:
3303552f7358SJed Brown . section - The PetscSection object
3304552f7358SJed Brown 
3305552f7358SJed 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
3306f8f126e8SMatthew G. Knepley   number of dof for field 0 on each edge.
3307f8f126e8SMatthew G. Knepley 
3308f8f126e8SMatthew G. Knepley   The chart permutation is the same one set using PetscSectionSetPermutation()
3309552f7358SJed Brown 
3310552f7358SJed Brown   Level: developer
3311552f7358SJed Brown 
33123813dfbdSMatthew G Knepley   Fortran Notes:
33133813dfbdSMatthew G Knepley   A Fortran 90 version is available as DMPlexCreateSectionF90()
33143813dfbdSMatthew G Knepley 
3315552f7358SJed Brown .keywords: mesh, elements
3316f8f126e8SMatthew G. Knepley .seealso: DMPlexCreate(), PetscSectionCreate(), PetscSectionSetPermutation()
3317552f7358SJed Brown @*/
3318ab1d0545SMatthew 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)
3319a6dfd86eSKarl Rupp {
3320a68b90caSToby Isaac   PetscSection   aSec;
3321552f7358SJed Brown   PetscErrorCode ierr;
3322552f7358SJed Brown 
3323552f7358SJed Brown   PetscFunctionBegin;
3324552f7358SJed Brown   ierr = DMPlexCreateSectionInitial(dm, dim, numFields, numComp, numDof, section);CHKERRQ(ierr);
3325ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSectionBCDof(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3326f8f126e8SMatthew G. Knepley   if (perm) {ierr = PetscSectionSetPermutation(*section, perm);CHKERRQ(ierr);}
3327552f7358SJed Brown   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
3328a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,NULL);CHKERRQ(ierr);
3329ab1d0545SMatthew G. Knepley   if (numBC || aSec) {
3330ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndicesField(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3331ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndices(dm, *section);CHKERRQ(ierr);
3332ab1d0545SMatthew G. Knepley   }
3333ce1779c8SBarry Smith   ierr = PetscSectionViewFromOptions(*section,NULL,"-section_view");CHKERRQ(ierr);
3334552f7358SJed Brown   PetscFunctionReturn(0);
3335552f7358SJed Brown }
3336552f7358SJed Brown 
3337552f7358SJed Brown #undef __FUNCT__
3338552f7358SJed Brown #define __FUNCT__ "DMCreateCoordinateDM_Plex"
33390adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
33400adebc6cSBarry Smith {
3341efe440bfSMatthew G. Knepley   PetscSection   section, s;
3342efe440bfSMatthew G. Knepley   Mat            m;
33433e922f36SToby Isaac   PetscInt       maxHeight;
3344552f7358SJed Brown   PetscErrorCode ierr;
3345552f7358SJed Brown 
3346552f7358SJed Brown   PetscFunctionBegin;
334738221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
33483e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
33493e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
335082f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
3351552f7358SJed Brown   ierr = DMSetDefaultSection(*cdm, section);CHKERRQ(ierr);
33521d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3353efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3354efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3355efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3356efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3357efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
3358552f7358SJed Brown   PetscFunctionReturn(0);
3359552f7358SJed Brown }
3360552f7358SJed Brown 
3361552f7358SJed Brown #undef __FUNCT__
3362552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeSection"
33630adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
33640adebc6cSBarry Smith {
3365552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3366552f7358SJed Brown 
3367552f7358SJed Brown   PetscFunctionBegin;
3368552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3369552f7358SJed Brown   if (section) *section = mesh->coneSection;
3370552f7358SJed Brown   PetscFunctionReturn(0);
3371552f7358SJed Brown }
3372552f7358SJed Brown 
3373552f7358SJed Brown #undef __FUNCT__
33748cb4d582SMatthew G. Knepley #define __FUNCT__ "DMPlexGetSupportSection"
33758cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
33768cb4d582SMatthew G. Knepley {
33778cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
33788cb4d582SMatthew G. Knepley 
33798cb4d582SMatthew G. Knepley   PetscFunctionBegin;
33808cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33818cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
33828cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
33838cb4d582SMatthew G. Knepley }
33848cb4d582SMatthew G. Knepley 
33858cb4d582SMatthew G. Knepley #undef __FUNCT__
3386552f7358SJed Brown #define __FUNCT__ "DMPlexGetCones"
3387a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3388a6dfd86eSKarl Rupp {
3389552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3390552f7358SJed Brown 
3391552f7358SJed Brown   PetscFunctionBegin;
3392552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3393552f7358SJed Brown   if (cones) *cones = mesh->cones;
3394552f7358SJed Brown   PetscFunctionReturn(0);
3395552f7358SJed Brown }
3396552f7358SJed Brown 
3397552f7358SJed Brown #undef __FUNCT__
3398552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeOrientations"
3399a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3400a6dfd86eSKarl Rupp {
3401552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3402552f7358SJed Brown 
3403552f7358SJed Brown   PetscFunctionBegin;
3404552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3405552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3406552f7358SJed Brown   PetscFunctionReturn(0);
3407552f7358SJed Brown }
3408552f7358SJed Brown 
3409552f7358SJed Brown /******************************** FEM Support **********************************/
3410552f7358SJed Brown 
3411552f7358SJed Brown #undef __FUNCT__
34123194fc30SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateSpectralClosurePermutation"
34133194fc30SMatthew G. Knepley PetscErrorCode DMPlexCreateSpectralClosurePermutation(DM dm, PetscSection section)
34143194fc30SMatthew G. Knepley {
34153194fc30SMatthew G. Knepley   PetscInt      *perm;
341689eabcffSMatthew G. Knepley   PetscInt       dim, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
34173194fc30SMatthew G. Knepley   PetscErrorCode ierr;
34183194fc30SMatthew G. Knepley 
34193194fc30SMatthew G. Knepley   PetscFunctionBegin;
34203194fc30SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
34213194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
34223194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
342389eabcffSMatthew G. Knepley   if (dim <= 1) PetscFunctionReturn(0);
34243194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
34253194fc30SMatthew G. Knepley     /* An order k SEM disc has k-1 dofs on an edge */
34263194fc30SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
34273194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
34283194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
34293194fc30SMatthew G. Knepley     k = k/Nc + 1;
343089eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
34313194fc30SMatthew G. Knepley   }
34323194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
34333194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
343489eabcffSMatthew G. Knepley     switch (dim) {
343589eabcffSMatthew G. Knepley     case 2:
34363194fc30SMatthew 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} */
34373194fc30SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
34383194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
34393194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
34403194fc30SMatthew G. Knepley       k = k/Nc + 1;
34413194fc30SMatthew G. Knepley       /* The SEM order is
34423194fc30SMatthew G. Knepley 
34433194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
344489eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
34453194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
34463194fc30SMatthew G. Knepley       */
34473194fc30SMatthew G. Knepley       {
34483194fc30SMatthew G. Knepley         const PetscInt of   = 0;
34493194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
34503194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
34513194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
34523194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
34533194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
34543194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
34553194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
34563194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
34573194fc30SMatthew G. Knepley         PetscInt       o;
34583194fc30SMatthew G. Knepley 
34593194fc30SMatthew G. Knepley         /* bottom */
34603194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
34613194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
34623194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
34633194fc30SMatthew G. Knepley         /* middle */
34643194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
34653194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
34663194fc30SMatthew 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;
34673194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
34683194fc30SMatthew G. Knepley         }
34693194fc30SMatthew G. Knepley         /* top */
34703194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
34713194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
34723194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
34733194fc30SMatthew G. Knepley         foffset = offset;
34743194fc30SMatthew G. Knepley       }
347589eabcffSMatthew G. Knepley       break;
347689eabcffSMatthew G. Knepley     case 3:
347789eabcffSMatthew G. Knepley       /* The original hex closure is
347889eabcffSMatthew G. Knepley 
347989eabcffSMatthew G. Knepley          {c,
348089eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
348189eabcffSMatthew 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,
348289eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
348389eabcffSMatthew G. Knepley       */
348489eabcffSMatthew G. Knepley       ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
348589eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
348689eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
348789eabcffSMatthew G. Knepley       k = k/Nc + 1;
348889eabcffSMatthew G. Knepley       /* The SEM order is
348989eabcffSMatthew G. Knepley          Bottom Slice
349089eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
349189eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
349289eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
349389eabcffSMatthew G. Knepley 
349489eabcffSMatthew G. Knepley          Middle Slice (j)
349589eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
349689eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
349789eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
349889eabcffSMatthew G. Knepley 
349989eabcffSMatthew G. Knepley          Top Slice
350089eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
350189eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
350289eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
350389eabcffSMatthew G. Knepley       */
350489eabcffSMatthew G. Knepley       {
350589eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
350689eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
350789eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
350889eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
350989eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
351089eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
351189eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
351289eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
351389eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
351489eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
351589eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
351689eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
351789eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
351889eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
351989eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
352089eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
352189eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
352289eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
352389eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
352489eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
352589eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
352689eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
352789eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
352889eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
352989eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
353089eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
353189eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
353289eabcffSMatthew G. Knepley         PetscInt       o, n;
353389eabcffSMatthew G. Knepley 
353489eabcffSMatthew G. Knepley         /* Bottom Slice */
353589eabcffSMatthew G. Knepley         /*   bottom */
353689eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
353789eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
353889eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
353989eabcffSMatthew G. Knepley         /*   middle */
354089eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
354189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
3542316b7f87SMax 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;}
354389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
35443194fc30SMatthew G. Knepley         }
354589eabcffSMatthew G. Knepley         /*   top */
354689eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
354789eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
354889eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
354989eabcffSMatthew G. Knepley 
355089eabcffSMatthew G. Knepley         /* Middle Slice */
355189eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
355289eabcffSMatthew G. Knepley           /*   bottom */
355389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
355489eabcffSMatthew 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;
355589eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
355689eabcffSMatthew G. Knepley           /*   middle */
355789eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
355889eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
355989eabcffSMatthew 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;
356089eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
356189eabcffSMatthew G. Knepley           }
356289eabcffSMatthew G. Knepley           /*   top */
356389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
356489eabcffSMatthew 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;
356589eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
356689eabcffSMatthew G. Knepley         }
356789eabcffSMatthew G. Knepley 
356889eabcffSMatthew G. Knepley         /* Top Slice */
356989eabcffSMatthew G. Knepley         /*   bottom */
357089eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
357189eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
357289eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
357389eabcffSMatthew G. Knepley         /*   middle */
357489eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
357589eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
357689eabcffSMatthew 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;
357789eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
357889eabcffSMatthew G. Knepley         }
357989eabcffSMatthew G. Knepley         /*   top */
358089eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
358189eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
358289eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
358389eabcffSMatthew G. Knepley 
358489eabcffSMatthew G. Knepley         foffset = offset;
358589eabcffSMatthew G. Knepley       }
358689eabcffSMatthew G. Knepley       break;
358789eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
358889eabcffSMatthew G. Knepley     }
358989eabcffSMatthew G. Knepley   }
359089eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
35913194fc30SMatthew G. Knepley   /* Check permutation */
35923194fc30SMatthew G. Knepley   {
35933194fc30SMatthew G. Knepley     PetscInt *check;
35943194fc30SMatthew G. Knepley 
35953194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
35963194fc30SMatthew 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]);}
35973194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
35983194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
35993194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
36003194fc30SMatthew G. Knepley   }
36011fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
36023194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
36033194fc30SMatthew G. Knepley }
36043194fc30SMatthew G. Knepley 
36053194fc30SMatthew G. Knepley #undef __FUNCT__
3606e071409bSToby Isaac #define __FUNCT__ "DMPlexGetPointDualSpaceFEM"
3607e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
3608e071409bSToby Isaac {
3609e071409bSToby Isaac   PetscDS        prob;
3610e071409bSToby Isaac   PetscInt       depth, Nf, h;
3611e071409bSToby Isaac   DMLabel        label;
3612e071409bSToby Isaac   PetscErrorCode ierr;
3613e071409bSToby Isaac 
3614e071409bSToby Isaac   PetscFunctionBeginHot;
3615e071409bSToby Isaac   prob    = dm->prob;
3616e071409bSToby Isaac   Nf      = prob->Nf;
3617e071409bSToby Isaac   label   = dm->depthLabel;
3618e071409bSToby Isaac   *dspace = NULL;
3619e071409bSToby Isaac   if (field < Nf) {
3620e071409bSToby Isaac     PetscObject disc = prob->disc[field];
3621e071409bSToby Isaac 
3622e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
3623e071409bSToby Isaac       PetscDualSpace dsp;
3624e071409bSToby Isaac 
3625e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
3626e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
3627e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
3628e071409bSToby Isaac       h    = depth - 1 - h;
3629e071409bSToby Isaac       if (h) {
3630e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
3631e071409bSToby Isaac       } else {
3632e071409bSToby Isaac         *dspace = dsp;
3633e071409bSToby Isaac       }
3634e071409bSToby Isaac     }
3635e071409bSToby Isaac   }
3636e071409bSToby Isaac   PetscFunctionReturn(0);
3637e071409bSToby Isaac }
3638e071409bSToby Isaac 
3639e071409bSToby Isaac 
3640e071409bSToby Isaac #undef __FUNCT__
36411a271a75SMatthew G. Knepley #define __FUNCT__ "DMPlexVecGetClosure_Depth1_Static"
36421a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3643a6dfd86eSKarl Rupp {
3644552f7358SJed Brown   PetscScalar    *array, *vArray;
3645d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
36461a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
3647552f7358SJed Brown   PetscErrorCode  ierr;
3648552f7358SJed Brown 
36491b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
36502a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
36515a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
36525a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
36535a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
36543f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
36559df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
36569df71ca4SMatthew G. Knepley       PetscInt dof;
3657d9917b9dSMatthew G. Knepley 
36589df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
36599df71ca4SMatthew G. Knepley       size += dof;
36609df71ca4SMatthew G. Knepley     }
36619df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
36629df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
36632a3aaacfSMatthew G. Knepley       PetscInt       dof;
36645a1bb5cfSMatthew G. Knepley 
36655a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
36662a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
36675a1bb5cfSMatthew G. Knepley       size += dof;
36685a1bb5cfSMatthew G. Knepley     }
36693f7cbbe7SMatthew G. Knepley     if (!values) {
36703f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
36713f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
36723f7cbbe7SMatthew G. Knepley     }
36735a1bb5cfSMatthew G. Knepley     ierr = DMGetWorkArray(dm, size, PETSC_SCALAR, &array);CHKERRQ(ierr);
3674982e9ed1SMatthew G. Knepley   } else {
3675982e9ed1SMatthew G. Knepley     array = *values;
3676982e9ed1SMatthew G. Knepley   }
36779df71ca4SMatthew G. Knepley   size = 0;
36785a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
36799df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
36809df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
36819df71ca4SMatthew G. Knepley     PetscScalar *varr;
3682d9917b9dSMatthew G. Knepley 
36839df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
36849df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
36859df71ca4SMatthew G. Knepley     varr = &vArray[off];
36861a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
36871a271a75SMatthew G. Knepley       array[offset] = varr[d];
36889df71ca4SMatthew G. Knepley     }
36899df71ca4SMatthew G. Knepley     size += dof;
36909df71ca4SMatthew G. Knepley   }
36919df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
36929df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
36939df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
36945a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
36955a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
36965a1bb5cfSMatthew G. Knepley 
369752ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
36985a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
36995a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
37005a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
37015a1bb5cfSMatthew G. Knepley     if (o >= 0) {
37021a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
37031a271a75SMatthew G. Knepley         array[offset] = varr[d];
37045a1bb5cfSMatthew G. Knepley       }
37055a1bb5cfSMatthew G. Knepley     } else {
37061a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
37071a271a75SMatthew G. Knepley         array[offset] = varr[d];
37085a1bb5cfSMatthew G. Knepley       }
37095a1bb5cfSMatthew G. Knepley     }
37109df71ca4SMatthew G. Knepley     size += dof;
37115a1bb5cfSMatthew G. Knepley   }
37125a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
37139df71ca4SMatthew G. Knepley   if (!*values) {
37145a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
37155a1bb5cfSMatthew G. Knepley     *values = array;
37169df71ca4SMatthew G. Knepley   } else {
37178ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
37188c312ff3SMatthew G. Knepley     *csize = size;
37199df71ca4SMatthew G. Knepley   }
37205a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
37215a1bb5cfSMatthew G. Knepley }
3722d9917b9dSMatthew G. Knepley 
3723d9917b9dSMatthew G. Knepley #undef __FUNCT__
3724923c78e0SToby Isaac #define __FUNCT__ "DMPlexGetCompressedClosure"
3725923c78e0SToby Isaac static PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3726923c78e0SToby Isaac {
3727923c78e0SToby Isaac   const PetscInt *cla;
3728923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
3729923c78e0SToby Isaac   PetscErrorCode ierr;
3730923c78e0SToby Isaac 
3731923c78e0SToby Isaac   PetscFunctionBeginHot;
3732923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
3733923c78e0SToby Isaac   if (!*clPoints) {
3734923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
3735923c78e0SToby Isaac 
3736923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3737923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
3738923c78e0SToby Isaac     /* Compress out points not in the section */
3739923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
3740923c78e0SToby Isaac       PetscInt r = pts[2*p];
3741923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
3742923c78e0SToby Isaac         pts[q*2]   = r;
3743923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
3744923c78e0SToby Isaac         ++q;
3745923c78e0SToby Isaac       }
3746923c78e0SToby Isaac     }
3747923c78e0SToby Isaac     np = q;
3748923c78e0SToby Isaac     cla = NULL;
3749923c78e0SToby Isaac   } else {
3750923c78e0SToby Isaac     PetscInt dof, off;
3751923c78e0SToby Isaac 
3752923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
3753923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
3754923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
3755923c78e0SToby Isaac     np   = dof/2;
3756923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
3757923c78e0SToby Isaac   }
3758923c78e0SToby Isaac   *numPoints = np;
3759923c78e0SToby Isaac   *points    = pts;
3760923c78e0SToby Isaac   *clp       = cla;
3761923c78e0SToby Isaac 
3762923c78e0SToby Isaac   PetscFunctionReturn(0);
3763923c78e0SToby Isaac }
3764923c78e0SToby Isaac 
3765923c78e0SToby Isaac #undef __FUNCT__
3766923c78e0SToby Isaac #define __FUNCT__ "DMPlexRestoreCompressedClosure"
3767923c78e0SToby Isaac static PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3768923c78e0SToby Isaac {
3769923c78e0SToby Isaac   PetscErrorCode ierr;
3770923c78e0SToby Isaac 
3771923c78e0SToby Isaac   PetscFunctionBeginHot;
3772923c78e0SToby Isaac   if (!*clPoints) {
3773923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
3774923c78e0SToby Isaac   } else {
3775923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
3776923c78e0SToby Isaac   }
3777923c78e0SToby Isaac   *numPoints = 0;
3778923c78e0SToby Isaac   *points    = NULL;
3779923c78e0SToby Isaac   *clSec     = NULL;
3780923c78e0SToby Isaac   *clPoints  = NULL;
3781923c78e0SToby Isaac   *clp       = NULL;
3782923c78e0SToby Isaac   PetscFunctionReturn(0);
3783923c78e0SToby Isaac }
3784923c78e0SToby Isaac 
3785923c78e0SToby Isaac #undef __FUNCT__
37861a271a75SMatthew G. Knepley #define __FUNCT__ "DMPlexVecGetClosure_Static"
378797e99dd9SToby 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[])
37881a271a75SMatthew G. Knepley {
37891a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
379097e99dd9SToby Isaac   const PetscInt    **perms = NULL;
379197e99dd9SToby Isaac   const PetscScalar **flips = NULL;
37921a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
37931a271a75SMatthew G. Knepley 
37941a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3795fe02ba77SJed Brown   *size = 0;
379697e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
379797e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
379897e99dd9SToby Isaac     const PetscInt    point = points[2*p];
379997e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
380097e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
38011a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
38021a271a75SMatthew G. Knepley     const PetscScalar *varr;
38031a271a75SMatthew G. Knepley 
38041a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
38051a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
38061a271a75SMatthew G. Knepley     varr = &vArray[off];
380797e99dd9SToby Isaac     if (clperm) {
380897e99dd9SToby Isaac       if (perm) {
380997e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
38101a271a75SMatthew G. Knepley       } else {
381197e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
381297e99dd9SToby Isaac       }
381397e99dd9SToby Isaac       if (flip) {
381497e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
381597e99dd9SToby Isaac       }
381697e99dd9SToby Isaac     } else {
381797e99dd9SToby Isaac       if (perm) {
381897e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
381997e99dd9SToby Isaac       } else {
382097e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
382197e99dd9SToby Isaac       }
382297e99dd9SToby Isaac       if (flip) {
382397e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
38241a271a75SMatthew G. Knepley       }
38251a271a75SMatthew G. Knepley     }
382697e99dd9SToby Isaac     offset += dof;
382797e99dd9SToby Isaac   }
382897e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
38291a271a75SMatthew G. Knepley   *size = offset;
38301a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
38311a271a75SMatthew G. Knepley }
38321a271a75SMatthew G. Knepley 
38331a271a75SMatthew G. Knepley #undef __FUNCT__
38341a271a75SMatthew G. Knepley #define __FUNCT__ "DMPlexVecGetClosure_Fields_Static"
383597e99dd9SToby 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[])
38361a271a75SMatthew G. Knepley {
38371a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
38381a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
38391a271a75SMatthew G. Knepley 
38401a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3841fe02ba77SJed Brown   *size = 0;
38421a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
384397e99dd9SToby Isaac     PetscInt          p;
384497e99dd9SToby Isaac     const PetscInt    **perms = NULL;
384597e99dd9SToby Isaac     const PetscScalar **flips = NULL;
38461a271a75SMatthew G. Knepley 
384797e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
384897e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
384997e99dd9SToby Isaac       const PetscInt    point = points[2*p];
385097e99dd9SToby Isaac       PetscInt          fdof, foff, b;
38511a271a75SMatthew G. Knepley       const PetscScalar *varr;
385297e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
385397e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
38541a271a75SMatthew G. Knepley 
38551a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
38561a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
38571a271a75SMatthew G. Knepley       varr = &vArray[foff];
385897e99dd9SToby Isaac       if (clperm) {
385997e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
386097e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
386197e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
38621a271a75SMatthew G. Knepley       } else {
386397e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
386497e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
386597e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
38661a271a75SMatthew G. Knepley       }
386797e99dd9SToby Isaac       offset += fdof;
38681a271a75SMatthew G. Knepley     }
386997e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
38701a271a75SMatthew G. Knepley   }
38711a271a75SMatthew G. Knepley   *size = offset;
38721a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
38731a271a75SMatthew G. Knepley }
38741a271a75SMatthew G. Knepley 
38751a271a75SMatthew G. Knepley #undef __FUNCT__
3876552f7358SJed Brown #define __FUNCT__ "DMPlexVecGetClosure"
3877552f7358SJed Brown /*@C
3878552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
3879552f7358SJed Brown 
3880552f7358SJed Brown   Not collective
3881552f7358SJed Brown 
3882552f7358SJed Brown   Input Parameters:
3883552f7358SJed Brown + dm - The DM
3884552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
3885552f7358SJed Brown . v - The local vector
3886552f7358SJed Brown - point - The sieve point in the DM
3887552f7358SJed Brown 
3888552f7358SJed Brown   Output Parameters:
3889552f7358SJed Brown + csize - The number of values in the closure, or NULL
3890552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
3891552f7358SJed Brown 
3892552f7358SJed Brown   Fortran Notes:
3893552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
3894552f7358SJed Brown   include petsc.h90 in your code.
3895552f7358SJed Brown 
3896552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
3897552f7358SJed Brown 
3898552f7358SJed Brown   Level: intermediate
3899552f7358SJed Brown 
3900552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
3901552f7358SJed Brown @*/
3902552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3903552f7358SJed Brown {
3904552f7358SJed Brown   PetscSection       clSection;
3905d9917b9dSMatthew G. Knepley   IS                 clPoints;
39068a84db2dSMatthew G. Knepley   PetscScalar       *array;
39078a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
3908552f7358SJed Brown   PetscInt          *points = NULL;
39098f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
39101a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
3911552f7358SJed Brown   PetscErrorCode     ierr;
3912552f7358SJed Brown 
3913d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
3914552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3915552f7358SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
39161a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
39171a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
3918552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3919552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3920552f7358SJed Brown   if (depth == 1 && numFields < 2) {
39211a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
3922552f7358SJed Brown     PetscFunctionReturn(0);
3923552f7358SJed Brown   }
39241a271a75SMatthew G. Knepley   /* Get points */
3925923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
39261fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
39271a271a75SMatthew G. Knepley   /* Get array */
3928bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
39291a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
3930552f7358SJed Brown 
39311a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
3932552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
39331a271a75SMatthew G. Knepley       asize += dof;
3934552f7358SJed Brown     }
3935bd6c0d32SMatthew G. Knepley     if (!values) {
3936923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
39371a271a75SMatthew G. Knepley       if (csize) *csize = asize;
3938bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
3939bd6c0d32SMatthew G. Knepley     }
39401a271a75SMatthew G. Knepley     ierr = DMGetWorkArray(dm, asize, PETSC_SCALAR, &array);CHKERRQ(ierr);
3941d0f6b257SMatthew G. Knepley   } else {
3942d0f6b257SMatthew G. Knepley     array = *values;
3943d0f6b257SMatthew G. Knepley   }
39448a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
39451a271a75SMatthew G. Knepley   /* Get values */
394697e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
394797e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
39481a271a75SMatthew G. Knepley   /* Cleanup points */
3949923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
39501a271a75SMatthew G. Knepley   /* Cleanup array */
39518a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
3952d0f6b257SMatthew G. Knepley   if (!*values) {
3953552f7358SJed Brown     if (csize) *csize = size;
3954552f7358SJed Brown     *values = array;
3955d0f6b257SMatthew G. Knepley   } else {
3956f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
3957d0f6b257SMatthew G. Knepley     *csize = size;
3958d0f6b257SMatthew G. Knepley   }
3959552f7358SJed Brown   PetscFunctionReturn(0);
3960552f7358SJed Brown }
3961552f7358SJed Brown 
3962552f7358SJed Brown #undef __FUNCT__
3963552f7358SJed Brown #define __FUNCT__ "DMPlexVecRestoreClosure"
3964552f7358SJed Brown /*@C
3965552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
3966552f7358SJed Brown 
3967552f7358SJed Brown   Not collective
3968552f7358SJed Brown 
3969552f7358SJed Brown   Input Parameters:
3970552f7358SJed Brown + dm - The DM
39710298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
3972552f7358SJed Brown . v - The local vector
3973552f7358SJed Brown . point - The sieve point in the DM
39740298fd71SBarry Smith . csize - The number of values in the closure, or NULL
3975552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
3976552f7358SJed Brown 
39773813dfbdSMatthew G Knepley   Fortran Notes:
39783813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
39793813dfbdSMatthew G Knepley   include petsc.h90 in your code.
39803813dfbdSMatthew G Knepley 
39813813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
39823813dfbdSMatthew G Knepley 
3983552f7358SJed Brown   Level: intermediate
3984552f7358SJed Brown 
3985552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
3986552f7358SJed Brown @*/
39877c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3988a6dfd86eSKarl Rupp {
3989552f7358SJed Brown   PetscInt       size = 0;
3990552f7358SJed Brown   PetscErrorCode ierr;
3991552f7358SJed Brown 
3992552f7358SJed Brown   PetscFunctionBegin;
3993552f7358SJed Brown   /* Should work without recalculating size */
3994552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, size, PETSC_SCALAR, (void*) values);CHKERRQ(ierr);
3995552f7358SJed Brown   PetscFunctionReturn(0);
3996552f7358SJed Brown }
3997552f7358SJed Brown 
3998552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
3999552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
4000552f7358SJed Brown 
4001552f7358SJed Brown #undef __FUNCT__
4002552f7358SJed Brown #define __FUNCT__ "updatePoint_private"
400397e99dd9SToby 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[])
4004552f7358SJed Brown {
4005552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4006552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4007552f7358SJed Brown   PetscScalar    *a;
4008552f7358SJed Brown   PetscInt        off, cind = 0, k;
4009552f7358SJed Brown   PetscErrorCode  ierr;
4010552f7358SJed Brown 
4011552f7358SJed Brown   PetscFunctionBegin;
4012552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4013552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4014552f7358SJed Brown   a    = &array[off];
4015552f7358SJed Brown   if (!cdof || setBC) {
401697e99dd9SToby Isaac     if (clperm) {
401797e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
401897e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
4019552f7358SJed Brown     } else {
402097e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
402197e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4022552f7358SJed Brown     }
4023552f7358SJed Brown   } else {
4024552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
402597e99dd9SToby Isaac     if (clperm) {
402697e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
4027552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
402897e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4029552f7358SJed Brown         }
4030552f7358SJed Brown       } else {
4031552f7358SJed Brown         for (k = 0; k < dof; ++k) {
4032552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
403397e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
403497e99dd9SToby Isaac         }
403597e99dd9SToby Isaac       }
403697e99dd9SToby Isaac     } else {
403797e99dd9SToby Isaac       if (perm) {
403897e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
403997e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
404097e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
404197e99dd9SToby Isaac         }
404297e99dd9SToby Isaac       } else {
404397e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
404497e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
404597e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
404697e99dd9SToby Isaac         }
4047552f7358SJed Brown       }
4048552f7358SJed Brown     }
4049552f7358SJed Brown   }
4050552f7358SJed Brown   PetscFunctionReturn(0);
4051552f7358SJed Brown }
4052552f7358SJed Brown 
4053552f7358SJed Brown #undef __FUNCT__
4054a5e93ea8SMatthew G. Knepley #define __FUNCT__ "updatePointBC_private"
405597e99dd9SToby 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[])
4056a5e93ea8SMatthew G. Knepley {
4057a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
4058a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4059a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
4060a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
4061a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4062a5e93ea8SMatthew G. Knepley 
4063a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4064a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4065a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4066a5e93ea8SMatthew G. Knepley   a    = &array[off];
4067a5e93ea8SMatthew G. Knepley   if (cdof) {
4068a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
406997e99dd9SToby Isaac     if (clperm) {
407097e99dd9SToby Isaac       if (perm) {
4071a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4072a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
407397e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
407497e99dd9SToby Isaac             cind++;
4075a5e93ea8SMatthew G. Knepley           }
4076a5e93ea8SMatthew G. Knepley         }
4077a5e93ea8SMatthew G. Knepley       } else {
4078a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4079a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
408097e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
408197e99dd9SToby Isaac             cind++;
408297e99dd9SToby Isaac           }
408397e99dd9SToby Isaac         }
408497e99dd9SToby Isaac       }
408597e99dd9SToby Isaac     } else {
408697e99dd9SToby Isaac       if (perm) {
408797e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
408897e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
408997e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
409097e99dd9SToby Isaac             cind++;
409197e99dd9SToby Isaac           }
409297e99dd9SToby Isaac         }
409397e99dd9SToby Isaac       } else {
409497e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
409597e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
409697e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
409797e99dd9SToby Isaac             cind++;
409897e99dd9SToby Isaac           }
4099a5e93ea8SMatthew G. Knepley         }
4100a5e93ea8SMatthew G. Knepley       }
4101a5e93ea8SMatthew G. Knepley     }
4102a5e93ea8SMatthew G. Knepley   }
4103a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4104a5e93ea8SMatthew G. Knepley }
4105a5e93ea8SMatthew G. Knepley 
4106a5e93ea8SMatthew G. Knepley #undef __FUNCT__
4107552f7358SJed Brown #define __FUNCT__ "updatePointFields_private"
410897e99dd9SToby 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[])
4109a6dfd86eSKarl Rupp {
4110552f7358SJed Brown   PetscScalar    *a;
41111a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
41121a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
411397e99dd9SToby Isaac   PetscInt        cind = 0, b;
4114552f7358SJed Brown   PetscErrorCode  ierr;
4115552f7358SJed Brown 
4116552f7358SJed Brown   PetscFunctionBegin;
4117552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4118552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
41191a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
41201a271a75SMatthew G. Knepley   a    = &array[foff];
4121552f7358SJed Brown   if (!fcdof || setBC) {
412297e99dd9SToby Isaac     if (clperm) {
412397e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
412497e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4125552f7358SJed Brown     } else {
412697e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
412797e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4128552f7358SJed Brown     }
4129552f7358SJed Brown   } else {
4130552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
413197e99dd9SToby Isaac     if (clperm) {
413297e99dd9SToby Isaac       if (perm) {
413397e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
413497e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
413597e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4136552f7358SJed Brown         }
4137552f7358SJed Brown       } else {
413897e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
413997e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
414097e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
414197e99dd9SToby Isaac         }
414297e99dd9SToby Isaac       }
414397e99dd9SToby Isaac     } else {
414497e99dd9SToby Isaac       if (perm) {
414597e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
414697e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
414797e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
414897e99dd9SToby Isaac         }
414997e99dd9SToby Isaac       } else {
415097e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
415197e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
415297e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4153552f7358SJed Brown         }
4154552f7358SJed Brown       }
4155552f7358SJed Brown     }
4156552f7358SJed Brown   }
41571a271a75SMatthew G. Knepley   *offset += fdof;
4158552f7358SJed Brown   PetscFunctionReturn(0);
4159552f7358SJed Brown }
4160552f7358SJed Brown 
4161552f7358SJed Brown #undef __FUNCT__
4162a5e93ea8SMatthew G. Knepley #define __FUNCT__ "updatePointFieldsBC_private"
416397e99dd9SToby 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[])
4164a5e93ea8SMatthew G. Knepley {
4165a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
41661a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
41671a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
416897e99dd9SToby Isaac   PetscInt        cind = 0, b;
4169a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4170a5e93ea8SMatthew G. Knepley 
4171a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4172a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4173a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
41741a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
41751a271a75SMatthew G. Knepley   a    = &array[foff];
4176a5e93ea8SMatthew G. Knepley   if (fcdof) {
4177a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
417897e99dd9SToby Isaac     if (clperm) {
417997e99dd9SToby Isaac       if (perm) {
418097e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
418197e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
418297e99dd9SToby Isaac             fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4183a5e93ea8SMatthew G. Knepley             ++cind;
4184a5e93ea8SMatthew G. Knepley           }
4185a5e93ea8SMatthew G. Knepley         }
4186a5e93ea8SMatthew G. Knepley       } else {
418797e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
418897e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
418997e99dd9SToby Isaac             fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
419097e99dd9SToby Isaac             ++cind;
419197e99dd9SToby Isaac           }
419297e99dd9SToby Isaac         }
419397e99dd9SToby Isaac       }
419497e99dd9SToby Isaac     } else {
419597e99dd9SToby Isaac       if (perm) {
419697e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
419797e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
419897e99dd9SToby Isaac             fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
419997e99dd9SToby Isaac             ++cind;
420097e99dd9SToby Isaac           }
420197e99dd9SToby Isaac         }
420297e99dd9SToby Isaac       } else {
420397e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
420497e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
420597e99dd9SToby Isaac             fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4206a5e93ea8SMatthew G. Knepley             ++cind;
4207a5e93ea8SMatthew G. Knepley           }
4208a5e93ea8SMatthew G. Knepley         }
4209a5e93ea8SMatthew G. Knepley       }
4210a5e93ea8SMatthew G. Knepley     }
4211a5e93ea8SMatthew G. Knepley   }
42121a271a75SMatthew G. Knepley   *offset += fdof;
4213a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4214a5e93ea8SMatthew G. Knepley }
4215a5e93ea8SMatthew G. Knepley 
4216a5e93ea8SMatthew G. Knepley #undef __FUNCT__
42178f3be42fSMatthew G. Knepley #define __FUNCT__ "DMPlexVecSetClosure_Depth1_Static"
42188f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4219a6dfd86eSKarl Rupp {
4220552f7358SJed Brown   PetscScalar    *array;
42211b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
42221b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4223552f7358SJed Brown   PetscErrorCode  ierr;
4224552f7358SJed Brown 
42251b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
4226b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4227b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
4228b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4229b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
4230b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4231b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4232b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
4233b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
4234b6ebb6e6SMatthew G. Knepley 
4235b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4236b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
4237b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
4238b6ebb6e6SMatthew G. Knepley     {
4239b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4240b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
4241b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
4242b6ebb6e6SMatthew G. Knepley 
4243b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
4244b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
4245b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
4246b6ebb6e6SMatthew G. Knepley       if (!cdof) {
4247b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4248b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4249b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4250b6ebb6e6SMatthew G. Knepley           }
4251b6ebb6e6SMatthew G. Knepley         } else {
4252b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4253b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4254b6ebb6e6SMatthew G. Knepley           }
4255b6ebb6e6SMatthew G. Knepley         }
4256b6ebb6e6SMatthew G. Knepley       } else {
4257b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
4258b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4259b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4260b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4261b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4262b6ebb6e6SMatthew G. Knepley           }
4263b6ebb6e6SMatthew G. Knepley         } else {
4264b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4265b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4266b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4267b6ebb6e6SMatthew G. Knepley           }
4268b6ebb6e6SMatthew G. Knepley         }
4269b6ebb6e6SMatthew G. Knepley       }
4270b6ebb6e6SMatthew G. Knepley     }
4271b6ebb6e6SMatthew G. Knepley   }
4272b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4273b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4274b6ebb6e6SMatthew G. Knepley }
42751b406b76SMatthew G. Knepley 
42761b406b76SMatthew G. Knepley #undef __FUNCT__
42771b406b76SMatthew G. Knepley #define __FUNCT__ "DMPlexVecSetClosure"
42781b406b76SMatthew G. Knepley /*@C
42791b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
42801b406b76SMatthew G. Knepley 
42811b406b76SMatthew G. Knepley   Not collective
42821b406b76SMatthew G. Knepley 
42831b406b76SMatthew G. Knepley   Input Parameters:
42841b406b76SMatthew G. Knepley + dm - The DM
42851b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
42861b406b76SMatthew G. Knepley . v - The local vector
42871b406b76SMatthew G. Knepley . point - The sieve point in the DM
42881b406b76SMatthew G. Knepley . values - The array of values
42891b406b76SMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
42901b406b76SMatthew G. Knepley 
42911b406b76SMatthew G. Knepley   Fortran Notes:
42921b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
42931b406b76SMatthew G. Knepley 
42941b406b76SMatthew G. Knepley   Level: intermediate
42951b406b76SMatthew G. Knepley 
42961b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
42971b406b76SMatthew G. Knepley @*/
42981b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
42991b406b76SMatthew G. Knepley {
43001b406b76SMatthew G. Knepley   PetscSection    clSection;
43011b406b76SMatthew G. Knepley   IS              clPoints;
43021b406b76SMatthew G. Knepley   PetscScalar    *array;
43031b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
430497e99dd9SToby Isaac   const PetscInt *clp, *clperm;
43051a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
43061b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
43071b406b76SMatthew G. Knepley 
43081a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
43091b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43101b406b76SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
43111a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43121a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
43131b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
43141b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
43151b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
43168f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
43171b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
43181b406b76SMatthew G. Knepley   }
43191a271a75SMatthew G. Knepley   /* Get points */
432097e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4321923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
43221a271a75SMatthew G. Knepley   /* Get array */
4323552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
43241a271a75SMatthew G. Knepley   /* Get values */
4325ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
432697e99dd9SToby Isaac     PetscInt offset = 0, f;
4327552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
432897e99dd9SToby Isaac       const PetscInt    **perms = NULL;
432997e99dd9SToby Isaac       const PetscScalar **flips = NULL;
433097e99dd9SToby Isaac 
433197e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4332552f7358SJed Brown       switch (mode) {
4333552f7358SJed Brown       case INSERT_VALUES:
433497e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
433597e99dd9SToby Isaac           const PetscInt    point = points[2*p];
433697e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
433797e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
433897e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4339552f7358SJed Brown         } break;
4340552f7358SJed Brown       case INSERT_ALL_VALUES:
434197e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
434297e99dd9SToby Isaac           const PetscInt    point = points[2*p];
434397e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
434497e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
434597e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4346552f7358SJed Brown         } break;
4347a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
434897e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
434997e99dd9SToby Isaac           const PetscInt    point = points[2*p];
435097e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
435197e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
435297e99dd9SToby Isaac           updatePointFieldsBC_private(section, point, perm, flip, f, insert, clperm, values, &offset, array);
4353a5e93ea8SMatthew G. Knepley         } break;
4354552f7358SJed Brown       case ADD_VALUES:
435597e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
435697e99dd9SToby Isaac           const PetscInt    point = points[2*p];
435797e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
435897e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
435997e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4360552f7358SJed Brown         } break;
4361552f7358SJed Brown       case ADD_ALL_VALUES:
436297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
436397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
436497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
436597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
436697e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4367552f7358SJed Brown         } break;
4368304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
436997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
437097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
437197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
437297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
437397e99dd9SToby Isaac           updatePointFieldsBC_private(section, point, perm, flip, f, add, clperm, values, &offset, array);
4374304ab55fSMatthew G. Knepley         } break;
4375552f7358SJed Brown       default:
4376f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4377552f7358SJed Brown       }
437897e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
43791a271a75SMatthew G. Knepley     }
4380552f7358SJed Brown   } else {
43811a271a75SMatthew G. Knepley     PetscInt dof, off;
438297e99dd9SToby Isaac     const PetscInt    **perms = NULL;
438397e99dd9SToby Isaac     const PetscScalar **flips = NULL;
43841a271a75SMatthew G. Knepley 
438597e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4386552f7358SJed Brown     switch (mode) {
4387552f7358SJed Brown     case INSERT_VALUES:
438897e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
438997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
439097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
439197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
439297e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
439397e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4394552f7358SJed Brown       } break;
4395552f7358SJed Brown     case INSERT_ALL_VALUES:
439697e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
439797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
439897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
439997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
440097e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
440197e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4402552f7358SJed Brown       } break;
4403a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
440497e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
440597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
440697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
440797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
440897e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
440997e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4410a5e93ea8SMatthew G. Knepley       } break;
4411552f7358SJed Brown     case ADD_VALUES:
441297e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
441397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
441497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
441597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
441697e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
441797e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4418552f7358SJed Brown       } break;
4419552f7358SJed Brown     case ADD_ALL_VALUES:
442097e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
442197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
442297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
442397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
442497e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
442597e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4426552f7358SJed Brown       } break;
4427304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
442897e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
442997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
443097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
443197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
443297e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
443397e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
4434304ab55fSMatthew G. Knepley       } break;
4435552f7358SJed Brown     default:
4436f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4437552f7358SJed Brown     }
443897e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4439552f7358SJed Brown   }
44401a271a75SMatthew G. Knepley   /* Cleanup points */
4441923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
44421a271a75SMatthew G. Knepley   /* Cleanup array */
4443552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4444552f7358SJed Brown   PetscFunctionReturn(0);
4445552f7358SJed Brown }
4446552f7358SJed Brown 
4447552f7358SJed Brown #undef __FUNCT__
4448e07394fbSMatthew G. Knepley #define __FUNCT__ "DMPlexVecSetFieldClosure_Internal"
4449e07394fbSMatthew G. Knepley PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, const PetscScalar values[], InsertMode mode)
4450e07394fbSMatthew G. Knepley {
4451e07394fbSMatthew G. Knepley   PetscSection      clSection;
4452e07394fbSMatthew G. Knepley   IS                clPoints;
4453e07394fbSMatthew G. Knepley   PetscScalar       *array;
4454e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
4455b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
4456e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
445797e99dd9SToby Isaac   PetscInt          offset = 0, f;
4458e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
4459e07394fbSMatthew G. Knepley 
4460e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
4461e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4462e07394fbSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
4463e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4464e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4465e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4466e07394fbSMatthew G. Knepley   /* Get points */
4467b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4468923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4469e07394fbSMatthew G. Knepley   /* Get array */
4470e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4471e07394fbSMatthew G. Knepley   /* Get values */
4472e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
447397e99dd9SToby Isaac     const PetscInt    **perms = NULL;
447497e99dd9SToby Isaac     const PetscScalar **flips = NULL;
447597e99dd9SToby Isaac 
4476e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
4477e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4478e07394fbSMatthew G. Knepley         PetscInt fdof;
4479e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
4480e07394fbSMatthew G. Knepley         offset += fdof;
4481e07394fbSMatthew G. Knepley       }
4482e07394fbSMatthew G. Knepley       continue;
4483e07394fbSMatthew G. Knepley     }
448497e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4485e07394fbSMatthew G. Knepley     switch (mode) {
4486e07394fbSMatthew G. Knepley     case INSERT_VALUES:
448797e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
448897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
448997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
449097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4491b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4492e07394fbSMatthew G. Knepley       } break;
4493e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
449497e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
449597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
449697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
449797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4498b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4499e07394fbSMatthew G. Knepley         } break;
4500e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
450197e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
450297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
450397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
450497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4505b04c866fSMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, insert, clperm, values, &offset, array);
4506e07394fbSMatthew G. Knepley       } break;
4507e07394fbSMatthew G. Knepley     case ADD_VALUES:
450897e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
450997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
451097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
451197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4512b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4513e07394fbSMatthew G. Knepley       } break;
4514e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
451597e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
451697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
451797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
451897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4519b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4520e07394fbSMatthew G. Knepley       } break;
4521e07394fbSMatthew G. Knepley     default:
4522f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4523e07394fbSMatthew G. Knepley     }
452497e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4525e07394fbSMatthew G. Knepley   }
4526e07394fbSMatthew G. Knepley   /* Cleanup points */
4527923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4528e07394fbSMatthew G. Knepley   /* Cleanup array */
4529e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4530e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
4531e07394fbSMatthew G. Knepley }
4532e07394fbSMatthew G. Knepley 
4533e07394fbSMatthew G. Knepley #undef __FUNCT__
4534552f7358SJed Brown #define __FUNCT__ "DMPlexPrintMatSetValues"
4535b0ecff45SMatthew G. Knepley PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
4536552f7358SJed Brown {
4537552f7358SJed Brown   PetscMPIInt    rank;
4538552f7358SJed Brown   PetscInt       i, j;
4539552f7358SJed Brown   PetscErrorCode ierr;
4540552f7358SJed Brown 
4541552f7358SJed Brown   PetscFunctionBegin;
454282f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
4543f347f43bSBarry Smith   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for sieve point %D\n", rank, point);CHKERRQ(ierr);
4544e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
4545e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
4546b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
4547b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
4548e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
4549b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
4550519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
45517eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
4552552f7358SJed Brown #else
4553b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
4554552f7358SJed Brown #endif
4555552f7358SJed Brown     }
455677a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
4557552f7358SJed Brown   }
4558552f7358SJed Brown   PetscFunctionReturn(0);
4559552f7358SJed Brown }
4560552f7358SJed Brown 
4561552f7358SJed Brown #undef __FUNCT__
4562415ce65aSMatthew G. Knepley #define __FUNCT__ "DMPlexGetIndicesPoint_Internal"
4563552f7358SJed Brown /* . off - The global offset of this point */
45644acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], PetscInt indices[])
4565a6dfd86eSKarl Rupp {
4566e6ccafaeSMatthew G Knepley   PetscInt        dof;    /* The number of unknowns on this point */
4567552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4568552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4569552f7358SJed Brown   PetscInt        cind = 0, k;
4570552f7358SJed Brown   PetscErrorCode  ierr;
4571552f7358SJed Brown 
4572552f7358SJed Brown   PetscFunctionBegin;
4573552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
4574552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4575552f7358SJed Brown   if (!cdof || setBC) {
45764acb8e1eSToby Isaac     if (perm) {
45774acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+perm[k]] = off + k;
4578552f7358SJed Brown     } else {
45794acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+k] = off + k;
4580552f7358SJed Brown     }
4581552f7358SJed Brown   } else {
4582552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
45834acb8e1eSToby Isaac     if (perm) {
45844acb8e1eSToby Isaac       for (k = 0; k < dof; ++k) {
45854acb8e1eSToby Isaac         if ((cind < cdof) && (k == cdofs[cind])) {
45864acb8e1eSToby Isaac           /* Insert check for returning constrained indices */
45874acb8e1eSToby Isaac           indices[*loff+perm[k]] = -(off+k+1);
45884acb8e1eSToby Isaac           ++cind;
45894acb8e1eSToby Isaac         } else {
45904acb8e1eSToby Isaac           indices[*loff+perm[k]] = off+k-cind;
45914acb8e1eSToby Isaac         }
45924acb8e1eSToby Isaac       }
45934acb8e1eSToby Isaac     } else {
4594552f7358SJed Brown       for (k = 0; k < dof; ++k) {
4595552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
4596552f7358SJed Brown           /* Insert check for returning constrained indices */
4597e6ccafaeSMatthew G Knepley           indices[*loff+k] = -(off+k+1);
4598552f7358SJed Brown           ++cind;
4599552f7358SJed Brown         } else {
4600e6ccafaeSMatthew G Knepley           indices[*loff+k] = off+k-cind;
4601552f7358SJed Brown         }
4602552f7358SJed Brown       }
4603552f7358SJed Brown     }
4604552f7358SJed Brown   }
4605e6ccafaeSMatthew G Knepley   *loff += dof;
4606552f7358SJed Brown   PetscFunctionReturn(0);
4607552f7358SJed Brown }
4608552f7358SJed Brown 
4609552f7358SJed Brown #undef __FUNCT__
4610415ce65aSMatthew G. Knepley #define __FUNCT__ "DMPlexGetIndicesPointFields_Internal"
4611552f7358SJed Brown /* . off - The global offset of this point */
46124acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, PetscInt indices[])
4613a6dfd86eSKarl Rupp {
4614552f7358SJed Brown   PetscInt       numFields, foff, f;
4615552f7358SJed Brown   PetscErrorCode ierr;
4616552f7358SJed Brown 
4617552f7358SJed Brown   PetscFunctionBegin;
4618552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4619552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
46204acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
4621552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
46224acb8e1eSToby Isaac     PetscInt        cind = 0, b;
46234acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
4624552f7358SJed Brown 
4625552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4626552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
4627552f7358SJed Brown     if (!cfdof || setBC) {
46284acb8e1eSToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {indices[foffs[f]+perm[b]] = off+foff+b;}}
46294acb8e1eSToby Isaac       else      {for (b = 0; b < fdof; b++) {indices[foffs[f]+     b ] = off+foff+b;}}
4630552f7358SJed Brown     } else {
4631552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
46324acb8e1eSToby Isaac       if (perm) {
46334acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
46344acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
46354acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = -(off+foff+b+1);
4636552f7358SJed Brown             ++cind;
4637552f7358SJed Brown           } else {
46384acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = off+foff+b-cind;
4639552f7358SJed Brown           }
4640552f7358SJed Brown         }
4641552f7358SJed Brown       } else {
46424acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
46434acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
46444acb8e1eSToby Isaac             indices[foffs[f]+b] = -(off+foff+b+1);
4645552f7358SJed Brown             ++cind;
4646552f7358SJed Brown           } else {
46474acb8e1eSToby Isaac             indices[foffs[f]+b] = off+foff+b-cind;
4648552f7358SJed Brown           }
4649552f7358SJed Brown         }
4650552f7358SJed Brown       }
4651552f7358SJed Brown     }
4652a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
4653552f7358SJed Brown     foffs[f] += fdof;
4654552f7358SJed Brown   }
4655552f7358SJed Brown   PetscFunctionReturn(0);
4656552f7358SJed Brown }
4657552f7358SJed Brown 
4658552f7358SJed Brown #undef __FUNCT__
4659f7c74593SToby Isaac #define __FUNCT__ "DMPlexAnchorsModifyMat"
46604acb8e1eSToby 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)
4661d3d1a6afSToby Isaac {
4662d3d1a6afSToby Isaac   Mat             cMat;
4663d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
4664d3d1a6afSToby Isaac   IS              aIS;
4665d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
4666d3d1a6afSToby Isaac   const PetscInt  *anchors;
4667e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
4668d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
4669d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
4670d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
4671d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
4672d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
4673d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
4674d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
46756ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
4676d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
4677d3d1a6afSToby Isaac   PetscErrorCode  ierr;
4678d3d1a6afSToby Isaac 
4679d3d1a6afSToby Isaac   PetscFunctionBegin;
4680d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4681d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4682d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4683d3d1a6afSToby Isaac 
4684a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
4685d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
4686d3d1a6afSToby Isaac   if (aSec) {
4687d3d1a6afSToby Isaac     ierr = PetscMemzero(newOffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
4688d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
4689d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
4690d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
4691d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
4692d3d1a6afSToby Isaac      * into the global matrix anyway) */
4693d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
4694d3d1a6afSToby Isaac       PetscInt b    = points[p];
46954b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4696d3d1a6afSToby Isaac 
46974b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
46984b2f2278SToby Isaac       if (!bSecDof) {
46994b2f2278SToby Isaac         continue;
47004b2f2278SToby Isaac       }
4701d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4702d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
4703d3d1a6afSToby Isaac       }
4704d3d1a6afSToby Isaac       if (bDof) {
4705d3d1a6afSToby Isaac         /* this point is constrained */
4706d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
4707d3d1a6afSToby Isaac         PetscInt bOff, q;
4708d3d1a6afSToby Isaac 
4709d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
4710d3d1a6afSToby Isaac         newNumPoints  += bDof;
4711d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
4712d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4713d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
4714d3d1a6afSToby Isaac           PetscInt aDof;
4715d3d1a6afSToby Isaac 
4716d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
4717d3d1a6afSToby Isaac           newNumIndices += aDof;
4718d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
4719d3d1a6afSToby Isaac             PetscInt fDof;
4720d3d1a6afSToby Isaac 
4721d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
4722d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
4723d3d1a6afSToby Isaac           }
4724d3d1a6afSToby Isaac         }
4725d3d1a6afSToby Isaac       }
4726d3d1a6afSToby Isaac       else {
4727d3d1a6afSToby Isaac         /* this point is not constrained */
4728d3d1a6afSToby Isaac         newNumPoints++;
47294b2f2278SToby Isaac         newNumIndices += bSecDof;
4730d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
4731d3d1a6afSToby Isaac           PetscInt fDof;
4732d3d1a6afSToby Isaac 
4733d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4734d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
4735d3d1a6afSToby Isaac         }
4736d3d1a6afSToby Isaac       }
4737d3d1a6afSToby Isaac     }
4738d3d1a6afSToby Isaac   }
4739d3d1a6afSToby Isaac   if (!anyConstrained) {
474072b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
474172b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
474272b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
474372b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
474472b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
4745d3d1a6afSToby Isaac     PetscFunctionReturn(0);
4746d3d1a6afSToby Isaac   }
4747d3d1a6afSToby Isaac 
47486ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
47496ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
47506ecaa68aSToby Isaac 
4751f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
4752d3d1a6afSToby Isaac 
47536ecaa68aSToby Isaac   if (!outPoints && !outValues) {
47546ecaa68aSToby Isaac     if (offsets) {
47556ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
47566ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
47576ecaa68aSToby Isaac       }
47586ecaa68aSToby Isaac     }
47596ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
47606ecaa68aSToby Isaac     PetscFunctionReturn(0);
47616ecaa68aSToby Isaac   }
47626ecaa68aSToby Isaac 
4763f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
4764d3d1a6afSToby Isaac 
4765f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
4766d3d1a6afSToby Isaac 
4767d3d1a6afSToby Isaac   /* workspaces */
4768d3d1a6afSToby Isaac   if (numFields) {
4769d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
4770d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
4771d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
4772d3d1a6afSToby Isaac     }
4773d3d1a6afSToby Isaac   }
4774d3d1a6afSToby Isaac   else {
4775d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
4776d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
4777d3d1a6afSToby Isaac   }
4778d3d1a6afSToby Isaac 
4779d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
4780d3d1a6afSToby Isaac   if (numFields) {
47814b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
47824b2f2278SToby Isaac 
4783d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4784d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
47854b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4786d3d1a6afSToby Isaac 
47874b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
47884b2f2278SToby Isaac       if (!bSecDof) {
47894b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
47904b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
47914b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
47924b2f2278SToby Isaac         }
47934b2f2278SToby Isaac         continue;
47944b2f2278SToby Isaac       }
4795d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4796d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4797d3d1a6afSToby Isaac       }
4798d3d1a6afSToby Isaac       if (bDof) {
4799d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4800d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
4801d3d1a6afSToby Isaac 
4802d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4803d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4804d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
4805d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
4806d3d1a6afSToby Isaac             PetscInt aFDof;
4807d3d1a6afSToby Isaac 
4808d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
4809d3d1a6afSToby Isaac             allFDof += aFDof;
4810d3d1a6afSToby Isaac           }
4811d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
4812d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
4813d3d1a6afSToby Isaac         }
4814d3d1a6afSToby Isaac       }
4815d3d1a6afSToby Isaac       else {
4816d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4817d3d1a6afSToby Isaac           PetscInt fDof;
4818d3d1a6afSToby Isaac 
4819d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4820d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
4821d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
4822d3d1a6afSToby Isaac         }
4823d3d1a6afSToby Isaac       }
4824d3d1a6afSToby Isaac     }
48254b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
48264b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
48274b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
4828d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
4829d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
4830d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
4831d3d1a6afSToby Isaac       }
483219f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
483319f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
4834d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
4835d3d1a6afSToby Isaac     }
4836d3d1a6afSToby Isaac   }
4837d3d1a6afSToby Isaac   else {
4838d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4839d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
48404b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4841d3d1a6afSToby Isaac 
48424b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
48434b2f2278SToby Isaac       if (!bSecDof) {
48444b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
48454b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
48464b2f2278SToby Isaac         continue;
48474b2f2278SToby Isaac       }
4848d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4849d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4850d3d1a6afSToby Isaac       }
4851d3d1a6afSToby Isaac       if (bDof) {
48524b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
4853d3d1a6afSToby Isaac 
4854d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4855d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4856d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
4857d3d1a6afSToby Isaac 
4858d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
4859d3d1a6afSToby Isaac           allDof += aDof;
4860d3d1a6afSToby Isaac         }
4861d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
48624b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
4863d3d1a6afSToby Isaac       }
4864d3d1a6afSToby Isaac       else {
48654b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
4866d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
4867d3d1a6afSToby Isaac       }
4868d3d1a6afSToby Isaac     }
4869d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
4870d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
4871d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4872d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
4873d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
4874d3d1a6afSToby Isaac     }
4875d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
4876d3d1a6afSToby Isaac   }
4877d3d1a6afSToby Isaac 
48786ecaa68aSToby Isaac   /* output arrays */
48796ecaa68aSToby Isaac   ierr = DMGetWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
48806ecaa68aSToby Isaac 
4881d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
4882d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
4883d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
4884d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
4885d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
4886d3d1a6afSToby Isaac   if (numFields) {
4887d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
4888d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4889d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
48904b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4891d3d1a6afSToby Isaac 
48924b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
48934b2f2278SToby Isaac       if (!bSecDof) {
48944b2f2278SToby Isaac         continue;
48954b2f2278SToby Isaac       }
4896d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4897d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4898d3d1a6afSToby Isaac       }
4899d3d1a6afSToby Isaac       if (bDof) {
4900d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
4901d3d1a6afSToby Isaac 
4902d3d1a6afSToby Isaac         fStart[0] = 0;
4903d3d1a6afSToby Isaac         fEnd[0]   = 0;
4904d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4905d3d1a6afSToby Isaac           PetscInt fDof;
4906d3d1a6afSToby Isaac 
4907d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
4908d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
4909d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
4910d3d1a6afSToby Isaac         }
4911d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
49124acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, indices);CHKERRQ(ierr);
4913d3d1a6afSToby Isaac 
4914d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
4915d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
4916d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4917d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
4918d3d1a6afSToby Isaac 
4919d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
4920d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
4921d3d1a6afSToby Isaac         }
4922d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4923d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4924d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
4925d3d1a6afSToby Isaac 
4926d3d1a6afSToby 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 */
4927d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
4928d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
4929302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
49304acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, newIndices);CHKERRQ(ierr);
4931d3d1a6afSToby Isaac         }
4932d3d1a6afSToby Isaac         newP += bDof;
4933d3d1a6afSToby Isaac 
49346ecaa68aSToby Isaac         if (outValues) {
4935d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
4936d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
4937d3d1a6afSToby 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);
4938d3d1a6afSToby Isaac           }
4939d3d1a6afSToby Isaac         }
49406ecaa68aSToby Isaac       }
4941d3d1a6afSToby Isaac       else {
4942d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
4943d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
4944d3d1a6afSToby Isaac         newP++;
4945d3d1a6afSToby Isaac       }
4946d3d1a6afSToby Isaac     }
4947d3d1a6afSToby Isaac   } else {
4948d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4949d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4950d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
49514b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4952d3d1a6afSToby Isaac 
49534b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
49544b2f2278SToby Isaac       if (!bSecDof) {
49554b2f2278SToby Isaac         continue;
49564b2f2278SToby Isaac       }
4957d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4958d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4959d3d1a6afSToby Isaac       }
4960d3d1a6afSToby Isaac       if (bDof) {
4961d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
4962d3d1a6afSToby Isaac 
4963d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
49644acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, indices);CHKERRQ(ierr);
4965d3d1a6afSToby Isaac 
4966d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
4967d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4968d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
4969d3d1a6afSToby Isaac 
4970d3d1a6afSToby 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 */
4971d3d1a6afSToby Isaac 
4972d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
4973d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
4974302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
49754acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, newIndices);CHKERRQ(ierr);
4976d3d1a6afSToby Isaac         }
4977d3d1a6afSToby Isaac         newP += bDof;
4978d3d1a6afSToby Isaac 
4979d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
49806ecaa68aSToby Isaac         if (outValues) {
4981d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
4982d3d1a6afSToby Isaac         }
49836ecaa68aSToby Isaac       }
4984d3d1a6afSToby Isaac       else {
4985d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
4986d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
4987d3d1a6afSToby Isaac         newP++;
4988d3d1a6afSToby Isaac       }
4989d3d1a6afSToby Isaac     }
4990d3d1a6afSToby Isaac   }
4991d3d1a6afSToby Isaac 
49926ecaa68aSToby Isaac   if (outValues) {
49936ecaa68aSToby Isaac     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
4994d3d1a6afSToby Isaac     ierr = PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));CHKERRQ(ierr);
4995d3d1a6afSToby Isaac     /* multiply constraints on the right */
4996d3d1a6afSToby Isaac     if (numFields) {
4997d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
4998d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
4999d3d1a6afSToby Isaac 
5000d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5001d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
5002d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5003d3d1a6afSToby Isaac           PetscInt c, r, k;
5004d3d1a6afSToby Isaac           PetscInt dof;
5005d3d1a6afSToby Isaac 
5006d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
50074b2f2278SToby Isaac           if (!dof) {
50084b2f2278SToby Isaac             continue;
50094b2f2278SToby Isaac           }
5010d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5011d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
5012d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
5013d3d1a6afSToby Isaac 
5014d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5015d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
5016d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
50174acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
5018d3d1a6afSToby Isaac                 }
5019d3d1a6afSToby Isaac               }
5020d3d1a6afSToby Isaac             }
5021d3d1a6afSToby Isaac           }
5022d3d1a6afSToby Isaac           else {
5023d3d1a6afSToby Isaac             /* copy this column as is */
5024d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5025d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
5026d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5027d3d1a6afSToby Isaac               }
5028d3d1a6afSToby Isaac             }
5029d3d1a6afSToby Isaac           }
5030d3d1a6afSToby Isaac           oldOff += dof;
5031d3d1a6afSToby Isaac         }
5032d3d1a6afSToby Isaac       }
5033d3d1a6afSToby Isaac     }
5034d3d1a6afSToby Isaac     else {
5035d3d1a6afSToby Isaac       PetscInt oldOff = 0;
5036d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5037d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
5038d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
5039d3d1a6afSToby Isaac         PetscInt c, r, k;
5040d3d1a6afSToby Isaac         PetscInt dof;
5041d3d1a6afSToby Isaac 
5042d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
50434b2f2278SToby Isaac         if (!dof) {
50444b2f2278SToby Isaac           continue;
50454b2f2278SToby Isaac         }
5046d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5047d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5048d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
5049d3d1a6afSToby Isaac 
5050d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5051d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
5052d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
5053d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5054d3d1a6afSToby Isaac               }
5055d3d1a6afSToby Isaac             }
5056d3d1a6afSToby Isaac           }
5057d3d1a6afSToby Isaac         }
5058d3d1a6afSToby Isaac         else {
5059d3d1a6afSToby Isaac           /* copy this column as is */
5060d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5061d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
5062d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5063d3d1a6afSToby Isaac             }
5064d3d1a6afSToby Isaac           }
5065d3d1a6afSToby Isaac         }
5066d3d1a6afSToby Isaac         oldOff += dof;
5067d3d1a6afSToby Isaac       }
5068d3d1a6afSToby Isaac     }
5069d3d1a6afSToby Isaac 
50706ecaa68aSToby Isaac     if (multiplyLeft) {
50716ecaa68aSToby Isaac       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
5072d3d1a6afSToby Isaac       ierr = PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));CHKERRQ(ierr);
5073d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
5074d3d1a6afSToby Isaac       if (numFields) {
5075d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5076d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
5077d3d1a6afSToby Isaac 
5078d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
5079d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
5080d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
5081d3d1a6afSToby Isaac             PetscInt c, r, k;
5082d3d1a6afSToby Isaac             PetscInt dof;
5083d3d1a6afSToby Isaac 
5084d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
5085d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5086d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5087d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
5088d3d1a6afSToby Isaac 
5089d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
5090d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5091d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
5092d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5093d3d1a6afSToby Isaac                   }
5094d3d1a6afSToby Isaac                 }
5095d3d1a6afSToby Isaac               }
5096d3d1a6afSToby Isaac             }
5097d3d1a6afSToby Isaac             else {
5098d3d1a6afSToby Isaac               /* copy this row as is */
5099d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
5100d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5101d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5102d3d1a6afSToby Isaac                 }
5103d3d1a6afSToby Isaac               }
5104d3d1a6afSToby Isaac             }
5105d3d1a6afSToby Isaac             oldOff += dof;
5106d3d1a6afSToby Isaac           }
5107d3d1a6afSToby Isaac         }
5108d3d1a6afSToby Isaac       }
5109d3d1a6afSToby Isaac       else {
5110d3d1a6afSToby Isaac         PetscInt oldOff = 0;
5111d3d1a6afSToby Isaac 
5112d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5113d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
5114d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5115d3d1a6afSToby Isaac           PetscInt c, r, k;
5116d3d1a6afSToby Isaac           PetscInt dof;
5117d3d1a6afSToby Isaac 
5118d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
5119d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5120d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5121d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5122d3d1a6afSToby Isaac 
5123d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5124d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5125d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5126d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5127d3d1a6afSToby Isaac                 }
5128d3d1a6afSToby Isaac               }
5129d3d1a6afSToby Isaac             }
5130d3d1a6afSToby Isaac           }
5131d3d1a6afSToby Isaac           else {
5132d3d1a6afSToby Isaac             /* copy this row as is */
51339fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5134d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5135d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5136d3d1a6afSToby Isaac               }
5137d3d1a6afSToby Isaac             }
5138d3d1a6afSToby Isaac           }
5139d3d1a6afSToby Isaac           oldOff += dof;
5140d3d1a6afSToby Isaac         }
5141d3d1a6afSToby Isaac       }
5142d3d1a6afSToby Isaac 
51436ecaa68aSToby Isaac       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
51446ecaa68aSToby Isaac     }
51456ecaa68aSToby Isaac     else {
51466ecaa68aSToby Isaac       newValues = tmpValues;
51476ecaa68aSToby Isaac     }
51486ecaa68aSToby Isaac   }
51496ecaa68aSToby Isaac 
5150d3d1a6afSToby Isaac   /* clean up */
5151d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
5152d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
51536ecaa68aSToby Isaac 
5154d3d1a6afSToby Isaac   if (numFields) {
5155d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
5156d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5157d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
5158d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5159d3d1a6afSToby Isaac     }
5160d3d1a6afSToby Isaac   }
5161d3d1a6afSToby Isaac   else {
5162d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5163d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
5164d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5165d3d1a6afSToby Isaac   }
5166d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5167d3d1a6afSToby Isaac 
5168d3d1a6afSToby Isaac   /* output */
51696ecaa68aSToby Isaac   if (outPoints) {
5170d3d1a6afSToby Isaac     *outPoints = newPoints;
51716ecaa68aSToby Isaac   }
51726ecaa68aSToby Isaac   else {
51736ecaa68aSToby Isaac     ierr = DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
51746ecaa68aSToby Isaac   }
517531620726SToby Isaac   if (outValues) {
5176d3d1a6afSToby Isaac     *outValues = newValues;
51776ecaa68aSToby Isaac   }
51786ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
5179d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
5180d3d1a6afSToby Isaac   }
5181d3d1a6afSToby Isaac   PetscFunctionReturn(0);
5182d3d1a6afSToby Isaac }
5183d3d1a6afSToby Isaac 
5184d3d1a6afSToby Isaac #undef __FUNCT__
51857773e69fSMatthew G. Knepley #define __FUNCT__ "DMPlexGetClosureIndices"
51866ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
51877773e69fSMatthew G. Knepley {
51887773e69fSMatthew G. Knepley   PetscSection    clSection;
51897773e69fSMatthew G. Knepley   IS              clPoints;
51907773e69fSMatthew G. Knepley   const PetscInt *clp;
51914acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
51927773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
51937773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
51947773e69fSMatthew G. Knepley   PetscInt        offsets[32];
51957773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
51967773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
51977773e69fSMatthew G. Knepley 
51987773e69fSMatthew G. Knepley   PetscFunctionBegin;
51997773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52007773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
52017773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
52027773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
52037773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
52047773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
52057773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
52067773e69fSMatthew G. Knepley   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
52077773e69fSMatthew G. Knepley   /* Get points in closure */
5208923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
52097773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
52107773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
52117773e69fSMatthew G. Knepley     PetscInt dof, fdof;
52127773e69fSMatthew G. Knepley 
52137773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
52147773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
52157773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
52167773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
52177773e69fSMatthew G. Knepley     }
52187773e69fSMatthew G. Knepley     Nind += dof;
52197773e69fSMatthew G. Knepley   }
52207773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
52218ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
52224acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
52234acb8e1eSToby Isaac   /* Get dual space symmetries */
52244acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
52254acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52264acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52274acb8e1eSToby Isaac   }
52287773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
52297773e69fSMatthew G. Knepley   {
52304acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
52317773e69fSMatthew G. Knepley     if (numPointsNew) {
52324acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
52334acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52344acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52354acb8e1eSToby Isaac       }
52364acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
52374acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
52384acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
52394acb8e1eSToby Isaac       }
5240923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
52417773e69fSMatthew G. Knepley       numPoints = numPointsNew;
52427773e69fSMatthew G. Knepley       Nind      = NindNew;
52437773e69fSMatthew G. Knepley       points    = pointsNew;
52447773e69fSMatthew G. Knepley     }
52457773e69fSMatthew G. Knepley   }
52467773e69fSMatthew G. Knepley   /* Calculate indices */
52477773e69fSMatthew G. Knepley   ierr = DMGetWorkArray(dm, Nind, PETSC_INT, indices);CHKERRQ(ierr);
52487773e69fSMatthew G. Knepley   if (Nf) {
52496ecaa68aSToby Isaac     if (outOffsets) {
52506ecaa68aSToby Isaac       PetscInt f;
52516ecaa68aSToby Isaac 
525246bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
52536ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
52546ecaa68aSToby Isaac       }
52556ecaa68aSToby Isaac     }
52564acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
52574acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
52584acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, *indices);
52597773e69fSMatthew G. Knepley     }
52607773e69fSMatthew G. Knepley   } else {
52614acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
52624acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
52634acb8e1eSToby Isaac 
52644acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
52654acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, *indices);
52667773e69fSMatthew G. Knepley     }
52677773e69fSMatthew G. Knepley   }
52687773e69fSMatthew G. Knepley   /* Cleanup points */
52694acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
52704acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52714acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52724acb8e1eSToby Isaac   }
52737773e69fSMatthew G. Knepley   if (numPointsNew) {
52747773e69fSMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, PETSC_INT, &pointsNew);CHKERRQ(ierr);
52757773e69fSMatthew G. Knepley   } else {
5276923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
52777773e69fSMatthew G. Knepley   }
52787773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
52797773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
52807773e69fSMatthew G. Knepley }
52817773e69fSMatthew G. Knepley 
52827773e69fSMatthew G. Knepley #undef __FUNCT__
52837773e69fSMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreClosureIndices"
528446bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
52857773e69fSMatthew G. Knepley {
52867773e69fSMatthew G. Knepley   PetscErrorCode ierr;
52877773e69fSMatthew G. Knepley 
52887773e69fSMatthew G. Knepley   PetscFunctionBegin;
52897773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52907773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
52917773e69fSMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, indices);CHKERRQ(ierr);
52927773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
52937773e69fSMatthew G. Knepley }
52947773e69fSMatthew G. Knepley 
52957773e69fSMatthew G. Knepley #undef __FUNCT__
5296552f7358SJed Brown #define __FUNCT__ "DMPlexMatSetClosure"
52977f5d1fdeSMatthew G. Knepley /*@C
52987f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
52997f5d1fdeSMatthew G. Knepley 
53007f5d1fdeSMatthew G. Knepley   Not collective
53017f5d1fdeSMatthew G. Knepley 
53027f5d1fdeSMatthew G. Knepley   Input Parameters:
53037f5d1fdeSMatthew G. Knepley + dm - The DM
5304ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5305ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
53067f5d1fdeSMatthew G. Knepley . A - The matrix
53077f5d1fdeSMatthew G. Knepley . point - The sieve point in the DM
53087f5d1fdeSMatthew G. Knepley . values - The array of values
53097f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
53107f5d1fdeSMatthew G. Knepley 
53117f5d1fdeSMatthew G. Knepley   Fortran Notes:
53127f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
53137f5d1fdeSMatthew G. Knepley 
53147f5d1fdeSMatthew G. Knepley   Level: intermediate
53157f5d1fdeSMatthew G. Knepley 
53167f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
53177f5d1fdeSMatthew G. Knepley @*/
53187c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5319552f7358SJed Brown {
5320552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
53211b406b76SMatthew G. Knepley   PetscSection        clSection;
53221b406b76SMatthew G. Knepley   IS                  clPoints;
5323d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
53241b406b76SMatthew G. Knepley   const PetscInt     *clp;
5325552f7358SJed Brown   PetscInt           *indices;
5326552f7358SJed Brown   PetscInt            offsets[32];
53274acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
53284acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
5329923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
53304acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
5331d3d1a6afSToby Isaac   PetscScalar        *newValues;
5332552f7358SJed Brown   PetscErrorCode      ierr;
5333552f7358SJed Brown 
5334552f7358SJed Brown   PetscFunctionBegin;
5335552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5336ebd6d717SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
53373dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5338ebd6d717SJed Brown   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
53393dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
53403dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5341552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
534282f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5343552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5344923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5345552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5346552f7358SJed Brown     PetscInt fdof;
5347552f7358SJed Brown 
5348552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5349552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5350552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5351552f7358SJed Brown       offsets[f+1] += fdof;
5352552f7358SJed Brown     }
5353552f7358SJed Brown     numIndices += dof;
5354552f7358SJed Brown   }
53550d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
53560d644c17SKarl Rupp 
53578ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
53584acb8e1eSToby Isaac   /* Get symmetries */
53594acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
53604acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
53614acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
53624acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
53634acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
53644acb8e1eSToby Isaac 
53654acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
53664acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
53674acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
53684acb8e1eSToby Isaac 
53694acb8e1eSToby Isaac         if (!numFields) {
53704acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
53714acb8e1eSToby Isaac         } else {
53724acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
53734acb8e1eSToby Isaac         }
53744acb8e1eSToby Isaac         if (flip) {
53754acb8e1eSToby Isaac           PetscInt i, j, k;
53764acb8e1eSToby Isaac 
53774acb8e1eSToby Isaac           if (!valCopy) {
53784acb8e1eSToby Isaac             ierr = DMGetWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
53794acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
53804acb8e1eSToby Isaac             values = valCopy;
53814acb8e1eSToby Isaac           }
53824acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
5383775f7be2SToby Isaac             PetscScalar fval = flip[i];
53844acb8e1eSToby Isaac 
53854acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
53864acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
53874acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
53884acb8e1eSToby Isaac             }
53894acb8e1eSToby Isaac           }
53904acb8e1eSToby Isaac         }
53914acb8e1eSToby Isaac         foffset += fdof;
53924acb8e1eSToby Isaac       }
53934acb8e1eSToby Isaac     }
53944acb8e1eSToby Isaac   }
53954acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
5396d3d1a6afSToby Isaac   if (newNumPoints) {
53974acb8e1eSToby Isaac     if (valCopy) {
53984acb8e1eSToby Isaac       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
53994acb8e1eSToby Isaac     }
54004acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
54014acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54024acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54034acb8e1eSToby Isaac     }
54044acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
54054acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
54064acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
54074acb8e1eSToby Isaac     }
5408923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5409d3d1a6afSToby Isaac     numPoints  = newNumPoints;
5410d3d1a6afSToby Isaac     numIndices = newNumIndices;
5411d3d1a6afSToby Isaac     points     = newPoints;
5412d3d1a6afSToby Isaac     values     = newValues;
5413d3d1a6afSToby Isaac   }
5414552f7358SJed Brown   ierr = DMGetWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5415552f7358SJed Brown   if (numFields) {
54164acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
54174acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
54184acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, indices);
5419552f7358SJed Brown     }
5420552f7358SJed Brown   } else {
54214acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
54224acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
54234acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
54244acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, indices);
5425552f7358SJed Brown     }
5426552f7358SJed Brown   }
5427b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
5428552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
5429ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
5430ab1d0545SMatthew G. Knepley     PetscInt i;
5431ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
54328ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
5433ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5434ab1d0545SMatthew G. Knepley   }
5435552f7358SJed Brown   if (ierr) {
5436552f7358SJed Brown     PetscMPIInt    rank;
5437552f7358SJed Brown     PetscErrorCode ierr2;
5438552f7358SJed Brown 
543982f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5440e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5441b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
54426a415b8fSMatthew G Knepley     ierr2 = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr2);
5443552f7358SJed Brown     CHKERRQ(ierr);
5444552f7358SJed Brown   }
54454acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
54464acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54474acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54484acb8e1eSToby Isaac   }
5449d3d1a6afSToby Isaac   if (newNumPoints) {
5450d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
5451d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
5452d3d1a6afSToby Isaac   }
5453d3d1a6afSToby Isaac   else {
54544acb8e1eSToby Isaac     if (valCopy) {
54554acb8e1eSToby Isaac       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
54564acb8e1eSToby Isaac     }
5457923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5458d3d1a6afSToby Isaac   }
5459552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5460552f7358SJed Brown   PetscFunctionReturn(0);
5461552f7358SJed Brown }
5462552f7358SJed Brown 
5463552f7358SJed Brown #undef __FUNCT__
5464de41b84cSMatthew G. Knepley #define __FUNCT__ "DMPlexMatSetClosureRefined"
5465de41b84cSMatthew 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)
5466de41b84cSMatthew G. Knepley {
5467de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
5468de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
5469de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
5470de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
5471de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
5472de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
54734ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
5474de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
5475de41b84cSMatthew G. Knepley 
5476de41b84cSMatthew G. Knepley   PetscFunctionBegin;
5477de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
5478de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
5479de41b84cSMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
5480de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
5481de41b84cSMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
5482de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
5483de41b84cSMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
5484de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
5485de41b84cSMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
5486de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
5487de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
5488de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
5489de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5490de41b84cSMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5491de41b84cSMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5492de41b84cSMatthew G. Knepley   /* Column indices */
5493de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
54944ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
5495de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
5496de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
5497de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
5498de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
5499de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
5500de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
5501de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
5502de41b84cSMatthew G. Knepley       ++q;
5503de41b84cSMatthew G. Knepley     }
5504de41b84cSMatthew G. Knepley   }
5505de41b84cSMatthew G. Knepley   numCPoints = q;
5506de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
5507de41b84cSMatthew G. Knepley     PetscInt fdof;
5508de41b84cSMatthew G. Knepley 
5509de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
55104ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5511de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5512de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
5513de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
5514de41b84cSMatthew G. Knepley     }
5515de41b84cSMatthew G. Knepley     numCIndices += dof;
5516de41b84cSMatthew G. Knepley   }
5517de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
5518de41b84cSMatthew G. Knepley   /* Row indices */
5519de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
5520de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
55211ba5f902SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
5522de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
5523de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
5524de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5525de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
5526de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
5527de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
5528de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
55294ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
55304ca5e9f5SMatthew G. Knepley         if (!dof) continue;
55314ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
55324ca5e9f5SMatthew G. Knepley         if (s < q) continue;
5533de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
5534de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
5535de41b84cSMatthew G. Knepley         ++q;
5536de41b84cSMatthew G. Knepley       }
5537de41b84cSMatthew G. Knepley     }
5538de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5539de41b84cSMatthew G. Knepley   }
5540de41b84cSMatthew G. Knepley   numFPoints = q;
5541de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
5542de41b84cSMatthew G. Knepley     PetscInt fdof;
5543de41b84cSMatthew G. Knepley 
5544de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
55454ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5546de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5547de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
5548de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
5549de41b84cSMatthew G. Knepley     }
5550de41b84cSMatthew G. Knepley     numFIndices += dof;
5551de41b84cSMatthew G. Knepley   }
5552de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
5553de41b84cSMatthew G. Knepley 
55548ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
55558ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
5556de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
5557de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
5558de41b84cSMatthew G. Knepley   if (numFields) {
55594acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
55604acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
55614acb8e1eSToby Isaac 
55624acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
55634acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
55644acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5565de41b84cSMatthew G. Knepley     }
55664acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
55674acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
55684acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
55694acb8e1eSToby Isaac     }
55704acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
55714acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
55724acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
55734acb8e1eSToby Isaac     }
55744acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
55754acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
55764acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5577de41b84cSMatthew G. Knepley     }
5578de41b84cSMatthew G. Knepley   } else {
55794acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
55804acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
55814acb8e1eSToby Isaac 
55824acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
55834acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
55844acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
55854acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
55864acb8e1eSToby Isaac 
55874acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
55884acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);CHKERRQ(ierr);
5589de41b84cSMatthew G. Knepley     }
55904acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
55914acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
55924acb8e1eSToby Isaac 
55934acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
55944acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);CHKERRQ(ierr);
5595de41b84cSMatthew G. Knepley     }
55964acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
55974acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
5598de41b84cSMatthew G. Knepley   }
5599de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
56004acb8e1eSToby Isaac   /* TODO: flips */
5601de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
5602de41b84cSMatthew G. Knepley   if (ierr) {
5603de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
5604de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
5605de41b84cSMatthew G. Knepley 
5606de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5607e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5608de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
5609de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr2);
5610de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr2);
5611de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
5612de41b84cSMatthew G. Knepley   }
5613549a8adaSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
5614de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
5615de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
5616de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
5617de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
5618de41b84cSMatthew G. Knepley }
5619de41b84cSMatthew G. Knepley 
5620de41b84cSMatthew G. Knepley #undef __FUNCT__
56217c927364SMatthew G. Knepley #define __FUNCT__ "DMPlexMatGetClosureIndicesRefined"
56227c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
56237c927364SMatthew G. Knepley {
56247c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
56257c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
56267c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
56277c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
56287c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
56297c927364SMatthew G. Knepley   PetscErrorCode ierr;
56307c927364SMatthew G. Knepley 
56317c927364SMatthew G. Knepley   PetscFunctionBegin;
56327c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
56337c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
56347c927364SMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
56357c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
56367c927364SMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
56377c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
56387c927364SMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
56397c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
56407c927364SMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
56417c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
56427c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
56437c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
56447c927364SMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
56457c927364SMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
56467c927364SMatthew G. Knepley   /* Column indices */
56477c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
56487c927364SMatthew G. Knepley   maxFPoints = numCPoints;
56497c927364SMatthew G. Knepley   /* Compress out points not in the section */
56507c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
56517c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
56527c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
56537c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
56547c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
56557c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
56567c927364SMatthew G. Knepley       ++q;
56577c927364SMatthew G. Knepley     }
56587c927364SMatthew G. Knepley   }
56597c927364SMatthew G. Knepley   numCPoints = q;
56607c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
56617c927364SMatthew G. Knepley     PetscInt fdof;
56627c927364SMatthew G. Knepley 
56637c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
56647c927364SMatthew G. Knepley     if (!dof) continue;
56657c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
56667c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
56677c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
56687c927364SMatthew G. Knepley     }
56697c927364SMatthew G. Knepley     numCIndices += dof;
56707c927364SMatthew G. Knepley   }
56717c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
56727c927364SMatthew G. Knepley   /* Row indices */
56737c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
56747c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
56757c927364SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
56767c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
56777c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
56787c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
56797c927364SMatthew G. Knepley     /* Compress out points not in the section */
56807c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
56817c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
56827c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
56837c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
56847c927364SMatthew G. Knepley         if (!dof) continue;
56857c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
56867c927364SMatthew G. Knepley         if (s < q) continue;
56877c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
56887c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
56897c927364SMatthew G. Knepley         ++q;
56907c927364SMatthew G. Knepley       }
56917c927364SMatthew G. Knepley     }
56927c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
56937c927364SMatthew G. Knepley   }
56947c927364SMatthew G. Knepley   numFPoints = q;
56957c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
56967c927364SMatthew G. Knepley     PetscInt fdof;
56977c927364SMatthew G. Knepley 
56987c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
56997c927364SMatthew G. Knepley     if (!dof) continue;
57007c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
57017c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
57027c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
57037c927364SMatthew G. Knepley     }
57047c927364SMatthew G. Knepley     numFIndices += dof;
57057c927364SMatthew G. Knepley   }
57067c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
57077c927364SMatthew G. Knepley 
57088ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
57098ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
57107c927364SMatthew G. Knepley   if (numFields) {
57114acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
57124acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
57134acb8e1eSToby Isaac 
57144acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
57154acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
57164acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
57177c927364SMatthew G. Knepley     }
57184acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
57194acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
57204acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
57214acb8e1eSToby Isaac     }
57224acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
57234acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
57244acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
57254acb8e1eSToby Isaac     }
57264acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
57274acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
57284acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
57297c927364SMatthew G. Knepley     }
57307c927364SMatthew G. Knepley   } else {
57314acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
57324acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
57334acb8e1eSToby Isaac 
57344acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
57354acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
57364acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
57374acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
57384acb8e1eSToby Isaac 
57394acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
57404acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);
57417c927364SMatthew G. Knepley     }
57424acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
57434acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
57444acb8e1eSToby Isaac 
57454acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
57464acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);
57477c927364SMatthew G. Knepley     }
57484acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
57494acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
57507c927364SMatthew G. Knepley   }
57517c927364SMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
57527c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
57537c927364SMatthew G. Knepley   PetscFunctionReturn(0);
57547c927364SMatthew G. Knepley }
57557c927364SMatthew G. Knepley 
57567c927364SMatthew G. Knepley #undef __FUNCT__
5757770b213bSMatthew G Knepley #define __FUNCT__ "DMPlexGetHybridBounds"
5758f96281f8SMatthew G. Knepley /*@
5759f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
5760f96281f8SMatthew G. Knepley 
5761f96281f8SMatthew G. Knepley   Input Parameter:
5762f96281f8SMatthew G. Knepley . dm - The DMPlex object
5763f96281f8SMatthew G. Knepley 
5764f96281f8SMatthew G. Knepley   Output Parameters:
5765f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
5766dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5767dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5768dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5769f96281f8SMatthew G. Knepley 
5770f96281f8SMatthew G. Knepley   Level: developer
5771f96281f8SMatthew G. Knepley 
5772dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
5773f96281f8SMatthew G. Knepley @*/
5774770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
5775552f7358SJed Brown {
5776552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5777770b213bSMatthew G Knepley   PetscInt       dim;
5778770b213bSMatthew G Knepley   PetscErrorCode ierr;
5779552f7358SJed Brown 
5780552f7358SJed Brown   PetscFunctionBegin;
5781552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5782c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5783770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
5784770b213bSMatthew G Knepley   if (fMax) *fMax = mesh->hybridPointMax[dim-1];
5785770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
5786770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
5787552f7358SJed Brown   PetscFunctionReturn(0);
5788552f7358SJed Brown }
5789552f7358SJed Brown 
5790552f7358SJed Brown #undef __FUNCT__
5791770b213bSMatthew G Knepley #define __FUNCT__ "DMPlexSetHybridBounds"
5792dc5a3409SMatthew G. Knepley /*@
5793dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
5794dc5a3409SMatthew G. Knepley 
5795dc5a3409SMatthew G. Knepley   Input Parameters:
5796dc5a3409SMatthew G. Knepley . dm   - The DMPlex object
5797dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
5798dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5799dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5800dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5801dc5a3409SMatthew G. Knepley 
5802dc5a3409SMatthew G. Knepley   Level: developer
5803dc5a3409SMatthew G. Knepley 
5804dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
5805dc5a3409SMatthew G. Knepley @*/
5806770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
5807552f7358SJed Brown {
5808552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5809770b213bSMatthew G Knepley   PetscInt       dim;
5810770b213bSMatthew G Knepley   PetscErrorCode ierr;
5811552f7358SJed Brown 
5812552f7358SJed Brown   PetscFunctionBegin;
5813552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5814c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5815770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]   = cMax;
5816770b213bSMatthew G Knepley   if (fMax >= 0) mesh->hybridPointMax[dim-1] = fMax;
5817770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]     = eMax;
5818770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]     = vMax;
5819552f7358SJed Brown   PetscFunctionReturn(0);
5820552f7358SJed Brown }
5821552f7358SJed Brown 
5822552f7358SJed Brown #undef __FUNCT__
5823552f7358SJed Brown #define __FUNCT__ "DMPlexGetVTKCellHeight"
5824552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
5825552f7358SJed Brown {
5826552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5827552f7358SJed Brown 
5828552f7358SJed Brown   PetscFunctionBegin;
5829552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5830552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
5831552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
5832552f7358SJed Brown   PetscFunctionReturn(0);
5833552f7358SJed Brown }
5834552f7358SJed Brown 
5835552f7358SJed Brown #undef __FUNCT__
5836552f7358SJed Brown #define __FUNCT__ "DMPlexSetVTKCellHeight"
5837552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
5838552f7358SJed Brown {
5839552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5840552f7358SJed Brown 
5841552f7358SJed Brown   PetscFunctionBegin;
5842552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5843552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
5844552f7358SJed Brown   PetscFunctionReturn(0);
5845552f7358SJed Brown }
5846552f7358SJed Brown 
5847552f7358SJed Brown #undef __FUNCT__
5848552f7358SJed Brown #define __FUNCT__ "DMPlexCreateNumbering_Private"
5849552f7358SJed Brown /* We can easily have a form that takes an IS instead */
5850ef48cebcSMatthew G. Knepley static PetscErrorCode DMPlexCreateNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
5851552f7358SJed Brown {
5852552f7358SJed Brown   PetscSection   section, globalSection;
5853552f7358SJed Brown   PetscInt      *numbers, p;
5854552f7358SJed Brown   PetscErrorCode ierr;
5855552f7358SJed Brown 
5856552f7358SJed Brown   PetscFunctionBegin;
585782f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
5858552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
5859552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5860552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
5861552f7358SJed Brown   }
5862552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
586315b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
5864854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
5865552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5866552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
5867ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
5868ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
5869552f7358SJed Brown   }
587082f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
5871ef48cebcSMatthew G. Knepley   if (globalSize) {
5872ef48cebcSMatthew G. Knepley     PetscLayout layout;
5873ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
5874ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
5875ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
5876ef48cebcSMatthew G. Knepley   }
5877552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
5878552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
5879552f7358SJed Brown   PetscFunctionReturn(0);
5880552f7358SJed Brown }
5881552f7358SJed Brown 
5882552f7358SJed Brown #undef __FUNCT__
588381ed3555SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateCellNumbering_Internal"
588481ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
5885552f7358SJed Brown {
5886552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
5887552f7358SJed Brown   PetscErrorCode ierr;
5888552f7358SJed Brown 
5889552f7358SJed Brown   PetscFunctionBegin;
5890552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
5891552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
58920298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
589381ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
589481ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
589581ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
5896552f7358SJed Brown }
589781ed3555SMatthew G. Knepley 
589881ed3555SMatthew G. Knepley #undef __FUNCT__
589981ed3555SMatthew G. Knepley #define __FUNCT__ "DMPlexGetCellNumbering"
590081ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
590181ed3555SMatthew G. Knepley {
590281ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
590381ed3555SMatthew G. Knepley   PetscErrorCode ierr;
590481ed3555SMatthew G. Knepley 
590581ed3555SMatthew G. Knepley   PetscFunctionBegin;
590681ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
590781ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
5908552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
5909552f7358SJed Brown   PetscFunctionReturn(0);
5910552f7358SJed Brown }
5911552f7358SJed Brown 
5912552f7358SJed Brown #undef __FUNCT__
591381ed3555SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateVertexNumbering_Internal"
591481ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
591581ed3555SMatthew G. Knepley {
591681ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
591781ed3555SMatthew G. Knepley   PetscErrorCode ierr;
591881ed3555SMatthew G. Knepley 
591981ed3555SMatthew G. Knepley   PetscFunctionBegin;
592081ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
592181ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
592281ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
592381ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
592481ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
592581ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
592681ed3555SMatthew G. Knepley }
592781ed3555SMatthew G. Knepley 
592881ed3555SMatthew G. Knepley #undef __FUNCT__
5929552f7358SJed Brown #define __FUNCT__ "DMPlexGetVertexNumbering"
5930552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
5931552f7358SJed Brown {
5932552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5933552f7358SJed Brown   PetscErrorCode ierr;
5934552f7358SJed Brown 
5935552f7358SJed Brown   PetscFunctionBegin;
5936552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
593781ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
5938552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
5939552f7358SJed Brown   PetscFunctionReturn(0);
5940552f7358SJed Brown }
5941552f7358SJed Brown 
5942ef48cebcSMatthew G. Knepley #undef __FUNCT__
5943ef48cebcSMatthew G. Knepley #define __FUNCT__ "DMPlexCreatePointNumbering"
5944ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
5945ef48cebcSMatthew G. Knepley {
5946ef48cebcSMatthew G. Knepley   IS             nums[4];
5947ef48cebcSMatthew G. Knepley   PetscInt       depths[4];
5948ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
5949ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
5950ef48cebcSMatthew G. Knepley 
5951ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
5952ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5953ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
59548abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
59558abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
5956ef48cebcSMatthew G. Knepley   depths[0] = depth; depths[1] = 0;
5957ef48cebcSMatthew G. Knepley   for (d = 2; d <= depth; ++d) depths[d] = depth-d+1;
5958ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
5959ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
5960ef48cebcSMatthew G. Knepley 
5961ef48cebcSMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, depths[d], &pStart, &pEnd);CHKERRQ(ierr);
5962ef48cebcSMatthew G. Knepley     ierr = DMPlexCreateNumbering_Private(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
5963ef48cebcSMatthew G. Knepley     shift += gsize;
5964ef48cebcSMatthew G. Knepley   }
5965302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
5966ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
5967ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
5968ef48cebcSMatthew G. Knepley }
5969ef48cebcSMatthew G. Knepley 
5970ca8062c8SMatthew G. Knepley #undef __FUNCT__
5971ca8062c8SMatthew G. Knepley #define __FUNCT__ "DMPlexCheckSymmetry"
5972ca8062c8SMatthew G. Knepley /*@
5973ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
5974ca8062c8SMatthew G. Knepley 
5975ca8062c8SMatthew G. Knepley   Input Parameters:
5976ca8062c8SMatthew G. Knepley   + dm - The DMPlex object
5977ca8062c8SMatthew G. Knepley 
5978ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
5979ca8062c8SMatthew G. Knepley 
5980ca8062c8SMatthew G. Knepley   Level: developer
5981ca8062c8SMatthew G. Knepley 
59829bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSkeleton(), DMCheckFaces()
5983ca8062c8SMatthew G. Knepley @*/
5984ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
5985ca8062c8SMatthew G. Knepley {
5986ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
5987ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
5988ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
5989ca8062c8SMatthew G. Knepley   PetscInt        pStart, pEnd, p, csize, ssize;
5990ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
5991ca8062c8SMatthew G. Knepley 
5992ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
5993ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5994ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
5995ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
5996ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
5997ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
5998ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
5999ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
6000ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
6001ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
600242e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
600342e66dfaSMatthew G. Knepley       PetscInt  d;
600442e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
600542e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
600642e66dfaSMatthew G. Knepley       }
6007ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
6008ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
6009ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
6010ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
6011ca8062c8SMatthew G. Knepley       }
601242e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
60138ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
6014ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
60158ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
6016ca8062c8SMatthew G. Knepley         }
6017302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
60188ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
6019ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
60208ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
6021ca8062c8SMatthew G. Knepley         }
6022302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
60238ccfff9cSToby 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]);
60248ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
6025ca8062c8SMatthew G. Knepley       }
602642e66dfaSMatthew G. Knepley     }
6027ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
6028ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
6029ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
6030ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
6031ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
6032ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
6033ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
6034ca8062c8SMatthew G. Knepley       }
6035ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
60368ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
6037ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
60388ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
6039ca8062c8SMatthew G. Knepley         }
6040302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
60418ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
6042ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
60438ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
6044ca8062c8SMatthew G. Knepley         }
6045302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
60468ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
6047ca8062c8SMatthew G. Knepley       }
6048ca8062c8SMatthew G. Knepley     }
6049ca8062c8SMatthew G. Knepley   }
6050ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
6051ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
60528ccfff9cSToby Isaac   if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
6053ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6054ca8062c8SMatthew G. Knepley }
6055ca8062c8SMatthew G. Knepley 
6056ca8062c8SMatthew G. Knepley #undef __FUNCT__
6057ca8062c8SMatthew G. Knepley #define __FUNCT__ "DMPlexCheckSkeleton"
6058ca8062c8SMatthew G. Knepley /*@
6059ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
6060ca8062c8SMatthew G. Knepley 
6061ca8062c8SMatthew G. Knepley   Input Parameters:
6062ca8062c8SMatthew G. Knepley + dm - The DMPlex object
606358723a97SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
606458723a97SMatthew G. Knepley - cellHeight - Normally 0
6065ca8062c8SMatthew G. Knepley 
6066ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6067ca8062c8SMatthew G. Knepley 
6068ca8062c8SMatthew G. Knepley   Level: developer
6069ca8062c8SMatthew G. Knepley 
60709bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckFaces()
6071ca8062c8SMatthew G. Knepley @*/
607258723a97SMatthew G. Knepley PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscBool isSimplex, PetscInt cellHeight)
6073ca8062c8SMatthew G. Knepley {
607442363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
6075ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
6076ca8062c8SMatthew G. Knepley 
6077ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6078ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6079c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6080ca8062c8SMatthew G. Knepley   switch (dim) {
608142363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
608242363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
608342363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
6084ca8062c8SMatthew G. Knepley   default:
60858ccfff9cSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
6086ca8062c8SMatthew G. Knepley   }
608758723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
608858723a97SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
6089ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
6090ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
6091ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
609258723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
609358723a97SMatthew G. Knepley 
609458723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
609558723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
609658723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
609758723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
609858723a97SMatthew G. Knepley     }
609958723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
61008ccfff9cSToby Isaac     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
6101ca8062c8SMatthew G. Knepley   }
610242363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
610342363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
610442363296SMatthew G. Knepley 
610542363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
610642363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
610742363296SMatthew G. Knepley       const PetscInt p = closure[cl];
610842363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
610942363296SMatthew G. Knepley     }
611042363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
61118ccfff9cSToby Isaac     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
611242363296SMatthew G. Knepley   }
6113ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6114ca8062c8SMatthew G. Knepley }
61159bf0dad6SMatthew G. Knepley 
61169bf0dad6SMatthew G. Knepley #undef __FUNCT__
61179bf0dad6SMatthew G. Knepley #define __FUNCT__ "DMPlexCheckFaces"
61189bf0dad6SMatthew G. Knepley /*@
61199bf0dad6SMatthew 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
61209bf0dad6SMatthew G. Knepley 
61219bf0dad6SMatthew G. Knepley   Input Parameters:
61229bf0dad6SMatthew G. Knepley + dm - The DMPlex object
61239bf0dad6SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
61249bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
61259bf0dad6SMatthew G. Knepley 
61269bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
61279bf0dad6SMatthew G. Knepley 
61289bf0dad6SMatthew G. Knepley   Level: developer
61299bf0dad6SMatthew G. Knepley 
61309bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckSkeleton()
61319bf0dad6SMatthew G. Knepley @*/
61329bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexCheckFaces(DM dm, PetscBool isSimplex, PetscInt cellHeight)
61339bf0dad6SMatthew G. Knepley {
61343554e41dSMatthew G. Knepley   PetscInt       pMax[4];
61353554e41dSMatthew G. Knepley   PetscInt       dim, vStart, vEnd, cStart, cEnd, c, h;
61369bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
61379bf0dad6SMatthew G. Knepley 
61389bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
61399bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6140c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
61419bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
614225abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
61433554e41dSMatthew G. Knepley   for (h = cellHeight; h < dim; ++h) {
61443554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
61453554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
61469bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
61479bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
61489bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
61499bf0dad6SMatthew G. Knepley 
615025abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
61519bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
61529bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
61539bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
61549bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
61559bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
61569bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
61579bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
61589bf0dad6SMatthew G. Knepley       }
61593554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
61608ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
61619bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
61629bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
61639bf0dad6SMatthew G. Knepley 
61649bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
61659bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
61669bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
61679bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
61689bf0dad6SMatthew G. Knepley         }
61698ccfff9cSToby 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);
61709bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
61718ccfff9cSToby 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]);
61729bf0dad6SMatthew G. Knepley         }
61739bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
61749bf0dad6SMatthew G. Knepley       }
61759bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
61769bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
61779bf0dad6SMatthew G. Knepley     }
61783554e41dSMatthew G. Knepley   }
6179552f7358SJed Brown   PetscFunctionReturn(0);
6180552f7358SJed Brown }
61813913d7c8SMatthew G. Knepley 
61823913d7c8SMatthew G. Knepley #undef __FUNCT__
6183bceba477SMatthew G. Knepley #define __FUNCT__ "DMCreateInterpolation_Plex"
6184bceba477SMatthew G. Knepley /* Pointwise interpolation
6185bceba477SMatthew G. Knepley      Just code FEM for now
6186bceba477SMatthew G. Knepley      u^f = I u^c
61874ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
61884ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
61894ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
6190bceba477SMatthew G. Knepley */
6191bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
6192bceba477SMatthew G. Knepley {
6193bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
6194bceba477SMatthew G. Knepley   PetscInt       m, n;
6195a063dac3SMatthew G. Knepley   void          *ctx;
619668132eb9SMatthew G. Knepley   DM             cdm;
619768132eb9SMatthew G. Knepley   PetscBool      regular;
6198bceba477SMatthew G. Knepley   PetscErrorCode ierr;
6199bceba477SMatthew G. Knepley 
6200bceba477SMatthew G. Knepley   PetscFunctionBegin;
6201bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6202bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6203bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6204bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
620568132eb9SMatthew G. Knepley 
6206bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
6207bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6208bceba477SMatthew G. Knepley   ierr = MatSetType(*interpolation, dmCoarse->mattype);CHKERRQ(ierr);
6209a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
621068132eb9SMatthew G. Knepley 
6211a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
621268132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
621368132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
621468132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
621568132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
62165d1c2e58SMatthew G. Knepley   /* Use naive scaling */
62175d1c2e58SMatthew G. Knepley   ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
6218a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
6219a063dac3SMatthew G. Knepley }
6220bceba477SMatthew G. Knepley 
6221a063dac3SMatthew G. Knepley #undef __FUNCT__
6222a063dac3SMatthew G. Knepley #define __FUNCT__ "DMCreateInjection_Plex"
62236dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
6224a063dac3SMatthew G. Knepley {
622590748bafSMatthew G. Knepley   PetscErrorCode ierr;
62266dbf9973SLawrence Mitchell   VecScatter     ctx;
622790748bafSMatthew G. Knepley 
6228a063dac3SMatthew G. Knepley   PetscFunctionBegin;
62296dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
62306dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
62316dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
6232bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6233bceba477SMatthew G. Knepley }
6234bceba477SMatthew G. Knepley 
6235bceba477SMatthew G. Knepley #undef __FUNCT__
6236fd59a867SMatthew G. Knepley #define __FUNCT__ "DMCreateDefaultSection_Plex"
6237fd59a867SMatthew G. Knepley PetscErrorCode DMCreateDefaultSection_Plex(DM dm)
6238fd59a867SMatthew G. Knepley {
6239fd59a867SMatthew G. Knepley   PetscSection   section;
6240ab1d0545SMatthew G. Knepley   IS            *bcPoints, *bcComps;
6241ebb3236fSMatthew G. Knepley   PetscBool     *isFE;
6242286d8613SMatthew G. Knepley   PetscInt      *bcFields, *numComp, *numDof;
6243ebb3236fSMatthew G. Knepley   PetscInt       depth, dim, numBd, numBC = 0, numFields, bd, bc = 0, f;
6244ebb3236fSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
6245fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
6246fd59a867SMatthew G. Knepley 
6247fd59a867SMatthew G. Knepley   PetscFunctionBegin;
6248ebb3236fSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
6249ae71db08SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
6250ebb3236fSMatthew G. Knepley   /* FE and FV boundary conditions are handled slightly differently */
6251ebb3236fSMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
6252ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6253ebb3236fSMatthew G. Knepley     PetscObject  obj;
6254ebb3236fSMatthew G. Knepley     PetscClassId id;
6255ebb3236fSMatthew G. Knepley 
6256ebb3236fSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6257ebb3236fSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
6258ebb3236fSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
6259ebb3236fSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
62608ccfff9cSToby Isaac     else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
6261ebb3236fSMatthew G. Knepley   }
62622f900235SMatthew G. Knepley   /* Allocate boundary point storage for FEM boundaries */
6263286d8613SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
6264c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6265ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
6266ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
6267dff059c6SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob, &numBd);CHKERRQ(ierr);
6268fd59a867SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
62692f900235SMatthew G. Knepley     PetscInt                field;
6270f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6271385532a5SToby Isaac     const char             *labelName;
6272385532a5SToby Isaac     DMLabel                 label;
62732f900235SMatthew G. Knepley 
6274f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &labelName, &field, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
6275385532a5SToby Isaac     ierr = DMGetLabel(dm,labelName,&label);CHKERRQ(ierr);
6276f971fd6bSMatthew G. Knepley     if (label && isFE[field] && (type & DM_BC_ESSENTIAL)) ++numBC;
6277fd59a867SMatthew G. Knepley   }
62782f900235SMatthew G. Knepley   /* Add ghost cell boundaries for FVM */
6279ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) if (!isFE[f] && cEndInterior >= 0) ++numBC;
62806c8d74c4SMatthew G. Knepley   ierr = PetscCalloc3(numBC,&bcFields,numBC,&bcPoints,numBC,&bcComps);CHKERRQ(ierr);
6281ebb3236fSMatthew G. Knepley   /* Constrain ghost cells for FV */
6282ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6283ebb3236fSMatthew G. Knepley     PetscInt *newidx, c;
6284ebb3236fSMatthew G. Knepley 
6285ebb3236fSMatthew G. Knepley     if (isFE[f] || cEndInterior < 0) continue;
6286ebb3236fSMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cEndInterior,&newidx);CHKERRQ(ierr);
6287ebb3236fSMatthew G. Knepley     for (c = cEndInterior; c < cEnd; ++c) newidx[c-cEndInterior] = c;
6288ebb3236fSMatthew G. Knepley     bcFields[bc] = f;
6289ebb3236fSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), cEnd-cEndInterior, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6290ebb3236fSMatthew G. Knepley   }
6291ebb3236fSMatthew G. Knepley   /* Handle FEM Dirichlet boundaries */
6292ebb3236fSMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
6293fd59a867SMatthew G. Knepley     const char             *bdLabel;
6294fd59a867SMatthew G. Knepley     DMLabel                 label;
62950e0f25f2SMatthew G. Knepley     const PetscInt         *comps;
6296fd59a867SMatthew G. Knepley     const PetscInt         *values;
62970e0f25f2SMatthew G. Knepley     PetscInt                bd2, field, numComps, numValues;
6298f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6299f971fd6bSMatthew G. Knepley     PetscBool               duplicate = PETSC_FALSE;
6300fd59a867SMatthew G. Knepley 
6301f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &bdLabel, &field, &numComps, &comps, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
6302c58f1c22SToby Isaac     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
6303385532a5SToby Isaac     if (!isFE[field] || !label) continue;
6304ebb3236fSMatthew G. Knepley     /* Only want to modify label once */
63057391c1cbSMatthew G. Knepley     for (bd2 = 0; bd2 < bd; ++bd2) {
63067391c1cbSMatthew G. Knepley       const char *bdname;
6307dff059c6SToby Isaac       ierr = PetscDSGetBoundary(dm->prob, bd2, NULL, NULL, &bdname, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
63087391c1cbSMatthew G. Knepley       ierr = PetscStrcmp(bdname, bdLabel, &duplicate);CHKERRQ(ierr);
63097391c1cbSMatthew G. Knepley       if (duplicate) break;
63107391c1cbSMatthew G. Knepley     }
6311ebb3236fSMatthew G. Knepley     if (!duplicate && (isFE[field])) {
6312b0bf5782SToby Isaac       /* don't complete cells, which are just present to give orientation to the boundary */
6313092e5057SToby Isaac       ierr = DMPlexLabelComplete(dm, label);CHKERRQ(ierr);
63147391c1cbSMatthew G. Knepley     }
6315ebb3236fSMatthew G. Knepley     /* Filter out cells, if you actually want to constrain cells you need to do things by hand right now */
6316f971fd6bSMatthew G. Knepley     if (type & DM_BC_ESSENTIAL) {
631793a5981aSMatthew G. Knepley       PetscInt       *newidx;
6318ebb3236fSMatthew G. Knepley       PetscInt        n, newn = 0, p, v;
631993a5981aSMatthew G. Knepley 
6320fd59a867SMatthew G. Knepley       bcFields[bc] = field;
63210e0f25f2SMatthew G. Knepley       if (numComps) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), numComps, comps, PETSC_COPY_VALUES, &bcComps[bc]);CHKERRQ(ierr);}
6322ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6323ebb3236fSMatthew G. Knepley         IS              tmp;
6324ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6325ebb3236fSMatthew G. Knepley 
6326c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6327ebb3236fSMatthew G. Knepley         if (!tmp) continue;
632893a5981aSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
632993a5981aSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6330ebb3236fSMatthew G. Knepley         if (isFE[field]) {
633193a5981aSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) ++newn;
6332ebb3236fSMatthew G. Knepley         } else {
6333ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) ++newn;
6334ebb3236fSMatthew G. Knepley         }
633593a5981aSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
633693a5981aSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6337fd59a867SMatthew G. Knepley       }
6338ebb3236fSMatthew G. Knepley       ierr = PetscMalloc1(newn,&newidx);CHKERRQ(ierr);
6339ebb3236fSMatthew G. Knepley       newn = 0;
6340ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6341ebb3236fSMatthew G. Knepley         IS              tmp;
6342ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6343ebb3236fSMatthew G. Knepley 
6344c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6345ebb3236fSMatthew G. Knepley         if (!tmp) continue;
6346ebb3236fSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
6347ebb3236fSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6348ebb3236fSMatthew G. Knepley         if (isFE[field]) {
6349ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) newidx[newn++] = idx[p];
6350ebb3236fSMatthew G. Knepley         } else {
6351ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) newidx[newn++] = idx[p];
6352ebb3236fSMatthew G. Knepley         }
6353ebb3236fSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
6354ebb3236fSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6355ebb3236fSMatthew G. Knepley       }
6356ebb3236fSMatthew G. Knepley       ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), newn, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6357ebb3236fSMatthew G. Knepley     }
6358fd59a867SMatthew G. Knepley   }
6359fd59a867SMatthew G. Knepley   /* Handle discretization */
6360ebb3236fSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&numComp,numFields*(dim+1),&numDof);CHKERRQ(ierr);
6361fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
63620bacfa0aSMatthew G. Knepley     PetscObject obj;
63630bacfa0aSMatthew G. Knepley 
63640bacfa0aSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6365ebb3236fSMatthew G. Knepley     if (isFE[f]) {
63660bacfa0aSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
6367286d8613SMatthew G. Knepley       const PetscInt *numFieldDof;
6368fd59a867SMatthew G. Knepley       PetscInt        d;
6369fd59a867SMatthew G. Knepley 
6370fd59a867SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
6371286d8613SMatthew G. Knepley       ierr = PetscFEGetNumDof(fe, &numFieldDof);CHKERRQ(ierr);
6372286d8613SMatthew G. Knepley       for (d = 0; d < dim+1; ++d) numDof[f*(dim+1)+d] = numFieldDof[d];
6373ebb3236fSMatthew G. Knepley     } else {
63740bacfa0aSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
63750bacfa0aSMatthew G. Knepley 
63760bacfa0aSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
63770bacfa0aSMatthew G. Knepley       numDof[f*(dim+1)+dim] = numComp[f];
6378ebb3236fSMatthew G. Knepley     }
6379fd59a867SMatthew G. Knepley   }
6380286d8613SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6381286d8613SMatthew G. Knepley     PetscInt d;
6382286d8613SMatthew G. Knepley     for (d = 1; d < dim; ++d) {
6383286d8613SMatthew 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.");
6384286d8613SMatthew G. Knepley     }
6385286d8613SMatthew G. Knepley   }
6386ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSection(dm, dim, numFields, numComp, numDof, numBC, bcFields, bcComps, bcPoints, NULL, &section);CHKERRQ(ierr);
6387fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6388fd59a867SMatthew G. Knepley     PetscFE     fe;
6389fd59a867SMatthew G. Knepley     const char *name;
639018abd763SMatthew G. Knepley 
639118abd763SMatthew G. Knepley     ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
6392fd59a867SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
6393fd59a867SMatthew G. Knepley     ierr = PetscSectionSetFieldName(section, f, name);CHKERRQ(ierr);
6394fd59a867SMatthew G. Knepley   }
6395fd59a867SMatthew G. Knepley   ierr = DMSetDefaultSection(dm, section);CHKERRQ(ierr);
6396fd59a867SMatthew G. Knepley   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
63970e0f25f2SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {ierr = ISDestroy(&bcPoints[bc]);CHKERRQ(ierr);ierr = ISDestroy(&bcComps[bc]);CHKERRQ(ierr);}
63980e0f25f2SMatthew G. Knepley   ierr = PetscFree3(bcFields,bcPoints,bcComps);CHKERRQ(ierr);
6399286d8613SMatthew G. Knepley   ierr = PetscFree2(numComp,numDof);CHKERRQ(ierr);
6400ebb3236fSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
6401bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6402bceba477SMatthew G. Knepley }
6403bceba477SMatthew G. Knepley 
6404bceba477SMatthew G. Knepley #undef __FUNCT__
64050aef6b92SMatthew G. Knepley #define __FUNCT__ "DMPlexGetRegularRefinement"
64060aef6b92SMatthew G. Knepley /*@
64070aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
64080aef6b92SMatthew G. Knepley 
64090aef6b92SMatthew G. Knepley   Input Parameter:
64100aef6b92SMatthew G. Knepley . dm - The DMPlex object
64110aef6b92SMatthew G. Knepley 
64120aef6b92SMatthew G. Knepley   Output Parameter:
64130aef6b92SMatthew G. Knepley . regular - The flag
64140aef6b92SMatthew G. Knepley 
64150aef6b92SMatthew G. Knepley   Level: intermediate
64160aef6b92SMatthew G. Knepley 
64170aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
64180aef6b92SMatthew G. Knepley @*/
64190aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
64200aef6b92SMatthew G. Knepley {
64210aef6b92SMatthew G. Knepley   PetscFunctionBegin;
64220aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64230aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
64240aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
64250aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
64260aef6b92SMatthew G. Knepley }
64270aef6b92SMatthew G. Knepley 
64280aef6b92SMatthew G. Knepley #undef __FUNCT__
64290aef6b92SMatthew G. Knepley #define __FUNCT__ "DMPlexSetRegularRefinement"
64300aef6b92SMatthew G. Knepley /*@
64310aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
64320aef6b92SMatthew G. Knepley 
64330aef6b92SMatthew G. Knepley   Input Parameters:
64340aef6b92SMatthew G. Knepley + dm - The DMPlex object
64350aef6b92SMatthew G. Knepley - regular - The flag
64360aef6b92SMatthew G. Knepley 
64370aef6b92SMatthew G. Knepley   Level: intermediate
64380aef6b92SMatthew G. Knepley 
64390aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
64400aef6b92SMatthew G. Knepley @*/
64410aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
64420aef6b92SMatthew G. Knepley {
64430aef6b92SMatthew G. Knepley   PetscFunctionBegin;
64440aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64450aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
64460aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
64470aef6b92SMatthew G. Knepley }
64480aef6b92SMatthew G. Knepley 
6449f7c74593SToby Isaac /* anchors */
6450a68b90caSToby Isaac #undef __FUNCT__
6451a17985deSToby Isaac #define __FUNCT__ "DMPlexGetAnchors"
6452a68b90caSToby Isaac /*@
6453f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
6454f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
6455a68b90caSToby Isaac 
6456e228b242SToby Isaac   not collective
6457a68b90caSToby Isaac 
6458a68b90caSToby Isaac   Input Parameters:
6459a68b90caSToby Isaac . dm - The DMPlex object
6460a68b90caSToby Isaac 
6461a68b90caSToby Isaac   Output Parameters:
6462a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
6463a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
6464a68b90caSToby Isaac 
6465a68b90caSToby Isaac 
6466a68b90caSToby Isaac   Level: intermediate
6467a68b90caSToby Isaac 
6468f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
6469a68b90caSToby Isaac @*/
6470a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
6471a68b90caSToby Isaac {
6472a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
647341e6d900SToby Isaac   PetscErrorCode ierr;
6474a68b90caSToby Isaac 
6475a68b90caSToby Isaac   PetscFunctionBegin;
6476a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
647741e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
6478a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
6479a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
6480a68b90caSToby Isaac   PetscFunctionReturn(0);
6481a68b90caSToby Isaac }
6482a68b90caSToby Isaac 
6483a68b90caSToby Isaac #undef __FUNCT__
6484a17985deSToby Isaac #define __FUNCT__ "DMPlexSetAnchors"
6485a68b90caSToby Isaac /*@
6486f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
6487f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
6488a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
6489a68b90caSToby Isaac 
6490a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
6491f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
6492a68b90caSToby Isaac 
6493e228b242SToby Isaac   collective on dm
6494a68b90caSToby Isaac 
6495a68b90caSToby Isaac   Input Parameters:
6496a68b90caSToby Isaac + dm - The DMPlex object
6497e228b242SToby 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).
6498e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
6499a68b90caSToby Isaac 
6500a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
6501a68b90caSToby Isaac 
6502a68b90caSToby Isaac   Level: intermediate
6503a68b90caSToby Isaac 
6504f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
6505a68b90caSToby Isaac @*/
6506a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
6507a68b90caSToby Isaac {
6508a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6509e228b242SToby Isaac   PetscMPIInt    result;
6510a68b90caSToby Isaac   PetscErrorCode ierr;
6511a68b90caSToby Isaac 
6512a68b90caSToby Isaac   PetscFunctionBegin;
6513a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6514e228b242SToby Isaac   if (anchorSection) {
6515e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
6516e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
6517e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
6518e228b242SToby Isaac   }
6519e228b242SToby Isaac   if (anchorIS) {
6520e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
6521e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
6522e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
6523e228b242SToby Isaac   }
6524a68b90caSToby Isaac 
6525a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
6526a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
6527a68b90caSToby Isaac   plex->anchorSection = anchorSection;
6528a68b90caSToby Isaac 
6529a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
6530a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
6531a68b90caSToby Isaac   plex->anchorIS = anchorIS;
6532a68b90caSToby Isaac 
6533a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
6534a68b90caSToby Isaac   if (anchorIS && anchorSection) {
6535a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
6536a68b90caSToby Isaac     const PetscInt *anchors;
6537a68b90caSToby Isaac 
6538a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
6539a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
6540a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
6541a68b90caSToby Isaac     for (a = 0; a < size; a++) {
6542a68b90caSToby Isaac       PetscInt p;
6543a68b90caSToby Isaac 
6544a68b90caSToby Isaac       p = anchors[a];
6545a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
6546a68b90caSToby Isaac         PetscInt dof;
6547a68b90caSToby Isaac 
6548a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6549a68b90caSToby Isaac         if (dof) {
6550a68b90caSToby Isaac           PetscErrorCode ierr2;
6551a68b90caSToby Isaac 
6552a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
65538ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
6554a68b90caSToby Isaac         }
6555a68b90caSToby Isaac       }
6556a68b90caSToby Isaac     }
6557a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
6558a68b90caSToby Isaac   }
6559a68b90caSToby Isaac #endif
6560f7c74593SToby Isaac   /* reset the generic constraints */
6561f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
6562a68b90caSToby Isaac   PetscFunctionReturn(0);
6563a68b90caSToby Isaac }
6564a68b90caSToby Isaac 
6565a68b90caSToby Isaac #undef __FUNCT__
6566f7c74593SToby Isaac #define __FUNCT__ "DMPlexCreateConstraintSection_Anchors"
6567f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
6568a68b90caSToby Isaac {
6569f7c74593SToby Isaac   PetscSection anchorSection;
65706995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
6571a68b90caSToby Isaac   PetscErrorCode ierr;
6572a68b90caSToby Isaac 
6573a68b90caSToby Isaac   PetscFunctionBegin;
6574a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6575a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
6576e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
6577a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
65786995de1eSToby Isaac   if (numFields) {
6579719ab38cSToby Isaac     PetscInt f;
6580a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
6581719ab38cSToby Isaac 
6582719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
6583719ab38cSToby Isaac       PetscInt numComp;
6584719ab38cSToby Isaac 
6585719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
6586719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
6587719ab38cSToby Isaac     }
65886995de1eSToby Isaac   }
6589a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
65906995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
65916995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
65926995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
65936995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
6594a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
6595a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
6596a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6597a68b90caSToby Isaac     if (dof) {
6598a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
6599a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
6600a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
6601a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
6602a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
6603a68b90caSToby Isaac       }
6604a68b90caSToby Isaac     }
6605a68b90caSToby Isaac   }
6606a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
6607a68b90caSToby Isaac   PetscFunctionReturn(0);
6608a68b90caSToby Isaac }
6609a68b90caSToby Isaac 
6610a68b90caSToby Isaac #undef __FUNCT__
6611f7c74593SToby Isaac #define __FUNCT__ "DMPlexCreateConstraintMatrix_Anchors"
6612f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
6613a68b90caSToby Isaac {
6614f7c74593SToby Isaac   PetscSection aSec;
66150ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
66160ac89760SToby Isaac   const PetscInt *anchors;
66170ac89760SToby Isaac   PetscInt numFields, f;
661866ad2231SToby Isaac   IS aIS;
66190ac89760SToby Isaac   PetscErrorCode ierr;
66200ac89760SToby Isaac 
66210ac89760SToby Isaac   PetscFunctionBegin;
66220ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66230ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
66240ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
66250ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
66260ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
6627302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
6628a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
662966ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
66306995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
66316995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
66320ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
66330ac89760SToby Isaac   i[0] = 0;
66340ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
66350ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
6636f19733c5SToby Isaac     PetscInt rDof, rOff, r;
6637f19733c5SToby Isaac 
6638f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
6639f19733c5SToby Isaac     if (!rDof) continue;
6640f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
66410ac89760SToby Isaac     if (numFields) {
66420ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
66430ac89760SToby Isaac         annz = 0;
6644f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
6645f19733c5SToby Isaac           a = anchors[rOff + r];
66460ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
66470ac89760SToby Isaac           annz += aDof;
66480ac89760SToby Isaac         }
66490ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
66500ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
66510ac89760SToby Isaac         for (q = 0; q < dof; q++) {
66520ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
66530ac89760SToby Isaac         }
66540ac89760SToby Isaac       }
66550ac89760SToby Isaac     }
66560ac89760SToby Isaac     else {
66570ac89760SToby Isaac       annz = 0;
66580ac89760SToby Isaac       for (q = 0; q < dof; q++) {
66590ac89760SToby Isaac         a = anchors[off + q];
66600ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
66610ac89760SToby Isaac         annz += aDof;
66620ac89760SToby Isaac       }
66630ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
66640ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
66650ac89760SToby Isaac       for (q = 0; q < dof; q++) {
66660ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
66670ac89760SToby Isaac       }
66680ac89760SToby Isaac     }
66690ac89760SToby Isaac   }
66700ac89760SToby Isaac   nnz = i[m];
66710ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
66720ac89760SToby Isaac   offset = 0;
66730ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
66740ac89760SToby Isaac     if (numFields) {
66750ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
66760ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
66770ac89760SToby Isaac         for (q = 0; q < dof; q++) {
66780ac89760SToby Isaac           PetscInt rDof, rOff, r;
66790ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
66800ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
66810ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
66820ac89760SToby Isaac             PetscInt s;
66830ac89760SToby Isaac 
66840ac89760SToby Isaac             a = anchors[rOff + r];
66850ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
66860ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
66870ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
66880ac89760SToby Isaac               j[offset++] = aOff + s;
66890ac89760SToby Isaac             }
66900ac89760SToby Isaac           }
66910ac89760SToby Isaac         }
66920ac89760SToby Isaac       }
66930ac89760SToby Isaac     }
66940ac89760SToby Isaac     else {
66950ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
66960ac89760SToby Isaac       for (q = 0; q < dof; q++) {
66970ac89760SToby Isaac         PetscInt rDof, rOff, r;
66980ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
66990ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
67000ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
67010ac89760SToby Isaac           PetscInt s;
67020ac89760SToby Isaac 
67030ac89760SToby Isaac           a = anchors[rOff + r];
67040ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
67050ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
67060ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
67070ac89760SToby Isaac             j[offset++] = aOff + s;
67080ac89760SToby Isaac           }
67090ac89760SToby Isaac         }
67100ac89760SToby Isaac       }
67110ac89760SToby Isaac     }
67120ac89760SToby Isaac   }
67130ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
671425570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
671525570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
671666ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
67170ac89760SToby Isaac   PetscFunctionReturn(0);
67180ac89760SToby Isaac }
67190ac89760SToby Isaac 
67200ac89760SToby Isaac #undef __FUNCT__
672166ad2231SToby Isaac #define __FUNCT__ "DMCreateDefaultConstraints_Plex"
672266ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
672366ad2231SToby Isaac {
6724f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6725f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
672666ad2231SToby Isaac   Mat            cMat;
672766ad2231SToby Isaac   PetscErrorCode ierr;
672866ad2231SToby Isaac 
672966ad2231SToby Isaac   PetscFunctionBegin;
673066ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6731a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
673266ad2231SToby Isaac   if (anchorSection) {
6733e228b242SToby Isaac     PetscDS  ds;
6734e228b242SToby Isaac     PetscInt nf;
6735e228b242SToby Isaac 
6736f7c74593SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
6737f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
6738f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
6739e228b242SToby Isaac     ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
6740302440fdSBarry Smith     ierr = PetscDSGetNumFields(ds,&nf);CHKERRQ(ierr);
6741e228b242SToby Isaac     if (nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
674266ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
674366ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
674466ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
674566ad2231SToby Isaac   }
674666ad2231SToby Isaac   PetscFunctionReturn(0);
674766ad2231SToby Isaac }
6748