xref: /petsc/src/dm/impls/plex/plex.c (revision d1df6f1d629c59d81ec428a5f2e73f0c1f5cd8cd)
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 
48552f7358SJed Brown #undef __FUNCT__
49f13a32a3SMatthew G. Knepley #define __FUNCT__ "VecView_Plex_Local_Draw"
50f13a32a3SMatthew G. Knepley PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
51e412dcbdSMatthew G. Knepley {
52e412dcbdSMatthew G. Knepley   DM                 dm;
53*d1df6f1dSMatthew G. Knepley   PetscSection       s;
54e412dcbdSMatthew G. Knepley   PetscDraw          draw, popup;
55e412dcbdSMatthew G. Knepley   DM                 cdm;
56e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
57e412dcbdSMatthew G. Knepley   Vec                coordinates;
58e412dcbdSMatthew G. Knepley   const PetscScalar *coords, *array;
59e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
60339e3443SMatthew G. Knepley   PetscReal          vbound[2], time;
61339e3443SMatthew G. Knepley   PetscBool          isnull, flg;
62*d1df6f1dSMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
63e412dcbdSMatthew G. Knepley   const char        *name;
64339e3443SMatthew G. Knepley   char               title[PETSC_MAX_PATH_LEN];
65e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
66e412dcbdSMatthew G. Knepley 
67e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
68*d1df6f1dSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
69*d1df6f1dSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
70*d1df6f1dSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
71*d1df6f1dSMatthew G. Knepley 
72e412dcbdSMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
73e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
74e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
75*d1df6f1dSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
76*d1df6f1dSMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
77e412dcbdSMatthew G. Knepley   ierr = DMGetCoarsenLevel(dm, &level);CHKERRQ(ierr);
78e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
79e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
80e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
81e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
82e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
83e412dcbdSMatthew G. Knepley 
84e412dcbdSMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
85339e3443SMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, &step, &time);CHKERRQ(ierr);
86e412dcbdSMatthew G. Knepley 
87e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
88e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
89e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
900c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
910c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
92e412dcbdSMatthew G. Knepley   }
93e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
94e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
95e412dcbdSMatthew G. Knepley 
96*d1df6f1dSMatthew G. Knepley   /* Could implement something like DMDASelectFields() */
97*d1df6f1dSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
98*d1df6f1dSMatthew G. Knepley     DM   fdm = dm;
99*d1df6f1dSMatthew G. Knepley     Vec  fv  = v;
100*d1df6f1dSMatthew G. Knepley     IS   fis;
101*d1df6f1dSMatthew G. Knepley     char prefix[PETSC_MAX_PATH_LEN];
102*d1df6f1dSMatthew G. Knepley     const char *fname;
103*d1df6f1dSMatthew G. Knepley 
104*d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
105*d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldName(s, f, &fname);CHKERRQ(ierr);
106*d1df6f1dSMatthew G. Knepley 
107*d1df6f1dSMatthew G. Knepley     if (v->hdr.prefix) {ierr = PetscStrcpy(prefix, v->hdr.prefix);CHKERRQ(ierr);}
108*d1df6f1dSMatthew G. Knepley     else               {prefix[0] = '\0';}
109*d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
110*d1df6f1dSMatthew G. Knepley       ierr = DMCreateSubDM(dm, 1, &f, &fis, &fdm);CHKERRQ(ierr);
111*d1df6f1dSMatthew G. Knepley       ierr = VecGetSubVector(v, fis, &fv);CHKERRQ(ierr);
112*d1df6f1dSMatthew G. Knepley       ierr = PetscStrcat(prefix, fname);CHKERRQ(ierr);
113*d1df6f1dSMatthew G. Knepley       ierr = PetscStrcat(prefix, "_");CHKERRQ(ierr);
114*d1df6f1dSMatthew G. Knepley     }
115*d1df6f1dSMatthew G. Knepley     for (comp = 0; comp < Nc; ++comp, ++w) {
116*d1df6f1dSMatthew G. Knepley       PetscInt nmax = 2;
117*d1df6f1dSMatthew G. Knepley 
118*d1df6f1dSMatthew G. Knepley       ierr = PetscViewerDrawGetDraw(viewer, w, &draw);CHKERRQ(ierr);
119*d1df6f1dSMatthew G. Knepley       if (Nc > 1) {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s_%D Step: %D Time: %.4g", name, fname, comp, step, time);CHKERRQ(ierr);}
120*d1df6f1dSMatthew G. Knepley       else        {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s Step: %D Time: %.4g", name, fname, step, time);CHKERRQ(ierr);}
121*d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetTitle(draw, title);CHKERRQ(ierr);
122*d1df6f1dSMatthew G. Knepley 
123*d1df6f1dSMatthew G. Knepley       /* TODO Get max and min only for this component */
124*d1df6f1dSMatthew G. Knepley       ierr = PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg);CHKERRQ(ierr);
125339e3443SMatthew G. Knepley       if (!flg) {
126*d1df6f1dSMatthew G. Knepley         ierr = VecMin(fv, NULL, &vbound[0]);CHKERRQ(ierr);
127*d1df6f1dSMatthew G. Knepley         ierr = VecMax(fv, NULL, &vbound[1]);CHKERRQ(ierr);
128*d1df6f1dSMatthew G. Knepley         if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
129339e3443SMatthew G. Knepley       }
130e412dcbdSMatthew G. Knepley       ierr = PetscDrawGetPopup(draw, &popup);CHKERRQ(ierr);
131339e3443SMatthew G. Knepley       ierr = PetscDrawScalePopup(popup, vbound[0], vbound[1]);CHKERRQ(ierr);
132*d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
133e412dcbdSMatthew G. Knepley 
134*d1df6f1dSMatthew G. Knepley       ierr = VecGetArrayRead(fv, &array);CHKERRQ(ierr);
135e412dcbdSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
13699a2f7bcSMatthew G. Knepley         PetscScalar *coords = NULL, *a = NULL;
137339e3443SMatthew G. Knepley         PetscInt     numCoords, color[4];
138e412dcbdSMatthew G. Knepley 
139*d1df6f1dSMatthew G. Knepley         ierr = DMPlexPointLocalRead(fdm, c, array, &a);CHKERRQ(ierr);
140339e3443SMatthew G. Knepley         if (a) {
141*d1df6f1dSMatthew G. Knepley           color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
142339e3443SMatthew G. Knepley           color[1] = color[2] = color[3] = color[0];
143339e3443SMatthew G. Knepley         } else {
144339e3443SMatthew G. Knepley           PetscScalar *vals = NULL;
145339e3443SMatthew G. Knepley           PetscInt     numVals, va;
146339e3443SMatthew G. Knepley 
147*d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
148*d1df6f1dSMatthew 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);
149*d1df6f1dSMatthew G. Knepley           switch (numVals/Nc) {
150*d1df6f1dSMatthew G. Knepley           case 3: /* P1 Triangle */
151*d1df6f1dSMatthew G. Knepley           case 4: /* P1 Quadrangle */
152*d1df6f1dSMatthew G. Knepley             for (va = 0; va < numVals/Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp]), vbound[0], vbound[1]);
153339e3443SMatthew G. Knepley             break;
154*d1df6f1dSMatthew G. Knepley           case 6: /* P2 Triangle */
155*d1df6f1dSMatthew G. Knepley           case 8: /* P2 Quadrangle */
156*d1df6f1dSMatthew 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]);
157*d1df6f1dSMatthew G. Knepley             break;
158*d1df6f1dSMatthew G. Knepley           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %D cannot be handled", numVals/Nc);
159339e3443SMatthew G. Knepley           }
160*d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
161339e3443SMatthew G. Knepley         }
162e412dcbdSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
163e412dcbdSMatthew G. Knepley         switch (numCoords) {
164e412dcbdSMatthew G. Knepley         case 6:
165339e3443SMatthew 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);
166e412dcbdSMatthew G. Knepley           break;
167e412dcbdSMatthew G. Knepley         case 8:
168339e3443SMatthew 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);
169339e3443SMatthew 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);
170e412dcbdSMatthew G. Knepley           break;
171e412dcbdSMatthew G. Knepley         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
172e412dcbdSMatthew G. Knepley         }
173e412dcbdSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
174e412dcbdSMatthew G. Knepley       }
175*d1df6f1dSMatthew G. Knepley       ierr = VecRestoreArrayRead(fv, &array);CHKERRQ(ierr);
176e412dcbdSMatthew G. Knepley       ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
177e412dcbdSMatthew G. Knepley       ierr = PetscDrawPause(draw);CHKERRQ(ierr);
178e412dcbdSMatthew G. Knepley       ierr = PetscDrawSave(draw);CHKERRQ(ierr);
179*d1df6f1dSMatthew G. Knepley     }
180*d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
181*d1df6f1dSMatthew G. Knepley       ierr = VecRestoreSubVector(v, fis, &fv);CHKERRQ(ierr);
182*d1df6f1dSMatthew G. Knepley       ierr = ISDestroy(&fis);CHKERRQ(ierr);
183*d1df6f1dSMatthew G. Knepley       ierr = DMDestroy(&fdm);CHKERRQ(ierr);
184*d1df6f1dSMatthew G. Knepley     }
185*d1df6f1dSMatthew G. Knepley   }
186e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
187e412dcbdSMatthew G. Knepley }
188e412dcbdSMatthew G. Knepley 
189e412dcbdSMatthew G. Knepley #undef __FUNCT__
190f13a32a3SMatthew G. Knepley #define __FUNCT__ "VecView_Plex_Local"
191552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
192552f7358SJed Brown {
193552f7358SJed Brown   DM             dm;
194f13a32a3SMatthew G. Knepley   PetscBool      isvtk, ishdf5, isdraw, isseq;
195552f7358SJed Brown   PetscErrorCode ierr;
196552f7358SJed Brown 
197552f7358SJed Brown   PetscFunctionBegin;
198552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
199552f7358SJed Brown   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
200552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
201552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
202f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW, &isdraw);CHKERRQ(ierr);
203552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
204552f7358SJed Brown   if (isvtk || ishdf5) {
205552f7358SJed Brown     PetscInt  numFields;
206552f7358SJed Brown     PetscBool fem = PETSC_FALSE;
207552f7358SJed Brown 
208552f7358SJed Brown     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
209552f7358SJed Brown     if (numFields) {
210552f7358SJed Brown       PetscObject fe;
211552f7358SJed Brown 
212552f7358SJed Brown       ierr = DMGetField(dm, 0, &fe);CHKERRQ(ierr);
213552f7358SJed Brown       if (fe->classid == PETSCFE_CLASSID) fem = PETSC_TRUE;
214552f7358SJed Brown     }
215552f7358SJed Brown     if (fem) {ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, v, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);}
216552f7358SJed Brown   }
217552f7358SJed Brown   if (isvtk) {
218552f7358SJed Brown     PetscSection            section;
219552f7358SJed Brown     PetscViewerVTKFieldType ft;
220552f7358SJed Brown     PetscInt                pStart, pEnd;
221552f7358SJed Brown 
222552f7358SJed Brown     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
223552f7358SJed Brown     ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
224552f7358SJed Brown     ierr = PetscObjectReference((PetscObject) v);CHKERRQ(ierr);  /* viewer drops reference */
225552f7358SJed Brown     ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, ft, (PetscObject) v);CHKERRQ(ierr);
226552f7358SJed Brown   } else if (ishdf5) {
227552f7358SJed Brown #if defined(PETSC_HAVE_HDF5)
228552f7358SJed Brown     ierr = VecView_Plex_Local_HDF5(v, viewer);CHKERRQ(ierr);
229552f7358SJed Brown #else
230552f7358SJed Brown     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
231552f7358SJed Brown #endif
232f13a32a3SMatthew G. Knepley   } else if (isdraw) {
233f13a32a3SMatthew G. Knepley     ierr = VecView_Plex_Local_Draw(v, viewer);CHKERRQ(ierr);
234552f7358SJed Brown   } else {
235552f7358SJed Brown     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
236552f7358SJed Brown     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
237552f7358SJed Brown   }
238552f7358SJed Brown   PetscFunctionReturn(0);
239552f7358SJed Brown }
240552f7358SJed Brown 
241f13a32a3SMatthew G. Knepley #undef __FUNCT__
242552f7358SJed Brown #define __FUNCT__ "VecView_Plex"
243552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
244552f7358SJed Brown {
245552f7358SJed Brown   DM             dm;
246f13a32a3SMatthew G. Knepley   PetscReal      time = 0.0;
247e412dcbdSMatthew G. Knepley   PetscBool      isvtk, ishdf5, isdraw, isseq;
248552f7358SJed Brown   PetscErrorCode ierr;
249552f7358SJed Brown 
250552f7358SJed Brown   PetscFunctionBegin;
251552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
25282f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
253552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
25433c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
255e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW, &isdraw);CHKERRQ(ierr);
25655f2e967SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
257552f7358SJed Brown   if (isvtk) {
258552f7358SJed Brown     Vec         locv;
259552f7358SJed Brown     const char *name;
260552f7358SJed Brown 
261552f7358SJed Brown     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
262552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
263552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
264552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
265552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
266cf9ba882SMatthew G. Knepley     ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
267cf9ba882SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
268552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
269552f7358SJed Brown     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
270b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
271b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
272b136c2c9SMatthew G. Knepley     ierr = VecView_Plex_HDF5(v, viewer);CHKERRQ(ierr);
273b136c2c9SMatthew G. Knepley #else
274b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
275b136c2c9SMatthew G. Knepley #endif
276e412dcbdSMatthew G. Knepley   } else if (isdraw) {
277f13a32a3SMatthew G. Knepley     Vec         locv;
278f13a32a3SMatthew G. Knepley     const char *name;
279f13a32a3SMatthew G. Knepley 
280f13a32a3SMatthew G. Knepley     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
281f13a32a3SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
282f13a32a3SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
283f13a32a3SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
284f13a32a3SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
285f13a32a3SMatthew G. Knepley     ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
286f13a32a3SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
287f13a32a3SMatthew G. Knepley     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
288f13a32a3SMatthew G. Knepley     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
289552f7358SJed Brown   } else {
29055f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
29155f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
292552f7358SJed Brown   }
293552f7358SJed Brown   PetscFunctionReturn(0);
294552f7358SJed Brown }
295552f7358SJed Brown 
296d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
297d930f514SMatthew G. Knepley {
298d930f514SMatthew G. Knepley   DM                dm;
299d930f514SMatthew G. Knepley   MPI_Comm          comm;
300d930f514SMatthew G. Knepley   PetscViewerFormat format;
301d930f514SMatthew G. Knepley   Vec               v;
302d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
303d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
304d930f514SMatthew G. Knepley 
305d930f514SMatthew G. Knepley   PetscFunctionBegin;
306d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
307d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
3082c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
309d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
310d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
311d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
312d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
313d930f514SMatthew G. Knepley     const char *vecname;
314d930f514SMatthew G. Knepley     PetscInt    n, nroots;
315d930f514SMatthew G. Knepley 
316d930f514SMatthew G. Knepley     if (dm->sfNatural) {
317ca43db0aSBarry Smith       ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
318d930f514SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
319d930f514SMatthew G. Knepley       if (n == nroots) {
320d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
321d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
322d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
323d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
324d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
325d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
326d930f514SMatthew G. Knepley     } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
327d930f514SMatthew G. Knepley   } else {
328d930f514SMatthew G. Knepley     /* we are viewing a natural DMPlex vec. */
329d930f514SMatthew G. Knepley     v = originalv;
330d930f514SMatthew G. Knepley   }
331d930f514SMatthew G. Knepley   if (ishdf5) {
332d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
333d930f514SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native(v, viewer);CHKERRQ(ierr);
334d930f514SMatthew G. Knepley #else
335d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
336d930f514SMatthew G. Knepley #endif
337d930f514SMatthew G. Knepley   } else if (isvtk) {
338d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
339d930f514SMatthew G. Knepley   } else {
340d930f514SMatthew G. Knepley     PetscBool isseq;
341d930f514SMatthew G. Knepley 
342d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
343d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
344d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
345d930f514SMatthew G. Knepley   }
346d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
347d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
348d930f514SMatthew G. Knepley }
349d930f514SMatthew G. Knepley 
3502c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
3512c40f234SMatthew G. Knepley {
3522c40f234SMatthew G. Knepley   DM             dm;
3532c40f234SMatthew G. Knepley   PetscBool      ishdf5;
3542c40f234SMatthew G. Knepley   PetscErrorCode ierr;
3552c40f234SMatthew G. Knepley 
3562c40f234SMatthew G. Knepley   PetscFunctionBegin;
3572c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
3582c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
3592c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
3602c40f234SMatthew G. Knepley   if (ishdf5) {
3612c40f234SMatthew G. Knepley     DM          dmBC;
3622c40f234SMatthew G. Knepley     Vec         gv;
3632c40f234SMatthew G. Knepley     const char *name;
3642c40f234SMatthew G. Knepley 
3652c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
3662c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
3672c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
3682c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
3692c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
3702c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
3712c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
3722c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
3732c40f234SMatthew G. Knepley   } else {
3742c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
3752c40f234SMatthew G. Knepley   }
3762c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
3772c40f234SMatthew G. Knepley }
3782c40f234SMatthew G. Knepley 
3792c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
3802c40f234SMatthew G. Knepley {
3812c40f234SMatthew G. Knepley   DM             dm;
3822c40f234SMatthew G. Knepley   PetscBool      ishdf5;
3832c40f234SMatthew G. Knepley   PetscErrorCode ierr;
3842c40f234SMatthew G. Knepley 
3852c40f234SMatthew G. Knepley   PetscFunctionBegin;
3862c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
3872c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
3882c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
3892c40f234SMatthew G. Knepley   if (ishdf5) {
390878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
391b136c2c9SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5(v, viewer);CHKERRQ(ierr);
392b136c2c9SMatthew G. Knepley #else
393b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
394878b459fSMatthew G. Knepley #endif
3952c40f234SMatthew G. Knepley   } else {
3962c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
397552f7358SJed Brown   }
398552f7358SJed Brown   PetscFunctionReturn(0);
399552f7358SJed Brown }
400552f7358SJed Brown 
401d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
402d930f514SMatthew G. Knepley {
403d930f514SMatthew G. Knepley   DM                dm;
404d930f514SMatthew G. Knepley   PetscViewerFormat format;
405d930f514SMatthew G. Knepley   PetscBool         ishdf5;
406d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
407d930f514SMatthew G. Knepley 
408d930f514SMatthew G. Knepley   PetscFunctionBegin;
409d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
410d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
411d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
412d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
413d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
414d930f514SMatthew G. Knepley     if (dm->sfNatural) {
415d930f514SMatthew G. Knepley       if (ishdf5) {
416d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
417d930f514SMatthew G. Knepley         Vec         v;
418d930f514SMatthew G. Knepley         const char *vecname;
419d930f514SMatthew G. Knepley 
420d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
421d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
422d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
423d930f514SMatthew G. Knepley         ierr = VecLoad_Plex_HDF5_Native(v, viewer);CHKERRQ(ierr);
424d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
425d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
426d930f514SMatthew G. Knepley         ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
427d930f514SMatthew G. Knepley #else
428d930f514SMatthew G. Knepley         SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
429d930f514SMatthew G. Knepley #endif
430d930f514SMatthew G. Knepley       } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
431d930f514SMatthew G. Knepley     }
432d930f514SMatthew G. Knepley   }
433d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
434d930f514SMatthew G. Knepley }
435d930f514SMatthew G. Knepley 
436731e8ddeSMatthew G. Knepley PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
437731e8ddeSMatthew G. Knepley {
438731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
439731e8ddeSMatthew G. Knepley   Vec                coordinates;
440731e8ddeSMatthew G. Knepley   DMLabel            depthLabel;
441731e8ddeSMatthew G. Knepley   const char        *name[4];
442731e8ddeSMatthew G. Knepley   const PetscScalar *a;
443731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
444731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
445731e8ddeSMatthew G. Knepley 
446731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
447731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
448731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
449731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
450731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
451731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
452731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
453731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
454731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
455731e8ddeSMatthew G. Knepley   name[1]     = "edge";
456731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
457731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
458731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
459731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
460731e8ddeSMatthew G. Knepley     PetscInt  closureSize, cl;
461731e8ddeSMatthew G. Knepley 
462f347f43bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D:\n", c);CHKERRQ(ierr);
463731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
464731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
465731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
466731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
467731e8ddeSMatthew G. Knepley 
468731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
469731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
470731e8ddeSMatthew G. Knepley       if (!dof) continue;
471731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
472731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
473f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
474731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
475731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
476731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
477731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
47841477eefSMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
479731e8ddeSMatthew G. Knepley         }
480731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
481731e8ddeSMatthew G. Knepley       }
482731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
483731e8ddeSMatthew G. Knepley     }
484731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
485731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
486731e8ddeSMatthew G. Knepley   }
487731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
488731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
489731e8ddeSMatthew G. Knepley }
490731e8ddeSMatthew G. Knepley 
491552f7358SJed Brown PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
492552f7358SJed Brown {
493552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
494552f7358SJed Brown   DM                cdm;
495552f7358SJed Brown   DMLabel           markers;
496552f7358SJed Brown   PetscSection      coordSection;
497552f7358SJed Brown   Vec               coordinates;
498552f7358SJed Brown   PetscViewerFormat format;
499552f7358SJed Brown   PetscErrorCode    ierr;
500552f7358SJed Brown 
501552f7358SJed Brown   PetscFunctionBegin;
502552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
503552f7358SJed Brown   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
504552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
505552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
506552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
507552f7358SJed Brown     const char *name;
508552f7358SJed Brown     PetscInt    maxConeSize, maxSupportSize;
509552f7358SJed Brown     PetscInt    pStart, pEnd, p;
510552f7358SJed Brown     PetscMPIInt rank, size;
511552f7358SJed Brown 
51282f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
51382f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
514552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
515552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
516552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
517552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "Mesh '%s':\n", name);CHKERRQ(ierr);
518552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "orientation is missing\n", name);CHKERRQ(ierr);
519552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "cap --> base:\n", name);CHKERRQ(ierr);
5204d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
5214d4c343aSBarry Smith     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max sizes cone: %D support: %D\n", rank,maxConeSize, maxSupportSize);CHKERRQ(ierr);
522552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
523552f7358SJed Brown       PetscInt dof, off, s;
524552f7358SJed Brown 
525552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
526552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
527552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
528e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
529552f7358SJed Brown       }
530552f7358SJed Brown     }
531552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
532552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "base <-- cap:\n", name);CHKERRQ(ierr);
533552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
534552f7358SJed Brown       PetscInt dof, off, c;
535552f7358SJed Brown 
536552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
537552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
538552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
539e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
540552f7358SJed Brown       }
541552f7358SJed Brown     }
542552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
5434d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
5440298fd71SBarry Smith     ierr = PetscSectionGetChart(coordSection, &pStart, NULL);CHKERRQ(ierr);
545552f7358SJed Brown     if (pStart >= 0) {ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);}
546c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
547552f7358SJed Brown     ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);
548552f7358SJed Brown     if (size > 1) {
549552f7358SJed Brown       PetscSF sf;
550552f7358SJed Brown 
551552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
552552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
553552f7358SJed Brown     }
554552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
555552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
5560588280cSMatthew G. Knepley     const char  *name, *color;
5570588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
5580588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
559552f7358SJed Brown     PetscReal    scale         = 2.0;
5600588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
5610588280cSMatthew G. Knepley     double       tcoords[3];
562552f7358SJed Brown     PetscScalar *coords;
5630588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
564552f7358SJed Brown     PetscMPIInt  rank, size;
5650588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
566552f7358SJed Brown 
5670588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
568552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
569c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
5700588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
5710588280cSMatthew G. Knepley     numColors  = 10;
5720588280cSMatthew G. Knepley     numLColors = 10;
5730588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
574c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
575c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
576c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
5770588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
578c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
5790588280cSMatthew G. Knepley     if (!useColors) {
5800588280cSMatthew G. Knepley       numColors = 3;
5810588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
5820588280cSMatthew G. Knepley     }
583c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
5840588280cSMatthew G. Knepley     if (!useColors) {
5850588280cSMatthew G. Knepley       numLColors = 4;
5860588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
5870588280cSMatthew G. Knepley     }
58882f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
58982f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
590552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
591770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
5920588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
593552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
594552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
595552f7358SJed Brown \\usetikzlibrary{arrows}\n\
5960588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
5970588280cSMatthew G. Knepley     if (size > 1) {
598f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
599770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
600770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
601770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
602770b213bSMatthew G Knepley         } else if (p > 0) {
603770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
604770b213bSMatthew G Knepley         }
605770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
606770b213bSMatthew G Knepley       }
6070588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
6080588280cSMatthew G. Knepley     }
6090588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", 1.0);CHKERRQ(ierr);
610552f7358SJed Brown     /* Plot vertices */
611552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
612552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
6134d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
614552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
615552f7358SJed Brown       PetscInt  off, dof, d;
6160588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
617552f7358SJed Brown 
618552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
619552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
6200588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
621f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
6220588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
6230588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
6240588280cSMatthew G. Knepley         tcoords[d] = PetscAbsReal(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
6250588280cSMatthew G. Knepley       }
6260588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
6270588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
628552f7358SJed Brown       for (d = 0; d < dof; ++d) {
629552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
6300588280cSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]);CHKERRQ(ierr);
631552f7358SJed Brown       }
6320588280cSMatthew G. Knepley       color = colors[rank%numColors];
6330588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
6340588280cSMatthew G. Knepley         PetscInt val;
635c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
6360588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
6370588280cSMatthew G. Knepley       }
6380588280cSMatthew G. Knepley       if (useNumbers) {
639e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
6400588280cSMatthew G. Knepley       } else {
641e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
6420588280cSMatthew G. Knepley       }
643552f7358SJed Brown     }
644552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
645552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
646552f7358SJed Brown     /* Plot edges */
6470588280cSMatthew G. Knepley     if (depth > 1) {ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);}
6480588280cSMatthew G. Knepley     if (dim < 3 && useNumbers) {
649552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
650552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
651552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
652552f7358SJed Brown         const PetscInt *cone;
653552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
654552f7358SJed Brown 
655552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
656f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
657552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
658552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
659552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
660552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
661552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
662552f7358SJed Brown         for (d = 0; d < dof; ++d) {
6630588280cSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
6640588280cSMatthew G. Knepley           tcoords[d] = PetscAbsReal(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
665552f7358SJed Brown         }
6660588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
6670588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
6680588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
6690588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
670f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
6710588280cSMatthew G. Knepley         }
6720588280cSMatthew G. Knepley         color = colors[rank%numColors];
6730588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
6740588280cSMatthew G. Knepley           PetscInt val;
675c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
6760750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
6770588280cSMatthew G. Knepley         }
678e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
679552f7358SJed Brown       }
680552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
681552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
682552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
6830588280cSMatthew G. Knepley     }
684552f7358SJed Brown     /* Plot cells */
6850588280cSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
6860588280cSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
6870588280cSMatthew G. Knepley         const PetscInt *cone;
6880588280cSMatthew G. Knepley 
6890588280cSMatthew G. Knepley         color = colors[rank%numColors];
6900588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
6910588280cSMatthew G. Knepley           PetscInt val;
692c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
6930588280cSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
6940588280cSMatthew G. Knepley         }
6950588280cSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
696e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
6970588280cSMatthew G. Knepley       }
6980588280cSMatthew G. Knepley     } else {
699552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
700552f7358SJed Brown       for (c = cStart; c < cEnd; ++c) {
7010298fd71SBarry Smith         PetscInt *closure = NULL;
702552f7358SJed Brown         PetscInt  closureSize, firstPoint = -1;
703552f7358SJed Brown 
704552f7358SJed Brown         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
705552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
706552f7358SJed Brown         for (p = 0; p < closureSize*2; p += 2) {
707552f7358SJed Brown           const PetscInt point = closure[p];
708552f7358SJed Brown 
709552f7358SJed Brown           if ((point < vStart) || (point >= vEnd)) continue;
710552f7358SJed Brown           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
711e4b003c7SBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
712552f7358SJed Brown           if (firstPoint < 0) firstPoint = point;
713552f7358SJed Brown         }
714552f7358SJed Brown         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
715e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
716552f7358SJed Brown         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
717552f7358SJed Brown       }
7180588280cSMatthew G. Knepley     }
719552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
7204d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
7210588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
722770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
7230588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
7240588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
7250588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
7260588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
727552f7358SJed Brown   } else {
72882f516ccSBarry Smith     MPI_Comm    comm;
729834065abSMatthew G. Knepley     PetscInt   *sizes, *hybsizes;
730834065abSMatthew G. Knepley     PetscInt    locDepth, depth, dim, d, pMax[4];
731552f7358SJed Brown     PetscInt    pStart, pEnd, p;
732a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
7331143a3c0SMatthew G. Knepley     const char *name;
734552f7358SJed Brown     PetscMPIInt size;
735552f7358SJed Brown 
73682f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
737552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
738c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7395f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
7401143a3c0SMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimensions:\n", name, dim);CHKERRQ(ierr);}
7411143a3c0SMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimensions:\n", dim);CHKERRQ(ierr);}
742552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
743b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
744bb81ee50SMatthew G. Knepley     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, &pMax[1], &pMax[0]);CHKERRQ(ierr);
745dcca6d9dSJed Brown     ierr = PetscMalloc2(size,&sizes,size,&hybsizes);CHKERRQ(ierr);
746552f7358SJed Brown     if (depth == 1) {
747552f7358SJed Brown       ierr = DMPlexGetDepthStratum(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);
750f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);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       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
754552f7358SJed Brown       pEnd = pEnd - pStart;
755552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
756552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
757552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
758552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
759552f7358SJed Brown     } else {
760cbb7f117SMark Adams       PetscMPIInt rank;
761cbb7f117SMark Adams       ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
762552f7358SJed Brown       for (d = 0; d <= dim; d++) {
763552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
764834065abSMatthew G. Knepley         pEnd    -= pStart;
765834065abSMatthew G. Knepley         pMax[d] -= pStart;
766552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
767834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
768552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
769834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
770cbb7f117SMark Adams           if (!rank) {
771834065abSMatthew G. Knepley             if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
772834065abSMatthew G. Knepley             else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
773834065abSMatthew G. Knepley           }
774cbb7f117SMark Adams         }
775552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
776552f7358SJed Brown       }
777552f7358SJed Brown     }
778834065abSMatthew G. Knepley     ierr = PetscFree2(sizes,hybsizes);CHKERRQ(ierr);
779c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
780a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
781a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
782a57dd577SMatthew G Knepley       DMLabel         label;
783a57dd577SMatthew G Knepley       const char     *name;
784a57dd577SMatthew G Knepley       IS              valueIS;
785a57dd577SMatthew G Knepley       const PetscInt *values;
786a57dd577SMatthew G Knepley       PetscInt        numValues, v;
787a57dd577SMatthew G Knepley 
788c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
789c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
790a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
791f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata of sizes (", name, numValues);CHKERRQ(ierr);
792a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
793a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
794120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
795a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
796a57dd577SMatthew G Knepley         PetscInt size;
797a57dd577SMatthew G Knepley 
798a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
799a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
800f347f43bSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer, "%D", size);CHKERRQ(ierr);
801a57dd577SMatthew G Knepley       }
802a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
803120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
804a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
8054d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
806a57dd577SMatthew G Knepley     }
807a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
8088e7ff633SMatthew G. Knepley     if (cdm) {
8098e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
8108e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
8118e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
8128e7ff633SMatthew G. Knepley     }
813552f7358SJed Brown   }
814552f7358SJed Brown   PetscFunctionReturn(0);
815552f7358SJed Brown }
816552f7358SJed Brown 
817e412dcbdSMatthew G. Knepley PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
818e412dcbdSMatthew G. Knepley {
819e412dcbdSMatthew G. Knepley   PetscDraw          draw;
820e412dcbdSMatthew G. Knepley   DM                 cdm;
821e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
822e412dcbdSMatthew G. Knepley   Vec                coordinates;
823e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
824e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
825e412dcbdSMatthew G. Knepley   PetscBool          isnull;
826e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
827e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
828e412dcbdSMatthew G. Knepley 
829e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
830e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
831e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
832e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
833e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
834e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
835e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
836e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
837e412dcbdSMatthew G. Knepley 
838e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
839e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
840e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
841e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
842e412dcbdSMatthew G. Knepley 
843e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
844e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
845e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
8460c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
8470c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
848e412dcbdSMatthew G. Knepley   }
849e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
850e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
851e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
852e412dcbdSMatthew G. Knepley 
853e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
854e412dcbdSMatthew G. Knepley     PetscScalar *coords = NULL;
855e412dcbdSMatthew G. Knepley     PetscInt     numCoords;
856e412dcbdSMatthew G. Knepley 
857e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
858e412dcbdSMatthew G. Knepley     switch (numCoords) {
859e412dcbdSMatthew G. Knepley     case 6:
8600c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8610c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8620c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
863e412dcbdSMatthew G. Knepley       break;
864e412dcbdSMatthew G. Knepley     case 8:
8650c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8660c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8670c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
8680c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
869e412dcbdSMatthew G. Knepley       break;
870e412dcbdSMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
871e412dcbdSMatthew G. Knepley     }
872e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
873e412dcbdSMatthew G. Knepley   }
874e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
875e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
876e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
877e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
878e412dcbdSMatthew G. Knepley }
879e412dcbdSMatthew G. Knepley 
880552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
881552f7358SJed Brown {
882e412dcbdSMatthew G. Knepley   PetscBool      iascii, ishdf5, isvtk, isdraw;
883552f7358SJed Brown   PetscErrorCode ierr;
884552f7358SJed Brown 
885552f7358SJed Brown   PetscFunctionBegin;
886552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
887552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
888552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
889fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
890c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
891e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
892552f7358SJed Brown   if (iascii) {
893552f7358SJed Brown     ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
894c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
895c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
8967afe7537SMatthew G. Knepley     ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_VIZ);CHKERRQ(ierr);
897c6ccd67eSMatthew G. Knepley     ierr = DMPlexView_HDF5(dm, viewer);CHKERRQ(ierr);
8987afe7537SMatthew G. Knepley     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
899c6ccd67eSMatthew G. Knepley #else
900c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
901552f7358SJed Brown #endif
902e412dcbdSMatthew G. Knepley   } else if (isvtk) {
903fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
904e412dcbdSMatthew G. Knepley   } else if (isdraw) {
905e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
906fcf6c8fdSToby Isaac   }
907552f7358SJed Brown   PetscFunctionReturn(0);
908552f7358SJed Brown }
909552f7358SJed Brown 
9102c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
9112c40f234SMatthew G. Knepley {
9122c40f234SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
9132c40f234SMatthew G. Knepley   PetscErrorCode ierr;
9142c40f234SMatthew G. Knepley 
9152c40f234SMatthew G. Knepley   PetscFunctionBegin;
9162c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9172c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
9182c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERBINARY, &isbinary);CHKERRQ(ierr);
9192c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
9202c40f234SMatthew G. Knepley   if (isbinary) {SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Do not yet support binary viewers");}
9212c40f234SMatthew G. Knepley   else if (ishdf5) {
9222c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
92310b7db4eSMatthew G. Knepley     ierr = DMPlexLoad_HDF5(dm, viewer);CHKERRQ(ierr);
9242c40f234SMatthew G. Knepley #else
9252c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
926552f7358SJed Brown #endif
927552f7358SJed Brown   }
928552f7358SJed Brown   PetscFunctionReturn(0);
929552f7358SJed Brown }
930552f7358SJed Brown 
9316c73c22cSMatthew G. Knepley 
932552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
933552f7358SJed Brown {
934552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
935552f7358SJed Brown   PetscErrorCode ierr;
936552f7358SJed Brown 
937552f7358SJed Brown   PetscFunctionBegin;
9380d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
939552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
940552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
941552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
942552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
943be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
944552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
945552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
946d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
947d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
94877623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
949a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
950552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
951552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
952a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
953a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
954d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
955d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
956d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
957d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
958d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
959d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
960fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
961552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
962552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
963713918a9SToby Isaac   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMAdaptLabel_C",NULL);CHKERRQ(ierr);
964552f7358SJed Brown   PetscFunctionReturn(0);
965552f7358SJed Brown }
966552f7358SJed Brown 
967b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
968552f7358SJed Brown {
9698d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
970acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
971552f7358SJed Brown   PetscInt               localSize;
972837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
973552f7358SJed Brown   PetscErrorCode         ierr;
974b412c318SBarry Smith   MatType                mtype;
9751428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
976552f7358SJed Brown 
977552f7358SJed Brown   PetscFunctionBegin;
978607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
979b412c318SBarry Smith   mtype = dm->mattype;
980552f7358SJed Brown   ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
981552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
982552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
98382f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
984552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
985552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
986552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
987acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
988acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
989552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
990552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
991552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
992552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
993552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
994552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
995552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
996837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
997552f7358SJed Brown   if (!isShell) {
998be36d101SStefano Zampini     PetscSection subSection;
999837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
1000be36d101SStefano Zampini     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal, bsMax, bsMin, *ltogidx, lsize;
1001fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1002552f7358SJed Brown 
100300140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1004be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1005be36d101SStefano Zampini       PetscSection section;
1006be36d101SStefano Zampini       PetscInt     size;
1007be36d101SStefano Zampini 
1008be36d101SStefano Zampini       ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1009be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1010be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1011be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1012be36d101SStefano Zampini     } else {
101300140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1014be36d101SStefano Zampini     }
1015552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1016be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1017a9d99c84SMatthew G. Knepley       PetscInt bdof;
1018a9d99c84SMatthew G. Knepley 
1019552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1020fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
10211d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
10221d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
10231d17a0a3SMatthew G. Knepley       if (dof) {
10241d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1025be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1026be36d101SStefano Zampini       }
1027be36d101SStefano Zampini       if (isMatIS) {
1028be36d101SStefano Zampini         PetscInt loff,c,off;
1029be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1030be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1031be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1032552f7358SJed Brown       }
10332a28c762SMatthew G Knepley     }
10342a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
10352a28c762SMatthew G Knepley     bsLocal = bs;
1036b2566f29SBarry Smith     ierr = MPIU_Allreduce(&bsLocal, &bsMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
10372a28c762SMatthew G Knepley     bsLocal = bs < 0 ? bsMax : bs;
1038b2566f29SBarry Smith     ierr = MPIU_Allreduce(&bsLocal, &bsMin, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1039bff27382SMatthew G. Knepley     if (bsMin != bsMax) {bs = 1;}
1040bff27382SMatthew G. Knepley     else                {bs = bsMax;}
10414fa26246SMatthew G. Knepley     bs   = bs < 0 ? 1 : bs;
1042be36d101SStefano Zampini     if (isMatIS) {
10434fa26246SMatthew G. Knepley       PetscInt l;
10444fa26246SMatthew G. Knepley       /* Must reduce indices by blocksize */
10454fa26246SMatthew G. Knepley       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] /= bs;
10464fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1047be36d101SStefano Zampini     }
1048be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1049be36d101SStefano Zampini     if (isMatIS) {
1050be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1051be36d101SStefano Zampini     }
10521795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
10538d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1054552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1055552f7358SJed Brown   }
1056b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1057552f7358SJed Brown   PetscFunctionReturn(0);
1058552f7358SJed Brown }
1059552f7358SJed Brown 
1060559da874SStefano Zampini /*
1061be36d101SStefano Zampini   DMPlexGetSubdomainGlobalSection - Returns the section associated with the subdomain
1062be36d101SStefano Zampini 
1063be36d101SStefano Zampini   Not collective
1064be36d101SStefano Zampini 
1065be36d101SStefano Zampini   Input Parameter:
1066be36d101SStefano Zampini . mesh - The DMPlex
1067be36d101SStefano Zampini 
1068be36d101SStefano Zampini   Output Parameters:
1069be36d101SStefano Zampini . subsection - The subdomain section
1070be36d101SStefano Zampini 
1071be36d101SStefano Zampini   Level: developer
1072be36d101SStefano Zampini 
1073be36d101SStefano Zampini .seealso:
1074559da874SStefano Zampini */
1075be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1076be36d101SStefano Zampini {
1077be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1078be36d101SStefano Zampini   PetscErrorCode ierr;
1079be36d101SStefano Zampini 
1080be36d101SStefano Zampini   PetscFunctionBegin;
1081be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1082be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1083be36d101SStefano Zampini     PetscSection section;
1084be36d101SStefano Zampini     PetscSF      sf;
1085be36d101SStefano Zampini 
1086be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
1087be36d101SStefano Zampini     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
1088be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1089be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1090be36d101SStefano Zampini   }
1091be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1092be36d101SStefano Zampini   PetscFunctionReturn(0);
1093be36d101SStefano Zampini }
1094be36d101SStefano Zampini 
1095552f7358SJed Brown /*@
1096552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1097552f7358SJed Brown 
1098552f7358SJed Brown   Not collective
1099552f7358SJed Brown 
1100552f7358SJed Brown   Input Parameter:
1101552f7358SJed Brown . mesh - The DMPlex
1102552f7358SJed Brown 
1103552f7358SJed Brown   Output Parameters:
1104552f7358SJed Brown + pStart - The first mesh point
1105552f7358SJed Brown - pEnd   - The upper bound for mesh points
1106552f7358SJed Brown 
1107552f7358SJed Brown   Level: beginner
1108552f7358SJed Brown 
1109552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1110552f7358SJed Brown @*/
1111552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1112552f7358SJed Brown {
1113552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1114552f7358SJed Brown   PetscErrorCode ierr;
1115552f7358SJed Brown 
1116552f7358SJed Brown   PetscFunctionBegin;
1117552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1118552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1119552f7358SJed Brown   PetscFunctionReturn(0);
1120552f7358SJed Brown }
1121552f7358SJed Brown 
1122552f7358SJed Brown /*@
1123552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1124552f7358SJed Brown 
1125552f7358SJed Brown   Not collective
1126552f7358SJed Brown 
1127552f7358SJed Brown   Input Parameters:
1128552f7358SJed Brown + mesh - The DMPlex
1129552f7358SJed Brown . pStart - The first mesh point
1130552f7358SJed Brown - pEnd   - The upper bound for mesh points
1131552f7358SJed Brown 
1132552f7358SJed Brown   Output Parameters:
1133552f7358SJed Brown 
1134552f7358SJed Brown   Level: beginner
1135552f7358SJed Brown 
1136552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1137552f7358SJed Brown @*/
1138552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1139552f7358SJed Brown {
1140552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1141552f7358SJed Brown   PetscErrorCode ierr;
1142552f7358SJed Brown 
1143552f7358SJed Brown   PetscFunctionBegin;
1144552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1145552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1146552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1147552f7358SJed Brown   PetscFunctionReturn(0);
1148552f7358SJed Brown }
1149552f7358SJed Brown 
1150552f7358SJed Brown /*@
1151552f7358SJed Brown   DMPlexGetConeSize - Return the number of in-edges for this point in the Sieve DAG
1152552f7358SJed Brown 
1153552f7358SJed Brown   Not collective
1154552f7358SJed Brown 
1155552f7358SJed Brown   Input Parameters:
1156552f7358SJed Brown + mesh - The DMPlex
1157552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1158552f7358SJed Brown 
1159552f7358SJed Brown   Output Parameter:
1160552f7358SJed Brown . size - The cone size for point p
1161552f7358SJed Brown 
1162552f7358SJed Brown   Level: beginner
1163552f7358SJed Brown 
1164552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1165552f7358SJed Brown @*/
1166552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1167552f7358SJed Brown {
1168552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1169552f7358SJed Brown   PetscErrorCode ierr;
1170552f7358SJed Brown 
1171552f7358SJed Brown   PetscFunctionBegin;
1172552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1173552f7358SJed Brown   PetscValidPointer(size, 3);
1174552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1175552f7358SJed Brown   PetscFunctionReturn(0);
1176552f7358SJed Brown }
1177552f7358SJed Brown 
1178552f7358SJed Brown /*@
1179552f7358SJed Brown   DMPlexSetConeSize - Set the number of in-edges for this point in the Sieve DAG
1180552f7358SJed Brown 
1181552f7358SJed Brown   Not collective
1182552f7358SJed Brown 
1183552f7358SJed Brown   Input Parameters:
1184552f7358SJed Brown + mesh - The DMPlex
1185552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1186552f7358SJed Brown - size - The cone size for point p
1187552f7358SJed Brown 
1188552f7358SJed Brown   Output Parameter:
1189552f7358SJed Brown 
1190552f7358SJed Brown   Note:
1191552f7358SJed Brown   This should be called after DMPlexSetChart().
1192552f7358SJed Brown 
1193552f7358SJed Brown   Level: beginner
1194552f7358SJed Brown 
1195552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1196552f7358SJed Brown @*/
1197552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1198552f7358SJed Brown {
1199552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1200552f7358SJed Brown   PetscErrorCode ierr;
1201552f7358SJed Brown 
1202552f7358SJed Brown   PetscFunctionBegin;
1203552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1204552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
12050d644c17SKarl Rupp 
1206552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1207552f7358SJed Brown   PetscFunctionReturn(0);
1208552f7358SJed Brown }
1209552f7358SJed Brown 
1210f5a469b9SMatthew G. Knepley /*@
1211f5a469b9SMatthew G. Knepley   DMPlexAddConeSize - Add the given number of in-edges to this point in the Sieve DAG
1212f5a469b9SMatthew G. Knepley 
1213f5a469b9SMatthew G. Knepley   Not collective
1214f5a469b9SMatthew G. Knepley 
1215f5a469b9SMatthew G. Knepley   Input Parameters:
1216f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1217f5a469b9SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1218f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1219f5a469b9SMatthew G. Knepley 
1220f5a469b9SMatthew G. Knepley   Output Parameter:
1221f5a469b9SMatthew G. Knepley 
1222f5a469b9SMatthew G. Knepley   Note:
1223f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1224f5a469b9SMatthew G. Knepley 
1225f5a469b9SMatthew G. Knepley   Level: beginner
1226f5a469b9SMatthew G. Knepley 
1227f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1228f5a469b9SMatthew G. Knepley @*/
1229f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1230f5a469b9SMatthew G. Knepley {
1231f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1232f5a469b9SMatthew G. Knepley   PetscInt       csize;
1233f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1234f5a469b9SMatthew G. Knepley 
1235f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1236f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1237f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1238f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1239f5a469b9SMatthew G. Knepley 
1240f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1241f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1242f5a469b9SMatthew G. Knepley }
1243f5a469b9SMatthew G. Knepley 
1244552f7358SJed Brown /*@C
1245552f7358SJed Brown   DMPlexGetCone - Return the points on the in-edges for this point in the Sieve DAG
1246552f7358SJed Brown 
1247552f7358SJed Brown   Not collective
1248552f7358SJed Brown 
1249552f7358SJed Brown   Input Parameters:
1250552f7358SJed Brown + mesh - The DMPlex
1251552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1252552f7358SJed Brown 
1253552f7358SJed Brown   Output Parameter:
1254552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1255552f7358SJed Brown 
1256552f7358SJed Brown   Level: beginner
1257552f7358SJed Brown 
12583813dfbdSMatthew G Knepley   Fortran Notes:
12593813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
12603813dfbdSMatthew G Knepley   include petsc.h90 in your code.
12613813dfbdSMatthew G Knepley 
12623813dfbdSMatthew G Knepley   You must also call DMPlexRestoreCone() after you finish using the returned array.
1263552f7358SJed Brown 
1264552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart()
1265552f7358SJed Brown @*/
1266552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1267552f7358SJed Brown {
1268552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1269552f7358SJed Brown   PetscInt       off;
1270552f7358SJed Brown   PetscErrorCode ierr;
1271552f7358SJed Brown 
1272552f7358SJed Brown   PetscFunctionBegin;
1273552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1274552f7358SJed Brown   PetscValidPointer(cone, 3);
1275552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1276552f7358SJed Brown   *cone = &mesh->cones[off];
1277552f7358SJed Brown   PetscFunctionReturn(0);
1278552f7358SJed Brown }
1279552f7358SJed Brown 
1280552f7358SJed Brown /*@
1281552f7358SJed Brown   DMPlexSetCone - Set the points on the in-edges for this point in the Sieve DAG
1282552f7358SJed Brown 
1283552f7358SJed Brown   Not collective
1284552f7358SJed Brown 
1285552f7358SJed Brown   Input Parameters:
1286552f7358SJed Brown + mesh - The DMPlex
1287552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1288552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1289552f7358SJed Brown 
1290552f7358SJed Brown   Output Parameter:
1291552f7358SJed Brown 
1292552f7358SJed Brown   Note:
1293552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1294552f7358SJed Brown 
1295552f7358SJed Brown   Level: beginner
1296552f7358SJed Brown 
1297552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1298552f7358SJed Brown @*/
1299552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1300552f7358SJed Brown {
1301552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1302552f7358SJed Brown   PetscInt       pStart, pEnd;
1303552f7358SJed Brown   PetscInt       dof, off, c;
1304552f7358SJed Brown   PetscErrorCode ierr;
1305552f7358SJed Brown 
1306552f7358SJed Brown   PetscFunctionBegin;
1307552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1308552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1309552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1310552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1311552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
131282f516ccSBarry 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);
1313552f7358SJed Brown   for (c = 0; c < dof; ++c) {
131482f516ccSBarry 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);
1315552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1316552f7358SJed Brown   }
1317552f7358SJed Brown   PetscFunctionReturn(0);
1318552f7358SJed Brown }
1319552f7358SJed Brown 
1320552f7358SJed Brown /*@C
1321552f7358SJed Brown   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the Sieve DAG
1322552f7358SJed Brown 
1323552f7358SJed Brown   Not collective
1324552f7358SJed Brown 
1325552f7358SJed Brown   Input Parameters:
1326552f7358SJed Brown + mesh - The DMPlex
1327552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1328552f7358SJed Brown 
1329552f7358SJed Brown   Output Parameter:
1330552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1331552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1332552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1333552f7358SJed Brown                     the index of the cone point on which to start.
1334552f7358SJed Brown 
1335552f7358SJed Brown   Level: beginner
1336552f7358SJed Brown 
13373813dfbdSMatthew G Knepley   Fortran Notes:
13383813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
13393813dfbdSMatthew G Knepley   include petsc.h90 in your code.
13403813dfbdSMatthew G Knepley 
13413813dfbdSMatthew G Knepley   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1342552f7358SJed Brown 
1343552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1344552f7358SJed Brown @*/
1345552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1346552f7358SJed Brown {
1347552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1348552f7358SJed Brown   PetscInt       off;
1349552f7358SJed Brown   PetscErrorCode ierr;
1350552f7358SJed Brown 
1351552f7358SJed Brown   PetscFunctionBegin;
1352552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1353552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1354552f7358SJed Brown   {
1355552f7358SJed Brown     PetscInt dof;
1356552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1357552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1358552f7358SJed Brown   }
1359552f7358SJed Brown #endif
1360552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
13610d644c17SKarl Rupp 
1362552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1363552f7358SJed Brown   PetscFunctionReturn(0);
1364552f7358SJed Brown }
1365552f7358SJed Brown 
1366552f7358SJed Brown /*@
1367552f7358SJed Brown   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the Sieve DAG
1368552f7358SJed Brown 
1369552f7358SJed Brown   Not collective
1370552f7358SJed Brown 
1371552f7358SJed Brown   Input Parameters:
1372552f7358SJed Brown + mesh - The DMPlex
1373552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1374552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1375552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1376552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1377552f7358SJed Brown                     the index of the cone point on which to start.
1378552f7358SJed Brown 
1379552f7358SJed Brown   Output Parameter:
1380552f7358SJed Brown 
1381552f7358SJed Brown   Note:
1382552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1383552f7358SJed Brown 
1384552f7358SJed Brown   Level: beginner
1385552f7358SJed Brown 
1386552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1387552f7358SJed Brown @*/
1388552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
1389552f7358SJed Brown {
1390552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1391552f7358SJed Brown   PetscInt       pStart, pEnd;
1392552f7358SJed Brown   PetscInt       dof, off, c;
1393552f7358SJed Brown   PetscErrorCode ierr;
1394552f7358SJed Brown 
1395552f7358SJed Brown   PetscFunctionBegin;
1396552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1397552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1398552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1399552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
1400552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
140182f516ccSBarry 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);
1402552f7358SJed Brown   for (c = 0; c < dof; ++c) {
1403552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
1404552f7358SJed Brown 
1405552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
140682f516ccSBarry 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);
1407552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
1408552f7358SJed Brown   }
1409552f7358SJed Brown   PetscFunctionReturn(0);
1410552f7358SJed Brown }
1411552f7358SJed Brown 
1412552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
1413552f7358SJed Brown {
1414552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1415552f7358SJed Brown   PetscInt       pStart, pEnd;
1416552f7358SJed Brown   PetscInt       dof, off;
1417552f7358SJed Brown   PetscErrorCode ierr;
1418552f7358SJed Brown 
1419552f7358SJed Brown   PetscFunctionBegin;
1420552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1421552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
142282f516ccSBarry 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);
142382f516ccSBarry 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);
142477c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
142577c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
142677c88f5bSMatthew 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);
1427552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
1428552f7358SJed Brown   PetscFunctionReturn(0);
1429552f7358SJed Brown }
1430552f7358SJed Brown 
143177c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
143277c88f5bSMatthew G Knepley {
143377c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
143477c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
143577c88f5bSMatthew G Knepley   PetscInt       dof, off;
143677c88f5bSMatthew G Knepley   PetscErrorCode ierr;
143777c88f5bSMatthew G Knepley 
143877c88f5bSMatthew G Knepley   PetscFunctionBegin;
143977c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
144077c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
144177c88f5bSMatthew 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);
144277c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
144377c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
144477c88f5bSMatthew 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);
144577c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
144677c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
144777c88f5bSMatthew G Knepley }
144877c88f5bSMatthew G Knepley 
1449552f7358SJed Brown /*@
1450552f7358SJed Brown   DMPlexGetSupportSize - Return the number of out-edges for this point in the Sieve DAG
1451552f7358SJed Brown 
1452552f7358SJed Brown   Not collective
1453552f7358SJed Brown 
1454552f7358SJed Brown   Input Parameters:
1455552f7358SJed Brown + mesh - The DMPlex
1456552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1457552f7358SJed Brown 
1458552f7358SJed Brown   Output Parameter:
1459552f7358SJed Brown . size - The support size for point p
1460552f7358SJed Brown 
1461552f7358SJed Brown   Level: beginner
1462552f7358SJed Brown 
1463552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
1464552f7358SJed Brown @*/
1465552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
1466552f7358SJed Brown {
1467552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1468552f7358SJed Brown   PetscErrorCode ierr;
1469552f7358SJed Brown 
1470552f7358SJed Brown   PetscFunctionBegin;
1471552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1472552f7358SJed Brown   PetscValidPointer(size, 3);
1473552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
1474552f7358SJed Brown   PetscFunctionReturn(0);
1475552f7358SJed Brown }
1476552f7358SJed Brown 
1477552f7358SJed Brown /*@
1478552f7358SJed Brown   DMPlexSetSupportSize - Set the number of out-edges for this point in the Sieve DAG
1479552f7358SJed Brown 
1480552f7358SJed Brown   Not collective
1481552f7358SJed Brown 
1482552f7358SJed Brown   Input Parameters:
1483552f7358SJed Brown + mesh - The DMPlex
1484552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1485552f7358SJed Brown - size - The support size for point p
1486552f7358SJed Brown 
1487552f7358SJed Brown   Output Parameter:
1488552f7358SJed Brown 
1489552f7358SJed Brown   Note:
1490552f7358SJed Brown   This should be called after DMPlexSetChart().
1491552f7358SJed Brown 
1492552f7358SJed Brown   Level: beginner
1493552f7358SJed Brown 
1494552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
1495552f7358SJed Brown @*/
1496552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
1497552f7358SJed Brown {
1498552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1499552f7358SJed Brown   PetscErrorCode ierr;
1500552f7358SJed Brown 
1501552f7358SJed Brown   PetscFunctionBegin;
1502552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1503552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
15040d644c17SKarl Rupp 
1505552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
1506552f7358SJed Brown   PetscFunctionReturn(0);
1507552f7358SJed Brown }
1508552f7358SJed Brown 
1509552f7358SJed Brown /*@C
1510552f7358SJed Brown   DMPlexGetSupport - Return the points on the out-edges for this point in the Sieve DAG
1511552f7358SJed Brown 
1512552f7358SJed Brown   Not collective
1513552f7358SJed Brown 
1514552f7358SJed Brown   Input Parameters:
1515552f7358SJed Brown + mesh - The DMPlex
1516552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1517552f7358SJed Brown 
1518552f7358SJed Brown   Output Parameter:
1519552f7358SJed Brown . support - An array of points which are on the out-edges for point p
1520552f7358SJed Brown 
1521552f7358SJed Brown   Level: beginner
1522552f7358SJed Brown 
15233813dfbdSMatthew G Knepley   Fortran Notes:
15243813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
15253813dfbdSMatthew G Knepley   include petsc.h90 in your code.
15263813dfbdSMatthew G Knepley 
15273813dfbdSMatthew G Knepley   You must also call DMPlexRestoreSupport() after you finish using the returned array.
15283813dfbdSMatthew G Knepley 
1529552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1530552f7358SJed Brown @*/
1531552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
1532552f7358SJed Brown {
1533552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1534552f7358SJed Brown   PetscInt       off;
1535552f7358SJed Brown   PetscErrorCode ierr;
1536552f7358SJed Brown 
1537552f7358SJed Brown   PetscFunctionBegin;
1538552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1539552f7358SJed Brown   PetscValidPointer(support, 3);
1540552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
1541552f7358SJed Brown   *support = &mesh->supports[off];
1542552f7358SJed Brown   PetscFunctionReturn(0);
1543552f7358SJed Brown }
1544552f7358SJed Brown 
1545552f7358SJed Brown /*@
1546552f7358SJed Brown   DMPlexSetSupport - Set the points on the out-edges for this point in the Sieve DAG
1547552f7358SJed Brown 
1548552f7358SJed Brown   Not collective
1549552f7358SJed Brown 
1550552f7358SJed Brown   Input Parameters:
1551552f7358SJed Brown + mesh - The DMPlex
1552552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1553552f7358SJed Brown - support - An array of points which are on the in-edges for point p
1554552f7358SJed Brown 
1555552f7358SJed Brown   Output Parameter:
1556552f7358SJed Brown 
1557552f7358SJed Brown   Note:
1558552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
1559552f7358SJed Brown 
1560552f7358SJed Brown   Level: beginner
1561552f7358SJed Brown 
1562552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
1563552f7358SJed Brown @*/
1564552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
1565552f7358SJed Brown {
1566552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1567552f7358SJed Brown   PetscInt       pStart, pEnd;
1568552f7358SJed Brown   PetscInt       dof, off, c;
1569552f7358SJed Brown   PetscErrorCode ierr;
1570552f7358SJed Brown 
1571552f7358SJed Brown   PetscFunctionBegin;
1572552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1573552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1574552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1575552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
1576552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
157782f516ccSBarry 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);
1578552f7358SJed Brown   for (c = 0; c < dof; ++c) {
157982f516ccSBarry 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);
1580552f7358SJed Brown     mesh->supports[off+c] = support[c];
1581552f7358SJed Brown   }
1582552f7358SJed Brown   PetscFunctionReturn(0);
1583552f7358SJed Brown }
1584552f7358SJed Brown 
1585552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
1586552f7358SJed Brown {
1587552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1588552f7358SJed Brown   PetscInt       pStart, pEnd;
1589552f7358SJed Brown   PetscInt       dof, off;
1590552f7358SJed Brown   PetscErrorCode ierr;
1591552f7358SJed Brown 
1592552f7358SJed Brown   PetscFunctionBegin;
1593552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1594552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1595552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1596552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
159782f516ccSBarry 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);
159882f516ccSBarry 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);
159982f516ccSBarry 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);
1600552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
1601552f7358SJed Brown   PetscFunctionReturn(0);
1602552f7358SJed Brown }
1603552f7358SJed Brown 
1604552f7358SJed Brown /*@C
1605552f7358SJed Brown   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the Sieve DAG
1606552f7358SJed Brown 
1607552f7358SJed Brown   Not collective
1608552f7358SJed Brown 
1609552f7358SJed Brown   Input Parameters:
1610552f7358SJed Brown + mesh - The DMPlex
1611552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1612552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
16130298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
1614552f7358SJed Brown 
1615552f7358SJed Brown   Output Parameters:
1616552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
1617552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
1618552f7358SJed Brown 
1619552f7358SJed Brown   Note:
16200298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
1621552f7358SJed Brown 
16223813dfbdSMatthew G Knepley   Fortran Notes:
16233813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16243813dfbdSMatthew G Knepley   include petsc.h90 in your code.
16253813dfbdSMatthew G Knepley 
16263813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
16273813dfbdSMatthew G Knepley 
1628552f7358SJed Brown   Level: beginner
1629552f7358SJed Brown 
1630552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1631552f7358SJed Brown @*/
1632552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1633552f7358SJed Brown {
1634552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
1635552f7358SJed Brown   PetscInt       *closure, *fifo;
16360298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
1637552f7358SJed Brown   PetscInt        tmpSize, t;
1638552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
1639552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
1640552f7358SJed Brown   PetscErrorCode  ierr;
1641552f7358SJed Brown 
1642552f7358SJed Brown   PetscFunctionBegin;
1643552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1644552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1645552f7358SJed Brown   /* This is only 1-level */
1646552f7358SJed Brown   if (useCone) {
1647552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
1648552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
1649552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
1650552f7358SJed Brown   } else {
1651552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
1652552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
1653552f7358SJed Brown   }
1654bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
1655bfbcdd7aSMatthew G. Knepley     if (*points) {
1656bfbcdd7aSMatthew G. Knepley       closure = *points;
1657bfbcdd7aSMatthew G. Knepley     } else {
1658bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
1659bfbcdd7aSMatthew G. Knepley       ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1660bfbcdd7aSMatthew G. Knepley     }
1661bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
1662bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
1663bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
1664bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
1665bfbcdd7aSMatthew G. Knepley     }
1666bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
1667bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
1668bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
1669bfbcdd7aSMatthew G. Knepley   }
167024c766afSToby Isaac   {
167124c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
167224c766afSToby Isaac 
167324c766afSToby Isaac     c = mesh->maxConeSize;
167424c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
167524c766afSToby Isaac     s = mesh->maxSupportSize;
167624c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
167724c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
167824c766afSToby Isaac   }
1679bfbcdd7aSMatthew G. Knepley   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1680bfbcdd7aSMatthew G. Knepley   if (*points) {
1681bfbcdd7aSMatthew G. Knepley     closure = *points;
1682bfbcdd7aSMatthew G. Knepley   } else {
1683bfbcdd7aSMatthew G. Knepley     ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1684bfbcdd7aSMatthew G. Knepley   }
1685bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
1686552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
1687552f7358SJed Brown     const PetscInt cp = tmp[t];
1688552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
1689552f7358SJed Brown 
1690552f7358SJed Brown     closure[closureSize]   = cp;
1691552f7358SJed Brown     closure[closureSize+1] = co;
1692552f7358SJed Brown     fifo[fifoSize]         = cp;
1693552f7358SJed Brown     fifo[fifoSize+1]       = co;
1694552f7358SJed Brown   }
1695bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
1696552f7358SJed Brown   while (fifoSize - fifoStart) {
1697552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
1698552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
1699552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
1700552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
1701552f7358SJed Brown 
1702552f7358SJed Brown     if (useCone) {
1703552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
1704552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
1705552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
1706552f7358SJed Brown     } else {
1707552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
1708552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
17090298fd71SBarry Smith       tmpO = NULL;
1710552f7358SJed Brown     }
1711552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
1712552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
1713552f7358SJed Brown       const PetscInt cp = tmp[i];
17142e1b13c2SMatthew 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. */
17152e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
17162e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
17172e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
1718552f7358SJed Brown       PetscInt       c;
1719552f7358SJed Brown 
17202e1b13c2SMatthew G. Knepley       if (rev) {
17212e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
17222e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
17232e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
17242e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
17252e1b13c2SMatthew G. Knepley       }
1726552f7358SJed Brown       /* Check for duplicate */
1727552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
1728552f7358SJed Brown         if (closure[c] == cp) break;
1729552f7358SJed Brown       }
1730552f7358SJed Brown       if (c == closureSize) {
1731552f7358SJed Brown         closure[closureSize]   = cp;
1732552f7358SJed Brown         closure[closureSize+1] = co;
1733552f7358SJed Brown         fifo[fifoSize]         = cp;
1734552f7358SJed Brown         fifo[fifoSize+1]       = co;
1735552f7358SJed Brown         closureSize           += 2;
1736552f7358SJed Brown         fifoSize              += 2;
1737552f7358SJed Brown       }
1738552f7358SJed Brown     }
1739552f7358SJed Brown     fifoStart += 2;
1740552f7358SJed Brown   }
1741552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
1742552f7358SJed Brown   if (points)    *points    = closure;
1743552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1744552f7358SJed Brown   PetscFunctionReturn(0);
1745552f7358SJed Brown }
1746552f7358SJed Brown 
17479bf0dad6SMatthew G. Knepley /*@C
17489bf0dad6SMatthew 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
17499bf0dad6SMatthew G. Knepley 
17509bf0dad6SMatthew G. Knepley   Not collective
17519bf0dad6SMatthew G. Knepley 
17529bf0dad6SMatthew G. Knepley   Input Parameters:
17539bf0dad6SMatthew G. Knepley + mesh - The DMPlex
17549bf0dad6SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
17559bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
17569bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
17579bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
17589bf0dad6SMatthew G. Knepley 
17599bf0dad6SMatthew G. Knepley   Output Parameters:
17609bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
17619bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
17629bf0dad6SMatthew G. Knepley 
17639bf0dad6SMatthew G. Knepley   Note:
17649bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
17659bf0dad6SMatthew G. Knepley 
17669bf0dad6SMatthew G. Knepley   Fortran Notes:
17679bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
17689bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
17699bf0dad6SMatthew G. Knepley 
17709bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
17719bf0dad6SMatthew G. Knepley 
17729bf0dad6SMatthew G. Knepley   Level: beginner
17739bf0dad6SMatthew G. Knepley 
17749bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
17759bf0dad6SMatthew G. Knepley @*/
17769bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
17779bf0dad6SMatthew G. Knepley {
17789bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
17799bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
17809bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
17819bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
17829bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
17839bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
17849bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
17859bf0dad6SMatthew G. Knepley 
17869bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
17879bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17889bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
17899bf0dad6SMatthew G. Knepley   /* This is only 1-level */
17909bf0dad6SMatthew G. Knepley   if (useCone) {
17919bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
17929bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
17939bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
17949bf0dad6SMatthew G. Knepley   } else {
17959bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
17969bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
17979bf0dad6SMatthew G. Knepley   }
17989bf0dad6SMatthew G. Knepley   if (depth == 1) {
17999bf0dad6SMatthew G. Knepley     if (*points) {
18009bf0dad6SMatthew G. Knepley       closure = *points;
18019bf0dad6SMatthew G. Knepley     } else {
18029bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
18039bf0dad6SMatthew G. Knepley       ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
18049bf0dad6SMatthew G. Knepley     }
18059bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
18069bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
18079bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
18089bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
18099bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
18109bf0dad6SMatthew G. Knepley     }
18119bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
18129bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
18139bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
18149bf0dad6SMatthew G. Knepley   }
181524c766afSToby Isaac   {
181624c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
181724c766afSToby Isaac 
181824c766afSToby Isaac     c = mesh->maxConeSize;
181924c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
182024c766afSToby Isaac     s = mesh->maxSupportSize;
182124c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
182224c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
182324c766afSToby Isaac   }
18249bf0dad6SMatthew G. Knepley   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
18259bf0dad6SMatthew G. Knepley   if (*points) {
18269bf0dad6SMatthew G. Knepley     closure = *points;
18279bf0dad6SMatthew G. Knepley   } else {
18289bf0dad6SMatthew G. Knepley     ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
18299bf0dad6SMatthew G. Knepley   }
18309bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
18319bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
18329bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
18339bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
18349bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
18359bf0dad6SMatthew G. Knepley 
18369bf0dad6SMatthew G. Knepley     if (ornt < 0) {
18379bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
18389bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
183986b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
18409bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
18419bf0dad6SMatthew G. Knepley     }
18429bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
18439bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
18449bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
18459bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
18469bf0dad6SMatthew G. Knepley   }
18479bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
18489bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
18499bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
18509bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
18519bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
18529bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
18539bf0dad6SMatthew G. Knepley 
18549bf0dad6SMatthew G. Knepley     if (useCone) {
18559bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
18569bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
18579bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
18589bf0dad6SMatthew G. Knepley     } else {
18599bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
18609bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
18619bf0dad6SMatthew G. Knepley       tmpO = NULL;
18629bf0dad6SMatthew G. Knepley     }
18639bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
18649bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
18659bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
18669bf0dad6SMatthew 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. */
18679bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
18689bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
18699bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
18709bf0dad6SMatthew G. Knepley       PetscInt       c;
18719bf0dad6SMatthew G. Knepley 
18729bf0dad6SMatthew G. Knepley       if (rev) {
18739bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
18749bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
18759bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
18769bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
18779bf0dad6SMatthew G. Knepley       }
18789bf0dad6SMatthew G. Knepley       /* Check for duplicate */
18799bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
18809bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
18819bf0dad6SMatthew G. Knepley       }
18829bf0dad6SMatthew G. Knepley       if (c == closureSize) {
18839bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
18849bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
18859bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
18869bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
18879bf0dad6SMatthew G. Knepley         closureSize           += 2;
18889bf0dad6SMatthew G. Knepley         fifoSize              += 2;
18899bf0dad6SMatthew G. Knepley       }
18909bf0dad6SMatthew G. Knepley     }
18919bf0dad6SMatthew G. Knepley     fifoStart += 2;
18929bf0dad6SMatthew G. Knepley   }
18939bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
18949bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
18959bf0dad6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
18969bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
18979bf0dad6SMatthew G. Knepley }
18989bf0dad6SMatthew G. Knepley 
1899552f7358SJed Brown /*@C
1900552f7358SJed 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
1901552f7358SJed Brown 
1902552f7358SJed Brown   Not collective
1903552f7358SJed Brown 
1904552f7358SJed Brown   Input Parameters:
1905552f7358SJed Brown + mesh - The DMPlex
1906552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1907552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
1908e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
1909e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
1910552f7358SJed Brown 
1911552f7358SJed Brown   Note:
19120298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
1913552f7358SJed Brown 
19143813dfbdSMatthew G Knepley   Fortran Notes:
19153813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
19163813dfbdSMatthew G Knepley   include petsc.h90 in your code.
19173813dfbdSMatthew G Knepley 
19183813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
19193813dfbdSMatthew G Knepley 
1920552f7358SJed Brown   Level: beginner
1921552f7358SJed Brown 
1922552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1923552f7358SJed Brown @*/
1924552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1925552f7358SJed Brown {
1926552f7358SJed Brown   PetscErrorCode ierr;
1927552f7358SJed Brown 
1928552f7358SJed Brown   PetscFunctionBegin;
1929552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1930e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
1931e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
1932552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, points);CHKERRQ(ierr);
19334ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
1934552f7358SJed Brown   PetscFunctionReturn(0);
1935552f7358SJed Brown }
1936552f7358SJed Brown 
1937552f7358SJed Brown /*@
1938552f7358SJed Brown   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the Sieve DAG
1939552f7358SJed Brown 
1940552f7358SJed Brown   Not collective
1941552f7358SJed Brown 
1942552f7358SJed Brown   Input Parameter:
1943552f7358SJed Brown . mesh - The DMPlex
1944552f7358SJed Brown 
1945552f7358SJed Brown   Output Parameters:
1946552f7358SJed Brown + maxConeSize - The maximum number of in-edges
1947552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
1948552f7358SJed Brown 
1949552f7358SJed Brown   Level: beginner
1950552f7358SJed Brown 
1951552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1952552f7358SJed Brown @*/
1953552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
1954552f7358SJed Brown {
1955552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
1956552f7358SJed Brown 
1957552f7358SJed Brown   PetscFunctionBegin;
1958552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1959552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
1960552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
1961552f7358SJed Brown   PetscFunctionReturn(0);
1962552f7358SJed Brown }
1963552f7358SJed Brown 
1964552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
1965552f7358SJed Brown {
1966552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1967552f7358SJed Brown   PetscInt       size;
1968552f7358SJed Brown   PetscErrorCode ierr;
1969552f7358SJed Brown 
1970552f7358SJed Brown   PetscFunctionBegin;
1971552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1972552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
1973552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
19741795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
19751795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
1976552f7358SJed Brown   if (mesh->maxSupportSize) {
1977552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
1978552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
19791795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
1980552f7358SJed Brown   }
1981552f7358SJed Brown   PetscFunctionReturn(0);
1982552f7358SJed Brown }
1983552f7358SJed Brown 
1984552f7358SJed Brown PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1985552f7358SJed Brown {
1986552f7358SJed Brown   PetscErrorCode ierr;
1987552f7358SJed Brown 
1988552f7358SJed Brown   PetscFunctionBegin;
19894d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
19904d9407bcSMatthew G. Knepley   ierr = DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1991552f7358SJed Brown   PetscFunctionReturn(0);
1992552f7358SJed Brown }
1993552f7358SJed Brown 
1994552f7358SJed Brown /*@
1995552f7358SJed Brown   DMPlexSymmetrize - Creates support (out-edge) information from cone (in-edge) inoformation
1996552f7358SJed Brown 
1997552f7358SJed Brown   Not collective
1998552f7358SJed Brown 
1999552f7358SJed Brown   Input Parameter:
2000552f7358SJed Brown . mesh - The DMPlex
2001552f7358SJed Brown 
2002552f7358SJed Brown   Output Parameter:
2003552f7358SJed Brown 
2004552f7358SJed Brown   Note:
2005552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2006552f7358SJed Brown 
2007552f7358SJed Brown   Level: beginner
2008552f7358SJed Brown 
2009552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2010552f7358SJed Brown @*/
2011552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2012552f7358SJed Brown {
2013552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2014552f7358SJed Brown   PetscInt      *offsets;
2015552f7358SJed Brown   PetscInt       supportSize;
2016552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2017552f7358SJed Brown   PetscErrorCode ierr;
2018552f7358SJed Brown 
2019552f7358SJed Brown   PetscFunctionBegin;
2020552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
202182f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
2022552f7358SJed Brown   /* Calculate support sizes */
2023552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2024552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2025552f7358SJed Brown     PetscInt dof, off, c;
2026552f7358SJed Brown 
2027552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2028552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2029552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2030552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2031552f7358SJed Brown     }
2032552f7358SJed Brown   }
2033552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2034552f7358SJed Brown     PetscInt dof;
2035552f7358SJed Brown 
2036552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
20370d644c17SKarl Rupp 
2038552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2039552f7358SJed Brown   }
2040552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2041552f7358SJed Brown   /* Calculate supports */
2042552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
20431795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
20441795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2045552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2046552f7358SJed Brown     PetscInt dof, off, c;
2047552f7358SJed Brown 
2048552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2049552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2050552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2051552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2052552f7358SJed Brown       PetscInt       offS;
2053552f7358SJed Brown 
2054552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
20550d644c17SKarl Rupp 
2056552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2057552f7358SJed Brown       ++offsets[q];
2058552f7358SJed Brown     }
2059552f7358SJed Brown   }
2060552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
2061552f7358SJed Brown   PetscFunctionReturn(0);
2062552f7358SJed Brown }
2063552f7358SJed Brown 
2064552f7358SJed Brown /*@
2065552f7358SJed Brown   DMPlexStratify - The Sieve DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
2066552f7358SJed Brown   can be illustrated by Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2067552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2068552f7358SJed Brown   the DAG.
2069552f7358SJed Brown 
2070bf4602e4SToby Isaac   Collective on dm
2071552f7358SJed Brown 
2072552f7358SJed Brown   Input Parameter:
2073552f7358SJed Brown . mesh - The DMPlex
2074552f7358SJed Brown 
2075552f7358SJed Brown   Output Parameter:
2076552f7358SJed Brown 
2077552f7358SJed Brown   Notes:
2078150b719bSJed Brown   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
2079150b719bSJed Brown   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2080c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2081150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2082552f7358SJed Brown 
2083150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2084552f7358SJed Brown 
2085552f7358SJed Brown   Level: beginner
2086552f7358SJed Brown 
2087552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
2088552f7358SJed Brown @*/
2089552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2090552f7358SJed Brown {
2091df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2092aa50250dSMatthew G. Knepley   DMLabel        label;
2093552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2094552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
2095552f7358SJed Brown   PetscErrorCode ierr;
2096552f7358SJed Brown 
2097552f7358SJed Brown   PetscFunctionBegin;
2098552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2099552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2100552f7358SJed Brown   /* Calculate depth */
2101aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2102c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2103aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2104552f7358SJed Brown   /* Initialize roots and count leaves */
2105552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2106552f7358SJed Brown     PetscInt coneSize, supportSize;
2107552f7358SJed Brown 
2108552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2109552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2110552f7358SJed Brown     if (!coneSize && supportSize) {
2111552f7358SJed Brown       ++numRoots;
2112aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2113552f7358SJed Brown     } else if (!supportSize && coneSize) {
2114552f7358SJed Brown       ++numLeaves;
2115552f7358SJed Brown     } else if (!supportSize && !coneSize) {
2116552f7358SJed Brown       /* Isolated points */
2117aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2118552f7358SJed Brown     }
2119552f7358SJed Brown   }
2120552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2121552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
2122552f7358SJed Brown       PetscInt coneSize, supportSize;
2123552f7358SJed Brown 
2124552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2125552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2126552f7358SJed Brown       if (!supportSize && coneSize) {
2127aa50250dSMatthew G. Knepley         ierr = DMLabelSetValue(label, p, 1);CHKERRQ(ierr);
2128552f7358SJed Brown       }
2129552f7358SJed Brown     }
2130552f7358SJed Brown   } else {
213174ef644bSMatthew G. Knepley     IS       pointIS;
213274ef644bSMatthew G. Knepley     PetscInt numPoints = 0, level = 0;
2133552f7358SJed Brown 
213474ef644bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
213574ef644bSMatthew G. Knepley     if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
213674ef644bSMatthew G. Knepley     while (numPoints) {
213774ef644bSMatthew G. Knepley       const PetscInt *points;
213874ef644bSMatthew G. Knepley       const PetscInt  newLevel = level+1;
213974ef644bSMatthew G. Knepley 
214074ef644bSMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
214174ef644bSMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
214274ef644bSMatthew G. Knepley         const PetscInt  point = points[p];
214374ef644bSMatthew G. Knepley         const PetscInt *support;
214474ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
214574ef644bSMatthew G. Knepley 
214674ef644bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
214774ef644bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
214874ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
214974ef644bSMatthew G. Knepley           ierr = DMLabelSetValue(label, support[s], newLevel);CHKERRQ(ierr);
2150552f7358SJed Brown         }
2151552f7358SJed Brown       }
21525edfc765SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
215374ef644bSMatthew G. Knepley       ++level;
215474ef644bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
215574ef644bSMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
215674ef644bSMatthew G. Knepley       if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
215774ef644bSMatthew G. Knepley       else         {numPoints = 0;}
215874ef644bSMatthew G. Knepley     }
215974ef644bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
216074ef644bSMatthew G. Knepley   }
2161bf4602e4SToby Isaac   { /* just in case there is an empty process */
2162bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2163bf4602e4SToby Isaac 
2164bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label,&numValues);CHKERRQ(ierr);
21654de306b1SToby Isaac     for (v = 0; v < numValues; v++) {
21664de306b1SToby Isaac       IS pointIS;
21674de306b1SToby Isaac 
21684de306b1SToby Isaac       ierr = DMLabelGetStratumIS(label, v, &pointIS);CHKERRQ(ierr);
21694de306b1SToby Isaac       if (pointIS) {
21704de306b1SToby Isaac         PetscInt  min, max, numPoints;
21714de306b1SToby Isaac         PetscInt  start;
21724de306b1SToby Isaac         PetscBool contig;
21734de306b1SToby Isaac 
21744de306b1SToby Isaac         ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
21754de306b1SToby Isaac         ierr = ISGetMinMax(pointIS, &min, &max);CHKERRQ(ierr);
21764de306b1SToby Isaac         ierr = ISContiguousLocal(pointIS,min,max+1,&start,&contig);CHKERRQ(ierr);
21774de306b1SToby Isaac         if (start == 0 && contig) {
21784de306b1SToby Isaac           ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
21794de306b1SToby Isaac           ierr = ISCreateStride(PETSC_COMM_SELF,numPoints,min,1,&pointIS);CHKERRQ(ierr);
21804de306b1SToby Isaac           ierr = DMLabelSetStratumIS(label, v, pointIS);CHKERRQ(ierr);
21814de306b1SToby Isaac         }
21824de306b1SToby Isaac       }
21834de306b1SToby Isaac       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
21844de306b1SToby Isaac     }
21856d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2186bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2187bf4602e4SToby Isaac       DMLabelAddStratum(label,v);CHKERRQ(ierr);
2188bf4602e4SToby Isaac     }
2189bf4602e4SToby Isaac   }
2190bf4602e4SToby Isaac 
2191df0420ecSMatthew G. Knepley   ierr = DMLabelGetState(label, &mesh->depthState);CHKERRQ(ierr);
2192552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2193552f7358SJed Brown   PetscFunctionReturn(0);
2194552f7358SJed Brown }
2195552f7358SJed Brown 
2196552f7358SJed Brown /*@C
2197552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
2198552f7358SJed Brown 
2199552f7358SJed Brown   Not Collective
2200552f7358SJed Brown 
2201552f7358SJed Brown   Input Parameters:
2202552f7358SJed Brown + dm - The DMPlex object
2203552f7358SJed Brown . numPoints - The number of input points for the join
2204552f7358SJed Brown - points - The input points
2205552f7358SJed Brown 
2206552f7358SJed Brown   Output Parameters:
2207552f7358SJed Brown + numCoveredPoints - The number of points in the join
2208552f7358SJed Brown - coveredPoints - The points in the join
2209552f7358SJed Brown 
2210552f7358SJed Brown   Level: intermediate
2211552f7358SJed Brown 
2212552f7358SJed Brown   Note: Currently, this is restricted to a single level join
2213552f7358SJed Brown 
22143813dfbdSMatthew G Knepley   Fortran Notes:
22153813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22163813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22173813dfbdSMatthew G Knepley 
22183813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
22193813dfbdSMatthew G Knepley 
2220552f7358SJed Brown .keywords: mesh
2221552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2222552f7358SJed Brown @*/
2223552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2224552f7358SJed Brown {
2225552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2226552f7358SJed Brown   PetscInt      *join[2];
2227552f7358SJed Brown   PetscInt       joinSize, i = 0;
2228552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2229552f7358SJed Brown   PetscErrorCode ierr;
2230552f7358SJed Brown 
2231552f7358SJed Brown   PetscFunctionBegin;
2232552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2233552f7358SJed Brown   PetscValidPointer(points, 2);
2234552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2235552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2236552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
2237552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
2238552f7358SJed Brown   /* Copy in support of first point */
2239552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
2240552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
2241552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
2242552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
2243552f7358SJed Brown   }
2244552f7358SJed Brown   /* Check each successive support */
2245552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2246552f7358SJed Brown     PetscInt newJoinSize = 0;
2247552f7358SJed Brown 
2248552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
2249552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
2250552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2251552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
2252552f7358SJed Brown 
2253552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
2254552f7358SJed Brown         if (point == join[i][m]) {
2255552f7358SJed Brown           join[1-i][newJoinSize++] = point;
2256552f7358SJed Brown           break;
2257552f7358SJed Brown         }
2258552f7358SJed Brown       }
2259552f7358SJed Brown     }
2260552f7358SJed Brown     joinSize = newJoinSize;
2261552f7358SJed Brown     i        = 1-i;
2262552f7358SJed Brown   }
2263552f7358SJed Brown   *numCoveredPoints = joinSize;
2264552f7358SJed Brown   *coveredPoints    = join[i];
2265552f7358SJed Brown   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
2266552f7358SJed Brown   PetscFunctionReturn(0);
2267552f7358SJed Brown }
2268552f7358SJed Brown 
2269552f7358SJed Brown /*@C
2270552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
2271552f7358SJed Brown 
2272552f7358SJed Brown   Not Collective
2273552f7358SJed Brown 
2274552f7358SJed Brown   Input Parameters:
2275552f7358SJed Brown + dm - The DMPlex object
2276552f7358SJed Brown . numPoints - The number of input points for the join
2277552f7358SJed Brown - points - The input points
2278552f7358SJed Brown 
2279552f7358SJed Brown   Output Parameters:
2280552f7358SJed Brown + numCoveredPoints - The number of points in the join
2281552f7358SJed Brown - coveredPoints - The points in the join
2282552f7358SJed Brown 
22833813dfbdSMatthew G Knepley   Fortran Notes:
22843813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22853813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22863813dfbdSMatthew G Knepley 
22873813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
22883813dfbdSMatthew G Knepley 
2289552f7358SJed Brown   Level: intermediate
2290552f7358SJed Brown 
2291552f7358SJed Brown .keywords: mesh
2292552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
2293552f7358SJed Brown @*/
2294552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2295552f7358SJed Brown {
2296552f7358SJed Brown   PetscErrorCode ierr;
2297552f7358SJed Brown 
2298552f7358SJed Brown   PetscFunctionBegin;
2299552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2300d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2301d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2302d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
2303552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
2304d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2305552f7358SJed Brown   PetscFunctionReturn(0);
2306552f7358SJed Brown }
2307552f7358SJed Brown 
2308552f7358SJed Brown /*@C
2309552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
2310552f7358SJed Brown 
2311552f7358SJed Brown   Not Collective
2312552f7358SJed Brown 
2313552f7358SJed Brown   Input Parameters:
2314552f7358SJed Brown + dm - The DMPlex object
2315552f7358SJed Brown . numPoints - The number of input points for the join
2316552f7358SJed Brown - points - The input points
2317552f7358SJed Brown 
2318552f7358SJed Brown   Output Parameters:
2319552f7358SJed Brown + numCoveredPoints - The number of points in the join
2320552f7358SJed Brown - coveredPoints - The points in the join
2321552f7358SJed Brown 
23223813dfbdSMatthew G Knepley   Fortran Notes:
23233813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
23243813dfbdSMatthew G Knepley   include petsc.h90 in your code.
23253813dfbdSMatthew G Knepley 
23263813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
23273813dfbdSMatthew G Knepley 
2328552f7358SJed Brown   Level: intermediate
2329552f7358SJed Brown 
2330552f7358SJed Brown .keywords: mesh
2331552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
2332552f7358SJed Brown @*/
2333552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2334552f7358SJed Brown {
2335552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2336552f7358SJed Brown   PetscInt      *offsets, **closures;
2337552f7358SJed Brown   PetscInt      *join[2];
2338552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
233924c766afSToby Isaac   PetscInt       p, d, c, m, ms;
2340552f7358SJed Brown   PetscErrorCode ierr;
2341552f7358SJed Brown 
2342552f7358SJed Brown   PetscFunctionBegin;
2343552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2344552f7358SJed Brown   PetscValidPointer(points, 2);
2345552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2346552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2347552f7358SJed Brown 
2348552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
23491795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
2350552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
235124c766afSToby Isaac   ms      = mesh->maxSupportSize;
235224c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
2353552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
2354552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
2355552f7358SJed Brown 
2356552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2357552f7358SJed Brown     PetscInt closureSize;
2358552f7358SJed Brown 
2359552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
23600d644c17SKarl Rupp 
2361552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
2362552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
2363552f7358SJed Brown       PetscInt pStart, pEnd, i;
2364552f7358SJed Brown 
2365552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
2366552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
2367552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2368552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
2369552f7358SJed Brown           break;
2370552f7358SJed Brown         }
2371552f7358SJed Brown       }
2372552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
2373552f7358SJed Brown     }
237482f516ccSBarry 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);
2375552f7358SJed Brown   }
2376552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
2377552f7358SJed Brown     PetscInt dof;
2378552f7358SJed Brown 
2379552f7358SJed Brown     /* Copy in support of first point */
2380552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
2381552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
2382552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
2383552f7358SJed Brown     }
2384552f7358SJed Brown     /* Check each successive cone */
2385552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
2386552f7358SJed Brown       PetscInt newJoinSize = 0;
2387552f7358SJed Brown 
2388552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
2389552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2390552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
2391552f7358SJed Brown 
2392552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
2393552f7358SJed Brown           if (point == join[i][m]) {
2394552f7358SJed Brown             join[1-i][newJoinSize++] = point;
2395552f7358SJed Brown             break;
2396552f7358SJed Brown           }
2397552f7358SJed Brown         }
2398552f7358SJed Brown       }
2399552f7358SJed Brown       joinSize = newJoinSize;
2400552f7358SJed Brown       i        = 1-i;
2401552f7358SJed Brown     }
2402552f7358SJed Brown     if (joinSize) break;
2403552f7358SJed Brown   }
2404552f7358SJed Brown   *numCoveredPoints = joinSize;
2405552f7358SJed Brown   *coveredPoints    = join[i];
2406552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
24070298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
2408552f7358SJed Brown   }
2409552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
2410552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
2411552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
2412552f7358SJed Brown   PetscFunctionReturn(0);
2413552f7358SJed Brown }
2414552f7358SJed Brown 
2415552f7358SJed Brown /*@C
2416552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
2417552f7358SJed Brown 
2418552f7358SJed Brown   Not Collective
2419552f7358SJed Brown 
2420552f7358SJed Brown   Input Parameters:
2421552f7358SJed Brown + dm - The DMPlex object
2422552f7358SJed Brown . numPoints - The number of input points for the meet
2423552f7358SJed Brown - points - The input points
2424552f7358SJed Brown 
2425552f7358SJed Brown   Output Parameters:
2426552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2427552f7358SJed Brown - coveredPoints - The points in the meet
2428552f7358SJed Brown 
2429552f7358SJed Brown   Level: intermediate
2430552f7358SJed Brown 
2431552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
2432552f7358SJed Brown 
24333813dfbdSMatthew G Knepley   Fortran Notes:
24343813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24353813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24363813dfbdSMatthew G Knepley 
24373813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24383813dfbdSMatthew G Knepley 
2439552f7358SJed Brown .keywords: mesh
2440552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
2441552f7358SJed Brown @*/
2442552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
2443552f7358SJed Brown {
2444552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2445552f7358SJed Brown   PetscInt      *meet[2];
2446552f7358SJed Brown   PetscInt       meetSize, i = 0;
2447552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2448552f7358SJed Brown   PetscErrorCode ierr;
2449552f7358SJed Brown 
2450552f7358SJed Brown   PetscFunctionBegin;
2451552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2452552f7358SJed Brown   PetscValidPointer(points, 2);
2453552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
2454552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
2455552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
2456552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
2457552f7358SJed Brown   /* Copy in cone of first point */
2458552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
2459552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
2460552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
2461552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
2462552f7358SJed Brown   }
2463552f7358SJed Brown   /* Check each successive cone */
2464552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2465552f7358SJed Brown     PetscInt newMeetSize = 0;
2466552f7358SJed Brown 
2467552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
2468552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
2469552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2470552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
2471552f7358SJed Brown 
2472552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
2473552f7358SJed Brown         if (point == meet[i][m]) {
2474552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
2475552f7358SJed Brown           break;
2476552f7358SJed Brown         }
2477552f7358SJed Brown       }
2478552f7358SJed Brown     }
2479552f7358SJed Brown     meetSize = newMeetSize;
2480552f7358SJed Brown     i        = 1-i;
2481552f7358SJed Brown   }
2482552f7358SJed Brown   *numCoveringPoints = meetSize;
2483552f7358SJed Brown   *coveringPoints    = meet[i];
2484552f7358SJed Brown   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
2485552f7358SJed Brown   PetscFunctionReturn(0);
2486552f7358SJed Brown }
2487552f7358SJed Brown 
2488552f7358SJed Brown /*@C
2489552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
2490552f7358SJed Brown 
2491552f7358SJed Brown   Not Collective
2492552f7358SJed Brown 
2493552f7358SJed Brown   Input Parameters:
2494552f7358SJed Brown + dm - The DMPlex object
2495552f7358SJed Brown . numPoints - The number of input points for the meet
2496552f7358SJed Brown - points - The input points
2497552f7358SJed Brown 
2498552f7358SJed Brown   Output Parameters:
2499552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2500552f7358SJed Brown - coveredPoints - The points in the meet
2501552f7358SJed Brown 
2502552f7358SJed Brown   Level: intermediate
2503552f7358SJed Brown 
25043813dfbdSMatthew G Knepley   Fortran Notes:
25053813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25063813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25073813dfbdSMatthew G Knepley 
25083813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25093813dfbdSMatthew G Knepley 
2510552f7358SJed Brown .keywords: mesh
2511552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
2512552f7358SJed Brown @*/
2513552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2514552f7358SJed Brown {
2515552f7358SJed Brown   PetscErrorCode ierr;
2516552f7358SJed Brown 
2517552f7358SJed Brown   PetscFunctionBegin;
2518552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2519d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2520d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2521d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
2522552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
2523d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2524552f7358SJed Brown   PetscFunctionReturn(0);
2525552f7358SJed Brown }
2526552f7358SJed Brown 
2527552f7358SJed Brown /*@C
2528552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
2529552f7358SJed Brown 
2530552f7358SJed Brown   Not Collective
2531552f7358SJed Brown 
2532552f7358SJed Brown   Input Parameters:
2533552f7358SJed Brown + dm - The DMPlex object
2534552f7358SJed Brown . numPoints - The number of input points for the meet
2535552f7358SJed Brown - points - The input points
2536552f7358SJed Brown 
2537552f7358SJed Brown   Output Parameters:
2538552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2539552f7358SJed Brown - coveredPoints - The points in the meet
2540552f7358SJed Brown 
2541552f7358SJed Brown   Level: intermediate
2542552f7358SJed Brown 
25433813dfbdSMatthew G Knepley   Fortran Notes:
25443813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25453813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25463813dfbdSMatthew G Knepley 
25473813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25483813dfbdSMatthew G Knepley 
2549552f7358SJed Brown .keywords: mesh
2550552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
2551552f7358SJed Brown @*/
2552552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2553552f7358SJed Brown {
2554552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2555552f7358SJed Brown   PetscInt      *offsets, **closures;
2556552f7358SJed Brown   PetscInt      *meet[2];
2557552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
255824c766afSToby Isaac   PetscInt       p, h, c, m, mc;
2559552f7358SJed Brown   PetscErrorCode ierr;
2560552f7358SJed Brown 
2561552f7358SJed Brown   PetscFunctionBegin;
2562552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2563552f7358SJed Brown   PetscValidPointer(points, 2);
2564552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2565552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2566552f7358SJed Brown 
2567552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
2568785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
2569552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
257024c766afSToby Isaac   mc      = mesh->maxConeSize;
257124c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
2572552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
2573552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
2574552f7358SJed Brown 
2575552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2576552f7358SJed Brown     PetscInt closureSize;
2577552f7358SJed Brown 
2578552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
25790d644c17SKarl Rupp 
2580552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
2581552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
2582552f7358SJed Brown       PetscInt pStart, pEnd, i;
2583552f7358SJed Brown 
2584552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
2585552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
2586552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2587552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
2588552f7358SJed Brown           break;
2589552f7358SJed Brown         }
2590552f7358SJed Brown       }
2591552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
2592552f7358SJed Brown     }
259382f516ccSBarry 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);
2594552f7358SJed Brown   }
2595552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
2596552f7358SJed Brown     PetscInt dof;
2597552f7358SJed Brown 
2598552f7358SJed Brown     /* Copy in cone of first point */
2599552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
2600552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
2601552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
2602552f7358SJed Brown     }
2603552f7358SJed Brown     /* Check each successive cone */
2604552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
2605552f7358SJed Brown       PetscInt newMeetSize = 0;
2606552f7358SJed Brown 
2607552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
2608552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2609552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
2610552f7358SJed Brown 
2611552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
2612552f7358SJed Brown           if (point == meet[i][m]) {
2613552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
2614552f7358SJed Brown             break;
2615552f7358SJed Brown           }
2616552f7358SJed Brown         }
2617552f7358SJed Brown       }
2618552f7358SJed Brown       meetSize = newMeetSize;
2619552f7358SJed Brown       i        = 1-i;
2620552f7358SJed Brown     }
2621552f7358SJed Brown     if (meetSize) break;
2622552f7358SJed Brown   }
2623552f7358SJed Brown   *numCoveredPoints = meetSize;
2624552f7358SJed Brown   *coveredPoints    = meet[i];
2625552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
26260298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
2627552f7358SJed Brown   }
2628552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
2629552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
2630552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
2631552f7358SJed Brown   PetscFunctionReturn(0);
2632552f7358SJed Brown }
2633552f7358SJed Brown 
26344e3744c5SMatthew G. Knepley /*@C
26354e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
26364e3744c5SMatthew G. Knepley 
26374e3744c5SMatthew G. Knepley   Not Collective
26384e3744c5SMatthew G. Knepley 
26394e3744c5SMatthew G. Knepley   Input Parameters:
26404e3744c5SMatthew G. Knepley + dmA - A DMPlex object
26414e3744c5SMatthew G. Knepley - dmB - A DMPlex object
26424e3744c5SMatthew G. Knepley 
26434e3744c5SMatthew G. Knepley   Output Parameters:
26444e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
26454e3744c5SMatthew G. Knepley 
26464e3744c5SMatthew G. Knepley   Level: intermediate
26474e3744c5SMatthew G. Knepley 
26484e3744c5SMatthew G. Knepley   Notes:
26494e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
26504e3744c5SMatthew G. Knepley 
26514e3744c5SMatthew G. Knepley .keywords: mesh
26524e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
26534e3744c5SMatthew G. Knepley @*/
26544e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
26554e3744c5SMatthew G. Knepley {
26564e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
26574e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
26584e3744c5SMatthew G. Knepley 
26594e3744c5SMatthew G. Knepley   PetscFunctionBegin;
26604e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
26614e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
26624e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
26634e3744c5SMatthew G. Knepley 
26644e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
26654e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
26664e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
26674e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
26684e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
26694e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
26704e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
26714e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
26724e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
26734e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
26744e3744c5SMatthew G. Knepley 
26754e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
26764e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
26774e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
26784e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
26794e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
26804e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
26814e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
26824e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
26834e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
26844e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
26854e3744c5SMatthew G. Knepley     }
26864e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
26874e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
26884e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
26894e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
26904e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
26914e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
26924e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
26934e3744c5SMatthew G. Knepley     }
26944e3744c5SMatthew G. Knepley   }
26954e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
26964e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
26974e3744c5SMatthew G. Knepley }
26984e3744c5SMatthew G. Knepley 
269918ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
2700a6dfd86eSKarl Rupp {
270182f516ccSBarry Smith   MPI_Comm       comm;
2702552f7358SJed Brown   PetscErrorCode ierr;
2703552f7358SJed Brown 
2704552f7358SJed Brown   PetscFunctionBegin;
270582f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2706552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
2707552f7358SJed Brown   switch (cellDim) {
2708552f7358SJed Brown   case 0:
2709552f7358SJed Brown     *numFaceVertices = 0;
2710552f7358SJed Brown     break;
2711552f7358SJed Brown   case 1:
2712552f7358SJed Brown     *numFaceVertices = 1;
2713552f7358SJed Brown     break;
2714552f7358SJed Brown   case 2:
2715552f7358SJed Brown     switch (numCorners) {
271619436ca2SJed Brown     case 3: /* triangle */
271719436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2718552f7358SJed Brown       break;
271919436ca2SJed Brown     case 4: /* quadrilateral */
272019436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2721552f7358SJed Brown       break;
272219436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
272319436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2724552f7358SJed Brown       break;
272519436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
272619436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2727552f7358SJed Brown       break;
2728552f7358SJed Brown     default:
2729f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2730552f7358SJed Brown     }
2731552f7358SJed Brown     break;
2732552f7358SJed Brown   case 3:
2733552f7358SJed Brown     switch (numCorners) {
273419436ca2SJed Brown     case 4: /* tetradehdron */
273519436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
2736552f7358SJed Brown       break;
273719436ca2SJed Brown     case 6: /* tet cohesive cells */
273819436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2739552f7358SJed Brown       break;
274019436ca2SJed Brown     case 8: /* hexahedron */
274119436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2742552f7358SJed Brown       break;
274319436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
274419436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2745552f7358SJed Brown       break;
274619436ca2SJed Brown     case 10: /* quadratic tetrahedron */
274719436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2748552f7358SJed Brown       break;
274919436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
275019436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2751552f7358SJed Brown       break;
275219436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
275319436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2754552f7358SJed Brown       break;
275519436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
275619436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
2757552f7358SJed Brown       break;
2758552f7358SJed Brown     default:
2759f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2760552f7358SJed Brown     }
2761552f7358SJed Brown     break;
2762552f7358SJed Brown   default:
2763f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
2764552f7358SJed Brown   }
2765552f7358SJed Brown   PetscFunctionReturn(0);
2766552f7358SJed Brown }
2767552f7358SJed Brown 
2768552f7358SJed Brown /*@
2769aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
2770552f7358SJed Brown 
2771552f7358SJed Brown   Not Collective
2772552f7358SJed Brown 
2773aa50250dSMatthew G. Knepley   Input Parameter:
2774552f7358SJed Brown . dm    - The DMPlex object
2775552f7358SJed Brown 
2776aa50250dSMatthew G. Knepley   Output Parameter:
2777aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
2778552f7358SJed Brown 
2779552f7358SJed Brown   Level: developer
2780552f7358SJed Brown 
2781aa50250dSMatthew G. Knepley .keywords: mesh, points
2782aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2783aa50250dSMatthew G. Knepley @*/
2784aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
2785aa50250dSMatthew G. Knepley {
2786aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
2787aa50250dSMatthew G. Knepley 
2788aa50250dSMatthew G. Knepley   PetscFunctionBegin;
2789aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2790aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
2791c58f1c22SToby Isaac   if (!dm->depthLabel) {ierr = DMGetLabel(dm, "depth", &dm->depthLabel);CHKERRQ(ierr);}
2792c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
2793aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
2794aa50250dSMatthew G. Knepley }
2795aa50250dSMatthew G. Knepley 
2796aa50250dSMatthew G. Knepley /*@
2797aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
2798aa50250dSMatthew G. Knepley 
2799aa50250dSMatthew G. Knepley   Not Collective
2800aa50250dSMatthew G. Knepley 
2801aa50250dSMatthew G. Knepley   Input Parameter:
2802aa50250dSMatthew G. Knepley . dm    - The DMPlex object
2803aa50250dSMatthew G. Knepley 
2804aa50250dSMatthew G. Knepley   Output Parameter:
2805aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
2806aa50250dSMatthew G. Knepley 
2807aa50250dSMatthew G. Knepley   Level: developer
2808552f7358SJed Brown 
2809552f7358SJed Brown .keywords: mesh, points
2810aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2811552f7358SJed Brown @*/
2812552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
2813552f7358SJed Brown {
2814aa50250dSMatthew G. Knepley   DMLabel        label;
2815aa50250dSMatthew G. Knepley   PetscInt       d = 0;
2816552f7358SJed Brown   PetscErrorCode ierr;
2817552f7358SJed Brown 
2818552f7358SJed Brown   PetscFunctionBegin;
2819552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2820552f7358SJed Brown   PetscValidPointer(depth, 2);
2821aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2822aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
2823552f7358SJed Brown   *depth = d-1;
2824552f7358SJed Brown   PetscFunctionReturn(0);
2825552f7358SJed Brown }
2826552f7358SJed Brown 
2827552f7358SJed Brown /*@
2828552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
2829552f7358SJed Brown 
2830552f7358SJed Brown   Not Collective
2831552f7358SJed Brown 
2832552f7358SJed Brown   Input Parameters:
2833552f7358SJed Brown + dm           - The DMPlex object
2834552f7358SJed Brown - stratumValue - The requested depth
2835552f7358SJed Brown 
2836552f7358SJed Brown   Output Parameters:
2837552f7358SJed Brown + start - The first point at this depth
2838552f7358SJed Brown - end   - One beyond the last point at this depth
2839552f7358SJed Brown 
2840552f7358SJed Brown   Level: developer
2841552f7358SJed Brown 
2842552f7358SJed Brown .keywords: mesh, points
2843552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
2844552f7358SJed Brown @*/
28450adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
28460adebc6cSBarry Smith {
2847aa50250dSMatthew G. Knepley   DMLabel        label;
284863d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
2849552f7358SJed Brown   PetscErrorCode ierr;
2850552f7358SJed Brown 
2851552f7358SJed Brown   PetscFunctionBegin;
2852552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
285363d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
285463d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
2855552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
28560d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
285763d1a920SMatthew G. Knepley   if (stratumValue < 0) {
285863d1a920SMatthew G. Knepley     if (start) *start = pStart;
285963d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
286063d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
2861552f7358SJed Brown   }
2862aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
286363d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
286463d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
2865552f7358SJed Brown   PetscFunctionReturn(0);
2866552f7358SJed Brown }
2867552f7358SJed Brown 
2868552f7358SJed Brown /*@
2869552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
2870552f7358SJed Brown 
2871552f7358SJed Brown   Not Collective
2872552f7358SJed Brown 
2873552f7358SJed Brown   Input Parameters:
2874552f7358SJed Brown + dm           - The DMPlex object
2875552f7358SJed Brown - stratumValue - The requested height
2876552f7358SJed Brown 
2877552f7358SJed Brown   Output Parameters:
2878552f7358SJed Brown + start - The first point at this height
2879552f7358SJed Brown - end   - One beyond the last point at this height
2880552f7358SJed Brown 
2881552f7358SJed Brown   Level: developer
2882552f7358SJed Brown 
2883552f7358SJed Brown .keywords: mesh, points
2884552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
2885552f7358SJed Brown @*/
28860adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
28870adebc6cSBarry Smith {
2888aa50250dSMatthew G. Knepley   DMLabel        label;
288963d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
2890552f7358SJed Brown   PetscErrorCode ierr;
2891552f7358SJed Brown 
2892552f7358SJed Brown   PetscFunctionBegin;
2893552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
289463d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
289563d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
2896552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
28970d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
289863d1a920SMatthew G. Knepley   if (stratumValue < 0) {
289963d1a920SMatthew G. Knepley     if (start) *start = pStart;
290063d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
290163d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
2902552f7358SJed Brown   }
2903aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2904aa50250dSMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");CHKERRQ(ierr);
290563d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
290663d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
2907552f7358SJed Brown   PetscFunctionReturn(0);
2908552f7358SJed Brown }
2909552f7358SJed Brown 
2910552f7358SJed Brown /* Set the number of dof on each point and separate by fields */
2911a6dfd86eSKarl Rupp PetscErrorCode DMPlexCreateSectionInitial(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscSection *section)
2912a6dfd86eSKarl Rupp {
2913ee55978aSMatthew G. Knepley   PetscInt      *pMax;
2914916f0f19SMatthew G. Knepley   PetscInt       depth, pStart = 0, pEnd = 0;
2915ee55978aSMatthew G. Knepley   PetscInt       Nf, p, d, dep, f;
2916fa716be8SMatthew G. Knepley   PetscBool     *isFE;
2917552f7358SJed Brown   PetscErrorCode ierr;
2918552f7358SJed Brown 
2919552f7358SJed Brown   PetscFunctionBegin;
2920fa716be8SMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
2921ee55978aSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
2922fa716be8SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
2923fa716be8SMatthew G. Knepley     PetscObject  obj;
2924fa716be8SMatthew G. Knepley     PetscClassId id;
2925fa716be8SMatthew G. Knepley 
2926ee55978aSMatthew G. Knepley     isFE[f] = PETSC_FALSE;
2927ee55978aSMatthew G. Knepley     if (f >= Nf) continue;
2928fa716be8SMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
2929fa716be8SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
2930fa716be8SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
2931fa716be8SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
2932552f7358SJed Brown   }
293382f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), section);CHKERRQ(ierr);
2934552f7358SJed Brown   if (numFields > 0) {
2935552f7358SJed Brown     ierr = PetscSectionSetNumFields(*section, numFields);CHKERRQ(ierr);
2936552f7358SJed Brown     if (numComp) {
2937552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
2938552f7358SJed Brown         ierr = PetscSectionSetFieldComponents(*section, f, numComp[f]);CHKERRQ(ierr);
2939d484f18aSToby Isaac         if (isFE[f]) {
2940d484f18aSToby Isaac           PetscFE           fe;
2941d484f18aSToby Isaac           PetscDualSpace    dspace;
2942d484f18aSToby Isaac           const PetscInt    ***perms;
2943d484f18aSToby Isaac           const PetscScalar ***flips;
2944d484f18aSToby Isaac           const PetscInt    *numDof;
2945d484f18aSToby Isaac 
2946d484f18aSToby Isaac           ierr = DMGetField(dm,f,(PetscObject *) &fe);CHKERRQ(ierr);
2947d484f18aSToby Isaac           ierr = PetscFEGetDualSpace(fe,&dspace);CHKERRQ(ierr);
2948d484f18aSToby Isaac           ierr = PetscDualSpaceGetSymmetries(dspace,&perms,&flips);CHKERRQ(ierr);
2949d484f18aSToby Isaac           ierr = PetscDualSpaceGetNumDof(dspace,&numDof);CHKERRQ(ierr);
2950d484f18aSToby Isaac           if (perms || flips) {
2951d484f18aSToby Isaac             DM               K;
2952d484f18aSToby Isaac             DMLabel          depthLabel;
2953d484f18aSToby Isaac             PetscInt         depth, h;
2954d484f18aSToby Isaac             PetscSectionSym  sym;
2955d484f18aSToby Isaac 
2956d484f18aSToby Isaac             ierr = PetscDualSpaceGetDM(dspace,&K);CHKERRQ(ierr);
2957d484f18aSToby Isaac             ierr = DMPlexGetDepthLabel(dm,&depthLabel);CHKERRQ(ierr);
2958d484f18aSToby Isaac             ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
2959d484f18aSToby Isaac             ierr = PetscSectionSymCreateLabel(PetscObjectComm((PetscObject)*section),depthLabel,&sym);CHKERRQ(ierr);
2960d484f18aSToby Isaac             for (h = 0; h <= depth; h++) {
2961d484f18aSToby Isaac               PetscDualSpace    hspace;
2962d484f18aSToby Isaac               PetscInt          kStart, kEnd;
2963d484f18aSToby Isaac               PetscInt          kConeSize;
2964d484f18aSToby Isaac               const PetscInt    **perms0 = NULL;
2965d484f18aSToby Isaac               const PetscScalar **flips0 = NULL;
2966d484f18aSToby Isaac 
2967d484f18aSToby Isaac               ierr = PetscDualSpaceGetHeightSubspace(dspace,h,&hspace);CHKERRQ(ierr);
2968d484f18aSToby Isaac               ierr = DMPlexGetHeightStratum(K,h,&kStart,&kEnd);CHKERRQ(ierr);
2969d484f18aSToby Isaac               if (!hspace) continue;
2970d484f18aSToby Isaac               ierr = PetscDualSpaceGetSymmetries(hspace,&perms,&flips);CHKERRQ(ierr);
2971d484f18aSToby Isaac               if (perms) perms0 = perms[0];
2972d484f18aSToby Isaac               if (flips) flips0 = flips[0];
2973d484f18aSToby Isaac               if (!(perms0 || flips0)) continue;
2974d484f18aSToby Isaac               ierr = DMPlexGetConeSize(K,kStart,&kConeSize);CHKERRQ(ierr);
2975d484f18aSToby Isaac               if (numComp[f] == 1) {
2976d484f18aSToby Isaac                 ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numDof[depth - h],-kConeSize,kConeSize,PETSC_USE_POINTER,perms0 ? &perms0[-kConeSize] : NULL,flips0 ? &flips0[-kConeSize] : NULL);CHKERRQ(ierr);
2977d484f18aSToby Isaac               } else {
2978d484f18aSToby Isaac                 PetscInt    **fieldPerms = NULL, o;
2979d484f18aSToby Isaac                 PetscScalar **fieldFlips = NULL;
2980d484f18aSToby Isaac 
2981d484f18aSToby Isaac                 ierr = PetscCalloc1(2 * kConeSize,&fieldPerms);CHKERRQ(ierr);
2982d484f18aSToby Isaac                 ierr = PetscCalloc1(2 * kConeSize,&fieldFlips);CHKERRQ(ierr);
2983d484f18aSToby Isaac                 for (o = -kConeSize; o < kConeSize; o++) {
2984d484f18aSToby Isaac                   if (perms0 && perms0[o]) {
2985d484f18aSToby Isaac                     PetscInt r, s;
2986d484f18aSToby Isaac 
2987d484f18aSToby Isaac                     ierr = PetscMalloc1(numComp[f] * numDof[depth - h],&fieldPerms[o+kConeSize]);CHKERRQ(ierr);
2988d484f18aSToby Isaac                     for (r = 0; r < numDof[depth - h]; r++) {
2989d484f18aSToby Isaac                       for (s = 0; s < numComp[f]; s++) {
2990d484f18aSToby Isaac                         fieldPerms[o+kConeSize][r * numComp[f] + s] = numComp[f] * perms0[o][r] + s;
2991d484f18aSToby Isaac                       }
2992d484f18aSToby Isaac                     }
2993d484f18aSToby Isaac                   }
2994d484f18aSToby Isaac                   if (flips0 && flips0[o]) {
2995d484f18aSToby Isaac                     PetscInt r, s;
2996d484f18aSToby Isaac 
2997d484f18aSToby Isaac                     ierr = PetscMalloc1(numComp[f] * numDof[depth - h],&fieldFlips[o+kConeSize]);CHKERRQ(ierr);
2998d484f18aSToby Isaac                     for (r = 0; r < numDof[depth - h]; r++) {
2999d484f18aSToby Isaac                       for (s = 0; s < numComp[f]; s++) {
3000d484f18aSToby Isaac                         fieldFlips[o+kConeSize][r * numComp[f] + s] = flips0[o][r];
3001d484f18aSToby Isaac                       }
3002d484f18aSToby Isaac                     }
3003d484f18aSToby Isaac                   }
3004d484f18aSToby Isaac                 }
3005d484f18aSToby Isaac                 ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numComp[f] * numDof[depth - h],-kConeSize,kConeSize,PETSC_OWN_POINTER,(const PetscInt **) fieldPerms,(const PetscScalar **)fieldFlips);CHKERRQ(ierr);
3006d484f18aSToby Isaac               }
3007d484f18aSToby Isaac             }
3008d484f18aSToby Isaac             ierr = PetscSectionSetFieldSym(*section,f,sym);CHKERRQ(ierr);
3009d484f18aSToby Isaac             ierr = PetscSectionSymDestroy(&sym);CHKERRQ(ierr);
3010d484f18aSToby Isaac           }
3011d484f18aSToby Isaac         }
3012552f7358SJed Brown       }
3013552f7358SJed Brown     }
3014552f7358SJed Brown   }
3015552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3016552f7358SJed Brown   ierr = PetscSectionSetChart(*section, pStart, pEnd);CHKERRQ(ierr);
3017916f0f19SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
30189ac3fadcSMatthew G. Knepley   ierr = PetscMalloc1(depth+1,&pMax);CHKERRQ(ierr);
30199ac3fadcSMatthew 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);
3020916f0f19SMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
3021916f0f19SMatthew G. Knepley     d    = dim == depth ? dep : (!dep ? 0 : dim);
3022916f0f19SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dep, &pStart, &pEnd);CHKERRQ(ierr);
3023fa716be8SMatthew G. Knepley     pMax[dep] = pMax[dep] < 0 ? pEnd : pMax[dep];
3024552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
3025ee55978aSMatthew G. Knepley       PetscInt tot = 0;
3026ee55978aSMatthew G. Knepley 
3027552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3028fa716be8SMatthew G. Knepley         if (isFE[f] && p >= pMax[dep]) continue;
3029552f7358SJed Brown         ierr = PetscSectionSetFieldDof(*section, p, f, numDof[f*(dim+1)+d]);CHKERRQ(ierr);
3030ee55978aSMatthew G. Knepley         tot += numDof[f*(dim+1)+d];
3031552f7358SJed Brown       }
3032ee55978aSMatthew G. Knepley       ierr = PetscSectionSetDof(*section, p, tot);CHKERRQ(ierr);
3033552f7358SJed Brown     }
3034552f7358SJed Brown   }
30359ac3fadcSMatthew G. Knepley   ierr = PetscFree(pMax);CHKERRQ(ierr);
3036fa716be8SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
3037552f7358SJed Brown   PetscFunctionReturn(0);
3038552f7358SJed Brown }
3039552f7358SJed Brown 
3040552f7358SJed Brown /* Set the number of dof on each point and separate by fields
3041ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3042552f7358SJed Brown */
3043ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSectionBCDof(DM dm, PetscInt numBC, const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
3044a6dfd86eSKarl Rupp {
3045552f7358SJed Brown   PetscInt       numFields;
3046552f7358SJed Brown   PetscInt       bc;
3047a68b90caSToby Isaac   PetscSection   aSec;
3048552f7358SJed Brown   PetscErrorCode ierr;
3049552f7358SJed Brown 
3050552f7358SJed Brown   PetscFunctionBegin;
3051552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3052552f7358SJed Brown   for (bc = 0; bc < numBC; ++bc) {
3053552f7358SJed Brown     PetscInt        field = 0;
3054ab1d0545SMatthew G. Knepley     const PetscInt *comp;
3055552f7358SJed Brown     const PetscInt *idx;
3056ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3057552f7358SJed Brown 
30580d644c17SKarl Rupp     if (numFields) field = bcField[bc];
3059ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3060ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3061552f7358SJed Brown     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3062552f7358SJed Brown     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3063552f7358SJed Brown     for (i = 0; i < n; ++i) {
3064552f7358SJed Brown       const PetscInt p = idx[i];
306506ff1081SMatthew G. Knepley       PetscInt       numConst;
3066552f7358SJed Brown 
3067552f7358SJed Brown       if (numFields) {
3068552f7358SJed Brown         ierr = PetscSectionGetFieldDof(section, p, field, &numConst);CHKERRQ(ierr);
3069552f7358SJed Brown       } else {
3070552f7358SJed Brown         ierr = PetscSectionGetDof(section, p, &numConst);CHKERRQ(ierr);
3071552f7358SJed Brown       }
307206ff1081SMatthew G. Knepley       /* If Nc < 0, constrain every dof on the point */
307306ff1081SMatthew G. Knepley       if (Nc > 0) numConst = PetscMin(numConst, Nc);
307406ff1081SMatthew G. Knepley       if (numFields) {ierr = PetscSectionAddFieldConstraintDof(section, p, field, numConst);CHKERRQ(ierr);}
3075552f7358SJed Brown       ierr = PetscSectionAddConstraintDof(section, p, numConst);CHKERRQ(ierr);
3076552f7358SJed Brown     }
3077552f7358SJed Brown     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
307806ff1081SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3079552f7358SJed Brown   }
3080a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3081a68b90caSToby Isaac   if (aSec) {
3082a68b90caSToby Isaac     PetscInt aStart, aEnd, a;
3083a68b90caSToby Isaac 
3084a68b90caSToby Isaac     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3085a68b90caSToby Isaac     for (a = aStart; a < aEnd; a++) {
3086ab1d0545SMatthew G. Knepley       PetscInt dof, f;
3087a68b90caSToby Isaac 
3088a68b90caSToby Isaac       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3089a68b90caSToby Isaac       if (dof) {
3090a68b90caSToby Isaac         /* if there are point-to-point constraints, then all dofs are constrained */
3091a68b90caSToby Isaac         ierr = PetscSectionGetDof(section, a, &dof);CHKERRQ(ierr);
3092a68b90caSToby Isaac         ierr = PetscSectionSetConstraintDof(section, a, dof);CHKERRQ(ierr);
3093a68b90caSToby Isaac         for (f = 0; f < numFields; f++) {
3094a68b90caSToby Isaac           ierr = PetscSectionGetFieldDof(section, a, f, &dof);CHKERRQ(ierr);
3095a68b90caSToby Isaac           ierr = PetscSectionSetFieldConstraintDof(section, a, f, dof);CHKERRQ(ierr);
3096a68b90caSToby Isaac         }
3097a68b90caSToby Isaac       }
3098a68b90caSToby Isaac     }
3099a68b90caSToby Isaac   }
3100552f7358SJed Brown   PetscFunctionReturn(0);
3101552f7358SJed Brown }
3102552f7358SJed Brown 
3103ab1d0545SMatthew G. Knepley /* Set the constrained field indices on each point
3104ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3105ab1d0545SMatthew G. Knepley */
3106ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSectionBCIndicesField(DM dm, PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
31070adebc6cSBarry Smith {
3108ab1d0545SMatthew G. Knepley   PetscSection   aSec;
3109ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3110ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, bc, f, d;
3111552f7358SJed Brown   PetscErrorCode ierr;
3112552f7358SJed Brown 
3113552f7358SJed Brown   PetscFunctionBegin;
3114552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3115ab1d0545SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
3116ab1d0545SMatthew G. Knepley   /* Initialize all field indices to -1 */
3117552f7358SJed Brown   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3118ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3119ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3120ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3121ab1d0545SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) for (f = 0; f < numFields; ++f) {ierr = PetscSectionSetFieldConstraintIndices(section, p, f, indices);CHKERRQ(ierr);}
3122ab1d0545SMatthew G. Knepley   /* Handle BC constraints */
3123ab1d0545SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {
3124ab1d0545SMatthew G. Knepley     const PetscInt  field = bcField[bc];
3125ab1d0545SMatthew G. Knepley     const PetscInt *comp, *idx;
3126ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3127552f7358SJed Brown 
3128ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3129ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3130ab1d0545SMatthew G. Knepley     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3131ab1d0545SMatthew G. Knepley     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3132ab1d0545SMatthew G. Knepley     for (i = 0; i < n; ++i) {
3133ab1d0545SMatthew G. Knepley       const PetscInt  p = idx[i];
3134ab1d0545SMatthew G. Knepley       const PetscInt *find;
3135ab1d0545SMatthew G. Knepley       PetscInt        fcdof, c;
3136ab1d0545SMatthew G. Knepley 
3137ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintDof(section, p, field, &fcdof);CHKERRQ(ierr);
3138ab1d0545SMatthew G. Knepley       if (Nc < 0) {
3139ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) indices[d] = d;
3140552f7358SJed Brown       } else {
3141ab1d0545SMatthew G. Knepley         ierr = PetscSectionGetFieldConstraintIndices(section, p, field, &find);CHKERRQ(ierr);
3142ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) {if (find[d] < 0) break; indices[d] = find[d];}
3143ab1d0545SMatthew G. Knepley         for (c = 0; c < Nc; ++c) indices[d+c] = comp[c];
3144ab1d0545SMatthew G. Knepley         ierr = PetscSortInt(d+Nc, indices);CHKERRQ(ierr);
3145ab1d0545SMatthew G. Knepley         for (c = d+Nc; c < fcdof; ++c) indices[c] = -1;
3146552f7358SJed Brown       }
3147ab1d0545SMatthew G. Knepley       ierr = PetscSectionSetFieldConstraintIndices(section, p, field, indices);CHKERRQ(ierr);
3148552f7358SJed Brown     }
3149ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3150ab1d0545SMatthew G. Knepley     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3151552f7358SJed Brown   }
3152ab1d0545SMatthew G. Knepley   /* Handle anchors */
3153ab1d0545SMatthew G. Knepley   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3154ab1d0545SMatthew G. Knepley   if (aSec) {
3155ab1d0545SMatthew G. Knepley     PetscInt aStart, aEnd, a;
3156552f7358SJed Brown 
3157ab1d0545SMatthew G. Knepley     for (d = 0; d < maxDof; ++d) indices[d] = d;
3158ab1d0545SMatthew G. Knepley     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3159ab1d0545SMatthew G. Knepley     for (a = aStart; a < aEnd; a++) {
3160ab1d0545SMatthew G. Knepley       PetscInt dof, fdof, f;
3161ab1d0545SMatthew G. Knepley 
3162ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3163ab1d0545SMatthew G. Knepley       if (dof) {
3164ab1d0545SMatthew G. Knepley         /* if there are point-to-point constraints, then all dofs are constrained */
3165ab1d0545SMatthew G. Knepley         for (f = 0; f < numFields; f++) {
3166ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, a, f, &fdof);CHKERRQ(ierr);
3167ab1d0545SMatthew G. Knepley           ierr = PetscSectionSetFieldConstraintIndices(section, a, f, indices);CHKERRQ(ierr);
3168ab1d0545SMatthew G. Knepley         }
3169ab1d0545SMatthew G. Knepley       }
3170ab1d0545SMatthew G. Knepley     }
3171ab1d0545SMatthew G. Knepley   }
3172ab1d0545SMatthew G. Knepley   ierr = PetscFree(indices);CHKERRQ(ierr);
3173ab1d0545SMatthew G. Knepley   PetscFunctionReturn(0);
3174ab1d0545SMatthew G. Knepley }
3175ab1d0545SMatthew G. Knepley 
3176ab1d0545SMatthew G. Knepley /* Set the constrained indices on each point */
3177ab1d0545SMatthew G. Knepley PetscErrorCode DMPlexCreateSectionBCIndices(DM dm, PetscSection section)
3178ab1d0545SMatthew G. Knepley {
3179ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3180ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, f, d;
3181ab1d0545SMatthew G. Knepley   PetscErrorCode ierr;
3182ab1d0545SMatthew G. Knepley 
3183ab1d0545SMatthew G. Knepley   PetscFunctionBegin;
3184ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3185ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3186ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3187ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3188ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3189552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3190552f7358SJed Brown     PetscInt cdof, d;
3191552f7358SJed Brown 
3192552f7358SJed Brown     ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
3193552f7358SJed Brown     if (cdof) {
3194552f7358SJed Brown       if (numFields) {
3195552f7358SJed Brown         PetscInt numConst = 0, foff = 0;
3196552f7358SJed Brown 
3197552f7358SJed Brown         for (f = 0; f < numFields; ++f) {
3198ab1d0545SMatthew G. Knepley           const PetscInt *find;
3199ab1d0545SMatthew G. Knepley           PetscInt        fcdof, fdof;
3200552f7358SJed Brown 
3201552f7358SJed Brown           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
3202ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
3203ab1d0545SMatthew G. Knepley           /* Change constraint numbering from field component to local dof number */
3204ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintIndices(section, p, f, &find);CHKERRQ(ierr);
3205ab1d0545SMatthew G. Knepley           for (d = 0; d < fcdof; ++d) indices[numConst+d] = find[d] + foff;
3206ab1d0545SMatthew G. Knepley           numConst += fcdof;
3207552f7358SJed Brown           foff     += fdof;
3208552f7358SJed Brown         }
3209552f7358SJed Brown         if (cdof != numConst) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Total number of field constraints %D should be %D", numConst, cdof);
3210552f7358SJed Brown       } else {
32110d644c17SKarl Rupp         for (d = 0; d < cdof; ++d) indices[d] = d;
3212552f7358SJed Brown       }
3213552f7358SJed Brown       ierr = PetscSectionSetConstraintIndices(section, p, indices);CHKERRQ(ierr);
3214552f7358SJed Brown     }
3215552f7358SJed Brown   }
3216552f7358SJed Brown   ierr = PetscFree(indices);CHKERRQ(ierr);
3217552f7358SJed Brown   PetscFunctionReturn(0);
3218552f7358SJed Brown }
3219552f7358SJed Brown 
3220552f7358SJed Brown /*@C
3221552f7358SJed Brown   DMPlexCreateSection - Create a PetscSection based upon the dof layout specification provided.
3222552f7358SJed Brown 
3223552f7358SJed Brown   Not Collective
3224552f7358SJed Brown 
3225552f7358SJed Brown   Input Parameters:
3226552f7358SJed Brown + dm        - The DMPlex object
3227552f7358SJed Brown . dim       - The spatial dimension of the problem
3228552f7358SJed Brown . numFields - The number of fields in the problem
3229552f7358SJed Brown . numComp   - An array of size numFields that holds the number of components for each field
3230552f7358SJed 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
3231552f7358SJed Brown . numBC     - The number of boundary conditions
3232552f7358SJed Brown . bcField   - An array of size numBC giving the field number for each boundry condition
3233ab1d0545SMatthew G. Knepley . bcComps   - [Optional] An array of size numBC giving an IS holding the field components to which each boundary condition applies
3234ab1d0545SMatthew G. Knepley . bcPoints  - An array of size numBC giving an IS holding the Plex points to which each boundary condition applies
3235f8f126e8SMatthew G. Knepley - perm      - Optional permutation of the chart, or NULL
3236552f7358SJed Brown 
3237552f7358SJed Brown   Output Parameter:
3238552f7358SJed Brown . section - The PetscSection object
3239552f7358SJed Brown 
3240552f7358SJed 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
3241f8f126e8SMatthew G. Knepley   number of dof for field 0 on each edge.
3242f8f126e8SMatthew G. Knepley 
3243f8f126e8SMatthew G. Knepley   The chart permutation is the same one set using PetscSectionSetPermutation()
3244552f7358SJed Brown 
3245552f7358SJed Brown   Level: developer
3246552f7358SJed Brown 
32473813dfbdSMatthew G Knepley   Fortran Notes:
32483813dfbdSMatthew G Knepley   A Fortran 90 version is available as DMPlexCreateSectionF90()
32493813dfbdSMatthew G Knepley 
3250552f7358SJed Brown .keywords: mesh, elements
3251f8f126e8SMatthew G. Knepley .seealso: DMPlexCreate(), PetscSectionCreate(), PetscSectionSetPermutation()
3252552f7358SJed Brown @*/
3253ab1d0545SMatthew 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)
3254a6dfd86eSKarl Rupp {
3255a68b90caSToby Isaac   PetscSection   aSec;
3256552f7358SJed Brown   PetscErrorCode ierr;
3257552f7358SJed Brown 
3258552f7358SJed Brown   PetscFunctionBegin;
3259552f7358SJed Brown   ierr = DMPlexCreateSectionInitial(dm, dim, numFields, numComp, numDof, section);CHKERRQ(ierr);
3260ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSectionBCDof(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3261f8f126e8SMatthew G. Knepley   if (perm) {ierr = PetscSectionSetPermutation(*section, perm);CHKERRQ(ierr);}
3262552f7358SJed Brown   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
3263a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,NULL);CHKERRQ(ierr);
3264ab1d0545SMatthew G. Knepley   if (numBC || aSec) {
3265ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndicesField(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3266ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndices(dm, *section);CHKERRQ(ierr);
3267ab1d0545SMatthew G. Knepley   }
3268ce1779c8SBarry Smith   ierr = PetscSectionViewFromOptions(*section,NULL,"-section_view");CHKERRQ(ierr);
3269552f7358SJed Brown   PetscFunctionReturn(0);
3270552f7358SJed Brown }
3271552f7358SJed Brown 
32720adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
32730adebc6cSBarry Smith {
3274efe440bfSMatthew G. Knepley   PetscSection   section, s;
3275efe440bfSMatthew G. Knepley   Mat            m;
32763e922f36SToby Isaac   PetscInt       maxHeight;
3277552f7358SJed Brown   PetscErrorCode ierr;
3278552f7358SJed Brown 
3279552f7358SJed Brown   PetscFunctionBegin;
328038221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
32813e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
32823e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
328382f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
3284552f7358SJed Brown   ierr = DMSetDefaultSection(*cdm, section);CHKERRQ(ierr);
32851d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3286efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3287efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3288efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3289efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3290efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
3291552f7358SJed Brown   PetscFunctionReturn(0);
3292552f7358SJed Brown }
3293552f7358SJed Brown 
32940adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
32950adebc6cSBarry Smith {
3296552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3297552f7358SJed Brown 
3298552f7358SJed Brown   PetscFunctionBegin;
3299552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3300552f7358SJed Brown   if (section) *section = mesh->coneSection;
3301552f7358SJed Brown   PetscFunctionReturn(0);
3302552f7358SJed Brown }
3303552f7358SJed Brown 
33048cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
33058cb4d582SMatthew G. Knepley {
33068cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
33078cb4d582SMatthew G. Knepley 
33088cb4d582SMatthew G. Knepley   PetscFunctionBegin;
33098cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33108cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
33118cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
33128cb4d582SMatthew G. Knepley }
33138cb4d582SMatthew G. Knepley 
3314a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3315a6dfd86eSKarl Rupp {
3316552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3317552f7358SJed Brown 
3318552f7358SJed Brown   PetscFunctionBegin;
3319552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3320552f7358SJed Brown   if (cones) *cones = mesh->cones;
3321552f7358SJed Brown   PetscFunctionReturn(0);
3322552f7358SJed Brown }
3323552f7358SJed Brown 
3324a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3325a6dfd86eSKarl Rupp {
3326552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3327552f7358SJed Brown 
3328552f7358SJed Brown   PetscFunctionBegin;
3329552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3330552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3331552f7358SJed Brown   PetscFunctionReturn(0);
3332552f7358SJed Brown }
3333552f7358SJed Brown 
3334552f7358SJed Brown /******************************** FEM Support **********************************/
3335552f7358SJed Brown 
33363194fc30SMatthew G. Knepley PetscErrorCode DMPlexCreateSpectralClosurePermutation(DM dm, PetscSection section)
33373194fc30SMatthew G. Knepley {
33383194fc30SMatthew G. Knepley   PetscInt      *perm;
333989eabcffSMatthew G. Knepley   PetscInt       dim, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
33403194fc30SMatthew G. Knepley   PetscErrorCode ierr;
33413194fc30SMatthew G. Knepley 
33423194fc30SMatthew G. Knepley   PetscFunctionBegin;
33433194fc30SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
33443194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
33453194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
334689eabcffSMatthew G. Knepley   if (dim <= 1) PetscFunctionReturn(0);
33473194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
33483194fc30SMatthew G. Knepley     /* An order k SEM disc has k-1 dofs on an edge */
33493194fc30SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
33503194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
33513194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
33523194fc30SMatthew G. Knepley     k = k/Nc + 1;
335389eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
33543194fc30SMatthew G. Knepley   }
33553194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
33563194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
335789eabcffSMatthew G. Knepley     switch (dim) {
335889eabcffSMatthew G. Knepley     case 2:
33593194fc30SMatthew 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} */
33603194fc30SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
33613194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
33623194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
33633194fc30SMatthew G. Knepley       k = k/Nc + 1;
33643194fc30SMatthew G. Knepley       /* The SEM order is
33653194fc30SMatthew G. Knepley 
33663194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
336789eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
33683194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
33693194fc30SMatthew G. Knepley       */
33703194fc30SMatthew G. Knepley       {
33713194fc30SMatthew G. Knepley         const PetscInt of   = 0;
33723194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
33733194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
33743194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
33753194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
33763194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
33773194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
33783194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
33793194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
33803194fc30SMatthew G. Knepley         PetscInt       o;
33813194fc30SMatthew G. Knepley 
33823194fc30SMatthew G. Knepley         /* bottom */
33833194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
33843194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
33853194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
33863194fc30SMatthew G. Knepley         /* middle */
33873194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
33883194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
33893194fc30SMatthew 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;
33903194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
33913194fc30SMatthew G. Knepley         }
33923194fc30SMatthew G. Knepley         /* top */
33933194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
33943194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
33953194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
33963194fc30SMatthew G. Knepley         foffset = offset;
33973194fc30SMatthew G. Knepley       }
339889eabcffSMatthew G. Knepley       break;
339989eabcffSMatthew G. Knepley     case 3:
340089eabcffSMatthew G. Knepley       /* The original hex closure is
340189eabcffSMatthew G. Knepley 
340289eabcffSMatthew G. Knepley          {c,
340389eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
340489eabcffSMatthew 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,
340589eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
340689eabcffSMatthew G. Knepley       */
340789eabcffSMatthew G. Knepley       ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
340889eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
340989eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
341089eabcffSMatthew G. Knepley       k = k/Nc + 1;
341189eabcffSMatthew G. Knepley       /* The SEM order is
341289eabcffSMatthew G. Knepley          Bottom Slice
341389eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
341489eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
341589eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
341689eabcffSMatthew G. Knepley 
341789eabcffSMatthew G. Knepley          Middle Slice (j)
341889eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
341989eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
342089eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
342189eabcffSMatthew G. Knepley 
342289eabcffSMatthew G. Knepley          Top Slice
342389eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
342489eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
342589eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
342689eabcffSMatthew G. Knepley       */
342789eabcffSMatthew G. Knepley       {
342889eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
342989eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
343089eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
343189eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
343289eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
343389eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
343489eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
343589eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
343689eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
343789eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
343889eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
343989eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
344089eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
344189eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
344289eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
344389eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
344489eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
344589eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
344689eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
344789eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
344889eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
344989eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
345089eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
345189eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
345289eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
345389eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
345489eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
345589eabcffSMatthew G. Knepley         PetscInt       o, n;
345689eabcffSMatthew G. Knepley 
345789eabcffSMatthew G. Knepley         /* Bottom Slice */
345889eabcffSMatthew G. Knepley         /*   bottom */
345989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
346089eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
346189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
346289eabcffSMatthew G. Knepley         /*   middle */
346389eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
346489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
3465316b7f87SMax 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;}
346689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
34673194fc30SMatthew G. Knepley         }
346889eabcffSMatthew G. Knepley         /*   top */
346989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
347089eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
347189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
347289eabcffSMatthew G. Knepley 
347389eabcffSMatthew G. Knepley         /* Middle Slice */
347489eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
347589eabcffSMatthew G. Knepley           /*   bottom */
347689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
347789eabcffSMatthew 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;
347889eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
347989eabcffSMatthew G. Knepley           /*   middle */
348089eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
348189eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
348289eabcffSMatthew 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;
348389eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
348489eabcffSMatthew G. Knepley           }
348589eabcffSMatthew G. Knepley           /*   top */
348689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
348789eabcffSMatthew 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;
348889eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
348989eabcffSMatthew G. Knepley         }
349089eabcffSMatthew G. Knepley 
349189eabcffSMatthew G. Knepley         /* Top Slice */
349289eabcffSMatthew G. Knepley         /*   bottom */
349389eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
349489eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
349589eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
349689eabcffSMatthew G. Knepley         /*   middle */
349789eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
349889eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
349989eabcffSMatthew 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;
350089eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
350189eabcffSMatthew G. Knepley         }
350289eabcffSMatthew G. Knepley         /*   top */
350389eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
350489eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
350589eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
350689eabcffSMatthew G. Knepley 
350789eabcffSMatthew G. Knepley         foffset = offset;
350889eabcffSMatthew G. Knepley       }
350989eabcffSMatthew G. Knepley       break;
351089eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
351189eabcffSMatthew G. Knepley     }
351289eabcffSMatthew G. Knepley   }
351389eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
35143194fc30SMatthew G. Knepley   /* Check permutation */
35153194fc30SMatthew G. Knepley   {
35163194fc30SMatthew G. Knepley     PetscInt *check;
35173194fc30SMatthew G. Knepley 
35183194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
35193194fc30SMatthew 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]);}
35203194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
35213194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
35223194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
35233194fc30SMatthew G. Knepley   }
35241fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
35253194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
35263194fc30SMatthew G. Knepley }
35273194fc30SMatthew G. Knepley 
3528e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
3529e071409bSToby Isaac {
3530e071409bSToby Isaac   PetscDS        prob;
3531e071409bSToby Isaac   PetscInt       depth, Nf, h;
3532e071409bSToby Isaac   DMLabel        label;
3533e071409bSToby Isaac   PetscErrorCode ierr;
3534e071409bSToby Isaac 
3535e071409bSToby Isaac   PetscFunctionBeginHot;
3536e071409bSToby Isaac   prob    = dm->prob;
3537e071409bSToby Isaac   Nf      = prob->Nf;
3538e071409bSToby Isaac   label   = dm->depthLabel;
3539e071409bSToby Isaac   *dspace = NULL;
3540e071409bSToby Isaac   if (field < Nf) {
3541e071409bSToby Isaac     PetscObject disc = prob->disc[field];
3542e071409bSToby Isaac 
3543e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
3544e071409bSToby Isaac       PetscDualSpace dsp;
3545e071409bSToby Isaac 
3546e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
3547e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
3548e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
3549e071409bSToby Isaac       h    = depth - 1 - h;
3550e071409bSToby Isaac       if (h) {
3551e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
3552e071409bSToby Isaac       } else {
3553e071409bSToby Isaac         *dspace = dsp;
3554e071409bSToby Isaac       }
3555e071409bSToby Isaac     }
3556e071409bSToby Isaac   }
3557e071409bSToby Isaac   PetscFunctionReturn(0);
3558e071409bSToby Isaac }
3559e071409bSToby Isaac 
3560e071409bSToby Isaac 
35611a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3562a6dfd86eSKarl Rupp {
3563552f7358SJed Brown   PetscScalar    *array, *vArray;
3564d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
35651a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
3566552f7358SJed Brown   PetscErrorCode  ierr;
3567552f7358SJed Brown 
35681b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
35692a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
35705a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
35715a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
35725a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
35733f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
35749df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
35759df71ca4SMatthew G. Knepley       PetscInt dof;
3576d9917b9dSMatthew G. Knepley 
35779df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
35789df71ca4SMatthew G. Knepley       size += dof;
35799df71ca4SMatthew G. Knepley     }
35809df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
35819df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
35822a3aaacfSMatthew G. Knepley       PetscInt       dof;
35835a1bb5cfSMatthew G. Knepley 
35845a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
35852a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
35865a1bb5cfSMatthew G. Knepley       size += dof;
35875a1bb5cfSMatthew G. Knepley     }
35883f7cbbe7SMatthew G. Knepley     if (!values) {
35893f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
35903f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
35913f7cbbe7SMatthew G. Knepley     }
35925a1bb5cfSMatthew G. Knepley     ierr = DMGetWorkArray(dm, size, PETSC_SCALAR, &array);CHKERRQ(ierr);
3593982e9ed1SMatthew G. Knepley   } else {
3594982e9ed1SMatthew G. Knepley     array = *values;
3595982e9ed1SMatthew G. Knepley   }
35969df71ca4SMatthew G. Knepley   size = 0;
35975a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
35989df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
35999df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
36009df71ca4SMatthew G. Knepley     PetscScalar *varr;
3601d9917b9dSMatthew G. Knepley 
36029df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
36039df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
36049df71ca4SMatthew G. Knepley     varr = &vArray[off];
36051a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
36061a271a75SMatthew G. Knepley       array[offset] = varr[d];
36079df71ca4SMatthew G. Knepley     }
36089df71ca4SMatthew G. Knepley     size += dof;
36099df71ca4SMatthew G. Knepley   }
36109df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
36119df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
36129df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
36135a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
36145a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
36155a1bb5cfSMatthew G. Knepley 
361652ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
36175a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
36185a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
36195a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
36205a1bb5cfSMatthew G. Knepley     if (o >= 0) {
36211a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
36221a271a75SMatthew G. Knepley         array[offset] = varr[d];
36235a1bb5cfSMatthew G. Knepley       }
36245a1bb5cfSMatthew G. Knepley     } else {
36251a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
36261a271a75SMatthew G. Knepley         array[offset] = varr[d];
36275a1bb5cfSMatthew G. Knepley       }
36285a1bb5cfSMatthew G. Knepley     }
36299df71ca4SMatthew G. Knepley     size += dof;
36305a1bb5cfSMatthew G. Knepley   }
36315a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
36329df71ca4SMatthew G. Knepley   if (!*values) {
36335a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
36345a1bb5cfSMatthew G. Knepley     *values = array;
36359df71ca4SMatthew G. Knepley   } else {
36368ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
36378c312ff3SMatthew G. Knepley     *csize = size;
36389df71ca4SMatthew G. Knepley   }
36395a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
36405a1bb5cfSMatthew G. Knepley }
3641d9917b9dSMatthew G. Knepley 
3642923c78e0SToby Isaac static PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3643923c78e0SToby Isaac {
3644923c78e0SToby Isaac   const PetscInt *cla;
3645923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
3646923c78e0SToby Isaac   PetscErrorCode ierr;
3647923c78e0SToby Isaac 
3648923c78e0SToby Isaac   PetscFunctionBeginHot;
3649923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
3650923c78e0SToby Isaac   if (!*clPoints) {
3651923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
3652923c78e0SToby Isaac 
3653923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3654923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
3655923c78e0SToby Isaac     /* Compress out points not in the section */
3656923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
3657923c78e0SToby Isaac       PetscInt r = pts[2*p];
3658923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
3659923c78e0SToby Isaac         pts[q*2]   = r;
3660923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
3661923c78e0SToby Isaac         ++q;
3662923c78e0SToby Isaac       }
3663923c78e0SToby Isaac     }
3664923c78e0SToby Isaac     np = q;
3665923c78e0SToby Isaac     cla = NULL;
3666923c78e0SToby Isaac   } else {
3667923c78e0SToby Isaac     PetscInt dof, off;
3668923c78e0SToby Isaac 
3669923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
3670923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
3671923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
3672923c78e0SToby Isaac     np   = dof/2;
3673923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
3674923c78e0SToby Isaac   }
3675923c78e0SToby Isaac   *numPoints = np;
3676923c78e0SToby Isaac   *points    = pts;
3677923c78e0SToby Isaac   *clp       = cla;
3678923c78e0SToby Isaac 
3679923c78e0SToby Isaac   PetscFunctionReturn(0);
3680923c78e0SToby Isaac }
3681923c78e0SToby Isaac 
3682923c78e0SToby Isaac static PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3683923c78e0SToby Isaac {
3684923c78e0SToby Isaac   PetscErrorCode ierr;
3685923c78e0SToby Isaac 
3686923c78e0SToby Isaac   PetscFunctionBeginHot;
3687923c78e0SToby Isaac   if (!*clPoints) {
3688923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
3689923c78e0SToby Isaac   } else {
3690923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
3691923c78e0SToby Isaac   }
3692923c78e0SToby Isaac   *numPoints = 0;
3693923c78e0SToby Isaac   *points    = NULL;
3694923c78e0SToby Isaac   *clSec     = NULL;
3695923c78e0SToby Isaac   *clPoints  = NULL;
3696923c78e0SToby Isaac   *clp       = NULL;
3697923c78e0SToby Isaac   PetscFunctionReturn(0);
3698923c78e0SToby Isaac }
3699923c78e0SToby Isaac 
370097e99dd9SToby 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[])
37011a271a75SMatthew G. Knepley {
37021a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
370397e99dd9SToby Isaac   const PetscInt    **perms = NULL;
370497e99dd9SToby Isaac   const PetscScalar **flips = NULL;
37051a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
37061a271a75SMatthew G. Knepley 
37071a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3708fe02ba77SJed Brown   *size = 0;
370997e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
371097e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
371197e99dd9SToby Isaac     const PetscInt    point = points[2*p];
371297e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
371397e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
37141a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
37151a271a75SMatthew G. Knepley     const PetscScalar *varr;
37161a271a75SMatthew G. Knepley 
37171a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
37181a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
37191a271a75SMatthew G. Knepley     varr = &vArray[off];
372097e99dd9SToby Isaac     if (clperm) {
372197e99dd9SToby Isaac       if (perm) {
372297e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
37231a271a75SMatthew G. Knepley       } else {
372497e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
372597e99dd9SToby Isaac       }
372697e99dd9SToby Isaac       if (flip) {
372797e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
372897e99dd9SToby Isaac       }
372997e99dd9SToby Isaac     } else {
373097e99dd9SToby Isaac       if (perm) {
373197e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
373297e99dd9SToby Isaac       } else {
373397e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
373497e99dd9SToby Isaac       }
373597e99dd9SToby Isaac       if (flip) {
373697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
37371a271a75SMatthew G. Knepley       }
37381a271a75SMatthew G. Knepley     }
373997e99dd9SToby Isaac     offset += dof;
374097e99dd9SToby Isaac   }
374197e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
37421a271a75SMatthew G. Knepley   *size = offset;
37431a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
37441a271a75SMatthew G. Knepley }
37451a271a75SMatthew G. Knepley 
374697e99dd9SToby 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[])
37471a271a75SMatthew G. Knepley {
37481a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
37491a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
37501a271a75SMatthew G. Knepley 
37511a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3752fe02ba77SJed Brown   *size = 0;
37531a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
375497e99dd9SToby Isaac     PetscInt          p;
375597e99dd9SToby Isaac     const PetscInt    **perms = NULL;
375697e99dd9SToby Isaac     const PetscScalar **flips = NULL;
37571a271a75SMatthew G. Knepley 
375897e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
375997e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
376097e99dd9SToby Isaac       const PetscInt    point = points[2*p];
376197e99dd9SToby Isaac       PetscInt          fdof, foff, b;
37621a271a75SMatthew G. Knepley       const PetscScalar *varr;
376397e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
376497e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
37651a271a75SMatthew G. Knepley 
37661a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
37671a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
37681a271a75SMatthew G. Knepley       varr = &vArray[foff];
376997e99dd9SToby Isaac       if (clperm) {
377097e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
377197e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
377297e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
37731a271a75SMatthew G. Knepley       } else {
377497e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
377597e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
377697e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
37771a271a75SMatthew G. Knepley       }
377897e99dd9SToby Isaac       offset += fdof;
37791a271a75SMatthew G. Knepley     }
378097e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
37811a271a75SMatthew G. Knepley   }
37821a271a75SMatthew G. Knepley   *size = offset;
37831a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
37841a271a75SMatthew G. Knepley }
37851a271a75SMatthew G. Knepley 
3786552f7358SJed Brown /*@C
3787552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
3788552f7358SJed Brown 
3789552f7358SJed Brown   Not collective
3790552f7358SJed Brown 
3791552f7358SJed Brown   Input Parameters:
3792552f7358SJed Brown + dm - The DM
3793552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
3794552f7358SJed Brown . v - The local vector
3795552f7358SJed Brown - point - The sieve point in the DM
3796552f7358SJed Brown 
3797552f7358SJed Brown   Output Parameters:
3798552f7358SJed Brown + csize - The number of values in the closure, or NULL
3799552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
3800552f7358SJed Brown 
3801552f7358SJed Brown   Fortran Notes:
3802552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
3803552f7358SJed Brown   include petsc.h90 in your code.
3804552f7358SJed Brown 
3805552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
3806552f7358SJed Brown 
3807552f7358SJed Brown   Level: intermediate
3808552f7358SJed Brown 
3809552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
3810552f7358SJed Brown @*/
3811552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3812552f7358SJed Brown {
3813552f7358SJed Brown   PetscSection       clSection;
3814d9917b9dSMatthew G. Knepley   IS                 clPoints;
38158a84db2dSMatthew G. Knepley   PetscScalar       *array;
38168a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
3817552f7358SJed Brown   PetscInt          *points = NULL;
38188f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
38191a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
3820552f7358SJed Brown   PetscErrorCode     ierr;
3821552f7358SJed Brown 
3822d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
3823552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3824552f7358SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
38251a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
38261a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
3827552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3828552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3829552f7358SJed Brown   if (depth == 1 && numFields < 2) {
38301a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
3831552f7358SJed Brown     PetscFunctionReturn(0);
3832552f7358SJed Brown   }
38331a271a75SMatthew G. Knepley   /* Get points */
3834923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
38351fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
38361a271a75SMatthew G. Knepley   /* Get array */
3837bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
38381a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
3839552f7358SJed Brown 
38401a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
3841552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
38421a271a75SMatthew G. Knepley       asize += dof;
3843552f7358SJed Brown     }
3844bd6c0d32SMatthew G. Knepley     if (!values) {
3845923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
38461a271a75SMatthew G. Knepley       if (csize) *csize = asize;
3847bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
3848bd6c0d32SMatthew G. Knepley     }
38491a271a75SMatthew G. Knepley     ierr = DMGetWorkArray(dm, asize, PETSC_SCALAR, &array);CHKERRQ(ierr);
3850d0f6b257SMatthew G. Knepley   } else {
3851d0f6b257SMatthew G. Knepley     array = *values;
3852d0f6b257SMatthew G. Knepley   }
38538a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
38541a271a75SMatthew G. Knepley   /* Get values */
385597e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
385697e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
38571a271a75SMatthew G. Knepley   /* Cleanup points */
3858923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
38591a271a75SMatthew G. Knepley   /* Cleanup array */
38608a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
3861d0f6b257SMatthew G. Knepley   if (!*values) {
3862552f7358SJed Brown     if (csize) *csize = size;
3863552f7358SJed Brown     *values = array;
3864d0f6b257SMatthew G. Knepley   } else {
3865f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
3866d0f6b257SMatthew G. Knepley     *csize = size;
3867d0f6b257SMatthew G. Knepley   }
3868552f7358SJed Brown   PetscFunctionReturn(0);
3869552f7358SJed Brown }
3870552f7358SJed Brown 
3871552f7358SJed Brown /*@C
3872552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
3873552f7358SJed Brown 
3874552f7358SJed Brown   Not collective
3875552f7358SJed Brown 
3876552f7358SJed Brown   Input Parameters:
3877552f7358SJed Brown + dm - The DM
38780298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
3879552f7358SJed Brown . v - The local vector
3880552f7358SJed Brown . point - The sieve point in the DM
38810298fd71SBarry Smith . csize - The number of values in the closure, or NULL
3882552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
3883552f7358SJed Brown 
38843813dfbdSMatthew G Knepley   Fortran Notes:
38853813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
38863813dfbdSMatthew G Knepley   include petsc.h90 in your code.
38873813dfbdSMatthew G Knepley 
38883813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
38893813dfbdSMatthew G Knepley 
3890552f7358SJed Brown   Level: intermediate
3891552f7358SJed Brown 
3892552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
3893552f7358SJed Brown @*/
38947c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3895a6dfd86eSKarl Rupp {
3896552f7358SJed Brown   PetscInt       size = 0;
3897552f7358SJed Brown   PetscErrorCode ierr;
3898552f7358SJed Brown 
3899552f7358SJed Brown   PetscFunctionBegin;
3900552f7358SJed Brown   /* Should work without recalculating size */
3901552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, size, PETSC_SCALAR, (void*) values);CHKERRQ(ierr);
3902552f7358SJed Brown   PetscFunctionReturn(0);
3903552f7358SJed Brown }
3904552f7358SJed Brown 
3905552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
3906552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
3907552f7358SJed Brown 
390897e99dd9SToby 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[])
3909552f7358SJed Brown {
3910552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
3911552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
3912552f7358SJed Brown   PetscScalar    *a;
3913552f7358SJed Brown   PetscInt        off, cind = 0, k;
3914552f7358SJed Brown   PetscErrorCode  ierr;
3915552f7358SJed Brown 
3916552f7358SJed Brown   PetscFunctionBegin;
3917552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
3918552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
3919552f7358SJed Brown   a    = &array[off];
3920552f7358SJed Brown   if (!cdof || setBC) {
392197e99dd9SToby Isaac     if (clperm) {
392297e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
392397e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
3924552f7358SJed Brown     } else {
392597e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
392697e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
3927552f7358SJed Brown     }
3928552f7358SJed Brown   } else {
3929552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
393097e99dd9SToby Isaac     if (clperm) {
393197e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
3932552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
393397e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
3934552f7358SJed Brown         }
3935552f7358SJed Brown       } else {
3936552f7358SJed Brown         for (k = 0; k < dof; ++k) {
3937552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
393897e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
393997e99dd9SToby Isaac         }
394097e99dd9SToby Isaac       }
394197e99dd9SToby Isaac     } else {
394297e99dd9SToby Isaac       if (perm) {
394397e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
394497e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
394597e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
394697e99dd9SToby Isaac         }
394797e99dd9SToby Isaac       } else {
394897e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
394997e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
395097e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
395197e99dd9SToby Isaac         }
3952552f7358SJed Brown       }
3953552f7358SJed Brown     }
3954552f7358SJed Brown   }
3955552f7358SJed Brown   PetscFunctionReturn(0);
3956552f7358SJed Brown }
3957552f7358SJed Brown 
395897e99dd9SToby 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[])
3959a5e93ea8SMatthew G. Knepley {
3960a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
3961a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
3962a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
3963a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
3964a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
3965a5e93ea8SMatthew G. Knepley 
3966a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
3967a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
3968a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
3969a5e93ea8SMatthew G. Knepley   a    = &array[off];
3970a5e93ea8SMatthew G. Knepley   if (cdof) {
3971a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
397297e99dd9SToby Isaac     if (clperm) {
397397e99dd9SToby Isaac       if (perm) {
3974a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
3975a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
397697e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
397797e99dd9SToby Isaac             cind++;
3978a5e93ea8SMatthew G. Knepley           }
3979a5e93ea8SMatthew G. Knepley         }
3980a5e93ea8SMatthew G. Knepley       } else {
3981a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
3982a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
398397e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
398497e99dd9SToby Isaac             cind++;
398597e99dd9SToby Isaac           }
398697e99dd9SToby Isaac         }
398797e99dd9SToby Isaac       }
398897e99dd9SToby Isaac     } else {
398997e99dd9SToby Isaac       if (perm) {
399097e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
399197e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
399297e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
399397e99dd9SToby Isaac             cind++;
399497e99dd9SToby Isaac           }
399597e99dd9SToby Isaac         }
399697e99dd9SToby Isaac       } else {
399797e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
399897e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
399997e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
400097e99dd9SToby Isaac             cind++;
400197e99dd9SToby Isaac           }
4002a5e93ea8SMatthew G. Knepley         }
4003a5e93ea8SMatthew G. Knepley       }
4004a5e93ea8SMatthew G. Knepley     }
4005a5e93ea8SMatthew G. Knepley   }
4006a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4007a5e93ea8SMatthew G. Knepley }
4008a5e93ea8SMatthew G. Knepley 
400997e99dd9SToby 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[])
4010a6dfd86eSKarl Rupp {
4011552f7358SJed Brown   PetscScalar    *a;
40121a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
40131a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
401497e99dd9SToby Isaac   PetscInt        cind = 0, b;
4015552f7358SJed Brown   PetscErrorCode  ierr;
4016552f7358SJed Brown 
4017552f7358SJed Brown   PetscFunctionBegin;
4018552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4019552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
40201a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
40211a271a75SMatthew G. Knepley   a    = &array[foff];
4022552f7358SJed Brown   if (!fcdof || setBC) {
402397e99dd9SToby Isaac     if (clperm) {
402497e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
402597e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4026552f7358SJed Brown     } else {
402797e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
402897e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4029552f7358SJed Brown     }
4030552f7358SJed Brown   } else {
4031552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
403297e99dd9SToby Isaac     if (clperm) {
403397e99dd9SToby Isaac       if (perm) {
403497e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
403597e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
403697e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4037552f7358SJed Brown         }
4038552f7358SJed Brown       } else {
403997e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
404097e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
404197e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
404297e99dd9SToby Isaac         }
404397e99dd9SToby Isaac       }
404497e99dd9SToby Isaac     } else {
404597e99dd9SToby Isaac       if (perm) {
404697e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
404797e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
404897e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
404997e99dd9SToby Isaac         }
405097e99dd9SToby Isaac       } else {
405197e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
405297e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
405397e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4054552f7358SJed Brown         }
4055552f7358SJed Brown       }
4056552f7358SJed Brown     }
4057552f7358SJed Brown   }
40581a271a75SMatthew G. Knepley   *offset += fdof;
4059552f7358SJed Brown   PetscFunctionReturn(0);
4060552f7358SJed Brown }
4061552f7358SJed Brown 
406297e99dd9SToby 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[])
4063a5e93ea8SMatthew G. Knepley {
4064a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
40651a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
40661a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
406797e99dd9SToby Isaac   PetscInt        cind = 0, b;
4068a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4069a5e93ea8SMatthew G. Knepley 
4070a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4071a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4072a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
40731a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
40741a271a75SMatthew G. Knepley   a    = &array[foff];
4075a5e93ea8SMatthew G. Knepley   if (fcdof) {
4076a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
407797e99dd9SToby Isaac     if (clperm) {
407897e99dd9SToby Isaac       if (perm) {
407997e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
408097e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
408197e99dd9SToby Isaac             fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4082a5e93ea8SMatthew G. Knepley             ++cind;
4083a5e93ea8SMatthew G. Knepley           }
4084a5e93ea8SMatthew G. Knepley         }
4085a5e93ea8SMatthew G. Knepley       } else {
408697e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
408797e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
408897e99dd9SToby Isaac             fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
408997e99dd9SToby Isaac             ++cind;
409097e99dd9SToby Isaac           }
409197e99dd9SToby Isaac         }
409297e99dd9SToby Isaac       }
409397e99dd9SToby Isaac     } else {
409497e99dd9SToby Isaac       if (perm) {
409597e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
409697e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
409797e99dd9SToby Isaac             fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
409897e99dd9SToby Isaac             ++cind;
409997e99dd9SToby Isaac           }
410097e99dd9SToby Isaac         }
410197e99dd9SToby Isaac       } else {
410297e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
410397e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
410497e99dd9SToby Isaac             fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4105a5e93ea8SMatthew G. Knepley             ++cind;
4106a5e93ea8SMatthew G. Knepley           }
4107a5e93ea8SMatthew G. Knepley         }
4108a5e93ea8SMatthew G. Knepley       }
4109a5e93ea8SMatthew G. Knepley     }
4110a5e93ea8SMatthew G. Knepley   }
41111a271a75SMatthew G. Knepley   *offset += fdof;
4112a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4113a5e93ea8SMatthew G. Knepley }
4114a5e93ea8SMatthew G. Knepley 
41158f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4116a6dfd86eSKarl Rupp {
4117552f7358SJed Brown   PetscScalar    *array;
41181b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
41191b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4120552f7358SJed Brown   PetscErrorCode  ierr;
4121552f7358SJed Brown 
41221b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
4123b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4124b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
4125b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4126b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
4127b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4128b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4129b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
4130b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
4131b6ebb6e6SMatthew G. Knepley 
4132b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4133b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
4134b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
4135b6ebb6e6SMatthew G. Knepley     {
4136b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4137b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
4138b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
4139b6ebb6e6SMatthew G. Knepley 
4140b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
4141b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
4142b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
4143b6ebb6e6SMatthew G. Knepley       if (!cdof) {
4144b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4145b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4146b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4147b6ebb6e6SMatthew G. Knepley           }
4148b6ebb6e6SMatthew G. Knepley         } else {
4149b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4150b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4151b6ebb6e6SMatthew G. Knepley           }
4152b6ebb6e6SMatthew G. Knepley         }
4153b6ebb6e6SMatthew G. Knepley       } else {
4154b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
4155b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4156b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4157b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4158b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4159b6ebb6e6SMatthew G. Knepley           }
4160b6ebb6e6SMatthew G. Knepley         } else {
4161b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4162b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4163b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4164b6ebb6e6SMatthew G. Knepley           }
4165b6ebb6e6SMatthew G. Knepley         }
4166b6ebb6e6SMatthew G. Knepley       }
4167b6ebb6e6SMatthew G. Knepley     }
4168b6ebb6e6SMatthew G. Knepley   }
4169b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4170b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4171b6ebb6e6SMatthew G. Knepley }
41721b406b76SMatthew G. Knepley 
41731b406b76SMatthew G. Knepley /*@C
41741b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
41751b406b76SMatthew G. Knepley 
41761b406b76SMatthew G. Knepley   Not collective
41771b406b76SMatthew G. Knepley 
41781b406b76SMatthew G. Knepley   Input Parameters:
41791b406b76SMatthew G. Knepley + dm - The DM
41801b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
41811b406b76SMatthew G. Knepley . v - The local vector
41821b406b76SMatthew G. Knepley . point - The sieve point in the DM
41831b406b76SMatthew G. Knepley . values - The array of values
41841b406b76SMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
41851b406b76SMatthew G. Knepley 
41861b406b76SMatthew G. Knepley   Fortran Notes:
41871b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
41881b406b76SMatthew G. Knepley 
41891b406b76SMatthew G. Knepley   Level: intermediate
41901b406b76SMatthew G. Knepley 
41911b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
41921b406b76SMatthew G. Knepley @*/
41931b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
41941b406b76SMatthew G. Knepley {
41951b406b76SMatthew G. Knepley   PetscSection    clSection;
41961b406b76SMatthew G. Knepley   IS              clPoints;
41971b406b76SMatthew G. Knepley   PetscScalar    *array;
41981b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
419997e99dd9SToby Isaac   const PetscInt *clp, *clperm;
42001a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
42011b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
42021b406b76SMatthew G. Knepley 
42031a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
42041b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42051b406b76SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
42061a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
42071a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
42081b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
42091b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
42101b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
42118f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
42121b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
42131b406b76SMatthew G. Knepley   }
42141a271a75SMatthew G. Knepley   /* Get points */
421597e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4216923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
42171a271a75SMatthew G. Knepley   /* Get array */
4218552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
42191a271a75SMatthew G. Knepley   /* Get values */
4220ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
422197e99dd9SToby Isaac     PetscInt offset = 0, f;
4222552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
422397e99dd9SToby Isaac       const PetscInt    **perms = NULL;
422497e99dd9SToby Isaac       const PetscScalar **flips = NULL;
422597e99dd9SToby Isaac 
422697e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4227552f7358SJed Brown       switch (mode) {
4228552f7358SJed Brown       case INSERT_VALUES:
422997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
423097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
423197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
423297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
423397e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4234552f7358SJed Brown         } break;
4235552f7358SJed Brown       case INSERT_ALL_VALUES:
423697e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
423797e99dd9SToby Isaac           const PetscInt    point = points[2*p];
423897e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
423997e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
424097e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4241552f7358SJed Brown         } break;
4242a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
424397e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
424497e99dd9SToby Isaac           const PetscInt    point = points[2*p];
424597e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
424697e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
424797e99dd9SToby Isaac           updatePointFieldsBC_private(section, point, perm, flip, f, insert, clperm, values, &offset, array);
4248a5e93ea8SMatthew G. Knepley         } break;
4249552f7358SJed Brown       case ADD_VALUES:
425097e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
425197e99dd9SToby Isaac           const PetscInt    point = points[2*p];
425297e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
425397e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
425497e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4255552f7358SJed Brown         } break;
4256552f7358SJed Brown       case ADD_ALL_VALUES:
425797e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
425897e99dd9SToby Isaac           const PetscInt    point = points[2*p];
425997e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
426097e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
426197e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4262552f7358SJed Brown         } break;
4263304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
426497e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
426597e99dd9SToby Isaac           const PetscInt    point = points[2*p];
426697e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
426797e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
426897e99dd9SToby Isaac           updatePointFieldsBC_private(section, point, perm, flip, f, add, clperm, values, &offset, array);
4269304ab55fSMatthew G. Knepley         } break;
4270552f7358SJed Brown       default:
4271f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4272552f7358SJed Brown       }
427397e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
42741a271a75SMatthew G. Knepley     }
4275552f7358SJed Brown   } else {
42761a271a75SMatthew G. Knepley     PetscInt dof, off;
427797e99dd9SToby Isaac     const PetscInt    **perms = NULL;
427897e99dd9SToby Isaac     const PetscScalar **flips = NULL;
42791a271a75SMatthew G. Knepley 
428097e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4281552f7358SJed Brown     switch (mode) {
4282552f7358SJed Brown     case INSERT_VALUES:
428397e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
428497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
428597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
428697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
428797e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
428897e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4289552f7358SJed Brown       } break;
4290552f7358SJed Brown     case INSERT_ALL_VALUES:
429197e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
429297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
429397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
429497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
429597e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
429697e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4297552f7358SJed Brown       } break;
4298a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
429997e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
430097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
430197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
430297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
430397e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
430497e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4305a5e93ea8SMatthew G. Knepley       } break;
4306552f7358SJed Brown     case ADD_VALUES:
430797e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
430897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
430997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
431097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
431197e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
431297e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4313552f7358SJed Brown       } break;
4314552f7358SJed Brown     case ADD_ALL_VALUES:
431597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
431697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
431797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
431897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
431997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
432097e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4321552f7358SJed Brown       } break;
4322304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
432397e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
432497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
432597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
432697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
432797e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
432897e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
4329304ab55fSMatthew G. Knepley       } break;
4330552f7358SJed Brown     default:
4331f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4332552f7358SJed Brown     }
433397e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4334552f7358SJed Brown   }
43351a271a75SMatthew G. Knepley   /* Cleanup points */
4336923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
43371a271a75SMatthew G. Knepley   /* Cleanup array */
4338552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4339552f7358SJed Brown   PetscFunctionReturn(0);
4340552f7358SJed Brown }
4341552f7358SJed Brown 
4342e07394fbSMatthew G. Knepley PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, const PetscScalar values[], InsertMode mode)
4343e07394fbSMatthew G. Knepley {
4344e07394fbSMatthew G. Knepley   PetscSection      clSection;
4345e07394fbSMatthew G. Knepley   IS                clPoints;
4346e07394fbSMatthew G. Knepley   PetscScalar       *array;
4347e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
4348b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
4349e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
435097e99dd9SToby Isaac   PetscInt          offset = 0, f;
4351e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
4352e07394fbSMatthew G. Knepley 
4353e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
4354e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4355e07394fbSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
4356e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4357e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4358e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4359e07394fbSMatthew G. Knepley   /* Get points */
4360b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4361923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4362e07394fbSMatthew G. Knepley   /* Get array */
4363e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4364e07394fbSMatthew G. Knepley   /* Get values */
4365e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
436697e99dd9SToby Isaac     const PetscInt    **perms = NULL;
436797e99dd9SToby Isaac     const PetscScalar **flips = NULL;
436897e99dd9SToby Isaac 
4369e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
4370e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4371e07394fbSMatthew G. Knepley         PetscInt fdof;
4372e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
4373e07394fbSMatthew G. Knepley         offset += fdof;
4374e07394fbSMatthew G. Knepley       }
4375e07394fbSMatthew G. Knepley       continue;
4376e07394fbSMatthew G. Knepley     }
437797e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4378e07394fbSMatthew G. Knepley     switch (mode) {
4379e07394fbSMatthew G. Knepley     case INSERT_VALUES:
438097e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
438197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
438297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
438397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4384b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4385e07394fbSMatthew G. Knepley       } break;
4386e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
438797e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
438897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
438997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
439097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4391b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4392e07394fbSMatthew G. Knepley         } break;
4393e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
439497e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
439597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
439697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
439797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4398b04c866fSMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, insert, clperm, values, &offset, array);
4399e07394fbSMatthew G. Knepley       } break;
4400e07394fbSMatthew G. Knepley     case ADD_VALUES:
440197e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
440297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
440397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
440497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4405b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4406e07394fbSMatthew G. Knepley       } break;
4407e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
440897e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
440997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
441097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
441197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4412b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4413e07394fbSMatthew G. Knepley       } break;
4414e07394fbSMatthew G. Knepley     default:
4415f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4416e07394fbSMatthew G. Knepley     }
441797e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4418e07394fbSMatthew G. Knepley   }
4419e07394fbSMatthew G. Knepley   /* Cleanup points */
4420923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4421e07394fbSMatthew G. Knepley   /* Cleanup array */
4422e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4423e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
4424e07394fbSMatthew G. Knepley }
4425e07394fbSMatthew G. Knepley 
4426b0ecff45SMatthew G. Knepley PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
4427552f7358SJed Brown {
4428552f7358SJed Brown   PetscMPIInt    rank;
4429552f7358SJed Brown   PetscInt       i, j;
4430552f7358SJed Brown   PetscErrorCode ierr;
4431552f7358SJed Brown 
4432552f7358SJed Brown   PetscFunctionBegin;
443382f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
4434f347f43bSBarry Smith   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for sieve point %D\n", rank, point);CHKERRQ(ierr);
4435e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
4436e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
4437b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
4438b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
4439e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
4440b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
4441519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
44427eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
4443552f7358SJed Brown #else
4444b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
4445552f7358SJed Brown #endif
4446552f7358SJed Brown     }
444777a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
4448552f7358SJed Brown   }
4449552f7358SJed Brown   PetscFunctionReturn(0);
4450552f7358SJed Brown }
4451552f7358SJed Brown 
4452552f7358SJed Brown /* . off - The global offset of this point */
44534acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], PetscInt indices[])
4454a6dfd86eSKarl Rupp {
4455e6ccafaeSMatthew G Knepley   PetscInt        dof;    /* The number of unknowns on this point */
4456552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4457552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4458552f7358SJed Brown   PetscInt        cind = 0, k;
4459552f7358SJed Brown   PetscErrorCode  ierr;
4460552f7358SJed Brown 
4461552f7358SJed Brown   PetscFunctionBegin;
4462552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
4463552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4464552f7358SJed Brown   if (!cdof || setBC) {
44654acb8e1eSToby Isaac     if (perm) {
44664acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+perm[k]] = off + k;
4467552f7358SJed Brown     } else {
44684acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+k] = off + k;
4469552f7358SJed Brown     }
4470552f7358SJed Brown   } else {
4471552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
44724acb8e1eSToby Isaac     if (perm) {
44734acb8e1eSToby Isaac       for (k = 0; k < dof; ++k) {
44744acb8e1eSToby Isaac         if ((cind < cdof) && (k == cdofs[cind])) {
44754acb8e1eSToby Isaac           /* Insert check for returning constrained indices */
44764acb8e1eSToby Isaac           indices[*loff+perm[k]] = -(off+k+1);
44774acb8e1eSToby Isaac           ++cind;
44784acb8e1eSToby Isaac         } else {
44794acb8e1eSToby Isaac           indices[*loff+perm[k]] = off+k-cind;
44804acb8e1eSToby Isaac         }
44814acb8e1eSToby Isaac       }
44824acb8e1eSToby Isaac     } else {
4483552f7358SJed Brown       for (k = 0; k < dof; ++k) {
4484552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
4485552f7358SJed Brown           /* Insert check for returning constrained indices */
4486e6ccafaeSMatthew G Knepley           indices[*loff+k] = -(off+k+1);
4487552f7358SJed Brown           ++cind;
4488552f7358SJed Brown         } else {
4489e6ccafaeSMatthew G Knepley           indices[*loff+k] = off+k-cind;
4490552f7358SJed Brown         }
4491552f7358SJed Brown       }
4492552f7358SJed Brown     }
4493552f7358SJed Brown   }
4494e6ccafaeSMatthew G Knepley   *loff += dof;
4495552f7358SJed Brown   PetscFunctionReturn(0);
4496552f7358SJed Brown }
4497552f7358SJed Brown 
4498552f7358SJed Brown /* . off - The global offset of this point */
44994acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, PetscInt indices[])
4500a6dfd86eSKarl Rupp {
4501552f7358SJed Brown   PetscInt       numFields, foff, f;
4502552f7358SJed Brown   PetscErrorCode ierr;
4503552f7358SJed Brown 
4504552f7358SJed Brown   PetscFunctionBegin;
4505552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4506552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
45074acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
4508552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
45094acb8e1eSToby Isaac     PetscInt        cind = 0, b;
45104acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
4511552f7358SJed Brown 
4512552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4513552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
4514552f7358SJed Brown     if (!cfdof || setBC) {
45154acb8e1eSToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {indices[foffs[f]+perm[b]] = off+foff+b;}}
45164acb8e1eSToby Isaac       else      {for (b = 0; b < fdof; b++) {indices[foffs[f]+     b ] = off+foff+b;}}
4517552f7358SJed Brown     } else {
4518552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
45194acb8e1eSToby Isaac       if (perm) {
45204acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
45214acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
45224acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = -(off+foff+b+1);
4523552f7358SJed Brown             ++cind;
4524552f7358SJed Brown           } else {
45254acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = off+foff+b-cind;
4526552f7358SJed Brown           }
4527552f7358SJed Brown         }
4528552f7358SJed Brown       } else {
45294acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
45304acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
45314acb8e1eSToby Isaac             indices[foffs[f]+b] = -(off+foff+b+1);
4532552f7358SJed Brown             ++cind;
4533552f7358SJed Brown           } else {
45344acb8e1eSToby Isaac             indices[foffs[f]+b] = off+foff+b-cind;
4535552f7358SJed Brown           }
4536552f7358SJed Brown         }
4537552f7358SJed Brown       }
4538552f7358SJed Brown     }
4539a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
4540552f7358SJed Brown     foffs[f] += fdof;
4541552f7358SJed Brown   }
4542552f7358SJed Brown   PetscFunctionReturn(0);
4543552f7358SJed Brown }
4544552f7358SJed Brown 
45454acb8e1eSToby 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)
4546d3d1a6afSToby Isaac {
4547d3d1a6afSToby Isaac   Mat             cMat;
4548d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
4549d3d1a6afSToby Isaac   IS              aIS;
4550d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
4551d3d1a6afSToby Isaac   const PetscInt  *anchors;
4552e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
4553d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
4554d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
4555d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
4556d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
4557d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
4558d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
4559d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
45606ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
4561d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
4562d3d1a6afSToby Isaac   PetscErrorCode  ierr;
4563d3d1a6afSToby Isaac 
4564d3d1a6afSToby Isaac   PetscFunctionBegin;
4565d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4566d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4567d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4568d3d1a6afSToby Isaac 
4569a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
4570d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
4571d3d1a6afSToby Isaac   if (aSec) {
4572d3d1a6afSToby Isaac     ierr = PetscMemzero(newOffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
4573d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
4574d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
4575d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
4576d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
4577d3d1a6afSToby Isaac      * into the global matrix anyway) */
4578d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
4579d3d1a6afSToby Isaac       PetscInt b    = points[p];
45804b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4581d3d1a6afSToby Isaac 
45824b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
45834b2f2278SToby Isaac       if (!bSecDof) {
45844b2f2278SToby Isaac         continue;
45854b2f2278SToby Isaac       }
4586d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4587d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
4588d3d1a6afSToby Isaac       }
4589d3d1a6afSToby Isaac       if (bDof) {
4590d3d1a6afSToby Isaac         /* this point is constrained */
4591d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
4592d3d1a6afSToby Isaac         PetscInt bOff, q;
4593d3d1a6afSToby Isaac 
4594d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
4595d3d1a6afSToby Isaac         newNumPoints  += bDof;
4596d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
4597d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4598d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
4599d3d1a6afSToby Isaac           PetscInt aDof;
4600d3d1a6afSToby Isaac 
4601d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
4602d3d1a6afSToby Isaac           newNumIndices += aDof;
4603d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
4604d3d1a6afSToby Isaac             PetscInt fDof;
4605d3d1a6afSToby Isaac 
4606d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
4607d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
4608d3d1a6afSToby Isaac           }
4609d3d1a6afSToby Isaac         }
4610d3d1a6afSToby Isaac       }
4611d3d1a6afSToby Isaac       else {
4612d3d1a6afSToby Isaac         /* this point is not constrained */
4613d3d1a6afSToby Isaac         newNumPoints++;
46144b2f2278SToby Isaac         newNumIndices += bSecDof;
4615d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
4616d3d1a6afSToby Isaac           PetscInt fDof;
4617d3d1a6afSToby Isaac 
4618d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4619d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
4620d3d1a6afSToby Isaac         }
4621d3d1a6afSToby Isaac       }
4622d3d1a6afSToby Isaac     }
4623d3d1a6afSToby Isaac   }
4624d3d1a6afSToby Isaac   if (!anyConstrained) {
462572b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
462672b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
462772b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
462872b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
462972b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
4630d3d1a6afSToby Isaac     PetscFunctionReturn(0);
4631d3d1a6afSToby Isaac   }
4632d3d1a6afSToby Isaac 
46336ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
46346ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
46356ecaa68aSToby Isaac 
4636f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
4637d3d1a6afSToby Isaac 
46386ecaa68aSToby Isaac   if (!outPoints && !outValues) {
46396ecaa68aSToby Isaac     if (offsets) {
46406ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
46416ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
46426ecaa68aSToby Isaac       }
46436ecaa68aSToby Isaac     }
46446ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
46456ecaa68aSToby Isaac     PetscFunctionReturn(0);
46466ecaa68aSToby Isaac   }
46476ecaa68aSToby Isaac 
4648f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
4649d3d1a6afSToby Isaac 
4650f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
4651d3d1a6afSToby Isaac 
4652d3d1a6afSToby Isaac   /* workspaces */
4653d3d1a6afSToby Isaac   if (numFields) {
4654d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
4655d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
4656d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
4657d3d1a6afSToby Isaac     }
4658d3d1a6afSToby Isaac   }
4659d3d1a6afSToby Isaac   else {
4660d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
4661d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
4662d3d1a6afSToby Isaac   }
4663d3d1a6afSToby Isaac 
4664d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
4665d3d1a6afSToby Isaac   if (numFields) {
46664b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
46674b2f2278SToby Isaac 
4668d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4669d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
46704b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4671d3d1a6afSToby Isaac 
46724b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
46734b2f2278SToby Isaac       if (!bSecDof) {
46744b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
46754b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
46764b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
46774b2f2278SToby Isaac         }
46784b2f2278SToby Isaac         continue;
46794b2f2278SToby Isaac       }
4680d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4681d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4682d3d1a6afSToby Isaac       }
4683d3d1a6afSToby Isaac       if (bDof) {
4684d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4685d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
4686d3d1a6afSToby Isaac 
4687d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4688d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4689d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
4690d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
4691d3d1a6afSToby Isaac             PetscInt aFDof;
4692d3d1a6afSToby Isaac 
4693d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
4694d3d1a6afSToby Isaac             allFDof += aFDof;
4695d3d1a6afSToby Isaac           }
4696d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
4697d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
4698d3d1a6afSToby Isaac         }
4699d3d1a6afSToby Isaac       }
4700d3d1a6afSToby Isaac       else {
4701d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4702d3d1a6afSToby Isaac           PetscInt fDof;
4703d3d1a6afSToby Isaac 
4704d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4705d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
4706d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
4707d3d1a6afSToby Isaac         }
4708d3d1a6afSToby Isaac       }
4709d3d1a6afSToby Isaac     }
47104b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
47114b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
47124b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
4713d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
4714d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
4715d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
4716d3d1a6afSToby Isaac       }
471719f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
471819f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
4719d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
4720d3d1a6afSToby Isaac     }
4721d3d1a6afSToby Isaac   }
4722d3d1a6afSToby Isaac   else {
4723d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4724d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
47254b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4726d3d1a6afSToby Isaac 
47274b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
47284b2f2278SToby Isaac       if (!bSecDof) {
47294b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
47304b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
47314b2f2278SToby Isaac         continue;
47324b2f2278SToby Isaac       }
4733d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4734d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4735d3d1a6afSToby Isaac       }
4736d3d1a6afSToby Isaac       if (bDof) {
47374b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
4738d3d1a6afSToby Isaac 
4739d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4740d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4741d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
4742d3d1a6afSToby Isaac 
4743d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
4744d3d1a6afSToby Isaac           allDof += aDof;
4745d3d1a6afSToby Isaac         }
4746d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
47474b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
4748d3d1a6afSToby Isaac       }
4749d3d1a6afSToby Isaac       else {
47504b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
4751d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
4752d3d1a6afSToby Isaac       }
4753d3d1a6afSToby Isaac     }
4754d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
4755d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
4756d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4757d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
4758d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
4759d3d1a6afSToby Isaac     }
4760d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
4761d3d1a6afSToby Isaac   }
4762d3d1a6afSToby Isaac 
47636ecaa68aSToby Isaac   /* output arrays */
47646ecaa68aSToby Isaac   ierr = DMGetWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
47656ecaa68aSToby Isaac 
4766d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
4767d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
4768d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
4769d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
4770d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
4771d3d1a6afSToby Isaac   if (numFields) {
4772d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
4773d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4774d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
47754b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4776d3d1a6afSToby Isaac 
47774b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
47784b2f2278SToby Isaac       if (!bSecDof) {
47794b2f2278SToby Isaac         continue;
47804b2f2278SToby Isaac       }
4781d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4782d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4783d3d1a6afSToby Isaac       }
4784d3d1a6afSToby Isaac       if (bDof) {
4785d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
4786d3d1a6afSToby Isaac 
4787d3d1a6afSToby Isaac         fStart[0] = 0;
4788d3d1a6afSToby Isaac         fEnd[0]   = 0;
4789d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4790d3d1a6afSToby Isaac           PetscInt fDof;
4791d3d1a6afSToby Isaac 
4792d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
4793d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
4794d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
4795d3d1a6afSToby Isaac         }
4796d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
47974acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, indices);CHKERRQ(ierr);
4798d3d1a6afSToby Isaac 
4799d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
4800d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
4801d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4802d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
4803d3d1a6afSToby Isaac 
4804d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
4805d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
4806d3d1a6afSToby Isaac         }
4807d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4808d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4809d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
4810d3d1a6afSToby Isaac 
4811d3d1a6afSToby 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 */
4812d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
4813d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
4814302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
48154acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, newIndices);CHKERRQ(ierr);
4816d3d1a6afSToby Isaac         }
4817d3d1a6afSToby Isaac         newP += bDof;
4818d3d1a6afSToby Isaac 
48196ecaa68aSToby Isaac         if (outValues) {
4820d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
4821d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
4822d3d1a6afSToby 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);
4823d3d1a6afSToby Isaac           }
4824d3d1a6afSToby Isaac         }
48256ecaa68aSToby Isaac       }
4826d3d1a6afSToby Isaac       else {
4827d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
4828d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
4829d3d1a6afSToby Isaac         newP++;
4830d3d1a6afSToby Isaac       }
4831d3d1a6afSToby Isaac     }
4832d3d1a6afSToby Isaac   } else {
4833d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4834d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4835d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
48364b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4837d3d1a6afSToby Isaac 
48384b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
48394b2f2278SToby Isaac       if (!bSecDof) {
48404b2f2278SToby Isaac         continue;
48414b2f2278SToby Isaac       }
4842d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4843d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4844d3d1a6afSToby Isaac       }
4845d3d1a6afSToby Isaac       if (bDof) {
4846d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
4847d3d1a6afSToby Isaac 
4848d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
48494acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, indices);CHKERRQ(ierr);
4850d3d1a6afSToby Isaac 
4851d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
4852d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4853d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
4854d3d1a6afSToby Isaac 
4855d3d1a6afSToby 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 */
4856d3d1a6afSToby Isaac 
4857d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
4858d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
4859302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
48604acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, newIndices);CHKERRQ(ierr);
4861d3d1a6afSToby Isaac         }
4862d3d1a6afSToby Isaac         newP += bDof;
4863d3d1a6afSToby Isaac 
4864d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
48656ecaa68aSToby Isaac         if (outValues) {
4866d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
4867d3d1a6afSToby Isaac         }
48686ecaa68aSToby Isaac       }
4869d3d1a6afSToby Isaac       else {
4870d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
4871d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
4872d3d1a6afSToby Isaac         newP++;
4873d3d1a6afSToby Isaac       }
4874d3d1a6afSToby Isaac     }
4875d3d1a6afSToby Isaac   }
4876d3d1a6afSToby Isaac 
48776ecaa68aSToby Isaac   if (outValues) {
48786ecaa68aSToby Isaac     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
4879d3d1a6afSToby Isaac     ierr = PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));CHKERRQ(ierr);
4880d3d1a6afSToby Isaac     /* multiply constraints on the right */
4881d3d1a6afSToby Isaac     if (numFields) {
4882d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
4883d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
4884d3d1a6afSToby Isaac 
4885d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
4886d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
4887d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
4888d3d1a6afSToby Isaac           PetscInt c, r, k;
4889d3d1a6afSToby Isaac           PetscInt dof;
4890d3d1a6afSToby Isaac 
4891d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
48924b2f2278SToby Isaac           if (!dof) {
48934b2f2278SToby Isaac             continue;
48944b2f2278SToby Isaac           }
4895d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
4896d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
4897d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
4898d3d1a6afSToby Isaac 
4899d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
4900d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
4901d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
49024acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
4903d3d1a6afSToby Isaac                 }
4904d3d1a6afSToby Isaac               }
4905d3d1a6afSToby Isaac             }
4906d3d1a6afSToby Isaac           }
4907d3d1a6afSToby Isaac           else {
4908d3d1a6afSToby Isaac             /* copy this column as is */
4909d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
4910d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
4911d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
4912d3d1a6afSToby Isaac               }
4913d3d1a6afSToby Isaac             }
4914d3d1a6afSToby Isaac           }
4915d3d1a6afSToby Isaac           oldOff += dof;
4916d3d1a6afSToby Isaac         }
4917d3d1a6afSToby Isaac       }
4918d3d1a6afSToby Isaac     }
4919d3d1a6afSToby Isaac     else {
4920d3d1a6afSToby Isaac       PetscInt oldOff = 0;
4921d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
4922d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
4923d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
4924d3d1a6afSToby Isaac         PetscInt c, r, k;
4925d3d1a6afSToby Isaac         PetscInt dof;
4926d3d1a6afSToby Isaac 
4927d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
49284b2f2278SToby Isaac         if (!dof) {
49294b2f2278SToby Isaac           continue;
49304b2f2278SToby Isaac         }
4931d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
4932d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
4933d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
4934d3d1a6afSToby Isaac 
4935d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
4936d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
4937d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
4938d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
4939d3d1a6afSToby Isaac               }
4940d3d1a6afSToby Isaac             }
4941d3d1a6afSToby Isaac           }
4942d3d1a6afSToby Isaac         }
4943d3d1a6afSToby Isaac         else {
4944d3d1a6afSToby Isaac           /* copy this column as is */
4945d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
4946d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
4947d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
4948d3d1a6afSToby Isaac             }
4949d3d1a6afSToby Isaac           }
4950d3d1a6afSToby Isaac         }
4951d3d1a6afSToby Isaac         oldOff += dof;
4952d3d1a6afSToby Isaac       }
4953d3d1a6afSToby Isaac     }
4954d3d1a6afSToby Isaac 
49556ecaa68aSToby Isaac     if (multiplyLeft) {
49566ecaa68aSToby Isaac       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
4957d3d1a6afSToby Isaac       ierr = PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));CHKERRQ(ierr);
4958d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
4959d3d1a6afSToby Isaac       if (numFields) {
4960d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4961d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
4962d3d1a6afSToby Isaac 
4963d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
4964d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
4965d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
4966d3d1a6afSToby Isaac             PetscInt c, r, k;
4967d3d1a6afSToby Isaac             PetscInt dof;
4968d3d1a6afSToby Isaac 
4969d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
4970d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
4971d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
4972d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
4973d3d1a6afSToby Isaac 
4974d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
4975d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
4976d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
4977d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
4978d3d1a6afSToby Isaac                   }
4979d3d1a6afSToby Isaac                 }
4980d3d1a6afSToby Isaac               }
4981d3d1a6afSToby Isaac             }
4982d3d1a6afSToby Isaac             else {
4983d3d1a6afSToby Isaac               /* copy this row as is */
4984d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
4985d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
4986d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
4987d3d1a6afSToby Isaac                 }
4988d3d1a6afSToby Isaac               }
4989d3d1a6afSToby Isaac             }
4990d3d1a6afSToby Isaac             oldOff += dof;
4991d3d1a6afSToby Isaac           }
4992d3d1a6afSToby Isaac         }
4993d3d1a6afSToby Isaac       }
4994d3d1a6afSToby Isaac       else {
4995d3d1a6afSToby Isaac         PetscInt oldOff = 0;
4996d3d1a6afSToby Isaac 
4997d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
4998d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
4999d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5000d3d1a6afSToby Isaac           PetscInt c, r, k;
5001d3d1a6afSToby Isaac           PetscInt dof;
5002d3d1a6afSToby Isaac 
5003d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
5004d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5005d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5006d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5007d3d1a6afSToby Isaac 
5008d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5009d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5010d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5011d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5012d3d1a6afSToby Isaac                 }
5013d3d1a6afSToby Isaac               }
5014d3d1a6afSToby Isaac             }
5015d3d1a6afSToby Isaac           }
5016d3d1a6afSToby Isaac           else {
5017d3d1a6afSToby Isaac             /* copy this row as is */
50189fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5019d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5020d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5021d3d1a6afSToby Isaac               }
5022d3d1a6afSToby Isaac             }
5023d3d1a6afSToby Isaac           }
5024d3d1a6afSToby Isaac           oldOff += dof;
5025d3d1a6afSToby Isaac         }
5026d3d1a6afSToby Isaac       }
5027d3d1a6afSToby Isaac 
50286ecaa68aSToby Isaac       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
50296ecaa68aSToby Isaac     }
50306ecaa68aSToby Isaac     else {
50316ecaa68aSToby Isaac       newValues = tmpValues;
50326ecaa68aSToby Isaac     }
50336ecaa68aSToby Isaac   }
50346ecaa68aSToby Isaac 
5035d3d1a6afSToby Isaac   /* clean up */
5036d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
5037d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
50386ecaa68aSToby Isaac 
5039d3d1a6afSToby Isaac   if (numFields) {
5040d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
5041d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5042d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
5043d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5044d3d1a6afSToby Isaac     }
5045d3d1a6afSToby Isaac   }
5046d3d1a6afSToby Isaac   else {
5047d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5048d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
5049d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5050d3d1a6afSToby Isaac   }
5051d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5052d3d1a6afSToby Isaac 
5053d3d1a6afSToby Isaac   /* output */
50546ecaa68aSToby Isaac   if (outPoints) {
5055d3d1a6afSToby Isaac     *outPoints = newPoints;
50566ecaa68aSToby Isaac   }
50576ecaa68aSToby Isaac   else {
50586ecaa68aSToby Isaac     ierr = DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
50596ecaa68aSToby Isaac   }
506031620726SToby Isaac   if (outValues) {
5061d3d1a6afSToby Isaac     *outValues = newValues;
50626ecaa68aSToby Isaac   }
50636ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
5064d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
5065d3d1a6afSToby Isaac   }
5066d3d1a6afSToby Isaac   PetscFunctionReturn(0);
5067d3d1a6afSToby Isaac }
5068d3d1a6afSToby Isaac 
50696ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
50707773e69fSMatthew G. Knepley {
50717773e69fSMatthew G. Knepley   PetscSection    clSection;
50727773e69fSMatthew G. Knepley   IS              clPoints;
50737773e69fSMatthew G. Knepley   const PetscInt *clp;
50744acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
50757773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
50767773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
50777773e69fSMatthew G. Knepley   PetscInt        offsets[32];
50787773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
50797773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
50807773e69fSMatthew G. Knepley 
50817773e69fSMatthew G. Knepley   PetscFunctionBegin;
50827773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50837773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
50847773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
50857773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
50867773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
50877773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
50887773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
50897773e69fSMatthew G. Knepley   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
50907773e69fSMatthew G. Knepley   /* Get points in closure */
5091923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
50927773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
50937773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
50947773e69fSMatthew G. Knepley     PetscInt dof, fdof;
50957773e69fSMatthew G. Knepley 
50967773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
50977773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
50987773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
50997773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
51007773e69fSMatthew G. Knepley     }
51017773e69fSMatthew G. Knepley     Nind += dof;
51027773e69fSMatthew G. Knepley   }
51037773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
51048ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
51054acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
51064acb8e1eSToby Isaac   /* Get dual space symmetries */
51074acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
51084acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
51094acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
51104acb8e1eSToby Isaac   }
51117773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
51127773e69fSMatthew G. Knepley   {
51134acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
51147773e69fSMatthew G. Knepley     if (numPointsNew) {
51154acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
51164acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
51174acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
51184acb8e1eSToby Isaac       }
51194acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
51204acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
51214acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
51224acb8e1eSToby Isaac       }
5123923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
51247773e69fSMatthew G. Knepley       numPoints = numPointsNew;
51257773e69fSMatthew G. Knepley       Nind      = NindNew;
51267773e69fSMatthew G. Knepley       points    = pointsNew;
51277773e69fSMatthew G. Knepley     }
51287773e69fSMatthew G. Knepley   }
51297773e69fSMatthew G. Knepley   /* Calculate indices */
51307773e69fSMatthew G. Knepley   ierr = DMGetWorkArray(dm, Nind, PETSC_INT, indices);CHKERRQ(ierr);
51317773e69fSMatthew G. Knepley   if (Nf) {
51326ecaa68aSToby Isaac     if (outOffsets) {
51336ecaa68aSToby Isaac       PetscInt f;
51346ecaa68aSToby Isaac 
513546bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
51366ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
51376ecaa68aSToby Isaac       }
51386ecaa68aSToby Isaac     }
51394acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
51404acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
51414acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, *indices);
51427773e69fSMatthew G. Knepley     }
51437773e69fSMatthew G. Knepley   } else {
51444acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
51454acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
51464acb8e1eSToby Isaac 
51474acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
51484acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, *indices);
51497773e69fSMatthew G. Knepley     }
51507773e69fSMatthew G. Knepley   }
51517773e69fSMatthew G. Knepley   /* Cleanup points */
51524acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
51534acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
51544acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
51554acb8e1eSToby Isaac   }
51567773e69fSMatthew G. Knepley   if (numPointsNew) {
51577773e69fSMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, PETSC_INT, &pointsNew);CHKERRQ(ierr);
51587773e69fSMatthew G. Knepley   } else {
5159923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
51607773e69fSMatthew G. Knepley   }
51617773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
51627773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
51637773e69fSMatthew G. Knepley }
51647773e69fSMatthew G. Knepley 
516546bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
51667773e69fSMatthew G. Knepley {
51677773e69fSMatthew G. Knepley   PetscErrorCode ierr;
51687773e69fSMatthew G. Knepley 
51697773e69fSMatthew G. Knepley   PetscFunctionBegin;
51707773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51717773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
51727773e69fSMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, indices);CHKERRQ(ierr);
51737773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
51747773e69fSMatthew G. Knepley }
51757773e69fSMatthew G. Knepley 
51767f5d1fdeSMatthew G. Knepley /*@C
51777f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
51787f5d1fdeSMatthew G. Knepley 
51797f5d1fdeSMatthew G. Knepley   Not collective
51807f5d1fdeSMatthew G. Knepley 
51817f5d1fdeSMatthew G. Knepley   Input Parameters:
51827f5d1fdeSMatthew G. Knepley + dm - The DM
5183ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5184ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
51857f5d1fdeSMatthew G. Knepley . A - The matrix
51867f5d1fdeSMatthew G. Knepley . point - The sieve point in the DM
51877f5d1fdeSMatthew G. Knepley . values - The array of values
51887f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
51897f5d1fdeSMatthew G. Knepley 
51907f5d1fdeSMatthew G. Knepley   Fortran Notes:
51917f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
51927f5d1fdeSMatthew G. Knepley 
51937f5d1fdeSMatthew G. Knepley   Level: intermediate
51947f5d1fdeSMatthew G. Knepley 
51957f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
51967f5d1fdeSMatthew G. Knepley @*/
51977c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5198552f7358SJed Brown {
5199552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
52001b406b76SMatthew G. Knepley   PetscSection        clSection;
52011b406b76SMatthew G. Knepley   IS                  clPoints;
5202d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
52031b406b76SMatthew G. Knepley   const PetscInt     *clp;
5204552f7358SJed Brown   PetscInt           *indices;
5205552f7358SJed Brown   PetscInt            offsets[32];
52064acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
52074acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
5208923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
52094acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
5210d3d1a6afSToby Isaac   PetscScalar        *newValues;
5211552f7358SJed Brown   PetscErrorCode      ierr;
5212552f7358SJed Brown 
5213552f7358SJed Brown   PetscFunctionBegin;
5214552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5215ebd6d717SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
52163dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5217ebd6d717SJed Brown   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
52183dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
52193dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5220552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
522182f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5222552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5223923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5224552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5225552f7358SJed Brown     PetscInt fdof;
5226552f7358SJed Brown 
5227552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5228552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5229552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5230552f7358SJed Brown       offsets[f+1] += fdof;
5231552f7358SJed Brown     }
5232552f7358SJed Brown     numIndices += dof;
5233552f7358SJed Brown   }
52340d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
52350d644c17SKarl Rupp 
52368ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
52374acb8e1eSToby Isaac   /* Get symmetries */
52384acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
52394acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
52404acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
52414acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
52424acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
52434acb8e1eSToby Isaac 
52444acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
52454acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
52464acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
52474acb8e1eSToby Isaac 
52484acb8e1eSToby Isaac         if (!numFields) {
52494acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
52504acb8e1eSToby Isaac         } else {
52514acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
52524acb8e1eSToby Isaac         }
52534acb8e1eSToby Isaac         if (flip) {
52544acb8e1eSToby Isaac           PetscInt i, j, k;
52554acb8e1eSToby Isaac 
52564acb8e1eSToby Isaac           if (!valCopy) {
52574acb8e1eSToby Isaac             ierr = DMGetWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
52584acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
52594acb8e1eSToby Isaac             values = valCopy;
52604acb8e1eSToby Isaac           }
52614acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
5262775f7be2SToby Isaac             PetscScalar fval = flip[i];
52634acb8e1eSToby Isaac 
52644acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
52654acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
52664acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
52674acb8e1eSToby Isaac             }
52684acb8e1eSToby Isaac           }
52694acb8e1eSToby Isaac         }
52704acb8e1eSToby Isaac         foffset += fdof;
52714acb8e1eSToby Isaac       }
52724acb8e1eSToby Isaac     }
52734acb8e1eSToby Isaac   }
52744acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
5275d3d1a6afSToby Isaac   if (newNumPoints) {
52764acb8e1eSToby Isaac     if (valCopy) {
52774acb8e1eSToby Isaac       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
52784acb8e1eSToby Isaac     }
52794acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
52804acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
52814acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
52824acb8e1eSToby Isaac     }
52834acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
52844acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
52854acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
52864acb8e1eSToby Isaac     }
5287923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5288d3d1a6afSToby Isaac     numPoints  = newNumPoints;
5289d3d1a6afSToby Isaac     numIndices = newNumIndices;
5290d3d1a6afSToby Isaac     points     = newPoints;
5291d3d1a6afSToby Isaac     values     = newValues;
5292d3d1a6afSToby Isaac   }
5293552f7358SJed Brown   ierr = DMGetWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5294552f7358SJed Brown   if (numFields) {
52954acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
52964acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
52974acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, indices);
5298552f7358SJed Brown     }
5299552f7358SJed Brown   } else {
53004acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
53014acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
53024acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
53034acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, indices);
5304552f7358SJed Brown     }
5305552f7358SJed Brown   }
5306b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
5307552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
5308ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
5309ab1d0545SMatthew G. Knepley     PetscInt i;
5310ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
53118ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
5312ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5313ab1d0545SMatthew G. Knepley   }
5314552f7358SJed Brown   if (ierr) {
5315552f7358SJed Brown     PetscMPIInt    rank;
5316552f7358SJed Brown     PetscErrorCode ierr2;
5317552f7358SJed Brown 
531882f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5319e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5320b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
53216a415b8fSMatthew G Knepley     ierr2 = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr2);
5322552f7358SJed Brown     CHKERRQ(ierr);
5323552f7358SJed Brown   }
53244acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
53254acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
53264acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
53274acb8e1eSToby Isaac   }
5328d3d1a6afSToby Isaac   if (newNumPoints) {
5329d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
5330d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
5331d3d1a6afSToby Isaac   }
5332d3d1a6afSToby Isaac   else {
53334acb8e1eSToby Isaac     if (valCopy) {
53344acb8e1eSToby Isaac       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
53354acb8e1eSToby Isaac     }
5336923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5337d3d1a6afSToby Isaac   }
5338552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5339552f7358SJed Brown   PetscFunctionReturn(0);
5340552f7358SJed Brown }
5341552f7358SJed Brown 
5342de41b84cSMatthew 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)
5343de41b84cSMatthew G. Knepley {
5344de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
5345de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
5346de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
5347de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
5348de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
5349de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
53504ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
5351de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
5352de41b84cSMatthew G. Knepley 
5353de41b84cSMatthew G. Knepley   PetscFunctionBegin;
5354de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
5355de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
5356de41b84cSMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
5357de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
5358de41b84cSMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
5359de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
5360de41b84cSMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
5361de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
5362de41b84cSMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
5363de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
5364de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
5365de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
5366de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5367de41b84cSMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5368de41b84cSMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5369de41b84cSMatthew G. Knepley   /* Column indices */
5370de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
53714ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
5372de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
5373de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
5374de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
5375de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
5376de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
5377de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
5378de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
5379de41b84cSMatthew G. Knepley       ++q;
5380de41b84cSMatthew G. Knepley     }
5381de41b84cSMatthew G. Knepley   }
5382de41b84cSMatthew G. Knepley   numCPoints = q;
5383de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
5384de41b84cSMatthew G. Knepley     PetscInt fdof;
5385de41b84cSMatthew G. Knepley 
5386de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
53874ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5388de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5389de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
5390de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
5391de41b84cSMatthew G. Knepley     }
5392de41b84cSMatthew G. Knepley     numCIndices += dof;
5393de41b84cSMatthew G. Knepley   }
5394de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
5395de41b84cSMatthew G. Knepley   /* Row indices */
5396de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
5397de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
53981ba5f902SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
5399de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
5400de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
5401de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5402de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
5403de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
5404de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
5405de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
54064ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
54074ca5e9f5SMatthew G. Knepley         if (!dof) continue;
54084ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
54094ca5e9f5SMatthew G. Knepley         if (s < q) continue;
5410de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
5411de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
5412de41b84cSMatthew G. Knepley         ++q;
5413de41b84cSMatthew G. Knepley       }
5414de41b84cSMatthew G. Knepley     }
5415de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5416de41b84cSMatthew G. Knepley   }
5417de41b84cSMatthew G. Knepley   numFPoints = q;
5418de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
5419de41b84cSMatthew G. Knepley     PetscInt fdof;
5420de41b84cSMatthew G. Knepley 
5421de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
54224ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5423de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5424de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
5425de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
5426de41b84cSMatthew G. Knepley     }
5427de41b84cSMatthew G. Knepley     numFIndices += dof;
5428de41b84cSMatthew G. Knepley   }
5429de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
5430de41b84cSMatthew G. Knepley 
54318ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
54328ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
5433de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
5434de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
5435de41b84cSMatthew G. Knepley   if (numFields) {
54364acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
54374acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
54384acb8e1eSToby Isaac 
54394acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
54404acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
54414acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5442de41b84cSMatthew G. Knepley     }
54434acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
54444acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
54454acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
54464acb8e1eSToby Isaac     }
54474acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
54484acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
54494acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
54504acb8e1eSToby Isaac     }
54514acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
54524acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
54534acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5454de41b84cSMatthew G. Knepley     }
5455de41b84cSMatthew G. Knepley   } else {
54564acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
54574acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
54584acb8e1eSToby Isaac 
54594acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
54604acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
54614acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
54624acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
54634acb8e1eSToby Isaac 
54644acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
54654acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);CHKERRQ(ierr);
5466de41b84cSMatthew G. Knepley     }
54674acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
54684acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
54694acb8e1eSToby Isaac 
54704acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
54714acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);CHKERRQ(ierr);
5472de41b84cSMatthew G. Knepley     }
54734acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
54744acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
5475de41b84cSMatthew G. Knepley   }
5476de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
54774acb8e1eSToby Isaac   /* TODO: flips */
5478de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
5479de41b84cSMatthew G. Knepley   if (ierr) {
5480de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
5481de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
5482de41b84cSMatthew G. Knepley 
5483de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5484e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5485de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
5486de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr2);
5487de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr2);
5488de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
5489de41b84cSMatthew G. Knepley   }
5490549a8adaSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
5491de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
5492de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
5493de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
5494de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
5495de41b84cSMatthew G. Knepley }
5496de41b84cSMatthew G. Knepley 
54977c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
54987c927364SMatthew G. Knepley {
54997c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
55007c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
55017c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
55027c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
55037c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
55047c927364SMatthew G. Knepley   PetscErrorCode ierr;
55057c927364SMatthew G. Knepley 
55067c927364SMatthew G. Knepley   PetscFunctionBegin;
55077c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
55087c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
55097c927364SMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
55107c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
55117c927364SMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
55127c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
55137c927364SMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
55147c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
55157c927364SMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
55167c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
55177c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
55187c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
55197c927364SMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
55207c927364SMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
55217c927364SMatthew G. Knepley   /* Column indices */
55227c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
55237c927364SMatthew G. Knepley   maxFPoints = numCPoints;
55247c927364SMatthew G. Knepley   /* Compress out points not in the section */
55257c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
55267c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
55277c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
55287c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
55297c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
55307c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
55317c927364SMatthew G. Knepley       ++q;
55327c927364SMatthew G. Knepley     }
55337c927364SMatthew G. Knepley   }
55347c927364SMatthew G. Knepley   numCPoints = q;
55357c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
55367c927364SMatthew G. Knepley     PetscInt fdof;
55377c927364SMatthew G. Knepley 
55387c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
55397c927364SMatthew G. Knepley     if (!dof) continue;
55407c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
55417c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
55427c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
55437c927364SMatthew G. Knepley     }
55447c927364SMatthew G. Knepley     numCIndices += dof;
55457c927364SMatthew G. Knepley   }
55467c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
55477c927364SMatthew G. Knepley   /* Row indices */
55487c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
55497c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
55507c927364SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
55517c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
55527c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
55537c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
55547c927364SMatthew G. Knepley     /* Compress out points not in the section */
55557c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
55567c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
55577c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
55587c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
55597c927364SMatthew G. Knepley         if (!dof) continue;
55607c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
55617c927364SMatthew G. Knepley         if (s < q) continue;
55627c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
55637c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
55647c927364SMatthew G. Knepley         ++q;
55657c927364SMatthew G. Knepley       }
55667c927364SMatthew G. Knepley     }
55677c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
55687c927364SMatthew G. Knepley   }
55697c927364SMatthew G. Knepley   numFPoints = q;
55707c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
55717c927364SMatthew G. Knepley     PetscInt fdof;
55727c927364SMatthew G. Knepley 
55737c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
55747c927364SMatthew G. Knepley     if (!dof) continue;
55757c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
55767c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
55777c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
55787c927364SMatthew G. Knepley     }
55797c927364SMatthew G. Knepley     numFIndices += dof;
55807c927364SMatthew G. Knepley   }
55817c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
55827c927364SMatthew G. Knepley 
55838ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
55848ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
55857c927364SMatthew G. Knepley   if (numFields) {
55864acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
55874acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
55884acb8e1eSToby Isaac 
55894acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
55904acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
55914acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
55927c927364SMatthew G. Knepley     }
55934acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
55944acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
55954acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
55964acb8e1eSToby Isaac     }
55974acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
55984acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
55994acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
56004acb8e1eSToby Isaac     }
56014acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
56024acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
56034acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
56047c927364SMatthew G. Knepley     }
56057c927364SMatthew G. Knepley   } else {
56064acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
56074acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
56084acb8e1eSToby Isaac 
56094acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
56104acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
56114acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
56124acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
56134acb8e1eSToby Isaac 
56144acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
56154acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);
56167c927364SMatthew G. Knepley     }
56174acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
56184acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
56194acb8e1eSToby Isaac 
56204acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
56214acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);
56227c927364SMatthew G. Knepley     }
56234acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
56244acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
56257c927364SMatthew G. Knepley   }
56267c927364SMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
56277c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
56287c927364SMatthew G. Knepley   PetscFunctionReturn(0);
56297c927364SMatthew G. Knepley }
56307c927364SMatthew G. Knepley 
5631f96281f8SMatthew G. Knepley /*@
5632f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
5633f96281f8SMatthew G. Knepley 
5634f96281f8SMatthew G. Knepley   Input Parameter:
5635f96281f8SMatthew G. Knepley . dm - The DMPlex object
5636f96281f8SMatthew G. Knepley 
5637f96281f8SMatthew G. Knepley   Output Parameters:
5638f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
5639dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5640dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5641dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5642f96281f8SMatthew G. Knepley 
5643f96281f8SMatthew G. Knepley   Level: developer
5644f96281f8SMatthew G. Knepley 
5645dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
5646f96281f8SMatthew G. Knepley @*/
5647770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
5648552f7358SJed Brown {
5649552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5650770b213bSMatthew G Knepley   PetscInt       dim;
5651770b213bSMatthew G Knepley   PetscErrorCode ierr;
5652552f7358SJed Brown 
5653552f7358SJed Brown   PetscFunctionBegin;
5654552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5655c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5656770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
5657770b213bSMatthew G Knepley   if (fMax) *fMax = mesh->hybridPointMax[dim-1];
5658770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
5659770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
5660552f7358SJed Brown   PetscFunctionReturn(0);
5661552f7358SJed Brown }
5662552f7358SJed Brown 
5663dc5a3409SMatthew G. Knepley /*@
5664dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
5665dc5a3409SMatthew G. Knepley 
5666dc5a3409SMatthew G. Knepley   Input Parameters:
5667dc5a3409SMatthew G. Knepley . dm   - The DMPlex object
5668dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
5669dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5670dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5671dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5672dc5a3409SMatthew G. Knepley 
5673dc5a3409SMatthew G. Knepley   Level: developer
5674dc5a3409SMatthew G. Knepley 
5675dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
5676dc5a3409SMatthew G. Knepley @*/
5677770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
5678552f7358SJed Brown {
5679552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5680770b213bSMatthew G Knepley   PetscInt       dim;
5681770b213bSMatthew G Knepley   PetscErrorCode ierr;
5682552f7358SJed Brown 
5683552f7358SJed Brown   PetscFunctionBegin;
5684552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5685c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5686770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]   = cMax;
5687770b213bSMatthew G Knepley   if (fMax >= 0) mesh->hybridPointMax[dim-1] = fMax;
5688770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]     = eMax;
5689770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]     = vMax;
5690552f7358SJed Brown   PetscFunctionReturn(0);
5691552f7358SJed Brown }
5692552f7358SJed Brown 
5693552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
5694552f7358SJed Brown {
5695552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5696552f7358SJed Brown 
5697552f7358SJed Brown   PetscFunctionBegin;
5698552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5699552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
5700552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
5701552f7358SJed Brown   PetscFunctionReturn(0);
5702552f7358SJed Brown }
5703552f7358SJed Brown 
5704552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
5705552f7358SJed Brown {
5706552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5707552f7358SJed Brown 
5708552f7358SJed Brown   PetscFunctionBegin;
5709552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5710552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
5711552f7358SJed Brown   PetscFunctionReturn(0);
5712552f7358SJed Brown }
5713552f7358SJed Brown 
5714552f7358SJed Brown /* We can easily have a form that takes an IS instead */
5715ef48cebcSMatthew G. Knepley static PetscErrorCode DMPlexCreateNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
5716552f7358SJed Brown {
5717552f7358SJed Brown   PetscSection   section, globalSection;
5718552f7358SJed Brown   PetscInt      *numbers, p;
5719552f7358SJed Brown   PetscErrorCode ierr;
5720552f7358SJed Brown 
5721552f7358SJed Brown   PetscFunctionBegin;
572282f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
5723552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
5724552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5725552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
5726552f7358SJed Brown   }
5727552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
572815b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
5729854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
5730552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5731552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
5732ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
5733ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
5734552f7358SJed Brown   }
573582f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
5736ef48cebcSMatthew G. Knepley   if (globalSize) {
5737ef48cebcSMatthew G. Knepley     PetscLayout layout;
5738ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
5739ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
5740ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
5741ef48cebcSMatthew G. Knepley   }
5742552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
5743552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
5744552f7358SJed Brown   PetscFunctionReturn(0);
5745552f7358SJed Brown }
5746552f7358SJed Brown 
574781ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
5748552f7358SJed Brown {
5749552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
5750552f7358SJed Brown   PetscErrorCode ierr;
5751552f7358SJed Brown 
5752552f7358SJed Brown   PetscFunctionBegin;
5753552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
5754552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
57550298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
575681ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
575781ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
575881ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
5759552f7358SJed Brown }
576081ed3555SMatthew G. Knepley 
576181ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
576281ed3555SMatthew G. Knepley {
576381ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
576481ed3555SMatthew G. Knepley   PetscErrorCode ierr;
576581ed3555SMatthew G. Knepley 
576681ed3555SMatthew G. Knepley   PetscFunctionBegin;
576781ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
576881ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
5769552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
5770552f7358SJed Brown   PetscFunctionReturn(0);
5771552f7358SJed Brown }
5772552f7358SJed Brown 
577381ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
577481ed3555SMatthew G. Knepley {
577581ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
577681ed3555SMatthew G. Knepley   PetscErrorCode ierr;
577781ed3555SMatthew G. Knepley 
577881ed3555SMatthew G. Knepley   PetscFunctionBegin;
577981ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
578081ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
578181ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
578281ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
578381ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
578481ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
578581ed3555SMatthew G. Knepley }
578681ed3555SMatthew G. Knepley 
5787552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
5788552f7358SJed Brown {
5789552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5790552f7358SJed Brown   PetscErrorCode ierr;
5791552f7358SJed Brown 
5792552f7358SJed Brown   PetscFunctionBegin;
5793552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
579481ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
5795552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
5796552f7358SJed Brown   PetscFunctionReturn(0);
5797552f7358SJed Brown }
5798552f7358SJed Brown 
5799ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
5800ef48cebcSMatthew G. Knepley {
5801ef48cebcSMatthew G. Knepley   IS             nums[4];
5802ef48cebcSMatthew G. Knepley   PetscInt       depths[4];
5803ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
5804ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
5805ef48cebcSMatthew G. Knepley 
5806ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
5807ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5808ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
58098abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
58108abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
5811ef48cebcSMatthew G. Knepley   depths[0] = depth; depths[1] = 0;
5812ef48cebcSMatthew G. Knepley   for (d = 2; d <= depth; ++d) depths[d] = depth-d+1;
5813ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
5814ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
5815ef48cebcSMatthew G. Knepley 
5816ef48cebcSMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, depths[d], &pStart, &pEnd);CHKERRQ(ierr);
5817ef48cebcSMatthew G. Knepley     ierr = DMPlexCreateNumbering_Private(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
5818ef48cebcSMatthew G. Knepley     shift += gsize;
5819ef48cebcSMatthew G. Knepley   }
5820302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
5821ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
5822ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
5823ef48cebcSMatthew G. Knepley }
5824ef48cebcSMatthew G. Knepley 
5825ca8062c8SMatthew G. Knepley /*@
5826ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
5827ca8062c8SMatthew G. Knepley 
5828ca8062c8SMatthew G. Knepley   Input Parameters:
5829ca8062c8SMatthew G. Knepley   + dm - The DMPlex object
5830ca8062c8SMatthew G. Knepley 
5831ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
5832ca8062c8SMatthew G. Knepley 
5833ca8062c8SMatthew G. Knepley   Level: developer
5834ca8062c8SMatthew G. Knepley 
58359bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSkeleton(), DMCheckFaces()
5836ca8062c8SMatthew G. Knepley @*/
5837ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
5838ca8062c8SMatthew G. Knepley {
5839ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
5840ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
5841ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
5842ca8062c8SMatthew G. Knepley   PetscInt        pStart, pEnd, p, csize, ssize;
5843ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
5844ca8062c8SMatthew G. Knepley 
5845ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
5846ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5847ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
5848ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
5849ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
5850ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
5851ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
5852ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
5853ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
5854ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
585542e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
585642e66dfaSMatthew G. Knepley       PetscInt  d;
585742e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
585842e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
585942e66dfaSMatthew G. Knepley       }
5860ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
5861ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
5862ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
5863ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
5864ca8062c8SMatthew G. Knepley       }
586542e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
58668ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
5867ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
58688ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
5869ca8062c8SMatthew G. Knepley         }
5870302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
58718ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
5872ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
58738ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
5874ca8062c8SMatthew G. Knepley         }
5875302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
58768ccfff9cSToby 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]);
58778ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
5878ca8062c8SMatthew G. Knepley       }
587942e66dfaSMatthew G. Knepley     }
5880ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
5881ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
5882ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
5883ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
5884ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
5885ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
5886ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
5887ca8062c8SMatthew G. Knepley       }
5888ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
58898ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
5890ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
58918ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
5892ca8062c8SMatthew G. Knepley         }
5893302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
58948ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
5895ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
58968ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
5897ca8062c8SMatthew G. Knepley         }
5898302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
58998ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
5900ca8062c8SMatthew G. Knepley       }
5901ca8062c8SMatthew G. Knepley     }
5902ca8062c8SMatthew G. Knepley   }
5903ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
5904ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
59058ccfff9cSToby Isaac   if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
5906ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
5907ca8062c8SMatthew G. Knepley }
5908ca8062c8SMatthew G. Knepley 
5909ca8062c8SMatthew G. Knepley /*@
5910ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
5911ca8062c8SMatthew G. Knepley 
5912ca8062c8SMatthew G. Knepley   Input Parameters:
5913ca8062c8SMatthew G. Knepley + dm - The DMPlex object
591458723a97SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
591558723a97SMatthew G. Knepley - cellHeight - Normally 0
5916ca8062c8SMatthew G. Knepley 
5917ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
5918ca8062c8SMatthew G. Knepley 
5919ca8062c8SMatthew G. Knepley   Level: developer
5920ca8062c8SMatthew G. Knepley 
59219bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckFaces()
5922ca8062c8SMatthew G. Knepley @*/
592358723a97SMatthew G. Knepley PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscBool isSimplex, PetscInt cellHeight)
5924ca8062c8SMatthew G. Knepley {
592542363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
5926ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
5927ca8062c8SMatthew G. Knepley 
5928ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
5929ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5930c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5931ca8062c8SMatthew G. Knepley   switch (dim) {
593242363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
593342363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
593442363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
5935ca8062c8SMatthew G. Knepley   default:
59368ccfff9cSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
5937ca8062c8SMatthew G. Knepley   }
593858723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
593958723a97SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
5940ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
5941ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
5942ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
594358723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
594458723a97SMatthew G. Knepley 
594558723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
594658723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
594758723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
594858723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
594958723a97SMatthew G. Knepley     }
595058723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
59518ccfff9cSToby Isaac     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
5952ca8062c8SMatthew G. Knepley   }
595342363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
595442363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
595542363296SMatthew G. Knepley 
595642363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
595742363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
595842363296SMatthew G. Knepley       const PetscInt p = closure[cl];
595942363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
596042363296SMatthew G. Knepley     }
596142363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
59628ccfff9cSToby Isaac     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
596342363296SMatthew G. Knepley   }
5964ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
5965ca8062c8SMatthew G. Knepley }
59669bf0dad6SMatthew G. Knepley 
59679bf0dad6SMatthew G. Knepley /*@
59689bf0dad6SMatthew 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
59699bf0dad6SMatthew G. Knepley 
59709bf0dad6SMatthew G. Knepley   Input Parameters:
59719bf0dad6SMatthew G. Knepley + dm - The DMPlex object
59729bf0dad6SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
59739bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
59749bf0dad6SMatthew G. Knepley 
59759bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
59769bf0dad6SMatthew G. Knepley 
59779bf0dad6SMatthew G. Knepley   Level: developer
59789bf0dad6SMatthew G. Knepley 
59799bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckSkeleton()
59809bf0dad6SMatthew G. Knepley @*/
59819bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexCheckFaces(DM dm, PetscBool isSimplex, PetscInt cellHeight)
59829bf0dad6SMatthew G. Knepley {
59833554e41dSMatthew G. Knepley   PetscInt       pMax[4];
59843554e41dSMatthew G. Knepley   PetscInt       dim, vStart, vEnd, cStart, cEnd, c, h;
59859bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
59869bf0dad6SMatthew G. Knepley 
59879bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
59889bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5989c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
59909bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
599125abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
59923554e41dSMatthew G. Knepley   for (h = cellHeight; h < dim; ++h) {
59933554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
59943554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
59959bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
59969bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
59979bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
59989bf0dad6SMatthew G. Knepley 
599925abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
60009bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
60019bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
60029bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
60039bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
60049bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
60059bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
60069bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
60079bf0dad6SMatthew G. Knepley       }
60083554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
60098ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
60109bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
60119bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
60129bf0dad6SMatthew G. Knepley 
60139bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
60149bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
60159bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
60169bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
60179bf0dad6SMatthew G. Knepley         }
60188ccfff9cSToby 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);
60199bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
60208ccfff9cSToby 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]);
60219bf0dad6SMatthew G. Knepley         }
60229bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
60239bf0dad6SMatthew G. Knepley       }
60249bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
60259bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
60269bf0dad6SMatthew G. Knepley     }
60273554e41dSMatthew G. Knepley   }
6028552f7358SJed Brown   PetscFunctionReturn(0);
6029552f7358SJed Brown }
60303913d7c8SMatthew G. Knepley 
6031bceba477SMatthew G. Knepley /* Pointwise interpolation
6032bceba477SMatthew G. Knepley      Just code FEM for now
6033bceba477SMatthew G. Knepley      u^f = I u^c
60344ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
60354ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
60364ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
6037bceba477SMatthew G. Knepley */
6038bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
6039bceba477SMatthew G. Knepley {
6040bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
6041bceba477SMatthew G. Knepley   PetscInt       m, n;
6042a063dac3SMatthew G. Knepley   void          *ctx;
604368132eb9SMatthew G. Knepley   DM             cdm;
604468132eb9SMatthew G. Knepley   PetscBool      regular;
6045bceba477SMatthew G. Knepley   PetscErrorCode ierr;
6046bceba477SMatthew G. Knepley 
6047bceba477SMatthew G. Knepley   PetscFunctionBegin;
6048bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6049bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6050bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6051bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
605268132eb9SMatthew G. Knepley 
6053bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
6054bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6055bceba477SMatthew G. Knepley   ierr = MatSetType(*interpolation, dmCoarse->mattype);CHKERRQ(ierr);
6056a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
605768132eb9SMatthew G. Knepley 
6058a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
605968132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
606068132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
606168132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
606268132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
60635d1c2e58SMatthew G. Knepley   /* Use naive scaling */
60645d1c2e58SMatthew G. Knepley   ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
6065a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
6066a063dac3SMatthew G. Knepley }
6067bceba477SMatthew G. Knepley 
60686dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
6069a063dac3SMatthew G. Knepley {
607090748bafSMatthew G. Knepley   PetscErrorCode ierr;
60716dbf9973SLawrence Mitchell   VecScatter     ctx;
607290748bafSMatthew G. Knepley 
6073a063dac3SMatthew G. Knepley   PetscFunctionBegin;
60746dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
60756dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
60766dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
6077bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6078bceba477SMatthew G. Knepley }
6079bceba477SMatthew G. Knepley 
6080fd59a867SMatthew G. Knepley PetscErrorCode DMCreateDefaultSection_Plex(DM dm)
6081fd59a867SMatthew G. Knepley {
6082fd59a867SMatthew G. Knepley   PetscSection   section;
6083ab1d0545SMatthew G. Knepley   IS            *bcPoints, *bcComps;
6084ebb3236fSMatthew G. Knepley   PetscBool     *isFE;
6085286d8613SMatthew G. Knepley   PetscInt      *bcFields, *numComp, *numDof;
6086ebb3236fSMatthew G. Knepley   PetscInt       depth, dim, numBd, numBC = 0, numFields, bd, bc = 0, f;
6087ebb3236fSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
6088fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
6089fd59a867SMatthew G. Knepley 
6090fd59a867SMatthew G. Knepley   PetscFunctionBegin;
6091ebb3236fSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
6092ae71db08SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
6093ebb3236fSMatthew G. Knepley   /* FE and FV boundary conditions are handled slightly differently */
6094ebb3236fSMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
6095ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6096ebb3236fSMatthew G. Knepley     PetscObject  obj;
6097ebb3236fSMatthew G. Knepley     PetscClassId id;
6098ebb3236fSMatthew G. Knepley 
6099ebb3236fSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6100ebb3236fSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
6101ebb3236fSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
6102ebb3236fSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
61038ccfff9cSToby Isaac     else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
6104ebb3236fSMatthew G. Knepley   }
61052f900235SMatthew G. Knepley   /* Allocate boundary point storage for FEM boundaries */
6106286d8613SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
6107c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6108ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
6109ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
6110dff059c6SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob, &numBd);CHKERRQ(ierr);
6111fd59a867SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
61122f900235SMatthew G. Knepley     PetscInt                field;
6113f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6114385532a5SToby Isaac     const char             *labelName;
6115385532a5SToby Isaac     DMLabel                 label;
61162f900235SMatthew G. Knepley 
6117f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &labelName, &field, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
6118385532a5SToby Isaac     ierr = DMGetLabel(dm,labelName,&label);CHKERRQ(ierr);
6119f971fd6bSMatthew G. Knepley     if (label && isFE[field] && (type & DM_BC_ESSENTIAL)) ++numBC;
6120fd59a867SMatthew G. Knepley   }
61212f900235SMatthew G. Knepley   /* Add ghost cell boundaries for FVM */
6122ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) if (!isFE[f] && cEndInterior >= 0) ++numBC;
61236c8d74c4SMatthew G. Knepley   ierr = PetscCalloc3(numBC,&bcFields,numBC,&bcPoints,numBC,&bcComps);CHKERRQ(ierr);
6124ebb3236fSMatthew G. Knepley   /* Constrain ghost cells for FV */
6125ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6126ebb3236fSMatthew G. Knepley     PetscInt *newidx, c;
6127ebb3236fSMatthew G. Knepley 
6128ebb3236fSMatthew G. Knepley     if (isFE[f] || cEndInterior < 0) continue;
6129ebb3236fSMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cEndInterior,&newidx);CHKERRQ(ierr);
6130ebb3236fSMatthew G. Knepley     for (c = cEndInterior; c < cEnd; ++c) newidx[c-cEndInterior] = c;
6131ebb3236fSMatthew G. Knepley     bcFields[bc] = f;
6132ebb3236fSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), cEnd-cEndInterior, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6133ebb3236fSMatthew G. Knepley   }
6134ebb3236fSMatthew G. Knepley   /* Handle FEM Dirichlet boundaries */
6135ebb3236fSMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
6136fd59a867SMatthew G. Knepley     const char             *bdLabel;
6137fd59a867SMatthew G. Knepley     DMLabel                 label;
61380e0f25f2SMatthew G. Knepley     const PetscInt         *comps;
6139fd59a867SMatthew G. Knepley     const PetscInt         *values;
61400e0f25f2SMatthew G. Knepley     PetscInt                bd2, field, numComps, numValues;
6141f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6142f971fd6bSMatthew G. Knepley     PetscBool               duplicate = PETSC_FALSE;
6143fd59a867SMatthew G. Knepley 
6144f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &bdLabel, &field, &numComps, &comps, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
6145c58f1c22SToby Isaac     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
6146385532a5SToby Isaac     if (!isFE[field] || !label) continue;
6147ebb3236fSMatthew G. Knepley     /* Only want to modify label once */
61487391c1cbSMatthew G. Knepley     for (bd2 = 0; bd2 < bd; ++bd2) {
61497391c1cbSMatthew G. Knepley       const char *bdname;
6150dff059c6SToby Isaac       ierr = PetscDSGetBoundary(dm->prob, bd2, NULL, NULL, &bdname, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
61517391c1cbSMatthew G. Knepley       ierr = PetscStrcmp(bdname, bdLabel, &duplicate);CHKERRQ(ierr);
61527391c1cbSMatthew G. Knepley       if (duplicate) break;
61537391c1cbSMatthew G. Knepley     }
6154ebb3236fSMatthew G. Knepley     if (!duplicate && (isFE[field])) {
6155b0bf5782SToby Isaac       /* don't complete cells, which are just present to give orientation to the boundary */
6156092e5057SToby Isaac       ierr = DMPlexLabelComplete(dm, label);CHKERRQ(ierr);
61577391c1cbSMatthew G. Knepley     }
6158ebb3236fSMatthew G. Knepley     /* Filter out cells, if you actually want to constrain cells you need to do things by hand right now */
6159f971fd6bSMatthew G. Knepley     if (type & DM_BC_ESSENTIAL) {
616093a5981aSMatthew G. Knepley       PetscInt       *newidx;
6161ebb3236fSMatthew G. Knepley       PetscInt        n, newn = 0, p, v;
616293a5981aSMatthew G. Knepley 
6163fd59a867SMatthew G. Knepley       bcFields[bc] = field;
61640e0f25f2SMatthew G. Knepley       if (numComps) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), numComps, comps, PETSC_COPY_VALUES, &bcComps[bc]);CHKERRQ(ierr);}
6165ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6166ebb3236fSMatthew G. Knepley         IS              tmp;
6167ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6168ebb3236fSMatthew G. Knepley 
6169c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6170ebb3236fSMatthew G. Knepley         if (!tmp) continue;
617193a5981aSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
617293a5981aSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6173ebb3236fSMatthew G. Knepley         if (isFE[field]) {
617493a5981aSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) ++newn;
6175ebb3236fSMatthew G. Knepley         } else {
6176ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) ++newn;
6177ebb3236fSMatthew G. Knepley         }
617893a5981aSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
617993a5981aSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6180fd59a867SMatthew G. Knepley       }
6181ebb3236fSMatthew G. Knepley       ierr = PetscMalloc1(newn,&newidx);CHKERRQ(ierr);
6182ebb3236fSMatthew G. Knepley       newn = 0;
6183ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6184ebb3236fSMatthew G. Knepley         IS              tmp;
6185ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6186ebb3236fSMatthew G. Knepley 
6187c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6188ebb3236fSMatthew G. Knepley         if (!tmp) continue;
6189ebb3236fSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
6190ebb3236fSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6191ebb3236fSMatthew G. Knepley         if (isFE[field]) {
6192ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) newidx[newn++] = idx[p];
6193ebb3236fSMatthew G. Knepley         } else {
6194ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) newidx[newn++] = idx[p];
6195ebb3236fSMatthew G. Knepley         }
6196ebb3236fSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
6197ebb3236fSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6198ebb3236fSMatthew G. Knepley       }
6199ebb3236fSMatthew G. Knepley       ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), newn, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6200ebb3236fSMatthew G. Knepley     }
6201fd59a867SMatthew G. Knepley   }
6202fd59a867SMatthew G. Knepley   /* Handle discretization */
6203ebb3236fSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&numComp,numFields*(dim+1),&numDof);CHKERRQ(ierr);
6204fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
62050bacfa0aSMatthew G. Knepley     PetscObject obj;
62060bacfa0aSMatthew G. Knepley 
62070bacfa0aSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6208ebb3236fSMatthew G. Knepley     if (isFE[f]) {
62090bacfa0aSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
6210286d8613SMatthew G. Knepley       const PetscInt *numFieldDof;
6211fd59a867SMatthew G. Knepley       PetscInt        d;
6212fd59a867SMatthew G. Knepley 
6213fd59a867SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
6214286d8613SMatthew G. Knepley       ierr = PetscFEGetNumDof(fe, &numFieldDof);CHKERRQ(ierr);
6215286d8613SMatthew G. Knepley       for (d = 0; d < dim+1; ++d) numDof[f*(dim+1)+d] = numFieldDof[d];
6216ebb3236fSMatthew G. Knepley     } else {
62170bacfa0aSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
62180bacfa0aSMatthew G. Knepley 
62190bacfa0aSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
62200bacfa0aSMatthew G. Knepley       numDof[f*(dim+1)+dim] = numComp[f];
6221ebb3236fSMatthew G. Knepley     }
6222fd59a867SMatthew G. Knepley   }
6223286d8613SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6224286d8613SMatthew G. Knepley     PetscInt d;
6225286d8613SMatthew G. Knepley     for (d = 1; d < dim; ++d) {
6226286d8613SMatthew 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.");
6227286d8613SMatthew G. Knepley     }
6228286d8613SMatthew G. Knepley   }
6229ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSection(dm, dim, numFields, numComp, numDof, numBC, bcFields, bcComps, bcPoints, NULL, &section);CHKERRQ(ierr);
6230fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6231fd59a867SMatthew G. Knepley     PetscFE     fe;
6232fd59a867SMatthew G. Knepley     const char *name;
623318abd763SMatthew G. Knepley 
623418abd763SMatthew G. Knepley     ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
6235fd59a867SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
6236fd59a867SMatthew G. Knepley     ierr = PetscSectionSetFieldName(section, f, name);CHKERRQ(ierr);
6237fd59a867SMatthew G. Knepley   }
6238fd59a867SMatthew G. Knepley   ierr = DMSetDefaultSection(dm, section);CHKERRQ(ierr);
6239fd59a867SMatthew G. Knepley   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
62400e0f25f2SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {ierr = ISDestroy(&bcPoints[bc]);CHKERRQ(ierr);ierr = ISDestroy(&bcComps[bc]);CHKERRQ(ierr);}
62410e0f25f2SMatthew G. Knepley   ierr = PetscFree3(bcFields,bcPoints,bcComps);CHKERRQ(ierr);
6242286d8613SMatthew G. Knepley   ierr = PetscFree2(numComp,numDof);CHKERRQ(ierr);
6243ebb3236fSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
6244bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6245bceba477SMatthew G. Knepley }
6246bceba477SMatthew G. Knepley 
62470aef6b92SMatthew G. Knepley /*@
62480aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
62490aef6b92SMatthew G. Knepley 
62500aef6b92SMatthew G. Knepley   Input Parameter:
62510aef6b92SMatthew G. Knepley . dm - The DMPlex object
62520aef6b92SMatthew G. Knepley 
62530aef6b92SMatthew G. Knepley   Output Parameter:
62540aef6b92SMatthew G. Knepley . regular - The flag
62550aef6b92SMatthew G. Knepley 
62560aef6b92SMatthew G. Knepley   Level: intermediate
62570aef6b92SMatthew G. Knepley 
62580aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
62590aef6b92SMatthew G. Knepley @*/
62600aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
62610aef6b92SMatthew G. Knepley {
62620aef6b92SMatthew G. Knepley   PetscFunctionBegin;
62630aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62640aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
62650aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
62660aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
62670aef6b92SMatthew G. Knepley }
62680aef6b92SMatthew G. Knepley 
62690aef6b92SMatthew G. Knepley /*@
62700aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
62710aef6b92SMatthew G. Knepley 
62720aef6b92SMatthew G. Knepley   Input Parameters:
62730aef6b92SMatthew G. Knepley + dm - The DMPlex object
62740aef6b92SMatthew G. Knepley - regular - The flag
62750aef6b92SMatthew G. Knepley 
62760aef6b92SMatthew G. Knepley   Level: intermediate
62770aef6b92SMatthew G. Knepley 
62780aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
62790aef6b92SMatthew G. Knepley @*/
62800aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
62810aef6b92SMatthew G. Knepley {
62820aef6b92SMatthew G. Knepley   PetscFunctionBegin;
62830aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62840aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
62850aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
62860aef6b92SMatthew G. Knepley }
62870aef6b92SMatthew G. Knepley 
6288f7c74593SToby Isaac /* anchors */
6289a68b90caSToby Isaac /*@
6290f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
6291f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
6292a68b90caSToby Isaac 
6293e228b242SToby Isaac   not collective
6294a68b90caSToby Isaac 
6295a68b90caSToby Isaac   Input Parameters:
6296a68b90caSToby Isaac . dm - The DMPlex object
6297a68b90caSToby Isaac 
6298a68b90caSToby Isaac   Output Parameters:
6299a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
6300a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
6301a68b90caSToby Isaac 
6302a68b90caSToby Isaac 
6303a68b90caSToby Isaac   Level: intermediate
6304a68b90caSToby Isaac 
6305f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
6306a68b90caSToby Isaac @*/
6307a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
6308a68b90caSToby Isaac {
6309a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
631041e6d900SToby Isaac   PetscErrorCode ierr;
6311a68b90caSToby Isaac 
6312a68b90caSToby Isaac   PetscFunctionBegin;
6313a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
631441e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
6315a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
6316a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
6317a68b90caSToby Isaac   PetscFunctionReturn(0);
6318a68b90caSToby Isaac }
6319a68b90caSToby Isaac 
6320a68b90caSToby Isaac /*@
6321f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
6322f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
6323a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
6324a68b90caSToby Isaac 
6325a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
6326f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
6327a68b90caSToby Isaac 
6328e228b242SToby Isaac   collective on dm
6329a68b90caSToby Isaac 
6330a68b90caSToby Isaac   Input Parameters:
6331a68b90caSToby Isaac + dm - The DMPlex object
6332e228b242SToby 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).
6333e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
6334a68b90caSToby Isaac 
6335a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
6336a68b90caSToby Isaac 
6337a68b90caSToby Isaac   Level: intermediate
6338a68b90caSToby Isaac 
6339f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
6340a68b90caSToby Isaac @*/
6341a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
6342a68b90caSToby Isaac {
6343a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6344e228b242SToby Isaac   PetscMPIInt    result;
6345a68b90caSToby Isaac   PetscErrorCode ierr;
6346a68b90caSToby Isaac 
6347a68b90caSToby Isaac   PetscFunctionBegin;
6348a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6349e228b242SToby Isaac   if (anchorSection) {
6350e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
6351e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
6352e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
6353e228b242SToby Isaac   }
6354e228b242SToby Isaac   if (anchorIS) {
6355e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
6356e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
6357e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
6358e228b242SToby Isaac   }
6359a68b90caSToby Isaac 
6360a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
6361a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
6362a68b90caSToby Isaac   plex->anchorSection = anchorSection;
6363a68b90caSToby Isaac 
6364a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
6365a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
6366a68b90caSToby Isaac   plex->anchorIS = anchorIS;
6367a68b90caSToby Isaac 
6368a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
6369a68b90caSToby Isaac   if (anchorIS && anchorSection) {
6370a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
6371a68b90caSToby Isaac     const PetscInt *anchors;
6372a68b90caSToby Isaac 
6373a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
6374a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
6375a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
6376a68b90caSToby Isaac     for (a = 0; a < size; a++) {
6377a68b90caSToby Isaac       PetscInt p;
6378a68b90caSToby Isaac 
6379a68b90caSToby Isaac       p = anchors[a];
6380a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
6381a68b90caSToby Isaac         PetscInt dof;
6382a68b90caSToby Isaac 
6383a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6384a68b90caSToby Isaac         if (dof) {
6385a68b90caSToby Isaac           PetscErrorCode ierr2;
6386a68b90caSToby Isaac 
6387a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
63888ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
6389a68b90caSToby Isaac         }
6390a68b90caSToby Isaac       }
6391a68b90caSToby Isaac     }
6392a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
6393a68b90caSToby Isaac   }
6394a68b90caSToby Isaac #endif
6395f7c74593SToby Isaac   /* reset the generic constraints */
6396f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
6397a68b90caSToby Isaac   PetscFunctionReturn(0);
6398a68b90caSToby Isaac }
6399a68b90caSToby Isaac 
6400f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
6401a68b90caSToby Isaac {
6402f7c74593SToby Isaac   PetscSection anchorSection;
64036995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
6404a68b90caSToby Isaac   PetscErrorCode ierr;
6405a68b90caSToby Isaac 
6406a68b90caSToby Isaac   PetscFunctionBegin;
6407a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6408a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
6409e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
6410a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
64116995de1eSToby Isaac   if (numFields) {
6412719ab38cSToby Isaac     PetscInt f;
6413a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
6414719ab38cSToby Isaac 
6415719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
6416719ab38cSToby Isaac       PetscInt numComp;
6417719ab38cSToby Isaac 
6418719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
6419719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
6420719ab38cSToby Isaac     }
64216995de1eSToby Isaac   }
6422a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
64236995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
64246995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
64256995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
64266995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
6427a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
6428a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
6429a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6430a68b90caSToby Isaac     if (dof) {
6431a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
6432a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
6433a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
6434a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
6435a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
6436a68b90caSToby Isaac       }
6437a68b90caSToby Isaac     }
6438a68b90caSToby Isaac   }
6439a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
6440a68b90caSToby Isaac   PetscFunctionReturn(0);
6441a68b90caSToby Isaac }
6442a68b90caSToby Isaac 
6443f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
6444a68b90caSToby Isaac {
6445f7c74593SToby Isaac   PetscSection aSec;
64460ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
64470ac89760SToby Isaac   const PetscInt *anchors;
64480ac89760SToby Isaac   PetscInt numFields, f;
644966ad2231SToby Isaac   IS aIS;
64500ac89760SToby Isaac   PetscErrorCode ierr;
64510ac89760SToby Isaac 
64520ac89760SToby Isaac   PetscFunctionBegin;
64530ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64540ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
64550ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
64560ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
64570ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
6458302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
6459a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
646066ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
64616995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
64626995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
64630ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
64640ac89760SToby Isaac   i[0] = 0;
64650ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
64660ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
6467f19733c5SToby Isaac     PetscInt rDof, rOff, r;
6468f19733c5SToby Isaac 
6469f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
6470f19733c5SToby Isaac     if (!rDof) continue;
6471f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
64720ac89760SToby Isaac     if (numFields) {
64730ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
64740ac89760SToby Isaac         annz = 0;
6475f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
6476f19733c5SToby Isaac           a = anchors[rOff + r];
64770ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
64780ac89760SToby Isaac           annz += aDof;
64790ac89760SToby Isaac         }
64800ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
64810ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
64820ac89760SToby Isaac         for (q = 0; q < dof; q++) {
64830ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
64840ac89760SToby Isaac         }
64850ac89760SToby Isaac       }
64860ac89760SToby Isaac     }
64870ac89760SToby Isaac     else {
64880ac89760SToby Isaac       annz = 0;
64890ac89760SToby Isaac       for (q = 0; q < dof; q++) {
64900ac89760SToby Isaac         a = anchors[off + q];
64910ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
64920ac89760SToby Isaac         annz += aDof;
64930ac89760SToby Isaac       }
64940ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
64950ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
64960ac89760SToby Isaac       for (q = 0; q < dof; q++) {
64970ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
64980ac89760SToby Isaac       }
64990ac89760SToby Isaac     }
65000ac89760SToby Isaac   }
65010ac89760SToby Isaac   nnz = i[m];
65020ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
65030ac89760SToby Isaac   offset = 0;
65040ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
65050ac89760SToby Isaac     if (numFields) {
65060ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
65070ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
65080ac89760SToby Isaac         for (q = 0; q < dof; q++) {
65090ac89760SToby Isaac           PetscInt rDof, rOff, r;
65100ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
65110ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
65120ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
65130ac89760SToby Isaac             PetscInt s;
65140ac89760SToby Isaac 
65150ac89760SToby Isaac             a = anchors[rOff + r];
65160ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
65170ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
65180ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
65190ac89760SToby Isaac               j[offset++] = aOff + s;
65200ac89760SToby Isaac             }
65210ac89760SToby Isaac           }
65220ac89760SToby Isaac         }
65230ac89760SToby Isaac       }
65240ac89760SToby Isaac     }
65250ac89760SToby Isaac     else {
65260ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
65270ac89760SToby Isaac       for (q = 0; q < dof; q++) {
65280ac89760SToby Isaac         PetscInt rDof, rOff, r;
65290ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
65300ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
65310ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
65320ac89760SToby Isaac           PetscInt s;
65330ac89760SToby Isaac 
65340ac89760SToby Isaac           a = anchors[rOff + r];
65350ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
65360ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
65370ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
65380ac89760SToby Isaac             j[offset++] = aOff + s;
65390ac89760SToby Isaac           }
65400ac89760SToby Isaac         }
65410ac89760SToby Isaac       }
65420ac89760SToby Isaac     }
65430ac89760SToby Isaac   }
65440ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
654525570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
654625570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
654766ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
65480ac89760SToby Isaac   PetscFunctionReturn(0);
65490ac89760SToby Isaac }
65500ac89760SToby Isaac 
655166ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
655266ad2231SToby Isaac {
6553f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6554f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
655566ad2231SToby Isaac   Mat            cMat;
655666ad2231SToby Isaac   PetscErrorCode ierr;
655766ad2231SToby Isaac 
655866ad2231SToby Isaac   PetscFunctionBegin;
655966ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6560a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
656166ad2231SToby Isaac   if (anchorSection) {
6562e228b242SToby Isaac     PetscDS  ds;
6563e228b242SToby Isaac     PetscInt nf;
6564e228b242SToby Isaac 
6565f7c74593SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
6566f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
6567f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
6568e228b242SToby Isaac     ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
6569302440fdSBarry Smith     ierr = PetscDSGetNumFields(ds,&nf);CHKERRQ(ierr);
6570e228b242SToby Isaac     if (nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
657166ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
657266ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
657366ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
657466ad2231SToby Isaac   }
657566ad2231SToby Isaac   PetscFunctionReturn(0);
657666ad2231SToby Isaac }
6577