xref: /petsc/src/dm/impls/plex/plex.c (revision f6a3d38cb093b94243bf9c812fced830a028f0d4)
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;
3168ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, 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);
3176ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
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;
3193ab1d0545SMatthew G. Knepley       PetscInt        fcdof, c;
3194ab1d0545SMatthew G. Knepley 
3195ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintDof(section, p, field, &fcdof);CHKERRQ(ierr);
3196ab1d0545SMatthew G. Knepley       if (Nc < 0) {
3197ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) indices[d] = d;
3198552f7358SJed Brown       } else {
3199ab1d0545SMatthew G. Knepley         ierr = PetscSectionGetFieldConstraintIndices(section, p, field, &find);CHKERRQ(ierr);
3200ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) {if (find[d] < 0) break; indices[d] = find[d];}
3201ab1d0545SMatthew G. Knepley         for (c = 0; c < Nc; ++c) indices[d+c] = comp[c];
3202ab1d0545SMatthew G. Knepley         ierr = PetscSortInt(d+Nc, indices);CHKERRQ(ierr);
3203ab1d0545SMatthew G. Knepley         for (c = d+Nc; c < fcdof; ++c) indices[c] = -1;
3204552f7358SJed Brown       }
3205ab1d0545SMatthew G. Knepley       ierr = PetscSectionSetFieldConstraintIndices(section, p, field, indices);CHKERRQ(ierr);
3206552f7358SJed Brown     }
3207ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3208ab1d0545SMatthew G. Knepley     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3209552f7358SJed Brown   }
3210ab1d0545SMatthew G. Knepley   /* Handle anchors */
3211ab1d0545SMatthew G. Knepley   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3212ab1d0545SMatthew G. Knepley   if (aSec) {
3213ab1d0545SMatthew G. Knepley     PetscInt aStart, aEnd, a;
3214552f7358SJed Brown 
3215ab1d0545SMatthew G. Knepley     for (d = 0; d < maxDof; ++d) indices[d] = d;
3216ab1d0545SMatthew G. Knepley     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3217ab1d0545SMatthew G. Knepley     for (a = aStart; a < aEnd; a++) {
3218ab1d0545SMatthew G. Knepley       PetscInt dof, fdof, f;
3219ab1d0545SMatthew G. Knepley 
3220ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3221ab1d0545SMatthew G. Knepley       if (dof) {
3222ab1d0545SMatthew G. Knepley         /* if there are point-to-point constraints, then all dofs are constrained */
3223ab1d0545SMatthew G. Knepley         for (f = 0; f < numFields; f++) {
3224ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, a, f, &fdof);CHKERRQ(ierr);
3225ab1d0545SMatthew G. Knepley           ierr = PetscSectionSetFieldConstraintIndices(section, a, f, indices);CHKERRQ(ierr);
3226ab1d0545SMatthew G. Knepley         }
3227ab1d0545SMatthew G. Knepley       }
3228ab1d0545SMatthew G. Knepley     }
3229ab1d0545SMatthew G. Knepley   }
3230ab1d0545SMatthew G. Knepley   ierr = PetscFree(indices);CHKERRQ(ierr);
3231ab1d0545SMatthew G. Knepley   PetscFunctionReturn(0);
3232ab1d0545SMatthew G. Knepley }
3233ab1d0545SMatthew G. Knepley 
3234ab1d0545SMatthew G. Knepley /* Set the constrained indices on each point */
32357cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCIndices(DM dm, PetscSection section)
3236ab1d0545SMatthew G. Knepley {
3237ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3238ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, f, d;
3239ab1d0545SMatthew G. Knepley   PetscErrorCode ierr;
3240ab1d0545SMatthew G. Knepley 
3241ab1d0545SMatthew G. Knepley   PetscFunctionBegin;
3242ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3243ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3244ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3245ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3246ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3247552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3248552f7358SJed Brown     PetscInt cdof, d;
3249552f7358SJed Brown 
3250552f7358SJed Brown     ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
3251552f7358SJed Brown     if (cdof) {
3252552f7358SJed Brown       if (numFields) {
3253552f7358SJed Brown         PetscInt numConst = 0, foff = 0;
3254552f7358SJed Brown 
3255552f7358SJed Brown         for (f = 0; f < numFields; ++f) {
3256ab1d0545SMatthew G. Knepley           const PetscInt *find;
3257ab1d0545SMatthew G. Knepley           PetscInt        fcdof, fdof;
3258552f7358SJed Brown 
3259552f7358SJed Brown           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
3260ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
3261ab1d0545SMatthew G. Knepley           /* Change constraint numbering from field component to local dof number */
3262ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintIndices(section, p, f, &find);CHKERRQ(ierr);
3263ab1d0545SMatthew G. Knepley           for (d = 0; d < fcdof; ++d) indices[numConst+d] = find[d] + foff;
3264ab1d0545SMatthew G. Knepley           numConst += fcdof;
3265552f7358SJed Brown           foff     += fdof;
3266552f7358SJed Brown         }
3267552f7358SJed Brown         if (cdof != numConst) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Total number of field constraints %D should be %D", numConst, cdof);
3268552f7358SJed Brown       } else {
32690d644c17SKarl Rupp         for (d = 0; d < cdof; ++d) indices[d] = d;
3270552f7358SJed Brown       }
3271552f7358SJed Brown       ierr = PetscSectionSetConstraintIndices(section, p, indices);CHKERRQ(ierr);
3272552f7358SJed Brown     }
3273552f7358SJed Brown   }
3274552f7358SJed Brown   ierr = PetscFree(indices);CHKERRQ(ierr);
3275552f7358SJed Brown   PetscFunctionReturn(0);
3276552f7358SJed Brown }
3277552f7358SJed Brown 
3278552f7358SJed Brown /*@C
3279552f7358SJed Brown   DMPlexCreateSection - Create a PetscSection based upon the dof layout specification provided.
3280552f7358SJed Brown 
3281552f7358SJed Brown   Not Collective
3282552f7358SJed Brown 
3283552f7358SJed Brown   Input Parameters:
3284552f7358SJed Brown + dm        - The DMPlex object
3285552f7358SJed Brown . dim       - The spatial dimension of the problem
3286552f7358SJed Brown . numFields - The number of fields in the problem
3287552f7358SJed Brown . numComp   - An array of size numFields that holds the number of components for each field
3288552f7358SJed 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
3289552f7358SJed Brown . numBC     - The number of boundary conditions
3290552f7358SJed Brown . bcField   - An array of size numBC giving the field number for each boundry condition
3291ab1d0545SMatthew G. Knepley . bcComps   - [Optional] An array of size numBC giving an IS holding the field components to which each boundary condition applies
3292ab1d0545SMatthew G. Knepley . bcPoints  - An array of size numBC giving an IS holding the Plex points to which each boundary condition applies
3293f8f126e8SMatthew G. Knepley - perm      - Optional permutation of the chart, or NULL
3294552f7358SJed Brown 
3295552f7358SJed Brown   Output Parameter:
3296552f7358SJed Brown . section - The PetscSection object
3297552f7358SJed Brown 
3298552f7358SJed 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
3299f8f126e8SMatthew G. Knepley   number of dof for field 0 on each edge.
3300f8f126e8SMatthew G. Knepley 
3301f8f126e8SMatthew G. Knepley   The chart permutation is the same one set using PetscSectionSetPermutation()
3302552f7358SJed Brown 
3303552f7358SJed Brown   Level: developer
3304552f7358SJed Brown 
33053813dfbdSMatthew G Knepley   Fortran Notes:
33063813dfbdSMatthew G Knepley   A Fortran 90 version is available as DMPlexCreateSectionF90()
33073813dfbdSMatthew G Knepley 
3308552f7358SJed Brown .keywords: mesh, elements
3309f8f126e8SMatthew G. Knepley .seealso: DMPlexCreate(), PetscSectionCreate(), PetscSectionSetPermutation()
3310552f7358SJed Brown @*/
3311ab1d0545SMatthew 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)
3312a6dfd86eSKarl Rupp {
3313a68b90caSToby Isaac   PetscSection   aSec;
3314552f7358SJed Brown   PetscErrorCode ierr;
3315552f7358SJed Brown 
3316552f7358SJed Brown   PetscFunctionBegin;
3317552f7358SJed Brown   ierr = DMPlexCreateSectionInitial(dm, dim, numFields, numComp, numDof, section);CHKERRQ(ierr);
3318ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSectionBCDof(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3319f8f126e8SMatthew G. Knepley   if (perm) {ierr = PetscSectionSetPermutation(*section, perm);CHKERRQ(ierr);}
3320552f7358SJed Brown   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
3321a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,NULL);CHKERRQ(ierr);
3322ab1d0545SMatthew G. Knepley   if (numBC || aSec) {
3323ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndicesField(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3324ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndices(dm, *section);CHKERRQ(ierr);
3325ab1d0545SMatthew G. Knepley   }
3326ce1779c8SBarry Smith   ierr = PetscSectionViewFromOptions(*section,NULL,"-section_view");CHKERRQ(ierr);
3327552f7358SJed Brown   PetscFunctionReturn(0);
3328552f7358SJed Brown }
3329552f7358SJed Brown 
33300adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
33310adebc6cSBarry Smith {
3332efe440bfSMatthew G. Knepley   PetscSection   section, s;
3333efe440bfSMatthew G. Knepley   Mat            m;
33343e922f36SToby Isaac   PetscInt       maxHeight;
3335552f7358SJed Brown   PetscErrorCode ierr;
3336552f7358SJed Brown 
3337552f7358SJed Brown   PetscFunctionBegin;
333838221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
33393e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
33403e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
334182f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
3342552f7358SJed Brown   ierr = DMSetDefaultSection(*cdm, section);CHKERRQ(ierr);
33431d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3344efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3345efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3346efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3347efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3348efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
3349552f7358SJed Brown   PetscFunctionReturn(0);
3350552f7358SJed Brown }
3351552f7358SJed Brown 
33527cd05799SMatthew G. Knepley /*@C
33537cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
33547cd05799SMatthew G. Knepley 
33557cd05799SMatthew G. Knepley   Not Collective
33567cd05799SMatthew G. Knepley 
33577cd05799SMatthew G. Knepley   Input Parameters:
33587cd05799SMatthew G. Knepley . dm        - The DMPlex object
33597cd05799SMatthew G. Knepley 
33607cd05799SMatthew G. Knepley   Output Parameter:
33617cd05799SMatthew G. Knepley . section - The PetscSection object
33627cd05799SMatthew G. Knepley 
33637cd05799SMatthew G. Knepley   Level: developer
33647cd05799SMatthew G. Knepley 
33657cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
33667cd05799SMatthew G. Knepley @*/
33670adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
33680adebc6cSBarry Smith {
3369552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3370552f7358SJed Brown 
3371552f7358SJed Brown   PetscFunctionBegin;
3372552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3373552f7358SJed Brown   if (section) *section = mesh->coneSection;
3374552f7358SJed Brown   PetscFunctionReturn(0);
3375552f7358SJed Brown }
3376552f7358SJed Brown 
33777cd05799SMatthew G. Knepley /*@C
33787cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
33797cd05799SMatthew G. Knepley 
33807cd05799SMatthew G. Knepley   Not Collective
33817cd05799SMatthew G. Knepley 
33827cd05799SMatthew G. Knepley   Input Parameters:
33837cd05799SMatthew G. Knepley . dm        - The DMPlex object
33847cd05799SMatthew G. Knepley 
33857cd05799SMatthew G. Knepley   Output Parameter:
33867cd05799SMatthew G. Knepley . section - The PetscSection object
33877cd05799SMatthew G. Knepley 
33887cd05799SMatthew G. Knepley   Level: developer
33897cd05799SMatthew G. Knepley 
33907cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
33917cd05799SMatthew G. Knepley @*/
33928cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
33938cb4d582SMatthew G. Knepley {
33948cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
33958cb4d582SMatthew G. Knepley 
33968cb4d582SMatthew G. Knepley   PetscFunctionBegin;
33978cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33988cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
33998cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
34008cb4d582SMatthew G. Knepley }
34018cb4d582SMatthew G. Knepley 
34027cd05799SMatthew G. Knepley /*@C
34037cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
34047cd05799SMatthew G. Knepley 
34057cd05799SMatthew G. Knepley   Not Collective
34067cd05799SMatthew G. Knepley 
34077cd05799SMatthew G. Knepley   Input Parameters:
34087cd05799SMatthew G. Knepley . dm        - The DMPlex object
34097cd05799SMatthew G. Knepley 
34107cd05799SMatthew G. Knepley   Output Parameter:
34117cd05799SMatthew G. Knepley . cones - The cone for each point
34127cd05799SMatthew G. Knepley 
34137cd05799SMatthew G. Knepley   Level: developer
34147cd05799SMatthew G. Knepley 
34157cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
34167cd05799SMatthew G. Knepley @*/
3417a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3418a6dfd86eSKarl Rupp {
3419552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3420552f7358SJed Brown 
3421552f7358SJed Brown   PetscFunctionBegin;
3422552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3423552f7358SJed Brown   if (cones) *cones = mesh->cones;
3424552f7358SJed Brown   PetscFunctionReturn(0);
3425552f7358SJed Brown }
3426552f7358SJed Brown 
34277cd05799SMatthew G. Knepley /*@C
34287cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
34297cd05799SMatthew G. Knepley 
34307cd05799SMatthew G. Knepley   Not Collective
34317cd05799SMatthew G. Knepley 
34327cd05799SMatthew G. Knepley   Input Parameters:
34337cd05799SMatthew G. Knepley . dm        - The DMPlex object
34347cd05799SMatthew G. Knepley 
34357cd05799SMatthew G. Knepley   Output Parameter:
34367cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
34377cd05799SMatthew G. Knepley 
34387cd05799SMatthew G. Knepley   Level: developer
34397cd05799SMatthew G. Knepley 
34407cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
34417cd05799SMatthew G. Knepley @*/
3442a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3443a6dfd86eSKarl Rupp {
3444552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3445552f7358SJed Brown 
3446552f7358SJed Brown   PetscFunctionBegin;
3447552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3448552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3449552f7358SJed Brown   PetscFunctionReturn(0);
3450552f7358SJed Brown }
3451552f7358SJed Brown 
3452552f7358SJed Brown /******************************** FEM Support **********************************/
3453552f7358SJed Brown 
34543194fc30SMatthew G. Knepley PetscErrorCode DMPlexCreateSpectralClosurePermutation(DM dm, PetscSection section)
34553194fc30SMatthew G. Knepley {
34563194fc30SMatthew G. Knepley   PetscInt      *perm;
345789eabcffSMatthew G. Knepley   PetscInt       dim, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
34583194fc30SMatthew G. Knepley   PetscErrorCode ierr;
34593194fc30SMatthew G. Knepley 
34603194fc30SMatthew G. Knepley   PetscFunctionBegin;
34613194fc30SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
34623194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
34633194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
346489eabcffSMatthew G. Knepley   if (dim <= 1) PetscFunctionReturn(0);
34653194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
34663194fc30SMatthew G. Knepley     /* An order k SEM disc has k-1 dofs on an edge */
34673194fc30SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
34683194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
34693194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
34703194fc30SMatthew G. Knepley     k = k/Nc + 1;
347189eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
34723194fc30SMatthew G. Knepley   }
34733194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
34743194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
347589eabcffSMatthew G. Knepley     switch (dim) {
347689eabcffSMatthew G. Knepley     case 2:
34773194fc30SMatthew 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} */
34783194fc30SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
34793194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
34803194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
34813194fc30SMatthew G. Knepley       k = k/Nc + 1;
34823194fc30SMatthew G. Knepley       /* The SEM order is
34833194fc30SMatthew G. Knepley 
34843194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
348589eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
34863194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
34873194fc30SMatthew G. Knepley       */
34883194fc30SMatthew G. Knepley       {
34893194fc30SMatthew G. Knepley         const PetscInt of   = 0;
34903194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
34913194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
34923194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
34933194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
34943194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
34953194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
34963194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
34973194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
34983194fc30SMatthew G. Knepley         PetscInt       o;
34993194fc30SMatthew G. Knepley 
35003194fc30SMatthew G. Knepley         /* bottom */
35013194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
35023194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
35033194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
35043194fc30SMatthew G. Knepley         /* middle */
35053194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
35063194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
35073194fc30SMatthew 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;
35083194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
35093194fc30SMatthew G. Knepley         }
35103194fc30SMatthew G. Knepley         /* top */
35113194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
35123194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
35133194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
35143194fc30SMatthew G. Knepley         foffset = offset;
35153194fc30SMatthew G. Knepley       }
351689eabcffSMatthew G. Knepley       break;
351789eabcffSMatthew G. Knepley     case 3:
351889eabcffSMatthew G. Knepley       /* The original hex closure is
351989eabcffSMatthew G. Knepley 
352089eabcffSMatthew G. Knepley          {c,
352189eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
352289eabcffSMatthew 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,
352389eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
352489eabcffSMatthew G. Knepley       */
352589eabcffSMatthew G. Knepley       ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
352689eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
352789eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
352889eabcffSMatthew G. Knepley       k = k/Nc + 1;
352989eabcffSMatthew G. Knepley       /* The SEM order is
353089eabcffSMatthew G. Knepley          Bottom Slice
353189eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
353289eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
353389eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
353489eabcffSMatthew G. Knepley 
353589eabcffSMatthew G. Knepley          Middle Slice (j)
353689eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
353789eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
353889eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
353989eabcffSMatthew G. Knepley 
354089eabcffSMatthew G. Knepley          Top Slice
354189eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
354289eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
354389eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
354489eabcffSMatthew G. Knepley       */
354589eabcffSMatthew G. Knepley       {
354689eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
354789eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
354889eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
354989eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
355089eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
355189eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
355289eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
355389eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
355489eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
355589eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
355689eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
355789eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
355889eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
355989eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
356089eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
356189eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
356289eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
356389eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
356489eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
356589eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
356689eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
356789eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
356889eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
356989eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
357089eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
357189eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
357289eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
357389eabcffSMatthew G. Knepley         PetscInt       o, n;
357489eabcffSMatthew G. Knepley 
357589eabcffSMatthew G. Knepley         /* Bottom Slice */
357689eabcffSMatthew G. Knepley         /*   bottom */
357789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
357889eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
357989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
358089eabcffSMatthew G. Knepley         /*   middle */
358189eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
358289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
3583316b7f87SMax 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;}
358489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
35853194fc30SMatthew G. Knepley         }
358689eabcffSMatthew G. Knepley         /*   top */
358789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
358889eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
358989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
359089eabcffSMatthew G. Knepley 
359189eabcffSMatthew G. Knepley         /* Middle Slice */
359289eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
359389eabcffSMatthew G. Knepley           /*   bottom */
359489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
359589eabcffSMatthew 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;
359689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
359789eabcffSMatthew G. Knepley           /*   middle */
359889eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
359989eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
360089eabcffSMatthew 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;
360189eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
360289eabcffSMatthew G. Knepley           }
360389eabcffSMatthew G. Knepley           /*   top */
360489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
360589eabcffSMatthew 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;
360689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
360789eabcffSMatthew G. Knepley         }
360889eabcffSMatthew G. Knepley 
360989eabcffSMatthew G. Knepley         /* Top Slice */
361089eabcffSMatthew G. Knepley         /*   bottom */
361189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
361289eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
361389eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
361489eabcffSMatthew G. Knepley         /*   middle */
361589eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
361689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
361789eabcffSMatthew 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;
361889eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
361989eabcffSMatthew G. Knepley         }
362089eabcffSMatthew G. Knepley         /*   top */
362189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
362289eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
362389eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
362489eabcffSMatthew G. Knepley 
362589eabcffSMatthew G. Knepley         foffset = offset;
362689eabcffSMatthew G. Knepley       }
362789eabcffSMatthew G. Knepley       break;
362889eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
362989eabcffSMatthew G. Knepley     }
363089eabcffSMatthew G. Knepley   }
363189eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
36323194fc30SMatthew G. Knepley   /* Check permutation */
36333194fc30SMatthew G. Knepley   {
36343194fc30SMatthew G. Knepley     PetscInt *check;
36353194fc30SMatthew G. Knepley 
36363194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
36373194fc30SMatthew 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]);}
36383194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
36393194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
36403194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
36413194fc30SMatthew G. Knepley   }
36421fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
36433194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
36443194fc30SMatthew G. Knepley }
36453194fc30SMatthew G. Knepley 
3646e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
3647e071409bSToby Isaac {
3648e071409bSToby Isaac   PetscDS        prob;
3649e071409bSToby Isaac   PetscInt       depth, Nf, h;
3650e071409bSToby Isaac   DMLabel        label;
3651e071409bSToby Isaac   PetscErrorCode ierr;
3652e071409bSToby Isaac 
3653e071409bSToby Isaac   PetscFunctionBeginHot;
3654e071409bSToby Isaac   prob    = dm->prob;
3655e071409bSToby Isaac   Nf      = prob->Nf;
3656e071409bSToby Isaac   label   = dm->depthLabel;
3657e071409bSToby Isaac   *dspace = NULL;
3658e071409bSToby Isaac   if (field < Nf) {
3659e071409bSToby Isaac     PetscObject disc = prob->disc[field];
3660e071409bSToby Isaac 
3661e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
3662e071409bSToby Isaac       PetscDualSpace dsp;
3663e071409bSToby Isaac 
3664e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
3665e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
3666e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
3667e071409bSToby Isaac       h    = depth - 1 - h;
3668e071409bSToby Isaac       if (h) {
3669e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
3670e071409bSToby Isaac       } else {
3671e071409bSToby Isaac         *dspace = dsp;
3672e071409bSToby Isaac       }
3673e071409bSToby Isaac     }
3674e071409bSToby Isaac   }
3675e071409bSToby Isaac   PetscFunctionReturn(0);
3676e071409bSToby Isaac }
3677e071409bSToby Isaac 
3678e071409bSToby Isaac 
36791a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3680a6dfd86eSKarl Rupp {
3681552f7358SJed Brown   PetscScalar    *array, *vArray;
3682d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
36831a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
3684552f7358SJed Brown   PetscErrorCode  ierr;
3685552f7358SJed Brown 
36861b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
36872a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
36885a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
36895a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
36905a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
36913f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
36929df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
36939df71ca4SMatthew G. Knepley       PetscInt dof;
3694d9917b9dSMatthew G. Knepley 
36959df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
36969df71ca4SMatthew G. Knepley       size += dof;
36979df71ca4SMatthew G. Knepley     }
36989df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
36999df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
37002a3aaacfSMatthew G. Knepley       PetscInt       dof;
37015a1bb5cfSMatthew G. Knepley 
37025a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
37032a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
37045a1bb5cfSMatthew G. Knepley       size += dof;
37055a1bb5cfSMatthew G. Knepley     }
37063f7cbbe7SMatthew G. Knepley     if (!values) {
37073f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
37083f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
37093f7cbbe7SMatthew G. Knepley     }
37105a1bb5cfSMatthew G. Knepley     ierr = DMGetWorkArray(dm, size, PETSC_SCALAR, &array);CHKERRQ(ierr);
3711982e9ed1SMatthew G. Knepley   } else {
3712982e9ed1SMatthew G. Knepley     array = *values;
3713982e9ed1SMatthew G. Knepley   }
37149df71ca4SMatthew G. Knepley   size = 0;
37155a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
37169df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
37179df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
37189df71ca4SMatthew G. Knepley     PetscScalar *varr;
3719d9917b9dSMatthew G. Knepley 
37209df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
37219df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
37229df71ca4SMatthew G. Knepley     varr = &vArray[off];
37231a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
37241a271a75SMatthew G. Knepley       array[offset] = varr[d];
37259df71ca4SMatthew G. Knepley     }
37269df71ca4SMatthew G. Knepley     size += dof;
37279df71ca4SMatthew G. Knepley   }
37289df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
37299df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
37309df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
37315a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
37325a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
37335a1bb5cfSMatthew G. Knepley 
373452ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
37355a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
37365a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
37375a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
37385a1bb5cfSMatthew G. Knepley     if (o >= 0) {
37391a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
37401a271a75SMatthew G. Knepley         array[offset] = varr[d];
37415a1bb5cfSMatthew G. Knepley       }
37425a1bb5cfSMatthew G. Knepley     } else {
37431a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
37441a271a75SMatthew G. Knepley         array[offset] = varr[d];
37455a1bb5cfSMatthew G. Knepley       }
37465a1bb5cfSMatthew G. Knepley     }
37479df71ca4SMatthew G. Knepley     size += dof;
37485a1bb5cfSMatthew G. Knepley   }
37495a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
37509df71ca4SMatthew G. Knepley   if (!*values) {
37515a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
37525a1bb5cfSMatthew G. Knepley     *values = array;
37539df71ca4SMatthew G. Knepley   } else {
37548ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
37558c312ff3SMatthew G. Knepley     *csize = size;
37569df71ca4SMatthew G. Knepley   }
37575a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
37585a1bb5cfSMatthew G. Knepley }
3759d9917b9dSMatthew G. Knepley 
3760923c78e0SToby Isaac static PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3761923c78e0SToby Isaac {
3762923c78e0SToby Isaac   const PetscInt *cla;
3763923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
3764923c78e0SToby Isaac   PetscErrorCode ierr;
3765923c78e0SToby Isaac 
3766923c78e0SToby Isaac   PetscFunctionBeginHot;
3767923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
3768923c78e0SToby Isaac   if (!*clPoints) {
3769923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
3770923c78e0SToby Isaac 
3771923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3772923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
3773923c78e0SToby Isaac     /* Compress out points not in the section */
3774923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
3775923c78e0SToby Isaac       PetscInt r = pts[2*p];
3776923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
3777923c78e0SToby Isaac         pts[q*2]   = r;
3778923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
3779923c78e0SToby Isaac         ++q;
3780923c78e0SToby Isaac       }
3781923c78e0SToby Isaac     }
3782923c78e0SToby Isaac     np = q;
3783923c78e0SToby Isaac     cla = NULL;
3784923c78e0SToby Isaac   } else {
3785923c78e0SToby Isaac     PetscInt dof, off;
3786923c78e0SToby Isaac 
3787923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
3788923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
3789923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
3790923c78e0SToby Isaac     np   = dof/2;
3791923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
3792923c78e0SToby Isaac   }
3793923c78e0SToby Isaac   *numPoints = np;
3794923c78e0SToby Isaac   *points    = pts;
3795923c78e0SToby Isaac   *clp       = cla;
3796923c78e0SToby Isaac 
3797923c78e0SToby Isaac   PetscFunctionReturn(0);
3798923c78e0SToby Isaac }
3799923c78e0SToby Isaac 
3800923c78e0SToby Isaac static PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3801923c78e0SToby Isaac {
3802923c78e0SToby Isaac   PetscErrorCode ierr;
3803923c78e0SToby Isaac 
3804923c78e0SToby Isaac   PetscFunctionBeginHot;
3805923c78e0SToby Isaac   if (!*clPoints) {
3806923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
3807923c78e0SToby Isaac   } else {
3808923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
3809923c78e0SToby Isaac   }
3810923c78e0SToby Isaac   *numPoints = 0;
3811923c78e0SToby Isaac   *points    = NULL;
3812923c78e0SToby Isaac   *clSec     = NULL;
3813923c78e0SToby Isaac   *clPoints  = NULL;
3814923c78e0SToby Isaac   *clp       = NULL;
3815923c78e0SToby Isaac   PetscFunctionReturn(0);
3816923c78e0SToby Isaac }
3817923c78e0SToby Isaac 
381897e99dd9SToby 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[])
38191a271a75SMatthew G. Knepley {
38201a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
382197e99dd9SToby Isaac   const PetscInt    **perms = NULL;
382297e99dd9SToby Isaac   const PetscScalar **flips = NULL;
38231a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
38241a271a75SMatthew G. Knepley 
38251a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3826fe02ba77SJed Brown   *size = 0;
382797e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
382897e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
382997e99dd9SToby Isaac     const PetscInt    point = points[2*p];
383097e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
383197e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
38321a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
38331a271a75SMatthew G. Knepley     const PetscScalar *varr;
38341a271a75SMatthew G. Knepley 
38351a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
38361a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
38371a271a75SMatthew G. Knepley     varr = &vArray[off];
383897e99dd9SToby Isaac     if (clperm) {
383997e99dd9SToby Isaac       if (perm) {
384097e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
38411a271a75SMatthew G. Knepley       } else {
384297e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
384397e99dd9SToby Isaac       }
384497e99dd9SToby Isaac       if (flip) {
384597e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
384697e99dd9SToby Isaac       }
384797e99dd9SToby Isaac     } else {
384897e99dd9SToby Isaac       if (perm) {
384997e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
385097e99dd9SToby Isaac       } else {
385197e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
385297e99dd9SToby Isaac       }
385397e99dd9SToby Isaac       if (flip) {
385497e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
38551a271a75SMatthew G. Knepley       }
38561a271a75SMatthew G. Knepley     }
385797e99dd9SToby Isaac     offset += dof;
385897e99dd9SToby Isaac   }
385997e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
38601a271a75SMatthew G. Knepley   *size = offset;
38611a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
38621a271a75SMatthew G. Knepley }
38631a271a75SMatthew G. Knepley 
386497e99dd9SToby 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[])
38651a271a75SMatthew G. Knepley {
38661a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
38671a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
38681a271a75SMatthew G. Knepley 
38691a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3870fe02ba77SJed Brown   *size = 0;
38711a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
387297e99dd9SToby Isaac     PetscInt          p;
387397e99dd9SToby Isaac     const PetscInt    **perms = NULL;
387497e99dd9SToby Isaac     const PetscScalar **flips = NULL;
38751a271a75SMatthew G. Knepley 
387697e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
387797e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
387897e99dd9SToby Isaac       const PetscInt    point = points[2*p];
387997e99dd9SToby Isaac       PetscInt          fdof, foff, b;
38801a271a75SMatthew G. Knepley       const PetscScalar *varr;
388197e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
388297e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
38831a271a75SMatthew G. Knepley 
38841a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
38851a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
38861a271a75SMatthew G. Knepley       varr = &vArray[foff];
388797e99dd9SToby Isaac       if (clperm) {
388897e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
388997e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
389097e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
38911a271a75SMatthew G. Knepley       } else {
389297e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
389397e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
389497e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
38951a271a75SMatthew G. Knepley       }
389697e99dd9SToby Isaac       offset += fdof;
38971a271a75SMatthew G. Knepley     }
389897e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
38991a271a75SMatthew G. Knepley   }
39001a271a75SMatthew G. Knepley   *size = offset;
39011a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
39021a271a75SMatthew G. Knepley }
39031a271a75SMatthew G. Knepley 
3904552f7358SJed Brown /*@C
3905552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
3906552f7358SJed Brown 
3907552f7358SJed Brown   Not collective
3908552f7358SJed Brown 
3909552f7358SJed Brown   Input Parameters:
3910552f7358SJed Brown + dm - The DM
3911552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
3912552f7358SJed Brown . v - The local vector
3913552f7358SJed Brown - point - The sieve point in the DM
3914552f7358SJed Brown 
3915552f7358SJed Brown   Output Parameters:
3916552f7358SJed Brown + csize - The number of values in the closure, or NULL
3917552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
3918552f7358SJed Brown 
3919552f7358SJed Brown   Fortran Notes:
3920552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
3921552f7358SJed Brown   include petsc.h90 in your code.
3922552f7358SJed Brown 
3923552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
3924552f7358SJed Brown 
3925552f7358SJed Brown   Level: intermediate
3926552f7358SJed Brown 
3927552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
3928552f7358SJed Brown @*/
3929552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3930552f7358SJed Brown {
3931552f7358SJed Brown   PetscSection       clSection;
3932d9917b9dSMatthew G. Knepley   IS                 clPoints;
39338a84db2dSMatthew G. Knepley   PetscScalar       *array;
39348a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
3935552f7358SJed Brown   PetscInt          *points = NULL;
39368f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
39371a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
3938552f7358SJed Brown   PetscErrorCode     ierr;
3939552f7358SJed Brown 
3940d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
3941552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3942552f7358SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
39431a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
39441a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
3945552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3946552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3947552f7358SJed Brown   if (depth == 1 && numFields < 2) {
39481a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
3949552f7358SJed Brown     PetscFunctionReturn(0);
3950552f7358SJed Brown   }
39511a271a75SMatthew G. Knepley   /* Get points */
3952923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
39531fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
39541a271a75SMatthew G. Knepley   /* Get array */
3955bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
39561a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
3957552f7358SJed Brown 
39581a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
3959552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
39601a271a75SMatthew G. Knepley       asize += dof;
3961552f7358SJed Brown     }
3962bd6c0d32SMatthew G. Knepley     if (!values) {
3963923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
39641a271a75SMatthew G. Knepley       if (csize) *csize = asize;
3965bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
3966bd6c0d32SMatthew G. Knepley     }
39671a271a75SMatthew G. Knepley     ierr = DMGetWorkArray(dm, asize, PETSC_SCALAR, &array);CHKERRQ(ierr);
3968d0f6b257SMatthew G. Knepley   } else {
3969d0f6b257SMatthew G. Knepley     array = *values;
3970d0f6b257SMatthew G. Knepley   }
39718a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
39721a271a75SMatthew G. Knepley   /* Get values */
397397e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
397497e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
39751a271a75SMatthew G. Knepley   /* Cleanup points */
3976923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
39771a271a75SMatthew G. Knepley   /* Cleanup array */
39788a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
3979d0f6b257SMatthew G. Knepley   if (!*values) {
3980552f7358SJed Brown     if (csize) *csize = size;
3981552f7358SJed Brown     *values = array;
3982d0f6b257SMatthew G. Knepley   } else {
3983f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
3984d0f6b257SMatthew G. Knepley     *csize = size;
3985d0f6b257SMatthew G. Knepley   }
3986552f7358SJed Brown   PetscFunctionReturn(0);
3987552f7358SJed Brown }
3988552f7358SJed Brown 
3989552f7358SJed Brown /*@C
3990552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
3991552f7358SJed Brown 
3992552f7358SJed Brown   Not collective
3993552f7358SJed Brown 
3994552f7358SJed Brown   Input Parameters:
3995552f7358SJed Brown + dm - The DM
39960298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
3997552f7358SJed Brown . v - The local vector
3998552f7358SJed Brown . point - The sieve point in the DM
39990298fd71SBarry Smith . csize - The number of values in the closure, or NULL
4000552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
4001552f7358SJed Brown 
40023813dfbdSMatthew G Knepley   Fortran Notes:
40033813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
40043813dfbdSMatthew G Knepley   include petsc.h90 in your code.
40053813dfbdSMatthew G Knepley 
40063813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
40073813dfbdSMatthew G Knepley 
4008552f7358SJed Brown   Level: intermediate
4009552f7358SJed Brown 
4010552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4011552f7358SJed Brown @*/
40127c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4013a6dfd86eSKarl Rupp {
4014552f7358SJed Brown   PetscInt       size = 0;
4015552f7358SJed Brown   PetscErrorCode ierr;
4016552f7358SJed Brown 
4017552f7358SJed Brown   PetscFunctionBegin;
4018552f7358SJed Brown   /* Should work without recalculating size */
4019552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, size, PETSC_SCALAR, (void*) values);CHKERRQ(ierr);
4020552f7358SJed Brown   PetscFunctionReturn(0);
4021552f7358SJed Brown }
4022552f7358SJed Brown 
4023552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
4024552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
4025552f7358SJed Brown 
402697e99dd9SToby 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[])
4027552f7358SJed Brown {
4028552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4029552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4030552f7358SJed Brown   PetscScalar    *a;
4031552f7358SJed Brown   PetscInt        off, cind = 0, k;
4032552f7358SJed Brown   PetscErrorCode  ierr;
4033552f7358SJed Brown 
4034552f7358SJed Brown   PetscFunctionBegin;
4035552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4036552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4037552f7358SJed Brown   a    = &array[off];
4038552f7358SJed Brown   if (!cdof || setBC) {
403997e99dd9SToby Isaac     if (clperm) {
404097e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
404197e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
4042552f7358SJed Brown     } else {
404397e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
404497e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4045552f7358SJed Brown     }
4046552f7358SJed Brown   } else {
4047552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
404897e99dd9SToby Isaac     if (clperm) {
404997e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
4050552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
405197e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4052552f7358SJed Brown         }
4053552f7358SJed Brown       } else {
4054552f7358SJed Brown         for (k = 0; k < dof; ++k) {
4055552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
405697e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
405797e99dd9SToby Isaac         }
405897e99dd9SToby Isaac       }
405997e99dd9SToby Isaac     } else {
406097e99dd9SToby Isaac       if (perm) {
406197e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
406297e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
406397e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
406497e99dd9SToby Isaac         }
406597e99dd9SToby Isaac       } else {
406697e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
406797e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
406897e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
406997e99dd9SToby Isaac         }
4070552f7358SJed Brown       }
4071552f7358SJed Brown     }
4072552f7358SJed Brown   }
4073552f7358SJed Brown   PetscFunctionReturn(0);
4074552f7358SJed Brown }
4075552f7358SJed Brown 
407697e99dd9SToby 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[])
4077a5e93ea8SMatthew G. Knepley {
4078a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
4079a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4080a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
4081a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
4082a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4083a5e93ea8SMatthew G. Knepley 
4084a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4085a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4086a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4087a5e93ea8SMatthew G. Knepley   a    = &array[off];
4088a5e93ea8SMatthew G. Knepley   if (cdof) {
4089a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
409097e99dd9SToby Isaac     if (clperm) {
409197e99dd9SToby Isaac       if (perm) {
4092a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4093a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
409497e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
409597e99dd9SToby Isaac             cind++;
4096a5e93ea8SMatthew G. Knepley           }
4097a5e93ea8SMatthew G. Knepley         }
4098a5e93ea8SMatthew G. Knepley       } else {
4099a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4100a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
410197e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
410297e99dd9SToby Isaac             cind++;
410397e99dd9SToby Isaac           }
410497e99dd9SToby Isaac         }
410597e99dd9SToby Isaac       }
410697e99dd9SToby Isaac     } else {
410797e99dd9SToby Isaac       if (perm) {
410897e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
410997e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
411097e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
411197e99dd9SToby Isaac             cind++;
411297e99dd9SToby Isaac           }
411397e99dd9SToby Isaac         }
411497e99dd9SToby Isaac       } else {
411597e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
411697e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
411797e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
411897e99dd9SToby Isaac             cind++;
411997e99dd9SToby Isaac           }
4120a5e93ea8SMatthew G. Knepley         }
4121a5e93ea8SMatthew G. Knepley       }
4122a5e93ea8SMatthew G. Knepley     }
4123a5e93ea8SMatthew G. Knepley   }
4124a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4125a5e93ea8SMatthew G. Knepley }
4126a5e93ea8SMatthew G. Knepley 
412797e99dd9SToby 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[])
4128a6dfd86eSKarl Rupp {
4129552f7358SJed Brown   PetscScalar    *a;
41301a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
41311a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
413297e99dd9SToby Isaac   PetscInt        cind = 0, b;
4133552f7358SJed Brown   PetscErrorCode  ierr;
4134552f7358SJed Brown 
4135552f7358SJed Brown   PetscFunctionBegin;
4136552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4137552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
41381a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
41391a271a75SMatthew G. Knepley   a    = &array[foff];
4140552f7358SJed Brown   if (!fcdof || setBC) {
414197e99dd9SToby Isaac     if (clperm) {
414297e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
414397e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4144552f7358SJed Brown     } else {
414597e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
414697e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4147552f7358SJed Brown     }
4148552f7358SJed Brown   } else {
4149552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
415097e99dd9SToby Isaac     if (clperm) {
415197e99dd9SToby Isaac       if (perm) {
415297e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
415397e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
415497e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4155552f7358SJed Brown         }
4156552f7358SJed Brown       } else {
415797e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
415897e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
415997e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
416097e99dd9SToby Isaac         }
416197e99dd9SToby Isaac       }
416297e99dd9SToby Isaac     } else {
416397e99dd9SToby Isaac       if (perm) {
416497e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
416597e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
416697e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
416797e99dd9SToby Isaac         }
416897e99dd9SToby Isaac       } else {
416997e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
417097e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
417197e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4172552f7358SJed Brown         }
4173552f7358SJed Brown       }
4174552f7358SJed Brown     }
4175552f7358SJed Brown   }
41761a271a75SMatthew G. Knepley   *offset += fdof;
4177552f7358SJed Brown   PetscFunctionReturn(0);
4178552f7358SJed Brown }
4179552f7358SJed Brown 
418097e99dd9SToby 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[])
4181a5e93ea8SMatthew G. Knepley {
4182a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
41831a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
41841a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
418597e99dd9SToby Isaac   PetscInt        cind = 0, b;
4186a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4187a5e93ea8SMatthew G. Knepley 
4188a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4189a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4190a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
41911a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
41921a271a75SMatthew G. Knepley   a    = &array[foff];
4193a5e93ea8SMatthew G. Knepley   if (fcdof) {
4194a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
419597e99dd9SToby Isaac     if (clperm) {
419697e99dd9SToby Isaac       if (perm) {
419797e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
419897e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
419997e99dd9SToby Isaac             fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4200a5e93ea8SMatthew G. Knepley             ++cind;
4201a5e93ea8SMatthew G. Knepley           }
4202a5e93ea8SMatthew G. Knepley         }
4203a5e93ea8SMatthew G. Knepley       } else {
420497e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
420597e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
420697e99dd9SToby Isaac             fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
420797e99dd9SToby Isaac             ++cind;
420897e99dd9SToby Isaac           }
420997e99dd9SToby Isaac         }
421097e99dd9SToby Isaac       }
421197e99dd9SToby Isaac     } else {
421297e99dd9SToby Isaac       if (perm) {
421397e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
421497e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
421597e99dd9SToby Isaac             fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
421697e99dd9SToby Isaac             ++cind;
421797e99dd9SToby Isaac           }
421897e99dd9SToby Isaac         }
421997e99dd9SToby Isaac       } else {
422097e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
422197e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
422297e99dd9SToby Isaac             fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4223a5e93ea8SMatthew G. Knepley             ++cind;
4224a5e93ea8SMatthew G. Knepley           }
4225a5e93ea8SMatthew G. Knepley         }
4226a5e93ea8SMatthew G. Knepley       }
4227a5e93ea8SMatthew G. Knepley     }
4228a5e93ea8SMatthew G. Knepley   }
42291a271a75SMatthew G. Knepley   *offset += fdof;
4230a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4231a5e93ea8SMatthew G. Knepley }
4232a5e93ea8SMatthew G. Knepley 
42338f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4234a6dfd86eSKarl Rupp {
4235552f7358SJed Brown   PetscScalar    *array;
42361b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
42371b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4238552f7358SJed Brown   PetscErrorCode  ierr;
4239552f7358SJed Brown 
42401b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
4241b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4242b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
4243b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4244b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
4245b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4246b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4247b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
4248b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
4249b6ebb6e6SMatthew G. Knepley 
4250b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4251b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
4252b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
4253b6ebb6e6SMatthew G. Knepley     {
4254b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4255b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
4256b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
4257b6ebb6e6SMatthew G. Knepley 
4258b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
4259b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
4260b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
4261b6ebb6e6SMatthew G. Knepley       if (!cdof) {
4262b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4263b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4264b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4265b6ebb6e6SMatthew G. Knepley           }
4266b6ebb6e6SMatthew G. Knepley         } else {
4267b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4268b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4269b6ebb6e6SMatthew G. Knepley           }
4270b6ebb6e6SMatthew G. Knepley         }
4271b6ebb6e6SMatthew G. Knepley       } else {
4272b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
4273b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4274b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4275b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4276b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4277b6ebb6e6SMatthew G. Knepley           }
4278b6ebb6e6SMatthew G. Knepley         } else {
4279b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4280b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4281b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4282b6ebb6e6SMatthew G. Knepley           }
4283b6ebb6e6SMatthew G. Knepley         }
4284b6ebb6e6SMatthew G. Knepley       }
4285b6ebb6e6SMatthew G. Knepley     }
4286b6ebb6e6SMatthew G. Knepley   }
4287b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4288b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4289b6ebb6e6SMatthew G. Knepley }
42901b406b76SMatthew G. Knepley 
42911b406b76SMatthew G. Knepley /*@C
42921b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
42931b406b76SMatthew G. Knepley 
42941b406b76SMatthew G. Knepley   Not collective
42951b406b76SMatthew G. Knepley 
42961b406b76SMatthew G. Knepley   Input Parameters:
42971b406b76SMatthew G. Knepley + dm - The DM
42981b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
42991b406b76SMatthew G. Knepley . v - The local vector
43001b406b76SMatthew G. Knepley . point - The sieve point in the DM
43011b406b76SMatthew G. Knepley . values - The array of values
43021b406b76SMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
43031b406b76SMatthew G. Knepley 
43041b406b76SMatthew G. Knepley   Fortran Notes:
43051b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
43061b406b76SMatthew G. Knepley 
43071b406b76SMatthew G. Knepley   Level: intermediate
43081b406b76SMatthew G. Knepley 
43091b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
43101b406b76SMatthew G. Knepley @*/
43111b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
43121b406b76SMatthew G. Knepley {
43131b406b76SMatthew G. Knepley   PetscSection    clSection;
43141b406b76SMatthew G. Knepley   IS              clPoints;
43151b406b76SMatthew G. Knepley   PetscScalar    *array;
43161b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
431797e99dd9SToby Isaac   const PetscInt *clp, *clperm;
43181a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
43191b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
43201b406b76SMatthew G. Knepley 
43211a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
43221b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43231b406b76SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
43241a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
43251a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
43261b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
43271b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
43281b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
43298f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
43301b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
43311b406b76SMatthew G. Knepley   }
43321a271a75SMatthew G. Knepley   /* Get points */
433397e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4334923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
43351a271a75SMatthew G. Knepley   /* Get array */
4336552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
43371a271a75SMatthew G. Knepley   /* Get values */
4338ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
433997e99dd9SToby Isaac     PetscInt offset = 0, f;
4340552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
434197e99dd9SToby Isaac       const PetscInt    **perms = NULL;
434297e99dd9SToby Isaac       const PetscScalar **flips = NULL;
434397e99dd9SToby Isaac 
434497e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4345552f7358SJed Brown       switch (mode) {
4346552f7358SJed Brown       case INSERT_VALUES:
434797e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
434897e99dd9SToby Isaac           const PetscInt    point = points[2*p];
434997e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
435097e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
435197e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4352552f7358SJed Brown         } break;
4353552f7358SJed Brown       case INSERT_ALL_VALUES:
435497e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
435597e99dd9SToby Isaac           const PetscInt    point = points[2*p];
435697e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
435797e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
435897e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4359552f7358SJed Brown         } break;
4360a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
436197e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
436297e99dd9SToby Isaac           const PetscInt    point = points[2*p];
436397e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
436497e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
436597e99dd9SToby Isaac           updatePointFieldsBC_private(section, point, perm, flip, f, insert, clperm, values, &offset, array);
4366a5e93ea8SMatthew G. Knepley         } break;
4367552f7358SJed Brown       case ADD_VALUES:
436897e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
436997e99dd9SToby Isaac           const PetscInt    point = points[2*p];
437097e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
437197e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
437297e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4373552f7358SJed Brown         } break;
4374552f7358SJed Brown       case ADD_ALL_VALUES:
437597e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
437697e99dd9SToby Isaac           const PetscInt    point = points[2*p];
437797e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
437897e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
437997e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4380552f7358SJed Brown         } break;
4381304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
438297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
438397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
438497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
438597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
438697e99dd9SToby Isaac           updatePointFieldsBC_private(section, point, perm, flip, f, add, clperm, values, &offset, array);
4387304ab55fSMatthew G. Knepley         } break;
4388552f7358SJed Brown       default:
4389f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4390552f7358SJed Brown       }
439197e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
43921a271a75SMatthew G. Knepley     }
4393552f7358SJed Brown   } else {
43941a271a75SMatthew G. Knepley     PetscInt dof, off;
439597e99dd9SToby Isaac     const PetscInt    **perms = NULL;
439697e99dd9SToby Isaac     const PetscScalar **flips = NULL;
43971a271a75SMatthew G. Knepley 
439897e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4399552f7358SJed Brown     switch (mode) {
4400552f7358SJed Brown     case INSERT_VALUES:
440197e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
440297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
440397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
440497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
440597e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
440697e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4407552f7358SJed Brown       } break;
4408552f7358SJed Brown     case INSERT_ALL_VALUES:
440997e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
441097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
441197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
441297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
441397e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
441497e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4415552f7358SJed Brown       } break;
4416a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
441797e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
441897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
441997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
442097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
442197e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
442297e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4423a5e93ea8SMatthew G. Knepley       } break;
4424552f7358SJed Brown     case ADD_VALUES:
442597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
442697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
442797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
442897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
442997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
443097e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4431552f7358SJed Brown       } break;
4432552f7358SJed Brown     case ADD_ALL_VALUES:
443397e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
443497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
443597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
443697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
443797e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
443897e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4439552f7358SJed Brown       } break;
4440304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
444197e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
444297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
444397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
444497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
444597e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
444697e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
4447304ab55fSMatthew G. Knepley       } break;
4448552f7358SJed Brown     default:
4449f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4450552f7358SJed Brown     }
445197e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4452552f7358SJed Brown   }
44531a271a75SMatthew G. Knepley   /* Cleanup points */
4454923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
44551a271a75SMatthew G. Knepley   /* Cleanup array */
4456552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4457552f7358SJed Brown   PetscFunctionReturn(0);
4458552f7358SJed Brown }
4459552f7358SJed Brown 
4460e07394fbSMatthew G. Knepley PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, const PetscScalar values[], InsertMode mode)
4461e07394fbSMatthew G. Knepley {
4462e07394fbSMatthew G. Knepley   PetscSection      clSection;
4463e07394fbSMatthew G. Knepley   IS                clPoints;
4464e07394fbSMatthew G. Knepley   PetscScalar       *array;
4465e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
4466b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
4467e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
446897e99dd9SToby Isaac   PetscInt          offset = 0, f;
4469e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
4470e07394fbSMatthew G. Knepley 
4471e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
4472e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4473e07394fbSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
4474e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4475e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4476e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4477e07394fbSMatthew G. Knepley   /* Get points */
4478b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4479923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4480e07394fbSMatthew G. Knepley   /* Get array */
4481e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4482e07394fbSMatthew G. Knepley   /* Get values */
4483e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
448497e99dd9SToby Isaac     const PetscInt    **perms = NULL;
448597e99dd9SToby Isaac     const PetscScalar **flips = NULL;
448697e99dd9SToby Isaac 
4487e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
4488e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4489e07394fbSMatthew G. Knepley         PetscInt fdof;
4490e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
4491e07394fbSMatthew G. Knepley         offset += fdof;
4492e07394fbSMatthew G. Knepley       }
4493e07394fbSMatthew G. Knepley       continue;
4494e07394fbSMatthew G. Knepley     }
449597e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4496e07394fbSMatthew G. Knepley     switch (mode) {
4497e07394fbSMatthew G. Knepley     case INSERT_VALUES:
449897e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
449997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
450097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
450197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4502b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4503e07394fbSMatthew G. Knepley       } break;
4504e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
450597e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
450697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
450797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
450897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4509b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4510e07394fbSMatthew G. Knepley         } break;
4511e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
451297e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
451397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
451497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
451597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4516b04c866fSMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, insert, clperm, values, &offset, array);
4517e07394fbSMatthew G. Knepley       } break;
4518e07394fbSMatthew G. Knepley     case ADD_VALUES:
451997e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
452097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
452197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
452297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4523b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4524e07394fbSMatthew G. Knepley       } break;
4525e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
452697e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
452797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
452897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
452997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4530b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4531e07394fbSMatthew G. Knepley       } break;
4532e07394fbSMatthew G. Knepley     default:
4533f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4534e07394fbSMatthew G. Knepley     }
453597e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4536e07394fbSMatthew G. Knepley   }
4537e07394fbSMatthew G. Knepley   /* Cleanup points */
4538923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4539e07394fbSMatthew G. Knepley   /* Cleanup array */
4540e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4541e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
4542e07394fbSMatthew G. Knepley }
4543e07394fbSMatthew G. Knepley 
45447cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
4545552f7358SJed Brown {
4546552f7358SJed Brown   PetscMPIInt    rank;
4547552f7358SJed Brown   PetscInt       i, j;
4548552f7358SJed Brown   PetscErrorCode ierr;
4549552f7358SJed Brown 
4550552f7358SJed Brown   PetscFunctionBegin;
455182f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
4552f347f43bSBarry Smith   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for sieve point %D\n", rank, point);CHKERRQ(ierr);
4553e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
4554e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
4555b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
4556b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
4557e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
4558b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
4559519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
45607eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
4561552f7358SJed Brown #else
4562b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
4563552f7358SJed Brown #endif
4564552f7358SJed Brown     }
456577a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
4566552f7358SJed Brown   }
4567552f7358SJed Brown   PetscFunctionReturn(0);
4568552f7358SJed Brown }
4569552f7358SJed Brown 
4570552f7358SJed Brown /* . off - The global offset of this point */
45714acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], PetscInt indices[])
4572a6dfd86eSKarl Rupp {
4573e6ccafaeSMatthew G Knepley   PetscInt        dof;    /* The number of unknowns on this point */
4574552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4575552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4576552f7358SJed Brown   PetscInt        cind = 0, k;
4577552f7358SJed Brown   PetscErrorCode  ierr;
4578552f7358SJed Brown 
4579552f7358SJed Brown   PetscFunctionBegin;
4580552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
4581552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4582552f7358SJed Brown   if (!cdof || setBC) {
45834acb8e1eSToby Isaac     if (perm) {
45844acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+perm[k]] = off + k;
4585552f7358SJed Brown     } else {
45864acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+k] = off + k;
4587552f7358SJed Brown     }
4588552f7358SJed Brown   } else {
4589552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
45904acb8e1eSToby Isaac     if (perm) {
45914acb8e1eSToby Isaac       for (k = 0; k < dof; ++k) {
45924acb8e1eSToby Isaac         if ((cind < cdof) && (k == cdofs[cind])) {
45934acb8e1eSToby Isaac           /* Insert check for returning constrained indices */
45944acb8e1eSToby Isaac           indices[*loff+perm[k]] = -(off+k+1);
45954acb8e1eSToby Isaac           ++cind;
45964acb8e1eSToby Isaac         } else {
45974acb8e1eSToby Isaac           indices[*loff+perm[k]] = off+k-cind;
45984acb8e1eSToby Isaac         }
45994acb8e1eSToby Isaac       }
46004acb8e1eSToby Isaac     } else {
4601552f7358SJed Brown       for (k = 0; k < dof; ++k) {
4602552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
4603552f7358SJed Brown           /* Insert check for returning constrained indices */
4604e6ccafaeSMatthew G Knepley           indices[*loff+k] = -(off+k+1);
4605552f7358SJed Brown           ++cind;
4606552f7358SJed Brown         } else {
4607e6ccafaeSMatthew G Knepley           indices[*loff+k] = off+k-cind;
4608552f7358SJed Brown         }
4609552f7358SJed Brown       }
4610552f7358SJed Brown     }
4611552f7358SJed Brown   }
4612e6ccafaeSMatthew G Knepley   *loff += dof;
4613552f7358SJed Brown   PetscFunctionReturn(0);
4614552f7358SJed Brown }
4615552f7358SJed Brown 
4616552f7358SJed Brown /* . off - The global offset of this point */
46174acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, PetscInt indices[])
4618a6dfd86eSKarl Rupp {
4619552f7358SJed Brown   PetscInt       numFields, foff, f;
4620552f7358SJed Brown   PetscErrorCode ierr;
4621552f7358SJed Brown 
4622552f7358SJed Brown   PetscFunctionBegin;
4623552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4624552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
46254acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
4626552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
46274acb8e1eSToby Isaac     PetscInt        cind = 0, b;
46284acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
4629552f7358SJed Brown 
4630552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4631552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
4632552f7358SJed Brown     if (!cfdof || setBC) {
46334acb8e1eSToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {indices[foffs[f]+perm[b]] = off+foff+b;}}
46344acb8e1eSToby Isaac       else      {for (b = 0; b < fdof; b++) {indices[foffs[f]+     b ] = off+foff+b;}}
4635552f7358SJed Brown     } else {
4636552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
46374acb8e1eSToby Isaac       if (perm) {
46384acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
46394acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
46404acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = -(off+foff+b+1);
4641552f7358SJed Brown             ++cind;
4642552f7358SJed Brown           } else {
46434acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = off+foff+b-cind;
4644552f7358SJed Brown           }
4645552f7358SJed Brown         }
4646552f7358SJed Brown       } else {
46474acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
46484acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
46494acb8e1eSToby Isaac             indices[foffs[f]+b] = -(off+foff+b+1);
4650552f7358SJed Brown             ++cind;
4651552f7358SJed Brown           } else {
46524acb8e1eSToby Isaac             indices[foffs[f]+b] = off+foff+b-cind;
4653552f7358SJed Brown           }
4654552f7358SJed Brown         }
4655552f7358SJed Brown       }
4656552f7358SJed Brown     }
4657a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
4658552f7358SJed Brown     foffs[f] += fdof;
4659552f7358SJed Brown   }
4660552f7358SJed Brown   PetscFunctionReturn(0);
4661552f7358SJed Brown }
4662552f7358SJed Brown 
46634acb8e1eSToby 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)
4664d3d1a6afSToby Isaac {
4665d3d1a6afSToby Isaac   Mat             cMat;
4666d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
4667d3d1a6afSToby Isaac   IS              aIS;
4668d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
4669d3d1a6afSToby Isaac   const PetscInt  *anchors;
4670e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
4671d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
4672d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
4673d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
4674d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
4675d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
4676d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
4677d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
46786ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
4679d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
4680d3d1a6afSToby Isaac   PetscErrorCode  ierr;
4681d3d1a6afSToby Isaac 
4682d3d1a6afSToby Isaac   PetscFunctionBegin;
4683d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4684d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4685d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4686d3d1a6afSToby Isaac 
4687a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
4688d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
4689d3d1a6afSToby Isaac   if (aSec) {
4690d3d1a6afSToby Isaac     ierr = PetscMemzero(newOffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
4691d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
4692d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
4693d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
4694d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
4695d3d1a6afSToby Isaac      * into the global matrix anyway) */
4696d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
4697d3d1a6afSToby Isaac       PetscInt b    = points[p];
46984b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4699d3d1a6afSToby Isaac 
47004b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
47014b2f2278SToby Isaac       if (!bSecDof) {
47024b2f2278SToby Isaac         continue;
47034b2f2278SToby Isaac       }
4704d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4705d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
4706d3d1a6afSToby Isaac       }
4707d3d1a6afSToby Isaac       if (bDof) {
4708d3d1a6afSToby Isaac         /* this point is constrained */
4709d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
4710d3d1a6afSToby Isaac         PetscInt bOff, q;
4711d3d1a6afSToby Isaac 
4712d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
4713d3d1a6afSToby Isaac         newNumPoints  += bDof;
4714d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
4715d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4716d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
4717d3d1a6afSToby Isaac           PetscInt aDof;
4718d3d1a6afSToby Isaac 
4719d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
4720d3d1a6afSToby Isaac           newNumIndices += aDof;
4721d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
4722d3d1a6afSToby Isaac             PetscInt fDof;
4723d3d1a6afSToby Isaac 
4724d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
4725d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
4726d3d1a6afSToby Isaac           }
4727d3d1a6afSToby Isaac         }
4728d3d1a6afSToby Isaac       }
4729d3d1a6afSToby Isaac       else {
4730d3d1a6afSToby Isaac         /* this point is not constrained */
4731d3d1a6afSToby Isaac         newNumPoints++;
47324b2f2278SToby Isaac         newNumIndices += bSecDof;
4733d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
4734d3d1a6afSToby Isaac           PetscInt fDof;
4735d3d1a6afSToby Isaac 
4736d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4737d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
4738d3d1a6afSToby Isaac         }
4739d3d1a6afSToby Isaac       }
4740d3d1a6afSToby Isaac     }
4741d3d1a6afSToby Isaac   }
4742d3d1a6afSToby Isaac   if (!anyConstrained) {
474372b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
474472b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
474572b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
474672b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
474772b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
4748d3d1a6afSToby Isaac     PetscFunctionReturn(0);
4749d3d1a6afSToby Isaac   }
4750d3d1a6afSToby Isaac 
47516ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
47526ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
47536ecaa68aSToby Isaac 
4754f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
4755d3d1a6afSToby Isaac 
47566ecaa68aSToby Isaac   if (!outPoints && !outValues) {
47576ecaa68aSToby Isaac     if (offsets) {
47586ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
47596ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
47606ecaa68aSToby Isaac       }
47616ecaa68aSToby Isaac     }
47626ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
47636ecaa68aSToby Isaac     PetscFunctionReturn(0);
47646ecaa68aSToby Isaac   }
47656ecaa68aSToby Isaac 
4766f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
4767d3d1a6afSToby Isaac 
4768f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
4769d3d1a6afSToby Isaac 
4770d3d1a6afSToby Isaac   /* workspaces */
4771d3d1a6afSToby Isaac   if (numFields) {
4772d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
4773d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
4774d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
4775d3d1a6afSToby Isaac     }
4776d3d1a6afSToby Isaac   }
4777d3d1a6afSToby Isaac   else {
4778d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
4779d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
4780d3d1a6afSToby Isaac   }
4781d3d1a6afSToby Isaac 
4782d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
4783d3d1a6afSToby Isaac   if (numFields) {
47844b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
47854b2f2278SToby Isaac 
4786d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4787d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
47884b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4789d3d1a6afSToby Isaac 
47904b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
47914b2f2278SToby Isaac       if (!bSecDof) {
47924b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
47934b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
47944b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
47954b2f2278SToby Isaac         }
47964b2f2278SToby Isaac         continue;
47974b2f2278SToby Isaac       }
4798d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4799d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4800d3d1a6afSToby Isaac       }
4801d3d1a6afSToby Isaac       if (bDof) {
4802d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4803d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
4804d3d1a6afSToby Isaac 
4805d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4806d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4807d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
4808d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
4809d3d1a6afSToby Isaac             PetscInt aFDof;
4810d3d1a6afSToby Isaac 
4811d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
4812d3d1a6afSToby Isaac             allFDof += aFDof;
4813d3d1a6afSToby Isaac           }
4814d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
4815d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
4816d3d1a6afSToby Isaac         }
4817d3d1a6afSToby Isaac       }
4818d3d1a6afSToby Isaac       else {
4819d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4820d3d1a6afSToby Isaac           PetscInt fDof;
4821d3d1a6afSToby Isaac 
4822d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4823d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
4824d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
4825d3d1a6afSToby Isaac         }
4826d3d1a6afSToby Isaac       }
4827d3d1a6afSToby Isaac     }
48284b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
48294b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
48304b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
4831d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
4832d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
4833d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
4834d3d1a6afSToby Isaac       }
483519f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
483619f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
4837d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
4838d3d1a6afSToby Isaac     }
4839d3d1a6afSToby Isaac   }
4840d3d1a6afSToby Isaac   else {
4841d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4842d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
48434b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4844d3d1a6afSToby Isaac 
48454b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
48464b2f2278SToby Isaac       if (!bSecDof) {
48474b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
48484b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
48494b2f2278SToby Isaac         continue;
48504b2f2278SToby Isaac       }
4851d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4852d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4853d3d1a6afSToby Isaac       }
4854d3d1a6afSToby Isaac       if (bDof) {
48554b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
4856d3d1a6afSToby Isaac 
4857d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4858d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4859d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
4860d3d1a6afSToby Isaac 
4861d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
4862d3d1a6afSToby Isaac           allDof += aDof;
4863d3d1a6afSToby Isaac         }
4864d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
48654b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
4866d3d1a6afSToby Isaac       }
4867d3d1a6afSToby Isaac       else {
48684b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
4869d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
4870d3d1a6afSToby Isaac       }
4871d3d1a6afSToby Isaac     }
4872d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
4873d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
4874d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4875d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
4876d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
4877d3d1a6afSToby Isaac     }
4878d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
4879d3d1a6afSToby Isaac   }
4880d3d1a6afSToby Isaac 
48816ecaa68aSToby Isaac   /* output arrays */
48826ecaa68aSToby Isaac   ierr = DMGetWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
48836ecaa68aSToby Isaac 
4884d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
4885d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
4886d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
4887d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
4888d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
4889d3d1a6afSToby Isaac   if (numFields) {
4890d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
4891d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4892d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
48934b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4894d3d1a6afSToby Isaac 
48954b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
48964b2f2278SToby Isaac       if (!bSecDof) {
48974b2f2278SToby Isaac         continue;
48984b2f2278SToby Isaac       }
4899d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4900d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4901d3d1a6afSToby Isaac       }
4902d3d1a6afSToby Isaac       if (bDof) {
4903d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
4904d3d1a6afSToby Isaac 
4905d3d1a6afSToby Isaac         fStart[0] = 0;
4906d3d1a6afSToby Isaac         fEnd[0]   = 0;
4907d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4908d3d1a6afSToby Isaac           PetscInt fDof;
4909d3d1a6afSToby Isaac 
4910d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
4911d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
4912d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
4913d3d1a6afSToby Isaac         }
4914d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
49154acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, indices);CHKERRQ(ierr);
4916d3d1a6afSToby Isaac 
4917d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
4918d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
4919d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4920d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
4921d3d1a6afSToby Isaac 
4922d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
4923d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
4924d3d1a6afSToby Isaac         }
4925d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4926d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4927d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
4928d3d1a6afSToby Isaac 
4929d3d1a6afSToby 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 */
4930d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
4931d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
4932302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
49334acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, newIndices);CHKERRQ(ierr);
4934d3d1a6afSToby Isaac         }
4935d3d1a6afSToby Isaac         newP += bDof;
4936d3d1a6afSToby Isaac 
49376ecaa68aSToby Isaac         if (outValues) {
4938d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
4939d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
4940d3d1a6afSToby 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);
4941d3d1a6afSToby Isaac           }
4942d3d1a6afSToby Isaac         }
49436ecaa68aSToby Isaac       }
4944d3d1a6afSToby Isaac       else {
4945d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
4946d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
4947d3d1a6afSToby Isaac         newP++;
4948d3d1a6afSToby Isaac       }
4949d3d1a6afSToby Isaac     }
4950d3d1a6afSToby Isaac   } else {
4951d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4952d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4953d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
49544b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4955d3d1a6afSToby Isaac 
49564b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
49574b2f2278SToby Isaac       if (!bSecDof) {
49584b2f2278SToby Isaac         continue;
49594b2f2278SToby Isaac       }
4960d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4961d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4962d3d1a6afSToby Isaac       }
4963d3d1a6afSToby Isaac       if (bDof) {
4964d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
4965d3d1a6afSToby Isaac 
4966d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
49674acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, indices);CHKERRQ(ierr);
4968d3d1a6afSToby Isaac 
4969d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
4970d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4971d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
4972d3d1a6afSToby Isaac 
4973d3d1a6afSToby 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 */
4974d3d1a6afSToby Isaac 
4975d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
4976d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
4977302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
49784acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, newIndices);CHKERRQ(ierr);
4979d3d1a6afSToby Isaac         }
4980d3d1a6afSToby Isaac         newP += bDof;
4981d3d1a6afSToby Isaac 
4982d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
49836ecaa68aSToby Isaac         if (outValues) {
4984d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
4985d3d1a6afSToby Isaac         }
49866ecaa68aSToby Isaac       }
4987d3d1a6afSToby Isaac       else {
4988d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
4989d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
4990d3d1a6afSToby Isaac         newP++;
4991d3d1a6afSToby Isaac       }
4992d3d1a6afSToby Isaac     }
4993d3d1a6afSToby Isaac   }
4994d3d1a6afSToby Isaac 
49956ecaa68aSToby Isaac   if (outValues) {
49966ecaa68aSToby Isaac     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
4997d3d1a6afSToby Isaac     ierr = PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));CHKERRQ(ierr);
4998d3d1a6afSToby Isaac     /* multiply constraints on the right */
4999d3d1a6afSToby Isaac     if (numFields) {
5000d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
5001d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
5002d3d1a6afSToby Isaac 
5003d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5004d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
5005d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5006d3d1a6afSToby Isaac           PetscInt c, r, k;
5007d3d1a6afSToby Isaac           PetscInt dof;
5008d3d1a6afSToby Isaac 
5009d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
50104b2f2278SToby Isaac           if (!dof) {
50114b2f2278SToby Isaac             continue;
50124b2f2278SToby Isaac           }
5013d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5014d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
5015d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
5016d3d1a6afSToby Isaac 
5017d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5018d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
5019d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
50204acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
5021d3d1a6afSToby Isaac                 }
5022d3d1a6afSToby Isaac               }
5023d3d1a6afSToby Isaac             }
5024d3d1a6afSToby Isaac           }
5025d3d1a6afSToby Isaac           else {
5026d3d1a6afSToby Isaac             /* copy this column as is */
5027d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5028d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
5029d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5030d3d1a6afSToby Isaac               }
5031d3d1a6afSToby Isaac             }
5032d3d1a6afSToby Isaac           }
5033d3d1a6afSToby Isaac           oldOff += dof;
5034d3d1a6afSToby Isaac         }
5035d3d1a6afSToby Isaac       }
5036d3d1a6afSToby Isaac     }
5037d3d1a6afSToby Isaac     else {
5038d3d1a6afSToby Isaac       PetscInt oldOff = 0;
5039d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5040d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
5041d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
5042d3d1a6afSToby Isaac         PetscInt c, r, k;
5043d3d1a6afSToby Isaac         PetscInt dof;
5044d3d1a6afSToby Isaac 
5045d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
50464b2f2278SToby Isaac         if (!dof) {
50474b2f2278SToby Isaac           continue;
50484b2f2278SToby Isaac         }
5049d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5050d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5051d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
5052d3d1a6afSToby Isaac 
5053d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5054d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
5055d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
5056d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5057d3d1a6afSToby Isaac               }
5058d3d1a6afSToby Isaac             }
5059d3d1a6afSToby Isaac           }
5060d3d1a6afSToby Isaac         }
5061d3d1a6afSToby Isaac         else {
5062d3d1a6afSToby Isaac           /* copy this column as is */
5063d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5064d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
5065d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5066d3d1a6afSToby Isaac             }
5067d3d1a6afSToby Isaac           }
5068d3d1a6afSToby Isaac         }
5069d3d1a6afSToby Isaac         oldOff += dof;
5070d3d1a6afSToby Isaac       }
5071d3d1a6afSToby Isaac     }
5072d3d1a6afSToby Isaac 
50736ecaa68aSToby Isaac     if (multiplyLeft) {
50746ecaa68aSToby Isaac       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
5075d3d1a6afSToby Isaac       ierr = PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));CHKERRQ(ierr);
5076d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
5077d3d1a6afSToby Isaac       if (numFields) {
5078d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5079d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
5080d3d1a6afSToby Isaac 
5081d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
5082d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
5083d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
5084d3d1a6afSToby Isaac             PetscInt c, r, k;
5085d3d1a6afSToby Isaac             PetscInt dof;
5086d3d1a6afSToby Isaac 
5087d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
5088d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5089d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5090d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
5091d3d1a6afSToby Isaac 
5092d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
5093d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5094d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
5095d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5096d3d1a6afSToby Isaac                   }
5097d3d1a6afSToby Isaac                 }
5098d3d1a6afSToby Isaac               }
5099d3d1a6afSToby Isaac             }
5100d3d1a6afSToby Isaac             else {
5101d3d1a6afSToby Isaac               /* copy this row as is */
5102d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
5103d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5104d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5105d3d1a6afSToby Isaac                 }
5106d3d1a6afSToby Isaac               }
5107d3d1a6afSToby Isaac             }
5108d3d1a6afSToby Isaac             oldOff += dof;
5109d3d1a6afSToby Isaac           }
5110d3d1a6afSToby Isaac         }
5111d3d1a6afSToby Isaac       }
5112d3d1a6afSToby Isaac       else {
5113d3d1a6afSToby Isaac         PetscInt oldOff = 0;
5114d3d1a6afSToby Isaac 
5115d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5116d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
5117d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5118d3d1a6afSToby Isaac           PetscInt c, r, k;
5119d3d1a6afSToby Isaac           PetscInt dof;
5120d3d1a6afSToby Isaac 
5121d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
5122d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5123d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5124d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5125d3d1a6afSToby Isaac 
5126d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5127d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5128d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5129d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5130d3d1a6afSToby Isaac                 }
5131d3d1a6afSToby Isaac               }
5132d3d1a6afSToby Isaac             }
5133d3d1a6afSToby Isaac           }
5134d3d1a6afSToby Isaac           else {
5135d3d1a6afSToby Isaac             /* copy this row as is */
51369fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5137d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5138d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5139d3d1a6afSToby Isaac               }
5140d3d1a6afSToby Isaac             }
5141d3d1a6afSToby Isaac           }
5142d3d1a6afSToby Isaac           oldOff += dof;
5143d3d1a6afSToby Isaac         }
5144d3d1a6afSToby Isaac       }
5145d3d1a6afSToby Isaac 
51466ecaa68aSToby Isaac       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
51476ecaa68aSToby Isaac     }
51486ecaa68aSToby Isaac     else {
51496ecaa68aSToby Isaac       newValues = tmpValues;
51506ecaa68aSToby Isaac     }
51516ecaa68aSToby Isaac   }
51526ecaa68aSToby Isaac 
5153d3d1a6afSToby Isaac   /* clean up */
5154d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
5155d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
51566ecaa68aSToby Isaac 
5157d3d1a6afSToby Isaac   if (numFields) {
5158d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
5159d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5160d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
5161d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5162d3d1a6afSToby Isaac     }
5163d3d1a6afSToby Isaac   }
5164d3d1a6afSToby Isaac   else {
5165d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5166d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
5167d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5168d3d1a6afSToby Isaac   }
5169d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5170d3d1a6afSToby Isaac 
5171d3d1a6afSToby Isaac   /* output */
51726ecaa68aSToby Isaac   if (outPoints) {
5173d3d1a6afSToby Isaac     *outPoints = newPoints;
51746ecaa68aSToby Isaac   }
51756ecaa68aSToby Isaac   else {
51766ecaa68aSToby Isaac     ierr = DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
51776ecaa68aSToby Isaac   }
517831620726SToby Isaac   if (outValues) {
5179d3d1a6afSToby Isaac     *outValues = newValues;
51806ecaa68aSToby Isaac   }
51816ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
5182d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
5183d3d1a6afSToby Isaac   }
5184d3d1a6afSToby Isaac   PetscFunctionReturn(0);
5185d3d1a6afSToby Isaac }
5186d3d1a6afSToby Isaac 
51877cd05799SMatthew G. Knepley /*@C
51887cd05799SMatthew G. Knepley   DMPlexGetClosureIndices - Get the indices in a vector v for all points in the closure of the given point
51897cd05799SMatthew G. Knepley 
51907cd05799SMatthew G. Knepley   Not collective
51917cd05799SMatthew G. Knepley 
51927cd05799SMatthew G. Knepley   Input Parameters:
51937cd05799SMatthew G. Knepley + dm - The DM
51947cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
51957cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
51967cd05799SMatthew G. Knepley - point - The mesh point
51977cd05799SMatthew G. Knepley 
51987cd05799SMatthew G. Knepley   Output parameters:
51997cd05799SMatthew G. Knepley + numIndices - The number of indices
52007cd05799SMatthew G. Knepley . indices - The indices
52017cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
52027cd05799SMatthew G. Knepley 
52037cd05799SMatthew G. Knepley   Note: Must call DMPlexRestoreClosureIndices() to free allocated memory
52047cd05799SMatthew G. Knepley 
52057cd05799SMatthew G. Knepley   Level: advanced
52067cd05799SMatthew G. Knepley 
52077cd05799SMatthew G. Knepley .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
52087cd05799SMatthew G. Knepley @*/
52096ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
52107773e69fSMatthew G. Knepley {
52117773e69fSMatthew G. Knepley   PetscSection    clSection;
52127773e69fSMatthew G. Knepley   IS              clPoints;
52137773e69fSMatthew G. Knepley   const PetscInt *clp;
52144acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
52157773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
52167773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
52177773e69fSMatthew G. Knepley   PetscInt        offsets[32];
52187773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
52197773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
52207773e69fSMatthew G. Knepley 
52217773e69fSMatthew G. Knepley   PetscFunctionBegin;
52227773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52237773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
52247773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
52257773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
52267773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
52277773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
52287773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
52297773e69fSMatthew G. Knepley   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
52307773e69fSMatthew G. Knepley   /* Get points in closure */
5231923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
52327773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
52337773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
52347773e69fSMatthew G. Knepley     PetscInt dof, fdof;
52357773e69fSMatthew G. Knepley 
52367773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
52377773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
52387773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
52397773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
52407773e69fSMatthew G. Knepley     }
52417773e69fSMatthew G. Knepley     Nind += dof;
52427773e69fSMatthew G. Knepley   }
52437773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
52448ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
52454acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
52464acb8e1eSToby Isaac   /* Get dual space symmetries */
52474acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
52484acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52494acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52504acb8e1eSToby Isaac   }
52517773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
52527773e69fSMatthew G. Knepley   {
52534acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
52547773e69fSMatthew G. Knepley     if (numPointsNew) {
52554acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
52564acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52574acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52584acb8e1eSToby Isaac       }
52594acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
52604acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
52614acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
52624acb8e1eSToby Isaac       }
5263923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
52647773e69fSMatthew G. Knepley       numPoints = numPointsNew;
52657773e69fSMatthew G. Knepley       Nind      = NindNew;
52667773e69fSMatthew G. Knepley       points    = pointsNew;
52677773e69fSMatthew G. Knepley     }
52687773e69fSMatthew G. Knepley   }
52697773e69fSMatthew G. Knepley   /* Calculate indices */
52707773e69fSMatthew G. Knepley   ierr = DMGetWorkArray(dm, Nind, PETSC_INT, indices);CHKERRQ(ierr);
52717773e69fSMatthew G. Knepley   if (Nf) {
52726ecaa68aSToby Isaac     if (outOffsets) {
52736ecaa68aSToby Isaac       PetscInt f;
52746ecaa68aSToby Isaac 
527546bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
52766ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
52776ecaa68aSToby Isaac       }
52786ecaa68aSToby Isaac     }
52794acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
52804acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
52814acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, *indices);
52827773e69fSMatthew G. Knepley     }
52837773e69fSMatthew G. Knepley   } else {
52844acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
52854acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
52864acb8e1eSToby Isaac 
52874acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
52884acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, *indices);
52897773e69fSMatthew G. Knepley     }
52907773e69fSMatthew G. Knepley   }
52917773e69fSMatthew G. Knepley   /* Cleanup points */
52924acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
52934acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52944acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
52954acb8e1eSToby Isaac   }
52967773e69fSMatthew G. Knepley   if (numPointsNew) {
52977773e69fSMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, PETSC_INT, &pointsNew);CHKERRQ(ierr);
52987773e69fSMatthew G. Knepley   } else {
5299923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
53007773e69fSMatthew G. Knepley   }
53017773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
53027773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
53037773e69fSMatthew G. Knepley }
53047773e69fSMatthew G. Knepley 
53057cd05799SMatthew G. Knepley /*@C
53067cd05799SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restore the indices in a vector v for all points in the closure of the given point
53077cd05799SMatthew G. Knepley 
53087cd05799SMatthew G. Knepley   Not collective
53097cd05799SMatthew G. Knepley 
53107cd05799SMatthew G. Knepley   Input Parameters:
53117cd05799SMatthew G. Knepley + dm - The DM
53127cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
53137cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
53147cd05799SMatthew G. Knepley . point - The mesh point
53157cd05799SMatthew G. Knepley . numIndices - The number of indices
53167cd05799SMatthew G. Knepley . indices - The indices
53177cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
53187cd05799SMatthew G. Knepley 
53197cd05799SMatthew G. Knepley   Level: advanced
53207cd05799SMatthew G. Knepley 
53217cd05799SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
53227cd05799SMatthew G. Knepley @*/
532346bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
53247773e69fSMatthew G. Knepley {
53257773e69fSMatthew G. Knepley   PetscErrorCode ierr;
53267773e69fSMatthew G. Knepley 
53277773e69fSMatthew G. Knepley   PetscFunctionBegin;
53287773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53297773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
53307773e69fSMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, indices);CHKERRQ(ierr);
53317773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
53327773e69fSMatthew G. Knepley }
53337773e69fSMatthew G. Knepley 
53347f5d1fdeSMatthew G. Knepley /*@C
53357f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
53367f5d1fdeSMatthew G. Knepley 
53377f5d1fdeSMatthew G. Knepley   Not collective
53387f5d1fdeSMatthew G. Knepley 
53397f5d1fdeSMatthew G. Knepley   Input Parameters:
53407f5d1fdeSMatthew G. Knepley + dm - The DM
5341ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5342ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
53437f5d1fdeSMatthew G. Knepley . A - The matrix
53447f5d1fdeSMatthew G. Knepley . point - The sieve point in the DM
53457f5d1fdeSMatthew G. Knepley . values - The array of values
53467f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
53477f5d1fdeSMatthew G. Knepley 
53487f5d1fdeSMatthew G. Knepley   Fortran Notes:
53497f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
53507f5d1fdeSMatthew G. Knepley 
53517f5d1fdeSMatthew G. Knepley   Level: intermediate
53527f5d1fdeSMatthew G. Knepley 
53537f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
53547f5d1fdeSMatthew G. Knepley @*/
53557c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5356552f7358SJed Brown {
5357552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
53581b406b76SMatthew G. Knepley   PetscSection        clSection;
53591b406b76SMatthew G. Knepley   IS                  clPoints;
5360d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
53611b406b76SMatthew G. Knepley   const PetscInt     *clp;
5362552f7358SJed Brown   PetscInt           *indices;
5363552f7358SJed Brown   PetscInt            offsets[32];
53644acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
53654acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
5366923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
53674acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
5368d3d1a6afSToby Isaac   PetscScalar        *newValues;
5369552f7358SJed Brown   PetscErrorCode      ierr;
5370552f7358SJed Brown 
5371552f7358SJed Brown   PetscFunctionBegin;
5372552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5373ebd6d717SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
53743dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5375ebd6d717SJed Brown   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
53763dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
53773dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5378552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
537982f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5380552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5381923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5382552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5383552f7358SJed Brown     PetscInt fdof;
5384552f7358SJed Brown 
5385552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5386552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5387552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5388552f7358SJed Brown       offsets[f+1] += fdof;
5389552f7358SJed Brown     }
5390552f7358SJed Brown     numIndices += dof;
5391552f7358SJed Brown   }
53920d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
53930d644c17SKarl Rupp 
53948ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
53954acb8e1eSToby Isaac   /* Get symmetries */
53964acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
53974acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
53984acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
53994acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
54004acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
54014acb8e1eSToby Isaac 
54024acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
54034acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
54044acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
54054acb8e1eSToby Isaac 
54064acb8e1eSToby Isaac         if (!numFields) {
54074acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
54084acb8e1eSToby Isaac         } else {
54094acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
54104acb8e1eSToby Isaac         }
54114acb8e1eSToby Isaac         if (flip) {
54124acb8e1eSToby Isaac           PetscInt i, j, k;
54134acb8e1eSToby Isaac 
54144acb8e1eSToby Isaac           if (!valCopy) {
54154acb8e1eSToby Isaac             ierr = DMGetWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
54164acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
54174acb8e1eSToby Isaac             values = valCopy;
54184acb8e1eSToby Isaac           }
54194acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
5420775f7be2SToby Isaac             PetscScalar fval = flip[i];
54214acb8e1eSToby Isaac 
54224acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
54234acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
54244acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
54254acb8e1eSToby Isaac             }
54264acb8e1eSToby Isaac           }
54274acb8e1eSToby Isaac         }
54284acb8e1eSToby Isaac         foffset += fdof;
54294acb8e1eSToby Isaac       }
54304acb8e1eSToby Isaac     }
54314acb8e1eSToby Isaac   }
54324acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
5433d3d1a6afSToby Isaac   if (newNumPoints) {
54344acb8e1eSToby Isaac     if (valCopy) {
54354acb8e1eSToby Isaac       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
54364acb8e1eSToby Isaac     }
54374acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
54384acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54394acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54404acb8e1eSToby Isaac     }
54414acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
54424acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
54434acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
54444acb8e1eSToby Isaac     }
5445923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5446d3d1a6afSToby Isaac     numPoints  = newNumPoints;
5447d3d1a6afSToby Isaac     numIndices = newNumIndices;
5448d3d1a6afSToby Isaac     points     = newPoints;
5449d3d1a6afSToby Isaac     values     = newValues;
5450d3d1a6afSToby Isaac   }
5451552f7358SJed Brown   ierr = DMGetWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5452552f7358SJed Brown   if (numFields) {
54534acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
54544acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
54554acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, indices);
5456552f7358SJed Brown     }
5457552f7358SJed Brown   } else {
54584acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
54594acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
54604acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
54614acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, indices);
5462552f7358SJed Brown     }
5463552f7358SJed Brown   }
5464b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
5465552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
5466ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
5467ab1d0545SMatthew G. Knepley     PetscInt i;
5468ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
54698ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
5470ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5471ab1d0545SMatthew G. Knepley   }
5472552f7358SJed Brown   if (ierr) {
5473552f7358SJed Brown     PetscMPIInt    rank;
5474552f7358SJed Brown     PetscErrorCode ierr2;
5475552f7358SJed Brown 
547682f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5477e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5478b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
54796a415b8fSMatthew G Knepley     ierr2 = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr2);
5480552f7358SJed Brown     CHKERRQ(ierr);
5481552f7358SJed Brown   }
54824acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
54834acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54844acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54854acb8e1eSToby Isaac   }
5486d3d1a6afSToby Isaac   if (newNumPoints) {
5487d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
5488d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
5489d3d1a6afSToby Isaac   }
5490d3d1a6afSToby Isaac   else {
54914acb8e1eSToby Isaac     if (valCopy) {
54924acb8e1eSToby Isaac       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
54934acb8e1eSToby Isaac     }
5494923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5495d3d1a6afSToby Isaac   }
5496552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5497552f7358SJed Brown   PetscFunctionReturn(0);
5498552f7358SJed Brown }
5499552f7358SJed Brown 
5500de41b84cSMatthew 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)
5501de41b84cSMatthew G. Knepley {
5502de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
5503de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
5504de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
5505de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
5506de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
5507de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
55084ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
5509de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
5510de41b84cSMatthew G. Knepley 
5511de41b84cSMatthew G. Knepley   PetscFunctionBegin;
5512de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
5513de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
5514de41b84cSMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
5515de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
5516de41b84cSMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
5517de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
5518de41b84cSMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
5519de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
5520de41b84cSMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
5521de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
5522de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
5523de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
5524de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5525de41b84cSMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5526de41b84cSMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5527de41b84cSMatthew G. Knepley   /* Column indices */
5528de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
55294ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
5530de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
5531de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
5532de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
5533de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
5534de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
5535de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
5536de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
5537de41b84cSMatthew G. Knepley       ++q;
5538de41b84cSMatthew G. Knepley     }
5539de41b84cSMatthew G. Knepley   }
5540de41b84cSMatthew G. Knepley   numCPoints = q;
5541de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
5542de41b84cSMatthew G. Knepley     PetscInt fdof;
5543de41b84cSMatthew G. Knepley 
5544de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
55454ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5546de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5547de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
5548de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
5549de41b84cSMatthew G. Knepley     }
5550de41b84cSMatthew G. Knepley     numCIndices += dof;
5551de41b84cSMatthew G. Knepley   }
5552de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
5553de41b84cSMatthew G. Knepley   /* Row indices */
5554de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
5555de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
55561ba5f902SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
5557de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
5558de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
5559de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5560de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
5561de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
5562de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
5563de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
55644ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
55654ca5e9f5SMatthew G. Knepley         if (!dof) continue;
55664ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
55674ca5e9f5SMatthew G. Knepley         if (s < q) continue;
5568de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
5569de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
5570de41b84cSMatthew G. Knepley         ++q;
5571de41b84cSMatthew G. Knepley       }
5572de41b84cSMatthew G. Knepley     }
5573de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5574de41b84cSMatthew G. Knepley   }
5575de41b84cSMatthew G. Knepley   numFPoints = q;
5576de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
5577de41b84cSMatthew G. Knepley     PetscInt fdof;
5578de41b84cSMatthew G. Knepley 
5579de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
55804ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5581de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5582de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
5583de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
5584de41b84cSMatthew G. Knepley     }
5585de41b84cSMatthew G. Knepley     numFIndices += dof;
5586de41b84cSMatthew G. Knepley   }
5587de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
5588de41b84cSMatthew G. Knepley 
55898ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
55908ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
5591de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
5592de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
5593de41b84cSMatthew G. Knepley   if (numFields) {
55944acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
55954acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
55964acb8e1eSToby Isaac 
55974acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
55984acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
55994acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5600de41b84cSMatthew G. Knepley     }
56014acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
56024acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
56034acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
56044acb8e1eSToby Isaac     }
56054acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
56064acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
56074acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
56084acb8e1eSToby Isaac     }
56094acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
56104acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
56114acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5612de41b84cSMatthew G. Knepley     }
5613de41b84cSMatthew G. Knepley   } else {
56144acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
56154acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
56164acb8e1eSToby Isaac 
56174acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
56184acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
56194acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
56204acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
56214acb8e1eSToby Isaac 
56224acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
56234acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);CHKERRQ(ierr);
5624de41b84cSMatthew G. Knepley     }
56254acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
56264acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
56274acb8e1eSToby Isaac 
56284acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
56294acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);CHKERRQ(ierr);
5630de41b84cSMatthew G. Knepley     }
56314acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
56324acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
5633de41b84cSMatthew G. Knepley   }
5634de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
56354acb8e1eSToby Isaac   /* TODO: flips */
5636de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
5637de41b84cSMatthew G. Knepley   if (ierr) {
5638de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
5639de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
5640de41b84cSMatthew G. Knepley 
5641de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5642e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5643de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
5644de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr2);
5645de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr2);
5646de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
5647de41b84cSMatthew G. Knepley   }
5648549a8adaSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
5649de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
5650de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
5651de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
5652de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
5653de41b84cSMatthew G. Knepley }
5654de41b84cSMatthew G. Knepley 
56557c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
56567c927364SMatthew G. Knepley {
56577c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
56587c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
56597c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
56607c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
56617c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
56627c927364SMatthew G. Knepley   PetscErrorCode ierr;
56637c927364SMatthew G. Knepley 
56647c927364SMatthew G. Knepley   PetscFunctionBegin;
56657c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
56667c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
56677c927364SMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
56687c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
56697c927364SMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
56707c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
56717c927364SMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
56727c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
56737c927364SMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
56747c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
56757c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
56767c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
56777c927364SMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
56787c927364SMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
56797c927364SMatthew G. Knepley   /* Column indices */
56807c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
56817c927364SMatthew G. Knepley   maxFPoints = numCPoints;
56827c927364SMatthew G. Knepley   /* Compress out points not in the section */
56837c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
56847c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
56857c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
56867c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
56877c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
56887c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
56897c927364SMatthew G. Knepley       ++q;
56907c927364SMatthew G. Knepley     }
56917c927364SMatthew G. Knepley   }
56927c927364SMatthew G. Knepley   numCPoints = q;
56937c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
56947c927364SMatthew G. Knepley     PetscInt fdof;
56957c927364SMatthew G. Knepley 
56967c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
56977c927364SMatthew G. Knepley     if (!dof) continue;
56987c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
56997c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
57007c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
57017c927364SMatthew G. Knepley     }
57027c927364SMatthew G. Knepley     numCIndices += dof;
57037c927364SMatthew G. Knepley   }
57047c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
57057c927364SMatthew G. Knepley   /* Row indices */
57067c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
57077c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
57087c927364SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
57097c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
57107c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
57117c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
57127c927364SMatthew G. Knepley     /* Compress out points not in the section */
57137c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
57147c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
57157c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
57167c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
57177c927364SMatthew G. Knepley         if (!dof) continue;
57187c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
57197c927364SMatthew G. Knepley         if (s < q) continue;
57207c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
57217c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
57227c927364SMatthew G. Knepley         ++q;
57237c927364SMatthew G. Knepley       }
57247c927364SMatthew G. Knepley     }
57257c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
57267c927364SMatthew G. Knepley   }
57277c927364SMatthew G. Knepley   numFPoints = q;
57287c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
57297c927364SMatthew G. Knepley     PetscInt fdof;
57307c927364SMatthew G. Knepley 
57317c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
57327c927364SMatthew G. Knepley     if (!dof) continue;
57337c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
57347c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
57357c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
57367c927364SMatthew G. Knepley     }
57377c927364SMatthew G. Knepley     numFIndices += dof;
57387c927364SMatthew G. Knepley   }
57397c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
57407c927364SMatthew G. Knepley 
57418ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
57428ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
57437c927364SMatthew G. Knepley   if (numFields) {
57444acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
57454acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
57464acb8e1eSToby Isaac 
57474acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
57484acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
57494acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
57507c927364SMatthew G. Knepley     }
57514acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
57524acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
57534acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
57544acb8e1eSToby Isaac     }
57554acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
57564acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
57574acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
57584acb8e1eSToby Isaac     }
57594acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
57604acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
57614acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
57627c927364SMatthew G. Knepley     }
57637c927364SMatthew G. Knepley   } else {
57644acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
57654acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
57664acb8e1eSToby Isaac 
57674acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
57684acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
57694acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
57704acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
57714acb8e1eSToby Isaac 
57724acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
57734acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);
57747c927364SMatthew G. Knepley     }
57754acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
57764acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
57774acb8e1eSToby Isaac 
57784acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
57794acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);
57807c927364SMatthew G. Knepley     }
57814acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
57824acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
57837c927364SMatthew G. Knepley   }
57847c927364SMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
57857c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
57867c927364SMatthew G. Knepley   PetscFunctionReturn(0);
57877c927364SMatthew G. Knepley }
57887c927364SMatthew G. Knepley 
5789f96281f8SMatthew G. Knepley /*@
5790f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
5791f96281f8SMatthew G. Knepley 
5792f96281f8SMatthew G. Knepley   Input Parameter:
5793f96281f8SMatthew G. Knepley . dm - The DMPlex object
5794f96281f8SMatthew G. Knepley 
5795f96281f8SMatthew G. Knepley   Output Parameters:
5796f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
5797dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5798dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5799dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5800f96281f8SMatthew G. Knepley 
5801f96281f8SMatthew G. Knepley   Level: developer
5802f96281f8SMatthew G. Knepley 
5803dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
5804f96281f8SMatthew G. Knepley @*/
5805770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
5806552f7358SJed Brown {
5807552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5808770b213bSMatthew G Knepley   PetscInt       dim;
5809770b213bSMatthew G Knepley   PetscErrorCode ierr;
5810552f7358SJed Brown 
5811552f7358SJed Brown   PetscFunctionBegin;
5812552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5813c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5814770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
5815770b213bSMatthew G Knepley   if (fMax) *fMax = mesh->hybridPointMax[dim-1];
5816770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
5817770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
5818552f7358SJed Brown   PetscFunctionReturn(0);
5819552f7358SJed Brown }
5820552f7358SJed Brown 
5821dc5a3409SMatthew G. Knepley /*@
5822dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
5823dc5a3409SMatthew G. Knepley 
5824dc5a3409SMatthew G. Knepley   Input Parameters:
5825dc5a3409SMatthew G. Knepley . dm   - The DMPlex object
5826dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
5827dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5828dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5829dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5830dc5a3409SMatthew G. Knepley 
5831dc5a3409SMatthew G. Knepley   Level: developer
5832dc5a3409SMatthew G. Knepley 
5833dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
5834dc5a3409SMatthew G. Knepley @*/
5835770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
5836552f7358SJed Brown {
5837552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5838770b213bSMatthew G Knepley   PetscInt       dim;
5839770b213bSMatthew G Knepley   PetscErrorCode ierr;
5840552f7358SJed Brown 
5841552f7358SJed Brown   PetscFunctionBegin;
5842552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5843c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5844770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]   = cMax;
5845770b213bSMatthew G Knepley   if (fMax >= 0) mesh->hybridPointMax[dim-1] = fMax;
5846770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]     = eMax;
5847770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]     = vMax;
5848552f7358SJed Brown   PetscFunctionReturn(0);
5849552f7358SJed Brown }
5850552f7358SJed Brown 
58517cd05799SMatthew G. Knepley /*@C
58527cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
58537cd05799SMatthew G. Knepley 
58547cd05799SMatthew G. Knepley   Input Parameter:
58557cd05799SMatthew G. Knepley . dm   - The DMPlex object
58567cd05799SMatthew G. Knepley 
58577cd05799SMatthew G. Knepley   Output Parameter:
58587cd05799SMatthew G. Knepley . cellHeight - The height of a cell
58597cd05799SMatthew G. Knepley 
58607cd05799SMatthew G. Knepley   Level: developer
58617cd05799SMatthew G. Knepley 
58627cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
58637cd05799SMatthew G. Knepley @*/
5864552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
5865552f7358SJed Brown {
5866552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5867552f7358SJed Brown 
5868552f7358SJed Brown   PetscFunctionBegin;
5869552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5870552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
5871552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
5872552f7358SJed Brown   PetscFunctionReturn(0);
5873552f7358SJed Brown }
5874552f7358SJed Brown 
58757cd05799SMatthew G. Knepley /*@C
58767cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
58777cd05799SMatthew G. Knepley 
58787cd05799SMatthew G. Knepley   Input Parameters:
58797cd05799SMatthew G. Knepley + dm   - The DMPlex object
58807cd05799SMatthew G. Knepley - cellHeight - The height of a cell
58817cd05799SMatthew G. Knepley 
58827cd05799SMatthew G. Knepley   Level: developer
58837cd05799SMatthew G. Knepley 
58847cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
58857cd05799SMatthew G. Knepley @*/
5886552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
5887552f7358SJed Brown {
5888552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5889552f7358SJed Brown 
5890552f7358SJed Brown   PetscFunctionBegin;
5891552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5892552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
5893552f7358SJed Brown   PetscFunctionReturn(0);
5894552f7358SJed Brown }
5895552f7358SJed Brown 
5896552f7358SJed Brown /* We can easily have a form that takes an IS instead */
5897ef48cebcSMatthew G. Knepley static PetscErrorCode DMPlexCreateNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
5898552f7358SJed Brown {
5899552f7358SJed Brown   PetscSection   section, globalSection;
5900552f7358SJed Brown   PetscInt      *numbers, p;
5901552f7358SJed Brown   PetscErrorCode ierr;
5902552f7358SJed Brown 
5903552f7358SJed Brown   PetscFunctionBegin;
590482f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
5905552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
5906552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5907552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
5908552f7358SJed Brown   }
5909552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
591015b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
5911854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
5912552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5913552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
5914ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
5915ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
5916552f7358SJed Brown   }
591782f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
5918ef48cebcSMatthew G. Knepley   if (globalSize) {
5919ef48cebcSMatthew G. Knepley     PetscLayout layout;
5920ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
5921ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
5922ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
5923ef48cebcSMatthew G. Knepley   }
5924552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
5925552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
5926552f7358SJed Brown   PetscFunctionReturn(0);
5927552f7358SJed Brown }
5928552f7358SJed Brown 
592981ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
5930552f7358SJed Brown {
5931552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
5932552f7358SJed Brown   PetscErrorCode ierr;
5933552f7358SJed Brown 
5934552f7358SJed Brown   PetscFunctionBegin;
5935552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
5936552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
59370298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
593881ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
593981ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
594081ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
5941552f7358SJed Brown }
594281ed3555SMatthew G. Knepley 
59437cd05799SMatthew G. Knepley /*@C
59447cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
59457cd05799SMatthew G. Knepley 
59467cd05799SMatthew G. Knepley   Input Parameter:
59477cd05799SMatthew G. Knepley . dm   - The DMPlex object
59487cd05799SMatthew G. Knepley 
59497cd05799SMatthew G. Knepley   Output Parameter:
59507cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
59517cd05799SMatthew G. Knepley 
59527cd05799SMatthew G. Knepley   Level: developer
59537cd05799SMatthew G. Knepley 
59547cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
59557cd05799SMatthew G. Knepley @*/
595681ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
595781ed3555SMatthew G. Knepley {
595881ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
595981ed3555SMatthew G. Knepley   PetscErrorCode ierr;
596081ed3555SMatthew G. Knepley 
596181ed3555SMatthew G. Knepley   PetscFunctionBegin;
596281ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
596381ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
5964552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
5965552f7358SJed Brown   PetscFunctionReturn(0);
5966552f7358SJed Brown }
5967552f7358SJed Brown 
596881ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
596981ed3555SMatthew G. Knepley {
597081ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
597181ed3555SMatthew G. Knepley   PetscErrorCode ierr;
597281ed3555SMatthew G. Knepley 
597381ed3555SMatthew G. Knepley   PetscFunctionBegin;
597481ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
597581ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
597681ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
597781ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
597881ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
597981ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
598081ed3555SMatthew G. Knepley }
598181ed3555SMatthew G. Knepley 
59827cd05799SMatthew G. Knepley /*@C
59837cd05799SMatthew G. Knepley   DMPlexGetVertexNumbering - Get a global certex numbering for all vertices on this process
59847cd05799SMatthew G. Knepley 
59857cd05799SMatthew G. Knepley   Input Parameter:
59867cd05799SMatthew G. Knepley . dm   - The DMPlex object
59877cd05799SMatthew G. Knepley 
59887cd05799SMatthew G. Knepley   Output Parameter:
59897cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
59907cd05799SMatthew G. Knepley 
59917cd05799SMatthew G. Knepley   Level: developer
59927cd05799SMatthew G. Knepley 
59937cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
59947cd05799SMatthew G. Knepley @*/
5995552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
5996552f7358SJed Brown {
5997552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5998552f7358SJed Brown   PetscErrorCode ierr;
5999552f7358SJed Brown 
6000552f7358SJed Brown   PetscFunctionBegin;
6001552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
600281ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
6003552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
6004552f7358SJed Brown   PetscFunctionReturn(0);
6005552f7358SJed Brown }
6006552f7358SJed Brown 
60077cd05799SMatthew G. Knepley /*@C
60087cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
60097cd05799SMatthew G. Knepley 
60107cd05799SMatthew G. Knepley   Input Parameter:
60117cd05799SMatthew G. Knepley . dm   - The DMPlex object
60127cd05799SMatthew G. Knepley 
60137cd05799SMatthew G. Knepley   Output Parameter:
60147cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
60157cd05799SMatthew G. Knepley 
60167cd05799SMatthew G. Knepley   Level: developer
60177cd05799SMatthew G. Knepley 
60187cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
60197cd05799SMatthew G. Knepley @*/
6020ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
6021ef48cebcSMatthew G. Knepley {
6022ef48cebcSMatthew G. Knepley   IS             nums[4];
6023ef48cebcSMatthew G. Knepley   PetscInt       depths[4];
6024ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
6025ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
6026ef48cebcSMatthew G. Knepley 
6027ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
6028ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6029ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
60308abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
60318abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
6032ef48cebcSMatthew G. Knepley   depths[0] = depth; depths[1] = 0;
6033ef48cebcSMatthew G. Knepley   for (d = 2; d <= depth; ++d) depths[d] = depth-d+1;
6034ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
6035ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
6036ef48cebcSMatthew G. Knepley 
6037ef48cebcSMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, depths[d], &pStart, &pEnd);CHKERRQ(ierr);
6038ef48cebcSMatthew G. Knepley     ierr = DMPlexCreateNumbering_Private(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
6039ef48cebcSMatthew G. Knepley     shift += gsize;
6040ef48cebcSMatthew G. Knepley   }
6041302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
6042ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
6043ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
6044ef48cebcSMatthew G. Knepley }
6045ef48cebcSMatthew G. Knepley 
6046ca8062c8SMatthew G. Knepley /*@
6047ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
6048ca8062c8SMatthew G. Knepley 
6049ca8062c8SMatthew G. Knepley   Input Parameters:
6050ca8062c8SMatthew G. Knepley   + dm - The DMPlex object
6051ca8062c8SMatthew G. Knepley 
6052ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6053ca8062c8SMatthew G. Knepley 
6054ca8062c8SMatthew G. Knepley   Level: developer
6055ca8062c8SMatthew G. Knepley 
60569bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSkeleton(), DMCheckFaces()
6057ca8062c8SMatthew G. Knepley @*/
6058ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
6059ca8062c8SMatthew G. Knepley {
6060ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
6061ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
6062ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
6063ca8062c8SMatthew G. Knepley   PetscInt        pStart, pEnd, p, csize, ssize;
6064ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
6065ca8062c8SMatthew G. Knepley 
6066ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6067ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6068ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
6069ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
6070ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
6071ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
6072ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
6073ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
6074ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
6075ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
607642e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
607742e66dfaSMatthew G. Knepley       PetscInt  d;
607842e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
607942e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
608042e66dfaSMatthew G. Knepley       }
6081ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
6082ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
6083ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
6084ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
6085ca8062c8SMatthew G. Knepley       }
608642e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
60878ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
6088ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
60898ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
6090ca8062c8SMatthew G. Knepley         }
6091302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
60928ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
6093ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
60948ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
6095ca8062c8SMatthew G. Knepley         }
6096302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
60978ccfff9cSToby 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]);
60988ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
6099ca8062c8SMatthew G. Knepley       }
610042e66dfaSMatthew G. Knepley     }
6101ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
6102ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
6103ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
6104ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
6105ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
6106ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
6107ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
6108ca8062c8SMatthew G. Knepley       }
6109ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
61108ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
6111ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
61128ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
6113ca8062c8SMatthew G. Knepley         }
6114302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
61158ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
6116ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
61178ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
6118ca8062c8SMatthew G. Knepley         }
6119302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
61208ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
6121ca8062c8SMatthew G. Knepley       }
6122ca8062c8SMatthew G. Knepley     }
6123ca8062c8SMatthew G. Knepley   }
6124ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
6125ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
61268ccfff9cSToby Isaac   if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
6127ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6128ca8062c8SMatthew G. Knepley }
6129ca8062c8SMatthew G. Knepley 
6130ca8062c8SMatthew G. Knepley /*@
6131ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
6132ca8062c8SMatthew G. Knepley 
6133ca8062c8SMatthew G. Knepley   Input Parameters:
6134ca8062c8SMatthew G. Knepley + dm - The DMPlex object
613558723a97SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
613658723a97SMatthew G. Knepley - cellHeight - Normally 0
6137ca8062c8SMatthew G. Knepley 
6138ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6139ca8062c8SMatthew G. Knepley 
6140ca8062c8SMatthew G. Knepley   Level: developer
6141ca8062c8SMatthew G. Knepley 
61429bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckFaces()
6143ca8062c8SMatthew G. Knepley @*/
614458723a97SMatthew G. Knepley PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscBool isSimplex, PetscInt cellHeight)
6145ca8062c8SMatthew G. Knepley {
614642363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
6147ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
6148ca8062c8SMatthew G. Knepley 
6149ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6150ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6151c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6152ca8062c8SMatthew G. Knepley   switch (dim) {
615342363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
615442363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
615542363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
6156ca8062c8SMatthew G. Knepley   default:
61578ccfff9cSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
6158ca8062c8SMatthew G. Knepley   }
615958723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
616058723a97SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
6161ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
6162ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
6163ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
616458723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
616558723a97SMatthew G. Knepley 
616658723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
616758723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
616858723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
616958723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
617058723a97SMatthew G. Knepley     }
617158723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
61728ccfff9cSToby Isaac     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
6173ca8062c8SMatthew G. Knepley   }
617442363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
617542363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
617642363296SMatthew G. Knepley 
617742363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
617842363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
617942363296SMatthew G. Knepley       const PetscInt p = closure[cl];
618042363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
618142363296SMatthew G. Knepley     }
618242363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
61838ccfff9cSToby Isaac     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
618442363296SMatthew G. Knepley   }
6185ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6186ca8062c8SMatthew G. Knepley }
61879bf0dad6SMatthew G. Knepley 
61889bf0dad6SMatthew G. Knepley /*@
61899bf0dad6SMatthew 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
61909bf0dad6SMatthew G. Knepley 
61919bf0dad6SMatthew G. Knepley   Input Parameters:
61929bf0dad6SMatthew G. Knepley + dm - The DMPlex object
61939bf0dad6SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
61949bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
61959bf0dad6SMatthew G. Knepley 
61969bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
61979bf0dad6SMatthew G. Knepley 
61989bf0dad6SMatthew G. Knepley   Level: developer
61999bf0dad6SMatthew G. Knepley 
62009bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckSkeleton()
62019bf0dad6SMatthew G. Knepley @*/
62029bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexCheckFaces(DM dm, PetscBool isSimplex, PetscInt cellHeight)
62039bf0dad6SMatthew G. Knepley {
62043554e41dSMatthew G. Knepley   PetscInt       pMax[4];
62053554e41dSMatthew G. Knepley   PetscInt       dim, vStart, vEnd, cStart, cEnd, c, h;
62069bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
62079bf0dad6SMatthew G. Knepley 
62089bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
62099bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6210c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
62119bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
621225abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
62133554e41dSMatthew G. Knepley   for (h = cellHeight; h < dim; ++h) {
62143554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
62153554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
62169bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
62179bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
62189bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
62199bf0dad6SMatthew G. Knepley 
622025abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
62219bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
62229bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
62239bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
62249bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
62259bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
62269bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
62279bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
62289bf0dad6SMatthew G. Knepley       }
62293554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
62308ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
62319bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
62329bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
62339bf0dad6SMatthew G. Knepley 
62349bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
62359bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
62369bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
62379bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
62389bf0dad6SMatthew G. Knepley         }
62398ccfff9cSToby 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);
62409bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
62418ccfff9cSToby 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]);
62429bf0dad6SMatthew G. Knepley         }
62439bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
62449bf0dad6SMatthew G. Knepley       }
62459bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
62469bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
62479bf0dad6SMatthew G. Knepley     }
62483554e41dSMatthew G. Knepley   }
6249552f7358SJed Brown   PetscFunctionReturn(0);
6250552f7358SJed Brown }
62513913d7c8SMatthew G. Knepley 
6252bceba477SMatthew G. Knepley /* Pointwise interpolation
6253bceba477SMatthew G. Knepley      Just code FEM for now
6254bceba477SMatthew G. Knepley      u^f = I u^c
62554ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
62564ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
62574ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
6258bceba477SMatthew G. Knepley */
6259bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
6260bceba477SMatthew G. Knepley {
6261bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
6262bceba477SMatthew G. Knepley   PetscInt       m, n;
6263a063dac3SMatthew G. Knepley   void          *ctx;
626468132eb9SMatthew G. Knepley   DM             cdm;
626568132eb9SMatthew G. Knepley   PetscBool      regular;
6266bceba477SMatthew G. Knepley   PetscErrorCode ierr;
6267bceba477SMatthew G. Knepley 
6268bceba477SMatthew G. Knepley   PetscFunctionBegin;
6269bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6270bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6271bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6272bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
627368132eb9SMatthew G. Knepley 
6274bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
6275bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6276bceba477SMatthew G. Knepley   ierr = MatSetType(*interpolation, dmCoarse->mattype);CHKERRQ(ierr);
6277a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
627868132eb9SMatthew G. Knepley 
6279a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
628068132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
628168132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
628268132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
628368132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
62845d1c2e58SMatthew G. Knepley   /* Use naive scaling */
62855d1c2e58SMatthew G. Knepley   ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
6286a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
6287a063dac3SMatthew G. Knepley }
6288bceba477SMatthew G. Knepley 
62896dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
6290a063dac3SMatthew G. Knepley {
629190748bafSMatthew G. Knepley   PetscErrorCode ierr;
62926dbf9973SLawrence Mitchell   VecScatter     ctx;
629390748bafSMatthew G. Knepley 
6294a063dac3SMatthew G. Knepley   PetscFunctionBegin;
62956dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
62966dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
62976dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
6298bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6299bceba477SMatthew G. Knepley }
6300bceba477SMatthew G. Knepley 
6301fd59a867SMatthew G. Knepley PetscErrorCode DMCreateDefaultSection_Plex(DM dm)
6302fd59a867SMatthew G. Knepley {
6303fd59a867SMatthew G. Knepley   PetscSection   section;
6304ab1d0545SMatthew G. Knepley   IS            *bcPoints, *bcComps;
6305ebb3236fSMatthew G. Knepley   PetscBool     *isFE;
6306286d8613SMatthew G. Knepley   PetscInt      *bcFields, *numComp, *numDof;
6307ebb3236fSMatthew G. Knepley   PetscInt       depth, dim, numBd, numBC = 0, numFields, bd, bc = 0, f;
6308ebb3236fSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
6309fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
6310fd59a867SMatthew G. Knepley 
6311fd59a867SMatthew G. Knepley   PetscFunctionBegin;
6312ebb3236fSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
6313ae71db08SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
6314ebb3236fSMatthew G. Knepley   /* FE and FV boundary conditions are handled slightly differently */
6315ebb3236fSMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
6316ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6317ebb3236fSMatthew G. Knepley     PetscObject  obj;
6318ebb3236fSMatthew G. Knepley     PetscClassId id;
6319ebb3236fSMatthew G. Knepley 
6320ebb3236fSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6321ebb3236fSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
6322ebb3236fSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
6323ebb3236fSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
63248ccfff9cSToby Isaac     else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
6325ebb3236fSMatthew G. Knepley   }
63262f900235SMatthew G. Knepley   /* Allocate boundary point storage for FEM boundaries */
6327286d8613SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
6328c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6329ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
6330ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
6331dff059c6SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob, &numBd);CHKERRQ(ierr);
6332fd59a867SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
63332f900235SMatthew G. Knepley     PetscInt                field;
6334f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6335385532a5SToby Isaac     const char             *labelName;
6336385532a5SToby Isaac     DMLabel                 label;
63372f900235SMatthew G. Knepley 
6338f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &labelName, &field, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
6339385532a5SToby Isaac     ierr = DMGetLabel(dm,labelName,&label);CHKERRQ(ierr);
6340f971fd6bSMatthew G. Knepley     if (label && isFE[field] && (type & DM_BC_ESSENTIAL)) ++numBC;
6341fd59a867SMatthew G. Knepley   }
63422f900235SMatthew G. Knepley   /* Add ghost cell boundaries for FVM */
6343ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) if (!isFE[f] && cEndInterior >= 0) ++numBC;
63446c8d74c4SMatthew G. Knepley   ierr = PetscCalloc3(numBC,&bcFields,numBC,&bcPoints,numBC,&bcComps);CHKERRQ(ierr);
6345ebb3236fSMatthew G. Knepley   /* Constrain ghost cells for FV */
6346ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6347ebb3236fSMatthew G. Knepley     PetscInt *newidx, c;
6348ebb3236fSMatthew G. Knepley 
6349ebb3236fSMatthew G. Knepley     if (isFE[f] || cEndInterior < 0) continue;
6350ebb3236fSMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cEndInterior,&newidx);CHKERRQ(ierr);
6351ebb3236fSMatthew G. Knepley     for (c = cEndInterior; c < cEnd; ++c) newidx[c-cEndInterior] = c;
6352ebb3236fSMatthew G. Knepley     bcFields[bc] = f;
6353ebb3236fSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), cEnd-cEndInterior, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6354ebb3236fSMatthew G. Knepley   }
6355ebb3236fSMatthew G. Knepley   /* Handle FEM Dirichlet boundaries */
6356ebb3236fSMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
6357fd59a867SMatthew G. Knepley     const char             *bdLabel;
6358fd59a867SMatthew G. Knepley     DMLabel                 label;
63590e0f25f2SMatthew G. Knepley     const PetscInt         *comps;
6360fd59a867SMatthew G. Knepley     const PetscInt         *values;
63610e0f25f2SMatthew G. Knepley     PetscInt                bd2, field, numComps, numValues;
6362f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6363f971fd6bSMatthew G. Knepley     PetscBool               duplicate = PETSC_FALSE;
6364fd59a867SMatthew G. Knepley 
6365f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &bdLabel, &field, &numComps, &comps, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
6366c58f1c22SToby Isaac     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
6367385532a5SToby Isaac     if (!isFE[field] || !label) continue;
6368ebb3236fSMatthew G. Knepley     /* Only want to modify label once */
63697391c1cbSMatthew G. Knepley     for (bd2 = 0; bd2 < bd; ++bd2) {
63707391c1cbSMatthew G. Knepley       const char *bdname;
6371dff059c6SToby Isaac       ierr = PetscDSGetBoundary(dm->prob, bd2, NULL, NULL, &bdname, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
63727391c1cbSMatthew G. Knepley       ierr = PetscStrcmp(bdname, bdLabel, &duplicate);CHKERRQ(ierr);
63737391c1cbSMatthew G. Knepley       if (duplicate) break;
63747391c1cbSMatthew G. Knepley     }
6375ebb3236fSMatthew G. Knepley     if (!duplicate && (isFE[field])) {
6376b0bf5782SToby Isaac       /* don't complete cells, which are just present to give orientation to the boundary */
6377092e5057SToby Isaac       ierr = DMPlexLabelComplete(dm, label);CHKERRQ(ierr);
63787391c1cbSMatthew G. Knepley     }
6379ebb3236fSMatthew G. Knepley     /* Filter out cells, if you actually want to constrain cells you need to do things by hand right now */
6380f971fd6bSMatthew G. Knepley     if (type & DM_BC_ESSENTIAL) {
638193a5981aSMatthew G. Knepley       PetscInt       *newidx;
6382ebb3236fSMatthew G. Knepley       PetscInt        n, newn = 0, p, v;
638393a5981aSMatthew G. Knepley 
6384fd59a867SMatthew G. Knepley       bcFields[bc] = field;
63850e0f25f2SMatthew G. Knepley       if (numComps) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), numComps, comps, PETSC_COPY_VALUES, &bcComps[bc]);CHKERRQ(ierr);}
6386ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6387ebb3236fSMatthew G. Knepley         IS              tmp;
6388ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6389ebb3236fSMatthew G. Knepley 
6390c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6391ebb3236fSMatthew G. Knepley         if (!tmp) continue;
639293a5981aSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
639393a5981aSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6394ebb3236fSMatthew G. Knepley         if (isFE[field]) {
639593a5981aSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) ++newn;
6396ebb3236fSMatthew G. Knepley         } else {
6397ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) ++newn;
6398ebb3236fSMatthew G. Knepley         }
639993a5981aSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
640093a5981aSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6401fd59a867SMatthew G. Knepley       }
6402ebb3236fSMatthew G. Knepley       ierr = PetscMalloc1(newn,&newidx);CHKERRQ(ierr);
6403ebb3236fSMatthew G. Knepley       newn = 0;
6404ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6405ebb3236fSMatthew G. Knepley         IS              tmp;
6406ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6407ebb3236fSMatthew G. Knepley 
6408c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6409ebb3236fSMatthew G. Knepley         if (!tmp) continue;
6410ebb3236fSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
6411ebb3236fSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6412ebb3236fSMatthew G. Knepley         if (isFE[field]) {
6413ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) newidx[newn++] = idx[p];
6414ebb3236fSMatthew G. Knepley         } else {
6415ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) newidx[newn++] = idx[p];
6416ebb3236fSMatthew G. Knepley         }
6417ebb3236fSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
6418ebb3236fSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6419ebb3236fSMatthew G. Knepley       }
6420ebb3236fSMatthew G. Knepley       ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), newn, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6421ebb3236fSMatthew G. Knepley     }
6422fd59a867SMatthew G. Knepley   }
6423fd59a867SMatthew G. Knepley   /* Handle discretization */
6424ebb3236fSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&numComp,numFields*(dim+1),&numDof);CHKERRQ(ierr);
6425fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
64260bacfa0aSMatthew G. Knepley     PetscObject obj;
64270bacfa0aSMatthew G. Knepley 
64280bacfa0aSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6429ebb3236fSMatthew G. Knepley     if (isFE[f]) {
64300bacfa0aSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
6431286d8613SMatthew G. Knepley       const PetscInt *numFieldDof;
6432fd59a867SMatthew G. Knepley       PetscInt        d;
6433fd59a867SMatthew G. Knepley 
6434fd59a867SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
6435286d8613SMatthew G. Knepley       ierr = PetscFEGetNumDof(fe, &numFieldDof);CHKERRQ(ierr);
6436286d8613SMatthew G. Knepley       for (d = 0; d < dim+1; ++d) numDof[f*(dim+1)+d] = numFieldDof[d];
6437ebb3236fSMatthew G. Knepley     } else {
64380bacfa0aSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
64390bacfa0aSMatthew G. Knepley 
64400bacfa0aSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
64410bacfa0aSMatthew G. Knepley       numDof[f*(dim+1)+dim] = numComp[f];
6442ebb3236fSMatthew G. Knepley     }
6443fd59a867SMatthew G. Knepley   }
6444286d8613SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6445286d8613SMatthew G. Knepley     PetscInt d;
6446286d8613SMatthew G. Knepley     for (d = 1; d < dim; ++d) {
6447286d8613SMatthew 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.");
6448286d8613SMatthew G. Knepley     }
6449286d8613SMatthew G. Knepley   }
6450ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSection(dm, dim, numFields, numComp, numDof, numBC, bcFields, bcComps, bcPoints, NULL, &section);CHKERRQ(ierr);
6451fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6452fd59a867SMatthew G. Knepley     PetscFE     fe;
6453fd59a867SMatthew G. Knepley     const char *name;
645418abd763SMatthew G. Knepley 
645518abd763SMatthew G. Knepley     ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
6456fd59a867SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
6457fd59a867SMatthew G. Knepley     ierr = PetscSectionSetFieldName(section, f, name);CHKERRQ(ierr);
6458fd59a867SMatthew G. Knepley   }
6459fd59a867SMatthew G. Knepley   ierr = DMSetDefaultSection(dm, section);CHKERRQ(ierr);
6460fd59a867SMatthew G. Knepley   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
64610e0f25f2SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {ierr = ISDestroy(&bcPoints[bc]);CHKERRQ(ierr);ierr = ISDestroy(&bcComps[bc]);CHKERRQ(ierr);}
64620e0f25f2SMatthew G. Knepley   ierr = PetscFree3(bcFields,bcPoints,bcComps);CHKERRQ(ierr);
6463286d8613SMatthew G. Knepley   ierr = PetscFree2(numComp,numDof);CHKERRQ(ierr);
6464ebb3236fSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
6465bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6466bceba477SMatthew G. Knepley }
6467bceba477SMatthew G. Knepley 
64680aef6b92SMatthew G. Knepley /*@
64690aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
64700aef6b92SMatthew G. Knepley 
64710aef6b92SMatthew G. Knepley   Input Parameter:
64720aef6b92SMatthew G. Knepley . dm - The DMPlex object
64730aef6b92SMatthew G. Knepley 
64740aef6b92SMatthew G. Knepley   Output Parameter:
64750aef6b92SMatthew G. Knepley . regular - The flag
64760aef6b92SMatthew G. Knepley 
64770aef6b92SMatthew G. Knepley   Level: intermediate
64780aef6b92SMatthew G. Knepley 
64790aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
64800aef6b92SMatthew G. Knepley @*/
64810aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
64820aef6b92SMatthew G. Knepley {
64830aef6b92SMatthew G. Knepley   PetscFunctionBegin;
64840aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64850aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
64860aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
64870aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
64880aef6b92SMatthew G. Knepley }
64890aef6b92SMatthew G. Knepley 
64900aef6b92SMatthew G. Knepley /*@
64910aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
64920aef6b92SMatthew G. Knepley 
64930aef6b92SMatthew G. Knepley   Input Parameters:
64940aef6b92SMatthew G. Knepley + dm - The DMPlex object
64950aef6b92SMatthew G. Knepley - regular - The flag
64960aef6b92SMatthew G. Knepley 
64970aef6b92SMatthew G. Knepley   Level: intermediate
64980aef6b92SMatthew G. Knepley 
64990aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
65000aef6b92SMatthew G. Knepley @*/
65010aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
65020aef6b92SMatthew G. Knepley {
65030aef6b92SMatthew G. Knepley   PetscFunctionBegin;
65040aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65050aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
65060aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
65070aef6b92SMatthew G. Knepley }
65080aef6b92SMatthew G. Knepley 
6509f7c74593SToby Isaac /* anchors */
6510a68b90caSToby Isaac /*@
6511f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
6512f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
6513a68b90caSToby Isaac 
6514e228b242SToby Isaac   not collective
6515a68b90caSToby Isaac 
6516a68b90caSToby Isaac   Input Parameters:
6517a68b90caSToby Isaac . dm - The DMPlex object
6518a68b90caSToby Isaac 
6519a68b90caSToby Isaac   Output Parameters:
6520a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
6521a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
6522a68b90caSToby Isaac 
6523a68b90caSToby Isaac 
6524a68b90caSToby Isaac   Level: intermediate
6525a68b90caSToby Isaac 
6526f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
6527a68b90caSToby Isaac @*/
6528a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
6529a68b90caSToby Isaac {
6530a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
653141e6d900SToby Isaac   PetscErrorCode ierr;
6532a68b90caSToby Isaac 
6533a68b90caSToby Isaac   PetscFunctionBegin;
6534a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
653541e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
6536a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
6537a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
6538a68b90caSToby Isaac   PetscFunctionReturn(0);
6539a68b90caSToby Isaac }
6540a68b90caSToby Isaac 
6541a68b90caSToby Isaac /*@
6542f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
6543f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
6544a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
6545a68b90caSToby Isaac 
6546a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
6547f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
6548a68b90caSToby Isaac 
6549e228b242SToby Isaac   collective on dm
6550a68b90caSToby Isaac 
6551a68b90caSToby Isaac   Input Parameters:
6552a68b90caSToby Isaac + dm - The DMPlex object
6553e228b242SToby 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).
6554e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
6555a68b90caSToby Isaac 
6556a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
6557a68b90caSToby Isaac 
6558a68b90caSToby Isaac   Level: intermediate
6559a68b90caSToby Isaac 
6560f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
6561a68b90caSToby Isaac @*/
6562a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
6563a68b90caSToby Isaac {
6564a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6565e228b242SToby Isaac   PetscMPIInt    result;
6566a68b90caSToby Isaac   PetscErrorCode ierr;
6567a68b90caSToby Isaac 
6568a68b90caSToby Isaac   PetscFunctionBegin;
6569a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6570e228b242SToby Isaac   if (anchorSection) {
6571e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
6572e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
6573*f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
6574e228b242SToby Isaac   }
6575e228b242SToby Isaac   if (anchorIS) {
6576e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
6577e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
6578*f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
6579e228b242SToby Isaac   }
6580a68b90caSToby Isaac 
6581a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
6582a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
6583a68b90caSToby Isaac   plex->anchorSection = anchorSection;
6584a68b90caSToby Isaac 
6585a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
6586a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
6587a68b90caSToby Isaac   plex->anchorIS = anchorIS;
6588a68b90caSToby Isaac 
6589a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
6590a68b90caSToby Isaac   if (anchorIS && anchorSection) {
6591a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
6592a68b90caSToby Isaac     const PetscInt *anchors;
6593a68b90caSToby Isaac 
6594a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
6595a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
6596a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
6597a68b90caSToby Isaac     for (a = 0; a < size; a++) {
6598a68b90caSToby Isaac       PetscInt p;
6599a68b90caSToby Isaac 
6600a68b90caSToby Isaac       p = anchors[a];
6601a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
6602a68b90caSToby Isaac         PetscInt dof;
6603a68b90caSToby Isaac 
6604a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6605a68b90caSToby Isaac         if (dof) {
6606a68b90caSToby Isaac           PetscErrorCode ierr2;
6607a68b90caSToby Isaac 
6608a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
66098ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
6610a68b90caSToby Isaac         }
6611a68b90caSToby Isaac       }
6612a68b90caSToby Isaac     }
6613a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
6614a68b90caSToby Isaac   }
6615a68b90caSToby Isaac #endif
6616f7c74593SToby Isaac   /* reset the generic constraints */
6617f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
6618a68b90caSToby Isaac   PetscFunctionReturn(0);
6619a68b90caSToby Isaac }
6620a68b90caSToby Isaac 
6621f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
6622a68b90caSToby Isaac {
6623f7c74593SToby Isaac   PetscSection anchorSection;
66246995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
6625a68b90caSToby Isaac   PetscErrorCode ierr;
6626a68b90caSToby Isaac 
6627a68b90caSToby Isaac   PetscFunctionBegin;
6628a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6629a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
6630e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
6631a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
66326995de1eSToby Isaac   if (numFields) {
6633719ab38cSToby Isaac     PetscInt f;
6634a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
6635719ab38cSToby Isaac 
6636719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
6637719ab38cSToby Isaac       PetscInt numComp;
6638719ab38cSToby Isaac 
6639719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
6640719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
6641719ab38cSToby Isaac     }
66426995de1eSToby Isaac   }
6643a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
66446995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
66456995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
66466995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
66476995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
6648a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
6649a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
6650a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6651a68b90caSToby Isaac     if (dof) {
6652a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
6653a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
6654a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
6655a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
6656a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
6657a68b90caSToby Isaac       }
6658a68b90caSToby Isaac     }
6659a68b90caSToby Isaac   }
6660a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
6661a68b90caSToby Isaac   PetscFunctionReturn(0);
6662a68b90caSToby Isaac }
6663a68b90caSToby Isaac 
6664f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
6665a68b90caSToby Isaac {
6666f7c74593SToby Isaac   PetscSection aSec;
66670ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
66680ac89760SToby Isaac   const PetscInt *anchors;
66690ac89760SToby Isaac   PetscInt numFields, f;
667066ad2231SToby Isaac   IS aIS;
66710ac89760SToby Isaac   PetscErrorCode ierr;
66720ac89760SToby Isaac 
66730ac89760SToby Isaac   PetscFunctionBegin;
66740ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66750ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
66760ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
66770ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
66780ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
6679302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
6680a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
668166ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
66826995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
66836995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
66840ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
66850ac89760SToby Isaac   i[0] = 0;
66860ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
66870ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
6688f19733c5SToby Isaac     PetscInt rDof, rOff, r;
6689f19733c5SToby Isaac 
6690f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
6691f19733c5SToby Isaac     if (!rDof) continue;
6692f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
66930ac89760SToby Isaac     if (numFields) {
66940ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
66950ac89760SToby Isaac         annz = 0;
6696f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
6697f19733c5SToby Isaac           a = anchors[rOff + r];
66980ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
66990ac89760SToby Isaac           annz += aDof;
67000ac89760SToby Isaac         }
67010ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
67020ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
67030ac89760SToby Isaac         for (q = 0; q < dof; q++) {
67040ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
67050ac89760SToby Isaac         }
67060ac89760SToby Isaac       }
67070ac89760SToby Isaac     }
67080ac89760SToby Isaac     else {
67090ac89760SToby Isaac       annz = 0;
67100ac89760SToby Isaac       for (q = 0; q < dof; q++) {
67110ac89760SToby Isaac         a = anchors[off + q];
67120ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
67130ac89760SToby Isaac         annz += aDof;
67140ac89760SToby Isaac       }
67150ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
67160ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
67170ac89760SToby Isaac       for (q = 0; q < dof; q++) {
67180ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
67190ac89760SToby Isaac       }
67200ac89760SToby Isaac     }
67210ac89760SToby Isaac   }
67220ac89760SToby Isaac   nnz = i[m];
67230ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
67240ac89760SToby Isaac   offset = 0;
67250ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
67260ac89760SToby Isaac     if (numFields) {
67270ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
67280ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
67290ac89760SToby Isaac         for (q = 0; q < dof; q++) {
67300ac89760SToby Isaac           PetscInt rDof, rOff, r;
67310ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
67320ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
67330ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
67340ac89760SToby Isaac             PetscInt s;
67350ac89760SToby Isaac 
67360ac89760SToby Isaac             a = anchors[rOff + r];
67370ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
67380ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
67390ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
67400ac89760SToby Isaac               j[offset++] = aOff + s;
67410ac89760SToby Isaac             }
67420ac89760SToby Isaac           }
67430ac89760SToby Isaac         }
67440ac89760SToby Isaac       }
67450ac89760SToby Isaac     }
67460ac89760SToby Isaac     else {
67470ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
67480ac89760SToby Isaac       for (q = 0; q < dof; q++) {
67490ac89760SToby Isaac         PetscInt rDof, rOff, r;
67500ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
67510ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
67520ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
67530ac89760SToby Isaac           PetscInt s;
67540ac89760SToby Isaac 
67550ac89760SToby Isaac           a = anchors[rOff + r];
67560ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
67570ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
67580ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
67590ac89760SToby Isaac             j[offset++] = aOff + s;
67600ac89760SToby Isaac           }
67610ac89760SToby Isaac         }
67620ac89760SToby Isaac       }
67630ac89760SToby Isaac     }
67640ac89760SToby Isaac   }
67650ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
676625570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
676725570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
676866ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
67690ac89760SToby Isaac   PetscFunctionReturn(0);
67700ac89760SToby Isaac }
67710ac89760SToby Isaac 
677266ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
677366ad2231SToby Isaac {
6774f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6775f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
677666ad2231SToby Isaac   Mat            cMat;
677766ad2231SToby Isaac   PetscErrorCode ierr;
677866ad2231SToby Isaac 
677966ad2231SToby Isaac   PetscFunctionBegin;
678066ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6781a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
678266ad2231SToby Isaac   if (anchorSection) {
6783e228b242SToby Isaac     PetscDS  ds;
6784e228b242SToby Isaac     PetscInt nf;
6785e228b242SToby Isaac 
6786f7c74593SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
6787f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
6788f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
6789e228b242SToby Isaac     ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
6790302440fdSBarry Smith     ierr = PetscDSGetNumFields(ds,&nf);CHKERRQ(ierr);
6791e228b242SToby Isaac     if (nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
679266ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
679366ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
679466ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
679566ad2231SToby Isaac   }
679666ad2231SToby Isaac   PetscFunctionReturn(0);
679766ad2231SToby Isaac }
6798