xref: /petsc/src/dm/impls/plex/plex.c (revision 39d25373a81a7d163a096e35e7b42dde88263231)
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 
48f13a32a3SMatthew G. Knepley 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)
224*39d25373SMatthew 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)
266*39d25373SMatthew 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)
327*39d25373SMatthew 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)
385*39d25373SMatthew 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);
417*39d25373SMatthew 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 
430731e8ddeSMatthew G. Knepley 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 
485552f7358SJed Brown 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 
811e412dcbdSMatthew G. Knepley 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);
891*39d25373SMatthew 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)
917*39d25373SMatthew 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 
9256c73c22cSMatthew G. Knepley 
926552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
927552f7358SJed Brown {
928552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
929552f7358SJed Brown   PetscErrorCode ierr;
930552f7358SJed Brown 
931552f7358SJed Brown   PetscFunctionBegin;
9320d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
933552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
934552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
935552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
936552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
937be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
938552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
939552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
940d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
941d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
94277623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
943a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
944552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
945552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
946a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
947a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
948d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
949d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
950d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
951d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
952d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
953d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
954fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
955552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
956552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
957713918a9SToby Isaac   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMAdaptLabel_C",NULL);CHKERRQ(ierr);
958552f7358SJed Brown   PetscFunctionReturn(0);
959552f7358SJed Brown }
960552f7358SJed Brown 
961b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
962552f7358SJed Brown {
9638d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
964acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
965552f7358SJed Brown   PetscInt               localSize;
966837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
967552f7358SJed Brown   PetscErrorCode         ierr;
968b412c318SBarry Smith   MatType                mtype;
9691428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
970552f7358SJed Brown 
971552f7358SJed Brown   PetscFunctionBegin;
972607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
973b412c318SBarry Smith   mtype = dm->mattype;
974552f7358SJed Brown   ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
975552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
976552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
97782f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
978552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
979552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
980552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
981acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
982acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
983552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
984552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
985552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
986552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
987552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
988552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
989552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
990837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
991552f7358SJed Brown   if (!isShell) {
992be36d101SStefano Zampini     PetscSection subSection;
993837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
994be36d101SStefano Zampini     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal, bsMax, bsMin, *ltogidx, lsize;
995fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
996552f7358SJed Brown 
99700140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
998be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
999be36d101SStefano Zampini       PetscSection section;
1000be36d101SStefano Zampini       PetscInt     size;
1001be36d101SStefano Zampini 
1002be36d101SStefano Zampini       ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1003be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1004be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1005be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1006be36d101SStefano Zampini     } else {
100700140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1008be36d101SStefano Zampini     }
1009552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1010be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1011a9d99c84SMatthew G. Knepley       PetscInt bdof;
1012a9d99c84SMatthew G. Knepley 
1013552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1014fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
10151d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
10161d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
10171d17a0a3SMatthew G. Knepley       if (dof) {
10181d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1019be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1020be36d101SStefano Zampini       }
1021be36d101SStefano Zampini       if (isMatIS) {
1022be36d101SStefano Zampini         PetscInt loff,c,off;
1023be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1024be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1025be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1026552f7358SJed Brown       }
10272a28c762SMatthew G Knepley     }
10282a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
10292a28c762SMatthew G Knepley     bsLocal = bs;
1030b2566f29SBarry Smith     ierr = MPIU_Allreduce(&bsLocal, &bsMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
10312a28c762SMatthew G Knepley     bsLocal = bs < 0 ? bsMax : bs;
1032b2566f29SBarry Smith     ierr = MPIU_Allreduce(&bsLocal, &bsMin, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1033bff27382SMatthew G. Knepley     if (bsMin != bsMax) {bs = 1;}
1034bff27382SMatthew G. Knepley     else                {bs = bsMax;}
10354fa26246SMatthew G. Knepley     bs   = bs < 0 ? 1 : bs;
1036be36d101SStefano Zampini     if (isMatIS) {
10374fa26246SMatthew G. Knepley       PetscInt l;
10384fa26246SMatthew G. Knepley       /* Must reduce indices by blocksize */
10394fa26246SMatthew G. Knepley       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] /= bs;
10404fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1041be36d101SStefano Zampini     }
1042be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1043be36d101SStefano Zampini     if (isMatIS) {
1044be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1045be36d101SStefano Zampini     }
10461795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
10478d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1048552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1049552f7358SJed Brown   }
1050b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1051552f7358SJed Brown   PetscFunctionReturn(0);
1052552f7358SJed Brown }
1053552f7358SJed Brown 
1054559da874SStefano Zampini /*
1055be36d101SStefano Zampini   DMPlexGetSubdomainGlobalSection - Returns the section associated with the subdomain
1056be36d101SStefano Zampini 
1057be36d101SStefano Zampini   Not collective
1058be36d101SStefano Zampini 
1059be36d101SStefano Zampini   Input Parameter:
1060be36d101SStefano Zampini . mesh - The DMPlex
1061be36d101SStefano Zampini 
1062be36d101SStefano Zampini   Output Parameters:
1063be36d101SStefano Zampini . subsection - The subdomain section
1064be36d101SStefano Zampini 
1065be36d101SStefano Zampini   Level: developer
1066be36d101SStefano Zampini 
1067be36d101SStefano Zampini .seealso:
1068559da874SStefano Zampini */
1069be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1070be36d101SStefano Zampini {
1071be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1072be36d101SStefano Zampini   PetscErrorCode ierr;
1073be36d101SStefano Zampini 
1074be36d101SStefano Zampini   PetscFunctionBegin;
1075be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1076be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1077be36d101SStefano Zampini     PetscSection section;
1078be36d101SStefano Zampini     PetscSF      sf;
1079be36d101SStefano Zampini 
1080be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
1081be36d101SStefano Zampini     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
1082be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1083be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1084be36d101SStefano Zampini   }
1085be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1086be36d101SStefano Zampini   PetscFunctionReturn(0);
1087be36d101SStefano Zampini }
1088be36d101SStefano Zampini 
1089552f7358SJed Brown /*@
1090552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1091552f7358SJed Brown 
1092552f7358SJed Brown   Not collective
1093552f7358SJed Brown 
1094552f7358SJed Brown   Input Parameter:
1095552f7358SJed Brown . mesh - The DMPlex
1096552f7358SJed Brown 
1097552f7358SJed Brown   Output Parameters:
1098552f7358SJed Brown + pStart - The first mesh point
1099552f7358SJed Brown - pEnd   - The upper bound for mesh points
1100552f7358SJed Brown 
1101552f7358SJed Brown   Level: beginner
1102552f7358SJed Brown 
1103552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1104552f7358SJed Brown @*/
1105552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1106552f7358SJed Brown {
1107552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1108552f7358SJed Brown   PetscErrorCode ierr;
1109552f7358SJed Brown 
1110552f7358SJed Brown   PetscFunctionBegin;
1111552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1112552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1113552f7358SJed Brown   PetscFunctionReturn(0);
1114552f7358SJed Brown }
1115552f7358SJed Brown 
1116552f7358SJed Brown /*@
1117552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1118552f7358SJed Brown 
1119552f7358SJed Brown   Not collective
1120552f7358SJed Brown 
1121552f7358SJed Brown   Input Parameters:
1122552f7358SJed Brown + mesh - The DMPlex
1123552f7358SJed Brown . pStart - The first mesh point
1124552f7358SJed Brown - pEnd   - The upper bound for mesh points
1125552f7358SJed Brown 
1126552f7358SJed Brown   Output Parameters:
1127552f7358SJed Brown 
1128552f7358SJed Brown   Level: beginner
1129552f7358SJed Brown 
1130552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1131552f7358SJed Brown @*/
1132552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1133552f7358SJed Brown {
1134552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1135552f7358SJed Brown   PetscErrorCode ierr;
1136552f7358SJed Brown 
1137552f7358SJed Brown   PetscFunctionBegin;
1138552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1139552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1140552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1141552f7358SJed Brown   PetscFunctionReturn(0);
1142552f7358SJed Brown }
1143552f7358SJed Brown 
1144552f7358SJed Brown /*@
1145552f7358SJed Brown   DMPlexGetConeSize - Return the number of in-edges for this point in the Sieve DAG
1146552f7358SJed Brown 
1147552f7358SJed Brown   Not collective
1148552f7358SJed Brown 
1149552f7358SJed Brown   Input Parameters:
1150552f7358SJed Brown + mesh - The DMPlex
1151552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1152552f7358SJed Brown 
1153552f7358SJed Brown   Output Parameter:
1154552f7358SJed Brown . size - The cone size for point p
1155552f7358SJed Brown 
1156552f7358SJed Brown   Level: beginner
1157552f7358SJed Brown 
1158552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1159552f7358SJed Brown @*/
1160552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1161552f7358SJed Brown {
1162552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1163552f7358SJed Brown   PetscErrorCode ierr;
1164552f7358SJed Brown 
1165552f7358SJed Brown   PetscFunctionBegin;
1166552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1167552f7358SJed Brown   PetscValidPointer(size, 3);
1168552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1169552f7358SJed Brown   PetscFunctionReturn(0);
1170552f7358SJed Brown }
1171552f7358SJed Brown 
1172552f7358SJed Brown /*@
1173552f7358SJed Brown   DMPlexSetConeSize - Set the number of in-edges for this point in the Sieve DAG
1174552f7358SJed Brown 
1175552f7358SJed Brown   Not collective
1176552f7358SJed Brown 
1177552f7358SJed Brown   Input Parameters:
1178552f7358SJed Brown + mesh - The DMPlex
1179552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1180552f7358SJed Brown - size - The cone size for point p
1181552f7358SJed Brown 
1182552f7358SJed Brown   Output Parameter:
1183552f7358SJed Brown 
1184552f7358SJed Brown   Note:
1185552f7358SJed Brown   This should be called after DMPlexSetChart().
1186552f7358SJed Brown 
1187552f7358SJed Brown   Level: beginner
1188552f7358SJed Brown 
1189552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1190552f7358SJed Brown @*/
1191552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1192552f7358SJed Brown {
1193552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1194552f7358SJed Brown   PetscErrorCode ierr;
1195552f7358SJed Brown 
1196552f7358SJed Brown   PetscFunctionBegin;
1197552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1198552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
11990d644c17SKarl Rupp 
1200552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1201552f7358SJed Brown   PetscFunctionReturn(0);
1202552f7358SJed Brown }
1203552f7358SJed Brown 
1204f5a469b9SMatthew G. Knepley /*@
1205f5a469b9SMatthew G. Knepley   DMPlexAddConeSize - Add the given number of in-edges to this point in the Sieve DAG
1206f5a469b9SMatthew G. Knepley 
1207f5a469b9SMatthew G. Knepley   Not collective
1208f5a469b9SMatthew G. Knepley 
1209f5a469b9SMatthew G. Knepley   Input Parameters:
1210f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1211f5a469b9SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1212f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1213f5a469b9SMatthew G. Knepley 
1214f5a469b9SMatthew G. Knepley   Output Parameter:
1215f5a469b9SMatthew G. Knepley 
1216f5a469b9SMatthew G. Knepley   Note:
1217f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1218f5a469b9SMatthew G. Knepley 
1219f5a469b9SMatthew G. Knepley   Level: beginner
1220f5a469b9SMatthew G. Knepley 
1221f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1222f5a469b9SMatthew G. Knepley @*/
1223f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1224f5a469b9SMatthew G. Knepley {
1225f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1226f5a469b9SMatthew G. Knepley   PetscInt       csize;
1227f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1228f5a469b9SMatthew G. Knepley 
1229f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1230f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1231f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1232f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1233f5a469b9SMatthew G. Knepley 
1234f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1235f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1236f5a469b9SMatthew G. Knepley }
1237f5a469b9SMatthew G. Knepley 
1238552f7358SJed Brown /*@C
1239552f7358SJed Brown   DMPlexGetCone - Return the points on the in-edges for this point in the Sieve DAG
1240552f7358SJed Brown 
1241552f7358SJed Brown   Not collective
1242552f7358SJed Brown 
1243552f7358SJed Brown   Input Parameters:
1244552f7358SJed Brown + mesh - The DMPlex
1245552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1246552f7358SJed Brown 
1247552f7358SJed Brown   Output Parameter:
1248552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1249552f7358SJed Brown 
1250552f7358SJed Brown   Level: beginner
1251552f7358SJed Brown 
12523813dfbdSMatthew G Knepley   Fortran Notes:
12533813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
12543813dfbdSMatthew G Knepley   include petsc.h90 in your code.
12553813dfbdSMatthew G Knepley 
12563813dfbdSMatthew G Knepley   You must also call DMPlexRestoreCone() after you finish using the returned array.
1257552f7358SJed Brown 
1258552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart()
1259552f7358SJed Brown @*/
1260552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1261552f7358SJed Brown {
1262552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1263552f7358SJed Brown   PetscInt       off;
1264552f7358SJed Brown   PetscErrorCode ierr;
1265552f7358SJed Brown 
1266552f7358SJed Brown   PetscFunctionBegin;
1267552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1268552f7358SJed Brown   PetscValidPointer(cone, 3);
1269552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1270552f7358SJed Brown   *cone = &mesh->cones[off];
1271552f7358SJed Brown   PetscFunctionReturn(0);
1272552f7358SJed Brown }
1273552f7358SJed Brown 
1274552f7358SJed Brown /*@
1275552f7358SJed Brown   DMPlexSetCone - Set the points on the in-edges for this point in the Sieve DAG
1276552f7358SJed Brown 
1277552f7358SJed Brown   Not collective
1278552f7358SJed Brown 
1279552f7358SJed Brown   Input Parameters:
1280552f7358SJed Brown + mesh - The DMPlex
1281552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1282552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1283552f7358SJed Brown 
1284552f7358SJed Brown   Output Parameter:
1285552f7358SJed Brown 
1286552f7358SJed Brown   Note:
1287552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1288552f7358SJed Brown 
1289552f7358SJed Brown   Level: beginner
1290552f7358SJed Brown 
1291552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1292552f7358SJed Brown @*/
1293552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1294552f7358SJed Brown {
1295552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1296552f7358SJed Brown   PetscInt       pStart, pEnd;
1297552f7358SJed Brown   PetscInt       dof, off, c;
1298552f7358SJed Brown   PetscErrorCode ierr;
1299552f7358SJed Brown 
1300552f7358SJed Brown   PetscFunctionBegin;
1301552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1302552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1303552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1304552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1305552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
130682f516ccSBarry 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);
1307552f7358SJed Brown   for (c = 0; c < dof; ++c) {
130882f516ccSBarry 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);
1309552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1310552f7358SJed Brown   }
1311552f7358SJed Brown   PetscFunctionReturn(0);
1312552f7358SJed Brown }
1313552f7358SJed Brown 
1314552f7358SJed Brown /*@C
1315552f7358SJed Brown   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the Sieve DAG
1316552f7358SJed Brown 
1317552f7358SJed Brown   Not collective
1318552f7358SJed Brown 
1319552f7358SJed Brown   Input Parameters:
1320552f7358SJed Brown + mesh - The DMPlex
1321552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1322552f7358SJed Brown 
1323552f7358SJed Brown   Output Parameter:
1324552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1325552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1326552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1327552f7358SJed Brown                     the index of the cone point on which to start.
1328552f7358SJed Brown 
1329552f7358SJed Brown   Level: beginner
1330552f7358SJed Brown 
13313813dfbdSMatthew G Knepley   Fortran Notes:
13323813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
13333813dfbdSMatthew G Knepley   include petsc.h90 in your code.
13343813dfbdSMatthew G Knepley 
13353813dfbdSMatthew G Knepley   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1336552f7358SJed Brown 
1337552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1338552f7358SJed Brown @*/
1339552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1340552f7358SJed Brown {
1341552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1342552f7358SJed Brown   PetscInt       off;
1343552f7358SJed Brown   PetscErrorCode ierr;
1344552f7358SJed Brown 
1345552f7358SJed Brown   PetscFunctionBegin;
1346552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1347552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1348552f7358SJed Brown   {
1349552f7358SJed Brown     PetscInt dof;
1350552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1351552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1352552f7358SJed Brown   }
1353552f7358SJed Brown #endif
1354552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
13550d644c17SKarl Rupp 
1356552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1357552f7358SJed Brown   PetscFunctionReturn(0);
1358552f7358SJed Brown }
1359552f7358SJed Brown 
1360552f7358SJed Brown /*@
1361552f7358SJed Brown   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the Sieve DAG
1362552f7358SJed Brown 
1363552f7358SJed Brown   Not collective
1364552f7358SJed Brown 
1365552f7358SJed Brown   Input Parameters:
1366552f7358SJed Brown + mesh - The DMPlex
1367552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1368552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1369552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1370552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1371552f7358SJed Brown                     the index of the cone point on which to start.
1372552f7358SJed Brown 
1373552f7358SJed Brown   Output Parameter:
1374552f7358SJed Brown 
1375552f7358SJed Brown   Note:
1376552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1377552f7358SJed Brown 
1378552f7358SJed Brown   Level: beginner
1379552f7358SJed Brown 
1380552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1381552f7358SJed Brown @*/
1382552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
1383552f7358SJed Brown {
1384552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1385552f7358SJed Brown   PetscInt       pStart, pEnd;
1386552f7358SJed Brown   PetscInt       dof, off, c;
1387552f7358SJed Brown   PetscErrorCode ierr;
1388552f7358SJed Brown 
1389552f7358SJed Brown   PetscFunctionBegin;
1390552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1391552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1392552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1393552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
1394552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
139582f516ccSBarry 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);
1396552f7358SJed Brown   for (c = 0; c < dof; ++c) {
1397552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
1398552f7358SJed Brown 
1399552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
140082f516ccSBarry 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);
1401552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
1402552f7358SJed Brown   }
1403552f7358SJed Brown   PetscFunctionReturn(0);
1404552f7358SJed Brown }
1405552f7358SJed Brown 
1406552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
1407552f7358SJed Brown {
1408552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1409552f7358SJed Brown   PetscInt       pStart, pEnd;
1410552f7358SJed Brown   PetscInt       dof, off;
1411552f7358SJed Brown   PetscErrorCode ierr;
1412552f7358SJed Brown 
1413552f7358SJed Brown   PetscFunctionBegin;
1414552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1415552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
141682f516ccSBarry 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);
141782f516ccSBarry 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);
141877c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
141977c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
142077c88f5bSMatthew 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);
1421552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
1422552f7358SJed Brown   PetscFunctionReturn(0);
1423552f7358SJed Brown }
1424552f7358SJed Brown 
142577c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
142677c88f5bSMatthew G Knepley {
142777c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
142877c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
142977c88f5bSMatthew G Knepley   PetscInt       dof, off;
143077c88f5bSMatthew G Knepley   PetscErrorCode ierr;
143177c88f5bSMatthew G Knepley 
143277c88f5bSMatthew G Knepley   PetscFunctionBegin;
143377c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
143477c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
143577c88f5bSMatthew 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);
143677c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
143777c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
143877c88f5bSMatthew 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);
143977c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
144077c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
144177c88f5bSMatthew G Knepley }
144277c88f5bSMatthew G Knepley 
1443552f7358SJed Brown /*@
1444552f7358SJed Brown   DMPlexGetSupportSize - Return the number of out-edges for this point in the Sieve DAG
1445552f7358SJed Brown 
1446552f7358SJed Brown   Not collective
1447552f7358SJed Brown 
1448552f7358SJed Brown   Input Parameters:
1449552f7358SJed Brown + mesh - The DMPlex
1450552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1451552f7358SJed Brown 
1452552f7358SJed Brown   Output Parameter:
1453552f7358SJed Brown . size - The support size for point p
1454552f7358SJed Brown 
1455552f7358SJed Brown   Level: beginner
1456552f7358SJed Brown 
1457552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
1458552f7358SJed Brown @*/
1459552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
1460552f7358SJed Brown {
1461552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1462552f7358SJed Brown   PetscErrorCode ierr;
1463552f7358SJed Brown 
1464552f7358SJed Brown   PetscFunctionBegin;
1465552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1466552f7358SJed Brown   PetscValidPointer(size, 3);
1467552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
1468552f7358SJed Brown   PetscFunctionReturn(0);
1469552f7358SJed Brown }
1470552f7358SJed Brown 
1471552f7358SJed Brown /*@
1472552f7358SJed Brown   DMPlexSetSupportSize - Set the number of out-edges for this point in the Sieve DAG
1473552f7358SJed Brown 
1474552f7358SJed Brown   Not collective
1475552f7358SJed Brown 
1476552f7358SJed Brown   Input Parameters:
1477552f7358SJed Brown + mesh - The DMPlex
1478552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1479552f7358SJed Brown - size - The support size for point p
1480552f7358SJed Brown 
1481552f7358SJed Brown   Output Parameter:
1482552f7358SJed Brown 
1483552f7358SJed Brown   Note:
1484552f7358SJed Brown   This should be called after DMPlexSetChart().
1485552f7358SJed Brown 
1486552f7358SJed Brown   Level: beginner
1487552f7358SJed Brown 
1488552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
1489552f7358SJed Brown @*/
1490552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
1491552f7358SJed Brown {
1492552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1493552f7358SJed Brown   PetscErrorCode ierr;
1494552f7358SJed Brown 
1495552f7358SJed Brown   PetscFunctionBegin;
1496552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1497552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
14980d644c17SKarl Rupp 
1499552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
1500552f7358SJed Brown   PetscFunctionReturn(0);
1501552f7358SJed Brown }
1502552f7358SJed Brown 
1503552f7358SJed Brown /*@C
1504552f7358SJed Brown   DMPlexGetSupport - Return the points on the out-edges for this point in the Sieve DAG
1505552f7358SJed Brown 
1506552f7358SJed Brown   Not collective
1507552f7358SJed Brown 
1508552f7358SJed Brown   Input Parameters:
1509552f7358SJed Brown + mesh - The DMPlex
1510552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1511552f7358SJed Brown 
1512552f7358SJed Brown   Output Parameter:
1513552f7358SJed Brown . support - An array of points which are on the out-edges for point p
1514552f7358SJed Brown 
1515552f7358SJed Brown   Level: beginner
1516552f7358SJed Brown 
15173813dfbdSMatthew G Knepley   Fortran Notes:
15183813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
15193813dfbdSMatthew G Knepley   include petsc.h90 in your code.
15203813dfbdSMatthew G Knepley 
15213813dfbdSMatthew G Knepley   You must also call DMPlexRestoreSupport() after you finish using the returned array.
15223813dfbdSMatthew G Knepley 
1523552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1524552f7358SJed Brown @*/
1525552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
1526552f7358SJed Brown {
1527552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1528552f7358SJed Brown   PetscInt       off;
1529552f7358SJed Brown   PetscErrorCode ierr;
1530552f7358SJed Brown 
1531552f7358SJed Brown   PetscFunctionBegin;
1532552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1533552f7358SJed Brown   PetscValidPointer(support, 3);
1534552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
1535552f7358SJed Brown   *support = &mesh->supports[off];
1536552f7358SJed Brown   PetscFunctionReturn(0);
1537552f7358SJed Brown }
1538552f7358SJed Brown 
1539552f7358SJed Brown /*@
1540552f7358SJed Brown   DMPlexSetSupport - Set the points on the out-edges for this point in the Sieve DAG
1541552f7358SJed Brown 
1542552f7358SJed Brown   Not collective
1543552f7358SJed Brown 
1544552f7358SJed Brown   Input Parameters:
1545552f7358SJed Brown + mesh - The DMPlex
1546552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1547552f7358SJed Brown - support - An array of points which are on the in-edges for point p
1548552f7358SJed Brown 
1549552f7358SJed Brown   Output Parameter:
1550552f7358SJed Brown 
1551552f7358SJed Brown   Note:
1552552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
1553552f7358SJed Brown 
1554552f7358SJed Brown   Level: beginner
1555552f7358SJed Brown 
1556552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
1557552f7358SJed Brown @*/
1558552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
1559552f7358SJed Brown {
1560552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1561552f7358SJed Brown   PetscInt       pStart, pEnd;
1562552f7358SJed Brown   PetscInt       dof, off, c;
1563552f7358SJed Brown   PetscErrorCode ierr;
1564552f7358SJed Brown 
1565552f7358SJed Brown   PetscFunctionBegin;
1566552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1567552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1568552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1569552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
1570552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
157182f516ccSBarry 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);
1572552f7358SJed Brown   for (c = 0; c < dof; ++c) {
157382f516ccSBarry 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);
1574552f7358SJed Brown     mesh->supports[off+c] = support[c];
1575552f7358SJed Brown   }
1576552f7358SJed Brown   PetscFunctionReturn(0);
1577552f7358SJed Brown }
1578552f7358SJed Brown 
1579552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
1580552f7358SJed Brown {
1581552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1582552f7358SJed Brown   PetscInt       pStart, pEnd;
1583552f7358SJed Brown   PetscInt       dof, off;
1584552f7358SJed Brown   PetscErrorCode ierr;
1585552f7358SJed Brown 
1586552f7358SJed Brown   PetscFunctionBegin;
1587552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1588552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1589552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1590552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
159182f516ccSBarry 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);
159282f516ccSBarry 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);
159382f516ccSBarry 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);
1594552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
1595552f7358SJed Brown   PetscFunctionReturn(0);
1596552f7358SJed Brown }
1597552f7358SJed Brown 
1598552f7358SJed Brown /*@C
1599552f7358SJed Brown   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the Sieve DAG
1600552f7358SJed Brown 
1601552f7358SJed Brown   Not collective
1602552f7358SJed Brown 
1603552f7358SJed Brown   Input Parameters:
1604552f7358SJed Brown + mesh - The DMPlex
1605552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1606552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
16070298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
1608552f7358SJed Brown 
1609552f7358SJed Brown   Output Parameters:
1610552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
1611552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
1612552f7358SJed Brown 
1613552f7358SJed Brown   Note:
16140298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
1615552f7358SJed Brown 
16163813dfbdSMatthew G Knepley   Fortran Notes:
16173813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16183813dfbdSMatthew G Knepley   include petsc.h90 in your code.
16193813dfbdSMatthew G Knepley 
16203813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
16213813dfbdSMatthew G Knepley 
1622552f7358SJed Brown   Level: beginner
1623552f7358SJed Brown 
1624552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1625552f7358SJed Brown @*/
1626552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1627552f7358SJed Brown {
1628552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
1629552f7358SJed Brown   PetscInt       *closure, *fifo;
16300298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
1631552f7358SJed Brown   PetscInt        tmpSize, t;
1632552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
1633552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
1634552f7358SJed Brown   PetscErrorCode  ierr;
1635552f7358SJed Brown 
1636552f7358SJed Brown   PetscFunctionBegin;
1637552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1638552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1639552f7358SJed Brown   /* This is only 1-level */
1640552f7358SJed Brown   if (useCone) {
1641552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
1642552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
1643552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
1644552f7358SJed Brown   } else {
1645552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
1646552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
1647552f7358SJed Brown   }
1648bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
1649bfbcdd7aSMatthew G. Knepley     if (*points) {
1650bfbcdd7aSMatthew G. Knepley       closure = *points;
1651bfbcdd7aSMatthew G. Knepley     } else {
1652bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
1653bfbcdd7aSMatthew G. Knepley       ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1654bfbcdd7aSMatthew G. Knepley     }
1655bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
1656bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
1657bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
1658bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
1659bfbcdd7aSMatthew G. Knepley     }
1660bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
1661bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
1662bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
1663bfbcdd7aSMatthew G. Knepley   }
166424c766afSToby Isaac   {
166524c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
166624c766afSToby Isaac 
166724c766afSToby Isaac     c = mesh->maxConeSize;
166824c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
166924c766afSToby Isaac     s = mesh->maxSupportSize;
167024c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
167124c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
167224c766afSToby Isaac   }
1673bfbcdd7aSMatthew G. Knepley   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1674bfbcdd7aSMatthew G. Knepley   if (*points) {
1675bfbcdd7aSMatthew G. Knepley     closure = *points;
1676bfbcdd7aSMatthew G. Knepley   } else {
1677bfbcdd7aSMatthew G. Knepley     ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1678bfbcdd7aSMatthew G. Knepley   }
1679bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
1680552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
1681552f7358SJed Brown     const PetscInt cp = tmp[t];
1682552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
1683552f7358SJed Brown 
1684552f7358SJed Brown     closure[closureSize]   = cp;
1685552f7358SJed Brown     closure[closureSize+1] = co;
1686552f7358SJed Brown     fifo[fifoSize]         = cp;
1687552f7358SJed Brown     fifo[fifoSize+1]       = co;
1688552f7358SJed Brown   }
1689bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
1690552f7358SJed Brown   while (fifoSize - fifoStart) {
1691552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
1692552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
1693552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
1694552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
1695552f7358SJed Brown 
1696552f7358SJed Brown     if (useCone) {
1697552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
1698552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
1699552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
1700552f7358SJed Brown     } else {
1701552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
1702552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
17030298fd71SBarry Smith       tmpO = NULL;
1704552f7358SJed Brown     }
1705552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
1706552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
1707552f7358SJed Brown       const PetscInt cp = tmp[i];
17082e1b13c2SMatthew 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. */
17092e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
17102e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
17112e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
1712552f7358SJed Brown       PetscInt       c;
1713552f7358SJed Brown 
17142e1b13c2SMatthew G. Knepley       if (rev) {
17152e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
17162e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
17172e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
17182e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
17192e1b13c2SMatthew G. Knepley       }
1720552f7358SJed Brown       /* Check for duplicate */
1721552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
1722552f7358SJed Brown         if (closure[c] == cp) break;
1723552f7358SJed Brown       }
1724552f7358SJed Brown       if (c == closureSize) {
1725552f7358SJed Brown         closure[closureSize]   = cp;
1726552f7358SJed Brown         closure[closureSize+1] = co;
1727552f7358SJed Brown         fifo[fifoSize]         = cp;
1728552f7358SJed Brown         fifo[fifoSize+1]       = co;
1729552f7358SJed Brown         closureSize           += 2;
1730552f7358SJed Brown         fifoSize              += 2;
1731552f7358SJed Brown       }
1732552f7358SJed Brown     }
1733552f7358SJed Brown     fifoStart += 2;
1734552f7358SJed Brown   }
1735552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
1736552f7358SJed Brown   if (points)    *points    = closure;
1737552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1738552f7358SJed Brown   PetscFunctionReturn(0);
1739552f7358SJed Brown }
1740552f7358SJed Brown 
17419bf0dad6SMatthew G. Knepley /*@C
17429bf0dad6SMatthew 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
17439bf0dad6SMatthew G. Knepley 
17449bf0dad6SMatthew G. Knepley   Not collective
17459bf0dad6SMatthew G. Knepley 
17469bf0dad6SMatthew G. Knepley   Input Parameters:
17479bf0dad6SMatthew G. Knepley + mesh - The DMPlex
17489bf0dad6SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
17499bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
17509bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
17519bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
17529bf0dad6SMatthew G. Knepley 
17539bf0dad6SMatthew G. Knepley   Output Parameters:
17549bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
17559bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
17569bf0dad6SMatthew G. Knepley 
17579bf0dad6SMatthew G. Knepley   Note:
17589bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
17599bf0dad6SMatthew G. Knepley 
17609bf0dad6SMatthew G. Knepley   Fortran Notes:
17619bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
17629bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
17639bf0dad6SMatthew G. Knepley 
17649bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
17659bf0dad6SMatthew G. Knepley 
17669bf0dad6SMatthew G. Knepley   Level: beginner
17679bf0dad6SMatthew G. Knepley 
17689bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
17699bf0dad6SMatthew G. Knepley @*/
17709bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
17719bf0dad6SMatthew G. Knepley {
17729bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
17739bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
17749bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
17759bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
17769bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
17779bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
17789bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
17799bf0dad6SMatthew G. Knepley 
17809bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
17819bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17829bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
17839bf0dad6SMatthew G. Knepley   /* This is only 1-level */
17849bf0dad6SMatthew G. Knepley   if (useCone) {
17859bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
17869bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
17879bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
17889bf0dad6SMatthew G. Knepley   } else {
17899bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
17909bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
17919bf0dad6SMatthew G. Knepley   }
17929bf0dad6SMatthew G. Knepley   if (depth == 1) {
17939bf0dad6SMatthew G. Knepley     if (*points) {
17949bf0dad6SMatthew G. Knepley       closure = *points;
17959bf0dad6SMatthew G. Knepley     } else {
17969bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
17979bf0dad6SMatthew G. Knepley       ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
17989bf0dad6SMatthew G. Knepley     }
17999bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
18009bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
18019bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
18029bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
18039bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
18049bf0dad6SMatthew G. Knepley     }
18059bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
18069bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
18079bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
18089bf0dad6SMatthew G. Knepley   }
180924c766afSToby Isaac   {
181024c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
181124c766afSToby Isaac 
181224c766afSToby Isaac     c = mesh->maxConeSize;
181324c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
181424c766afSToby Isaac     s = mesh->maxSupportSize;
181524c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
181624c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
181724c766afSToby Isaac   }
18189bf0dad6SMatthew G. Knepley   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
18199bf0dad6SMatthew G. Knepley   if (*points) {
18209bf0dad6SMatthew G. Knepley     closure = *points;
18219bf0dad6SMatthew G. Knepley   } else {
18229bf0dad6SMatthew G. Knepley     ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
18239bf0dad6SMatthew G. Knepley   }
18249bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
18259bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
18269bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
18279bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
18289bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
18299bf0dad6SMatthew G. Knepley 
18309bf0dad6SMatthew G. Knepley     if (ornt < 0) {
18319bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
18329bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
183386b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
18349bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
18359bf0dad6SMatthew G. Knepley     }
18369bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
18379bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
18389bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
18399bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
18409bf0dad6SMatthew G. Knepley   }
18419bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
18429bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
18439bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
18449bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
18459bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
18469bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
18479bf0dad6SMatthew G. Knepley 
18489bf0dad6SMatthew G. Knepley     if (useCone) {
18499bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
18509bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
18519bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
18529bf0dad6SMatthew G. Knepley     } else {
18539bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
18549bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
18559bf0dad6SMatthew G. Knepley       tmpO = NULL;
18569bf0dad6SMatthew G. Knepley     }
18579bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
18589bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
18599bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
18609bf0dad6SMatthew 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. */
18619bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
18629bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
18639bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
18649bf0dad6SMatthew G. Knepley       PetscInt       c;
18659bf0dad6SMatthew G. Knepley 
18669bf0dad6SMatthew G. Knepley       if (rev) {
18679bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
18689bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
18699bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
18709bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
18719bf0dad6SMatthew G. Knepley       }
18729bf0dad6SMatthew G. Knepley       /* Check for duplicate */
18739bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
18749bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
18759bf0dad6SMatthew G. Knepley       }
18769bf0dad6SMatthew G. Knepley       if (c == closureSize) {
18779bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
18789bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
18799bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
18809bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
18819bf0dad6SMatthew G. Knepley         closureSize           += 2;
18829bf0dad6SMatthew G. Knepley         fifoSize              += 2;
18839bf0dad6SMatthew G. Knepley       }
18849bf0dad6SMatthew G. Knepley     }
18859bf0dad6SMatthew G. Knepley     fifoStart += 2;
18869bf0dad6SMatthew G. Knepley   }
18879bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
18889bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
18899bf0dad6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
18909bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
18919bf0dad6SMatthew G. Knepley }
18929bf0dad6SMatthew G. Knepley 
1893552f7358SJed Brown /*@C
1894552f7358SJed 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
1895552f7358SJed Brown 
1896552f7358SJed Brown   Not collective
1897552f7358SJed Brown 
1898552f7358SJed Brown   Input Parameters:
1899552f7358SJed Brown + mesh - The DMPlex
1900552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1901552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
1902e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
1903e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
1904552f7358SJed Brown 
1905552f7358SJed Brown   Note:
19060298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
1907552f7358SJed Brown 
19083813dfbdSMatthew G Knepley   Fortran Notes:
19093813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
19103813dfbdSMatthew G Knepley   include petsc.h90 in your code.
19113813dfbdSMatthew G Knepley 
19123813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
19133813dfbdSMatthew G Knepley 
1914552f7358SJed Brown   Level: beginner
1915552f7358SJed Brown 
1916552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1917552f7358SJed Brown @*/
1918552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1919552f7358SJed Brown {
1920552f7358SJed Brown   PetscErrorCode ierr;
1921552f7358SJed Brown 
1922552f7358SJed Brown   PetscFunctionBegin;
1923552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1924e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
1925e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
1926552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, points);CHKERRQ(ierr);
19274ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
1928552f7358SJed Brown   PetscFunctionReturn(0);
1929552f7358SJed Brown }
1930552f7358SJed Brown 
1931552f7358SJed Brown /*@
1932552f7358SJed Brown   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the Sieve DAG
1933552f7358SJed Brown 
1934552f7358SJed Brown   Not collective
1935552f7358SJed Brown 
1936552f7358SJed Brown   Input Parameter:
1937552f7358SJed Brown . mesh - The DMPlex
1938552f7358SJed Brown 
1939552f7358SJed Brown   Output Parameters:
1940552f7358SJed Brown + maxConeSize - The maximum number of in-edges
1941552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
1942552f7358SJed Brown 
1943552f7358SJed Brown   Level: beginner
1944552f7358SJed Brown 
1945552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1946552f7358SJed Brown @*/
1947552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
1948552f7358SJed Brown {
1949552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
1950552f7358SJed Brown 
1951552f7358SJed Brown   PetscFunctionBegin;
1952552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1953552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
1954552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
1955552f7358SJed Brown   PetscFunctionReturn(0);
1956552f7358SJed Brown }
1957552f7358SJed Brown 
1958552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
1959552f7358SJed Brown {
1960552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1961552f7358SJed Brown   PetscInt       size;
1962552f7358SJed Brown   PetscErrorCode ierr;
1963552f7358SJed Brown 
1964552f7358SJed Brown   PetscFunctionBegin;
1965552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1966552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
1967552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
19681795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
19691795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
1970552f7358SJed Brown   if (mesh->maxSupportSize) {
1971552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
1972552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
19731795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
1974552f7358SJed Brown   }
1975552f7358SJed Brown   PetscFunctionReturn(0);
1976552f7358SJed Brown }
1977552f7358SJed Brown 
1978552f7358SJed Brown PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1979552f7358SJed Brown {
1980552f7358SJed Brown   PetscErrorCode ierr;
1981552f7358SJed Brown 
1982552f7358SJed Brown   PetscFunctionBegin;
19834d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
19844d9407bcSMatthew G. Knepley   ierr = DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1985552f7358SJed Brown   PetscFunctionReturn(0);
1986552f7358SJed Brown }
1987552f7358SJed Brown 
1988552f7358SJed Brown /*@
1989552f7358SJed Brown   DMPlexSymmetrize - Creates support (out-edge) information from cone (in-edge) inoformation
1990552f7358SJed Brown 
1991552f7358SJed Brown   Not collective
1992552f7358SJed Brown 
1993552f7358SJed Brown   Input Parameter:
1994552f7358SJed Brown . mesh - The DMPlex
1995552f7358SJed Brown 
1996552f7358SJed Brown   Output Parameter:
1997552f7358SJed Brown 
1998552f7358SJed Brown   Note:
1999552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2000552f7358SJed Brown 
2001552f7358SJed Brown   Level: beginner
2002552f7358SJed Brown 
2003552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2004552f7358SJed Brown @*/
2005552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2006552f7358SJed Brown {
2007552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2008552f7358SJed Brown   PetscInt      *offsets;
2009552f7358SJed Brown   PetscInt       supportSize;
2010552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2011552f7358SJed Brown   PetscErrorCode ierr;
2012552f7358SJed Brown 
2013552f7358SJed Brown   PetscFunctionBegin;
2014552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
201582f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
2016552f7358SJed Brown   /* Calculate support sizes */
2017552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2018552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2019552f7358SJed Brown     PetscInt dof, off, c;
2020552f7358SJed Brown 
2021552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2022552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2023552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2024552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2025552f7358SJed Brown     }
2026552f7358SJed Brown   }
2027552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2028552f7358SJed Brown     PetscInt dof;
2029552f7358SJed Brown 
2030552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
20310d644c17SKarl Rupp 
2032552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2033552f7358SJed Brown   }
2034552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2035552f7358SJed Brown   /* Calculate supports */
2036552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
20371795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
20381795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2039552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2040552f7358SJed Brown     PetscInt dof, off, c;
2041552f7358SJed Brown 
2042552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2043552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2044552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2045552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2046552f7358SJed Brown       PetscInt       offS;
2047552f7358SJed Brown 
2048552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
20490d644c17SKarl Rupp 
2050552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2051552f7358SJed Brown       ++offsets[q];
2052552f7358SJed Brown     }
2053552f7358SJed Brown   }
2054552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
2055552f7358SJed Brown   PetscFunctionReturn(0);
2056552f7358SJed Brown }
2057552f7358SJed Brown 
2058552f7358SJed Brown /*@
2059552f7358SJed Brown   DMPlexStratify - The Sieve DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
2060552f7358SJed Brown   can be illustrated by Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2061552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2062552f7358SJed Brown   the DAG.
2063552f7358SJed Brown 
2064bf4602e4SToby Isaac   Collective on dm
2065552f7358SJed Brown 
2066552f7358SJed Brown   Input Parameter:
2067552f7358SJed Brown . mesh - The DMPlex
2068552f7358SJed Brown 
2069552f7358SJed Brown   Output Parameter:
2070552f7358SJed Brown 
2071552f7358SJed Brown   Notes:
2072150b719bSJed Brown   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
2073150b719bSJed Brown   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2074c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2075150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2076552f7358SJed Brown 
2077150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2078552f7358SJed Brown 
2079552f7358SJed Brown   Level: beginner
2080552f7358SJed Brown 
2081552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
2082552f7358SJed Brown @*/
2083552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2084552f7358SJed Brown {
2085df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2086aa50250dSMatthew G. Knepley   DMLabel        label;
2087552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2088552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
2089552f7358SJed Brown   PetscErrorCode ierr;
2090552f7358SJed Brown 
2091552f7358SJed Brown   PetscFunctionBegin;
2092552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2093552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2094552f7358SJed Brown   /* Calculate depth */
2095aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2096c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2097aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2098552f7358SJed Brown   /* Initialize roots and count leaves */
2099552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2100552f7358SJed Brown     PetscInt coneSize, supportSize;
2101552f7358SJed Brown 
2102552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2103552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2104552f7358SJed Brown     if (!coneSize && supportSize) {
2105552f7358SJed Brown       ++numRoots;
2106aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2107552f7358SJed Brown     } else if (!supportSize && coneSize) {
2108552f7358SJed Brown       ++numLeaves;
2109552f7358SJed Brown     } else if (!supportSize && !coneSize) {
2110552f7358SJed Brown       /* Isolated points */
2111aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2112552f7358SJed Brown     }
2113552f7358SJed Brown   }
2114552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2115552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
2116552f7358SJed Brown       PetscInt coneSize, supportSize;
2117552f7358SJed Brown 
2118552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2119552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2120552f7358SJed Brown       if (!supportSize && coneSize) {
2121aa50250dSMatthew G. Knepley         ierr = DMLabelSetValue(label, p, 1);CHKERRQ(ierr);
2122552f7358SJed Brown       }
2123552f7358SJed Brown     }
2124552f7358SJed Brown   } else {
212574ef644bSMatthew G. Knepley     IS       pointIS;
212674ef644bSMatthew G. Knepley     PetscInt numPoints = 0, level = 0;
2127552f7358SJed Brown 
212874ef644bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
212974ef644bSMatthew G. Knepley     if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
213074ef644bSMatthew G. Knepley     while (numPoints) {
213174ef644bSMatthew G. Knepley       const PetscInt *points;
213274ef644bSMatthew G. Knepley       const PetscInt  newLevel = level+1;
213374ef644bSMatthew G. Knepley 
213474ef644bSMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
213574ef644bSMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
213674ef644bSMatthew G. Knepley         const PetscInt  point = points[p];
213774ef644bSMatthew G. Knepley         const PetscInt *support;
213874ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
213974ef644bSMatthew G. Knepley 
214074ef644bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
214174ef644bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
214274ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
214374ef644bSMatthew G. Knepley           ierr = DMLabelSetValue(label, support[s], newLevel);CHKERRQ(ierr);
2144552f7358SJed Brown         }
2145552f7358SJed Brown       }
21465edfc765SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
214774ef644bSMatthew G. Knepley       ++level;
214874ef644bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
214974ef644bSMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
215074ef644bSMatthew G. Knepley       if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
215174ef644bSMatthew G. Knepley       else         {numPoints = 0;}
215274ef644bSMatthew G. Knepley     }
215374ef644bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
215474ef644bSMatthew G. Knepley   }
2155bf4602e4SToby Isaac   { /* just in case there is an empty process */
2156bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2157bf4602e4SToby Isaac 
2158bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label,&numValues);CHKERRQ(ierr);
21594de306b1SToby Isaac     for (v = 0; v < numValues; v++) {
21604de306b1SToby Isaac       IS pointIS;
21614de306b1SToby Isaac 
21624de306b1SToby Isaac       ierr = DMLabelGetStratumIS(label, v, &pointIS);CHKERRQ(ierr);
21634de306b1SToby Isaac       if (pointIS) {
21644de306b1SToby Isaac         PetscInt  min, max, numPoints;
21654de306b1SToby Isaac         PetscInt  start;
21664de306b1SToby Isaac         PetscBool contig;
21674de306b1SToby Isaac 
21684de306b1SToby Isaac         ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
21694de306b1SToby Isaac         ierr = ISGetMinMax(pointIS, &min, &max);CHKERRQ(ierr);
21704de306b1SToby Isaac         ierr = ISContiguousLocal(pointIS,min,max+1,&start,&contig);CHKERRQ(ierr);
21714de306b1SToby Isaac         if (start == 0 && contig) {
21724de306b1SToby Isaac           ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
21734de306b1SToby Isaac           ierr = ISCreateStride(PETSC_COMM_SELF,numPoints,min,1,&pointIS);CHKERRQ(ierr);
21744de306b1SToby Isaac           ierr = DMLabelSetStratumIS(label, v, pointIS);CHKERRQ(ierr);
21754de306b1SToby Isaac         }
21764de306b1SToby Isaac       }
21774de306b1SToby Isaac       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
21784de306b1SToby Isaac     }
21796d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2180bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2181bf4602e4SToby Isaac       DMLabelAddStratum(label,v);CHKERRQ(ierr);
2182bf4602e4SToby Isaac     }
2183bf4602e4SToby Isaac   }
2184bf4602e4SToby Isaac 
2185df0420ecSMatthew G. Knepley   ierr = DMLabelGetState(label, &mesh->depthState);CHKERRQ(ierr);
2186552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2187552f7358SJed Brown   PetscFunctionReturn(0);
2188552f7358SJed Brown }
2189552f7358SJed Brown 
2190552f7358SJed Brown /*@C
2191552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
2192552f7358SJed Brown 
2193552f7358SJed Brown   Not Collective
2194552f7358SJed Brown 
2195552f7358SJed Brown   Input Parameters:
2196552f7358SJed Brown + dm - The DMPlex object
2197552f7358SJed Brown . numPoints - The number of input points for the join
2198552f7358SJed Brown - points - The input points
2199552f7358SJed Brown 
2200552f7358SJed Brown   Output Parameters:
2201552f7358SJed Brown + numCoveredPoints - The number of points in the join
2202552f7358SJed Brown - coveredPoints - The points in the join
2203552f7358SJed Brown 
2204552f7358SJed Brown   Level: intermediate
2205552f7358SJed Brown 
2206552f7358SJed Brown   Note: Currently, this is restricted to a single level join
2207552f7358SJed Brown 
22083813dfbdSMatthew G Knepley   Fortran Notes:
22093813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22103813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22113813dfbdSMatthew G Knepley 
22123813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
22133813dfbdSMatthew G Knepley 
2214552f7358SJed Brown .keywords: mesh
2215552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2216552f7358SJed Brown @*/
2217552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2218552f7358SJed Brown {
2219552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2220552f7358SJed Brown   PetscInt      *join[2];
2221552f7358SJed Brown   PetscInt       joinSize, i = 0;
2222552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2223552f7358SJed Brown   PetscErrorCode ierr;
2224552f7358SJed Brown 
2225552f7358SJed Brown   PetscFunctionBegin;
2226552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2227552f7358SJed Brown   PetscValidPointer(points, 2);
2228552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2229552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2230552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
2231552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
2232552f7358SJed Brown   /* Copy in support of first point */
2233552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
2234552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
2235552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
2236552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
2237552f7358SJed Brown   }
2238552f7358SJed Brown   /* Check each successive support */
2239552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2240552f7358SJed Brown     PetscInt newJoinSize = 0;
2241552f7358SJed Brown 
2242552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
2243552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
2244552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2245552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
2246552f7358SJed Brown 
2247552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
2248552f7358SJed Brown         if (point == join[i][m]) {
2249552f7358SJed Brown           join[1-i][newJoinSize++] = point;
2250552f7358SJed Brown           break;
2251552f7358SJed Brown         }
2252552f7358SJed Brown       }
2253552f7358SJed Brown     }
2254552f7358SJed Brown     joinSize = newJoinSize;
2255552f7358SJed Brown     i        = 1-i;
2256552f7358SJed Brown   }
2257552f7358SJed Brown   *numCoveredPoints = joinSize;
2258552f7358SJed Brown   *coveredPoints    = join[i];
2259552f7358SJed Brown   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
2260552f7358SJed Brown   PetscFunctionReturn(0);
2261552f7358SJed Brown }
2262552f7358SJed Brown 
2263552f7358SJed Brown /*@C
2264552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
2265552f7358SJed Brown 
2266552f7358SJed Brown   Not Collective
2267552f7358SJed Brown 
2268552f7358SJed Brown   Input Parameters:
2269552f7358SJed Brown + dm - The DMPlex object
2270552f7358SJed Brown . numPoints - The number of input points for the join
2271552f7358SJed Brown - points - The input points
2272552f7358SJed Brown 
2273552f7358SJed Brown   Output Parameters:
2274552f7358SJed Brown + numCoveredPoints - The number of points in the join
2275552f7358SJed Brown - coveredPoints - The points in the join
2276552f7358SJed Brown 
22773813dfbdSMatthew G Knepley   Fortran Notes:
22783813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22793813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22803813dfbdSMatthew G Knepley 
22813813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
22823813dfbdSMatthew G Knepley 
2283552f7358SJed Brown   Level: intermediate
2284552f7358SJed Brown 
2285552f7358SJed Brown .keywords: mesh
2286552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
2287552f7358SJed Brown @*/
2288552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2289552f7358SJed Brown {
2290552f7358SJed Brown   PetscErrorCode ierr;
2291552f7358SJed Brown 
2292552f7358SJed Brown   PetscFunctionBegin;
2293552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2294d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2295d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2296d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
2297552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
2298d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2299552f7358SJed Brown   PetscFunctionReturn(0);
2300552f7358SJed Brown }
2301552f7358SJed Brown 
2302552f7358SJed Brown /*@C
2303552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
2304552f7358SJed Brown 
2305552f7358SJed Brown   Not Collective
2306552f7358SJed Brown 
2307552f7358SJed Brown   Input Parameters:
2308552f7358SJed Brown + dm - The DMPlex object
2309552f7358SJed Brown . numPoints - The number of input points for the join
2310552f7358SJed Brown - points - The input points
2311552f7358SJed Brown 
2312552f7358SJed Brown   Output Parameters:
2313552f7358SJed Brown + numCoveredPoints - The number of points in the join
2314552f7358SJed Brown - coveredPoints - The points in the join
2315552f7358SJed Brown 
23163813dfbdSMatthew G Knepley   Fortran Notes:
23173813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
23183813dfbdSMatthew G Knepley   include petsc.h90 in your code.
23193813dfbdSMatthew G Knepley 
23203813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
23213813dfbdSMatthew G Knepley 
2322552f7358SJed Brown   Level: intermediate
2323552f7358SJed Brown 
2324552f7358SJed Brown .keywords: mesh
2325552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
2326552f7358SJed Brown @*/
2327552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2328552f7358SJed Brown {
2329552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2330552f7358SJed Brown   PetscInt      *offsets, **closures;
2331552f7358SJed Brown   PetscInt      *join[2];
2332552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
233324c766afSToby Isaac   PetscInt       p, d, c, m, ms;
2334552f7358SJed Brown   PetscErrorCode ierr;
2335552f7358SJed Brown 
2336552f7358SJed Brown   PetscFunctionBegin;
2337552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2338552f7358SJed Brown   PetscValidPointer(points, 2);
2339552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2340552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2341552f7358SJed Brown 
2342552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
23431795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
2344552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
234524c766afSToby Isaac   ms      = mesh->maxSupportSize;
234624c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
2347552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
2348552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
2349552f7358SJed Brown 
2350552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2351552f7358SJed Brown     PetscInt closureSize;
2352552f7358SJed Brown 
2353552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
23540d644c17SKarl Rupp 
2355552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
2356552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
2357552f7358SJed Brown       PetscInt pStart, pEnd, i;
2358552f7358SJed Brown 
2359552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
2360552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
2361552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2362552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
2363552f7358SJed Brown           break;
2364552f7358SJed Brown         }
2365552f7358SJed Brown       }
2366552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
2367552f7358SJed Brown     }
236882f516ccSBarry 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);
2369552f7358SJed Brown   }
2370552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
2371552f7358SJed Brown     PetscInt dof;
2372552f7358SJed Brown 
2373552f7358SJed Brown     /* Copy in support of first point */
2374552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
2375552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
2376552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
2377552f7358SJed Brown     }
2378552f7358SJed Brown     /* Check each successive cone */
2379552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
2380552f7358SJed Brown       PetscInt newJoinSize = 0;
2381552f7358SJed Brown 
2382552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
2383552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2384552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
2385552f7358SJed Brown 
2386552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
2387552f7358SJed Brown           if (point == join[i][m]) {
2388552f7358SJed Brown             join[1-i][newJoinSize++] = point;
2389552f7358SJed Brown             break;
2390552f7358SJed Brown           }
2391552f7358SJed Brown         }
2392552f7358SJed Brown       }
2393552f7358SJed Brown       joinSize = newJoinSize;
2394552f7358SJed Brown       i        = 1-i;
2395552f7358SJed Brown     }
2396552f7358SJed Brown     if (joinSize) break;
2397552f7358SJed Brown   }
2398552f7358SJed Brown   *numCoveredPoints = joinSize;
2399552f7358SJed Brown   *coveredPoints    = join[i];
2400552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
24010298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
2402552f7358SJed Brown   }
2403552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
2404552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
2405552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
2406552f7358SJed Brown   PetscFunctionReturn(0);
2407552f7358SJed Brown }
2408552f7358SJed Brown 
2409552f7358SJed Brown /*@C
2410552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
2411552f7358SJed Brown 
2412552f7358SJed Brown   Not Collective
2413552f7358SJed Brown 
2414552f7358SJed Brown   Input Parameters:
2415552f7358SJed Brown + dm - The DMPlex object
2416552f7358SJed Brown . numPoints - The number of input points for the meet
2417552f7358SJed Brown - points - The input points
2418552f7358SJed Brown 
2419552f7358SJed Brown   Output Parameters:
2420552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2421552f7358SJed Brown - coveredPoints - The points in the meet
2422552f7358SJed Brown 
2423552f7358SJed Brown   Level: intermediate
2424552f7358SJed Brown 
2425552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
2426552f7358SJed Brown 
24273813dfbdSMatthew G Knepley   Fortran Notes:
24283813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24293813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24303813dfbdSMatthew G Knepley 
24313813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24323813dfbdSMatthew G Knepley 
2433552f7358SJed Brown .keywords: mesh
2434552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
2435552f7358SJed Brown @*/
2436552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
2437552f7358SJed Brown {
2438552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2439552f7358SJed Brown   PetscInt      *meet[2];
2440552f7358SJed Brown   PetscInt       meetSize, i = 0;
2441552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2442552f7358SJed Brown   PetscErrorCode ierr;
2443552f7358SJed Brown 
2444552f7358SJed Brown   PetscFunctionBegin;
2445552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2446552f7358SJed Brown   PetscValidPointer(points, 2);
2447552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
2448552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
2449552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
2450552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
2451552f7358SJed Brown   /* Copy in cone of first point */
2452552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
2453552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
2454552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
2455552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
2456552f7358SJed Brown   }
2457552f7358SJed Brown   /* Check each successive cone */
2458552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2459552f7358SJed Brown     PetscInt newMeetSize = 0;
2460552f7358SJed Brown 
2461552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
2462552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
2463552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2464552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
2465552f7358SJed Brown 
2466552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
2467552f7358SJed Brown         if (point == meet[i][m]) {
2468552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
2469552f7358SJed Brown           break;
2470552f7358SJed Brown         }
2471552f7358SJed Brown       }
2472552f7358SJed Brown     }
2473552f7358SJed Brown     meetSize = newMeetSize;
2474552f7358SJed Brown     i        = 1-i;
2475552f7358SJed Brown   }
2476552f7358SJed Brown   *numCoveringPoints = meetSize;
2477552f7358SJed Brown   *coveringPoints    = meet[i];
2478552f7358SJed Brown   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
2479552f7358SJed Brown   PetscFunctionReturn(0);
2480552f7358SJed Brown }
2481552f7358SJed Brown 
2482552f7358SJed Brown /*@C
2483552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
2484552f7358SJed Brown 
2485552f7358SJed Brown   Not Collective
2486552f7358SJed Brown 
2487552f7358SJed Brown   Input Parameters:
2488552f7358SJed Brown + dm - The DMPlex object
2489552f7358SJed Brown . numPoints - The number of input points for the meet
2490552f7358SJed Brown - points - The input points
2491552f7358SJed Brown 
2492552f7358SJed Brown   Output Parameters:
2493552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2494552f7358SJed Brown - coveredPoints - The points in the meet
2495552f7358SJed Brown 
2496552f7358SJed Brown   Level: intermediate
2497552f7358SJed Brown 
24983813dfbdSMatthew G Knepley   Fortran Notes:
24993813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25003813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25013813dfbdSMatthew G Knepley 
25023813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25033813dfbdSMatthew G Knepley 
2504552f7358SJed Brown .keywords: mesh
2505552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
2506552f7358SJed Brown @*/
2507552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2508552f7358SJed Brown {
2509552f7358SJed Brown   PetscErrorCode ierr;
2510552f7358SJed Brown 
2511552f7358SJed Brown   PetscFunctionBegin;
2512552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2513d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2514d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2515d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
2516552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
2517d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2518552f7358SJed Brown   PetscFunctionReturn(0);
2519552f7358SJed Brown }
2520552f7358SJed Brown 
2521552f7358SJed Brown /*@C
2522552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
2523552f7358SJed Brown 
2524552f7358SJed Brown   Not Collective
2525552f7358SJed Brown 
2526552f7358SJed Brown   Input Parameters:
2527552f7358SJed Brown + dm - The DMPlex object
2528552f7358SJed Brown . numPoints - The number of input points for the meet
2529552f7358SJed Brown - points - The input points
2530552f7358SJed Brown 
2531552f7358SJed Brown   Output Parameters:
2532552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2533552f7358SJed Brown - coveredPoints - The points in the meet
2534552f7358SJed Brown 
2535552f7358SJed Brown   Level: intermediate
2536552f7358SJed Brown 
25373813dfbdSMatthew G Knepley   Fortran Notes:
25383813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25393813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25403813dfbdSMatthew G Knepley 
25413813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25423813dfbdSMatthew G Knepley 
2543552f7358SJed Brown .keywords: mesh
2544552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
2545552f7358SJed Brown @*/
2546552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2547552f7358SJed Brown {
2548552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2549552f7358SJed Brown   PetscInt      *offsets, **closures;
2550552f7358SJed Brown   PetscInt      *meet[2];
2551552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
255224c766afSToby Isaac   PetscInt       p, h, c, m, mc;
2553552f7358SJed Brown   PetscErrorCode ierr;
2554552f7358SJed Brown 
2555552f7358SJed Brown   PetscFunctionBegin;
2556552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2557552f7358SJed Brown   PetscValidPointer(points, 2);
2558552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2559552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2560552f7358SJed Brown 
2561552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
2562785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
2563552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
256424c766afSToby Isaac   mc      = mesh->maxConeSize;
256524c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
2566552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
2567552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
2568552f7358SJed Brown 
2569552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2570552f7358SJed Brown     PetscInt closureSize;
2571552f7358SJed Brown 
2572552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
25730d644c17SKarl Rupp 
2574552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
2575552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
2576552f7358SJed Brown       PetscInt pStart, pEnd, i;
2577552f7358SJed Brown 
2578552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
2579552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
2580552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2581552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
2582552f7358SJed Brown           break;
2583552f7358SJed Brown         }
2584552f7358SJed Brown       }
2585552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
2586552f7358SJed Brown     }
258782f516ccSBarry 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);
2588552f7358SJed Brown   }
2589552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
2590552f7358SJed Brown     PetscInt dof;
2591552f7358SJed Brown 
2592552f7358SJed Brown     /* Copy in cone of first point */
2593552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
2594552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
2595552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
2596552f7358SJed Brown     }
2597552f7358SJed Brown     /* Check each successive cone */
2598552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
2599552f7358SJed Brown       PetscInt newMeetSize = 0;
2600552f7358SJed Brown 
2601552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
2602552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2603552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
2604552f7358SJed Brown 
2605552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
2606552f7358SJed Brown           if (point == meet[i][m]) {
2607552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
2608552f7358SJed Brown             break;
2609552f7358SJed Brown           }
2610552f7358SJed Brown         }
2611552f7358SJed Brown       }
2612552f7358SJed Brown       meetSize = newMeetSize;
2613552f7358SJed Brown       i        = 1-i;
2614552f7358SJed Brown     }
2615552f7358SJed Brown     if (meetSize) break;
2616552f7358SJed Brown   }
2617552f7358SJed Brown   *numCoveredPoints = meetSize;
2618552f7358SJed Brown   *coveredPoints    = meet[i];
2619552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
26200298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
2621552f7358SJed Brown   }
2622552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
2623552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
2624552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
2625552f7358SJed Brown   PetscFunctionReturn(0);
2626552f7358SJed Brown }
2627552f7358SJed Brown 
26284e3744c5SMatthew G. Knepley /*@C
26294e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
26304e3744c5SMatthew G. Knepley 
26314e3744c5SMatthew G. Knepley   Not Collective
26324e3744c5SMatthew G. Knepley 
26334e3744c5SMatthew G. Knepley   Input Parameters:
26344e3744c5SMatthew G. Knepley + dmA - A DMPlex object
26354e3744c5SMatthew G. Knepley - dmB - A DMPlex object
26364e3744c5SMatthew G. Knepley 
26374e3744c5SMatthew G. Knepley   Output Parameters:
26384e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
26394e3744c5SMatthew G. Knepley 
26404e3744c5SMatthew G. Knepley   Level: intermediate
26414e3744c5SMatthew G. Knepley 
26424e3744c5SMatthew G. Knepley   Notes:
26434e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
26444e3744c5SMatthew G. Knepley 
26454e3744c5SMatthew G. Knepley .keywords: mesh
26464e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
26474e3744c5SMatthew G. Knepley @*/
26484e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
26494e3744c5SMatthew G. Knepley {
26504e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
26514e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
26524e3744c5SMatthew G. Knepley 
26534e3744c5SMatthew G. Knepley   PetscFunctionBegin;
26544e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
26554e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
26564e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
26574e3744c5SMatthew G. Knepley 
26584e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
26594e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
26604e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
26614e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
26624e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
26634e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
26644e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
26654e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
26664e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
26674e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
26684e3744c5SMatthew G. Knepley 
26694e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
26704e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
26714e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
26724e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
26734e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
26744e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
26754e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
26764e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
26774e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
26784e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
26794e3744c5SMatthew G. Knepley     }
26804e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
26814e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
26824e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
26834e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
26844e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
26854e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
26864e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
26874e3744c5SMatthew G. Knepley     }
26884e3744c5SMatthew G. Knepley   }
26894e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
26904e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
26914e3744c5SMatthew G. Knepley }
26924e3744c5SMatthew G. Knepley 
269318ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
2694a6dfd86eSKarl Rupp {
269582f516ccSBarry Smith   MPI_Comm       comm;
2696552f7358SJed Brown   PetscErrorCode ierr;
2697552f7358SJed Brown 
2698552f7358SJed Brown   PetscFunctionBegin;
269982f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2700552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
2701552f7358SJed Brown   switch (cellDim) {
2702552f7358SJed Brown   case 0:
2703552f7358SJed Brown     *numFaceVertices = 0;
2704552f7358SJed Brown     break;
2705552f7358SJed Brown   case 1:
2706552f7358SJed Brown     *numFaceVertices = 1;
2707552f7358SJed Brown     break;
2708552f7358SJed Brown   case 2:
2709552f7358SJed Brown     switch (numCorners) {
271019436ca2SJed Brown     case 3: /* triangle */
271119436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2712552f7358SJed Brown       break;
271319436ca2SJed Brown     case 4: /* quadrilateral */
271419436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2715552f7358SJed Brown       break;
271619436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
271719436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2718552f7358SJed Brown       break;
271919436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
272019436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2721552f7358SJed Brown       break;
2722552f7358SJed Brown     default:
2723f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2724552f7358SJed Brown     }
2725552f7358SJed Brown     break;
2726552f7358SJed Brown   case 3:
2727552f7358SJed Brown     switch (numCorners) {
272819436ca2SJed Brown     case 4: /* tetradehdron */
272919436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
2730552f7358SJed Brown       break;
273119436ca2SJed Brown     case 6: /* tet cohesive cells */
273219436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2733552f7358SJed Brown       break;
273419436ca2SJed Brown     case 8: /* hexahedron */
273519436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2736552f7358SJed Brown       break;
273719436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
273819436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2739552f7358SJed Brown       break;
274019436ca2SJed Brown     case 10: /* quadratic tetrahedron */
274119436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2742552f7358SJed Brown       break;
274319436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
274419436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2745552f7358SJed Brown       break;
274619436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
274719436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2748552f7358SJed Brown       break;
274919436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
275019436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
2751552f7358SJed Brown       break;
2752552f7358SJed Brown     default:
2753f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2754552f7358SJed Brown     }
2755552f7358SJed Brown     break;
2756552f7358SJed Brown   default:
2757f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
2758552f7358SJed Brown   }
2759552f7358SJed Brown   PetscFunctionReturn(0);
2760552f7358SJed Brown }
2761552f7358SJed Brown 
2762552f7358SJed Brown /*@
2763aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
2764552f7358SJed Brown 
2765552f7358SJed Brown   Not Collective
2766552f7358SJed Brown 
2767aa50250dSMatthew G. Knepley   Input Parameter:
2768552f7358SJed Brown . dm    - The DMPlex object
2769552f7358SJed Brown 
2770aa50250dSMatthew G. Knepley   Output Parameter:
2771aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
2772552f7358SJed Brown 
2773552f7358SJed Brown   Level: developer
2774552f7358SJed Brown 
2775aa50250dSMatthew G. Knepley .keywords: mesh, points
2776aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2777aa50250dSMatthew G. Knepley @*/
2778aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
2779aa50250dSMatthew G. Knepley {
2780aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
2781aa50250dSMatthew G. Knepley 
2782aa50250dSMatthew G. Knepley   PetscFunctionBegin;
2783aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2784aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
2785c58f1c22SToby Isaac   if (!dm->depthLabel) {ierr = DMGetLabel(dm, "depth", &dm->depthLabel);CHKERRQ(ierr);}
2786c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
2787aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
2788aa50250dSMatthew G. Knepley }
2789aa50250dSMatthew G. Knepley 
2790aa50250dSMatthew G. Knepley /*@
2791aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
2792aa50250dSMatthew G. Knepley 
2793aa50250dSMatthew G. Knepley   Not Collective
2794aa50250dSMatthew G. Knepley 
2795aa50250dSMatthew G. Knepley   Input Parameter:
2796aa50250dSMatthew G. Knepley . dm    - The DMPlex object
2797aa50250dSMatthew G. Knepley 
2798aa50250dSMatthew G. Knepley   Output Parameter:
2799aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
2800aa50250dSMatthew G. Knepley 
2801aa50250dSMatthew G. Knepley   Level: developer
2802552f7358SJed Brown 
2803552f7358SJed Brown .keywords: mesh, points
2804aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2805552f7358SJed Brown @*/
2806552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
2807552f7358SJed Brown {
2808aa50250dSMatthew G. Knepley   DMLabel        label;
2809aa50250dSMatthew G. Knepley   PetscInt       d = 0;
2810552f7358SJed Brown   PetscErrorCode ierr;
2811552f7358SJed Brown 
2812552f7358SJed Brown   PetscFunctionBegin;
2813552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2814552f7358SJed Brown   PetscValidPointer(depth, 2);
2815aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2816aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
2817552f7358SJed Brown   *depth = d-1;
2818552f7358SJed Brown   PetscFunctionReturn(0);
2819552f7358SJed Brown }
2820552f7358SJed Brown 
2821552f7358SJed Brown /*@
2822552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
2823552f7358SJed Brown 
2824552f7358SJed Brown   Not Collective
2825552f7358SJed Brown 
2826552f7358SJed Brown   Input Parameters:
2827552f7358SJed Brown + dm           - The DMPlex object
2828552f7358SJed Brown - stratumValue - The requested depth
2829552f7358SJed Brown 
2830552f7358SJed Brown   Output Parameters:
2831552f7358SJed Brown + start - The first point at this depth
2832552f7358SJed Brown - end   - One beyond the last point at this depth
2833552f7358SJed Brown 
2834552f7358SJed Brown   Level: developer
2835552f7358SJed Brown 
2836552f7358SJed Brown .keywords: mesh, points
2837552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
2838552f7358SJed Brown @*/
28390adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
28400adebc6cSBarry Smith {
2841aa50250dSMatthew G. Knepley   DMLabel        label;
284263d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
2843552f7358SJed Brown   PetscErrorCode ierr;
2844552f7358SJed Brown 
2845552f7358SJed Brown   PetscFunctionBegin;
2846552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
284763d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
284863d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
2849552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
28500d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
285163d1a920SMatthew G. Knepley   if (stratumValue < 0) {
285263d1a920SMatthew G. Knepley     if (start) *start = pStart;
285363d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
285463d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
2855552f7358SJed Brown   }
2856aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
285763d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
285863d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
2859552f7358SJed Brown   PetscFunctionReturn(0);
2860552f7358SJed Brown }
2861552f7358SJed Brown 
2862552f7358SJed Brown /*@
2863552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
2864552f7358SJed Brown 
2865552f7358SJed Brown   Not Collective
2866552f7358SJed Brown 
2867552f7358SJed Brown   Input Parameters:
2868552f7358SJed Brown + dm           - The DMPlex object
2869552f7358SJed Brown - stratumValue - The requested height
2870552f7358SJed Brown 
2871552f7358SJed Brown   Output Parameters:
2872552f7358SJed Brown + start - The first point at this height
2873552f7358SJed Brown - end   - One beyond the last point at this height
2874552f7358SJed Brown 
2875552f7358SJed Brown   Level: developer
2876552f7358SJed Brown 
2877552f7358SJed Brown .keywords: mesh, points
2878552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
2879552f7358SJed Brown @*/
28800adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
28810adebc6cSBarry Smith {
2882aa50250dSMatthew G. Knepley   DMLabel        label;
288363d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
2884552f7358SJed Brown   PetscErrorCode ierr;
2885552f7358SJed Brown 
2886552f7358SJed Brown   PetscFunctionBegin;
2887552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
288863d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
288963d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
2890552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
28910d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
289263d1a920SMatthew G. Knepley   if (stratumValue < 0) {
289363d1a920SMatthew G. Knepley     if (start) *start = pStart;
289463d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
289563d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
2896552f7358SJed Brown   }
2897aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2898aa50250dSMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");CHKERRQ(ierr);
289963d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
290063d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
2901552f7358SJed Brown   PetscFunctionReturn(0);
2902552f7358SJed Brown }
2903552f7358SJed Brown 
2904552f7358SJed Brown /* Set the number of dof on each point and separate by fields */
2905a6dfd86eSKarl Rupp PetscErrorCode DMPlexCreateSectionInitial(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscSection *section)
2906a6dfd86eSKarl Rupp {
2907ee55978aSMatthew G. Knepley   PetscInt      *pMax;
2908916f0f19SMatthew G. Knepley   PetscInt       depth, pStart = 0, pEnd = 0;
2909ee55978aSMatthew G. Knepley   PetscInt       Nf, p, d, dep, f;
2910fa716be8SMatthew G. Knepley   PetscBool     *isFE;
2911552f7358SJed Brown   PetscErrorCode ierr;
2912552f7358SJed Brown 
2913552f7358SJed Brown   PetscFunctionBegin;
2914fa716be8SMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
2915ee55978aSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
2916fa716be8SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
2917fa716be8SMatthew G. Knepley     PetscObject  obj;
2918fa716be8SMatthew G. Knepley     PetscClassId id;
2919fa716be8SMatthew G. Knepley 
2920ee55978aSMatthew G. Knepley     isFE[f] = PETSC_FALSE;
2921ee55978aSMatthew G. Knepley     if (f >= Nf) continue;
2922fa716be8SMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
2923fa716be8SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2924fa716be8SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
2925fa716be8SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
2926552f7358SJed Brown   }
292782f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), section);CHKERRQ(ierr);
2928552f7358SJed Brown   if (numFields > 0) {
2929552f7358SJed Brown     ierr = PetscSectionSetNumFields(*section, numFields);CHKERRQ(ierr);
2930552f7358SJed Brown     if (numComp) {
2931552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
2932552f7358SJed Brown         ierr = PetscSectionSetFieldComponents(*section, f, numComp[f]);CHKERRQ(ierr);
2933d484f18aSToby Isaac         if (isFE[f]) {
2934d484f18aSToby Isaac           PetscFE           fe;
2935d484f18aSToby Isaac           PetscDualSpace    dspace;
2936d484f18aSToby Isaac           const PetscInt    ***perms;
2937d484f18aSToby Isaac           const PetscScalar ***flips;
2938d484f18aSToby Isaac           const PetscInt    *numDof;
2939d484f18aSToby Isaac 
2940d484f18aSToby Isaac           ierr = DMGetField(dm,f,(PetscObject *) &fe);CHKERRQ(ierr);
2941d484f18aSToby Isaac           ierr = PetscFEGetDualSpace(fe,&dspace);CHKERRQ(ierr);
2942d484f18aSToby Isaac           ierr = PetscDualSpaceGetSymmetries(dspace,&perms,&flips);CHKERRQ(ierr);
2943d484f18aSToby Isaac           ierr = PetscDualSpaceGetNumDof(dspace,&numDof);CHKERRQ(ierr);
2944d484f18aSToby Isaac           if (perms || flips) {
2945d484f18aSToby Isaac             DM               K;
2946d484f18aSToby Isaac             DMLabel          depthLabel;
2947d484f18aSToby Isaac             PetscInt         depth, h;
2948d484f18aSToby Isaac             PetscSectionSym  sym;
2949d484f18aSToby Isaac 
2950d484f18aSToby Isaac             ierr = PetscDualSpaceGetDM(dspace,&K);CHKERRQ(ierr);
2951d484f18aSToby Isaac             ierr = DMPlexGetDepthLabel(dm,&depthLabel);CHKERRQ(ierr);
2952d484f18aSToby Isaac             ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
2953d484f18aSToby Isaac             ierr = PetscSectionSymCreateLabel(PetscObjectComm((PetscObject)*section),depthLabel,&sym);CHKERRQ(ierr);
2954d484f18aSToby Isaac             for (h = 0; h <= depth; h++) {
2955d484f18aSToby Isaac               PetscDualSpace    hspace;
2956d484f18aSToby Isaac               PetscInt          kStart, kEnd;
2957d484f18aSToby Isaac               PetscInt          kConeSize;
2958d484f18aSToby Isaac               const PetscInt    **perms0 = NULL;
2959d484f18aSToby Isaac               const PetscScalar **flips0 = NULL;
2960d484f18aSToby Isaac 
2961d484f18aSToby Isaac               ierr = PetscDualSpaceGetHeightSubspace(dspace,h,&hspace);CHKERRQ(ierr);
2962d484f18aSToby Isaac               ierr = DMPlexGetHeightStratum(K,h,&kStart,&kEnd);CHKERRQ(ierr);
2963d484f18aSToby Isaac               if (!hspace) continue;
2964d484f18aSToby Isaac               ierr = PetscDualSpaceGetSymmetries(hspace,&perms,&flips);CHKERRQ(ierr);
2965d484f18aSToby Isaac               if (perms) perms0 = perms[0];
2966d484f18aSToby Isaac               if (flips) flips0 = flips[0];
2967d484f18aSToby Isaac               if (!(perms0 || flips0)) continue;
2968d484f18aSToby Isaac               ierr = DMPlexGetConeSize(K,kStart,&kConeSize);CHKERRQ(ierr);
2969d484f18aSToby Isaac               if (numComp[f] == 1) {
2970d484f18aSToby Isaac                 ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numDof[depth - h],-kConeSize,kConeSize,PETSC_USE_POINTER,perms0 ? &perms0[-kConeSize] : NULL,flips0 ? &flips0[-kConeSize] : NULL);CHKERRQ(ierr);
2971d484f18aSToby Isaac               } else {
2972d484f18aSToby Isaac                 PetscInt    **fieldPerms = NULL, o;
2973d484f18aSToby Isaac                 PetscScalar **fieldFlips = NULL;
2974d484f18aSToby Isaac 
2975d484f18aSToby Isaac                 ierr = PetscCalloc1(2 * kConeSize,&fieldPerms);CHKERRQ(ierr);
2976d484f18aSToby Isaac                 ierr = PetscCalloc1(2 * kConeSize,&fieldFlips);CHKERRQ(ierr);
2977d484f18aSToby Isaac                 for (o = -kConeSize; o < kConeSize; o++) {
2978d484f18aSToby Isaac                   if (perms0 && perms0[o]) {
2979d484f18aSToby Isaac                     PetscInt r, s;
2980d484f18aSToby Isaac 
2981d484f18aSToby Isaac                     ierr = PetscMalloc1(numComp[f] * numDof[depth - h],&fieldPerms[o+kConeSize]);CHKERRQ(ierr);
2982d484f18aSToby Isaac                     for (r = 0; r < numDof[depth - h]; r++) {
2983d484f18aSToby Isaac                       for (s = 0; s < numComp[f]; s++) {
2984d484f18aSToby Isaac                         fieldPerms[o+kConeSize][r * numComp[f] + s] = numComp[f] * perms0[o][r] + s;
2985d484f18aSToby Isaac                       }
2986d484f18aSToby Isaac                     }
2987d484f18aSToby Isaac                   }
2988d484f18aSToby Isaac                   if (flips0 && flips0[o]) {
2989d484f18aSToby Isaac                     PetscInt r, s;
2990d484f18aSToby Isaac 
2991d484f18aSToby Isaac                     ierr = PetscMalloc1(numComp[f] * numDof[depth - h],&fieldFlips[o+kConeSize]);CHKERRQ(ierr);
2992d484f18aSToby Isaac                     for (r = 0; r < numDof[depth - h]; r++) {
2993d484f18aSToby Isaac                       for (s = 0; s < numComp[f]; s++) {
2994d484f18aSToby Isaac                         fieldFlips[o+kConeSize][r * numComp[f] + s] = flips0[o][r];
2995d484f18aSToby Isaac                       }
2996d484f18aSToby Isaac                     }
2997d484f18aSToby Isaac                   }
2998d484f18aSToby Isaac                 }
2999d484f18aSToby Isaac                 ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numComp[f] * numDof[depth - h],-kConeSize,kConeSize,PETSC_OWN_POINTER,(const PetscInt **) fieldPerms,(const PetscScalar **)fieldFlips);CHKERRQ(ierr);
3000d484f18aSToby Isaac               }
3001d484f18aSToby Isaac             }
3002d484f18aSToby Isaac             ierr = PetscSectionSetFieldSym(*section,f,sym);CHKERRQ(ierr);
3003d484f18aSToby Isaac             ierr = PetscSectionSymDestroy(&sym);CHKERRQ(ierr);
3004d484f18aSToby Isaac           }
3005d484f18aSToby Isaac         }
3006552f7358SJed Brown       }
3007552f7358SJed Brown     }
3008552f7358SJed Brown   }
3009552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3010552f7358SJed Brown   ierr = PetscSectionSetChart(*section, pStart, pEnd);CHKERRQ(ierr);
3011916f0f19SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
30129ac3fadcSMatthew G. Knepley   ierr = PetscMalloc1(depth+1,&pMax);CHKERRQ(ierr);
30139ac3fadcSMatthew 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);
3014916f0f19SMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
3015916f0f19SMatthew G. Knepley     d    = dim == depth ? dep : (!dep ? 0 : dim);
3016916f0f19SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dep, &pStart, &pEnd);CHKERRQ(ierr);
3017fa716be8SMatthew G. Knepley     pMax[dep] = pMax[dep] < 0 ? pEnd : pMax[dep];
3018552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
3019ee55978aSMatthew G. Knepley       PetscInt tot = 0;
3020ee55978aSMatthew G. Knepley 
3021552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3022fa716be8SMatthew G. Knepley         if (isFE[f] && p >= pMax[dep]) continue;
3023552f7358SJed Brown         ierr = PetscSectionSetFieldDof(*section, p, f, numDof[f*(dim+1)+d]);CHKERRQ(ierr);
3024ee55978aSMatthew G. Knepley         tot += numDof[f*(dim+1)+d];
3025552f7358SJed Brown       }
3026ee55978aSMatthew G. Knepley       ierr = PetscSectionSetDof(*section, p, tot);CHKERRQ(ierr);
3027552f7358SJed Brown     }
3028552f7358SJed Brown   }
30299ac3fadcSMatthew G. Knepley   ierr = PetscFree(pMax);CHKERRQ(ierr);
3030fa716be8SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
3031552f7358SJed Brown   PetscFunctionReturn(0);
3032552f7358SJed Brown }
3033552f7358SJed Brown 
3034552f7358SJed Brown /* Set the number of dof on each point and separate by fields
3035ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3036552f7358SJed Brown */
3037ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSectionBCDof(DM dm, PetscInt numBC, const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
3038a6dfd86eSKarl Rupp {
3039552f7358SJed Brown   PetscInt       numFields;
3040552f7358SJed Brown   PetscInt       bc;
3041a68b90caSToby Isaac   PetscSection   aSec;
3042552f7358SJed Brown   PetscErrorCode ierr;
3043552f7358SJed Brown 
3044552f7358SJed Brown   PetscFunctionBegin;
3045552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3046552f7358SJed Brown   for (bc = 0; bc < numBC; ++bc) {
3047552f7358SJed Brown     PetscInt        field = 0;
3048ab1d0545SMatthew G. Knepley     const PetscInt *comp;
3049552f7358SJed Brown     const PetscInt *idx;
3050ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3051552f7358SJed Brown 
30520d644c17SKarl Rupp     if (numFields) field = bcField[bc];
3053ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3054ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3055552f7358SJed Brown     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3056552f7358SJed Brown     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3057552f7358SJed Brown     for (i = 0; i < n; ++i) {
3058552f7358SJed Brown       const PetscInt p = idx[i];
305906ff1081SMatthew G. Knepley       PetscInt       numConst;
3060552f7358SJed Brown 
3061552f7358SJed Brown       if (numFields) {
3062552f7358SJed Brown         ierr = PetscSectionGetFieldDof(section, p, field, &numConst);CHKERRQ(ierr);
3063552f7358SJed Brown       } else {
3064552f7358SJed Brown         ierr = PetscSectionGetDof(section, p, &numConst);CHKERRQ(ierr);
3065552f7358SJed Brown       }
306606ff1081SMatthew G. Knepley       /* If Nc < 0, constrain every dof on the point */
306706ff1081SMatthew G. Knepley       if (Nc > 0) numConst = PetscMin(numConst, Nc);
306806ff1081SMatthew G. Knepley       if (numFields) {ierr = PetscSectionAddFieldConstraintDof(section, p, field, numConst);CHKERRQ(ierr);}
3069552f7358SJed Brown       ierr = PetscSectionAddConstraintDof(section, p, numConst);CHKERRQ(ierr);
3070552f7358SJed Brown     }
3071552f7358SJed Brown     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
307206ff1081SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3073552f7358SJed Brown   }
3074a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3075a68b90caSToby Isaac   if (aSec) {
3076a68b90caSToby Isaac     PetscInt aStart, aEnd, a;
3077a68b90caSToby Isaac 
3078a68b90caSToby Isaac     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3079a68b90caSToby Isaac     for (a = aStart; a < aEnd; a++) {
3080ab1d0545SMatthew G. Knepley       PetscInt dof, f;
3081a68b90caSToby Isaac 
3082a68b90caSToby Isaac       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3083a68b90caSToby Isaac       if (dof) {
3084a68b90caSToby Isaac         /* if there are point-to-point constraints, then all dofs are constrained */
3085a68b90caSToby Isaac         ierr = PetscSectionGetDof(section, a, &dof);CHKERRQ(ierr);
3086a68b90caSToby Isaac         ierr = PetscSectionSetConstraintDof(section, a, dof);CHKERRQ(ierr);
3087a68b90caSToby Isaac         for (f = 0; f < numFields; f++) {
3088a68b90caSToby Isaac           ierr = PetscSectionGetFieldDof(section, a, f, &dof);CHKERRQ(ierr);
3089a68b90caSToby Isaac           ierr = PetscSectionSetFieldConstraintDof(section, a, f, dof);CHKERRQ(ierr);
3090a68b90caSToby Isaac         }
3091a68b90caSToby Isaac       }
3092a68b90caSToby Isaac     }
3093a68b90caSToby Isaac   }
3094552f7358SJed Brown   PetscFunctionReturn(0);
3095552f7358SJed Brown }
3096552f7358SJed Brown 
3097ab1d0545SMatthew G. Knepley /* Set the constrained field indices on each point
3098ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3099ab1d0545SMatthew G. Knepley */
3100ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSectionBCIndicesField(DM dm, PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
31010adebc6cSBarry Smith {
3102ab1d0545SMatthew G. Knepley   PetscSection   aSec;
3103ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3104ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, bc, f, d;
3105552f7358SJed Brown   PetscErrorCode ierr;
3106552f7358SJed Brown 
3107552f7358SJed Brown   PetscFunctionBegin;
3108552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3109ab1d0545SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
3110ab1d0545SMatthew G. Knepley   /* Initialize all field indices to -1 */
3111552f7358SJed Brown   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3112ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3113ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3114ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3115ab1d0545SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) for (f = 0; f < numFields; ++f) {ierr = PetscSectionSetFieldConstraintIndices(section, p, f, indices);CHKERRQ(ierr);}
3116ab1d0545SMatthew G. Knepley   /* Handle BC constraints */
3117ab1d0545SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {
3118ab1d0545SMatthew G. Knepley     const PetscInt  field = bcField[bc];
3119ab1d0545SMatthew G. Knepley     const PetscInt *comp, *idx;
3120ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3121552f7358SJed Brown 
3122ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3123ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3124ab1d0545SMatthew G. Knepley     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3125ab1d0545SMatthew G. Knepley     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3126ab1d0545SMatthew G. Knepley     for (i = 0; i < n; ++i) {
3127ab1d0545SMatthew G. Knepley       const PetscInt  p = idx[i];
3128ab1d0545SMatthew G. Knepley       const PetscInt *find;
3129ab1d0545SMatthew G. Knepley       PetscInt        fcdof, c;
3130ab1d0545SMatthew G. Knepley 
3131ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintDof(section, p, field, &fcdof);CHKERRQ(ierr);
3132ab1d0545SMatthew G. Knepley       if (Nc < 0) {
3133ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) indices[d] = d;
3134552f7358SJed Brown       } else {
3135ab1d0545SMatthew G. Knepley         ierr = PetscSectionGetFieldConstraintIndices(section, p, field, &find);CHKERRQ(ierr);
3136ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) {if (find[d] < 0) break; indices[d] = find[d];}
3137ab1d0545SMatthew G. Knepley         for (c = 0; c < Nc; ++c) indices[d+c] = comp[c];
3138ab1d0545SMatthew G. Knepley         ierr = PetscSortInt(d+Nc, indices);CHKERRQ(ierr);
3139ab1d0545SMatthew G. Knepley         for (c = d+Nc; c < fcdof; ++c) indices[c] = -1;
3140552f7358SJed Brown       }
3141ab1d0545SMatthew G. Knepley       ierr = PetscSectionSetFieldConstraintIndices(section, p, field, indices);CHKERRQ(ierr);
3142552f7358SJed Brown     }
3143ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3144ab1d0545SMatthew G. Knepley     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3145552f7358SJed Brown   }
3146ab1d0545SMatthew G. Knepley   /* Handle anchors */
3147ab1d0545SMatthew G. Knepley   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3148ab1d0545SMatthew G. Knepley   if (aSec) {
3149ab1d0545SMatthew G. Knepley     PetscInt aStart, aEnd, a;
3150552f7358SJed Brown 
3151ab1d0545SMatthew G. Knepley     for (d = 0; d < maxDof; ++d) indices[d] = d;
3152ab1d0545SMatthew G. Knepley     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3153ab1d0545SMatthew G. Knepley     for (a = aStart; a < aEnd; a++) {
3154ab1d0545SMatthew G. Knepley       PetscInt dof, fdof, f;
3155ab1d0545SMatthew G. Knepley 
3156ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3157ab1d0545SMatthew G. Knepley       if (dof) {
3158ab1d0545SMatthew G. Knepley         /* if there are point-to-point constraints, then all dofs are constrained */
3159ab1d0545SMatthew G. Knepley         for (f = 0; f < numFields; f++) {
3160ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, a, f, &fdof);CHKERRQ(ierr);
3161ab1d0545SMatthew G. Knepley           ierr = PetscSectionSetFieldConstraintIndices(section, a, f, indices);CHKERRQ(ierr);
3162ab1d0545SMatthew G. Knepley         }
3163ab1d0545SMatthew G. Knepley       }
3164ab1d0545SMatthew G. Knepley     }
3165ab1d0545SMatthew G. Knepley   }
3166ab1d0545SMatthew G. Knepley   ierr = PetscFree(indices);CHKERRQ(ierr);
3167ab1d0545SMatthew G. Knepley   PetscFunctionReturn(0);
3168ab1d0545SMatthew G. Knepley }
3169ab1d0545SMatthew G. Knepley 
3170ab1d0545SMatthew G. Knepley /* Set the constrained indices on each point */
3171ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSectionBCIndices(DM dm, PetscSection section)
3172ab1d0545SMatthew G. Knepley {
3173ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3174ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, f, d;
3175ab1d0545SMatthew G. Knepley   PetscErrorCode ierr;
3176ab1d0545SMatthew G. Knepley 
3177ab1d0545SMatthew G. Knepley   PetscFunctionBegin;
3178ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3179ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3180ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3181ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3182ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3183552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3184552f7358SJed Brown     PetscInt cdof, d;
3185552f7358SJed Brown 
3186552f7358SJed Brown     ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
3187552f7358SJed Brown     if (cdof) {
3188552f7358SJed Brown       if (numFields) {
3189552f7358SJed Brown         PetscInt numConst = 0, foff = 0;
3190552f7358SJed Brown 
3191552f7358SJed Brown         for (f = 0; f < numFields; ++f) {
3192ab1d0545SMatthew G. Knepley           const PetscInt *find;
3193ab1d0545SMatthew G. Knepley           PetscInt        fcdof, fdof;
3194552f7358SJed Brown 
3195552f7358SJed Brown           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
3196ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
3197ab1d0545SMatthew G. Knepley           /* Change constraint numbering from field component to local dof number */
3198ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintIndices(section, p, f, &find);CHKERRQ(ierr);
3199ab1d0545SMatthew G. Knepley           for (d = 0; d < fcdof; ++d) indices[numConst+d] = find[d] + foff;
3200ab1d0545SMatthew G. Knepley           numConst += fcdof;
3201552f7358SJed Brown           foff     += fdof;
3202552f7358SJed Brown         }
3203552f7358SJed Brown         if (cdof != numConst) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Total number of field constraints %D should be %D", numConst, cdof);
3204552f7358SJed Brown       } else {
32050d644c17SKarl Rupp         for (d = 0; d < cdof; ++d) indices[d] = d;
3206552f7358SJed Brown       }
3207552f7358SJed Brown       ierr = PetscSectionSetConstraintIndices(section, p, indices);CHKERRQ(ierr);
3208552f7358SJed Brown     }
3209552f7358SJed Brown   }
3210552f7358SJed Brown   ierr = PetscFree(indices);CHKERRQ(ierr);
3211552f7358SJed Brown   PetscFunctionReturn(0);
3212552f7358SJed Brown }
3213552f7358SJed Brown 
3214552f7358SJed Brown /*@C
3215552f7358SJed Brown   DMPlexCreateSection - Create a PetscSection based upon the dof layout specification provided.
3216552f7358SJed Brown 
3217552f7358SJed Brown   Not Collective
3218552f7358SJed Brown 
3219552f7358SJed Brown   Input Parameters:
3220552f7358SJed Brown + dm        - The DMPlex object
3221552f7358SJed Brown . dim       - The spatial dimension of the problem
3222552f7358SJed Brown . numFields - The number of fields in the problem
3223552f7358SJed Brown . numComp   - An array of size numFields that holds the number of components for each field
3224552f7358SJed 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
3225552f7358SJed Brown . numBC     - The number of boundary conditions
3226552f7358SJed Brown . bcField   - An array of size numBC giving the field number for each boundry condition
3227ab1d0545SMatthew G. Knepley . bcComps   - [Optional] An array of size numBC giving an IS holding the field components to which each boundary condition applies
3228ab1d0545SMatthew G. Knepley . bcPoints  - An array of size numBC giving an IS holding the Plex points to which each boundary condition applies
3229f8f126e8SMatthew G. Knepley - perm      - Optional permutation of the chart, or NULL
3230552f7358SJed Brown 
3231552f7358SJed Brown   Output Parameter:
3232552f7358SJed Brown . section - The PetscSection object
3233552f7358SJed Brown 
3234552f7358SJed 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
3235f8f126e8SMatthew G. Knepley   number of dof for field 0 on each edge.
3236f8f126e8SMatthew G. Knepley 
3237f8f126e8SMatthew G. Knepley   The chart permutation is the same one set using PetscSectionSetPermutation()
3238552f7358SJed Brown 
3239552f7358SJed Brown   Level: developer
3240552f7358SJed Brown 
32413813dfbdSMatthew G Knepley   Fortran Notes:
32423813dfbdSMatthew G Knepley   A Fortran 90 version is available as DMPlexCreateSectionF90()
32433813dfbdSMatthew G Knepley 
3244552f7358SJed Brown .keywords: mesh, elements
3245f8f126e8SMatthew G. Knepley .seealso: DMPlexCreate(), PetscSectionCreate(), PetscSectionSetPermutation()
3246552f7358SJed Brown @*/
3247ab1d0545SMatthew 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)
3248a6dfd86eSKarl Rupp {
3249a68b90caSToby Isaac   PetscSection   aSec;
3250552f7358SJed Brown   PetscErrorCode ierr;
3251552f7358SJed Brown 
3252552f7358SJed Brown   PetscFunctionBegin;
3253552f7358SJed Brown   ierr = DMPlexCreateSectionInitial(dm, dim, numFields, numComp, numDof, section);CHKERRQ(ierr);
3254ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSectionBCDof(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3255f8f126e8SMatthew G. Knepley   if (perm) {ierr = PetscSectionSetPermutation(*section, perm);CHKERRQ(ierr);}
3256552f7358SJed Brown   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
3257a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,NULL);CHKERRQ(ierr);
3258ab1d0545SMatthew G. Knepley   if (numBC || aSec) {
3259ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndicesField(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3260ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndices(dm, *section);CHKERRQ(ierr);
3261ab1d0545SMatthew G. Knepley   }
3262ce1779c8SBarry Smith   ierr = PetscSectionViewFromOptions(*section,NULL,"-section_view");CHKERRQ(ierr);
3263552f7358SJed Brown   PetscFunctionReturn(0);
3264552f7358SJed Brown }
3265552f7358SJed Brown 
32660adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
32670adebc6cSBarry Smith {
3268efe440bfSMatthew G. Knepley   PetscSection   section, s;
3269efe440bfSMatthew G. Knepley   Mat            m;
32703e922f36SToby Isaac   PetscInt       maxHeight;
3271552f7358SJed Brown   PetscErrorCode ierr;
3272552f7358SJed Brown 
3273552f7358SJed Brown   PetscFunctionBegin;
327438221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
32753e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
32763e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
327782f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
3278552f7358SJed Brown   ierr = DMSetDefaultSection(*cdm, section);CHKERRQ(ierr);
32791d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3280efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3281efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3282efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3283efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3284efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
3285552f7358SJed Brown   PetscFunctionReturn(0);
3286552f7358SJed Brown }
3287552f7358SJed Brown 
32880adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
32890adebc6cSBarry Smith {
3290552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3291552f7358SJed Brown 
3292552f7358SJed Brown   PetscFunctionBegin;
3293552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3294552f7358SJed Brown   if (section) *section = mesh->coneSection;
3295552f7358SJed Brown   PetscFunctionReturn(0);
3296552f7358SJed Brown }
3297552f7358SJed Brown 
32988cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
32998cb4d582SMatthew G. Knepley {
33008cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
33018cb4d582SMatthew G. Knepley 
33028cb4d582SMatthew G. Knepley   PetscFunctionBegin;
33038cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33048cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
33058cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
33068cb4d582SMatthew G. Knepley }
33078cb4d582SMatthew G. Knepley 
3308a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3309a6dfd86eSKarl Rupp {
3310552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3311552f7358SJed Brown 
3312552f7358SJed Brown   PetscFunctionBegin;
3313552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3314552f7358SJed Brown   if (cones) *cones = mesh->cones;
3315552f7358SJed Brown   PetscFunctionReturn(0);
3316552f7358SJed Brown }
3317552f7358SJed Brown 
3318a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3319a6dfd86eSKarl Rupp {
3320552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3321552f7358SJed Brown 
3322552f7358SJed Brown   PetscFunctionBegin;
3323552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3324552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3325552f7358SJed Brown   PetscFunctionReturn(0);
3326552f7358SJed Brown }
3327552f7358SJed Brown 
3328552f7358SJed Brown /******************************** FEM Support **********************************/
3329552f7358SJed Brown 
33303194fc30SMatthew G. Knepley PetscErrorCode DMPlexCreateSpectralClosurePermutation(DM dm, PetscSection section)
33313194fc30SMatthew G. Knepley {
33323194fc30SMatthew G. Knepley   PetscInt      *perm;
333389eabcffSMatthew G. Knepley   PetscInt       dim, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
33343194fc30SMatthew G. Knepley   PetscErrorCode ierr;
33353194fc30SMatthew G. Knepley 
33363194fc30SMatthew G. Knepley   PetscFunctionBegin;
33373194fc30SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
33383194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
33393194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
334089eabcffSMatthew G. Knepley   if (dim <= 1) PetscFunctionReturn(0);
33413194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
33423194fc30SMatthew G. Knepley     /* An order k SEM disc has k-1 dofs on an edge */
33433194fc30SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
33443194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
33453194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
33463194fc30SMatthew G. Knepley     k = k/Nc + 1;
334789eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
33483194fc30SMatthew G. Knepley   }
33493194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
33503194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
335189eabcffSMatthew G. Knepley     switch (dim) {
335289eabcffSMatthew G. Knepley     case 2:
33533194fc30SMatthew 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} */
33543194fc30SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
33553194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
33563194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
33573194fc30SMatthew G. Knepley       k = k/Nc + 1;
33583194fc30SMatthew G. Knepley       /* The SEM order is
33593194fc30SMatthew G. Knepley 
33603194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
336189eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
33623194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
33633194fc30SMatthew G. Knepley       */
33643194fc30SMatthew G. Knepley       {
33653194fc30SMatthew G. Knepley         const PetscInt of   = 0;
33663194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
33673194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
33683194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
33693194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
33703194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
33713194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
33723194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
33733194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
33743194fc30SMatthew G. Knepley         PetscInt       o;
33753194fc30SMatthew G. Knepley 
33763194fc30SMatthew G. Knepley         /* bottom */
33773194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
33783194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
33793194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
33803194fc30SMatthew G. Knepley         /* middle */
33813194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
33823194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
33833194fc30SMatthew 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;
33843194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
33853194fc30SMatthew G. Knepley         }
33863194fc30SMatthew G. Knepley         /* top */
33873194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
33883194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
33893194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
33903194fc30SMatthew G. Knepley         foffset = offset;
33913194fc30SMatthew G. Knepley       }
339289eabcffSMatthew G. Knepley       break;
339389eabcffSMatthew G. Knepley     case 3:
339489eabcffSMatthew G. Knepley       /* The original hex closure is
339589eabcffSMatthew G. Knepley 
339689eabcffSMatthew G. Knepley          {c,
339789eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
339889eabcffSMatthew 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,
339989eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
340089eabcffSMatthew G. Knepley       */
340189eabcffSMatthew G. Knepley       ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
340289eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
340389eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
340489eabcffSMatthew G. Knepley       k = k/Nc + 1;
340589eabcffSMatthew G. Knepley       /* The SEM order is
340689eabcffSMatthew G. Knepley          Bottom Slice
340789eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
340889eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
340989eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
341089eabcffSMatthew G. Knepley 
341189eabcffSMatthew G. Knepley          Middle Slice (j)
341289eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
341389eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
341489eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
341589eabcffSMatthew G. Knepley 
341689eabcffSMatthew G. Knepley          Top Slice
341789eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
341889eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
341989eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
342089eabcffSMatthew G. Knepley       */
342189eabcffSMatthew G. Knepley       {
342289eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
342389eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
342489eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
342589eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
342689eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
342789eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
342889eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
342989eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
343089eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
343189eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
343289eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
343389eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
343489eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
343589eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
343689eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
343789eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
343889eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
343989eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
344089eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
344189eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
344289eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
344389eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
344489eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
344589eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
344689eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
344789eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
344889eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
344989eabcffSMatthew G. Knepley         PetscInt       o, n;
345089eabcffSMatthew G. Knepley 
345189eabcffSMatthew G. Knepley         /* Bottom Slice */
345289eabcffSMatthew G. Knepley         /*   bottom */
345389eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
345489eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
345589eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
345689eabcffSMatthew G. Knepley         /*   middle */
345789eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
345889eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
3459316b7f87SMax 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;}
346089eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
34613194fc30SMatthew G. Knepley         }
346289eabcffSMatthew G. Knepley         /*   top */
346389eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
346489eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
346589eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
346689eabcffSMatthew G. Knepley 
346789eabcffSMatthew G. Knepley         /* Middle Slice */
346889eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
346989eabcffSMatthew G. Knepley           /*   bottom */
347089eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
347189eabcffSMatthew 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;
347289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
347389eabcffSMatthew G. Knepley           /*   middle */
347489eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
347589eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
347689eabcffSMatthew 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;
347789eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
347889eabcffSMatthew G. Knepley           }
347989eabcffSMatthew G. Knepley           /*   top */
348089eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
348189eabcffSMatthew 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;
348289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
348389eabcffSMatthew G. Knepley         }
348489eabcffSMatthew G. Knepley 
348589eabcffSMatthew G. Knepley         /* Top Slice */
348689eabcffSMatthew G. Knepley         /*   bottom */
348789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
348889eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
348989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
349089eabcffSMatthew G. Knepley         /*   middle */
349189eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
349289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
349389eabcffSMatthew 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;
349489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
349589eabcffSMatthew G. Knepley         }
349689eabcffSMatthew G. Knepley         /*   top */
349789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
349889eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
349989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
350089eabcffSMatthew G. Knepley 
350189eabcffSMatthew G. Knepley         foffset = offset;
350289eabcffSMatthew G. Knepley       }
350389eabcffSMatthew G. Knepley       break;
350489eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
350589eabcffSMatthew G. Knepley     }
350689eabcffSMatthew G. Knepley   }
350789eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
35083194fc30SMatthew G. Knepley   /* Check permutation */
35093194fc30SMatthew G. Knepley   {
35103194fc30SMatthew G. Knepley     PetscInt *check;
35113194fc30SMatthew G. Knepley 
35123194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
35133194fc30SMatthew 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]);}
35143194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
35153194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
35163194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
35173194fc30SMatthew G. Knepley   }
35181fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
35193194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
35203194fc30SMatthew G. Knepley }
35213194fc30SMatthew G. Knepley 
3522e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
3523e071409bSToby Isaac {
3524e071409bSToby Isaac   PetscDS        prob;
3525e071409bSToby Isaac   PetscInt       depth, Nf, h;
3526e071409bSToby Isaac   DMLabel        label;
3527e071409bSToby Isaac   PetscErrorCode ierr;
3528e071409bSToby Isaac 
3529e071409bSToby Isaac   PetscFunctionBeginHot;
3530e071409bSToby Isaac   prob    = dm->prob;
3531e071409bSToby Isaac   Nf      = prob->Nf;
3532e071409bSToby Isaac   label   = dm->depthLabel;
3533e071409bSToby Isaac   *dspace = NULL;
3534e071409bSToby Isaac   if (field < Nf) {
3535e071409bSToby Isaac     PetscObject disc = prob->disc[field];
3536e071409bSToby Isaac 
3537e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
3538e071409bSToby Isaac       PetscDualSpace dsp;
3539e071409bSToby Isaac 
3540e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
3541e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
3542e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
3543e071409bSToby Isaac       h    = depth - 1 - h;
3544e071409bSToby Isaac       if (h) {
3545e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
3546e071409bSToby Isaac       } else {
3547e071409bSToby Isaac         *dspace = dsp;
3548e071409bSToby Isaac       }
3549e071409bSToby Isaac     }
3550e071409bSToby Isaac   }
3551e071409bSToby Isaac   PetscFunctionReturn(0);
3552e071409bSToby Isaac }
3553e071409bSToby Isaac 
3554e071409bSToby Isaac 
35551a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3556a6dfd86eSKarl Rupp {
3557552f7358SJed Brown   PetscScalar    *array, *vArray;
3558d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
35591a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
3560552f7358SJed Brown   PetscErrorCode  ierr;
3561552f7358SJed Brown 
35621b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
35632a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
35645a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
35655a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
35665a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
35673f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
35689df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
35699df71ca4SMatthew G. Knepley       PetscInt dof;
3570d9917b9dSMatthew G. Knepley 
35719df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
35729df71ca4SMatthew G. Knepley       size += dof;
35739df71ca4SMatthew G. Knepley     }
35749df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
35759df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
35762a3aaacfSMatthew G. Knepley       PetscInt       dof;
35775a1bb5cfSMatthew G. Knepley 
35785a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
35792a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
35805a1bb5cfSMatthew G. Knepley       size += dof;
35815a1bb5cfSMatthew G. Knepley     }
35823f7cbbe7SMatthew G. Knepley     if (!values) {
35833f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
35843f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
35853f7cbbe7SMatthew G. Knepley     }
35865a1bb5cfSMatthew G. Knepley     ierr = DMGetWorkArray(dm, size, PETSC_SCALAR, &array);CHKERRQ(ierr);
3587982e9ed1SMatthew G. Knepley   } else {
3588982e9ed1SMatthew G. Knepley     array = *values;
3589982e9ed1SMatthew G. Knepley   }
35909df71ca4SMatthew G. Knepley   size = 0;
35915a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
35929df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
35939df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
35949df71ca4SMatthew G. Knepley     PetscScalar *varr;
3595d9917b9dSMatthew G. Knepley 
35969df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
35979df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
35989df71ca4SMatthew G. Knepley     varr = &vArray[off];
35991a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
36001a271a75SMatthew G. Knepley       array[offset] = varr[d];
36019df71ca4SMatthew G. Knepley     }
36029df71ca4SMatthew G. Knepley     size += dof;
36039df71ca4SMatthew G. Knepley   }
36049df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
36059df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
36069df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
36075a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
36085a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
36095a1bb5cfSMatthew G. Knepley 
361052ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
36115a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
36125a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
36135a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
36145a1bb5cfSMatthew G. Knepley     if (o >= 0) {
36151a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
36161a271a75SMatthew G. Knepley         array[offset] = varr[d];
36175a1bb5cfSMatthew G. Knepley       }
36185a1bb5cfSMatthew G. Knepley     } else {
36191a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
36201a271a75SMatthew G. Knepley         array[offset] = varr[d];
36215a1bb5cfSMatthew G. Knepley       }
36225a1bb5cfSMatthew G. Knepley     }
36239df71ca4SMatthew G. Knepley     size += dof;
36245a1bb5cfSMatthew G. Knepley   }
36255a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
36269df71ca4SMatthew G. Knepley   if (!*values) {
36275a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
36285a1bb5cfSMatthew G. Knepley     *values = array;
36299df71ca4SMatthew G. Knepley   } else {
36308ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
36318c312ff3SMatthew G. Knepley     *csize = size;
36329df71ca4SMatthew G. Knepley   }
36335a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
36345a1bb5cfSMatthew G. Knepley }
3635d9917b9dSMatthew G. Knepley 
3636923c78e0SToby Isaac static PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3637923c78e0SToby Isaac {
3638923c78e0SToby Isaac   const PetscInt *cla;
3639923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
3640923c78e0SToby Isaac   PetscErrorCode ierr;
3641923c78e0SToby Isaac 
3642923c78e0SToby Isaac   PetscFunctionBeginHot;
3643923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
3644923c78e0SToby Isaac   if (!*clPoints) {
3645923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
3646923c78e0SToby Isaac 
3647923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3648923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
3649923c78e0SToby Isaac     /* Compress out points not in the section */
3650923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
3651923c78e0SToby Isaac       PetscInt r = pts[2*p];
3652923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
3653923c78e0SToby Isaac         pts[q*2]   = r;
3654923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
3655923c78e0SToby Isaac         ++q;
3656923c78e0SToby Isaac       }
3657923c78e0SToby Isaac     }
3658923c78e0SToby Isaac     np = q;
3659923c78e0SToby Isaac     cla = NULL;
3660923c78e0SToby Isaac   } else {
3661923c78e0SToby Isaac     PetscInt dof, off;
3662923c78e0SToby Isaac 
3663923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
3664923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
3665923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
3666923c78e0SToby Isaac     np   = dof/2;
3667923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
3668923c78e0SToby Isaac   }
3669923c78e0SToby Isaac   *numPoints = np;
3670923c78e0SToby Isaac   *points    = pts;
3671923c78e0SToby Isaac   *clp       = cla;
3672923c78e0SToby Isaac 
3673923c78e0SToby Isaac   PetscFunctionReturn(0);
3674923c78e0SToby Isaac }
3675923c78e0SToby Isaac 
3676923c78e0SToby Isaac static PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3677923c78e0SToby Isaac {
3678923c78e0SToby Isaac   PetscErrorCode ierr;
3679923c78e0SToby Isaac 
3680923c78e0SToby Isaac   PetscFunctionBeginHot;
3681923c78e0SToby Isaac   if (!*clPoints) {
3682923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
3683923c78e0SToby Isaac   } else {
3684923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
3685923c78e0SToby Isaac   }
3686923c78e0SToby Isaac   *numPoints = 0;
3687923c78e0SToby Isaac   *points    = NULL;
3688923c78e0SToby Isaac   *clSec     = NULL;
3689923c78e0SToby Isaac   *clPoints  = NULL;
3690923c78e0SToby Isaac   *clp       = NULL;
3691923c78e0SToby Isaac   PetscFunctionReturn(0);
3692923c78e0SToby Isaac }
3693923c78e0SToby Isaac 
369497e99dd9SToby 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[])
36951a271a75SMatthew G. Knepley {
36961a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
369797e99dd9SToby Isaac   const PetscInt    **perms = NULL;
369897e99dd9SToby Isaac   const PetscScalar **flips = NULL;
36991a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
37001a271a75SMatthew G. Knepley 
37011a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3702fe02ba77SJed Brown   *size = 0;
370397e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
370497e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
370597e99dd9SToby Isaac     const PetscInt    point = points[2*p];
370697e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
370797e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
37081a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
37091a271a75SMatthew G. Knepley     const PetscScalar *varr;
37101a271a75SMatthew G. Knepley 
37111a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
37121a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
37131a271a75SMatthew G. Knepley     varr = &vArray[off];
371497e99dd9SToby Isaac     if (clperm) {
371597e99dd9SToby Isaac       if (perm) {
371697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
37171a271a75SMatthew G. Knepley       } else {
371897e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
371997e99dd9SToby Isaac       }
372097e99dd9SToby Isaac       if (flip) {
372197e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
372297e99dd9SToby Isaac       }
372397e99dd9SToby Isaac     } else {
372497e99dd9SToby Isaac       if (perm) {
372597e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
372697e99dd9SToby Isaac       } else {
372797e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
372897e99dd9SToby Isaac       }
372997e99dd9SToby Isaac       if (flip) {
373097e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
37311a271a75SMatthew G. Knepley       }
37321a271a75SMatthew G. Knepley     }
373397e99dd9SToby Isaac     offset += dof;
373497e99dd9SToby Isaac   }
373597e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
37361a271a75SMatthew G. Knepley   *size = offset;
37371a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
37381a271a75SMatthew G. Knepley }
37391a271a75SMatthew G. Knepley 
374097e99dd9SToby 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[])
37411a271a75SMatthew G. Knepley {
37421a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
37431a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
37441a271a75SMatthew G. Knepley 
37451a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3746fe02ba77SJed Brown   *size = 0;
37471a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
374897e99dd9SToby Isaac     PetscInt          p;
374997e99dd9SToby Isaac     const PetscInt    **perms = NULL;
375097e99dd9SToby Isaac     const PetscScalar **flips = NULL;
37511a271a75SMatthew G. Knepley 
375297e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
375397e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
375497e99dd9SToby Isaac       const PetscInt    point = points[2*p];
375597e99dd9SToby Isaac       PetscInt          fdof, foff, b;
37561a271a75SMatthew G. Knepley       const PetscScalar *varr;
375797e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
375897e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
37591a271a75SMatthew G. Knepley 
37601a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
37611a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
37621a271a75SMatthew G. Knepley       varr = &vArray[foff];
376397e99dd9SToby Isaac       if (clperm) {
376497e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
376597e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
376697e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
37671a271a75SMatthew G. Knepley       } else {
376897e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
376997e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
377097e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
37711a271a75SMatthew G. Knepley       }
377297e99dd9SToby Isaac       offset += fdof;
37731a271a75SMatthew G. Knepley     }
377497e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
37751a271a75SMatthew G. Knepley   }
37761a271a75SMatthew G. Knepley   *size = offset;
37771a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
37781a271a75SMatthew G. Knepley }
37791a271a75SMatthew G. Knepley 
3780552f7358SJed Brown /*@C
3781552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
3782552f7358SJed Brown 
3783552f7358SJed Brown   Not collective
3784552f7358SJed Brown 
3785552f7358SJed Brown   Input Parameters:
3786552f7358SJed Brown + dm - The DM
3787552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
3788552f7358SJed Brown . v - The local vector
3789552f7358SJed Brown - point - The sieve point in the DM
3790552f7358SJed Brown 
3791552f7358SJed Brown   Output Parameters:
3792552f7358SJed Brown + csize - The number of values in the closure, or NULL
3793552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
3794552f7358SJed Brown 
3795552f7358SJed Brown   Fortran Notes:
3796552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
3797552f7358SJed Brown   include petsc.h90 in your code.
3798552f7358SJed Brown 
3799552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
3800552f7358SJed Brown 
3801552f7358SJed Brown   Level: intermediate
3802552f7358SJed Brown 
3803552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
3804552f7358SJed Brown @*/
3805552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3806552f7358SJed Brown {
3807552f7358SJed Brown   PetscSection       clSection;
3808d9917b9dSMatthew G. Knepley   IS                 clPoints;
38098a84db2dSMatthew G. Knepley   PetscScalar       *array;
38108a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
3811552f7358SJed Brown   PetscInt          *points = NULL;
38128f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
38131a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
3814552f7358SJed Brown   PetscErrorCode     ierr;
3815552f7358SJed Brown 
3816d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
3817552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3818552f7358SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
38191a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
38201a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
3821552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3822552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3823552f7358SJed Brown   if (depth == 1 && numFields < 2) {
38241a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
3825552f7358SJed Brown     PetscFunctionReturn(0);
3826552f7358SJed Brown   }
38271a271a75SMatthew G. Knepley   /* Get points */
3828923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
38291fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
38301a271a75SMatthew G. Knepley   /* Get array */
3831bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
38321a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
3833552f7358SJed Brown 
38341a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
3835552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
38361a271a75SMatthew G. Knepley       asize += dof;
3837552f7358SJed Brown     }
3838bd6c0d32SMatthew G. Knepley     if (!values) {
3839923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
38401a271a75SMatthew G. Knepley       if (csize) *csize = asize;
3841bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
3842bd6c0d32SMatthew G. Knepley     }
38431a271a75SMatthew G. Knepley     ierr = DMGetWorkArray(dm, asize, PETSC_SCALAR, &array);CHKERRQ(ierr);
3844d0f6b257SMatthew G. Knepley   } else {
3845d0f6b257SMatthew G. Knepley     array = *values;
3846d0f6b257SMatthew G. Knepley   }
38478a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
38481a271a75SMatthew G. Knepley   /* Get values */
384997e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
385097e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
38511a271a75SMatthew G. Knepley   /* Cleanup points */
3852923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
38531a271a75SMatthew G. Knepley   /* Cleanup array */
38548a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
3855d0f6b257SMatthew G. Knepley   if (!*values) {
3856552f7358SJed Brown     if (csize) *csize = size;
3857552f7358SJed Brown     *values = array;
3858d0f6b257SMatthew G. Knepley   } else {
3859f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
3860d0f6b257SMatthew G. Knepley     *csize = size;
3861d0f6b257SMatthew G. Knepley   }
3862552f7358SJed Brown   PetscFunctionReturn(0);
3863552f7358SJed Brown }
3864552f7358SJed Brown 
3865552f7358SJed Brown /*@C
3866552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
3867552f7358SJed Brown 
3868552f7358SJed Brown   Not collective
3869552f7358SJed Brown 
3870552f7358SJed Brown   Input Parameters:
3871552f7358SJed Brown + dm - The DM
38720298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
3873552f7358SJed Brown . v - The local vector
3874552f7358SJed Brown . point - The sieve point in the DM
38750298fd71SBarry Smith . csize - The number of values in the closure, or NULL
3876552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
3877552f7358SJed Brown 
38783813dfbdSMatthew G Knepley   Fortran Notes:
38793813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
38803813dfbdSMatthew G Knepley   include petsc.h90 in your code.
38813813dfbdSMatthew G Knepley 
38823813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
38833813dfbdSMatthew G Knepley 
3884552f7358SJed Brown   Level: intermediate
3885552f7358SJed Brown 
3886552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
3887552f7358SJed Brown @*/
38887c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3889a6dfd86eSKarl Rupp {
3890552f7358SJed Brown   PetscInt       size = 0;
3891552f7358SJed Brown   PetscErrorCode ierr;
3892552f7358SJed Brown 
3893552f7358SJed Brown   PetscFunctionBegin;
3894552f7358SJed Brown   /* Should work without recalculating size */
3895552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, size, PETSC_SCALAR, (void*) values);CHKERRQ(ierr);
3896552f7358SJed Brown   PetscFunctionReturn(0);
3897552f7358SJed Brown }
3898552f7358SJed Brown 
3899552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
3900552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
3901552f7358SJed Brown 
390297e99dd9SToby 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[])
3903552f7358SJed Brown {
3904552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
3905552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
3906552f7358SJed Brown   PetscScalar    *a;
3907552f7358SJed Brown   PetscInt        off, cind = 0, k;
3908552f7358SJed Brown   PetscErrorCode  ierr;
3909552f7358SJed Brown 
3910552f7358SJed Brown   PetscFunctionBegin;
3911552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
3912552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
3913552f7358SJed Brown   a    = &array[off];
3914552f7358SJed Brown   if (!cdof || setBC) {
391597e99dd9SToby Isaac     if (clperm) {
391697e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
391797e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
3918552f7358SJed Brown     } else {
391997e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
392097e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
3921552f7358SJed Brown     }
3922552f7358SJed Brown   } else {
3923552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
392497e99dd9SToby Isaac     if (clperm) {
392597e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
3926552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
392797e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
3928552f7358SJed Brown         }
3929552f7358SJed Brown       } else {
3930552f7358SJed Brown         for (k = 0; k < dof; ++k) {
3931552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
393297e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
393397e99dd9SToby Isaac         }
393497e99dd9SToby Isaac       }
393597e99dd9SToby Isaac     } else {
393697e99dd9SToby Isaac       if (perm) {
393797e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
393897e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
393997e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
394097e99dd9SToby Isaac         }
394197e99dd9SToby Isaac       } else {
394297e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
394397e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
394497e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
394597e99dd9SToby Isaac         }
3946552f7358SJed Brown       }
3947552f7358SJed Brown     }
3948552f7358SJed Brown   }
3949552f7358SJed Brown   PetscFunctionReturn(0);
3950552f7358SJed Brown }
3951552f7358SJed Brown 
395297e99dd9SToby 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[])
3953a5e93ea8SMatthew G. Knepley {
3954a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
3955a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
3956a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
3957a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
3958a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
3959a5e93ea8SMatthew G. Knepley 
3960a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
3961a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
3962a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
3963a5e93ea8SMatthew G. Knepley   a    = &array[off];
3964a5e93ea8SMatthew G. Knepley   if (cdof) {
3965a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
396697e99dd9SToby Isaac     if (clperm) {
396797e99dd9SToby Isaac       if (perm) {
3968a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
3969a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
397097e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
397197e99dd9SToby Isaac             cind++;
3972a5e93ea8SMatthew G. Knepley           }
3973a5e93ea8SMatthew G. Knepley         }
3974a5e93ea8SMatthew G. Knepley       } else {
3975a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
3976a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
397797e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
397897e99dd9SToby Isaac             cind++;
397997e99dd9SToby Isaac           }
398097e99dd9SToby Isaac         }
398197e99dd9SToby Isaac       }
398297e99dd9SToby Isaac     } else {
398397e99dd9SToby Isaac       if (perm) {
398497e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
398597e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
398697e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
398797e99dd9SToby Isaac             cind++;
398897e99dd9SToby Isaac           }
398997e99dd9SToby Isaac         }
399097e99dd9SToby Isaac       } else {
399197e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
399297e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
399397e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
399497e99dd9SToby Isaac             cind++;
399597e99dd9SToby Isaac           }
3996a5e93ea8SMatthew G. Knepley         }
3997a5e93ea8SMatthew G. Knepley       }
3998a5e93ea8SMatthew G. Knepley     }
3999a5e93ea8SMatthew G. Knepley   }
4000a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4001a5e93ea8SMatthew G. Knepley }
4002a5e93ea8SMatthew G. Knepley 
400397e99dd9SToby 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[])
4004a6dfd86eSKarl Rupp {
4005552f7358SJed Brown   PetscScalar    *a;
40061a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
40071a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
400897e99dd9SToby Isaac   PetscInt        cind = 0, b;
4009552f7358SJed Brown   PetscErrorCode  ierr;
4010552f7358SJed Brown 
4011552f7358SJed Brown   PetscFunctionBegin;
4012552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4013552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
40141a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
40151a271a75SMatthew G. Knepley   a    = &array[foff];
4016552f7358SJed Brown   if (!fcdof || setBC) {
401797e99dd9SToby Isaac     if (clperm) {
401897e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
401997e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4020552f7358SJed Brown     } else {
402197e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
402297e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4023552f7358SJed Brown     }
4024552f7358SJed Brown   } else {
4025552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
402697e99dd9SToby Isaac     if (clperm) {
402797e99dd9SToby Isaac       if (perm) {
402897e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
402997e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
403097e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4031552f7358SJed Brown         }
4032552f7358SJed Brown       } else {
403397e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
403497e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
403597e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
403697e99dd9SToby Isaac         }
403797e99dd9SToby Isaac       }
403897e99dd9SToby Isaac     } else {
403997e99dd9SToby Isaac       if (perm) {
404097e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
404197e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
404297e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
404397e99dd9SToby Isaac         }
404497e99dd9SToby Isaac       } else {
404597e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
404697e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
404797e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4048552f7358SJed Brown         }
4049552f7358SJed Brown       }
4050552f7358SJed Brown     }
4051552f7358SJed Brown   }
40521a271a75SMatthew G. Knepley   *offset += fdof;
4053552f7358SJed Brown   PetscFunctionReturn(0);
4054552f7358SJed Brown }
4055552f7358SJed Brown 
405697e99dd9SToby 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[])
4057a5e93ea8SMatthew G. Knepley {
4058a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
40591a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
40601a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
406197e99dd9SToby Isaac   PetscInt        cind = 0, b;
4062a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4063a5e93ea8SMatthew G. Knepley 
4064a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4065a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4066a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
40671a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
40681a271a75SMatthew G. Knepley   a    = &array[foff];
4069a5e93ea8SMatthew G. Knepley   if (fcdof) {
4070a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
407197e99dd9SToby Isaac     if (clperm) {
407297e99dd9SToby Isaac       if (perm) {
407397e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
407497e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
407597e99dd9SToby Isaac             fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4076a5e93ea8SMatthew G. Knepley             ++cind;
4077a5e93ea8SMatthew G. Knepley           }
4078a5e93ea8SMatthew G. Knepley         }
4079a5e93ea8SMatthew G. Knepley       } else {
408097e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
408197e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
408297e99dd9SToby Isaac             fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
408397e99dd9SToby Isaac             ++cind;
408497e99dd9SToby Isaac           }
408597e99dd9SToby Isaac         }
408697e99dd9SToby Isaac       }
408797e99dd9SToby Isaac     } else {
408897e99dd9SToby Isaac       if (perm) {
408997e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
409097e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
409197e99dd9SToby Isaac             fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
409297e99dd9SToby Isaac             ++cind;
409397e99dd9SToby Isaac           }
409497e99dd9SToby Isaac         }
409597e99dd9SToby Isaac       } else {
409697e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
409797e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
409897e99dd9SToby Isaac             fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4099a5e93ea8SMatthew G. Knepley             ++cind;
4100a5e93ea8SMatthew G. Knepley           }
4101a5e93ea8SMatthew G. Knepley         }
4102a5e93ea8SMatthew G. Knepley       }
4103a5e93ea8SMatthew G. Knepley     }
4104a5e93ea8SMatthew G. Knepley   }
41051a271a75SMatthew G. Knepley   *offset += fdof;
4106a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4107a5e93ea8SMatthew G. Knepley }
4108a5e93ea8SMatthew G. Knepley 
41098f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4110a6dfd86eSKarl Rupp {
4111552f7358SJed Brown   PetscScalar    *array;
41121b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
41131b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4114552f7358SJed Brown   PetscErrorCode  ierr;
4115552f7358SJed Brown 
41161b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
4117b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4118b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
4119b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4120b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
4121b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4122b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4123b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
4124b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
4125b6ebb6e6SMatthew G. Knepley 
4126b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4127b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
4128b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
4129b6ebb6e6SMatthew G. Knepley     {
4130b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4131b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
4132b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
4133b6ebb6e6SMatthew G. Knepley 
4134b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
4135b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
4136b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
4137b6ebb6e6SMatthew G. Knepley       if (!cdof) {
4138b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4139b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4140b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4141b6ebb6e6SMatthew G. Knepley           }
4142b6ebb6e6SMatthew G. Knepley         } else {
4143b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4144b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4145b6ebb6e6SMatthew G. Knepley           }
4146b6ebb6e6SMatthew G. Knepley         }
4147b6ebb6e6SMatthew G. Knepley       } else {
4148b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
4149b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4150b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4151b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4152b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4153b6ebb6e6SMatthew G. Knepley           }
4154b6ebb6e6SMatthew G. Knepley         } else {
4155b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4156b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4157b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4158b6ebb6e6SMatthew G. Knepley           }
4159b6ebb6e6SMatthew G. Knepley         }
4160b6ebb6e6SMatthew G. Knepley       }
4161b6ebb6e6SMatthew G. Knepley     }
4162b6ebb6e6SMatthew G. Knepley   }
4163b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4164b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4165b6ebb6e6SMatthew G. Knepley }
41661b406b76SMatthew G. Knepley 
41671b406b76SMatthew G. Knepley /*@C
41681b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
41691b406b76SMatthew G. Knepley 
41701b406b76SMatthew G. Knepley   Not collective
41711b406b76SMatthew G. Knepley 
41721b406b76SMatthew G. Knepley   Input Parameters:
41731b406b76SMatthew G. Knepley + dm - The DM
41741b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
41751b406b76SMatthew G. Knepley . v - The local vector
41761b406b76SMatthew G. Knepley . point - The sieve point in the DM
41771b406b76SMatthew G. Knepley . values - The array of values
41781b406b76SMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
41791b406b76SMatthew G. Knepley 
41801b406b76SMatthew G. Knepley   Fortran Notes:
41811b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
41821b406b76SMatthew G. Knepley 
41831b406b76SMatthew G. Knepley   Level: intermediate
41841b406b76SMatthew G. Knepley 
41851b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
41861b406b76SMatthew G. Knepley @*/
41871b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
41881b406b76SMatthew G. Knepley {
41891b406b76SMatthew G. Knepley   PetscSection    clSection;
41901b406b76SMatthew G. Knepley   IS              clPoints;
41911b406b76SMatthew G. Knepley   PetscScalar    *array;
41921b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
419397e99dd9SToby Isaac   const PetscInt *clp, *clperm;
41941a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
41951b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
41961b406b76SMatthew G. Knepley 
41971a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
41981b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41991b406b76SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
42001a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
42011a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
42021b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
42031b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
42041b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
42058f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
42061b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
42071b406b76SMatthew G. Knepley   }
42081a271a75SMatthew G. Knepley   /* Get points */
420997e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4210923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
42111a271a75SMatthew G. Knepley   /* Get array */
4212552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
42131a271a75SMatthew G. Knepley   /* Get values */
4214ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
421597e99dd9SToby Isaac     PetscInt offset = 0, f;
4216552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
421797e99dd9SToby Isaac       const PetscInt    **perms = NULL;
421897e99dd9SToby Isaac       const PetscScalar **flips = NULL;
421997e99dd9SToby Isaac 
422097e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4221552f7358SJed Brown       switch (mode) {
4222552f7358SJed Brown       case INSERT_VALUES:
422397e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
422497e99dd9SToby Isaac           const PetscInt    point = points[2*p];
422597e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
422697e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
422797e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4228552f7358SJed Brown         } break;
4229552f7358SJed Brown       case INSERT_ALL_VALUES:
423097e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
423197e99dd9SToby Isaac           const PetscInt    point = points[2*p];
423297e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
423397e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
423497e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4235552f7358SJed Brown         } break;
4236a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
423797e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
423897e99dd9SToby Isaac           const PetscInt    point = points[2*p];
423997e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
424097e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
424197e99dd9SToby Isaac           updatePointFieldsBC_private(section, point, perm, flip, f, insert, clperm, values, &offset, array);
4242a5e93ea8SMatthew G. Knepley         } break;
4243552f7358SJed Brown       case ADD_VALUES:
424497e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
424597e99dd9SToby Isaac           const PetscInt    point = points[2*p];
424697e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
424797e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
424897e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4249552f7358SJed Brown         } break;
4250552f7358SJed Brown       case ADD_ALL_VALUES:
425197e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
425297e99dd9SToby Isaac           const PetscInt    point = points[2*p];
425397e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
425497e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
425597e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4256552f7358SJed Brown         } break;
4257304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
425897e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
425997e99dd9SToby Isaac           const PetscInt    point = points[2*p];
426097e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
426197e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
426297e99dd9SToby Isaac           updatePointFieldsBC_private(section, point, perm, flip, f, add, clperm, values, &offset, array);
4263304ab55fSMatthew G. Knepley         } break;
4264552f7358SJed Brown       default:
4265f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4266552f7358SJed Brown       }
426797e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
42681a271a75SMatthew G. Knepley     }
4269552f7358SJed Brown   } else {
42701a271a75SMatthew G. Knepley     PetscInt dof, off;
427197e99dd9SToby Isaac     const PetscInt    **perms = NULL;
427297e99dd9SToby Isaac     const PetscScalar **flips = NULL;
42731a271a75SMatthew G. Knepley 
427497e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4275552f7358SJed Brown     switch (mode) {
4276552f7358SJed Brown     case INSERT_VALUES:
427797e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
427897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
427997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
428097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
428197e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
428297e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4283552f7358SJed Brown       } break;
4284552f7358SJed Brown     case INSERT_ALL_VALUES:
428597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
428697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
428797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
428897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
428997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
429097e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4291552f7358SJed Brown       } break;
4292a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
429397e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
429497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
429597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
429697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
429797e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
429897e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4299a5e93ea8SMatthew G. Knepley       } break;
4300552f7358SJed Brown     case ADD_VALUES:
430197e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
430297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
430397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
430497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
430597e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
430697e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4307552f7358SJed Brown       } break;
4308552f7358SJed Brown     case ADD_ALL_VALUES:
430997e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
431097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
431197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
431297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
431397e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
431497e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4315552f7358SJed Brown       } break;
4316304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
431797e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
431897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
431997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
432097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
432197e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
432297e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
4323304ab55fSMatthew G. Knepley       } break;
4324552f7358SJed Brown     default:
4325f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4326552f7358SJed Brown     }
432797e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4328552f7358SJed Brown   }
43291a271a75SMatthew G. Knepley   /* Cleanup points */
4330923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
43311a271a75SMatthew G. Knepley   /* Cleanup array */
4332552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4333552f7358SJed Brown   PetscFunctionReturn(0);
4334552f7358SJed Brown }
4335552f7358SJed Brown 
4336e07394fbSMatthew G. Knepley PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, const PetscScalar values[], InsertMode mode)
4337e07394fbSMatthew G. Knepley {
4338e07394fbSMatthew G. Knepley   PetscSection      clSection;
4339e07394fbSMatthew G. Knepley   IS                clPoints;
4340e07394fbSMatthew G. Knepley   PetscScalar       *array;
4341e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
4342b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
4343e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
434497e99dd9SToby Isaac   PetscInt          offset = 0, f;
4345e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
4346e07394fbSMatthew G. Knepley 
4347e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
4348e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4349e07394fbSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
4350e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4351e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4352e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4353e07394fbSMatthew G. Knepley   /* Get points */
4354b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4355923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4356e07394fbSMatthew G. Knepley   /* Get array */
4357e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4358e07394fbSMatthew G. Knepley   /* Get values */
4359e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
436097e99dd9SToby Isaac     const PetscInt    **perms = NULL;
436197e99dd9SToby Isaac     const PetscScalar **flips = NULL;
436297e99dd9SToby Isaac 
4363e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
4364e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4365e07394fbSMatthew G. Knepley         PetscInt fdof;
4366e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
4367e07394fbSMatthew G. Knepley         offset += fdof;
4368e07394fbSMatthew G. Knepley       }
4369e07394fbSMatthew G. Knepley       continue;
4370e07394fbSMatthew G. Knepley     }
437197e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4372e07394fbSMatthew G. Knepley     switch (mode) {
4373e07394fbSMatthew G. Knepley     case INSERT_VALUES:
437497e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
437597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
437697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
437797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4378b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4379e07394fbSMatthew G. Knepley       } break;
4380e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
438197e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
438297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
438397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
438497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4385b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4386e07394fbSMatthew G. Knepley         } break;
4387e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
438897e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
438997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
439097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
439197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4392b04c866fSMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, insert, clperm, values, &offset, array);
4393e07394fbSMatthew G. Knepley       } break;
4394e07394fbSMatthew G. Knepley     case ADD_VALUES:
439597e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
439697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
439797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
439897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4399b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4400e07394fbSMatthew G. Knepley       } break;
4401e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
440297e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
440397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
440497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
440597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4406b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4407e07394fbSMatthew G. Knepley       } break;
4408e07394fbSMatthew G. Knepley     default:
4409f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4410e07394fbSMatthew G. Knepley     }
441197e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4412e07394fbSMatthew G. Knepley   }
4413e07394fbSMatthew G. Knepley   /* Cleanup points */
4414923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4415e07394fbSMatthew G. Knepley   /* Cleanup array */
4416e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4417e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
4418e07394fbSMatthew G. Knepley }
4419e07394fbSMatthew G. Knepley 
4420b0ecff45SMatthew G. Knepley PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
4421552f7358SJed Brown {
4422552f7358SJed Brown   PetscMPIInt    rank;
4423552f7358SJed Brown   PetscInt       i, j;
4424552f7358SJed Brown   PetscErrorCode ierr;
4425552f7358SJed Brown 
4426552f7358SJed Brown   PetscFunctionBegin;
442782f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
4428f347f43bSBarry Smith   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for sieve point %D\n", rank, point);CHKERRQ(ierr);
4429e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
4430e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
4431b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
4432b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
4433e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
4434b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
4435519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
44367eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
4437552f7358SJed Brown #else
4438b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
4439552f7358SJed Brown #endif
4440552f7358SJed Brown     }
444177a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
4442552f7358SJed Brown   }
4443552f7358SJed Brown   PetscFunctionReturn(0);
4444552f7358SJed Brown }
4445552f7358SJed Brown 
4446552f7358SJed Brown /* . off - The global offset of this point */
44474acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], PetscInt indices[])
4448a6dfd86eSKarl Rupp {
4449e6ccafaeSMatthew G Knepley   PetscInt        dof;    /* The number of unknowns on this point */
4450552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4451552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4452552f7358SJed Brown   PetscInt        cind = 0, k;
4453552f7358SJed Brown   PetscErrorCode  ierr;
4454552f7358SJed Brown 
4455552f7358SJed Brown   PetscFunctionBegin;
4456552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
4457552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4458552f7358SJed Brown   if (!cdof || setBC) {
44594acb8e1eSToby Isaac     if (perm) {
44604acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+perm[k]] = off + k;
4461552f7358SJed Brown     } else {
44624acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+k] = off + k;
4463552f7358SJed Brown     }
4464552f7358SJed Brown   } else {
4465552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
44664acb8e1eSToby Isaac     if (perm) {
44674acb8e1eSToby Isaac       for (k = 0; k < dof; ++k) {
44684acb8e1eSToby Isaac         if ((cind < cdof) && (k == cdofs[cind])) {
44694acb8e1eSToby Isaac           /* Insert check for returning constrained indices */
44704acb8e1eSToby Isaac           indices[*loff+perm[k]] = -(off+k+1);
44714acb8e1eSToby Isaac           ++cind;
44724acb8e1eSToby Isaac         } else {
44734acb8e1eSToby Isaac           indices[*loff+perm[k]] = off+k-cind;
44744acb8e1eSToby Isaac         }
44754acb8e1eSToby Isaac       }
44764acb8e1eSToby Isaac     } else {
4477552f7358SJed Brown       for (k = 0; k < dof; ++k) {
4478552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
4479552f7358SJed Brown           /* Insert check for returning constrained indices */
4480e6ccafaeSMatthew G Knepley           indices[*loff+k] = -(off+k+1);
4481552f7358SJed Brown           ++cind;
4482552f7358SJed Brown         } else {
4483e6ccafaeSMatthew G Knepley           indices[*loff+k] = off+k-cind;
4484552f7358SJed Brown         }
4485552f7358SJed Brown       }
4486552f7358SJed Brown     }
4487552f7358SJed Brown   }
4488e6ccafaeSMatthew G Knepley   *loff += dof;
4489552f7358SJed Brown   PetscFunctionReturn(0);
4490552f7358SJed Brown }
4491552f7358SJed Brown 
4492552f7358SJed Brown /* . off - The global offset of this point */
44934acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, PetscInt indices[])
4494a6dfd86eSKarl Rupp {
4495552f7358SJed Brown   PetscInt       numFields, foff, f;
4496552f7358SJed Brown   PetscErrorCode ierr;
4497552f7358SJed Brown 
4498552f7358SJed Brown   PetscFunctionBegin;
4499552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4500552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
45014acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
4502552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
45034acb8e1eSToby Isaac     PetscInt        cind = 0, b;
45044acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
4505552f7358SJed Brown 
4506552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4507552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
4508552f7358SJed Brown     if (!cfdof || setBC) {
45094acb8e1eSToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {indices[foffs[f]+perm[b]] = off+foff+b;}}
45104acb8e1eSToby Isaac       else      {for (b = 0; b < fdof; b++) {indices[foffs[f]+     b ] = off+foff+b;}}
4511552f7358SJed Brown     } else {
4512552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
45134acb8e1eSToby Isaac       if (perm) {
45144acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
45154acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
45164acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = -(off+foff+b+1);
4517552f7358SJed Brown             ++cind;
4518552f7358SJed Brown           } else {
45194acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = off+foff+b-cind;
4520552f7358SJed Brown           }
4521552f7358SJed Brown         }
4522552f7358SJed Brown       } else {
45234acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
45244acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
45254acb8e1eSToby Isaac             indices[foffs[f]+b] = -(off+foff+b+1);
4526552f7358SJed Brown             ++cind;
4527552f7358SJed Brown           } else {
45284acb8e1eSToby Isaac             indices[foffs[f]+b] = off+foff+b-cind;
4529552f7358SJed Brown           }
4530552f7358SJed Brown         }
4531552f7358SJed Brown       }
4532552f7358SJed Brown     }
4533a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
4534552f7358SJed Brown     foffs[f] += fdof;
4535552f7358SJed Brown   }
4536552f7358SJed Brown   PetscFunctionReturn(0);
4537552f7358SJed Brown }
4538552f7358SJed Brown 
45394acb8e1eSToby 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)
4540d3d1a6afSToby Isaac {
4541d3d1a6afSToby Isaac   Mat             cMat;
4542d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
4543d3d1a6afSToby Isaac   IS              aIS;
4544d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
4545d3d1a6afSToby Isaac   const PetscInt  *anchors;
4546e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
4547d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
4548d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
4549d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
4550d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
4551d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
4552d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
4553d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
45546ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
4555d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
4556d3d1a6afSToby Isaac   PetscErrorCode  ierr;
4557d3d1a6afSToby Isaac 
4558d3d1a6afSToby Isaac   PetscFunctionBegin;
4559d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4560d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4561d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4562d3d1a6afSToby Isaac 
4563a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
4564d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
4565d3d1a6afSToby Isaac   if (aSec) {
4566d3d1a6afSToby Isaac     ierr = PetscMemzero(newOffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
4567d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
4568d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
4569d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
4570d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
4571d3d1a6afSToby Isaac      * into the global matrix anyway) */
4572d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
4573d3d1a6afSToby Isaac       PetscInt b    = points[p];
45744b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4575d3d1a6afSToby Isaac 
45764b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
45774b2f2278SToby Isaac       if (!bSecDof) {
45784b2f2278SToby Isaac         continue;
45794b2f2278SToby Isaac       }
4580d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4581d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
4582d3d1a6afSToby Isaac       }
4583d3d1a6afSToby Isaac       if (bDof) {
4584d3d1a6afSToby Isaac         /* this point is constrained */
4585d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
4586d3d1a6afSToby Isaac         PetscInt bOff, q;
4587d3d1a6afSToby Isaac 
4588d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
4589d3d1a6afSToby Isaac         newNumPoints  += bDof;
4590d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
4591d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4592d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
4593d3d1a6afSToby Isaac           PetscInt aDof;
4594d3d1a6afSToby Isaac 
4595d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
4596d3d1a6afSToby Isaac           newNumIndices += aDof;
4597d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
4598d3d1a6afSToby Isaac             PetscInt fDof;
4599d3d1a6afSToby Isaac 
4600d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
4601d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
4602d3d1a6afSToby Isaac           }
4603d3d1a6afSToby Isaac         }
4604d3d1a6afSToby Isaac       }
4605d3d1a6afSToby Isaac       else {
4606d3d1a6afSToby Isaac         /* this point is not constrained */
4607d3d1a6afSToby Isaac         newNumPoints++;
46084b2f2278SToby Isaac         newNumIndices += bSecDof;
4609d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
4610d3d1a6afSToby Isaac           PetscInt fDof;
4611d3d1a6afSToby Isaac 
4612d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4613d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
4614d3d1a6afSToby Isaac         }
4615d3d1a6afSToby Isaac       }
4616d3d1a6afSToby Isaac     }
4617d3d1a6afSToby Isaac   }
4618d3d1a6afSToby Isaac   if (!anyConstrained) {
461972b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
462072b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
462172b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
462272b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
462372b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
4624d3d1a6afSToby Isaac     PetscFunctionReturn(0);
4625d3d1a6afSToby Isaac   }
4626d3d1a6afSToby Isaac 
46276ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
46286ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
46296ecaa68aSToby Isaac 
4630f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
4631d3d1a6afSToby Isaac 
46326ecaa68aSToby Isaac   if (!outPoints && !outValues) {
46336ecaa68aSToby Isaac     if (offsets) {
46346ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
46356ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
46366ecaa68aSToby Isaac       }
46376ecaa68aSToby Isaac     }
46386ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
46396ecaa68aSToby Isaac     PetscFunctionReturn(0);
46406ecaa68aSToby Isaac   }
46416ecaa68aSToby Isaac 
4642f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
4643d3d1a6afSToby Isaac 
4644f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
4645d3d1a6afSToby Isaac 
4646d3d1a6afSToby Isaac   /* workspaces */
4647d3d1a6afSToby Isaac   if (numFields) {
4648d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
4649d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
4650d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
4651d3d1a6afSToby Isaac     }
4652d3d1a6afSToby Isaac   }
4653d3d1a6afSToby Isaac   else {
4654d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
4655d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
4656d3d1a6afSToby Isaac   }
4657d3d1a6afSToby Isaac 
4658d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
4659d3d1a6afSToby Isaac   if (numFields) {
46604b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
46614b2f2278SToby Isaac 
4662d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4663d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
46644b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4665d3d1a6afSToby Isaac 
46664b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
46674b2f2278SToby Isaac       if (!bSecDof) {
46684b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
46694b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
46704b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
46714b2f2278SToby Isaac         }
46724b2f2278SToby Isaac         continue;
46734b2f2278SToby Isaac       }
4674d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4675d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4676d3d1a6afSToby Isaac       }
4677d3d1a6afSToby Isaac       if (bDof) {
4678d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4679d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
4680d3d1a6afSToby Isaac 
4681d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4682d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4683d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
4684d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
4685d3d1a6afSToby Isaac             PetscInt aFDof;
4686d3d1a6afSToby Isaac 
4687d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
4688d3d1a6afSToby Isaac             allFDof += aFDof;
4689d3d1a6afSToby Isaac           }
4690d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
4691d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
4692d3d1a6afSToby Isaac         }
4693d3d1a6afSToby Isaac       }
4694d3d1a6afSToby Isaac       else {
4695d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4696d3d1a6afSToby Isaac           PetscInt fDof;
4697d3d1a6afSToby Isaac 
4698d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4699d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
4700d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
4701d3d1a6afSToby Isaac         }
4702d3d1a6afSToby Isaac       }
4703d3d1a6afSToby Isaac     }
47044b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
47054b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
47064b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
4707d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
4708d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
4709d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
4710d3d1a6afSToby Isaac       }
471119f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
471219f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
4713d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
4714d3d1a6afSToby Isaac     }
4715d3d1a6afSToby Isaac   }
4716d3d1a6afSToby Isaac   else {
4717d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4718d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
47194b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4720d3d1a6afSToby Isaac 
47214b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
47224b2f2278SToby Isaac       if (!bSecDof) {
47234b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
47244b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
47254b2f2278SToby Isaac         continue;
47264b2f2278SToby Isaac       }
4727d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4728d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4729d3d1a6afSToby Isaac       }
4730d3d1a6afSToby Isaac       if (bDof) {
47314b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
4732d3d1a6afSToby Isaac 
4733d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4734d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4735d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
4736d3d1a6afSToby Isaac 
4737d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
4738d3d1a6afSToby Isaac           allDof += aDof;
4739d3d1a6afSToby Isaac         }
4740d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
47414b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
4742d3d1a6afSToby Isaac       }
4743d3d1a6afSToby Isaac       else {
47444b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
4745d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
4746d3d1a6afSToby Isaac       }
4747d3d1a6afSToby Isaac     }
4748d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
4749d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
4750d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4751d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
4752d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
4753d3d1a6afSToby Isaac     }
4754d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
4755d3d1a6afSToby Isaac   }
4756d3d1a6afSToby Isaac 
47576ecaa68aSToby Isaac   /* output arrays */
47586ecaa68aSToby Isaac   ierr = DMGetWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
47596ecaa68aSToby Isaac 
4760d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
4761d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
4762d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
4763d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
4764d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
4765d3d1a6afSToby Isaac   if (numFields) {
4766d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
4767d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4768d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
47694b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4770d3d1a6afSToby Isaac 
47714b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
47724b2f2278SToby Isaac       if (!bSecDof) {
47734b2f2278SToby Isaac         continue;
47744b2f2278SToby Isaac       }
4775d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4776d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4777d3d1a6afSToby Isaac       }
4778d3d1a6afSToby Isaac       if (bDof) {
4779d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
4780d3d1a6afSToby Isaac 
4781d3d1a6afSToby Isaac         fStart[0] = 0;
4782d3d1a6afSToby Isaac         fEnd[0]   = 0;
4783d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4784d3d1a6afSToby Isaac           PetscInt fDof;
4785d3d1a6afSToby Isaac 
4786d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
4787d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
4788d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
4789d3d1a6afSToby Isaac         }
4790d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
47914acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, indices);CHKERRQ(ierr);
4792d3d1a6afSToby Isaac 
4793d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
4794d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
4795d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4796d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
4797d3d1a6afSToby Isaac 
4798d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
4799d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
4800d3d1a6afSToby Isaac         }
4801d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4802d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4803d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
4804d3d1a6afSToby Isaac 
4805d3d1a6afSToby 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 */
4806d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
4807d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
4808302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
48094acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, newIndices);CHKERRQ(ierr);
4810d3d1a6afSToby Isaac         }
4811d3d1a6afSToby Isaac         newP += bDof;
4812d3d1a6afSToby Isaac 
48136ecaa68aSToby Isaac         if (outValues) {
4814d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
4815d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
4816d3d1a6afSToby 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);
4817d3d1a6afSToby Isaac           }
4818d3d1a6afSToby Isaac         }
48196ecaa68aSToby Isaac       }
4820d3d1a6afSToby Isaac       else {
4821d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
4822d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
4823d3d1a6afSToby Isaac         newP++;
4824d3d1a6afSToby Isaac       }
4825d3d1a6afSToby Isaac     }
4826d3d1a6afSToby Isaac   } else {
4827d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4828d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4829d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
48304b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4831d3d1a6afSToby Isaac 
48324b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
48334b2f2278SToby Isaac       if (!bSecDof) {
48344b2f2278SToby Isaac         continue;
48354b2f2278SToby Isaac       }
4836d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4837d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4838d3d1a6afSToby Isaac       }
4839d3d1a6afSToby Isaac       if (bDof) {
4840d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
4841d3d1a6afSToby Isaac 
4842d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
48434acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, indices);CHKERRQ(ierr);
4844d3d1a6afSToby Isaac 
4845d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
4846d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4847d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
4848d3d1a6afSToby Isaac 
4849d3d1a6afSToby 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 */
4850d3d1a6afSToby Isaac 
4851d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
4852d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
4853302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
48544acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, newIndices);CHKERRQ(ierr);
4855d3d1a6afSToby Isaac         }
4856d3d1a6afSToby Isaac         newP += bDof;
4857d3d1a6afSToby Isaac 
4858d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
48596ecaa68aSToby Isaac         if (outValues) {
4860d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
4861d3d1a6afSToby Isaac         }
48626ecaa68aSToby Isaac       }
4863d3d1a6afSToby Isaac       else {
4864d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
4865d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
4866d3d1a6afSToby Isaac         newP++;
4867d3d1a6afSToby Isaac       }
4868d3d1a6afSToby Isaac     }
4869d3d1a6afSToby Isaac   }
4870d3d1a6afSToby Isaac 
48716ecaa68aSToby Isaac   if (outValues) {
48726ecaa68aSToby Isaac     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
4873d3d1a6afSToby Isaac     ierr = PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));CHKERRQ(ierr);
4874d3d1a6afSToby Isaac     /* multiply constraints on the right */
4875d3d1a6afSToby Isaac     if (numFields) {
4876d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
4877d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
4878d3d1a6afSToby Isaac 
4879d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
4880d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
4881d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
4882d3d1a6afSToby Isaac           PetscInt c, r, k;
4883d3d1a6afSToby Isaac           PetscInt dof;
4884d3d1a6afSToby Isaac 
4885d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
48864b2f2278SToby Isaac           if (!dof) {
48874b2f2278SToby Isaac             continue;
48884b2f2278SToby Isaac           }
4889d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
4890d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
4891d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
4892d3d1a6afSToby Isaac 
4893d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
4894d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
4895d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
48964acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
4897d3d1a6afSToby Isaac                 }
4898d3d1a6afSToby Isaac               }
4899d3d1a6afSToby Isaac             }
4900d3d1a6afSToby Isaac           }
4901d3d1a6afSToby Isaac           else {
4902d3d1a6afSToby Isaac             /* copy this column as is */
4903d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
4904d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
4905d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
4906d3d1a6afSToby Isaac               }
4907d3d1a6afSToby Isaac             }
4908d3d1a6afSToby Isaac           }
4909d3d1a6afSToby Isaac           oldOff += dof;
4910d3d1a6afSToby Isaac         }
4911d3d1a6afSToby Isaac       }
4912d3d1a6afSToby Isaac     }
4913d3d1a6afSToby Isaac     else {
4914d3d1a6afSToby Isaac       PetscInt oldOff = 0;
4915d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
4916d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
4917d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
4918d3d1a6afSToby Isaac         PetscInt c, r, k;
4919d3d1a6afSToby Isaac         PetscInt dof;
4920d3d1a6afSToby Isaac 
4921d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
49224b2f2278SToby Isaac         if (!dof) {
49234b2f2278SToby Isaac           continue;
49244b2f2278SToby Isaac         }
4925d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
4926d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
4927d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
4928d3d1a6afSToby Isaac 
4929d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
4930d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
4931d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
4932d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
4933d3d1a6afSToby Isaac               }
4934d3d1a6afSToby Isaac             }
4935d3d1a6afSToby Isaac           }
4936d3d1a6afSToby Isaac         }
4937d3d1a6afSToby Isaac         else {
4938d3d1a6afSToby Isaac           /* copy this column as is */
4939d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
4940d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
4941d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
4942d3d1a6afSToby Isaac             }
4943d3d1a6afSToby Isaac           }
4944d3d1a6afSToby Isaac         }
4945d3d1a6afSToby Isaac         oldOff += dof;
4946d3d1a6afSToby Isaac       }
4947d3d1a6afSToby Isaac     }
4948d3d1a6afSToby Isaac 
49496ecaa68aSToby Isaac     if (multiplyLeft) {
49506ecaa68aSToby Isaac       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
4951d3d1a6afSToby Isaac       ierr = PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));CHKERRQ(ierr);
4952d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
4953d3d1a6afSToby Isaac       if (numFields) {
4954d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4955d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
4956d3d1a6afSToby Isaac 
4957d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
4958d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
4959d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
4960d3d1a6afSToby Isaac             PetscInt c, r, k;
4961d3d1a6afSToby Isaac             PetscInt dof;
4962d3d1a6afSToby Isaac 
4963d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
4964d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
4965d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
4966d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
4967d3d1a6afSToby Isaac 
4968d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
4969d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
4970d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
4971d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
4972d3d1a6afSToby Isaac                   }
4973d3d1a6afSToby Isaac                 }
4974d3d1a6afSToby Isaac               }
4975d3d1a6afSToby Isaac             }
4976d3d1a6afSToby Isaac             else {
4977d3d1a6afSToby Isaac               /* copy this row as is */
4978d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
4979d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
4980d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
4981d3d1a6afSToby Isaac                 }
4982d3d1a6afSToby Isaac               }
4983d3d1a6afSToby Isaac             }
4984d3d1a6afSToby Isaac             oldOff += dof;
4985d3d1a6afSToby Isaac           }
4986d3d1a6afSToby Isaac         }
4987d3d1a6afSToby Isaac       }
4988d3d1a6afSToby Isaac       else {
4989d3d1a6afSToby Isaac         PetscInt oldOff = 0;
4990d3d1a6afSToby Isaac 
4991d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
4992d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
4993d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
4994d3d1a6afSToby Isaac           PetscInt c, r, k;
4995d3d1a6afSToby Isaac           PetscInt dof;
4996d3d1a6afSToby Isaac 
4997d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
4998d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
4999d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5000d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5001d3d1a6afSToby Isaac 
5002d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5003d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5004d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5005d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5006d3d1a6afSToby Isaac                 }
5007d3d1a6afSToby Isaac               }
5008d3d1a6afSToby Isaac             }
5009d3d1a6afSToby Isaac           }
5010d3d1a6afSToby Isaac           else {
5011d3d1a6afSToby Isaac             /* copy this row as is */
50129fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5013d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5014d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5015d3d1a6afSToby Isaac               }
5016d3d1a6afSToby Isaac             }
5017d3d1a6afSToby Isaac           }
5018d3d1a6afSToby Isaac           oldOff += dof;
5019d3d1a6afSToby Isaac         }
5020d3d1a6afSToby Isaac       }
5021d3d1a6afSToby Isaac 
50226ecaa68aSToby Isaac       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
50236ecaa68aSToby Isaac     }
50246ecaa68aSToby Isaac     else {
50256ecaa68aSToby Isaac       newValues = tmpValues;
50266ecaa68aSToby Isaac     }
50276ecaa68aSToby Isaac   }
50286ecaa68aSToby Isaac 
5029d3d1a6afSToby Isaac   /* clean up */
5030d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
5031d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
50326ecaa68aSToby Isaac 
5033d3d1a6afSToby Isaac   if (numFields) {
5034d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
5035d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5036d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
5037d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5038d3d1a6afSToby Isaac     }
5039d3d1a6afSToby Isaac   }
5040d3d1a6afSToby Isaac   else {
5041d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5042d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
5043d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5044d3d1a6afSToby Isaac   }
5045d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5046d3d1a6afSToby Isaac 
5047d3d1a6afSToby Isaac   /* output */
50486ecaa68aSToby Isaac   if (outPoints) {
5049d3d1a6afSToby Isaac     *outPoints = newPoints;
50506ecaa68aSToby Isaac   }
50516ecaa68aSToby Isaac   else {
50526ecaa68aSToby Isaac     ierr = DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
50536ecaa68aSToby Isaac   }
505431620726SToby Isaac   if (outValues) {
5055d3d1a6afSToby Isaac     *outValues = newValues;
50566ecaa68aSToby Isaac   }
50576ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
5058d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
5059d3d1a6afSToby Isaac   }
5060d3d1a6afSToby Isaac   PetscFunctionReturn(0);
5061d3d1a6afSToby Isaac }
5062d3d1a6afSToby Isaac 
50636ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
50647773e69fSMatthew G. Knepley {
50657773e69fSMatthew G. Knepley   PetscSection    clSection;
50667773e69fSMatthew G. Knepley   IS              clPoints;
50677773e69fSMatthew G. Knepley   const PetscInt *clp;
50684acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
50697773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
50707773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
50717773e69fSMatthew G. Knepley   PetscInt        offsets[32];
50727773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
50737773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
50747773e69fSMatthew G. Knepley 
50757773e69fSMatthew G. Knepley   PetscFunctionBegin;
50767773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50777773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
50787773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
50797773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
50807773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
50817773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
50827773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
50837773e69fSMatthew G. Knepley   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
50847773e69fSMatthew G. Knepley   /* Get points in closure */
5085923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
50867773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
50877773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
50887773e69fSMatthew G. Knepley     PetscInt dof, fdof;
50897773e69fSMatthew G. Knepley 
50907773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
50917773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
50927773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
50937773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
50947773e69fSMatthew G. Knepley     }
50957773e69fSMatthew G. Knepley     Nind += dof;
50967773e69fSMatthew G. Knepley   }
50977773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
50988ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
50994acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
51004acb8e1eSToby Isaac   /* Get dual space symmetries */
51014acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
51024acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
51034acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
51044acb8e1eSToby Isaac   }
51057773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
51067773e69fSMatthew G. Knepley   {
51074acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
51087773e69fSMatthew G. Knepley     if (numPointsNew) {
51094acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
51104acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
51114acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
51124acb8e1eSToby Isaac       }
51134acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
51144acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
51154acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
51164acb8e1eSToby Isaac       }
5117923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
51187773e69fSMatthew G. Knepley       numPoints = numPointsNew;
51197773e69fSMatthew G. Knepley       Nind      = NindNew;
51207773e69fSMatthew G. Knepley       points    = pointsNew;
51217773e69fSMatthew G. Knepley     }
51227773e69fSMatthew G. Knepley   }
51237773e69fSMatthew G. Knepley   /* Calculate indices */
51247773e69fSMatthew G. Knepley   ierr = DMGetWorkArray(dm, Nind, PETSC_INT, indices);CHKERRQ(ierr);
51257773e69fSMatthew G. Knepley   if (Nf) {
51266ecaa68aSToby Isaac     if (outOffsets) {
51276ecaa68aSToby Isaac       PetscInt f;
51286ecaa68aSToby Isaac 
512946bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
51306ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
51316ecaa68aSToby Isaac       }
51326ecaa68aSToby Isaac     }
51334acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
51344acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
51354acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, *indices);
51367773e69fSMatthew G. Knepley     }
51377773e69fSMatthew G. Knepley   } else {
51384acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
51394acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
51404acb8e1eSToby Isaac 
51414acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
51424acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, *indices);
51437773e69fSMatthew G. Knepley     }
51447773e69fSMatthew G. Knepley   }
51457773e69fSMatthew G. Knepley   /* Cleanup points */
51464acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
51474acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
51484acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
51494acb8e1eSToby Isaac   }
51507773e69fSMatthew G. Knepley   if (numPointsNew) {
51517773e69fSMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, PETSC_INT, &pointsNew);CHKERRQ(ierr);
51527773e69fSMatthew G. Knepley   } else {
5153923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
51547773e69fSMatthew G. Knepley   }
51557773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
51567773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
51577773e69fSMatthew G. Knepley }
51587773e69fSMatthew G. Knepley 
515946bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
51607773e69fSMatthew G. Knepley {
51617773e69fSMatthew G. Knepley   PetscErrorCode ierr;
51627773e69fSMatthew G. Knepley 
51637773e69fSMatthew G. Knepley   PetscFunctionBegin;
51647773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51657773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
51667773e69fSMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, indices);CHKERRQ(ierr);
51677773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
51687773e69fSMatthew G. Knepley }
51697773e69fSMatthew G. Knepley 
51707f5d1fdeSMatthew G. Knepley /*@C
51717f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
51727f5d1fdeSMatthew G. Knepley 
51737f5d1fdeSMatthew G. Knepley   Not collective
51747f5d1fdeSMatthew G. Knepley 
51757f5d1fdeSMatthew G. Knepley   Input Parameters:
51767f5d1fdeSMatthew G. Knepley + dm - The DM
5177ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5178ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
51797f5d1fdeSMatthew G. Knepley . A - The matrix
51807f5d1fdeSMatthew G. Knepley . point - The sieve point in the DM
51817f5d1fdeSMatthew G. Knepley . values - The array of values
51827f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
51837f5d1fdeSMatthew G. Knepley 
51847f5d1fdeSMatthew G. Knepley   Fortran Notes:
51857f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
51867f5d1fdeSMatthew G. Knepley 
51877f5d1fdeSMatthew G. Knepley   Level: intermediate
51887f5d1fdeSMatthew G. Knepley 
51897f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
51907f5d1fdeSMatthew G. Knepley @*/
51917c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5192552f7358SJed Brown {
5193552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
51941b406b76SMatthew G. Knepley   PetscSection        clSection;
51951b406b76SMatthew G. Knepley   IS                  clPoints;
5196d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
51971b406b76SMatthew G. Knepley   const PetscInt     *clp;
5198552f7358SJed Brown   PetscInt           *indices;
5199552f7358SJed Brown   PetscInt            offsets[32];
52004acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
52014acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
5202923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
52034acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
5204d3d1a6afSToby Isaac   PetscScalar        *newValues;
5205552f7358SJed Brown   PetscErrorCode      ierr;
5206552f7358SJed Brown 
5207552f7358SJed Brown   PetscFunctionBegin;
5208552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5209ebd6d717SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
52103dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5211ebd6d717SJed Brown   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
52123dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
52133dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5214552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
521582f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5216552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5217923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5218552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5219552f7358SJed Brown     PetscInt fdof;
5220552f7358SJed Brown 
5221552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5222552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5223552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5224552f7358SJed Brown       offsets[f+1] += fdof;
5225552f7358SJed Brown     }
5226552f7358SJed Brown     numIndices += dof;
5227552f7358SJed Brown   }
52280d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
52290d644c17SKarl Rupp 
52308ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
52314acb8e1eSToby Isaac   /* Get symmetries */
52324acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
52334acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
52344acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
52354acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
52364acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
52374acb8e1eSToby Isaac 
52384acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
52394acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
52404acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
52414acb8e1eSToby Isaac 
52424acb8e1eSToby Isaac         if (!numFields) {
52434acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
52444acb8e1eSToby Isaac         } else {
52454acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
52464acb8e1eSToby Isaac         }
52474acb8e1eSToby Isaac         if (flip) {
52484acb8e1eSToby Isaac           PetscInt i, j, k;
52494acb8e1eSToby Isaac 
52504acb8e1eSToby Isaac           if (!valCopy) {
52514acb8e1eSToby Isaac             ierr = DMGetWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
52524acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
52534acb8e1eSToby Isaac             values = valCopy;
52544acb8e1eSToby Isaac           }
52554acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
5256775f7be2SToby Isaac             PetscScalar fval = flip[i];
52574acb8e1eSToby Isaac 
52584acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
52594acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
52604acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
52614acb8e1eSToby Isaac             }
52624acb8e1eSToby Isaac           }
52634acb8e1eSToby Isaac         }
52644acb8e1eSToby Isaac         foffset += fdof;
52654acb8e1eSToby Isaac       }
52664acb8e1eSToby Isaac     }
52674acb8e1eSToby Isaac   }
52684acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
5269d3d1a6afSToby Isaac   if (newNumPoints) {
52704acb8e1eSToby Isaac     if (valCopy) {
52714acb8e1eSToby Isaac       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
52724acb8e1eSToby Isaac     }
52734acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
52744acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
52754acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
52764acb8e1eSToby Isaac     }
52774acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
52784acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
52794acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
52804acb8e1eSToby Isaac     }
5281923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5282d3d1a6afSToby Isaac     numPoints  = newNumPoints;
5283d3d1a6afSToby Isaac     numIndices = newNumIndices;
5284d3d1a6afSToby Isaac     points     = newPoints;
5285d3d1a6afSToby Isaac     values     = newValues;
5286d3d1a6afSToby Isaac   }
5287552f7358SJed Brown   ierr = DMGetWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5288552f7358SJed Brown   if (numFields) {
52894acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
52904acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
52914acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, indices);
5292552f7358SJed Brown     }
5293552f7358SJed Brown   } else {
52944acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
52954acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
52964acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
52974acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, indices);
5298552f7358SJed Brown     }
5299552f7358SJed Brown   }
5300b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
5301552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
5302ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
5303ab1d0545SMatthew G. Knepley     PetscInt i;
5304ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
53058ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
5306ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5307ab1d0545SMatthew G. Knepley   }
5308552f7358SJed Brown   if (ierr) {
5309552f7358SJed Brown     PetscMPIInt    rank;
5310552f7358SJed Brown     PetscErrorCode ierr2;
5311552f7358SJed Brown 
531282f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5313e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5314b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
53156a415b8fSMatthew G Knepley     ierr2 = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr2);
5316552f7358SJed Brown     CHKERRQ(ierr);
5317552f7358SJed Brown   }
53184acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
53194acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
53204acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
53214acb8e1eSToby Isaac   }
5322d3d1a6afSToby Isaac   if (newNumPoints) {
5323d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
5324d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
5325d3d1a6afSToby Isaac   }
5326d3d1a6afSToby Isaac   else {
53274acb8e1eSToby Isaac     if (valCopy) {
53284acb8e1eSToby Isaac       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
53294acb8e1eSToby Isaac     }
5330923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5331d3d1a6afSToby Isaac   }
5332552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5333552f7358SJed Brown   PetscFunctionReturn(0);
5334552f7358SJed Brown }
5335552f7358SJed Brown 
5336de41b84cSMatthew 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)
5337de41b84cSMatthew G. Knepley {
5338de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
5339de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
5340de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
5341de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
5342de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
5343de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
53444ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
5345de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
5346de41b84cSMatthew G. Knepley 
5347de41b84cSMatthew G. Knepley   PetscFunctionBegin;
5348de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
5349de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
5350de41b84cSMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
5351de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
5352de41b84cSMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
5353de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
5354de41b84cSMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
5355de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
5356de41b84cSMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
5357de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
5358de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
5359de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
5360de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5361de41b84cSMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5362de41b84cSMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5363de41b84cSMatthew G. Knepley   /* Column indices */
5364de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
53654ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
5366de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
5367de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
5368de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
5369de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
5370de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
5371de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
5372de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
5373de41b84cSMatthew G. Knepley       ++q;
5374de41b84cSMatthew G. Knepley     }
5375de41b84cSMatthew G. Knepley   }
5376de41b84cSMatthew G. Knepley   numCPoints = q;
5377de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
5378de41b84cSMatthew G. Knepley     PetscInt fdof;
5379de41b84cSMatthew G. Knepley 
5380de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
53814ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5382de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5383de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
5384de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
5385de41b84cSMatthew G. Knepley     }
5386de41b84cSMatthew G. Knepley     numCIndices += dof;
5387de41b84cSMatthew G. Knepley   }
5388de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
5389de41b84cSMatthew G. Knepley   /* Row indices */
5390de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
5391de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
53921ba5f902SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
5393de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
5394de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
5395de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5396de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
5397de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
5398de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
5399de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
54004ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
54014ca5e9f5SMatthew G. Knepley         if (!dof) continue;
54024ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
54034ca5e9f5SMatthew G. Knepley         if (s < q) continue;
5404de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
5405de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
5406de41b84cSMatthew G. Knepley         ++q;
5407de41b84cSMatthew G. Knepley       }
5408de41b84cSMatthew G. Knepley     }
5409de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5410de41b84cSMatthew G. Knepley   }
5411de41b84cSMatthew G. Knepley   numFPoints = q;
5412de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
5413de41b84cSMatthew G. Knepley     PetscInt fdof;
5414de41b84cSMatthew G. Knepley 
5415de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
54164ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5417de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5418de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
5419de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
5420de41b84cSMatthew G. Knepley     }
5421de41b84cSMatthew G. Knepley     numFIndices += dof;
5422de41b84cSMatthew G. Knepley   }
5423de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
5424de41b84cSMatthew G. Knepley 
54258ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
54268ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
5427de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
5428de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
5429de41b84cSMatthew G. Knepley   if (numFields) {
54304acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
54314acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
54324acb8e1eSToby Isaac 
54334acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
54344acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
54354acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5436de41b84cSMatthew G. Knepley     }
54374acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
54384acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
54394acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
54404acb8e1eSToby Isaac     }
54414acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
54424acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
54434acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
54444acb8e1eSToby Isaac     }
54454acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
54464acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
54474acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5448de41b84cSMatthew G. Knepley     }
5449de41b84cSMatthew G. Knepley   } else {
54504acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
54514acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
54524acb8e1eSToby Isaac 
54534acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
54544acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
54554acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
54564acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
54574acb8e1eSToby Isaac 
54584acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
54594acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);CHKERRQ(ierr);
5460de41b84cSMatthew G. Knepley     }
54614acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
54624acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
54634acb8e1eSToby Isaac 
54644acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
54654acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);CHKERRQ(ierr);
5466de41b84cSMatthew G. Knepley     }
54674acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
54684acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
5469de41b84cSMatthew G. Knepley   }
5470de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
54714acb8e1eSToby Isaac   /* TODO: flips */
5472de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
5473de41b84cSMatthew G. Knepley   if (ierr) {
5474de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
5475de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
5476de41b84cSMatthew G. Knepley 
5477de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5478e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5479de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
5480de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr2);
5481de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr2);
5482de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
5483de41b84cSMatthew G. Knepley   }
5484549a8adaSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
5485de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
5486de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
5487de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
5488de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
5489de41b84cSMatthew G. Knepley }
5490de41b84cSMatthew G. Knepley 
54917c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
54927c927364SMatthew G. Knepley {
54937c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
54947c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
54957c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
54967c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
54977c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
54987c927364SMatthew G. Knepley   PetscErrorCode ierr;
54997c927364SMatthew G. Knepley 
55007c927364SMatthew G. Knepley   PetscFunctionBegin;
55017c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
55027c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
55037c927364SMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
55047c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
55057c927364SMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
55067c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
55077c927364SMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
55087c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
55097c927364SMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
55107c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
55117c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
55127c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
55137c927364SMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
55147c927364SMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
55157c927364SMatthew G. Knepley   /* Column indices */
55167c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
55177c927364SMatthew G. Knepley   maxFPoints = numCPoints;
55187c927364SMatthew G. Knepley   /* Compress out points not in the section */
55197c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
55207c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
55217c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
55227c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
55237c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
55247c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
55257c927364SMatthew G. Knepley       ++q;
55267c927364SMatthew G. Knepley     }
55277c927364SMatthew G. Knepley   }
55287c927364SMatthew G. Knepley   numCPoints = q;
55297c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
55307c927364SMatthew G. Knepley     PetscInt fdof;
55317c927364SMatthew G. Knepley 
55327c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
55337c927364SMatthew G. Knepley     if (!dof) continue;
55347c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
55357c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
55367c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
55377c927364SMatthew G. Knepley     }
55387c927364SMatthew G. Knepley     numCIndices += dof;
55397c927364SMatthew G. Knepley   }
55407c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
55417c927364SMatthew G. Knepley   /* Row indices */
55427c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
55437c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
55447c927364SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
55457c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
55467c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
55477c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
55487c927364SMatthew G. Knepley     /* Compress out points not in the section */
55497c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
55507c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
55517c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
55527c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
55537c927364SMatthew G. Knepley         if (!dof) continue;
55547c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
55557c927364SMatthew G. Knepley         if (s < q) continue;
55567c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
55577c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
55587c927364SMatthew G. Knepley         ++q;
55597c927364SMatthew G. Knepley       }
55607c927364SMatthew G. Knepley     }
55617c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
55627c927364SMatthew G. Knepley   }
55637c927364SMatthew G. Knepley   numFPoints = q;
55647c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
55657c927364SMatthew G. Knepley     PetscInt fdof;
55667c927364SMatthew G. Knepley 
55677c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
55687c927364SMatthew G. Knepley     if (!dof) continue;
55697c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
55707c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
55717c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
55727c927364SMatthew G. Knepley     }
55737c927364SMatthew G. Knepley     numFIndices += dof;
55747c927364SMatthew G. Knepley   }
55757c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
55767c927364SMatthew G. Knepley 
55778ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
55788ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
55797c927364SMatthew G. Knepley   if (numFields) {
55804acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
55814acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
55824acb8e1eSToby Isaac 
55834acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
55844acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
55854acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
55867c927364SMatthew G. Knepley     }
55874acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
55884acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
55894acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
55904acb8e1eSToby Isaac     }
55914acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
55924acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
55934acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
55944acb8e1eSToby Isaac     }
55954acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
55964acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
55974acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
55987c927364SMatthew G. Knepley     }
55997c927364SMatthew G. Knepley   } else {
56004acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
56014acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
56024acb8e1eSToby Isaac 
56034acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
56044acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
56054acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
56064acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
56074acb8e1eSToby Isaac 
56084acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
56094acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);
56107c927364SMatthew G. Knepley     }
56114acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
56124acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
56134acb8e1eSToby Isaac 
56144acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
56154acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);
56167c927364SMatthew G. Knepley     }
56174acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
56184acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
56197c927364SMatthew G. Knepley   }
56207c927364SMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
56217c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
56227c927364SMatthew G. Knepley   PetscFunctionReturn(0);
56237c927364SMatthew G. Knepley }
56247c927364SMatthew G. Knepley 
5625f96281f8SMatthew G. Knepley /*@
5626f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
5627f96281f8SMatthew G. Knepley 
5628f96281f8SMatthew G. Knepley   Input Parameter:
5629f96281f8SMatthew G. Knepley . dm - The DMPlex object
5630f96281f8SMatthew G. Knepley 
5631f96281f8SMatthew G. Knepley   Output Parameters:
5632f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
5633dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5634dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5635dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5636f96281f8SMatthew G. Knepley 
5637f96281f8SMatthew G. Knepley   Level: developer
5638f96281f8SMatthew G. Knepley 
5639dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
5640f96281f8SMatthew G. Knepley @*/
5641770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
5642552f7358SJed Brown {
5643552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5644770b213bSMatthew G Knepley   PetscInt       dim;
5645770b213bSMatthew G Knepley   PetscErrorCode ierr;
5646552f7358SJed Brown 
5647552f7358SJed Brown   PetscFunctionBegin;
5648552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5649c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5650770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
5651770b213bSMatthew G Knepley   if (fMax) *fMax = mesh->hybridPointMax[dim-1];
5652770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
5653770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
5654552f7358SJed Brown   PetscFunctionReturn(0);
5655552f7358SJed Brown }
5656552f7358SJed Brown 
5657dc5a3409SMatthew G. Knepley /*@
5658dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
5659dc5a3409SMatthew G. Knepley 
5660dc5a3409SMatthew G. Knepley   Input Parameters:
5661dc5a3409SMatthew G. Knepley . dm   - The DMPlex object
5662dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
5663dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5664dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5665dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5666dc5a3409SMatthew G. Knepley 
5667dc5a3409SMatthew G. Knepley   Level: developer
5668dc5a3409SMatthew G. Knepley 
5669dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
5670dc5a3409SMatthew G. Knepley @*/
5671770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
5672552f7358SJed Brown {
5673552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5674770b213bSMatthew G Knepley   PetscInt       dim;
5675770b213bSMatthew G Knepley   PetscErrorCode ierr;
5676552f7358SJed Brown 
5677552f7358SJed Brown   PetscFunctionBegin;
5678552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5679c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5680770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]   = cMax;
5681770b213bSMatthew G Knepley   if (fMax >= 0) mesh->hybridPointMax[dim-1] = fMax;
5682770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]     = eMax;
5683770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]     = vMax;
5684552f7358SJed Brown   PetscFunctionReturn(0);
5685552f7358SJed Brown }
5686552f7358SJed Brown 
5687552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
5688552f7358SJed Brown {
5689552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5690552f7358SJed Brown 
5691552f7358SJed Brown   PetscFunctionBegin;
5692552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5693552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
5694552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
5695552f7358SJed Brown   PetscFunctionReturn(0);
5696552f7358SJed Brown }
5697552f7358SJed Brown 
5698552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
5699552f7358SJed Brown {
5700552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5701552f7358SJed Brown 
5702552f7358SJed Brown   PetscFunctionBegin;
5703552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5704552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
5705552f7358SJed Brown   PetscFunctionReturn(0);
5706552f7358SJed Brown }
5707552f7358SJed Brown 
5708552f7358SJed Brown /* We can easily have a form that takes an IS instead */
5709ef48cebcSMatthew G. Knepley static PetscErrorCode DMPlexCreateNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
5710552f7358SJed Brown {
5711552f7358SJed Brown   PetscSection   section, globalSection;
5712552f7358SJed Brown   PetscInt      *numbers, p;
5713552f7358SJed Brown   PetscErrorCode ierr;
5714552f7358SJed Brown 
5715552f7358SJed Brown   PetscFunctionBegin;
571682f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
5717552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
5718552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5719552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
5720552f7358SJed Brown   }
5721552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
572215b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
5723854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
5724552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5725552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
5726ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
5727ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
5728552f7358SJed Brown   }
572982f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
5730ef48cebcSMatthew G. Knepley   if (globalSize) {
5731ef48cebcSMatthew G. Knepley     PetscLayout layout;
5732ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
5733ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
5734ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
5735ef48cebcSMatthew G. Knepley   }
5736552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
5737552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
5738552f7358SJed Brown   PetscFunctionReturn(0);
5739552f7358SJed Brown }
5740552f7358SJed Brown 
574181ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
5742552f7358SJed Brown {
5743552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
5744552f7358SJed Brown   PetscErrorCode ierr;
5745552f7358SJed Brown 
5746552f7358SJed Brown   PetscFunctionBegin;
5747552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
5748552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
57490298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
575081ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
575181ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
575281ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
5753552f7358SJed Brown }
575481ed3555SMatthew G. Knepley 
575581ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
575681ed3555SMatthew G. Knepley {
575781ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
575881ed3555SMatthew G. Knepley   PetscErrorCode ierr;
575981ed3555SMatthew G. Knepley 
576081ed3555SMatthew G. Knepley   PetscFunctionBegin;
576181ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
576281ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
5763552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
5764552f7358SJed Brown   PetscFunctionReturn(0);
5765552f7358SJed Brown }
5766552f7358SJed Brown 
576781ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
576881ed3555SMatthew G. Knepley {
576981ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
577081ed3555SMatthew G. Knepley   PetscErrorCode ierr;
577181ed3555SMatthew G. Knepley 
577281ed3555SMatthew G. Knepley   PetscFunctionBegin;
577381ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
577481ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
577581ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
577681ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
577781ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
577881ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
577981ed3555SMatthew G. Knepley }
578081ed3555SMatthew G. Knepley 
5781552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
5782552f7358SJed Brown {
5783552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5784552f7358SJed Brown   PetscErrorCode ierr;
5785552f7358SJed Brown 
5786552f7358SJed Brown   PetscFunctionBegin;
5787552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
578881ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
5789552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
5790552f7358SJed Brown   PetscFunctionReturn(0);
5791552f7358SJed Brown }
5792552f7358SJed Brown 
5793ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
5794ef48cebcSMatthew G. Knepley {
5795ef48cebcSMatthew G. Knepley   IS             nums[4];
5796ef48cebcSMatthew G. Knepley   PetscInt       depths[4];
5797ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
5798ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
5799ef48cebcSMatthew G. Knepley 
5800ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
5801ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5802ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
58038abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
58048abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
5805ef48cebcSMatthew G. Knepley   depths[0] = depth; depths[1] = 0;
5806ef48cebcSMatthew G. Knepley   for (d = 2; d <= depth; ++d) depths[d] = depth-d+1;
5807ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
5808ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
5809ef48cebcSMatthew G. Knepley 
5810ef48cebcSMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, depths[d], &pStart, &pEnd);CHKERRQ(ierr);
5811ef48cebcSMatthew G. Knepley     ierr = DMPlexCreateNumbering_Private(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
5812ef48cebcSMatthew G. Knepley     shift += gsize;
5813ef48cebcSMatthew G. Knepley   }
5814302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
5815ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
5816ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
5817ef48cebcSMatthew G. Knepley }
5818ef48cebcSMatthew G. Knepley 
5819ca8062c8SMatthew G. Knepley /*@
5820ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
5821ca8062c8SMatthew G. Knepley 
5822ca8062c8SMatthew G. Knepley   Input Parameters:
5823ca8062c8SMatthew G. Knepley   + dm - The DMPlex object
5824ca8062c8SMatthew G. Knepley 
5825ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
5826ca8062c8SMatthew G. Knepley 
5827ca8062c8SMatthew G. Knepley   Level: developer
5828ca8062c8SMatthew G. Knepley 
58299bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSkeleton(), DMCheckFaces()
5830ca8062c8SMatthew G. Knepley @*/
5831ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
5832ca8062c8SMatthew G. Knepley {
5833ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
5834ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
5835ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
5836ca8062c8SMatthew G. Knepley   PetscInt        pStart, pEnd, p, csize, ssize;
5837ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
5838ca8062c8SMatthew G. Knepley 
5839ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
5840ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5841ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
5842ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
5843ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
5844ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
5845ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
5846ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
5847ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
5848ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
584942e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
585042e66dfaSMatthew G. Knepley       PetscInt  d;
585142e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
585242e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
585342e66dfaSMatthew G. Knepley       }
5854ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
5855ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
5856ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
5857ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
5858ca8062c8SMatthew G. Knepley       }
585942e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
58608ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
5861ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
58628ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
5863ca8062c8SMatthew G. Knepley         }
5864302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
58658ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
5866ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
58678ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
5868ca8062c8SMatthew G. Knepley         }
5869302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
58708ccfff9cSToby 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]);
58718ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
5872ca8062c8SMatthew G. Knepley       }
587342e66dfaSMatthew G. Knepley     }
5874ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
5875ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
5876ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
5877ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
5878ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
5879ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
5880ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
5881ca8062c8SMatthew G. Knepley       }
5882ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
58838ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
5884ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
58858ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
5886ca8062c8SMatthew G. Knepley         }
5887302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
58888ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
5889ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
58908ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
5891ca8062c8SMatthew G. Knepley         }
5892302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
58938ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
5894ca8062c8SMatthew G. Knepley       }
5895ca8062c8SMatthew G. Knepley     }
5896ca8062c8SMatthew G. Knepley   }
5897ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
5898ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
58998ccfff9cSToby Isaac   if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
5900ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
5901ca8062c8SMatthew G. Knepley }
5902ca8062c8SMatthew G. Knepley 
5903ca8062c8SMatthew G. Knepley /*@
5904ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
5905ca8062c8SMatthew G. Knepley 
5906ca8062c8SMatthew G. Knepley   Input Parameters:
5907ca8062c8SMatthew G. Knepley + dm - The DMPlex object
590858723a97SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
590958723a97SMatthew G. Knepley - cellHeight - Normally 0
5910ca8062c8SMatthew G. Knepley 
5911ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
5912ca8062c8SMatthew G. Knepley 
5913ca8062c8SMatthew G. Knepley   Level: developer
5914ca8062c8SMatthew G. Knepley 
59159bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckFaces()
5916ca8062c8SMatthew G. Knepley @*/
591758723a97SMatthew G. Knepley PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscBool isSimplex, PetscInt cellHeight)
5918ca8062c8SMatthew G. Knepley {
591942363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
5920ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
5921ca8062c8SMatthew G. Knepley 
5922ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
5923ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5924c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5925ca8062c8SMatthew G. Knepley   switch (dim) {
592642363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
592742363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
592842363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
5929ca8062c8SMatthew G. Knepley   default:
59308ccfff9cSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
5931ca8062c8SMatthew G. Knepley   }
593258723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
593358723a97SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
5934ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
5935ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
5936ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
593758723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
593858723a97SMatthew G. Knepley 
593958723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
594058723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
594158723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
594258723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
594358723a97SMatthew G. Knepley     }
594458723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
59458ccfff9cSToby Isaac     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
5946ca8062c8SMatthew G. Knepley   }
594742363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
594842363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
594942363296SMatthew G. Knepley 
595042363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
595142363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
595242363296SMatthew G. Knepley       const PetscInt p = closure[cl];
595342363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
595442363296SMatthew G. Knepley     }
595542363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
59568ccfff9cSToby Isaac     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
595742363296SMatthew G. Knepley   }
5958ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
5959ca8062c8SMatthew G. Knepley }
59609bf0dad6SMatthew G. Knepley 
59619bf0dad6SMatthew G. Knepley /*@
59629bf0dad6SMatthew 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
59639bf0dad6SMatthew G. Knepley 
59649bf0dad6SMatthew G. Knepley   Input Parameters:
59659bf0dad6SMatthew G. Knepley + dm - The DMPlex object
59669bf0dad6SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
59679bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
59689bf0dad6SMatthew G. Knepley 
59699bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
59709bf0dad6SMatthew G. Knepley 
59719bf0dad6SMatthew G. Knepley   Level: developer
59729bf0dad6SMatthew G. Knepley 
59739bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckSkeleton()
59749bf0dad6SMatthew G. Knepley @*/
59759bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexCheckFaces(DM dm, PetscBool isSimplex, PetscInt cellHeight)
59769bf0dad6SMatthew G. Knepley {
59773554e41dSMatthew G. Knepley   PetscInt       pMax[4];
59783554e41dSMatthew G. Knepley   PetscInt       dim, vStart, vEnd, cStart, cEnd, c, h;
59799bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
59809bf0dad6SMatthew G. Knepley 
59819bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
59829bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5983c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
59849bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
598525abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
59863554e41dSMatthew G. Knepley   for (h = cellHeight; h < dim; ++h) {
59873554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
59883554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
59899bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
59909bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
59919bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
59929bf0dad6SMatthew G. Knepley 
599325abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
59949bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
59959bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
59969bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
59979bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
59989bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
59999bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
60009bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
60019bf0dad6SMatthew G. Knepley       }
60023554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
60038ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
60049bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
60059bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
60069bf0dad6SMatthew G. Knepley 
60079bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
60089bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
60099bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
60109bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
60119bf0dad6SMatthew G. Knepley         }
60128ccfff9cSToby 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);
60139bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
60148ccfff9cSToby 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]);
60159bf0dad6SMatthew G. Knepley         }
60169bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
60179bf0dad6SMatthew G. Knepley       }
60189bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
60199bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
60209bf0dad6SMatthew G. Knepley     }
60213554e41dSMatthew G. Knepley   }
6022552f7358SJed Brown   PetscFunctionReturn(0);
6023552f7358SJed Brown }
60243913d7c8SMatthew G. Knepley 
6025bceba477SMatthew G. Knepley /* Pointwise interpolation
6026bceba477SMatthew G. Knepley      Just code FEM for now
6027bceba477SMatthew G. Knepley      u^f = I u^c
60284ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
60294ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
60304ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
6031bceba477SMatthew G. Knepley */
6032bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
6033bceba477SMatthew G. Knepley {
6034bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
6035bceba477SMatthew G. Knepley   PetscInt       m, n;
6036a063dac3SMatthew G. Knepley   void          *ctx;
603768132eb9SMatthew G. Knepley   DM             cdm;
603868132eb9SMatthew G. Knepley   PetscBool      regular;
6039bceba477SMatthew G. Knepley   PetscErrorCode ierr;
6040bceba477SMatthew G. Knepley 
6041bceba477SMatthew G. Knepley   PetscFunctionBegin;
6042bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6043bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6044bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6045bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
604668132eb9SMatthew G. Knepley 
6047bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
6048bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6049bceba477SMatthew G. Knepley   ierr = MatSetType(*interpolation, dmCoarse->mattype);CHKERRQ(ierr);
6050a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
605168132eb9SMatthew G. Knepley 
6052a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
605368132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
605468132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
605568132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
605668132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
60575d1c2e58SMatthew G. Knepley   /* Use naive scaling */
60585d1c2e58SMatthew G. Knepley   ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
6059a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
6060a063dac3SMatthew G. Knepley }
6061bceba477SMatthew G. Knepley 
60626dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
6063a063dac3SMatthew G. Knepley {
606490748bafSMatthew G. Knepley   PetscErrorCode ierr;
60656dbf9973SLawrence Mitchell   VecScatter     ctx;
606690748bafSMatthew G. Knepley 
6067a063dac3SMatthew G. Knepley   PetscFunctionBegin;
60686dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
60696dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
60706dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
6071bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6072bceba477SMatthew G. Knepley }
6073bceba477SMatthew G. Knepley 
6074fd59a867SMatthew G. Knepley PetscErrorCode DMCreateDefaultSection_Plex(DM dm)
6075fd59a867SMatthew G. Knepley {
6076fd59a867SMatthew G. Knepley   PetscSection   section;
6077ab1d0545SMatthew G. Knepley   IS            *bcPoints, *bcComps;
6078ebb3236fSMatthew G. Knepley   PetscBool     *isFE;
6079286d8613SMatthew G. Knepley   PetscInt      *bcFields, *numComp, *numDof;
6080ebb3236fSMatthew G. Knepley   PetscInt       depth, dim, numBd, numBC = 0, numFields, bd, bc = 0, f;
6081ebb3236fSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
6082fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
6083fd59a867SMatthew G. Knepley 
6084fd59a867SMatthew G. Knepley   PetscFunctionBegin;
6085ebb3236fSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
6086ae71db08SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
6087ebb3236fSMatthew G. Knepley   /* FE and FV boundary conditions are handled slightly differently */
6088ebb3236fSMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
6089ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6090ebb3236fSMatthew G. Knepley     PetscObject  obj;
6091ebb3236fSMatthew G. Knepley     PetscClassId id;
6092ebb3236fSMatthew G. Knepley 
6093ebb3236fSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6094ebb3236fSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
6095ebb3236fSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
6096ebb3236fSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
60978ccfff9cSToby Isaac     else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
6098ebb3236fSMatthew G. Knepley   }
60992f900235SMatthew G. Knepley   /* Allocate boundary point storage for FEM boundaries */
6100286d8613SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
6101c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6102ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
6103ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
6104dff059c6SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob, &numBd);CHKERRQ(ierr);
6105fd59a867SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
61062f900235SMatthew G. Knepley     PetscInt                field;
6107f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6108385532a5SToby Isaac     const char             *labelName;
6109385532a5SToby Isaac     DMLabel                 label;
61102f900235SMatthew G. Knepley 
6111f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &labelName, &field, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
6112385532a5SToby Isaac     ierr = DMGetLabel(dm,labelName,&label);CHKERRQ(ierr);
6113f971fd6bSMatthew G. Knepley     if (label && isFE[field] && (type & DM_BC_ESSENTIAL)) ++numBC;
6114fd59a867SMatthew G. Knepley   }
61152f900235SMatthew G. Knepley   /* Add ghost cell boundaries for FVM */
6116ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) if (!isFE[f] && cEndInterior >= 0) ++numBC;
61176c8d74c4SMatthew G. Knepley   ierr = PetscCalloc3(numBC,&bcFields,numBC,&bcPoints,numBC,&bcComps);CHKERRQ(ierr);
6118ebb3236fSMatthew G. Knepley   /* Constrain ghost cells for FV */
6119ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6120ebb3236fSMatthew G. Knepley     PetscInt *newidx, c;
6121ebb3236fSMatthew G. Knepley 
6122ebb3236fSMatthew G. Knepley     if (isFE[f] || cEndInterior < 0) continue;
6123ebb3236fSMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cEndInterior,&newidx);CHKERRQ(ierr);
6124ebb3236fSMatthew G. Knepley     for (c = cEndInterior; c < cEnd; ++c) newidx[c-cEndInterior] = c;
6125ebb3236fSMatthew G. Knepley     bcFields[bc] = f;
6126ebb3236fSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), cEnd-cEndInterior, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6127ebb3236fSMatthew G. Knepley   }
6128ebb3236fSMatthew G. Knepley   /* Handle FEM Dirichlet boundaries */
6129ebb3236fSMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
6130fd59a867SMatthew G. Knepley     const char             *bdLabel;
6131fd59a867SMatthew G. Knepley     DMLabel                 label;
61320e0f25f2SMatthew G. Knepley     const PetscInt         *comps;
6133fd59a867SMatthew G. Knepley     const PetscInt         *values;
61340e0f25f2SMatthew G. Knepley     PetscInt                bd2, field, numComps, numValues;
6135f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6136f971fd6bSMatthew G. Knepley     PetscBool               duplicate = PETSC_FALSE;
6137fd59a867SMatthew G. Knepley 
6138f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &bdLabel, &field, &numComps, &comps, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
6139c58f1c22SToby Isaac     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
6140385532a5SToby Isaac     if (!isFE[field] || !label) continue;
6141ebb3236fSMatthew G. Knepley     /* Only want to modify label once */
61427391c1cbSMatthew G. Knepley     for (bd2 = 0; bd2 < bd; ++bd2) {
61437391c1cbSMatthew G. Knepley       const char *bdname;
6144dff059c6SToby Isaac       ierr = PetscDSGetBoundary(dm->prob, bd2, NULL, NULL, &bdname, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
61457391c1cbSMatthew G. Knepley       ierr = PetscStrcmp(bdname, bdLabel, &duplicate);CHKERRQ(ierr);
61467391c1cbSMatthew G. Knepley       if (duplicate) break;
61477391c1cbSMatthew G. Knepley     }
6148ebb3236fSMatthew G. Knepley     if (!duplicate && (isFE[field])) {
6149b0bf5782SToby Isaac       /* don't complete cells, which are just present to give orientation to the boundary */
6150092e5057SToby Isaac       ierr = DMPlexLabelComplete(dm, label);CHKERRQ(ierr);
61517391c1cbSMatthew G. Knepley     }
6152ebb3236fSMatthew G. Knepley     /* Filter out cells, if you actually want to constrain cells you need to do things by hand right now */
6153f971fd6bSMatthew G. Knepley     if (type & DM_BC_ESSENTIAL) {
615493a5981aSMatthew G. Knepley       PetscInt       *newidx;
6155ebb3236fSMatthew G. Knepley       PetscInt        n, newn = 0, p, v;
615693a5981aSMatthew G. Knepley 
6157fd59a867SMatthew G. Knepley       bcFields[bc] = field;
61580e0f25f2SMatthew G. Knepley       if (numComps) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), numComps, comps, PETSC_COPY_VALUES, &bcComps[bc]);CHKERRQ(ierr);}
6159ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6160ebb3236fSMatthew G. Knepley         IS              tmp;
6161ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6162ebb3236fSMatthew G. Knepley 
6163c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6164ebb3236fSMatthew G. Knepley         if (!tmp) continue;
616593a5981aSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
616693a5981aSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6167ebb3236fSMatthew G. Knepley         if (isFE[field]) {
616893a5981aSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) ++newn;
6169ebb3236fSMatthew G. Knepley         } else {
6170ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) ++newn;
6171ebb3236fSMatthew G. Knepley         }
617293a5981aSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
617393a5981aSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6174fd59a867SMatthew G. Knepley       }
6175ebb3236fSMatthew G. Knepley       ierr = PetscMalloc1(newn,&newidx);CHKERRQ(ierr);
6176ebb3236fSMatthew G. Knepley       newn = 0;
6177ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6178ebb3236fSMatthew G. Knepley         IS              tmp;
6179ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6180ebb3236fSMatthew G. Knepley 
6181c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6182ebb3236fSMatthew G. Knepley         if (!tmp) continue;
6183ebb3236fSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
6184ebb3236fSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6185ebb3236fSMatthew G. Knepley         if (isFE[field]) {
6186ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) newidx[newn++] = idx[p];
6187ebb3236fSMatthew G. Knepley         } else {
6188ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) newidx[newn++] = idx[p];
6189ebb3236fSMatthew G. Knepley         }
6190ebb3236fSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
6191ebb3236fSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6192ebb3236fSMatthew G. Knepley       }
6193ebb3236fSMatthew G. Knepley       ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), newn, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6194ebb3236fSMatthew G. Knepley     }
6195fd59a867SMatthew G. Knepley   }
6196fd59a867SMatthew G. Knepley   /* Handle discretization */
6197ebb3236fSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&numComp,numFields*(dim+1),&numDof);CHKERRQ(ierr);
6198fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
61990bacfa0aSMatthew G. Knepley     PetscObject obj;
62000bacfa0aSMatthew G. Knepley 
62010bacfa0aSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6202ebb3236fSMatthew G. Knepley     if (isFE[f]) {
62030bacfa0aSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
6204286d8613SMatthew G. Knepley       const PetscInt *numFieldDof;
6205fd59a867SMatthew G. Knepley       PetscInt        d;
6206fd59a867SMatthew G. Knepley 
6207fd59a867SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
6208286d8613SMatthew G. Knepley       ierr = PetscFEGetNumDof(fe, &numFieldDof);CHKERRQ(ierr);
6209286d8613SMatthew G. Knepley       for (d = 0; d < dim+1; ++d) numDof[f*(dim+1)+d] = numFieldDof[d];
6210ebb3236fSMatthew G. Knepley     } else {
62110bacfa0aSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
62120bacfa0aSMatthew G. Knepley 
62130bacfa0aSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
62140bacfa0aSMatthew G. Knepley       numDof[f*(dim+1)+dim] = numComp[f];
6215ebb3236fSMatthew G. Knepley     }
6216fd59a867SMatthew G. Knepley   }
6217286d8613SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6218286d8613SMatthew G. Knepley     PetscInt d;
6219286d8613SMatthew G. Knepley     for (d = 1; d < dim; ++d) {
6220286d8613SMatthew 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.");
6221286d8613SMatthew G. Knepley     }
6222286d8613SMatthew G. Knepley   }
6223ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSection(dm, dim, numFields, numComp, numDof, numBC, bcFields, bcComps, bcPoints, NULL, &section);CHKERRQ(ierr);
6224fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6225fd59a867SMatthew G. Knepley     PetscFE     fe;
6226fd59a867SMatthew G. Knepley     const char *name;
622718abd763SMatthew G. Knepley 
622818abd763SMatthew G. Knepley     ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
6229fd59a867SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
6230fd59a867SMatthew G. Knepley     ierr = PetscSectionSetFieldName(section, f, name);CHKERRQ(ierr);
6231fd59a867SMatthew G. Knepley   }
6232fd59a867SMatthew G. Knepley   ierr = DMSetDefaultSection(dm, section);CHKERRQ(ierr);
6233fd59a867SMatthew G. Knepley   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
62340e0f25f2SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {ierr = ISDestroy(&bcPoints[bc]);CHKERRQ(ierr);ierr = ISDestroy(&bcComps[bc]);CHKERRQ(ierr);}
62350e0f25f2SMatthew G. Knepley   ierr = PetscFree3(bcFields,bcPoints,bcComps);CHKERRQ(ierr);
6236286d8613SMatthew G. Knepley   ierr = PetscFree2(numComp,numDof);CHKERRQ(ierr);
6237ebb3236fSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
6238bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6239bceba477SMatthew G. Knepley }
6240bceba477SMatthew G. Knepley 
62410aef6b92SMatthew G. Knepley /*@
62420aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
62430aef6b92SMatthew G. Knepley 
62440aef6b92SMatthew G. Knepley   Input Parameter:
62450aef6b92SMatthew G. Knepley . dm - The DMPlex object
62460aef6b92SMatthew G. Knepley 
62470aef6b92SMatthew G. Knepley   Output Parameter:
62480aef6b92SMatthew G. Knepley . regular - The flag
62490aef6b92SMatthew G. Knepley 
62500aef6b92SMatthew G. Knepley   Level: intermediate
62510aef6b92SMatthew G. Knepley 
62520aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
62530aef6b92SMatthew G. Knepley @*/
62540aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
62550aef6b92SMatthew G. Knepley {
62560aef6b92SMatthew G. Knepley   PetscFunctionBegin;
62570aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62580aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
62590aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
62600aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
62610aef6b92SMatthew G. Knepley }
62620aef6b92SMatthew G. Knepley 
62630aef6b92SMatthew G. Knepley /*@
62640aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
62650aef6b92SMatthew G. Knepley 
62660aef6b92SMatthew G. Knepley   Input Parameters:
62670aef6b92SMatthew G. Knepley + dm - The DMPlex object
62680aef6b92SMatthew G. Knepley - regular - The flag
62690aef6b92SMatthew G. Knepley 
62700aef6b92SMatthew G. Knepley   Level: intermediate
62710aef6b92SMatthew G. Knepley 
62720aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
62730aef6b92SMatthew G. Knepley @*/
62740aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
62750aef6b92SMatthew G. Knepley {
62760aef6b92SMatthew G. Knepley   PetscFunctionBegin;
62770aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62780aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
62790aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
62800aef6b92SMatthew G. Knepley }
62810aef6b92SMatthew G. Knepley 
6282f7c74593SToby Isaac /* anchors */
6283a68b90caSToby Isaac /*@
6284f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
6285f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
6286a68b90caSToby Isaac 
6287e228b242SToby Isaac   not collective
6288a68b90caSToby Isaac 
6289a68b90caSToby Isaac   Input Parameters:
6290a68b90caSToby Isaac . dm - The DMPlex object
6291a68b90caSToby Isaac 
6292a68b90caSToby Isaac   Output Parameters:
6293a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
6294a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
6295a68b90caSToby Isaac 
6296a68b90caSToby Isaac 
6297a68b90caSToby Isaac   Level: intermediate
6298a68b90caSToby Isaac 
6299f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
6300a68b90caSToby Isaac @*/
6301a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
6302a68b90caSToby Isaac {
6303a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
630441e6d900SToby Isaac   PetscErrorCode ierr;
6305a68b90caSToby Isaac 
6306a68b90caSToby Isaac   PetscFunctionBegin;
6307a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
630841e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
6309a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
6310a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
6311a68b90caSToby Isaac   PetscFunctionReturn(0);
6312a68b90caSToby Isaac }
6313a68b90caSToby Isaac 
6314a68b90caSToby Isaac /*@
6315f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
6316f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
6317a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
6318a68b90caSToby Isaac 
6319a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
6320f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
6321a68b90caSToby Isaac 
6322e228b242SToby Isaac   collective on dm
6323a68b90caSToby Isaac 
6324a68b90caSToby Isaac   Input Parameters:
6325a68b90caSToby Isaac + dm - The DMPlex object
6326e228b242SToby 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).
6327e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
6328a68b90caSToby Isaac 
6329a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
6330a68b90caSToby Isaac 
6331a68b90caSToby Isaac   Level: intermediate
6332a68b90caSToby Isaac 
6333f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
6334a68b90caSToby Isaac @*/
6335a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
6336a68b90caSToby Isaac {
6337a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6338e228b242SToby Isaac   PetscMPIInt    result;
6339a68b90caSToby Isaac   PetscErrorCode ierr;
6340a68b90caSToby Isaac 
6341a68b90caSToby Isaac   PetscFunctionBegin;
6342a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6343e228b242SToby Isaac   if (anchorSection) {
6344e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
6345e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
6346e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
6347e228b242SToby Isaac   }
6348e228b242SToby Isaac   if (anchorIS) {
6349e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
6350e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
6351e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
6352e228b242SToby Isaac   }
6353a68b90caSToby Isaac 
6354a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
6355a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
6356a68b90caSToby Isaac   plex->anchorSection = anchorSection;
6357a68b90caSToby Isaac 
6358a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
6359a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
6360a68b90caSToby Isaac   plex->anchorIS = anchorIS;
6361a68b90caSToby Isaac 
6362a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
6363a68b90caSToby Isaac   if (anchorIS && anchorSection) {
6364a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
6365a68b90caSToby Isaac     const PetscInt *anchors;
6366a68b90caSToby Isaac 
6367a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
6368a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
6369a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
6370a68b90caSToby Isaac     for (a = 0; a < size; a++) {
6371a68b90caSToby Isaac       PetscInt p;
6372a68b90caSToby Isaac 
6373a68b90caSToby Isaac       p = anchors[a];
6374a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
6375a68b90caSToby Isaac         PetscInt dof;
6376a68b90caSToby Isaac 
6377a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6378a68b90caSToby Isaac         if (dof) {
6379a68b90caSToby Isaac           PetscErrorCode ierr2;
6380a68b90caSToby Isaac 
6381a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
63828ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
6383a68b90caSToby Isaac         }
6384a68b90caSToby Isaac       }
6385a68b90caSToby Isaac     }
6386a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
6387a68b90caSToby Isaac   }
6388a68b90caSToby Isaac #endif
6389f7c74593SToby Isaac   /* reset the generic constraints */
6390f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
6391a68b90caSToby Isaac   PetscFunctionReturn(0);
6392a68b90caSToby Isaac }
6393a68b90caSToby Isaac 
6394f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
6395a68b90caSToby Isaac {
6396f7c74593SToby Isaac   PetscSection anchorSection;
63976995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
6398a68b90caSToby Isaac   PetscErrorCode ierr;
6399a68b90caSToby Isaac 
6400a68b90caSToby Isaac   PetscFunctionBegin;
6401a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6402a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
6403e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
6404a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
64056995de1eSToby Isaac   if (numFields) {
6406719ab38cSToby Isaac     PetscInt f;
6407a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
6408719ab38cSToby Isaac 
6409719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
6410719ab38cSToby Isaac       PetscInt numComp;
6411719ab38cSToby Isaac 
6412719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
6413719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
6414719ab38cSToby Isaac     }
64156995de1eSToby Isaac   }
6416a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
64176995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
64186995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
64196995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
64206995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
6421a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
6422a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
6423a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6424a68b90caSToby Isaac     if (dof) {
6425a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
6426a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
6427a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
6428a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
6429a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
6430a68b90caSToby Isaac       }
6431a68b90caSToby Isaac     }
6432a68b90caSToby Isaac   }
6433a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
6434a68b90caSToby Isaac   PetscFunctionReturn(0);
6435a68b90caSToby Isaac }
6436a68b90caSToby Isaac 
6437f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
6438a68b90caSToby Isaac {
6439f7c74593SToby Isaac   PetscSection aSec;
64400ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
64410ac89760SToby Isaac   const PetscInt *anchors;
64420ac89760SToby Isaac   PetscInt numFields, f;
644366ad2231SToby Isaac   IS aIS;
64440ac89760SToby Isaac   PetscErrorCode ierr;
64450ac89760SToby Isaac 
64460ac89760SToby Isaac   PetscFunctionBegin;
64470ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64480ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
64490ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
64500ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
64510ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
6452302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
6453a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
645466ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
64556995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
64566995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
64570ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
64580ac89760SToby Isaac   i[0] = 0;
64590ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
64600ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
6461f19733c5SToby Isaac     PetscInt rDof, rOff, r;
6462f19733c5SToby Isaac 
6463f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
6464f19733c5SToby Isaac     if (!rDof) continue;
6465f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
64660ac89760SToby Isaac     if (numFields) {
64670ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
64680ac89760SToby Isaac         annz = 0;
6469f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
6470f19733c5SToby Isaac           a = anchors[rOff + r];
64710ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
64720ac89760SToby Isaac           annz += aDof;
64730ac89760SToby Isaac         }
64740ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
64750ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
64760ac89760SToby Isaac         for (q = 0; q < dof; q++) {
64770ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
64780ac89760SToby Isaac         }
64790ac89760SToby Isaac       }
64800ac89760SToby Isaac     }
64810ac89760SToby Isaac     else {
64820ac89760SToby Isaac       annz = 0;
64830ac89760SToby Isaac       for (q = 0; q < dof; q++) {
64840ac89760SToby Isaac         a = anchors[off + q];
64850ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
64860ac89760SToby Isaac         annz += aDof;
64870ac89760SToby Isaac       }
64880ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
64890ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
64900ac89760SToby Isaac       for (q = 0; q < dof; q++) {
64910ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
64920ac89760SToby Isaac       }
64930ac89760SToby Isaac     }
64940ac89760SToby Isaac   }
64950ac89760SToby Isaac   nnz = i[m];
64960ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
64970ac89760SToby Isaac   offset = 0;
64980ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
64990ac89760SToby Isaac     if (numFields) {
65000ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
65010ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
65020ac89760SToby Isaac         for (q = 0; q < dof; q++) {
65030ac89760SToby Isaac           PetscInt rDof, rOff, r;
65040ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
65050ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
65060ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
65070ac89760SToby Isaac             PetscInt s;
65080ac89760SToby Isaac 
65090ac89760SToby Isaac             a = anchors[rOff + r];
65100ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
65110ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
65120ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
65130ac89760SToby Isaac               j[offset++] = aOff + s;
65140ac89760SToby Isaac             }
65150ac89760SToby Isaac           }
65160ac89760SToby Isaac         }
65170ac89760SToby Isaac       }
65180ac89760SToby Isaac     }
65190ac89760SToby Isaac     else {
65200ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
65210ac89760SToby Isaac       for (q = 0; q < dof; q++) {
65220ac89760SToby Isaac         PetscInt rDof, rOff, r;
65230ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
65240ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
65250ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
65260ac89760SToby Isaac           PetscInt s;
65270ac89760SToby Isaac 
65280ac89760SToby Isaac           a = anchors[rOff + r];
65290ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
65300ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
65310ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
65320ac89760SToby Isaac             j[offset++] = aOff + s;
65330ac89760SToby Isaac           }
65340ac89760SToby Isaac         }
65350ac89760SToby Isaac       }
65360ac89760SToby Isaac     }
65370ac89760SToby Isaac   }
65380ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
653925570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
654025570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
654166ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
65420ac89760SToby Isaac   PetscFunctionReturn(0);
65430ac89760SToby Isaac }
65440ac89760SToby Isaac 
654566ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
654666ad2231SToby Isaac {
6547f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6548f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
654966ad2231SToby Isaac   Mat            cMat;
655066ad2231SToby Isaac   PetscErrorCode ierr;
655166ad2231SToby Isaac 
655266ad2231SToby Isaac   PetscFunctionBegin;
655366ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6554a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
655566ad2231SToby Isaac   if (anchorSection) {
6556e228b242SToby Isaac     PetscDS  ds;
6557e228b242SToby Isaac     PetscInt nf;
6558e228b242SToby Isaac 
6559f7c74593SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
6560f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
6561f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
6562e228b242SToby Isaac     ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
6563302440fdSBarry Smith     ierr = PetscDSGetNumFields(ds,&nf);CHKERRQ(ierr);
6564e228b242SToby Isaac     if (nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
656566ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
656666ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
656766ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
656866ad2231SToby Isaac   }
656966ad2231SToby Isaac   PetscFunctionReturn(0);
657066ad2231SToby Isaac }
6571