xref: /petsc/src/dm/impls/plex/plex.c (revision 89eabcff77fe848446d2f8dd49ac575bd4df58fe)
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 */
9fa534816SMatthew G. Knepley PetscLogEvent DMPLEX_Interpolate, PETSCPARTITIONER_Partition, DMPLEX_Distribute, DMPLEX_DistributeCones, DMPLEX_DistributeLabels, DMPLEX_DistributeSF, DMPLEX_DistributeOverlap, DMPLEX_DistributeField, DMPLEX_DistributeData, DMPLEX_Migrate, DMPLEX_GlobalToNaturalBegin, DMPLEX_GlobalToNaturalEnd, DMPLEX_NaturalToGlobalBegin, DMPLEX_NaturalToGlobalEnd, DMPLEX_Stratify, DMPLEX_Preallocate, DMPLEX_ResidualFEM, DMPLEX_JacobianFEM, DMPLEX_InterpolatorFEM, DMPLEX_InjectorFEM, DMPLEX_IntegralFEM, DMPLEX_CreateGmsh;
10552f7358SJed Brown 
115a576424SJed Brown PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);
122c40f234SMatthew G. Knepley PETSC_EXTERN PetscErrorCode VecLoad_Default(Vec, PetscViewer);
13552f7358SJed Brown 
147e42fee7SMatthew G. Knepley #undef __FUNCT__
157afe7537SMatthew G. Knepley #define __FUNCT__ "DMPlexGetFieldType_Internal"
167afe7537SMatthew G. Knepley PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
177e42fee7SMatthew G. Knepley {
1880d5bdc6SMatthew G. Knepley   PetscInt       dim, pStart, pEnd, vStart, vEnd, cStart, cEnd, cEndInterior, vdof = 0, cdof = 0;
197e42fee7SMatthew G. Knepley   PetscErrorCode ierr;
207e42fee7SMatthew G. Knepley 
217e42fee7SMatthew G. Knepley   PetscFunctionBegin;
227e42fee7SMatthew G. Knepley   *ft  = PETSC_VTK_POINT_FIELD;
23c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
247e42fee7SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
257e42fee7SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2680d5bdc6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2780d5bdc6SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
287e42fee7SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
297e42fee7SMatthew G. Knepley   if (field >= 0) {
307e42fee7SMatthew G. Knepley     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, vStart, field, &vdof);CHKERRQ(ierr);}
317e42fee7SMatthew G. Knepley     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, cStart, field, &cdof);CHKERRQ(ierr);}
327e42fee7SMatthew G. Knepley   } else {
337e42fee7SMatthew G. Knepley     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetDof(section, vStart, &vdof);CHKERRQ(ierr);}
347e42fee7SMatthew G. Knepley     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetDof(section, cStart, &cdof);CHKERRQ(ierr);}
357e42fee7SMatthew G. Knepley   }
367e42fee7SMatthew G. Knepley   if (vdof) {
377e42fee7SMatthew G. Knepley     *sStart = vStart;
387e42fee7SMatthew G. Knepley     *sEnd   = vEnd;
397e42fee7SMatthew G. Knepley     if (vdof == dim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
407e42fee7SMatthew G. Knepley     else             *ft = PETSC_VTK_POINT_FIELD;
417e42fee7SMatthew G. Knepley   } else if (cdof) {
427e42fee7SMatthew G. Knepley     *sStart = cStart;
437e42fee7SMatthew G. Knepley     *sEnd   = cEnd;
447e42fee7SMatthew G. Knepley     if (cdof == dim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
457e42fee7SMatthew G. Knepley     else             *ft = PETSC_VTK_CELL_FIELD;
467e42fee7SMatthew G. Knepley   } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Could not classify input Vec for VTK");
477e42fee7SMatthew G. Knepley   PetscFunctionReturn(0);
487e42fee7SMatthew G. Knepley }
497e42fee7SMatthew G. Knepley 
50552f7358SJed Brown #undef __FUNCT__
51552f7358SJed Brown #define __FUNCT__ "VecView_Plex_Local"
52552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
53552f7358SJed Brown {
54552f7358SJed Brown   DM             dm;
55b136c2c9SMatthew G. Knepley   PetscBool      isvtk, ishdf5, isseq;
56552f7358SJed Brown   PetscErrorCode ierr;
57552f7358SJed Brown 
58552f7358SJed Brown   PetscFunctionBegin;
59552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
6082f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
61552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
62b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
63b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
64ef31f671SMatthew G. Knepley   if (isvtk || ishdf5) {
65ef31f671SMatthew G. Knepley     PetscInt  numFields;
66ef31f671SMatthew G. Knepley     PetscBool fem = PETSC_FALSE;
67ef31f671SMatthew G. Knepley 
68ef31f671SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
69ef31f671SMatthew G. Knepley     if (numFields) {
70ef31f671SMatthew G. Knepley       PetscObject fe;
71ef31f671SMatthew G. Knepley 
72ef31f671SMatthew G. Knepley       ierr = DMGetField(dm, 0, &fe);CHKERRQ(ierr);
73ef31f671SMatthew G. Knepley       if (fe->classid == PETSCFE_CLASSID) fem = PETSC_TRUE;
74ef31f671SMatthew G. Knepley     }
75bdd6f66aSToby Isaac     if (fem) {ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, v, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);}
76ef31f671SMatthew G. Knepley   }
77552f7358SJed Brown   if (isvtk) {
78552f7358SJed Brown     PetscSection            section;
79b136c2c9SMatthew G. Knepley     PetscViewerVTKFieldType ft;
80b136c2c9SMatthew G. Knepley     PetscInt                pStart, pEnd;
81552f7358SJed Brown 
82552f7358SJed Brown     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
837afe7537SMatthew G. Knepley     ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
84552f7358SJed Brown     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr); /* viewer drops reference */
85552f7358SJed Brown     ierr = PetscObjectReference((PetscObject) v);CHKERRQ(ierr);  /* viewer drops reference */
86552f7358SJed Brown     ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, ft, (PetscObject) v);CHKERRQ(ierr);
87b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
88b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
89b136c2c9SMatthew G. Knepley     ierr = VecView_Plex_Local_HDF5(v, viewer);CHKERRQ(ierr);
90b136c2c9SMatthew G. Knepley #else
91b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
92b136c2c9SMatthew G. Knepley #endif
93552f7358SJed Brown   } else {
9455f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
9555f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
96552f7358SJed Brown   }
97552f7358SJed Brown   PetscFunctionReturn(0);
98552f7358SJed Brown }
99552f7358SJed Brown 
100552f7358SJed Brown #undef __FUNCT__
101e412dcbdSMatthew G. Knepley #define __FUNCT__ "VecView_Plex_Draw"
102e412dcbdSMatthew G. Knepley PetscErrorCode VecView_Plex_Draw(Vec v, PetscViewer viewer)
103e412dcbdSMatthew G. Knepley {
104e412dcbdSMatthew G. Knepley   DM                 dm;
105e412dcbdSMatthew G. Knepley   PetscDraw          draw, popup;
106e412dcbdSMatthew G. Knepley   DM                 cdm;
107e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
108e412dcbdSMatthew G. Knepley   Vec                coordinates;
109e412dcbdSMatthew G. Knepley   const PetscScalar *coords, *array;
110e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
111e412dcbdSMatthew G. Knepley   PetscReal          min, max;
112e412dcbdSMatthew G. Knepley   PetscBool          isnull;
113e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N, level;
114e412dcbdSMatthew G. Knepley   const char        *name;
115e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
116e412dcbdSMatthew G. Knepley 
117e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
118e412dcbdSMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
119e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
120e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
121e412dcbdSMatthew G. Knepley   ierr = DMGetCoarsenLevel(dm, &level);CHKERRQ(ierr);
122e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
123e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
124e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
125e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
126e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
127e412dcbdSMatthew G. Knepley 
128e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
129e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
130e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
131e412dcbdSMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
132e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, name);CHKERRQ(ierr);
133e412dcbdSMatthew G. Knepley 
134e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
135e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
136e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
1370c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
1380c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
139e412dcbdSMatthew G. Knepley   }
140e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
141e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
142e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
143e412dcbdSMatthew G. Knepley 
144e412dcbdSMatthew G. Knepley   ierr = VecMin(v, NULL, &min);CHKERRQ(ierr);
145e412dcbdSMatthew G. Knepley   ierr = VecMax(v, NULL, &max);CHKERRQ(ierr);
146e412dcbdSMatthew G. Knepley   ierr = PetscDrawGetPopup(draw, &popup);CHKERRQ(ierr);
147e412dcbdSMatthew G. Knepley   ierr = PetscDrawScalePopup(popup, min, max);CHKERRQ(ierr);
148e412dcbdSMatthew G. Knepley 
149e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(v, &array);CHKERRQ(ierr);
150e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
15199a2f7bcSMatthew G. Knepley     PetscScalar *coords = NULL, *a = NULL;
152e412dcbdSMatthew G. Knepley     PetscInt     numCoords, color;
153e412dcbdSMatthew G. Knepley 
154e412dcbdSMatthew G. Knepley     ierr = DMPlexPointGlobalRead(dm, c, array, &a);CHKERRQ(ierr);
155e412dcbdSMatthew G. Knepley     color = PetscDrawRealToColor(PetscRealPart(a[0]), min, max);
156e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
157e412dcbdSMatthew G. Knepley     switch (numCoords) {
158e412dcbdSMatthew G. Knepley     case 6:
1590c81f2a8SMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), color, color, color);CHKERRQ(ierr);
160e412dcbdSMatthew G. Knepley       break;
161e412dcbdSMatthew G. Knepley     case 8:
1620c81f2a8SMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), color, color, color);CHKERRQ(ierr);
1630c81f2a8SMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), color, color, color);CHKERRQ(ierr);
164e412dcbdSMatthew G. Knepley       break;
165e412dcbdSMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
166e412dcbdSMatthew G. Knepley     }
167e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
168e412dcbdSMatthew G. Knepley   }
169e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &array);CHKERRQ(ierr);
170e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
171e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
172e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
173e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
174e412dcbdSMatthew G. Knepley }
175e412dcbdSMatthew G. Knepley 
176e412dcbdSMatthew G. Knepley #undef __FUNCT__
177552f7358SJed Brown #define __FUNCT__ "VecView_Plex"
178552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
179552f7358SJed Brown {
180552f7358SJed Brown   DM             dm;
181e412dcbdSMatthew G. Knepley   PetscBool      isvtk, ishdf5, isdraw, isseq;
182552f7358SJed Brown   PetscErrorCode ierr;
183552f7358SJed Brown 
184552f7358SJed Brown   PetscFunctionBegin;
185552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
18682f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
187552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
18833c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
189e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW, &isdraw);CHKERRQ(ierr);
19055f2e967SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
191552f7358SJed Brown   if (isvtk) {
192552f7358SJed Brown     Vec         locv;
193552f7358SJed Brown     const char *name;
194552f7358SJed Brown 
195552f7358SJed Brown     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
196552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
197552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
198552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
199552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
200552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
201552f7358SJed Brown     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
202b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
203b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
204b136c2c9SMatthew G. Knepley     ierr = VecView_Plex_HDF5(v, viewer);CHKERRQ(ierr);
205b136c2c9SMatthew G. Knepley #else
206b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
207b136c2c9SMatthew G. Knepley #endif
208e412dcbdSMatthew G. Knepley   } else if (isdraw) {
209e412dcbdSMatthew G. Knepley     ierr = VecView_Plex_Draw(v, viewer);CHKERRQ(ierr);
210552f7358SJed Brown   } else {
21155f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
21255f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
213552f7358SJed Brown   }
214552f7358SJed Brown   PetscFunctionReturn(0);
215552f7358SJed Brown }
216552f7358SJed Brown 
217552f7358SJed Brown #undef __FUNCT__
218d930f514SMatthew G. Knepley #define __FUNCT__ "VecView_Plex_Native"
219d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
220d930f514SMatthew G. Knepley {
221d930f514SMatthew G. Knepley   DM                dm;
222d930f514SMatthew G. Knepley   MPI_Comm          comm;
223d930f514SMatthew G. Knepley   PetscViewerFormat format;
224d930f514SMatthew G. Knepley   Vec               v;
225d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
226d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
227d930f514SMatthew G. Knepley 
228d930f514SMatthew G. Knepley   PetscFunctionBegin;
229d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
230d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
2312c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
232d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
233d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
234d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
235d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
236d930f514SMatthew G. Knepley     const char *vecname;
237d930f514SMatthew G. Knepley     PetscInt    n, nroots;
238d930f514SMatthew G. Knepley 
239d930f514SMatthew G. Knepley     if (dm->sfNatural) {
240ca43db0aSBarry Smith       ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
241d930f514SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
242d930f514SMatthew G. Knepley       if (n == nroots) {
243d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
244d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
245d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
246d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
247d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
248d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
249d930f514SMatthew G. Knepley     } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
250d930f514SMatthew G. Knepley   } else {
251d930f514SMatthew G. Knepley     /* we are viewing a natural DMPlex vec. */
252d930f514SMatthew G. Knepley     v = originalv;
253d930f514SMatthew G. Knepley   }
254d930f514SMatthew G. Knepley   if (ishdf5) {
255d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
256d930f514SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native(v, viewer);CHKERRQ(ierr);
257d930f514SMatthew G. Knepley #else
258d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
259d930f514SMatthew G. Knepley #endif
260d930f514SMatthew G. Knepley   } else if (isvtk) {
261d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
262d930f514SMatthew G. Knepley   } else {
263d930f514SMatthew G. Knepley     PetscBool isseq;
264d930f514SMatthew G. Knepley 
265d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
266d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
267d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
268d930f514SMatthew G. Knepley   }
269d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
270d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
271d930f514SMatthew G. Knepley }
272d930f514SMatthew G. Knepley 
273d930f514SMatthew G. Knepley #undef __FUNCT__
2742c40f234SMatthew G. Knepley #define __FUNCT__ "VecLoad_Plex_Local"
2752c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
2762c40f234SMatthew G. Knepley {
2772c40f234SMatthew G. Knepley   DM             dm;
2782c40f234SMatthew G. Knepley   PetscBool      ishdf5;
2792c40f234SMatthew G. Knepley   PetscErrorCode ierr;
2802c40f234SMatthew G. Knepley 
2812c40f234SMatthew G. Knepley   PetscFunctionBegin;
2822c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
2832c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
2842c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
2852c40f234SMatthew G. Knepley   if (ishdf5) {
2862c40f234SMatthew G. Knepley     DM          dmBC;
2872c40f234SMatthew G. Knepley     Vec         gv;
2882c40f234SMatthew G. Knepley     const char *name;
2892c40f234SMatthew G. Knepley 
2902c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
2912c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
2922c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
2932c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
2942c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
2952c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
2962c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
2972c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
2982c40f234SMatthew G. Knepley   } else {
2992c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
3002c40f234SMatthew G. Knepley   }
3012c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
3022c40f234SMatthew G. Knepley }
3032c40f234SMatthew G. Knepley 
3042c40f234SMatthew G. Knepley #undef __FUNCT__
3052c40f234SMatthew G. Knepley #define __FUNCT__ "VecLoad_Plex"
3062c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
3072c40f234SMatthew G. Knepley {
3082c40f234SMatthew G. Knepley   DM             dm;
3092c40f234SMatthew G. Knepley   PetscBool      ishdf5;
3102c40f234SMatthew G. Knepley   PetscErrorCode ierr;
3112c40f234SMatthew G. Knepley 
3122c40f234SMatthew G. Knepley   PetscFunctionBegin;
3132c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
3142c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
3152c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
3162c40f234SMatthew G. Knepley   if (ishdf5) {
317878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
318b136c2c9SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5(v, viewer);CHKERRQ(ierr);
319b136c2c9SMatthew G. Knepley #else
320b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
321878b459fSMatthew G. Knepley #endif
3222c40f234SMatthew G. Knepley   } else {
3232c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
324552f7358SJed Brown   }
325552f7358SJed Brown   PetscFunctionReturn(0);
326552f7358SJed Brown }
327552f7358SJed Brown 
328552f7358SJed Brown #undef __FUNCT__
329d930f514SMatthew G. Knepley #define __FUNCT__ "VecLoad_Plex_Native"
330d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
331d930f514SMatthew G. Knepley {
332d930f514SMatthew G. Knepley   DM                dm;
333d930f514SMatthew G. Knepley   PetscViewerFormat format;
334d930f514SMatthew G. Knepley   PetscBool         ishdf5;
335d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
336d930f514SMatthew G. Knepley 
337d930f514SMatthew G. Knepley   PetscFunctionBegin;
338d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
339d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
340d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
341d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
342d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
343d930f514SMatthew G. Knepley     if (dm->sfNatural) {
344d930f514SMatthew G. Knepley       if (ishdf5) {
345d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
346d930f514SMatthew G. Knepley         Vec         v;
347d930f514SMatthew G. Knepley         const char *vecname;
348d930f514SMatthew G. Knepley 
349d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
350d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
351d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
352d930f514SMatthew G. Knepley         ierr = VecLoad_Plex_HDF5_Native(v, viewer);CHKERRQ(ierr);
353d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
354d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
355d930f514SMatthew G. Knepley         ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
356d930f514SMatthew G. Knepley #else
357d930f514SMatthew G. Knepley         SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
358d930f514SMatthew G. Knepley #endif
359d930f514SMatthew G. Knepley       } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
360d930f514SMatthew G. Knepley     }
361d930f514SMatthew G. Knepley   }
362d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
363d930f514SMatthew G. Knepley }
364d930f514SMatthew G. Knepley 
365d930f514SMatthew G. Knepley #undef __FUNCT__
366731e8ddeSMatthew G. Knepley #define __FUNCT__ "DMPlexView_Ascii_Geometry"
367731e8ddeSMatthew G. Knepley PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
368731e8ddeSMatthew G. Knepley {
369731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
370731e8ddeSMatthew G. Knepley   Vec                coordinates;
371731e8ddeSMatthew G. Knepley   DMLabel            depthLabel;
372731e8ddeSMatthew G. Knepley   const char        *name[4];
373731e8ddeSMatthew G. Knepley   const PetscScalar *a;
374731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
375731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
376731e8ddeSMatthew G. Knepley 
377731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
378731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
379731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
380731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
381731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
382731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
383731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
384731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
385731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
386731e8ddeSMatthew G. Knepley   name[1]     = "edge";
387731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
388731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
389731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
390731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
391731e8ddeSMatthew G. Knepley     PetscInt  closureSize, cl;
392731e8ddeSMatthew G. Knepley 
393f347f43bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D:\n", c);CHKERRQ(ierr);
394731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
395731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
396731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
397731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
398731e8ddeSMatthew G. Knepley 
399731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
400731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
401731e8ddeSMatthew G. Knepley       if (!dof) continue;
402731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
403731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
404f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
405731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
406731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
407731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
408731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
40941477eefSMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
410731e8ddeSMatthew G. Knepley         }
411731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
412731e8ddeSMatthew G. Knepley       }
413731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
414731e8ddeSMatthew G. Knepley     }
415731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
416731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
417731e8ddeSMatthew G. Knepley   }
418731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
419731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
420731e8ddeSMatthew G. Knepley }
421731e8ddeSMatthew G. Knepley 
422731e8ddeSMatthew G. Knepley #undef __FUNCT__
423552f7358SJed Brown #define __FUNCT__ "DMPlexView_Ascii"
424552f7358SJed Brown PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
425552f7358SJed Brown {
426552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
427552f7358SJed Brown   DM                cdm;
428552f7358SJed Brown   DMLabel           markers;
429552f7358SJed Brown   PetscSection      coordSection;
430552f7358SJed Brown   Vec               coordinates;
431552f7358SJed Brown   PetscViewerFormat format;
432552f7358SJed Brown   PetscErrorCode    ierr;
433552f7358SJed Brown 
434552f7358SJed Brown   PetscFunctionBegin;
435552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
436552f7358SJed Brown   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
437552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
438552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
439552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
440552f7358SJed Brown     const char *name;
441552f7358SJed Brown     PetscInt    maxConeSize, maxSupportSize;
442552f7358SJed Brown     PetscInt    pStart, pEnd, p;
443552f7358SJed Brown     PetscMPIInt rank, size;
444552f7358SJed Brown 
44582f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
44682f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
447552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
448552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
449552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
450552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "Mesh '%s':\n", name);CHKERRQ(ierr);
451552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "orientation is missing\n", name);CHKERRQ(ierr);
452552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "cap --> base:\n", name);CHKERRQ(ierr);
4534d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
4544d4c343aSBarry Smith     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max sizes cone: %D support: %D\n", rank,maxConeSize, maxSupportSize);CHKERRQ(ierr);
455552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
456552f7358SJed Brown       PetscInt dof, off, s;
457552f7358SJed Brown 
458552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
459552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
460552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
461e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
462552f7358SJed Brown       }
463552f7358SJed Brown     }
464552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
465552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "base <-- cap:\n", name);CHKERRQ(ierr);
466552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
467552f7358SJed Brown       PetscInt dof, off, c;
468552f7358SJed Brown 
469552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
470552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
471552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
472e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
473552f7358SJed Brown       }
474552f7358SJed Brown     }
475552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4764d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
4770298fd71SBarry Smith     ierr = PetscSectionGetChart(coordSection, &pStart, NULL);CHKERRQ(ierr);
478552f7358SJed Brown     if (pStart >= 0) {ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);}
479c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
480552f7358SJed Brown     ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);
481552f7358SJed Brown     if (size > 1) {
482552f7358SJed Brown       PetscSF sf;
483552f7358SJed Brown 
484552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
485552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
486552f7358SJed Brown     }
487552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
488552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
4890588280cSMatthew G. Knepley     const char  *name, *color;
4900588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
4910588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
492552f7358SJed Brown     PetscReal    scale         = 2.0;
4930588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
4940588280cSMatthew G. Knepley     double       tcoords[3];
495552f7358SJed Brown     PetscScalar *coords;
4960588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
497552f7358SJed Brown     PetscMPIInt  rank, size;
4980588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
499552f7358SJed Brown 
5000588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
501552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
502c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
5030588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
5040588280cSMatthew G. Knepley     numColors  = 10;
5050588280cSMatthew G. Knepley     numLColors = 10;
5060588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
507c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
508c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
509c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
5100588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
511c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
5120588280cSMatthew G. Knepley     if (!useColors) {
5130588280cSMatthew G. Knepley       numColors = 3;
5140588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
5150588280cSMatthew G. Knepley     }
516c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
5170588280cSMatthew G. Knepley     if (!useColors) {
5180588280cSMatthew G. Knepley       numLColors = 4;
5190588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
5200588280cSMatthew G. Knepley     }
52182f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
52282f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
523552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
524770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
5250588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
526552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
527552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
528552f7358SJed Brown \\usetikzlibrary{arrows}\n\
5290588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
5300588280cSMatthew G. Knepley     if (size > 1) {
531f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
532770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
533770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
534770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
535770b213bSMatthew G Knepley         } else if (p > 0) {
536770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
537770b213bSMatthew G Knepley         }
538770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
539770b213bSMatthew G Knepley       }
5400588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
5410588280cSMatthew G. Knepley     }
5420588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", 1.0);CHKERRQ(ierr);
543552f7358SJed Brown     /* Plot vertices */
544552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
545552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
5464d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
547552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
548552f7358SJed Brown       PetscInt  off, dof, d;
5490588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
550552f7358SJed Brown 
551552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
552552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
5530588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
554f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
5550588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
5560588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
5570588280cSMatthew G. Knepley         tcoords[d] = PetscAbsReal(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
5580588280cSMatthew G. Knepley       }
5590588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
5600588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
561552f7358SJed Brown       for (d = 0; d < dof; ++d) {
562552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
5630588280cSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]);CHKERRQ(ierr);
564552f7358SJed Brown       }
5650588280cSMatthew G. Knepley       color = colors[rank%numColors];
5660588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
5670588280cSMatthew G. Knepley         PetscInt val;
568c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
5690588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
5700588280cSMatthew G. Knepley       }
5710588280cSMatthew G. Knepley       if (useNumbers) {
572e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
5730588280cSMatthew G. Knepley       } else {
574e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
5750588280cSMatthew G. Knepley       }
576552f7358SJed Brown     }
577552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
578552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
579552f7358SJed Brown     /* Plot edges */
5800588280cSMatthew G. Knepley     if (depth > 1) {ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);}
5810588280cSMatthew G. Knepley     if (dim < 3 && useNumbers) {
582552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
583552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
584552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
585552f7358SJed Brown         const PetscInt *cone;
586552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
587552f7358SJed Brown 
588552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
589f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
590552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
591552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
592552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
593552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
594552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
595552f7358SJed Brown         for (d = 0; d < dof; ++d) {
5960588280cSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
5970588280cSMatthew G. Knepley           tcoords[d] = PetscAbsReal(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
598552f7358SJed Brown         }
5990588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
6000588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
6010588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
6020588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
603f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
6040588280cSMatthew G. Knepley         }
6050588280cSMatthew G. Knepley         color = colors[rank%numColors];
6060588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
6070588280cSMatthew G. Knepley           PetscInt val;
608c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
6090750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
6100588280cSMatthew G. Knepley         }
611e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
612552f7358SJed Brown       }
613552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
614552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
615552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
6160588280cSMatthew G. Knepley     }
617552f7358SJed Brown     /* Plot cells */
6180588280cSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
6190588280cSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
6200588280cSMatthew G. Knepley         const PetscInt *cone;
6210588280cSMatthew G. Knepley 
6220588280cSMatthew G. Knepley         color = colors[rank%numColors];
6230588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
6240588280cSMatthew G. Knepley           PetscInt val;
625c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
6260588280cSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
6270588280cSMatthew G. Knepley         }
6280588280cSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
629e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
6300588280cSMatthew G. Knepley       }
6310588280cSMatthew G. Knepley     } else {
632552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
633552f7358SJed Brown       for (c = cStart; c < cEnd; ++c) {
6340298fd71SBarry Smith         PetscInt *closure = NULL;
635552f7358SJed Brown         PetscInt  closureSize, firstPoint = -1;
636552f7358SJed Brown 
637552f7358SJed Brown         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
638552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
639552f7358SJed Brown         for (p = 0; p < closureSize*2; p += 2) {
640552f7358SJed Brown           const PetscInt point = closure[p];
641552f7358SJed Brown 
642552f7358SJed Brown           if ((point < vStart) || (point >= vEnd)) continue;
643552f7358SJed Brown           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
644e4b003c7SBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
645552f7358SJed Brown           if (firstPoint < 0) firstPoint = point;
646552f7358SJed Brown         }
647552f7358SJed Brown         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
648e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
649552f7358SJed Brown         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
650552f7358SJed Brown       }
6510588280cSMatthew G. Knepley     }
652552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6534d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
6540588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
655770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
6560588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
6570588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
6580588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
6590588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
660552f7358SJed Brown   } else {
66182f516ccSBarry Smith     MPI_Comm    comm;
662834065abSMatthew G. Knepley     PetscInt   *sizes, *hybsizes;
663834065abSMatthew G. Knepley     PetscInt    locDepth, depth, dim, d, pMax[4];
664552f7358SJed Brown     PetscInt    pStart, pEnd, p;
665a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
6661143a3c0SMatthew G. Knepley     const char *name;
667552f7358SJed Brown     PetscMPIInt size;
668552f7358SJed Brown 
66982f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
670552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
671c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6725f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
6731143a3c0SMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimensions:\n", name, dim);CHKERRQ(ierr);}
6741143a3c0SMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimensions:\n", dim);CHKERRQ(ierr);}
675552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
676b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
677bb81ee50SMatthew G. Knepley     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, &pMax[1], &pMax[0]);CHKERRQ(ierr);
678dcca6d9dSJed Brown     ierr = PetscMalloc2(size,&sizes,size,&hybsizes);CHKERRQ(ierr);
679552f7358SJed Brown     if (depth == 1) {
680552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
681552f7358SJed Brown       pEnd = pEnd - pStart;
682552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
683f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);CHKERRQ(ierr);
684552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
685552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
686552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
687552f7358SJed Brown       pEnd = pEnd - pStart;
688552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
689552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
690552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
691552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
692552f7358SJed Brown     } else {
693552f7358SJed Brown       for (d = 0; d <= dim; d++) {
694552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
695834065abSMatthew G. Knepley         pEnd    -= pStart;
696834065abSMatthew G. Knepley         pMax[d] -= pStart;
697552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
698834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
699552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
700834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
701834065abSMatthew G. Knepley           if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
702834065abSMatthew G. Knepley           else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
703834065abSMatthew G. Knepley         }
704552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
705552f7358SJed Brown       }
706552f7358SJed Brown     }
707834065abSMatthew G. Knepley     ierr = PetscFree2(sizes,hybsizes);CHKERRQ(ierr);
708c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
709a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
710a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
711a57dd577SMatthew G Knepley       DMLabel         label;
712a57dd577SMatthew G Knepley       const char     *name;
713a57dd577SMatthew G Knepley       IS              valueIS;
714a57dd577SMatthew G Knepley       const PetscInt *values;
715a57dd577SMatthew G Knepley       PetscInt        numValues, v;
716a57dd577SMatthew G Knepley 
717c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
718c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
719a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
720f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata of sizes (", name, numValues);CHKERRQ(ierr);
721a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
722a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
723120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
724a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
725a57dd577SMatthew G Knepley         PetscInt size;
726a57dd577SMatthew G Knepley 
727a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
728a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
729f347f43bSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer, "%D", size);CHKERRQ(ierr);
730a57dd577SMatthew G Knepley       }
731a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
732120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
733a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
7344d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
735a57dd577SMatthew G Knepley     }
736a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
7378e7ff633SMatthew G. Knepley     if (cdm) {
7388e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
7398e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
7408e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
7418e7ff633SMatthew G. Knepley     }
742552f7358SJed Brown   }
743552f7358SJed Brown   PetscFunctionReturn(0);
744552f7358SJed Brown }
745552f7358SJed Brown 
746552f7358SJed Brown #undef __FUNCT__
747e412dcbdSMatthew G. Knepley #define __FUNCT__ "DMPlexView_Draw"
748e412dcbdSMatthew G. Knepley PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
749e412dcbdSMatthew G. Knepley {
750e412dcbdSMatthew G. Knepley   PetscDraw          draw;
751e412dcbdSMatthew G. Knepley   DM                 cdm;
752e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
753e412dcbdSMatthew G. Knepley   Vec                coordinates;
754e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
755e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
756e412dcbdSMatthew G. Knepley   PetscBool          isnull;
757e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
758e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
759e412dcbdSMatthew G. Knepley 
760e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
761e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
762e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
763e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
764e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
765e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
766e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
767e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
768e412dcbdSMatthew G. Knepley 
769e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
770e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
771e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
772e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
773e412dcbdSMatthew G. Knepley 
774e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
775e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
776e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
7770c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
7780c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
779e412dcbdSMatthew G. Knepley   }
780e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
781e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
782e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
783e412dcbdSMatthew G. Knepley 
784e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
785e412dcbdSMatthew G. Knepley     PetscScalar *coords = NULL;
786e412dcbdSMatthew G. Knepley     PetscInt     numCoords;
787e412dcbdSMatthew G. Knepley 
788e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
789e412dcbdSMatthew G. Knepley     switch (numCoords) {
790e412dcbdSMatthew G. Knepley     case 6:
7910c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
7920c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
7930c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
794e412dcbdSMatthew G. Knepley       break;
795e412dcbdSMatthew G. Knepley     case 8:
7960c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
7970c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
7980c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
7990c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
800e412dcbdSMatthew G. Knepley       break;
801e412dcbdSMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
802e412dcbdSMatthew G. Knepley     }
803e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
804e412dcbdSMatthew G. Knepley   }
805e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
806e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
807e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
808e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
809e412dcbdSMatthew G. Knepley }
810e412dcbdSMatthew G. Knepley 
811e412dcbdSMatthew G. Knepley #undef __FUNCT__
812552f7358SJed Brown #define __FUNCT__ "DMView_Plex"
813552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
814552f7358SJed Brown {
815e412dcbdSMatthew G. Knepley   PetscBool      iascii, ishdf5, isvtk, isdraw;
816552f7358SJed Brown   PetscErrorCode ierr;
817552f7358SJed Brown 
818552f7358SJed Brown   PetscFunctionBegin;
819552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
820552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
821552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
822fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
823c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
824e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
825552f7358SJed Brown   if (iascii) {
826552f7358SJed Brown     ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
827c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
828c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
8297afe7537SMatthew G. Knepley     ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_VIZ);CHKERRQ(ierr);
830c6ccd67eSMatthew G. Knepley     ierr = DMPlexView_HDF5(dm, viewer);CHKERRQ(ierr);
8317afe7537SMatthew G. Knepley     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
832c6ccd67eSMatthew G. Knepley #else
833c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
834552f7358SJed Brown #endif
835e412dcbdSMatthew G. Knepley   } else if (isvtk) {
836fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
837e412dcbdSMatthew G. Knepley   } else if (isdraw) {
838e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
839fcf6c8fdSToby Isaac   }
840552f7358SJed Brown   PetscFunctionReturn(0);
841552f7358SJed Brown }
842552f7358SJed Brown 
8432c40f234SMatthew G. Knepley #undef __FUNCT__
8442c40f234SMatthew G. Knepley #define __FUNCT__ "DMLoad_Plex"
8452c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
8462c40f234SMatthew G. Knepley {
8472c40f234SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
8482c40f234SMatthew G. Knepley   PetscErrorCode ierr;
8492c40f234SMatthew G. Knepley 
8502c40f234SMatthew G. Knepley   PetscFunctionBegin;
8512c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8522c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
8532c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERBINARY, &isbinary);CHKERRQ(ierr);
8542c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
8552c40f234SMatthew G. Knepley   if (isbinary) {SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Do not yet support binary viewers");}
8562c40f234SMatthew G. Knepley   else if (ishdf5) {
8572c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
85810b7db4eSMatthew G. Knepley     ierr = DMPlexLoad_HDF5(dm, viewer);CHKERRQ(ierr);
8592c40f234SMatthew G. Knepley #else
8602c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
861552f7358SJed Brown #endif
862552f7358SJed Brown   }
863552f7358SJed Brown   PetscFunctionReturn(0);
864552f7358SJed Brown }
865552f7358SJed Brown 
8666c73c22cSMatthew G. Knepley 
8676c73c22cSMatthew G. Knepley #undef __FUNCT__
868552f7358SJed Brown #define __FUNCT__ "DMDestroy_Plex"
869552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
870552f7358SJed Brown {
871552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
872552f7358SJed Brown   PetscErrorCode ierr;
873552f7358SJed Brown 
874552f7358SJed Brown   PetscFunctionBegin;
8750d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
876552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
877552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
878552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
879552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
880be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
881552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
882552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
883d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
884d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
88577623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
886a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
887552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
888552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
889a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
890a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
891d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
892d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
893d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
894d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
895d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
896d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
897fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
898552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
899552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
900713918a9SToby Isaac   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMAdaptLabel_C",NULL);CHKERRQ(ierr);
901552f7358SJed Brown   PetscFunctionReturn(0);
902552f7358SJed Brown }
903552f7358SJed Brown 
904552f7358SJed Brown #undef __FUNCT__
905552f7358SJed Brown #define __FUNCT__ "DMCreateMatrix_Plex"
906b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
907552f7358SJed Brown {
9088d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
909acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
910552f7358SJed Brown   PetscInt               localSize;
911837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
912552f7358SJed Brown   PetscErrorCode         ierr;
913b412c318SBarry Smith   MatType                mtype;
9141428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
915552f7358SJed Brown 
916552f7358SJed Brown   PetscFunctionBegin;
917607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
918b412c318SBarry Smith   mtype = dm->mattype;
919552f7358SJed Brown   ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
920552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
921552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
92282f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
923552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
924552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
925552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
926acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
927acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
928552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
929552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
930552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
931552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
932552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
933552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
934552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
935837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
936552f7358SJed Brown   if (!isShell) {
937be36d101SStefano Zampini     PetscSection subSection;
938837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
939be36d101SStefano Zampini     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal, bsMax, bsMin, *ltogidx, lsize;
940fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
941552f7358SJed Brown 
94200140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
943be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
944be36d101SStefano Zampini       PetscSection section;
945be36d101SStefano Zampini       PetscInt     size;
946be36d101SStefano Zampini 
947be36d101SStefano Zampini       ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
948be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
949be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
950be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
951be36d101SStefano Zampini     } else {
95200140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
953be36d101SStefano Zampini     }
954552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
955be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
956a9d99c84SMatthew G. Knepley       PetscInt bdof;
957a9d99c84SMatthew G. Knepley 
958552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
959fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
9601d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
9611d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
9621d17a0a3SMatthew G. Knepley       if (dof) {
9631d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
964be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
965be36d101SStefano Zampini       }
966be36d101SStefano Zampini       if (isMatIS) {
967be36d101SStefano Zampini         PetscInt loff,c,off;
968be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
969be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
970be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
971552f7358SJed Brown       }
9722a28c762SMatthew G Knepley     }
9732a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
9742a28c762SMatthew G Knepley     bsLocal = bs;
975b2566f29SBarry Smith     ierr = MPIU_Allreduce(&bsLocal, &bsMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
9762a28c762SMatthew G Knepley     bsLocal = bs < 0 ? bsMax : bs;
977b2566f29SBarry Smith     ierr = MPIU_Allreduce(&bsLocal, &bsMin, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
978bff27382SMatthew G. Knepley     if (bsMin != bsMax) {bs = 1;}
979bff27382SMatthew G. Knepley     else                {bs = bsMax;}
980be36d101SStefano Zampini     if (isMatIS) {
981be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs < 0 ? 1 : bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
982be36d101SStefano Zampini     }
983be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
984be36d101SStefano Zampini     if (isMatIS) {
985be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
986be36d101SStefano Zampini     }
9871795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
9888d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
989552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
990552f7358SJed Brown   }
991552f7358SJed Brown   PetscFunctionReturn(0);
992552f7358SJed Brown }
993552f7358SJed Brown 
994552f7358SJed Brown #undef __FUNCT__
995be36d101SStefano Zampini #define __FUNCT__ "DMPlexGetSubdomainSection"
996559da874SStefano Zampini /*
997be36d101SStefano Zampini   DMPlexGetSubdomainGlobalSection - Returns the section associated with the subdomain
998be36d101SStefano Zampini 
999be36d101SStefano Zampini   Not collective
1000be36d101SStefano Zampini 
1001be36d101SStefano Zampini   Input Parameter:
1002be36d101SStefano Zampini . mesh - The DMPlex
1003be36d101SStefano Zampini 
1004be36d101SStefano Zampini   Output Parameters:
1005be36d101SStefano Zampini . subsection - The subdomain section
1006be36d101SStefano Zampini 
1007be36d101SStefano Zampini   Level: developer
1008be36d101SStefano Zampini 
1009be36d101SStefano Zampini .seealso:
1010559da874SStefano Zampini */
1011be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1012be36d101SStefano Zampini {
1013be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1014be36d101SStefano Zampini   PetscErrorCode ierr;
1015be36d101SStefano Zampini 
1016be36d101SStefano Zampini   PetscFunctionBegin;
1017be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1018be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1019be36d101SStefano Zampini     PetscSection section;
1020be36d101SStefano Zampini     PetscSF      sf;
1021be36d101SStefano Zampini 
1022be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
1023be36d101SStefano Zampini     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
1024be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1025be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1026be36d101SStefano Zampini   }
1027be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1028be36d101SStefano Zampini   PetscFunctionReturn(0);
1029be36d101SStefano Zampini }
1030be36d101SStefano Zampini 
1031be36d101SStefano Zampini #undef __FUNCT__
1032552f7358SJed Brown #define __FUNCT__ "DMPlexGetChart"
1033552f7358SJed Brown /*@
1034552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1035552f7358SJed Brown 
1036552f7358SJed Brown   Not collective
1037552f7358SJed Brown 
1038552f7358SJed Brown   Input Parameter:
1039552f7358SJed Brown . mesh - The DMPlex
1040552f7358SJed Brown 
1041552f7358SJed Brown   Output Parameters:
1042552f7358SJed Brown + pStart - The first mesh point
1043552f7358SJed Brown - pEnd   - The upper bound for mesh points
1044552f7358SJed Brown 
1045552f7358SJed Brown   Level: beginner
1046552f7358SJed Brown 
1047552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1048552f7358SJed Brown @*/
1049552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1050552f7358SJed Brown {
1051552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1052552f7358SJed Brown   PetscErrorCode ierr;
1053552f7358SJed Brown 
1054552f7358SJed Brown   PetscFunctionBegin;
1055552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1056552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1057552f7358SJed Brown   PetscFunctionReturn(0);
1058552f7358SJed Brown }
1059552f7358SJed Brown 
1060552f7358SJed Brown #undef __FUNCT__
1061552f7358SJed Brown #define __FUNCT__ "DMPlexSetChart"
1062552f7358SJed Brown /*@
1063552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1064552f7358SJed Brown 
1065552f7358SJed Brown   Not collective
1066552f7358SJed Brown 
1067552f7358SJed Brown   Input Parameters:
1068552f7358SJed Brown + mesh - The DMPlex
1069552f7358SJed Brown . pStart - The first mesh point
1070552f7358SJed Brown - pEnd   - The upper bound for mesh points
1071552f7358SJed Brown 
1072552f7358SJed Brown   Output Parameters:
1073552f7358SJed Brown 
1074552f7358SJed Brown   Level: beginner
1075552f7358SJed Brown 
1076552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1077552f7358SJed Brown @*/
1078552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1079552f7358SJed Brown {
1080552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1081552f7358SJed Brown   PetscErrorCode ierr;
1082552f7358SJed Brown 
1083552f7358SJed Brown   PetscFunctionBegin;
1084552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1085552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1086552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1087552f7358SJed Brown   PetscFunctionReturn(0);
1088552f7358SJed Brown }
1089552f7358SJed Brown 
1090552f7358SJed Brown #undef __FUNCT__
1091552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeSize"
1092552f7358SJed Brown /*@
1093552f7358SJed Brown   DMPlexGetConeSize - Return the number of in-edges for this point in the Sieve DAG
1094552f7358SJed Brown 
1095552f7358SJed Brown   Not collective
1096552f7358SJed Brown 
1097552f7358SJed Brown   Input Parameters:
1098552f7358SJed Brown + mesh - The DMPlex
1099552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1100552f7358SJed Brown 
1101552f7358SJed Brown   Output Parameter:
1102552f7358SJed Brown . size - The cone size for point p
1103552f7358SJed Brown 
1104552f7358SJed Brown   Level: beginner
1105552f7358SJed Brown 
1106552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1107552f7358SJed Brown @*/
1108552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1109552f7358SJed Brown {
1110552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1111552f7358SJed Brown   PetscErrorCode ierr;
1112552f7358SJed Brown 
1113552f7358SJed Brown   PetscFunctionBegin;
1114552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1115552f7358SJed Brown   PetscValidPointer(size, 3);
1116552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1117552f7358SJed Brown   PetscFunctionReturn(0);
1118552f7358SJed Brown }
1119552f7358SJed Brown 
1120552f7358SJed Brown #undef __FUNCT__
1121552f7358SJed Brown #define __FUNCT__ "DMPlexSetConeSize"
1122552f7358SJed Brown /*@
1123552f7358SJed Brown   DMPlexSetConeSize - Set the number of in-edges for this point in the Sieve DAG
1124552f7358SJed Brown 
1125552f7358SJed Brown   Not collective
1126552f7358SJed Brown 
1127552f7358SJed Brown   Input Parameters:
1128552f7358SJed Brown + mesh - The DMPlex
1129552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1130552f7358SJed Brown - size - The cone size for point p
1131552f7358SJed Brown 
1132552f7358SJed Brown   Output Parameter:
1133552f7358SJed Brown 
1134552f7358SJed Brown   Note:
1135552f7358SJed Brown   This should be called after DMPlexSetChart().
1136552f7358SJed Brown 
1137552f7358SJed Brown   Level: beginner
1138552f7358SJed Brown 
1139552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1140552f7358SJed Brown @*/
1141552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1142552f7358SJed Brown {
1143552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1144552f7358SJed Brown   PetscErrorCode ierr;
1145552f7358SJed Brown 
1146552f7358SJed Brown   PetscFunctionBegin;
1147552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1148552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
11490d644c17SKarl Rupp 
1150552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1151552f7358SJed Brown   PetscFunctionReturn(0);
1152552f7358SJed Brown }
1153552f7358SJed Brown 
1154552f7358SJed Brown #undef __FUNCT__
1155f5a469b9SMatthew G. Knepley #define __FUNCT__ "DMPlexAddConeSize"
1156f5a469b9SMatthew G. Knepley /*@
1157f5a469b9SMatthew G. Knepley   DMPlexAddConeSize - Add the given number of in-edges to this point in the Sieve DAG
1158f5a469b9SMatthew G. Knepley 
1159f5a469b9SMatthew G. Knepley   Not collective
1160f5a469b9SMatthew G. Knepley 
1161f5a469b9SMatthew G. Knepley   Input Parameters:
1162f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1163f5a469b9SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1164f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1165f5a469b9SMatthew G. Knepley 
1166f5a469b9SMatthew G. Knepley   Output Parameter:
1167f5a469b9SMatthew G. Knepley 
1168f5a469b9SMatthew G. Knepley   Note:
1169f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1170f5a469b9SMatthew G. Knepley 
1171f5a469b9SMatthew G. Knepley   Level: beginner
1172f5a469b9SMatthew G. Knepley 
1173f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1174f5a469b9SMatthew G. Knepley @*/
1175f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1176f5a469b9SMatthew G. Knepley {
1177f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1178f5a469b9SMatthew G. Knepley   PetscInt       csize;
1179f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1180f5a469b9SMatthew G. Knepley 
1181f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1182f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1183f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1184f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1185f5a469b9SMatthew G. Knepley 
1186f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1187f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1188f5a469b9SMatthew G. Knepley }
1189f5a469b9SMatthew G. Knepley 
1190f5a469b9SMatthew G. Knepley #undef __FUNCT__
1191552f7358SJed Brown #define __FUNCT__ "DMPlexGetCone"
1192552f7358SJed Brown /*@C
1193552f7358SJed Brown   DMPlexGetCone - Return the points on the in-edges for this point in the Sieve DAG
1194552f7358SJed Brown 
1195552f7358SJed Brown   Not collective
1196552f7358SJed Brown 
1197552f7358SJed Brown   Input Parameters:
1198552f7358SJed Brown + mesh - The DMPlex
1199552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1200552f7358SJed Brown 
1201552f7358SJed Brown   Output Parameter:
1202552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1203552f7358SJed Brown 
1204552f7358SJed Brown   Level: beginner
1205552f7358SJed Brown 
12063813dfbdSMatthew G Knepley   Fortran Notes:
12073813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
12083813dfbdSMatthew G Knepley   include petsc.h90 in your code.
12093813dfbdSMatthew G Knepley 
12103813dfbdSMatthew G Knepley   You must also call DMPlexRestoreCone() after you finish using the returned array.
1211552f7358SJed Brown 
1212552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart()
1213552f7358SJed Brown @*/
1214552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1215552f7358SJed Brown {
1216552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1217552f7358SJed Brown   PetscInt       off;
1218552f7358SJed Brown   PetscErrorCode ierr;
1219552f7358SJed Brown 
1220552f7358SJed Brown   PetscFunctionBegin;
1221552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1222552f7358SJed Brown   PetscValidPointer(cone, 3);
1223552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1224552f7358SJed Brown   *cone = &mesh->cones[off];
1225552f7358SJed Brown   PetscFunctionReturn(0);
1226552f7358SJed Brown }
1227552f7358SJed Brown 
1228552f7358SJed Brown #undef __FUNCT__
1229552f7358SJed Brown #define __FUNCT__ "DMPlexSetCone"
1230552f7358SJed Brown /*@
1231552f7358SJed Brown   DMPlexSetCone - Set the points on the in-edges for this point in the Sieve DAG
1232552f7358SJed Brown 
1233552f7358SJed Brown   Not collective
1234552f7358SJed Brown 
1235552f7358SJed Brown   Input Parameters:
1236552f7358SJed Brown + mesh - The DMPlex
1237552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1238552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1239552f7358SJed Brown 
1240552f7358SJed Brown   Output Parameter:
1241552f7358SJed Brown 
1242552f7358SJed Brown   Note:
1243552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1244552f7358SJed Brown 
1245552f7358SJed Brown   Level: beginner
1246552f7358SJed Brown 
1247552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1248552f7358SJed Brown @*/
1249552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1250552f7358SJed Brown {
1251552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1252552f7358SJed Brown   PetscInt       pStart, pEnd;
1253552f7358SJed Brown   PetscInt       dof, off, c;
1254552f7358SJed Brown   PetscErrorCode ierr;
1255552f7358SJed Brown 
1256552f7358SJed Brown   PetscFunctionBegin;
1257552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1258552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1259552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1260552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1261552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
126282f516ccSBarry 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);
1263552f7358SJed Brown   for (c = 0; c < dof; ++c) {
126482f516ccSBarry 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);
1265552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1266552f7358SJed Brown   }
1267552f7358SJed Brown   PetscFunctionReturn(0);
1268552f7358SJed Brown }
1269552f7358SJed Brown 
1270552f7358SJed Brown #undef __FUNCT__
1271552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeOrientation"
1272552f7358SJed Brown /*@C
1273552f7358SJed Brown   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the Sieve DAG
1274552f7358SJed Brown 
1275552f7358SJed Brown   Not collective
1276552f7358SJed Brown 
1277552f7358SJed Brown   Input Parameters:
1278552f7358SJed Brown + mesh - The DMPlex
1279552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1280552f7358SJed Brown 
1281552f7358SJed Brown   Output Parameter:
1282552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1283552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1284552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1285552f7358SJed Brown                     the index of the cone point on which to start.
1286552f7358SJed Brown 
1287552f7358SJed Brown   Level: beginner
1288552f7358SJed Brown 
12893813dfbdSMatthew G Knepley   Fortran Notes:
12903813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
12913813dfbdSMatthew G Knepley   include petsc.h90 in your code.
12923813dfbdSMatthew G Knepley 
12933813dfbdSMatthew G Knepley   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1294552f7358SJed Brown 
1295552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1296552f7358SJed Brown @*/
1297552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1298552f7358SJed Brown {
1299552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1300552f7358SJed Brown   PetscInt       off;
1301552f7358SJed Brown   PetscErrorCode ierr;
1302552f7358SJed Brown 
1303552f7358SJed Brown   PetscFunctionBegin;
1304552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1305552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1306552f7358SJed Brown   {
1307552f7358SJed Brown     PetscInt dof;
1308552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1309552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1310552f7358SJed Brown   }
1311552f7358SJed Brown #endif
1312552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
13130d644c17SKarl Rupp 
1314552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1315552f7358SJed Brown   PetscFunctionReturn(0);
1316552f7358SJed Brown }
1317552f7358SJed Brown 
1318552f7358SJed Brown #undef __FUNCT__
1319552f7358SJed Brown #define __FUNCT__ "DMPlexSetConeOrientation"
1320552f7358SJed Brown /*@
1321552f7358SJed Brown   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the Sieve DAG
1322552f7358SJed Brown 
1323552f7358SJed Brown   Not collective
1324552f7358SJed Brown 
1325552f7358SJed Brown   Input Parameters:
1326552f7358SJed Brown + mesh - The DMPlex
1327552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1328552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1329552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1330552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1331552f7358SJed Brown                     the index of the cone point on which to start.
1332552f7358SJed Brown 
1333552f7358SJed Brown   Output Parameter:
1334552f7358SJed Brown 
1335552f7358SJed Brown   Note:
1336552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1337552f7358SJed Brown 
1338552f7358SJed Brown   Level: beginner
1339552f7358SJed Brown 
1340552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1341552f7358SJed Brown @*/
1342552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
1343552f7358SJed Brown {
1344552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1345552f7358SJed Brown   PetscInt       pStart, pEnd;
1346552f7358SJed Brown   PetscInt       dof, off, c;
1347552f7358SJed Brown   PetscErrorCode ierr;
1348552f7358SJed Brown 
1349552f7358SJed Brown   PetscFunctionBegin;
1350552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1351552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1352552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1353552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
1354552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
135582f516ccSBarry 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);
1356552f7358SJed Brown   for (c = 0; c < dof; ++c) {
1357552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
1358552f7358SJed Brown 
1359552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
136082f516ccSBarry 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);
1361552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
1362552f7358SJed Brown   }
1363552f7358SJed Brown   PetscFunctionReturn(0);
1364552f7358SJed Brown }
1365552f7358SJed Brown 
1366552f7358SJed Brown #undef __FUNCT__
1367552f7358SJed Brown #define __FUNCT__ "DMPlexInsertCone"
1368552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
1369552f7358SJed Brown {
1370552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1371552f7358SJed Brown   PetscInt       pStart, pEnd;
1372552f7358SJed Brown   PetscInt       dof, off;
1373552f7358SJed Brown   PetscErrorCode ierr;
1374552f7358SJed Brown 
1375552f7358SJed Brown   PetscFunctionBegin;
1376552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1377552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
137882f516ccSBarry 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);
137982f516ccSBarry 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);
138077c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
138177c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
138277c88f5bSMatthew 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);
1383552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
1384552f7358SJed Brown   PetscFunctionReturn(0);
1385552f7358SJed Brown }
1386552f7358SJed Brown 
1387552f7358SJed Brown #undef __FUNCT__
138877c88f5bSMatthew G Knepley #define __FUNCT__ "DMPlexInsertConeOrientation"
138977c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
139077c88f5bSMatthew G Knepley {
139177c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
139277c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
139377c88f5bSMatthew G Knepley   PetscInt       dof, off;
139477c88f5bSMatthew G Knepley   PetscErrorCode ierr;
139577c88f5bSMatthew G Knepley 
139677c88f5bSMatthew G Knepley   PetscFunctionBegin;
139777c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
139877c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
139977c88f5bSMatthew 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);
140077c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
140177c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
140277c88f5bSMatthew 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);
140377c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
140477c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
140577c88f5bSMatthew G Knepley }
140677c88f5bSMatthew G Knepley 
140777c88f5bSMatthew G Knepley #undef __FUNCT__
1408552f7358SJed Brown #define __FUNCT__ "DMPlexGetSupportSize"
1409552f7358SJed Brown /*@
1410552f7358SJed Brown   DMPlexGetSupportSize - Return the number of out-edges for this point in the Sieve DAG
1411552f7358SJed Brown 
1412552f7358SJed Brown   Not collective
1413552f7358SJed Brown 
1414552f7358SJed Brown   Input Parameters:
1415552f7358SJed Brown + mesh - The DMPlex
1416552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1417552f7358SJed Brown 
1418552f7358SJed Brown   Output Parameter:
1419552f7358SJed Brown . size - The support size for point p
1420552f7358SJed Brown 
1421552f7358SJed Brown   Level: beginner
1422552f7358SJed Brown 
1423552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
1424552f7358SJed Brown @*/
1425552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
1426552f7358SJed Brown {
1427552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1428552f7358SJed Brown   PetscErrorCode ierr;
1429552f7358SJed Brown 
1430552f7358SJed Brown   PetscFunctionBegin;
1431552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1432552f7358SJed Brown   PetscValidPointer(size, 3);
1433552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
1434552f7358SJed Brown   PetscFunctionReturn(0);
1435552f7358SJed Brown }
1436552f7358SJed Brown 
1437552f7358SJed Brown #undef __FUNCT__
1438552f7358SJed Brown #define __FUNCT__ "DMPlexSetSupportSize"
1439552f7358SJed Brown /*@
1440552f7358SJed Brown   DMPlexSetSupportSize - Set the number of out-edges for this point in the Sieve DAG
1441552f7358SJed Brown 
1442552f7358SJed Brown   Not collective
1443552f7358SJed Brown 
1444552f7358SJed Brown   Input Parameters:
1445552f7358SJed Brown + mesh - The DMPlex
1446552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1447552f7358SJed Brown - size - The support size for point p
1448552f7358SJed Brown 
1449552f7358SJed Brown   Output Parameter:
1450552f7358SJed Brown 
1451552f7358SJed Brown   Note:
1452552f7358SJed Brown   This should be called after DMPlexSetChart().
1453552f7358SJed Brown 
1454552f7358SJed Brown   Level: beginner
1455552f7358SJed Brown 
1456552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
1457552f7358SJed Brown @*/
1458552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
1459552f7358SJed Brown {
1460552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1461552f7358SJed Brown   PetscErrorCode ierr;
1462552f7358SJed Brown 
1463552f7358SJed Brown   PetscFunctionBegin;
1464552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1465552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
14660d644c17SKarl Rupp 
1467552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
1468552f7358SJed Brown   PetscFunctionReturn(0);
1469552f7358SJed Brown }
1470552f7358SJed Brown 
1471552f7358SJed Brown #undef __FUNCT__
1472552f7358SJed Brown #define __FUNCT__ "DMPlexGetSupport"
1473552f7358SJed Brown /*@C
1474552f7358SJed Brown   DMPlexGetSupport - Return the points on the out-edges for this point in the Sieve DAG
1475552f7358SJed Brown 
1476552f7358SJed Brown   Not collective
1477552f7358SJed Brown 
1478552f7358SJed Brown   Input Parameters:
1479552f7358SJed Brown + mesh - The DMPlex
1480552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1481552f7358SJed Brown 
1482552f7358SJed Brown   Output Parameter:
1483552f7358SJed Brown . support - An array of points which are on the out-edges for point p
1484552f7358SJed Brown 
1485552f7358SJed Brown   Level: beginner
1486552f7358SJed Brown 
14873813dfbdSMatthew G Knepley   Fortran Notes:
14883813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
14893813dfbdSMatthew G Knepley   include petsc.h90 in your code.
14903813dfbdSMatthew G Knepley 
14913813dfbdSMatthew G Knepley   You must also call DMPlexRestoreSupport() after you finish using the returned array.
14923813dfbdSMatthew G Knepley 
1493552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1494552f7358SJed Brown @*/
1495552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
1496552f7358SJed Brown {
1497552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1498552f7358SJed Brown   PetscInt       off;
1499552f7358SJed Brown   PetscErrorCode ierr;
1500552f7358SJed Brown 
1501552f7358SJed Brown   PetscFunctionBegin;
1502552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1503552f7358SJed Brown   PetscValidPointer(support, 3);
1504552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
1505552f7358SJed Brown   *support = &mesh->supports[off];
1506552f7358SJed Brown   PetscFunctionReturn(0);
1507552f7358SJed Brown }
1508552f7358SJed Brown 
1509552f7358SJed Brown #undef __FUNCT__
1510552f7358SJed Brown #define __FUNCT__ "DMPlexSetSupport"
1511552f7358SJed Brown /*@
1512552f7358SJed Brown   DMPlexSetSupport - Set the points on the out-edges for this point in the Sieve DAG
1513552f7358SJed Brown 
1514552f7358SJed Brown   Not collective
1515552f7358SJed Brown 
1516552f7358SJed Brown   Input Parameters:
1517552f7358SJed Brown + mesh - The DMPlex
1518552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1519552f7358SJed Brown - support - An array of points which are on the in-edges for point p
1520552f7358SJed Brown 
1521552f7358SJed Brown   Output Parameter:
1522552f7358SJed Brown 
1523552f7358SJed Brown   Note:
1524552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
1525552f7358SJed Brown 
1526552f7358SJed Brown   Level: beginner
1527552f7358SJed Brown 
1528552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
1529552f7358SJed Brown @*/
1530552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
1531552f7358SJed Brown {
1532552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1533552f7358SJed Brown   PetscInt       pStart, pEnd;
1534552f7358SJed Brown   PetscInt       dof, off, c;
1535552f7358SJed Brown   PetscErrorCode ierr;
1536552f7358SJed Brown 
1537552f7358SJed Brown   PetscFunctionBegin;
1538552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1539552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1540552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1541552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
1542552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
154382f516ccSBarry 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);
1544552f7358SJed Brown   for (c = 0; c < dof; ++c) {
154582f516ccSBarry 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);
1546552f7358SJed Brown     mesh->supports[off+c] = support[c];
1547552f7358SJed Brown   }
1548552f7358SJed Brown   PetscFunctionReturn(0);
1549552f7358SJed Brown }
1550552f7358SJed Brown 
1551552f7358SJed Brown #undef __FUNCT__
1552552f7358SJed Brown #define __FUNCT__ "DMPlexInsertSupport"
1553552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
1554552f7358SJed Brown {
1555552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1556552f7358SJed Brown   PetscInt       pStart, pEnd;
1557552f7358SJed Brown   PetscInt       dof, off;
1558552f7358SJed Brown   PetscErrorCode ierr;
1559552f7358SJed Brown 
1560552f7358SJed Brown   PetscFunctionBegin;
1561552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1562552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1563552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1564552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
156582f516ccSBarry 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);
156682f516ccSBarry 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);
156782f516ccSBarry 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);
1568552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
1569552f7358SJed Brown   PetscFunctionReturn(0);
1570552f7358SJed Brown }
1571552f7358SJed Brown 
1572552f7358SJed Brown #undef __FUNCT__
1573552f7358SJed Brown #define __FUNCT__ "DMPlexGetTransitiveClosure"
1574552f7358SJed Brown /*@C
1575552f7358SJed Brown   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the Sieve DAG
1576552f7358SJed Brown 
1577552f7358SJed Brown   Not collective
1578552f7358SJed Brown 
1579552f7358SJed Brown   Input Parameters:
1580552f7358SJed Brown + mesh - The DMPlex
1581552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1582552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
15830298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
1584552f7358SJed Brown 
1585552f7358SJed Brown   Output Parameters:
1586552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
1587552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
1588552f7358SJed Brown 
1589552f7358SJed Brown   Note:
15900298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
1591552f7358SJed Brown 
15923813dfbdSMatthew G Knepley   Fortran Notes:
15933813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
15943813dfbdSMatthew G Knepley   include petsc.h90 in your code.
15953813dfbdSMatthew G Knepley 
15963813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
15973813dfbdSMatthew G Knepley 
1598552f7358SJed Brown   Level: beginner
1599552f7358SJed Brown 
1600552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1601552f7358SJed Brown @*/
1602552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1603552f7358SJed Brown {
1604552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
1605552f7358SJed Brown   PetscInt       *closure, *fifo;
16060298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
1607552f7358SJed Brown   PetscInt        tmpSize, t;
1608552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
1609552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
1610552f7358SJed Brown   PetscErrorCode  ierr;
1611552f7358SJed Brown 
1612552f7358SJed Brown   PetscFunctionBegin;
1613552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1614552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1615552f7358SJed Brown   /* This is only 1-level */
1616552f7358SJed Brown   if (useCone) {
1617552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
1618552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
1619552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
1620552f7358SJed Brown   } else {
1621552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
1622552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
1623552f7358SJed Brown   }
1624bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
1625bfbcdd7aSMatthew G. Knepley     if (*points) {
1626bfbcdd7aSMatthew G. Knepley       closure = *points;
1627bfbcdd7aSMatthew G. Knepley     } else {
1628bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
1629bfbcdd7aSMatthew G. Knepley       ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1630bfbcdd7aSMatthew G. Knepley     }
1631bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
1632bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
1633bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
1634bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
1635bfbcdd7aSMatthew G. Knepley     }
1636bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
1637bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
1638bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
1639bfbcdd7aSMatthew G. Knepley   }
164024c766afSToby Isaac   {
164124c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
164224c766afSToby Isaac 
164324c766afSToby Isaac     c = mesh->maxConeSize;
164424c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
164524c766afSToby Isaac     s = mesh->maxSupportSize;
164624c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
164724c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
164824c766afSToby Isaac   }
1649bfbcdd7aSMatthew G. Knepley   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1650bfbcdd7aSMatthew G. Knepley   if (*points) {
1651bfbcdd7aSMatthew G. Knepley     closure = *points;
1652bfbcdd7aSMatthew G. Knepley   } else {
1653bfbcdd7aSMatthew G. Knepley     ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1654bfbcdd7aSMatthew G. Knepley   }
1655bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
1656552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
1657552f7358SJed Brown     const PetscInt cp = tmp[t];
1658552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
1659552f7358SJed Brown 
1660552f7358SJed Brown     closure[closureSize]   = cp;
1661552f7358SJed Brown     closure[closureSize+1] = co;
1662552f7358SJed Brown     fifo[fifoSize]         = cp;
1663552f7358SJed Brown     fifo[fifoSize+1]       = co;
1664552f7358SJed Brown   }
1665bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
1666552f7358SJed Brown   while (fifoSize - fifoStart) {
1667552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
1668552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
1669552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
1670552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
1671552f7358SJed Brown 
1672552f7358SJed Brown     if (useCone) {
1673552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
1674552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
1675552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
1676552f7358SJed Brown     } else {
1677552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
1678552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
16790298fd71SBarry Smith       tmpO = NULL;
1680552f7358SJed Brown     }
1681552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
1682552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
1683552f7358SJed Brown       const PetscInt cp = tmp[i];
16842e1b13c2SMatthew 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. */
16852e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
16862e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
16872e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
1688552f7358SJed Brown       PetscInt       c;
1689552f7358SJed Brown 
16902e1b13c2SMatthew G. Knepley       if (rev) {
16912e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
16922e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
16932e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
16942e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
16952e1b13c2SMatthew G. Knepley       }
1696552f7358SJed Brown       /* Check for duplicate */
1697552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
1698552f7358SJed Brown         if (closure[c] == cp) break;
1699552f7358SJed Brown       }
1700552f7358SJed Brown       if (c == closureSize) {
1701552f7358SJed Brown         closure[closureSize]   = cp;
1702552f7358SJed Brown         closure[closureSize+1] = co;
1703552f7358SJed Brown         fifo[fifoSize]         = cp;
1704552f7358SJed Brown         fifo[fifoSize+1]       = co;
1705552f7358SJed Brown         closureSize           += 2;
1706552f7358SJed Brown         fifoSize              += 2;
1707552f7358SJed Brown       }
1708552f7358SJed Brown     }
1709552f7358SJed Brown     fifoStart += 2;
1710552f7358SJed Brown   }
1711552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
1712552f7358SJed Brown   if (points)    *points    = closure;
1713552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1714552f7358SJed Brown   PetscFunctionReturn(0);
1715552f7358SJed Brown }
1716552f7358SJed Brown 
1717552f7358SJed Brown #undef __FUNCT__
17189bf0dad6SMatthew G. Knepley #define __FUNCT__ "DMPlexGetTransitiveClosure_Internal"
17199bf0dad6SMatthew G. Knepley /*@C
17209bf0dad6SMatthew 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
17219bf0dad6SMatthew G. Knepley 
17229bf0dad6SMatthew G. Knepley   Not collective
17239bf0dad6SMatthew G. Knepley 
17249bf0dad6SMatthew G. Knepley   Input Parameters:
17259bf0dad6SMatthew G. Knepley + mesh - The DMPlex
17269bf0dad6SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
17279bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
17289bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
17299bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
17309bf0dad6SMatthew G. Knepley 
17319bf0dad6SMatthew G. Knepley   Output Parameters:
17329bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
17339bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
17349bf0dad6SMatthew G. Knepley 
17359bf0dad6SMatthew G. Knepley   Note:
17369bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
17379bf0dad6SMatthew G. Knepley 
17389bf0dad6SMatthew G. Knepley   Fortran Notes:
17399bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
17409bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
17419bf0dad6SMatthew G. Knepley 
17429bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
17439bf0dad6SMatthew G. Knepley 
17449bf0dad6SMatthew G. Knepley   Level: beginner
17459bf0dad6SMatthew G. Knepley 
17469bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
17479bf0dad6SMatthew G. Knepley @*/
17489bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
17499bf0dad6SMatthew G. Knepley {
17509bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
17519bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
17529bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
17539bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
17549bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
17559bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
17569bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
17579bf0dad6SMatthew G. Knepley 
17589bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
17599bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17609bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
17619bf0dad6SMatthew G. Knepley   /* This is only 1-level */
17629bf0dad6SMatthew G. Knepley   if (useCone) {
17639bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
17649bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
17659bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
17669bf0dad6SMatthew G. Knepley   } else {
17679bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
17689bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
17699bf0dad6SMatthew G. Knepley   }
17709bf0dad6SMatthew G. Knepley   if (depth == 1) {
17719bf0dad6SMatthew G. Knepley     if (*points) {
17729bf0dad6SMatthew G. Knepley       closure = *points;
17739bf0dad6SMatthew G. Knepley     } else {
17749bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
17759bf0dad6SMatthew G. Knepley       ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
17769bf0dad6SMatthew G. Knepley     }
17779bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
17789bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
17799bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
17809bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
17819bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
17829bf0dad6SMatthew G. Knepley     }
17839bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
17849bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
17859bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
17869bf0dad6SMatthew G. Knepley   }
178724c766afSToby Isaac   {
178824c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
178924c766afSToby Isaac 
179024c766afSToby Isaac     c = mesh->maxConeSize;
179124c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
179224c766afSToby Isaac     s = mesh->maxSupportSize;
179324c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
179424c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
179524c766afSToby Isaac   }
17969bf0dad6SMatthew G. Knepley   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
17979bf0dad6SMatthew G. Knepley   if (*points) {
17989bf0dad6SMatthew G. Knepley     closure = *points;
17999bf0dad6SMatthew G. Knepley   } else {
18009bf0dad6SMatthew G. Knepley     ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
18019bf0dad6SMatthew G. Knepley   }
18029bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
18039bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
18049bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
18059bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
18069bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
18079bf0dad6SMatthew G. Knepley 
18089bf0dad6SMatthew G. Knepley     if (ornt < 0) {
18099bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
18109bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
181186b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
18129bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
18139bf0dad6SMatthew G. Knepley     }
18149bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
18159bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
18169bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
18179bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
18189bf0dad6SMatthew G. Knepley   }
18199bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
18209bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
18219bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
18229bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
18239bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
18249bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
18259bf0dad6SMatthew G. Knepley 
18269bf0dad6SMatthew G. Knepley     if (useCone) {
18279bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
18289bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
18299bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
18309bf0dad6SMatthew G. Knepley     } else {
18319bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
18329bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
18339bf0dad6SMatthew G. Knepley       tmpO = NULL;
18349bf0dad6SMatthew G. Knepley     }
18359bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
18369bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
18379bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
18389bf0dad6SMatthew 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. */
18399bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
18409bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
18419bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
18429bf0dad6SMatthew G. Knepley       PetscInt       c;
18439bf0dad6SMatthew G. Knepley 
18449bf0dad6SMatthew G. Knepley       if (rev) {
18459bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
18469bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
18479bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
18489bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
18499bf0dad6SMatthew G. Knepley       }
18509bf0dad6SMatthew G. Knepley       /* Check for duplicate */
18519bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
18529bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
18539bf0dad6SMatthew G. Knepley       }
18549bf0dad6SMatthew G. Knepley       if (c == closureSize) {
18559bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
18569bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
18579bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
18589bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
18599bf0dad6SMatthew G. Knepley         closureSize           += 2;
18609bf0dad6SMatthew G. Knepley         fifoSize              += 2;
18619bf0dad6SMatthew G. Knepley       }
18629bf0dad6SMatthew G. Knepley     }
18639bf0dad6SMatthew G. Knepley     fifoStart += 2;
18649bf0dad6SMatthew G. Knepley   }
18659bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
18669bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
18679bf0dad6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
18689bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
18699bf0dad6SMatthew G. Knepley }
18709bf0dad6SMatthew G. Knepley 
18719bf0dad6SMatthew G. Knepley #undef __FUNCT__
1872552f7358SJed Brown #define __FUNCT__ "DMPlexRestoreTransitiveClosure"
1873552f7358SJed Brown /*@C
1874552f7358SJed 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
1875552f7358SJed Brown 
1876552f7358SJed Brown   Not collective
1877552f7358SJed Brown 
1878552f7358SJed Brown   Input Parameters:
1879552f7358SJed Brown + mesh - The DMPlex
1880552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1881552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
1882e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
1883e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
1884552f7358SJed Brown 
1885552f7358SJed Brown   Note:
18860298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
1887552f7358SJed Brown 
18883813dfbdSMatthew G Knepley   Fortran Notes:
18893813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
18903813dfbdSMatthew G Knepley   include petsc.h90 in your code.
18913813dfbdSMatthew G Knepley 
18923813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
18933813dfbdSMatthew G Knepley 
1894552f7358SJed Brown   Level: beginner
1895552f7358SJed Brown 
1896552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1897552f7358SJed Brown @*/
1898552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1899552f7358SJed Brown {
1900552f7358SJed Brown   PetscErrorCode ierr;
1901552f7358SJed Brown 
1902552f7358SJed Brown   PetscFunctionBegin;
1903552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1904e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
1905e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
1906552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, points);CHKERRQ(ierr);
19074ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
1908552f7358SJed Brown   PetscFunctionReturn(0);
1909552f7358SJed Brown }
1910552f7358SJed Brown 
1911552f7358SJed Brown #undef __FUNCT__
1912552f7358SJed Brown #define __FUNCT__ "DMPlexGetMaxSizes"
1913552f7358SJed Brown /*@
1914552f7358SJed Brown   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the Sieve DAG
1915552f7358SJed Brown 
1916552f7358SJed Brown   Not collective
1917552f7358SJed Brown 
1918552f7358SJed Brown   Input Parameter:
1919552f7358SJed Brown . mesh - The DMPlex
1920552f7358SJed Brown 
1921552f7358SJed Brown   Output Parameters:
1922552f7358SJed Brown + maxConeSize - The maximum number of in-edges
1923552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
1924552f7358SJed Brown 
1925552f7358SJed Brown   Level: beginner
1926552f7358SJed Brown 
1927552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1928552f7358SJed Brown @*/
1929552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
1930552f7358SJed Brown {
1931552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
1932552f7358SJed Brown 
1933552f7358SJed Brown   PetscFunctionBegin;
1934552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1935552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
1936552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
1937552f7358SJed Brown   PetscFunctionReturn(0);
1938552f7358SJed Brown }
1939552f7358SJed Brown 
1940552f7358SJed Brown #undef __FUNCT__
1941552f7358SJed Brown #define __FUNCT__ "DMSetUp_Plex"
1942552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
1943552f7358SJed Brown {
1944552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1945552f7358SJed Brown   PetscInt       size;
1946552f7358SJed Brown   PetscErrorCode ierr;
1947552f7358SJed Brown 
1948552f7358SJed Brown   PetscFunctionBegin;
1949552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1950552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
1951552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
19521795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
19531795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
1954552f7358SJed Brown   if (mesh->maxSupportSize) {
1955552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
1956552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
19571795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
1958552f7358SJed Brown   }
1959552f7358SJed Brown   PetscFunctionReturn(0);
1960552f7358SJed Brown }
1961552f7358SJed Brown 
1962552f7358SJed Brown #undef __FUNCT__
1963552f7358SJed Brown #define __FUNCT__ "DMCreateSubDM_Plex"
1964552f7358SJed Brown PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1965552f7358SJed Brown {
1966552f7358SJed Brown   PetscErrorCode ierr;
1967552f7358SJed Brown 
1968552f7358SJed Brown   PetscFunctionBegin;
19694d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
19704d9407bcSMatthew G. Knepley   ierr = DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1971552f7358SJed Brown   PetscFunctionReturn(0);
1972552f7358SJed Brown }
1973552f7358SJed Brown 
1974552f7358SJed Brown #undef __FUNCT__
1975552f7358SJed Brown #define __FUNCT__ "DMPlexSymmetrize"
1976552f7358SJed Brown /*@
1977552f7358SJed Brown   DMPlexSymmetrize - Creates support (out-edge) information from cone (in-edge) inoformation
1978552f7358SJed Brown 
1979552f7358SJed Brown   Not collective
1980552f7358SJed Brown 
1981552f7358SJed Brown   Input Parameter:
1982552f7358SJed Brown . mesh - The DMPlex
1983552f7358SJed Brown 
1984552f7358SJed Brown   Output Parameter:
1985552f7358SJed Brown 
1986552f7358SJed Brown   Note:
1987552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
1988552f7358SJed Brown 
1989552f7358SJed Brown   Level: beginner
1990552f7358SJed Brown 
1991552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
1992552f7358SJed Brown @*/
1993552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
1994552f7358SJed Brown {
1995552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1996552f7358SJed Brown   PetscInt      *offsets;
1997552f7358SJed Brown   PetscInt       supportSize;
1998552f7358SJed Brown   PetscInt       pStart, pEnd, p;
1999552f7358SJed Brown   PetscErrorCode ierr;
2000552f7358SJed Brown 
2001552f7358SJed Brown   PetscFunctionBegin;
2002552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
200382f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
2004552f7358SJed Brown   /* Calculate support sizes */
2005552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2006552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2007552f7358SJed Brown     PetscInt dof, off, c;
2008552f7358SJed Brown 
2009552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2010552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2011552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2012552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2013552f7358SJed Brown     }
2014552f7358SJed Brown   }
2015552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2016552f7358SJed Brown     PetscInt dof;
2017552f7358SJed Brown 
2018552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
20190d644c17SKarl Rupp 
2020552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2021552f7358SJed Brown   }
2022552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2023552f7358SJed Brown   /* Calculate supports */
2024552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
20251795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
20261795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2027552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2028552f7358SJed Brown     PetscInt dof, off, c;
2029552f7358SJed Brown 
2030552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2031552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2032552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2033552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2034552f7358SJed Brown       PetscInt       offS;
2035552f7358SJed Brown 
2036552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
20370d644c17SKarl Rupp 
2038552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2039552f7358SJed Brown       ++offsets[q];
2040552f7358SJed Brown     }
2041552f7358SJed Brown   }
2042552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
2043552f7358SJed Brown   PetscFunctionReturn(0);
2044552f7358SJed Brown }
2045552f7358SJed Brown 
2046552f7358SJed Brown #undef __FUNCT__
2047552f7358SJed Brown #define __FUNCT__ "DMPlexStratify"
2048552f7358SJed Brown /*@
2049552f7358SJed Brown   DMPlexStratify - The Sieve DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
2050552f7358SJed Brown   can be illustrated by Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2051552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2052552f7358SJed Brown   the DAG.
2053552f7358SJed Brown 
2054bf4602e4SToby Isaac   Collective on dm
2055552f7358SJed Brown 
2056552f7358SJed Brown   Input Parameter:
2057552f7358SJed Brown . mesh - The DMPlex
2058552f7358SJed Brown 
2059552f7358SJed Brown   Output Parameter:
2060552f7358SJed Brown 
2061552f7358SJed Brown   Notes:
2062150b719bSJed Brown   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
2063150b719bSJed Brown   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2064c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2065150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2066552f7358SJed Brown 
2067150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2068552f7358SJed Brown 
2069552f7358SJed Brown   Level: beginner
2070552f7358SJed Brown 
2071552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
2072552f7358SJed Brown @*/
2073552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2074552f7358SJed Brown {
2075df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2076aa50250dSMatthew G. Knepley   DMLabel        label;
2077552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2078552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
2079552f7358SJed Brown   PetscErrorCode ierr;
2080552f7358SJed Brown 
2081552f7358SJed Brown   PetscFunctionBegin;
2082552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2083552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2084552f7358SJed Brown   /* Calculate depth */
2085aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2086c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2087aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2088552f7358SJed Brown   /* Initialize roots and count leaves */
2089552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2090552f7358SJed Brown     PetscInt coneSize, supportSize;
2091552f7358SJed Brown 
2092552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2093552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2094552f7358SJed Brown     if (!coneSize && supportSize) {
2095552f7358SJed Brown       ++numRoots;
2096aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2097552f7358SJed Brown     } else if (!supportSize && coneSize) {
2098552f7358SJed Brown       ++numLeaves;
2099552f7358SJed Brown     } else if (!supportSize && !coneSize) {
2100552f7358SJed Brown       /* Isolated points */
2101aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2102552f7358SJed Brown     }
2103552f7358SJed Brown   }
2104552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2105552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
2106552f7358SJed Brown       PetscInt coneSize, supportSize;
2107552f7358SJed Brown 
2108552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2109552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2110552f7358SJed Brown       if (!supportSize && coneSize) {
2111aa50250dSMatthew G. Knepley         ierr = DMLabelSetValue(label, p, 1);CHKERRQ(ierr);
2112552f7358SJed Brown       }
2113552f7358SJed Brown     }
2114552f7358SJed Brown   } else {
211574ef644bSMatthew G. Knepley     IS       pointIS;
211674ef644bSMatthew G. Knepley     PetscInt numPoints = 0, level = 0;
2117552f7358SJed Brown 
211874ef644bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
211974ef644bSMatthew G. Knepley     if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
212074ef644bSMatthew G. Knepley     while (numPoints) {
212174ef644bSMatthew G. Knepley       const PetscInt *points;
212274ef644bSMatthew G. Knepley       const PetscInt  newLevel = level+1;
212374ef644bSMatthew G. Knepley 
212474ef644bSMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
212574ef644bSMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
212674ef644bSMatthew G. Knepley         const PetscInt  point = points[p];
212774ef644bSMatthew G. Knepley         const PetscInt *support;
212874ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
212974ef644bSMatthew G. Knepley 
213074ef644bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
213174ef644bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
213274ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
213374ef644bSMatthew G. Knepley           ierr = DMLabelSetValue(label, support[s], newLevel);CHKERRQ(ierr);
2134552f7358SJed Brown         }
2135552f7358SJed Brown       }
21365edfc765SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
213774ef644bSMatthew G. Knepley       ++level;
213874ef644bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
213974ef644bSMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
214074ef644bSMatthew G. Knepley       if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
214174ef644bSMatthew G. Knepley       else         {numPoints = 0;}
214274ef644bSMatthew G. Knepley     }
214374ef644bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
214474ef644bSMatthew G. Knepley   }
2145bf4602e4SToby Isaac   { /* just in case there is an empty process */
2146bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2147bf4602e4SToby Isaac 
2148bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label,&numValues);CHKERRQ(ierr);
21494de306b1SToby Isaac     for (v = 0; v < numValues; v++) {
21504de306b1SToby Isaac       IS pointIS;
21514de306b1SToby Isaac 
21524de306b1SToby Isaac       ierr = DMLabelGetStratumIS(label, v, &pointIS);CHKERRQ(ierr);
21534de306b1SToby Isaac       if (pointIS) {
21544de306b1SToby Isaac         PetscInt  min, max, numPoints;
21554de306b1SToby Isaac         PetscInt  start;
21564de306b1SToby Isaac         PetscBool contig;
21574de306b1SToby Isaac 
21584de306b1SToby Isaac         ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
21594de306b1SToby Isaac         ierr = ISGetMinMax(pointIS, &min, &max);CHKERRQ(ierr);
21604de306b1SToby Isaac         ierr = ISContiguousLocal(pointIS,min,max+1,&start,&contig);CHKERRQ(ierr);
21614de306b1SToby Isaac         if (start == 0 && contig) {
21624de306b1SToby Isaac           ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
21634de306b1SToby Isaac           ierr = ISCreateStride(PETSC_COMM_SELF,numPoints,min,1,&pointIS);CHKERRQ(ierr);
21644de306b1SToby Isaac           ierr = DMLabelSetStratumIS(label, v, pointIS);CHKERRQ(ierr);
21654de306b1SToby Isaac         }
21664de306b1SToby Isaac       }
21674de306b1SToby Isaac       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
21684de306b1SToby Isaac     }
21696d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2170bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2171bf4602e4SToby Isaac       DMLabelAddStratum(label,v);CHKERRQ(ierr);
2172bf4602e4SToby Isaac     }
2173bf4602e4SToby Isaac   }
2174bf4602e4SToby Isaac 
2175df0420ecSMatthew G. Knepley   ierr = DMLabelGetState(label, &mesh->depthState);CHKERRQ(ierr);
2176552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2177552f7358SJed Brown   PetscFunctionReturn(0);
2178552f7358SJed Brown }
2179552f7358SJed Brown 
2180552f7358SJed Brown #undef __FUNCT__
2181552f7358SJed Brown #define __FUNCT__ "DMPlexGetJoin"
2182552f7358SJed Brown /*@C
2183552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
2184552f7358SJed Brown 
2185552f7358SJed Brown   Not Collective
2186552f7358SJed Brown 
2187552f7358SJed Brown   Input Parameters:
2188552f7358SJed Brown + dm - The DMPlex object
2189552f7358SJed Brown . numPoints - The number of input points for the join
2190552f7358SJed Brown - points - The input points
2191552f7358SJed Brown 
2192552f7358SJed Brown   Output Parameters:
2193552f7358SJed Brown + numCoveredPoints - The number of points in the join
2194552f7358SJed Brown - coveredPoints - The points in the join
2195552f7358SJed Brown 
2196552f7358SJed Brown   Level: intermediate
2197552f7358SJed Brown 
2198552f7358SJed Brown   Note: Currently, this is restricted to a single level join
2199552f7358SJed Brown 
22003813dfbdSMatthew G Knepley   Fortran Notes:
22013813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22023813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22033813dfbdSMatthew G Knepley 
22043813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
22053813dfbdSMatthew G Knepley 
2206552f7358SJed Brown .keywords: mesh
2207552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2208552f7358SJed Brown @*/
2209552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2210552f7358SJed Brown {
2211552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2212552f7358SJed Brown   PetscInt      *join[2];
2213552f7358SJed Brown   PetscInt       joinSize, i = 0;
2214552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2215552f7358SJed Brown   PetscErrorCode ierr;
2216552f7358SJed Brown 
2217552f7358SJed Brown   PetscFunctionBegin;
2218552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2219552f7358SJed Brown   PetscValidPointer(points, 2);
2220552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2221552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2222552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
2223552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
2224552f7358SJed Brown   /* Copy in support of first point */
2225552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
2226552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
2227552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
2228552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
2229552f7358SJed Brown   }
2230552f7358SJed Brown   /* Check each successive support */
2231552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2232552f7358SJed Brown     PetscInt newJoinSize = 0;
2233552f7358SJed Brown 
2234552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
2235552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
2236552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2237552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
2238552f7358SJed Brown 
2239552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
2240552f7358SJed Brown         if (point == join[i][m]) {
2241552f7358SJed Brown           join[1-i][newJoinSize++] = point;
2242552f7358SJed Brown           break;
2243552f7358SJed Brown         }
2244552f7358SJed Brown       }
2245552f7358SJed Brown     }
2246552f7358SJed Brown     joinSize = newJoinSize;
2247552f7358SJed Brown     i        = 1-i;
2248552f7358SJed Brown   }
2249552f7358SJed Brown   *numCoveredPoints = joinSize;
2250552f7358SJed Brown   *coveredPoints    = join[i];
2251552f7358SJed Brown   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
2252552f7358SJed Brown   PetscFunctionReturn(0);
2253552f7358SJed Brown }
2254552f7358SJed Brown 
2255552f7358SJed Brown #undef __FUNCT__
2256552f7358SJed Brown #define __FUNCT__ "DMPlexRestoreJoin"
2257552f7358SJed Brown /*@C
2258552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
2259552f7358SJed Brown 
2260552f7358SJed Brown   Not Collective
2261552f7358SJed Brown 
2262552f7358SJed Brown   Input Parameters:
2263552f7358SJed Brown + dm - The DMPlex object
2264552f7358SJed Brown . numPoints - The number of input points for the join
2265552f7358SJed Brown - points - The input points
2266552f7358SJed Brown 
2267552f7358SJed Brown   Output Parameters:
2268552f7358SJed Brown + numCoveredPoints - The number of points in the join
2269552f7358SJed Brown - coveredPoints - The points in the join
2270552f7358SJed Brown 
22713813dfbdSMatthew G Knepley   Fortran Notes:
22723813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22733813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22743813dfbdSMatthew G Knepley 
22753813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
22763813dfbdSMatthew G Knepley 
2277552f7358SJed Brown   Level: intermediate
2278552f7358SJed Brown 
2279552f7358SJed Brown .keywords: mesh
2280552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
2281552f7358SJed Brown @*/
2282552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2283552f7358SJed Brown {
2284552f7358SJed Brown   PetscErrorCode ierr;
2285552f7358SJed Brown 
2286552f7358SJed Brown   PetscFunctionBegin;
2287552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2288d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2289d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2290d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
2291552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
2292d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2293552f7358SJed Brown   PetscFunctionReturn(0);
2294552f7358SJed Brown }
2295552f7358SJed Brown 
2296552f7358SJed Brown #undef __FUNCT__
2297552f7358SJed Brown #define __FUNCT__ "DMPlexGetFullJoin"
2298552f7358SJed Brown /*@C
2299552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
2300552f7358SJed Brown 
2301552f7358SJed Brown   Not Collective
2302552f7358SJed Brown 
2303552f7358SJed Brown   Input Parameters:
2304552f7358SJed Brown + dm - The DMPlex object
2305552f7358SJed Brown . numPoints - The number of input points for the join
2306552f7358SJed Brown - points - The input points
2307552f7358SJed Brown 
2308552f7358SJed Brown   Output Parameters:
2309552f7358SJed Brown + numCoveredPoints - The number of points in the join
2310552f7358SJed Brown - coveredPoints - The points in the join
2311552f7358SJed Brown 
23123813dfbdSMatthew G Knepley   Fortran Notes:
23133813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
23143813dfbdSMatthew G Knepley   include petsc.h90 in your code.
23153813dfbdSMatthew G Knepley 
23163813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
23173813dfbdSMatthew G Knepley 
2318552f7358SJed Brown   Level: intermediate
2319552f7358SJed Brown 
2320552f7358SJed Brown .keywords: mesh
2321552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
2322552f7358SJed Brown @*/
2323552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2324552f7358SJed Brown {
2325552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2326552f7358SJed Brown   PetscInt      *offsets, **closures;
2327552f7358SJed Brown   PetscInt      *join[2];
2328552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
232924c766afSToby Isaac   PetscInt       p, d, c, m, ms;
2330552f7358SJed Brown   PetscErrorCode ierr;
2331552f7358SJed Brown 
2332552f7358SJed Brown   PetscFunctionBegin;
2333552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2334552f7358SJed Brown   PetscValidPointer(points, 2);
2335552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2336552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2337552f7358SJed Brown 
2338552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
23391795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
2340552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
234124c766afSToby Isaac   ms      = mesh->maxSupportSize;
234224c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
2343552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
2344552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
2345552f7358SJed Brown 
2346552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2347552f7358SJed Brown     PetscInt closureSize;
2348552f7358SJed Brown 
2349552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
23500d644c17SKarl Rupp 
2351552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
2352552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
2353552f7358SJed Brown       PetscInt pStart, pEnd, i;
2354552f7358SJed Brown 
2355552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
2356552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
2357552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2358552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
2359552f7358SJed Brown           break;
2360552f7358SJed Brown         }
2361552f7358SJed Brown       }
2362552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
2363552f7358SJed Brown     }
236482f516ccSBarry 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);
2365552f7358SJed Brown   }
2366552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
2367552f7358SJed Brown     PetscInt dof;
2368552f7358SJed Brown 
2369552f7358SJed Brown     /* Copy in support of first point */
2370552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
2371552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
2372552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
2373552f7358SJed Brown     }
2374552f7358SJed Brown     /* Check each successive cone */
2375552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
2376552f7358SJed Brown       PetscInt newJoinSize = 0;
2377552f7358SJed Brown 
2378552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
2379552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2380552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
2381552f7358SJed Brown 
2382552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
2383552f7358SJed Brown           if (point == join[i][m]) {
2384552f7358SJed Brown             join[1-i][newJoinSize++] = point;
2385552f7358SJed Brown             break;
2386552f7358SJed Brown           }
2387552f7358SJed Brown         }
2388552f7358SJed Brown       }
2389552f7358SJed Brown       joinSize = newJoinSize;
2390552f7358SJed Brown       i        = 1-i;
2391552f7358SJed Brown     }
2392552f7358SJed Brown     if (joinSize) break;
2393552f7358SJed Brown   }
2394552f7358SJed Brown   *numCoveredPoints = joinSize;
2395552f7358SJed Brown   *coveredPoints    = join[i];
2396552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
23970298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
2398552f7358SJed Brown   }
2399552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
2400552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
2401552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
2402552f7358SJed Brown   PetscFunctionReturn(0);
2403552f7358SJed Brown }
2404552f7358SJed Brown 
2405552f7358SJed Brown #undef __FUNCT__
2406552f7358SJed Brown #define __FUNCT__ "DMPlexGetMeet"
2407552f7358SJed Brown /*@C
2408552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
2409552f7358SJed Brown 
2410552f7358SJed Brown   Not Collective
2411552f7358SJed Brown 
2412552f7358SJed Brown   Input Parameters:
2413552f7358SJed Brown + dm - The DMPlex object
2414552f7358SJed Brown . numPoints - The number of input points for the meet
2415552f7358SJed Brown - points - The input points
2416552f7358SJed Brown 
2417552f7358SJed Brown   Output Parameters:
2418552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2419552f7358SJed Brown - coveredPoints - The points in the meet
2420552f7358SJed Brown 
2421552f7358SJed Brown   Level: intermediate
2422552f7358SJed Brown 
2423552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
2424552f7358SJed Brown 
24253813dfbdSMatthew G Knepley   Fortran Notes:
24263813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24273813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24283813dfbdSMatthew G Knepley 
24293813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24303813dfbdSMatthew G Knepley 
2431552f7358SJed Brown .keywords: mesh
2432552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
2433552f7358SJed Brown @*/
2434552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
2435552f7358SJed Brown {
2436552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2437552f7358SJed Brown   PetscInt      *meet[2];
2438552f7358SJed Brown   PetscInt       meetSize, i = 0;
2439552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2440552f7358SJed Brown   PetscErrorCode ierr;
2441552f7358SJed Brown 
2442552f7358SJed Brown   PetscFunctionBegin;
2443552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2444552f7358SJed Brown   PetscValidPointer(points, 2);
2445552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
2446552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
2447552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
2448552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
2449552f7358SJed Brown   /* Copy in cone of first point */
2450552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
2451552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
2452552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
2453552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
2454552f7358SJed Brown   }
2455552f7358SJed Brown   /* Check each successive cone */
2456552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2457552f7358SJed Brown     PetscInt newMeetSize = 0;
2458552f7358SJed Brown 
2459552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
2460552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
2461552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2462552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
2463552f7358SJed Brown 
2464552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
2465552f7358SJed Brown         if (point == meet[i][m]) {
2466552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
2467552f7358SJed Brown           break;
2468552f7358SJed Brown         }
2469552f7358SJed Brown       }
2470552f7358SJed Brown     }
2471552f7358SJed Brown     meetSize = newMeetSize;
2472552f7358SJed Brown     i        = 1-i;
2473552f7358SJed Brown   }
2474552f7358SJed Brown   *numCoveringPoints = meetSize;
2475552f7358SJed Brown   *coveringPoints    = meet[i];
2476552f7358SJed Brown   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
2477552f7358SJed Brown   PetscFunctionReturn(0);
2478552f7358SJed Brown }
2479552f7358SJed Brown 
2480552f7358SJed Brown #undef __FUNCT__
2481552f7358SJed Brown #define __FUNCT__ "DMPlexRestoreMeet"
2482552f7358SJed Brown /*@C
2483552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
2484552f7358SJed Brown 
2485552f7358SJed Brown   Not Collective
2486552f7358SJed Brown 
2487552f7358SJed Brown   Input Parameters:
2488552f7358SJed Brown + dm - The DMPlex object
2489552f7358SJed Brown . numPoints - The number of input points for the meet
2490552f7358SJed Brown - points - The input points
2491552f7358SJed Brown 
2492552f7358SJed Brown   Output Parameters:
2493552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2494552f7358SJed Brown - coveredPoints - The points in the meet
2495552f7358SJed Brown 
2496552f7358SJed Brown   Level: intermediate
2497552f7358SJed Brown 
24983813dfbdSMatthew G Knepley   Fortran Notes:
24993813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25003813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25013813dfbdSMatthew G Knepley 
25023813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25033813dfbdSMatthew G Knepley 
2504552f7358SJed Brown .keywords: mesh
2505552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
2506552f7358SJed Brown @*/
2507552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2508552f7358SJed Brown {
2509552f7358SJed Brown   PetscErrorCode ierr;
2510552f7358SJed Brown 
2511552f7358SJed Brown   PetscFunctionBegin;
2512552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2513d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2514d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2515d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
2516552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
2517d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2518552f7358SJed Brown   PetscFunctionReturn(0);
2519552f7358SJed Brown }
2520552f7358SJed Brown 
2521552f7358SJed Brown #undef __FUNCT__
2522552f7358SJed Brown #define __FUNCT__ "DMPlexGetFullMeet"
2523552f7358SJed Brown /*@C
2524552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
2525552f7358SJed Brown 
2526552f7358SJed Brown   Not Collective
2527552f7358SJed Brown 
2528552f7358SJed Brown   Input Parameters:
2529552f7358SJed Brown + dm - The DMPlex object
2530552f7358SJed Brown . numPoints - The number of input points for the meet
2531552f7358SJed Brown - points - The input points
2532552f7358SJed Brown 
2533552f7358SJed Brown   Output Parameters:
2534552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2535552f7358SJed Brown - coveredPoints - The points in the meet
2536552f7358SJed Brown 
2537552f7358SJed Brown   Level: intermediate
2538552f7358SJed Brown 
25393813dfbdSMatthew G Knepley   Fortran Notes:
25403813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25413813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25423813dfbdSMatthew G Knepley 
25433813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25443813dfbdSMatthew G Knepley 
2545552f7358SJed Brown .keywords: mesh
2546552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
2547552f7358SJed Brown @*/
2548552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2549552f7358SJed Brown {
2550552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2551552f7358SJed Brown   PetscInt      *offsets, **closures;
2552552f7358SJed Brown   PetscInt      *meet[2];
2553552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
255424c766afSToby Isaac   PetscInt       p, h, c, m, mc;
2555552f7358SJed Brown   PetscErrorCode ierr;
2556552f7358SJed Brown 
2557552f7358SJed Brown   PetscFunctionBegin;
2558552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2559552f7358SJed Brown   PetscValidPointer(points, 2);
2560552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2561552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2562552f7358SJed Brown 
2563552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
2564785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
2565552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
256624c766afSToby Isaac   mc      = mesh->maxConeSize;
256724c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
2568552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
2569552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
2570552f7358SJed Brown 
2571552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2572552f7358SJed Brown     PetscInt closureSize;
2573552f7358SJed Brown 
2574552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
25750d644c17SKarl Rupp 
2576552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
2577552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
2578552f7358SJed Brown       PetscInt pStart, pEnd, i;
2579552f7358SJed Brown 
2580552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
2581552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
2582552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2583552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
2584552f7358SJed Brown           break;
2585552f7358SJed Brown         }
2586552f7358SJed Brown       }
2587552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
2588552f7358SJed Brown     }
258982f516ccSBarry 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);
2590552f7358SJed Brown   }
2591552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
2592552f7358SJed Brown     PetscInt dof;
2593552f7358SJed Brown 
2594552f7358SJed Brown     /* Copy in cone of first point */
2595552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
2596552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
2597552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
2598552f7358SJed Brown     }
2599552f7358SJed Brown     /* Check each successive cone */
2600552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
2601552f7358SJed Brown       PetscInt newMeetSize = 0;
2602552f7358SJed Brown 
2603552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
2604552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2605552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
2606552f7358SJed Brown 
2607552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
2608552f7358SJed Brown           if (point == meet[i][m]) {
2609552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
2610552f7358SJed Brown             break;
2611552f7358SJed Brown           }
2612552f7358SJed Brown         }
2613552f7358SJed Brown       }
2614552f7358SJed Brown       meetSize = newMeetSize;
2615552f7358SJed Brown       i        = 1-i;
2616552f7358SJed Brown     }
2617552f7358SJed Brown     if (meetSize) break;
2618552f7358SJed Brown   }
2619552f7358SJed Brown   *numCoveredPoints = meetSize;
2620552f7358SJed Brown   *coveredPoints    = meet[i];
2621552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
26220298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
2623552f7358SJed Brown   }
2624552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
2625552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
2626552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
2627552f7358SJed Brown   PetscFunctionReturn(0);
2628552f7358SJed Brown }
2629552f7358SJed Brown 
2630552f7358SJed Brown #undef __FUNCT__
26314e3744c5SMatthew G. Knepley #define __FUNCT__ "DMPlexEqual"
26324e3744c5SMatthew G. Knepley /*@C
26334e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
26344e3744c5SMatthew G. Knepley 
26354e3744c5SMatthew G. Knepley   Not Collective
26364e3744c5SMatthew G. Knepley 
26374e3744c5SMatthew G. Knepley   Input Parameters:
26384e3744c5SMatthew G. Knepley + dmA - A DMPlex object
26394e3744c5SMatthew G. Knepley - dmB - A DMPlex object
26404e3744c5SMatthew G. Knepley 
26414e3744c5SMatthew G. Knepley   Output Parameters:
26424e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
26434e3744c5SMatthew G. Knepley 
26444e3744c5SMatthew G. Knepley   Level: intermediate
26454e3744c5SMatthew G. Knepley 
26464e3744c5SMatthew G. Knepley   Notes:
26474e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
26484e3744c5SMatthew G. Knepley 
26494e3744c5SMatthew G. Knepley .keywords: mesh
26504e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
26514e3744c5SMatthew G. Knepley @*/
26524e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
26534e3744c5SMatthew G. Knepley {
26544e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
26554e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
26564e3744c5SMatthew G. Knepley 
26574e3744c5SMatthew G. Knepley   PetscFunctionBegin;
26584e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
26594e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
26604e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
26614e3744c5SMatthew G. Knepley 
26624e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
26634e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
26644e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
26654e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
26664e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
26674e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
26684e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
26694e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
26704e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
26714e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
26724e3744c5SMatthew G. Knepley 
26734e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
26744e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
26754e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
26764e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
26774e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
26784e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
26794e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
26804e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
26814e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
26824e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
26834e3744c5SMatthew G. Knepley     }
26844e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
26854e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
26864e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
26874e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
26884e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
26894e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
26904e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
26914e3744c5SMatthew G. Knepley     }
26924e3744c5SMatthew G. Knepley   }
26934e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
26944e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
26954e3744c5SMatthew G. Knepley }
26964e3744c5SMatthew G. Knepley 
26974e3744c5SMatthew G. Knepley #undef __FUNCT__
269818ad9376SMatthew G. Knepley #define __FUNCT__ "DMPlexGetNumFaceVertices"
269918ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
2700a6dfd86eSKarl Rupp {
270182f516ccSBarry Smith   MPI_Comm       comm;
2702552f7358SJed Brown   PetscErrorCode ierr;
2703552f7358SJed Brown 
2704552f7358SJed Brown   PetscFunctionBegin;
270582f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2706552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
2707552f7358SJed Brown   switch (cellDim) {
2708552f7358SJed Brown   case 0:
2709552f7358SJed Brown     *numFaceVertices = 0;
2710552f7358SJed Brown     break;
2711552f7358SJed Brown   case 1:
2712552f7358SJed Brown     *numFaceVertices = 1;
2713552f7358SJed Brown     break;
2714552f7358SJed Brown   case 2:
2715552f7358SJed Brown     switch (numCorners) {
271619436ca2SJed Brown     case 3: /* triangle */
271719436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2718552f7358SJed Brown       break;
271919436ca2SJed Brown     case 4: /* quadrilateral */
272019436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2721552f7358SJed Brown       break;
272219436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
272319436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2724552f7358SJed Brown       break;
272519436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
272619436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2727552f7358SJed Brown       break;
2728552f7358SJed Brown     default:
2729f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2730552f7358SJed Brown     }
2731552f7358SJed Brown     break;
2732552f7358SJed Brown   case 3:
2733552f7358SJed Brown     switch (numCorners) {
273419436ca2SJed Brown     case 4: /* tetradehdron */
273519436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
2736552f7358SJed Brown       break;
273719436ca2SJed Brown     case 6: /* tet cohesive cells */
273819436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2739552f7358SJed Brown       break;
274019436ca2SJed Brown     case 8: /* hexahedron */
274119436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2742552f7358SJed Brown       break;
274319436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
274419436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2745552f7358SJed Brown       break;
274619436ca2SJed Brown     case 10: /* quadratic tetrahedron */
274719436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2748552f7358SJed Brown       break;
274919436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
275019436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2751552f7358SJed Brown       break;
275219436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
275319436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2754552f7358SJed Brown       break;
275519436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
275619436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
2757552f7358SJed Brown       break;
2758552f7358SJed Brown     default:
2759f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2760552f7358SJed Brown     }
2761552f7358SJed Brown     break;
2762552f7358SJed Brown   default:
2763f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
2764552f7358SJed Brown   }
2765552f7358SJed Brown   PetscFunctionReturn(0);
2766552f7358SJed Brown }
2767552f7358SJed Brown 
2768552f7358SJed Brown #undef __FUNCT__
2769aa50250dSMatthew G. Knepley #define __FUNCT__ "DMPlexGetDepthLabel"
2770552f7358SJed Brown /*@
2771aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
2772552f7358SJed Brown 
2773552f7358SJed Brown   Not Collective
2774552f7358SJed Brown 
2775aa50250dSMatthew G. Knepley   Input Parameter:
2776552f7358SJed Brown . dm    - The DMPlex object
2777552f7358SJed Brown 
2778aa50250dSMatthew G. Knepley   Output Parameter:
2779aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
2780552f7358SJed Brown 
2781552f7358SJed Brown   Level: developer
2782552f7358SJed Brown 
2783aa50250dSMatthew G. Knepley .keywords: mesh, points
2784aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2785aa50250dSMatthew G. Knepley @*/
2786aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
2787aa50250dSMatthew G. Knepley {
2788aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
2789aa50250dSMatthew G. Knepley 
2790aa50250dSMatthew G. Knepley   PetscFunctionBegin;
2791aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2792aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
2793c58f1c22SToby Isaac   if (!dm->depthLabel) {ierr = DMGetLabel(dm, "depth", &dm->depthLabel);CHKERRQ(ierr);}
2794c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
2795aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
2796aa50250dSMatthew G. Knepley }
2797aa50250dSMatthew G. Knepley 
2798aa50250dSMatthew G. Knepley #undef __FUNCT__
2799aa50250dSMatthew G. Knepley #define __FUNCT__ "DMPlexGetDepth"
2800aa50250dSMatthew G. Knepley /*@
2801aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
2802aa50250dSMatthew G. Knepley 
2803aa50250dSMatthew G. Knepley   Not Collective
2804aa50250dSMatthew G. Knepley 
2805aa50250dSMatthew G. Knepley   Input Parameter:
2806aa50250dSMatthew G. Knepley . dm    - The DMPlex object
2807aa50250dSMatthew G. Knepley 
2808aa50250dSMatthew G. Knepley   Output Parameter:
2809aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
2810aa50250dSMatthew G. Knepley 
2811aa50250dSMatthew G. Knepley   Level: developer
2812552f7358SJed Brown 
2813552f7358SJed Brown .keywords: mesh, points
2814aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2815552f7358SJed Brown @*/
2816552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
2817552f7358SJed Brown {
2818aa50250dSMatthew G. Knepley   DMLabel        label;
2819aa50250dSMatthew G. Knepley   PetscInt       d = 0;
2820552f7358SJed Brown   PetscErrorCode ierr;
2821552f7358SJed Brown 
2822552f7358SJed Brown   PetscFunctionBegin;
2823552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2824552f7358SJed Brown   PetscValidPointer(depth, 2);
2825aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2826aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
2827552f7358SJed Brown   *depth = d-1;
2828552f7358SJed Brown   PetscFunctionReturn(0);
2829552f7358SJed Brown }
2830552f7358SJed Brown 
2831552f7358SJed Brown #undef __FUNCT__
2832552f7358SJed Brown #define __FUNCT__ "DMPlexGetDepthStratum"
2833552f7358SJed Brown /*@
2834552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
2835552f7358SJed Brown 
2836552f7358SJed Brown   Not Collective
2837552f7358SJed Brown 
2838552f7358SJed Brown   Input Parameters:
2839552f7358SJed Brown + dm           - The DMPlex object
2840552f7358SJed Brown - stratumValue - The requested depth
2841552f7358SJed Brown 
2842552f7358SJed Brown   Output Parameters:
2843552f7358SJed Brown + start - The first point at this depth
2844552f7358SJed Brown - end   - One beyond the last point at this depth
2845552f7358SJed Brown 
2846552f7358SJed Brown   Level: developer
2847552f7358SJed Brown 
2848552f7358SJed Brown .keywords: mesh, points
2849552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
2850552f7358SJed Brown @*/
28510adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
28520adebc6cSBarry Smith {
2853aa50250dSMatthew G. Knepley   DMLabel        label;
285463d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
2855552f7358SJed Brown   PetscErrorCode ierr;
2856552f7358SJed Brown 
2857552f7358SJed Brown   PetscFunctionBegin;
2858552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
285963d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
286063d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
2861552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
28620d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
286363d1a920SMatthew G. Knepley   if (stratumValue < 0) {
286463d1a920SMatthew G. Knepley     if (start) *start = pStart;
286563d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
286663d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
2867552f7358SJed Brown   }
2868aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
286963d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
287063d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
2871552f7358SJed Brown   PetscFunctionReturn(0);
2872552f7358SJed Brown }
2873552f7358SJed Brown 
2874552f7358SJed Brown #undef __FUNCT__
2875552f7358SJed Brown #define __FUNCT__ "DMPlexGetHeightStratum"
2876552f7358SJed Brown /*@
2877552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
2878552f7358SJed Brown 
2879552f7358SJed Brown   Not Collective
2880552f7358SJed Brown 
2881552f7358SJed Brown   Input Parameters:
2882552f7358SJed Brown + dm           - The DMPlex object
2883552f7358SJed Brown - stratumValue - The requested height
2884552f7358SJed Brown 
2885552f7358SJed Brown   Output Parameters:
2886552f7358SJed Brown + start - The first point at this height
2887552f7358SJed Brown - end   - One beyond the last point at this height
2888552f7358SJed Brown 
2889552f7358SJed Brown   Level: developer
2890552f7358SJed Brown 
2891552f7358SJed Brown .keywords: mesh, points
2892552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
2893552f7358SJed Brown @*/
28940adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
28950adebc6cSBarry Smith {
2896aa50250dSMatthew G. Knepley   DMLabel        label;
289763d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
2898552f7358SJed Brown   PetscErrorCode ierr;
2899552f7358SJed Brown 
2900552f7358SJed Brown   PetscFunctionBegin;
2901552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
290263d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
290363d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
2904552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
29050d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
290663d1a920SMatthew G. Knepley   if (stratumValue < 0) {
290763d1a920SMatthew G. Knepley     if (start) *start = pStart;
290863d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
290963d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
2910552f7358SJed Brown   }
2911aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2912aa50250dSMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");CHKERRQ(ierr);
291363d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
291463d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
2915552f7358SJed Brown   PetscFunctionReturn(0);
2916552f7358SJed Brown }
2917552f7358SJed Brown 
2918552f7358SJed Brown #undef __FUNCT__
2919552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSectionInitial"
2920552f7358SJed Brown /* Set the number of dof on each point and separate by fields */
2921a6dfd86eSKarl Rupp PetscErrorCode DMPlexCreateSectionInitial(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscSection *section)
2922a6dfd86eSKarl Rupp {
2923ee55978aSMatthew G. Knepley   PetscInt      *pMax;
2924916f0f19SMatthew G. Knepley   PetscInt       depth, pStart = 0, pEnd = 0;
2925ee55978aSMatthew G. Knepley   PetscInt       Nf, p, d, dep, f;
2926fa716be8SMatthew G. Knepley   PetscBool     *isFE;
2927552f7358SJed Brown   PetscErrorCode ierr;
2928552f7358SJed Brown 
2929552f7358SJed Brown   PetscFunctionBegin;
2930fa716be8SMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
2931ee55978aSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
2932fa716be8SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
2933fa716be8SMatthew G. Knepley     PetscObject  obj;
2934fa716be8SMatthew G. Knepley     PetscClassId id;
2935fa716be8SMatthew G. Knepley 
2936ee55978aSMatthew G. Knepley     isFE[f] = PETSC_FALSE;
2937ee55978aSMatthew G. Knepley     if (f >= Nf) continue;
2938fa716be8SMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
2939fa716be8SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2940fa716be8SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
2941fa716be8SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
2942552f7358SJed Brown   }
294382f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), section);CHKERRQ(ierr);
2944552f7358SJed Brown   if (numFields > 0) {
2945552f7358SJed Brown     ierr = PetscSectionSetNumFields(*section, numFields);CHKERRQ(ierr);
2946552f7358SJed Brown     if (numComp) {
2947552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
2948552f7358SJed Brown         ierr = PetscSectionSetFieldComponents(*section, f, numComp[f]);CHKERRQ(ierr);
2949552f7358SJed Brown       }
2950552f7358SJed Brown     }
2951552f7358SJed Brown   }
2952552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2953552f7358SJed Brown   ierr = PetscSectionSetChart(*section, pStart, pEnd);CHKERRQ(ierr);
2954916f0f19SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
29559ac3fadcSMatthew G. Knepley   ierr = PetscMalloc1(depth+1,&pMax);CHKERRQ(ierr);
29569ac3fadcSMatthew 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);
2957916f0f19SMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
2958916f0f19SMatthew G. Knepley     d    = dim == depth ? dep : (!dep ? 0 : dim);
2959916f0f19SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dep, &pStart, &pEnd);CHKERRQ(ierr);
2960fa716be8SMatthew G. Knepley     pMax[dep] = pMax[dep] < 0 ? pEnd : pMax[dep];
2961552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
2962ee55978aSMatthew G. Knepley       PetscInt tot = 0;
2963ee55978aSMatthew G. Knepley 
2964552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
2965fa716be8SMatthew G. Knepley         if (isFE[f] && p >= pMax[dep]) continue;
2966552f7358SJed Brown         ierr = PetscSectionSetFieldDof(*section, p, f, numDof[f*(dim+1)+d]);CHKERRQ(ierr);
2967ee55978aSMatthew G. Knepley         tot += numDof[f*(dim+1)+d];
2968552f7358SJed Brown       }
2969ee55978aSMatthew G. Knepley       ierr = PetscSectionSetDof(*section, p, tot);CHKERRQ(ierr);
2970552f7358SJed Brown     }
2971552f7358SJed Brown   }
29729ac3fadcSMatthew G. Knepley   ierr = PetscFree(pMax);CHKERRQ(ierr);
2973fa716be8SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
2974552f7358SJed Brown   PetscFunctionReturn(0);
2975552f7358SJed Brown }
2976552f7358SJed Brown 
2977552f7358SJed Brown #undef __FUNCT__
2978552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSectionBCDof"
2979552f7358SJed Brown /* Set the number of dof on each point and separate by fields
2980ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
2981552f7358SJed Brown */
2982ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSectionBCDof(DM dm, PetscInt numBC, const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
2983a6dfd86eSKarl Rupp {
2984552f7358SJed Brown   PetscInt       numFields;
2985552f7358SJed Brown   PetscInt       bc;
2986a68b90caSToby Isaac   PetscSection   aSec;
2987552f7358SJed Brown   PetscErrorCode ierr;
2988552f7358SJed Brown 
2989552f7358SJed Brown   PetscFunctionBegin;
2990552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
2991552f7358SJed Brown   for (bc = 0; bc < numBC; ++bc) {
2992552f7358SJed Brown     PetscInt        field = 0;
2993ab1d0545SMatthew G. Knepley     const PetscInt *comp;
2994552f7358SJed Brown     const PetscInt *idx;
2995ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
2996552f7358SJed Brown 
29970d644c17SKarl Rupp     if (numFields) field = bcField[bc];
2998ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
2999ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3000552f7358SJed Brown     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3001552f7358SJed Brown     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3002552f7358SJed Brown     for (i = 0; i < n; ++i) {
3003552f7358SJed Brown       const PetscInt p = idx[i];
300406ff1081SMatthew G. Knepley       PetscInt       numConst;
3005552f7358SJed Brown 
3006552f7358SJed Brown       if (numFields) {
3007552f7358SJed Brown         ierr = PetscSectionGetFieldDof(section, p, field, &numConst);CHKERRQ(ierr);
3008552f7358SJed Brown       } else {
3009552f7358SJed Brown         ierr = PetscSectionGetDof(section, p, &numConst);CHKERRQ(ierr);
3010552f7358SJed Brown       }
301106ff1081SMatthew G. Knepley       /* If Nc < 0, constrain every dof on the point */
301206ff1081SMatthew G. Knepley       if (Nc > 0) numConst = PetscMin(numConst, Nc);
301306ff1081SMatthew G. Knepley       if (numFields) {ierr = PetscSectionAddFieldConstraintDof(section, p, field, numConst);CHKERRQ(ierr);}
3014552f7358SJed Brown       ierr = PetscSectionAddConstraintDof(section, p, numConst);CHKERRQ(ierr);
3015552f7358SJed Brown     }
3016552f7358SJed Brown     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
301706ff1081SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3018552f7358SJed Brown   }
3019a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3020a68b90caSToby Isaac   if (aSec) {
3021a68b90caSToby Isaac     PetscInt aStart, aEnd, a;
3022a68b90caSToby Isaac 
3023a68b90caSToby Isaac     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3024a68b90caSToby Isaac     for (a = aStart; a < aEnd; a++) {
3025ab1d0545SMatthew G. Knepley       PetscInt dof, f;
3026a68b90caSToby Isaac 
3027a68b90caSToby Isaac       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3028a68b90caSToby Isaac       if (dof) {
3029a68b90caSToby Isaac         /* if there are point-to-point constraints, then all dofs are constrained */
3030a68b90caSToby Isaac         ierr = PetscSectionGetDof(section, a, &dof);CHKERRQ(ierr);
3031a68b90caSToby Isaac         ierr = PetscSectionSetConstraintDof(section, a, dof);CHKERRQ(ierr);
3032a68b90caSToby Isaac         for (f = 0; f < numFields; f++) {
3033a68b90caSToby Isaac           ierr = PetscSectionGetFieldDof(section, a, f, &dof);CHKERRQ(ierr);
3034a68b90caSToby Isaac           ierr = PetscSectionSetFieldConstraintDof(section, a, f, dof);CHKERRQ(ierr);
3035a68b90caSToby Isaac         }
3036a68b90caSToby Isaac       }
3037a68b90caSToby Isaac     }
3038a68b90caSToby Isaac   }
3039552f7358SJed Brown   PetscFunctionReturn(0);
3040552f7358SJed Brown }
3041552f7358SJed Brown 
3042552f7358SJed Brown #undef __FUNCT__
3043ab1d0545SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateSectionBCIndicesField"
3044ab1d0545SMatthew G. Knepley /* Set the constrained field indices on each point
3045ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3046ab1d0545SMatthew G. Knepley */
3047ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSectionBCIndicesField(DM dm, PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
30480adebc6cSBarry Smith {
3049ab1d0545SMatthew G. Knepley   PetscSection   aSec;
3050ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3051ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, bc, f, d;
3052552f7358SJed Brown   PetscErrorCode ierr;
3053552f7358SJed Brown 
3054552f7358SJed Brown   PetscFunctionBegin;
3055552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3056ab1d0545SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
3057ab1d0545SMatthew G. Knepley   /* Initialize all field indices to -1 */
3058552f7358SJed Brown   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3059ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3060ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3061ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3062ab1d0545SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) for (f = 0; f < numFields; ++f) {ierr = PetscSectionSetFieldConstraintIndices(section, p, f, indices);CHKERRQ(ierr);}
3063ab1d0545SMatthew G. Knepley   /* Handle BC constraints */
3064ab1d0545SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {
3065ab1d0545SMatthew G. Knepley     const PetscInt  field = bcField[bc];
3066ab1d0545SMatthew G. Knepley     const PetscInt *comp, *idx;
3067ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3068552f7358SJed Brown 
3069ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3070ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3071ab1d0545SMatthew G. Knepley     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3072ab1d0545SMatthew G. Knepley     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3073ab1d0545SMatthew G. Knepley     for (i = 0; i < n; ++i) {
3074ab1d0545SMatthew G. Knepley       const PetscInt  p = idx[i];
3075ab1d0545SMatthew G. Knepley       const PetscInt *find;
3076ab1d0545SMatthew G. Knepley       PetscInt        fcdof, c;
3077ab1d0545SMatthew G. Knepley 
3078ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintDof(section, p, field, &fcdof);CHKERRQ(ierr);
3079ab1d0545SMatthew G. Knepley       if (Nc < 0) {
3080ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) indices[d] = d;
3081552f7358SJed Brown       } else {
3082ab1d0545SMatthew G. Knepley         ierr = PetscSectionGetFieldConstraintIndices(section, p, field, &find);CHKERRQ(ierr);
3083ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) {if (find[d] < 0) break; indices[d] = find[d];}
3084ab1d0545SMatthew G. Knepley         for (c = 0; c < Nc; ++c) indices[d+c] = comp[c];
3085ab1d0545SMatthew G. Knepley         ierr = PetscSortInt(d+Nc, indices);CHKERRQ(ierr);
3086ab1d0545SMatthew G. Knepley         for (c = d+Nc; c < fcdof; ++c) indices[c] = -1;
3087552f7358SJed Brown       }
3088ab1d0545SMatthew G. Knepley       ierr = PetscSectionSetFieldConstraintIndices(section, p, field, indices);CHKERRQ(ierr);
3089552f7358SJed Brown     }
3090ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3091ab1d0545SMatthew G. Knepley     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3092552f7358SJed Brown   }
3093ab1d0545SMatthew G. Knepley   /* Handle anchors */
3094ab1d0545SMatthew G. Knepley   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3095ab1d0545SMatthew G. Knepley   if (aSec) {
3096ab1d0545SMatthew G. Knepley     PetscInt aStart, aEnd, a;
3097552f7358SJed Brown 
3098ab1d0545SMatthew G. Knepley     for (d = 0; d < maxDof; ++d) indices[d] = d;
3099ab1d0545SMatthew G. Knepley     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3100ab1d0545SMatthew G. Knepley     for (a = aStart; a < aEnd; a++) {
3101ab1d0545SMatthew G. Knepley       PetscInt dof, fdof, f;
3102ab1d0545SMatthew G. Knepley 
3103ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3104ab1d0545SMatthew G. Knepley       if (dof) {
3105ab1d0545SMatthew G. Knepley         /* if there are point-to-point constraints, then all dofs are constrained */
3106ab1d0545SMatthew G. Knepley         for (f = 0; f < numFields; f++) {
3107ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, a, f, &fdof);CHKERRQ(ierr);
3108ab1d0545SMatthew G. Knepley           ierr = PetscSectionSetFieldConstraintIndices(section, a, f, indices);CHKERRQ(ierr);
3109ab1d0545SMatthew G. Knepley         }
3110ab1d0545SMatthew G. Knepley       }
3111ab1d0545SMatthew G. Knepley     }
3112ab1d0545SMatthew G. Knepley   }
3113ab1d0545SMatthew G. Knepley   ierr = PetscFree(indices);CHKERRQ(ierr);
3114ab1d0545SMatthew G. Knepley   PetscFunctionReturn(0);
3115ab1d0545SMatthew G. Knepley }
3116ab1d0545SMatthew G. Knepley 
3117ab1d0545SMatthew G. Knepley #undef __FUNCT__
3118ab1d0545SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateSectionBCIndices"
3119ab1d0545SMatthew G. Knepley /* Set the constrained indices on each point */
3120ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSectionBCIndices(DM dm, PetscSection section)
3121ab1d0545SMatthew G. Knepley {
3122ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3123ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, f, d;
3124ab1d0545SMatthew G. Knepley   PetscErrorCode ierr;
3125ab1d0545SMatthew G. Knepley 
3126ab1d0545SMatthew G. Knepley   PetscFunctionBegin;
3127ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3128ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3129ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3130ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3131ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3132552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3133552f7358SJed Brown     PetscInt cdof, d;
3134552f7358SJed Brown 
3135552f7358SJed Brown     ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
3136552f7358SJed Brown     if (cdof) {
3137552f7358SJed Brown       if (numFields) {
3138552f7358SJed Brown         PetscInt numConst = 0, foff = 0;
3139552f7358SJed Brown 
3140552f7358SJed Brown         for (f = 0; f < numFields; ++f) {
3141ab1d0545SMatthew G. Knepley           const PetscInt *find;
3142ab1d0545SMatthew G. Knepley           PetscInt        fcdof, fdof;
3143552f7358SJed Brown 
3144552f7358SJed Brown           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
3145ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
3146ab1d0545SMatthew G. Knepley           /* Change constraint numbering from field component to local dof number */
3147ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintIndices(section, p, f, &find);CHKERRQ(ierr);
3148ab1d0545SMatthew G. Knepley           for (d = 0; d < fcdof; ++d) indices[numConst+d] = find[d] + foff;
3149ab1d0545SMatthew G. Knepley           numConst += fcdof;
3150552f7358SJed Brown           foff     += fdof;
3151552f7358SJed Brown         }
3152552f7358SJed Brown         if (cdof != numConst) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Total number of field constraints %D should be %D", numConst, cdof);
3153552f7358SJed Brown       } else {
31540d644c17SKarl Rupp         for (d = 0; d < cdof; ++d) indices[d] = d;
3155552f7358SJed Brown       }
3156552f7358SJed Brown       ierr = PetscSectionSetConstraintIndices(section, p, indices);CHKERRQ(ierr);
3157552f7358SJed Brown     }
3158552f7358SJed Brown   }
3159552f7358SJed Brown   ierr = PetscFree(indices);CHKERRQ(ierr);
3160552f7358SJed Brown   PetscFunctionReturn(0);
3161552f7358SJed Brown }
3162552f7358SJed Brown 
3163552f7358SJed Brown #undef __FUNCT__
3164552f7358SJed Brown #define __FUNCT__ "DMPlexCreateSection"
3165552f7358SJed Brown /*@C
3166552f7358SJed Brown   DMPlexCreateSection - Create a PetscSection based upon the dof layout specification provided.
3167552f7358SJed Brown 
3168552f7358SJed Brown   Not Collective
3169552f7358SJed Brown 
3170552f7358SJed Brown   Input Parameters:
3171552f7358SJed Brown + dm        - The DMPlex object
3172552f7358SJed Brown . dim       - The spatial dimension of the problem
3173552f7358SJed Brown . numFields - The number of fields in the problem
3174552f7358SJed Brown . numComp   - An array of size numFields that holds the number of components for each field
3175552f7358SJed 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
3176552f7358SJed Brown . numBC     - The number of boundary conditions
3177552f7358SJed Brown . bcField   - An array of size numBC giving the field number for each boundry condition
3178ab1d0545SMatthew G. Knepley . bcComps   - [Optional] An array of size numBC giving an IS holding the field components to which each boundary condition applies
3179ab1d0545SMatthew G. Knepley . bcPoints  - An array of size numBC giving an IS holding the Plex points to which each boundary condition applies
3180f8f126e8SMatthew G. Knepley - perm      - Optional permutation of the chart, or NULL
3181552f7358SJed Brown 
3182552f7358SJed Brown   Output Parameter:
3183552f7358SJed Brown . section - The PetscSection object
3184552f7358SJed Brown 
3185552f7358SJed 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
3186f8f126e8SMatthew G. Knepley   number of dof for field 0 on each edge.
3187f8f126e8SMatthew G. Knepley 
3188f8f126e8SMatthew G. Knepley   The chart permutation is the same one set using PetscSectionSetPermutation()
3189552f7358SJed Brown 
3190552f7358SJed Brown   Level: developer
3191552f7358SJed Brown 
31923813dfbdSMatthew G Knepley   Fortran Notes:
31933813dfbdSMatthew G Knepley   A Fortran 90 version is available as DMPlexCreateSectionF90()
31943813dfbdSMatthew G Knepley 
3195552f7358SJed Brown .keywords: mesh, elements
3196f8f126e8SMatthew G. Knepley .seealso: DMPlexCreate(), PetscSectionCreate(), PetscSectionSetPermutation()
3197552f7358SJed Brown @*/
3198ab1d0545SMatthew 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)
3199a6dfd86eSKarl Rupp {
3200a68b90caSToby Isaac   PetscSection   aSec;
3201552f7358SJed Brown   PetscErrorCode ierr;
3202552f7358SJed Brown 
3203552f7358SJed Brown   PetscFunctionBegin;
3204552f7358SJed Brown   ierr = DMPlexCreateSectionInitial(dm, dim, numFields, numComp, numDof, section);CHKERRQ(ierr);
3205ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSectionBCDof(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3206f8f126e8SMatthew G. Knepley   if (perm) {ierr = PetscSectionSetPermutation(*section, perm);CHKERRQ(ierr);}
3207552f7358SJed Brown   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
3208a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,NULL);CHKERRQ(ierr);
3209ab1d0545SMatthew G. Knepley   if (numBC || aSec) {
3210ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndicesField(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3211ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndices(dm, *section);CHKERRQ(ierr);
3212ab1d0545SMatthew G. Knepley   }
3213ce1779c8SBarry Smith   ierr = PetscSectionViewFromOptions(*section,NULL,"-section_view");CHKERRQ(ierr);
3214552f7358SJed Brown   PetscFunctionReturn(0);
3215552f7358SJed Brown }
3216552f7358SJed Brown 
3217552f7358SJed Brown #undef __FUNCT__
3218552f7358SJed Brown #define __FUNCT__ "DMCreateCoordinateDM_Plex"
32190adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
32200adebc6cSBarry Smith {
3221efe440bfSMatthew G. Knepley   PetscSection   section, s;
3222efe440bfSMatthew G. Knepley   Mat            m;
32233e922f36SToby Isaac   PetscInt       maxHeight;
3224552f7358SJed Brown   PetscErrorCode ierr;
3225552f7358SJed Brown 
3226552f7358SJed Brown   PetscFunctionBegin;
322738221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
32283e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
32293e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
323082f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
3231552f7358SJed Brown   ierr = DMSetDefaultSection(*cdm, section);CHKERRQ(ierr);
32321d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3233efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3234efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3235efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3236efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3237efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
3238552f7358SJed Brown   PetscFunctionReturn(0);
3239552f7358SJed Brown }
3240552f7358SJed Brown 
3241552f7358SJed Brown #undef __FUNCT__
3242552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeSection"
32430adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
32440adebc6cSBarry Smith {
3245552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3246552f7358SJed Brown 
3247552f7358SJed Brown   PetscFunctionBegin;
3248552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3249552f7358SJed Brown   if (section) *section = mesh->coneSection;
3250552f7358SJed Brown   PetscFunctionReturn(0);
3251552f7358SJed Brown }
3252552f7358SJed Brown 
3253552f7358SJed Brown #undef __FUNCT__
32548cb4d582SMatthew G. Knepley #define __FUNCT__ "DMPlexGetSupportSection"
32558cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
32568cb4d582SMatthew G. Knepley {
32578cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
32588cb4d582SMatthew G. Knepley 
32598cb4d582SMatthew G. Knepley   PetscFunctionBegin;
32608cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32618cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
32628cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
32638cb4d582SMatthew G. Knepley }
32648cb4d582SMatthew G. Knepley 
32658cb4d582SMatthew G. Knepley #undef __FUNCT__
3266552f7358SJed Brown #define __FUNCT__ "DMPlexGetCones"
3267a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3268a6dfd86eSKarl Rupp {
3269552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3270552f7358SJed Brown 
3271552f7358SJed Brown   PetscFunctionBegin;
3272552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3273552f7358SJed Brown   if (cones) *cones = mesh->cones;
3274552f7358SJed Brown   PetscFunctionReturn(0);
3275552f7358SJed Brown }
3276552f7358SJed Brown 
3277552f7358SJed Brown #undef __FUNCT__
3278552f7358SJed Brown #define __FUNCT__ "DMPlexGetConeOrientations"
3279a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3280a6dfd86eSKarl Rupp {
3281552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3282552f7358SJed Brown 
3283552f7358SJed Brown   PetscFunctionBegin;
3284552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3285552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3286552f7358SJed Brown   PetscFunctionReturn(0);
3287552f7358SJed Brown }
3288552f7358SJed Brown 
3289552f7358SJed Brown /******************************** FEM Support **********************************/
3290552f7358SJed Brown 
3291552f7358SJed Brown #undef __FUNCT__
32923194fc30SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateSpectralClosurePermutation"
32933194fc30SMatthew G. Knepley PetscErrorCode DMPlexCreateSpectralClosurePermutation(DM dm, PetscSection section)
32943194fc30SMatthew G. Knepley {
32953194fc30SMatthew G. Knepley   PetscInt      *perm;
3296*89eabcffSMatthew G. Knepley   PetscInt       dim, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
32973194fc30SMatthew G. Knepley   PetscErrorCode ierr;
32983194fc30SMatthew G. Knepley 
32993194fc30SMatthew G. Knepley   PetscFunctionBegin;
33003194fc30SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
33013194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
33023194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
3303*89eabcffSMatthew G. Knepley   if (dim <= 1) PetscFunctionReturn(0);
33043194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
33053194fc30SMatthew G. Knepley     /* An order k SEM disc has k-1 dofs on an edge */
33063194fc30SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
33073194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
33083194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
33093194fc30SMatthew G. Knepley     k = k/Nc + 1;
3310*89eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
33113194fc30SMatthew G. Knepley   }
33123194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
33133194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
3314*89eabcffSMatthew G. Knepley     switch (dim) {
3315*89eabcffSMatthew G. Knepley     case 2:
33163194fc30SMatthew 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} */
33173194fc30SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
33183194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
33193194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
33203194fc30SMatthew G. Knepley       k = k/Nc + 1;
33213194fc30SMatthew G. Knepley       /* The SEM order is
33223194fc30SMatthew G. Knepley 
33233194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
3324*89eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
33253194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
33263194fc30SMatthew G. Knepley       */
33273194fc30SMatthew G. Knepley       {
33283194fc30SMatthew G. Knepley         const PetscInt of   = 0;
33293194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
33303194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
33313194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
33323194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
33333194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
33343194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
33353194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
33363194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
33373194fc30SMatthew G. Knepley         PetscInt       o;
33383194fc30SMatthew G. Knepley 
33393194fc30SMatthew G. Knepley         /* bottom */
33403194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
33413194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
33423194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
33433194fc30SMatthew G. Knepley         /* middle */
33443194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
33453194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
33463194fc30SMatthew 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;
33473194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
33483194fc30SMatthew G. Knepley         }
33493194fc30SMatthew G. Knepley         /* top */
33503194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
33513194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
33523194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
33533194fc30SMatthew G. Knepley         foffset = offset;
33543194fc30SMatthew G. Knepley       }
3355*89eabcffSMatthew G. Knepley       break;
3356*89eabcffSMatthew G. Knepley     case 3:
3357*89eabcffSMatthew G. Knepley       /* The original hex closure is
3358*89eabcffSMatthew G. Knepley 
3359*89eabcffSMatthew G. Knepley          {c,
3360*89eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
3361*89eabcffSMatthew 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,
3362*89eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
3363*89eabcffSMatthew G. Knepley       */
3364*89eabcffSMatthew G. Knepley       ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
3365*89eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
3366*89eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
3367*89eabcffSMatthew G. Knepley       k = k/Nc + 1;
3368*89eabcffSMatthew G. Knepley       /* The SEM order is
3369*89eabcffSMatthew G. Knepley          Bottom Slice
3370*89eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
3371*89eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
3372*89eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
3373*89eabcffSMatthew G. Knepley 
3374*89eabcffSMatthew G. Knepley          Middle Slice (j)
3375*89eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
3376*89eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
3377*89eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
3378*89eabcffSMatthew G. Knepley 
3379*89eabcffSMatthew G. Knepley          Top Slice
3380*89eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
3381*89eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
3382*89eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
3383*89eabcffSMatthew G. Knepley       */
3384*89eabcffSMatthew G. Knepley       {
3385*89eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
3386*89eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
3387*89eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
3388*89eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
3389*89eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
3390*89eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
3391*89eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
3392*89eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
3393*89eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
3394*89eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
3395*89eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
3396*89eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
3397*89eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
3398*89eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
3399*89eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
3400*89eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
3401*89eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
3402*89eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
3403*89eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
3404*89eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
3405*89eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
3406*89eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
3407*89eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
3408*89eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
3409*89eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
3410*89eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
3411*89eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
3412*89eabcffSMatthew G. Knepley         PetscInt       o, n;
3413*89eabcffSMatthew G. Knepley 
3414*89eabcffSMatthew G. Knepley         /* Bottom Slice */
3415*89eabcffSMatthew G. Knepley         /*   bottom */
3416*89eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
3417*89eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
3418*89eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
3419*89eabcffSMatthew G. Knepley         /*   middle */
3420*89eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
3421*89eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
3422*89eabcffSMatthew G. Knepley           for (n = 0; n < k-1; ++n) {o = ofb+n*(k-1)+(k-2)-i; for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;}
3423*89eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
34243194fc30SMatthew G. Knepley         }
3425*89eabcffSMatthew G. Knepley         /*   top */
3426*89eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
3427*89eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
3428*89eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
3429*89eabcffSMatthew G. Knepley 
3430*89eabcffSMatthew G. Knepley         /* Middle Slice */
3431*89eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
3432*89eabcffSMatthew G. Knepley           /*   bottom */
3433*89eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
3434*89eabcffSMatthew 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;
3435*89eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
3436*89eabcffSMatthew G. Knepley           /*   middle */
3437*89eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
3438*89eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
3439*89eabcffSMatthew 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;
3440*89eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
3441*89eabcffSMatthew G. Knepley           }
3442*89eabcffSMatthew G. Knepley           /*   top */
3443*89eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
3444*89eabcffSMatthew 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;
3445*89eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
3446*89eabcffSMatthew G. Knepley         }
3447*89eabcffSMatthew G. Knepley 
3448*89eabcffSMatthew G. Knepley         /* Top Slice */
3449*89eabcffSMatthew G. Knepley         /*   bottom */
3450*89eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
3451*89eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
3452*89eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
3453*89eabcffSMatthew G. Knepley         /*   middle */
3454*89eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
3455*89eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
3456*89eabcffSMatthew 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;
3457*89eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
3458*89eabcffSMatthew G. Knepley         }
3459*89eabcffSMatthew G. Knepley         /*   top */
3460*89eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
3461*89eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
3462*89eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
3463*89eabcffSMatthew G. Knepley 
3464*89eabcffSMatthew G. Knepley         foffset = offset;
3465*89eabcffSMatthew G. Knepley       }
3466*89eabcffSMatthew G. Knepley       break;
3467*89eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
3468*89eabcffSMatthew G. Knepley     }
3469*89eabcffSMatthew G. Knepley   }
3470*89eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
34713194fc30SMatthew G. Knepley   /* Check permutation */
34723194fc30SMatthew G. Knepley   {
34733194fc30SMatthew G. Knepley     PetscInt *check;
34743194fc30SMatthew G. Knepley 
34753194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
34763194fc30SMatthew 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]);}
34773194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
34783194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
34793194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
34803194fc30SMatthew G. Knepley   }
34811fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
34823194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
34833194fc30SMatthew G. Knepley }
34843194fc30SMatthew G. Knepley 
34853194fc30SMatthew G. Knepley #undef __FUNCT__
34861a271a75SMatthew G. Knepley #define __FUNCT__ "DMPlexVecGetClosure_Depth1_Static"
34871a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3488a6dfd86eSKarl Rupp {
3489552f7358SJed Brown   PetscScalar    *array, *vArray;
3490d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
34911a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
3492552f7358SJed Brown   PetscErrorCode  ierr;
3493552f7358SJed Brown 
34941b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
34952a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
34965a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
34975a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
34985a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
34993f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
35009df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
35019df71ca4SMatthew G. Knepley       PetscInt dof;
3502d9917b9dSMatthew G. Knepley 
35039df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
35049df71ca4SMatthew G. Knepley       size += dof;
35059df71ca4SMatthew G. Knepley     }
35069df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
35079df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
35082a3aaacfSMatthew G. Knepley       PetscInt       dof;
35095a1bb5cfSMatthew G. Knepley 
35105a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
35112a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
35125a1bb5cfSMatthew G. Knepley       size += dof;
35135a1bb5cfSMatthew G. Knepley     }
35143f7cbbe7SMatthew G. Knepley     if (!values) {
35153f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
35163f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
35173f7cbbe7SMatthew G. Knepley     }
35185a1bb5cfSMatthew G. Knepley     ierr = DMGetWorkArray(dm, size, PETSC_SCALAR, &array);CHKERRQ(ierr);
3519982e9ed1SMatthew G. Knepley   } else {
3520982e9ed1SMatthew G. Knepley     array = *values;
3521982e9ed1SMatthew G. Knepley   }
35229df71ca4SMatthew G. Knepley   size = 0;
35235a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
35249df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
35259df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
35269df71ca4SMatthew G. Knepley     PetscScalar *varr;
3527d9917b9dSMatthew G. Knepley 
35289df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
35299df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
35309df71ca4SMatthew G. Knepley     varr = &vArray[off];
35311a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
35321a271a75SMatthew G. Knepley       array[offset] = varr[d];
35339df71ca4SMatthew G. Knepley     }
35349df71ca4SMatthew G. Knepley     size += dof;
35359df71ca4SMatthew G. Knepley   }
35369df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
35379df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
35389df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
35395a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
35405a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
35415a1bb5cfSMatthew G. Knepley 
354252ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
35435a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
35445a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
35455a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
35465a1bb5cfSMatthew G. Knepley     if (o >= 0) {
35471a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
35481a271a75SMatthew G. Knepley         array[offset] = varr[d];
35495a1bb5cfSMatthew G. Knepley       }
35505a1bb5cfSMatthew G. Knepley     } else {
35511a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
35521a271a75SMatthew G. Knepley         array[offset] = varr[d];
35535a1bb5cfSMatthew G. Knepley       }
35545a1bb5cfSMatthew G. Knepley     }
35559df71ca4SMatthew G. Knepley     size += dof;
35565a1bb5cfSMatthew G. Knepley   }
35575a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
35589df71ca4SMatthew G. Knepley   if (!*values) {
35595a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
35605a1bb5cfSMatthew G. Knepley     *values = array;
35619df71ca4SMatthew G. Knepley   } else {
35629df71ca4SMatthew G. Knepley     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %d < actual size %d", *csize, size);
35638c312ff3SMatthew G. Knepley     *csize = size;
35649df71ca4SMatthew G. Knepley   }
35655a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
35665a1bb5cfSMatthew G. Knepley }
3567d9917b9dSMatthew G. Knepley 
3568d9917b9dSMatthew G. Knepley #undef __FUNCT__
35691a271a75SMatthew G. Knepley #define __FUNCT__ "DMPlexVecGetClosure_Static"
35708f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Static(PetscSection section, PetscInt numPoints, const PetscInt points[], const PetscInt perm[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
35711a271a75SMatthew G. Knepley {
35721a271a75SMatthew G. Knepley   PetscInt       offset = 0, p;
35731a271a75SMatthew G. Knepley   PetscErrorCode ierr;
35741a271a75SMatthew G. Knepley 
35751a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3576fe02ba77SJed Brown   *size = 0;
35771a271a75SMatthew G. Knepley   for (p = 0; p < numPoints*2; p += 2) {
35781a271a75SMatthew G. Knepley     const PetscInt point = points[p];
35791a271a75SMatthew G. Knepley     const PetscInt o     = points[p+1];
35801a271a75SMatthew G. Knepley     PetscInt       dof, off, d;
35811a271a75SMatthew G. Knepley     const PetscScalar *varr;
35821a271a75SMatthew G. Knepley 
35831a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
35841a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
35851a271a75SMatthew G. Knepley     varr = &vArray[off];
35861a271a75SMatthew G. Knepley     if (o >= 0) {
35878f3be42fSMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset)    array[perm ? perm[offset] : offset] = varr[d];
35881a271a75SMatthew G. Knepley     } else {
35898f3be42fSMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) array[perm ? perm[offset] : offset] = varr[d];
35901a271a75SMatthew G. Knepley     }
35911a271a75SMatthew G. Knepley   }
35921a271a75SMatthew G. Knepley   *size = offset;
35931a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
35941a271a75SMatthew G. Knepley }
35951a271a75SMatthew G. Knepley 
35961a271a75SMatthew G. Knepley #undef __FUNCT__
35971a271a75SMatthew G. Knepley #define __FUNCT__ "DMPlexVecGetClosure_Fields_Static"
35988f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Fields_Static(PetscSection section, PetscInt numPoints, const PetscInt points[], PetscInt numFields, const PetscInt perm[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
35991a271a75SMatthew G. Knepley {
36001a271a75SMatthew G. Knepley   PetscInt       offset = 0, f;
36011a271a75SMatthew G. Knepley   PetscErrorCode ierr;
36021a271a75SMatthew G. Knepley 
36031a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3604fe02ba77SJed Brown   *size = 0;
36051a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
36061a271a75SMatthew G. Knepley     PetscInt fcomp, p;
36071a271a75SMatthew G. Knepley 
36081a271a75SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &fcomp);CHKERRQ(ierr);
36091a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
36101a271a75SMatthew G. Knepley       const PetscInt point = points[p];
36111a271a75SMatthew G. Knepley       const PetscInt o     = points[p+1];
36121a271a75SMatthew G. Knepley       PetscInt       fdof, foff, d, c;
36131a271a75SMatthew G. Knepley       const PetscScalar *varr;
36141a271a75SMatthew G. Knepley 
36151a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
36161a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
36171a271a75SMatthew G. Knepley       varr = &vArray[foff];
36181a271a75SMatthew G. Knepley       if (o >= 0) {
36198f3be42fSMatthew G. Knepley         for (d = 0; d < fdof; ++d, ++offset) array[perm ? perm[offset] : offset] = varr[d];
36201a271a75SMatthew G. Knepley       } else {
36211a271a75SMatthew G. Knepley         for (d = fdof/fcomp-1; d >= 0; --d) {
36221a271a75SMatthew G. Knepley           for (c = 0; c < fcomp; ++c, ++offset) {
36238f3be42fSMatthew G. Knepley             array[perm ? perm[offset] : offset] = varr[d*fcomp+c];
36241a271a75SMatthew G. Knepley           }
36251a271a75SMatthew G. Knepley         }
36261a271a75SMatthew G. Knepley       }
36271a271a75SMatthew G. Knepley     }
36281a271a75SMatthew G. Knepley   }
36291a271a75SMatthew G. Knepley   *size = offset;
36301a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
36311a271a75SMatthew G. Knepley }
36321a271a75SMatthew G. Knepley 
36331a271a75SMatthew G. Knepley #undef __FUNCT__
3634552f7358SJed Brown #define __FUNCT__ "DMPlexVecGetClosure"
3635552f7358SJed Brown /*@C
3636552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
3637552f7358SJed Brown 
3638552f7358SJed Brown   Not collective
3639552f7358SJed Brown 
3640552f7358SJed Brown   Input Parameters:
3641552f7358SJed Brown + dm - The DM
3642552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
3643552f7358SJed Brown . v - The local vector
3644552f7358SJed Brown - point - The sieve point in the DM
3645552f7358SJed Brown 
3646552f7358SJed Brown   Output Parameters:
3647552f7358SJed Brown + csize - The number of values in the closure, or NULL
3648552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
3649552f7358SJed Brown 
3650552f7358SJed Brown   Fortran Notes:
3651552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
3652552f7358SJed Brown   include petsc.h90 in your code.
3653552f7358SJed Brown 
3654552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
3655552f7358SJed Brown 
3656552f7358SJed Brown   Level: intermediate
3657552f7358SJed Brown 
3658552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
3659552f7358SJed Brown @*/
3660552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3661552f7358SJed Brown {
3662552f7358SJed Brown   PetscSection    clSection;
3663d9917b9dSMatthew G. Knepley   IS              clPoints;
3664552f7358SJed Brown   PetscScalar    *array, *vArray;
3665552f7358SJed Brown   PetscInt       *points = NULL;
36668f3be42fSMatthew G. Knepley   const PetscInt *clp, *perm;
36671a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, size;
3668552f7358SJed Brown   PetscErrorCode  ierr;
3669552f7358SJed Brown 
3670d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
3671552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3672552f7358SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
36731a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
36741a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
3675552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3676552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3677552f7358SJed Brown   if (depth == 1 && numFields < 2) {
36781a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
3679552f7358SJed Brown     PetscFunctionReturn(0);
3680552f7358SJed Brown   }
36811a271a75SMatthew G. Knepley   /* Get points */
36821fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
3683d9917b9dSMatthew G. Knepley   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, &clSection, &clPoints);CHKERRQ(ierr);
3684d9917b9dSMatthew G. Knepley   if (!clPoints) {
36851a271a75SMatthew G. Knepley     PetscInt pStart, pEnd, p, q;
3686552f7358SJed Brown 
36871a271a75SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3688552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
3689552f7358SJed Brown     /* Compress out points not in the section */
3690552f7358SJed Brown     for (p = 0, q = 0; p < numPoints*2; p += 2) {
3691552f7358SJed Brown       if ((points[p] >= pStart) && (points[p] < pEnd)) {
3692552f7358SJed Brown         points[q*2]   = points[p];
3693552f7358SJed Brown         points[q*2+1] = points[p+1];
3694552f7358SJed Brown         ++q;
3695552f7358SJed Brown       }
3696552f7358SJed Brown     }
3697552f7358SJed Brown     numPoints = q;
3698d9917b9dSMatthew G. Knepley   } else {
3699d9917b9dSMatthew G. Knepley     PetscInt dof, off;
3700552f7358SJed Brown 
3701d9917b9dSMatthew G. Knepley     ierr = PetscSectionGetDof(clSection, point, &dof);CHKERRQ(ierr);
3702d9917b9dSMatthew G. Knepley     ierr = PetscSectionGetOffset(clSection, point, &off);CHKERRQ(ierr);
3703d9917b9dSMatthew G. Knepley     ierr = ISGetIndices(clPoints, &clp);CHKERRQ(ierr);
37041a271a75SMatthew G. Knepley     numPoints = dof/2;
3705d9917b9dSMatthew G. Knepley     points    = (PetscInt *) &clp[off];
3706552f7358SJed Brown   }
37071a271a75SMatthew G. Knepley   /* Get array */
3708552f7358SJed Brown   if (!values || !*values) {
37091a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
3710552f7358SJed Brown 
37111a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
3712552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
37131a271a75SMatthew G. Knepley       asize += dof;
3714552f7358SJed Brown     }
3715bd6c0d32SMatthew G. Knepley     if (!values) {
3716d9917b9dSMatthew G. Knepley       if (!clPoints) {ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);}
37171a271a75SMatthew G. Knepley       else           {ierr = ISRestoreIndices(clPoints, &clp);CHKERRQ(ierr);}
37181a271a75SMatthew G. Knepley       if (csize) *csize = asize;
3719bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
3720bd6c0d32SMatthew G. Knepley     }
37211a271a75SMatthew G. Knepley     ierr = DMGetWorkArray(dm, asize, PETSC_SCALAR, &array);CHKERRQ(ierr);
3722d0f6b257SMatthew G. Knepley   } else {
3723d0f6b257SMatthew G. Knepley     array = *values;
3724d0f6b257SMatthew G. Knepley   }
3725552f7358SJed Brown   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
37261a271a75SMatthew G. Knepley   /* Get values */
37278f3be42fSMatthew G. Knepley   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
37288f3be42fSMatthew G. Knepley   else               {ierr = DMPlexVecGetClosure_Static(section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
37291a271a75SMatthew G. Knepley   /* Cleanup points */
37301a271a75SMatthew G. Knepley   if (!clPoints) {ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);}
37311a271a75SMatthew G. Knepley   else           {ierr = ISRestoreIndices(clPoints, &clp);CHKERRQ(ierr);}
37321a271a75SMatthew G. Knepley   /* Cleanup array */
3733552f7358SJed Brown   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
3734d0f6b257SMatthew G. Knepley   if (!*values) {
3735552f7358SJed Brown     if (csize) *csize = size;
3736552f7358SJed Brown     *values = array;
3737d0f6b257SMatthew G. Knepley   } else {
3738f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
3739d0f6b257SMatthew G. Knepley     *csize = size;
3740d0f6b257SMatthew G. Knepley   }
3741552f7358SJed Brown   PetscFunctionReturn(0);
3742552f7358SJed Brown }
3743552f7358SJed Brown 
3744552f7358SJed Brown #undef __FUNCT__
3745552f7358SJed Brown #define __FUNCT__ "DMPlexVecRestoreClosure"
3746552f7358SJed Brown /*@C
3747552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
3748552f7358SJed Brown 
3749552f7358SJed Brown   Not collective
3750552f7358SJed Brown 
3751552f7358SJed Brown   Input Parameters:
3752552f7358SJed Brown + dm - The DM
37530298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
3754552f7358SJed Brown . v - The local vector
3755552f7358SJed Brown . point - The sieve point in the DM
37560298fd71SBarry Smith . csize - The number of values in the closure, or NULL
3757552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
3758552f7358SJed Brown 
37593813dfbdSMatthew G Knepley   Fortran Notes:
37603813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
37613813dfbdSMatthew G Knepley   include petsc.h90 in your code.
37623813dfbdSMatthew G Knepley 
37633813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
37643813dfbdSMatthew G Knepley 
3765552f7358SJed Brown   Level: intermediate
3766552f7358SJed Brown 
3767552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
3768552f7358SJed Brown @*/
37697c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3770a6dfd86eSKarl Rupp {
3771552f7358SJed Brown   PetscInt       size = 0;
3772552f7358SJed Brown   PetscErrorCode ierr;
3773552f7358SJed Brown 
3774552f7358SJed Brown   PetscFunctionBegin;
3775552f7358SJed Brown   /* Should work without recalculating size */
3776552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, size, PETSC_SCALAR, (void*) values);CHKERRQ(ierr);
3777552f7358SJed Brown   PetscFunctionReturn(0);
3778552f7358SJed Brown }
3779552f7358SJed Brown 
3780552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
3781552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
3782552f7358SJed Brown 
3783552f7358SJed Brown #undef __FUNCT__
3784552f7358SJed Brown #define __FUNCT__ "updatePoint_private"
37858f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode updatePoint_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar*, PetscScalar), PetscBool setBC, PetscInt orientation, const PetscInt perm[], const PetscScalar values[], PetscInt offset, PetscScalar array[])
3786552f7358SJed Brown {
3787552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
3788552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
3789552f7358SJed Brown   PetscScalar    *a;
3790552f7358SJed Brown   PetscInt        off, cind = 0, k;
3791552f7358SJed Brown   PetscErrorCode  ierr;
3792552f7358SJed Brown 
3793552f7358SJed Brown   PetscFunctionBegin;
3794552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
3795552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
3796552f7358SJed Brown   a    = &array[off];
3797552f7358SJed Brown   if (!cdof || setBC) {
3798552f7358SJed Brown     if (orientation >= 0) {
3799552f7358SJed Brown       for (k = 0; k < dof; ++k) {
38008f3be42fSMatthew G. Knepley         fuse(&a[k], values[perm ? perm[offset+k] : offset+k]);
3801552f7358SJed Brown       }
3802552f7358SJed Brown     } else {
3803552f7358SJed Brown       for (k = 0; k < dof; ++k) {
38048f3be42fSMatthew G. Knepley         fuse(&a[k], values[perm ? perm[offset+dof-k-1] : offset+dof-k-1]);
3805552f7358SJed Brown       }
3806552f7358SJed Brown     }
3807552f7358SJed Brown   } else {
3808552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
3809552f7358SJed Brown     if (orientation >= 0) {
3810552f7358SJed Brown       for (k = 0; k < dof; ++k) {
3811552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
38128f3be42fSMatthew G. Knepley         fuse(&a[k], values[perm ? perm[offset+k] : offset+k]);
3813552f7358SJed Brown       }
3814552f7358SJed Brown     } else {
3815552f7358SJed Brown       for (k = 0; k < dof; ++k) {
3816552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
38178f3be42fSMatthew G. Knepley         fuse(&a[k], values[perm ? perm[offset+dof-k-1] : offset+dof-k-1]);
3818552f7358SJed Brown       }
3819552f7358SJed Brown     }
3820552f7358SJed Brown   }
3821552f7358SJed Brown   PetscFunctionReturn(0);
3822552f7358SJed Brown }
3823552f7358SJed Brown 
3824552f7358SJed Brown #undef __FUNCT__
3825a5e93ea8SMatthew G. Knepley #define __FUNCT__ "updatePointBC_private"
38268f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode updatePointBC_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar*, PetscScalar), PetscInt orientation, const PetscInt perm[], const PetscScalar values[], PetscInt offset, PetscScalar array[])
3827a5e93ea8SMatthew G. Knepley {
3828a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
3829a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
3830a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
3831a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
3832a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
3833a5e93ea8SMatthew G. Knepley 
3834a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
3835a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
3836a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
3837a5e93ea8SMatthew G. Knepley   a    = &array[off];
3838a5e93ea8SMatthew G. Knepley   if (cdof) {
3839a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
3840a5e93ea8SMatthew G. Knepley     if (orientation >= 0) {
3841a5e93ea8SMatthew G. Knepley       for (k = 0; k < dof; ++k) {
3842a5e93ea8SMatthew G. Knepley         if ((cind < cdof) && (k == cdofs[cind])) {
38438f3be42fSMatthew G. Knepley           fuse(&a[k], values[perm ? perm[offset+k] : offset+k]);
3844a5e93ea8SMatthew G. Knepley           ++cind;
3845a5e93ea8SMatthew G. Knepley         }
3846a5e93ea8SMatthew G. Knepley       }
3847a5e93ea8SMatthew G. Knepley     } else {
3848a5e93ea8SMatthew G. Knepley       for (k = 0; k < dof; ++k) {
3849a5e93ea8SMatthew G. Knepley         if ((cind < cdof) && (k == cdofs[cind])) {
38508f3be42fSMatthew G. Knepley           fuse(&a[k], values[perm ? perm[offset+dof-k-1] : offset+dof-k-1]);
3851a5e93ea8SMatthew G. Knepley           ++cind;
3852a5e93ea8SMatthew G. Knepley         }
3853a5e93ea8SMatthew G. Knepley       }
3854a5e93ea8SMatthew G. Knepley     }
3855a5e93ea8SMatthew G. Knepley   }
3856a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3857a5e93ea8SMatthew G. Knepley }
3858a5e93ea8SMatthew G. Knepley 
3859a5e93ea8SMatthew G. Knepley #undef __FUNCT__
3860552f7358SJed Brown #define __FUNCT__ "updatePointFields_private"
38618f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode updatePointFields_private(PetscSection section, PetscInt point, PetscInt o, PetscInt f, PetscInt fcomp, void (*fuse)(PetscScalar*, PetscScalar), PetscBool setBC, const PetscInt perm[], const PetscScalar values[], PetscInt *offset, PetscScalar array[])
3862a6dfd86eSKarl Rupp {
3863552f7358SJed Brown   PetscScalar    *a;
38641a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
38651a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
38661a271a75SMatthew G. Knepley   PetscInt        cind = 0, k, c;
3867552f7358SJed Brown   PetscErrorCode  ierr;
3868552f7358SJed Brown 
3869552f7358SJed Brown   PetscFunctionBegin;
3870552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
3871552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
38721a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
38731a271a75SMatthew G. Knepley   a    = &array[foff];
3874552f7358SJed Brown   if (!fcdof || setBC) {
38751a271a75SMatthew G. Knepley     if (o >= 0) {
38768f3be42fSMatthew G. Knepley       for (k = 0; k < fdof; ++k) fuse(&a[k], values[perm ? perm[foffset+k] : foffset+k]);
3877552f7358SJed Brown     } else {
3878552f7358SJed Brown       for (k = fdof/fcomp-1; k >= 0; --k) {
3879552f7358SJed Brown         for (c = 0; c < fcomp; ++c) {
38808f3be42fSMatthew G. Knepley           fuse(&a[(fdof/fcomp-1-k)*fcomp+c], values[perm ? perm[foffset+k*fcomp+c] : foffset+k*fcomp+c]);
3881552f7358SJed Brown         }
3882552f7358SJed Brown       }
3883552f7358SJed Brown     }
3884552f7358SJed Brown   } else {
3885552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
38861a271a75SMatthew G. Knepley     if (o >= 0) {
3887552f7358SJed Brown       for (k = 0; k < fdof; ++k) {
3888552f7358SJed Brown         if ((cind < fcdof) && (k == fcdofs[cind])) {++cind; continue;}
38898f3be42fSMatthew G. Knepley         fuse(&a[k], values[perm ? perm[foffset+k] : foffset+k]);
3890552f7358SJed Brown       }
3891552f7358SJed Brown     } else {
3892552f7358SJed Brown       for (k = fdof/fcomp-1; k >= 0; --k) {
3893552f7358SJed Brown         for (c = 0; c < fcomp; ++c) {
3894552f7358SJed Brown           if ((cind < fcdof) && (k*fcomp+c == fcdofs[cind])) {++cind; continue;}
38958f3be42fSMatthew G. Knepley           fuse(&a[(fdof/fcomp-1-k)*fcomp+c], values[perm ? perm[foffset+k*fcomp+c] : foffset+k*fcomp+c]);
3896552f7358SJed Brown         }
3897552f7358SJed Brown       }
3898552f7358SJed Brown     }
3899552f7358SJed Brown   }
39001a271a75SMatthew G. Knepley   *offset += fdof;
3901552f7358SJed Brown   PetscFunctionReturn(0);
3902552f7358SJed Brown }
3903552f7358SJed Brown 
3904552f7358SJed Brown #undef __FUNCT__
3905a5e93ea8SMatthew G. Knepley #define __FUNCT__ "updatePointFieldsBC_private"
39068f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode updatePointFieldsBC_private(PetscSection section, PetscInt point, PetscInt o, PetscInt f, PetscInt fcomp, void (*fuse)(PetscScalar*, PetscScalar), const PetscInt perm[], const PetscScalar values[], PetscInt *offset, PetscScalar array[])
3907a5e93ea8SMatthew G. Knepley {
3908a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
39091a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
39101a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
39111a271a75SMatthew G. Knepley   PetscInt        cind = 0, k, c;
3912a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
3913a5e93ea8SMatthew G. Knepley 
3914a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
3915a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
3916a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
39171a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
39181a271a75SMatthew G. Knepley   a    = &array[foff];
3919a5e93ea8SMatthew G. Knepley   if (fcdof) {
3920a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
39211a271a75SMatthew G. Knepley     if (o >= 0) {
3922a5e93ea8SMatthew G. Knepley       for (k = 0; k < fdof; ++k) {
3923a5e93ea8SMatthew G. Knepley         if ((cind < fcdof) && (k == fcdofs[cind])) {
39248f3be42fSMatthew G. Knepley           fuse(&a[k], values[perm ? perm[foffset+k] : foffset+k]);
3925a5e93ea8SMatthew G. Knepley           ++cind;
3926a5e93ea8SMatthew G. Knepley         }
3927a5e93ea8SMatthew G. Knepley       }
3928a5e93ea8SMatthew G. Knepley     } else {
3929a5e93ea8SMatthew G. Knepley       for (k = fdof/fcomp-1; k >= 0; --k) {
3930a5e93ea8SMatthew G. Knepley         for (c = 0; c < fcomp; ++c) {
3931a5e93ea8SMatthew G. Knepley           if ((cind < fcdof) && (k*fcomp+c == fcdofs[cind])) {
39328f3be42fSMatthew G. Knepley             fuse(&a[(fdof/fcomp-1-k)*fcomp+c], values[perm ? perm[foffset+k*fcomp+c] : foffset+k*fcomp+c]);
3933a5e93ea8SMatthew G. Knepley             ++cind;
3934a5e93ea8SMatthew G. Knepley           }
3935a5e93ea8SMatthew G. Knepley         }
3936a5e93ea8SMatthew G. Knepley       }
3937a5e93ea8SMatthew G. Knepley     }
3938a5e93ea8SMatthew G. Knepley   }
39391a271a75SMatthew G. Knepley   *offset += fdof;
3940a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
3941a5e93ea8SMatthew G. Knepley }
3942a5e93ea8SMatthew G. Knepley 
3943a5e93ea8SMatthew G. Knepley #undef __FUNCT__
39448f3be42fSMatthew G. Knepley #define __FUNCT__ "DMPlexVecSetClosure_Depth1_Static"
39458f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
3946a6dfd86eSKarl Rupp {
3947552f7358SJed Brown   PetscScalar    *array;
39481b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
39491b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
3950552f7358SJed Brown   PetscErrorCode  ierr;
3951552f7358SJed Brown 
39521b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
3953b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3954b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
3955b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
3956b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
3957b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
3958b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
3959b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
3960b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
3961b6ebb6e6SMatthew G. Knepley 
3962b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
3963b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
3964b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
3965b6ebb6e6SMatthew G. Knepley     {
3966b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
3967b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
3968b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
3969b6ebb6e6SMatthew G. Knepley 
3970b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
3971b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
3972b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
3973b6ebb6e6SMatthew G. Knepley       if (!cdof) {
3974b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
3975b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
3976b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
3977b6ebb6e6SMatthew G. Knepley           }
3978b6ebb6e6SMatthew G. Knepley         } else {
3979b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
3980b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
3981b6ebb6e6SMatthew G. Knepley           }
3982b6ebb6e6SMatthew G. Knepley         }
3983b6ebb6e6SMatthew G. Knepley       } else {
3984b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
3985b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
3986b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
3987b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
3988b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
3989b6ebb6e6SMatthew G. Knepley           }
3990b6ebb6e6SMatthew G. Knepley         } else {
3991b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
3992b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
3993b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
3994b6ebb6e6SMatthew G. Knepley           }
3995b6ebb6e6SMatthew G. Knepley         }
3996b6ebb6e6SMatthew G. Knepley       }
3997b6ebb6e6SMatthew G. Knepley     }
3998b6ebb6e6SMatthew G. Knepley   }
3999b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4000b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4001b6ebb6e6SMatthew G. Knepley }
40021b406b76SMatthew G. Knepley 
40031b406b76SMatthew G. Knepley #undef __FUNCT__
40041b406b76SMatthew G. Knepley #define __FUNCT__ "DMPlexVecSetClosure"
40051b406b76SMatthew G. Knepley /*@C
40061b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
40071b406b76SMatthew G. Knepley 
40081b406b76SMatthew G. Knepley   Not collective
40091b406b76SMatthew G. Knepley 
40101b406b76SMatthew G. Knepley   Input Parameters:
40111b406b76SMatthew G. Knepley + dm - The DM
40121b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
40131b406b76SMatthew G. Knepley . v - The local vector
40141b406b76SMatthew G. Knepley . point - The sieve point in the DM
40151b406b76SMatthew G. Knepley . values - The array of values
40161b406b76SMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
40171b406b76SMatthew G. Knepley 
40181b406b76SMatthew G. Knepley   Fortran Notes:
40191b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
40201b406b76SMatthew G. Knepley 
40211b406b76SMatthew G. Knepley   Level: intermediate
40221b406b76SMatthew G. Knepley 
40231b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
40241b406b76SMatthew G. Knepley @*/
40251b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
40261b406b76SMatthew G. Knepley {
40271b406b76SMatthew G. Knepley   PetscSection    clSection;
40281b406b76SMatthew G. Knepley   IS              clPoints;
40291b406b76SMatthew G. Knepley   PetscScalar    *array;
40301b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
40318f3be42fSMatthew G. Knepley   const PetscInt *clp, *perm;
40321a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
40331b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
40341b406b76SMatthew G. Knepley 
40351a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
40361b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40371b406b76SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
40381a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
40391a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
40401b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
40411b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
40421b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
40438f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
40441b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
40451b406b76SMatthew G. Knepley   }
40461a271a75SMatthew G. Knepley   /* Get points */
40471fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
40481b406b76SMatthew G. Knepley   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, &clSection, &clPoints);CHKERRQ(ierr);
40491b406b76SMatthew G. Knepley   if (!clPoints) {
40501a271a75SMatthew G. Knepley     PetscInt pStart, pEnd, q;
40511a271a75SMatthew G. Knepley 
40521a271a75SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4053552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
4054552f7358SJed Brown     /* Compress out points not in the section */
4055552f7358SJed Brown     for (p = 0, q = 0; p < numPoints*2; p += 2) {
4056552f7358SJed Brown       if ((points[p] >= pStart) && (points[p] < pEnd)) {
4057552f7358SJed Brown         points[q*2]   = points[p];
4058552f7358SJed Brown         points[q*2+1] = points[p+1];
4059552f7358SJed Brown         ++q;
4060552f7358SJed Brown       }
4061552f7358SJed Brown     }
4062552f7358SJed Brown     numPoints = q;
40631b406b76SMatthew G. Knepley   } else {
40641b406b76SMatthew G. Knepley     PetscInt dof, off;
4065552f7358SJed Brown 
40661b406b76SMatthew G. Knepley     ierr = PetscSectionGetDof(clSection, point, &dof);CHKERRQ(ierr);
40671b406b76SMatthew G. Knepley     ierr = PetscSectionGetOffset(clSection, point, &off);CHKERRQ(ierr);
40681b406b76SMatthew G. Knepley     ierr = ISGetIndices(clPoints, &clp);CHKERRQ(ierr);
40691a271a75SMatthew G. Knepley     numPoints = dof/2;
40701b406b76SMatthew G. Knepley     points    = (PetscInt *) &clp[off];
4071552f7358SJed Brown   }
40721a271a75SMatthew G. Knepley   /* Get array */
4073552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
40741a271a75SMatthew G. Knepley   /* Get values */
4075ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
40761a271a75SMatthew G. Knepley     PetscInt offset = 0, fcomp, f;
4077552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
40781a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &fcomp);CHKERRQ(ierr);
4079552f7358SJed Brown       switch (mode) {
4080552f7358SJed Brown       case INSERT_VALUES:
4081552f7358SJed Brown         for (p = 0; p < numPoints*2; p += 2) {
40821a271a75SMatthew G. Knepley           const PetscInt point = points[p];
40831a271a75SMatthew G. Knepley           const PetscInt o     = points[p+1];
40848f3be42fSMatthew G. Knepley           updatePointFields_private(section, point, o, f, fcomp, insert, PETSC_FALSE, perm, values, &offset, array);
4085552f7358SJed Brown         } break;
4086552f7358SJed Brown       case INSERT_ALL_VALUES:
4087552f7358SJed Brown         for (p = 0; p < numPoints*2; p += 2) {
40881a271a75SMatthew G. Knepley           const PetscInt point = points[p];
40891a271a75SMatthew G. Knepley           const PetscInt o     = points[p+1];
40908f3be42fSMatthew G. Knepley           updatePointFields_private(section, point, o, f, fcomp, insert, PETSC_TRUE, perm, values, &offset, array);
4091552f7358SJed Brown         } break;
4092a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
4093a5e93ea8SMatthew G. Knepley         for (p = 0; p < numPoints*2; p += 2) {
40941a271a75SMatthew G. Knepley           const PetscInt point = points[p];
40951a271a75SMatthew G. Knepley           const PetscInt o     = points[p+1];
40968f3be42fSMatthew G. Knepley           updatePointFieldsBC_private(section, point, o, f, fcomp, insert, perm, values, &offset, array);
4097a5e93ea8SMatthew G. Knepley         } break;
4098552f7358SJed Brown       case ADD_VALUES:
4099552f7358SJed Brown         for (p = 0; p < numPoints*2; p += 2) {
41001a271a75SMatthew G. Knepley           const PetscInt point = points[p];
41011a271a75SMatthew G. Knepley           const PetscInt o     = points[p+1];
41028f3be42fSMatthew G. Knepley           updatePointFields_private(section, point, o, f, fcomp, add, PETSC_FALSE, perm, values, &offset, array);
4103552f7358SJed Brown         } break;
4104552f7358SJed Brown       case ADD_ALL_VALUES:
4105552f7358SJed Brown         for (p = 0; p < numPoints*2; p += 2) {
41061a271a75SMatthew G. Knepley           const PetscInt point = points[p];
41071a271a75SMatthew G. Knepley           const PetscInt o     = points[p+1];
41088f3be42fSMatthew G. Knepley           updatePointFields_private(section, point, o, f, fcomp, add, PETSC_TRUE, perm, values, &offset, array);
4109552f7358SJed Brown         } break;
4110304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
4111304ab55fSMatthew G. Knepley         for (p = 0; p < numPoints*2; p += 2) {
4112304ab55fSMatthew G. Knepley           const PetscInt point = points[p];
4113304ab55fSMatthew G. Knepley           const PetscInt o     = points[p+1];
41148f3be42fSMatthew G. Knepley           updatePointFieldsBC_private(section, point, o, f, fcomp, add, perm, values, &offset, array);
4115304ab55fSMatthew G. Knepley         } break;
4116552f7358SJed Brown       default:
4117f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4118552f7358SJed Brown       }
41191a271a75SMatthew G. Knepley     }
4120552f7358SJed Brown   } else {
41211a271a75SMatthew G. Knepley     PetscInt dof, off;
41221a271a75SMatthew G. Knepley 
4123552f7358SJed Brown     switch (mode) {
4124552f7358SJed Brown     case INSERT_VALUES:
4125552f7358SJed Brown       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
4126552f7358SJed Brown         PetscInt o = points[p+1];
4127552f7358SJed Brown         ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
41288f3be42fSMatthew G. Knepley         updatePoint_private(section, points[p], dof, insert, PETSC_FALSE, o, perm, values, off, array);
4129552f7358SJed Brown       } break;
4130552f7358SJed Brown     case INSERT_ALL_VALUES:
4131552f7358SJed Brown       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
4132552f7358SJed Brown         PetscInt o = points[p+1];
4133552f7358SJed Brown         ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
41348f3be42fSMatthew G. Knepley         updatePoint_private(section, points[p], dof, insert, PETSC_TRUE,  o, perm, values, off, array);
4135552f7358SJed Brown       } break;
4136a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
4137a5e93ea8SMatthew G. Knepley       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
4138a5e93ea8SMatthew G. Knepley         PetscInt o = points[p+1];
4139a5e93ea8SMatthew G. Knepley         ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
41408f3be42fSMatthew G. Knepley         updatePointBC_private(section, points[p], dof, insert,  o, perm, values, off, array);
4141a5e93ea8SMatthew G. Knepley       } break;
4142552f7358SJed Brown     case ADD_VALUES:
4143552f7358SJed Brown       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
4144552f7358SJed Brown         PetscInt o = points[p+1];
4145552f7358SJed Brown         ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
41468f3be42fSMatthew G. Knepley         updatePoint_private(section, points[p], dof, add,    PETSC_FALSE, o, perm, values, off, array);
4147552f7358SJed Brown       } break;
4148552f7358SJed Brown     case ADD_ALL_VALUES:
4149552f7358SJed Brown       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
4150552f7358SJed Brown         PetscInt o = points[p+1];
4151552f7358SJed Brown         ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
41528f3be42fSMatthew G. Knepley         updatePoint_private(section, points[p], dof, add,    PETSC_TRUE,  o, perm, values, off, array);
4153552f7358SJed Brown       } break;
4154304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
4155304ab55fSMatthew G. Knepley       for (p = 0, off = 0; p < numPoints*2; p += 2, off += dof) {
4156304ab55fSMatthew G. Knepley         PetscInt o = points[p+1];
4157304ab55fSMatthew G. Knepley         ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
41588f3be42fSMatthew G. Knepley         updatePointBC_private(section, points[p], dof, add,  o, perm, values, off, array);
4159304ab55fSMatthew G. Knepley       } break;
4160552f7358SJed Brown     default:
4161f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4162552f7358SJed Brown     }
4163552f7358SJed Brown   }
41641a271a75SMatthew G. Knepley   /* Cleanup points */
41651a271a75SMatthew G. Knepley   if (!clPoints) {ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);}
41661a271a75SMatthew G. Knepley   else           {ierr = ISRestoreIndices(clPoints, &clp);CHKERRQ(ierr);}
41671a271a75SMatthew G. Knepley   /* Cleanup array */
4168552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4169552f7358SJed Brown   PetscFunctionReturn(0);
4170552f7358SJed Brown }
4171552f7358SJed Brown 
4172552f7358SJed Brown #undef __FUNCT__
4173e07394fbSMatthew G. Knepley #define __FUNCT__ "DMPlexVecSetFieldClosure_Internal"
4174e07394fbSMatthew G. Knepley PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, const PetscScalar values[], InsertMode mode)
4175e07394fbSMatthew G. Knepley {
4176e07394fbSMatthew G. Knepley   PetscSection    clSection;
4177e07394fbSMatthew G. Knepley   IS              clPoints;
4178e07394fbSMatthew G. Knepley   PetscScalar    *array;
4179e07394fbSMatthew G. Knepley   PetscInt       *points = NULL;
41808f3be42fSMatthew G. Knepley   const PetscInt *clp, *perm;
4181e07394fbSMatthew G. Knepley   PetscInt        numFields, numPoints, p;
4182e07394fbSMatthew G. Knepley   PetscInt        offset = 0, fcomp, f;
4183e07394fbSMatthew G. Knepley   PetscErrorCode  ierr;
4184e07394fbSMatthew G. Knepley 
4185e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
4186e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4187e07394fbSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
4188e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4189e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4190e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4191e07394fbSMatthew G. Knepley   /* Get points */
41921fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
4193e07394fbSMatthew G. Knepley   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, &clSection, &clPoints);CHKERRQ(ierr);
4194e07394fbSMatthew G. Knepley   if (!clPoints) {
4195e07394fbSMatthew G. Knepley     PetscInt pStart, pEnd, q;
4196e07394fbSMatthew G. Knepley 
4197e07394fbSMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4198e07394fbSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
4199e07394fbSMatthew G. Knepley     /* Compress out points not in the section */
4200e07394fbSMatthew G. Knepley     for (p = 0, q = 0; p < numPoints*2; p += 2) {
4201e07394fbSMatthew G. Knepley       if ((points[p] >= pStart) && (points[p] < pEnd)) {
4202e07394fbSMatthew G. Knepley         points[q*2]   = points[p];
4203e07394fbSMatthew G. Knepley         points[q*2+1] = points[p+1];
4204e07394fbSMatthew G. Knepley         ++q;
4205e07394fbSMatthew G. Knepley       }
4206e07394fbSMatthew G. Knepley     }
4207e07394fbSMatthew G. Knepley     numPoints = q;
4208e07394fbSMatthew G. Knepley   } else {
4209e07394fbSMatthew G. Knepley     PetscInt dof, off;
4210e07394fbSMatthew G. Knepley 
4211e07394fbSMatthew G. Knepley     ierr = PetscSectionGetDof(clSection, point, &dof);CHKERRQ(ierr);
4212e07394fbSMatthew G. Knepley     ierr = PetscSectionGetOffset(clSection, point, &off);CHKERRQ(ierr);
4213e07394fbSMatthew G. Knepley     ierr = ISGetIndices(clPoints, &clp);CHKERRQ(ierr);
4214e07394fbSMatthew G. Knepley     numPoints = dof/2;
4215e07394fbSMatthew G. Knepley     points    = (PetscInt *) &clp[off];
4216e07394fbSMatthew G. Knepley   }
4217e07394fbSMatthew G. Knepley   /* Get array */
4218e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4219e07394fbSMatthew G. Knepley   /* Get values */
4220e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
4221e07394fbSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &fcomp);CHKERRQ(ierr);
4222e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
4223e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4224e07394fbSMatthew G. Knepley         PetscInt fdof;
4225e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
4226e07394fbSMatthew G. Knepley         offset += fdof;
4227e07394fbSMatthew G. Knepley       }
4228e07394fbSMatthew G. Knepley       continue;
4229e07394fbSMatthew G. Knepley     }
4230e07394fbSMatthew G. Knepley     switch (mode) {
4231e07394fbSMatthew G. Knepley     case INSERT_VALUES:
4232e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4233e07394fbSMatthew G. Knepley         const PetscInt point = points[p];
4234e07394fbSMatthew G. Knepley         const PetscInt o     = points[p+1];
42358f3be42fSMatthew G. Knepley         updatePointFields_private(section, point, o, f, fcomp, insert, PETSC_FALSE, perm, values, &offset, array);
4236e07394fbSMatthew G. Knepley       } break;
4237e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
4238e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4239e07394fbSMatthew G. Knepley         const PetscInt point = points[p];
4240e07394fbSMatthew G. Knepley         const PetscInt o     = points[p+1];
42418f3be42fSMatthew G. Knepley         updatePointFields_private(section, point, o, f, fcomp, insert, PETSC_TRUE, perm, values, &offset, array);
4242e07394fbSMatthew G. Knepley         } break;
4243e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
4244e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4245e07394fbSMatthew G. Knepley         const PetscInt point = points[p];
4246e07394fbSMatthew G. Knepley         const PetscInt o     = points[p+1];
42478f3be42fSMatthew G. Knepley         updatePointFieldsBC_private(section, point, o, f, fcomp, insert, perm, values, &offset, array);
4248e07394fbSMatthew G. Knepley       } break;
4249e07394fbSMatthew G. Knepley     case ADD_VALUES:
4250e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4251e07394fbSMatthew G. Knepley         const PetscInt point = points[p];
4252e07394fbSMatthew G. Knepley         const PetscInt o     = points[p+1];
42538f3be42fSMatthew G. Knepley         updatePointFields_private(section, point, o, f, fcomp, add, PETSC_FALSE, perm, values, &offset, array);
4254e07394fbSMatthew G. Knepley       } break;
4255e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
4256e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4257e07394fbSMatthew G. Knepley         const PetscInt point = points[p];
4258e07394fbSMatthew G. Knepley         const PetscInt o     = points[p+1];
42598f3be42fSMatthew G. Knepley         updatePointFields_private(section, point, o, f, fcomp, add, PETSC_TRUE, perm, values, &offset, array);
4260e07394fbSMatthew G. Knepley       } break;
4261e07394fbSMatthew G. Knepley     default:
4262f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4263e07394fbSMatthew G. Knepley     }
4264e07394fbSMatthew G. Knepley   }
4265e07394fbSMatthew G. Knepley   /* Cleanup points */
4266e07394fbSMatthew G. Knepley   if (!clPoints) {ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);}
4267e07394fbSMatthew G. Knepley   else           {ierr = ISRestoreIndices(clPoints, &clp);CHKERRQ(ierr);}
4268e07394fbSMatthew G. Knepley   /* Cleanup array */
4269e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4270e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
4271e07394fbSMatthew G. Knepley }
4272e07394fbSMatthew G. Knepley 
4273e07394fbSMatthew G. Knepley #undef __FUNCT__
4274552f7358SJed Brown #define __FUNCT__ "DMPlexPrintMatSetValues"
4275b0ecff45SMatthew G. Knepley PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
4276552f7358SJed Brown {
4277552f7358SJed Brown   PetscMPIInt    rank;
4278552f7358SJed Brown   PetscInt       i, j;
4279552f7358SJed Brown   PetscErrorCode ierr;
4280552f7358SJed Brown 
4281552f7358SJed Brown   PetscFunctionBegin;
428282f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
4283f347f43bSBarry Smith   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for sieve point %D\n", rank, point);CHKERRQ(ierr);
4284e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
4285e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
4286b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
4287b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
4288e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
4289b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
4290519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
42917eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
4292552f7358SJed Brown #else
4293b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
4294552f7358SJed Brown #endif
4295552f7358SJed Brown     }
429677a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
4297552f7358SJed Brown   }
4298552f7358SJed Brown   PetscFunctionReturn(0);
4299552f7358SJed Brown }
4300552f7358SJed Brown 
4301552f7358SJed Brown #undef __FUNCT__
4302415ce65aSMatthew G. Knepley #define __FUNCT__ "DMPlexGetIndicesPoint_Internal"
4303552f7358SJed Brown /* . off - The global offset of this point */
4304415ce65aSMatthew G. Knepley PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, PetscInt orientation, PetscInt indices[])
4305a6dfd86eSKarl Rupp {
4306e6ccafaeSMatthew G Knepley   PetscInt        dof;    /* The number of unknowns on this point */
4307552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4308552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4309552f7358SJed Brown   PetscInt        cind = 0, k;
4310552f7358SJed Brown   PetscErrorCode  ierr;
4311552f7358SJed Brown 
4312552f7358SJed Brown   PetscFunctionBegin;
4313552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
4314552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4315552f7358SJed Brown   if (!cdof || setBC) {
4316552f7358SJed Brown     if (orientation >= 0) {
4317e6ccafaeSMatthew G Knepley       for (k = 0; k < dof; ++k) indices[*loff+k] = off+k;
4318552f7358SJed Brown     } else {
4319e6ccafaeSMatthew G Knepley       for (k = 0; k < dof; ++k) indices[*loff+dof-k-1] = off+k;
4320552f7358SJed Brown     }
4321552f7358SJed Brown   } else {
4322552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
4323552f7358SJed Brown     if (orientation >= 0) {
4324552f7358SJed Brown       for (k = 0; k < dof; ++k) {
4325552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
4326552f7358SJed Brown           /* Insert check for returning constrained indices */
4327e6ccafaeSMatthew G Knepley           indices[*loff+k] = -(off+k+1);
4328552f7358SJed Brown           ++cind;
4329552f7358SJed Brown         } else {
4330e6ccafaeSMatthew G Knepley           indices[*loff+k] = off+k-cind;
4331552f7358SJed Brown         }
4332552f7358SJed Brown       }
4333552f7358SJed Brown     } else {
4334552f7358SJed Brown       for (k = 0; k < dof; ++k) {
4335552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
4336552f7358SJed Brown           /* Insert check for returning constrained indices */
4337e6ccafaeSMatthew G Knepley           indices[*loff+dof-k-1] = -(off+k+1);
4338552f7358SJed Brown           ++cind;
4339552f7358SJed Brown         } else {
4340e6ccafaeSMatthew G Knepley           indices[*loff+dof-k-1] = off+k-cind;
4341552f7358SJed Brown         }
4342552f7358SJed Brown       }
4343552f7358SJed Brown     }
4344552f7358SJed Brown   }
4345e6ccafaeSMatthew G Knepley   *loff += dof;
4346552f7358SJed Brown   PetscFunctionReturn(0);
4347552f7358SJed Brown }
4348552f7358SJed Brown 
4349552f7358SJed Brown #undef __FUNCT__
4350415ce65aSMatthew G. Knepley #define __FUNCT__ "DMPlexGetIndicesPointFields_Internal"
4351552f7358SJed Brown /* . off - The global offset of this point */
4352415ce65aSMatthew G. Knepley PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, PetscInt orientation, PetscInt indices[])
4353a6dfd86eSKarl Rupp {
4354552f7358SJed Brown   PetscInt       numFields, foff, f;
4355552f7358SJed Brown   PetscErrorCode ierr;
4356552f7358SJed Brown 
4357552f7358SJed Brown   PetscFunctionBegin;
4358552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4359552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
4360552f7358SJed Brown     PetscInt        fdof, fcomp, cfdof;
4361552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
4362552f7358SJed Brown     PetscInt        cind = 0, k, c;
4363552f7358SJed Brown 
4364552f7358SJed Brown     ierr = PetscSectionGetFieldComponents(section, f, &fcomp);CHKERRQ(ierr);
4365552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4366552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
4367552f7358SJed Brown     if (!cfdof || setBC) {
4368552f7358SJed Brown       if (orientation >= 0) {
43690d644c17SKarl Rupp         for (k = 0; k < fdof; ++k) indices[foffs[f]+k] = off+foff+k;
4370552f7358SJed Brown       } else {
4371552f7358SJed Brown         for (k = fdof/fcomp-1; k >= 0; --k) {
4372552f7358SJed Brown           for (c = 0; c < fcomp; ++c) {
4373552f7358SJed Brown             indices[foffs[f]+k*fcomp+c] = off+foff+(fdof/fcomp-1-k)*fcomp+c;
4374552f7358SJed Brown           }
4375552f7358SJed Brown         }
4376552f7358SJed Brown       }
4377552f7358SJed Brown     } else {
4378552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
4379552f7358SJed Brown       if (orientation >= 0) {
4380552f7358SJed Brown         for (k = 0; k < fdof; ++k) {
4381552f7358SJed Brown           if ((cind < cfdof) && (k == fcdofs[cind])) {
4382552f7358SJed Brown             indices[foffs[f]+k] = -(off+foff+k+1);
4383552f7358SJed Brown             ++cind;
4384552f7358SJed Brown           } else {
4385552f7358SJed Brown             indices[foffs[f]+k] = off+foff+k-cind;
4386552f7358SJed Brown           }
4387552f7358SJed Brown         }
4388552f7358SJed Brown       } else {
4389552f7358SJed Brown         for (k = fdof/fcomp-1; k >= 0; --k) {
4390552f7358SJed Brown           for (c = 0; c < fcomp; ++c) {
4391552f7358SJed Brown             if ((cind < cfdof) && ((fdof/fcomp-1-k)*fcomp+c == fcdofs[cind])) {
4392552f7358SJed Brown               indices[foffs[f]+k*fcomp+c] = -(off+foff+(fdof/fcomp-1-k)*fcomp+c+1);
4393552f7358SJed Brown               ++cind;
4394552f7358SJed Brown             } else {
4395552f7358SJed Brown               indices[foffs[f]+k*fcomp+c] = off+foff+(fdof/fcomp-1-k)*fcomp+c-cind;
4396552f7358SJed Brown             }
4397552f7358SJed Brown           }
4398552f7358SJed Brown         }
4399552f7358SJed Brown       }
4400552f7358SJed Brown     }
4401a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
4402552f7358SJed Brown     foffs[f] += fdof;
4403552f7358SJed Brown   }
4404552f7358SJed Brown   PetscFunctionReturn(0);
4405552f7358SJed Brown }
4406552f7358SJed Brown 
4407552f7358SJed Brown #undef __FUNCT__
4408f7c74593SToby Isaac #define __FUNCT__ "DMPlexAnchorsModifyMat"
44096ecaa68aSToby Isaac PetscErrorCode DMPlexAnchorsModifyMat(DM dm, PetscSection section, PetscInt numPoints, PetscInt numIndices, const PetscInt points[], const PetscScalar values[], PetscInt *outNumPoints, PetscInt *outNumIndices, PetscInt *outPoints[], PetscScalar *outValues[], PetscInt offsets[], PetscBool multiplyLeft)
4410d3d1a6afSToby Isaac {
4411d3d1a6afSToby Isaac   Mat             cMat;
4412d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
4413d3d1a6afSToby Isaac   IS              aIS;
4414d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
4415d3d1a6afSToby Isaac   const PetscInt  *anchors;
4416e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
4417d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
4418d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
4419d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
4420d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
4421d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
4422d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
4423d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
44246ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
4425d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
4426d3d1a6afSToby Isaac   PetscErrorCode  ierr;
4427d3d1a6afSToby Isaac 
4428d3d1a6afSToby Isaac   PetscFunctionBegin;
4429d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4430d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4431d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4432d3d1a6afSToby Isaac 
4433a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
4434d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
4435d3d1a6afSToby Isaac   if (aSec) {
4436d3d1a6afSToby Isaac     ierr = PetscMemzero(newOffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
4437d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
4438d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
4439d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
4440d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
4441d3d1a6afSToby Isaac      * into the global matrix anyway) */
4442d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
4443d3d1a6afSToby Isaac       PetscInt b    = points[p];
44444b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4445d3d1a6afSToby Isaac 
44464b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
44474b2f2278SToby Isaac       if (!bSecDof) {
44484b2f2278SToby Isaac         continue;
44494b2f2278SToby Isaac       }
4450d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4451d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
4452d3d1a6afSToby Isaac       }
4453d3d1a6afSToby Isaac       if (bDof) {
4454d3d1a6afSToby Isaac         /* this point is constrained */
4455d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
4456d3d1a6afSToby Isaac         PetscInt bOff, q;
4457d3d1a6afSToby Isaac 
4458d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
4459d3d1a6afSToby Isaac         newNumPoints  += bDof;
4460d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
4461d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4462d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
4463d3d1a6afSToby Isaac           PetscInt aDof;
4464d3d1a6afSToby Isaac 
4465d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
4466d3d1a6afSToby Isaac           newNumIndices += aDof;
4467d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
4468d3d1a6afSToby Isaac             PetscInt fDof;
4469d3d1a6afSToby Isaac 
4470d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
4471d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
4472d3d1a6afSToby Isaac           }
4473d3d1a6afSToby Isaac         }
4474d3d1a6afSToby Isaac       }
4475d3d1a6afSToby Isaac       else {
4476d3d1a6afSToby Isaac         /* this point is not constrained */
4477d3d1a6afSToby Isaac         newNumPoints++;
44784b2f2278SToby Isaac         newNumIndices += bSecDof;
4479d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
4480d3d1a6afSToby Isaac           PetscInt fDof;
4481d3d1a6afSToby Isaac 
4482d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4483d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
4484d3d1a6afSToby Isaac         }
4485d3d1a6afSToby Isaac       }
4486d3d1a6afSToby Isaac     }
4487d3d1a6afSToby Isaac   }
4488d3d1a6afSToby Isaac   if (!anyConstrained) {
448972b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
449072b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
449172b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
449272b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
449372b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
4494d3d1a6afSToby Isaac     PetscFunctionReturn(0);
4495d3d1a6afSToby Isaac   }
4496d3d1a6afSToby Isaac 
44976ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
44986ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
44996ecaa68aSToby Isaac 
4500f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
4501d3d1a6afSToby Isaac 
45026ecaa68aSToby Isaac   if (!outPoints && !outValues) {
45036ecaa68aSToby Isaac     if (offsets) {
45046ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
45056ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
45066ecaa68aSToby Isaac       }
45076ecaa68aSToby Isaac     }
45086ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
45096ecaa68aSToby Isaac     PetscFunctionReturn(0);
45106ecaa68aSToby Isaac   }
45116ecaa68aSToby Isaac 
4512f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
4513d3d1a6afSToby Isaac 
4514f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
4515d3d1a6afSToby Isaac 
4516d3d1a6afSToby Isaac   /* workspaces */
4517d3d1a6afSToby Isaac   if (numFields) {
4518d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
4519d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
4520d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
4521d3d1a6afSToby Isaac     }
4522d3d1a6afSToby Isaac   }
4523d3d1a6afSToby Isaac   else {
4524d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
4525d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
4526d3d1a6afSToby Isaac   }
4527d3d1a6afSToby Isaac 
4528d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
4529d3d1a6afSToby Isaac   if (numFields) {
45304b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
45314b2f2278SToby Isaac 
4532d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4533d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
45344b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4535d3d1a6afSToby Isaac 
45364b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
45374b2f2278SToby Isaac       if (!bSecDof) {
45384b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
45394b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
45404b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
45414b2f2278SToby Isaac         }
45424b2f2278SToby Isaac         continue;
45434b2f2278SToby Isaac       }
4544d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4545d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4546d3d1a6afSToby Isaac       }
4547d3d1a6afSToby Isaac       if (bDof) {
4548d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4549d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
4550d3d1a6afSToby Isaac 
4551d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4552d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4553d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
4554d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
4555d3d1a6afSToby Isaac             PetscInt aFDof;
4556d3d1a6afSToby Isaac 
4557d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
4558d3d1a6afSToby Isaac             allFDof += aFDof;
4559d3d1a6afSToby Isaac           }
4560d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
4561d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
4562d3d1a6afSToby Isaac         }
4563d3d1a6afSToby Isaac       }
4564d3d1a6afSToby Isaac       else {
4565d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4566d3d1a6afSToby Isaac           PetscInt fDof;
4567d3d1a6afSToby Isaac 
4568d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4569d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
4570d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
4571d3d1a6afSToby Isaac         }
4572d3d1a6afSToby Isaac       }
4573d3d1a6afSToby Isaac     }
45744b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
45754b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
45764b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
4577d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
4578d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
4579d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
4580d3d1a6afSToby Isaac       }
458119f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
458219f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
4583d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
4584d3d1a6afSToby Isaac     }
4585d3d1a6afSToby Isaac   }
4586d3d1a6afSToby Isaac   else {
4587d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4588d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
45894b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4590d3d1a6afSToby Isaac 
45914b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
45924b2f2278SToby Isaac       if (!bSecDof) {
45934b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
45944b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
45954b2f2278SToby Isaac         continue;
45964b2f2278SToby Isaac       }
4597d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4598d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4599d3d1a6afSToby Isaac       }
4600d3d1a6afSToby Isaac       if (bDof) {
46014b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
4602d3d1a6afSToby Isaac 
4603d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4604d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4605d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
4606d3d1a6afSToby Isaac 
4607d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
4608d3d1a6afSToby Isaac           allDof += aDof;
4609d3d1a6afSToby Isaac         }
4610d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
46114b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
4612d3d1a6afSToby Isaac       }
4613d3d1a6afSToby Isaac       else {
46144b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
4615d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
4616d3d1a6afSToby Isaac       }
4617d3d1a6afSToby Isaac     }
4618d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
4619d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
4620d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4621d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
4622d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
4623d3d1a6afSToby Isaac     }
4624d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
4625d3d1a6afSToby Isaac   }
4626d3d1a6afSToby Isaac 
46276ecaa68aSToby Isaac   /* output arrays */
46286ecaa68aSToby Isaac   ierr = DMGetWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
46296ecaa68aSToby Isaac 
4630d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
4631d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
4632d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
4633d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
4634d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
4635d3d1a6afSToby Isaac   if (numFields) {
4636d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
4637d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4638d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
46394b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4640d3d1a6afSToby Isaac 
46414b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
46424b2f2278SToby Isaac       if (!bSecDof) {
46434b2f2278SToby Isaac         continue;
46444b2f2278SToby Isaac       }
4645d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4646d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4647d3d1a6afSToby Isaac       }
4648d3d1a6afSToby Isaac       if (bDof) {
4649d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
4650d3d1a6afSToby Isaac 
4651d3d1a6afSToby Isaac         fStart[0] = 0;
4652d3d1a6afSToby Isaac         fEnd[0]   = 0;
4653d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4654d3d1a6afSToby Isaac           PetscInt fDof;
4655d3d1a6afSToby Isaac 
4656d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
4657d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
4658d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
4659d3d1a6afSToby Isaac         }
4660d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
4661415ce65aSMatthew G. Knepley         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, o, indices);CHKERRQ(ierr);
4662d3d1a6afSToby Isaac 
4663d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
4664d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
4665d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4666d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
4667d3d1a6afSToby Isaac 
4668d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
4669d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
4670d3d1a6afSToby Isaac         }
4671d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4672d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4673d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
4674d3d1a6afSToby Isaac 
4675d3d1a6afSToby 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 */
4676d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
4677d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
4678302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
4679415ce65aSMatthew G. Knepley           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, 0, newIndices);CHKERRQ(ierr);
4680d3d1a6afSToby Isaac         }
4681d3d1a6afSToby Isaac         newP += bDof;
4682d3d1a6afSToby Isaac 
46836ecaa68aSToby Isaac         if (outValues) {
4684d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
4685d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
4686d3d1a6afSToby 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);
4687d3d1a6afSToby Isaac           }
4688d3d1a6afSToby Isaac         }
46896ecaa68aSToby Isaac       }
4690d3d1a6afSToby Isaac       else {
4691d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
4692d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
4693d3d1a6afSToby Isaac         newP++;
4694d3d1a6afSToby Isaac       }
4695d3d1a6afSToby Isaac     }
4696d3d1a6afSToby Isaac   } else {
4697d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4698d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4699d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
47004b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4701d3d1a6afSToby Isaac 
47024b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
47034b2f2278SToby Isaac       if (!bSecDof) {
47044b2f2278SToby Isaac         continue;
47054b2f2278SToby Isaac       }
4706d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4707d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4708d3d1a6afSToby Isaac       }
4709d3d1a6afSToby Isaac       if (bDof) {
4710d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
4711d3d1a6afSToby Isaac 
4712d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
4713415ce65aSMatthew G. Knepley         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, o, indices);CHKERRQ(ierr);
4714d3d1a6afSToby Isaac 
4715d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
4716d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4717d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
4718d3d1a6afSToby Isaac 
4719d3d1a6afSToby 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 */
4720d3d1a6afSToby Isaac 
4721d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
4722d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
4723302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
4724415ce65aSMatthew G. Knepley           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, 0, newIndices);CHKERRQ(ierr);
4725d3d1a6afSToby Isaac         }
4726d3d1a6afSToby Isaac         newP += bDof;
4727d3d1a6afSToby Isaac 
4728d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
47296ecaa68aSToby Isaac         if (outValues) {
4730d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
4731d3d1a6afSToby Isaac         }
47326ecaa68aSToby Isaac       }
4733d3d1a6afSToby Isaac       else {
4734d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
4735d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
4736d3d1a6afSToby Isaac         newP++;
4737d3d1a6afSToby Isaac       }
4738d3d1a6afSToby Isaac     }
4739d3d1a6afSToby Isaac   }
4740d3d1a6afSToby Isaac 
47416ecaa68aSToby Isaac   if (outValues) {
47426ecaa68aSToby Isaac     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
4743d3d1a6afSToby Isaac     ierr = PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));CHKERRQ(ierr);
4744d3d1a6afSToby Isaac     /* multiply constraints on the right */
4745d3d1a6afSToby Isaac     if (numFields) {
4746d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
4747d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
4748d3d1a6afSToby Isaac 
4749d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
4750d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
4751d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
4752d3d1a6afSToby Isaac           PetscInt c, r, k;
4753d3d1a6afSToby Isaac           PetscInt dof;
4754d3d1a6afSToby Isaac 
4755d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
47564b2f2278SToby Isaac           if (!dof) {
47574b2f2278SToby Isaac             continue;
47584b2f2278SToby Isaac           }
4759d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
4760d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
4761d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
4762d3d1a6afSToby Isaac 
4763d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
4764d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
4765d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
4766d3d1a6afSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
4767d3d1a6afSToby Isaac                 }
4768d3d1a6afSToby Isaac               }
4769d3d1a6afSToby Isaac             }
4770d3d1a6afSToby Isaac           }
4771d3d1a6afSToby Isaac           else {
4772d3d1a6afSToby Isaac             /* copy this column as is */
4773d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
4774d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
4775d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
4776d3d1a6afSToby Isaac               }
4777d3d1a6afSToby Isaac             }
4778d3d1a6afSToby Isaac           }
4779d3d1a6afSToby Isaac           oldOff += dof;
4780d3d1a6afSToby Isaac         }
4781d3d1a6afSToby Isaac       }
4782d3d1a6afSToby Isaac     }
4783d3d1a6afSToby Isaac     else {
4784d3d1a6afSToby Isaac       PetscInt oldOff = 0;
4785d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
4786d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
4787d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
4788d3d1a6afSToby Isaac         PetscInt c, r, k;
4789d3d1a6afSToby Isaac         PetscInt dof;
4790d3d1a6afSToby Isaac 
4791d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
47924b2f2278SToby Isaac         if (!dof) {
47934b2f2278SToby Isaac           continue;
47944b2f2278SToby Isaac         }
4795d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
4796d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
4797d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
4798d3d1a6afSToby Isaac 
4799d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
4800d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
4801d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
4802d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
4803d3d1a6afSToby Isaac               }
4804d3d1a6afSToby Isaac             }
4805d3d1a6afSToby Isaac           }
4806d3d1a6afSToby Isaac         }
4807d3d1a6afSToby Isaac         else {
4808d3d1a6afSToby Isaac           /* copy this column as is */
4809d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
4810d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
4811d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
4812d3d1a6afSToby Isaac             }
4813d3d1a6afSToby Isaac           }
4814d3d1a6afSToby Isaac         }
4815d3d1a6afSToby Isaac         oldOff += dof;
4816d3d1a6afSToby Isaac       }
4817d3d1a6afSToby Isaac     }
4818d3d1a6afSToby Isaac 
48196ecaa68aSToby Isaac     if (multiplyLeft) {
48206ecaa68aSToby Isaac       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
4821d3d1a6afSToby Isaac       ierr = PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));CHKERRQ(ierr);
4822d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
4823d3d1a6afSToby Isaac       if (numFields) {
4824d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4825d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
4826d3d1a6afSToby Isaac 
4827d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
4828d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
4829d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
4830d3d1a6afSToby Isaac             PetscInt c, r, k;
4831d3d1a6afSToby Isaac             PetscInt dof;
4832d3d1a6afSToby Isaac 
4833d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
4834d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
4835d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
4836d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
4837d3d1a6afSToby Isaac 
4838d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
4839d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
4840d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
4841d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
4842d3d1a6afSToby Isaac                   }
4843d3d1a6afSToby Isaac                 }
4844d3d1a6afSToby Isaac               }
4845d3d1a6afSToby Isaac             }
4846d3d1a6afSToby Isaac             else {
4847d3d1a6afSToby Isaac               /* copy this row as is */
4848d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
4849d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
4850d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
4851d3d1a6afSToby Isaac                 }
4852d3d1a6afSToby Isaac               }
4853d3d1a6afSToby Isaac             }
4854d3d1a6afSToby Isaac             oldOff += dof;
4855d3d1a6afSToby Isaac           }
4856d3d1a6afSToby Isaac         }
4857d3d1a6afSToby Isaac       }
4858d3d1a6afSToby Isaac       else {
4859d3d1a6afSToby Isaac         PetscInt oldOff = 0;
4860d3d1a6afSToby Isaac 
4861d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
4862d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
4863d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
4864d3d1a6afSToby Isaac           PetscInt c, r, k;
4865d3d1a6afSToby Isaac           PetscInt dof;
4866d3d1a6afSToby Isaac 
4867d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
4868d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
4869d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
4870d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
4871d3d1a6afSToby Isaac 
4872d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
4873d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
4874d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
4875d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
4876d3d1a6afSToby Isaac                 }
4877d3d1a6afSToby Isaac               }
4878d3d1a6afSToby Isaac             }
4879d3d1a6afSToby Isaac           }
4880d3d1a6afSToby Isaac           else {
4881d3d1a6afSToby Isaac             /* copy this row as is */
48829fc93327SToby Isaac             for (r = 0; r < dof; r++) {
4883d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
4884d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
4885d3d1a6afSToby Isaac               }
4886d3d1a6afSToby Isaac             }
4887d3d1a6afSToby Isaac           }
4888d3d1a6afSToby Isaac           oldOff += dof;
4889d3d1a6afSToby Isaac         }
4890d3d1a6afSToby Isaac       }
4891d3d1a6afSToby Isaac 
48926ecaa68aSToby Isaac       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
48936ecaa68aSToby Isaac     }
48946ecaa68aSToby Isaac     else {
48956ecaa68aSToby Isaac       newValues = tmpValues;
48966ecaa68aSToby Isaac     }
48976ecaa68aSToby Isaac   }
48986ecaa68aSToby Isaac 
4899d3d1a6afSToby Isaac   /* clean up */
4900d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
4901d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
49026ecaa68aSToby Isaac 
4903d3d1a6afSToby Isaac   if (numFields) {
4904d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
4905d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
4906d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
4907d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
4908d3d1a6afSToby Isaac     }
4909d3d1a6afSToby Isaac   }
4910d3d1a6afSToby Isaac   else {
4911d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
4912d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
4913d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
4914d3d1a6afSToby Isaac   }
4915d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
4916d3d1a6afSToby Isaac 
4917d3d1a6afSToby Isaac   /* output */
49186ecaa68aSToby Isaac   if (outPoints) {
4919d3d1a6afSToby Isaac     *outPoints = newPoints;
49206ecaa68aSToby Isaac   }
49216ecaa68aSToby Isaac   else {
49226ecaa68aSToby Isaac     ierr = DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
49236ecaa68aSToby Isaac   }
492431620726SToby Isaac   if (outValues) {
4925d3d1a6afSToby Isaac     *outValues = newValues;
49266ecaa68aSToby Isaac   }
49276ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
4928d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
4929d3d1a6afSToby Isaac   }
4930d3d1a6afSToby Isaac   PetscFunctionReturn(0);
4931d3d1a6afSToby Isaac }
4932d3d1a6afSToby Isaac 
4933d3d1a6afSToby Isaac #undef __FUNCT__
49347773e69fSMatthew G. Knepley #define __FUNCT__ "DMPlexGetClosureIndices"
49356ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
49367773e69fSMatthew G. Knepley {
49377773e69fSMatthew G. Knepley   PetscSection    clSection;
49387773e69fSMatthew G. Knepley   IS              clPoints;
49397773e69fSMatthew G. Knepley   const PetscInt *clp;
49407773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
49417773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
49427773e69fSMatthew G. Knepley   PetscInt        offsets[32];
49437773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
49447773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
49457773e69fSMatthew G. Knepley 
49467773e69fSMatthew G. Knepley   PetscFunctionBegin;
49477773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49487773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
49497773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
49507773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
49517773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
49527773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
49537773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
49547773e69fSMatthew G. Knepley   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
49557773e69fSMatthew G. Knepley   /* Get points in closure */
49567773e69fSMatthew G. Knepley   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, &clSection, &clPoints);CHKERRQ(ierr);
49577773e69fSMatthew G. Knepley   if (!clPoints) {
49587773e69fSMatthew G. Knepley     PetscInt pStart, pEnd, q;
49597773e69fSMatthew G. Knepley 
49607773e69fSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
49617773e69fSMatthew G. Knepley     /* Compress out points not in the section */
49627773e69fSMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
49637773e69fSMatthew G. Knepley     for (p = 0, q = 0; p < numPoints*2; p += 2) {
49647773e69fSMatthew G. Knepley       if ((points[p] >= pStart) && (points[p] < pEnd)) {
49657773e69fSMatthew G. Knepley         points[q*2]   = points[p];
49667773e69fSMatthew G. Knepley         points[q*2+1] = points[p+1];
49677773e69fSMatthew G. Knepley         ++q;
49687773e69fSMatthew G. Knepley       }
49697773e69fSMatthew G. Knepley     }
49707773e69fSMatthew G. Knepley     numPoints = q;
49717773e69fSMatthew G. Knepley   } else {
49727773e69fSMatthew G. Knepley     PetscInt dof, off;
49737773e69fSMatthew G. Knepley 
49747773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(clSection, point, &dof);CHKERRQ(ierr);
49757773e69fSMatthew G. Knepley     numPoints = dof/2;
49767773e69fSMatthew G. Knepley     ierr = PetscSectionGetOffset(clSection, point, &off);CHKERRQ(ierr);
49777773e69fSMatthew G. Knepley     ierr = ISGetIndices(clPoints, &clp);CHKERRQ(ierr);
49787773e69fSMatthew G. Knepley     points = (PetscInt *) &clp[off];
49797773e69fSMatthew G. Knepley   }
49807773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
49817773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
49827773e69fSMatthew G. Knepley     PetscInt dof, fdof;
49837773e69fSMatthew G. Knepley 
49847773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
49857773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
49867773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
49877773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
49887773e69fSMatthew G. Knepley     }
49897773e69fSMatthew G. Knepley     Nind += dof;
49907773e69fSMatthew G. Knepley   }
49917773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
49927773e69fSMatthew G. Knepley   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", offsets[Nf], Nind);
49937773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
49947773e69fSMatthew G. Knepley   {
49956ecaa68aSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
49967773e69fSMatthew G. Knepley     if (numPointsNew) {
49977773e69fSMatthew G. Knepley       if (!clPoints) {ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);}
49987773e69fSMatthew G. Knepley       else           {ierr = ISRestoreIndices(clPoints, &clp);CHKERRQ(ierr);}
49997773e69fSMatthew G. Knepley       numPoints = numPointsNew;
50007773e69fSMatthew G. Knepley       Nind      = NindNew;
50017773e69fSMatthew G. Knepley       points    = pointsNew;
50027773e69fSMatthew G. Knepley     }
50037773e69fSMatthew G. Knepley   }
50047773e69fSMatthew G. Knepley   /* Calculate indices */
50057773e69fSMatthew G. Knepley   ierr = DMGetWorkArray(dm, Nind, PETSC_INT, indices);CHKERRQ(ierr);
50067773e69fSMatthew G. Knepley   if (Nf) {
50076ecaa68aSToby Isaac     if (outOffsets) {
50086ecaa68aSToby Isaac       PetscInt f;
50096ecaa68aSToby Isaac 
501046bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
50116ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
50126ecaa68aSToby Isaac       }
50136ecaa68aSToby Isaac     }
50147773e69fSMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
50157773e69fSMatthew G. Knepley       PetscInt o = points[p+1];
50167773e69fSMatthew G. Knepley       ierr = PetscSectionGetOffset(globalSection, points[p], &globalOff);CHKERRQ(ierr);
5017415ce65aSMatthew G. Knepley       DMPlexGetIndicesPointFields_Internal(section, points[p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, o, *indices);
50187773e69fSMatthew G. Knepley     }
50197773e69fSMatthew G. Knepley   } else {
50207773e69fSMatthew G. Knepley     for (p = 0, off = 0; p < numPoints*2; p += 2) {
50217773e69fSMatthew G. Knepley       PetscInt o = points[p+1];
50227773e69fSMatthew G. Knepley       ierr = PetscSectionGetOffset(globalSection, points[p], &globalOff);CHKERRQ(ierr);
5023415ce65aSMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(section, points[p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, o, *indices);
50247773e69fSMatthew G. Knepley     }
50257773e69fSMatthew G. Knepley   }
50267773e69fSMatthew G. Knepley   /* Cleanup points */
50277773e69fSMatthew G. Knepley   if (numPointsNew) {
50287773e69fSMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, PETSC_INT, &pointsNew);CHKERRQ(ierr);
50297773e69fSMatthew G. Knepley   } else {
50307773e69fSMatthew G. Knepley     if (!clPoints) {ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);}
50317773e69fSMatthew G. Knepley     else           {ierr = ISRestoreIndices(clPoints, &clp);CHKERRQ(ierr);}
50327773e69fSMatthew G. Knepley   }
50337773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
50347773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
50357773e69fSMatthew G. Knepley }
50367773e69fSMatthew G. Knepley 
50377773e69fSMatthew G. Knepley #undef __FUNCT__
50387773e69fSMatthew G. Knepley #define __FUNCT__ "DMPlexRestoreClosureIndices"
503946bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
50407773e69fSMatthew G. Knepley {
50417773e69fSMatthew G. Knepley   PetscErrorCode ierr;
50427773e69fSMatthew G. Knepley 
50437773e69fSMatthew G. Knepley   PetscFunctionBegin;
50447773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50457773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
50467773e69fSMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, indices);CHKERRQ(ierr);
50477773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
50487773e69fSMatthew G. Knepley }
50497773e69fSMatthew G. Knepley 
50507773e69fSMatthew G. Knepley #undef __FUNCT__
5051552f7358SJed Brown #define __FUNCT__ "DMPlexMatSetClosure"
50527f5d1fdeSMatthew G. Knepley /*@C
50537f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
50547f5d1fdeSMatthew G. Knepley 
50557f5d1fdeSMatthew G. Knepley   Not collective
50567f5d1fdeSMatthew G. Knepley 
50577f5d1fdeSMatthew G. Knepley   Input Parameters:
50587f5d1fdeSMatthew G. Knepley + dm - The DM
5059ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5060ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
50617f5d1fdeSMatthew G. Knepley . A - The matrix
50627f5d1fdeSMatthew G. Knepley . point - The sieve point in the DM
50637f5d1fdeSMatthew G. Knepley . values - The array of values
50647f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
50657f5d1fdeSMatthew G. Knepley 
50667f5d1fdeSMatthew G. Knepley   Fortran Notes:
50677f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
50687f5d1fdeSMatthew G. Knepley 
50697f5d1fdeSMatthew G. Knepley   Level: intermediate
50707f5d1fdeSMatthew G. Knepley 
50717f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
50727f5d1fdeSMatthew G. Knepley @*/
50737c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5074552f7358SJed Brown {
5075552f7358SJed Brown   DM_Plex        *mesh   = (DM_Plex*) dm->data;
50761b406b76SMatthew G. Knepley   PetscSection    clSection;
50771b406b76SMatthew G. Knepley   IS              clPoints;
5078d3d1a6afSToby Isaac   PetscInt       *points = NULL, *newPoints;
50791b406b76SMatthew G. Knepley   const PetscInt *clp;
5080552f7358SJed Brown   PetscInt       *indices;
5081552f7358SJed Brown   PetscInt        offsets[32];
5082d3d1a6afSToby Isaac   PetscInt        numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, pStart, pEnd, p, q, f;
5083d3d1a6afSToby Isaac   PetscScalar    *newValues;
5084552f7358SJed Brown   PetscErrorCode  ierr;
5085552f7358SJed Brown 
5086552f7358SJed Brown   PetscFunctionBegin;
5087552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5088ebd6d717SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
50893dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5090ebd6d717SJed Brown   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
50913dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
50923dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5093552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
509482f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5095552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
50961b406b76SMatthew G. Knepley   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, &clSection, &clPoints);CHKERRQ(ierr);
50971b406b76SMatthew G. Knepley   if (!clPoints) {
5098552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
5099552f7358SJed Brown     /* Compress out points not in the section */
5100552f7358SJed Brown     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
5101552f7358SJed Brown     for (p = 0, q = 0; p < numPoints*2; p += 2) {
5102552f7358SJed Brown       if ((points[p] >= pStart) && (points[p] < pEnd)) {
5103552f7358SJed Brown         points[q*2]   = points[p];
5104552f7358SJed Brown         points[q*2+1] = points[p+1];
5105552f7358SJed Brown         ++q;
5106552f7358SJed Brown       }
5107552f7358SJed Brown     }
5108552f7358SJed Brown     numPoints = q;
51091b406b76SMatthew G. Knepley   } else {
51101b406b76SMatthew G. Knepley     PetscInt dof, off;
51111b406b76SMatthew G. Knepley 
51121b406b76SMatthew G. Knepley     ierr = PetscSectionGetDof(clSection, point, &dof);CHKERRQ(ierr);
51131b406b76SMatthew G. Knepley     numPoints = dof/2;
51141b406b76SMatthew G. Knepley     ierr = PetscSectionGetOffset(clSection, point, &off);CHKERRQ(ierr);
51151b406b76SMatthew G. Knepley     ierr = ISGetIndices(clPoints, &clp);CHKERRQ(ierr);
51161b406b76SMatthew G. Knepley     points = (PetscInt *) &clp[off];
51171b406b76SMatthew G. Knepley   }
5118552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5119552f7358SJed Brown     PetscInt fdof;
5120552f7358SJed Brown 
5121552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5122552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5123552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5124552f7358SJed Brown       offsets[f+1] += fdof;
5125552f7358SJed Brown     }
5126552f7358SJed Brown     numIndices += dof;
5127552f7358SJed Brown   }
51280d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
51290d644c17SKarl Rupp 
513082f516ccSBarry Smith   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", offsets[numFields], numIndices);
51316ecaa68aSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
5132d3d1a6afSToby Isaac   if (newNumPoints) {
5133d3d1a6afSToby Isaac     if (!clPoints) {
5134d3d1a6afSToby Isaac       ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
5135d3d1a6afSToby Isaac     } else {
5136d3d1a6afSToby Isaac       ierr = ISRestoreIndices(clPoints, &clp);CHKERRQ(ierr);
5137d3d1a6afSToby Isaac     }
5138d3d1a6afSToby Isaac     numPoints  = newNumPoints;
5139d3d1a6afSToby Isaac     numIndices = newNumIndices;
5140d3d1a6afSToby Isaac     points     = newPoints;
5141d3d1a6afSToby Isaac     values     = newValues;
5142d3d1a6afSToby Isaac   }
5143552f7358SJed Brown   ierr = DMGetWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5144552f7358SJed Brown   if (numFields) {
5145552f7358SJed Brown     for (p = 0; p < numPoints*2; p += 2) {
5146552f7358SJed Brown       PetscInt o = points[p+1];
5147552f7358SJed Brown       ierr = PetscSectionGetOffset(globalSection, points[p], &globalOff);CHKERRQ(ierr);
5148415ce65aSMatthew G. Knepley       DMPlexGetIndicesPointFields_Internal(section, points[p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, o, indices);
5149552f7358SJed Brown     }
5150552f7358SJed Brown   } else {
5151e6ccafaeSMatthew G Knepley     for (p = 0, off = 0; p < numPoints*2; p += 2) {
5152552f7358SJed Brown       PetscInt o = points[p+1];
5153552f7358SJed Brown       ierr = PetscSectionGetOffset(globalSection, points[p], &globalOff);CHKERRQ(ierr);
5154415ce65aSMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(section, points[p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, o, indices);
5155552f7358SJed Brown     }
5156552f7358SJed Brown   }
5157b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
5158552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
5159ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
5160ab1d0545SMatthew G. Knepley     PetscInt i;
5161ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
5162ab1d0545SMatthew G. Knepley     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %d", indices[i]);CHKERRQ(ierr);}
5163ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5164ab1d0545SMatthew G. Knepley   }
5165552f7358SJed Brown   if (ierr) {
5166552f7358SJed Brown     PetscMPIInt    rank;
5167552f7358SJed Brown     PetscErrorCode ierr2;
5168552f7358SJed Brown 
516982f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5170e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5171b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
51726a415b8fSMatthew G Knepley     ierr2 = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr2);
5173552f7358SJed Brown     CHKERRQ(ierr);
5174552f7358SJed Brown   }
5175d3d1a6afSToby Isaac   if (newNumPoints) {
5176d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
5177d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
5178d3d1a6afSToby Isaac   }
5179d3d1a6afSToby Isaac   else {
51801b406b76SMatthew G. Knepley     if (!clPoints) {
5181552f7358SJed Brown       ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &numPoints, &points);CHKERRQ(ierr);
51821b406b76SMatthew G. Knepley     } else {
51831b406b76SMatthew G. Knepley       ierr = ISRestoreIndices(clPoints, &clp);CHKERRQ(ierr);
51841b406b76SMatthew G. Knepley     }
5185d3d1a6afSToby Isaac   }
5186552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5187552f7358SJed Brown   PetscFunctionReturn(0);
5188552f7358SJed Brown }
5189552f7358SJed Brown 
5190552f7358SJed Brown #undef __FUNCT__
5191de41b84cSMatthew G. Knepley #define __FUNCT__ "DMPlexMatSetClosureRefined"
5192de41b84cSMatthew 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)
5193de41b84cSMatthew G. Knepley {
5194de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
5195de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
5196de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
5197de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
5198de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
5199de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
52004ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
5201de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
5202de41b84cSMatthew G. Knepley 
5203de41b84cSMatthew G. Knepley   PetscFunctionBegin;
5204de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
5205de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
5206de41b84cSMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
5207de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
5208de41b84cSMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
5209de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
5210de41b84cSMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
5211de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
5212de41b84cSMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
5213de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
5214de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
5215de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
5216de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5217de41b84cSMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5218de41b84cSMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5219de41b84cSMatthew G. Knepley   /* Column indices */
5220de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
52214ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
5222de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
5223de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
5224de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
5225de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
5226de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
5227de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
5228de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
5229de41b84cSMatthew G. Knepley       ++q;
5230de41b84cSMatthew G. Knepley     }
5231de41b84cSMatthew G. Knepley   }
5232de41b84cSMatthew G. Knepley   numCPoints = q;
5233de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
5234de41b84cSMatthew G. Knepley     PetscInt fdof;
5235de41b84cSMatthew G. Knepley 
5236de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
52374ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5238de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5239de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
5240de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
5241de41b84cSMatthew G. Knepley     }
5242de41b84cSMatthew G. Knepley     numCIndices += dof;
5243de41b84cSMatthew G. Knepley   }
5244de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
5245de41b84cSMatthew G. Knepley   /* Row indices */
5246de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
5247de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
52481ba5f902SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
5249de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
5250de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
5251de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5252de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
5253de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
5254de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
5255de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
52564ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
52574ca5e9f5SMatthew G. Knepley         if (!dof) continue;
52584ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
52594ca5e9f5SMatthew G. Knepley         if (s < q) continue;
5260de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
5261de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
5262de41b84cSMatthew G. Knepley         ++q;
5263de41b84cSMatthew G. Knepley       }
5264de41b84cSMatthew G. Knepley     }
5265de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5266de41b84cSMatthew G. Knepley   }
5267de41b84cSMatthew G. Knepley   numFPoints = q;
5268de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
5269de41b84cSMatthew G. Knepley     PetscInt fdof;
5270de41b84cSMatthew G. Knepley 
5271de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
52724ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5273de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5274de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
5275de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
5276de41b84cSMatthew G. Knepley     }
5277de41b84cSMatthew G. Knepley     numFIndices += dof;
5278de41b84cSMatthew G. Knepley   }
5279de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
5280de41b84cSMatthew G. Knepley 
5281de41b84cSMatthew G. Knepley   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", foffsets[numFields], numFIndices);
5282de41b84cSMatthew G. Knepley   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", coffsets[numFields], numCIndices);
5283de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
5284de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
5285de41b84cSMatthew G. Knepley   if (numFields) {
5286de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
5287de41b84cSMatthew G. Knepley       PetscInt o = ftotpoints[p+1];
5288de41b84cSMatthew G. Knepley       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[p], &globalOff);CHKERRQ(ierr);
5289415ce65aSMatthew G. Knepley       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, o, findices);
5290de41b84cSMatthew G. Knepley     }
5291de41b84cSMatthew G. Knepley     for (p = 0; p < numCPoints*2; p += 2) {
5292de41b84cSMatthew G. Knepley       PetscInt o = cpoints[p+1];
5293de41b84cSMatthew G. Knepley       ierr = PetscSectionGetOffset(globalCSection, cpoints[p], &globalOff);CHKERRQ(ierr);
5294415ce65aSMatthew G. Knepley       DMPlexGetIndicesPointFields_Internal(csection, cpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, o, cindices);
5295de41b84cSMatthew G. Knepley     }
5296de41b84cSMatthew G. Knepley   } else {
5297de41b84cSMatthew G. Knepley     for (p = 0, off = 0; p < numFPoints*2; p += 2) {
5298de41b84cSMatthew G. Knepley       PetscInt o = ftotpoints[p+1];
5299de41b84cSMatthew G. Knepley       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[p], &globalOff);CHKERRQ(ierr);
5300415ce65aSMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, o, findices);
5301de41b84cSMatthew G. Knepley     }
5302de41b84cSMatthew G. Knepley     for (p = 0, off = 0; p < numCPoints*2; p += 2) {
5303de41b84cSMatthew G. Knepley       PetscInt o = cpoints[p+1];
5304de41b84cSMatthew G. Knepley       ierr = PetscSectionGetOffset(globalCSection, cpoints[p], &globalOff);CHKERRQ(ierr);
5305415ce65aSMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(csection, cpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, o, cindices);
5306de41b84cSMatthew G. Knepley     }
5307de41b84cSMatthew G. Knepley   }
5308de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
5309de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
5310de41b84cSMatthew G. Knepley   if (ierr) {
5311de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
5312de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
5313de41b84cSMatthew G. Knepley 
5314de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5315e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5316de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
5317de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr2);
5318de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr2);
5319de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
5320de41b84cSMatthew G. Knepley   }
5321549a8adaSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
5322de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
5323de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
5324de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
5325de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
5326de41b84cSMatthew G. Knepley }
5327de41b84cSMatthew G. Knepley 
5328de41b84cSMatthew G. Knepley #undef __FUNCT__
53297c927364SMatthew G. Knepley #define __FUNCT__ "DMPlexMatGetClosureIndicesRefined"
53307c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
53317c927364SMatthew G. Knepley {
53327c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
53337c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
53347c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
53357c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
53367c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
53377c927364SMatthew G. Knepley   PetscErrorCode ierr;
53387c927364SMatthew G. Knepley 
53397c927364SMatthew G. Knepley   PetscFunctionBegin;
53407c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
53417c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
53427c927364SMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
53437c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
53447c927364SMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
53457c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
53467c927364SMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
53477c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
53487c927364SMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
53497c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
53507c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
53517c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
53527c927364SMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
53537c927364SMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
53547c927364SMatthew G. Knepley   /* Column indices */
53557c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
53567c927364SMatthew G. Knepley   maxFPoints = numCPoints;
53577c927364SMatthew G. Knepley   /* Compress out points not in the section */
53587c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
53597c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
53607c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
53617c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
53627c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
53637c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
53647c927364SMatthew G. Knepley       ++q;
53657c927364SMatthew G. Knepley     }
53667c927364SMatthew G. Knepley   }
53677c927364SMatthew G. Knepley   numCPoints = q;
53687c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
53697c927364SMatthew G. Knepley     PetscInt fdof;
53707c927364SMatthew G. Knepley 
53717c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
53727c927364SMatthew G. Knepley     if (!dof) continue;
53737c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
53747c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
53757c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
53767c927364SMatthew G. Knepley     }
53777c927364SMatthew G. Knepley     numCIndices += dof;
53787c927364SMatthew G. Knepley   }
53797c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
53807c927364SMatthew G. Knepley   /* Row indices */
53817c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
53827c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
53837c927364SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
53847c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
53857c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
53867c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
53877c927364SMatthew G. Knepley     /* Compress out points not in the section */
53887c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
53897c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
53907c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
53917c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
53927c927364SMatthew G. Knepley         if (!dof) continue;
53937c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
53947c927364SMatthew G. Knepley         if (s < q) continue;
53957c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
53967c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
53977c927364SMatthew G. Knepley         ++q;
53987c927364SMatthew G. Knepley       }
53997c927364SMatthew G. Knepley     }
54007c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
54017c927364SMatthew G. Knepley   }
54027c927364SMatthew G. Knepley   numFPoints = q;
54037c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
54047c927364SMatthew G. Knepley     PetscInt fdof;
54057c927364SMatthew G. Knepley 
54067c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
54077c927364SMatthew G. Knepley     if (!dof) continue;
54087c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
54097c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
54107c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
54117c927364SMatthew G. Knepley     }
54127c927364SMatthew G. Knepley     numFIndices += dof;
54137c927364SMatthew G. Knepley   }
54147c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
54157c927364SMatthew G. Knepley 
54167c927364SMatthew G. Knepley   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", foffsets[numFields], numFIndices);
54177c927364SMatthew G. Knepley   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %d should be %d", coffsets[numFields], numCIndices);
54187c927364SMatthew G. Knepley   if (numFields) {
54197c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
54207c927364SMatthew G. Knepley       PetscInt o = ftotpoints[p+1];
54217c927364SMatthew G. Knepley       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[p], &globalOff);CHKERRQ(ierr);
5422415ce65aSMatthew G. Knepley       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, o, findices);
54237c927364SMatthew G. Knepley     }
54247c927364SMatthew G. Knepley     for (p = 0; p < numCPoints*2; p += 2) {
54257c927364SMatthew G. Knepley       PetscInt o = cpoints[p+1];
54267c927364SMatthew G. Knepley       ierr = PetscSectionGetOffset(globalCSection, cpoints[p], &globalOff);CHKERRQ(ierr);
5427415ce65aSMatthew G. Knepley       DMPlexGetIndicesPointFields_Internal(csection, cpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, o, cindices);
54287c927364SMatthew G. Knepley     }
54297c927364SMatthew G. Knepley   } else {
54307c927364SMatthew G. Knepley     for (p = 0, off = 0; p < numFPoints*2; p += 2) {
54317c927364SMatthew G. Knepley       PetscInt o = ftotpoints[p+1];
54327c927364SMatthew G. Knepley       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[p], &globalOff);CHKERRQ(ierr);
5433415ce65aSMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, o, findices);
54347c927364SMatthew G. Knepley     }
54357c927364SMatthew G. Knepley     for (p = 0, off = 0; p < numCPoints*2; p += 2) {
54367c927364SMatthew G. Knepley       PetscInt o = cpoints[p+1];
54377c927364SMatthew G. Knepley       ierr = PetscSectionGetOffset(globalCSection, cpoints[p], &globalOff);CHKERRQ(ierr);
5438415ce65aSMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(csection, cpoints[p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, o, cindices);
54397c927364SMatthew G. Knepley     }
54407c927364SMatthew G. Knepley   }
54417c927364SMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
54427c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
54437c927364SMatthew G. Knepley   PetscFunctionReturn(0);
54447c927364SMatthew G. Knepley }
54457c927364SMatthew G. Knepley 
54467c927364SMatthew G. Knepley #undef __FUNCT__
5447770b213bSMatthew G Knepley #define __FUNCT__ "DMPlexGetHybridBounds"
5448f96281f8SMatthew G. Knepley /*@
5449f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
5450f96281f8SMatthew G. Knepley 
5451f96281f8SMatthew G. Knepley   Input Parameter:
5452f96281f8SMatthew G. Knepley . dm - The DMPlex object
5453f96281f8SMatthew G. Knepley 
5454f96281f8SMatthew G. Knepley   Output Parameters:
5455f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
5456dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5457dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5458dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5459f96281f8SMatthew G. Knepley 
5460f96281f8SMatthew G. Knepley   Level: developer
5461f96281f8SMatthew G. Knepley 
5462dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
5463f96281f8SMatthew G. Knepley @*/
5464770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
5465552f7358SJed Brown {
5466552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5467770b213bSMatthew G Knepley   PetscInt       dim;
5468770b213bSMatthew G Knepley   PetscErrorCode ierr;
5469552f7358SJed Brown 
5470552f7358SJed Brown   PetscFunctionBegin;
5471552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5472c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5473770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
5474770b213bSMatthew G Knepley   if (fMax) *fMax = mesh->hybridPointMax[dim-1];
5475770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
5476770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
5477552f7358SJed Brown   PetscFunctionReturn(0);
5478552f7358SJed Brown }
5479552f7358SJed Brown 
5480552f7358SJed Brown #undef __FUNCT__
5481770b213bSMatthew G Knepley #define __FUNCT__ "DMPlexSetHybridBounds"
5482dc5a3409SMatthew G. Knepley /*@
5483dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
5484dc5a3409SMatthew G. Knepley 
5485dc5a3409SMatthew G. Knepley   Input Parameters:
5486dc5a3409SMatthew G. Knepley . dm   - The DMPlex object
5487dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
5488dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5489dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5490dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5491dc5a3409SMatthew G. Knepley 
5492dc5a3409SMatthew G. Knepley   Level: developer
5493dc5a3409SMatthew G. Knepley 
5494dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
5495dc5a3409SMatthew G. Knepley @*/
5496770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
5497552f7358SJed Brown {
5498552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5499770b213bSMatthew G Knepley   PetscInt       dim;
5500770b213bSMatthew G Knepley   PetscErrorCode ierr;
5501552f7358SJed Brown 
5502552f7358SJed Brown   PetscFunctionBegin;
5503552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5504c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5505770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]   = cMax;
5506770b213bSMatthew G Knepley   if (fMax >= 0) mesh->hybridPointMax[dim-1] = fMax;
5507770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]     = eMax;
5508770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]     = vMax;
5509552f7358SJed Brown   PetscFunctionReturn(0);
5510552f7358SJed Brown }
5511552f7358SJed Brown 
5512552f7358SJed Brown #undef __FUNCT__
5513552f7358SJed Brown #define __FUNCT__ "DMPlexGetVTKCellHeight"
5514552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
5515552f7358SJed Brown {
5516552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5517552f7358SJed Brown 
5518552f7358SJed Brown   PetscFunctionBegin;
5519552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5520552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
5521552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
5522552f7358SJed Brown   PetscFunctionReturn(0);
5523552f7358SJed Brown }
5524552f7358SJed Brown 
5525552f7358SJed Brown #undef __FUNCT__
5526552f7358SJed Brown #define __FUNCT__ "DMPlexSetVTKCellHeight"
5527552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
5528552f7358SJed Brown {
5529552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5530552f7358SJed Brown 
5531552f7358SJed Brown   PetscFunctionBegin;
5532552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5533552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
5534552f7358SJed Brown   PetscFunctionReturn(0);
5535552f7358SJed Brown }
5536552f7358SJed Brown 
5537552f7358SJed Brown #undef __FUNCT__
5538552f7358SJed Brown #define __FUNCT__ "DMPlexCreateNumbering_Private"
5539552f7358SJed Brown /* We can easily have a form that takes an IS instead */
5540ef48cebcSMatthew G. Knepley static PetscErrorCode DMPlexCreateNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
5541552f7358SJed Brown {
5542552f7358SJed Brown   PetscSection   section, globalSection;
5543552f7358SJed Brown   PetscInt      *numbers, p;
5544552f7358SJed Brown   PetscErrorCode ierr;
5545552f7358SJed Brown 
5546552f7358SJed Brown   PetscFunctionBegin;
554782f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
5548552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
5549552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5550552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
5551552f7358SJed Brown   }
5552552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
555315b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
5554854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
5555552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5556552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
5557ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
5558ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
5559552f7358SJed Brown   }
556082f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
5561ef48cebcSMatthew G. Knepley   if (globalSize) {
5562ef48cebcSMatthew G. Knepley     PetscLayout layout;
5563ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
5564ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
5565ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
5566ef48cebcSMatthew G. Knepley   }
5567552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
5568552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
5569552f7358SJed Brown   PetscFunctionReturn(0);
5570552f7358SJed Brown }
5571552f7358SJed Brown 
5572552f7358SJed Brown #undef __FUNCT__
557381ed3555SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateCellNumbering_Internal"
557481ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
5575552f7358SJed Brown {
5576552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
5577552f7358SJed Brown   PetscErrorCode ierr;
5578552f7358SJed Brown 
5579552f7358SJed Brown   PetscFunctionBegin;
5580552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
5581552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
55820298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
558381ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
558481ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
558581ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
5586552f7358SJed Brown }
558781ed3555SMatthew G. Knepley 
558881ed3555SMatthew G. Knepley #undef __FUNCT__
558981ed3555SMatthew G. Knepley #define __FUNCT__ "DMPlexGetCellNumbering"
559081ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
559181ed3555SMatthew G. Knepley {
559281ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
559381ed3555SMatthew G. Knepley   PetscErrorCode ierr;
559481ed3555SMatthew G. Knepley 
559581ed3555SMatthew G. Knepley   PetscFunctionBegin;
559681ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
559781ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
5598552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
5599552f7358SJed Brown   PetscFunctionReturn(0);
5600552f7358SJed Brown }
5601552f7358SJed Brown 
5602552f7358SJed Brown #undef __FUNCT__
560381ed3555SMatthew G. Knepley #define __FUNCT__ "DMPlexCreateVertexNumbering_Internal"
560481ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
560581ed3555SMatthew G. Knepley {
560681ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
560781ed3555SMatthew G. Knepley   PetscErrorCode ierr;
560881ed3555SMatthew G. Knepley 
560981ed3555SMatthew G. Knepley   PetscFunctionBegin;
561081ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
561181ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
561281ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
561381ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
561481ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
561581ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
561681ed3555SMatthew G. Knepley }
561781ed3555SMatthew G. Knepley 
561881ed3555SMatthew G. Knepley #undef __FUNCT__
5619552f7358SJed Brown #define __FUNCT__ "DMPlexGetVertexNumbering"
5620552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
5621552f7358SJed Brown {
5622552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5623552f7358SJed Brown   PetscErrorCode ierr;
5624552f7358SJed Brown 
5625552f7358SJed Brown   PetscFunctionBegin;
5626552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
562781ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
5628552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
5629552f7358SJed Brown   PetscFunctionReturn(0);
5630552f7358SJed Brown }
5631552f7358SJed Brown 
5632ef48cebcSMatthew G. Knepley #undef __FUNCT__
5633ef48cebcSMatthew G. Knepley #define __FUNCT__ "DMPlexCreatePointNumbering"
5634ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
5635ef48cebcSMatthew G. Knepley {
5636ef48cebcSMatthew G. Knepley   IS             nums[4];
5637ef48cebcSMatthew G. Knepley   PetscInt       depths[4];
5638ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
5639ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
5640ef48cebcSMatthew G. Knepley 
5641ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
5642ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5643ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
56448abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
56458abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
5646ef48cebcSMatthew G. Knepley   depths[0] = depth; depths[1] = 0;
5647ef48cebcSMatthew G. Knepley   for (d = 2; d <= depth; ++d) depths[d] = depth-d+1;
5648ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
5649ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
5650ef48cebcSMatthew G. Knepley 
5651ef48cebcSMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, depths[d], &pStart, &pEnd);CHKERRQ(ierr);
5652ef48cebcSMatthew G. Knepley     ierr = DMPlexCreateNumbering_Private(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
5653ef48cebcSMatthew G. Knepley     shift += gsize;
5654ef48cebcSMatthew G. Knepley   }
5655302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
5656ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
5657ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
5658ef48cebcSMatthew G. Knepley }
5659ef48cebcSMatthew G. Knepley 
5660ca8062c8SMatthew G. Knepley #undef __FUNCT__
5661ca8062c8SMatthew G. Knepley #define __FUNCT__ "DMPlexCheckSymmetry"
5662ca8062c8SMatthew G. Knepley /*@
5663ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
5664ca8062c8SMatthew G. Knepley 
5665ca8062c8SMatthew G. Knepley   Input Parameters:
5666ca8062c8SMatthew G. Knepley   + dm - The DMPlex object
5667ca8062c8SMatthew G. Knepley 
5668ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
5669ca8062c8SMatthew G. Knepley 
5670ca8062c8SMatthew G. Knepley   Level: developer
5671ca8062c8SMatthew G. Knepley 
56729bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSkeleton(), DMCheckFaces()
5673ca8062c8SMatthew G. Knepley @*/
5674ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
5675ca8062c8SMatthew G. Knepley {
5676ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
5677ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
5678ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
5679ca8062c8SMatthew G. Knepley   PetscInt        pStart, pEnd, p, csize, ssize;
5680ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
5681ca8062c8SMatthew G. Knepley 
5682ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
5683ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5684ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
5685ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
5686ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
5687ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
5688ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
5689ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
5690ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
5691ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
569242e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
569342e66dfaSMatthew G. Knepley       PetscInt  d;
569442e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
569542e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
569642e66dfaSMatthew G. Knepley       }
5697ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
5698ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
5699ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
5700ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
5701ca8062c8SMatthew G. Knepley       }
570242e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
5703302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %d cone: ", p);CHKERRQ(ierr);
5704ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
5705302440fdSBarry Smith           ierr = PetscPrintf(PETSC_COMM_SELF, "%d, ", cone[s]);CHKERRQ(ierr);
5706ca8062c8SMatthew G. Knepley         }
5707302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5708302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %d support: ", cone[c]);CHKERRQ(ierr);
5709ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
5710302440fdSBarry Smith           ierr = PetscPrintf(PETSC_COMM_SELF, "%d, ", support[s]);CHKERRQ(ierr);
5711ca8062c8SMatthew G. Knepley         }
5712302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5713302440fdSBarry Smith         if (dup) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %d not repeatedly found in support of repeated cone point %d", p, cone[c]);
5714302440fdSBarry Smith         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %d not found in support of cone point %d", p, cone[c]);
5715ca8062c8SMatthew G. Knepley       }
571642e66dfaSMatthew G. Knepley     }
5717ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
5718ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
5719ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
5720ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
5721ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
5722ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
5723ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
5724ca8062c8SMatthew G. Knepley       }
5725ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
5726302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %d support: ", p);CHKERRQ(ierr);
5727ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
5728302440fdSBarry Smith           ierr = PetscPrintf(PETSC_COMM_SELF, "%d, ", support[c]);CHKERRQ(ierr);
5729ca8062c8SMatthew G. Knepley         }
5730302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5731302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %d cone: ", support[s]);CHKERRQ(ierr);
5732ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
5733302440fdSBarry Smith           ierr = PetscPrintf(PETSC_COMM_SELF, "%d, ", cone[c]);CHKERRQ(ierr);
5734ca8062c8SMatthew G. Knepley         }
5735302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5736ca8062c8SMatthew G. Knepley         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %d not found in cone of support point %d", p, support[s]);
5737ca8062c8SMatthew G. Knepley       }
5738ca8062c8SMatthew G. Knepley     }
5739ca8062c8SMatthew G. Knepley   }
5740ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
5741ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
5742ca8062c8SMatthew G. Knepley   if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %d != Total support size %d", csize, ssize);
5743ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
5744ca8062c8SMatthew G. Knepley }
5745ca8062c8SMatthew G. Knepley 
5746ca8062c8SMatthew G. Knepley #undef __FUNCT__
5747ca8062c8SMatthew G. Knepley #define __FUNCT__ "DMPlexCheckSkeleton"
5748ca8062c8SMatthew G. Knepley /*@
5749ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
5750ca8062c8SMatthew G. Knepley 
5751ca8062c8SMatthew G. Knepley   Input Parameters:
5752ca8062c8SMatthew G. Knepley + dm - The DMPlex object
575358723a97SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
575458723a97SMatthew G. Knepley - cellHeight - Normally 0
5755ca8062c8SMatthew G. Knepley 
5756ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
5757ca8062c8SMatthew G. Knepley 
5758ca8062c8SMatthew G. Knepley   Level: developer
5759ca8062c8SMatthew G. Knepley 
57609bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckFaces()
5761ca8062c8SMatthew G. Knepley @*/
576258723a97SMatthew G. Knepley PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscBool isSimplex, PetscInt cellHeight)
5763ca8062c8SMatthew G. Knepley {
576442363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
5765ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
5766ca8062c8SMatthew G. Knepley 
5767ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
5768ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5769c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5770ca8062c8SMatthew G. Knepley   switch (dim) {
577142363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
577242363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
577342363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
5774ca8062c8SMatthew G. Knepley   default:
5775ca8062c8SMatthew G. Knepley     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %d", dim);
5776ca8062c8SMatthew G. Knepley   }
577758723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
577858723a97SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
5779ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
5780ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
5781ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
578258723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
578358723a97SMatthew G. Knepley 
578458723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
578558723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
578658723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
578758723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
578858723a97SMatthew G. Knepley     }
578958723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
5790ca8062c8SMatthew G. Knepley     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %d has  %d vertices != %d", c, coneSize, numCorners);
5791ca8062c8SMatthew G. Knepley   }
579242363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
579342363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
579442363296SMatthew G. Knepley 
579542363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
579642363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
579742363296SMatthew G. Knepley       const PetscInt p = closure[cl];
579842363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
579942363296SMatthew G. Knepley     }
580042363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
580142363296SMatthew G. Knepley     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %d has  %d vertices > %d", c, coneSize, numHybridCorners);
580242363296SMatthew G. Knepley   }
5803ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
5804ca8062c8SMatthew G. Knepley }
58059bf0dad6SMatthew G. Knepley 
58069bf0dad6SMatthew G. Knepley #undef __FUNCT__
58079bf0dad6SMatthew G. Knepley #define __FUNCT__ "DMPlexCheckFaces"
58089bf0dad6SMatthew G. Knepley /*@
58099bf0dad6SMatthew 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
58109bf0dad6SMatthew G. Knepley 
58119bf0dad6SMatthew G. Knepley   Input Parameters:
58129bf0dad6SMatthew G. Knepley + dm - The DMPlex object
58139bf0dad6SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
58149bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
58159bf0dad6SMatthew G. Knepley 
58169bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
58179bf0dad6SMatthew G. Knepley 
58189bf0dad6SMatthew G. Knepley   Level: developer
58199bf0dad6SMatthew G. Knepley 
58209bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckSkeleton()
58219bf0dad6SMatthew G. Knepley @*/
58229bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexCheckFaces(DM dm, PetscBool isSimplex, PetscInt cellHeight)
58239bf0dad6SMatthew G. Knepley {
58243554e41dSMatthew G. Knepley   PetscInt       pMax[4];
58253554e41dSMatthew G. Knepley   PetscInt       dim, vStart, vEnd, cStart, cEnd, c, h;
58269bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
58279bf0dad6SMatthew G. Knepley 
58289bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
58299bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5830c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
58319bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
583225abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
58333554e41dSMatthew G. Knepley   for (h = cellHeight; h < dim; ++h) {
58343554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
58353554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
58369bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
58379bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
58389bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
58399bf0dad6SMatthew G. Knepley 
584025abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
58419bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
58429bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
58439bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
58449bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
58459bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
58469bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
58479bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
58489bf0dad6SMatthew G. Knepley       }
58493554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
58509bf0dad6SMatthew G. Knepley       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %d has %d faces but should have %d", c, coneSize, numFaces);
58519bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
58529bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
58539bf0dad6SMatthew G. Knepley 
58549bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
58559bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
58569bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
58579bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
58589bf0dad6SMatthew G. Knepley         }
5859e003ed69SMatthew G. Knepley         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);
58609bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
5861e003ed69SMatthew G. Knepley           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]);
58629bf0dad6SMatthew G. Knepley         }
58639bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
58649bf0dad6SMatthew G. Knepley       }
58659bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
58669bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
58679bf0dad6SMatthew G. Knepley     }
58683554e41dSMatthew G. Knepley   }
5869552f7358SJed Brown   PetscFunctionReturn(0);
5870552f7358SJed Brown }
58713913d7c8SMatthew G. Knepley 
58723913d7c8SMatthew G. Knepley #undef __FUNCT__
5873bceba477SMatthew G. Knepley #define __FUNCT__ "DMCreateInterpolation_Plex"
5874bceba477SMatthew G. Knepley /* Pointwise interpolation
5875bceba477SMatthew G. Knepley      Just code FEM for now
5876bceba477SMatthew G. Knepley      u^f = I u^c
58774ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
58784ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
58794ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
5880bceba477SMatthew G. Knepley */
5881bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
5882bceba477SMatthew G. Knepley {
5883bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
5884bceba477SMatthew G. Knepley   PetscInt       m, n;
5885a063dac3SMatthew G. Knepley   void          *ctx;
588668132eb9SMatthew G. Knepley   DM             cdm;
588768132eb9SMatthew G. Knepley   PetscBool      regular;
5888bceba477SMatthew G. Knepley   PetscErrorCode ierr;
5889bceba477SMatthew G. Knepley 
5890bceba477SMatthew G. Knepley   PetscFunctionBegin;
5891bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
5892bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
5893bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
5894bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
589568132eb9SMatthew G. Knepley 
5896bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
5897bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
5898bceba477SMatthew G. Knepley   ierr = MatSetType(*interpolation, dmCoarse->mattype);CHKERRQ(ierr);
5899a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
590068132eb9SMatthew G. Knepley 
5901a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
590268132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
590368132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
590468132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
590568132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
59065d1c2e58SMatthew G. Knepley   /* Use naive scaling */
59075d1c2e58SMatthew G. Knepley   ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
5908a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
5909a063dac3SMatthew G. Knepley }
5910bceba477SMatthew G. Knepley 
5911a063dac3SMatthew G. Knepley #undef __FUNCT__
5912a063dac3SMatthew G. Knepley #define __FUNCT__ "DMCreateInjection_Plex"
59136dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
5914a063dac3SMatthew G. Knepley {
591590748bafSMatthew G. Knepley   PetscErrorCode ierr;
59166dbf9973SLawrence Mitchell   VecScatter     ctx;
591790748bafSMatthew G. Knepley 
5918a063dac3SMatthew G. Knepley   PetscFunctionBegin;
59196dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
59206dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
59216dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
5922bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
5923bceba477SMatthew G. Knepley }
5924bceba477SMatthew G. Knepley 
5925bceba477SMatthew G. Knepley #undef __FUNCT__
5926fd59a867SMatthew G. Knepley #define __FUNCT__ "DMCreateDefaultSection_Plex"
5927fd59a867SMatthew G. Knepley PetscErrorCode DMCreateDefaultSection_Plex(DM dm)
5928fd59a867SMatthew G. Knepley {
5929fd59a867SMatthew G. Knepley   PetscSection   section;
5930ab1d0545SMatthew G. Knepley   IS            *bcPoints, *bcComps;
5931ebb3236fSMatthew G. Knepley   PetscBool     *isFE;
5932286d8613SMatthew G. Knepley   PetscInt      *bcFields, *numComp, *numDof;
5933ebb3236fSMatthew G. Knepley   PetscInt       depth, dim, numBd, numBC = 0, numFields, bd, bc = 0, f;
5934ebb3236fSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
5935fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
5936fd59a867SMatthew G. Knepley 
5937fd59a867SMatthew G. Knepley   PetscFunctionBegin;
5938ebb3236fSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
5939ae71db08SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
5940ebb3236fSMatthew G. Knepley   /* FE and FV boundary conditions are handled slightly differently */
5941ebb3236fSMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
5942ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
5943ebb3236fSMatthew G. Knepley     PetscObject  obj;
5944ebb3236fSMatthew G. Knepley     PetscClassId id;
5945ebb3236fSMatthew G. Knepley 
5946ebb3236fSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
5947ebb3236fSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
5948ebb3236fSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
5949ebb3236fSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
5950ebb3236fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %d", f);
5951ebb3236fSMatthew G. Knepley   }
59522f900235SMatthew G. Knepley   /* Allocate boundary point storage for FEM boundaries */
5953286d8613SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
5954c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5955ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
5956ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
5957dff059c6SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob, &numBd);CHKERRQ(ierr);
5958fd59a867SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
59592f900235SMatthew G. Knepley     PetscInt    field;
5960fd59a867SMatthew G. Knepley     PetscBool   isEssential;
5961385532a5SToby Isaac     const char  *labelName;
5962385532a5SToby Isaac     DMLabel     label;
59632f900235SMatthew G. Knepley 
5964385532a5SToby Isaac     ierr = PetscDSGetBoundary(dm->prob, bd, &isEssential, NULL, &labelName, &field, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
5965385532a5SToby Isaac     ierr = DMGetLabel(dm,labelName,&label);CHKERRQ(ierr);
5966385532a5SToby Isaac     if (label && isFE[field] && isEssential) ++numBC;
5967fd59a867SMatthew G. Knepley   }
59682f900235SMatthew G. Knepley   /* Add ghost cell boundaries for FVM */
5969ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) if (!isFE[f] && cEndInterior >= 0) ++numBC;
59706c8d74c4SMatthew G. Knepley   ierr = PetscCalloc3(numBC,&bcFields,numBC,&bcPoints,numBC,&bcComps);CHKERRQ(ierr);
5971ebb3236fSMatthew G. Knepley   /* Constrain ghost cells for FV */
5972ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
5973ebb3236fSMatthew G. Knepley     PetscInt *newidx, c;
5974ebb3236fSMatthew G. Knepley 
5975ebb3236fSMatthew G. Knepley     if (isFE[f] || cEndInterior < 0) continue;
5976ebb3236fSMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cEndInterior,&newidx);CHKERRQ(ierr);
5977ebb3236fSMatthew G. Knepley     for (c = cEndInterior; c < cEnd; ++c) newidx[c-cEndInterior] = c;
5978ebb3236fSMatthew G. Knepley     bcFields[bc] = f;
5979ebb3236fSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), cEnd-cEndInterior, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
5980ebb3236fSMatthew G. Knepley   }
5981ebb3236fSMatthew G. Knepley   /* Handle FEM Dirichlet boundaries */
5982ebb3236fSMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
5983fd59a867SMatthew G. Knepley     const char     *bdLabel;
5984fd59a867SMatthew G. Knepley     DMLabel         label;
59850e0f25f2SMatthew G. Knepley     const PetscInt *comps;
5986fd59a867SMatthew G. Knepley     const PetscInt *values;
59870e0f25f2SMatthew G. Knepley     PetscInt        bd2, field, numComps, numValues;
5988c945be8dSMatthew G. Knepley     PetscBool       isEssential, duplicate = PETSC_FALSE;
5989fd59a867SMatthew G. Knepley 
5990dff059c6SToby Isaac     ierr = PetscDSGetBoundary(dm->prob, bd, &isEssential, NULL, &bdLabel, &field, &numComps, &comps, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
5991c58f1c22SToby Isaac     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
5992385532a5SToby Isaac     if (!isFE[field] || !label) continue;
5993ebb3236fSMatthew G. Knepley     /* Only want to modify label once */
59947391c1cbSMatthew G. Knepley     for (bd2 = 0; bd2 < bd; ++bd2) {
59957391c1cbSMatthew G. Knepley       const char *bdname;
5996dff059c6SToby Isaac       ierr = PetscDSGetBoundary(dm->prob, bd2, NULL, NULL, &bdname, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
59977391c1cbSMatthew G. Knepley       ierr = PetscStrcmp(bdname, bdLabel, &duplicate);CHKERRQ(ierr);
59987391c1cbSMatthew G. Knepley       if (duplicate) break;
59997391c1cbSMatthew G. Knepley     }
6000ebb3236fSMatthew G. Knepley     if (!duplicate && (isFE[field])) {
6001b0bf5782SToby Isaac       /* don't complete cells, which are just present to give orientation to the boundary */
6002092e5057SToby Isaac       ierr = DMPlexLabelComplete(dm, label);CHKERRQ(ierr);
60037391c1cbSMatthew G. Knepley     }
6004ebb3236fSMatthew G. Knepley     /* Filter out cells, if you actually want to constrain cells you need to do things by hand right now */
6005fd59a867SMatthew G. Knepley     if (isEssential) {
600693a5981aSMatthew G. Knepley       PetscInt       *newidx;
6007ebb3236fSMatthew G. Knepley       PetscInt        n, newn = 0, p, v;
600893a5981aSMatthew G. Knepley 
6009fd59a867SMatthew G. Knepley       bcFields[bc] = field;
60100e0f25f2SMatthew G. Knepley       if (numComps) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), numComps, comps, PETSC_COPY_VALUES, &bcComps[bc]);CHKERRQ(ierr);}
6011ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6012ebb3236fSMatthew G. Knepley         IS              tmp;
6013ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6014ebb3236fSMatthew G. Knepley 
6015c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6016ebb3236fSMatthew G. Knepley         if (!tmp) continue;
601793a5981aSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
601893a5981aSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6019ebb3236fSMatthew G. Knepley         if (isFE[field]) {
602093a5981aSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) ++newn;
6021ebb3236fSMatthew G. Knepley         } else {
6022ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) ++newn;
6023ebb3236fSMatthew G. Knepley         }
602493a5981aSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
602593a5981aSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6026fd59a867SMatthew G. Knepley       }
6027ebb3236fSMatthew G. Knepley       ierr = PetscMalloc1(newn,&newidx);CHKERRQ(ierr);
6028ebb3236fSMatthew G. Knepley       newn = 0;
6029ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6030ebb3236fSMatthew G. Knepley         IS              tmp;
6031ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6032ebb3236fSMatthew G. Knepley 
6033c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6034ebb3236fSMatthew G. Knepley         if (!tmp) continue;
6035ebb3236fSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
6036ebb3236fSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6037ebb3236fSMatthew G. Knepley         if (isFE[field]) {
6038ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) newidx[newn++] = idx[p];
6039ebb3236fSMatthew G. Knepley         } else {
6040ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) newidx[newn++] = idx[p];
6041ebb3236fSMatthew G. Knepley         }
6042ebb3236fSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
6043ebb3236fSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6044ebb3236fSMatthew G. Knepley       }
6045ebb3236fSMatthew G. Knepley       ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), newn, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6046ebb3236fSMatthew G. Knepley     }
6047fd59a867SMatthew G. Knepley   }
6048fd59a867SMatthew G. Knepley   /* Handle discretization */
6049ebb3236fSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&numComp,numFields*(dim+1),&numDof);CHKERRQ(ierr);
6050fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
60510bacfa0aSMatthew G. Knepley     PetscObject obj;
60520bacfa0aSMatthew G. Knepley 
60530bacfa0aSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6054ebb3236fSMatthew G. Knepley     if (isFE[f]) {
60550bacfa0aSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
6056286d8613SMatthew G. Knepley       const PetscInt *numFieldDof;
6057fd59a867SMatthew G. Knepley       PetscInt        d;
6058fd59a867SMatthew G. Knepley 
6059fd59a867SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
6060286d8613SMatthew G. Knepley       ierr = PetscFEGetNumDof(fe, &numFieldDof);CHKERRQ(ierr);
6061286d8613SMatthew G. Knepley       for (d = 0; d < dim+1; ++d) numDof[f*(dim+1)+d] = numFieldDof[d];
6062ebb3236fSMatthew G. Knepley     } else {
60630bacfa0aSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
60640bacfa0aSMatthew G. Knepley 
60650bacfa0aSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
60660bacfa0aSMatthew G. Knepley       numDof[f*(dim+1)+dim] = numComp[f];
6067ebb3236fSMatthew G. Knepley     }
6068fd59a867SMatthew G. Knepley   }
6069286d8613SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6070286d8613SMatthew G. Knepley     PetscInt d;
6071286d8613SMatthew G. Knepley     for (d = 1; d < dim; ++d) {
6072286d8613SMatthew 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.");
6073286d8613SMatthew G. Knepley     }
6074286d8613SMatthew G. Knepley   }
6075ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSection(dm, dim, numFields, numComp, numDof, numBC, bcFields, bcComps, bcPoints, NULL, &section);CHKERRQ(ierr);
6076fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6077fd59a867SMatthew G. Knepley     PetscFE     fe;
6078fd59a867SMatthew G. Knepley     const char *name;
607918abd763SMatthew G. Knepley 
608018abd763SMatthew G. Knepley     ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
6081fd59a867SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
6082fd59a867SMatthew G. Knepley     ierr = PetscSectionSetFieldName(section, f, name);CHKERRQ(ierr);
6083fd59a867SMatthew G. Knepley   }
6084fd59a867SMatthew G. Knepley   ierr = DMSetDefaultSection(dm, section);CHKERRQ(ierr);
6085fd59a867SMatthew G. Knepley   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
60860e0f25f2SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {ierr = ISDestroy(&bcPoints[bc]);CHKERRQ(ierr);ierr = ISDestroy(&bcComps[bc]);CHKERRQ(ierr);}
60870e0f25f2SMatthew G. Knepley   ierr = PetscFree3(bcFields,bcPoints,bcComps);CHKERRQ(ierr);
6088286d8613SMatthew G. Knepley   ierr = PetscFree2(numComp,numDof);CHKERRQ(ierr);
6089ebb3236fSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
6090bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6091bceba477SMatthew G. Knepley }
6092bceba477SMatthew G. Knepley 
6093bceba477SMatthew G. Knepley #undef __FUNCT__
60940aef6b92SMatthew G. Knepley #define __FUNCT__ "DMPlexGetRegularRefinement"
60950aef6b92SMatthew G. Knepley /*@
60960aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
60970aef6b92SMatthew G. Knepley 
60980aef6b92SMatthew G. Knepley   Input Parameter:
60990aef6b92SMatthew G. Knepley . dm - The DMPlex object
61000aef6b92SMatthew G. Knepley 
61010aef6b92SMatthew G. Knepley   Output Parameter:
61020aef6b92SMatthew G. Knepley . regular - The flag
61030aef6b92SMatthew G. Knepley 
61040aef6b92SMatthew G. Knepley   Level: intermediate
61050aef6b92SMatthew G. Knepley 
61060aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
61070aef6b92SMatthew G. Knepley @*/
61080aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
61090aef6b92SMatthew G. Knepley {
61100aef6b92SMatthew G. Knepley   PetscFunctionBegin;
61110aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61120aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
61130aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
61140aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
61150aef6b92SMatthew G. Knepley }
61160aef6b92SMatthew G. Knepley 
61170aef6b92SMatthew G. Knepley #undef __FUNCT__
61180aef6b92SMatthew G. Knepley #define __FUNCT__ "DMPlexSetRegularRefinement"
61190aef6b92SMatthew G. Knepley /*@
61200aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
61210aef6b92SMatthew G. Knepley 
61220aef6b92SMatthew G. Knepley   Input Parameters:
61230aef6b92SMatthew G. Knepley + dm - The DMPlex object
61240aef6b92SMatthew G. Knepley - regular - The flag
61250aef6b92SMatthew G. Knepley 
61260aef6b92SMatthew G. Knepley   Level: intermediate
61270aef6b92SMatthew G. Knepley 
61280aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
61290aef6b92SMatthew G. Knepley @*/
61300aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
61310aef6b92SMatthew G. Knepley {
61320aef6b92SMatthew G. Knepley   PetscFunctionBegin;
61330aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61340aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
61350aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
61360aef6b92SMatthew G. Knepley }
61370aef6b92SMatthew G. Knepley 
6138f7c74593SToby Isaac /* anchors */
6139a68b90caSToby Isaac #undef __FUNCT__
6140a17985deSToby Isaac #define __FUNCT__ "DMPlexGetAnchors"
6141a68b90caSToby Isaac /*@
6142f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
6143f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
6144a68b90caSToby Isaac 
6145e228b242SToby Isaac   not collective
6146a68b90caSToby Isaac 
6147a68b90caSToby Isaac   Input Parameters:
6148a68b90caSToby Isaac . dm - The DMPlex object
6149a68b90caSToby Isaac 
6150a68b90caSToby Isaac   Output Parameters:
6151a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
6152a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
6153a68b90caSToby Isaac 
6154a68b90caSToby Isaac 
6155a68b90caSToby Isaac   Level: intermediate
6156a68b90caSToby Isaac 
6157f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
6158a68b90caSToby Isaac @*/
6159a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
6160a68b90caSToby Isaac {
6161a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
616241e6d900SToby Isaac   PetscErrorCode ierr;
6163a68b90caSToby Isaac 
6164a68b90caSToby Isaac   PetscFunctionBegin;
6165a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
616641e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
6167a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
6168a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
6169a68b90caSToby Isaac   PetscFunctionReturn(0);
6170a68b90caSToby Isaac }
6171a68b90caSToby Isaac 
6172a68b90caSToby Isaac #undef __FUNCT__
6173a17985deSToby Isaac #define __FUNCT__ "DMPlexSetAnchors"
6174a68b90caSToby Isaac /*@
6175f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
6176f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
6177a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
6178a68b90caSToby Isaac 
6179a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
6180f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
6181a68b90caSToby Isaac 
6182e228b242SToby Isaac   collective on dm
6183a68b90caSToby Isaac 
6184a68b90caSToby Isaac   Input Parameters:
6185a68b90caSToby Isaac + dm - The DMPlex object
6186e228b242SToby 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).
6187e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
6188a68b90caSToby Isaac 
6189a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
6190a68b90caSToby Isaac 
6191a68b90caSToby Isaac   Level: intermediate
6192a68b90caSToby Isaac 
6193f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
6194a68b90caSToby Isaac @*/
6195a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
6196a68b90caSToby Isaac {
6197a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6198e228b242SToby Isaac   PetscMPIInt    result;
6199a68b90caSToby Isaac   PetscErrorCode ierr;
6200a68b90caSToby Isaac 
6201a68b90caSToby Isaac   PetscFunctionBegin;
6202a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6203e228b242SToby Isaac   if (anchorSection) {
6204e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
6205e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
6206e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
6207e228b242SToby Isaac   }
6208e228b242SToby Isaac   if (anchorIS) {
6209e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
6210e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
6211e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
6212e228b242SToby Isaac   }
6213a68b90caSToby Isaac 
6214a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
6215a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
6216a68b90caSToby Isaac   plex->anchorSection = anchorSection;
6217a68b90caSToby Isaac 
6218a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
6219a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
6220a68b90caSToby Isaac   plex->anchorIS = anchorIS;
6221a68b90caSToby Isaac 
6222a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
6223a68b90caSToby Isaac   if (anchorIS && anchorSection) {
6224a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
6225a68b90caSToby Isaac     const PetscInt *anchors;
6226a68b90caSToby Isaac 
6227a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
6228a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
6229a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
6230a68b90caSToby Isaac     for (a = 0; a < size; a++) {
6231a68b90caSToby Isaac       PetscInt p;
6232a68b90caSToby Isaac 
6233a68b90caSToby Isaac       p = anchors[a];
6234a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
6235a68b90caSToby Isaac         PetscInt dof;
6236a68b90caSToby Isaac 
6237a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6238a68b90caSToby Isaac         if (dof) {
6239a68b90caSToby Isaac           PetscErrorCode ierr2;
6240a68b90caSToby Isaac 
6241a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
6242a68b90caSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %d cannot be constrained and an anchor",p);
6243a68b90caSToby Isaac         }
6244a68b90caSToby Isaac       }
6245a68b90caSToby Isaac     }
6246a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
6247a68b90caSToby Isaac   }
6248a68b90caSToby Isaac #endif
6249f7c74593SToby Isaac   /* reset the generic constraints */
6250f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
6251a68b90caSToby Isaac   PetscFunctionReturn(0);
6252a68b90caSToby Isaac }
6253a68b90caSToby Isaac 
6254a68b90caSToby Isaac #undef __FUNCT__
6255f7c74593SToby Isaac #define __FUNCT__ "DMPlexCreateConstraintSection_Anchors"
6256f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
6257a68b90caSToby Isaac {
6258f7c74593SToby Isaac   PetscSection anchorSection;
62596995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
6260a68b90caSToby Isaac   PetscErrorCode ierr;
6261a68b90caSToby Isaac 
6262a68b90caSToby Isaac   PetscFunctionBegin;
6263a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6264a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
6265e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
6266a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
62676995de1eSToby Isaac   if (numFields) {
6268719ab38cSToby Isaac     PetscInt f;
6269a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
6270719ab38cSToby Isaac 
6271719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
6272719ab38cSToby Isaac       PetscInt numComp;
6273719ab38cSToby Isaac 
6274719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
6275719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
6276719ab38cSToby Isaac     }
62776995de1eSToby Isaac   }
6278a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
62796995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
62806995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
62816995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
62826995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
6283a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
6284a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
6285a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6286a68b90caSToby Isaac     if (dof) {
6287a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
6288a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
6289a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
6290a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
6291a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
6292a68b90caSToby Isaac       }
6293a68b90caSToby Isaac     }
6294a68b90caSToby Isaac   }
6295a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
6296a68b90caSToby Isaac   PetscFunctionReturn(0);
6297a68b90caSToby Isaac }
6298a68b90caSToby Isaac 
6299a68b90caSToby Isaac #undef __FUNCT__
6300f7c74593SToby Isaac #define __FUNCT__ "DMPlexCreateConstraintMatrix_Anchors"
6301f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
6302a68b90caSToby Isaac {
6303f7c74593SToby Isaac   PetscSection aSec;
63040ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
63050ac89760SToby Isaac   const PetscInt *anchors;
63060ac89760SToby Isaac   PetscInt numFields, f;
630766ad2231SToby Isaac   IS aIS;
63080ac89760SToby Isaac   PetscErrorCode ierr;
63090ac89760SToby Isaac 
63100ac89760SToby Isaac   PetscFunctionBegin;
63110ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63120ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
63130ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
63140ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
63150ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
6316302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
6317a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
631866ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
63196995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
63206995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
63210ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
63220ac89760SToby Isaac   i[0] = 0;
63230ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
63240ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
6325f19733c5SToby Isaac     PetscInt rDof, rOff, r;
6326f19733c5SToby Isaac 
6327f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
6328f19733c5SToby Isaac     if (!rDof) continue;
6329f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
63300ac89760SToby Isaac     if (numFields) {
63310ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
63320ac89760SToby Isaac         annz = 0;
6333f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
6334f19733c5SToby Isaac           a = anchors[rOff + r];
63350ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
63360ac89760SToby Isaac           annz += aDof;
63370ac89760SToby Isaac         }
63380ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
63390ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
63400ac89760SToby Isaac         for (q = 0; q < dof; q++) {
63410ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
63420ac89760SToby Isaac         }
63430ac89760SToby Isaac       }
63440ac89760SToby Isaac     }
63450ac89760SToby Isaac     else {
63460ac89760SToby Isaac       annz = 0;
63470ac89760SToby Isaac       for (q = 0; q < dof; q++) {
63480ac89760SToby Isaac         a = anchors[off + q];
63490ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
63500ac89760SToby Isaac         annz += aDof;
63510ac89760SToby Isaac       }
63520ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
63530ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
63540ac89760SToby Isaac       for (q = 0; q < dof; q++) {
63550ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
63560ac89760SToby Isaac       }
63570ac89760SToby Isaac     }
63580ac89760SToby Isaac   }
63590ac89760SToby Isaac   nnz = i[m];
63600ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
63610ac89760SToby Isaac   offset = 0;
63620ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
63630ac89760SToby Isaac     if (numFields) {
63640ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
63650ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
63660ac89760SToby Isaac         for (q = 0; q < dof; q++) {
63670ac89760SToby Isaac           PetscInt rDof, rOff, r;
63680ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
63690ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
63700ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
63710ac89760SToby Isaac             PetscInt s;
63720ac89760SToby Isaac 
63730ac89760SToby Isaac             a = anchors[rOff + r];
63740ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
63750ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
63760ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
63770ac89760SToby Isaac               j[offset++] = aOff + s;
63780ac89760SToby Isaac             }
63790ac89760SToby Isaac           }
63800ac89760SToby Isaac         }
63810ac89760SToby Isaac       }
63820ac89760SToby Isaac     }
63830ac89760SToby Isaac     else {
63840ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
63850ac89760SToby Isaac       for (q = 0; q < dof; q++) {
63860ac89760SToby Isaac         PetscInt rDof, rOff, r;
63870ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
63880ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
63890ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
63900ac89760SToby Isaac           PetscInt s;
63910ac89760SToby Isaac 
63920ac89760SToby Isaac           a = anchors[rOff + r];
63930ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
63940ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
63950ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
63960ac89760SToby Isaac             j[offset++] = aOff + s;
63970ac89760SToby Isaac           }
63980ac89760SToby Isaac         }
63990ac89760SToby Isaac       }
64000ac89760SToby Isaac     }
64010ac89760SToby Isaac   }
64020ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
640325570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
640425570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
640566ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
64060ac89760SToby Isaac   PetscFunctionReturn(0);
64070ac89760SToby Isaac }
64080ac89760SToby Isaac 
64090ac89760SToby Isaac #undef __FUNCT__
641066ad2231SToby Isaac #define __FUNCT__ "DMCreateDefaultConstraints_Plex"
641166ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
641266ad2231SToby Isaac {
6413f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6414f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
641566ad2231SToby Isaac   Mat            cMat;
641666ad2231SToby Isaac   PetscErrorCode ierr;
641766ad2231SToby Isaac 
641866ad2231SToby Isaac   PetscFunctionBegin;
641966ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6420a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
642166ad2231SToby Isaac   if (anchorSection) {
6422e228b242SToby Isaac     PetscDS  ds;
6423e228b242SToby Isaac     PetscInt nf;
6424e228b242SToby Isaac 
6425f7c74593SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
6426f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
6427f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
6428e228b242SToby Isaac     ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
6429302440fdSBarry Smith     ierr = PetscDSGetNumFields(ds,&nf);CHKERRQ(ierr);
6430e228b242SToby Isaac     if (nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
643166ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
643266ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
643366ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
643466ad2231SToby Isaac   }
643566ad2231SToby Isaac   PetscFunctionReturn(0);
643666ad2231SToby Isaac }
6437