xref: /petsc/src/dm/impls/plex/plex.c (revision 503335f2bb2e737c72788c1ddaf9ea14155ebaae)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2af0996ceSBarry Smith #include <petsc/private/isimpl.h>
3e5c6e791SKarl Rupp #include <petsc/private/vecimpl.h>
40c312b8eSJed Brown #include <petscsf.h>
5e228b242SToby Isaac #include <petscds.h>
6e412dcbdSMatthew G. Knepley #include <petscdraw.h>
7552f7358SJed Brown 
8552f7358SJed Brown /* Logging support */
925afeb17SMatthew G. Knepley PetscLogEvent DMPLEX_Interpolate, PETSCPARTITIONER_Partition, DMPLEX_Distribute, DMPLEX_DistributeCones, DMPLEX_DistributeLabels, DMPLEX_DistributeSF, DMPLEX_DistributeOverlap, DMPLEX_DistributeField, DMPLEX_DistributeData, DMPLEX_Migrate, DMPLEX_InterpolateSF, DMPLEX_GlobalToNaturalBegin, DMPLEX_GlobalToNaturalEnd, DMPLEX_NaturalToGlobalBegin, DMPLEX_NaturalToGlobalEnd, DMPLEX_Stratify, DMPLEX_Preallocate, DMPLEX_ResidualFEM, DMPLEX_JacobianFEM, DMPLEX_InterpolatorFEM, DMPLEX_InjectorFEM, DMPLEX_IntegralFEM, DMPLEX_CreateGmsh;
10552f7358SJed Brown 
115a576424SJed Brown PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);
122c40f234SMatthew G. Knepley PETSC_EXTERN PetscErrorCode VecLoad_Default(Vec, PetscViewer);
13552f7358SJed Brown 
147afe7537SMatthew G. Knepley PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
157e42fee7SMatthew G. Knepley {
1680d5bdc6SMatthew G. Knepley   PetscInt       dim, pStart, pEnd, vStart, vEnd, cStart, cEnd, cEndInterior, vdof = 0, cdof = 0;
177e42fee7SMatthew G. Knepley   PetscErrorCode ierr;
187e42fee7SMatthew G. Knepley 
197e42fee7SMatthew G. Knepley   PetscFunctionBegin;
207e42fee7SMatthew G. Knepley   *ft  = PETSC_VTK_POINT_FIELD;
21c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
227e42fee7SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
237e42fee7SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2480d5bdc6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
2580d5bdc6SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
267e42fee7SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
277e42fee7SMatthew G. Knepley   if (field >= 0) {
287e42fee7SMatthew G. Knepley     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, vStart, field, &vdof);CHKERRQ(ierr);}
297e42fee7SMatthew G. Knepley     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, cStart, field, &cdof);CHKERRQ(ierr);}
307e42fee7SMatthew G. Knepley   } else {
317e42fee7SMatthew G. Knepley     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetDof(section, vStart, &vdof);CHKERRQ(ierr);}
327e42fee7SMatthew G. Knepley     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetDof(section, cStart, &cdof);CHKERRQ(ierr);}
337e42fee7SMatthew G. Knepley   }
347e42fee7SMatthew G. Knepley   if (vdof) {
357e42fee7SMatthew G. Knepley     *sStart = vStart;
367e42fee7SMatthew G. Knepley     *sEnd   = vEnd;
377e42fee7SMatthew G. Knepley     if (vdof == dim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
387e42fee7SMatthew G. Knepley     else             *ft = PETSC_VTK_POINT_FIELD;
397e42fee7SMatthew G. Knepley   } else if (cdof) {
407e42fee7SMatthew G. Knepley     *sStart = cStart;
417e42fee7SMatthew G. Knepley     *sEnd   = cEnd;
427e42fee7SMatthew G. Knepley     if (cdof == dim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
437e42fee7SMatthew G. Knepley     else             *ft = PETSC_VTK_CELL_FIELD;
447e42fee7SMatthew G. Knepley   } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Could not classify input Vec for VTK");
457e42fee7SMatthew G. Knepley   PetscFunctionReturn(0);
467e42fee7SMatthew G. Knepley }
477e42fee7SMatthew G. Knepley 
487cd05799SMatthew G. Knepley static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
49e412dcbdSMatthew G. Knepley {
50e412dcbdSMatthew G. Knepley   DM                 dm;
51d1df6f1dSMatthew G. Knepley   PetscSection       s;
52e412dcbdSMatthew G. Knepley   PetscDraw          draw, popup;
53e412dcbdSMatthew G. Knepley   DM                 cdm;
54e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
55e412dcbdSMatthew G. Knepley   Vec                coordinates;
56e412dcbdSMatthew G. Knepley   const PetscScalar *coords, *array;
57e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
58339e3443SMatthew G. Knepley   PetscReal          vbound[2], time;
59339e3443SMatthew G. Knepley   PetscBool          isnull, flg;
60d1df6f1dSMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
61e412dcbdSMatthew G. Knepley   const char        *name;
62339e3443SMatthew G. Knepley   char               title[PETSC_MAX_PATH_LEN];
63e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
64e412dcbdSMatthew G. Knepley 
65e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
66d1df6f1dSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
67d1df6f1dSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
68d1df6f1dSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
69d1df6f1dSMatthew G. Knepley 
70e412dcbdSMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
71e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
72e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
73d1df6f1dSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
74d1df6f1dSMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
75e412dcbdSMatthew G. Knepley   ierr = DMGetCoarsenLevel(dm, &level);CHKERRQ(ierr);
76e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
77e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
78e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
79e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
80e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
81e412dcbdSMatthew G. Knepley 
82e412dcbdSMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
83339e3443SMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, &step, &time);CHKERRQ(ierr);
84e412dcbdSMatthew G. Knepley 
85e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
86e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
87e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
880c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
890c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
90e412dcbdSMatthew G. Knepley   }
91e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
92e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
93e412dcbdSMatthew G. Knepley 
94d1df6f1dSMatthew G. Knepley   /* Could implement something like DMDASelectFields() */
95d1df6f1dSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
96d1df6f1dSMatthew G. Knepley     DM   fdm = dm;
97d1df6f1dSMatthew G. Knepley     Vec  fv  = v;
98d1df6f1dSMatthew G. Knepley     IS   fis;
99d1df6f1dSMatthew G. Knepley     char prefix[PETSC_MAX_PATH_LEN];
100d1df6f1dSMatthew G. Knepley     const char *fname;
101d1df6f1dSMatthew G. Knepley 
102d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
103d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldName(s, f, &fname);CHKERRQ(ierr);
104d1df6f1dSMatthew G. Knepley 
105d1df6f1dSMatthew G. Knepley     if (v->hdr.prefix) {ierr = PetscStrcpy(prefix, v->hdr.prefix);CHKERRQ(ierr);}
106d1df6f1dSMatthew G. Knepley     else               {prefix[0] = '\0';}
107d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
108d1df6f1dSMatthew G. Knepley       ierr = DMCreateSubDM(dm, 1, &f, &fis, &fdm);CHKERRQ(ierr);
109d1df6f1dSMatthew G. Knepley       ierr = VecGetSubVector(v, fis, &fv);CHKERRQ(ierr);
110d1df6f1dSMatthew G. Knepley       ierr = PetscStrcat(prefix, fname);CHKERRQ(ierr);
111d1df6f1dSMatthew G. Knepley       ierr = PetscStrcat(prefix, "_");CHKERRQ(ierr);
112d1df6f1dSMatthew G. Knepley     }
113d1df6f1dSMatthew G. Knepley     for (comp = 0; comp < Nc; ++comp, ++w) {
114d1df6f1dSMatthew G. Knepley       PetscInt nmax = 2;
115d1df6f1dSMatthew G. Knepley 
116d1df6f1dSMatthew G. Knepley       ierr = PetscViewerDrawGetDraw(viewer, w, &draw);CHKERRQ(ierr);
117d1df6f1dSMatthew G. Knepley       if (Nc > 1) {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s_%D Step: %D Time: %.4g", name, fname, comp, step, time);CHKERRQ(ierr);}
118d1df6f1dSMatthew G. Knepley       else        {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s Step: %D Time: %.4g", name, fname, step, time);CHKERRQ(ierr);}
119d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetTitle(draw, title);CHKERRQ(ierr);
120d1df6f1dSMatthew G. Knepley 
121d1df6f1dSMatthew G. Knepley       /* TODO Get max and min only for this component */
122d1df6f1dSMatthew G. Knepley       ierr = PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg);CHKERRQ(ierr);
123339e3443SMatthew G. Knepley       if (!flg) {
124d1df6f1dSMatthew G. Knepley         ierr = VecMin(fv, NULL, &vbound[0]);CHKERRQ(ierr);
125d1df6f1dSMatthew G. Knepley         ierr = VecMax(fv, NULL, &vbound[1]);CHKERRQ(ierr);
126d1df6f1dSMatthew G. Knepley         if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
127339e3443SMatthew G. Knepley       }
128e412dcbdSMatthew G. Knepley       ierr = PetscDrawGetPopup(draw, &popup);CHKERRQ(ierr);
129339e3443SMatthew G. Knepley       ierr = PetscDrawScalePopup(popup, vbound[0], vbound[1]);CHKERRQ(ierr);
130d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
131e412dcbdSMatthew G. Knepley 
132d1df6f1dSMatthew G. Knepley       ierr = VecGetArrayRead(fv, &array);CHKERRQ(ierr);
133e412dcbdSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
13499a2f7bcSMatthew G. Knepley         PetscScalar *coords = NULL, *a = NULL;
135339e3443SMatthew G. Knepley         PetscInt     numCoords, color[4];
136e412dcbdSMatthew G. Knepley 
137d1df6f1dSMatthew G. Knepley         ierr = DMPlexPointLocalRead(fdm, c, array, &a);CHKERRQ(ierr);
138339e3443SMatthew G. Knepley         if (a) {
139d1df6f1dSMatthew G. Knepley           color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
140339e3443SMatthew G. Knepley           color[1] = color[2] = color[3] = color[0];
141339e3443SMatthew G. Knepley         } else {
142339e3443SMatthew G. Knepley           PetscScalar *vals = NULL;
143339e3443SMatthew G. Knepley           PetscInt     numVals, va;
144339e3443SMatthew G. Knepley 
145d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
146d1df6f1dSMatthew G. Knepley           if (numVals % Nc) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of components %D does not divide the number of values in the closure %D", Nc, numVals);
147d1df6f1dSMatthew G. Knepley           switch (numVals/Nc) {
148d1df6f1dSMatthew G. Knepley           case 3: /* P1 Triangle */
149d1df6f1dSMatthew G. Knepley           case 4: /* P1 Quadrangle */
150d1df6f1dSMatthew G. Knepley             for (va = 0; va < numVals/Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp]), vbound[0], vbound[1]);
151339e3443SMatthew G. Knepley             break;
152d1df6f1dSMatthew G. Knepley           case 6: /* P2 Triangle */
153d1df6f1dSMatthew G. Knepley           case 8: /* P2 Quadrangle */
154d1df6f1dSMatthew G. Knepley             for (va = 0; va < numVals/(Nc*2); ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp + numVals/(Nc*2)]), vbound[0], vbound[1]);
155d1df6f1dSMatthew G. Knepley             break;
156d1df6f1dSMatthew G. Knepley           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %D cannot be handled", numVals/Nc);
157339e3443SMatthew G. Knepley           }
158d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
159339e3443SMatthew G. Knepley         }
160e412dcbdSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
161e412dcbdSMatthew G. Knepley         switch (numCoords) {
162e412dcbdSMatthew G. Knepley         case 6:
163339e3443SMatthew G. Knepley           ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), color[0], color[1], color[2]);CHKERRQ(ierr);
164e412dcbdSMatthew G. Knepley           break;
165e412dcbdSMatthew G. Knepley         case 8:
166339e3443SMatthew G. Knepley           ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), color[0], color[1], color[2]);CHKERRQ(ierr);
167339e3443SMatthew G. Knepley           ierr = PetscDrawTriangle(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), color[2], color[3], color[0]);CHKERRQ(ierr);
168e412dcbdSMatthew G. Knepley           break;
169e412dcbdSMatthew G. Knepley         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
170e412dcbdSMatthew G. Knepley         }
171e412dcbdSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
172e412dcbdSMatthew G. Knepley       }
173d1df6f1dSMatthew G. Knepley       ierr = VecRestoreArrayRead(fv, &array);CHKERRQ(ierr);
174e412dcbdSMatthew G. Knepley       ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
175e412dcbdSMatthew G. Knepley       ierr = PetscDrawPause(draw);CHKERRQ(ierr);
176e412dcbdSMatthew G. Knepley       ierr = PetscDrawSave(draw);CHKERRQ(ierr);
177d1df6f1dSMatthew G. Knepley     }
178d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
179d1df6f1dSMatthew G. Knepley       ierr = VecRestoreSubVector(v, fis, &fv);CHKERRQ(ierr);
180d1df6f1dSMatthew G. Knepley       ierr = ISDestroy(&fis);CHKERRQ(ierr);
181d1df6f1dSMatthew G. Knepley       ierr = DMDestroy(&fdm);CHKERRQ(ierr);
182d1df6f1dSMatthew G. Knepley     }
183d1df6f1dSMatthew G. Knepley   }
184e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
185e412dcbdSMatthew G. Knepley }
186e412dcbdSMatthew G. Knepley 
187552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
188552f7358SJed Brown {
189552f7358SJed Brown   DM             dm;
190f13a32a3SMatthew G. Knepley   PetscBool      isvtk, ishdf5, isdraw, isseq;
191552f7358SJed Brown   PetscErrorCode ierr;
192552f7358SJed Brown 
193552f7358SJed Brown   PetscFunctionBegin;
194552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
19582f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
196552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
197b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
198f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW, &isdraw);CHKERRQ(ierr);
199b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
200ef31f671SMatthew G. Knepley   if (isvtk || ishdf5) {
201ef31f671SMatthew G. Knepley     PetscInt  numFields;
202ef31f671SMatthew G. Knepley     PetscBool fem = PETSC_FALSE;
203ef31f671SMatthew G. Knepley 
204ef31f671SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
205ef31f671SMatthew G. Knepley     if (numFields) {
206ef31f671SMatthew G. Knepley       PetscObject fe;
207ef31f671SMatthew G. Knepley 
208ef31f671SMatthew G. Knepley       ierr = DMGetField(dm, 0, &fe);CHKERRQ(ierr);
209ef31f671SMatthew G. Knepley       if (fe->classid == PETSCFE_CLASSID) fem = PETSC_TRUE;
210ef31f671SMatthew G. Knepley     }
211bdd6f66aSToby Isaac     if (fem) {ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, v, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);}
212ef31f671SMatthew G. Knepley   }
213552f7358SJed Brown   if (isvtk) {
214552f7358SJed Brown     PetscSection            section;
215b136c2c9SMatthew G. Knepley     PetscViewerVTKFieldType ft;
216b136c2c9SMatthew G. Knepley     PetscInt                pStart, pEnd;
217552f7358SJed Brown 
218552f7358SJed Brown     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
2197afe7537SMatthew G. Knepley     ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
220552f7358SJed Brown     ierr = PetscObjectReference((PetscObject) v);CHKERRQ(ierr);  /* viewer drops reference */
221552f7358SJed Brown     ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, ft, (PetscObject) v);CHKERRQ(ierr);
222552f7358SJed Brown   } else if (ishdf5) {
223b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
22439d25373SMatthew G. Knepley     ierr = VecView_Plex_Local_HDF5_Internal(v, viewer);CHKERRQ(ierr);
225b136c2c9SMatthew G. Knepley #else
226b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
227b136c2c9SMatthew G. Knepley #endif
228f13a32a3SMatthew G. Knepley   } else if (isdraw) {
229f13a32a3SMatthew G. Knepley     ierr = VecView_Plex_Local_Draw(v, viewer);CHKERRQ(ierr);
230552f7358SJed Brown   } else {
23155f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
23255f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
233552f7358SJed Brown   }
234552f7358SJed Brown   PetscFunctionReturn(0);
235552f7358SJed Brown }
236552f7358SJed Brown 
237552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
238552f7358SJed Brown {
239552f7358SJed Brown   DM             dm;
240f13a32a3SMatthew G. Knepley   PetscReal      time = 0.0;
241e412dcbdSMatthew G. Knepley   PetscBool      isvtk, ishdf5, isdraw, isseq;
242552f7358SJed Brown   PetscErrorCode ierr;
243552f7358SJed Brown 
244552f7358SJed Brown   PetscFunctionBegin;
245552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
24682f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
247552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
24833c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
249e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW, &isdraw);CHKERRQ(ierr);
25055f2e967SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
251552f7358SJed Brown   if (isvtk) {
252552f7358SJed Brown     Vec         locv;
253552f7358SJed Brown     const char *name;
254552f7358SJed Brown 
255552f7358SJed Brown     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
256552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
257552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
258552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
259552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
260cf9ba882SMatthew G. Knepley     ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
261cf9ba882SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
262552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
263552f7358SJed Brown     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
264b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
265b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
26639d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
267b136c2c9SMatthew G. Knepley #else
268b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
269b136c2c9SMatthew G. Knepley #endif
270e412dcbdSMatthew G. Knepley   } else if (isdraw) {
271f13a32a3SMatthew G. Knepley     Vec         locv;
272f13a32a3SMatthew G. Knepley     const char *name;
273f13a32a3SMatthew G. Knepley 
274f13a32a3SMatthew G. Knepley     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
275f13a32a3SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
276f13a32a3SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
277f13a32a3SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
278f13a32a3SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
279f13a32a3SMatthew G. Knepley     ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
280f13a32a3SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
281f13a32a3SMatthew G. Knepley     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
282f13a32a3SMatthew G. Knepley     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
283552f7358SJed Brown   } else {
28455f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
28555f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
286552f7358SJed Brown   }
287552f7358SJed Brown   PetscFunctionReturn(0);
288552f7358SJed Brown }
289552f7358SJed Brown 
290d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
291d930f514SMatthew G. Knepley {
292d930f514SMatthew G. Knepley   DM                dm;
293d930f514SMatthew G. Knepley   MPI_Comm          comm;
294d930f514SMatthew G. Knepley   PetscViewerFormat format;
295d930f514SMatthew G. Knepley   Vec               v;
296d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
297d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
298d930f514SMatthew G. Knepley 
299d930f514SMatthew G. Knepley   PetscFunctionBegin;
300d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
301d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
3022c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
303d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
304d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
305d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
306d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
307d930f514SMatthew G. Knepley     const char *vecname;
308d930f514SMatthew G. Knepley     PetscInt    n, nroots;
309d930f514SMatthew G. Knepley 
310d930f514SMatthew G. Knepley     if (dm->sfNatural) {
311ca43db0aSBarry Smith       ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
312d930f514SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
313d930f514SMatthew G. Knepley       if (n == nroots) {
314d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
315d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
316d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
317d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
318d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
319d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
320d930f514SMatthew G. Knepley     } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
321d930f514SMatthew G. Knepley   } else {
322d930f514SMatthew G. Knepley     /* we are viewing a natural DMPlex vec. */
323d930f514SMatthew G. Knepley     v = originalv;
324d930f514SMatthew G. Knepley   }
325d930f514SMatthew G. Knepley   if (ishdf5) {
326d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
32739d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
328d930f514SMatthew G. Knepley #else
329d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
330d930f514SMatthew G. Knepley #endif
331d930f514SMatthew G. Knepley   } else if (isvtk) {
332d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
333d930f514SMatthew G. Knepley   } else {
334d930f514SMatthew G. Knepley     PetscBool isseq;
335d930f514SMatthew G. Knepley 
336d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
337d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
338d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
339d930f514SMatthew G. Knepley   }
340d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
341d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
342d930f514SMatthew G. Knepley }
343d930f514SMatthew G. Knepley 
3442c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
3452c40f234SMatthew G. Knepley {
3462c40f234SMatthew G. Knepley   DM             dm;
3472c40f234SMatthew G. Knepley   PetscBool      ishdf5;
3482c40f234SMatthew G. Knepley   PetscErrorCode ierr;
3492c40f234SMatthew G. Knepley 
3502c40f234SMatthew G. Knepley   PetscFunctionBegin;
3512c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
3522c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
3532c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
3542c40f234SMatthew G. Knepley   if (ishdf5) {
3552c40f234SMatthew G. Knepley     DM          dmBC;
3562c40f234SMatthew G. Knepley     Vec         gv;
3572c40f234SMatthew G. Knepley     const char *name;
3582c40f234SMatthew G. Knepley 
3592c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
3602c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
3612c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
3622c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
3632c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
3642c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
3652c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
3662c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
3672c40f234SMatthew G. Knepley   } else {
3682c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
3692c40f234SMatthew G. Knepley   }
3702c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
3712c40f234SMatthew G. Knepley }
3722c40f234SMatthew G. Knepley 
3732c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
3742c40f234SMatthew G. Knepley {
3752c40f234SMatthew G. Knepley   DM             dm;
3762c40f234SMatthew G. Knepley   PetscBool      ishdf5;
3772c40f234SMatthew G. Knepley   PetscErrorCode ierr;
3782c40f234SMatthew G. Knepley 
3792c40f234SMatthew G. Knepley   PetscFunctionBegin;
3802c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
3812c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
3822c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
3832c40f234SMatthew G. Knepley   if (ishdf5) {
384878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
38539d25373SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
386b136c2c9SMatthew G. Knepley #else
387b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
388878b459fSMatthew G. Knepley #endif
3892c40f234SMatthew G. Knepley   } else {
3902c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
391552f7358SJed Brown   }
392552f7358SJed Brown   PetscFunctionReturn(0);
393552f7358SJed Brown }
394552f7358SJed Brown 
395d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
396d930f514SMatthew G. Knepley {
397d930f514SMatthew G. Knepley   DM                dm;
398d930f514SMatthew G. Knepley   PetscViewerFormat format;
399d930f514SMatthew G. Knepley   PetscBool         ishdf5;
400d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
401d930f514SMatthew G. Knepley 
402d930f514SMatthew G. Knepley   PetscFunctionBegin;
403d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
404d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
405d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
406d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
407d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
408d930f514SMatthew G. Knepley     if (dm->sfNatural) {
409d930f514SMatthew G. Knepley       if (ishdf5) {
410d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
411d930f514SMatthew G. Knepley         Vec         v;
412d930f514SMatthew G. Knepley         const char *vecname;
413d930f514SMatthew G. Knepley 
414d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
415d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
416d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
41739d25373SMatthew G. Knepley         ierr = VecLoad_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
418d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
419d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
420d930f514SMatthew G. Knepley         ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
421d930f514SMatthew G. Knepley #else
422d930f514SMatthew G. Knepley         SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
423d930f514SMatthew G. Knepley #endif
424d930f514SMatthew G. Knepley       } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
425d930f514SMatthew G. Knepley     }
426d930f514SMatthew G. Knepley   }
427d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
428d930f514SMatthew G. Knepley }
429d930f514SMatthew G. Knepley 
4307cd05799SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
431731e8ddeSMatthew G. Knepley {
432731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
433731e8ddeSMatthew G. Knepley   Vec                coordinates;
434731e8ddeSMatthew G. Knepley   DMLabel            depthLabel;
435731e8ddeSMatthew G. Knepley   const char        *name[4];
436731e8ddeSMatthew G. Knepley   const PetscScalar *a;
437731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
438731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
439731e8ddeSMatthew G. Knepley 
440731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
441731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
442731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
443731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
444731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
445731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
446731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
447731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
448731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
449731e8ddeSMatthew G. Knepley   name[1]     = "edge";
450731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
451731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
452731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
453731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
454731e8ddeSMatthew G. Knepley     PetscInt  closureSize, cl;
455731e8ddeSMatthew G. Knepley 
456f347f43bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D:\n", c);CHKERRQ(ierr);
457731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
458731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
459731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
460731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
461731e8ddeSMatthew G. Knepley 
462731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
463731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
464731e8ddeSMatthew G. Knepley       if (!dof) continue;
465731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
466731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
467f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
468731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
469731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
470731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
471731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
47241477eefSMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
473731e8ddeSMatthew G. Knepley         }
474731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
475731e8ddeSMatthew G. Knepley       }
476731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
477731e8ddeSMatthew G. Knepley     }
478731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
479731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
480731e8ddeSMatthew G. Knepley   }
481731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
482731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
483731e8ddeSMatthew G. Knepley }
484731e8ddeSMatthew G. Knepley 
4857cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
486552f7358SJed Brown {
487552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
488552f7358SJed Brown   DM                cdm;
489552f7358SJed Brown   DMLabel           markers;
490552f7358SJed Brown   PetscSection      coordSection;
491552f7358SJed Brown   Vec               coordinates;
492552f7358SJed Brown   PetscViewerFormat format;
493552f7358SJed Brown   PetscErrorCode    ierr;
494552f7358SJed Brown 
495552f7358SJed Brown   PetscFunctionBegin;
496552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
497552f7358SJed Brown   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
498552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
499552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
500552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
501552f7358SJed Brown     const char *name;
502552f7358SJed Brown     PetscInt    maxConeSize, maxSupportSize;
503552f7358SJed Brown     PetscInt    pStart, pEnd, p;
504552f7358SJed Brown     PetscMPIInt rank, size;
505552f7358SJed Brown 
50682f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
50782f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
508552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
509552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
510552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
511552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "Mesh '%s':\n", name);CHKERRQ(ierr);
512552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "orientation is missing\n", name);CHKERRQ(ierr);
513552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "cap --> base:\n", name);CHKERRQ(ierr);
5144d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
5154d4c343aSBarry Smith     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max sizes cone: %D support: %D\n", rank,maxConeSize, maxSupportSize);CHKERRQ(ierr);
516552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
517552f7358SJed Brown       PetscInt dof, off, s;
518552f7358SJed Brown 
519552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
520552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
521552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
522e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
523552f7358SJed Brown       }
524552f7358SJed Brown     }
525552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
526552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "base <-- cap:\n", name);CHKERRQ(ierr);
527552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
528552f7358SJed Brown       PetscInt dof, off, c;
529552f7358SJed Brown 
530552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
531552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
532552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
533e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
534552f7358SJed Brown       }
535552f7358SJed Brown     }
536552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
5374d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
5380298fd71SBarry Smith     ierr = PetscSectionGetChart(coordSection, &pStart, NULL);CHKERRQ(ierr);
539552f7358SJed Brown     if (pStart >= 0) {ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);}
540c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
541552f7358SJed Brown     ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);
542552f7358SJed Brown     if (size > 1) {
543552f7358SJed Brown       PetscSF sf;
544552f7358SJed Brown 
545552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
546552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
547552f7358SJed Brown     }
548552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
549552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
5500588280cSMatthew G. Knepley     const char  *name, *color;
5510588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
5520588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
553552f7358SJed Brown     PetscReal    scale         = 2.0;
5540588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
5550588280cSMatthew G. Knepley     double       tcoords[3];
556552f7358SJed Brown     PetscScalar *coords;
5570588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
558552f7358SJed Brown     PetscMPIInt  rank, size;
5590588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
560552f7358SJed Brown 
5610588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
562552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
563c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
5640588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
5650588280cSMatthew G. Knepley     numColors  = 10;
5660588280cSMatthew G. Knepley     numLColors = 10;
5670588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
568c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
569c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
570c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
5710588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
572c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
5730588280cSMatthew G. Knepley     if (!useColors) {
5740588280cSMatthew G. Knepley       numColors = 3;
5750588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
5760588280cSMatthew G. Knepley     }
577c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
5780588280cSMatthew G. Knepley     if (!useColors) {
5790588280cSMatthew G. Knepley       numLColors = 4;
5800588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
5810588280cSMatthew G. Knepley     }
58282f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
58382f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
584552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
585770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
5860588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
587552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
588552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
589552f7358SJed Brown \\usetikzlibrary{arrows}\n\
5900588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
5910588280cSMatthew G. Knepley     if (size > 1) {
592f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
593770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
594770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
595770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
596770b213bSMatthew G Knepley         } else if (p > 0) {
597770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
598770b213bSMatthew G Knepley         }
599770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
600770b213bSMatthew G Knepley       }
6010588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
6020588280cSMatthew G. Knepley     }
6030588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", 1.0);CHKERRQ(ierr);
604552f7358SJed Brown     /* Plot vertices */
605552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
606552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
6074d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
608552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
609552f7358SJed Brown       PetscInt  off, dof, d;
6100588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
611552f7358SJed Brown 
612552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
613552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
6140588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
615f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
6160588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
6170588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
6180588280cSMatthew G. Knepley         tcoords[d] = PetscAbsReal(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
6190588280cSMatthew G. Knepley       }
6200588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
6210588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
622552f7358SJed Brown       for (d = 0; d < dof; ++d) {
623552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
6240588280cSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]);CHKERRQ(ierr);
625552f7358SJed Brown       }
6260588280cSMatthew G. Knepley       color = colors[rank%numColors];
6270588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
6280588280cSMatthew G. Knepley         PetscInt val;
629c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
6300588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
6310588280cSMatthew G. Knepley       }
6320588280cSMatthew G. Knepley       if (useNumbers) {
633e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
6340588280cSMatthew G. Knepley       } else {
635e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
6360588280cSMatthew G. Knepley       }
637552f7358SJed Brown     }
638552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
639552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
640552f7358SJed Brown     /* Plot edges */
6410588280cSMatthew G. Knepley     if (depth > 1) {ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);}
6420588280cSMatthew G. Knepley     if (dim < 3 && useNumbers) {
643552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
644552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
645552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
646552f7358SJed Brown         const PetscInt *cone;
647552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
648552f7358SJed Brown 
649552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
650f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
651552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
652552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
653552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
654552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
655552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
656552f7358SJed Brown         for (d = 0; d < dof; ++d) {
6570588280cSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
6580588280cSMatthew G. Knepley           tcoords[d] = PetscAbsReal(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
659552f7358SJed Brown         }
6600588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
6610588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
6620588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
6630588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
664f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
6650588280cSMatthew G. Knepley         }
6660588280cSMatthew G. Knepley         color = colors[rank%numColors];
6670588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
6680588280cSMatthew G. Knepley           PetscInt val;
669c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
6700750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
6710588280cSMatthew G. Knepley         }
672e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
673552f7358SJed Brown       }
674552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
675552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
676552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
6770588280cSMatthew G. Knepley     }
678552f7358SJed Brown     /* Plot cells */
6790588280cSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
6800588280cSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
6810588280cSMatthew G. Knepley         const PetscInt *cone;
6820588280cSMatthew G. Knepley 
6830588280cSMatthew G. Knepley         color = colors[rank%numColors];
6840588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
6850588280cSMatthew G. Knepley           PetscInt val;
686c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
6870588280cSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
6880588280cSMatthew G. Knepley         }
6890588280cSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
690e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
6910588280cSMatthew G. Knepley       }
6920588280cSMatthew G. Knepley     } else {
693552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
694552f7358SJed Brown       for (c = cStart; c < cEnd; ++c) {
6950298fd71SBarry Smith         PetscInt *closure = NULL;
696552f7358SJed Brown         PetscInt  closureSize, firstPoint = -1;
697552f7358SJed Brown 
698552f7358SJed Brown         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
699552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
700552f7358SJed Brown         for (p = 0; p < closureSize*2; p += 2) {
701552f7358SJed Brown           const PetscInt point = closure[p];
702552f7358SJed Brown 
703552f7358SJed Brown           if ((point < vStart) || (point >= vEnd)) continue;
704552f7358SJed Brown           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
705e4b003c7SBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
706552f7358SJed Brown           if (firstPoint < 0) firstPoint = point;
707552f7358SJed Brown         }
708552f7358SJed Brown         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
709e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
710552f7358SJed Brown         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
711552f7358SJed Brown       }
7120588280cSMatthew G. Knepley     }
713552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
7144d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
7150588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
716770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
7170588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
7180588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
7190588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
7200588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
721552f7358SJed Brown   } else {
72282f516ccSBarry Smith     MPI_Comm    comm;
723834065abSMatthew G. Knepley     PetscInt   *sizes, *hybsizes;
724834065abSMatthew G. Knepley     PetscInt    locDepth, depth, dim, d, pMax[4];
725552f7358SJed Brown     PetscInt    pStart, pEnd, p;
726a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
7271143a3c0SMatthew G. Knepley     const char *name;
728552f7358SJed Brown     PetscMPIInt size;
729552f7358SJed Brown 
73082f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
731552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
732c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7335f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
7341143a3c0SMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimensions:\n", name, dim);CHKERRQ(ierr);}
7351143a3c0SMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimensions:\n", dim);CHKERRQ(ierr);}
736552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
737b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
738bb81ee50SMatthew G. Knepley     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, &pMax[1], &pMax[0]);CHKERRQ(ierr);
739dcca6d9dSJed Brown     ierr = PetscMalloc2(size,&sizes,size,&hybsizes);CHKERRQ(ierr);
740552f7358SJed Brown     if (depth == 1) {
741552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
742552f7358SJed Brown       pEnd = pEnd - pStart;
743552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
744f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);CHKERRQ(ierr);
745552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
746552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
747552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
748552f7358SJed Brown       pEnd = pEnd - pStart;
749552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
750552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
751552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
752552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
753552f7358SJed Brown     } else {
754cbb7f117SMark Adams       PetscMPIInt rank;
755cbb7f117SMark Adams       ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
756552f7358SJed Brown       for (d = 0; d <= dim; d++) {
757552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
758834065abSMatthew G. Knepley         pEnd    -= pStart;
759834065abSMatthew G. Knepley         pMax[d] -= pStart;
760552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
761834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
762552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
763834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
764cbb7f117SMark Adams           if (!rank) {
765834065abSMatthew G. Knepley             if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
766834065abSMatthew G. Knepley             else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
767834065abSMatthew G. Knepley           }
768cbb7f117SMark Adams         }
769552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
770552f7358SJed Brown       }
771552f7358SJed Brown     }
772834065abSMatthew G. Knepley     ierr = PetscFree2(sizes,hybsizes);CHKERRQ(ierr);
773c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
774a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
775a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
776a57dd577SMatthew G Knepley       DMLabel         label;
777a57dd577SMatthew G Knepley       const char     *name;
778a57dd577SMatthew G Knepley       IS              valueIS;
779a57dd577SMatthew G Knepley       const PetscInt *values;
780a57dd577SMatthew G Knepley       PetscInt        numValues, v;
781a57dd577SMatthew G Knepley 
782c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
783c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
784a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
785f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata of sizes (", name, numValues);CHKERRQ(ierr);
786a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
787a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
788120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
789a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
790a57dd577SMatthew G Knepley         PetscInt size;
791a57dd577SMatthew G Knepley 
792a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
793a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
794f347f43bSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer, "%D", size);CHKERRQ(ierr);
795a57dd577SMatthew G Knepley       }
796a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
797120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
798a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
7994d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
800a57dd577SMatthew G Knepley     }
801a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
8028e7ff633SMatthew G. Knepley     if (cdm) {
8038e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
8048e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
8058e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
8068e7ff633SMatthew G. Knepley     }
807552f7358SJed Brown   }
808552f7358SJed Brown   PetscFunctionReturn(0);
809552f7358SJed Brown }
810552f7358SJed Brown 
8117cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
812e412dcbdSMatthew G. Knepley {
813e412dcbdSMatthew G. Knepley   PetscDraw          draw;
814e412dcbdSMatthew G. Knepley   DM                 cdm;
815e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
816e412dcbdSMatthew G. Knepley   Vec                coordinates;
817e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
818e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
819e412dcbdSMatthew G. Knepley   PetscBool          isnull;
820e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
821e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
822e412dcbdSMatthew G. Knepley 
823e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
824e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
825e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
826e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
827e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
828e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
829e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
830e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
831e412dcbdSMatthew G. Knepley 
832e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
833e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
834e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
835e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
836e412dcbdSMatthew G. Knepley 
837e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
838e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
839e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
8400c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
8410c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
842e412dcbdSMatthew G. Knepley   }
843e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
844e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
845e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
846e412dcbdSMatthew G. Knepley 
847e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
848e412dcbdSMatthew G. Knepley     PetscScalar *coords = NULL;
849e412dcbdSMatthew G. Knepley     PetscInt     numCoords;
850e412dcbdSMatthew G. Knepley 
851e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
852e412dcbdSMatthew G. Knepley     switch (numCoords) {
853e412dcbdSMatthew G. Knepley     case 6:
8540c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8550c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8560c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
857e412dcbdSMatthew G. Knepley       break;
858e412dcbdSMatthew G. Knepley     case 8:
8590c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8600c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8610c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8620c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
863e412dcbdSMatthew G. Knepley       break;
864e412dcbdSMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
865e412dcbdSMatthew G. Knepley     }
866e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
867e412dcbdSMatthew G. Knepley   }
868e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
869e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
870e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
871e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
872e412dcbdSMatthew G. Knepley }
873e412dcbdSMatthew G. Knepley 
874552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
875552f7358SJed Brown {
876e412dcbdSMatthew G. Knepley   PetscBool      iascii, ishdf5, isvtk, isdraw;
877552f7358SJed Brown   PetscErrorCode ierr;
878552f7358SJed Brown 
879552f7358SJed Brown   PetscFunctionBegin;
880552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
881552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
882552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
883fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
884c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
885e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
886552f7358SJed Brown   if (iascii) {
887552f7358SJed Brown     ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
888c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
889c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
8907afe7537SMatthew G. Knepley     ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_VIZ);CHKERRQ(ierr);
89139d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
8927afe7537SMatthew G. Knepley     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
893c6ccd67eSMatthew G. Knepley #else
894c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
895552f7358SJed Brown #endif
896e412dcbdSMatthew G. Knepley   } else if (isvtk) {
897fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
898e412dcbdSMatthew G. Knepley   } else if (isdraw) {
899e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
900fcf6c8fdSToby Isaac   }
901552f7358SJed Brown   PetscFunctionReturn(0);
902552f7358SJed Brown }
903552f7358SJed Brown 
9042c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
9052c40f234SMatthew G. Knepley {
9062c40f234SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
9072c40f234SMatthew G. Knepley   PetscErrorCode ierr;
9082c40f234SMatthew G. Knepley 
9092c40f234SMatthew G. Knepley   PetscFunctionBegin;
9102c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9112c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
9122c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERBINARY, &isbinary);CHKERRQ(ierr);
9132c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
9142c40f234SMatthew G. Knepley   if (isbinary) {SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Do not yet support binary viewers");}
9152c40f234SMatthew G. Knepley   else if (ishdf5) {
9162c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
91739d25373SMatthew G. Knepley     ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
9182c40f234SMatthew G. Knepley #else
9192c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
920552f7358SJed Brown #endif
921552f7358SJed Brown   }
922552f7358SJed Brown   PetscFunctionReturn(0);
923552f7358SJed Brown }
924552f7358SJed Brown 
925552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
926552f7358SJed Brown {
927552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
928552f7358SJed Brown   PetscErrorCode ierr;
929552f7358SJed Brown 
930552f7358SJed Brown   PetscFunctionBegin;
9310d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
932552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
933552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
934552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
935552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
936be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
937552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
938552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
939d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
940d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
94177623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
942a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
943552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
944552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
945a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
946a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
947d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
948d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
949d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
950d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
951d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
952d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
953fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
954552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
955552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
956713918a9SToby Isaac   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMAdaptLabel_C",NULL);CHKERRQ(ierr);
957552f7358SJed Brown   PetscFunctionReturn(0);
958552f7358SJed Brown }
959552f7358SJed Brown 
960b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
961552f7358SJed Brown {
9628d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
963acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
964552f7358SJed Brown   PetscInt               localSize;
965837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
966552f7358SJed Brown   PetscErrorCode         ierr;
967b412c318SBarry Smith   MatType                mtype;
9681428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
969552f7358SJed Brown 
970552f7358SJed Brown   PetscFunctionBegin;
971607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
972b412c318SBarry Smith   mtype = dm->mattype;
973552f7358SJed Brown   ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
974552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
975552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
97682f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
977552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
978552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
979552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
980acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
981acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
982552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
983552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
984552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
985552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
986552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
987552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
988552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
989837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
990552f7358SJed Brown   if (!isShell) {
991be36d101SStefano Zampini     PetscSection subSection;
992837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
993be36d101SStefano Zampini     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal, bsMax, bsMin, *ltogidx, lsize;
994fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
995552f7358SJed Brown 
99600140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
997be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
998be36d101SStefano Zampini       PetscSection section;
999be36d101SStefano Zampini       PetscInt     size;
1000be36d101SStefano Zampini 
1001be36d101SStefano Zampini       ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1002be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1003be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1004be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1005be36d101SStefano Zampini     } else {
100600140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1007be36d101SStefano Zampini     }
1008552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1009be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1010a9d99c84SMatthew G. Knepley       PetscInt bdof;
1011a9d99c84SMatthew G. Knepley 
1012552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1013fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
10141d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
10151d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
10161d17a0a3SMatthew G. Knepley       if (dof) {
10171d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1018be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1019be36d101SStefano Zampini       }
1020be36d101SStefano Zampini       if (isMatIS) {
1021be36d101SStefano Zampini         PetscInt loff,c,off;
1022be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1023be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1024be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1025552f7358SJed Brown       }
10262a28c762SMatthew G Knepley     }
10272a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
10282a28c762SMatthew G Knepley     bsLocal = bs;
1029b2566f29SBarry Smith     ierr = MPIU_Allreduce(&bsLocal, &bsMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
10302a28c762SMatthew G Knepley     bsLocal = bs < 0 ? bsMax : bs;
1031b2566f29SBarry Smith     ierr = MPIU_Allreduce(&bsLocal, &bsMin, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1032bff27382SMatthew G. Knepley     if (bsMin != bsMax) {bs = 1;}
1033bff27382SMatthew G. Knepley     else                {bs = bsMax;}
10344fa26246SMatthew G. Knepley     bs   = bs < 0 ? 1 : bs;
1035be36d101SStefano Zampini     if (isMatIS) {
10364fa26246SMatthew G. Knepley       PetscInt l;
10374fa26246SMatthew G. Knepley       /* Must reduce indices by blocksize */
10384fa26246SMatthew G. Knepley       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] /= bs;
10394fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1040be36d101SStefano Zampini     }
1041be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1042be36d101SStefano Zampini     if (isMatIS) {
1043be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1044be36d101SStefano Zampini     }
10451795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
10468d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1047552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1048552f7358SJed Brown   }
1049b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1050552f7358SJed Brown   PetscFunctionReturn(0);
1051552f7358SJed Brown }
1052552f7358SJed Brown 
10537cd05799SMatthew G. Knepley /*@
1054a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1055be36d101SStefano Zampini 
1056be36d101SStefano Zampini   Not collective
1057be36d101SStefano Zampini 
1058be36d101SStefano Zampini   Input Parameter:
1059be36d101SStefano Zampini . mesh - The DMPlex
1060be36d101SStefano Zampini 
1061be36d101SStefano Zampini   Output Parameters:
1062be36d101SStefano Zampini . subsection - The subdomain section
1063be36d101SStefano Zampini 
1064be36d101SStefano Zampini   Level: developer
1065be36d101SStefano Zampini 
1066be36d101SStefano Zampini .seealso:
10677cd05799SMatthew G. Knepley @*/
1068be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1069be36d101SStefano Zampini {
1070be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1071be36d101SStefano Zampini   PetscErrorCode ierr;
1072be36d101SStefano Zampini 
1073be36d101SStefano Zampini   PetscFunctionBegin;
1074be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1075be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1076be36d101SStefano Zampini     PetscSection section;
1077be36d101SStefano Zampini     PetscSF      sf;
1078be36d101SStefano Zampini 
1079be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
1080be36d101SStefano Zampini     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
1081be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1082be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1083be36d101SStefano Zampini   }
1084be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1085be36d101SStefano Zampini   PetscFunctionReturn(0);
1086be36d101SStefano Zampini }
1087be36d101SStefano Zampini 
1088552f7358SJed Brown /*@
1089552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1090552f7358SJed Brown 
1091552f7358SJed Brown   Not collective
1092552f7358SJed Brown 
1093552f7358SJed Brown   Input Parameter:
1094552f7358SJed Brown . mesh - The DMPlex
1095552f7358SJed Brown 
1096552f7358SJed Brown   Output Parameters:
1097552f7358SJed Brown + pStart - The first mesh point
1098552f7358SJed Brown - pEnd   - The upper bound for mesh points
1099552f7358SJed Brown 
1100552f7358SJed Brown   Level: beginner
1101552f7358SJed Brown 
1102552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1103552f7358SJed Brown @*/
1104552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1105552f7358SJed Brown {
1106552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1107552f7358SJed Brown   PetscErrorCode ierr;
1108552f7358SJed Brown 
1109552f7358SJed Brown   PetscFunctionBegin;
1110552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1111552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1112552f7358SJed Brown   PetscFunctionReturn(0);
1113552f7358SJed Brown }
1114552f7358SJed Brown 
1115552f7358SJed Brown /*@
1116552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1117552f7358SJed Brown 
1118552f7358SJed Brown   Not collective
1119552f7358SJed Brown 
1120552f7358SJed Brown   Input Parameters:
1121552f7358SJed Brown + mesh - The DMPlex
1122552f7358SJed Brown . pStart - The first mesh point
1123552f7358SJed Brown - pEnd   - The upper bound for mesh points
1124552f7358SJed Brown 
1125552f7358SJed Brown   Output Parameters:
1126552f7358SJed Brown 
1127552f7358SJed Brown   Level: beginner
1128552f7358SJed Brown 
1129552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1130552f7358SJed Brown @*/
1131552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1132552f7358SJed Brown {
1133552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1134552f7358SJed Brown   PetscErrorCode ierr;
1135552f7358SJed Brown 
1136552f7358SJed Brown   PetscFunctionBegin;
1137552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1138552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1139552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1140552f7358SJed Brown   PetscFunctionReturn(0);
1141552f7358SJed Brown }
1142552f7358SJed Brown 
1143552f7358SJed Brown /*@
1144552f7358SJed Brown   DMPlexGetConeSize - Return the number of in-edges for this point in the Sieve DAG
1145552f7358SJed Brown 
1146552f7358SJed Brown   Not collective
1147552f7358SJed Brown 
1148552f7358SJed Brown   Input Parameters:
1149552f7358SJed Brown + mesh - The DMPlex
1150552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1151552f7358SJed Brown 
1152552f7358SJed Brown   Output Parameter:
1153552f7358SJed Brown . size - The cone size for point p
1154552f7358SJed Brown 
1155552f7358SJed Brown   Level: beginner
1156552f7358SJed Brown 
1157552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1158552f7358SJed Brown @*/
1159552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1160552f7358SJed Brown {
1161552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1162552f7358SJed Brown   PetscErrorCode ierr;
1163552f7358SJed Brown 
1164552f7358SJed Brown   PetscFunctionBegin;
1165552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1166552f7358SJed Brown   PetscValidPointer(size, 3);
1167552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1168552f7358SJed Brown   PetscFunctionReturn(0);
1169552f7358SJed Brown }
1170552f7358SJed Brown 
1171552f7358SJed Brown /*@
1172552f7358SJed Brown   DMPlexSetConeSize - Set the number of in-edges for this point in the Sieve DAG
1173552f7358SJed Brown 
1174552f7358SJed Brown   Not collective
1175552f7358SJed Brown 
1176552f7358SJed Brown   Input Parameters:
1177552f7358SJed Brown + mesh - The DMPlex
1178552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1179552f7358SJed Brown - size - The cone size for point p
1180552f7358SJed Brown 
1181552f7358SJed Brown   Output Parameter:
1182552f7358SJed Brown 
1183552f7358SJed Brown   Note:
1184552f7358SJed Brown   This should be called after DMPlexSetChart().
1185552f7358SJed Brown 
1186552f7358SJed Brown   Level: beginner
1187552f7358SJed Brown 
1188552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1189552f7358SJed Brown @*/
1190552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1191552f7358SJed Brown {
1192552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1193552f7358SJed Brown   PetscErrorCode ierr;
1194552f7358SJed Brown 
1195552f7358SJed Brown   PetscFunctionBegin;
1196552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1197552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
11980d644c17SKarl Rupp 
1199552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1200552f7358SJed Brown   PetscFunctionReturn(0);
1201552f7358SJed Brown }
1202552f7358SJed Brown 
1203f5a469b9SMatthew G. Knepley /*@
1204f5a469b9SMatthew G. Knepley   DMPlexAddConeSize - Add the given number of in-edges to this point in the Sieve DAG
1205f5a469b9SMatthew G. Knepley 
1206f5a469b9SMatthew G. Knepley   Not collective
1207f5a469b9SMatthew G. Knepley 
1208f5a469b9SMatthew G. Knepley   Input Parameters:
1209f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1210f5a469b9SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1211f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1212f5a469b9SMatthew G. Knepley 
1213f5a469b9SMatthew G. Knepley   Output Parameter:
1214f5a469b9SMatthew G. Knepley 
1215f5a469b9SMatthew G. Knepley   Note:
1216f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1217f5a469b9SMatthew G. Knepley 
1218f5a469b9SMatthew G. Knepley   Level: beginner
1219f5a469b9SMatthew G. Knepley 
1220f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1221f5a469b9SMatthew G. Knepley @*/
1222f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1223f5a469b9SMatthew G. Knepley {
1224f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1225f5a469b9SMatthew G. Knepley   PetscInt       csize;
1226f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1227f5a469b9SMatthew G. Knepley 
1228f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1229f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1230f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1231f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1232f5a469b9SMatthew G. Knepley 
1233f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1234f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1235f5a469b9SMatthew G. Knepley }
1236f5a469b9SMatthew G. Knepley 
1237552f7358SJed Brown /*@C
1238552f7358SJed Brown   DMPlexGetCone - Return the points on the in-edges for this point in the Sieve DAG
1239552f7358SJed Brown 
1240552f7358SJed Brown   Not collective
1241552f7358SJed Brown 
1242552f7358SJed Brown   Input Parameters:
1243552f7358SJed Brown + mesh - The DMPlex
1244552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1245552f7358SJed Brown 
1246552f7358SJed Brown   Output Parameter:
1247552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1248552f7358SJed Brown 
1249552f7358SJed Brown   Level: beginner
1250552f7358SJed Brown 
12513813dfbdSMatthew G Knepley   Fortran Notes:
12523813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
12533813dfbdSMatthew G Knepley   include petsc.h90 in your code.
12543813dfbdSMatthew G Knepley 
12553813dfbdSMatthew G Knepley   You must also call DMPlexRestoreCone() after you finish using the returned array.
1256552f7358SJed Brown 
1257552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart()
1258552f7358SJed Brown @*/
1259552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1260552f7358SJed Brown {
1261552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1262552f7358SJed Brown   PetscInt       off;
1263552f7358SJed Brown   PetscErrorCode ierr;
1264552f7358SJed Brown 
1265552f7358SJed Brown   PetscFunctionBegin;
1266552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1267552f7358SJed Brown   PetscValidPointer(cone, 3);
1268552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1269552f7358SJed Brown   *cone = &mesh->cones[off];
1270552f7358SJed Brown   PetscFunctionReturn(0);
1271552f7358SJed Brown }
1272552f7358SJed Brown 
1273552f7358SJed Brown /*@
1274552f7358SJed Brown   DMPlexSetCone - Set the points on the in-edges for this point in the Sieve DAG
1275552f7358SJed Brown 
1276552f7358SJed Brown   Not collective
1277552f7358SJed Brown 
1278552f7358SJed Brown   Input Parameters:
1279552f7358SJed Brown + mesh - The DMPlex
1280552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1281552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1282552f7358SJed Brown 
1283552f7358SJed Brown   Output Parameter:
1284552f7358SJed Brown 
1285552f7358SJed Brown   Note:
1286552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1287552f7358SJed Brown 
1288552f7358SJed Brown   Level: beginner
1289552f7358SJed Brown 
1290552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1291552f7358SJed Brown @*/
1292552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1293552f7358SJed Brown {
1294552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1295552f7358SJed Brown   PetscInt       pStart, pEnd;
1296552f7358SJed Brown   PetscInt       dof, off, c;
1297552f7358SJed Brown   PetscErrorCode ierr;
1298552f7358SJed Brown 
1299552f7358SJed Brown   PetscFunctionBegin;
1300552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1301552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1302552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1303552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1304552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
130582f516ccSBarry 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);
1306552f7358SJed Brown   for (c = 0; c < dof; ++c) {
130782f516ccSBarry 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);
1308552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1309552f7358SJed Brown   }
1310552f7358SJed Brown   PetscFunctionReturn(0);
1311552f7358SJed Brown }
1312552f7358SJed Brown 
1313552f7358SJed Brown /*@C
1314552f7358SJed Brown   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the Sieve DAG
1315552f7358SJed Brown 
1316552f7358SJed Brown   Not collective
1317552f7358SJed Brown 
1318552f7358SJed Brown   Input Parameters:
1319552f7358SJed Brown + mesh - The DMPlex
1320552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1321552f7358SJed Brown 
1322552f7358SJed Brown   Output Parameter:
1323552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1324552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1325552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1326552f7358SJed Brown                     the index of the cone point on which to start.
1327552f7358SJed Brown 
1328552f7358SJed Brown   Level: beginner
1329552f7358SJed Brown 
13303813dfbdSMatthew G Knepley   Fortran Notes:
13313813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
13323813dfbdSMatthew G Knepley   include petsc.h90 in your code.
13333813dfbdSMatthew G Knepley 
13343813dfbdSMatthew G Knepley   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1335552f7358SJed Brown 
1336552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1337552f7358SJed Brown @*/
1338552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1339552f7358SJed Brown {
1340552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1341552f7358SJed Brown   PetscInt       off;
1342552f7358SJed Brown   PetscErrorCode ierr;
1343552f7358SJed Brown 
1344552f7358SJed Brown   PetscFunctionBegin;
1345552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1346552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1347552f7358SJed Brown   {
1348552f7358SJed Brown     PetscInt dof;
1349552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1350552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1351552f7358SJed Brown   }
1352552f7358SJed Brown #endif
1353552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
13540d644c17SKarl Rupp 
1355552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1356552f7358SJed Brown   PetscFunctionReturn(0);
1357552f7358SJed Brown }
1358552f7358SJed Brown 
1359552f7358SJed Brown /*@
1360552f7358SJed Brown   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the Sieve DAG
1361552f7358SJed Brown 
1362552f7358SJed Brown   Not collective
1363552f7358SJed Brown 
1364552f7358SJed Brown   Input Parameters:
1365552f7358SJed Brown + mesh - The DMPlex
1366552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1367552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1368552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1369552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1370552f7358SJed Brown                     the index of the cone point on which to start.
1371552f7358SJed Brown 
1372552f7358SJed Brown   Output Parameter:
1373552f7358SJed Brown 
1374552f7358SJed Brown   Note:
1375552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1376552f7358SJed Brown 
1377552f7358SJed Brown   Level: beginner
1378552f7358SJed Brown 
1379552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1380552f7358SJed Brown @*/
1381552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
1382552f7358SJed Brown {
1383552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1384552f7358SJed Brown   PetscInt       pStart, pEnd;
1385552f7358SJed Brown   PetscInt       dof, off, c;
1386552f7358SJed Brown   PetscErrorCode ierr;
1387552f7358SJed Brown 
1388552f7358SJed Brown   PetscFunctionBegin;
1389552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1390552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1391552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1392552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
1393552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
139482f516ccSBarry 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);
1395552f7358SJed Brown   for (c = 0; c < dof; ++c) {
1396552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
1397552f7358SJed Brown 
1398552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
139982f516ccSBarry 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);
1400552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
1401552f7358SJed Brown   }
1402552f7358SJed Brown   PetscFunctionReturn(0);
1403552f7358SJed Brown }
1404552f7358SJed Brown 
14057cd05799SMatthew G. Knepley /*@
14067cd05799SMatthew G. Knepley   DMPlexInsertCone - Insert a point into the in-edges for the point p in the Sieve DAG
14077cd05799SMatthew G. Knepley 
14087cd05799SMatthew G. Knepley   Not collective
14097cd05799SMatthew G. Knepley 
14107cd05799SMatthew G. Knepley   Input Parameters:
14117cd05799SMatthew G. Knepley + mesh - The DMPlex
14127cd05799SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
14137cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
14147cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
14157cd05799SMatthew G. Knepley 
14167cd05799SMatthew G. Knepley   Level: beginner
14177cd05799SMatthew G. Knepley 
14187cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
14197cd05799SMatthew G. Knepley @*/
1420552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
1421552f7358SJed Brown {
1422552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1423552f7358SJed Brown   PetscInt       pStart, pEnd;
1424552f7358SJed Brown   PetscInt       dof, off;
1425552f7358SJed Brown   PetscErrorCode ierr;
1426552f7358SJed Brown 
1427552f7358SJed Brown   PetscFunctionBegin;
1428552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1429552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
143082f516ccSBarry 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);
143182f516ccSBarry 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);
143277c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
143377c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
143477c88f5bSMatthew 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);
1435552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
1436552f7358SJed Brown   PetscFunctionReturn(0);
1437552f7358SJed Brown }
1438552f7358SJed Brown 
14397cd05799SMatthew G. Knepley /*@
14407cd05799SMatthew G. Knepley   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the Sieve DAG
14417cd05799SMatthew G. Knepley 
14427cd05799SMatthew G. Knepley   Not collective
14437cd05799SMatthew G. Knepley 
14447cd05799SMatthew G. Knepley   Input Parameters:
14457cd05799SMatthew G. Knepley + mesh - The DMPlex
14467cd05799SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
14477cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
14487cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
14497cd05799SMatthew G. Knepley 
14507cd05799SMatthew G. Knepley   Level: beginner
14517cd05799SMatthew G. Knepley 
14527cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
14537cd05799SMatthew G. Knepley @*/
145477c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
145577c88f5bSMatthew G Knepley {
145677c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
145777c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
145877c88f5bSMatthew G Knepley   PetscInt       dof, off;
145977c88f5bSMatthew G Knepley   PetscErrorCode ierr;
146077c88f5bSMatthew G Knepley 
146177c88f5bSMatthew G Knepley   PetscFunctionBegin;
146277c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
146377c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
146477c88f5bSMatthew 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);
146577c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
146677c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
146777c88f5bSMatthew 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);
146877c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
146977c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
147077c88f5bSMatthew G Knepley }
147177c88f5bSMatthew G Knepley 
1472552f7358SJed Brown /*@
1473552f7358SJed Brown   DMPlexGetSupportSize - Return the number of out-edges for this point in the Sieve DAG
1474552f7358SJed Brown 
1475552f7358SJed Brown   Not collective
1476552f7358SJed Brown 
1477552f7358SJed Brown   Input Parameters:
1478552f7358SJed Brown + mesh - The DMPlex
1479552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1480552f7358SJed Brown 
1481552f7358SJed Brown   Output Parameter:
1482552f7358SJed Brown . size - The support size for point p
1483552f7358SJed Brown 
1484552f7358SJed Brown   Level: beginner
1485552f7358SJed Brown 
1486552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
1487552f7358SJed Brown @*/
1488552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
1489552f7358SJed Brown {
1490552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1491552f7358SJed Brown   PetscErrorCode ierr;
1492552f7358SJed Brown 
1493552f7358SJed Brown   PetscFunctionBegin;
1494552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1495552f7358SJed Brown   PetscValidPointer(size, 3);
1496552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
1497552f7358SJed Brown   PetscFunctionReturn(0);
1498552f7358SJed Brown }
1499552f7358SJed Brown 
1500552f7358SJed Brown /*@
1501552f7358SJed Brown   DMPlexSetSupportSize - Set the number of out-edges for this point in the Sieve DAG
1502552f7358SJed Brown 
1503552f7358SJed Brown   Not collective
1504552f7358SJed Brown 
1505552f7358SJed Brown   Input Parameters:
1506552f7358SJed Brown + mesh - The DMPlex
1507552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1508552f7358SJed Brown - size - The support size for point p
1509552f7358SJed Brown 
1510552f7358SJed Brown   Output Parameter:
1511552f7358SJed Brown 
1512552f7358SJed Brown   Note:
1513552f7358SJed Brown   This should be called after DMPlexSetChart().
1514552f7358SJed Brown 
1515552f7358SJed Brown   Level: beginner
1516552f7358SJed Brown 
1517552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
1518552f7358SJed Brown @*/
1519552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
1520552f7358SJed Brown {
1521552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1522552f7358SJed Brown   PetscErrorCode ierr;
1523552f7358SJed Brown 
1524552f7358SJed Brown   PetscFunctionBegin;
1525552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1526552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
15270d644c17SKarl Rupp 
1528552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
1529552f7358SJed Brown   PetscFunctionReturn(0);
1530552f7358SJed Brown }
1531552f7358SJed Brown 
1532552f7358SJed Brown /*@C
1533552f7358SJed Brown   DMPlexGetSupport - Return the points on the out-edges for this point in the Sieve DAG
1534552f7358SJed Brown 
1535552f7358SJed Brown   Not collective
1536552f7358SJed Brown 
1537552f7358SJed Brown   Input Parameters:
1538552f7358SJed Brown + mesh - The DMPlex
1539552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1540552f7358SJed Brown 
1541552f7358SJed Brown   Output Parameter:
1542552f7358SJed Brown . support - An array of points which are on the out-edges for point p
1543552f7358SJed Brown 
1544552f7358SJed Brown   Level: beginner
1545552f7358SJed Brown 
15463813dfbdSMatthew G Knepley   Fortran Notes:
15473813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
15483813dfbdSMatthew G Knepley   include petsc.h90 in your code.
15493813dfbdSMatthew G Knepley 
15503813dfbdSMatthew G Knepley   You must also call DMPlexRestoreSupport() after you finish using the returned array.
15513813dfbdSMatthew G Knepley 
1552552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1553552f7358SJed Brown @*/
1554552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
1555552f7358SJed Brown {
1556552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1557552f7358SJed Brown   PetscInt       off;
1558552f7358SJed Brown   PetscErrorCode ierr;
1559552f7358SJed Brown 
1560552f7358SJed Brown   PetscFunctionBegin;
1561552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1562552f7358SJed Brown   PetscValidPointer(support, 3);
1563552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
1564552f7358SJed Brown   *support = &mesh->supports[off];
1565552f7358SJed Brown   PetscFunctionReturn(0);
1566552f7358SJed Brown }
1567552f7358SJed Brown 
1568552f7358SJed Brown /*@
1569552f7358SJed Brown   DMPlexSetSupport - Set the points on the out-edges for this point in the Sieve DAG
1570552f7358SJed Brown 
1571552f7358SJed Brown   Not collective
1572552f7358SJed Brown 
1573552f7358SJed Brown   Input Parameters:
1574552f7358SJed Brown + mesh - The DMPlex
1575552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1576552f7358SJed Brown - support - An array of points which are on the in-edges for point p
1577552f7358SJed Brown 
1578552f7358SJed Brown   Output Parameter:
1579552f7358SJed Brown 
1580552f7358SJed Brown   Note:
1581552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
1582552f7358SJed Brown 
1583552f7358SJed Brown   Level: beginner
1584552f7358SJed Brown 
1585552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
1586552f7358SJed Brown @*/
1587552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
1588552f7358SJed Brown {
1589552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1590552f7358SJed Brown   PetscInt       pStart, pEnd;
1591552f7358SJed Brown   PetscInt       dof, off, c;
1592552f7358SJed Brown   PetscErrorCode ierr;
1593552f7358SJed Brown 
1594552f7358SJed Brown   PetscFunctionBegin;
1595552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1596552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1597552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1598552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
1599552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
160082f516ccSBarry 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);
1601552f7358SJed Brown   for (c = 0; c < dof; ++c) {
160282f516ccSBarry 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);
1603552f7358SJed Brown     mesh->supports[off+c] = support[c];
1604552f7358SJed Brown   }
1605552f7358SJed Brown   PetscFunctionReturn(0);
1606552f7358SJed Brown }
1607552f7358SJed Brown 
16087cd05799SMatthew G. Knepley /*@
16097cd05799SMatthew G. Knepley   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the Sieve DAG
16107cd05799SMatthew G. Knepley 
16117cd05799SMatthew G. Knepley   Not collective
16127cd05799SMatthew G. Knepley 
16137cd05799SMatthew G. Knepley   Input Parameters:
16147cd05799SMatthew G. Knepley + mesh - The DMPlex
16157cd05799SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
16167cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
16177cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
16187cd05799SMatthew G. Knepley 
16197cd05799SMatthew G. Knepley   Level: beginner
16207cd05799SMatthew G. Knepley 
16217cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
16227cd05799SMatthew G. Knepley @*/
1623552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
1624552f7358SJed Brown {
1625552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1626552f7358SJed Brown   PetscInt       pStart, pEnd;
1627552f7358SJed Brown   PetscInt       dof, off;
1628552f7358SJed Brown   PetscErrorCode ierr;
1629552f7358SJed Brown 
1630552f7358SJed Brown   PetscFunctionBegin;
1631552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1632552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1633552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1634552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
163582f516ccSBarry 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);
163682f516ccSBarry 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);
163782f516ccSBarry 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);
1638552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
1639552f7358SJed Brown   PetscFunctionReturn(0);
1640552f7358SJed Brown }
1641552f7358SJed Brown 
1642552f7358SJed Brown /*@C
1643552f7358SJed Brown   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the Sieve DAG
1644552f7358SJed Brown 
1645552f7358SJed Brown   Not collective
1646552f7358SJed Brown 
1647552f7358SJed Brown   Input Parameters:
1648552f7358SJed Brown + mesh - The DMPlex
1649552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1650552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
16510298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
1652552f7358SJed Brown 
1653552f7358SJed Brown   Output Parameters:
1654552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
1655552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
1656552f7358SJed Brown 
1657552f7358SJed Brown   Note:
16580298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
1659552f7358SJed Brown 
16603813dfbdSMatthew G Knepley   Fortran Notes:
16613813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16623813dfbdSMatthew G Knepley   include petsc.h90 in your code.
16633813dfbdSMatthew G Knepley 
16643813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
16653813dfbdSMatthew G Knepley 
1666552f7358SJed Brown   Level: beginner
1667552f7358SJed Brown 
1668552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1669552f7358SJed Brown @*/
1670552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1671552f7358SJed Brown {
1672552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
1673552f7358SJed Brown   PetscInt       *closure, *fifo;
16740298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
1675552f7358SJed Brown   PetscInt        tmpSize, t;
1676552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
1677552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
1678552f7358SJed Brown   PetscErrorCode  ierr;
1679552f7358SJed Brown 
1680552f7358SJed Brown   PetscFunctionBegin;
1681552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1682552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1683552f7358SJed Brown   /* This is only 1-level */
1684552f7358SJed Brown   if (useCone) {
1685552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
1686552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
1687552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
1688552f7358SJed Brown   } else {
1689552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
1690552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
1691552f7358SJed Brown   }
1692bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
1693bfbcdd7aSMatthew G. Knepley     if (*points) {
1694bfbcdd7aSMatthew G. Knepley       closure = *points;
1695bfbcdd7aSMatthew G. Knepley     } else {
1696bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
1697bfbcdd7aSMatthew G. Knepley       ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1698bfbcdd7aSMatthew G. Knepley     }
1699bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
1700bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
1701bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
1702bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
1703bfbcdd7aSMatthew G. Knepley     }
1704bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
1705bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
1706bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
1707bfbcdd7aSMatthew G. Knepley   }
170824c766afSToby Isaac   {
170924c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
171024c766afSToby Isaac 
171124c766afSToby Isaac     c = mesh->maxConeSize;
171224c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
171324c766afSToby Isaac     s = mesh->maxSupportSize;
171424c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
171524c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
171624c766afSToby Isaac   }
1717bfbcdd7aSMatthew G. Knepley   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1718bfbcdd7aSMatthew G. Knepley   if (*points) {
1719bfbcdd7aSMatthew G. Knepley     closure = *points;
1720bfbcdd7aSMatthew G. Knepley   } else {
1721bfbcdd7aSMatthew G. Knepley     ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1722bfbcdd7aSMatthew G. Knepley   }
1723bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
1724552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
1725552f7358SJed Brown     const PetscInt cp = tmp[t];
1726552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
1727552f7358SJed Brown 
1728552f7358SJed Brown     closure[closureSize]   = cp;
1729552f7358SJed Brown     closure[closureSize+1] = co;
1730552f7358SJed Brown     fifo[fifoSize]         = cp;
1731552f7358SJed Brown     fifo[fifoSize+1]       = co;
1732552f7358SJed Brown   }
1733bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
1734552f7358SJed Brown   while (fifoSize - fifoStart) {
1735552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
1736552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
1737552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
1738552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
1739552f7358SJed Brown 
1740552f7358SJed Brown     if (useCone) {
1741552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
1742552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
1743552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
1744552f7358SJed Brown     } else {
1745552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
1746552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
17470298fd71SBarry Smith       tmpO = NULL;
1748552f7358SJed Brown     }
1749552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
1750552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
1751552f7358SJed Brown       const PetscInt cp = tmp[i];
17522e1b13c2SMatthew 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. */
17532e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
17542e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
17552e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
1756552f7358SJed Brown       PetscInt       c;
1757552f7358SJed Brown 
17582e1b13c2SMatthew G. Knepley       if (rev) {
17592e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
17602e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
17612e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
17622e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
17632e1b13c2SMatthew G. Knepley       }
1764552f7358SJed Brown       /* Check for duplicate */
1765552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
1766552f7358SJed Brown         if (closure[c] == cp) break;
1767552f7358SJed Brown       }
1768552f7358SJed Brown       if (c == closureSize) {
1769552f7358SJed Brown         closure[closureSize]   = cp;
1770552f7358SJed Brown         closure[closureSize+1] = co;
1771552f7358SJed Brown         fifo[fifoSize]         = cp;
1772552f7358SJed Brown         fifo[fifoSize+1]       = co;
1773552f7358SJed Brown         closureSize           += 2;
1774552f7358SJed Brown         fifoSize              += 2;
1775552f7358SJed Brown       }
1776552f7358SJed Brown     }
1777552f7358SJed Brown     fifoStart += 2;
1778552f7358SJed Brown   }
1779552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
1780552f7358SJed Brown   if (points)    *points    = closure;
1781552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1782552f7358SJed Brown   PetscFunctionReturn(0);
1783552f7358SJed Brown }
1784552f7358SJed Brown 
17859bf0dad6SMatthew G. Knepley /*@C
17869bf0dad6SMatthew 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
17879bf0dad6SMatthew G. Knepley 
17889bf0dad6SMatthew G. Knepley   Not collective
17899bf0dad6SMatthew G. Knepley 
17909bf0dad6SMatthew G. Knepley   Input Parameters:
17919bf0dad6SMatthew G. Knepley + mesh - The DMPlex
17929bf0dad6SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
17939bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
17949bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
17959bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
17969bf0dad6SMatthew G. Knepley 
17979bf0dad6SMatthew G. Knepley   Output Parameters:
17989bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
17999bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
18009bf0dad6SMatthew G. Knepley 
18019bf0dad6SMatthew G. Knepley   Note:
18029bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
18039bf0dad6SMatthew G. Knepley 
18049bf0dad6SMatthew G. Knepley   Fortran Notes:
18059bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
18069bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
18079bf0dad6SMatthew G. Knepley 
18089bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
18099bf0dad6SMatthew G. Knepley 
18109bf0dad6SMatthew G. Knepley   Level: beginner
18119bf0dad6SMatthew G. Knepley 
18129bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
18139bf0dad6SMatthew G. Knepley @*/
18149bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
18159bf0dad6SMatthew G. Knepley {
18169bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
18179bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
18189bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
18199bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
18209bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
18219bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
18229bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
18239bf0dad6SMatthew G. Knepley 
18249bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
18259bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18269bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
18279bf0dad6SMatthew G. Knepley   /* This is only 1-level */
18289bf0dad6SMatthew G. Knepley   if (useCone) {
18299bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
18309bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
18319bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
18329bf0dad6SMatthew G. Knepley   } else {
18339bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
18349bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
18359bf0dad6SMatthew G. Knepley   }
18369bf0dad6SMatthew G. Knepley   if (depth == 1) {
18379bf0dad6SMatthew G. Knepley     if (*points) {
18389bf0dad6SMatthew G. Knepley       closure = *points;
18399bf0dad6SMatthew G. Knepley     } else {
18409bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
18419bf0dad6SMatthew G. Knepley       ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
18429bf0dad6SMatthew G. Knepley     }
18439bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
18449bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
18459bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
18469bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
18479bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
18489bf0dad6SMatthew G. Knepley     }
18499bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
18509bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
18519bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
18529bf0dad6SMatthew G. Knepley   }
185324c766afSToby Isaac   {
185424c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
185524c766afSToby Isaac 
185624c766afSToby Isaac     c = mesh->maxConeSize;
185724c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
185824c766afSToby Isaac     s = mesh->maxSupportSize;
185924c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
186024c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
186124c766afSToby Isaac   }
18629bf0dad6SMatthew G. Knepley   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
18639bf0dad6SMatthew G. Knepley   if (*points) {
18649bf0dad6SMatthew G. Knepley     closure = *points;
18659bf0dad6SMatthew G. Knepley   } else {
18669bf0dad6SMatthew G. Knepley     ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
18679bf0dad6SMatthew G. Knepley   }
18689bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
18699bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
18709bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
18719bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
18729bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
18739bf0dad6SMatthew G. Knepley 
18749bf0dad6SMatthew G. Knepley     if (ornt < 0) {
18759bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
18769bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
187786b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
18789bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
18799bf0dad6SMatthew G. Knepley     }
18809bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
18819bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
18829bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
18839bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
18849bf0dad6SMatthew G. Knepley   }
18859bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
18869bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
18879bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
18889bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
18899bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
18909bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
18919bf0dad6SMatthew G. Knepley 
18929bf0dad6SMatthew G. Knepley     if (useCone) {
18939bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
18949bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
18959bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
18969bf0dad6SMatthew G. Knepley     } else {
18979bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
18989bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
18999bf0dad6SMatthew G. Knepley       tmpO = NULL;
19009bf0dad6SMatthew G. Knepley     }
19019bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
19029bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
19039bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
19049bf0dad6SMatthew 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. */
19059bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
19069bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
19079bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
19089bf0dad6SMatthew G. Knepley       PetscInt       c;
19099bf0dad6SMatthew G. Knepley 
19109bf0dad6SMatthew G. Knepley       if (rev) {
19119bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
19129bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
19139bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
19149bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
19159bf0dad6SMatthew G. Knepley       }
19169bf0dad6SMatthew G. Knepley       /* Check for duplicate */
19179bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
19189bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
19199bf0dad6SMatthew G. Knepley       }
19209bf0dad6SMatthew G. Knepley       if (c == closureSize) {
19219bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
19229bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
19239bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
19249bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
19259bf0dad6SMatthew G. Knepley         closureSize           += 2;
19269bf0dad6SMatthew G. Knepley         fifoSize              += 2;
19279bf0dad6SMatthew G. Knepley       }
19289bf0dad6SMatthew G. Knepley     }
19299bf0dad6SMatthew G. Knepley     fifoStart += 2;
19309bf0dad6SMatthew G. Knepley   }
19319bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
19329bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
19339bf0dad6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
19349bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
19359bf0dad6SMatthew G. Knepley }
19369bf0dad6SMatthew G. Knepley 
1937552f7358SJed Brown /*@C
1938552f7358SJed 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
1939552f7358SJed Brown 
1940552f7358SJed Brown   Not collective
1941552f7358SJed Brown 
1942552f7358SJed Brown   Input Parameters:
1943552f7358SJed Brown + mesh - The DMPlex
1944552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1945552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
1946e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
1947e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
1948552f7358SJed Brown 
1949552f7358SJed Brown   Note:
19500298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
1951552f7358SJed Brown 
19523813dfbdSMatthew G Knepley   Fortran Notes:
19533813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
19543813dfbdSMatthew G Knepley   include petsc.h90 in your code.
19553813dfbdSMatthew G Knepley 
19563813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
19573813dfbdSMatthew G Knepley 
1958552f7358SJed Brown   Level: beginner
1959552f7358SJed Brown 
1960552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1961552f7358SJed Brown @*/
1962552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1963552f7358SJed Brown {
1964552f7358SJed Brown   PetscErrorCode ierr;
1965552f7358SJed Brown 
1966552f7358SJed Brown   PetscFunctionBegin;
1967552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1968e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
1969e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
1970552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, points);CHKERRQ(ierr);
19714ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
1972552f7358SJed Brown   PetscFunctionReturn(0);
1973552f7358SJed Brown }
1974552f7358SJed Brown 
1975552f7358SJed Brown /*@
1976552f7358SJed Brown   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the Sieve DAG
1977552f7358SJed Brown 
1978552f7358SJed Brown   Not collective
1979552f7358SJed Brown 
1980552f7358SJed Brown   Input Parameter:
1981552f7358SJed Brown . mesh - The DMPlex
1982552f7358SJed Brown 
1983552f7358SJed Brown   Output Parameters:
1984552f7358SJed Brown + maxConeSize - The maximum number of in-edges
1985552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
1986552f7358SJed Brown 
1987552f7358SJed Brown   Level: beginner
1988552f7358SJed Brown 
1989552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1990552f7358SJed Brown @*/
1991552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
1992552f7358SJed Brown {
1993552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
1994552f7358SJed Brown 
1995552f7358SJed Brown   PetscFunctionBegin;
1996552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1997552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
1998552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
1999552f7358SJed Brown   PetscFunctionReturn(0);
2000552f7358SJed Brown }
2001552f7358SJed Brown 
2002552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2003552f7358SJed Brown {
2004552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2005552f7358SJed Brown   PetscInt       size;
2006552f7358SJed Brown   PetscErrorCode ierr;
2007552f7358SJed Brown 
2008552f7358SJed Brown   PetscFunctionBegin;
2009552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2010552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2011552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
20121795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
20131795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2014552f7358SJed Brown   if (mesh->maxSupportSize) {
2015552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2016552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
20171795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2018552f7358SJed Brown   }
2019552f7358SJed Brown   PetscFunctionReturn(0);
2020552f7358SJed Brown }
2021552f7358SJed Brown 
2022552f7358SJed Brown PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
2023552f7358SJed Brown {
2024552f7358SJed Brown   PetscErrorCode ierr;
2025552f7358SJed Brown 
2026552f7358SJed Brown   PetscFunctionBegin;
20274d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
20284d9407bcSMatthew G. Knepley   ierr = DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2029552f7358SJed Brown   PetscFunctionReturn(0);
2030552f7358SJed Brown }
2031552f7358SJed Brown 
2032552f7358SJed Brown /*@
2033552f7358SJed Brown   DMPlexSymmetrize - Creates support (out-edge) information from cone (in-edge) inoformation
2034552f7358SJed Brown 
2035552f7358SJed Brown   Not collective
2036552f7358SJed Brown 
2037552f7358SJed Brown   Input Parameter:
2038552f7358SJed Brown . mesh - The DMPlex
2039552f7358SJed Brown 
2040552f7358SJed Brown   Output Parameter:
2041552f7358SJed Brown 
2042552f7358SJed Brown   Note:
2043552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2044552f7358SJed Brown 
2045552f7358SJed Brown   Level: beginner
2046552f7358SJed Brown 
2047552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2048552f7358SJed Brown @*/
2049552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2050552f7358SJed Brown {
2051552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2052552f7358SJed Brown   PetscInt      *offsets;
2053552f7358SJed Brown   PetscInt       supportSize;
2054552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2055552f7358SJed Brown   PetscErrorCode ierr;
2056552f7358SJed Brown 
2057552f7358SJed Brown   PetscFunctionBegin;
2058552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
205982f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
2060552f7358SJed Brown   /* Calculate support sizes */
2061552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2062552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2063552f7358SJed Brown     PetscInt dof, off, c;
2064552f7358SJed Brown 
2065552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2066552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2067552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2068552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2069552f7358SJed Brown     }
2070552f7358SJed Brown   }
2071552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2072552f7358SJed Brown     PetscInt dof;
2073552f7358SJed Brown 
2074552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
20750d644c17SKarl Rupp 
2076552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2077552f7358SJed Brown   }
2078552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2079552f7358SJed Brown   /* Calculate supports */
2080552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
20811795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
20821795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2083552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2084552f7358SJed Brown     PetscInt dof, off, c;
2085552f7358SJed Brown 
2086552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2087552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2088552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2089552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2090552f7358SJed Brown       PetscInt       offS;
2091552f7358SJed Brown 
2092552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
20930d644c17SKarl Rupp 
2094552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2095552f7358SJed Brown       ++offsets[q];
2096552f7358SJed Brown     }
2097552f7358SJed Brown   }
2098552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
2099552f7358SJed Brown   PetscFunctionReturn(0);
2100552f7358SJed Brown }
2101552f7358SJed Brown 
2102552f7358SJed Brown /*@
2103552f7358SJed Brown   DMPlexStratify - The Sieve DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
2104552f7358SJed Brown   can be illustrated by Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2105552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2106552f7358SJed Brown   the DAG.
2107552f7358SJed Brown 
2108bf4602e4SToby Isaac   Collective on dm
2109552f7358SJed Brown 
2110552f7358SJed Brown   Input Parameter:
2111552f7358SJed Brown . mesh - The DMPlex
2112552f7358SJed Brown 
2113552f7358SJed Brown   Output Parameter:
2114552f7358SJed Brown 
2115552f7358SJed Brown   Notes:
2116150b719bSJed Brown   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
2117150b719bSJed Brown   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2118c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2119150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2120552f7358SJed Brown 
2121150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2122552f7358SJed Brown 
2123552f7358SJed Brown   Level: beginner
2124552f7358SJed Brown 
2125552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
2126552f7358SJed Brown @*/
2127552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2128552f7358SJed Brown {
2129df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2130aa50250dSMatthew G. Knepley   DMLabel        label;
2131552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2132552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
2133552f7358SJed Brown   PetscErrorCode ierr;
2134552f7358SJed Brown 
2135552f7358SJed Brown   PetscFunctionBegin;
2136552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2137552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2138552f7358SJed Brown   /* Calculate depth */
2139aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2140c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2141aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2142552f7358SJed Brown   /* Initialize roots and count leaves */
2143552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2144552f7358SJed Brown     PetscInt coneSize, supportSize;
2145552f7358SJed Brown 
2146552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2147552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2148552f7358SJed Brown     if (!coneSize && supportSize) {
2149552f7358SJed Brown       ++numRoots;
2150aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2151552f7358SJed Brown     } else if (!supportSize && coneSize) {
2152552f7358SJed Brown       ++numLeaves;
2153552f7358SJed Brown     } else if (!supportSize && !coneSize) {
2154552f7358SJed Brown       /* Isolated points */
2155aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2156552f7358SJed Brown     }
2157552f7358SJed Brown   }
2158552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2159552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
2160552f7358SJed Brown       PetscInt coneSize, supportSize;
2161552f7358SJed Brown 
2162552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2163552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2164552f7358SJed Brown       if (!supportSize && coneSize) {
2165aa50250dSMatthew G. Knepley         ierr = DMLabelSetValue(label, p, 1);CHKERRQ(ierr);
2166552f7358SJed Brown       }
2167552f7358SJed Brown     }
2168552f7358SJed Brown   } else {
216974ef644bSMatthew G. Knepley     IS       pointIS;
217074ef644bSMatthew G. Knepley     PetscInt numPoints = 0, level = 0;
2171552f7358SJed Brown 
217274ef644bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
217374ef644bSMatthew G. Knepley     if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
217474ef644bSMatthew G. Knepley     while (numPoints) {
217574ef644bSMatthew G. Knepley       const PetscInt *points;
217674ef644bSMatthew G. Knepley       const PetscInt  newLevel = level+1;
217774ef644bSMatthew G. Knepley 
217874ef644bSMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
217974ef644bSMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
218074ef644bSMatthew G. Knepley         const PetscInt  point = points[p];
218174ef644bSMatthew G. Knepley         const PetscInt *support;
218274ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
218374ef644bSMatthew G. Knepley 
218474ef644bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
218574ef644bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
218674ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
218774ef644bSMatthew G. Knepley           ierr = DMLabelSetValue(label, support[s], newLevel);CHKERRQ(ierr);
2188552f7358SJed Brown         }
2189552f7358SJed Brown       }
21905edfc765SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
219174ef644bSMatthew G. Knepley       ++level;
219274ef644bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
219374ef644bSMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
219474ef644bSMatthew G. Knepley       if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
219574ef644bSMatthew G. Knepley       else         {numPoints = 0;}
219674ef644bSMatthew G. Knepley     }
219774ef644bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
219874ef644bSMatthew G. Knepley   }
2199bf4602e4SToby Isaac   { /* just in case there is an empty process */
2200bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2201bf4602e4SToby Isaac 
2202bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label,&numValues);CHKERRQ(ierr);
22034de306b1SToby Isaac     for (v = 0; v < numValues; v++) {
22044de306b1SToby Isaac       IS pointIS;
22054de306b1SToby Isaac 
22064de306b1SToby Isaac       ierr = DMLabelGetStratumIS(label, v, &pointIS);CHKERRQ(ierr);
22074de306b1SToby Isaac       if (pointIS) {
22084de306b1SToby Isaac         PetscInt  min, max, numPoints;
22094de306b1SToby Isaac         PetscInt  start;
22104de306b1SToby Isaac         PetscBool contig;
22114de306b1SToby Isaac 
22124de306b1SToby Isaac         ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
22134de306b1SToby Isaac         ierr = ISGetMinMax(pointIS, &min, &max);CHKERRQ(ierr);
22144de306b1SToby Isaac         ierr = ISContiguousLocal(pointIS,min,max+1,&start,&contig);CHKERRQ(ierr);
22154de306b1SToby Isaac         if (start == 0 && contig) {
22164de306b1SToby Isaac           ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
22174de306b1SToby Isaac           ierr = ISCreateStride(PETSC_COMM_SELF,numPoints,min,1,&pointIS);CHKERRQ(ierr);
22184de306b1SToby Isaac           ierr = DMLabelSetStratumIS(label, v, pointIS);CHKERRQ(ierr);
22194de306b1SToby Isaac         }
22204de306b1SToby Isaac       }
22214de306b1SToby Isaac       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
22224de306b1SToby Isaac     }
22236d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2224bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2225bf4602e4SToby Isaac       DMLabelAddStratum(label,v);CHKERRQ(ierr);
2226bf4602e4SToby Isaac     }
2227bf4602e4SToby Isaac   }
2228bf4602e4SToby Isaac 
2229df0420ecSMatthew G. Knepley   ierr = DMLabelGetState(label, &mesh->depthState);CHKERRQ(ierr);
2230552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2231552f7358SJed Brown   PetscFunctionReturn(0);
2232552f7358SJed Brown }
2233552f7358SJed Brown 
2234552f7358SJed Brown /*@C
2235552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
2236552f7358SJed Brown 
2237552f7358SJed Brown   Not Collective
2238552f7358SJed Brown 
2239552f7358SJed Brown   Input Parameters:
2240552f7358SJed Brown + dm - The DMPlex object
2241552f7358SJed Brown . numPoints - The number of input points for the join
2242552f7358SJed Brown - points - The input points
2243552f7358SJed Brown 
2244552f7358SJed Brown   Output Parameters:
2245552f7358SJed Brown + numCoveredPoints - The number of points in the join
2246552f7358SJed Brown - coveredPoints - The points in the join
2247552f7358SJed Brown 
2248552f7358SJed Brown   Level: intermediate
2249552f7358SJed Brown 
2250552f7358SJed Brown   Note: Currently, this is restricted to a single level join
2251552f7358SJed Brown 
22523813dfbdSMatthew G Knepley   Fortran Notes:
22533813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22543813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22553813dfbdSMatthew G Knepley 
22563813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
22573813dfbdSMatthew G Knepley 
2258552f7358SJed Brown .keywords: mesh
2259552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2260552f7358SJed Brown @*/
2261552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2262552f7358SJed Brown {
2263552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2264552f7358SJed Brown   PetscInt      *join[2];
2265552f7358SJed Brown   PetscInt       joinSize, i = 0;
2266552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2267552f7358SJed Brown   PetscErrorCode ierr;
2268552f7358SJed Brown 
2269552f7358SJed Brown   PetscFunctionBegin;
2270552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2271552f7358SJed Brown   PetscValidPointer(points, 2);
2272552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2273552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2274552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
2275552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
2276552f7358SJed Brown   /* Copy in support of first point */
2277552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
2278552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
2279552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
2280552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
2281552f7358SJed Brown   }
2282552f7358SJed Brown   /* Check each successive support */
2283552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2284552f7358SJed Brown     PetscInt newJoinSize = 0;
2285552f7358SJed Brown 
2286552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
2287552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
2288552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2289552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
2290552f7358SJed Brown 
2291552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
2292552f7358SJed Brown         if (point == join[i][m]) {
2293552f7358SJed Brown           join[1-i][newJoinSize++] = point;
2294552f7358SJed Brown           break;
2295552f7358SJed Brown         }
2296552f7358SJed Brown       }
2297552f7358SJed Brown     }
2298552f7358SJed Brown     joinSize = newJoinSize;
2299552f7358SJed Brown     i        = 1-i;
2300552f7358SJed Brown   }
2301552f7358SJed Brown   *numCoveredPoints = joinSize;
2302552f7358SJed Brown   *coveredPoints    = join[i];
2303552f7358SJed Brown   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
2304552f7358SJed Brown   PetscFunctionReturn(0);
2305552f7358SJed Brown }
2306552f7358SJed Brown 
2307552f7358SJed Brown /*@C
2308552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
2309552f7358SJed Brown 
2310552f7358SJed Brown   Not Collective
2311552f7358SJed Brown 
2312552f7358SJed Brown   Input Parameters:
2313552f7358SJed Brown + dm - The DMPlex object
2314552f7358SJed Brown . numPoints - The number of input points for the join
2315552f7358SJed Brown - points - The input points
2316552f7358SJed Brown 
2317552f7358SJed Brown   Output Parameters:
2318552f7358SJed Brown + numCoveredPoints - The number of points in the join
2319552f7358SJed Brown - coveredPoints - The points in the join
2320552f7358SJed Brown 
23213813dfbdSMatthew G Knepley   Fortran Notes:
23223813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
23233813dfbdSMatthew G Knepley   include petsc.h90 in your code.
23243813dfbdSMatthew G Knepley 
23253813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
23263813dfbdSMatthew G Knepley 
2327552f7358SJed Brown   Level: intermediate
2328552f7358SJed Brown 
2329552f7358SJed Brown .keywords: mesh
2330552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
2331552f7358SJed Brown @*/
2332552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2333552f7358SJed Brown {
2334552f7358SJed Brown   PetscErrorCode ierr;
2335552f7358SJed Brown 
2336552f7358SJed Brown   PetscFunctionBegin;
2337552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2338d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2339d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2340d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
2341552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
2342d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2343552f7358SJed Brown   PetscFunctionReturn(0);
2344552f7358SJed Brown }
2345552f7358SJed Brown 
2346552f7358SJed Brown /*@C
2347552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
2348552f7358SJed Brown 
2349552f7358SJed Brown   Not Collective
2350552f7358SJed Brown 
2351552f7358SJed Brown   Input Parameters:
2352552f7358SJed Brown + dm - The DMPlex object
2353552f7358SJed Brown . numPoints - The number of input points for the join
2354552f7358SJed Brown - points - The input points
2355552f7358SJed Brown 
2356552f7358SJed Brown   Output Parameters:
2357552f7358SJed Brown + numCoveredPoints - The number of points in the join
2358552f7358SJed Brown - coveredPoints - The points in the join
2359552f7358SJed Brown 
23603813dfbdSMatthew G Knepley   Fortran Notes:
23613813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
23623813dfbdSMatthew G Knepley   include petsc.h90 in your code.
23633813dfbdSMatthew G Knepley 
23643813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
23653813dfbdSMatthew G Knepley 
2366552f7358SJed Brown   Level: intermediate
2367552f7358SJed Brown 
2368552f7358SJed Brown .keywords: mesh
2369552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
2370552f7358SJed Brown @*/
2371552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2372552f7358SJed Brown {
2373552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2374552f7358SJed Brown   PetscInt      *offsets, **closures;
2375552f7358SJed Brown   PetscInt      *join[2];
2376552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
237724c766afSToby Isaac   PetscInt       p, d, c, m, ms;
2378552f7358SJed Brown   PetscErrorCode ierr;
2379552f7358SJed Brown 
2380552f7358SJed Brown   PetscFunctionBegin;
2381552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2382552f7358SJed Brown   PetscValidPointer(points, 2);
2383552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2384552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2385552f7358SJed Brown 
2386552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
23871795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
2388552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
238924c766afSToby Isaac   ms      = mesh->maxSupportSize;
239024c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
2391552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
2392552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
2393552f7358SJed Brown 
2394552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2395552f7358SJed Brown     PetscInt closureSize;
2396552f7358SJed Brown 
2397552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
23980d644c17SKarl Rupp 
2399552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
2400552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
2401552f7358SJed Brown       PetscInt pStart, pEnd, i;
2402552f7358SJed Brown 
2403552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
2404552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
2405552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2406552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
2407552f7358SJed Brown           break;
2408552f7358SJed Brown         }
2409552f7358SJed Brown       }
2410552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
2411552f7358SJed Brown     }
241282f516ccSBarry 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);
2413552f7358SJed Brown   }
2414552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
2415552f7358SJed Brown     PetscInt dof;
2416552f7358SJed Brown 
2417552f7358SJed Brown     /* Copy in support of first point */
2418552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
2419552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
2420552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
2421552f7358SJed Brown     }
2422552f7358SJed Brown     /* Check each successive cone */
2423552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
2424552f7358SJed Brown       PetscInt newJoinSize = 0;
2425552f7358SJed Brown 
2426552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
2427552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2428552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
2429552f7358SJed Brown 
2430552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
2431552f7358SJed Brown           if (point == join[i][m]) {
2432552f7358SJed Brown             join[1-i][newJoinSize++] = point;
2433552f7358SJed Brown             break;
2434552f7358SJed Brown           }
2435552f7358SJed Brown         }
2436552f7358SJed Brown       }
2437552f7358SJed Brown       joinSize = newJoinSize;
2438552f7358SJed Brown       i        = 1-i;
2439552f7358SJed Brown     }
2440552f7358SJed Brown     if (joinSize) break;
2441552f7358SJed Brown   }
2442552f7358SJed Brown   *numCoveredPoints = joinSize;
2443552f7358SJed Brown   *coveredPoints    = join[i];
2444552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
24450298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
2446552f7358SJed Brown   }
2447552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
2448552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
2449552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
2450552f7358SJed Brown   PetscFunctionReturn(0);
2451552f7358SJed Brown }
2452552f7358SJed Brown 
2453552f7358SJed Brown /*@C
2454552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
2455552f7358SJed Brown 
2456552f7358SJed Brown   Not Collective
2457552f7358SJed Brown 
2458552f7358SJed Brown   Input Parameters:
2459552f7358SJed Brown + dm - The DMPlex object
2460552f7358SJed Brown . numPoints - The number of input points for the meet
2461552f7358SJed Brown - points - The input points
2462552f7358SJed Brown 
2463552f7358SJed Brown   Output Parameters:
2464552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2465552f7358SJed Brown - coveredPoints - The points in the meet
2466552f7358SJed Brown 
2467552f7358SJed Brown   Level: intermediate
2468552f7358SJed Brown 
2469552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
2470552f7358SJed Brown 
24713813dfbdSMatthew G Knepley   Fortran Notes:
24723813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24733813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24743813dfbdSMatthew G Knepley 
24753813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24763813dfbdSMatthew G Knepley 
2477552f7358SJed Brown .keywords: mesh
2478552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
2479552f7358SJed Brown @*/
2480552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
2481552f7358SJed Brown {
2482552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2483552f7358SJed Brown   PetscInt      *meet[2];
2484552f7358SJed Brown   PetscInt       meetSize, i = 0;
2485552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2486552f7358SJed Brown   PetscErrorCode ierr;
2487552f7358SJed Brown 
2488552f7358SJed Brown   PetscFunctionBegin;
2489552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2490552f7358SJed Brown   PetscValidPointer(points, 2);
2491552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
2492552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
2493552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
2494552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
2495552f7358SJed Brown   /* Copy in cone of first point */
2496552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
2497552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
2498552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
2499552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
2500552f7358SJed Brown   }
2501552f7358SJed Brown   /* Check each successive cone */
2502552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2503552f7358SJed Brown     PetscInt newMeetSize = 0;
2504552f7358SJed Brown 
2505552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
2506552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
2507552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2508552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
2509552f7358SJed Brown 
2510552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
2511552f7358SJed Brown         if (point == meet[i][m]) {
2512552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
2513552f7358SJed Brown           break;
2514552f7358SJed Brown         }
2515552f7358SJed Brown       }
2516552f7358SJed Brown     }
2517552f7358SJed Brown     meetSize = newMeetSize;
2518552f7358SJed Brown     i        = 1-i;
2519552f7358SJed Brown   }
2520552f7358SJed Brown   *numCoveringPoints = meetSize;
2521552f7358SJed Brown   *coveringPoints    = meet[i];
2522552f7358SJed Brown   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
2523552f7358SJed Brown   PetscFunctionReturn(0);
2524552f7358SJed Brown }
2525552f7358SJed Brown 
2526552f7358SJed Brown /*@C
2527552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
2528552f7358SJed Brown 
2529552f7358SJed Brown   Not Collective
2530552f7358SJed Brown 
2531552f7358SJed Brown   Input Parameters:
2532552f7358SJed Brown + dm - The DMPlex object
2533552f7358SJed Brown . numPoints - The number of input points for the meet
2534552f7358SJed Brown - points - The input points
2535552f7358SJed Brown 
2536552f7358SJed Brown   Output Parameters:
2537552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2538552f7358SJed Brown - coveredPoints - The points in the meet
2539552f7358SJed Brown 
2540552f7358SJed Brown   Level: intermediate
2541552f7358SJed Brown 
25423813dfbdSMatthew G Knepley   Fortran Notes:
25433813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25443813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25453813dfbdSMatthew G Knepley 
25463813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25473813dfbdSMatthew G Knepley 
2548552f7358SJed Brown .keywords: mesh
2549552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
2550552f7358SJed Brown @*/
2551552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2552552f7358SJed Brown {
2553552f7358SJed Brown   PetscErrorCode ierr;
2554552f7358SJed Brown 
2555552f7358SJed Brown   PetscFunctionBegin;
2556552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2557d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2558d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2559d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
2560552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
2561d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2562552f7358SJed Brown   PetscFunctionReturn(0);
2563552f7358SJed Brown }
2564552f7358SJed Brown 
2565552f7358SJed Brown /*@C
2566552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
2567552f7358SJed Brown 
2568552f7358SJed Brown   Not Collective
2569552f7358SJed Brown 
2570552f7358SJed Brown   Input Parameters:
2571552f7358SJed Brown + dm - The DMPlex object
2572552f7358SJed Brown . numPoints - The number of input points for the meet
2573552f7358SJed Brown - points - The input points
2574552f7358SJed Brown 
2575552f7358SJed Brown   Output Parameters:
2576552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2577552f7358SJed Brown - coveredPoints - The points in the meet
2578552f7358SJed Brown 
2579552f7358SJed Brown   Level: intermediate
2580552f7358SJed Brown 
25813813dfbdSMatthew G Knepley   Fortran Notes:
25823813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25833813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25843813dfbdSMatthew G Knepley 
25853813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25863813dfbdSMatthew G Knepley 
2587552f7358SJed Brown .keywords: mesh
2588552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
2589552f7358SJed Brown @*/
2590552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2591552f7358SJed Brown {
2592552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2593552f7358SJed Brown   PetscInt      *offsets, **closures;
2594552f7358SJed Brown   PetscInt      *meet[2];
2595552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
259624c766afSToby Isaac   PetscInt       p, h, c, m, mc;
2597552f7358SJed Brown   PetscErrorCode ierr;
2598552f7358SJed Brown 
2599552f7358SJed Brown   PetscFunctionBegin;
2600552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2601552f7358SJed Brown   PetscValidPointer(points, 2);
2602552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2603552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2604552f7358SJed Brown 
2605552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
2606785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
2607552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
260824c766afSToby Isaac   mc      = mesh->maxConeSize;
260924c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
2610552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
2611552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
2612552f7358SJed Brown 
2613552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2614552f7358SJed Brown     PetscInt closureSize;
2615552f7358SJed Brown 
2616552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
26170d644c17SKarl Rupp 
2618552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
2619552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
2620552f7358SJed Brown       PetscInt pStart, pEnd, i;
2621552f7358SJed Brown 
2622552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
2623552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
2624552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2625552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
2626552f7358SJed Brown           break;
2627552f7358SJed Brown         }
2628552f7358SJed Brown       }
2629552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
2630552f7358SJed Brown     }
263182f516ccSBarry 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);
2632552f7358SJed Brown   }
2633552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
2634552f7358SJed Brown     PetscInt dof;
2635552f7358SJed Brown 
2636552f7358SJed Brown     /* Copy in cone of first point */
2637552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
2638552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
2639552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
2640552f7358SJed Brown     }
2641552f7358SJed Brown     /* Check each successive cone */
2642552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
2643552f7358SJed Brown       PetscInt newMeetSize = 0;
2644552f7358SJed Brown 
2645552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
2646552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2647552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
2648552f7358SJed Brown 
2649552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
2650552f7358SJed Brown           if (point == meet[i][m]) {
2651552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
2652552f7358SJed Brown             break;
2653552f7358SJed Brown           }
2654552f7358SJed Brown         }
2655552f7358SJed Brown       }
2656552f7358SJed Brown       meetSize = newMeetSize;
2657552f7358SJed Brown       i        = 1-i;
2658552f7358SJed Brown     }
2659552f7358SJed Brown     if (meetSize) break;
2660552f7358SJed Brown   }
2661552f7358SJed Brown   *numCoveredPoints = meetSize;
2662552f7358SJed Brown   *coveredPoints    = meet[i];
2663552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
26640298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
2665552f7358SJed Brown   }
2666552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
2667552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
2668552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
2669552f7358SJed Brown   PetscFunctionReturn(0);
2670552f7358SJed Brown }
2671552f7358SJed Brown 
26724e3744c5SMatthew G. Knepley /*@C
26734e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
26744e3744c5SMatthew G. Knepley 
26754e3744c5SMatthew G. Knepley   Not Collective
26764e3744c5SMatthew G. Knepley 
26774e3744c5SMatthew G. Knepley   Input Parameters:
26784e3744c5SMatthew G. Knepley + dmA - A DMPlex object
26794e3744c5SMatthew G. Knepley - dmB - A DMPlex object
26804e3744c5SMatthew G. Knepley 
26814e3744c5SMatthew G. Knepley   Output Parameters:
26824e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
26834e3744c5SMatthew G. Knepley 
26844e3744c5SMatthew G. Knepley   Level: intermediate
26854e3744c5SMatthew G. Knepley 
26864e3744c5SMatthew G. Knepley   Notes:
26874e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
26884e3744c5SMatthew G. Knepley 
26894e3744c5SMatthew G. Knepley .keywords: mesh
26904e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
26914e3744c5SMatthew G. Knepley @*/
26924e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
26934e3744c5SMatthew G. Knepley {
26944e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
26954e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
26964e3744c5SMatthew G. Knepley 
26974e3744c5SMatthew G. Knepley   PetscFunctionBegin;
26984e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
26994e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
27004e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
27014e3744c5SMatthew G. Knepley 
27024e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
27034e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
27044e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
27054e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
27064e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
27074e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
27084e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
27094e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
27104e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
27114e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
27124e3744c5SMatthew G. Knepley 
27134e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
27144e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
27154e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
27164e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
27174e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
27184e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
27194e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
27204e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
27214e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
27224e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
27234e3744c5SMatthew G. Knepley     }
27244e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
27254e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
27264e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
27274e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
27284e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
27294e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
27304e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
27314e3744c5SMatthew G. Knepley     }
27324e3744c5SMatthew G. Knepley   }
27334e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
27344e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
27354e3744c5SMatthew G. Knepley }
27364e3744c5SMatthew G. Knepley 
27377cd05799SMatthew G. Knepley /*@C
27387cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
27397cd05799SMatthew G. Knepley 
27407cd05799SMatthew G. Knepley   Not Collective
27417cd05799SMatthew G. Knepley 
27427cd05799SMatthew G. Knepley   Input Parameters:
27437cd05799SMatthew G. Knepley + dm         - The DMPlex
27447cd05799SMatthew G. Knepley . cellDim    - The cell dimension
27457cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
27467cd05799SMatthew G. Knepley 
27477cd05799SMatthew G. Knepley   Output Parameters:
27487cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
27497cd05799SMatthew G. Knepley 
27507cd05799SMatthew G. Knepley   Level: developer
27517cd05799SMatthew G. Knepley 
27527cd05799SMatthew G. Knepley   Notes:
27537cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
27547cd05799SMatthew G. Knepley 
27557cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
27567cd05799SMatthew G. Knepley @*/
275718ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
2758a6dfd86eSKarl Rupp {
275982f516ccSBarry Smith   MPI_Comm       comm;
2760552f7358SJed Brown   PetscErrorCode ierr;
2761552f7358SJed Brown 
2762552f7358SJed Brown   PetscFunctionBegin;
276382f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2764552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
2765552f7358SJed Brown   switch (cellDim) {
2766552f7358SJed Brown   case 0:
2767552f7358SJed Brown     *numFaceVertices = 0;
2768552f7358SJed Brown     break;
2769552f7358SJed Brown   case 1:
2770552f7358SJed Brown     *numFaceVertices = 1;
2771552f7358SJed Brown     break;
2772552f7358SJed Brown   case 2:
2773552f7358SJed Brown     switch (numCorners) {
277419436ca2SJed Brown     case 3: /* triangle */
277519436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2776552f7358SJed Brown       break;
277719436ca2SJed Brown     case 4: /* quadrilateral */
277819436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2779552f7358SJed Brown       break;
278019436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
278119436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2782552f7358SJed Brown       break;
278319436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
278419436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2785552f7358SJed Brown       break;
2786552f7358SJed Brown     default:
2787f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2788552f7358SJed Brown     }
2789552f7358SJed Brown     break;
2790552f7358SJed Brown   case 3:
2791552f7358SJed Brown     switch (numCorners) {
279219436ca2SJed Brown     case 4: /* tetradehdron */
279319436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
2794552f7358SJed Brown       break;
279519436ca2SJed Brown     case 6: /* tet cohesive cells */
279619436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2797552f7358SJed Brown       break;
279819436ca2SJed Brown     case 8: /* hexahedron */
279919436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2800552f7358SJed Brown       break;
280119436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
280219436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2803552f7358SJed Brown       break;
280419436ca2SJed Brown     case 10: /* quadratic tetrahedron */
280519436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2806552f7358SJed Brown       break;
280719436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
280819436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2809552f7358SJed Brown       break;
281019436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
281119436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2812552f7358SJed Brown       break;
281319436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
281419436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
2815552f7358SJed Brown       break;
2816552f7358SJed Brown     default:
2817f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2818552f7358SJed Brown     }
2819552f7358SJed Brown     break;
2820552f7358SJed Brown   default:
2821f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
2822552f7358SJed Brown   }
2823552f7358SJed Brown   PetscFunctionReturn(0);
2824552f7358SJed Brown }
2825552f7358SJed Brown 
2826552f7358SJed Brown /*@
2827aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
2828552f7358SJed Brown 
2829552f7358SJed Brown   Not Collective
2830552f7358SJed Brown 
2831aa50250dSMatthew G. Knepley   Input Parameter:
2832552f7358SJed Brown . dm    - The DMPlex object
2833552f7358SJed Brown 
2834aa50250dSMatthew G. Knepley   Output Parameter:
2835aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
2836552f7358SJed Brown 
2837552f7358SJed Brown   Level: developer
2838552f7358SJed Brown 
2839aa50250dSMatthew G. Knepley .keywords: mesh, points
2840aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2841aa50250dSMatthew G. Knepley @*/
2842aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
2843aa50250dSMatthew G. Knepley {
2844aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
2845aa50250dSMatthew G. Knepley 
2846aa50250dSMatthew G. Knepley   PetscFunctionBegin;
2847aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2848aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
2849c58f1c22SToby Isaac   if (!dm->depthLabel) {ierr = DMGetLabel(dm, "depth", &dm->depthLabel);CHKERRQ(ierr);}
2850c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
2851aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
2852aa50250dSMatthew G. Knepley }
2853aa50250dSMatthew G. Knepley 
2854aa50250dSMatthew G. Knepley /*@
2855aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
2856aa50250dSMatthew G. Knepley 
2857aa50250dSMatthew G. Knepley   Not Collective
2858aa50250dSMatthew G. Knepley 
2859aa50250dSMatthew G. Knepley   Input Parameter:
2860aa50250dSMatthew G. Knepley . dm    - The DMPlex object
2861aa50250dSMatthew G. Knepley 
2862aa50250dSMatthew G. Knepley   Output Parameter:
2863aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
2864aa50250dSMatthew G. Knepley 
2865aa50250dSMatthew G. Knepley   Level: developer
2866552f7358SJed Brown 
2867552f7358SJed Brown .keywords: mesh, points
2868aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2869552f7358SJed Brown @*/
2870552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
2871552f7358SJed Brown {
2872aa50250dSMatthew G. Knepley   DMLabel        label;
2873aa50250dSMatthew G. Knepley   PetscInt       d = 0;
2874552f7358SJed Brown   PetscErrorCode ierr;
2875552f7358SJed Brown 
2876552f7358SJed Brown   PetscFunctionBegin;
2877552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2878552f7358SJed Brown   PetscValidPointer(depth, 2);
2879aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2880aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
2881552f7358SJed Brown   *depth = d-1;
2882552f7358SJed Brown   PetscFunctionReturn(0);
2883552f7358SJed Brown }
2884552f7358SJed Brown 
2885552f7358SJed Brown /*@
2886552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
2887552f7358SJed Brown 
2888552f7358SJed Brown   Not Collective
2889552f7358SJed Brown 
2890552f7358SJed Brown   Input Parameters:
2891552f7358SJed Brown + dm           - The DMPlex object
2892552f7358SJed Brown - stratumValue - The requested depth
2893552f7358SJed Brown 
2894552f7358SJed Brown   Output Parameters:
2895552f7358SJed Brown + start - The first point at this depth
2896552f7358SJed Brown - end   - One beyond the last point at this depth
2897552f7358SJed Brown 
2898552f7358SJed Brown   Level: developer
2899552f7358SJed Brown 
2900552f7358SJed Brown .keywords: mesh, points
2901552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
2902552f7358SJed Brown @*/
29030adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
29040adebc6cSBarry Smith {
2905aa50250dSMatthew G. Knepley   DMLabel        label;
290663d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
2907552f7358SJed Brown   PetscErrorCode ierr;
2908552f7358SJed Brown 
2909552f7358SJed Brown   PetscFunctionBegin;
2910552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
291163d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
291263d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
2913552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
29140d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
291563d1a920SMatthew G. Knepley   if (stratumValue < 0) {
291663d1a920SMatthew G. Knepley     if (start) *start = pStart;
291763d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
291863d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
2919552f7358SJed Brown   }
2920aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
292163d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
292263d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
2923552f7358SJed Brown   PetscFunctionReturn(0);
2924552f7358SJed Brown }
2925552f7358SJed Brown 
2926552f7358SJed Brown /*@
2927552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
2928552f7358SJed Brown 
2929552f7358SJed Brown   Not Collective
2930552f7358SJed Brown 
2931552f7358SJed Brown   Input Parameters:
2932552f7358SJed Brown + dm           - The DMPlex object
2933552f7358SJed Brown - stratumValue - The requested height
2934552f7358SJed Brown 
2935552f7358SJed Brown   Output Parameters:
2936552f7358SJed Brown + start - The first point at this height
2937552f7358SJed Brown - end   - One beyond the last point at this height
2938552f7358SJed Brown 
2939552f7358SJed Brown   Level: developer
2940552f7358SJed Brown 
2941552f7358SJed Brown .keywords: mesh, points
2942552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
2943552f7358SJed Brown @*/
29440adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
29450adebc6cSBarry Smith {
2946aa50250dSMatthew G. Knepley   DMLabel        label;
294763d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
2948552f7358SJed Brown   PetscErrorCode ierr;
2949552f7358SJed Brown 
2950552f7358SJed Brown   PetscFunctionBegin;
2951552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
295263d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
295363d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
2954552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
29550d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
295663d1a920SMatthew G. Knepley   if (stratumValue < 0) {
295763d1a920SMatthew G. Knepley     if (start) *start = pStart;
295863d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
295963d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
2960552f7358SJed Brown   }
2961aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2962aa50250dSMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");CHKERRQ(ierr);
296363d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
296463d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
2965552f7358SJed Brown   PetscFunctionReturn(0);
2966552f7358SJed Brown }
2967552f7358SJed Brown 
2968552f7358SJed Brown /* Set the number of dof on each point and separate by fields */
29697cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionInitial(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscSection *section)
2970a6dfd86eSKarl Rupp {
2971ee55978aSMatthew G. Knepley   PetscInt      *pMax;
2972916f0f19SMatthew G. Knepley   PetscInt       depth, pStart = 0, pEnd = 0;
2973ee55978aSMatthew G. Knepley   PetscInt       Nf, p, d, dep, f;
2974fa716be8SMatthew G. Knepley   PetscBool     *isFE;
2975552f7358SJed Brown   PetscErrorCode ierr;
2976552f7358SJed Brown 
2977552f7358SJed Brown   PetscFunctionBegin;
2978fa716be8SMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
2979ee55978aSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
2980fa716be8SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
2981fa716be8SMatthew G. Knepley     PetscObject  obj;
2982fa716be8SMatthew G. Knepley     PetscClassId id;
2983fa716be8SMatthew G. Knepley 
2984ee55978aSMatthew G. Knepley     isFE[f] = PETSC_FALSE;
2985ee55978aSMatthew G. Knepley     if (f >= Nf) continue;
2986fa716be8SMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
2987fa716be8SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2988fa716be8SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
2989fa716be8SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
2990552f7358SJed Brown   }
299182f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), section);CHKERRQ(ierr);
2992552f7358SJed Brown   if (numFields > 0) {
2993552f7358SJed Brown     ierr = PetscSectionSetNumFields(*section, numFields);CHKERRQ(ierr);
2994552f7358SJed Brown     if (numComp) {
2995552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
2996552f7358SJed Brown         ierr = PetscSectionSetFieldComponents(*section, f, numComp[f]);CHKERRQ(ierr);
2997d484f18aSToby Isaac         if (isFE[f]) {
2998d484f18aSToby Isaac           PetscFE           fe;
2999d484f18aSToby Isaac           PetscDualSpace    dspace;
3000d484f18aSToby Isaac           const PetscInt    ***perms;
3001d484f18aSToby Isaac           const PetscScalar ***flips;
3002d484f18aSToby Isaac           const PetscInt    *numDof;
3003d484f18aSToby Isaac 
3004d484f18aSToby Isaac           ierr = DMGetField(dm,f,(PetscObject *) &fe);CHKERRQ(ierr);
3005d484f18aSToby Isaac           ierr = PetscFEGetDualSpace(fe,&dspace);CHKERRQ(ierr);
3006d484f18aSToby Isaac           ierr = PetscDualSpaceGetSymmetries(dspace,&perms,&flips);CHKERRQ(ierr);
3007d484f18aSToby Isaac           ierr = PetscDualSpaceGetNumDof(dspace,&numDof);CHKERRQ(ierr);
3008d484f18aSToby Isaac           if (perms || flips) {
3009d484f18aSToby Isaac             DM               K;
3010d484f18aSToby Isaac             DMLabel          depthLabel;
3011d484f18aSToby Isaac             PetscInt         depth, h;
3012d484f18aSToby Isaac             PetscSectionSym  sym;
3013d484f18aSToby Isaac 
3014d484f18aSToby Isaac             ierr = PetscDualSpaceGetDM(dspace,&K);CHKERRQ(ierr);
3015d484f18aSToby Isaac             ierr = DMPlexGetDepthLabel(dm,&depthLabel);CHKERRQ(ierr);
3016d484f18aSToby Isaac             ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
3017d484f18aSToby Isaac             ierr = PetscSectionSymCreateLabel(PetscObjectComm((PetscObject)*section),depthLabel,&sym);CHKERRQ(ierr);
3018d484f18aSToby Isaac             for (h = 0; h <= depth; h++) {
3019d484f18aSToby Isaac               PetscDualSpace    hspace;
3020d484f18aSToby Isaac               PetscInt          kStart, kEnd;
3021d484f18aSToby Isaac               PetscInt          kConeSize;
3022d484f18aSToby Isaac               const PetscInt    **perms0 = NULL;
3023d484f18aSToby Isaac               const PetscScalar **flips0 = NULL;
3024d484f18aSToby Isaac 
3025d484f18aSToby Isaac               ierr = PetscDualSpaceGetHeightSubspace(dspace,h,&hspace);CHKERRQ(ierr);
3026d484f18aSToby Isaac               ierr = DMPlexGetHeightStratum(K,h,&kStart,&kEnd);CHKERRQ(ierr);
3027d484f18aSToby Isaac               if (!hspace) continue;
3028d484f18aSToby Isaac               ierr = PetscDualSpaceGetSymmetries(hspace,&perms,&flips);CHKERRQ(ierr);
3029d484f18aSToby Isaac               if (perms) perms0 = perms[0];
3030d484f18aSToby Isaac               if (flips) flips0 = flips[0];
3031d484f18aSToby Isaac               if (!(perms0 || flips0)) continue;
3032d484f18aSToby Isaac               ierr = DMPlexGetConeSize(K,kStart,&kConeSize);CHKERRQ(ierr);
3033d484f18aSToby Isaac               if (numComp[f] == 1) {
3034d484f18aSToby Isaac                 ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numDof[depth - h],-kConeSize,kConeSize,PETSC_USE_POINTER,perms0 ? &perms0[-kConeSize] : NULL,flips0 ? &flips0[-kConeSize] : NULL);CHKERRQ(ierr);
3035d484f18aSToby Isaac               } else {
3036d484f18aSToby Isaac                 PetscInt    **fieldPerms = NULL, o;
3037d484f18aSToby Isaac                 PetscScalar **fieldFlips = NULL;
3038d484f18aSToby Isaac 
3039d484f18aSToby Isaac                 ierr = PetscCalloc1(2 * kConeSize,&fieldPerms);CHKERRQ(ierr);
3040d484f18aSToby Isaac                 ierr = PetscCalloc1(2 * kConeSize,&fieldFlips);CHKERRQ(ierr);
3041d484f18aSToby Isaac                 for (o = -kConeSize; o < kConeSize; o++) {
3042d484f18aSToby Isaac                   if (perms0 && perms0[o]) {
3043d484f18aSToby Isaac                     PetscInt r, s;
3044d484f18aSToby Isaac 
3045d484f18aSToby Isaac                     ierr = PetscMalloc1(numComp[f] * numDof[depth - h],&fieldPerms[o+kConeSize]);CHKERRQ(ierr);
3046d484f18aSToby Isaac                     for (r = 0; r < numDof[depth - h]; r++) {
3047d484f18aSToby Isaac                       for (s = 0; s < numComp[f]; s++) {
3048d484f18aSToby Isaac                         fieldPerms[o+kConeSize][r * numComp[f] + s] = numComp[f] * perms0[o][r] + s;
3049d484f18aSToby Isaac                       }
3050d484f18aSToby Isaac                     }
3051d484f18aSToby Isaac                   }
3052d484f18aSToby Isaac                   if (flips0 && flips0[o]) {
3053d484f18aSToby Isaac                     PetscInt r, s;
3054d484f18aSToby Isaac 
3055d484f18aSToby Isaac                     ierr = PetscMalloc1(numComp[f] * numDof[depth - h],&fieldFlips[o+kConeSize]);CHKERRQ(ierr);
3056d484f18aSToby Isaac                     for (r = 0; r < numDof[depth - h]; r++) {
3057d484f18aSToby Isaac                       for (s = 0; s < numComp[f]; s++) {
3058d484f18aSToby Isaac                         fieldFlips[o+kConeSize][r * numComp[f] + s] = flips0[o][r];
3059d484f18aSToby Isaac                       }
3060d484f18aSToby Isaac                     }
3061d484f18aSToby Isaac                   }
3062d484f18aSToby Isaac                 }
3063d484f18aSToby Isaac                 ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numComp[f] * numDof[depth - h],-kConeSize,kConeSize,PETSC_OWN_POINTER,(const PetscInt **) fieldPerms,(const PetscScalar **)fieldFlips);CHKERRQ(ierr);
3064d484f18aSToby Isaac               }
3065d484f18aSToby Isaac             }
3066d484f18aSToby Isaac             ierr = PetscSectionSetFieldSym(*section,f,sym);CHKERRQ(ierr);
3067d484f18aSToby Isaac             ierr = PetscSectionSymDestroy(&sym);CHKERRQ(ierr);
3068d484f18aSToby Isaac           }
3069d484f18aSToby Isaac         }
3070552f7358SJed Brown       }
3071552f7358SJed Brown     }
3072552f7358SJed Brown   }
3073552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3074552f7358SJed Brown   ierr = PetscSectionSetChart(*section, pStart, pEnd);CHKERRQ(ierr);
3075916f0f19SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
30769ac3fadcSMatthew G. Knepley   ierr = PetscMalloc1(depth+1,&pMax);CHKERRQ(ierr);
30779ac3fadcSMatthew 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);
3078916f0f19SMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
3079916f0f19SMatthew G. Knepley     d    = dim == depth ? dep : (!dep ? 0 : dim);
3080916f0f19SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dep, &pStart, &pEnd);CHKERRQ(ierr);
3081fa716be8SMatthew G. Knepley     pMax[dep] = pMax[dep] < 0 ? pEnd : pMax[dep];
3082552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
3083ee55978aSMatthew G. Knepley       PetscInt tot = 0;
3084ee55978aSMatthew G. Knepley 
3085552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3086fa716be8SMatthew G. Knepley         if (isFE[f] && p >= pMax[dep]) continue;
3087552f7358SJed Brown         ierr = PetscSectionSetFieldDof(*section, p, f, numDof[f*(dim+1)+d]);CHKERRQ(ierr);
3088ee55978aSMatthew G. Knepley         tot += numDof[f*(dim+1)+d];
3089552f7358SJed Brown       }
3090ee55978aSMatthew G. Knepley       ierr = PetscSectionSetDof(*section, p, tot);CHKERRQ(ierr);
3091552f7358SJed Brown     }
3092552f7358SJed Brown   }
30939ac3fadcSMatthew G. Knepley   ierr = PetscFree(pMax);CHKERRQ(ierr);
3094fa716be8SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
3095552f7358SJed Brown   PetscFunctionReturn(0);
3096552f7358SJed Brown }
3097552f7358SJed Brown 
3098552f7358SJed Brown /* Set the number of dof on each point and separate by fields
3099ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3100552f7358SJed Brown */
31017cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCDof(DM dm, PetscInt numBC, const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
3102a6dfd86eSKarl Rupp {
3103552f7358SJed Brown   PetscInt       numFields;
3104552f7358SJed Brown   PetscInt       bc;
3105a68b90caSToby Isaac   PetscSection   aSec;
3106552f7358SJed Brown   PetscErrorCode ierr;
3107552f7358SJed Brown 
3108552f7358SJed Brown   PetscFunctionBegin;
3109552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3110552f7358SJed Brown   for (bc = 0; bc < numBC; ++bc) {
3111552f7358SJed Brown     PetscInt        field = 0;
3112ab1d0545SMatthew G. Knepley     const PetscInt *comp;
3113552f7358SJed Brown     const PetscInt *idx;
3114ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3115552f7358SJed Brown 
31160d644c17SKarl Rupp     if (numFields) field = bcField[bc];
3117ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3118ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3119552f7358SJed Brown     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3120552f7358SJed Brown     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3121552f7358SJed Brown     for (i = 0; i < n; ++i) {
3122552f7358SJed Brown       const PetscInt p = idx[i];
312306ff1081SMatthew G. Knepley       PetscInt       numConst;
3124552f7358SJed Brown 
3125552f7358SJed Brown       if (numFields) {
3126552f7358SJed Brown         ierr = PetscSectionGetFieldDof(section, p, field, &numConst);CHKERRQ(ierr);
3127552f7358SJed Brown       } else {
3128552f7358SJed Brown         ierr = PetscSectionGetDof(section, p, &numConst);CHKERRQ(ierr);
3129552f7358SJed Brown       }
313006ff1081SMatthew G. Knepley       /* If Nc < 0, constrain every dof on the point */
313106ff1081SMatthew G. Knepley       if (Nc > 0) numConst = PetscMin(numConst, Nc);
313206ff1081SMatthew G. Knepley       if (numFields) {ierr = PetscSectionAddFieldConstraintDof(section, p, field, numConst);CHKERRQ(ierr);}
3133552f7358SJed Brown       ierr = PetscSectionAddConstraintDof(section, p, numConst);CHKERRQ(ierr);
3134552f7358SJed Brown     }
3135552f7358SJed Brown     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
313606ff1081SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3137552f7358SJed Brown   }
3138a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3139a68b90caSToby Isaac   if (aSec) {
3140a68b90caSToby Isaac     PetscInt aStart, aEnd, a;
3141a68b90caSToby Isaac 
3142a68b90caSToby Isaac     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3143a68b90caSToby Isaac     for (a = aStart; a < aEnd; a++) {
3144ab1d0545SMatthew G. Knepley       PetscInt dof, f;
3145a68b90caSToby Isaac 
3146a68b90caSToby Isaac       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3147a68b90caSToby Isaac       if (dof) {
3148a68b90caSToby Isaac         /* if there are point-to-point constraints, then all dofs are constrained */
3149a68b90caSToby Isaac         ierr = PetscSectionGetDof(section, a, &dof);CHKERRQ(ierr);
3150a68b90caSToby Isaac         ierr = PetscSectionSetConstraintDof(section, a, dof);CHKERRQ(ierr);
3151a68b90caSToby Isaac         for (f = 0; f < numFields; f++) {
3152a68b90caSToby Isaac           ierr = PetscSectionGetFieldDof(section, a, f, &dof);CHKERRQ(ierr);
3153a68b90caSToby Isaac           ierr = PetscSectionSetFieldConstraintDof(section, a, f, dof);CHKERRQ(ierr);
3154a68b90caSToby Isaac         }
3155a68b90caSToby Isaac       }
3156a68b90caSToby Isaac     }
3157a68b90caSToby Isaac   }
3158552f7358SJed Brown   PetscFunctionReturn(0);
3159552f7358SJed Brown }
3160552f7358SJed Brown 
3161ab1d0545SMatthew G. Knepley /* Set the constrained field indices on each point
3162ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3163ab1d0545SMatthew G. Knepley */
31647cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCIndicesField(DM dm, PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
31650adebc6cSBarry Smith {
3166ab1d0545SMatthew G. Knepley   PetscSection   aSec;
3167ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3168*503335f2SSander Arens   PetscInt       numFields, cdof, maxDof = 0, pStart, pEnd, p, bc, f, d;
3169552f7358SJed Brown   PetscErrorCode ierr;
3170552f7358SJed Brown 
3171552f7358SJed Brown   PetscFunctionBegin;
3172552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3173ab1d0545SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
3174ab1d0545SMatthew G. Knepley   /* Initialize all field indices to -1 */
3175552f7358SJed Brown   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3176*503335f2SSander Arens   for (p = pStart; p < pEnd; ++p) {ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr); maxDof = PetscMax(maxDof, cdof);}
3177ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3178ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3179ab1d0545SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) for (f = 0; f < numFields; ++f) {ierr = PetscSectionSetFieldConstraintIndices(section, p, f, indices);CHKERRQ(ierr);}
3180ab1d0545SMatthew G. Knepley   /* Handle BC constraints */
3181ab1d0545SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {
3182ab1d0545SMatthew G. Knepley     const PetscInt  field = bcField[bc];
3183ab1d0545SMatthew G. Knepley     const PetscInt *comp, *idx;
3184ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3185552f7358SJed Brown 
3186ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3187ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3188ab1d0545SMatthew G. Knepley     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3189ab1d0545SMatthew G. Knepley     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3190ab1d0545SMatthew G. Knepley     for (i = 0; i < n; ++i) {
3191ab1d0545SMatthew G. Knepley       const PetscInt  p = idx[i];
3192ab1d0545SMatthew G. Knepley       const PetscInt *find;
3193*503335f2SSander Arens       PetscInt        fdof, fcdof, c;
3194ab1d0545SMatthew G. Knepley 
3195*503335f2SSander Arens       ierr = PetscSectionGetFieldDof(section, p, field, &fdof);CHKERRQ(ierr);
3196*503335f2SSander Arens       if (!fdof) continue;
3197ab1d0545SMatthew G. Knepley       if (Nc < 0) {
3198*503335f2SSander Arens         for (d = 0; d < fdof; ++d) indices[d] = d;
3199*503335f2SSander Arens         fcdof = fdof;
3200552f7358SJed Brown       } else {
3201*503335f2SSander Arens         ierr = PetscSectionGetFieldConstraintDof(section, p, field, &fcdof);CHKERRQ(ierr);
3202ab1d0545SMatthew G. Knepley         ierr = PetscSectionGetFieldConstraintIndices(section, p, field, &find);CHKERRQ(ierr);
3203ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) {if (find[d] < 0) break; indices[d] = find[d];}
3204*503335f2SSander Arens         for (c = 0; c < Nc; ++c) indices[d++] = comp[c];
3205*503335f2SSander Arens         ierr = PetscSortRemoveDupsInt(&d, indices);CHKERRQ(ierr);
3206*503335f2SSander Arens         for (c = d; c < fcdof; ++c) indices[c] = -1;
3207*503335f2SSander Arens         fcdof = d;
3208552f7358SJed Brown       }
3209*503335f2SSander Arens       ierr = PetscSectionSetFieldConstraintDof(section, p, field, fcdof);CHKERRQ(ierr);
3210ab1d0545SMatthew G. Knepley       ierr = PetscSectionSetFieldConstraintIndices(section, p, field, indices);CHKERRQ(ierr);
3211552f7358SJed Brown     }
3212ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3213ab1d0545SMatthew G. Knepley     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3214552f7358SJed Brown   }
3215ab1d0545SMatthew G. Knepley   /* Handle anchors */
3216ab1d0545SMatthew G. Knepley   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3217ab1d0545SMatthew G. Knepley   if (aSec) {
3218ab1d0545SMatthew G. Knepley     PetscInt aStart, aEnd, a;
3219552f7358SJed Brown 
3220ab1d0545SMatthew G. Knepley     for (d = 0; d < maxDof; ++d) indices[d] = d;
3221ab1d0545SMatthew G. Knepley     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3222ab1d0545SMatthew G. Knepley     for (a = aStart; a < aEnd; a++) {
3223*503335f2SSander Arens       PetscInt dof, f;
3224ab1d0545SMatthew G. Knepley 
3225ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3226ab1d0545SMatthew G. Knepley       if (dof) {
3227ab1d0545SMatthew G. Knepley         /* if there are point-to-point constraints, then all dofs are constrained */
3228ab1d0545SMatthew G. Knepley         for (f = 0; f < numFields; f++) {
3229ab1d0545SMatthew G. Knepley           ierr = PetscSectionSetFieldConstraintIndices(section, a, f, indices);CHKERRQ(ierr);
3230ab1d0545SMatthew G. Knepley         }
3231ab1d0545SMatthew G. Knepley       }
3232ab1d0545SMatthew G. Knepley     }
3233ab1d0545SMatthew G. Knepley   }
3234ab1d0545SMatthew G. Knepley   ierr = PetscFree(indices);CHKERRQ(ierr);
3235ab1d0545SMatthew G. Knepley   PetscFunctionReturn(0);
3236ab1d0545SMatthew G. Knepley }
3237ab1d0545SMatthew G. Knepley 
3238ab1d0545SMatthew G. Knepley /* Set the constrained indices on each point */
32397cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCIndices(DM dm, PetscSection section)
3240ab1d0545SMatthew G. Knepley {
3241ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3242ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, f, d;
3243ab1d0545SMatthew G. Knepley   PetscErrorCode ierr;
3244ab1d0545SMatthew G. Knepley 
3245ab1d0545SMatthew G. Knepley   PetscFunctionBegin;
3246ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3247ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3248ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3249ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3250ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3251552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3252552f7358SJed Brown     PetscInt cdof, d;
3253552f7358SJed Brown 
3254552f7358SJed Brown     ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
3255552f7358SJed Brown     if (cdof) {
3256552f7358SJed Brown       if (numFields) {
3257552f7358SJed Brown         PetscInt numConst = 0, foff = 0;
3258552f7358SJed Brown 
3259552f7358SJed Brown         for (f = 0; f < numFields; ++f) {
3260ab1d0545SMatthew G. Knepley           const PetscInt *find;
3261ab1d0545SMatthew G. Knepley           PetscInt        fcdof, fdof;
3262552f7358SJed Brown 
3263552f7358SJed Brown           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
3264ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
3265ab1d0545SMatthew G. Knepley           /* Change constraint numbering from field component to local dof number */
3266ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintIndices(section, p, f, &find);CHKERRQ(ierr);
3267ab1d0545SMatthew G. Knepley           for (d = 0; d < fcdof; ++d) indices[numConst+d] = find[d] + foff;
3268ab1d0545SMatthew G. Knepley           numConst += fcdof;
3269552f7358SJed Brown           foff     += fdof;
3270552f7358SJed Brown         }
3271*503335f2SSander Arens         if (cdof != numConst) {ierr = PetscSectionSetConstraintDof(section, p, numConst);CHKERRQ(ierr);}
3272552f7358SJed Brown       } else {
32730d644c17SKarl Rupp         for (d = 0; d < cdof; ++d) indices[d] = d;
3274552f7358SJed Brown       }
3275552f7358SJed Brown       ierr = PetscSectionSetConstraintIndices(section, p, indices);CHKERRQ(ierr);
3276552f7358SJed Brown     }
3277552f7358SJed Brown   }
3278552f7358SJed Brown   ierr = PetscFree(indices);CHKERRQ(ierr);
3279552f7358SJed Brown   PetscFunctionReturn(0);
3280552f7358SJed Brown }
3281552f7358SJed Brown 
3282552f7358SJed Brown /*@C
3283552f7358SJed Brown   DMPlexCreateSection - Create a PetscSection based upon the dof layout specification provided.
3284552f7358SJed Brown 
3285552f7358SJed Brown   Not Collective
3286552f7358SJed Brown 
3287552f7358SJed Brown   Input Parameters:
3288552f7358SJed Brown + dm        - The DMPlex object
3289552f7358SJed Brown . dim       - The spatial dimension of the problem
3290552f7358SJed Brown . numFields - The number of fields in the problem
3291552f7358SJed Brown . numComp   - An array of size numFields that holds the number of components for each field
3292552f7358SJed 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
3293552f7358SJed Brown . numBC     - The number of boundary conditions
3294552f7358SJed Brown . bcField   - An array of size numBC giving the field number for each boundry condition
3295ab1d0545SMatthew G. Knepley . bcComps   - [Optional] An array of size numBC giving an IS holding the field components to which each boundary condition applies
3296ab1d0545SMatthew G. Knepley . bcPoints  - An array of size numBC giving an IS holding the Plex points to which each boundary condition applies
3297f8f126e8SMatthew G. Knepley - perm      - Optional permutation of the chart, or NULL
3298552f7358SJed Brown 
3299552f7358SJed Brown   Output Parameter:
3300552f7358SJed Brown . section - The PetscSection object
3301552f7358SJed Brown 
3302552f7358SJed 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
3303f8f126e8SMatthew G. Knepley   number of dof for field 0 on each edge.
3304f8f126e8SMatthew G. Knepley 
3305f8f126e8SMatthew G. Knepley   The chart permutation is the same one set using PetscSectionSetPermutation()
3306552f7358SJed Brown 
3307552f7358SJed Brown   Level: developer
3308552f7358SJed Brown 
33093813dfbdSMatthew G Knepley   Fortran Notes:
33103813dfbdSMatthew G Knepley   A Fortran 90 version is available as DMPlexCreateSectionF90()
33113813dfbdSMatthew G Knepley 
3312552f7358SJed Brown .keywords: mesh, elements
3313f8f126e8SMatthew G. Knepley .seealso: DMPlexCreate(), PetscSectionCreate(), PetscSectionSetPermutation()
3314552f7358SJed Brown @*/
3315ab1d0545SMatthew 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)
3316a6dfd86eSKarl Rupp {
3317a68b90caSToby Isaac   PetscSection   aSec;
3318552f7358SJed Brown   PetscErrorCode ierr;
3319552f7358SJed Brown 
3320552f7358SJed Brown   PetscFunctionBegin;
3321552f7358SJed Brown   ierr = DMPlexCreateSectionInitial(dm, dim, numFields, numComp, numDof, section);CHKERRQ(ierr);
3322ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSectionBCDof(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3323f8f126e8SMatthew G. Knepley   if (perm) {ierr = PetscSectionSetPermutation(*section, perm);CHKERRQ(ierr);}
3324552f7358SJed Brown   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
3325a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,NULL);CHKERRQ(ierr);
3326ab1d0545SMatthew G. Knepley   if (numBC || aSec) {
3327ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndicesField(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3328ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndices(dm, *section);CHKERRQ(ierr);
3329ab1d0545SMatthew G. Knepley   }
3330ce1779c8SBarry Smith   ierr = PetscSectionViewFromOptions(*section,NULL,"-section_view");CHKERRQ(ierr);
3331552f7358SJed Brown   PetscFunctionReturn(0);
3332552f7358SJed Brown }
3333552f7358SJed Brown 
33340adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
33350adebc6cSBarry Smith {
3336efe440bfSMatthew G. Knepley   PetscSection   section, s;
3337efe440bfSMatthew G. Knepley   Mat            m;
33383e922f36SToby Isaac   PetscInt       maxHeight;
3339552f7358SJed Brown   PetscErrorCode ierr;
3340552f7358SJed Brown 
3341552f7358SJed Brown   PetscFunctionBegin;
334238221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
33433e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
33443e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
334582f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
3346552f7358SJed Brown   ierr = DMSetDefaultSection(*cdm, section);CHKERRQ(ierr);
33471d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3348efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3349efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3350efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3351efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3352efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
3353552f7358SJed Brown   PetscFunctionReturn(0);
3354552f7358SJed Brown }
3355552f7358SJed Brown 
33567cd05799SMatthew G. Knepley /*@C
33577cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
33587cd05799SMatthew G. Knepley 
33597cd05799SMatthew G. Knepley   Not Collective
33607cd05799SMatthew G. Knepley 
33617cd05799SMatthew G. Knepley   Input Parameters:
33627cd05799SMatthew G. Knepley . dm        - The DMPlex object
33637cd05799SMatthew G. Knepley 
33647cd05799SMatthew G. Knepley   Output Parameter:
33657cd05799SMatthew G. Knepley . section - The PetscSection object
33667cd05799SMatthew G. Knepley 
33677cd05799SMatthew G. Knepley   Level: developer
33687cd05799SMatthew G. Knepley 
33697cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
33707cd05799SMatthew G. Knepley @*/
33710adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
33720adebc6cSBarry Smith {
3373552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3374552f7358SJed Brown 
3375552f7358SJed Brown   PetscFunctionBegin;
3376552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3377552f7358SJed Brown   if (section) *section = mesh->coneSection;
3378552f7358SJed Brown   PetscFunctionReturn(0);
3379552f7358SJed Brown }
3380552f7358SJed Brown 
33817cd05799SMatthew G. Knepley /*@C
33827cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
33837cd05799SMatthew G. Knepley 
33847cd05799SMatthew G. Knepley   Not Collective
33857cd05799SMatthew G. Knepley 
33867cd05799SMatthew G. Knepley   Input Parameters:
33877cd05799SMatthew G. Knepley . dm        - The DMPlex object
33887cd05799SMatthew G. Knepley 
33897cd05799SMatthew G. Knepley   Output Parameter:
33907cd05799SMatthew G. Knepley . section - The PetscSection object
33917cd05799SMatthew G. Knepley 
33927cd05799SMatthew G. Knepley   Level: developer
33937cd05799SMatthew G. Knepley 
33947cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
33957cd05799SMatthew G. Knepley @*/
33968cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
33978cb4d582SMatthew G. Knepley {
33988cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
33998cb4d582SMatthew G. Knepley 
34008cb4d582SMatthew G. Knepley   PetscFunctionBegin;
34018cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34028cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
34038cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
34048cb4d582SMatthew G. Knepley }
34058cb4d582SMatthew G. Knepley 
34067cd05799SMatthew G. Knepley /*@C
34077cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
34087cd05799SMatthew G. Knepley 
34097cd05799SMatthew G. Knepley   Not Collective
34107cd05799SMatthew G. Knepley 
34117cd05799SMatthew G. Knepley   Input Parameters:
34127cd05799SMatthew G. Knepley . dm        - The DMPlex object
34137cd05799SMatthew G. Knepley 
34147cd05799SMatthew G. Knepley   Output Parameter:
34157cd05799SMatthew G. Knepley . cones - The cone for each point
34167cd05799SMatthew G. Knepley 
34177cd05799SMatthew G. Knepley   Level: developer
34187cd05799SMatthew G. Knepley 
34197cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
34207cd05799SMatthew G. Knepley @*/
3421a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3422a6dfd86eSKarl Rupp {
3423552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3424552f7358SJed Brown 
3425552f7358SJed Brown   PetscFunctionBegin;
3426552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3427552f7358SJed Brown   if (cones) *cones = mesh->cones;
3428552f7358SJed Brown   PetscFunctionReturn(0);
3429552f7358SJed Brown }
3430552f7358SJed Brown 
34317cd05799SMatthew G. Knepley /*@C
34327cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
34337cd05799SMatthew G. Knepley 
34347cd05799SMatthew G. Knepley   Not Collective
34357cd05799SMatthew G. Knepley 
34367cd05799SMatthew G. Knepley   Input Parameters:
34377cd05799SMatthew G. Knepley . dm        - The DMPlex object
34387cd05799SMatthew G. Knepley 
34397cd05799SMatthew G. Knepley   Output Parameter:
34407cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
34417cd05799SMatthew G. Knepley 
34427cd05799SMatthew G. Knepley   Level: developer
34437cd05799SMatthew G. Knepley 
34447cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
34457cd05799SMatthew G. Knepley @*/
3446a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3447a6dfd86eSKarl Rupp {
3448552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3449552f7358SJed Brown 
3450552f7358SJed Brown   PetscFunctionBegin;
3451552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3452552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3453552f7358SJed Brown   PetscFunctionReturn(0);
3454552f7358SJed Brown }
3455552f7358SJed Brown 
3456552f7358SJed Brown /******************************** FEM Support **********************************/
3457552f7358SJed Brown 
34583194fc30SMatthew G. Knepley PetscErrorCode DMPlexCreateSpectralClosurePermutation(DM dm, PetscSection section)
34593194fc30SMatthew G. Knepley {
34603194fc30SMatthew G. Knepley   PetscInt      *perm;
346189eabcffSMatthew G. Knepley   PetscInt       dim, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
34623194fc30SMatthew G. Knepley   PetscErrorCode ierr;
34633194fc30SMatthew G. Knepley 
34643194fc30SMatthew G. Knepley   PetscFunctionBegin;
34653194fc30SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
34663194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
34673194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
346889eabcffSMatthew G. Knepley   if (dim <= 1) PetscFunctionReturn(0);
34693194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
34703194fc30SMatthew G. Knepley     /* An order k SEM disc has k-1 dofs on an edge */
34713194fc30SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
34723194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
34733194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
34743194fc30SMatthew G. Knepley     k = k/Nc + 1;
347589eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
34763194fc30SMatthew G. Knepley   }
34773194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
34783194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
347989eabcffSMatthew G. Knepley     switch (dim) {
348089eabcffSMatthew G. Knepley     case 2:
34813194fc30SMatthew 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} */
34823194fc30SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
34833194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
34843194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
34853194fc30SMatthew G. Knepley       k = k/Nc + 1;
34863194fc30SMatthew G. Knepley       /* The SEM order is
34873194fc30SMatthew G. Knepley 
34883194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
348989eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
34903194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
34913194fc30SMatthew G. Knepley       */
34923194fc30SMatthew G. Knepley       {
34933194fc30SMatthew G. Knepley         const PetscInt of   = 0;
34943194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
34953194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
34963194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
34973194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
34983194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
34993194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
35003194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
35013194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
35023194fc30SMatthew G. Knepley         PetscInt       o;
35033194fc30SMatthew G. Knepley 
35043194fc30SMatthew G. Knepley         /* bottom */
35053194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
35063194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
35073194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
35083194fc30SMatthew G. Knepley         /* middle */
35093194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
35103194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
35113194fc30SMatthew 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;
35123194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
35133194fc30SMatthew G. Knepley         }
35143194fc30SMatthew G. Knepley         /* top */
35153194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
35163194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
35173194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
35183194fc30SMatthew G. Knepley         foffset = offset;
35193194fc30SMatthew G. Knepley       }
352089eabcffSMatthew G. Knepley       break;
352189eabcffSMatthew G. Knepley     case 3:
352289eabcffSMatthew G. Knepley       /* The original hex closure is
352389eabcffSMatthew G. Knepley 
352489eabcffSMatthew G. Knepley          {c,
352589eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
352689eabcffSMatthew 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,
352789eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
352889eabcffSMatthew G. Knepley       */
352989eabcffSMatthew G. Knepley       ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
353089eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
353189eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
353289eabcffSMatthew G. Knepley       k = k/Nc + 1;
353389eabcffSMatthew G. Knepley       /* The SEM order is
353489eabcffSMatthew G. Knepley          Bottom Slice
353589eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
353689eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
353789eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
353889eabcffSMatthew G. Knepley 
353989eabcffSMatthew G. Knepley          Middle Slice (j)
354089eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
354189eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
354289eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
354389eabcffSMatthew G. Knepley 
354489eabcffSMatthew G. Knepley          Top Slice
354589eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
354689eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
354789eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
354889eabcffSMatthew G. Knepley       */
354989eabcffSMatthew G. Knepley       {
355089eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
355189eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
355289eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
355389eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
355489eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
355589eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
355689eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
355789eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
355889eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
355989eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
356089eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
356189eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
356289eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
356389eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
356489eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
356589eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
356689eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
356789eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
356889eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
356989eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
357089eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
357189eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
357289eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
357389eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
357489eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
357589eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
357689eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
357789eabcffSMatthew G. Knepley         PetscInt       o, n;
357889eabcffSMatthew G. Knepley 
357989eabcffSMatthew G. Knepley         /* Bottom Slice */
358089eabcffSMatthew G. Knepley         /*   bottom */
358189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
358289eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
358389eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
358489eabcffSMatthew G. Knepley         /*   middle */
358589eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
358689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
3587316b7f87SMax Rietmann           for (n = 0; n < k-1; ++n) {o = ofb+n*(k-1)+i; for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;}
358889eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
35893194fc30SMatthew G. Knepley         }
359089eabcffSMatthew G. Knepley         /*   top */
359189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
359289eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
359389eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
359489eabcffSMatthew G. Knepley 
359589eabcffSMatthew G. Knepley         /* Middle Slice */
359689eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
359789eabcffSMatthew G. Knepley           /*   bottom */
359889eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
359989eabcffSMatthew 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;
360089eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
360189eabcffSMatthew G. Knepley           /*   middle */
360289eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
360389eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
360489eabcffSMatthew 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;
360589eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
360689eabcffSMatthew G. Knepley           }
360789eabcffSMatthew G. Knepley           /*   top */
360889eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
360989eabcffSMatthew 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;
361089eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
361189eabcffSMatthew G. Knepley         }
361289eabcffSMatthew G. Knepley 
361389eabcffSMatthew G. Knepley         /* Top Slice */
361489eabcffSMatthew G. Knepley         /*   bottom */
361589eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
361689eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
361789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
361889eabcffSMatthew G. Knepley         /*   middle */
361989eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
362089eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
362189eabcffSMatthew 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;
362289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
362389eabcffSMatthew G. Knepley         }
362489eabcffSMatthew G. Knepley         /*   top */
362589eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
362689eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
362789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
362889eabcffSMatthew G. Knepley 
362989eabcffSMatthew G. Knepley         foffset = offset;
363089eabcffSMatthew G. Knepley       }
363189eabcffSMatthew G. Knepley       break;
363289eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
363389eabcffSMatthew G. Knepley     }
363489eabcffSMatthew G. Knepley   }
363589eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
36363194fc30SMatthew G. Knepley   /* Check permutation */
36373194fc30SMatthew G. Knepley   {
36383194fc30SMatthew G. Knepley     PetscInt *check;
36393194fc30SMatthew G. Knepley 
36403194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
36413194fc30SMatthew 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]);}
36423194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
36433194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
36443194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
36453194fc30SMatthew G. Knepley   }
36461fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
36473194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
36483194fc30SMatthew G. Knepley }
36493194fc30SMatthew G. Knepley 
3650e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
3651e071409bSToby Isaac {
3652e071409bSToby Isaac   PetscDS        prob;
3653e071409bSToby Isaac   PetscInt       depth, Nf, h;
3654e071409bSToby Isaac   DMLabel        label;
3655e071409bSToby Isaac   PetscErrorCode ierr;
3656e071409bSToby Isaac 
3657e071409bSToby Isaac   PetscFunctionBeginHot;
3658e071409bSToby Isaac   prob    = dm->prob;
3659e071409bSToby Isaac   Nf      = prob->Nf;
3660e071409bSToby Isaac   label   = dm->depthLabel;
3661e071409bSToby Isaac   *dspace = NULL;
3662e071409bSToby Isaac   if (field < Nf) {
3663e071409bSToby Isaac     PetscObject disc = prob->disc[field];
3664e071409bSToby Isaac 
3665e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
3666e071409bSToby Isaac       PetscDualSpace dsp;
3667e071409bSToby Isaac 
3668e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
3669e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
3670e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
3671e071409bSToby Isaac       h    = depth - 1 - h;
3672e071409bSToby Isaac       if (h) {
3673e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
3674e071409bSToby Isaac       } else {
3675e071409bSToby Isaac         *dspace = dsp;
3676e071409bSToby Isaac       }
3677e071409bSToby Isaac     }
3678e071409bSToby Isaac   }
3679e071409bSToby Isaac   PetscFunctionReturn(0);
3680e071409bSToby Isaac }
3681e071409bSToby Isaac 
3682e071409bSToby Isaac 
36831a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3684a6dfd86eSKarl Rupp {
3685552f7358SJed Brown   PetscScalar    *array, *vArray;
3686d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
36871a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
3688552f7358SJed Brown   PetscErrorCode  ierr;
3689552f7358SJed Brown 
36901b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
36912a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
36925a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
36935a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
36945a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
36953f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
36969df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
36979df71ca4SMatthew G. Knepley       PetscInt dof;
3698d9917b9dSMatthew G. Knepley 
36999df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
37009df71ca4SMatthew G. Knepley       size += dof;
37019df71ca4SMatthew G. Knepley     }
37029df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
37039df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
37042a3aaacfSMatthew G. Knepley       PetscInt       dof;
37055a1bb5cfSMatthew G. Knepley 
37065a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
37072a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
37085a1bb5cfSMatthew G. Knepley       size += dof;
37095a1bb5cfSMatthew G. Knepley     }
37103f7cbbe7SMatthew G. Knepley     if (!values) {
37113f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
37123f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
37133f7cbbe7SMatthew G. Knepley     }
37145a1bb5cfSMatthew G. Knepley     ierr = DMGetWorkArray(dm, size, PETSC_SCALAR, &array);CHKERRQ(ierr);
3715982e9ed1SMatthew G. Knepley   } else {
3716982e9ed1SMatthew G. Knepley     array = *values;
3717982e9ed1SMatthew G. Knepley   }
37189df71ca4SMatthew G. Knepley   size = 0;
37195a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
37209df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
37219df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
37229df71ca4SMatthew G. Knepley     PetscScalar *varr;
3723d9917b9dSMatthew G. Knepley 
37249df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
37259df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
37269df71ca4SMatthew G. Knepley     varr = &vArray[off];
37271a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
37281a271a75SMatthew G. Knepley       array[offset] = varr[d];
37299df71ca4SMatthew G. Knepley     }
37309df71ca4SMatthew G. Knepley     size += dof;
37319df71ca4SMatthew G. Knepley   }
37329df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
37339df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
37349df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
37355a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
37365a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
37375a1bb5cfSMatthew G. Knepley 
373852ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
37395a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
37405a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
37415a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
37425a1bb5cfSMatthew G. Knepley     if (o >= 0) {
37431a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
37441a271a75SMatthew G. Knepley         array[offset] = varr[d];
37455a1bb5cfSMatthew G. Knepley       }
37465a1bb5cfSMatthew G. Knepley     } else {
37471a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
37481a271a75SMatthew G. Knepley         array[offset] = varr[d];
37495a1bb5cfSMatthew G. Knepley       }
37505a1bb5cfSMatthew G. Knepley     }
37519df71ca4SMatthew G. Knepley     size += dof;
37525a1bb5cfSMatthew G. Knepley   }
37535a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
37549df71ca4SMatthew G. Knepley   if (!*values) {
37555a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
37565a1bb5cfSMatthew G. Knepley     *values = array;
37579df71ca4SMatthew G. Knepley   } else {
37588ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
37598c312ff3SMatthew G. Knepley     *csize = size;
37609df71ca4SMatthew G. Knepley   }
37615a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
37625a1bb5cfSMatthew G. Knepley }
3763d9917b9dSMatthew G. Knepley 
3764923c78e0SToby Isaac static PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3765923c78e0SToby Isaac {
3766923c78e0SToby Isaac   const PetscInt *cla;
3767923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
3768923c78e0SToby Isaac   PetscErrorCode ierr;
3769923c78e0SToby Isaac 
3770923c78e0SToby Isaac   PetscFunctionBeginHot;
3771923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
3772923c78e0SToby Isaac   if (!*clPoints) {
3773923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
3774923c78e0SToby Isaac 
3775923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3776923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
3777923c78e0SToby Isaac     /* Compress out points not in the section */
3778923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
3779923c78e0SToby Isaac       PetscInt r = pts[2*p];
3780923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
3781923c78e0SToby Isaac         pts[q*2]   = r;
3782923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
3783923c78e0SToby Isaac         ++q;
3784923c78e0SToby Isaac       }
3785923c78e0SToby Isaac     }
3786923c78e0SToby Isaac     np = q;
3787923c78e0SToby Isaac     cla = NULL;
3788923c78e0SToby Isaac   } else {
3789923c78e0SToby Isaac     PetscInt dof, off;
3790923c78e0SToby Isaac 
3791923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
3792923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
3793923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
3794923c78e0SToby Isaac     np   = dof/2;
3795923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
3796923c78e0SToby Isaac   }
3797923c78e0SToby Isaac   *numPoints = np;
3798923c78e0SToby Isaac   *points    = pts;
3799923c78e0SToby Isaac   *clp       = cla;
3800923c78e0SToby Isaac 
3801923c78e0SToby Isaac   PetscFunctionReturn(0);
3802923c78e0SToby Isaac }
3803923c78e0SToby Isaac 
3804923c78e0SToby Isaac static PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3805923c78e0SToby Isaac {
3806923c78e0SToby Isaac   PetscErrorCode ierr;
3807923c78e0SToby Isaac 
3808923c78e0SToby Isaac   PetscFunctionBeginHot;
3809923c78e0SToby Isaac   if (!*clPoints) {
3810923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
3811923c78e0SToby Isaac   } else {
3812923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
3813923c78e0SToby Isaac   }
3814923c78e0SToby Isaac   *numPoints = 0;
3815923c78e0SToby Isaac   *points    = NULL;
3816923c78e0SToby Isaac   *clSec     = NULL;
3817923c78e0SToby Isaac   *clPoints  = NULL;
3818923c78e0SToby Isaac   *clp       = NULL;
3819923c78e0SToby Isaac   PetscFunctionReturn(0);
3820923c78e0SToby Isaac }
3821923c78e0SToby Isaac 
382297e99dd9SToby Isaac PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Static(DM dm, PetscSection section, PetscInt numPoints, const PetscInt points[], const PetscInt clperm[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
38231a271a75SMatthew G. Knepley {
38241a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
382597e99dd9SToby Isaac   const PetscInt    **perms = NULL;
382697e99dd9SToby Isaac   const PetscScalar **flips = NULL;
38271a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
38281a271a75SMatthew G. Knepley 
38291a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3830fe02ba77SJed Brown   *size = 0;
383197e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
383297e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
383397e99dd9SToby Isaac     const PetscInt    point = points[2*p];
383497e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
383597e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
38361a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
38371a271a75SMatthew G. Knepley     const PetscScalar *varr;
38381a271a75SMatthew G. Knepley 
38391a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
38401a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
38411a271a75SMatthew G. Knepley     varr = &vArray[off];
384297e99dd9SToby Isaac     if (clperm) {
384397e99dd9SToby Isaac       if (perm) {
384497e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
38451a271a75SMatthew G. Knepley       } else {
384697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
384797e99dd9SToby Isaac       }
384897e99dd9SToby Isaac       if (flip) {
384997e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
385097e99dd9SToby Isaac       }
385197e99dd9SToby Isaac     } else {
385297e99dd9SToby Isaac       if (perm) {
385397e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
385497e99dd9SToby Isaac       } else {
385597e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
385697e99dd9SToby Isaac       }
385797e99dd9SToby Isaac       if (flip) {
385897e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
38591a271a75SMatthew G. Knepley       }
38601a271a75SMatthew G. Knepley     }
386197e99dd9SToby Isaac     offset += dof;
386297e99dd9SToby Isaac   }
386397e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
38641a271a75SMatthew G. Knepley   *size = offset;
38651a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
38661a271a75SMatthew G. Knepley }
38671a271a75SMatthew G. Knepley 
386897e99dd9SToby Isaac PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Fields_Static(DM dm, PetscSection section, PetscInt numPoints, const PetscInt points[], PetscInt numFields, const PetscInt clperm[], const PetscScalar vArray[], PetscInt *size, PetscScalar array[])
38691a271a75SMatthew G. Knepley {
38701a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
38711a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
38721a271a75SMatthew G. Knepley 
38731a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3874fe02ba77SJed Brown   *size = 0;
38751a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
387697e99dd9SToby Isaac     PetscInt          p;
387797e99dd9SToby Isaac     const PetscInt    **perms = NULL;
387897e99dd9SToby Isaac     const PetscScalar **flips = NULL;
38791a271a75SMatthew G. Knepley 
388097e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
388197e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
388297e99dd9SToby Isaac       const PetscInt    point = points[2*p];
388397e99dd9SToby Isaac       PetscInt          fdof, foff, b;
38841a271a75SMatthew G. Knepley       const PetscScalar *varr;
388597e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
388697e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
38871a271a75SMatthew G. Knepley 
38881a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
38891a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
38901a271a75SMatthew G. Knepley       varr = &vArray[foff];
389197e99dd9SToby Isaac       if (clperm) {
389297e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
389397e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
389497e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
38951a271a75SMatthew G. Knepley       } else {
389697e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
389797e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
389897e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
38991a271a75SMatthew G. Knepley       }
390097e99dd9SToby Isaac       offset += fdof;
39011a271a75SMatthew G. Knepley     }
390297e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
39031a271a75SMatthew G. Knepley   }
39041a271a75SMatthew G. Knepley   *size = offset;
39051a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
39061a271a75SMatthew G. Knepley }
39071a271a75SMatthew G. Knepley 
3908552f7358SJed Brown /*@C
3909552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
3910552f7358SJed Brown 
3911552f7358SJed Brown   Not collective
3912552f7358SJed Brown 
3913552f7358SJed Brown   Input Parameters:
3914552f7358SJed Brown + dm - The DM
3915552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
3916552f7358SJed Brown . v - The local vector
3917552f7358SJed Brown - point - The sieve point in the DM
3918552f7358SJed Brown 
3919552f7358SJed Brown   Output Parameters:
3920552f7358SJed Brown + csize - The number of values in the closure, or NULL
3921552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
3922552f7358SJed Brown 
3923552f7358SJed Brown   Fortran Notes:
3924552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
3925552f7358SJed Brown   include petsc.h90 in your code.
3926552f7358SJed Brown 
3927552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
3928552f7358SJed Brown 
3929552f7358SJed Brown   Level: intermediate
3930552f7358SJed Brown 
3931552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
3932552f7358SJed Brown @*/
3933552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3934552f7358SJed Brown {
3935552f7358SJed Brown   PetscSection       clSection;
3936d9917b9dSMatthew G. Knepley   IS                 clPoints;
39378a84db2dSMatthew G. Knepley   PetscScalar       *array;
39388a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
3939552f7358SJed Brown   PetscInt          *points = NULL;
39408f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
39411a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
3942552f7358SJed Brown   PetscErrorCode     ierr;
3943552f7358SJed Brown 
3944d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
3945552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3946552f7358SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
39471a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
39481a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
3949552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3950552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3951552f7358SJed Brown   if (depth == 1 && numFields < 2) {
39521a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
3953552f7358SJed Brown     PetscFunctionReturn(0);
3954552f7358SJed Brown   }
39551a271a75SMatthew G. Knepley   /* Get points */
3956923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
39571fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
39581a271a75SMatthew G. Knepley   /* Get array */
3959bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
39601a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
3961552f7358SJed Brown 
39621a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
3963552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
39641a271a75SMatthew G. Knepley       asize += dof;
3965552f7358SJed Brown     }
3966bd6c0d32SMatthew G. Knepley     if (!values) {
3967923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
39681a271a75SMatthew G. Knepley       if (csize) *csize = asize;
3969bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
3970bd6c0d32SMatthew G. Knepley     }
39711a271a75SMatthew G. Knepley     ierr = DMGetWorkArray(dm, asize, PETSC_SCALAR, &array);CHKERRQ(ierr);
3972d0f6b257SMatthew G. Knepley   } else {
3973d0f6b257SMatthew G. Knepley     array = *values;
3974d0f6b257SMatthew G. Knepley   }
39758a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
39761a271a75SMatthew G. Knepley   /* Get values */
397797e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
397897e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
39791a271a75SMatthew G. Knepley   /* Cleanup points */
3980923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
39811a271a75SMatthew G. Knepley   /* Cleanup array */
39828a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
3983d0f6b257SMatthew G. Knepley   if (!*values) {
3984552f7358SJed Brown     if (csize) *csize = size;
3985552f7358SJed Brown     *values = array;
3986d0f6b257SMatthew G. Knepley   } else {
3987f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
3988d0f6b257SMatthew G. Knepley     *csize = size;
3989d0f6b257SMatthew G. Knepley   }
3990552f7358SJed Brown   PetscFunctionReturn(0);
3991552f7358SJed Brown }
3992552f7358SJed Brown 
3993552f7358SJed Brown /*@C
3994552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
3995552f7358SJed Brown 
3996552f7358SJed Brown   Not collective
3997552f7358SJed Brown 
3998552f7358SJed Brown   Input Parameters:
3999552f7358SJed Brown + dm - The DM
40000298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
4001552f7358SJed Brown . v - The local vector
4002552f7358SJed Brown . point - The sieve point in the DM
40030298fd71SBarry Smith . csize - The number of values in the closure, or NULL
4004552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
4005552f7358SJed Brown 
40063813dfbdSMatthew G Knepley   Fortran Notes:
40073813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
40083813dfbdSMatthew G Knepley   include petsc.h90 in your code.
40093813dfbdSMatthew G Knepley 
40103813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
40113813dfbdSMatthew G Knepley 
4012552f7358SJed Brown   Level: intermediate
4013552f7358SJed Brown 
4014552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4015552f7358SJed Brown @*/
40167c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4017a6dfd86eSKarl Rupp {
4018552f7358SJed Brown   PetscInt       size = 0;
4019552f7358SJed Brown   PetscErrorCode ierr;
4020552f7358SJed Brown 
4021552f7358SJed Brown   PetscFunctionBegin;
4022552f7358SJed Brown   /* Should work without recalculating size */
4023552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, size, PETSC_SCALAR, (void*) values);CHKERRQ(ierr);
4024552f7358SJed Brown   PetscFunctionReturn(0);
4025552f7358SJed Brown }
4026552f7358SJed Brown 
4027552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
4028552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
4029552f7358SJed Brown 
403097e99dd9SToby Isaac PETSC_STATIC_INLINE PetscErrorCode updatePoint_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar*, PetscScalar), PetscBool setBC, const PetscInt perm[], const PetscScalar flip[], const PetscInt clperm[], const PetscScalar values[], PetscInt offset, PetscScalar array[])
4031552f7358SJed Brown {
4032552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4033552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4034552f7358SJed Brown   PetscScalar    *a;
4035552f7358SJed Brown   PetscInt        off, cind = 0, k;
4036552f7358SJed Brown   PetscErrorCode  ierr;
4037552f7358SJed Brown 
4038552f7358SJed Brown   PetscFunctionBegin;
4039552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4040552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4041552f7358SJed Brown   a    = &array[off];
4042552f7358SJed Brown   if (!cdof || setBC) {
404397e99dd9SToby Isaac     if (clperm) {
404497e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
404597e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
4046552f7358SJed Brown     } else {
404797e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
404897e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4049552f7358SJed Brown     }
4050552f7358SJed Brown   } else {
4051552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
405297e99dd9SToby Isaac     if (clperm) {
405397e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
4054552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
405597e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4056552f7358SJed Brown         }
4057552f7358SJed Brown       } else {
4058552f7358SJed Brown         for (k = 0; k < dof; ++k) {
4059552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
406097e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
406197e99dd9SToby Isaac         }
406297e99dd9SToby Isaac       }
406397e99dd9SToby Isaac     } else {
406497e99dd9SToby Isaac       if (perm) {
406597e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
406697e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
406797e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
406897e99dd9SToby Isaac         }
406997e99dd9SToby Isaac       } else {
407097e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
407197e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
407297e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
407397e99dd9SToby Isaac         }
4074552f7358SJed Brown       }
4075552f7358SJed Brown     }
4076552f7358SJed Brown   }
4077552f7358SJed Brown   PetscFunctionReturn(0);
4078552f7358SJed Brown }
4079552f7358SJed Brown 
408097e99dd9SToby Isaac PETSC_STATIC_INLINE PetscErrorCode updatePointBC_private(PetscSection section, PetscInt point, PetscInt dof, void (*fuse)(PetscScalar*, PetscScalar), const PetscInt perm[], const PetscScalar flip[], const PetscInt clperm[], const PetscScalar values[], PetscInt offset, PetscScalar array[])
4081a5e93ea8SMatthew G. Knepley {
4082a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
4083a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4084a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
4085a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
4086a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4087a5e93ea8SMatthew G. Knepley 
4088a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4089a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4090a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4091a5e93ea8SMatthew G. Knepley   a    = &array[off];
4092a5e93ea8SMatthew G. Knepley   if (cdof) {
4093a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
409497e99dd9SToby Isaac     if (clperm) {
409597e99dd9SToby Isaac       if (perm) {
4096a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4097a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
409897e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
409997e99dd9SToby Isaac             cind++;
4100a5e93ea8SMatthew G. Knepley           }
4101a5e93ea8SMatthew G. Knepley         }
4102a5e93ea8SMatthew G. Knepley       } else {
4103a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4104a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
410597e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
410697e99dd9SToby Isaac             cind++;
410797e99dd9SToby Isaac           }
410897e99dd9SToby Isaac         }
410997e99dd9SToby Isaac       }
411097e99dd9SToby Isaac     } else {
411197e99dd9SToby Isaac       if (perm) {
411297e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
411397e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
411497e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
411597e99dd9SToby Isaac             cind++;
411697e99dd9SToby Isaac           }
411797e99dd9SToby Isaac         }
411897e99dd9SToby Isaac       } else {
411997e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
412097e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
412197e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
412297e99dd9SToby Isaac             cind++;
412397e99dd9SToby Isaac           }
4124a5e93ea8SMatthew G. Knepley         }
4125a5e93ea8SMatthew G. Knepley       }
4126a5e93ea8SMatthew G. Knepley     }
4127a5e93ea8SMatthew G. Knepley   }
4128a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4129a5e93ea8SMatthew G. Knepley }
4130a5e93ea8SMatthew G. Knepley 
413197e99dd9SToby Isaac PETSC_STATIC_INLINE PetscErrorCode updatePointFields_private(PetscSection section, PetscInt point, const PetscInt *perm, const PetscScalar *flip, PetscInt f, void (*fuse)(PetscScalar*, PetscScalar), PetscBool setBC, const PetscInt clperm[], const PetscScalar values[], PetscInt *offset, PetscScalar array[])
4132a6dfd86eSKarl Rupp {
4133552f7358SJed Brown   PetscScalar    *a;
41341a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
41351a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
413697e99dd9SToby Isaac   PetscInt        cind = 0, b;
4137552f7358SJed Brown   PetscErrorCode  ierr;
4138552f7358SJed Brown 
4139552f7358SJed Brown   PetscFunctionBegin;
4140552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4141552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
41421a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
41431a271a75SMatthew G. Knepley   a    = &array[foff];
4144552f7358SJed Brown   if (!fcdof || setBC) {
414597e99dd9SToby Isaac     if (clperm) {
414697e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
414797e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4148552f7358SJed Brown     } else {
414997e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
415097e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4151552f7358SJed Brown     }
4152552f7358SJed Brown   } else {
4153552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
415497e99dd9SToby Isaac     if (clperm) {
415597e99dd9SToby Isaac       if (perm) {
415697e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
415797e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
415897e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4159552f7358SJed Brown         }
4160552f7358SJed Brown       } else {
416197e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
416297e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
416397e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
416497e99dd9SToby Isaac         }
416597e99dd9SToby Isaac       }
416697e99dd9SToby Isaac     } else {
416797e99dd9SToby Isaac       if (perm) {
416897e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
416997e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
417097e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
417197e99dd9SToby Isaac         }
417297e99dd9SToby Isaac       } else {
417397e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
417497e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
417597e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4176552f7358SJed Brown         }
4177552f7358SJed Brown       }
4178552f7358SJed Brown     }
4179552f7358SJed Brown   }
41801a271a75SMatthew G. Knepley   *offset += fdof;
4181552f7358SJed Brown   PetscFunctionReturn(0);
4182552f7358SJed Brown }
4183552f7358SJed Brown 
418497e99dd9SToby Isaac PETSC_STATIC_INLINE PetscErrorCode updatePointFieldsBC_private(PetscSection section, PetscInt point, const PetscInt perm[], const PetscScalar flip[], PetscInt f, void (*fuse)(PetscScalar*, PetscScalar), const PetscInt clperm[], const PetscScalar values[], PetscInt *offset, PetscScalar array[])
4185a5e93ea8SMatthew G. Knepley {
4186a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
41871a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
41881a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
418997e99dd9SToby Isaac   PetscInt        cind = 0, b;
4190a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4191a5e93ea8SMatthew G. Knepley 
4192a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4193a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4194a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
41951a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
41961a271a75SMatthew G. Knepley   a    = &array[foff];
4197a5e93ea8SMatthew G. Knepley   if (fcdof) {
4198a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
419997e99dd9SToby Isaac     if (clperm) {
420097e99dd9SToby Isaac       if (perm) {
420197e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
420297e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
420397e99dd9SToby Isaac             fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4204a5e93ea8SMatthew G. Knepley             ++cind;
4205a5e93ea8SMatthew G. Knepley           }
4206a5e93ea8SMatthew G. Knepley         }
4207a5e93ea8SMatthew G. Knepley       } else {
420897e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
420997e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
421097e99dd9SToby Isaac             fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
421197e99dd9SToby Isaac             ++cind;
421297e99dd9SToby Isaac           }
421397e99dd9SToby Isaac         }
421497e99dd9SToby Isaac       }
421597e99dd9SToby Isaac     } else {
421697e99dd9SToby Isaac       if (perm) {
421797e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
421897e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
421997e99dd9SToby Isaac             fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
422097e99dd9SToby Isaac             ++cind;
422197e99dd9SToby Isaac           }
422297e99dd9SToby Isaac         }
422397e99dd9SToby Isaac       } else {
422497e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
422597e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
422697e99dd9SToby Isaac             fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4227a5e93ea8SMatthew G. Knepley             ++cind;
4228a5e93ea8SMatthew G. Knepley           }
4229a5e93ea8SMatthew G. Knepley         }
4230a5e93ea8SMatthew G. Knepley       }
4231a5e93ea8SMatthew G. Knepley     }
4232a5e93ea8SMatthew G. Knepley   }
42331a271a75SMatthew G. Knepley   *offset += fdof;
4234a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4235a5e93ea8SMatthew G. Knepley }
4236a5e93ea8SMatthew G. Knepley 
42378f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4238a6dfd86eSKarl Rupp {
4239552f7358SJed Brown   PetscScalar    *array;
42401b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
42411b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4242552f7358SJed Brown   PetscErrorCode  ierr;
4243552f7358SJed Brown 
42441b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
4245b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4246b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
4247b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4248b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
4249b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4250b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4251b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
4252b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
4253b6ebb6e6SMatthew G. Knepley 
4254b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4255b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
4256b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
4257b6ebb6e6SMatthew G. Knepley     {
4258b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4259b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
4260b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
4261b6ebb6e6SMatthew G. Knepley 
4262b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
4263b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
4264b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
4265b6ebb6e6SMatthew G. Knepley       if (!cdof) {
4266b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4267b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4268b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4269b6ebb6e6SMatthew G. Knepley           }
4270b6ebb6e6SMatthew G. Knepley         } else {
4271b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4272b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4273b6ebb6e6SMatthew G. Knepley           }
4274b6ebb6e6SMatthew G. Knepley         }
4275b6ebb6e6SMatthew G. Knepley       } else {
4276b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
4277b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4278b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4279b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4280b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4281b6ebb6e6SMatthew G. Knepley           }
4282b6ebb6e6SMatthew G. Knepley         } else {
4283b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4284b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4285b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4286b6ebb6e6SMatthew G. Knepley           }
4287b6ebb6e6SMatthew G. Knepley         }
4288b6ebb6e6SMatthew G. Knepley       }
4289b6ebb6e6SMatthew G. Knepley     }
4290b6ebb6e6SMatthew G. Knepley   }
4291b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4292b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4293b6ebb6e6SMatthew G. Knepley }
42941b406b76SMatthew G. Knepley 
42951b406b76SMatthew G. Knepley /*@C
42961b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
42971b406b76SMatthew G. Knepley 
42981b406b76SMatthew G. Knepley   Not collective
42991b406b76SMatthew G. Knepley 
43001b406b76SMatthew G. Knepley   Input Parameters:
43011b406b76SMatthew G. Knepley + dm - The DM
43021b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
43031b406b76SMatthew G. Knepley . v - The local vector
43041b406b76SMatthew G. Knepley . point - The sieve point in the DM
43051b406b76SMatthew G. Knepley . values - The array of values
43061b406b76SMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
43071b406b76SMatthew G. Knepley 
43081b406b76SMatthew G. Knepley   Fortran Notes:
43091b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
43101b406b76SMatthew G. Knepley 
43111b406b76SMatthew G. Knepley   Level: intermediate
43121b406b76SMatthew G. Knepley 
43131b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
43141b406b76SMatthew G. Knepley @*/
43151b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
43161b406b76SMatthew G. Knepley {
43171b406b76SMatthew G. Knepley   PetscSection    clSection;
43181b406b76SMatthew G. Knepley   IS              clPoints;
43191b406b76SMatthew G. Knepley   PetscScalar    *array;
43201b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
432197e99dd9SToby Isaac   const PetscInt *clp, *clperm;
43221a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
43231b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
43241b406b76SMatthew G. Knepley 
43251a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
43261b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43271b406b76SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
43281a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43291a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
43301b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
43311b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
43321b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
43338f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
43341b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
43351b406b76SMatthew G. Knepley   }
43361a271a75SMatthew G. Knepley   /* Get points */
433797e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4338923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
43391a271a75SMatthew G. Knepley   /* Get array */
4340552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
43411a271a75SMatthew G. Knepley   /* Get values */
4342ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
434397e99dd9SToby Isaac     PetscInt offset = 0, f;
4344552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
434597e99dd9SToby Isaac       const PetscInt    **perms = NULL;
434697e99dd9SToby Isaac       const PetscScalar **flips = NULL;
434797e99dd9SToby Isaac 
434897e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4349552f7358SJed Brown       switch (mode) {
4350552f7358SJed Brown       case INSERT_VALUES:
435197e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
435297e99dd9SToby Isaac           const PetscInt    point = points[2*p];
435397e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
435497e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
435597e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4356552f7358SJed Brown         } break;
4357552f7358SJed Brown       case INSERT_ALL_VALUES:
435897e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
435997e99dd9SToby Isaac           const PetscInt    point = points[2*p];
436097e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
436197e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
436297e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4363552f7358SJed Brown         } break;
4364a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
436597e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
436697e99dd9SToby Isaac           const PetscInt    point = points[2*p];
436797e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
436897e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
436997e99dd9SToby Isaac           updatePointFieldsBC_private(section, point, perm, flip, f, insert, clperm, values, &offset, array);
4370a5e93ea8SMatthew G. Knepley         } break;
4371552f7358SJed Brown       case ADD_VALUES:
437297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
437397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
437497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
437597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
437697e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4377552f7358SJed Brown         } break;
4378552f7358SJed Brown       case ADD_ALL_VALUES:
437997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
438097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
438197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
438297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
438397e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4384552f7358SJed Brown         } break;
4385304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
438697e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
438797e99dd9SToby Isaac           const PetscInt    point = points[2*p];
438897e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
438997e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
439097e99dd9SToby Isaac           updatePointFieldsBC_private(section, point, perm, flip, f, add, clperm, values, &offset, array);
4391304ab55fSMatthew G. Knepley         } break;
4392552f7358SJed Brown       default:
4393f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4394552f7358SJed Brown       }
439597e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
43961a271a75SMatthew G. Knepley     }
4397552f7358SJed Brown   } else {
43981a271a75SMatthew G. Knepley     PetscInt dof, off;
439997e99dd9SToby Isaac     const PetscInt    **perms = NULL;
440097e99dd9SToby Isaac     const PetscScalar **flips = NULL;
44011a271a75SMatthew G. Knepley 
440297e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4403552f7358SJed Brown     switch (mode) {
4404552f7358SJed Brown     case INSERT_VALUES:
440597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
440697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
440797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
440897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
440997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
441097e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4411552f7358SJed Brown       } break;
4412552f7358SJed Brown     case INSERT_ALL_VALUES:
441397e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
441497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
441597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
441697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
441797e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
441897e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4419552f7358SJed Brown       } break;
4420a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
442197e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
442297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
442397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
442497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
442597e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
442697e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4427a5e93ea8SMatthew G. Knepley       } break;
4428552f7358SJed Brown     case ADD_VALUES:
442997e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
443097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
443197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
443297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
443397e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
443497e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4435552f7358SJed Brown       } break;
4436552f7358SJed Brown     case ADD_ALL_VALUES:
443797e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
443897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
443997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
444097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
444197e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
444297e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4443552f7358SJed Brown       } break;
4444304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
444597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
444697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
444797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
444897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
444997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
445097e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
4451304ab55fSMatthew G. Knepley       } break;
4452552f7358SJed Brown     default:
4453f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4454552f7358SJed Brown     }
445597e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4456552f7358SJed Brown   }
44571a271a75SMatthew G. Knepley   /* Cleanup points */
4458923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
44591a271a75SMatthew G. Knepley   /* Cleanup array */
4460552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4461552f7358SJed Brown   PetscFunctionReturn(0);
4462552f7358SJed Brown }
4463552f7358SJed Brown 
4464e07394fbSMatthew G. Knepley PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, const PetscScalar values[], InsertMode mode)
4465e07394fbSMatthew G. Knepley {
4466e07394fbSMatthew G. Knepley   PetscSection      clSection;
4467e07394fbSMatthew G. Knepley   IS                clPoints;
4468e07394fbSMatthew G. Knepley   PetscScalar       *array;
4469e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
4470b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
4471e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
447297e99dd9SToby Isaac   PetscInt          offset = 0, f;
4473e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
4474e07394fbSMatthew G. Knepley 
4475e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
4476e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4477e07394fbSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
4478e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4479e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4480e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4481e07394fbSMatthew G. Knepley   /* Get points */
4482b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4483923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4484e07394fbSMatthew G. Knepley   /* Get array */
4485e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4486e07394fbSMatthew G. Knepley   /* Get values */
4487e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
448897e99dd9SToby Isaac     const PetscInt    **perms = NULL;
448997e99dd9SToby Isaac     const PetscScalar **flips = NULL;
449097e99dd9SToby Isaac 
4491e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
4492e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4493e07394fbSMatthew G. Knepley         PetscInt fdof;
4494e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
4495e07394fbSMatthew G. Knepley         offset += fdof;
4496e07394fbSMatthew G. Knepley       }
4497e07394fbSMatthew G. Knepley       continue;
4498e07394fbSMatthew G. Knepley     }
449997e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4500e07394fbSMatthew G. Knepley     switch (mode) {
4501e07394fbSMatthew G. Knepley     case INSERT_VALUES:
450297e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
450397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
450497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
450597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4506b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4507e07394fbSMatthew G. Knepley       } break;
4508e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
450997e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
451097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
451197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
451297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4513b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4514e07394fbSMatthew G. Knepley         } break;
4515e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
451697e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
451797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
451897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
451997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4520b04c866fSMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, insert, clperm, values, &offset, array);
4521e07394fbSMatthew G. Knepley       } break;
4522e07394fbSMatthew G. Knepley     case ADD_VALUES:
452397e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
452497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
452597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
452697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4527b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4528e07394fbSMatthew G. Knepley       } break;
4529e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
453097e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
453197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
453297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
453397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4534b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4535e07394fbSMatthew G. Knepley       } break;
4536e07394fbSMatthew G. Knepley     default:
4537f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4538e07394fbSMatthew G. Knepley     }
453997e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4540e07394fbSMatthew G. Knepley   }
4541e07394fbSMatthew G. Knepley   /* Cleanup points */
4542923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4543e07394fbSMatthew G. Knepley   /* Cleanup array */
4544e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4545e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
4546e07394fbSMatthew G. Knepley }
4547e07394fbSMatthew G. Knepley 
45487cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
4549552f7358SJed Brown {
4550552f7358SJed Brown   PetscMPIInt    rank;
4551552f7358SJed Brown   PetscInt       i, j;
4552552f7358SJed Brown   PetscErrorCode ierr;
4553552f7358SJed Brown 
4554552f7358SJed Brown   PetscFunctionBegin;
455582f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
4556f347f43bSBarry Smith   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for sieve point %D\n", rank, point);CHKERRQ(ierr);
4557e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
4558e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
4559b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
4560b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
4561e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
4562b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
4563519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
45647eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
4565552f7358SJed Brown #else
4566b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
4567552f7358SJed Brown #endif
4568552f7358SJed Brown     }
456977a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
4570552f7358SJed Brown   }
4571552f7358SJed Brown   PetscFunctionReturn(0);
4572552f7358SJed Brown }
4573552f7358SJed Brown 
4574552f7358SJed Brown /* . off - The global offset of this point */
45754acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], PetscInt indices[])
4576a6dfd86eSKarl Rupp {
4577e6ccafaeSMatthew G Knepley   PetscInt        dof;    /* The number of unknowns on this point */
4578552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4579552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4580552f7358SJed Brown   PetscInt        cind = 0, k;
4581552f7358SJed Brown   PetscErrorCode  ierr;
4582552f7358SJed Brown 
4583552f7358SJed Brown   PetscFunctionBegin;
4584552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
4585552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4586552f7358SJed Brown   if (!cdof || setBC) {
45874acb8e1eSToby Isaac     if (perm) {
45884acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+perm[k]] = off + k;
4589552f7358SJed Brown     } else {
45904acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+k] = off + k;
4591552f7358SJed Brown     }
4592552f7358SJed Brown   } else {
4593552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
45944acb8e1eSToby Isaac     if (perm) {
45954acb8e1eSToby Isaac       for (k = 0; k < dof; ++k) {
45964acb8e1eSToby Isaac         if ((cind < cdof) && (k == cdofs[cind])) {
45974acb8e1eSToby Isaac           /* Insert check for returning constrained indices */
45984acb8e1eSToby Isaac           indices[*loff+perm[k]] = -(off+k+1);
45994acb8e1eSToby Isaac           ++cind;
46004acb8e1eSToby Isaac         } else {
46014acb8e1eSToby Isaac           indices[*loff+perm[k]] = off+k-cind;
46024acb8e1eSToby Isaac         }
46034acb8e1eSToby Isaac       }
46044acb8e1eSToby Isaac     } else {
4605552f7358SJed Brown       for (k = 0; k < dof; ++k) {
4606552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
4607552f7358SJed Brown           /* Insert check for returning constrained indices */
4608e6ccafaeSMatthew G Knepley           indices[*loff+k] = -(off+k+1);
4609552f7358SJed Brown           ++cind;
4610552f7358SJed Brown         } else {
4611e6ccafaeSMatthew G Knepley           indices[*loff+k] = off+k-cind;
4612552f7358SJed Brown         }
4613552f7358SJed Brown       }
4614552f7358SJed Brown     }
4615552f7358SJed Brown   }
4616e6ccafaeSMatthew G Knepley   *loff += dof;
4617552f7358SJed Brown   PetscFunctionReturn(0);
4618552f7358SJed Brown }
4619552f7358SJed Brown 
4620552f7358SJed Brown /* . off - The global offset of this point */
46214acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, PetscInt indices[])
4622a6dfd86eSKarl Rupp {
4623552f7358SJed Brown   PetscInt       numFields, foff, f;
4624552f7358SJed Brown   PetscErrorCode ierr;
4625552f7358SJed Brown 
4626552f7358SJed Brown   PetscFunctionBegin;
4627552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4628552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
46294acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
4630552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
46314acb8e1eSToby Isaac     PetscInt        cind = 0, b;
46324acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
4633552f7358SJed Brown 
4634552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4635552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
4636552f7358SJed Brown     if (!cfdof || setBC) {
46374acb8e1eSToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {indices[foffs[f]+perm[b]] = off+foff+b;}}
46384acb8e1eSToby Isaac       else      {for (b = 0; b < fdof; b++) {indices[foffs[f]+     b ] = off+foff+b;}}
4639552f7358SJed Brown     } else {
4640552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
46414acb8e1eSToby Isaac       if (perm) {
46424acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
46434acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
46444acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = -(off+foff+b+1);
4645552f7358SJed Brown             ++cind;
4646552f7358SJed Brown           } else {
46474acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = off+foff+b-cind;
4648552f7358SJed Brown           }
4649552f7358SJed Brown         }
4650552f7358SJed Brown       } else {
46514acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
46524acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
46534acb8e1eSToby Isaac             indices[foffs[f]+b] = -(off+foff+b+1);
4654552f7358SJed Brown             ++cind;
4655552f7358SJed Brown           } else {
46564acb8e1eSToby Isaac             indices[foffs[f]+b] = off+foff+b-cind;
4657552f7358SJed Brown           }
4658552f7358SJed Brown         }
4659552f7358SJed Brown       }
4660552f7358SJed Brown     }
4661a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
4662552f7358SJed Brown     foffs[f] += fdof;
4663552f7358SJed Brown   }
4664552f7358SJed Brown   PetscFunctionReturn(0);
4665552f7358SJed Brown }
4666552f7358SJed Brown 
46674acb8e1eSToby Isaac PetscErrorCode DMPlexAnchorsModifyMat(DM dm, PetscSection section, PetscInt numPoints, PetscInt numIndices, const PetscInt points[], const PetscInt ***perms, const PetscScalar values[], PetscInt *outNumPoints, PetscInt *outNumIndices, PetscInt *outPoints[], PetscScalar *outValues[], PetscInt offsets[], PetscBool multiplyLeft)
4668d3d1a6afSToby Isaac {
4669d3d1a6afSToby Isaac   Mat             cMat;
4670d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
4671d3d1a6afSToby Isaac   IS              aIS;
4672d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
4673d3d1a6afSToby Isaac   const PetscInt  *anchors;
4674e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
4675d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
4676d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
4677d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
4678d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
4679d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
4680d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
4681d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
46826ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
4683d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
4684d3d1a6afSToby Isaac   PetscErrorCode  ierr;
4685d3d1a6afSToby Isaac 
4686d3d1a6afSToby Isaac   PetscFunctionBegin;
4687d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4688d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4689d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4690d3d1a6afSToby Isaac 
4691a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
4692d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
4693d3d1a6afSToby Isaac   if (aSec) {
4694d3d1a6afSToby Isaac     ierr = PetscMemzero(newOffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
4695d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
4696d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
4697d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
4698d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
4699d3d1a6afSToby Isaac      * into the global matrix anyway) */
4700d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
4701d3d1a6afSToby Isaac       PetscInt b    = points[p];
47024b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4703d3d1a6afSToby Isaac 
47044b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
47054b2f2278SToby Isaac       if (!bSecDof) {
47064b2f2278SToby Isaac         continue;
47074b2f2278SToby Isaac       }
4708d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4709d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
4710d3d1a6afSToby Isaac       }
4711d3d1a6afSToby Isaac       if (bDof) {
4712d3d1a6afSToby Isaac         /* this point is constrained */
4713d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
4714d3d1a6afSToby Isaac         PetscInt bOff, q;
4715d3d1a6afSToby Isaac 
4716d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
4717d3d1a6afSToby Isaac         newNumPoints  += bDof;
4718d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
4719d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4720d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
4721d3d1a6afSToby Isaac           PetscInt aDof;
4722d3d1a6afSToby Isaac 
4723d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
4724d3d1a6afSToby Isaac           newNumIndices += aDof;
4725d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
4726d3d1a6afSToby Isaac             PetscInt fDof;
4727d3d1a6afSToby Isaac 
4728d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
4729d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
4730d3d1a6afSToby Isaac           }
4731d3d1a6afSToby Isaac         }
4732d3d1a6afSToby Isaac       }
4733d3d1a6afSToby Isaac       else {
4734d3d1a6afSToby Isaac         /* this point is not constrained */
4735d3d1a6afSToby Isaac         newNumPoints++;
47364b2f2278SToby Isaac         newNumIndices += bSecDof;
4737d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
4738d3d1a6afSToby Isaac           PetscInt fDof;
4739d3d1a6afSToby Isaac 
4740d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4741d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
4742d3d1a6afSToby Isaac         }
4743d3d1a6afSToby Isaac       }
4744d3d1a6afSToby Isaac     }
4745d3d1a6afSToby Isaac   }
4746d3d1a6afSToby Isaac   if (!anyConstrained) {
474772b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
474872b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
474972b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
475072b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
475172b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
4752d3d1a6afSToby Isaac     PetscFunctionReturn(0);
4753d3d1a6afSToby Isaac   }
4754d3d1a6afSToby Isaac 
47556ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
47566ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
47576ecaa68aSToby Isaac 
4758f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
4759d3d1a6afSToby Isaac 
47606ecaa68aSToby Isaac   if (!outPoints && !outValues) {
47616ecaa68aSToby Isaac     if (offsets) {
47626ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
47636ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
47646ecaa68aSToby Isaac       }
47656ecaa68aSToby Isaac     }
47666ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
47676ecaa68aSToby Isaac     PetscFunctionReturn(0);
47686ecaa68aSToby Isaac   }
47696ecaa68aSToby Isaac 
4770f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
4771d3d1a6afSToby Isaac 
4772f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
4773d3d1a6afSToby Isaac 
4774d3d1a6afSToby Isaac   /* workspaces */
4775d3d1a6afSToby Isaac   if (numFields) {
4776d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
4777d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
4778d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
4779d3d1a6afSToby Isaac     }
4780d3d1a6afSToby Isaac   }
4781d3d1a6afSToby Isaac   else {
4782d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
4783d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
4784d3d1a6afSToby Isaac   }
4785d3d1a6afSToby Isaac 
4786d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
4787d3d1a6afSToby Isaac   if (numFields) {
47884b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
47894b2f2278SToby Isaac 
4790d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4791d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
47924b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4793d3d1a6afSToby Isaac 
47944b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
47954b2f2278SToby Isaac       if (!bSecDof) {
47964b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
47974b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
47984b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
47994b2f2278SToby Isaac         }
48004b2f2278SToby Isaac         continue;
48014b2f2278SToby Isaac       }
4802d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4803d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4804d3d1a6afSToby Isaac       }
4805d3d1a6afSToby Isaac       if (bDof) {
4806d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4807d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
4808d3d1a6afSToby Isaac 
4809d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4810d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4811d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
4812d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
4813d3d1a6afSToby Isaac             PetscInt aFDof;
4814d3d1a6afSToby Isaac 
4815d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
4816d3d1a6afSToby Isaac             allFDof += aFDof;
4817d3d1a6afSToby Isaac           }
4818d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
4819d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
4820d3d1a6afSToby Isaac         }
4821d3d1a6afSToby Isaac       }
4822d3d1a6afSToby Isaac       else {
4823d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4824d3d1a6afSToby Isaac           PetscInt fDof;
4825d3d1a6afSToby Isaac 
4826d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4827d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
4828d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
4829d3d1a6afSToby Isaac         }
4830d3d1a6afSToby Isaac       }
4831d3d1a6afSToby Isaac     }
48324b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
48334b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
48344b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
4835d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
4836d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
4837d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
4838d3d1a6afSToby Isaac       }
483919f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
484019f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
4841d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
4842d3d1a6afSToby Isaac     }
4843d3d1a6afSToby Isaac   }
4844d3d1a6afSToby Isaac   else {
4845d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4846d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
48474b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4848d3d1a6afSToby Isaac 
48494b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
48504b2f2278SToby Isaac       if (!bSecDof) {
48514b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
48524b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
48534b2f2278SToby Isaac         continue;
48544b2f2278SToby Isaac       }
4855d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4856d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4857d3d1a6afSToby Isaac       }
4858d3d1a6afSToby Isaac       if (bDof) {
48594b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
4860d3d1a6afSToby Isaac 
4861d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4862d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4863d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
4864d3d1a6afSToby Isaac 
4865d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
4866d3d1a6afSToby Isaac           allDof += aDof;
4867d3d1a6afSToby Isaac         }
4868d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
48694b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
4870d3d1a6afSToby Isaac       }
4871d3d1a6afSToby Isaac       else {
48724b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
4873d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
4874d3d1a6afSToby Isaac       }
4875d3d1a6afSToby Isaac     }
4876d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
4877d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
4878d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4879d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
4880d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
4881d3d1a6afSToby Isaac     }
4882d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
4883d3d1a6afSToby Isaac   }
4884d3d1a6afSToby Isaac 
48856ecaa68aSToby Isaac   /* output arrays */
48866ecaa68aSToby Isaac   ierr = DMGetWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
48876ecaa68aSToby Isaac 
4888d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
4889d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
4890d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
4891d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
4892d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
4893d3d1a6afSToby Isaac   if (numFields) {
4894d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
4895d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4896d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
48974b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4898d3d1a6afSToby Isaac 
48994b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
49004b2f2278SToby Isaac       if (!bSecDof) {
49014b2f2278SToby Isaac         continue;
49024b2f2278SToby Isaac       }
4903d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4904d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4905d3d1a6afSToby Isaac       }
4906d3d1a6afSToby Isaac       if (bDof) {
4907d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
4908d3d1a6afSToby Isaac 
4909d3d1a6afSToby Isaac         fStart[0] = 0;
4910d3d1a6afSToby Isaac         fEnd[0]   = 0;
4911d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4912d3d1a6afSToby Isaac           PetscInt fDof;
4913d3d1a6afSToby Isaac 
4914d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
4915d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
4916d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
4917d3d1a6afSToby Isaac         }
4918d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
49194acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, indices);CHKERRQ(ierr);
4920d3d1a6afSToby Isaac 
4921d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
4922d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
4923d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4924d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
4925d3d1a6afSToby Isaac 
4926d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
4927d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
4928d3d1a6afSToby Isaac         }
4929d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4930d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4931d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
4932d3d1a6afSToby Isaac 
4933d3d1a6afSToby 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 */
4934d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
4935d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
4936302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
49374acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, newIndices);CHKERRQ(ierr);
4938d3d1a6afSToby Isaac         }
4939d3d1a6afSToby Isaac         newP += bDof;
4940d3d1a6afSToby Isaac 
49416ecaa68aSToby Isaac         if (outValues) {
4942d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
4943d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
4944d3d1a6afSToby 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);
4945d3d1a6afSToby Isaac           }
4946d3d1a6afSToby Isaac         }
49476ecaa68aSToby Isaac       }
4948d3d1a6afSToby Isaac       else {
4949d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
4950d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
4951d3d1a6afSToby Isaac         newP++;
4952d3d1a6afSToby Isaac       }
4953d3d1a6afSToby Isaac     }
4954d3d1a6afSToby Isaac   } else {
4955d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4956d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4957d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
49584b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4959d3d1a6afSToby Isaac 
49604b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
49614b2f2278SToby Isaac       if (!bSecDof) {
49624b2f2278SToby Isaac         continue;
49634b2f2278SToby Isaac       }
4964d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4965d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4966d3d1a6afSToby Isaac       }
4967d3d1a6afSToby Isaac       if (bDof) {
4968d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
4969d3d1a6afSToby Isaac 
4970d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
49714acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, indices);CHKERRQ(ierr);
4972d3d1a6afSToby Isaac 
4973d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
4974d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4975d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
4976d3d1a6afSToby Isaac 
4977d3d1a6afSToby 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 */
4978d3d1a6afSToby Isaac 
4979d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
4980d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
4981302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
49824acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, newIndices);CHKERRQ(ierr);
4983d3d1a6afSToby Isaac         }
4984d3d1a6afSToby Isaac         newP += bDof;
4985d3d1a6afSToby Isaac 
4986d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
49876ecaa68aSToby Isaac         if (outValues) {
4988d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
4989d3d1a6afSToby Isaac         }
49906ecaa68aSToby Isaac       }
4991d3d1a6afSToby Isaac       else {
4992d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
4993d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
4994d3d1a6afSToby Isaac         newP++;
4995d3d1a6afSToby Isaac       }
4996d3d1a6afSToby Isaac     }
4997d3d1a6afSToby Isaac   }
4998d3d1a6afSToby Isaac 
49996ecaa68aSToby Isaac   if (outValues) {
50006ecaa68aSToby Isaac     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
5001d3d1a6afSToby Isaac     ierr = PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));CHKERRQ(ierr);
5002d3d1a6afSToby Isaac     /* multiply constraints on the right */
5003d3d1a6afSToby Isaac     if (numFields) {
5004d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
5005d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
5006d3d1a6afSToby Isaac 
5007d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5008d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
5009d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5010d3d1a6afSToby Isaac           PetscInt c, r, k;
5011d3d1a6afSToby Isaac           PetscInt dof;
5012d3d1a6afSToby Isaac 
5013d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
50144b2f2278SToby Isaac           if (!dof) {
50154b2f2278SToby Isaac             continue;
50164b2f2278SToby Isaac           }
5017d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5018d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
5019d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
5020d3d1a6afSToby Isaac 
5021d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5022d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
5023d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
50244acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
5025d3d1a6afSToby Isaac                 }
5026d3d1a6afSToby Isaac               }
5027d3d1a6afSToby Isaac             }
5028d3d1a6afSToby Isaac           }
5029d3d1a6afSToby Isaac           else {
5030d3d1a6afSToby Isaac             /* copy this column as is */
5031d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5032d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
5033d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5034d3d1a6afSToby Isaac               }
5035d3d1a6afSToby Isaac             }
5036d3d1a6afSToby Isaac           }
5037d3d1a6afSToby Isaac           oldOff += dof;
5038d3d1a6afSToby Isaac         }
5039d3d1a6afSToby Isaac       }
5040d3d1a6afSToby Isaac     }
5041d3d1a6afSToby Isaac     else {
5042d3d1a6afSToby Isaac       PetscInt oldOff = 0;
5043d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5044d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
5045d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
5046d3d1a6afSToby Isaac         PetscInt c, r, k;
5047d3d1a6afSToby Isaac         PetscInt dof;
5048d3d1a6afSToby Isaac 
5049d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
50504b2f2278SToby Isaac         if (!dof) {
50514b2f2278SToby Isaac           continue;
50524b2f2278SToby Isaac         }
5053d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5054d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5055d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
5056d3d1a6afSToby Isaac 
5057d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5058d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
5059d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
5060d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5061d3d1a6afSToby Isaac               }
5062d3d1a6afSToby Isaac             }
5063d3d1a6afSToby Isaac           }
5064d3d1a6afSToby Isaac         }
5065d3d1a6afSToby Isaac         else {
5066d3d1a6afSToby Isaac           /* copy this column as is */
5067d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5068d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
5069d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5070d3d1a6afSToby Isaac             }
5071d3d1a6afSToby Isaac           }
5072d3d1a6afSToby Isaac         }
5073d3d1a6afSToby Isaac         oldOff += dof;
5074d3d1a6afSToby Isaac       }
5075d3d1a6afSToby Isaac     }
5076d3d1a6afSToby Isaac 
50776ecaa68aSToby Isaac     if (multiplyLeft) {
50786ecaa68aSToby Isaac       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
5079d3d1a6afSToby Isaac       ierr = PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));CHKERRQ(ierr);
5080d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
5081d3d1a6afSToby Isaac       if (numFields) {
5082d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5083d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
5084d3d1a6afSToby Isaac 
5085d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
5086d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
5087d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
5088d3d1a6afSToby Isaac             PetscInt c, r, k;
5089d3d1a6afSToby Isaac             PetscInt dof;
5090d3d1a6afSToby Isaac 
5091d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
5092d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5093d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5094d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
5095d3d1a6afSToby Isaac 
5096d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
5097d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5098d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
5099d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5100d3d1a6afSToby Isaac                   }
5101d3d1a6afSToby Isaac                 }
5102d3d1a6afSToby Isaac               }
5103d3d1a6afSToby Isaac             }
5104d3d1a6afSToby Isaac             else {
5105d3d1a6afSToby Isaac               /* copy this row as is */
5106d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
5107d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5108d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5109d3d1a6afSToby Isaac                 }
5110d3d1a6afSToby Isaac               }
5111d3d1a6afSToby Isaac             }
5112d3d1a6afSToby Isaac             oldOff += dof;
5113d3d1a6afSToby Isaac           }
5114d3d1a6afSToby Isaac         }
5115d3d1a6afSToby Isaac       }
5116d3d1a6afSToby Isaac       else {
5117d3d1a6afSToby Isaac         PetscInt oldOff = 0;
5118d3d1a6afSToby Isaac 
5119d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5120d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
5121d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5122d3d1a6afSToby Isaac           PetscInt c, r, k;
5123d3d1a6afSToby Isaac           PetscInt dof;
5124d3d1a6afSToby Isaac 
5125d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
5126d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5127d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5128d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5129d3d1a6afSToby Isaac 
5130d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5131d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5132d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5133d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5134d3d1a6afSToby Isaac                 }
5135d3d1a6afSToby Isaac               }
5136d3d1a6afSToby Isaac             }
5137d3d1a6afSToby Isaac           }
5138d3d1a6afSToby Isaac           else {
5139d3d1a6afSToby Isaac             /* copy this row as is */
51409fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5141d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5142d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5143d3d1a6afSToby Isaac               }
5144d3d1a6afSToby Isaac             }
5145d3d1a6afSToby Isaac           }
5146d3d1a6afSToby Isaac           oldOff += dof;
5147d3d1a6afSToby Isaac         }
5148d3d1a6afSToby Isaac       }
5149d3d1a6afSToby Isaac 
51506ecaa68aSToby Isaac       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
51516ecaa68aSToby Isaac     }
51526ecaa68aSToby Isaac     else {
51536ecaa68aSToby Isaac       newValues = tmpValues;
51546ecaa68aSToby Isaac     }
51556ecaa68aSToby Isaac   }
51566ecaa68aSToby Isaac 
5157d3d1a6afSToby Isaac   /* clean up */
5158d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
5159d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
51606ecaa68aSToby Isaac 
5161d3d1a6afSToby Isaac   if (numFields) {
5162d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
5163d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5164d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
5165d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5166d3d1a6afSToby Isaac     }
5167d3d1a6afSToby Isaac   }
5168d3d1a6afSToby Isaac   else {
5169d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5170d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
5171d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5172d3d1a6afSToby Isaac   }
5173d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5174d3d1a6afSToby Isaac 
5175d3d1a6afSToby Isaac   /* output */
51766ecaa68aSToby Isaac   if (outPoints) {
5177d3d1a6afSToby Isaac     *outPoints = newPoints;
51786ecaa68aSToby Isaac   }
51796ecaa68aSToby Isaac   else {
51806ecaa68aSToby Isaac     ierr = DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
51816ecaa68aSToby Isaac   }
518231620726SToby Isaac   if (outValues) {
5183d3d1a6afSToby Isaac     *outValues = newValues;
51846ecaa68aSToby Isaac   }
51856ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
5186d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
5187d3d1a6afSToby Isaac   }
5188d3d1a6afSToby Isaac   PetscFunctionReturn(0);
5189d3d1a6afSToby Isaac }
5190d3d1a6afSToby Isaac 
51917cd05799SMatthew G. Knepley /*@C
51927cd05799SMatthew G. Knepley   DMPlexGetClosureIndices - Get the indices in a vector v for all points in the closure of the given point
51937cd05799SMatthew G. Knepley 
51947cd05799SMatthew G. Knepley   Not collective
51957cd05799SMatthew G. Knepley 
51967cd05799SMatthew G. Knepley   Input Parameters:
51977cd05799SMatthew G. Knepley + dm - The DM
51987cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
51997cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
52007cd05799SMatthew G. Knepley - point - The mesh point
52017cd05799SMatthew G. Knepley 
52027cd05799SMatthew G. Knepley   Output parameters:
52037cd05799SMatthew G. Knepley + numIndices - The number of indices
52047cd05799SMatthew G. Knepley . indices - The indices
52057cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
52067cd05799SMatthew G. Knepley 
52077cd05799SMatthew G. Knepley   Note: Must call DMPlexRestoreClosureIndices() to free allocated memory
52087cd05799SMatthew G. Knepley 
52097cd05799SMatthew G. Knepley   Level: advanced
52107cd05799SMatthew G. Knepley 
52117cd05799SMatthew G. Knepley .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
52127cd05799SMatthew G. Knepley @*/
52136ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
52147773e69fSMatthew G. Knepley {
52157773e69fSMatthew G. Knepley   PetscSection    clSection;
52167773e69fSMatthew G. Knepley   IS              clPoints;
52177773e69fSMatthew G. Knepley   const PetscInt *clp;
52184acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
52197773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
52207773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
52217773e69fSMatthew G. Knepley   PetscInt        offsets[32];
52227773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
52237773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
52247773e69fSMatthew G. Knepley 
52257773e69fSMatthew G. Knepley   PetscFunctionBegin;
52267773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52277773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
52287773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
52297773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
52307773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
52317773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
52327773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
52337773e69fSMatthew G. Knepley   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
52347773e69fSMatthew G. Knepley   /* Get points in closure */
5235923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
52367773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
52377773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
52387773e69fSMatthew G. Knepley     PetscInt dof, fdof;
52397773e69fSMatthew G. Knepley 
52407773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
52417773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
52427773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
52437773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
52447773e69fSMatthew G. Knepley     }
52457773e69fSMatthew G. Knepley     Nind += dof;
52467773e69fSMatthew G. Knepley   }
52477773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
52488ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
52494acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
52504acb8e1eSToby Isaac   /* Get dual space symmetries */
52514acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
52524acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52534acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52544acb8e1eSToby Isaac   }
52557773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
52567773e69fSMatthew G. Knepley   {
52574acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
52587773e69fSMatthew G. Knepley     if (numPointsNew) {
52594acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
52604acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52614acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52624acb8e1eSToby Isaac       }
52634acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
52644acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
52654acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
52664acb8e1eSToby Isaac       }
5267923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
52687773e69fSMatthew G. Knepley       numPoints = numPointsNew;
52697773e69fSMatthew G. Knepley       Nind      = NindNew;
52707773e69fSMatthew G. Knepley       points    = pointsNew;
52717773e69fSMatthew G. Knepley     }
52727773e69fSMatthew G. Knepley   }
52737773e69fSMatthew G. Knepley   /* Calculate indices */
52747773e69fSMatthew G. Knepley   ierr = DMGetWorkArray(dm, Nind, PETSC_INT, indices);CHKERRQ(ierr);
52757773e69fSMatthew G. Knepley   if (Nf) {
52766ecaa68aSToby Isaac     if (outOffsets) {
52776ecaa68aSToby Isaac       PetscInt f;
52786ecaa68aSToby Isaac 
527946bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
52806ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
52816ecaa68aSToby Isaac       }
52826ecaa68aSToby Isaac     }
52834acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
52844acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
52854acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, *indices);
52867773e69fSMatthew G. Knepley     }
52877773e69fSMatthew G. Knepley   } else {
52884acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
52894acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
52904acb8e1eSToby Isaac 
52914acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
52924acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, *indices);
52937773e69fSMatthew G. Knepley     }
52947773e69fSMatthew G. Knepley   }
52957773e69fSMatthew G. Knepley   /* Cleanup points */
52964acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
52974acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52984acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52994acb8e1eSToby Isaac   }
53007773e69fSMatthew G. Knepley   if (numPointsNew) {
53017773e69fSMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, PETSC_INT, &pointsNew);CHKERRQ(ierr);
53027773e69fSMatthew G. Knepley   } else {
5303923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
53047773e69fSMatthew G. Knepley   }
53057773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
53067773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
53077773e69fSMatthew G. Knepley }
53087773e69fSMatthew G. Knepley 
53097cd05799SMatthew G. Knepley /*@C
53107cd05799SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restore the indices in a vector v for all points in the closure of the given point
53117cd05799SMatthew G. Knepley 
53127cd05799SMatthew G. Knepley   Not collective
53137cd05799SMatthew G. Knepley 
53147cd05799SMatthew G. Knepley   Input Parameters:
53157cd05799SMatthew G. Knepley + dm - The DM
53167cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
53177cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
53187cd05799SMatthew G. Knepley . point - The mesh point
53197cd05799SMatthew G. Knepley . numIndices - The number of indices
53207cd05799SMatthew G. Knepley . indices - The indices
53217cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
53227cd05799SMatthew G. Knepley 
53237cd05799SMatthew G. Knepley   Level: advanced
53247cd05799SMatthew G. Knepley 
53257cd05799SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
53267cd05799SMatthew G. Knepley @*/
532746bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
53287773e69fSMatthew G. Knepley {
53297773e69fSMatthew G. Knepley   PetscErrorCode ierr;
53307773e69fSMatthew G. Knepley 
53317773e69fSMatthew G. Knepley   PetscFunctionBegin;
53327773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53337773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
53347773e69fSMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, indices);CHKERRQ(ierr);
53357773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
53367773e69fSMatthew G. Knepley }
53377773e69fSMatthew G. Knepley 
53387f5d1fdeSMatthew G. Knepley /*@C
53397f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
53407f5d1fdeSMatthew G. Knepley 
53417f5d1fdeSMatthew G. Knepley   Not collective
53427f5d1fdeSMatthew G. Knepley 
53437f5d1fdeSMatthew G. Knepley   Input Parameters:
53447f5d1fdeSMatthew G. Knepley + dm - The DM
5345ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5346ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
53477f5d1fdeSMatthew G. Knepley . A - The matrix
53487f5d1fdeSMatthew G. Knepley . point - The sieve point in the DM
53497f5d1fdeSMatthew G. Knepley . values - The array of values
53507f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
53517f5d1fdeSMatthew G. Knepley 
53527f5d1fdeSMatthew G. Knepley   Fortran Notes:
53537f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
53547f5d1fdeSMatthew G. Knepley 
53557f5d1fdeSMatthew G. Knepley   Level: intermediate
53567f5d1fdeSMatthew G. Knepley 
53577f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
53587f5d1fdeSMatthew G. Knepley @*/
53597c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5360552f7358SJed Brown {
5361552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
53621b406b76SMatthew G. Knepley   PetscSection        clSection;
53631b406b76SMatthew G. Knepley   IS                  clPoints;
5364d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
53651b406b76SMatthew G. Knepley   const PetscInt     *clp;
5366552f7358SJed Brown   PetscInt           *indices;
5367552f7358SJed Brown   PetscInt            offsets[32];
53684acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
53694acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
5370923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
53714acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
5372d3d1a6afSToby Isaac   PetscScalar        *newValues;
5373552f7358SJed Brown   PetscErrorCode      ierr;
5374552f7358SJed Brown 
5375552f7358SJed Brown   PetscFunctionBegin;
5376552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5377ebd6d717SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
53783dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5379ebd6d717SJed Brown   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
53803dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
53813dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5382552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
538382f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5384552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5385923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5386552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5387552f7358SJed Brown     PetscInt fdof;
5388552f7358SJed Brown 
5389552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5390552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5391552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5392552f7358SJed Brown       offsets[f+1] += fdof;
5393552f7358SJed Brown     }
5394552f7358SJed Brown     numIndices += dof;
5395552f7358SJed Brown   }
53960d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
53970d644c17SKarl Rupp 
53988ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
53994acb8e1eSToby Isaac   /* Get symmetries */
54004acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
54014acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54024acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54034acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
54044acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
54054acb8e1eSToby Isaac 
54064acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
54074acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
54084acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
54094acb8e1eSToby Isaac 
54104acb8e1eSToby Isaac         if (!numFields) {
54114acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
54124acb8e1eSToby Isaac         } else {
54134acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
54144acb8e1eSToby Isaac         }
54154acb8e1eSToby Isaac         if (flip) {
54164acb8e1eSToby Isaac           PetscInt i, j, k;
54174acb8e1eSToby Isaac 
54184acb8e1eSToby Isaac           if (!valCopy) {
54194acb8e1eSToby Isaac             ierr = DMGetWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
54204acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
54214acb8e1eSToby Isaac             values = valCopy;
54224acb8e1eSToby Isaac           }
54234acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
5424775f7be2SToby Isaac             PetscScalar fval = flip[i];
54254acb8e1eSToby Isaac 
54264acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
54274acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
54284acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
54294acb8e1eSToby Isaac             }
54304acb8e1eSToby Isaac           }
54314acb8e1eSToby Isaac         }
54324acb8e1eSToby Isaac         foffset += fdof;
54334acb8e1eSToby Isaac       }
54344acb8e1eSToby Isaac     }
54354acb8e1eSToby Isaac   }
54364acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
5437d3d1a6afSToby Isaac   if (newNumPoints) {
54384acb8e1eSToby Isaac     if (valCopy) {
54394acb8e1eSToby Isaac       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
54404acb8e1eSToby Isaac     }
54414acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
54424acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54434acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54444acb8e1eSToby Isaac     }
54454acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
54464acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
54474acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
54484acb8e1eSToby Isaac     }
5449923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5450d3d1a6afSToby Isaac     numPoints  = newNumPoints;
5451d3d1a6afSToby Isaac     numIndices = newNumIndices;
5452d3d1a6afSToby Isaac     points     = newPoints;
5453d3d1a6afSToby Isaac     values     = newValues;
5454d3d1a6afSToby Isaac   }
5455552f7358SJed Brown   ierr = DMGetWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5456552f7358SJed Brown   if (numFields) {
54574acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
54584acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
54594acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, indices);
5460552f7358SJed Brown     }
5461552f7358SJed Brown   } else {
54624acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
54634acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
54644acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
54654acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, indices);
5466552f7358SJed Brown     }
5467552f7358SJed Brown   }
5468b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
5469552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
5470ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
5471ab1d0545SMatthew G. Knepley     PetscInt i;
5472ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
54738ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
5474ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5475ab1d0545SMatthew G. Knepley   }
5476552f7358SJed Brown   if (ierr) {
5477552f7358SJed Brown     PetscMPIInt    rank;
5478552f7358SJed Brown     PetscErrorCode ierr2;
5479552f7358SJed Brown 
548082f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5481e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5482b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
54836a415b8fSMatthew G Knepley     ierr2 = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr2);
5484552f7358SJed Brown     CHKERRQ(ierr);
5485552f7358SJed Brown   }
54864acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
54874acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54884acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54894acb8e1eSToby Isaac   }
5490d3d1a6afSToby Isaac   if (newNumPoints) {
5491d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
5492d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
5493d3d1a6afSToby Isaac   }
5494d3d1a6afSToby Isaac   else {
54954acb8e1eSToby Isaac     if (valCopy) {
54964acb8e1eSToby Isaac       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
54974acb8e1eSToby Isaac     }
5498923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5499d3d1a6afSToby Isaac   }
5500552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5501552f7358SJed Brown   PetscFunctionReturn(0);
5502552f7358SJed Brown }
5503552f7358SJed Brown 
5504de41b84cSMatthew 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)
5505de41b84cSMatthew G. Knepley {
5506de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
5507de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
5508de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
5509de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
5510de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
5511de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
55124ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
5513de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
5514de41b84cSMatthew G. Knepley 
5515de41b84cSMatthew G. Knepley   PetscFunctionBegin;
5516de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
5517de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
5518de41b84cSMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
5519de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
5520de41b84cSMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
5521de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
5522de41b84cSMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
5523de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
5524de41b84cSMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
5525de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
5526de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
5527de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
5528de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5529de41b84cSMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5530de41b84cSMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5531de41b84cSMatthew G. Knepley   /* Column indices */
5532de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
55334ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
5534de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
5535de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
5536de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
5537de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
5538de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
5539de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
5540de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
5541de41b84cSMatthew G. Knepley       ++q;
5542de41b84cSMatthew G. Knepley     }
5543de41b84cSMatthew G. Knepley   }
5544de41b84cSMatthew G. Knepley   numCPoints = q;
5545de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
5546de41b84cSMatthew G. Knepley     PetscInt fdof;
5547de41b84cSMatthew G. Knepley 
5548de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
55494ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5550de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5551de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
5552de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
5553de41b84cSMatthew G. Knepley     }
5554de41b84cSMatthew G. Knepley     numCIndices += dof;
5555de41b84cSMatthew G. Knepley   }
5556de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
5557de41b84cSMatthew G. Knepley   /* Row indices */
5558de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
5559de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
55601ba5f902SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
5561de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
5562de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
5563de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5564de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
5565de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
5566de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
5567de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
55684ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
55694ca5e9f5SMatthew G. Knepley         if (!dof) continue;
55704ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
55714ca5e9f5SMatthew G. Knepley         if (s < q) continue;
5572de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
5573de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
5574de41b84cSMatthew G. Knepley         ++q;
5575de41b84cSMatthew G. Knepley       }
5576de41b84cSMatthew G. Knepley     }
5577de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5578de41b84cSMatthew G. Knepley   }
5579de41b84cSMatthew G. Knepley   numFPoints = q;
5580de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
5581de41b84cSMatthew G. Knepley     PetscInt fdof;
5582de41b84cSMatthew G. Knepley 
5583de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
55844ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5585de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5586de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
5587de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
5588de41b84cSMatthew G. Knepley     }
5589de41b84cSMatthew G. Knepley     numFIndices += dof;
5590de41b84cSMatthew G. Knepley   }
5591de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
5592de41b84cSMatthew G. Knepley 
55938ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
55948ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
5595de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
5596de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
5597de41b84cSMatthew G. Knepley   if (numFields) {
55984acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
55994acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
56004acb8e1eSToby Isaac 
56014acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
56024acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
56034acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5604de41b84cSMatthew G. Knepley     }
56054acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
56064acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
56074acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
56084acb8e1eSToby Isaac     }
56094acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
56104acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
56114acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
56124acb8e1eSToby Isaac     }
56134acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
56144acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
56154acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5616de41b84cSMatthew G. Knepley     }
5617de41b84cSMatthew G. Knepley   } else {
56184acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
56194acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
56204acb8e1eSToby Isaac 
56214acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
56224acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
56234acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
56244acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
56254acb8e1eSToby Isaac 
56264acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
56274acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);CHKERRQ(ierr);
5628de41b84cSMatthew G. Knepley     }
56294acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
56304acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
56314acb8e1eSToby Isaac 
56324acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
56334acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);CHKERRQ(ierr);
5634de41b84cSMatthew G. Knepley     }
56354acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
56364acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
5637de41b84cSMatthew G. Knepley   }
5638de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
56394acb8e1eSToby Isaac   /* TODO: flips */
5640de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
5641de41b84cSMatthew G. Knepley   if (ierr) {
5642de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
5643de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
5644de41b84cSMatthew G. Knepley 
5645de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5646e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5647de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
5648de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr2);
5649de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr2);
5650de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
5651de41b84cSMatthew G. Knepley   }
5652549a8adaSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
5653de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
5654de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
5655de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
5656de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
5657de41b84cSMatthew G. Knepley }
5658de41b84cSMatthew G. Knepley 
56597c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
56607c927364SMatthew G. Knepley {
56617c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
56627c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
56637c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
56647c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
56657c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
56667c927364SMatthew G. Knepley   PetscErrorCode ierr;
56677c927364SMatthew G. Knepley 
56687c927364SMatthew G. Knepley   PetscFunctionBegin;
56697c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
56707c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
56717c927364SMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
56727c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
56737c927364SMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
56747c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
56757c927364SMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
56767c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
56777c927364SMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
56787c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
56797c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
56807c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
56817c927364SMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
56827c927364SMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
56837c927364SMatthew G. Knepley   /* Column indices */
56847c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
56857c927364SMatthew G. Knepley   maxFPoints = numCPoints;
56867c927364SMatthew G. Knepley   /* Compress out points not in the section */
56877c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
56887c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
56897c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
56907c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
56917c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
56927c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
56937c927364SMatthew G. Knepley       ++q;
56947c927364SMatthew G. Knepley     }
56957c927364SMatthew G. Knepley   }
56967c927364SMatthew G. Knepley   numCPoints = q;
56977c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
56987c927364SMatthew G. Knepley     PetscInt fdof;
56997c927364SMatthew G. Knepley 
57007c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
57017c927364SMatthew G. Knepley     if (!dof) continue;
57027c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
57037c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
57047c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
57057c927364SMatthew G. Knepley     }
57067c927364SMatthew G. Knepley     numCIndices += dof;
57077c927364SMatthew G. Knepley   }
57087c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
57097c927364SMatthew G. Knepley   /* Row indices */
57107c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
57117c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
57127c927364SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
57137c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
57147c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
57157c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
57167c927364SMatthew G. Knepley     /* Compress out points not in the section */
57177c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
57187c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
57197c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
57207c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
57217c927364SMatthew G. Knepley         if (!dof) continue;
57227c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
57237c927364SMatthew G. Knepley         if (s < q) continue;
57247c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
57257c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
57267c927364SMatthew G. Knepley         ++q;
57277c927364SMatthew G. Knepley       }
57287c927364SMatthew G. Knepley     }
57297c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
57307c927364SMatthew G. Knepley   }
57317c927364SMatthew G. Knepley   numFPoints = q;
57327c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
57337c927364SMatthew G. Knepley     PetscInt fdof;
57347c927364SMatthew G. Knepley 
57357c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
57367c927364SMatthew G. Knepley     if (!dof) continue;
57377c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
57387c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
57397c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
57407c927364SMatthew G. Knepley     }
57417c927364SMatthew G. Knepley     numFIndices += dof;
57427c927364SMatthew G. Knepley   }
57437c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
57447c927364SMatthew G. Knepley 
57458ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
57468ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
57477c927364SMatthew G. Knepley   if (numFields) {
57484acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
57494acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
57504acb8e1eSToby Isaac 
57514acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
57524acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
57534acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
57547c927364SMatthew G. Knepley     }
57554acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
57564acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
57574acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
57584acb8e1eSToby Isaac     }
57594acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
57604acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
57614acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
57624acb8e1eSToby Isaac     }
57634acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
57644acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
57654acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
57667c927364SMatthew G. Knepley     }
57677c927364SMatthew G. Knepley   } else {
57684acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
57694acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
57704acb8e1eSToby Isaac 
57714acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
57724acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
57734acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
57744acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
57754acb8e1eSToby Isaac 
57764acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
57774acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);
57787c927364SMatthew G. Knepley     }
57794acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
57804acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
57814acb8e1eSToby Isaac 
57824acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
57834acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);
57847c927364SMatthew G. Knepley     }
57854acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
57864acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
57877c927364SMatthew G. Knepley   }
57887c927364SMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
57897c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
57907c927364SMatthew G. Knepley   PetscFunctionReturn(0);
57917c927364SMatthew G. Knepley }
57927c927364SMatthew G. Knepley 
5793f96281f8SMatthew G. Knepley /*@
5794f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
5795f96281f8SMatthew G. Knepley 
5796f96281f8SMatthew G. Knepley   Input Parameter:
5797f96281f8SMatthew G. Knepley . dm - The DMPlex object
5798f96281f8SMatthew G. Knepley 
5799f96281f8SMatthew G. Knepley   Output Parameters:
5800f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
5801dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5802dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5803dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5804f96281f8SMatthew G. Knepley 
5805f96281f8SMatthew G. Knepley   Level: developer
5806f96281f8SMatthew G. Knepley 
5807dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
5808f96281f8SMatthew G. Knepley @*/
5809770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
5810552f7358SJed Brown {
5811552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5812770b213bSMatthew G Knepley   PetscInt       dim;
5813770b213bSMatthew G Knepley   PetscErrorCode ierr;
5814552f7358SJed Brown 
5815552f7358SJed Brown   PetscFunctionBegin;
5816552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5817c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5818770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
5819770b213bSMatthew G Knepley   if (fMax) *fMax = mesh->hybridPointMax[dim-1];
5820770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
5821770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
5822552f7358SJed Brown   PetscFunctionReturn(0);
5823552f7358SJed Brown }
5824552f7358SJed Brown 
5825dc5a3409SMatthew G. Knepley /*@
5826dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
5827dc5a3409SMatthew G. Knepley 
5828dc5a3409SMatthew G. Knepley   Input Parameters:
5829dc5a3409SMatthew G. Knepley . dm   - The DMPlex object
5830dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
5831dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5832dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5833dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5834dc5a3409SMatthew G. Knepley 
5835dc5a3409SMatthew G. Knepley   Level: developer
5836dc5a3409SMatthew G. Knepley 
5837dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
5838dc5a3409SMatthew G. Knepley @*/
5839770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
5840552f7358SJed Brown {
5841552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5842770b213bSMatthew G Knepley   PetscInt       dim;
5843770b213bSMatthew G Knepley   PetscErrorCode ierr;
5844552f7358SJed Brown 
5845552f7358SJed Brown   PetscFunctionBegin;
5846552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5847c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5848770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]   = cMax;
5849770b213bSMatthew G Knepley   if (fMax >= 0) mesh->hybridPointMax[dim-1] = fMax;
5850770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]     = eMax;
5851770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]     = vMax;
5852552f7358SJed Brown   PetscFunctionReturn(0);
5853552f7358SJed Brown }
5854552f7358SJed Brown 
58557cd05799SMatthew G. Knepley /*@C
58567cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
58577cd05799SMatthew G. Knepley 
58587cd05799SMatthew G. Knepley   Input Parameter:
58597cd05799SMatthew G. Knepley . dm   - The DMPlex object
58607cd05799SMatthew G. Knepley 
58617cd05799SMatthew G. Knepley   Output Parameter:
58627cd05799SMatthew G. Knepley . cellHeight - The height of a cell
58637cd05799SMatthew G. Knepley 
58647cd05799SMatthew G. Knepley   Level: developer
58657cd05799SMatthew G. Knepley 
58667cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
58677cd05799SMatthew G. Knepley @*/
5868552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
5869552f7358SJed Brown {
5870552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5871552f7358SJed Brown 
5872552f7358SJed Brown   PetscFunctionBegin;
5873552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5874552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
5875552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
5876552f7358SJed Brown   PetscFunctionReturn(0);
5877552f7358SJed Brown }
5878552f7358SJed Brown 
58797cd05799SMatthew G. Knepley /*@C
58807cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
58817cd05799SMatthew G. Knepley 
58827cd05799SMatthew G. Knepley   Input Parameters:
58837cd05799SMatthew G. Knepley + dm   - The DMPlex object
58847cd05799SMatthew G. Knepley - cellHeight - The height of a cell
58857cd05799SMatthew G. Knepley 
58867cd05799SMatthew G. Knepley   Level: developer
58877cd05799SMatthew G. Knepley 
58887cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
58897cd05799SMatthew G. Knepley @*/
5890552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
5891552f7358SJed Brown {
5892552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5893552f7358SJed Brown 
5894552f7358SJed Brown   PetscFunctionBegin;
5895552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5896552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
5897552f7358SJed Brown   PetscFunctionReturn(0);
5898552f7358SJed Brown }
5899552f7358SJed Brown 
5900552f7358SJed Brown /* We can easily have a form that takes an IS instead */
5901ef48cebcSMatthew G. Knepley static PetscErrorCode DMPlexCreateNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
5902552f7358SJed Brown {
5903552f7358SJed Brown   PetscSection   section, globalSection;
5904552f7358SJed Brown   PetscInt      *numbers, p;
5905552f7358SJed Brown   PetscErrorCode ierr;
5906552f7358SJed Brown 
5907552f7358SJed Brown   PetscFunctionBegin;
590882f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
5909552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
5910552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5911552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
5912552f7358SJed Brown   }
5913552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
591415b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
5915854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
5916552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5917552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
5918ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
5919ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
5920552f7358SJed Brown   }
592182f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
5922ef48cebcSMatthew G. Knepley   if (globalSize) {
5923ef48cebcSMatthew G. Knepley     PetscLayout layout;
5924ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
5925ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
5926ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
5927ef48cebcSMatthew G. Knepley   }
5928552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
5929552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
5930552f7358SJed Brown   PetscFunctionReturn(0);
5931552f7358SJed Brown }
5932552f7358SJed Brown 
593381ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
5934552f7358SJed Brown {
5935552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
5936552f7358SJed Brown   PetscErrorCode ierr;
5937552f7358SJed Brown 
5938552f7358SJed Brown   PetscFunctionBegin;
5939552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
5940552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
59410298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
594281ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
594381ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
594481ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
5945552f7358SJed Brown }
594681ed3555SMatthew G. Knepley 
59477cd05799SMatthew G. Knepley /*@C
59487cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
59497cd05799SMatthew G. Knepley 
59507cd05799SMatthew G. Knepley   Input Parameter:
59517cd05799SMatthew G. Knepley . dm   - The DMPlex object
59527cd05799SMatthew G. Knepley 
59537cd05799SMatthew G. Knepley   Output Parameter:
59547cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
59557cd05799SMatthew G. Knepley 
59567cd05799SMatthew G. Knepley   Level: developer
59577cd05799SMatthew G. Knepley 
59587cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
59597cd05799SMatthew G. Knepley @*/
596081ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
596181ed3555SMatthew G. Knepley {
596281ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
596381ed3555SMatthew G. Knepley   PetscErrorCode ierr;
596481ed3555SMatthew G. Knepley 
596581ed3555SMatthew G. Knepley   PetscFunctionBegin;
596681ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
596781ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
5968552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
5969552f7358SJed Brown   PetscFunctionReturn(0);
5970552f7358SJed Brown }
5971552f7358SJed Brown 
597281ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
597381ed3555SMatthew G. Knepley {
597481ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
597581ed3555SMatthew G. Knepley   PetscErrorCode ierr;
597681ed3555SMatthew G. Knepley 
597781ed3555SMatthew G. Knepley   PetscFunctionBegin;
597881ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
597981ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
598081ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
598181ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
598281ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
598381ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
598481ed3555SMatthew G. Knepley }
598581ed3555SMatthew G. Knepley 
59867cd05799SMatthew G. Knepley /*@C
59877cd05799SMatthew G. Knepley   DMPlexGetVertexNumbering - Get a global certex numbering for all vertices on this process
59887cd05799SMatthew G. Knepley 
59897cd05799SMatthew G. Knepley   Input Parameter:
59907cd05799SMatthew G. Knepley . dm   - The DMPlex object
59917cd05799SMatthew G. Knepley 
59927cd05799SMatthew G. Knepley   Output Parameter:
59937cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
59947cd05799SMatthew G. Knepley 
59957cd05799SMatthew G. Knepley   Level: developer
59967cd05799SMatthew G. Knepley 
59977cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
59987cd05799SMatthew G. Knepley @*/
5999552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
6000552f7358SJed Brown {
6001552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6002552f7358SJed Brown   PetscErrorCode ierr;
6003552f7358SJed Brown 
6004552f7358SJed Brown   PetscFunctionBegin;
6005552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
600681ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
6007552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
6008552f7358SJed Brown   PetscFunctionReturn(0);
6009552f7358SJed Brown }
6010552f7358SJed Brown 
60117cd05799SMatthew G. Knepley /*@C
60127cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
60137cd05799SMatthew G. Knepley 
60147cd05799SMatthew G. Knepley   Input Parameter:
60157cd05799SMatthew G. Knepley . dm   - The DMPlex object
60167cd05799SMatthew G. Knepley 
60177cd05799SMatthew G. Knepley   Output Parameter:
60187cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
60197cd05799SMatthew G. Knepley 
60207cd05799SMatthew G. Knepley   Level: developer
60217cd05799SMatthew G. Knepley 
60227cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
60237cd05799SMatthew G. Knepley @*/
6024ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
6025ef48cebcSMatthew G. Knepley {
6026ef48cebcSMatthew G. Knepley   IS             nums[4];
6027ef48cebcSMatthew G. Knepley   PetscInt       depths[4];
6028ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
6029ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
6030ef48cebcSMatthew G. Knepley 
6031ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
6032ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6033ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
60348abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
60358abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
6036ef48cebcSMatthew G. Knepley   depths[0] = depth; depths[1] = 0;
6037ef48cebcSMatthew G. Knepley   for (d = 2; d <= depth; ++d) depths[d] = depth-d+1;
6038ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
6039ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
6040ef48cebcSMatthew G. Knepley 
6041ef48cebcSMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, depths[d], &pStart, &pEnd);CHKERRQ(ierr);
6042ef48cebcSMatthew G. Knepley     ierr = DMPlexCreateNumbering_Private(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
6043ef48cebcSMatthew G. Knepley     shift += gsize;
6044ef48cebcSMatthew G. Knepley   }
6045302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
6046ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
6047ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
6048ef48cebcSMatthew G. Knepley }
6049ef48cebcSMatthew G. Knepley 
6050ca8062c8SMatthew G. Knepley /*@
6051ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
6052ca8062c8SMatthew G. Knepley 
6053ca8062c8SMatthew G. Knepley   Input Parameters:
6054ca8062c8SMatthew G. Knepley   + dm - The DMPlex object
6055ca8062c8SMatthew G. Knepley 
6056ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6057ca8062c8SMatthew G. Knepley 
6058ca8062c8SMatthew G. Knepley   Level: developer
6059ca8062c8SMatthew G. Knepley 
60609bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSkeleton(), DMCheckFaces()
6061ca8062c8SMatthew G. Knepley @*/
6062ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
6063ca8062c8SMatthew G. Knepley {
6064ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
6065ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
6066ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
6067ca8062c8SMatthew G. Knepley   PetscInt        pStart, pEnd, p, csize, ssize;
6068ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
6069ca8062c8SMatthew G. Knepley 
6070ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6071ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6072ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
6073ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
6074ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
6075ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
6076ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
6077ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
6078ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
6079ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
608042e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
608142e66dfaSMatthew G. Knepley       PetscInt  d;
608242e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
608342e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
608442e66dfaSMatthew G. Knepley       }
6085ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
6086ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
6087ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
6088ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
6089ca8062c8SMatthew G. Knepley       }
609042e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
60918ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
6092ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
60938ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
6094ca8062c8SMatthew G. Knepley         }
6095302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
60968ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
6097ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
60988ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
6099ca8062c8SMatthew G. Knepley         }
6100302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
61018ccfff9cSToby Isaac         if (dup) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not repeatedly found in support of repeated cone point %D", p, cone[c]);
61028ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
6103ca8062c8SMatthew G. Knepley       }
610442e66dfaSMatthew G. Knepley     }
6105ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
6106ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
6107ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
6108ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
6109ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
6110ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
6111ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
6112ca8062c8SMatthew G. Knepley       }
6113ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
61148ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
6115ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
61168ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
6117ca8062c8SMatthew G. Knepley         }
6118302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
61198ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
6120ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
61218ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
6122ca8062c8SMatthew G. Knepley         }
6123302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
61248ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
6125ca8062c8SMatthew G. Knepley       }
6126ca8062c8SMatthew G. Knepley     }
6127ca8062c8SMatthew G. Knepley   }
6128ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
6129ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
61308ccfff9cSToby Isaac   if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
6131ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6132ca8062c8SMatthew G. Knepley }
6133ca8062c8SMatthew G. Knepley 
6134ca8062c8SMatthew G. Knepley /*@
6135ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
6136ca8062c8SMatthew G. Knepley 
6137ca8062c8SMatthew G. Knepley   Input Parameters:
6138ca8062c8SMatthew G. Knepley + dm - The DMPlex object
613958723a97SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
614058723a97SMatthew G. Knepley - cellHeight - Normally 0
6141ca8062c8SMatthew G. Knepley 
6142ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6143ca8062c8SMatthew G. Knepley 
6144ca8062c8SMatthew G. Knepley   Level: developer
6145ca8062c8SMatthew G. Knepley 
61469bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckFaces()
6147ca8062c8SMatthew G. Knepley @*/
614858723a97SMatthew G. Knepley PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscBool isSimplex, PetscInt cellHeight)
6149ca8062c8SMatthew G. Knepley {
615042363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
6151ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
6152ca8062c8SMatthew G. Knepley 
6153ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6154ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6155c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6156ca8062c8SMatthew G. Knepley   switch (dim) {
615742363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
615842363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
615942363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
6160ca8062c8SMatthew G. Knepley   default:
61618ccfff9cSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
6162ca8062c8SMatthew G. Knepley   }
616358723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
616458723a97SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
6165ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
6166ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
6167ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
616858723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
616958723a97SMatthew G. Knepley 
617058723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
617158723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
617258723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
617358723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
617458723a97SMatthew G. Knepley     }
617558723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
61768ccfff9cSToby Isaac     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
6177ca8062c8SMatthew G. Knepley   }
617842363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
617942363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
618042363296SMatthew G. Knepley 
618142363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
618242363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
618342363296SMatthew G. Knepley       const PetscInt p = closure[cl];
618442363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
618542363296SMatthew G. Knepley     }
618642363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
61878ccfff9cSToby Isaac     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
618842363296SMatthew G. Knepley   }
6189ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6190ca8062c8SMatthew G. Knepley }
61919bf0dad6SMatthew G. Knepley 
61929bf0dad6SMatthew G. Knepley /*@
61939bf0dad6SMatthew 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
61949bf0dad6SMatthew G. Knepley 
61959bf0dad6SMatthew G. Knepley   Input Parameters:
61969bf0dad6SMatthew G. Knepley + dm - The DMPlex object
61979bf0dad6SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
61989bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
61999bf0dad6SMatthew G. Knepley 
62009bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
62019bf0dad6SMatthew G. Knepley 
62029bf0dad6SMatthew G. Knepley   Level: developer
62039bf0dad6SMatthew G. Knepley 
62049bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckSkeleton()
62059bf0dad6SMatthew G. Knepley @*/
62069bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexCheckFaces(DM dm, PetscBool isSimplex, PetscInt cellHeight)
62079bf0dad6SMatthew G. Knepley {
62083554e41dSMatthew G. Knepley   PetscInt       pMax[4];
62093554e41dSMatthew G. Knepley   PetscInt       dim, vStart, vEnd, cStart, cEnd, c, h;
62109bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
62119bf0dad6SMatthew G. Knepley 
62129bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
62139bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6214c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
62159bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
621625abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
62173554e41dSMatthew G. Knepley   for (h = cellHeight; h < dim; ++h) {
62183554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
62193554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
62209bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
62219bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
62229bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
62239bf0dad6SMatthew G. Knepley 
622425abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
62259bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
62269bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
62279bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
62289bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
62299bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
62309bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
62319bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
62329bf0dad6SMatthew G. Knepley       }
62333554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
62348ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
62359bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
62369bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
62379bf0dad6SMatthew G. Knepley 
62389bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
62399bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
62409bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
62419bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
62429bf0dad6SMatthew G. Knepley         }
62438ccfff9cSToby Isaac         if (fnumCorners != faceSize) SETERRQ5(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D (%D) of cell %D has %D vertices but should have %D", cone[f], f, c, fnumCorners, faceSize);
62449bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
62458ccfff9cSToby Isaac           if (fclosure[v] != faces[f*faceSize+v]) SETERRQ6(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Face %D (%d) of cell %D vertex %D, %D != %D", cone[f], f, c, v, fclosure[v], faces[f*faceSize+v]);
62469bf0dad6SMatthew G. Knepley         }
62479bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
62489bf0dad6SMatthew G. Knepley       }
62499bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
62509bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
62519bf0dad6SMatthew G. Knepley     }
62523554e41dSMatthew G. Knepley   }
6253552f7358SJed Brown   PetscFunctionReturn(0);
6254552f7358SJed Brown }
62553913d7c8SMatthew G. Knepley 
6256bceba477SMatthew G. Knepley /* Pointwise interpolation
6257bceba477SMatthew G. Knepley      Just code FEM for now
6258bceba477SMatthew G. Knepley      u^f = I u^c
62594ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
62604ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
62614ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
6262bceba477SMatthew G. Knepley */
6263bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
6264bceba477SMatthew G. Knepley {
6265bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
6266bceba477SMatthew G. Knepley   PetscInt       m, n;
6267a063dac3SMatthew G. Knepley   void          *ctx;
626868132eb9SMatthew G. Knepley   DM             cdm;
626968132eb9SMatthew G. Knepley   PetscBool      regular;
6270bceba477SMatthew G. Knepley   PetscErrorCode ierr;
6271bceba477SMatthew G. Knepley 
6272bceba477SMatthew G. Knepley   PetscFunctionBegin;
6273bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6274bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6275bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6276bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
627768132eb9SMatthew G. Knepley 
6278bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
6279bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6280bceba477SMatthew G. Knepley   ierr = MatSetType(*interpolation, dmCoarse->mattype);CHKERRQ(ierr);
6281a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
628268132eb9SMatthew G. Knepley 
6283a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
628468132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
628568132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
628668132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
628768132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
62885d1c2e58SMatthew G. Knepley   /* Use naive scaling */
62895d1c2e58SMatthew G. Knepley   ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
6290a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
6291a063dac3SMatthew G. Knepley }
6292bceba477SMatthew G. Knepley 
62936dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
6294a063dac3SMatthew G. Knepley {
629590748bafSMatthew G. Knepley   PetscErrorCode ierr;
62966dbf9973SLawrence Mitchell   VecScatter     ctx;
629790748bafSMatthew G. Knepley 
6298a063dac3SMatthew G. Knepley   PetscFunctionBegin;
62996dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
63006dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
63016dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
6302bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6303bceba477SMatthew G. Knepley }
6304bceba477SMatthew G. Knepley 
6305fd59a867SMatthew G. Knepley PetscErrorCode DMCreateDefaultSection_Plex(DM dm)
6306fd59a867SMatthew G. Knepley {
6307fd59a867SMatthew G. Knepley   PetscSection   section;
6308ab1d0545SMatthew G. Knepley   IS            *bcPoints, *bcComps;
6309ebb3236fSMatthew G. Knepley   PetscBool     *isFE;
6310286d8613SMatthew G. Knepley   PetscInt      *bcFields, *numComp, *numDof;
6311ebb3236fSMatthew G. Knepley   PetscInt       depth, dim, numBd, numBC = 0, numFields, bd, bc = 0, f;
6312ebb3236fSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
6313fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
6314fd59a867SMatthew G. Knepley 
6315fd59a867SMatthew G. Knepley   PetscFunctionBegin;
6316ebb3236fSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
6317ae71db08SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
6318ebb3236fSMatthew G. Knepley   /* FE and FV boundary conditions are handled slightly differently */
6319ebb3236fSMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
6320ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6321ebb3236fSMatthew G. Knepley     PetscObject  obj;
6322ebb3236fSMatthew G. Knepley     PetscClassId id;
6323ebb3236fSMatthew G. Knepley 
6324ebb3236fSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6325ebb3236fSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
6326ebb3236fSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
6327ebb3236fSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
63288ccfff9cSToby Isaac     else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
6329ebb3236fSMatthew G. Knepley   }
63302f900235SMatthew G. Knepley   /* Allocate boundary point storage for FEM boundaries */
6331286d8613SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
6332c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6333ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
6334ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
6335dff059c6SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob, &numBd);CHKERRQ(ierr);
6336fd59a867SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
63372f900235SMatthew G. Knepley     PetscInt                field;
6338f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6339385532a5SToby Isaac     const char             *labelName;
6340385532a5SToby Isaac     DMLabel                 label;
63412f900235SMatthew G. Knepley 
6342f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &labelName, &field, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
6343385532a5SToby Isaac     ierr = DMGetLabel(dm,labelName,&label);CHKERRQ(ierr);
6344f971fd6bSMatthew G. Knepley     if (label && isFE[field] && (type & DM_BC_ESSENTIAL)) ++numBC;
6345fd59a867SMatthew G. Knepley   }
63462f900235SMatthew G. Knepley   /* Add ghost cell boundaries for FVM */
6347ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) if (!isFE[f] && cEndInterior >= 0) ++numBC;
63486c8d74c4SMatthew G. Knepley   ierr = PetscCalloc3(numBC,&bcFields,numBC,&bcPoints,numBC,&bcComps);CHKERRQ(ierr);
6349ebb3236fSMatthew G. Knepley   /* Constrain ghost cells for FV */
6350ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6351ebb3236fSMatthew G. Knepley     PetscInt *newidx, c;
6352ebb3236fSMatthew G. Knepley 
6353ebb3236fSMatthew G. Knepley     if (isFE[f] || cEndInterior < 0) continue;
6354ebb3236fSMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cEndInterior,&newidx);CHKERRQ(ierr);
6355ebb3236fSMatthew G. Knepley     for (c = cEndInterior; c < cEnd; ++c) newidx[c-cEndInterior] = c;
6356ebb3236fSMatthew G. Knepley     bcFields[bc] = f;
6357ebb3236fSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), cEnd-cEndInterior, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6358ebb3236fSMatthew G. Knepley   }
6359ebb3236fSMatthew G. Knepley   /* Handle FEM Dirichlet boundaries */
6360ebb3236fSMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
6361fd59a867SMatthew G. Knepley     const char             *bdLabel;
6362fd59a867SMatthew G. Knepley     DMLabel                 label;
63630e0f25f2SMatthew G. Knepley     const PetscInt         *comps;
6364fd59a867SMatthew G. Knepley     const PetscInt         *values;
63650e0f25f2SMatthew G. Knepley     PetscInt                bd2, field, numComps, numValues;
6366f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6367f971fd6bSMatthew G. Knepley     PetscBool               duplicate = PETSC_FALSE;
6368fd59a867SMatthew G. Knepley 
6369f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &bdLabel, &field, &numComps, &comps, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
6370c58f1c22SToby Isaac     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
6371385532a5SToby Isaac     if (!isFE[field] || !label) continue;
6372ebb3236fSMatthew G. Knepley     /* Only want to modify label once */
63737391c1cbSMatthew G. Knepley     for (bd2 = 0; bd2 < bd; ++bd2) {
63747391c1cbSMatthew G. Knepley       const char *bdname;
6375dff059c6SToby Isaac       ierr = PetscDSGetBoundary(dm->prob, bd2, NULL, NULL, &bdname, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
63767391c1cbSMatthew G. Knepley       ierr = PetscStrcmp(bdname, bdLabel, &duplicate);CHKERRQ(ierr);
63777391c1cbSMatthew G. Knepley       if (duplicate) break;
63787391c1cbSMatthew G. Knepley     }
6379ebb3236fSMatthew G. Knepley     if (!duplicate && (isFE[field])) {
6380b0bf5782SToby Isaac       /* don't complete cells, which are just present to give orientation to the boundary */
6381092e5057SToby Isaac       ierr = DMPlexLabelComplete(dm, label);CHKERRQ(ierr);
63827391c1cbSMatthew G. Knepley     }
6383ebb3236fSMatthew G. Knepley     /* Filter out cells, if you actually want to constrain cells you need to do things by hand right now */
6384f971fd6bSMatthew G. Knepley     if (type & DM_BC_ESSENTIAL) {
638593a5981aSMatthew G. Knepley       PetscInt       *newidx;
6386ebb3236fSMatthew G. Knepley       PetscInt        n, newn = 0, p, v;
638793a5981aSMatthew G. Knepley 
6388fd59a867SMatthew G. Knepley       bcFields[bc] = field;
63890e0f25f2SMatthew G. Knepley       if (numComps) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), numComps, comps, PETSC_COPY_VALUES, &bcComps[bc]);CHKERRQ(ierr);}
6390ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6391ebb3236fSMatthew G. Knepley         IS              tmp;
6392ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6393ebb3236fSMatthew G. Knepley 
6394c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6395ebb3236fSMatthew G. Knepley         if (!tmp) continue;
639693a5981aSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
639793a5981aSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6398ebb3236fSMatthew G. Knepley         if (isFE[field]) {
639993a5981aSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) ++newn;
6400ebb3236fSMatthew G. Knepley         } else {
6401ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) ++newn;
6402ebb3236fSMatthew G. Knepley         }
640393a5981aSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
640493a5981aSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6405fd59a867SMatthew G. Knepley       }
6406ebb3236fSMatthew G. Knepley       ierr = PetscMalloc1(newn,&newidx);CHKERRQ(ierr);
6407ebb3236fSMatthew G. Knepley       newn = 0;
6408ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6409ebb3236fSMatthew G. Knepley         IS              tmp;
6410ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6411ebb3236fSMatthew G. Knepley 
6412c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6413ebb3236fSMatthew G. Knepley         if (!tmp) continue;
6414ebb3236fSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
6415ebb3236fSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6416ebb3236fSMatthew G. Knepley         if (isFE[field]) {
6417ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) newidx[newn++] = idx[p];
6418ebb3236fSMatthew G. Knepley         } else {
6419ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) newidx[newn++] = idx[p];
6420ebb3236fSMatthew G. Knepley         }
6421ebb3236fSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
6422ebb3236fSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6423ebb3236fSMatthew G. Knepley       }
6424ebb3236fSMatthew G. Knepley       ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), newn, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6425ebb3236fSMatthew G. Knepley     }
6426fd59a867SMatthew G. Knepley   }
6427fd59a867SMatthew G. Knepley   /* Handle discretization */
6428ebb3236fSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&numComp,numFields*(dim+1),&numDof);CHKERRQ(ierr);
6429fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
64300bacfa0aSMatthew G. Knepley     PetscObject obj;
64310bacfa0aSMatthew G. Knepley 
64320bacfa0aSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6433ebb3236fSMatthew G. Knepley     if (isFE[f]) {
64340bacfa0aSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
6435286d8613SMatthew G. Knepley       const PetscInt *numFieldDof;
6436fd59a867SMatthew G. Knepley       PetscInt        d;
6437fd59a867SMatthew G. Knepley 
6438fd59a867SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
6439286d8613SMatthew G. Knepley       ierr = PetscFEGetNumDof(fe, &numFieldDof);CHKERRQ(ierr);
6440286d8613SMatthew G. Knepley       for (d = 0; d < dim+1; ++d) numDof[f*(dim+1)+d] = numFieldDof[d];
6441ebb3236fSMatthew G. Knepley     } else {
64420bacfa0aSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
64430bacfa0aSMatthew G. Knepley 
64440bacfa0aSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
64450bacfa0aSMatthew G. Knepley       numDof[f*(dim+1)+dim] = numComp[f];
6446ebb3236fSMatthew G. Knepley     }
6447fd59a867SMatthew G. Knepley   }
6448286d8613SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6449286d8613SMatthew G. Knepley     PetscInt d;
6450286d8613SMatthew G. Knepley     for (d = 1; d < dim; ++d) {
6451286d8613SMatthew 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.");
6452286d8613SMatthew G. Knepley     }
6453286d8613SMatthew G. Knepley   }
6454ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSection(dm, dim, numFields, numComp, numDof, numBC, bcFields, bcComps, bcPoints, NULL, &section);CHKERRQ(ierr);
6455fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6456fd59a867SMatthew G. Knepley     PetscFE     fe;
6457fd59a867SMatthew G. Knepley     const char *name;
645818abd763SMatthew G. Knepley 
645918abd763SMatthew G. Knepley     ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
6460fd59a867SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
6461fd59a867SMatthew G. Knepley     ierr = PetscSectionSetFieldName(section, f, name);CHKERRQ(ierr);
6462fd59a867SMatthew G. Knepley   }
6463fd59a867SMatthew G. Knepley   ierr = DMSetDefaultSection(dm, section);CHKERRQ(ierr);
6464fd59a867SMatthew G. Knepley   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
64650e0f25f2SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {ierr = ISDestroy(&bcPoints[bc]);CHKERRQ(ierr);ierr = ISDestroy(&bcComps[bc]);CHKERRQ(ierr);}
64660e0f25f2SMatthew G. Knepley   ierr = PetscFree3(bcFields,bcPoints,bcComps);CHKERRQ(ierr);
6467286d8613SMatthew G. Knepley   ierr = PetscFree2(numComp,numDof);CHKERRQ(ierr);
6468ebb3236fSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
6469bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6470bceba477SMatthew G. Knepley }
6471bceba477SMatthew G. Knepley 
64720aef6b92SMatthew G. Knepley /*@
64730aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
64740aef6b92SMatthew G. Knepley 
64750aef6b92SMatthew G. Knepley   Input Parameter:
64760aef6b92SMatthew G. Knepley . dm - The DMPlex object
64770aef6b92SMatthew G. Knepley 
64780aef6b92SMatthew G. Knepley   Output Parameter:
64790aef6b92SMatthew G. Knepley . regular - The flag
64800aef6b92SMatthew G. Knepley 
64810aef6b92SMatthew G. Knepley   Level: intermediate
64820aef6b92SMatthew G. Knepley 
64830aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
64840aef6b92SMatthew G. Knepley @*/
64850aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
64860aef6b92SMatthew G. Knepley {
64870aef6b92SMatthew G. Knepley   PetscFunctionBegin;
64880aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64890aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
64900aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
64910aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
64920aef6b92SMatthew G. Knepley }
64930aef6b92SMatthew G. Knepley 
64940aef6b92SMatthew G. Knepley /*@
64950aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
64960aef6b92SMatthew G. Knepley 
64970aef6b92SMatthew G. Knepley   Input Parameters:
64980aef6b92SMatthew G. Knepley + dm - The DMPlex object
64990aef6b92SMatthew G. Knepley - regular - The flag
65000aef6b92SMatthew G. Knepley 
65010aef6b92SMatthew G. Knepley   Level: intermediate
65020aef6b92SMatthew G. Knepley 
65030aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
65040aef6b92SMatthew G. Knepley @*/
65050aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
65060aef6b92SMatthew G. Knepley {
65070aef6b92SMatthew G. Knepley   PetscFunctionBegin;
65080aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65090aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
65100aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
65110aef6b92SMatthew G. Knepley }
65120aef6b92SMatthew G. Knepley 
6513f7c74593SToby Isaac /* anchors */
6514a68b90caSToby Isaac /*@
6515f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
6516f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
6517a68b90caSToby Isaac 
6518e228b242SToby Isaac   not collective
6519a68b90caSToby Isaac 
6520a68b90caSToby Isaac   Input Parameters:
6521a68b90caSToby Isaac . dm - The DMPlex object
6522a68b90caSToby Isaac 
6523a68b90caSToby Isaac   Output Parameters:
6524a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
6525a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
6526a68b90caSToby Isaac 
6527a68b90caSToby Isaac 
6528a68b90caSToby Isaac   Level: intermediate
6529a68b90caSToby Isaac 
6530f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
6531a68b90caSToby Isaac @*/
6532a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
6533a68b90caSToby Isaac {
6534a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
653541e6d900SToby Isaac   PetscErrorCode ierr;
6536a68b90caSToby Isaac 
6537a68b90caSToby Isaac   PetscFunctionBegin;
6538a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
653941e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
6540a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
6541a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
6542a68b90caSToby Isaac   PetscFunctionReturn(0);
6543a68b90caSToby Isaac }
6544a68b90caSToby Isaac 
6545a68b90caSToby Isaac /*@
6546f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
6547f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
6548a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
6549a68b90caSToby Isaac 
6550a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
6551f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
6552a68b90caSToby Isaac 
6553e228b242SToby Isaac   collective on dm
6554a68b90caSToby Isaac 
6555a68b90caSToby Isaac   Input Parameters:
6556a68b90caSToby Isaac + dm - The DMPlex object
6557e228b242SToby 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).
6558e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
6559a68b90caSToby Isaac 
6560a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
6561a68b90caSToby Isaac 
6562a68b90caSToby Isaac   Level: intermediate
6563a68b90caSToby Isaac 
6564f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
6565a68b90caSToby Isaac @*/
6566a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
6567a68b90caSToby Isaac {
6568a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6569e228b242SToby Isaac   PetscMPIInt    result;
6570a68b90caSToby Isaac   PetscErrorCode ierr;
6571a68b90caSToby Isaac 
6572a68b90caSToby Isaac   PetscFunctionBegin;
6573a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6574e228b242SToby Isaac   if (anchorSection) {
6575e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
6576e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
6577e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
6578e228b242SToby Isaac   }
6579e228b242SToby Isaac   if (anchorIS) {
6580e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
6581e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
6582e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
6583e228b242SToby Isaac   }
6584a68b90caSToby Isaac 
6585a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
6586a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
6587a68b90caSToby Isaac   plex->anchorSection = anchorSection;
6588a68b90caSToby Isaac 
6589a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
6590a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
6591a68b90caSToby Isaac   plex->anchorIS = anchorIS;
6592a68b90caSToby Isaac 
6593a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
6594a68b90caSToby Isaac   if (anchorIS && anchorSection) {
6595a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
6596a68b90caSToby Isaac     const PetscInt *anchors;
6597a68b90caSToby Isaac 
6598a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
6599a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
6600a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
6601a68b90caSToby Isaac     for (a = 0; a < size; a++) {
6602a68b90caSToby Isaac       PetscInt p;
6603a68b90caSToby Isaac 
6604a68b90caSToby Isaac       p = anchors[a];
6605a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
6606a68b90caSToby Isaac         PetscInt dof;
6607a68b90caSToby Isaac 
6608a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6609a68b90caSToby Isaac         if (dof) {
6610a68b90caSToby Isaac           PetscErrorCode ierr2;
6611a68b90caSToby Isaac 
6612a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
66138ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
6614a68b90caSToby Isaac         }
6615a68b90caSToby Isaac       }
6616a68b90caSToby Isaac     }
6617a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
6618a68b90caSToby Isaac   }
6619a68b90caSToby Isaac #endif
6620f7c74593SToby Isaac   /* reset the generic constraints */
6621f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
6622a68b90caSToby Isaac   PetscFunctionReturn(0);
6623a68b90caSToby Isaac }
6624a68b90caSToby Isaac 
6625f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
6626a68b90caSToby Isaac {
6627f7c74593SToby Isaac   PetscSection anchorSection;
66286995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
6629a68b90caSToby Isaac   PetscErrorCode ierr;
6630a68b90caSToby Isaac 
6631a68b90caSToby Isaac   PetscFunctionBegin;
6632a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6633a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
6634e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
6635a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
66366995de1eSToby Isaac   if (numFields) {
6637719ab38cSToby Isaac     PetscInt f;
6638a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
6639719ab38cSToby Isaac 
6640719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
6641719ab38cSToby Isaac       PetscInt numComp;
6642719ab38cSToby Isaac 
6643719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
6644719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
6645719ab38cSToby Isaac     }
66466995de1eSToby Isaac   }
6647a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
66486995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
66496995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
66506995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
66516995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
6652a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
6653a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
6654a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6655a68b90caSToby Isaac     if (dof) {
6656a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
6657a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
6658a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
6659a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
6660a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
6661a68b90caSToby Isaac       }
6662a68b90caSToby Isaac     }
6663a68b90caSToby Isaac   }
6664a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
6665a68b90caSToby Isaac   PetscFunctionReturn(0);
6666a68b90caSToby Isaac }
6667a68b90caSToby Isaac 
6668f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
6669a68b90caSToby Isaac {
6670f7c74593SToby Isaac   PetscSection aSec;
66710ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
66720ac89760SToby Isaac   const PetscInt *anchors;
66730ac89760SToby Isaac   PetscInt numFields, f;
667466ad2231SToby Isaac   IS aIS;
66750ac89760SToby Isaac   PetscErrorCode ierr;
66760ac89760SToby Isaac 
66770ac89760SToby Isaac   PetscFunctionBegin;
66780ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66790ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
66800ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
66810ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
66820ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
6683302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
6684a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
668566ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
66866995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
66876995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
66880ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
66890ac89760SToby Isaac   i[0] = 0;
66900ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
66910ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
6692f19733c5SToby Isaac     PetscInt rDof, rOff, r;
6693f19733c5SToby Isaac 
6694f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
6695f19733c5SToby Isaac     if (!rDof) continue;
6696f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
66970ac89760SToby Isaac     if (numFields) {
66980ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
66990ac89760SToby Isaac         annz = 0;
6700f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
6701f19733c5SToby Isaac           a = anchors[rOff + r];
67020ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
67030ac89760SToby Isaac           annz += aDof;
67040ac89760SToby Isaac         }
67050ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
67060ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
67070ac89760SToby Isaac         for (q = 0; q < dof; q++) {
67080ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
67090ac89760SToby Isaac         }
67100ac89760SToby Isaac       }
67110ac89760SToby Isaac     }
67120ac89760SToby Isaac     else {
67130ac89760SToby Isaac       annz = 0;
67140ac89760SToby Isaac       for (q = 0; q < dof; q++) {
67150ac89760SToby Isaac         a = anchors[off + q];
67160ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
67170ac89760SToby Isaac         annz += aDof;
67180ac89760SToby Isaac       }
67190ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
67200ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
67210ac89760SToby Isaac       for (q = 0; q < dof; q++) {
67220ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
67230ac89760SToby Isaac       }
67240ac89760SToby Isaac     }
67250ac89760SToby Isaac   }
67260ac89760SToby Isaac   nnz = i[m];
67270ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
67280ac89760SToby Isaac   offset = 0;
67290ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
67300ac89760SToby Isaac     if (numFields) {
67310ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
67320ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
67330ac89760SToby Isaac         for (q = 0; q < dof; q++) {
67340ac89760SToby Isaac           PetscInt rDof, rOff, r;
67350ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
67360ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
67370ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
67380ac89760SToby Isaac             PetscInt s;
67390ac89760SToby Isaac 
67400ac89760SToby Isaac             a = anchors[rOff + r];
67410ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
67420ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
67430ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
67440ac89760SToby Isaac               j[offset++] = aOff + s;
67450ac89760SToby Isaac             }
67460ac89760SToby Isaac           }
67470ac89760SToby Isaac         }
67480ac89760SToby Isaac       }
67490ac89760SToby Isaac     }
67500ac89760SToby Isaac     else {
67510ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
67520ac89760SToby Isaac       for (q = 0; q < dof; q++) {
67530ac89760SToby Isaac         PetscInt rDof, rOff, r;
67540ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
67550ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
67560ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
67570ac89760SToby Isaac           PetscInt s;
67580ac89760SToby Isaac 
67590ac89760SToby Isaac           a = anchors[rOff + r];
67600ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
67610ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
67620ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
67630ac89760SToby Isaac             j[offset++] = aOff + s;
67640ac89760SToby Isaac           }
67650ac89760SToby Isaac         }
67660ac89760SToby Isaac       }
67670ac89760SToby Isaac     }
67680ac89760SToby Isaac   }
67690ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
677025570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
677125570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
677266ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
67730ac89760SToby Isaac   PetscFunctionReturn(0);
67740ac89760SToby Isaac }
67750ac89760SToby Isaac 
677666ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
677766ad2231SToby Isaac {
6778f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6779f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
678066ad2231SToby Isaac   Mat            cMat;
678166ad2231SToby Isaac   PetscErrorCode ierr;
678266ad2231SToby Isaac 
678366ad2231SToby Isaac   PetscFunctionBegin;
678466ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6785a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
678666ad2231SToby Isaac   if (anchorSection) {
6787e228b242SToby Isaac     PetscDS  ds;
6788e228b242SToby Isaac     PetscInt nf;
6789e228b242SToby Isaac 
6790f7c74593SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
6791f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
6792f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
6793e228b242SToby Isaac     ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
6794302440fdSBarry Smith     ierr = PetscDSGetNumFields(ds,&nf);CHKERRQ(ierr);
6795e228b242SToby Isaac     if (nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
679666ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
679766ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
679866ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
679966ad2231SToby Isaac   }
680066ad2231SToby Isaac   PetscFunctionReturn(0);
680166ad2231SToby Isaac }
6802