xref: /petsc/src/dm/impls/plex/plex.c (revision e53375922a6f5ddf331bb8e144d248f0354459b7)
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);
12552f7358SJed Brown 
13*e5337592SStefano Zampini /*@
14*e5337592SStefano Zampini   DMPlexRefineSimplexToTensor - Uniformly refines simplicial cells into tensor product cells.
15*e5337592SStefano Zampini   3 quadrilaterals per triangle in 2D and 4 hexahedra per tetrahedron in 3D.
16*e5337592SStefano Zampini 
17*e5337592SStefano Zampini   Collective
18*e5337592SStefano Zampini 
19*e5337592SStefano Zampini   Input Parameters:
20*e5337592SStefano Zampini . dm - The DMPlex object
21*e5337592SStefano Zampini 
22*e5337592SStefano Zampini   Output Parameters:
23*e5337592SStefano Zampini . dmRefined - The refined DMPlex object
24*e5337592SStefano Zampini 
25*e5337592SStefano Zampini   Note: Returns NULL if the mesh is already a tensor product mesh.
26*e5337592SStefano Zampini 
27*e5337592SStefano Zampini   Level: intermediate
28*e5337592SStefano Zampini 
29*e5337592SStefano Zampini .seealso: DMPlexCreate(), DMPlexSetRefinementUniform()
30*e5337592SStefano Zampini @*/
31*e5337592SStefano Zampini PetscErrorCode DMPlexRefineSimplexToTensor(DM dm, DM *dmRefined)
32*e5337592SStefano Zampini {
33*e5337592SStefano Zampini   PetscInt         dim, cMax, fMax, cStart, cEnd, coneSize;
34*e5337592SStefano Zampini   CellRefiner      cellRefiner;
35*e5337592SStefano Zampini   PetscBool        lop, allnoop, localized;
36*e5337592SStefano Zampini   PetscErrorCode   ierr;
37*e5337592SStefano Zampini 
38*e5337592SStefano Zampini   PetscFunctionBegin;
39*e5337592SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40*e5337592SStefano Zampini   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
41*e5337592SStefano Zampini   ierr = DMPlexGetHybridBounds(dm,&cMax,&fMax,NULL,NULL);CHKERRQ(ierr);
42*e5337592SStefano Zampini   if (cMax >= 0 || fMax >= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle hybrid meshes yet");
43*e5337592SStefano Zampini   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
44*e5337592SStefano Zampini   if (!(cEnd - cStart)) cellRefiner = REFINER_NOOP;
45*e5337592SStefano Zampini   else {
46*e5337592SStefano Zampini     ierr = DMPlexGetConeSize(dm,cStart,&coneSize);CHKERRQ(ierr);
47*e5337592SStefano Zampini     switch (dim) {
48*e5337592SStefano Zampini     case 1:
49*e5337592SStefano Zampini       cellRefiner = REFINER_NOOP;
50*e5337592SStefano Zampini     break;
51*e5337592SStefano Zampini     case 2:
52*e5337592SStefano Zampini       switch (coneSize) {
53*e5337592SStefano Zampini       case 3:
54*e5337592SStefano Zampini         cellRefiner = REFINER_SIMPLEX_TO_HEX_2D;
55*e5337592SStefano Zampini       break;
56*e5337592SStefano Zampini       case 4:
57*e5337592SStefano Zampini         cellRefiner = REFINER_NOOP;
58*e5337592SStefano Zampini       break;
59*e5337592SStefano Zampini       default: SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle coneSize %D with dimension %D",coneSize,dim);
60*e5337592SStefano Zampini       }
61*e5337592SStefano Zampini     break;
62*e5337592SStefano Zampini     case 3:
63*e5337592SStefano Zampini       switch (coneSize) {
64*e5337592SStefano Zampini       case 4:
65*e5337592SStefano Zampini         cellRefiner = REFINER_SIMPLEX_TO_HEX_3D;
66*e5337592SStefano Zampini       break;
67*e5337592SStefano Zampini       case 6:
68*e5337592SStefano Zampini         cellRefiner = REFINER_NOOP;
69*e5337592SStefano Zampini       break;
70*e5337592SStefano Zampini       default: SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle coneSize %D with dimension %D",coneSize,dim);
71*e5337592SStefano Zampini       }
72*e5337592SStefano Zampini     break;
73*e5337592SStefano Zampini     default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle dimension %D",dim);
74*e5337592SStefano Zampini     }
75*e5337592SStefano Zampini   }
76*e5337592SStefano Zampini   /* return if we don't need to refine */
77*e5337592SStefano Zampini   lop = (cellRefiner == REFINER_NOOP) ? PETSC_TRUE : PETSC_FALSE;
78*e5337592SStefano Zampini   ierr = MPIU_Allreduce(&lop,&allnoop,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
79*e5337592SStefano Zampini   if (allnoop) {
80*e5337592SStefano Zampini     *dmRefined = NULL;
81*e5337592SStefano Zampini     PetscFunctionReturn(0);
82*e5337592SStefano Zampini   }
83*e5337592SStefano Zampini   ierr = DMPlexRefineUniform_Internal(dm, cellRefiner, dmRefined);CHKERRQ(ierr);
84*e5337592SStefano Zampini   ierr = DMCopyBoundary(dm, *dmRefined);CHKERRQ(ierr);
85*e5337592SStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
86*e5337592SStefano Zampini   if (localized) {
87*e5337592SStefano Zampini     ierr = DMLocalizeCoordinates(*dmRefined);CHKERRQ(ierr);
88*e5337592SStefano Zampini   }
89*e5337592SStefano Zampini   PetscFunctionReturn(0);
90*e5337592SStefano Zampini }
91*e5337592SStefano Zampini 
927afe7537SMatthew G. Knepley PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
937e42fee7SMatthew G. Knepley {
9480d5bdc6SMatthew G. Knepley   PetscInt       dim, pStart, pEnd, vStart, vEnd, cStart, cEnd, cEndInterior, vdof = 0, cdof = 0;
957e42fee7SMatthew G. Knepley   PetscErrorCode ierr;
967e42fee7SMatthew G. Knepley 
977e42fee7SMatthew G. Knepley   PetscFunctionBegin;
987e42fee7SMatthew G. Knepley   *ft  = PETSC_VTK_POINT_FIELD;
99c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1007e42fee7SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1017e42fee7SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
10280d5bdc6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
10380d5bdc6SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1047e42fee7SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1057e42fee7SMatthew G. Knepley   if (field >= 0) {
1067e42fee7SMatthew G. Knepley     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, vStart, field, &vdof);CHKERRQ(ierr);}
1077e42fee7SMatthew G. Knepley     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, cStart, field, &cdof);CHKERRQ(ierr);}
1087e42fee7SMatthew G. Knepley   } else {
1097e42fee7SMatthew G. Knepley     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetDof(section, vStart, &vdof);CHKERRQ(ierr);}
1107e42fee7SMatthew G. Knepley     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetDof(section, cStart, &cdof);CHKERRQ(ierr);}
1117e42fee7SMatthew G. Knepley   }
1127e42fee7SMatthew G. Knepley   if (vdof) {
1137e42fee7SMatthew G. Knepley     *sStart = vStart;
1147e42fee7SMatthew G. Knepley     *sEnd   = vEnd;
1157e42fee7SMatthew G. Knepley     if (vdof == dim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
1167e42fee7SMatthew G. Knepley     else             *ft = PETSC_VTK_POINT_FIELD;
1177e42fee7SMatthew G. Knepley   } else if (cdof) {
1187e42fee7SMatthew G. Knepley     *sStart = cStart;
1197e42fee7SMatthew G. Knepley     *sEnd   = cEnd;
1207e42fee7SMatthew G. Knepley     if (cdof == dim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
1217e42fee7SMatthew G. Knepley     else             *ft = PETSC_VTK_CELL_FIELD;
1227e42fee7SMatthew G. Knepley   } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Could not classify input Vec for VTK");
1237e42fee7SMatthew G. Knepley   PetscFunctionReturn(0);
1247e42fee7SMatthew G. Knepley }
1257e42fee7SMatthew G. Knepley 
1267cd05799SMatthew G. Knepley static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
127e412dcbdSMatthew G. Knepley {
128e412dcbdSMatthew G. Knepley   DM                 dm;
129d1df6f1dSMatthew G. Knepley   PetscSection       s;
130e412dcbdSMatthew G. Knepley   PetscDraw          draw, popup;
131e412dcbdSMatthew G. Knepley   DM                 cdm;
132e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
133e412dcbdSMatthew G. Knepley   Vec                coordinates;
134e412dcbdSMatthew G. Knepley   const PetscScalar *coords, *array;
135e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
136339e3443SMatthew G. Knepley   PetscReal          vbound[2], time;
137339e3443SMatthew G. Knepley   PetscBool          isnull, flg;
138d1df6f1dSMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
139e412dcbdSMatthew G. Knepley   const char        *name;
140339e3443SMatthew G. Knepley   char               title[PETSC_MAX_PATH_LEN];
141e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
142e412dcbdSMatthew G. Knepley 
143e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
144d1df6f1dSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
145d1df6f1dSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
146d1df6f1dSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
147d1df6f1dSMatthew G. Knepley 
148e412dcbdSMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
149e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
150e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
151d1df6f1dSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
152d1df6f1dSMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
153e412dcbdSMatthew G. Knepley   ierr = DMGetCoarsenLevel(dm, &level);CHKERRQ(ierr);
154e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
155e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
156e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
157e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
158e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
159e412dcbdSMatthew G. Knepley 
160e412dcbdSMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
161339e3443SMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, &step, &time);CHKERRQ(ierr);
162e412dcbdSMatthew G. Knepley 
163e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
164e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
165e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
1660c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
1670c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
168e412dcbdSMatthew G. Knepley   }
169e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
170e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
171e412dcbdSMatthew G. Knepley 
172d1df6f1dSMatthew G. Knepley   /* Could implement something like DMDASelectFields() */
173d1df6f1dSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
174d1df6f1dSMatthew G. Knepley     DM   fdm = dm;
175d1df6f1dSMatthew G. Knepley     Vec  fv  = v;
176d1df6f1dSMatthew G. Knepley     IS   fis;
177d1df6f1dSMatthew G. Knepley     char prefix[PETSC_MAX_PATH_LEN];
178d1df6f1dSMatthew G. Knepley     const char *fname;
179d1df6f1dSMatthew G. Knepley 
180d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
181d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldName(s, f, &fname);CHKERRQ(ierr);
182d1df6f1dSMatthew G. Knepley 
183d1df6f1dSMatthew G. Knepley     if (v->hdr.prefix) {ierr = PetscStrcpy(prefix, v->hdr.prefix);CHKERRQ(ierr);}
184d1df6f1dSMatthew G. Knepley     else               {prefix[0] = '\0';}
185d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
186d1df6f1dSMatthew G. Knepley       ierr = DMCreateSubDM(dm, 1, &f, &fis, &fdm);CHKERRQ(ierr);
187d1df6f1dSMatthew G. Knepley       ierr = VecGetSubVector(v, fis, &fv);CHKERRQ(ierr);
188d1df6f1dSMatthew G. Knepley       ierr = PetscStrcat(prefix, fname);CHKERRQ(ierr);
189d1df6f1dSMatthew G. Knepley       ierr = PetscStrcat(prefix, "_");CHKERRQ(ierr);
190d1df6f1dSMatthew G. Knepley     }
191d1df6f1dSMatthew G. Knepley     for (comp = 0; comp < Nc; ++comp, ++w) {
192d1df6f1dSMatthew G. Knepley       PetscInt nmax = 2;
193d1df6f1dSMatthew G. Knepley 
194d1df6f1dSMatthew G. Knepley       ierr = PetscViewerDrawGetDraw(viewer, w, &draw);CHKERRQ(ierr);
195d1df6f1dSMatthew G. Knepley       if (Nc > 1) {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s_%D Step: %D Time: %.4g", name, fname, comp, step, time);CHKERRQ(ierr);}
196d1df6f1dSMatthew G. Knepley       else        {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s Step: %D Time: %.4g", name, fname, step, time);CHKERRQ(ierr);}
197d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetTitle(draw, title);CHKERRQ(ierr);
198d1df6f1dSMatthew G. Knepley 
199d1df6f1dSMatthew G. Knepley       /* TODO Get max and min only for this component */
200d1df6f1dSMatthew G. Knepley       ierr = PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg);CHKERRQ(ierr);
201339e3443SMatthew G. Knepley       if (!flg) {
202d1df6f1dSMatthew G. Knepley         ierr = VecMin(fv, NULL, &vbound[0]);CHKERRQ(ierr);
203d1df6f1dSMatthew G. Knepley         ierr = VecMax(fv, NULL, &vbound[1]);CHKERRQ(ierr);
204d1df6f1dSMatthew G. Knepley         if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
205339e3443SMatthew G. Knepley       }
206e412dcbdSMatthew G. Knepley       ierr = PetscDrawGetPopup(draw, &popup);CHKERRQ(ierr);
207339e3443SMatthew G. Knepley       ierr = PetscDrawScalePopup(popup, vbound[0], vbound[1]);CHKERRQ(ierr);
208d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
209e412dcbdSMatthew G. Knepley 
210d1df6f1dSMatthew G. Knepley       ierr = VecGetArrayRead(fv, &array);CHKERRQ(ierr);
211e412dcbdSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
21299a2f7bcSMatthew G. Knepley         PetscScalar *coords = NULL, *a = NULL;
213339e3443SMatthew G. Knepley         PetscInt     numCoords, color[4];
214e412dcbdSMatthew G. Knepley 
215d1df6f1dSMatthew G. Knepley         ierr = DMPlexPointLocalRead(fdm, c, array, &a);CHKERRQ(ierr);
216339e3443SMatthew G. Knepley         if (a) {
217d1df6f1dSMatthew G. Knepley           color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
218339e3443SMatthew G. Knepley           color[1] = color[2] = color[3] = color[0];
219339e3443SMatthew G. Knepley         } else {
220339e3443SMatthew G. Knepley           PetscScalar *vals = NULL;
221339e3443SMatthew G. Knepley           PetscInt     numVals, va;
222339e3443SMatthew G. Knepley 
223d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
224d1df6f1dSMatthew 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);
225d1df6f1dSMatthew G. Knepley           switch (numVals/Nc) {
226d1df6f1dSMatthew G. Knepley           case 3: /* P1 Triangle */
227d1df6f1dSMatthew G. Knepley           case 4: /* P1 Quadrangle */
228d1df6f1dSMatthew G. Knepley             for (va = 0; va < numVals/Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp]), vbound[0], vbound[1]);
229339e3443SMatthew G. Knepley             break;
230d1df6f1dSMatthew G. Knepley           case 6: /* P2 Triangle */
231d1df6f1dSMatthew G. Knepley           case 8: /* P2 Quadrangle */
232d1df6f1dSMatthew 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]);
233d1df6f1dSMatthew G. Knepley             break;
234d1df6f1dSMatthew G. Knepley           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %D cannot be handled", numVals/Nc);
235339e3443SMatthew G. Knepley           }
236d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
237339e3443SMatthew G. Knepley         }
238e412dcbdSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
239e412dcbdSMatthew G. Knepley         switch (numCoords) {
240e412dcbdSMatthew G. Knepley         case 6:
241339e3443SMatthew 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);
242e412dcbdSMatthew G. Knepley           break;
243e412dcbdSMatthew G. Knepley         case 8:
244339e3443SMatthew 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);
245339e3443SMatthew 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);
246e412dcbdSMatthew G. Knepley           break;
247e412dcbdSMatthew G. Knepley         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
248e412dcbdSMatthew G. Knepley         }
249e412dcbdSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
250e412dcbdSMatthew G. Knepley       }
251d1df6f1dSMatthew G. Knepley       ierr = VecRestoreArrayRead(fv, &array);CHKERRQ(ierr);
252e412dcbdSMatthew G. Knepley       ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
253e412dcbdSMatthew G. Knepley       ierr = PetscDrawPause(draw);CHKERRQ(ierr);
254e412dcbdSMatthew G. Knepley       ierr = PetscDrawSave(draw);CHKERRQ(ierr);
255d1df6f1dSMatthew G. Knepley     }
256d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
257d1df6f1dSMatthew G. Knepley       ierr = VecRestoreSubVector(v, fis, &fv);CHKERRQ(ierr);
258d1df6f1dSMatthew G. Knepley       ierr = ISDestroy(&fis);CHKERRQ(ierr);
259d1df6f1dSMatthew G. Knepley       ierr = DMDestroy(&fdm);CHKERRQ(ierr);
260d1df6f1dSMatthew G. Knepley     }
261d1df6f1dSMatthew G. Knepley   }
262e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
263e412dcbdSMatthew G. Knepley }
264e412dcbdSMatthew G. Knepley 
265552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
266552f7358SJed Brown {
267552f7358SJed Brown   DM             dm;
268f13a32a3SMatthew G. Knepley   PetscBool      isvtk, ishdf5, isdraw, isseq;
269552f7358SJed Brown   PetscErrorCode ierr;
270552f7358SJed Brown 
271552f7358SJed Brown   PetscFunctionBegin;
272552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
27382f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
274552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
275b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
276f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW, &isdraw);CHKERRQ(ierr);
277b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
278ef31f671SMatthew G. Knepley   if (isvtk || ishdf5) {
279ef31f671SMatthew G. Knepley     PetscInt  numFields;
280ef31f671SMatthew G. Knepley     PetscBool fem = PETSC_FALSE;
281ef31f671SMatthew G. Knepley 
282ef31f671SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
283ef31f671SMatthew G. Knepley     if (numFields) {
284ef31f671SMatthew G. Knepley       PetscObject fe;
285ef31f671SMatthew G. Knepley 
286ef31f671SMatthew G. Knepley       ierr = DMGetField(dm, 0, &fe);CHKERRQ(ierr);
287ef31f671SMatthew G. Knepley       if (fe->classid == PETSCFE_CLASSID) fem = PETSC_TRUE;
288ef31f671SMatthew G. Knepley     }
289bdd6f66aSToby Isaac     if (fem) {ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, v, 0.0, NULL, NULL, NULL);CHKERRQ(ierr);}
290ef31f671SMatthew G. Knepley   }
291552f7358SJed Brown   if (isvtk) {
292552f7358SJed Brown     PetscSection            section;
293b136c2c9SMatthew G. Knepley     PetscViewerVTKFieldType ft;
294b136c2c9SMatthew G. Knepley     PetscInt                pStart, pEnd;
295552f7358SJed Brown 
296552f7358SJed Brown     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
2977afe7537SMatthew G. Knepley     ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
298552f7358SJed Brown     ierr = PetscObjectReference((PetscObject) v);CHKERRQ(ierr);  /* viewer drops reference */
299552f7358SJed Brown     ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, ft, (PetscObject) v);CHKERRQ(ierr);
300552f7358SJed Brown   } else if (ishdf5) {
301b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
30239d25373SMatthew G. Knepley     ierr = VecView_Plex_Local_HDF5_Internal(v, viewer);CHKERRQ(ierr);
303b136c2c9SMatthew G. Knepley #else
304b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
305b136c2c9SMatthew G. Knepley #endif
306f13a32a3SMatthew G. Knepley   } else if (isdraw) {
307f13a32a3SMatthew G. Knepley     ierr = VecView_Plex_Local_Draw(v, viewer);CHKERRQ(ierr);
308552f7358SJed Brown   } else {
30955f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
31055f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
311552f7358SJed Brown   }
312552f7358SJed Brown   PetscFunctionReturn(0);
313552f7358SJed Brown }
314552f7358SJed Brown 
315552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
316552f7358SJed Brown {
317552f7358SJed Brown   DM             dm;
318f13a32a3SMatthew G. Knepley   PetscReal      time = 0.0;
319e412dcbdSMatthew G. Knepley   PetscBool      isvtk, ishdf5, isdraw, isseq;
320552f7358SJed Brown   PetscErrorCode ierr;
321552f7358SJed Brown 
322552f7358SJed Brown   PetscFunctionBegin;
323552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
32482f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
325552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
32633c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
327e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW, &isdraw);CHKERRQ(ierr);
32855f2e967SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
329552f7358SJed Brown   if (isvtk) {
330552f7358SJed Brown     Vec         locv;
331552f7358SJed Brown     const char *name;
332552f7358SJed Brown 
333552f7358SJed Brown     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
334552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
335552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
336552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
337552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
338cf9ba882SMatthew G. Knepley     ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
339cf9ba882SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
340552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
341552f7358SJed Brown     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
342b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
343b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
34439d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
345b136c2c9SMatthew G. Knepley #else
346b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
347b136c2c9SMatthew G. Knepley #endif
348e412dcbdSMatthew G. Knepley   } else if (isdraw) {
349f13a32a3SMatthew G. Knepley     Vec         locv;
350f13a32a3SMatthew G. Knepley     const char *name;
351f13a32a3SMatthew G. Knepley 
352f13a32a3SMatthew G. Knepley     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
353f13a32a3SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
354f13a32a3SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
355f13a32a3SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
356f13a32a3SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
357f13a32a3SMatthew G. Knepley     ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
358f13a32a3SMatthew G. Knepley     ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
359f13a32a3SMatthew G. Knepley     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
360f13a32a3SMatthew G. Knepley     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
361552f7358SJed Brown   } else {
36255f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
36355f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
364552f7358SJed Brown   }
365552f7358SJed Brown   PetscFunctionReturn(0);
366552f7358SJed Brown }
367552f7358SJed Brown 
368d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
369d930f514SMatthew G. Knepley {
370d930f514SMatthew G. Knepley   DM                dm;
371d930f514SMatthew G. Knepley   MPI_Comm          comm;
372d930f514SMatthew G. Knepley   PetscViewerFormat format;
373d930f514SMatthew G. Knepley   Vec               v;
374d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
375d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
376d930f514SMatthew G. Knepley 
377d930f514SMatthew G. Knepley   PetscFunctionBegin;
378d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
379d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
3802c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
381d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
382d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
383d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
384d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
385d930f514SMatthew G. Knepley     const char *vecname;
386d930f514SMatthew G. Knepley     PetscInt    n, nroots;
387d930f514SMatthew G. Knepley 
388d930f514SMatthew G. Knepley     if (dm->sfNatural) {
389ca43db0aSBarry Smith       ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
390d930f514SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
391d930f514SMatthew G. Knepley       if (n == nroots) {
392d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
393d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
394d930f514SMatthew G. Knepley         ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
395d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
396d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
397d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
398d930f514SMatthew G. Knepley     } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
399d930f514SMatthew G. Knepley   } else {
400d930f514SMatthew G. Knepley     /* we are viewing a natural DMPlex vec. */
401d930f514SMatthew G. Knepley     v = originalv;
402d930f514SMatthew G. Knepley   }
403d930f514SMatthew G. Knepley   if (ishdf5) {
404d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
40539d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
406d930f514SMatthew G. Knepley #else
407d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
408d930f514SMatthew G. Knepley #endif
409d930f514SMatthew G. Knepley   } else if (isvtk) {
410d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
411d930f514SMatthew G. Knepley   } else {
412d930f514SMatthew G. Knepley     PetscBool isseq;
413d930f514SMatthew G. Knepley 
414d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
415d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
416d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
417d930f514SMatthew G. Knepley   }
418d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
419d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
420d930f514SMatthew G. Knepley }
421d930f514SMatthew G. Knepley 
4222c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
4232c40f234SMatthew G. Knepley {
4242c40f234SMatthew G. Knepley   DM             dm;
4252c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4262c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4272c40f234SMatthew G. Knepley 
4282c40f234SMatthew G. Knepley   PetscFunctionBegin;
4292c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4302c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4312c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4322c40f234SMatthew G. Knepley   if (ishdf5) {
4332c40f234SMatthew G. Knepley     DM          dmBC;
4342c40f234SMatthew G. Knepley     Vec         gv;
4352c40f234SMatthew G. Knepley     const char *name;
4362c40f234SMatthew G. Knepley 
4372c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
4382c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4392c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
4402c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
4412c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
4422c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4432c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4442c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4452c40f234SMatthew G. Knepley   } else {
4462c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
4472c40f234SMatthew G. Knepley   }
4482c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
4492c40f234SMatthew G. Knepley }
4502c40f234SMatthew G. Knepley 
4512c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
4522c40f234SMatthew G. Knepley {
4532c40f234SMatthew G. Knepley   DM             dm;
4542c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4552c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4562c40f234SMatthew G. Knepley 
4572c40f234SMatthew G. Knepley   PetscFunctionBegin;
4582c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4592c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4602c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4612c40f234SMatthew G. Knepley   if (ishdf5) {
462878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
46339d25373SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
464b136c2c9SMatthew G. Knepley #else
465b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
466878b459fSMatthew G. Knepley #endif
4672c40f234SMatthew G. Knepley   } else {
4682c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
469552f7358SJed Brown   }
470552f7358SJed Brown   PetscFunctionReturn(0);
471552f7358SJed Brown }
472552f7358SJed Brown 
473d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
474d930f514SMatthew G. Knepley {
475d930f514SMatthew G. Knepley   DM                dm;
476d930f514SMatthew G. Knepley   PetscViewerFormat format;
477d930f514SMatthew G. Knepley   PetscBool         ishdf5;
478d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
479d930f514SMatthew G. Knepley 
480d930f514SMatthew G. Knepley   PetscFunctionBegin;
481d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
482d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
483d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
484d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
485d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
486d930f514SMatthew G. Knepley     if (dm->sfNatural) {
487d930f514SMatthew G. Knepley       if (ishdf5) {
488d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
489d930f514SMatthew G. Knepley         Vec         v;
490d930f514SMatthew G. Knepley         const char *vecname;
491d930f514SMatthew G. Knepley 
492d930f514SMatthew G. Knepley         ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
493d930f514SMatthew G. Knepley         ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
494d930f514SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
49539d25373SMatthew G. Knepley         ierr = VecLoad_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
496d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
497d930f514SMatthew G. Knepley         ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
498d930f514SMatthew G. Knepley         ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
499d930f514SMatthew G. Knepley #else
500d930f514SMatthew G. Knepley         SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
501d930f514SMatthew G. Knepley #endif
502d930f514SMatthew G. Knepley       } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
503d930f514SMatthew G. Knepley     }
504d930f514SMatthew G. Knepley   }
505d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
506d930f514SMatthew G. Knepley }
507d930f514SMatthew G. Knepley 
5087cd05799SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
509731e8ddeSMatthew G. Knepley {
510731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
511731e8ddeSMatthew G. Knepley   Vec                coordinates;
512731e8ddeSMatthew G. Knepley   DMLabel            depthLabel;
513731e8ddeSMatthew G. Knepley   const char        *name[4];
514731e8ddeSMatthew G. Knepley   const PetscScalar *a;
515731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
516731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
517731e8ddeSMatthew G. Knepley 
518731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
519731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
520731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
521731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
522731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
523731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
524731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
525731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
526731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
527731e8ddeSMatthew G. Knepley   name[1]     = "edge";
528731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
529731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
530731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
531731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
532731e8ddeSMatthew G. Knepley     PetscInt  closureSize, cl;
533731e8ddeSMatthew G. Knepley 
534f347f43bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D:\n", c);CHKERRQ(ierr);
535731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
536731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
537731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
538731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
539731e8ddeSMatthew G. Knepley 
540731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
541731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
542731e8ddeSMatthew G. Knepley       if (!dof) continue;
543731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
544731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
545f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
546731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
547731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
548731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
549731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
55041477eefSMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
551731e8ddeSMatthew G. Knepley         }
552731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
553731e8ddeSMatthew G. Knepley       }
554731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
555731e8ddeSMatthew G. Knepley     }
556731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
557731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
558731e8ddeSMatthew G. Knepley   }
559731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
560731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
561731e8ddeSMatthew G. Knepley }
562731e8ddeSMatthew G. Knepley 
5637cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
564552f7358SJed Brown {
565552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
566552f7358SJed Brown   DM                cdm;
567552f7358SJed Brown   DMLabel           markers;
568552f7358SJed Brown   PetscSection      coordSection;
569552f7358SJed Brown   Vec               coordinates;
570552f7358SJed Brown   PetscViewerFormat format;
571552f7358SJed Brown   PetscErrorCode    ierr;
572552f7358SJed Brown 
573552f7358SJed Brown   PetscFunctionBegin;
574552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
575552f7358SJed Brown   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
576552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
577552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
578552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
579552f7358SJed Brown     const char *name;
580552f7358SJed Brown     PetscInt    maxConeSize, maxSupportSize;
581552f7358SJed Brown     PetscInt    pStart, pEnd, p;
582552f7358SJed Brown     PetscMPIInt rank, size;
583552f7358SJed Brown 
58482f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
58582f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
586552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
587552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
588552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
589552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "Mesh '%s':\n", name);CHKERRQ(ierr);
590552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "orientation is missing\n", name);CHKERRQ(ierr);
591552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "cap --> base:\n", name);CHKERRQ(ierr);
5924d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
5934d4c343aSBarry Smith     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max sizes cone: %D support: %D\n", rank,maxConeSize, maxSupportSize);CHKERRQ(ierr);
594552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
595552f7358SJed Brown       PetscInt dof, off, s;
596552f7358SJed Brown 
597552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
598552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
599552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
600e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
601552f7358SJed Brown       }
602552f7358SJed Brown     }
603552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
604552f7358SJed Brown     ierr = PetscViewerASCIIPrintf(viewer, "base <-- cap:\n", name);CHKERRQ(ierr);
605552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
606552f7358SJed Brown       PetscInt dof, off, c;
607552f7358SJed Brown 
608552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
609552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
610552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
611e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
612552f7358SJed Brown       }
613552f7358SJed Brown     }
614552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6154d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
6160298fd71SBarry Smith     ierr = PetscSectionGetChart(coordSection, &pStart, NULL);CHKERRQ(ierr);
617552f7358SJed Brown     if (pStart >= 0) {ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);}
618c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
619552f7358SJed Brown     ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);
620552f7358SJed Brown     if (size > 1) {
621552f7358SJed Brown       PetscSF sf;
622552f7358SJed Brown 
623552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
624552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
625552f7358SJed Brown     }
626552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
627552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
6280588280cSMatthew G. Knepley     const char  *name, *color;
6290588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
6300588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
631552f7358SJed Brown     PetscReal    scale         = 2.0;
6320588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
6330588280cSMatthew G. Knepley     double       tcoords[3];
634552f7358SJed Brown     PetscScalar *coords;
6350588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
636552f7358SJed Brown     PetscMPIInt  rank, size;
6370588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
638552f7358SJed Brown 
6390588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
640552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
641c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
6420588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
6430588280cSMatthew G. Knepley     numColors  = 10;
6440588280cSMatthew G. Knepley     numLColors = 10;
6450588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
646c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
647c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
648c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
6490588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
650c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
6510588280cSMatthew G. Knepley     if (!useColors) {
6520588280cSMatthew G. Knepley       numColors = 3;
6530588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
6540588280cSMatthew G. Knepley     }
655c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
6560588280cSMatthew G. Knepley     if (!useColors) {
6570588280cSMatthew G. Knepley       numLColors = 4;
6580588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
6590588280cSMatthew G. Knepley     }
66082f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
66182f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
662552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
663770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
6640588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
665552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
666552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
667552f7358SJed Brown \\usetikzlibrary{arrows}\n\
6680588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
6690588280cSMatthew G. Knepley     if (size > 1) {
670f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
671770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
672770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
673770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
674770b213bSMatthew G Knepley         } else if (p > 0) {
675770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
676770b213bSMatthew G Knepley         }
677770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
678770b213bSMatthew G Knepley       }
6790588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
6800588280cSMatthew G. Knepley     }
6810588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", 1.0);CHKERRQ(ierr);
682552f7358SJed Brown     /* Plot vertices */
683552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
684552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
6854d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
686552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
687552f7358SJed Brown       PetscInt  off, dof, d;
6880588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
689552f7358SJed Brown 
690552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
691552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
6920588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
693f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
6940588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
6950588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
6960588280cSMatthew G. Knepley         tcoords[d] = PetscAbsReal(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
6970588280cSMatthew G. Knepley       }
6980588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
6990588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
700552f7358SJed Brown       for (d = 0; d < dof; ++d) {
701552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
7020588280cSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]);CHKERRQ(ierr);
703552f7358SJed Brown       }
7040588280cSMatthew G. Knepley       color = colors[rank%numColors];
7050588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
7060588280cSMatthew G. Knepley         PetscInt val;
707c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7080588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
7090588280cSMatthew G. Knepley       }
7100588280cSMatthew G. Knepley       if (useNumbers) {
711e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
7120588280cSMatthew G. Knepley       } else {
713e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
7140588280cSMatthew G. Knepley       }
715552f7358SJed Brown     }
716552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
717552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
718552f7358SJed Brown     /* Plot edges */
7190588280cSMatthew G. Knepley     if (depth > 1) {ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);}
7200588280cSMatthew G. Knepley     if (dim < 3 && useNumbers) {
721552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
722552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
723552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
724552f7358SJed Brown         const PetscInt *cone;
725552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
726552f7358SJed Brown 
727552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
728f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
729552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
730552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
731552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
732552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
733552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
734552f7358SJed Brown         for (d = 0; d < dof; ++d) {
7350588280cSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
7360588280cSMatthew G. Knepley           tcoords[d] = PetscAbsReal(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
737552f7358SJed Brown         }
7380588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
7390588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
7400588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
7410588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
742f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
7430588280cSMatthew G. Knepley         }
7440588280cSMatthew G. Knepley         color = colors[rank%numColors];
7450588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
7460588280cSMatthew G. Knepley           PetscInt val;
747c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7480750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
7490588280cSMatthew G. Knepley         }
750e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
751552f7358SJed Brown       }
752552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
753552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
754552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
7550588280cSMatthew G. Knepley     }
756552f7358SJed Brown     /* Plot cells */
7570588280cSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
7580588280cSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
7590588280cSMatthew G. Knepley         const PetscInt *cone;
7600588280cSMatthew G. Knepley 
7610588280cSMatthew G. Knepley         color = colors[rank%numColors];
7620588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
7630588280cSMatthew G. Knepley           PetscInt val;
764c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
7650588280cSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
7660588280cSMatthew G. Knepley         }
7670588280cSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
768e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
7690588280cSMatthew G. Knepley       }
7700588280cSMatthew G. Knepley     } else {
771552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
772552f7358SJed Brown       for (c = cStart; c < cEnd; ++c) {
7730298fd71SBarry Smith         PetscInt *closure = NULL;
774552f7358SJed Brown         PetscInt  closureSize, firstPoint = -1;
775552f7358SJed Brown 
776552f7358SJed Brown         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
777552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
778552f7358SJed Brown         for (p = 0; p < closureSize*2; p += 2) {
779552f7358SJed Brown           const PetscInt point = closure[p];
780552f7358SJed Brown 
781552f7358SJed Brown           if ((point < vStart) || (point >= vEnd)) continue;
782552f7358SJed Brown           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
783e4b003c7SBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
784552f7358SJed Brown           if (firstPoint < 0) firstPoint = point;
785552f7358SJed Brown         }
786552f7358SJed Brown         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
787e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
788552f7358SJed Brown         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
789552f7358SJed Brown       }
7900588280cSMatthew G. Knepley     }
791552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
7924d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
7930588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
794770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
7950588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
7960588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
7970588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
7980588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
799552f7358SJed Brown   } else {
80082f516ccSBarry Smith     MPI_Comm    comm;
801834065abSMatthew G. Knepley     PetscInt   *sizes, *hybsizes;
802834065abSMatthew G. Knepley     PetscInt    locDepth, depth, dim, d, pMax[4];
803552f7358SJed Brown     PetscInt    pStart, pEnd, p;
804a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
8051143a3c0SMatthew G. Knepley     const char *name;
806552f7358SJed Brown     PetscMPIInt size;
807552f7358SJed Brown 
80882f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
809552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
810c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
8115f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
8121143a3c0SMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimensions:\n", name, dim);CHKERRQ(ierr);}
8131143a3c0SMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimensions:\n", dim);CHKERRQ(ierr);}
814552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
815b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
816bb81ee50SMatthew G. Knepley     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, &pMax[1], &pMax[0]);CHKERRQ(ierr);
817dcca6d9dSJed Brown     ierr = PetscMalloc2(size,&sizes,size,&hybsizes);CHKERRQ(ierr);
818552f7358SJed Brown     if (depth == 1) {
819552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
820552f7358SJed Brown       pEnd = pEnd - pStart;
821552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
822f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);CHKERRQ(ierr);
823552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
824552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
825552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
826552f7358SJed Brown       pEnd = pEnd - pStart;
827552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
828552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
829552f7358SJed Brown       for (p = 0; p < size; ++p) {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
830552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
831552f7358SJed Brown     } else {
832cbb7f117SMark Adams       PetscMPIInt rank;
833cbb7f117SMark Adams       ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
834552f7358SJed Brown       for (d = 0; d <= dim; d++) {
835552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
836834065abSMatthew G. Knepley         pEnd    -= pStart;
837834065abSMatthew G. Knepley         pMax[d] -= pStart;
838552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
839834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
840552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
841834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
842cbb7f117SMark Adams           if (!rank) {
843834065abSMatthew G. Knepley             if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
844834065abSMatthew G. Knepley             else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
845834065abSMatthew G. Knepley           }
846cbb7f117SMark Adams         }
847552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
848552f7358SJed Brown       }
849552f7358SJed Brown     }
850834065abSMatthew G. Knepley     ierr = PetscFree2(sizes,hybsizes);CHKERRQ(ierr);
851c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
852a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
853a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
854a57dd577SMatthew G Knepley       DMLabel         label;
855a57dd577SMatthew G Knepley       const char     *name;
856a57dd577SMatthew G Knepley       IS              valueIS;
857a57dd577SMatthew G Knepley       const PetscInt *values;
858a57dd577SMatthew G Knepley       PetscInt        numValues, v;
859a57dd577SMatthew G Knepley 
860c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
861c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
862a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
863f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata of sizes (", name, numValues);CHKERRQ(ierr);
864a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
865a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
866120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
867a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
868a57dd577SMatthew G Knepley         PetscInt size;
869a57dd577SMatthew G Knepley 
870a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
871a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
872f347f43bSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer, "%D", size);CHKERRQ(ierr);
873a57dd577SMatthew G Knepley       }
874a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
875120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
876a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
8774d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
878a57dd577SMatthew G Knepley     }
879a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
8808e7ff633SMatthew G. Knepley     if (cdm) {
8818e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
8828e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
8838e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
8848e7ff633SMatthew G. Knepley     }
885552f7358SJed Brown   }
886552f7358SJed Brown   PetscFunctionReturn(0);
887552f7358SJed Brown }
888552f7358SJed Brown 
8897cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
890e412dcbdSMatthew G. Knepley {
891e412dcbdSMatthew G. Knepley   PetscDraw          draw;
892e412dcbdSMatthew G. Knepley   DM                 cdm;
893e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
894e412dcbdSMatthew G. Knepley   Vec                coordinates;
895e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
896e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
897e412dcbdSMatthew G. Knepley   PetscBool          isnull;
898e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
899e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
900e412dcbdSMatthew G. Knepley 
901e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
902e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
903e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
904e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
905e412dcbdSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, &coordSection);CHKERRQ(ierr);
906e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
907e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
908e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
909e412dcbdSMatthew G. Knepley 
910e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
911e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
912e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
913e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
914e412dcbdSMatthew G. Knepley 
915e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
916e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
917e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
9180c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
9190c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
920e412dcbdSMatthew G. Knepley   }
921e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
922e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
923e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
924e412dcbdSMatthew G. Knepley 
925e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
926e412dcbdSMatthew G. Knepley     PetscScalar *coords = NULL;
927e412dcbdSMatthew G. Knepley     PetscInt     numCoords;
928e412dcbdSMatthew G. Knepley 
929e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
930e412dcbdSMatthew G. Knepley     switch (numCoords) {
931e412dcbdSMatthew G. Knepley     case 6:
9320c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9330c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9340c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
935e412dcbdSMatthew G. Knepley       break;
936e412dcbdSMatthew G. Knepley     case 8:
9370c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9380c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9390c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
9400c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
941e412dcbdSMatthew G. Knepley       break;
942e412dcbdSMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
943e412dcbdSMatthew G. Knepley     }
944e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
945e412dcbdSMatthew G. Knepley   }
946e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
947e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
948e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
949e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
950e412dcbdSMatthew G. Knepley }
951e412dcbdSMatthew G. Knepley 
952552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
953552f7358SJed Brown {
954e412dcbdSMatthew G. Knepley   PetscBool      iascii, ishdf5, isvtk, isdraw;
955552f7358SJed Brown   PetscErrorCode ierr;
956552f7358SJed Brown 
957552f7358SJed Brown   PetscFunctionBegin;
958552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
959552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
960552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
961fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
962c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
963e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
964552f7358SJed Brown   if (iascii) {
965552f7358SJed Brown     ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
966c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
967c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
9687afe7537SMatthew G. Knepley     ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_VIZ);CHKERRQ(ierr);
96939d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
9707afe7537SMatthew G. Knepley     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
971c6ccd67eSMatthew G. Knepley #else
972c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
973552f7358SJed Brown #endif
974e412dcbdSMatthew G. Knepley   } else if (isvtk) {
975fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
976e412dcbdSMatthew G. Knepley   } else if (isdraw) {
977e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
978fcf6c8fdSToby Isaac   }
979552f7358SJed Brown   PetscFunctionReturn(0);
980552f7358SJed Brown }
981552f7358SJed Brown 
9822c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
9832c40f234SMatthew G. Knepley {
9842c40f234SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
9852c40f234SMatthew G. Knepley   PetscErrorCode ierr;
9862c40f234SMatthew G. Knepley 
9872c40f234SMatthew G. Knepley   PetscFunctionBegin;
9882c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9892c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
9902c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERBINARY, &isbinary);CHKERRQ(ierr);
9912c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
9922c40f234SMatthew G. Knepley   if (isbinary) {SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Do not yet support binary viewers");}
9932c40f234SMatthew G. Knepley   else if (ishdf5) {
9942c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
99539d25373SMatthew G. Knepley     ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
9962c40f234SMatthew G. Knepley #else
9972c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
998552f7358SJed Brown #endif
999552f7358SJed Brown   }
1000552f7358SJed Brown   PetscFunctionReturn(0);
1001552f7358SJed Brown }
1002552f7358SJed Brown 
1003552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1004552f7358SJed Brown {
1005552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1006552f7358SJed Brown   PetscErrorCode ierr;
1007552f7358SJed Brown 
1008552f7358SJed Brown   PetscFunctionBegin;
10090d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1010552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1011552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1012552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1013552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1014be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1015552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1016552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1017d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1018d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
101977623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1020a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
1021552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1022552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1023a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1024a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1025d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1026d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1027d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1028d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1029d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1030d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1031fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
1032552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1033552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1034713918a9SToby Isaac   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMAdaptLabel_C",NULL);CHKERRQ(ierr);
1035552f7358SJed Brown   PetscFunctionReturn(0);
1036552f7358SJed Brown }
1037552f7358SJed Brown 
1038b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1039552f7358SJed Brown {
10408d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1041acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1042552f7358SJed Brown   PetscInt               localSize;
1043837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1044552f7358SJed Brown   PetscErrorCode         ierr;
1045b412c318SBarry Smith   MatType                mtype;
10461428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1047552f7358SJed Brown 
1048552f7358SJed Brown   PetscFunctionBegin;
1049607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1050b412c318SBarry Smith   mtype = dm->mattype;
1051552f7358SJed Brown   ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1052552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1053552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
105482f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1055552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1056552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1057552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1058acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1059acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1060552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1061552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1062552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1063552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1064552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1065552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1066552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1067837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1068552f7358SJed Brown   if (!isShell) {
1069be36d101SStefano Zampini     PetscSection subSection;
1070837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
1071be36d101SStefano Zampini     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal, bsMax, bsMin, *ltogidx, lsize;
1072fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1073552f7358SJed Brown 
107400140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1075be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1076be36d101SStefano Zampini       PetscSection section;
1077be36d101SStefano Zampini       PetscInt     size;
1078be36d101SStefano Zampini 
1079be36d101SStefano Zampini       ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1080be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1081be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1082be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1083be36d101SStefano Zampini     } else {
108400140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1085be36d101SStefano Zampini     }
1086552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1087be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1088a9d99c84SMatthew G. Knepley       PetscInt bdof;
1089a9d99c84SMatthew G. Knepley 
1090552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1091fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
10921d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
10931d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
10941d17a0a3SMatthew G. Knepley       if (dof) {
10951d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1096be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1097be36d101SStefano Zampini       }
1098be36d101SStefano Zampini       if (isMatIS) {
1099be36d101SStefano Zampini         PetscInt loff,c,off;
1100be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1101be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1102be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1103552f7358SJed Brown       }
11042a28c762SMatthew G Knepley     }
11052a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
11062a28c762SMatthew G Knepley     bsLocal = bs;
1107b2566f29SBarry Smith     ierr = MPIU_Allreduce(&bsLocal, &bsMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
11082a28c762SMatthew G Knepley     bsLocal = bs < 0 ? bsMax : bs;
1109b2566f29SBarry Smith     ierr = MPIU_Allreduce(&bsLocal, &bsMin, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1110bff27382SMatthew G. Knepley     if (bsMin != bsMax) {bs = 1;}
1111bff27382SMatthew G. Knepley     else                {bs = bsMax;}
11124fa26246SMatthew G. Knepley     bs   = bs < 0 ? 1 : bs;
1113be36d101SStefano Zampini     if (isMatIS) {
11144fa26246SMatthew G. Knepley       PetscInt l;
11154fa26246SMatthew G. Knepley       /* Must reduce indices by blocksize */
11164fa26246SMatthew G. Knepley       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] /= bs;
11174fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1118be36d101SStefano Zampini     }
1119be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1120be36d101SStefano Zampini     if (isMatIS) {
1121be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1122be36d101SStefano Zampini     }
11231795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
11248d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1125552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1126552f7358SJed Brown   }
1127b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1128552f7358SJed Brown   PetscFunctionReturn(0);
1129552f7358SJed Brown }
1130552f7358SJed Brown 
11317cd05799SMatthew G. Knepley /*@
1132a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1133be36d101SStefano Zampini 
1134be36d101SStefano Zampini   Not collective
1135be36d101SStefano Zampini 
1136be36d101SStefano Zampini   Input Parameter:
1137be36d101SStefano Zampini . mesh - The DMPlex
1138be36d101SStefano Zampini 
1139be36d101SStefano Zampini   Output Parameters:
1140be36d101SStefano Zampini . subsection - The subdomain section
1141be36d101SStefano Zampini 
1142be36d101SStefano Zampini   Level: developer
1143be36d101SStefano Zampini 
1144be36d101SStefano Zampini .seealso:
11457cd05799SMatthew G. Knepley @*/
1146be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1147be36d101SStefano Zampini {
1148be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1149be36d101SStefano Zampini   PetscErrorCode ierr;
1150be36d101SStefano Zampini 
1151be36d101SStefano Zampini   PetscFunctionBegin;
1152be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1153be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1154be36d101SStefano Zampini     PetscSection section;
1155be36d101SStefano Zampini     PetscSF      sf;
1156be36d101SStefano Zampini 
1157be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
1158be36d101SStefano Zampini     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
1159be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1160be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1161be36d101SStefano Zampini   }
1162be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1163be36d101SStefano Zampini   PetscFunctionReturn(0);
1164be36d101SStefano Zampini }
1165be36d101SStefano Zampini 
1166552f7358SJed Brown /*@
1167552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1168552f7358SJed Brown 
1169552f7358SJed Brown   Not collective
1170552f7358SJed Brown 
1171552f7358SJed Brown   Input Parameter:
1172552f7358SJed Brown . mesh - The DMPlex
1173552f7358SJed Brown 
1174552f7358SJed Brown   Output Parameters:
1175552f7358SJed Brown + pStart - The first mesh point
1176552f7358SJed Brown - pEnd   - The upper bound for mesh points
1177552f7358SJed Brown 
1178552f7358SJed Brown   Level: beginner
1179552f7358SJed Brown 
1180552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1181552f7358SJed Brown @*/
1182552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1183552f7358SJed Brown {
1184552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1185552f7358SJed Brown   PetscErrorCode ierr;
1186552f7358SJed Brown 
1187552f7358SJed Brown   PetscFunctionBegin;
1188552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1189552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1190552f7358SJed Brown   PetscFunctionReturn(0);
1191552f7358SJed Brown }
1192552f7358SJed Brown 
1193552f7358SJed Brown /*@
1194552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1195552f7358SJed Brown 
1196552f7358SJed Brown   Not collective
1197552f7358SJed Brown 
1198552f7358SJed Brown   Input Parameters:
1199552f7358SJed Brown + mesh - The DMPlex
1200552f7358SJed Brown . pStart - The first mesh point
1201552f7358SJed Brown - pEnd   - The upper bound for mesh points
1202552f7358SJed Brown 
1203552f7358SJed Brown   Output Parameters:
1204552f7358SJed Brown 
1205552f7358SJed Brown   Level: beginner
1206552f7358SJed Brown 
1207552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1208552f7358SJed Brown @*/
1209552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1210552f7358SJed Brown {
1211552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1212552f7358SJed Brown   PetscErrorCode ierr;
1213552f7358SJed Brown 
1214552f7358SJed Brown   PetscFunctionBegin;
1215552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1216552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1217552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1218552f7358SJed Brown   PetscFunctionReturn(0);
1219552f7358SJed Brown }
1220552f7358SJed Brown 
1221552f7358SJed Brown /*@
1222552f7358SJed Brown   DMPlexGetConeSize - Return the number of in-edges for this point in the Sieve DAG
1223552f7358SJed Brown 
1224552f7358SJed Brown   Not collective
1225552f7358SJed Brown 
1226552f7358SJed Brown   Input Parameters:
1227552f7358SJed Brown + mesh - The DMPlex
1228552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1229552f7358SJed Brown 
1230552f7358SJed Brown   Output Parameter:
1231552f7358SJed Brown . size - The cone size for point p
1232552f7358SJed Brown 
1233552f7358SJed Brown   Level: beginner
1234552f7358SJed Brown 
1235552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1236552f7358SJed Brown @*/
1237552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1238552f7358SJed Brown {
1239552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1240552f7358SJed Brown   PetscErrorCode ierr;
1241552f7358SJed Brown 
1242552f7358SJed Brown   PetscFunctionBegin;
1243552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1244552f7358SJed Brown   PetscValidPointer(size, 3);
1245552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1246552f7358SJed Brown   PetscFunctionReturn(0);
1247552f7358SJed Brown }
1248552f7358SJed Brown 
1249552f7358SJed Brown /*@
1250552f7358SJed Brown   DMPlexSetConeSize - Set the number of in-edges for this point in the Sieve DAG
1251552f7358SJed Brown 
1252552f7358SJed Brown   Not collective
1253552f7358SJed Brown 
1254552f7358SJed Brown   Input Parameters:
1255552f7358SJed Brown + mesh - The DMPlex
1256552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1257552f7358SJed Brown - size - The cone size for point p
1258552f7358SJed Brown 
1259552f7358SJed Brown   Output Parameter:
1260552f7358SJed Brown 
1261552f7358SJed Brown   Note:
1262552f7358SJed Brown   This should be called after DMPlexSetChart().
1263552f7358SJed Brown 
1264552f7358SJed Brown   Level: beginner
1265552f7358SJed Brown 
1266552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1267552f7358SJed Brown @*/
1268552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1269552f7358SJed Brown {
1270552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1271552f7358SJed Brown   PetscErrorCode ierr;
1272552f7358SJed Brown 
1273552f7358SJed Brown   PetscFunctionBegin;
1274552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1275552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
12760d644c17SKarl Rupp 
1277552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1278552f7358SJed Brown   PetscFunctionReturn(0);
1279552f7358SJed Brown }
1280552f7358SJed Brown 
1281f5a469b9SMatthew G. Knepley /*@
1282f5a469b9SMatthew G. Knepley   DMPlexAddConeSize - Add the given number of in-edges to this point in the Sieve DAG
1283f5a469b9SMatthew G. Knepley 
1284f5a469b9SMatthew G. Knepley   Not collective
1285f5a469b9SMatthew G. Knepley 
1286f5a469b9SMatthew G. Knepley   Input Parameters:
1287f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1288f5a469b9SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1289f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1290f5a469b9SMatthew G. Knepley 
1291f5a469b9SMatthew G. Knepley   Output Parameter:
1292f5a469b9SMatthew G. Knepley 
1293f5a469b9SMatthew G. Knepley   Note:
1294f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1295f5a469b9SMatthew G. Knepley 
1296f5a469b9SMatthew G. Knepley   Level: beginner
1297f5a469b9SMatthew G. Knepley 
1298f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1299f5a469b9SMatthew G. Knepley @*/
1300f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1301f5a469b9SMatthew G. Knepley {
1302f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1303f5a469b9SMatthew G. Knepley   PetscInt       csize;
1304f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1305f5a469b9SMatthew G. Knepley 
1306f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1307f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1308f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1309f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1310f5a469b9SMatthew G. Knepley 
1311f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1312f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1313f5a469b9SMatthew G. Knepley }
1314f5a469b9SMatthew G. Knepley 
1315552f7358SJed Brown /*@C
1316552f7358SJed Brown   DMPlexGetCone - Return the points on the in-edges for this point in the Sieve DAG
1317552f7358SJed Brown 
1318552f7358SJed Brown   Not collective
1319552f7358SJed Brown 
1320552f7358SJed Brown   Input Parameters:
1321552f7358SJed Brown + mesh - The DMPlex
1322552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1323552f7358SJed Brown 
1324552f7358SJed Brown   Output Parameter:
1325552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1326552f7358SJed Brown 
1327552f7358SJed Brown   Level: beginner
1328552f7358SJed Brown 
13293813dfbdSMatthew G Knepley   Fortran Notes:
13303813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
13313813dfbdSMatthew G Knepley   include petsc.h90 in your code.
13323813dfbdSMatthew G Knepley 
13333813dfbdSMatthew G Knepley   You must also call DMPlexRestoreCone() after you finish using the returned array.
1334552f7358SJed Brown 
1335552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart()
1336552f7358SJed Brown @*/
1337552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1338552f7358SJed Brown {
1339552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1340552f7358SJed Brown   PetscInt       off;
1341552f7358SJed Brown   PetscErrorCode ierr;
1342552f7358SJed Brown 
1343552f7358SJed Brown   PetscFunctionBegin;
1344552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1345552f7358SJed Brown   PetscValidPointer(cone, 3);
1346552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1347552f7358SJed Brown   *cone = &mesh->cones[off];
1348552f7358SJed Brown   PetscFunctionReturn(0);
1349552f7358SJed Brown }
1350552f7358SJed Brown 
1351552f7358SJed Brown /*@
1352552f7358SJed Brown   DMPlexSetCone - Set the points on the in-edges for this point in the Sieve DAG
1353552f7358SJed Brown 
1354552f7358SJed Brown   Not collective
1355552f7358SJed Brown 
1356552f7358SJed Brown   Input Parameters:
1357552f7358SJed Brown + mesh - The DMPlex
1358552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1359552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1360552f7358SJed Brown 
1361552f7358SJed Brown   Output Parameter:
1362552f7358SJed Brown 
1363552f7358SJed Brown   Note:
1364552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1365552f7358SJed Brown 
1366552f7358SJed Brown   Level: beginner
1367552f7358SJed Brown 
1368552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1369552f7358SJed Brown @*/
1370552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1371552f7358SJed Brown {
1372552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1373552f7358SJed Brown   PetscInt       pStart, pEnd;
1374552f7358SJed Brown   PetscInt       dof, off, c;
1375552f7358SJed Brown   PetscErrorCode ierr;
1376552f7358SJed Brown 
1377552f7358SJed Brown   PetscFunctionBegin;
1378552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1379552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1380552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1381552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1382552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
138382f516ccSBarry 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);
1384552f7358SJed Brown   for (c = 0; c < dof; ++c) {
138582f516ccSBarry 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);
1386552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1387552f7358SJed Brown   }
1388552f7358SJed Brown   PetscFunctionReturn(0);
1389552f7358SJed Brown }
1390552f7358SJed Brown 
1391552f7358SJed Brown /*@C
1392552f7358SJed Brown   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the Sieve DAG
1393552f7358SJed Brown 
1394552f7358SJed Brown   Not collective
1395552f7358SJed Brown 
1396552f7358SJed Brown   Input Parameters:
1397552f7358SJed Brown + mesh - The DMPlex
1398552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1399552f7358SJed Brown 
1400552f7358SJed Brown   Output Parameter:
1401552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1402552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1403552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1404552f7358SJed Brown                     the index of the cone point on which to start.
1405552f7358SJed Brown 
1406552f7358SJed Brown   Level: beginner
1407552f7358SJed Brown 
14083813dfbdSMatthew G Knepley   Fortran Notes:
14093813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
14103813dfbdSMatthew G Knepley   include petsc.h90 in your code.
14113813dfbdSMatthew G Knepley 
14123813dfbdSMatthew G Knepley   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1413552f7358SJed Brown 
1414552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1415552f7358SJed Brown @*/
1416552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1417552f7358SJed Brown {
1418552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1419552f7358SJed Brown   PetscInt       off;
1420552f7358SJed Brown   PetscErrorCode ierr;
1421552f7358SJed Brown 
1422552f7358SJed Brown   PetscFunctionBegin;
1423552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1424552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1425552f7358SJed Brown   {
1426552f7358SJed Brown     PetscInt dof;
1427552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1428552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1429552f7358SJed Brown   }
1430552f7358SJed Brown #endif
1431552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
14320d644c17SKarl Rupp 
1433552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1434552f7358SJed Brown   PetscFunctionReturn(0);
1435552f7358SJed Brown }
1436552f7358SJed Brown 
1437552f7358SJed Brown /*@
1438552f7358SJed Brown   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the Sieve DAG
1439552f7358SJed Brown 
1440552f7358SJed Brown   Not collective
1441552f7358SJed Brown 
1442552f7358SJed Brown   Input Parameters:
1443552f7358SJed Brown + mesh - The DMPlex
1444552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1445552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1446552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1447552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1448552f7358SJed Brown                     the index of the cone point on which to start.
1449552f7358SJed Brown 
1450552f7358SJed Brown   Output Parameter:
1451552f7358SJed Brown 
1452552f7358SJed Brown   Note:
1453552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1454552f7358SJed Brown 
1455552f7358SJed Brown   Level: beginner
1456552f7358SJed Brown 
1457552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1458552f7358SJed Brown @*/
1459552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
1460552f7358SJed Brown {
1461552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1462552f7358SJed Brown   PetscInt       pStart, pEnd;
1463552f7358SJed Brown   PetscInt       dof, off, c;
1464552f7358SJed Brown   PetscErrorCode ierr;
1465552f7358SJed Brown 
1466552f7358SJed Brown   PetscFunctionBegin;
1467552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1468552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1469552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1470552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
1471552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
147282f516ccSBarry 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);
1473552f7358SJed Brown   for (c = 0; c < dof; ++c) {
1474552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
1475552f7358SJed Brown 
1476552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
147782f516ccSBarry 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);
1478552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
1479552f7358SJed Brown   }
1480552f7358SJed Brown   PetscFunctionReturn(0);
1481552f7358SJed Brown }
1482552f7358SJed Brown 
14837cd05799SMatthew G. Knepley /*@
14847cd05799SMatthew G. Knepley   DMPlexInsertCone - Insert a point into the in-edges for the point p in the Sieve DAG
14857cd05799SMatthew G. Knepley 
14867cd05799SMatthew G. Knepley   Not collective
14877cd05799SMatthew G. Knepley 
14887cd05799SMatthew G. Knepley   Input Parameters:
14897cd05799SMatthew G. Knepley + mesh - The DMPlex
14907cd05799SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
14917cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
14927cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
14937cd05799SMatthew G. Knepley 
14947cd05799SMatthew G. Knepley   Level: beginner
14957cd05799SMatthew G. Knepley 
14967cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
14977cd05799SMatthew G. Knepley @*/
1498552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
1499552f7358SJed Brown {
1500552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1501552f7358SJed Brown   PetscInt       pStart, pEnd;
1502552f7358SJed Brown   PetscInt       dof, off;
1503552f7358SJed Brown   PetscErrorCode ierr;
1504552f7358SJed Brown 
1505552f7358SJed Brown   PetscFunctionBegin;
1506552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1507552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
150882f516ccSBarry 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);
150982f516ccSBarry 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);
151077c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
151177c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
151277c88f5bSMatthew 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);
1513552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
1514552f7358SJed Brown   PetscFunctionReturn(0);
1515552f7358SJed Brown }
1516552f7358SJed Brown 
15177cd05799SMatthew G. Knepley /*@
15187cd05799SMatthew G. Knepley   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the Sieve DAG
15197cd05799SMatthew G. Knepley 
15207cd05799SMatthew G. Knepley   Not collective
15217cd05799SMatthew G. Knepley 
15227cd05799SMatthew G. Knepley   Input Parameters:
15237cd05799SMatthew G. Knepley + mesh - The DMPlex
15247cd05799SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
15257cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
15267cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
15277cd05799SMatthew G. Knepley 
15287cd05799SMatthew G. Knepley   Level: beginner
15297cd05799SMatthew G. Knepley 
15307cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
15317cd05799SMatthew G. Knepley @*/
153277c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
153377c88f5bSMatthew G Knepley {
153477c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
153577c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
153677c88f5bSMatthew G Knepley   PetscInt       dof, off;
153777c88f5bSMatthew G Knepley   PetscErrorCode ierr;
153877c88f5bSMatthew G Knepley 
153977c88f5bSMatthew G Knepley   PetscFunctionBegin;
154077c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
154177c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
154277c88f5bSMatthew 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);
154377c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
154477c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
154577c88f5bSMatthew 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);
154677c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
154777c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
154877c88f5bSMatthew G Knepley }
154977c88f5bSMatthew G Knepley 
1550552f7358SJed Brown /*@
1551552f7358SJed Brown   DMPlexGetSupportSize - Return the number of out-edges for this point in the Sieve DAG
1552552f7358SJed Brown 
1553552f7358SJed Brown   Not collective
1554552f7358SJed Brown 
1555552f7358SJed Brown   Input Parameters:
1556552f7358SJed Brown + mesh - The DMPlex
1557552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1558552f7358SJed Brown 
1559552f7358SJed Brown   Output Parameter:
1560552f7358SJed Brown . size - The support size for point p
1561552f7358SJed Brown 
1562552f7358SJed Brown   Level: beginner
1563552f7358SJed Brown 
1564552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
1565552f7358SJed Brown @*/
1566552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
1567552f7358SJed Brown {
1568552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1569552f7358SJed Brown   PetscErrorCode ierr;
1570552f7358SJed Brown 
1571552f7358SJed Brown   PetscFunctionBegin;
1572552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1573552f7358SJed Brown   PetscValidPointer(size, 3);
1574552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
1575552f7358SJed Brown   PetscFunctionReturn(0);
1576552f7358SJed Brown }
1577552f7358SJed Brown 
1578552f7358SJed Brown /*@
1579552f7358SJed Brown   DMPlexSetSupportSize - Set the number of out-edges for this point in the Sieve DAG
1580552f7358SJed Brown 
1581552f7358SJed Brown   Not collective
1582552f7358SJed Brown 
1583552f7358SJed Brown   Input Parameters:
1584552f7358SJed Brown + mesh - The DMPlex
1585552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1586552f7358SJed Brown - size - The support size for point p
1587552f7358SJed Brown 
1588552f7358SJed Brown   Output Parameter:
1589552f7358SJed Brown 
1590552f7358SJed Brown   Note:
1591552f7358SJed Brown   This should be called after DMPlexSetChart().
1592552f7358SJed Brown 
1593552f7358SJed Brown   Level: beginner
1594552f7358SJed Brown 
1595552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
1596552f7358SJed Brown @*/
1597552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
1598552f7358SJed Brown {
1599552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1600552f7358SJed Brown   PetscErrorCode ierr;
1601552f7358SJed Brown 
1602552f7358SJed Brown   PetscFunctionBegin;
1603552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1604552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
16050d644c17SKarl Rupp 
1606552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
1607552f7358SJed Brown   PetscFunctionReturn(0);
1608552f7358SJed Brown }
1609552f7358SJed Brown 
1610552f7358SJed Brown /*@C
1611552f7358SJed Brown   DMPlexGetSupport - Return the points on the out-edges for this point in the Sieve DAG
1612552f7358SJed Brown 
1613552f7358SJed Brown   Not collective
1614552f7358SJed Brown 
1615552f7358SJed Brown   Input Parameters:
1616552f7358SJed Brown + mesh - The DMPlex
1617552f7358SJed Brown - p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1618552f7358SJed Brown 
1619552f7358SJed Brown   Output Parameter:
1620552f7358SJed Brown . support - An array of points which are on the out-edges for point p
1621552f7358SJed Brown 
1622552f7358SJed Brown   Level: beginner
1623552f7358SJed Brown 
16243813dfbdSMatthew G Knepley   Fortran Notes:
16253813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16263813dfbdSMatthew G Knepley   include petsc.h90 in your code.
16273813dfbdSMatthew G Knepley 
16283813dfbdSMatthew G Knepley   You must also call DMPlexRestoreSupport() after you finish using the returned array.
16293813dfbdSMatthew G Knepley 
1630552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1631552f7358SJed Brown @*/
1632552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
1633552f7358SJed Brown {
1634552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1635552f7358SJed Brown   PetscInt       off;
1636552f7358SJed Brown   PetscErrorCode ierr;
1637552f7358SJed Brown 
1638552f7358SJed Brown   PetscFunctionBegin;
1639552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1640552f7358SJed Brown   PetscValidPointer(support, 3);
1641552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
1642552f7358SJed Brown   *support = &mesh->supports[off];
1643552f7358SJed Brown   PetscFunctionReturn(0);
1644552f7358SJed Brown }
1645552f7358SJed Brown 
1646552f7358SJed Brown /*@
1647552f7358SJed Brown   DMPlexSetSupport - Set the points on the out-edges for this point in the Sieve DAG
1648552f7358SJed Brown 
1649552f7358SJed Brown   Not collective
1650552f7358SJed Brown 
1651552f7358SJed Brown   Input Parameters:
1652552f7358SJed Brown + mesh - The DMPlex
1653552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1654552f7358SJed Brown - support - An array of points which are on the in-edges for point p
1655552f7358SJed Brown 
1656552f7358SJed Brown   Output Parameter:
1657552f7358SJed Brown 
1658552f7358SJed Brown   Note:
1659552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
1660552f7358SJed Brown 
1661552f7358SJed Brown   Level: beginner
1662552f7358SJed Brown 
1663552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
1664552f7358SJed Brown @*/
1665552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
1666552f7358SJed Brown {
1667552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1668552f7358SJed Brown   PetscInt       pStart, pEnd;
1669552f7358SJed Brown   PetscInt       dof, off, c;
1670552f7358SJed Brown   PetscErrorCode ierr;
1671552f7358SJed Brown 
1672552f7358SJed Brown   PetscFunctionBegin;
1673552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1674552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1675552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1676552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
1677552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
167882f516ccSBarry 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);
1679552f7358SJed Brown   for (c = 0; c < dof; ++c) {
168082f516ccSBarry 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);
1681552f7358SJed Brown     mesh->supports[off+c] = support[c];
1682552f7358SJed Brown   }
1683552f7358SJed Brown   PetscFunctionReturn(0);
1684552f7358SJed Brown }
1685552f7358SJed Brown 
16867cd05799SMatthew G. Knepley /*@
16877cd05799SMatthew G. Knepley   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the Sieve DAG
16887cd05799SMatthew G. Knepley 
16897cd05799SMatthew G. Knepley   Not collective
16907cd05799SMatthew G. Knepley 
16917cd05799SMatthew G. Knepley   Input Parameters:
16927cd05799SMatthew G. Knepley + mesh - The DMPlex
16937cd05799SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
16947cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
16957cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
16967cd05799SMatthew G. Knepley 
16977cd05799SMatthew G. Knepley   Level: beginner
16987cd05799SMatthew G. Knepley 
16997cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
17007cd05799SMatthew G. Knepley @*/
1701552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
1702552f7358SJed Brown {
1703552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1704552f7358SJed Brown   PetscInt       pStart, pEnd;
1705552f7358SJed Brown   PetscInt       dof, off;
1706552f7358SJed Brown   PetscErrorCode ierr;
1707552f7358SJed Brown 
1708552f7358SJed Brown   PetscFunctionBegin;
1709552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1710552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
1711552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
1712552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
171382f516ccSBarry 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);
171482f516ccSBarry 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);
171582f516ccSBarry 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);
1716552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
1717552f7358SJed Brown   PetscFunctionReturn(0);
1718552f7358SJed Brown }
1719552f7358SJed Brown 
1720552f7358SJed Brown /*@C
1721552f7358SJed Brown   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the Sieve DAG
1722552f7358SJed Brown 
1723552f7358SJed Brown   Not collective
1724552f7358SJed Brown 
1725552f7358SJed Brown   Input Parameters:
1726552f7358SJed Brown + mesh - The DMPlex
1727552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
1728552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
17290298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
1730552f7358SJed Brown 
1731552f7358SJed Brown   Output Parameters:
1732552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
1733552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
1734552f7358SJed Brown 
1735552f7358SJed Brown   Note:
17360298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
1737552f7358SJed Brown 
17383813dfbdSMatthew G Knepley   Fortran Notes:
17393813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
17403813dfbdSMatthew G Knepley   include petsc.h90 in your code.
17413813dfbdSMatthew G Knepley 
17423813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
17433813dfbdSMatthew G Knepley 
1744552f7358SJed Brown   Level: beginner
1745552f7358SJed Brown 
1746552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
1747552f7358SJed Brown @*/
1748552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
1749552f7358SJed Brown {
1750552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
1751552f7358SJed Brown   PetscInt       *closure, *fifo;
17520298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
1753552f7358SJed Brown   PetscInt        tmpSize, t;
1754552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
1755552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
1756552f7358SJed Brown   PetscErrorCode  ierr;
1757552f7358SJed Brown 
1758552f7358SJed Brown   PetscFunctionBegin;
1759552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1760552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1761552f7358SJed Brown   /* This is only 1-level */
1762552f7358SJed Brown   if (useCone) {
1763552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
1764552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
1765552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
1766552f7358SJed Brown   } else {
1767552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
1768552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
1769552f7358SJed Brown   }
1770bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
1771bfbcdd7aSMatthew G. Knepley     if (*points) {
1772bfbcdd7aSMatthew G. Knepley       closure = *points;
1773bfbcdd7aSMatthew G. Knepley     } else {
1774bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
1775bfbcdd7aSMatthew G. Knepley       ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1776bfbcdd7aSMatthew G. Knepley     }
1777bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
1778bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
1779bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
1780bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
1781bfbcdd7aSMatthew G. Knepley     }
1782bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
1783bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
1784bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
1785bfbcdd7aSMatthew G. Knepley   }
178624c766afSToby Isaac   {
178724c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
178824c766afSToby Isaac 
178924c766afSToby Isaac     c = mesh->maxConeSize;
179024c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
179124c766afSToby Isaac     s = mesh->maxSupportSize;
179224c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
179324c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
179424c766afSToby Isaac   }
1795bfbcdd7aSMatthew G. Knepley   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1796bfbcdd7aSMatthew G. Knepley   if (*points) {
1797bfbcdd7aSMatthew G. Knepley     closure = *points;
1798bfbcdd7aSMatthew G. Knepley   } else {
1799bfbcdd7aSMatthew G. Knepley     ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
1800bfbcdd7aSMatthew G. Knepley   }
1801bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
1802552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
1803552f7358SJed Brown     const PetscInt cp = tmp[t];
1804552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
1805552f7358SJed Brown 
1806552f7358SJed Brown     closure[closureSize]   = cp;
1807552f7358SJed Brown     closure[closureSize+1] = co;
1808552f7358SJed Brown     fifo[fifoSize]         = cp;
1809552f7358SJed Brown     fifo[fifoSize+1]       = co;
1810552f7358SJed Brown   }
1811bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
1812552f7358SJed Brown   while (fifoSize - fifoStart) {
1813552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
1814552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
1815552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
1816552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
1817552f7358SJed Brown 
1818552f7358SJed Brown     if (useCone) {
1819552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
1820552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
1821552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
1822552f7358SJed Brown     } else {
1823552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
1824552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
18250298fd71SBarry Smith       tmpO = NULL;
1826552f7358SJed Brown     }
1827552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
1828552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
1829552f7358SJed Brown       const PetscInt cp = tmp[i];
18302e1b13c2SMatthew 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. */
18312e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
18322e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
18332e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
1834552f7358SJed Brown       PetscInt       c;
1835552f7358SJed Brown 
18362e1b13c2SMatthew G. Knepley       if (rev) {
18372e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
18382e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
18392e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
18402e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
18412e1b13c2SMatthew G. Knepley       }
1842552f7358SJed Brown       /* Check for duplicate */
1843552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
1844552f7358SJed Brown         if (closure[c] == cp) break;
1845552f7358SJed Brown       }
1846552f7358SJed Brown       if (c == closureSize) {
1847552f7358SJed Brown         closure[closureSize]   = cp;
1848552f7358SJed Brown         closure[closureSize+1] = co;
1849552f7358SJed Brown         fifo[fifoSize]         = cp;
1850552f7358SJed Brown         fifo[fifoSize+1]       = co;
1851552f7358SJed Brown         closureSize           += 2;
1852552f7358SJed Brown         fifoSize              += 2;
1853552f7358SJed Brown       }
1854552f7358SJed Brown     }
1855552f7358SJed Brown     fifoStart += 2;
1856552f7358SJed Brown   }
1857552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
1858552f7358SJed Brown   if (points)    *points    = closure;
1859552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
1860552f7358SJed Brown   PetscFunctionReturn(0);
1861552f7358SJed Brown }
1862552f7358SJed Brown 
18639bf0dad6SMatthew G. Knepley /*@C
18649bf0dad6SMatthew 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
18659bf0dad6SMatthew G. Knepley 
18669bf0dad6SMatthew G. Knepley   Not collective
18679bf0dad6SMatthew G. Knepley 
18689bf0dad6SMatthew G. Knepley   Input Parameters:
18699bf0dad6SMatthew G. Knepley + mesh - The DMPlex
18709bf0dad6SMatthew G. Knepley . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
18719bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
18729bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
18739bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
18749bf0dad6SMatthew G. Knepley 
18759bf0dad6SMatthew G. Knepley   Output Parameters:
18769bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
18779bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
18789bf0dad6SMatthew G. Knepley 
18799bf0dad6SMatthew G. Knepley   Note:
18809bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
18819bf0dad6SMatthew G. Knepley 
18829bf0dad6SMatthew G. Knepley   Fortran Notes:
18839bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
18849bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
18859bf0dad6SMatthew G. Knepley 
18869bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
18879bf0dad6SMatthew G. Knepley 
18889bf0dad6SMatthew G. Knepley   Level: beginner
18899bf0dad6SMatthew G. Knepley 
18909bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
18919bf0dad6SMatthew G. Knepley @*/
18929bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
18939bf0dad6SMatthew G. Knepley {
18949bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
18959bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
18969bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
18979bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
18989bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
18999bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
19009bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
19019bf0dad6SMatthew G. Knepley 
19029bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
19039bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19049bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
19059bf0dad6SMatthew G. Knepley   /* This is only 1-level */
19069bf0dad6SMatthew G. Knepley   if (useCone) {
19079bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
19089bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
19099bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
19109bf0dad6SMatthew G. Knepley   } else {
19119bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
19129bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
19139bf0dad6SMatthew G. Knepley   }
19149bf0dad6SMatthew G. Knepley   if (depth == 1) {
19159bf0dad6SMatthew G. Knepley     if (*points) {
19169bf0dad6SMatthew G. Knepley       closure = *points;
19179bf0dad6SMatthew G. Knepley     } else {
19189bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
19199bf0dad6SMatthew G. Knepley       ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
19209bf0dad6SMatthew G. Knepley     }
19219bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
19229bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
19239bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
19249bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
19259bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
19269bf0dad6SMatthew G. Knepley     }
19279bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
19289bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
19299bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
19309bf0dad6SMatthew G. Knepley   }
193124c766afSToby Isaac   {
193224c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
193324c766afSToby Isaac 
193424c766afSToby Isaac     c = mesh->maxConeSize;
193524c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
193624c766afSToby Isaac     s = mesh->maxSupportSize;
193724c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
193824c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
193924c766afSToby Isaac   }
19409bf0dad6SMatthew G. Knepley   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
19419bf0dad6SMatthew G. Knepley   if (*points) {
19429bf0dad6SMatthew G. Knepley     closure = *points;
19439bf0dad6SMatthew G. Knepley   } else {
19449bf0dad6SMatthew G. Knepley     ierr = DMGetWorkArray(dm, maxSize, PETSC_INT, &closure);CHKERRQ(ierr);
19459bf0dad6SMatthew G. Knepley   }
19469bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
19479bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
19489bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
19499bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
19509bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
19519bf0dad6SMatthew G. Knepley 
19529bf0dad6SMatthew G. Knepley     if (ornt < 0) {
19539bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
19549bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
195586b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
19569bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
19579bf0dad6SMatthew G. Knepley     }
19589bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
19599bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
19609bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
19619bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
19629bf0dad6SMatthew G. Knepley   }
19639bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
19649bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
19659bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
19669bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
19679bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
19689bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
19699bf0dad6SMatthew G. Knepley 
19709bf0dad6SMatthew G. Knepley     if (useCone) {
19719bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
19729bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
19739bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
19749bf0dad6SMatthew G. Knepley     } else {
19759bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
19769bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
19779bf0dad6SMatthew G. Knepley       tmpO = NULL;
19789bf0dad6SMatthew G. Knepley     }
19799bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
19809bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
19819bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
19829bf0dad6SMatthew 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. */
19839bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
19849bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
19859bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
19869bf0dad6SMatthew G. Knepley       PetscInt       c;
19879bf0dad6SMatthew G. Knepley 
19889bf0dad6SMatthew G. Knepley       if (rev) {
19899bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
19909bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
19919bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
19929bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
19939bf0dad6SMatthew G. Knepley       }
19949bf0dad6SMatthew G. Knepley       /* Check for duplicate */
19959bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
19969bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
19979bf0dad6SMatthew G. Knepley       }
19989bf0dad6SMatthew G. Knepley       if (c == closureSize) {
19999bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
20009bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
20019bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
20029bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
20039bf0dad6SMatthew G. Knepley         closureSize           += 2;
20049bf0dad6SMatthew G. Knepley         fifoSize              += 2;
20059bf0dad6SMatthew G. Knepley       }
20069bf0dad6SMatthew G. Knepley     }
20079bf0dad6SMatthew G. Knepley     fifoStart += 2;
20089bf0dad6SMatthew G. Knepley   }
20099bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
20109bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
20119bf0dad6SMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, maxSize, PETSC_INT, &fifo);CHKERRQ(ierr);
20129bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
20139bf0dad6SMatthew G. Knepley }
20149bf0dad6SMatthew G. Knepley 
2015552f7358SJed Brown /*@C
2016552f7358SJed 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
2017552f7358SJed Brown 
2018552f7358SJed Brown   Not collective
2019552f7358SJed Brown 
2020552f7358SJed Brown   Input Parameters:
2021552f7358SJed Brown + mesh - The DMPlex
2022552f7358SJed Brown . p - The Sieve point, which must lie in the chart set with DMPlexSetChart()
2023552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2024e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2025e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2026552f7358SJed Brown 
2027552f7358SJed Brown   Note:
20280298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2029552f7358SJed Brown 
20303813dfbdSMatthew G Knepley   Fortran Notes:
20313813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
20323813dfbdSMatthew G Knepley   include petsc.h90 in your code.
20333813dfbdSMatthew G Knepley 
20343813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
20353813dfbdSMatthew G Knepley 
2036552f7358SJed Brown   Level: beginner
2037552f7358SJed Brown 
2038552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2039552f7358SJed Brown @*/
2040552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2041552f7358SJed Brown {
2042552f7358SJed Brown   PetscErrorCode ierr;
2043552f7358SJed Brown 
2044552f7358SJed Brown   PetscFunctionBegin;
2045552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2046e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2047e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
2048552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, points);CHKERRQ(ierr);
20494ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2050552f7358SJed Brown   PetscFunctionReturn(0);
2051552f7358SJed Brown }
2052552f7358SJed Brown 
2053552f7358SJed Brown /*@
2054552f7358SJed Brown   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the Sieve DAG
2055552f7358SJed Brown 
2056552f7358SJed Brown   Not collective
2057552f7358SJed Brown 
2058552f7358SJed Brown   Input Parameter:
2059552f7358SJed Brown . mesh - The DMPlex
2060552f7358SJed Brown 
2061552f7358SJed Brown   Output Parameters:
2062552f7358SJed Brown + maxConeSize - The maximum number of in-edges
2063552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
2064552f7358SJed Brown 
2065552f7358SJed Brown   Level: beginner
2066552f7358SJed Brown 
2067552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2068552f7358SJed Brown @*/
2069552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2070552f7358SJed Brown {
2071552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2072552f7358SJed Brown 
2073552f7358SJed Brown   PetscFunctionBegin;
2074552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2075552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2076552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2077552f7358SJed Brown   PetscFunctionReturn(0);
2078552f7358SJed Brown }
2079552f7358SJed Brown 
2080552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2081552f7358SJed Brown {
2082552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2083552f7358SJed Brown   PetscInt       size;
2084552f7358SJed Brown   PetscErrorCode ierr;
2085552f7358SJed Brown 
2086552f7358SJed Brown   PetscFunctionBegin;
2087552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2088552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2089552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
20901795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
20911795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2092552f7358SJed Brown   if (mesh->maxSupportSize) {
2093552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2094552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
20951795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2096552f7358SJed Brown   }
2097552f7358SJed Brown   PetscFunctionReturn(0);
2098552f7358SJed Brown }
2099552f7358SJed Brown 
2100552f7358SJed Brown PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
2101552f7358SJed Brown {
2102552f7358SJed Brown   PetscErrorCode ierr;
2103552f7358SJed Brown 
2104552f7358SJed Brown   PetscFunctionBegin;
21054d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
21064d9407bcSMatthew G. Knepley   ierr = DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2107552f7358SJed Brown   PetscFunctionReturn(0);
2108552f7358SJed Brown }
2109552f7358SJed Brown 
2110552f7358SJed Brown /*@
2111552f7358SJed Brown   DMPlexSymmetrize - Creates support (out-edge) information from cone (in-edge) inoformation
2112552f7358SJed Brown 
2113552f7358SJed Brown   Not collective
2114552f7358SJed Brown 
2115552f7358SJed Brown   Input Parameter:
2116552f7358SJed Brown . mesh - The DMPlex
2117552f7358SJed Brown 
2118552f7358SJed Brown   Output Parameter:
2119552f7358SJed Brown 
2120552f7358SJed Brown   Note:
2121552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2122552f7358SJed Brown 
2123552f7358SJed Brown   Level: beginner
2124552f7358SJed Brown 
2125552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2126552f7358SJed Brown @*/
2127552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2128552f7358SJed Brown {
2129552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2130552f7358SJed Brown   PetscInt      *offsets;
2131552f7358SJed Brown   PetscInt       supportSize;
2132552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2133552f7358SJed Brown   PetscErrorCode ierr;
2134552f7358SJed Brown 
2135552f7358SJed Brown   PetscFunctionBegin;
2136552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
213782f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
2138552f7358SJed Brown   /* Calculate support sizes */
2139552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2140552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2141552f7358SJed Brown     PetscInt dof, off, c;
2142552f7358SJed Brown 
2143552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2144552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2145552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2146552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2147552f7358SJed Brown     }
2148552f7358SJed Brown   }
2149552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2150552f7358SJed Brown     PetscInt dof;
2151552f7358SJed Brown 
2152552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
21530d644c17SKarl Rupp 
2154552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2155552f7358SJed Brown   }
2156552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2157552f7358SJed Brown   /* Calculate supports */
2158552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
21591795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
21601795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2161552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2162552f7358SJed Brown     PetscInt dof, off, c;
2163552f7358SJed Brown 
2164552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2165552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2166552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2167552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2168552f7358SJed Brown       PetscInt       offS;
2169552f7358SJed Brown 
2170552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
21710d644c17SKarl Rupp 
2172552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2173552f7358SJed Brown       ++offsets[q];
2174552f7358SJed Brown     }
2175552f7358SJed Brown   }
2176552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
2177552f7358SJed Brown   PetscFunctionReturn(0);
2178552f7358SJed Brown }
2179552f7358SJed Brown 
2180552f7358SJed Brown /*@
2181552f7358SJed Brown   DMPlexStratify - The Sieve DAG for most topologies is a graded poset (http://en.wikipedia.org/wiki/Graded_poset), and
2182552f7358SJed Brown   can be illustrated by Hasse Diagram (a http://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2183552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2184552f7358SJed Brown   the DAG.
2185552f7358SJed Brown 
2186bf4602e4SToby Isaac   Collective on dm
2187552f7358SJed Brown 
2188552f7358SJed Brown   Input Parameter:
2189552f7358SJed Brown . mesh - The DMPlex
2190552f7358SJed Brown 
2191552f7358SJed Brown   Output Parameter:
2192552f7358SJed Brown 
2193552f7358SJed Brown   Notes:
2194150b719bSJed Brown   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
2195150b719bSJed Brown   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2196c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2197150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2198552f7358SJed Brown 
2199150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2200552f7358SJed Brown 
2201552f7358SJed Brown   Level: beginner
2202552f7358SJed Brown 
2203552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
2204552f7358SJed Brown @*/
2205552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2206552f7358SJed Brown {
2207df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2208aa50250dSMatthew G. Knepley   DMLabel        label;
2209552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2210552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
2211552f7358SJed Brown   PetscErrorCode ierr;
2212552f7358SJed Brown 
2213552f7358SJed Brown   PetscFunctionBegin;
2214552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2215552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2216552f7358SJed Brown   /* Calculate depth */
2217aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2218c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2219aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2220552f7358SJed Brown   /* Initialize roots and count leaves */
2221552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2222552f7358SJed Brown     PetscInt coneSize, supportSize;
2223552f7358SJed Brown 
2224552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2225552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2226552f7358SJed Brown     if (!coneSize && supportSize) {
2227552f7358SJed Brown       ++numRoots;
2228aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2229552f7358SJed Brown     } else if (!supportSize && coneSize) {
2230552f7358SJed Brown       ++numLeaves;
2231552f7358SJed Brown     } else if (!supportSize && !coneSize) {
2232552f7358SJed Brown       /* Isolated points */
2233aa50250dSMatthew G. Knepley       ierr = DMLabelSetValue(label, p, 0);CHKERRQ(ierr);
2234552f7358SJed Brown     }
2235552f7358SJed Brown   }
2236552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2237552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
2238552f7358SJed Brown       PetscInt coneSize, supportSize;
2239552f7358SJed Brown 
2240552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2241552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2242552f7358SJed Brown       if (!supportSize && coneSize) {
2243aa50250dSMatthew G. Knepley         ierr = DMLabelSetValue(label, p, 1);CHKERRQ(ierr);
2244552f7358SJed Brown       }
2245552f7358SJed Brown     }
2246552f7358SJed Brown   } else {
224774ef644bSMatthew G. Knepley     IS       pointIS;
224874ef644bSMatthew G. Knepley     PetscInt numPoints = 0, level = 0;
2249552f7358SJed Brown 
225074ef644bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
225174ef644bSMatthew G. Knepley     if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
225274ef644bSMatthew G. Knepley     while (numPoints) {
225374ef644bSMatthew G. Knepley       const PetscInt *points;
225474ef644bSMatthew G. Knepley       const PetscInt  newLevel = level+1;
225574ef644bSMatthew G. Knepley 
225674ef644bSMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
225774ef644bSMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
225874ef644bSMatthew G. Knepley         const PetscInt  point = points[p];
225974ef644bSMatthew G. Knepley         const PetscInt *support;
226074ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
226174ef644bSMatthew G. Knepley 
226274ef644bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
226374ef644bSMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
226474ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
226574ef644bSMatthew G. Knepley           ierr = DMLabelSetValue(label, support[s], newLevel);CHKERRQ(ierr);
2266552f7358SJed Brown         }
2267552f7358SJed Brown       }
22685edfc765SMatthew G. Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
226974ef644bSMatthew G. Knepley       ++level;
227074ef644bSMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
227174ef644bSMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, level, &pointIS);CHKERRQ(ierr);
227274ef644bSMatthew G. Knepley       if (pointIS) {ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);}
227374ef644bSMatthew G. Knepley       else         {numPoints = 0;}
227474ef644bSMatthew G. Knepley     }
227574ef644bSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
227674ef644bSMatthew G. Knepley   }
2277bf4602e4SToby Isaac   { /* just in case there is an empty process */
2278bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2279bf4602e4SToby Isaac 
2280bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label,&numValues);CHKERRQ(ierr);
22814de306b1SToby Isaac     for (v = 0; v < numValues; v++) {
22824de306b1SToby Isaac       IS pointIS;
22834de306b1SToby Isaac 
22844de306b1SToby Isaac       ierr = DMLabelGetStratumIS(label, v, &pointIS);CHKERRQ(ierr);
22854de306b1SToby Isaac       if (pointIS) {
22864de306b1SToby Isaac         PetscInt  min, max, numPoints;
22874de306b1SToby Isaac         PetscInt  start;
22884de306b1SToby Isaac         PetscBool contig;
22894de306b1SToby Isaac 
22904de306b1SToby Isaac         ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
22914de306b1SToby Isaac         ierr = ISGetMinMax(pointIS, &min, &max);CHKERRQ(ierr);
22924de306b1SToby Isaac         ierr = ISContiguousLocal(pointIS,min,max+1,&start,&contig);CHKERRQ(ierr);
22934de306b1SToby Isaac         if (start == 0 && contig) {
22944de306b1SToby Isaac           ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
22954de306b1SToby Isaac           ierr = ISCreateStride(PETSC_COMM_SELF,numPoints,min,1,&pointIS);CHKERRQ(ierr);
22964de306b1SToby Isaac           ierr = DMLabelSetStratumIS(label, v, pointIS);CHKERRQ(ierr);
22974de306b1SToby Isaac         }
22984de306b1SToby Isaac       }
22994de306b1SToby Isaac       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
23004de306b1SToby Isaac     }
23016d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2302bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2303bf4602e4SToby Isaac       DMLabelAddStratum(label,v);CHKERRQ(ierr);
2304bf4602e4SToby Isaac     }
2305bf4602e4SToby Isaac   }
2306bf4602e4SToby Isaac 
2307df0420ecSMatthew G. Knepley   ierr = DMLabelGetState(label, &mesh->depthState);CHKERRQ(ierr);
2308552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2309552f7358SJed Brown   PetscFunctionReturn(0);
2310552f7358SJed Brown }
2311552f7358SJed Brown 
2312552f7358SJed Brown /*@C
2313552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
2314552f7358SJed Brown 
2315552f7358SJed Brown   Not Collective
2316552f7358SJed Brown 
2317552f7358SJed Brown   Input Parameters:
2318552f7358SJed Brown + dm - The DMPlex object
2319552f7358SJed Brown . numPoints - The number of input points for the join
2320552f7358SJed Brown - points - The input points
2321552f7358SJed Brown 
2322552f7358SJed Brown   Output Parameters:
2323552f7358SJed Brown + numCoveredPoints - The number of points in the join
2324552f7358SJed Brown - coveredPoints - The points in the join
2325552f7358SJed Brown 
2326552f7358SJed Brown   Level: intermediate
2327552f7358SJed Brown 
2328552f7358SJed Brown   Note: Currently, this is restricted to a single level join
2329552f7358SJed Brown 
23303813dfbdSMatthew G Knepley   Fortran Notes:
23313813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
23323813dfbdSMatthew G Knepley   include petsc.h90 in your code.
23333813dfbdSMatthew G Knepley 
23343813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
23353813dfbdSMatthew G Knepley 
2336552f7358SJed Brown .keywords: mesh
2337552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2338552f7358SJed Brown @*/
2339552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2340552f7358SJed Brown {
2341552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2342552f7358SJed Brown   PetscInt      *join[2];
2343552f7358SJed Brown   PetscInt       joinSize, i = 0;
2344552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2345552f7358SJed Brown   PetscErrorCode ierr;
2346552f7358SJed Brown 
2347552f7358SJed Brown   PetscFunctionBegin;
2348552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2349552f7358SJed Brown   PetscValidPointer(points, 2);
2350552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2351552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2352552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
2353552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
2354552f7358SJed Brown   /* Copy in support of first point */
2355552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
2356552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
2357552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
2358552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
2359552f7358SJed Brown   }
2360552f7358SJed Brown   /* Check each successive support */
2361552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2362552f7358SJed Brown     PetscInt newJoinSize = 0;
2363552f7358SJed Brown 
2364552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
2365552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
2366552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2367552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
2368552f7358SJed Brown 
2369552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
2370552f7358SJed Brown         if (point == join[i][m]) {
2371552f7358SJed Brown           join[1-i][newJoinSize++] = point;
2372552f7358SJed Brown           break;
2373552f7358SJed Brown         }
2374552f7358SJed Brown       }
2375552f7358SJed Brown     }
2376552f7358SJed Brown     joinSize = newJoinSize;
2377552f7358SJed Brown     i        = 1-i;
2378552f7358SJed Brown   }
2379552f7358SJed Brown   *numCoveredPoints = joinSize;
2380552f7358SJed Brown   *coveredPoints    = join[i];
2381552f7358SJed Brown   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
2382552f7358SJed Brown   PetscFunctionReturn(0);
2383552f7358SJed Brown }
2384552f7358SJed Brown 
2385552f7358SJed Brown /*@C
2386552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
2387552f7358SJed Brown 
2388552f7358SJed Brown   Not Collective
2389552f7358SJed Brown 
2390552f7358SJed Brown   Input Parameters:
2391552f7358SJed Brown + dm - The DMPlex object
2392552f7358SJed Brown . numPoints - The number of input points for the join
2393552f7358SJed Brown - points - The input points
2394552f7358SJed Brown 
2395552f7358SJed Brown   Output Parameters:
2396552f7358SJed Brown + numCoveredPoints - The number of points in the join
2397552f7358SJed Brown - coveredPoints - The points in the join
2398552f7358SJed Brown 
23993813dfbdSMatthew G Knepley   Fortran Notes:
24003813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24013813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24023813dfbdSMatthew G Knepley 
24033813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24043813dfbdSMatthew G Knepley 
2405552f7358SJed Brown   Level: intermediate
2406552f7358SJed Brown 
2407552f7358SJed Brown .keywords: mesh
2408552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
2409552f7358SJed Brown @*/
2410552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2411552f7358SJed Brown {
2412552f7358SJed Brown   PetscErrorCode ierr;
2413552f7358SJed Brown 
2414552f7358SJed Brown   PetscFunctionBegin;
2415552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2416d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2417d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2418d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
2419552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
2420d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2421552f7358SJed Brown   PetscFunctionReturn(0);
2422552f7358SJed Brown }
2423552f7358SJed Brown 
2424552f7358SJed Brown /*@C
2425552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
2426552f7358SJed Brown 
2427552f7358SJed Brown   Not Collective
2428552f7358SJed Brown 
2429552f7358SJed Brown   Input Parameters:
2430552f7358SJed Brown + dm - The DMPlex object
2431552f7358SJed Brown . numPoints - The number of input points for the join
2432552f7358SJed Brown - points - The input points
2433552f7358SJed Brown 
2434552f7358SJed Brown   Output Parameters:
2435552f7358SJed Brown + numCoveredPoints - The number of points in the join
2436552f7358SJed Brown - coveredPoints - The points in the join
2437552f7358SJed Brown 
24383813dfbdSMatthew G Knepley   Fortran Notes:
24393813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24403813dfbdSMatthew G Knepley   include petsc.h90 in your code.
24413813dfbdSMatthew G Knepley 
24423813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24433813dfbdSMatthew G Knepley 
2444552f7358SJed Brown   Level: intermediate
2445552f7358SJed Brown 
2446552f7358SJed Brown .keywords: mesh
2447552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
2448552f7358SJed Brown @*/
2449552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2450552f7358SJed Brown {
2451552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2452552f7358SJed Brown   PetscInt      *offsets, **closures;
2453552f7358SJed Brown   PetscInt      *join[2];
2454552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
245524c766afSToby Isaac   PetscInt       p, d, c, m, ms;
2456552f7358SJed Brown   PetscErrorCode ierr;
2457552f7358SJed Brown 
2458552f7358SJed Brown   PetscFunctionBegin;
2459552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2460552f7358SJed Brown   PetscValidPointer(points, 2);
2461552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2462552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2463552f7358SJed Brown 
2464552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
24651795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
2466552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
246724c766afSToby Isaac   ms      = mesh->maxSupportSize;
246824c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
2469552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[0]);CHKERRQ(ierr);
2470552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &join[1]);CHKERRQ(ierr);
2471552f7358SJed Brown 
2472552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2473552f7358SJed Brown     PetscInt closureSize;
2474552f7358SJed Brown 
2475552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
24760d644c17SKarl Rupp 
2477552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
2478552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
2479552f7358SJed Brown       PetscInt pStart, pEnd, i;
2480552f7358SJed Brown 
2481552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
2482552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
2483552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2484552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
2485552f7358SJed Brown           break;
2486552f7358SJed Brown         }
2487552f7358SJed Brown       }
2488552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
2489552f7358SJed Brown     }
249082f516ccSBarry 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);
2491552f7358SJed Brown   }
2492552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
2493552f7358SJed Brown     PetscInt dof;
2494552f7358SJed Brown 
2495552f7358SJed Brown     /* Copy in support of first point */
2496552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
2497552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
2498552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
2499552f7358SJed Brown     }
2500552f7358SJed Brown     /* Check each successive cone */
2501552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
2502552f7358SJed Brown       PetscInt newJoinSize = 0;
2503552f7358SJed Brown 
2504552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
2505552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2506552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
2507552f7358SJed Brown 
2508552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
2509552f7358SJed Brown           if (point == join[i][m]) {
2510552f7358SJed Brown             join[1-i][newJoinSize++] = point;
2511552f7358SJed Brown             break;
2512552f7358SJed Brown           }
2513552f7358SJed Brown         }
2514552f7358SJed Brown       }
2515552f7358SJed Brown       joinSize = newJoinSize;
2516552f7358SJed Brown       i        = 1-i;
2517552f7358SJed Brown     }
2518552f7358SJed Brown     if (joinSize) break;
2519552f7358SJed Brown   }
2520552f7358SJed Brown   *numCoveredPoints = joinSize;
2521552f7358SJed Brown   *coveredPoints    = join[i];
2522552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
25230298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
2524552f7358SJed Brown   }
2525552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
2526552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), PETSC_INT, &offsets);CHKERRQ(ierr);
2527552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, PETSC_INT, &join[1-i]);CHKERRQ(ierr);
2528552f7358SJed Brown   PetscFunctionReturn(0);
2529552f7358SJed Brown }
2530552f7358SJed Brown 
2531552f7358SJed Brown /*@C
2532552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
2533552f7358SJed Brown 
2534552f7358SJed Brown   Not Collective
2535552f7358SJed Brown 
2536552f7358SJed Brown   Input Parameters:
2537552f7358SJed Brown + dm - The DMPlex object
2538552f7358SJed Brown . numPoints - The number of input points for the meet
2539552f7358SJed Brown - points - The input points
2540552f7358SJed Brown 
2541552f7358SJed Brown   Output Parameters:
2542552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2543552f7358SJed Brown - coveredPoints - The points in the meet
2544552f7358SJed Brown 
2545552f7358SJed Brown   Level: intermediate
2546552f7358SJed Brown 
2547552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
2548552f7358SJed Brown 
25493813dfbdSMatthew G Knepley   Fortran Notes:
25503813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25513813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25523813dfbdSMatthew G Knepley 
25533813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25543813dfbdSMatthew G Knepley 
2555552f7358SJed Brown .keywords: mesh
2556552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
2557552f7358SJed Brown @*/
2558552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
2559552f7358SJed Brown {
2560552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2561552f7358SJed Brown   PetscInt      *meet[2];
2562552f7358SJed Brown   PetscInt       meetSize, i = 0;
2563552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2564552f7358SJed Brown   PetscErrorCode ierr;
2565552f7358SJed Brown 
2566552f7358SJed Brown   PetscFunctionBegin;
2567552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2568552f7358SJed Brown   PetscValidPointer(points, 2);
2569552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
2570552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
2571552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
2572552f7358SJed Brown   ierr = DMGetWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
2573552f7358SJed Brown   /* Copy in cone of first point */
2574552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
2575552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
2576552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
2577552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
2578552f7358SJed Brown   }
2579552f7358SJed Brown   /* Check each successive cone */
2580552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2581552f7358SJed Brown     PetscInt newMeetSize = 0;
2582552f7358SJed Brown 
2583552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
2584552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
2585552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2586552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
2587552f7358SJed Brown 
2588552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
2589552f7358SJed Brown         if (point == meet[i][m]) {
2590552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
2591552f7358SJed Brown           break;
2592552f7358SJed Brown         }
2593552f7358SJed Brown       }
2594552f7358SJed Brown     }
2595552f7358SJed Brown     meetSize = newMeetSize;
2596552f7358SJed Brown     i        = 1-i;
2597552f7358SJed Brown   }
2598552f7358SJed Brown   *numCoveringPoints = meetSize;
2599552f7358SJed Brown   *coveringPoints    = meet[i];
2600552f7358SJed Brown   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
2601552f7358SJed Brown   PetscFunctionReturn(0);
2602552f7358SJed Brown }
2603552f7358SJed Brown 
2604552f7358SJed Brown /*@C
2605552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
2606552f7358SJed Brown 
2607552f7358SJed Brown   Not Collective
2608552f7358SJed Brown 
2609552f7358SJed Brown   Input Parameters:
2610552f7358SJed Brown + dm - The DMPlex object
2611552f7358SJed Brown . numPoints - The number of input points for the meet
2612552f7358SJed Brown - points - The input points
2613552f7358SJed Brown 
2614552f7358SJed Brown   Output Parameters:
2615552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2616552f7358SJed Brown - coveredPoints - The points in the meet
2617552f7358SJed Brown 
2618552f7358SJed Brown   Level: intermediate
2619552f7358SJed Brown 
26203813dfbdSMatthew G Knepley   Fortran Notes:
26213813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
26223813dfbdSMatthew G Knepley   include petsc.h90 in your code.
26233813dfbdSMatthew G Knepley 
26243813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
26253813dfbdSMatthew G Knepley 
2626552f7358SJed Brown .keywords: mesh
2627552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
2628552f7358SJed Brown @*/
2629552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2630552f7358SJed Brown {
2631552f7358SJed Brown   PetscErrorCode ierr;
2632552f7358SJed Brown 
2633552f7358SJed Brown   PetscFunctionBegin;
2634552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2635d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
2636d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
2637d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
2638552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void*) coveredPoints);CHKERRQ(ierr);
2639d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
2640552f7358SJed Brown   PetscFunctionReturn(0);
2641552f7358SJed Brown }
2642552f7358SJed Brown 
2643552f7358SJed Brown /*@C
2644552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
2645552f7358SJed Brown 
2646552f7358SJed Brown   Not Collective
2647552f7358SJed Brown 
2648552f7358SJed Brown   Input Parameters:
2649552f7358SJed Brown + dm - The DMPlex object
2650552f7358SJed Brown . numPoints - The number of input points for the meet
2651552f7358SJed Brown - points - The input points
2652552f7358SJed Brown 
2653552f7358SJed Brown   Output Parameters:
2654552f7358SJed Brown + numCoveredPoints - The number of points in the meet
2655552f7358SJed Brown - coveredPoints - The points in the meet
2656552f7358SJed Brown 
2657552f7358SJed Brown   Level: intermediate
2658552f7358SJed Brown 
26593813dfbdSMatthew G Knepley   Fortran Notes:
26603813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
26613813dfbdSMatthew G Knepley   include petsc.h90 in your code.
26623813dfbdSMatthew G Knepley 
26633813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
26643813dfbdSMatthew G Knepley 
2665552f7358SJed Brown .keywords: mesh
2666552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
2667552f7358SJed Brown @*/
2668552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2669552f7358SJed Brown {
2670552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2671552f7358SJed Brown   PetscInt      *offsets, **closures;
2672552f7358SJed Brown   PetscInt      *meet[2];
2673552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
267424c766afSToby Isaac   PetscInt       p, h, c, m, mc;
2675552f7358SJed Brown   PetscErrorCode ierr;
2676552f7358SJed Brown 
2677552f7358SJed Brown   PetscFunctionBegin;
2678552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2679552f7358SJed Brown   PetscValidPointer(points, 2);
2680552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
2681552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
2682552f7358SJed Brown 
2683552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
2684785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
2685552f7358SJed Brown   ierr    = DMGetWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
268624c766afSToby Isaac   mc      = mesh->maxConeSize;
268724c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
2688552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[0]);CHKERRQ(ierr);
2689552f7358SJed Brown   ierr    = DMGetWorkArray(dm, maxSize, PETSC_INT, &meet[1]);CHKERRQ(ierr);
2690552f7358SJed Brown 
2691552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
2692552f7358SJed Brown     PetscInt closureSize;
2693552f7358SJed Brown 
2694552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
26950d644c17SKarl Rupp 
2696552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
2697552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
2698552f7358SJed Brown       PetscInt pStart, pEnd, i;
2699552f7358SJed Brown 
2700552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
2701552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
2702552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
2703552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
2704552f7358SJed Brown           break;
2705552f7358SJed Brown         }
2706552f7358SJed Brown       }
2707552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
2708552f7358SJed Brown     }
270982f516ccSBarry 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);
2710552f7358SJed Brown   }
2711552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
2712552f7358SJed Brown     PetscInt dof;
2713552f7358SJed Brown 
2714552f7358SJed Brown     /* Copy in cone of first point */
2715552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
2716552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
2717552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
2718552f7358SJed Brown     }
2719552f7358SJed Brown     /* Check each successive cone */
2720552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
2721552f7358SJed Brown       PetscInt newMeetSize = 0;
2722552f7358SJed Brown 
2723552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
2724552f7358SJed Brown       for (c = 0; c < dof; ++c) {
2725552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
2726552f7358SJed Brown 
2727552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
2728552f7358SJed Brown           if (point == meet[i][m]) {
2729552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
2730552f7358SJed Brown             break;
2731552f7358SJed Brown           }
2732552f7358SJed Brown         }
2733552f7358SJed Brown       }
2734552f7358SJed Brown       meetSize = newMeetSize;
2735552f7358SJed Brown       i        = 1-i;
2736552f7358SJed Brown     }
2737552f7358SJed Brown     if (meetSize) break;
2738552f7358SJed Brown   }
2739552f7358SJed Brown   *numCoveredPoints = meetSize;
2740552f7358SJed Brown   *coveredPoints    = meet[i];
2741552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
27420298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
2743552f7358SJed Brown   }
2744552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
2745552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), PETSC_INT, &offsets);CHKERRQ(ierr);
2746552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, PETSC_INT, &meet[1-i]);CHKERRQ(ierr);
2747552f7358SJed Brown   PetscFunctionReturn(0);
2748552f7358SJed Brown }
2749552f7358SJed Brown 
27504e3744c5SMatthew G. Knepley /*@C
27514e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
27524e3744c5SMatthew G. Knepley 
27534e3744c5SMatthew G. Knepley   Not Collective
27544e3744c5SMatthew G. Knepley 
27554e3744c5SMatthew G. Knepley   Input Parameters:
27564e3744c5SMatthew G. Knepley + dmA - A DMPlex object
27574e3744c5SMatthew G. Knepley - dmB - A DMPlex object
27584e3744c5SMatthew G. Knepley 
27594e3744c5SMatthew G. Knepley   Output Parameters:
27604e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
27614e3744c5SMatthew G. Knepley 
27624e3744c5SMatthew G. Knepley   Level: intermediate
27634e3744c5SMatthew G. Knepley 
27644e3744c5SMatthew G. Knepley   Notes:
27654e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
27664e3744c5SMatthew G. Knepley 
27674e3744c5SMatthew G. Knepley .keywords: mesh
27684e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
27694e3744c5SMatthew G. Knepley @*/
27704e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
27714e3744c5SMatthew G. Knepley {
27724e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
27734e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
27744e3744c5SMatthew G. Knepley 
27754e3744c5SMatthew G. Knepley   PetscFunctionBegin;
27764e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
27774e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
27784e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
27794e3744c5SMatthew G. Knepley 
27804e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
27814e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
27824e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
27834e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
27844e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
27854e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
27864e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
27874e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
27884e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
27894e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
27904e3744c5SMatthew G. Knepley 
27914e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
27924e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
27934e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
27944e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
27954e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
27964e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
27974e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
27984e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
27994e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
28004e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
28014e3744c5SMatthew G. Knepley     }
28024e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
28034e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
28044e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
28054e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
28064e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
28074e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
28084e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
28094e3744c5SMatthew G. Knepley     }
28104e3744c5SMatthew G. Knepley   }
28114e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
28124e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
28134e3744c5SMatthew G. Knepley }
28144e3744c5SMatthew G. Knepley 
28157cd05799SMatthew G. Knepley /*@C
28167cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
28177cd05799SMatthew G. Knepley 
28187cd05799SMatthew G. Knepley   Not Collective
28197cd05799SMatthew G. Knepley 
28207cd05799SMatthew G. Knepley   Input Parameters:
28217cd05799SMatthew G. Knepley + dm         - The DMPlex
28227cd05799SMatthew G. Knepley . cellDim    - The cell dimension
28237cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
28247cd05799SMatthew G. Knepley 
28257cd05799SMatthew G. Knepley   Output Parameters:
28267cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
28277cd05799SMatthew G. Knepley 
28287cd05799SMatthew G. Knepley   Level: developer
28297cd05799SMatthew G. Knepley 
28307cd05799SMatthew G. Knepley   Notes:
28317cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
28327cd05799SMatthew G. Knepley 
28337cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
28347cd05799SMatthew G. Knepley @*/
283518ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
2836a6dfd86eSKarl Rupp {
283782f516ccSBarry Smith   MPI_Comm       comm;
2838552f7358SJed Brown   PetscErrorCode ierr;
2839552f7358SJed Brown 
2840552f7358SJed Brown   PetscFunctionBegin;
284182f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2842552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
2843552f7358SJed Brown   switch (cellDim) {
2844552f7358SJed Brown   case 0:
2845552f7358SJed Brown     *numFaceVertices = 0;
2846552f7358SJed Brown     break;
2847552f7358SJed Brown   case 1:
2848552f7358SJed Brown     *numFaceVertices = 1;
2849552f7358SJed Brown     break;
2850552f7358SJed Brown   case 2:
2851552f7358SJed Brown     switch (numCorners) {
285219436ca2SJed Brown     case 3: /* triangle */
285319436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2854552f7358SJed Brown       break;
285519436ca2SJed Brown     case 4: /* quadrilateral */
285619436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
2857552f7358SJed Brown       break;
285819436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
285919436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2860552f7358SJed Brown       break;
286119436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
286219436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
2863552f7358SJed Brown       break;
2864552f7358SJed Brown     default:
2865f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2866552f7358SJed Brown     }
2867552f7358SJed Brown     break;
2868552f7358SJed Brown   case 3:
2869552f7358SJed Brown     switch (numCorners) {
287019436ca2SJed Brown     case 4: /* tetradehdron */
287119436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
2872552f7358SJed Brown       break;
287319436ca2SJed Brown     case 6: /* tet cohesive cells */
287419436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2875552f7358SJed Brown       break;
287619436ca2SJed Brown     case 8: /* hexahedron */
287719436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
2878552f7358SJed Brown       break;
287919436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
288019436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2881552f7358SJed Brown       break;
288219436ca2SJed Brown     case 10: /* quadratic tetrahedron */
288319436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2884552f7358SJed Brown       break;
288519436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
288619436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2887552f7358SJed Brown       break;
288819436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
288919436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
2890552f7358SJed Brown       break;
289119436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
289219436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
2893552f7358SJed Brown       break;
2894552f7358SJed Brown     default:
2895f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
2896552f7358SJed Brown     }
2897552f7358SJed Brown     break;
2898552f7358SJed Brown   default:
2899f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
2900552f7358SJed Brown   }
2901552f7358SJed Brown   PetscFunctionReturn(0);
2902552f7358SJed Brown }
2903552f7358SJed Brown 
2904552f7358SJed Brown /*@
2905aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
2906552f7358SJed Brown 
2907552f7358SJed Brown   Not Collective
2908552f7358SJed Brown 
2909aa50250dSMatthew G. Knepley   Input Parameter:
2910552f7358SJed Brown . dm    - The DMPlex object
2911552f7358SJed Brown 
2912aa50250dSMatthew G. Knepley   Output Parameter:
2913aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
2914552f7358SJed Brown 
2915552f7358SJed Brown   Level: developer
2916552f7358SJed Brown 
2917aa50250dSMatthew G. Knepley .keywords: mesh, points
2918aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2919aa50250dSMatthew G. Knepley @*/
2920aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
2921aa50250dSMatthew G. Knepley {
2922aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
2923aa50250dSMatthew G. Knepley 
2924aa50250dSMatthew G. Knepley   PetscFunctionBegin;
2925aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2926aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
2927c58f1c22SToby Isaac   if (!dm->depthLabel) {ierr = DMGetLabel(dm, "depth", &dm->depthLabel);CHKERRQ(ierr);}
2928c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
2929aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
2930aa50250dSMatthew G. Knepley }
2931aa50250dSMatthew G. Knepley 
2932aa50250dSMatthew G. Knepley /*@
2933aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
2934aa50250dSMatthew G. Knepley 
2935aa50250dSMatthew G. Knepley   Not Collective
2936aa50250dSMatthew G. Knepley 
2937aa50250dSMatthew G. Knepley   Input Parameter:
2938aa50250dSMatthew G. Knepley . dm    - The DMPlex object
2939aa50250dSMatthew G. Knepley 
2940aa50250dSMatthew G. Knepley   Output Parameter:
2941aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
2942aa50250dSMatthew G. Knepley 
2943aa50250dSMatthew G. Knepley   Level: developer
2944552f7358SJed Brown 
2945552f7358SJed Brown .keywords: mesh, points
2946aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
2947552f7358SJed Brown @*/
2948552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
2949552f7358SJed Brown {
2950aa50250dSMatthew G. Knepley   DMLabel        label;
2951aa50250dSMatthew G. Knepley   PetscInt       d = 0;
2952552f7358SJed Brown   PetscErrorCode ierr;
2953552f7358SJed Brown 
2954552f7358SJed Brown   PetscFunctionBegin;
2955552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2956552f7358SJed Brown   PetscValidPointer(depth, 2);
2957aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2958aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
2959552f7358SJed Brown   *depth = d-1;
2960552f7358SJed Brown   PetscFunctionReturn(0);
2961552f7358SJed Brown }
2962552f7358SJed Brown 
2963552f7358SJed Brown /*@
2964552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
2965552f7358SJed Brown 
2966552f7358SJed Brown   Not Collective
2967552f7358SJed Brown 
2968552f7358SJed Brown   Input Parameters:
2969552f7358SJed Brown + dm           - The DMPlex object
2970552f7358SJed Brown - stratumValue - The requested depth
2971552f7358SJed Brown 
2972552f7358SJed Brown   Output Parameters:
2973552f7358SJed Brown + start - The first point at this depth
2974552f7358SJed Brown - end   - One beyond the last point at this depth
2975552f7358SJed Brown 
2976552f7358SJed Brown   Level: developer
2977552f7358SJed Brown 
2978552f7358SJed Brown .keywords: mesh, points
2979552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
2980552f7358SJed Brown @*/
29810adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
29820adebc6cSBarry Smith {
2983aa50250dSMatthew G. Knepley   DMLabel        label;
298463d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
2985552f7358SJed Brown   PetscErrorCode ierr;
2986552f7358SJed Brown 
2987552f7358SJed Brown   PetscFunctionBegin;
2988552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
298963d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
299063d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
2991552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
29920d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
299363d1a920SMatthew G. Knepley   if (stratumValue < 0) {
299463d1a920SMatthew G. Knepley     if (start) *start = pStart;
299563d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
299663d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
2997552f7358SJed Brown   }
2998aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
299963d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
300063d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
3001552f7358SJed Brown   PetscFunctionReturn(0);
3002552f7358SJed Brown }
3003552f7358SJed Brown 
3004552f7358SJed Brown /*@
3005552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
3006552f7358SJed Brown 
3007552f7358SJed Brown   Not Collective
3008552f7358SJed Brown 
3009552f7358SJed Brown   Input Parameters:
3010552f7358SJed Brown + dm           - The DMPlex object
3011552f7358SJed Brown - stratumValue - The requested height
3012552f7358SJed Brown 
3013552f7358SJed Brown   Output Parameters:
3014552f7358SJed Brown + start - The first point at this height
3015552f7358SJed Brown - end   - One beyond the last point at this height
3016552f7358SJed Brown 
3017552f7358SJed Brown   Level: developer
3018552f7358SJed Brown 
3019552f7358SJed Brown .keywords: mesh, points
3020552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
3021552f7358SJed Brown @*/
30220adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
30230adebc6cSBarry Smith {
3024aa50250dSMatthew G. Knepley   DMLabel        label;
302563d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
3026552f7358SJed Brown   PetscErrorCode ierr;
3027552f7358SJed Brown 
3028552f7358SJed Brown   PetscFunctionBegin;
3029552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
303063d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
303163d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3032552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
30330d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
303463d1a920SMatthew G. Knepley   if (stratumValue < 0) {
303563d1a920SMatthew G. Knepley     if (start) *start = pStart;
303663d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
303763d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3038552f7358SJed Brown   }
3039aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3040aa50250dSMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");CHKERRQ(ierr);
304163d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
304263d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
3043552f7358SJed Brown   PetscFunctionReturn(0);
3044552f7358SJed Brown }
3045552f7358SJed Brown 
3046552f7358SJed Brown /* Set the number of dof on each point and separate by fields */
30477cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionInitial(DM dm, PetscInt dim, PetscInt numFields,const PetscInt numComp[],const PetscInt numDof[], PetscSection *section)
3048a6dfd86eSKarl Rupp {
3049ee55978aSMatthew G. Knepley   PetscInt      *pMax;
3050916f0f19SMatthew G. Knepley   PetscInt       depth, pStart = 0, pEnd = 0;
3051ee55978aSMatthew G. Knepley   PetscInt       Nf, p, d, dep, f;
3052fa716be8SMatthew G. Knepley   PetscBool     *isFE;
3053552f7358SJed Brown   PetscErrorCode ierr;
3054552f7358SJed Brown 
3055552f7358SJed Brown   PetscFunctionBegin;
3056fa716be8SMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
3057ee55978aSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
3058fa716be8SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
3059fa716be8SMatthew G. Knepley     PetscObject  obj;
3060fa716be8SMatthew G. Knepley     PetscClassId id;
3061fa716be8SMatthew G. Knepley 
3062ee55978aSMatthew G. Knepley     isFE[f] = PETSC_FALSE;
3063ee55978aSMatthew G. Knepley     if (f >= Nf) continue;
3064fa716be8SMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
3065fa716be8SMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
3066fa716be8SMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
3067fa716be8SMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
3068552f7358SJed Brown   }
306982f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), section);CHKERRQ(ierr);
3070552f7358SJed Brown   if (numFields > 0) {
3071552f7358SJed Brown     ierr = PetscSectionSetNumFields(*section, numFields);CHKERRQ(ierr);
3072552f7358SJed Brown     if (numComp) {
3073552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3074552f7358SJed Brown         ierr = PetscSectionSetFieldComponents(*section, f, numComp[f]);CHKERRQ(ierr);
3075d484f18aSToby Isaac         if (isFE[f]) {
3076d484f18aSToby Isaac           PetscFE           fe;
3077d484f18aSToby Isaac           PetscDualSpace    dspace;
3078d484f18aSToby Isaac           const PetscInt    ***perms;
3079d484f18aSToby Isaac           const PetscScalar ***flips;
3080d484f18aSToby Isaac           const PetscInt    *numDof;
3081d484f18aSToby Isaac 
3082d484f18aSToby Isaac           ierr = DMGetField(dm,f,(PetscObject *) &fe);CHKERRQ(ierr);
3083d484f18aSToby Isaac           ierr = PetscFEGetDualSpace(fe,&dspace);CHKERRQ(ierr);
3084d484f18aSToby Isaac           ierr = PetscDualSpaceGetSymmetries(dspace,&perms,&flips);CHKERRQ(ierr);
3085d484f18aSToby Isaac           ierr = PetscDualSpaceGetNumDof(dspace,&numDof);CHKERRQ(ierr);
3086d484f18aSToby Isaac           if (perms || flips) {
3087d484f18aSToby Isaac             DM               K;
3088d484f18aSToby Isaac             DMLabel          depthLabel;
3089d484f18aSToby Isaac             PetscInt         depth, h;
3090d484f18aSToby Isaac             PetscSectionSym  sym;
3091d484f18aSToby Isaac 
3092d484f18aSToby Isaac             ierr = PetscDualSpaceGetDM(dspace,&K);CHKERRQ(ierr);
3093d484f18aSToby Isaac             ierr = DMPlexGetDepthLabel(dm,&depthLabel);CHKERRQ(ierr);
3094d484f18aSToby Isaac             ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
3095d484f18aSToby Isaac             ierr = PetscSectionSymCreateLabel(PetscObjectComm((PetscObject)*section),depthLabel,&sym);CHKERRQ(ierr);
3096d484f18aSToby Isaac             for (h = 0; h <= depth; h++) {
3097d484f18aSToby Isaac               PetscDualSpace    hspace;
3098d484f18aSToby Isaac               PetscInt          kStart, kEnd;
3099d484f18aSToby Isaac               PetscInt          kConeSize;
3100d484f18aSToby Isaac               const PetscInt    **perms0 = NULL;
3101d484f18aSToby Isaac               const PetscScalar **flips0 = NULL;
3102d484f18aSToby Isaac 
3103d484f18aSToby Isaac               ierr = PetscDualSpaceGetHeightSubspace(dspace,h,&hspace);CHKERRQ(ierr);
3104d484f18aSToby Isaac               ierr = DMPlexGetHeightStratum(K,h,&kStart,&kEnd);CHKERRQ(ierr);
3105d484f18aSToby Isaac               if (!hspace) continue;
3106d484f18aSToby Isaac               ierr = PetscDualSpaceGetSymmetries(hspace,&perms,&flips);CHKERRQ(ierr);
3107d484f18aSToby Isaac               if (perms) perms0 = perms[0];
3108d484f18aSToby Isaac               if (flips) flips0 = flips[0];
3109d484f18aSToby Isaac               if (!(perms0 || flips0)) continue;
3110d484f18aSToby Isaac               ierr = DMPlexGetConeSize(K,kStart,&kConeSize);CHKERRQ(ierr);
3111d484f18aSToby Isaac               if (numComp[f] == 1) {
3112d484f18aSToby Isaac                 ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numDof[depth - h],-kConeSize,kConeSize,PETSC_USE_POINTER,perms0 ? &perms0[-kConeSize] : NULL,flips0 ? &flips0[-kConeSize] : NULL);CHKERRQ(ierr);
3113d484f18aSToby Isaac               } else {
3114d484f18aSToby Isaac                 PetscInt    **fieldPerms = NULL, o;
3115d484f18aSToby Isaac                 PetscScalar **fieldFlips = NULL;
3116d484f18aSToby Isaac 
3117d484f18aSToby Isaac                 ierr = PetscCalloc1(2 * kConeSize,&fieldPerms);CHKERRQ(ierr);
3118d484f18aSToby Isaac                 ierr = PetscCalloc1(2 * kConeSize,&fieldFlips);CHKERRQ(ierr);
3119d484f18aSToby Isaac                 for (o = -kConeSize; o < kConeSize; o++) {
3120d484f18aSToby Isaac                   if (perms0 && perms0[o]) {
3121d484f18aSToby Isaac                     PetscInt r, s;
3122d484f18aSToby Isaac 
3123d484f18aSToby Isaac                     ierr = PetscMalloc1(numComp[f] * numDof[depth - h],&fieldPerms[o+kConeSize]);CHKERRQ(ierr);
3124d484f18aSToby Isaac                     for (r = 0; r < numDof[depth - h]; r++) {
3125d484f18aSToby Isaac                       for (s = 0; s < numComp[f]; s++) {
3126d484f18aSToby Isaac                         fieldPerms[o+kConeSize][r * numComp[f] + s] = numComp[f] * perms0[o][r] + s;
3127d484f18aSToby Isaac                       }
3128d484f18aSToby Isaac                     }
3129d484f18aSToby Isaac                   }
3130d484f18aSToby Isaac                   if (flips0 && flips0[o]) {
3131d484f18aSToby Isaac                     PetscInt r, s;
3132d484f18aSToby Isaac 
3133d484f18aSToby Isaac                     ierr = PetscMalloc1(numComp[f] * numDof[depth - h],&fieldFlips[o+kConeSize]);CHKERRQ(ierr);
3134d484f18aSToby Isaac                     for (r = 0; r < numDof[depth - h]; r++) {
3135d484f18aSToby Isaac                       for (s = 0; s < numComp[f]; s++) {
3136d484f18aSToby Isaac                         fieldFlips[o+kConeSize][r * numComp[f] + s] = flips0[o][r];
3137d484f18aSToby Isaac                       }
3138d484f18aSToby Isaac                     }
3139d484f18aSToby Isaac                   }
3140d484f18aSToby Isaac                 }
3141d484f18aSToby Isaac                 ierr = PetscSectionSymLabelSetStratum(sym,depth - h,numComp[f] * numDof[depth - h],-kConeSize,kConeSize,PETSC_OWN_POINTER,(const PetscInt **) fieldPerms,(const PetscScalar **)fieldFlips);CHKERRQ(ierr);
3142d484f18aSToby Isaac               }
3143d484f18aSToby Isaac             }
3144d484f18aSToby Isaac             ierr = PetscSectionSetFieldSym(*section,f,sym);CHKERRQ(ierr);
3145d484f18aSToby Isaac             ierr = PetscSectionSymDestroy(&sym);CHKERRQ(ierr);
3146d484f18aSToby Isaac           }
3147d484f18aSToby Isaac         }
3148552f7358SJed Brown       }
3149552f7358SJed Brown     }
3150552f7358SJed Brown   }
3151552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3152552f7358SJed Brown   ierr = PetscSectionSetChart(*section, pStart, pEnd);CHKERRQ(ierr);
3153916f0f19SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
31549ac3fadcSMatthew G. Knepley   ierr = PetscMalloc1(depth+1,&pMax);CHKERRQ(ierr);
31559ac3fadcSMatthew 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);
3156916f0f19SMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
3157916f0f19SMatthew G. Knepley     d    = dim == depth ? dep : (!dep ? 0 : dim);
3158916f0f19SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dep, &pStart, &pEnd);CHKERRQ(ierr);
3159fa716be8SMatthew G. Knepley     pMax[dep] = pMax[dep] < 0 ? pEnd : pMax[dep];
3160552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
3161ee55978aSMatthew G. Knepley       PetscInt tot = 0;
3162ee55978aSMatthew G. Knepley 
3163552f7358SJed Brown       for (f = 0; f < numFields; ++f) {
3164fa716be8SMatthew G. Knepley         if (isFE[f] && p >= pMax[dep]) continue;
3165552f7358SJed Brown         ierr = PetscSectionSetFieldDof(*section, p, f, numDof[f*(dim+1)+d]);CHKERRQ(ierr);
3166ee55978aSMatthew G. Knepley         tot += numDof[f*(dim+1)+d];
3167552f7358SJed Brown       }
3168ee55978aSMatthew G. Knepley       ierr = PetscSectionSetDof(*section, p, tot);CHKERRQ(ierr);
3169552f7358SJed Brown     }
3170552f7358SJed Brown   }
31719ac3fadcSMatthew G. Knepley   ierr = PetscFree(pMax);CHKERRQ(ierr);
3172fa716be8SMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
3173552f7358SJed Brown   PetscFunctionReturn(0);
3174552f7358SJed Brown }
3175552f7358SJed Brown 
3176552f7358SJed Brown /* Set the number of dof on each point and separate by fields
3177ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3178552f7358SJed Brown */
31797cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCDof(DM dm, PetscInt numBC, const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
3180a6dfd86eSKarl Rupp {
3181552f7358SJed Brown   PetscInt       numFields;
3182552f7358SJed Brown   PetscInt       bc;
3183a68b90caSToby Isaac   PetscSection   aSec;
3184552f7358SJed Brown   PetscErrorCode ierr;
3185552f7358SJed Brown 
3186552f7358SJed Brown   PetscFunctionBegin;
3187552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3188552f7358SJed Brown   for (bc = 0; bc < numBC; ++bc) {
3189552f7358SJed Brown     PetscInt        field = 0;
3190ab1d0545SMatthew G. Knepley     const PetscInt *comp;
3191552f7358SJed Brown     const PetscInt *idx;
3192ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3193552f7358SJed Brown 
31940d644c17SKarl Rupp     if (numFields) field = bcField[bc];
3195ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3196ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3197552f7358SJed Brown     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3198552f7358SJed Brown     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3199552f7358SJed Brown     for (i = 0; i < n; ++i) {
3200552f7358SJed Brown       const PetscInt p = idx[i];
320106ff1081SMatthew G. Knepley       PetscInt       numConst;
3202552f7358SJed Brown 
3203552f7358SJed Brown       if (numFields) {
3204552f7358SJed Brown         ierr = PetscSectionGetFieldDof(section, p, field, &numConst);CHKERRQ(ierr);
3205552f7358SJed Brown       } else {
3206552f7358SJed Brown         ierr = PetscSectionGetDof(section, p, &numConst);CHKERRQ(ierr);
3207552f7358SJed Brown       }
320806ff1081SMatthew G. Knepley       /* If Nc < 0, constrain every dof on the point */
320906ff1081SMatthew G. Knepley       if (Nc > 0) numConst = PetscMin(numConst, Nc);
321006ff1081SMatthew G. Knepley       if (numFields) {ierr = PetscSectionAddFieldConstraintDof(section, p, field, numConst);CHKERRQ(ierr);}
3211552f7358SJed Brown       ierr = PetscSectionAddConstraintDof(section, p, numConst);CHKERRQ(ierr);
3212552f7358SJed Brown     }
3213552f7358SJed Brown     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
321406ff1081SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3215552f7358SJed Brown   }
3216a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3217a68b90caSToby Isaac   if (aSec) {
3218a68b90caSToby Isaac     PetscInt aStart, aEnd, a;
3219a68b90caSToby Isaac 
3220a68b90caSToby Isaac     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3221a68b90caSToby Isaac     for (a = aStart; a < aEnd; a++) {
3222ab1d0545SMatthew G. Knepley       PetscInt dof, f;
3223a68b90caSToby Isaac 
3224a68b90caSToby Isaac       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3225a68b90caSToby Isaac       if (dof) {
3226a68b90caSToby Isaac         /* if there are point-to-point constraints, then all dofs are constrained */
3227a68b90caSToby Isaac         ierr = PetscSectionGetDof(section, a, &dof);CHKERRQ(ierr);
3228a68b90caSToby Isaac         ierr = PetscSectionSetConstraintDof(section, a, dof);CHKERRQ(ierr);
3229a68b90caSToby Isaac         for (f = 0; f < numFields; f++) {
3230a68b90caSToby Isaac           ierr = PetscSectionGetFieldDof(section, a, f, &dof);CHKERRQ(ierr);
3231a68b90caSToby Isaac           ierr = PetscSectionSetFieldConstraintDof(section, a, f, dof);CHKERRQ(ierr);
3232a68b90caSToby Isaac         }
3233a68b90caSToby Isaac       }
3234a68b90caSToby Isaac     }
3235a68b90caSToby Isaac   }
3236552f7358SJed Brown   PetscFunctionReturn(0);
3237552f7358SJed Brown }
3238552f7358SJed Brown 
3239ab1d0545SMatthew G. Knepley /* Set the constrained field indices on each point
3240ab1d0545SMatthew G. Knepley    If bcComps is NULL or the IS is NULL, constrain every dof on the point
3241ab1d0545SMatthew G. Knepley */
32427cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCIndicesField(DM dm, PetscInt numBC,const PetscInt bcField[], const IS bcComps[], const IS bcPoints[], PetscSection section)
32430adebc6cSBarry Smith {
3244ab1d0545SMatthew G. Knepley   PetscSection   aSec;
3245ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3246ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, bc, f, d;
3247552f7358SJed Brown   PetscErrorCode ierr;
3248552f7358SJed Brown 
3249552f7358SJed Brown   PetscFunctionBegin;
3250552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3251ab1d0545SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
3252ab1d0545SMatthew G. Knepley   /* Initialize all field indices to -1 */
3253552f7358SJed Brown   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3254ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3255ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3256ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3257ab1d0545SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) for (f = 0; f < numFields; ++f) {ierr = PetscSectionSetFieldConstraintIndices(section, p, f, indices);CHKERRQ(ierr);}
3258ab1d0545SMatthew G. Knepley   /* Handle BC constraints */
3259ab1d0545SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {
3260ab1d0545SMatthew G. Knepley     const PetscInt  field = bcField[bc];
3261ab1d0545SMatthew G. Knepley     const PetscInt *comp, *idx;
3262ab1d0545SMatthew G. Knepley     PetscInt        Nc = -1, n, i;
3263552f7358SJed Brown 
3264ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetLocalSize(bcComps[bc], &Nc);CHKERRQ(ierr);}
3265ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISGetIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3266ab1d0545SMatthew G. Knepley     ierr = ISGetLocalSize(bcPoints[bc], &n);CHKERRQ(ierr);
3267ab1d0545SMatthew G. Knepley     ierr = ISGetIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3268ab1d0545SMatthew G. Knepley     for (i = 0; i < n; ++i) {
3269ab1d0545SMatthew G. Knepley       const PetscInt  p = idx[i];
3270ab1d0545SMatthew G. Knepley       const PetscInt *find;
3271ab1d0545SMatthew G. Knepley       PetscInt        fcdof, c;
3272ab1d0545SMatthew G. Knepley 
3273ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintDof(section, p, field, &fcdof);CHKERRQ(ierr);
3274ab1d0545SMatthew G. Knepley       if (Nc < 0) {
3275ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) indices[d] = d;
3276552f7358SJed Brown       } else {
3277ab1d0545SMatthew G. Knepley         ierr = PetscSectionGetFieldConstraintIndices(section, p, field, &find);CHKERRQ(ierr);
3278ab1d0545SMatthew G. Knepley         for (d = 0; d < fcdof; ++d) {if (find[d] < 0) break; indices[d] = find[d];}
3279ab1d0545SMatthew G. Knepley         for (c = 0; c < Nc; ++c) indices[d+c] = comp[c];
3280ab1d0545SMatthew G. Knepley         ierr = PetscSortInt(d+Nc, indices);CHKERRQ(ierr);
3281ab1d0545SMatthew G. Knepley         for (c = d+Nc; c < fcdof; ++c) indices[c] = -1;
3282552f7358SJed Brown       }
3283ab1d0545SMatthew G. Knepley       ierr = PetscSectionSetFieldConstraintIndices(section, p, field, indices);CHKERRQ(ierr);
3284552f7358SJed Brown     }
3285ab1d0545SMatthew G. Knepley     if (bcComps && bcComps[bc]) {ierr = ISRestoreIndices(bcComps[bc], &comp);CHKERRQ(ierr);}
3286ab1d0545SMatthew G. Knepley     ierr = ISRestoreIndices(bcPoints[bc], &idx);CHKERRQ(ierr);
3287552f7358SJed Brown   }
3288ab1d0545SMatthew G. Knepley   /* Handle anchors */
3289ab1d0545SMatthew G. Knepley   ierr = DMPlexGetAnchors(dm, &aSec, NULL);CHKERRQ(ierr);
3290ab1d0545SMatthew G. Knepley   if (aSec) {
3291ab1d0545SMatthew G. Knepley     PetscInt aStart, aEnd, a;
3292552f7358SJed Brown 
3293ab1d0545SMatthew G. Knepley     for (d = 0; d < maxDof; ++d) indices[d] = d;
3294ab1d0545SMatthew G. Knepley     ierr = PetscSectionGetChart(aSec, &aStart, &aEnd);CHKERRQ(ierr);
3295ab1d0545SMatthew G. Knepley     for (a = aStart; a < aEnd; a++) {
3296ab1d0545SMatthew G. Knepley       PetscInt dof, fdof, f;
3297ab1d0545SMatthew G. Knepley 
3298ab1d0545SMatthew G. Knepley       ierr = PetscSectionGetDof(aSec, a, &dof);CHKERRQ(ierr);
3299ab1d0545SMatthew G. Knepley       if (dof) {
3300ab1d0545SMatthew G. Knepley         /* if there are point-to-point constraints, then all dofs are constrained */
3301ab1d0545SMatthew G. Knepley         for (f = 0; f < numFields; f++) {
3302ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, a, f, &fdof);CHKERRQ(ierr);
3303ab1d0545SMatthew G. Knepley           ierr = PetscSectionSetFieldConstraintIndices(section, a, f, indices);CHKERRQ(ierr);
3304ab1d0545SMatthew G. Knepley         }
3305ab1d0545SMatthew G. Knepley       }
3306ab1d0545SMatthew G. Knepley     }
3307ab1d0545SMatthew G. Knepley   }
3308ab1d0545SMatthew G. Knepley   ierr = PetscFree(indices);CHKERRQ(ierr);
3309ab1d0545SMatthew G. Knepley   PetscFunctionReturn(0);
3310ab1d0545SMatthew G. Knepley }
3311ab1d0545SMatthew G. Knepley 
3312ab1d0545SMatthew G. Knepley /* Set the constrained indices on each point */
33137cd05799SMatthew G. Knepley static PetscErrorCode DMPlexCreateSectionBCIndices(DM dm, PetscSection section)
3314ab1d0545SMatthew G. Knepley {
3315ab1d0545SMatthew G. Knepley   PetscInt      *indices;
3316ab1d0545SMatthew G. Knepley   PetscInt       numFields, maxDof, pStart, pEnd, p, f, d;
3317ab1d0545SMatthew G. Knepley   PetscErrorCode ierr;
3318ab1d0545SMatthew G. Knepley 
3319ab1d0545SMatthew G. Knepley   PetscFunctionBegin;
3320ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
3321ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
3322ab1d0545SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3323ab1d0545SMatthew G. Knepley   ierr = PetscMalloc1(maxDof, &indices);CHKERRQ(ierr);
3324ab1d0545SMatthew G. Knepley   for (d = 0; d < maxDof; ++d) indices[d] = -1;
3325552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
3326552f7358SJed Brown     PetscInt cdof, d;
3327552f7358SJed Brown 
3328552f7358SJed Brown     ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
3329552f7358SJed Brown     if (cdof) {
3330552f7358SJed Brown       if (numFields) {
3331552f7358SJed Brown         PetscInt numConst = 0, foff = 0;
3332552f7358SJed Brown 
3333552f7358SJed Brown         for (f = 0; f < numFields; ++f) {
3334ab1d0545SMatthew G. Knepley           const PetscInt *find;
3335ab1d0545SMatthew G. Knepley           PetscInt        fcdof, fdof;
3336552f7358SJed Brown 
3337552f7358SJed Brown           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
3338ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
3339ab1d0545SMatthew G. Knepley           /* Change constraint numbering from field component to local dof number */
3340ab1d0545SMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintIndices(section, p, f, &find);CHKERRQ(ierr);
3341ab1d0545SMatthew G. Knepley           for (d = 0; d < fcdof; ++d) indices[numConst+d] = find[d] + foff;
3342ab1d0545SMatthew G. Knepley           numConst += fcdof;
3343552f7358SJed Brown           foff     += fdof;
3344552f7358SJed Brown         }
3345552f7358SJed Brown         if (cdof != numConst) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_LIB, "Total number of field constraints %D should be %D", numConst, cdof);
3346552f7358SJed Brown       } else {
33470d644c17SKarl Rupp         for (d = 0; d < cdof; ++d) indices[d] = d;
3348552f7358SJed Brown       }
3349552f7358SJed Brown       ierr = PetscSectionSetConstraintIndices(section, p, indices);CHKERRQ(ierr);
3350552f7358SJed Brown     }
3351552f7358SJed Brown   }
3352552f7358SJed Brown   ierr = PetscFree(indices);CHKERRQ(ierr);
3353552f7358SJed Brown   PetscFunctionReturn(0);
3354552f7358SJed Brown }
3355552f7358SJed Brown 
3356552f7358SJed Brown /*@C
3357552f7358SJed Brown   DMPlexCreateSection - Create a PetscSection based upon the dof layout specification provided.
3358552f7358SJed Brown 
3359552f7358SJed Brown   Not Collective
3360552f7358SJed Brown 
3361552f7358SJed Brown   Input Parameters:
3362552f7358SJed Brown + dm        - The DMPlex object
3363552f7358SJed Brown . dim       - The spatial dimension of the problem
3364552f7358SJed Brown . numFields - The number of fields in the problem
3365552f7358SJed Brown . numComp   - An array of size numFields that holds the number of components for each field
3366552f7358SJed 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
3367552f7358SJed Brown . numBC     - The number of boundary conditions
3368552f7358SJed Brown . bcField   - An array of size numBC giving the field number for each boundry condition
3369ab1d0545SMatthew G. Knepley . bcComps   - [Optional] An array of size numBC giving an IS holding the field components to which each boundary condition applies
3370ab1d0545SMatthew G. Knepley . bcPoints  - An array of size numBC giving an IS holding the Plex points to which each boundary condition applies
3371f8f126e8SMatthew G. Knepley - perm      - Optional permutation of the chart, or NULL
3372552f7358SJed Brown 
3373552f7358SJed Brown   Output Parameter:
3374552f7358SJed Brown . section - The PetscSection object
3375552f7358SJed Brown 
3376552f7358SJed 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
3377f8f126e8SMatthew G. Knepley   number of dof for field 0 on each edge.
3378f8f126e8SMatthew G. Knepley 
3379f8f126e8SMatthew G. Knepley   The chart permutation is the same one set using PetscSectionSetPermutation()
3380552f7358SJed Brown 
3381552f7358SJed Brown   Level: developer
3382552f7358SJed Brown 
33833813dfbdSMatthew G Knepley   Fortran Notes:
33843813dfbdSMatthew G Knepley   A Fortran 90 version is available as DMPlexCreateSectionF90()
33853813dfbdSMatthew G Knepley 
3386552f7358SJed Brown .keywords: mesh, elements
3387f8f126e8SMatthew G. Knepley .seealso: DMPlexCreate(), PetscSectionCreate(), PetscSectionSetPermutation()
3388552f7358SJed Brown @*/
3389ab1d0545SMatthew 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)
3390a6dfd86eSKarl Rupp {
3391a68b90caSToby Isaac   PetscSection   aSec;
3392552f7358SJed Brown   PetscErrorCode ierr;
3393552f7358SJed Brown 
3394552f7358SJed Brown   PetscFunctionBegin;
3395552f7358SJed Brown   ierr = DMPlexCreateSectionInitial(dm, dim, numFields, numComp, numDof, section);CHKERRQ(ierr);
3396ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSectionBCDof(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3397f8f126e8SMatthew G. Knepley   if (perm) {ierr = PetscSectionSetPermutation(*section, perm);CHKERRQ(ierr);}
3398552f7358SJed Brown   ierr = PetscSectionSetUp(*section);CHKERRQ(ierr);
3399a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,NULL);CHKERRQ(ierr);
3400ab1d0545SMatthew G. Knepley   if (numBC || aSec) {
3401ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndicesField(dm, numBC, bcField, bcComps, bcPoints, *section);CHKERRQ(ierr);
3402ab1d0545SMatthew G. Knepley     ierr = DMPlexCreateSectionBCIndices(dm, *section);CHKERRQ(ierr);
3403ab1d0545SMatthew G. Knepley   }
3404ce1779c8SBarry Smith   ierr = PetscSectionViewFromOptions(*section,NULL,"-section_view");CHKERRQ(ierr);
3405552f7358SJed Brown   PetscFunctionReturn(0);
3406552f7358SJed Brown }
3407552f7358SJed Brown 
34080adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
34090adebc6cSBarry Smith {
3410efe440bfSMatthew G. Knepley   PetscSection   section, s;
3411efe440bfSMatthew G. Knepley   Mat            m;
34123e922f36SToby Isaac   PetscInt       maxHeight;
3413552f7358SJed Brown   PetscErrorCode ierr;
3414552f7358SJed Brown 
3415552f7358SJed Brown   PetscFunctionBegin;
341638221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
34173e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
34183e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
341982f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
3420552f7358SJed Brown   ierr = DMSetDefaultSection(*cdm, section);CHKERRQ(ierr);
34211d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3422efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3423efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3424efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3425efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3426efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
3427552f7358SJed Brown   PetscFunctionReturn(0);
3428552f7358SJed Brown }
3429552f7358SJed Brown 
34307cd05799SMatthew G. Knepley /*@C
34317cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
34327cd05799SMatthew G. Knepley 
34337cd05799SMatthew G. Knepley   Not Collective
34347cd05799SMatthew G. Knepley 
34357cd05799SMatthew G. Knepley   Input Parameters:
34367cd05799SMatthew G. Knepley . dm        - The DMPlex object
34377cd05799SMatthew G. Knepley 
34387cd05799SMatthew G. Knepley   Output Parameter:
34397cd05799SMatthew G. Knepley . section - The PetscSection object
34407cd05799SMatthew G. Knepley 
34417cd05799SMatthew G. Knepley   Level: developer
34427cd05799SMatthew G. Knepley 
34437cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
34447cd05799SMatthew G. Knepley @*/
34450adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
34460adebc6cSBarry Smith {
3447552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3448552f7358SJed Brown 
3449552f7358SJed Brown   PetscFunctionBegin;
3450552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3451552f7358SJed Brown   if (section) *section = mesh->coneSection;
3452552f7358SJed Brown   PetscFunctionReturn(0);
3453552f7358SJed Brown }
3454552f7358SJed Brown 
34557cd05799SMatthew G. Knepley /*@C
34567cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
34577cd05799SMatthew G. Knepley 
34587cd05799SMatthew G. Knepley   Not Collective
34597cd05799SMatthew G. Knepley 
34607cd05799SMatthew G. Knepley   Input Parameters:
34617cd05799SMatthew G. Knepley . dm        - The DMPlex object
34627cd05799SMatthew G. Knepley 
34637cd05799SMatthew G. Knepley   Output Parameter:
34647cd05799SMatthew G. Knepley . section - The PetscSection object
34657cd05799SMatthew G. Knepley 
34667cd05799SMatthew G. Knepley   Level: developer
34677cd05799SMatthew G. Knepley 
34687cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
34697cd05799SMatthew G. Knepley @*/
34708cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
34718cb4d582SMatthew G. Knepley {
34728cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
34738cb4d582SMatthew G. Knepley 
34748cb4d582SMatthew G. Knepley   PetscFunctionBegin;
34758cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34768cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
34778cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
34788cb4d582SMatthew G. Knepley }
34798cb4d582SMatthew G. Knepley 
34807cd05799SMatthew G. Knepley /*@C
34817cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
34827cd05799SMatthew G. Knepley 
34837cd05799SMatthew G. Knepley   Not Collective
34847cd05799SMatthew G. Knepley 
34857cd05799SMatthew G. Knepley   Input Parameters:
34867cd05799SMatthew G. Knepley . dm        - The DMPlex object
34877cd05799SMatthew G. Knepley 
34887cd05799SMatthew G. Knepley   Output Parameter:
34897cd05799SMatthew G. Knepley . cones - The cone for each point
34907cd05799SMatthew G. Knepley 
34917cd05799SMatthew G. Knepley   Level: developer
34927cd05799SMatthew G. Knepley 
34937cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
34947cd05799SMatthew G. Knepley @*/
3495a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3496a6dfd86eSKarl Rupp {
3497552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3498552f7358SJed Brown 
3499552f7358SJed Brown   PetscFunctionBegin;
3500552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3501552f7358SJed Brown   if (cones) *cones = mesh->cones;
3502552f7358SJed Brown   PetscFunctionReturn(0);
3503552f7358SJed Brown }
3504552f7358SJed Brown 
35057cd05799SMatthew G. Knepley /*@C
35067cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
35077cd05799SMatthew G. Knepley 
35087cd05799SMatthew G. Knepley   Not Collective
35097cd05799SMatthew G. Knepley 
35107cd05799SMatthew G. Knepley   Input Parameters:
35117cd05799SMatthew G. Knepley . dm        - The DMPlex object
35127cd05799SMatthew G. Knepley 
35137cd05799SMatthew G. Knepley   Output Parameter:
35147cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
35157cd05799SMatthew G. Knepley 
35167cd05799SMatthew G. Knepley   Level: developer
35177cd05799SMatthew G. Knepley 
35187cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
35197cd05799SMatthew G. Knepley @*/
3520a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3521a6dfd86eSKarl Rupp {
3522552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3523552f7358SJed Brown 
3524552f7358SJed Brown   PetscFunctionBegin;
3525552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3526552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3527552f7358SJed Brown   PetscFunctionReturn(0);
3528552f7358SJed Brown }
3529552f7358SJed Brown 
3530552f7358SJed Brown /******************************** FEM Support **********************************/
3531552f7358SJed Brown 
35323194fc30SMatthew G. Knepley PetscErrorCode DMPlexCreateSpectralClosurePermutation(DM dm, PetscSection section)
35333194fc30SMatthew G. Knepley {
35343194fc30SMatthew G. Knepley   PetscInt      *perm;
353589eabcffSMatthew G. Knepley   PetscInt       dim, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
35363194fc30SMatthew G. Knepley   PetscErrorCode ierr;
35373194fc30SMatthew G. Knepley 
35383194fc30SMatthew G. Knepley   PetscFunctionBegin;
35393194fc30SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
35403194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
35413194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
354289eabcffSMatthew G. Knepley   if (dim <= 1) PetscFunctionReturn(0);
35433194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
35443194fc30SMatthew G. Knepley     /* An order k SEM disc has k-1 dofs on an edge */
35453194fc30SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
35463194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
35473194fc30SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
35483194fc30SMatthew G. Knepley     k = k/Nc + 1;
354989eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
35503194fc30SMatthew G. Knepley   }
35513194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
35523194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
355389eabcffSMatthew G. Knepley     switch (dim) {
355489eabcffSMatthew G. Knepley     case 2:
35553194fc30SMatthew 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} */
35563194fc30SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
35573194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
35583194fc30SMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
35593194fc30SMatthew G. Knepley       k = k/Nc + 1;
35603194fc30SMatthew G. Knepley       /* The SEM order is
35613194fc30SMatthew G. Knepley 
35623194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
356389eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
35643194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
35653194fc30SMatthew G. Knepley       */
35663194fc30SMatthew G. Knepley       {
35673194fc30SMatthew G. Knepley         const PetscInt of   = 0;
35683194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
35693194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
35703194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
35713194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
35723194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
35733194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
35743194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
35753194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
35763194fc30SMatthew G. Knepley         PetscInt       o;
35773194fc30SMatthew G. Knepley 
35783194fc30SMatthew G. Knepley         /* bottom */
35793194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
35803194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
35813194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
35823194fc30SMatthew G. Knepley         /* middle */
35833194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
35843194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
35853194fc30SMatthew 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;
35863194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
35873194fc30SMatthew G. Knepley         }
35883194fc30SMatthew G. Knepley         /* top */
35893194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
35903194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
35913194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
35923194fc30SMatthew G. Knepley         foffset = offset;
35933194fc30SMatthew G. Knepley       }
359489eabcffSMatthew G. Knepley       break;
359589eabcffSMatthew G. Knepley     case 3:
359689eabcffSMatthew G. Knepley       /* The original hex closure is
359789eabcffSMatthew G. Knepley 
359889eabcffSMatthew G. Knepley          {c,
359989eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
360089eabcffSMatthew 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,
360189eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
360289eabcffSMatthew G. Knepley       */
360389eabcffSMatthew G. Knepley       ierr = DMPlexGetDepthStratum(dm, 1, &eStart, NULL);CHKERRQ(ierr);
360489eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, eStart, f, &k);CHKERRQ(ierr);
360589eabcffSMatthew G. Knepley       ierr = PetscSectionGetFieldComponents(section, f, &Nc);CHKERRQ(ierr);
360689eabcffSMatthew G. Knepley       k = k/Nc + 1;
360789eabcffSMatthew G. Knepley       /* The SEM order is
360889eabcffSMatthew G. Knepley          Bottom Slice
360989eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
361089eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
361189eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
361289eabcffSMatthew G. Knepley 
361389eabcffSMatthew G. Knepley          Middle Slice (j)
361489eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
361589eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
361689eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
361789eabcffSMatthew G. Knepley 
361889eabcffSMatthew G. Knepley          Top Slice
361989eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
362089eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
362189eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
362289eabcffSMatthew G. Knepley       */
362389eabcffSMatthew G. Knepley       {
362489eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
362589eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
362689eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
362789eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
362889eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
362989eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
363089eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
363189eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
363289eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
363389eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
363489eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
363589eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
363689eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
363789eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
363889eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
363989eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
364089eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
364189eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
364289eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
364389eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
364489eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
364589eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
364689eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
364789eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
364889eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
364989eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
365089eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
365189eabcffSMatthew G. Knepley         PetscInt       o, n;
365289eabcffSMatthew G. Knepley 
365389eabcffSMatthew G. Knepley         /* Bottom Slice */
365489eabcffSMatthew G. Knepley         /*   bottom */
365589eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
365689eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
365789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
365889eabcffSMatthew G. Knepley         /*   middle */
365989eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
366089eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
3661316b7f87SMax 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;}
366289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
36633194fc30SMatthew G. Knepley         }
366489eabcffSMatthew G. Knepley         /*   top */
366589eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
366689eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
366789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
366889eabcffSMatthew G. Knepley 
366989eabcffSMatthew G. Knepley         /* Middle Slice */
367089eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
367189eabcffSMatthew G. Knepley           /*   bottom */
367289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
367389eabcffSMatthew 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;
367489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
367589eabcffSMatthew G. Knepley           /*   middle */
367689eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
367789eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
367889eabcffSMatthew 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;
367989eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
368089eabcffSMatthew G. Knepley           }
368189eabcffSMatthew G. Knepley           /*   top */
368289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
368389eabcffSMatthew 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;
368489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
368589eabcffSMatthew G. Knepley         }
368689eabcffSMatthew G. Knepley 
368789eabcffSMatthew G. Knepley         /* Top Slice */
368889eabcffSMatthew G. Knepley         /*   bottom */
368989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
369089eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
369189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
369289eabcffSMatthew G. Knepley         /*   middle */
369389eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
369489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
369589eabcffSMatthew 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;
369689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
369789eabcffSMatthew G. Knepley         }
369889eabcffSMatthew G. Knepley         /*   top */
369989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
370089eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
370189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
370289eabcffSMatthew G. Knepley 
370389eabcffSMatthew G. Knepley         foffset = offset;
370489eabcffSMatthew G. Knepley       }
370589eabcffSMatthew G. Knepley       break;
370689eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
370789eabcffSMatthew G. Knepley     }
370889eabcffSMatthew G. Knepley   }
370989eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
37103194fc30SMatthew G. Knepley   /* Check permutation */
37113194fc30SMatthew G. Knepley   {
37123194fc30SMatthew G. Knepley     PetscInt *check;
37133194fc30SMatthew G. Knepley 
37143194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
37153194fc30SMatthew 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]);}
37163194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
37173194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
37183194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
37193194fc30SMatthew G. Knepley   }
37201fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
37213194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
37223194fc30SMatthew G. Knepley }
37233194fc30SMatthew G. Knepley 
3724e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
3725e071409bSToby Isaac {
3726e071409bSToby Isaac   PetscDS        prob;
3727e071409bSToby Isaac   PetscInt       depth, Nf, h;
3728e071409bSToby Isaac   DMLabel        label;
3729e071409bSToby Isaac   PetscErrorCode ierr;
3730e071409bSToby Isaac 
3731e071409bSToby Isaac   PetscFunctionBeginHot;
3732e071409bSToby Isaac   prob    = dm->prob;
3733e071409bSToby Isaac   Nf      = prob->Nf;
3734e071409bSToby Isaac   label   = dm->depthLabel;
3735e071409bSToby Isaac   *dspace = NULL;
3736e071409bSToby Isaac   if (field < Nf) {
3737e071409bSToby Isaac     PetscObject disc = prob->disc[field];
3738e071409bSToby Isaac 
3739e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
3740e071409bSToby Isaac       PetscDualSpace dsp;
3741e071409bSToby Isaac 
3742e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
3743e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
3744e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
3745e071409bSToby Isaac       h    = depth - 1 - h;
3746e071409bSToby Isaac       if (h) {
3747e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
3748e071409bSToby Isaac       } else {
3749e071409bSToby Isaac         *dspace = dsp;
3750e071409bSToby Isaac       }
3751e071409bSToby Isaac     }
3752e071409bSToby Isaac   }
3753e071409bSToby Isaac   PetscFunctionReturn(0);
3754e071409bSToby Isaac }
3755e071409bSToby Isaac 
3756e071409bSToby Isaac 
37571a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
3758a6dfd86eSKarl Rupp {
3759552f7358SJed Brown   PetscScalar    *array, *vArray;
3760d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
37611a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
3762552f7358SJed Brown   PetscErrorCode  ierr;
3763552f7358SJed Brown 
37641b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
37652a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
37665a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
37675a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
37685a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
37693f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
37709df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
37719df71ca4SMatthew G. Knepley       PetscInt dof;
3772d9917b9dSMatthew G. Knepley 
37739df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
37749df71ca4SMatthew G. Knepley       size += dof;
37759df71ca4SMatthew G. Knepley     }
37769df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
37779df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
37782a3aaacfSMatthew G. Knepley       PetscInt       dof;
37795a1bb5cfSMatthew G. Knepley 
37805a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
37812a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
37825a1bb5cfSMatthew G. Knepley       size += dof;
37835a1bb5cfSMatthew G. Knepley     }
37843f7cbbe7SMatthew G. Knepley     if (!values) {
37853f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
37863f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
37873f7cbbe7SMatthew G. Knepley     }
37885a1bb5cfSMatthew G. Knepley     ierr = DMGetWorkArray(dm, size, PETSC_SCALAR, &array);CHKERRQ(ierr);
3789982e9ed1SMatthew G. Knepley   } else {
3790982e9ed1SMatthew G. Knepley     array = *values;
3791982e9ed1SMatthew G. Knepley   }
37929df71ca4SMatthew G. Knepley   size = 0;
37935a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
37949df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
37959df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
37969df71ca4SMatthew G. Knepley     PetscScalar *varr;
3797d9917b9dSMatthew G. Knepley 
37989df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
37999df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
38009df71ca4SMatthew G. Knepley     varr = &vArray[off];
38011a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
38021a271a75SMatthew G. Knepley       array[offset] = varr[d];
38039df71ca4SMatthew G. Knepley     }
38049df71ca4SMatthew G. Knepley     size += dof;
38059df71ca4SMatthew G. Knepley   }
38069df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
38079df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
38089df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
38095a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
38105a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
38115a1bb5cfSMatthew G. Knepley 
381252ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
38135a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
38145a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
38155a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
38165a1bb5cfSMatthew G. Knepley     if (o >= 0) {
38171a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
38181a271a75SMatthew G. Knepley         array[offset] = varr[d];
38195a1bb5cfSMatthew G. Knepley       }
38205a1bb5cfSMatthew G. Knepley     } else {
38211a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
38221a271a75SMatthew G. Knepley         array[offset] = varr[d];
38235a1bb5cfSMatthew G. Knepley       }
38245a1bb5cfSMatthew G. Knepley     }
38259df71ca4SMatthew G. Knepley     size += dof;
38265a1bb5cfSMatthew G. Knepley   }
38275a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
38289df71ca4SMatthew G. Knepley   if (!*values) {
38295a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
38305a1bb5cfSMatthew G. Knepley     *values = array;
38319df71ca4SMatthew G. Knepley   } else {
38328ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
38338c312ff3SMatthew G. Knepley     *csize = size;
38349df71ca4SMatthew G. Knepley   }
38355a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
38365a1bb5cfSMatthew G. Knepley }
3837d9917b9dSMatthew G. Knepley 
3838923c78e0SToby Isaac static PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3839923c78e0SToby Isaac {
3840923c78e0SToby Isaac   const PetscInt *cla;
3841923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
3842923c78e0SToby Isaac   PetscErrorCode ierr;
3843923c78e0SToby Isaac 
3844923c78e0SToby Isaac   PetscFunctionBeginHot;
3845923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
3846923c78e0SToby Isaac   if (!*clPoints) {
3847923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
3848923c78e0SToby Isaac 
3849923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
3850923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
3851923c78e0SToby Isaac     /* Compress out points not in the section */
3852923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
3853923c78e0SToby Isaac       PetscInt r = pts[2*p];
3854923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
3855923c78e0SToby Isaac         pts[q*2]   = r;
3856923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
3857923c78e0SToby Isaac         ++q;
3858923c78e0SToby Isaac       }
3859923c78e0SToby Isaac     }
3860923c78e0SToby Isaac     np = q;
3861923c78e0SToby Isaac     cla = NULL;
3862923c78e0SToby Isaac   } else {
3863923c78e0SToby Isaac     PetscInt dof, off;
3864923c78e0SToby Isaac 
3865923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
3866923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
3867923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
3868923c78e0SToby Isaac     np   = dof/2;
3869923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
3870923c78e0SToby Isaac   }
3871923c78e0SToby Isaac   *numPoints = np;
3872923c78e0SToby Isaac   *points    = pts;
3873923c78e0SToby Isaac   *clp       = cla;
3874923c78e0SToby Isaac 
3875923c78e0SToby Isaac   PetscFunctionReturn(0);
3876923c78e0SToby Isaac }
3877923c78e0SToby Isaac 
3878923c78e0SToby Isaac static PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
3879923c78e0SToby Isaac {
3880923c78e0SToby Isaac   PetscErrorCode ierr;
3881923c78e0SToby Isaac 
3882923c78e0SToby Isaac   PetscFunctionBeginHot;
3883923c78e0SToby Isaac   if (!*clPoints) {
3884923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
3885923c78e0SToby Isaac   } else {
3886923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
3887923c78e0SToby Isaac   }
3888923c78e0SToby Isaac   *numPoints = 0;
3889923c78e0SToby Isaac   *points    = NULL;
3890923c78e0SToby Isaac   *clSec     = NULL;
3891923c78e0SToby Isaac   *clPoints  = NULL;
3892923c78e0SToby Isaac   *clp       = NULL;
3893923c78e0SToby Isaac   PetscFunctionReturn(0);
3894923c78e0SToby Isaac }
3895923c78e0SToby Isaac 
389697e99dd9SToby 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[])
38971a271a75SMatthew G. Knepley {
38981a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
389997e99dd9SToby Isaac   const PetscInt    **perms = NULL;
390097e99dd9SToby Isaac   const PetscScalar **flips = NULL;
39011a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
39021a271a75SMatthew G. Knepley 
39031a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3904fe02ba77SJed Brown   *size = 0;
390597e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
390697e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
390797e99dd9SToby Isaac     const PetscInt    point = points[2*p];
390897e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
390997e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
39101a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
39111a271a75SMatthew G. Knepley     const PetscScalar *varr;
39121a271a75SMatthew G. Knepley 
39131a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
39141a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
39151a271a75SMatthew G. Knepley     varr = &vArray[off];
391697e99dd9SToby Isaac     if (clperm) {
391797e99dd9SToby Isaac       if (perm) {
391897e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
39191a271a75SMatthew G. Knepley       } else {
392097e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
392197e99dd9SToby Isaac       }
392297e99dd9SToby Isaac       if (flip) {
392397e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
392497e99dd9SToby Isaac       }
392597e99dd9SToby Isaac     } else {
392697e99dd9SToby Isaac       if (perm) {
392797e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
392897e99dd9SToby Isaac       } else {
392997e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
393097e99dd9SToby Isaac       }
393197e99dd9SToby Isaac       if (flip) {
393297e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
39331a271a75SMatthew G. Knepley       }
39341a271a75SMatthew G. Knepley     }
393597e99dd9SToby Isaac     offset += dof;
393697e99dd9SToby Isaac   }
393797e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
39381a271a75SMatthew G. Knepley   *size = offset;
39391a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
39401a271a75SMatthew G. Knepley }
39411a271a75SMatthew G. Knepley 
394297e99dd9SToby 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[])
39431a271a75SMatthew G. Knepley {
39441a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
39451a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
39461a271a75SMatthew G. Knepley 
39471a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
3948fe02ba77SJed Brown   *size = 0;
39491a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
395097e99dd9SToby Isaac     PetscInt          p;
395197e99dd9SToby Isaac     const PetscInt    **perms = NULL;
395297e99dd9SToby Isaac     const PetscScalar **flips = NULL;
39531a271a75SMatthew G. Knepley 
395497e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
395597e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
395697e99dd9SToby Isaac       const PetscInt    point = points[2*p];
395797e99dd9SToby Isaac       PetscInt          fdof, foff, b;
39581a271a75SMatthew G. Knepley       const PetscScalar *varr;
395997e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
396097e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
39611a271a75SMatthew G. Knepley 
39621a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
39631a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
39641a271a75SMatthew G. Knepley       varr = &vArray[foff];
396597e99dd9SToby Isaac       if (clperm) {
396697e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
396797e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
396897e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
39691a271a75SMatthew G. Knepley       } else {
397097e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
397197e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
397297e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
39731a271a75SMatthew G. Knepley       }
397497e99dd9SToby Isaac       offset += fdof;
39751a271a75SMatthew G. Knepley     }
397697e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
39771a271a75SMatthew G. Knepley   }
39781a271a75SMatthew G. Knepley   *size = offset;
39791a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
39801a271a75SMatthew G. Knepley }
39811a271a75SMatthew G. Knepley 
3982552f7358SJed Brown /*@C
3983552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
3984552f7358SJed Brown 
3985552f7358SJed Brown   Not collective
3986552f7358SJed Brown 
3987552f7358SJed Brown   Input Parameters:
3988552f7358SJed Brown + dm - The DM
3989552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
3990552f7358SJed Brown . v - The local vector
3991552f7358SJed Brown - point - The sieve point in the DM
3992552f7358SJed Brown 
3993552f7358SJed Brown   Output Parameters:
3994552f7358SJed Brown + csize - The number of values in the closure, or NULL
3995552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
3996552f7358SJed Brown 
3997552f7358SJed Brown   Fortran Notes:
3998552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
3999552f7358SJed Brown   include petsc.h90 in your code.
4000552f7358SJed Brown 
4001552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
4002552f7358SJed Brown 
4003552f7358SJed Brown   Level: intermediate
4004552f7358SJed Brown 
4005552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4006552f7358SJed Brown @*/
4007552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4008552f7358SJed Brown {
4009552f7358SJed Brown   PetscSection       clSection;
4010d9917b9dSMatthew G. Knepley   IS                 clPoints;
40118a84db2dSMatthew G. Knepley   PetscScalar       *array;
40128a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
4013552f7358SJed Brown   PetscInt          *points = NULL;
40148f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
40151a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
4016552f7358SJed Brown   PetscErrorCode     ierr;
4017552f7358SJed Brown 
4018d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
4019552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4020552f7358SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
40211a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
40221a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4023552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4024552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4025552f7358SJed Brown   if (depth == 1 && numFields < 2) {
40261a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
4027552f7358SJed Brown     PetscFunctionReturn(0);
4028552f7358SJed Brown   }
40291a271a75SMatthew G. Knepley   /* Get points */
4030923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
40311fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
40321a271a75SMatthew G. Knepley   /* Get array */
4033bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
40341a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
4035552f7358SJed Brown 
40361a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
4037552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
40381a271a75SMatthew G. Knepley       asize += dof;
4039552f7358SJed Brown     }
4040bd6c0d32SMatthew G. Knepley     if (!values) {
4041923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
40421a271a75SMatthew G. Knepley       if (csize) *csize = asize;
4043bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
4044bd6c0d32SMatthew G. Knepley     }
40451a271a75SMatthew G. Knepley     ierr = DMGetWorkArray(dm, asize, PETSC_SCALAR, &array);CHKERRQ(ierr);
4046d0f6b257SMatthew G. Knepley   } else {
4047d0f6b257SMatthew G. Knepley     array = *values;
4048d0f6b257SMatthew G. Knepley   }
40498a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
40501a271a75SMatthew G. Knepley   /* Get values */
405197e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
405297e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
40531a271a75SMatthew G. Knepley   /* Cleanup points */
4054923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
40551a271a75SMatthew G. Knepley   /* Cleanup array */
40568a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
4057d0f6b257SMatthew G. Knepley   if (!*values) {
4058552f7358SJed Brown     if (csize) *csize = size;
4059552f7358SJed Brown     *values = array;
4060d0f6b257SMatthew G. Knepley   } else {
4061f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4062d0f6b257SMatthew G. Knepley     *csize = size;
4063d0f6b257SMatthew G. Knepley   }
4064552f7358SJed Brown   PetscFunctionReturn(0);
4065552f7358SJed Brown }
4066552f7358SJed Brown 
4067552f7358SJed Brown /*@C
4068552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
4069552f7358SJed Brown 
4070552f7358SJed Brown   Not collective
4071552f7358SJed Brown 
4072552f7358SJed Brown   Input Parameters:
4073552f7358SJed Brown + dm - The DM
40740298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
4075552f7358SJed Brown . v - The local vector
4076552f7358SJed Brown . point - The sieve point in the DM
40770298fd71SBarry Smith . csize - The number of values in the closure, or NULL
4078552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
4079552f7358SJed Brown 
40803813dfbdSMatthew G Knepley   Fortran Notes:
40813813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
40823813dfbdSMatthew G Knepley   include petsc.h90 in your code.
40833813dfbdSMatthew G Knepley 
40843813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
40853813dfbdSMatthew G Knepley 
4086552f7358SJed Brown   Level: intermediate
4087552f7358SJed Brown 
4088552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4089552f7358SJed Brown @*/
40907c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4091a6dfd86eSKarl Rupp {
4092552f7358SJed Brown   PetscInt       size = 0;
4093552f7358SJed Brown   PetscErrorCode ierr;
4094552f7358SJed Brown 
4095552f7358SJed Brown   PetscFunctionBegin;
4096552f7358SJed Brown   /* Should work without recalculating size */
4097552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, size, PETSC_SCALAR, (void*) values);CHKERRQ(ierr);
4098552f7358SJed Brown   PetscFunctionReturn(0);
4099552f7358SJed Brown }
4100552f7358SJed Brown 
4101552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
4102552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
4103552f7358SJed Brown 
410497e99dd9SToby 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[])
4105552f7358SJed Brown {
4106552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4107552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4108552f7358SJed Brown   PetscScalar    *a;
4109552f7358SJed Brown   PetscInt        off, cind = 0, k;
4110552f7358SJed Brown   PetscErrorCode  ierr;
4111552f7358SJed Brown 
4112552f7358SJed Brown   PetscFunctionBegin;
4113552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4114552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4115552f7358SJed Brown   a    = &array[off];
4116552f7358SJed Brown   if (!cdof || setBC) {
411797e99dd9SToby Isaac     if (clperm) {
411897e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
411997e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
4120552f7358SJed Brown     } else {
412197e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
412297e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4123552f7358SJed Brown     }
4124552f7358SJed Brown   } else {
4125552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
412697e99dd9SToby Isaac     if (clperm) {
412797e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
4128552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
412997e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4130552f7358SJed Brown         }
4131552f7358SJed Brown       } else {
4132552f7358SJed Brown         for (k = 0; k < dof; ++k) {
4133552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
413497e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
413597e99dd9SToby Isaac         }
413697e99dd9SToby Isaac       }
413797e99dd9SToby Isaac     } else {
413897e99dd9SToby Isaac       if (perm) {
413997e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
414097e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
414197e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
414297e99dd9SToby Isaac         }
414397e99dd9SToby Isaac       } else {
414497e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
414597e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
414697e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
414797e99dd9SToby Isaac         }
4148552f7358SJed Brown       }
4149552f7358SJed Brown     }
4150552f7358SJed Brown   }
4151552f7358SJed Brown   PetscFunctionReturn(0);
4152552f7358SJed Brown }
4153552f7358SJed Brown 
415497e99dd9SToby 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[])
4155a5e93ea8SMatthew G. Knepley {
4156a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
4157a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4158a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
4159a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
4160a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4161a5e93ea8SMatthew G. Knepley 
4162a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4163a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4164a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4165a5e93ea8SMatthew G. Knepley   a    = &array[off];
4166a5e93ea8SMatthew G. Knepley   if (cdof) {
4167a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
416897e99dd9SToby Isaac     if (clperm) {
416997e99dd9SToby Isaac       if (perm) {
4170a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4171a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
417297e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
417397e99dd9SToby Isaac             cind++;
4174a5e93ea8SMatthew G. Knepley           }
4175a5e93ea8SMatthew G. Knepley         }
4176a5e93ea8SMatthew G. Knepley       } else {
4177a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4178a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
417997e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
418097e99dd9SToby Isaac             cind++;
418197e99dd9SToby Isaac           }
418297e99dd9SToby Isaac         }
418397e99dd9SToby Isaac       }
418497e99dd9SToby Isaac     } else {
418597e99dd9SToby Isaac       if (perm) {
418697e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
418797e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
418897e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
418997e99dd9SToby Isaac             cind++;
419097e99dd9SToby Isaac           }
419197e99dd9SToby Isaac         }
419297e99dd9SToby Isaac       } else {
419397e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
419497e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
419597e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
419697e99dd9SToby Isaac             cind++;
419797e99dd9SToby Isaac           }
4198a5e93ea8SMatthew G. Knepley         }
4199a5e93ea8SMatthew G. Knepley       }
4200a5e93ea8SMatthew G. Knepley     }
4201a5e93ea8SMatthew G. Knepley   }
4202a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4203a5e93ea8SMatthew G. Knepley }
4204a5e93ea8SMatthew G. Knepley 
420597e99dd9SToby 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[])
4206a6dfd86eSKarl Rupp {
4207552f7358SJed Brown   PetscScalar    *a;
42081a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
42091a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
421097e99dd9SToby Isaac   PetscInt        cind = 0, b;
4211552f7358SJed Brown   PetscErrorCode  ierr;
4212552f7358SJed Brown 
4213552f7358SJed Brown   PetscFunctionBegin;
4214552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4215552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
42161a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
42171a271a75SMatthew G. Knepley   a    = &array[foff];
4218552f7358SJed Brown   if (!fcdof || setBC) {
421997e99dd9SToby Isaac     if (clperm) {
422097e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
422197e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4222552f7358SJed Brown     } else {
422397e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
422497e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4225552f7358SJed Brown     }
4226552f7358SJed Brown   } else {
4227552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
422897e99dd9SToby Isaac     if (clperm) {
422997e99dd9SToby Isaac       if (perm) {
423097e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
423197e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
423297e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4233552f7358SJed Brown         }
4234552f7358SJed Brown       } else {
423597e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
423697e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
423797e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
423897e99dd9SToby Isaac         }
423997e99dd9SToby Isaac       }
424097e99dd9SToby Isaac     } else {
424197e99dd9SToby Isaac       if (perm) {
424297e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
424397e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
424497e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
424597e99dd9SToby Isaac         }
424697e99dd9SToby Isaac       } else {
424797e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
424897e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
424997e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4250552f7358SJed Brown         }
4251552f7358SJed Brown       }
4252552f7358SJed Brown     }
4253552f7358SJed Brown   }
42541a271a75SMatthew G. Knepley   *offset += fdof;
4255552f7358SJed Brown   PetscFunctionReturn(0);
4256552f7358SJed Brown }
4257552f7358SJed Brown 
425897e99dd9SToby 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[])
4259a5e93ea8SMatthew G. Knepley {
4260a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
42611a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
42621a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
426397e99dd9SToby Isaac   PetscInt        cind = 0, b;
4264a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4265a5e93ea8SMatthew G. Knepley 
4266a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4267a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4268a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
42691a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
42701a271a75SMatthew G. Knepley   a    = &array[foff];
4271a5e93ea8SMatthew G. Knepley   if (fcdof) {
4272a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
427397e99dd9SToby Isaac     if (clperm) {
427497e99dd9SToby Isaac       if (perm) {
427597e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
427697e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
427797e99dd9SToby Isaac             fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4278a5e93ea8SMatthew G. Knepley             ++cind;
4279a5e93ea8SMatthew G. Knepley           }
4280a5e93ea8SMatthew G. Knepley         }
4281a5e93ea8SMatthew G. Knepley       } else {
428297e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
428397e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
428497e99dd9SToby Isaac             fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
428597e99dd9SToby Isaac             ++cind;
428697e99dd9SToby Isaac           }
428797e99dd9SToby Isaac         }
428897e99dd9SToby Isaac       }
428997e99dd9SToby Isaac     } else {
429097e99dd9SToby Isaac       if (perm) {
429197e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
429297e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
429397e99dd9SToby Isaac             fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
429497e99dd9SToby Isaac             ++cind;
429597e99dd9SToby Isaac           }
429697e99dd9SToby Isaac         }
429797e99dd9SToby Isaac       } else {
429897e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
429997e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {
430097e99dd9SToby Isaac             fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4301a5e93ea8SMatthew G. Knepley             ++cind;
4302a5e93ea8SMatthew G. Knepley           }
4303a5e93ea8SMatthew G. Knepley         }
4304a5e93ea8SMatthew G. Knepley       }
4305a5e93ea8SMatthew G. Knepley     }
4306a5e93ea8SMatthew G. Knepley   }
43071a271a75SMatthew G. Knepley   *offset += fdof;
4308a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4309a5e93ea8SMatthew G. Knepley }
4310a5e93ea8SMatthew G. Knepley 
43118f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4312a6dfd86eSKarl Rupp {
4313552f7358SJed Brown   PetscScalar    *array;
43141b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
43151b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4316552f7358SJed Brown   PetscErrorCode  ierr;
4317552f7358SJed Brown 
43181b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
4319b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4320b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
4321b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4322b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
4323b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4324b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4325b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
4326b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
4327b6ebb6e6SMatthew G. Knepley 
4328b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4329b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
4330b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
4331b6ebb6e6SMatthew G. Knepley     {
4332b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4333b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
4334b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
4335b6ebb6e6SMatthew G. Knepley 
4336b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
4337b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
4338b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
4339b6ebb6e6SMatthew G. Knepley       if (!cdof) {
4340b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4341b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4342b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4343b6ebb6e6SMatthew G. Knepley           }
4344b6ebb6e6SMatthew G. Knepley         } else {
4345b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4346b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4347b6ebb6e6SMatthew G. Knepley           }
4348b6ebb6e6SMatthew G. Knepley         }
4349b6ebb6e6SMatthew G. Knepley       } else {
4350b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
4351b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4352b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4353b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4354b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4355b6ebb6e6SMatthew G. Knepley           }
4356b6ebb6e6SMatthew G. Knepley         } else {
4357b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4358b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4359b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4360b6ebb6e6SMatthew G. Knepley           }
4361b6ebb6e6SMatthew G. Knepley         }
4362b6ebb6e6SMatthew G. Knepley       }
4363b6ebb6e6SMatthew G. Knepley     }
4364b6ebb6e6SMatthew G. Knepley   }
4365b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4366b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4367b6ebb6e6SMatthew G. Knepley }
43681b406b76SMatthew G. Knepley 
43691b406b76SMatthew G. Knepley /*@C
43701b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
43711b406b76SMatthew G. Knepley 
43721b406b76SMatthew G. Knepley   Not collective
43731b406b76SMatthew G. Knepley 
43741b406b76SMatthew G. Knepley   Input Parameters:
43751b406b76SMatthew G. Knepley + dm - The DM
43761b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
43771b406b76SMatthew G. Knepley . v - The local vector
43781b406b76SMatthew G. Knepley . point - The sieve point in the DM
43791b406b76SMatthew G. Knepley . values - The array of values
43801b406b76SMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
43811b406b76SMatthew G. Knepley 
43821b406b76SMatthew G. Knepley   Fortran Notes:
43831b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
43841b406b76SMatthew G. Knepley 
43851b406b76SMatthew G. Knepley   Level: intermediate
43861b406b76SMatthew G. Knepley 
43871b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
43881b406b76SMatthew G. Knepley @*/
43891b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
43901b406b76SMatthew G. Knepley {
43911b406b76SMatthew G. Knepley   PetscSection    clSection;
43921b406b76SMatthew G. Knepley   IS              clPoints;
43931b406b76SMatthew G. Knepley   PetscScalar    *array;
43941b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
439597e99dd9SToby Isaac   const PetscInt *clp, *clperm;
43961a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
43971b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
43981b406b76SMatthew G. Knepley 
43991a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
44001b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44011b406b76SMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
44021a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44031a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
44041b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
44051b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
44061b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
44078f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
44081b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
44091b406b76SMatthew G. Knepley   }
44101a271a75SMatthew G. Knepley   /* Get points */
441197e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4412923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
44131a271a75SMatthew G. Knepley   /* Get array */
4414552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
44151a271a75SMatthew G. Knepley   /* Get values */
4416ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
441797e99dd9SToby Isaac     PetscInt offset = 0, f;
4418552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
441997e99dd9SToby Isaac       const PetscInt    **perms = NULL;
442097e99dd9SToby Isaac       const PetscScalar **flips = NULL;
442197e99dd9SToby Isaac 
442297e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4423552f7358SJed Brown       switch (mode) {
4424552f7358SJed Brown       case INSERT_VALUES:
442597e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
442697e99dd9SToby Isaac           const PetscInt    point = points[2*p];
442797e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
442897e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
442997e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4430552f7358SJed Brown         } break;
4431552f7358SJed Brown       case INSERT_ALL_VALUES:
443297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
443397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
443497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
443597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
443697e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4437552f7358SJed Brown         } break;
4438a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
443997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
444097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
444197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
444297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
444397e99dd9SToby Isaac           updatePointFieldsBC_private(section, point, perm, flip, f, insert, clperm, values, &offset, array);
4444a5e93ea8SMatthew G. Knepley         } break;
4445552f7358SJed Brown       case ADD_VALUES:
444697e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
444797e99dd9SToby Isaac           const PetscInt    point = points[2*p];
444897e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
444997e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
445097e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4451552f7358SJed Brown         } break;
4452552f7358SJed Brown       case ADD_ALL_VALUES:
445397e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
445497e99dd9SToby Isaac           const PetscInt    point = points[2*p];
445597e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
445697e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
445797e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4458552f7358SJed Brown         } break;
4459304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
446097e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
446197e99dd9SToby Isaac           const PetscInt    point = points[2*p];
446297e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
446397e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
446497e99dd9SToby Isaac           updatePointFieldsBC_private(section, point, perm, flip, f, add, clperm, values, &offset, array);
4465304ab55fSMatthew G. Knepley         } break;
4466552f7358SJed Brown       default:
4467f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4468552f7358SJed Brown       }
446997e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
44701a271a75SMatthew G. Knepley     }
4471552f7358SJed Brown   } else {
44721a271a75SMatthew G. Knepley     PetscInt dof, off;
447397e99dd9SToby Isaac     const PetscInt    **perms = NULL;
447497e99dd9SToby Isaac     const PetscScalar **flips = NULL;
44751a271a75SMatthew G. Knepley 
447697e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4477552f7358SJed Brown     switch (mode) {
4478552f7358SJed Brown     case INSERT_VALUES:
447997e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
448097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
448197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
448297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
448397e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
448497e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4485552f7358SJed Brown       } break;
4486552f7358SJed Brown     case INSERT_ALL_VALUES:
448797e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
448897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
448997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
449097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
449197e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
449297e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4493552f7358SJed Brown       } break;
4494a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
449597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
449697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
449797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
449897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
449997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
450097e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4501a5e93ea8SMatthew G. Knepley       } break;
4502552f7358SJed Brown     case ADD_VALUES:
450397e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
450497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
450597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
450697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
450797e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
450897e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4509552f7358SJed Brown       } break;
4510552f7358SJed Brown     case ADD_ALL_VALUES:
451197e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
451297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
451397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
451497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
451597e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
451697e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4517552f7358SJed Brown       } break;
4518304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
451997e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
452097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
452197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
452297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
452397e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
452497e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
4525304ab55fSMatthew G. Knepley       } break;
4526552f7358SJed Brown     default:
4527f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4528552f7358SJed Brown     }
452997e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4530552f7358SJed Brown   }
45311a271a75SMatthew G. Knepley   /* Cleanup points */
4532923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
45331a271a75SMatthew G. Knepley   /* Cleanup array */
4534552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4535552f7358SJed Brown   PetscFunctionReturn(0);
4536552f7358SJed Brown }
4537552f7358SJed Brown 
4538e07394fbSMatthew G. Knepley PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, const PetscScalar values[], InsertMode mode)
4539e07394fbSMatthew G. Knepley {
4540e07394fbSMatthew G. Knepley   PetscSection      clSection;
4541e07394fbSMatthew G. Knepley   IS                clPoints;
4542e07394fbSMatthew G. Knepley   PetscScalar       *array;
4543e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
4544b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
4545e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
454697e99dd9SToby Isaac   PetscInt          offset = 0, f;
4547e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
4548e07394fbSMatthew G. Knepley 
4549e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
4550e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4551e07394fbSMatthew G. Knepley   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
4552e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4553e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4554e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4555e07394fbSMatthew G. Knepley   /* Get points */
4556b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4557923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4558e07394fbSMatthew G. Knepley   /* Get array */
4559e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4560e07394fbSMatthew G. Knepley   /* Get values */
4561e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
456297e99dd9SToby Isaac     const PetscInt    **perms = NULL;
456397e99dd9SToby Isaac     const PetscScalar **flips = NULL;
456497e99dd9SToby Isaac 
4565e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
4566e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
4567e07394fbSMatthew G. Knepley         PetscInt fdof;
4568e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
4569e07394fbSMatthew G. Knepley         offset += fdof;
4570e07394fbSMatthew G. Knepley       }
4571e07394fbSMatthew G. Knepley       continue;
4572e07394fbSMatthew G. Knepley     }
457397e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4574e07394fbSMatthew G. Knepley     switch (mode) {
4575e07394fbSMatthew G. Knepley     case INSERT_VALUES:
457697e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
457797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
457897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
457997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4580b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4581e07394fbSMatthew G. Knepley       } break;
4582e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
458397e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
458497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
458597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
458697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4587b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4588e07394fbSMatthew G. Knepley         } break;
4589e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
459097e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
459197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
459297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
459397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4594b04c866fSMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, insert, clperm, values, &offset, array);
4595e07394fbSMatthew G. Knepley       } break;
4596e07394fbSMatthew G. Knepley     case ADD_VALUES:
459797e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
459897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
459997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
460097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4601b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4602e07394fbSMatthew G. Knepley       } break;
4603e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
460497e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
460597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
460697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
460797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
4608b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4609e07394fbSMatthew G. Knepley       } break;
4610e07394fbSMatthew G. Knepley     default:
4611f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4612e07394fbSMatthew G. Knepley     }
461397e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4614e07394fbSMatthew G. Knepley   }
4615e07394fbSMatthew G. Knepley   /* Cleanup points */
4616923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
4617e07394fbSMatthew G. Knepley   /* Cleanup array */
4618e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4619e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
4620e07394fbSMatthew G. Knepley }
4621e07394fbSMatthew G. Knepley 
46227cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
4623552f7358SJed Brown {
4624552f7358SJed Brown   PetscMPIInt    rank;
4625552f7358SJed Brown   PetscInt       i, j;
4626552f7358SJed Brown   PetscErrorCode ierr;
4627552f7358SJed Brown 
4628552f7358SJed Brown   PetscFunctionBegin;
462982f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
4630f347f43bSBarry Smith   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for sieve point %D\n", rank, point);CHKERRQ(ierr);
4631e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
4632e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
4633b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
4634b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
4635e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
4636b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
4637519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
46387eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
4639552f7358SJed Brown #else
4640b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
4641552f7358SJed Brown #endif
4642552f7358SJed Brown     }
464377a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
4644552f7358SJed Brown   }
4645552f7358SJed Brown   PetscFunctionReturn(0);
4646552f7358SJed Brown }
4647552f7358SJed Brown 
4648552f7358SJed Brown /* . off - The global offset of this point */
46494acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], PetscInt indices[])
4650a6dfd86eSKarl Rupp {
4651e6ccafaeSMatthew G Knepley   PetscInt        dof;    /* The number of unknowns on this point */
4652552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4653552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4654552f7358SJed Brown   PetscInt        cind = 0, k;
4655552f7358SJed Brown   PetscErrorCode  ierr;
4656552f7358SJed Brown 
4657552f7358SJed Brown   PetscFunctionBegin;
4658552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
4659552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4660552f7358SJed Brown   if (!cdof || setBC) {
46614acb8e1eSToby Isaac     if (perm) {
46624acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+perm[k]] = off + k;
4663552f7358SJed Brown     } else {
46644acb8e1eSToby Isaac       for (k = 0; k < dof; k++) indices[*loff+k] = off + k;
4665552f7358SJed Brown     }
4666552f7358SJed Brown   } else {
4667552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
46684acb8e1eSToby Isaac     if (perm) {
46694acb8e1eSToby Isaac       for (k = 0; k < dof; ++k) {
46704acb8e1eSToby Isaac         if ((cind < cdof) && (k == cdofs[cind])) {
46714acb8e1eSToby Isaac           /* Insert check for returning constrained indices */
46724acb8e1eSToby Isaac           indices[*loff+perm[k]] = -(off+k+1);
46734acb8e1eSToby Isaac           ++cind;
46744acb8e1eSToby Isaac         } else {
46754acb8e1eSToby Isaac           indices[*loff+perm[k]] = off+k-cind;
46764acb8e1eSToby Isaac         }
46774acb8e1eSToby Isaac       }
46784acb8e1eSToby Isaac     } else {
4679552f7358SJed Brown       for (k = 0; k < dof; ++k) {
4680552f7358SJed Brown         if ((cind < cdof) && (k == cdofs[cind])) {
4681552f7358SJed Brown           /* Insert check for returning constrained indices */
4682e6ccafaeSMatthew G Knepley           indices[*loff+k] = -(off+k+1);
4683552f7358SJed Brown           ++cind;
4684552f7358SJed Brown         } else {
4685e6ccafaeSMatthew G Knepley           indices[*loff+k] = off+k-cind;
4686552f7358SJed Brown         }
4687552f7358SJed Brown       }
4688552f7358SJed Brown     }
4689552f7358SJed Brown   }
4690e6ccafaeSMatthew G Knepley   *loff += dof;
4691552f7358SJed Brown   PetscFunctionReturn(0);
4692552f7358SJed Brown }
4693552f7358SJed Brown 
4694552f7358SJed Brown /* . off - The global offset of this point */
46954acb8e1eSToby Isaac PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, PetscInt indices[])
4696a6dfd86eSKarl Rupp {
4697552f7358SJed Brown   PetscInt       numFields, foff, f;
4698552f7358SJed Brown   PetscErrorCode ierr;
4699552f7358SJed Brown 
4700552f7358SJed Brown   PetscFunctionBegin;
4701552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4702552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
47034acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
4704552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
47054acb8e1eSToby Isaac     PetscInt        cind = 0, b;
47064acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
4707552f7358SJed Brown 
4708552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4709552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
4710552f7358SJed Brown     if (!cfdof || setBC) {
47114acb8e1eSToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {indices[foffs[f]+perm[b]] = off+foff+b;}}
47124acb8e1eSToby Isaac       else      {for (b = 0; b < fdof; b++) {indices[foffs[f]+     b ] = off+foff+b;}}
4713552f7358SJed Brown     } else {
4714552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
47154acb8e1eSToby Isaac       if (perm) {
47164acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
47174acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
47184acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = -(off+foff+b+1);
4719552f7358SJed Brown             ++cind;
4720552f7358SJed Brown           } else {
47214acb8e1eSToby Isaac             indices[foffs[f]+perm[b]] = off+foff+b-cind;
4722552f7358SJed Brown           }
4723552f7358SJed Brown         }
4724552f7358SJed Brown       } else {
47254acb8e1eSToby Isaac         for (b = 0; b < fdof; b++) {
47264acb8e1eSToby Isaac           if ((cind < cfdof) && (b == fcdofs[cind])) {
47274acb8e1eSToby Isaac             indices[foffs[f]+b] = -(off+foff+b+1);
4728552f7358SJed Brown             ++cind;
4729552f7358SJed Brown           } else {
47304acb8e1eSToby Isaac             indices[foffs[f]+b] = off+foff+b-cind;
4731552f7358SJed Brown           }
4732552f7358SJed Brown         }
4733552f7358SJed Brown       }
4734552f7358SJed Brown     }
4735a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
4736552f7358SJed Brown     foffs[f] += fdof;
4737552f7358SJed Brown   }
4738552f7358SJed Brown   PetscFunctionReturn(0);
4739552f7358SJed Brown }
4740552f7358SJed Brown 
47414acb8e1eSToby 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)
4742d3d1a6afSToby Isaac {
4743d3d1a6afSToby Isaac   Mat             cMat;
4744d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
4745d3d1a6afSToby Isaac   IS              aIS;
4746d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
4747d3d1a6afSToby Isaac   const PetscInt  *anchors;
4748e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
4749d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
4750d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
4751d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
4752d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
4753d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
4754d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
4755d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
47566ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
4757d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
4758d3d1a6afSToby Isaac   PetscErrorCode  ierr;
4759d3d1a6afSToby Isaac 
4760d3d1a6afSToby Isaac   PetscFunctionBegin;
4761d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4762d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
4763d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4764d3d1a6afSToby Isaac 
4765a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
4766d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
4767d3d1a6afSToby Isaac   if (aSec) {
4768d3d1a6afSToby Isaac     ierr = PetscMemzero(newOffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
4769d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
4770d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
4771d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
4772d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
4773d3d1a6afSToby Isaac      * into the global matrix anyway) */
4774d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
4775d3d1a6afSToby Isaac       PetscInt b    = points[p];
47764b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4777d3d1a6afSToby Isaac 
47784b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
47794b2f2278SToby Isaac       if (!bSecDof) {
47804b2f2278SToby Isaac         continue;
47814b2f2278SToby Isaac       }
4782d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4783d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
4784d3d1a6afSToby Isaac       }
4785d3d1a6afSToby Isaac       if (bDof) {
4786d3d1a6afSToby Isaac         /* this point is constrained */
4787d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
4788d3d1a6afSToby Isaac         PetscInt bOff, q;
4789d3d1a6afSToby Isaac 
4790d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
4791d3d1a6afSToby Isaac         newNumPoints  += bDof;
4792d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
4793d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4794d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
4795d3d1a6afSToby Isaac           PetscInt aDof;
4796d3d1a6afSToby Isaac 
4797d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
4798d3d1a6afSToby Isaac           newNumIndices += aDof;
4799d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
4800d3d1a6afSToby Isaac             PetscInt fDof;
4801d3d1a6afSToby Isaac 
4802d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
4803d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
4804d3d1a6afSToby Isaac           }
4805d3d1a6afSToby Isaac         }
4806d3d1a6afSToby Isaac       }
4807d3d1a6afSToby Isaac       else {
4808d3d1a6afSToby Isaac         /* this point is not constrained */
4809d3d1a6afSToby Isaac         newNumPoints++;
48104b2f2278SToby Isaac         newNumIndices += bSecDof;
4811d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
4812d3d1a6afSToby Isaac           PetscInt fDof;
4813d3d1a6afSToby Isaac 
4814d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4815d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
4816d3d1a6afSToby Isaac         }
4817d3d1a6afSToby Isaac       }
4818d3d1a6afSToby Isaac     }
4819d3d1a6afSToby Isaac   }
4820d3d1a6afSToby Isaac   if (!anyConstrained) {
482172b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
482272b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
482372b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
482472b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
482572b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
4826d3d1a6afSToby Isaac     PetscFunctionReturn(0);
4827d3d1a6afSToby Isaac   }
4828d3d1a6afSToby Isaac 
48296ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
48306ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
48316ecaa68aSToby Isaac 
4832f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
4833d3d1a6afSToby Isaac 
48346ecaa68aSToby Isaac   if (!outPoints && !outValues) {
48356ecaa68aSToby Isaac     if (offsets) {
48366ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
48376ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
48386ecaa68aSToby Isaac       }
48396ecaa68aSToby Isaac     }
48406ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
48416ecaa68aSToby Isaac     PetscFunctionReturn(0);
48426ecaa68aSToby Isaac   }
48436ecaa68aSToby Isaac 
4844f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
4845d3d1a6afSToby Isaac 
4846f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
4847d3d1a6afSToby Isaac 
4848d3d1a6afSToby Isaac   /* workspaces */
4849d3d1a6afSToby Isaac   if (numFields) {
4850d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
4851d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
4852d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
4853d3d1a6afSToby Isaac     }
4854d3d1a6afSToby Isaac   }
4855d3d1a6afSToby Isaac   else {
4856d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
4857d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,numPoints,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
4858d3d1a6afSToby Isaac   }
4859d3d1a6afSToby Isaac 
4860d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
4861d3d1a6afSToby Isaac   if (numFields) {
48624b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
48634b2f2278SToby Isaac 
4864d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4865d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
48664b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4867d3d1a6afSToby Isaac 
48684b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
48694b2f2278SToby Isaac       if (!bSecDof) {
48704b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
48714b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
48724b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
48734b2f2278SToby Isaac         }
48744b2f2278SToby Isaac         continue;
48754b2f2278SToby Isaac       }
4876d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4877d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4878d3d1a6afSToby Isaac       }
4879d3d1a6afSToby Isaac       if (bDof) {
4880d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4881d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
4882d3d1a6afSToby Isaac 
4883d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4884d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4885d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
4886d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
4887d3d1a6afSToby Isaac             PetscInt aFDof;
4888d3d1a6afSToby Isaac 
4889d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
4890d3d1a6afSToby Isaac             allFDof += aFDof;
4891d3d1a6afSToby Isaac           }
4892d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
4893d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
4894d3d1a6afSToby Isaac         }
4895d3d1a6afSToby Isaac       }
4896d3d1a6afSToby Isaac       else {
4897d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4898d3d1a6afSToby Isaac           PetscInt fDof;
4899d3d1a6afSToby Isaac 
4900d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
4901d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
4902d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
4903d3d1a6afSToby Isaac         }
4904d3d1a6afSToby Isaac       }
4905d3d1a6afSToby Isaac     }
49064b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
49074b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
49084b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
4909d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
4910d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
4911d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
4912d3d1a6afSToby Isaac       }
491319f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
491419f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
4915d3d1a6afSToby Isaac       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
4916d3d1a6afSToby Isaac     }
4917d3d1a6afSToby Isaac   }
4918d3d1a6afSToby Isaac   else {
4919d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4920d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
49214b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4922d3d1a6afSToby Isaac 
49234b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
49244b2f2278SToby Isaac       if (!bSecDof) {
49254b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
49264b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
49274b2f2278SToby Isaac         continue;
49284b2f2278SToby Isaac       }
4929d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4930d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4931d3d1a6afSToby Isaac       }
4932d3d1a6afSToby Isaac       if (bDof) {
49334b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
4934d3d1a6afSToby Isaac 
4935d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
4936d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
4937d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
4938d3d1a6afSToby Isaac 
4939d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
4940d3d1a6afSToby Isaac           allDof += aDof;
4941d3d1a6afSToby Isaac         }
4942d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
49434b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
4944d3d1a6afSToby Isaac       }
4945d3d1a6afSToby Isaac       else {
49464b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
4947d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
4948d3d1a6afSToby Isaac       }
4949d3d1a6afSToby Isaac     }
4950d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
4951d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
4952d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
4953d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
4954d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
4955d3d1a6afSToby Isaac     }
4956d3d1a6afSToby Isaac     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
4957d3d1a6afSToby Isaac   }
4958d3d1a6afSToby Isaac 
49596ecaa68aSToby Isaac   /* output arrays */
49606ecaa68aSToby Isaac   ierr = DMGetWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
49616ecaa68aSToby Isaac 
4962d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
4963d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
4964d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
4965d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
4966d3d1a6afSToby Isaac   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
4967d3d1a6afSToby Isaac   if (numFields) {
4968d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
4969d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
4970d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
49714b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
4972d3d1a6afSToby Isaac 
49734b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
49744b2f2278SToby Isaac       if (!bSecDof) {
49754b2f2278SToby Isaac         continue;
49764b2f2278SToby Isaac       }
4977d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
4978d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
4979d3d1a6afSToby Isaac       }
4980d3d1a6afSToby Isaac       if (bDof) {
4981d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
4982d3d1a6afSToby Isaac 
4983d3d1a6afSToby Isaac         fStart[0] = 0;
4984d3d1a6afSToby Isaac         fEnd[0]   = 0;
4985d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4986d3d1a6afSToby Isaac           PetscInt fDof;
4987d3d1a6afSToby Isaac 
4988d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
4989d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
4990d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
4991d3d1a6afSToby Isaac         }
4992d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
49934acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, indices);CHKERRQ(ierr);
4994d3d1a6afSToby Isaac 
4995d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
4996d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
4997d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
4998d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
4999d3d1a6afSToby Isaac 
5000d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
5001d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
5002d3d1a6afSToby Isaac         }
5003d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5004d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5005d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5006d3d1a6afSToby Isaac 
5007d3d1a6afSToby 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 */
5008d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5009d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5010302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
50114acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, newIndices);CHKERRQ(ierr);
5012d3d1a6afSToby Isaac         }
5013d3d1a6afSToby Isaac         newP += bDof;
5014d3d1a6afSToby Isaac 
50156ecaa68aSToby Isaac         if (outValues) {
5016d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
5017d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
5018d3d1a6afSToby 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);
5019d3d1a6afSToby Isaac           }
5020d3d1a6afSToby Isaac         }
50216ecaa68aSToby Isaac       }
5022d3d1a6afSToby Isaac       else {
5023d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5024d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5025d3d1a6afSToby Isaac         newP++;
5026d3d1a6afSToby Isaac       }
5027d3d1a6afSToby Isaac     }
5028d3d1a6afSToby Isaac   } else {
5029d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5030d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5031d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
50324b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5033d3d1a6afSToby Isaac 
50344b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
50354b2f2278SToby Isaac       if (!bSecDof) {
50364b2f2278SToby Isaac         continue;
50374b2f2278SToby Isaac       }
5038d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5039d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5040d3d1a6afSToby Isaac       }
5041d3d1a6afSToby Isaac       if (bDof) {
5042d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
5043d3d1a6afSToby Isaac 
5044d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
50454acb8e1eSToby Isaac         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, indices);CHKERRQ(ierr);
5046d3d1a6afSToby Isaac 
5047d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
5048d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5049d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5050d3d1a6afSToby Isaac 
5051d3d1a6afSToby 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 */
5052d3d1a6afSToby Isaac 
5053d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5054d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5055302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
50564acb8e1eSToby Isaac           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, newIndices);CHKERRQ(ierr);
5057d3d1a6afSToby Isaac         }
5058d3d1a6afSToby Isaac         newP += bDof;
5059d3d1a6afSToby Isaac 
5060d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
50616ecaa68aSToby Isaac         if (outValues) {
5062d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
5063d3d1a6afSToby Isaac         }
50646ecaa68aSToby Isaac       }
5065d3d1a6afSToby Isaac       else {
5066d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5067d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5068d3d1a6afSToby Isaac         newP++;
5069d3d1a6afSToby Isaac       }
5070d3d1a6afSToby Isaac     }
5071d3d1a6afSToby Isaac   }
5072d3d1a6afSToby Isaac 
50736ecaa68aSToby Isaac   if (outValues) {
50746ecaa68aSToby Isaac     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
5075d3d1a6afSToby Isaac     ierr = PetscMemzero(tmpValues,newNumIndices*numIndices*sizeof(*tmpValues));CHKERRQ(ierr);
5076d3d1a6afSToby Isaac     /* multiply constraints on the right */
5077d3d1a6afSToby Isaac     if (numFields) {
5078d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
5079d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
5080d3d1a6afSToby Isaac 
5081d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5082d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
5083d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5084d3d1a6afSToby Isaac           PetscInt c, r, k;
5085d3d1a6afSToby Isaac           PetscInt dof;
5086d3d1a6afSToby Isaac 
5087d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
50884b2f2278SToby Isaac           if (!dof) {
50894b2f2278SToby Isaac             continue;
50904b2f2278SToby Isaac           }
5091d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5092d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
5093d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
5094d3d1a6afSToby Isaac 
5095d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5096d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
5097d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
50984acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
5099d3d1a6afSToby Isaac                 }
5100d3d1a6afSToby Isaac               }
5101d3d1a6afSToby Isaac             }
5102d3d1a6afSToby Isaac           }
5103d3d1a6afSToby Isaac           else {
5104d3d1a6afSToby Isaac             /* copy this column as is */
5105d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5106d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
5107d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5108d3d1a6afSToby Isaac               }
5109d3d1a6afSToby Isaac             }
5110d3d1a6afSToby Isaac           }
5111d3d1a6afSToby Isaac           oldOff += dof;
5112d3d1a6afSToby Isaac         }
5113d3d1a6afSToby Isaac       }
5114d3d1a6afSToby Isaac     }
5115d3d1a6afSToby Isaac     else {
5116d3d1a6afSToby Isaac       PetscInt oldOff = 0;
5117d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5118d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
5119d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
5120d3d1a6afSToby Isaac         PetscInt c, r, k;
5121d3d1a6afSToby Isaac         PetscInt dof;
5122d3d1a6afSToby Isaac 
5123d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
51244b2f2278SToby Isaac         if (!dof) {
51254b2f2278SToby Isaac           continue;
51264b2f2278SToby Isaac         }
5127d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5128d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5129d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
5130d3d1a6afSToby Isaac 
5131d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5132d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
5133d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
5134d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5135d3d1a6afSToby Isaac               }
5136d3d1a6afSToby Isaac             }
5137d3d1a6afSToby Isaac           }
5138d3d1a6afSToby Isaac         }
5139d3d1a6afSToby Isaac         else {
5140d3d1a6afSToby Isaac           /* copy this column as is */
5141d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5142d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
5143d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5144d3d1a6afSToby Isaac             }
5145d3d1a6afSToby Isaac           }
5146d3d1a6afSToby Isaac         }
5147d3d1a6afSToby Isaac         oldOff += dof;
5148d3d1a6afSToby Isaac       }
5149d3d1a6afSToby Isaac     }
5150d3d1a6afSToby Isaac 
51516ecaa68aSToby Isaac     if (multiplyLeft) {
51526ecaa68aSToby Isaac       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
5153d3d1a6afSToby Isaac       ierr = PetscMemzero(newValues,newNumIndices*newNumIndices*sizeof(*newValues));CHKERRQ(ierr);
5154d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
5155d3d1a6afSToby Isaac       if (numFields) {
5156d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5157d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
5158d3d1a6afSToby Isaac 
5159d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
5160d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
5161d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
5162d3d1a6afSToby Isaac             PetscInt c, r, k;
5163d3d1a6afSToby Isaac             PetscInt dof;
5164d3d1a6afSToby Isaac 
5165d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
5166d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5167d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5168d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
5169d3d1a6afSToby Isaac 
5170d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
5171d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5172d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
5173d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5174d3d1a6afSToby Isaac                   }
5175d3d1a6afSToby Isaac                 }
5176d3d1a6afSToby Isaac               }
5177d3d1a6afSToby Isaac             }
5178d3d1a6afSToby Isaac             else {
5179d3d1a6afSToby Isaac               /* copy this row as is */
5180d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
5181d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5182d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5183d3d1a6afSToby Isaac                 }
5184d3d1a6afSToby Isaac               }
5185d3d1a6afSToby Isaac             }
5186d3d1a6afSToby Isaac             oldOff += dof;
5187d3d1a6afSToby Isaac           }
5188d3d1a6afSToby Isaac         }
5189d3d1a6afSToby Isaac       }
5190d3d1a6afSToby Isaac       else {
5191d3d1a6afSToby Isaac         PetscInt oldOff = 0;
5192d3d1a6afSToby Isaac 
5193d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5194d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
5195d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5196d3d1a6afSToby Isaac           PetscInt c, r, k;
5197d3d1a6afSToby Isaac           PetscInt dof;
5198d3d1a6afSToby Isaac 
5199d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
5200d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5201d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5202d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5203d3d1a6afSToby Isaac 
5204d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5205d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5206d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5207d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5208d3d1a6afSToby Isaac                 }
5209d3d1a6afSToby Isaac               }
5210d3d1a6afSToby Isaac             }
5211d3d1a6afSToby Isaac           }
5212d3d1a6afSToby Isaac           else {
5213d3d1a6afSToby Isaac             /* copy this row as is */
52149fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5215d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5216d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5217d3d1a6afSToby Isaac               }
5218d3d1a6afSToby Isaac             }
5219d3d1a6afSToby Isaac           }
5220d3d1a6afSToby Isaac           oldOff += dof;
5221d3d1a6afSToby Isaac         }
5222d3d1a6afSToby Isaac       }
5223d3d1a6afSToby Isaac 
52246ecaa68aSToby Isaac       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,PETSC_SCALAR,&tmpValues);CHKERRQ(ierr);
52256ecaa68aSToby Isaac     }
52266ecaa68aSToby Isaac     else {
52276ecaa68aSToby Isaac       newValues = tmpValues;
52286ecaa68aSToby Isaac     }
52296ecaa68aSToby Isaac   }
52306ecaa68aSToby Isaac 
5231d3d1a6afSToby Isaac   /* clean up */
5232d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxDof,PETSC_INT,&indices);CHKERRQ(ierr);
5233d3d1a6afSToby Isaac   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,PETSC_INT,&newIndices);CHKERRQ(ierr);
52346ecaa68aSToby Isaac 
5235d3d1a6afSToby Isaac   if (numFields) {
5236d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
5237d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],PETSC_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5238d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
5239d3d1a6afSToby Isaac       ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5240d3d1a6afSToby Isaac     }
5241d3d1a6afSToby Isaac   }
5242d3d1a6afSToby Isaac   else {
5243d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],PETSC_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5244d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
5245d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,numPoints+1,PETSC_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5246d3d1a6afSToby Isaac   }
5247d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5248d3d1a6afSToby Isaac 
5249d3d1a6afSToby Isaac   /* output */
52506ecaa68aSToby Isaac   if (outPoints) {
5251d3d1a6afSToby Isaac     *outPoints = newPoints;
52526ecaa68aSToby Isaac   }
52536ecaa68aSToby Isaac   else {
52546ecaa68aSToby Isaac     ierr = DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
52556ecaa68aSToby Isaac   }
525631620726SToby Isaac   if (outValues) {
5257d3d1a6afSToby Isaac     *outValues = newValues;
52586ecaa68aSToby Isaac   }
52596ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
5260d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
5261d3d1a6afSToby Isaac   }
5262d3d1a6afSToby Isaac   PetscFunctionReturn(0);
5263d3d1a6afSToby Isaac }
5264d3d1a6afSToby Isaac 
52657cd05799SMatthew G. Knepley /*@C
52667cd05799SMatthew G. Knepley   DMPlexGetClosureIndices - Get the indices in a vector v for all points in the closure of the given point
52677cd05799SMatthew G. Knepley 
52687cd05799SMatthew G. Knepley   Not collective
52697cd05799SMatthew G. Knepley 
52707cd05799SMatthew G. Knepley   Input Parameters:
52717cd05799SMatthew G. Knepley + dm - The DM
52727cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
52737cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
52747cd05799SMatthew G. Knepley - point - The mesh point
52757cd05799SMatthew G. Knepley 
52767cd05799SMatthew G. Knepley   Output parameters:
52777cd05799SMatthew G. Knepley + numIndices - The number of indices
52787cd05799SMatthew G. Knepley . indices - The indices
52797cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
52807cd05799SMatthew G. Knepley 
52817cd05799SMatthew G. Knepley   Note: Must call DMPlexRestoreClosureIndices() to free allocated memory
52827cd05799SMatthew G. Knepley 
52837cd05799SMatthew G. Knepley   Level: advanced
52847cd05799SMatthew G. Knepley 
52857cd05799SMatthew G. Knepley .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
52867cd05799SMatthew G. Knepley @*/
52876ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
52887773e69fSMatthew G. Knepley {
52897773e69fSMatthew G. Knepley   PetscSection    clSection;
52907773e69fSMatthew G. Knepley   IS              clPoints;
52917773e69fSMatthew G. Knepley   const PetscInt *clp;
52924acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
52937773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
52947773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
52957773e69fSMatthew G. Knepley   PetscInt        offsets[32];
52967773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
52977773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
52987773e69fSMatthew G. Knepley 
52997773e69fSMatthew G. Knepley   PetscFunctionBegin;
53007773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53017773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
53027773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
53037773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
53047773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
53057773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
53067773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
53077773e69fSMatthew G. Knepley   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
53087773e69fSMatthew G. Knepley   /* Get points in closure */
5309923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
53107773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
53117773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
53127773e69fSMatthew G. Knepley     PetscInt dof, fdof;
53137773e69fSMatthew G. Knepley 
53147773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
53157773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
53167773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
53177773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
53187773e69fSMatthew G. Knepley     }
53197773e69fSMatthew G. Knepley     Nind += dof;
53207773e69fSMatthew G. Knepley   }
53217773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
53228ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
53234acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
53244acb8e1eSToby Isaac   /* Get dual space symmetries */
53254acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
53264acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
53274acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
53284acb8e1eSToby Isaac   }
53297773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
53307773e69fSMatthew G. Knepley   {
53314acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
53327773e69fSMatthew G. Knepley     if (numPointsNew) {
53334acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
53344acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
53354acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
53364acb8e1eSToby Isaac       }
53374acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
53384acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
53394acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
53404acb8e1eSToby Isaac       }
5341923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
53427773e69fSMatthew G. Knepley       numPoints = numPointsNew;
53437773e69fSMatthew G. Knepley       Nind      = NindNew;
53447773e69fSMatthew G. Knepley       points    = pointsNew;
53457773e69fSMatthew G. Knepley     }
53467773e69fSMatthew G. Knepley   }
53477773e69fSMatthew G. Knepley   /* Calculate indices */
53487773e69fSMatthew G. Knepley   ierr = DMGetWorkArray(dm, Nind, PETSC_INT, indices);CHKERRQ(ierr);
53497773e69fSMatthew G. Knepley   if (Nf) {
53506ecaa68aSToby Isaac     if (outOffsets) {
53516ecaa68aSToby Isaac       PetscInt f;
53526ecaa68aSToby Isaac 
535346bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
53546ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
53556ecaa68aSToby Isaac       }
53566ecaa68aSToby Isaac     }
53574acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
53584acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
53594acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, *indices);
53607773e69fSMatthew G. Knepley     }
53617773e69fSMatthew G. Knepley   } else {
53624acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
53634acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
53644acb8e1eSToby Isaac 
53654acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
53664acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, *indices);
53677773e69fSMatthew G. Knepley     }
53687773e69fSMatthew G. Knepley   }
53697773e69fSMatthew G. Knepley   /* Cleanup points */
53704acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
53714acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
53724acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
53734acb8e1eSToby Isaac   }
53747773e69fSMatthew G. Knepley   if (numPointsNew) {
53757773e69fSMatthew G. Knepley     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, PETSC_INT, &pointsNew);CHKERRQ(ierr);
53767773e69fSMatthew G. Knepley   } else {
5377923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
53787773e69fSMatthew G. Knepley   }
53797773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
53807773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
53817773e69fSMatthew G. Knepley }
53827773e69fSMatthew G. Knepley 
53837cd05799SMatthew G. Knepley /*@C
53847cd05799SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restore the indices in a vector v for all points in the closure of the given point
53857cd05799SMatthew G. Knepley 
53867cd05799SMatthew G. Knepley   Not collective
53877cd05799SMatthew G. Knepley 
53887cd05799SMatthew G. Knepley   Input Parameters:
53897cd05799SMatthew G. Knepley + dm - The DM
53907cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
53917cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
53927cd05799SMatthew G. Knepley . point - The mesh point
53937cd05799SMatthew G. Knepley . numIndices - The number of indices
53947cd05799SMatthew G. Knepley . indices - The indices
53957cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
53967cd05799SMatthew G. Knepley 
53977cd05799SMatthew G. Knepley   Level: advanced
53987cd05799SMatthew G. Knepley 
53997cd05799SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
54007cd05799SMatthew G. Knepley @*/
540146bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
54027773e69fSMatthew G. Knepley {
54037773e69fSMatthew G. Knepley   PetscErrorCode ierr;
54047773e69fSMatthew G. Knepley 
54057773e69fSMatthew G. Knepley   PetscFunctionBegin;
54067773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54077773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
54087773e69fSMatthew G. Knepley   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, indices);CHKERRQ(ierr);
54097773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
54107773e69fSMatthew G. Knepley }
54117773e69fSMatthew G. Knepley 
54127f5d1fdeSMatthew G. Knepley /*@C
54137f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
54147f5d1fdeSMatthew G. Knepley 
54157f5d1fdeSMatthew G. Knepley   Not collective
54167f5d1fdeSMatthew G. Knepley 
54177f5d1fdeSMatthew G. Knepley   Input Parameters:
54187f5d1fdeSMatthew G. Knepley + dm - The DM
5419ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5420ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
54217f5d1fdeSMatthew G. Knepley . A - The matrix
54227f5d1fdeSMatthew G. Knepley . point - The sieve point in the DM
54237f5d1fdeSMatthew G. Knepley . values - The array of values
54247f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
54257f5d1fdeSMatthew G. Knepley 
54267f5d1fdeSMatthew G. Knepley   Fortran Notes:
54277f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
54287f5d1fdeSMatthew G. Knepley 
54297f5d1fdeSMatthew G. Knepley   Level: intermediate
54307f5d1fdeSMatthew G. Knepley 
54317f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
54327f5d1fdeSMatthew G. Knepley @*/
54337c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5434552f7358SJed Brown {
5435552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
54361b406b76SMatthew G. Knepley   PetscSection        clSection;
54371b406b76SMatthew G. Knepley   IS                  clPoints;
5438d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
54391b406b76SMatthew G. Knepley   const PetscInt     *clp;
5440552f7358SJed Brown   PetscInt           *indices;
5441552f7358SJed Brown   PetscInt            offsets[32];
54424acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
54434acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
5444923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
54454acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
5446d3d1a6afSToby Isaac   PetscScalar        *newValues;
5447552f7358SJed Brown   PetscErrorCode      ierr;
5448552f7358SJed Brown 
5449552f7358SJed Brown   PetscFunctionBegin;
5450552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5451ebd6d717SJed Brown   if (!section) {ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);}
54523dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5453ebd6d717SJed Brown   if (!globalSection) {ierr = DMGetDefaultGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
54543dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
54553dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5456552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
545782f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5458552f7358SJed Brown   ierr = PetscMemzero(offsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5459923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5460552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5461552f7358SJed Brown     PetscInt fdof;
5462552f7358SJed Brown 
5463552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5464552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5465552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5466552f7358SJed Brown       offsets[f+1] += fdof;
5467552f7358SJed Brown     }
5468552f7358SJed Brown     numIndices += dof;
5469552f7358SJed Brown   }
54700d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
54710d644c17SKarl Rupp 
54728ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
54734acb8e1eSToby Isaac   /* Get symmetries */
54744acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
54754acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54764acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
54774acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
54784acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
54794acb8e1eSToby Isaac 
54804acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
54814acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
54824acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
54834acb8e1eSToby Isaac 
54844acb8e1eSToby Isaac         if (!numFields) {
54854acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
54864acb8e1eSToby Isaac         } else {
54874acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
54884acb8e1eSToby Isaac         }
54894acb8e1eSToby Isaac         if (flip) {
54904acb8e1eSToby Isaac           PetscInt i, j, k;
54914acb8e1eSToby Isaac 
54924acb8e1eSToby Isaac           if (!valCopy) {
54934acb8e1eSToby Isaac             ierr = DMGetWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
54944acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
54954acb8e1eSToby Isaac             values = valCopy;
54964acb8e1eSToby Isaac           }
54974acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
5498775f7be2SToby Isaac             PetscScalar fval = flip[i];
54994acb8e1eSToby Isaac 
55004acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
55014acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
55024acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
55034acb8e1eSToby Isaac             }
55044acb8e1eSToby Isaac           }
55054acb8e1eSToby Isaac         }
55064acb8e1eSToby Isaac         foffset += fdof;
55074acb8e1eSToby Isaac       }
55084acb8e1eSToby Isaac     }
55094acb8e1eSToby Isaac   }
55104acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
5511d3d1a6afSToby Isaac   if (newNumPoints) {
55124acb8e1eSToby Isaac     if (valCopy) {
55134acb8e1eSToby Isaac       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
55144acb8e1eSToby Isaac     }
55154acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
55164acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
55174acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
55184acb8e1eSToby Isaac     }
55194acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
55204acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
55214acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
55224acb8e1eSToby Isaac     }
5523923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5524d3d1a6afSToby Isaac     numPoints  = newNumPoints;
5525d3d1a6afSToby Isaac     numIndices = newNumIndices;
5526d3d1a6afSToby Isaac     points     = newPoints;
5527d3d1a6afSToby Isaac     values     = newValues;
5528d3d1a6afSToby Isaac   }
5529552f7358SJed Brown   ierr = DMGetWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5530552f7358SJed Brown   if (numFields) {
55314acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
55324acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
55334acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, indices);
5534552f7358SJed Brown     }
5535552f7358SJed Brown   } else {
55364acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
55374acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
55384acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
55394acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, indices);
5540552f7358SJed Brown     }
5541552f7358SJed Brown   }
5542b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
5543552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
5544ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
5545ab1d0545SMatthew G. Knepley     PetscInt i;
5546ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
55478ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
5548ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
5549ab1d0545SMatthew G. Knepley   }
5550552f7358SJed Brown   if (ierr) {
5551552f7358SJed Brown     PetscMPIInt    rank;
5552552f7358SJed Brown     PetscErrorCode ierr2;
5553552f7358SJed Brown 
555482f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5555e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5556b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
55576a415b8fSMatthew G Knepley     ierr2 = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr2);
5558552f7358SJed Brown     CHKERRQ(ierr);
5559552f7358SJed Brown   }
55604acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
55614acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
55624acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
55634acb8e1eSToby Isaac   }
5564d3d1a6afSToby Isaac   if (newNumPoints) {
5565d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,PETSC_SCALAR,&newValues);CHKERRQ(ierr);
5566d3d1a6afSToby Isaac     ierr = DMRestoreWorkArray(dm,2*newNumPoints,PETSC_INT,&newPoints);CHKERRQ(ierr);
5567d3d1a6afSToby Isaac   }
5568d3d1a6afSToby Isaac   else {
55694acb8e1eSToby Isaac     if (valCopy) {
55704acb8e1eSToby Isaac       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,PETSC_SCALAR,&valCopy);CHKERRQ(ierr);
55714acb8e1eSToby Isaac     }
5572923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5573d3d1a6afSToby Isaac   }
5574552f7358SJed Brown   ierr = DMRestoreWorkArray(dm, numIndices, PETSC_INT, &indices);CHKERRQ(ierr);
5575552f7358SJed Brown   PetscFunctionReturn(0);
5576552f7358SJed Brown }
5577552f7358SJed Brown 
5578de41b84cSMatthew 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)
5579de41b84cSMatthew G. Knepley {
5580de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
5581de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
5582de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
5583de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
5584de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
5585de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
55864ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
5587de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
5588de41b84cSMatthew G. Knepley 
5589de41b84cSMatthew G. Knepley   PetscFunctionBegin;
5590de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
5591de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
5592de41b84cSMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
5593de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
5594de41b84cSMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
5595de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
5596de41b84cSMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
5597de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
5598de41b84cSMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
5599de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
5600de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
5601de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
5602de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5603de41b84cSMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5604de41b84cSMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
5605de41b84cSMatthew G. Knepley   /* Column indices */
5606de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
56074ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
5608de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
5609de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
5610de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
5611de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
5612de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
5613de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
5614de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
5615de41b84cSMatthew G. Knepley       ++q;
5616de41b84cSMatthew G. Knepley     }
5617de41b84cSMatthew G. Knepley   }
5618de41b84cSMatthew G. Knepley   numCPoints = q;
5619de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
5620de41b84cSMatthew G. Knepley     PetscInt fdof;
5621de41b84cSMatthew G. Knepley 
5622de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
56234ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5624de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5625de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
5626de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
5627de41b84cSMatthew G. Knepley     }
5628de41b84cSMatthew G. Knepley     numCIndices += dof;
5629de41b84cSMatthew G. Knepley   }
5630de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
5631de41b84cSMatthew G. Knepley   /* Row indices */
5632de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
5633de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
56341ba5f902SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
5635de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
5636de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
5637de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5638de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
5639de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
5640de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
5641de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
56424ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
56434ca5e9f5SMatthew G. Knepley         if (!dof) continue;
56444ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
56454ca5e9f5SMatthew G. Knepley         if (s < q) continue;
5646de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
5647de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
5648de41b84cSMatthew G. Knepley         ++q;
5649de41b84cSMatthew G. Knepley       }
5650de41b84cSMatthew G. Knepley     }
5651de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
5652de41b84cSMatthew G. Knepley   }
5653de41b84cSMatthew G. Knepley   numFPoints = q;
5654de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
5655de41b84cSMatthew G. Knepley     PetscInt fdof;
5656de41b84cSMatthew G. Knepley 
5657de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
56584ca5e9f5SMatthew G. Knepley     if (!dof) continue;
5659de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
5660de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
5661de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
5662de41b84cSMatthew G. Knepley     }
5663de41b84cSMatthew G. Knepley     numFIndices += dof;
5664de41b84cSMatthew G. Knepley   }
5665de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
5666de41b84cSMatthew G. Knepley 
56678ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
56688ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
5669de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
5670de41b84cSMatthew G. Knepley   ierr = DMGetWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
5671de41b84cSMatthew G. Knepley   if (numFields) {
56724acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
56734acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
56744acb8e1eSToby Isaac 
56754acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
56764acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
56774acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5678de41b84cSMatthew G. Knepley     }
56794acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
56804acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
56814acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
56824acb8e1eSToby Isaac     }
56834acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
56844acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
56854acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
56864acb8e1eSToby Isaac     }
56874acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
56884acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
56894acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
5690de41b84cSMatthew G. Knepley     }
5691de41b84cSMatthew G. Knepley   } else {
56924acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
56934acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
56944acb8e1eSToby Isaac 
56954acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
56964acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
56974acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
56984acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
56994acb8e1eSToby Isaac 
57004acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
57014acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);CHKERRQ(ierr);
5702de41b84cSMatthew G. Knepley     }
57034acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
57044acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
57054acb8e1eSToby Isaac 
57064acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
57074acb8e1eSToby Isaac       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);CHKERRQ(ierr);
5708de41b84cSMatthew G. Knepley     }
57094acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
57104acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
5711de41b84cSMatthew G. Knepley   }
5712de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
57134acb8e1eSToby Isaac   /* TODO: flips */
5714de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
5715de41b84cSMatthew G. Knepley   if (ierr) {
5716de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
5717de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
5718de41b84cSMatthew G. Knepley 
5719de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
5720e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
5721de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
5722de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr2);
5723de41b84cSMatthew G. Knepley     ierr2 = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr2);
5724de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
5725de41b84cSMatthew G. Knepley   }
5726549a8adaSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
5727de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
5728de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numFIndices, PETSC_INT, &findices);CHKERRQ(ierr);
5729de41b84cSMatthew G. Knepley   ierr = DMRestoreWorkArray(dmc, numCIndices, PETSC_INT, &cindices);CHKERRQ(ierr);
5730de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
5731de41b84cSMatthew G. Knepley }
5732de41b84cSMatthew G. Knepley 
57337c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
57347c927364SMatthew G. Knepley {
57357c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
57367c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
57377c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
57387c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
57397c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
57407c927364SMatthew G. Knepley   PetscErrorCode ierr;
57417c927364SMatthew G. Knepley 
57427c927364SMatthew G. Knepley   PetscFunctionBegin;
57437c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
57447c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
57457c927364SMatthew G. Knepley   if (!fsection) {ierr = DMGetDefaultSection(dmf, &fsection);CHKERRQ(ierr);}
57467c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
57477c927364SMatthew G. Knepley   if (!csection) {ierr = DMGetDefaultSection(dmc, &csection);CHKERRQ(ierr);}
57487c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
57497c927364SMatthew G. Knepley   if (!globalFSection) {ierr = DMGetDefaultGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
57507c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
57517c927364SMatthew G. Knepley   if (!globalCSection) {ierr = DMGetDefaultGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
57527c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
57537c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
57547c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
57557c927364SMatthew G. Knepley   ierr = PetscMemzero(foffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
57567c927364SMatthew G. Knepley   ierr = PetscMemzero(coffsets, 32 * sizeof(PetscInt));CHKERRQ(ierr);
57577c927364SMatthew G. Knepley   /* Column indices */
57587c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
57597c927364SMatthew G. Knepley   maxFPoints = numCPoints;
57607c927364SMatthew G. Knepley   /* Compress out points not in the section */
57617c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
57627c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
57637c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
57647c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
57657c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
57667c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
57677c927364SMatthew G. Knepley       ++q;
57687c927364SMatthew G. Knepley     }
57697c927364SMatthew G. Knepley   }
57707c927364SMatthew G. Knepley   numCPoints = q;
57717c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
57727c927364SMatthew G. Knepley     PetscInt fdof;
57737c927364SMatthew G. Knepley 
57747c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
57757c927364SMatthew G. Knepley     if (!dof) continue;
57767c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
57777c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
57787c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
57797c927364SMatthew G. Knepley     }
57807c927364SMatthew G. Knepley     numCIndices += dof;
57817c927364SMatthew G. Knepley   }
57827c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
57837c927364SMatthew G. Knepley   /* Row indices */
57847c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
57857c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
57867c927364SMatthew G. Knepley   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
57877c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
57887c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
57897c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
57907c927364SMatthew G. Knepley     /* Compress out points not in the section */
57917c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
57927c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
57937c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
57947c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
57957c927364SMatthew G. Knepley         if (!dof) continue;
57967c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
57977c927364SMatthew G. Knepley         if (s < q) continue;
57987c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
57997c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
58007c927364SMatthew G. Knepley         ++q;
58017c927364SMatthew G. Knepley       }
58027c927364SMatthew G. Knepley     }
58037c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
58047c927364SMatthew G. Knepley   }
58057c927364SMatthew G. Knepley   numFPoints = q;
58067c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
58077c927364SMatthew G. Knepley     PetscInt fdof;
58087c927364SMatthew G. Knepley 
58097c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
58107c927364SMatthew G. Knepley     if (!dof) continue;
58117c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
58127c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
58137c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
58147c927364SMatthew G. Knepley     }
58157c927364SMatthew G. Knepley     numFIndices += dof;
58167c927364SMatthew G. Knepley   }
58177c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
58187c927364SMatthew G. Knepley 
58198ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
58208ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
58217c927364SMatthew G. Knepley   if (numFields) {
58224acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
58234acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
58244acb8e1eSToby Isaac 
58254acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
58264acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
58274acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
58287c927364SMatthew G. Knepley     }
58294acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
58304acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
58314acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, findices);
58324acb8e1eSToby Isaac     }
58334acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
58344acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
58354acb8e1eSToby Isaac       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cindices);
58364acb8e1eSToby Isaac     }
58374acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
58384acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
58394acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
58407c927364SMatthew G. Knepley     }
58417c927364SMatthew G. Knepley   } else {
58424acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
58434acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
58444acb8e1eSToby Isaac 
58454acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
58464acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
58474acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
58484acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
58494acb8e1eSToby Isaac 
58504acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
58514acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, findices);
58527c927364SMatthew G. Knepley     }
58534acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
58544acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
58554acb8e1eSToby Isaac 
58564acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
58574acb8e1eSToby Isaac       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cindices);
58587c927364SMatthew G. Knepley     }
58594acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
58604acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
58617c927364SMatthew G. Knepley   }
58627c927364SMatthew G. Knepley   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, PETSC_INT, &ftotpoints);CHKERRQ(ierr);
58637c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
58647c927364SMatthew G. Knepley   PetscFunctionReturn(0);
58657c927364SMatthew G. Knepley }
58667c927364SMatthew G. Knepley 
5867f96281f8SMatthew G. Knepley /*@
5868f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
5869f96281f8SMatthew G. Knepley 
5870f96281f8SMatthew G. Knepley   Input Parameter:
5871f96281f8SMatthew G. Knepley . dm - The DMPlex object
5872f96281f8SMatthew G. Knepley 
5873f96281f8SMatthew G. Knepley   Output Parameters:
5874f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
5875dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5876dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5877dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5878f96281f8SMatthew G. Knepley 
5879f96281f8SMatthew G. Knepley   Level: developer
5880f96281f8SMatthew G. Knepley 
5881dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
5882f96281f8SMatthew G. Knepley @*/
5883770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
5884552f7358SJed Brown {
5885552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5886770b213bSMatthew G Knepley   PetscInt       dim;
5887770b213bSMatthew G Knepley   PetscErrorCode ierr;
5888552f7358SJed Brown 
5889552f7358SJed Brown   PetscFunctionBegin;
5890552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5891c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5892770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
5893770b213bSMatthew G Knepley   if (fMax) *fMax = mesh->hybridPointMax[dim-1];
5894770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
5895770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
5896552f7358SJed Brown   PetscFunctionReturn(0);
5897552f7358SJed Brown }
5898552f7358SJed Brown 
5899dc5a3409SMatthew G. Knepley /*@
5900dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
5901dc5a3409SMatthew G. Knepley 
5902dc5a3409SMatthew G. Knepley   Input Parameters:
5903dc5a3409SMatthew G. Knepley . dm   - The DMPlex object
5904dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
5905dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
5906dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
5907dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
5908dc5a3409SMatthew G. Knepley 
5909dc5a3409SMatthew G. Knepley   Level: developer
5910dc5a3409SMatthew G. Knepley 
5911dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
5912dc5a3409SMatthew G. Knepley @*/
5913770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
5914552f7358SJed Brown {
5915552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
5916770b213bSMatthew G Knepley   PetscInt       dim;
5917770b213bSMatthew G Knepley   PetscErrorCode ierr;
5918552f7358SJed Brown 
5919552f7358SJed Brown   PetscFunctionBegin;
5920552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5921c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5922770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]   = cMax;
5923770b213bSMatthew G Knepley   if (fMax >= 0) mesh->hybridPointMax[dim-1] = fMax;
5924770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]     = eMax;
5925770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]     = vMax;
5926552f7358SJed Brown   PetscFunctionReturn(0);
5927552f7358SJed Brown }
5928552f7358SJed Brown 
59297cd05799SMatthew G. Knepley /*@C
59307cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
59317cd05799SMatthew G. Knepley 
59327cd05799SMatthew G. Knepley   Input Parameter:
59337cd05799SMatthew G. Knepley . dm   - The DMPlex object
59347cd05799SMatthew G. Knepley 
59357cd05799SMatthew G. Knepley   Output Parameter:
59367cd05799SMatthew G. Knepley . cellHeight - The height of a cell
59377cd05799SMatthew G. Knepley 
59387cd05799SMatthew G. Knepley   Level: developer
59397cd05799SMatthew G. Knepley 
59407cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
59417cd05799SMatthew G. Knepley @*/
5942552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
5943552f7358SJed Brown {
5944552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5945552f7358SJed Brown 
5946552f7358SJed Brown   PetscFunctionBegin;
5947552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5948552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
5949552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
5950552f7358SJed Brown   PetscFunctionReturn(0);
5951552f7358SJed Brown }
5952552f7358SJed Brown 
59537cd05799SMatthew G. Knepley /*@C
59547cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
59557cd05799SMatthew G. Knepley 
59567cd05799SMatthew G. Knepley   Input Parameters:
59577cd05799SMatthew G. Knepley + dm   - The DMPlex object
59587cd05799SMatthew G. Knepley - cellHeight - The height of a cell
59597cd05799SMatthew G. Knepley 
59607cd05799SMatthew G. Knepley   Level: developer
59617cd05799SMatthew G. Knepley 
59627cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
59637cd05799SMatthew G. Knepley @*/
5964552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
5965552f7358SJed Brown {
5966552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
5967552f7358SJed Brown 
5968552f7358SJed Brown   PetscFunctionBegin;
5969552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5970552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
5971552f7358SJed Brown   PetscFunctionReturn(0);
5972552f7358SJed Brown }
5973552f7358SJed Brown 
5974552f7358SJed Brown /* We can easily have a form that takes an IS instead */
5975ef48cebcSMatthew G. Knepley static PetscErrorCode DMPlexCreateNumbering_Private(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
5976552f7358SJed Brown {
5977552f7358SJed Brown   PetscSection   section, globalSection;
5978552f7358SJed Brown   PetscInt      *numbers, p;
5979552f7358SJed Brown   PetscErrorCode ierr;
5980552f7358SJed Brown 
5981552f7358SJed Brown   PetscFunctionBegin;
598282f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
5983552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
5984552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5985552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
5986552f7358SJed Brown   }
5987552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
598815b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
5989854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
5990552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
5991552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
5992ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
5993ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
5994552f7358SJed Brown   }
599582f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
5996ef48cebcSMatthew G. Knepley   if (globalSize) {
5997ef48cebcSMatthew G. Knepley     PetscLayout layout;
5998ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
5999ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
6000ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
6001ef48cebcSMatthew G. Knepley   }
6002552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
6003552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
6004552f7358SJed Brown   PetscFunctionReturn(0);
6005552f7358SJed Brown }
6006552f7358SJed Brown 
600781ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
6008552f7358SJed Brown {
6009552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
6010552f7358SJed Brown   PetscErrorCode ierr;
6011552f7358SJed Brown 
6012552f7358SJed Brown   PetscFunctionBegin;
6013552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
6014552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
60150298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
601681ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
601781ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
601881ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
6019552f7358SJed Brown }
602081ed3555SMatthew G. Knepley 
60217cd05799SMatthew G. Knepley /*@C
60227cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
60237cd05799SMatthew G. Knepley 
60247cd05799SMatthew G. Knepley   Input Parameter:
60257cd05799SMatthew G. Knepley . dm   - The DMPlex object
60267cd05799SMatthew G. Knepley 
60277cd05799SMatthew G. Knepley   Output Parameter:
60287cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
60297cd05799SMatthew G. Knepley 
60307cd05799SMatthew G. Knepley   Level: developer
60317cd05799SMatthew G. Knepley 
60327cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
60337cd05799SMatthew G. Knepley @*/
603481ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
603581ed3555SMatthew G. Knepley {
603681ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
603781ed3555SMatthew G. Knepley   PetscErrorCode ierr;
603881ed3555SMatthew G. Knepley 
603981ed3555SMatthew G. Knepley   PetscFunctionBegin;
604081ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
604181ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
6042552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
6043552f7358SJed Brown   PetscFunctionReturn(0);
6044552f7358SJed Brown }
6045552f7358SJed Brown 
604681ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
604781ed3555SMatthew G. Knepley {
604881ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
604981ed3555SMatthew G. Knepley   PetscErrorCode ierr;
605081ed3555SMatthew G. Knepley 
605181ed3555SMatthew G. Knepley   PetscFunctionBegin;
605281ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
605381ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
605481ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
605581ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
605681ed3555SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Private(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
605781ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
605881ed3555SMatthew G. Knepley }
605981ed3555SMatthew G. Knepley 
60607cd05799SMatthew G. Knepley /*@C
60617cd05799SMatthew G. Knepley   DMPlexGetVertexNumbering - Get a global certex numbering for all vertices on this process
60627cd05799SMatthew G. Knepley 
60637cd05799SMatthew G. Knepley   Input Parameter:
60647cd05799SMatthew G. Knepley . dm   - The DMPlex object
60657cd05799SMatthew G. Knepley 
60667cd05799SMatthew G. Knepley   Output Parameter:
60677cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
60687cd05799SMatthew G. Knepley 
60697cd05799SMatthew G. Knepley   Level: developer
60707cd05799SMatthew G. Knepley 
60717cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
60727cd05799SMatthew G. Knepley @*/
6073552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
6074552f7358SJed Brown {
6075552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6076552f7358SJed Brown   PetscErrorCode ierr;
6077552f7358SJed Brown 
6078552f7358SJed Brown   PetscFunctionBegin;
6079552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
608081ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
6081552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
6082552f7358SJed Brown   PetscFunctionReturn(0);
6083552f7358SJed Brown }
6084552f7358SJed Brown 
60857cd05799SMatthew G. Knepley /*@C
60867cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
60877cd05799SMatthew G. Knepley 
60887cd05799SMatthew G. Knepley   Input Parameter:
60897cd05799SMatthew G. Knepley . dm   - The DMPlex object
60907cd05799SMatthew G. Knepley 
60917cd05799SMatthew G. Knepley   Output Parameter:
60927cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
60937cd05799SMatthew G. Knepley 
60947cd05799SMatthew G. Knepley   Level: developer
60957cd05799SMatthew G. Knepley 
60967cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
60977cd05799SMatthew G. Knepley @*/
6098ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
6099ef48cebcSMatthew G. Knepley {
6100ef48cebcSMatthew G. Knepley   IS             nums[4];
6101ef48cebcSMatthew G. Knepley   PetscInt       depths[4];
6102ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
6103ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
6104ef48cebcSMatthew G. Knepley 
6105ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
6106ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6107ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
61088abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
61098abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
6110ef48cebcSMatthew G. Knepley   depths[0] = depth; depths[1] = 0;
6111ef48cebcSMatthew G. Knepley   for (d = 2; d <= depth; ++d) depths[d] = depth-d+1;
6112ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
6113ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
6114ef48cebcSMatthew G. Knepley 
6115ef48cebcSMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, depths[d], &pStart, &pEnd);CHKERRQ(ierr);
6116ef48cebcSMatthew G. Knepley     ierr = DMPlexCreateNumbering_Private(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
6117ef48cebcSMatthew G. Knepley     shift += gsize;
6118ef48cebcSMatthew G. Knepley   }
6119302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
6120ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
6121ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
6122ef48cebcSMatthew G. Knepley }
6123ef48cebcSMatthew G. Knepley 
6124ca8062c8SMatthew G. Knepley /*@
6125ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
6126ca8062c8SMatthew G. Knepley 
6127ca8062c8SMatthew G. Knepley   Input Parameters:
6128ca8062c8SMatthew G. Knepley   + dm - The DMPlex object
6129ca8062c8SMatthew G. Knepley 
6130ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6131ca8062c8SMatthew G. Knepley 
6132ca8062c8SMatthew G. Knepley   Level: developer
6133ca8062c8SMatthew G. Knepley 
61349bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSkeleton(), DMCheckFaces()
6135ca8062c8SMatthew G. Knepley @*/
6136ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
6137ca8062c8SMatthew G. Knepley {
6138ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
6139ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
6140ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
6141ca8062c8SMatthew G. Knepley   PetscInt        pStart, pEnd, p, csize, ssize;
6142ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
6143ca8062c8SMatthew G. Knepley 
6144ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6145ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6146ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
6147ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
6148ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
6149ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
6150ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
6151ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
6152ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
6153ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
615442e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
615542e66dfaSMatthew G. Knepley       PetscInt  d;
615642e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
615742e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
615842e66dfaSMatthew G. Knepley       }
6159ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
6160ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
6161ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
6162ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
6163ca8062c8SMatthew G. Knepley       }
616442e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
61658ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
6166ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
61678ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
6168ca8062c8SMatthew G. Knepley         }
6169302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
61708ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
6171ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
61728ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
6173ca8062c8SMatthew G. Knepley         }
6174302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
61758ccfff9cSToby 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]);
61768ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
6177ca8062c8SMatthew G. Knepley       }
617842e66dfaSMatthew G. Knepley     }
6179ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
6180ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
6181ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
6182ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
6183ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
6184ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
6185ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
6186ca8062c8SMatthew G. Knepley       }
6187ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
61888ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
6189ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
61908ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
6191ca8062c8SMatthew G. Knepley         }
6192302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
61938ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
6194ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
61958ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
6196ca8062c8SMatthew G. Knepley         }
6197302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
61988ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
6199ca8062c8SMatthew G. Knepley       }
6200ca8062c8SMatthew G. Knepley     }
6201ca8062c8SMatthew G. Knepley   }
6202ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
6203ca8062c8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
62048ccfff9cSToby Isaac   if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
6205ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6206ca8062c8SMatthew G. Knepley }
6207ca8062c8SMatthew G. Knepley 
6208ca8062c8SMatthew G. Knepley /*@
6209ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
6210ca8062c8SMatthew G. Knepley 
6211ca8062c8SMatthew G. Knepley   Input Parameters:
6212ca8062c8SMatthew G. Knepley + dm - The DMPlex object
621358723a97SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
621458723a97SMatthew G. Knepley - cellHeight - Normally 0
6215ca8062c8SMatthew G. Knepley 
6216ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6217ca8062c8SMatthew G. Knepley 
6218ca8062c8SMatthew G. Knepley   Level: developer
6219ca8062c8SMatthew G. Knepley 
62209bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckFaces()
6221ca8062c8SMatthew G. Knepley @*/
622258723a97SMatthew G. Knepley PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscBool isSimplex, PetscInt cellHeight)
6223ca8062c8SMatthew G. Knepley {
622442363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
6225ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
6226ca8062c8SMatthew G. Knepley 
6227ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6228ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6229c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6230ca8062c8SMatthew G. Knepley   switch (dim) {
623142363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
623242363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
623342363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
6234ca8062c8SMatthew G. Knepley   default:
62358ccfff9cSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
6236ca8062c8SMatthew G. Knepley   }
623758723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
623858723a97SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
6239ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
6240ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
6241ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
624258723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
624358723a97SMatthew G. Knepley 
624458723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
624558723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
624658723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
624758723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
624858723a97SMatthew G. Knepley     }
624958723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
62508ccfff9cSToby Isaac     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
6251ca8062c8SMatthew G. Knepley   }
625242363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
625342363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
625442363296SMatthew G. Knepley 
625542363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
625642363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
625742363296SMatthew G. Knepley       const PetscInt p = closure[cl];
625842363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
625942363296SMatthew G. Knepley     }
626042363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
62618ccfff9cSToby Isaac     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
626242363296SMatthew G. Knepley   }
6263ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6264ca8062c8SMatthew G. Knepley }
62659bf0dad6SMatthew G. Knepley 
62669bf0dad6SMatthew G. Knepley /*@
62679bf0dad6SMatthew 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
62689bf0dad6SMatthew G. Knepley 
62699bf0dad6SMatthew G. Knepley   Input Parameters:
62709bf0dad6SMatthew G. Knepley + dm - The DMPlex object
62719bf0dad6SMatthew G. Knepley . isSimplex - Are the cells simplices or tensor products
62729bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
62739bf0dad6SMatthew G. Knepley 
62749bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
62759bf0dad6SMatthew G. Knepley 
62769bf0dad6SMatthew G. Knepley   Level: developer
62779bf0dad6SMatthew G. Knepley 
62789bf0dad6SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckSkeleton()
62799bf0dad6SMatthew G. Knepley @*/
62809bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexCheckFaces(DM dm, PetscBool isSimplex, PetscInt cellHeight)
62819bf0dad6SMatthew G. Knepley {
62823554e41dSMatthew G. Knepley   PetscInt       pMax[4];
62833554e41dSMatthew G. Knepley   PetscInt       dim, vStart, vEnd, cStart, cEnd, c, h;
62849bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
62859bf0dad6SMatthew G. Knepley 
62869bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
62879bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6288c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
62899bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
629025abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
62913554e41dSMatthew G. Knepley   for (h = cellHeight; h < dim; ++h) {
62923554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
62933554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
62949bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
62959bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
62969bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
62979bf0dad6SMatthew G. Knepley 
629825abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
62999bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
63009bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
63019bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
63029bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
63039bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
63049bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
63059bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
63069bf0dad6SMatthew G. Knepley       }
63073554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
63088ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
63099bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
63109bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
63119bf0dad6SMatthew G. Knepley 
63129bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
63139bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
63149bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
63159bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
63169bf0dad6SMatthew G. Knepley         }
63178ccfff9cSToby 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);
63189bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
63198ccfff9cSToby 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]);
63209bf0dad6SMatthew G. Knepley         }
63219bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
63229bf0dad6SMatthew G. Knepley       }
63239bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
63249bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
63259bf0dad6SMatthew G. Knepley     }
63263554e41dSMatthew G. Knepley   }
6327552f7358SJed Brown   PetscFunctionReturn(0);
6328552f7358SJed Brown }
63293913d7c8SMatthew G. Knepley 
6330bceba477SMatthew G. Knepley /* Pointwise interpolation
6331bceba477SMatthew G. Knepley      Just code FEM for now
6332bceba477SMatthew G. Knepley      u^f = I u^c
63334ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
63344ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
63354ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
6336bceba477SMatthew G. Knepley */
6337bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
6338bceba477SMatthew G. Knepley {
6339bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
6340bceba477SMatthew G. Knepley   PetscInt       m, n;
6341a063dac3SMatthew G. Knepley   void          *ctx;
634268132eb9SMatthew G. Knepley   DM             cdm;
634368132eb9SMatthew G. Knepley   PetscBool      regular;
6344bceba477SMatthew G. Knepley   PetscErrorCode ierr;
6345bceba477SMatthew G. Knepley 
6346bceba477SMatthew G. Knepley   PetscFunctionBegin;
6347bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
6348bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
6349bceba477SMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
6350bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
635168132eb9SMatthew G. Knepley 
6352bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
6353bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
6354bceba477SMatthew G. Knepley   ierr = MatSetType(*interpolation, dmCoarse->mattype);CHKERRQ(ierr);
6355a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
635668132eb9SMatthew G. Knepley 
6357a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
635868132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
635968132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
636068132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
636168132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
63625d1c2e58SMatthew G. Knepley   /* Use naive scaling */
63635d1c2e58SMatthew G. Knepley   ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
6364a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
6365a063dac3SMatthew G. Knepley }
6366bceba477SMatthew G. Knepley 
63676dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
6368a063dac3SMatthew G. Knepley {
636990748bafSMatthew G. Knepley   PetscErrorCode ierr;
63706dbf9973SLawrence Mitchell   VecScatter     ctx;
637190748bafSMatthew G. Knepley 
6372a063dac3SMatthew G. Knepley   PetscFunctionBegin;
63736dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
63746dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
63756dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
6376bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6377bceba477SMatthew G. Knepley }
6378bceba477SMatthew G. Knepley 
6379fd59a867SMatthew G. Knepley PetscErrorCode DMCreateDefaultSection_Plex(DM dm)
6380fd59a867SMatthew G. Knepley {
6381fd59a867SMatthew G. Knepley   PetscSection   section;
6382ab1d0545SMatthew G. Knepley   IS            *bcPoints, *bcComps;
6383ebb3236fSMatthew G. Knepley   PetscBool     *isFE;
6384286d8613SMatthew G. Knepley   PetscInt      *bcFields, *numComp, *numDof;
6385ebb3236fSMatthew G. Knepley   PetscInt       depth, dim, numBd, numBC = 0, numFields, bd, bc = 0, f;
6386ebb3236fSMatthew G. Knepley   PetscInt       cStart, cEnd, cEndInterior;
6387fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
6388fd59a867SMatthew G. Knepley 
6389fd59a867SMatthew G. Knepley   PetscFunctionBegin;
6390ebb3236fSMatthew G. Knepley   ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
6391ae71db08SMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
6392ebb3236fSMatthew G. Knepley   /* FE and FV boundary conditions are handled slightly differently */
6393ebb3236fSMatthew G. Knepley   ierr = PetscMalloc1(numFields, &isFE);CHKERRQ(ierr);
6394ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6395ebb3236fSMatthew G. Knepley     PetscObject  obj;
6396ebb3236fSMatthew G. Knepley     PetscClassId id;
6397ebb3236fSMatthew G. Knepley 
6398ebb3236fSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6399ebb3236fSMatthew G. Knepley     ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
6400ebb3236fSMatthew G. Knepley     if (id == PETSCFE_CLASSID)      {isFE[f] = PETSC_TRUE;}
6401ebb3236fSMatthew G. Knepley     else if (id == PETSCFV_CLASSID) {isFE[f] = PETSC_FALSE;}
64028ccfff9cSToby Isaac     else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Unknown discretization type for field %D", f);
6403ebb3236fSMatthew G. Knepley   }
64042f900235SMatthew G. Knepley   /* Allocate boundary point storage for FEM boundaries */
6405286d8613SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
6406c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6407ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
6408ebb3236fSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
6409dff059c6SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob, &numBd);CHKERRQ(ierr);
6410fd59a867SMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
64112f900235SMatthew G. Knepley     PetscInt                field;
6412f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6413385532a5SToby Isaac     const char             *labelName;
6414385532a5SToby Isaac     DMLabel                 label;
64152f900235SMatthew G. Knepley 
6416f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &labelName, &field, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
6417385532a5SToby Isaac     ierr = DMGetLabel(dm,labelName,&label);CHKERRQ(ierr);
6418f971fd6bSMatthew G. Knepley     if (label && isFE[field] && (type & DM_BC_ESSENTIAL)) ++numBC;
6419fd59a867SMatthew G. Knepley   }
64202f900235SMatthew G. Knepley   /* Add ghost cell boundaries for FVM */
6421ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) if (!isFE[f] && cEndInterior >= 0) ++numBC;
64226c8d74c4SMatthew G. Knepley   ierr = PetscCalloc3(numBC,&bcFields,numBC,&bcPoints,numBC,&bcComps);CHKERRQ(ierr);
6423ebb3236fSMatthew G. Knepley   /* Constrain ghost cells for FV */
6424ebb3236fSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6425ebb3236fSMatthew G. Knepley     PetscInt *newidx, c;
6426ebb3236fSMatthew G. Knepley 
6427ebb3236fSMatthew G. Knepley     if (isFE[f] || cEndInterior < 0) continue;
6428ebb3236fSMatthew G. Knepley     ierr = PetscMalloc1(cEnd-cEndInterior,&newidx);CHKERRQ(ierr);
6429ebb3236fSMatthew G. Knepley     for (c = cEndInterior; c < cEnd; ++c) newidx[c-cEndInterior] = c;
6430ebb3236fSMatthew G. Knepley     bcFields[bc] = f;
6431ebb3236fSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), cEnd-cEndInterior, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6432ebb3236fSMatthew G. Knepley   }
6433ebb3236fSMatthew G. Knepley   /* Handle FEM Dirichlet boundaries */
6434ebb3236fSMatthew G. Knepley   for (bd = 0; bd < numBd; ++bd) {
6435fd59a867SMatthew G. Knepley     const char             *bdLabel;
6436fd59a867SMatthew G. Knepley     DMLabel                 label;
64370e0f25f2SMatthew G. Knepley     const PetscInt         *comps;
6438fd59a867SMatthew G. Knepley     const PetscInt         *values;
64390e0f25f2SMatthew G. Knepley     PetscInt                bd2, field, numComps, numValues;
6440f971fd6bSMatthew G. Knepley     DMBoundaryConditionType type;
6441f971fd6bSMatthew G. Knepley     PetscBool               duplicate = PETSC_FALSE;
6442fd59a867SMatthew G. Knepley 
6443f971fd6bSMatthew G. Knepley     ierr = PetscDSGetBoundary(dm->prob, bd, &type, NULL, &bdLabel, &field, &numComps, &comps, NULL, &numValues, &values, NULL);CHKERRQ(ierr);
6444c58f1c22SToby Isaac     ierr = DMGetLabel(dm, bdLabel, &label);CHKERRQ(ierr);
6445385532a5SToby Isaac     if (!isFE[field] || !label) continue;
6446ebb3236fSMatthew G. Knepley     /* Only want to modify label once */
64477391c1cbSMatthew G. Knepley     for (bd2 = 0; bd2 < bd; ++bd2) {
64487391c1cbSMatthew G. Knepley       const char *bdname;
6449dff059c6SToby Isaac       ierr = PetscDSGetBoundary(dm->prob, bd2, NULL, NULL, &bdname, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
64507391c1cbSMatthew G. Knepley       ierr = PetscStrcmp(bdname, bdLabel, &duplicate);CHKERRQ(ierr);
64517391c1cbSMatthew G. Knepley       if (duplicate) break;
64527391c1cbSMatthew G. Knepley     }
6453ebb3236fSMatthew G. Knepley     if (!duplicate && (isFE[field])) {
6454b0bf5782SToby Isaac       /* don't complete cells, which are just present to give orientation to the boundary */
6455092e5057SToby Isaac       ierr = DMPlexLabelComplete(dm, label);CHKERRQ(ierr);
64567391c1cbSMatthew G. Knepley     }
6457ebb3236fSMatthew G. Knepley     /* Filter out cells, if you actually want to constrain cells you need to do things by hand right now */
6458f971fd6bSMatthew G. Knepley     if (type & DM_BC_ESSENTIAL) {
645993a5981aSMatthew G. Knepley       PetscInt       *newidx;
6460ebb3236fSMatthew G. Knepley       PetscInt        n, newn = 0, p, v;
646193a5981aSMatthew G. Knepley 
6462fd59a867SMatthew G. Knepley       bcFields[bc] = field;
64630e0f25f2SMatthew G. Knepley       if (numComps) {ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), numComps, comps, PETSC_COPY_VALUES, &bcComps[bc]);CHKERRQ(ierr);}
6464ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6465ebb3236fSMatthew G. Knepley         IS              tmp;
6466ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6467ebb3236fSMatthew G. Knepley 
6468c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6469ebb3236fSMatthew G. Knepley         if (!tmp) continue;
647093a5981aSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
647193a5981aSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6472ebb3236fSMatthew G. Knepley         if (isFE[field]) {
647393a5981aSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) ++newn;
6474ebb3236fSMatthew G. Knepley         } else {
6475ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) ++newn;
6476ebb3236fSMatthew G. Knepley         }
647793a5981aSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
647893a5981aSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6479fd59a867SMatthew G. Knepley       }
6480ebb3236fSMatthew G. Knepley       ierr = PetscMalloc1(newn,&newidx);CHKERRQ(ierr);
6481ebb3236fSMatthew G. Knepley       newn = 0;
6482ebb3236fSMatthew G. Knepley       for (v = 0; v < numValues; ++v) {
6483ebb3236fSMatthew G. Knepley         IS              tmp;
6484ebb3236fSMatthew G. Knepley         const PetscInt *idx;
6485ebb3236fSMatthew G. Knepley 
6486c58f1c22SToby Isaac         ierr = DMGetStratumIS(dm, bdLabel, values[v], &tmp);CHKERRQ(ierr);
6487ebb3236fSMatthew G. Knepley         if (!tmp) continue;
6488ebb3236fSMatthew G. Knepley         ierr = ISGetLocalSize(tmp, &n);CHKERRQ(ierr);
6489ebb3236fSMatthew G. Knepley         ierr = ISGetIndices(tmp, &idx);CHKERRQ(ierr);
6490ebb3236fSMatthew G. Knepley         if (isFE[field]) {
6491ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] < cStart) || (idx[p] >= cEnd)) newidx[newn++] = idx[p];
6492ebb3236fSMatthew G. Knepley         } else {
6493ebb3236fSMatthew G. Knepley           for (p = 0; p < n; ++p) if ((idx[p] >= cStart) || (idx[p] < cEnd)) newidx[newn++] = idx[p];
6494ebb3236fSMatthew G. Knepley         }
6495ebb3236fSMatthew G. Knepley         ierr = ISRestoreIndices(tmp, &idx);CHKERRQ(ierr);
6496ebb3236fSMatthew G. Knepley         ierr = ISDestroy(&tmp);CHKERRQ(ierr);
6497ebb3236fSMatthew G. Knepley       }
6498ebb3236fSMatthew G. Knepley       ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), newn, newidx, PETSC_OWN_POINTER, &bcPoints[bc++]);CHKERRQ(ierr);
6499ebb3236fSMatthew G. Knepley     }
6500fd59a867SMatthew G. Knepley   }
6501fd59a867SMatthew G. Knepley   /* Handle discretization */
6502ebb3236fSMatthew G. Knepley   ierr = PetscCalloc2(numFields,&numComp,numFields*(dim+1),&numDof);CHKERRQ(ierr);
6503fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
65040bacfa0aSMatthew G. Knepley     PetscObject obj;
65050bacfa0aSMatthew G. Knepley 
65060bacfa0aSMatthew G. Knepley     ierr = DMGetField(dm, f, &obj);CHKERRQ(ierr);
6507ebb3236fSMatthew G. Knepley     if (isFE[f]) {
65080bacfa0aSMatthew G. Knepley       PetscFE         fe = (PetscFE) obj;
6509286d8613SMatthew G. Knepley       const PetscInt *numFieldDof;
6510fd59a867SMatthew G. Knepley       PetscInt        d;
6511fd59a867SMatthew G. Knepley 
6512fd59a867SMatthew G. Knepley       ierr = PetscFEGetNumComponents(fe, &numComp[f]);CHKERRQ(ierr);
6513286d8613SMatthew G. Knepley       ierr = PetscFEGetNumDof(fe, &numFieldDof);CHKERRQ(ierr);
6514286d8613SMatthew G. Knepley       for (d = 0; d < dim+1; ++d) numDof[f*(dim+1)+d] = numFieldDof[d];
6515ebb3236fSMatthew G. Knepley     } else {
65160bacfa0aSMatthew G. Knepley       PetscFV fv = (PetscFV) obj;
65170bacfa0aSMatthew G. Knepley 
65180bacfa0aSMatthew G. Knepley       ierr = PetscFVGetNumComponents(fv, &numComp[f]);CHKERRQ(ierr);
65190bacfa0aSMatthew G. Knepley       numDof[f*(dim+1)+dim] = numComp[f];
6520ebb3236fSMatthew G. Knepley     }
6521fd59a867SMatthew G. Knepley   }
6522286d8613SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6523286d8613SMatthew G. Knepley     PetscInt d;
6524286d8613SMatthew G. Knepley     for (d = 1; d < dim; ++d) {
6525286d8613SMatthew 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.");
6526286d8613SMatthew G. Knepley     }
6527286d8613SMatthew G. Knepley   }
6528ab1d0545SMatthew G. Knepley   ierr = DMPlexCreateSection(dm, dim, numFields, numComp, numDof, numBC, bcFields, bcComps, bcPoints, NULL, &section);CHKERRQ(ierr);
6529fd59a867SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
6530fd59a867SMatthew G. Knepley     PetscFE     fe;
6531fd59a867SMatthew G. Knepley     const char *name;
653218abd763SMatthew G. Knepley 
653318abd763SMatthew G. Knepley     ierr = DMGetField(dm, f, (PetscObject *) &fe);CHKERRQ(ierr);
6534fd59a867SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) fe, &name);CHKERRQ(ierr);
6535fd59a867SMatthew G. Knepley     ierr = PetscSectionSetFieldName(section, f, name);CHKERRQ(ierr);
6536fd59a867SMatthew G. Knepley   }
6537fd59a867SMatthew G. Knepley   ierr = DMSetDefaultSection(dm, section);CHKERRQ(ierr);
6538fd59a867SMatthew G. Knepley   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
65390e0f25f2SMatthew G. Knepley   for (bc = 0; bc < numBC; ++bc) {ierr = ISDestroy(&bcPoints[bc]);CHKERRQ(ierr);ierr = ISDestroy(&bcComps[bc]);CHKERRQ(ierr);}
65400e0f25f2SMatthew G. Knepley   ierr = PetscFree3(bcFields,bcPoints,bcComps);CHKERRQ(ierr);
6541286d8613SMatthew G. Knepley   ierr = PetscFree2(numComp,numDof);CHKERRQ(ierr);
6542ebb3236fSMatthew G. Knepley   ierr = PetscFree(isFE);CHKERRQ(ierr);
6543bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
6544bceba477SMatthew G. Knepley }
6545bceba477SMatthew G. Knepley 
65460aef6b92SMatthew G. Knepley /*@
65470aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
65480aef6b92SMatthew G. Knepley 
65490aef6b92SMatthew G. Knepley   Input Parameter:
65500aef6b92SMatthew G. Knepley . dm - The DMPlex object
65510aef6b92SMatthew G. Knepley 
65520aef6b92SMatthew G. Knepley   Output Parameter:
65530aef6b92SMatthew G. Knepley . regular - The flag
65540aef6b92SMatthew G. Knepley 
65550aef6b92SMatthew G. Knepley   Level: intermediate
65560aef6b92SMatthew G. Knepley 
65570aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
65580aef6b92SMatthew G. Knepley @*/
65590aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
65600aef6b92SMatthew G. Knepley {
65610aef6b92SMatthew G. Knepley   PetscFunctionBegin;
65620aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65630aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
65640aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
65650aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
65660aef6b92SMatthew G. Knepley }
65670aef6b92SMatthew G. Knepley 
65680aef6b92SMatthew G. Knepley /*@
65690aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
65700aef6b92SMatthew G. Knepley 
65710aef6b92SMatthew G. Knepley   Input Parameters:
65720aef6b92SMatthew G. Knepley + dm - The DMPlex object
65730aef6b92SMatthew G. Knepley - regular - The flag
65740aef6b92SMatthew G. Knepley 
65750aef6b92SMatthew G. Knepley   Level: intermediate
65760aef6b92SMatthew G. Knepley 
65770aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
65780aef6b92SMatthew G. Knepley @*/
65790aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
65800aef6b92SMatthew G. Knepley {
65810aef6b92SMatthew G. Knepley   PetscFunctionBegin;
65820aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65830aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
65840aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
65850aef6b92SMatthew G. Knepley }
65860aef6b92SMatthew G. Knepley 
6587f7c74593SToby Isaac /* anchors */
6588a68b90caSToby Isaac /*@
6589f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
6590f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
6591a68b90caSToby Isaac 
6592e228b242SToby Isaac   not collective
6593a68b90caSToby Isaac 
6594a68b90caSToby Isaac   Input Parameters:
6595a68b90caSToby Isaac . dm - The DMPlex object
6596a68b90caSToby Isaac 
6597a68b90caSToby Isaac   Output Parameters:
6598a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
6599a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
6600a68b90caSToby Isaac 
6601a68b90caSToby Isaac 
6602a68b90caSToby Isaac   Level: intermediate
6603a68b90caSToby Isaac 
6604f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
6605a68b90caSToby Isaac @*/
6606a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
6607a68b90caSToby Isaac {
6608a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
660941e6d900SToby Isaac   PetscErrorCode ierr;
6610a68b90caSToby Isaac 
6611a68b90caSToby Isaac   PetscFunctionBegin;
6612a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
661341e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
6614a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
6615a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
6616a68b90caSToby Isaac   PetscFunctionReturn(0);
6617a68b90caSToby Isaac }
6618a68b90caSToby Isaac 
6619a68b90caSToby Isaac /*@
6620f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
6621f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
6622a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
6623a68b90caSToby Isaac 
6624a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
6625f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
6626a68b90caSToby Isaac 
6627e228b242SToby Isaac   collective on dm
6628a68b90caSToby Isaac 
6629a68b90caSToby Isaac   Input Parameters:
6630a68b90caSToby Isaac + dm - The DMPlex object
6631e228b242SToby 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).
6632e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
6633a68b90caSToby Isaac 
6634a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
6635a68b90caSToby Isaac 
6636a68b90caSToby Isaac   Level: intermediate
6637a68b90caSToby Isaac 
6638f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
6639a68b90caSToby Isaac @*/
6640a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
6641a68b90caSToby Isaac {
6642a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6643e228b242SToby Isaac   PetscMPIInt    result;
6644a68b90caSToby Isaac   PetscErrorCode ierr;
6645a68b90caSToby Isaac 
6646a68b90caSToby Isaac   PetscFunctionBegin;
6647a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6648e228b242SToby Isaac   if (anchorSection) {
6649e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
6650e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
6651e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
6652e228b242SToby Isaac   }
6653e228b242SToby Isaac   if (anchorIS) {
6654e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
6655e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
6656e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
6657e228b242SToby Isaac   }
6658a68b90caSToby Isaac 
6659a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
6660a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
6661a68b90caSToby Isaac   plex->anchorSection = anchorSection;
6662a68b90caSToby Isaac 
6663a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
6664a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
6665a68b90caSToby Isaac   plex->anchorIS = anchorIS;
6666a68b90caSToby Isaac 
6667a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
6668a68b90caSToby Isaac   if (anchorIS && anchorSection) {
6669a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
6670a68b90caSToby Isaac     const PetscInt *anchors;
6671a68b90caSToby Isaac 
6672a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
6673a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
6674a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
6675a68b90caSToby Isaac     for (a = 0; a < size; a++) {
6676a68b90caSToby Isaac       PetscInt p;
6677a68b90caSToby Isaac 
6678a68b90caSToby Isaac       p = anchors[a];
6679a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
6680a68b90caSToby Isaac         PetscInt dof;
6681a68b90caSToby Isaac 
6682a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6683a68b90caSToby Isaac         if (dof) {
6684a68b90caSToby Isaac           PetscErrorCode ierr2;
6685a68b90caSToby Isaac 
6686a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
66878ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
6688a68b90caSToby Isaac         }
6689a68b90caSToby Isaac       }
6690a68b90caSToby Isaac     }
6691a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
6692a68b90caSToby Isaac   }
6693a68b90caSToby Isaac #endif
6694f7c74593SToby Isaac   /* reset the generic constraints */
6695f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
6696a68b90caSToby Isaac   PetscFunctionReturn(0);
6697a68b90caSToby Isaac }
6698a68b90caSToby Isaac 
6699f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
6700a68b90caSToby Isaac {
6701f7c74593SToby Isaac   PetscSection anchorSection;
67026995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
6703a68b90caSToby Isaac   PetscErrorCode ierr;
6704a68b90caSToby Isaac 
6705a68b90caSToby Isaac   PetscFunctionBegin;
6706a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6707a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
6708e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
6709a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
67106995de1eSToby Isaac   if (numFields) {
6711719ab38cSToby Isaac     PetscInt f;
6712a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
6713719ab38cSToby Isaac 
6714719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
6715719ab38cSToby Isaac       PetscInt numComp;
6716719ab38cSToby Isaac 
6717719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
6718719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
6719719ab38cSToby Isaac     }
67206995de1eSToby Isaac   }
6721a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
67226995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
67236995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
67246995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
67256995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
6726a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
6727a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
6728a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
6729a68b90caSToby Isaac     if (dof) {
6730a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
6731a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
6732a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
6733a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
6734a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
6735a68b90caSToby Isaac       }
6736a68b90caSToby Isaac     }
6737a68b90caSToby Isaac   }
6738a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
6739a68b90caSToby Isaac   PetscFunctionReturn(0);
6740a68b90caSToby Isaac }
6741a68b90caSToby Isaac 
6742f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
6743a68b90caSToby Isaac {
6744f7c74593SToby Isaac   PetscSection aSec;
67450ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
67460ac89760SToby Isaac   const PetscInt *anchors;
67470ac89760SToby Isaac   PetscInt numFields, f;
674866ad2231SToby Isaac   IS aIS;
67490ac89760SToby Isaac   PetscErrorCode ierr;
67500ac89760SToby Isaac 
67510ac89760SToby Isaac   PetscFunctionBegin;
67520ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67530ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
67540ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
67550ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
67560ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
6757302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
6758a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
675966ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
67606995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
67616995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
67620ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
67630ac89760SToby Isaac   i[0] = 0;
67640ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
67650ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
6766f19733c5SToby Isaac     PetscInt rDof, rOff, r;
6767f19733c5SToby Isaac 
6768f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
6769f19733c5SToby Isaac     if (!rDof) continue;
6770f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
67710ac89760SToby Isaac     if (numFields) {
67720ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
67730ac89760SToby Isaac         annz = 0;
6774f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
6775f19733c5SToby Isaac           a = anchors[rOff + r];
67760ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
67770ac89760SToby Isaac           annz += aDof;
67780ac89760SToby Isaac         }
67790ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
67800ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
67810ac89760SToby Isaac         for (q = 0; q < dof; q++) {
67820ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
67830ac89760SToby Isaac         }
67840ac89760SToby Isaac       }
67850ac89760SToby Isaac     }
67860ac89760SToby Isaac     else {
67870ac89760SToby Isaac       annz = 0;
67880ac89760SToby Isaac       for (q = 0; q < dof; q++) {
67890ac89760SToby Isaac         a = anchors[off + q];
67900ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
67910ac89760SToby Isaac         annz += aDof;
67920ac89760SToby Isaac       }
67930ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
67940ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
67950ac89760SToby Isaac       for (q = 0; q < dof; q++) {
67960ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
67970ac89760SToby Isaac       }
67980ac89760SToby Isaac     }
67990ac89760SToby Isaac   }
68000ac89760SToby Isaac   nnz = i[m];
68010ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
68020ac89760SToby Isaac   offset = 0;
68030ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
68040ac89760SToby Isaac     if (numFields) {
68050ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
68060ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
68070ac89760SToby Isaac         for (q = 0; q < dof; q++) {
68080ac89760SToby Isaac           PetscInt rDof, rOff, r;
68090ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
68100ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
68110ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
68120ac89760SToby Isaac             PetscInt s;
68130ac89760SToby Isaac 
68140ac89760SToby Isaac             a = anchors[rOff + r];
68150ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
68160ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
68170ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
68180ac89760SToby Isaac               j[offset++] = aOff + s;
68190ac89760SToby Isaac             }
68200ac89760SToby Isaac           }
68210ac89760SToby Isaac         }
68220ac89760SToby Isaac       }
68230ac89760SToby Isaac     }
68240ac89760SToby Isaac     else {
68250ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
68260ac89760SToby Isaac       for (q = 0; q < dof; q++) {
68270ac89760SToby Isaac         PetscInt rDof, rOff, r;
68280ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
68290ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
68300ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
68310ac89760SToby Isaac           PetscInt s;
68320ac89760SToby Isaac 
68330ac89760SToby Isaac           a = anchors[rOff + r];
68340ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
68350ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
68360ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
68370ac89760SToby Isaac             j[offset++] = aOff + s;
68380ac89760SToby Isaac           }
68390ac89760SToby Isaac         }
68400ac89760SToby Isaac       }
68410ac89760SToby Isaac     }
68420ac89760SToby Isaac   }
68430ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
684425570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
684525570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
684666ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
68470ac89760SToby Isaac   PetscFunctionReturn(0);
68480ac89760SToby Isaac }
68490ac89760SToby Isaac 
685066ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
685166ad2231SToby Isaac {
6852f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
6853f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
685466ad2231SToby Isaac   Mat            cMat;
685566ad2231SToby Isaac   PetscErrorCode ierr;
685666ad2231SToby Isaac 
685766ad2231SToby Isaac   PetscFunctionBegin;
685866ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6859a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
686066ad2231SToby Isaac   if (anchorSection) {
6861e228b242SToby Isaac     PetscDS  ds;
6862e228b242SToby Isaac     PetscInt nf;
6863e228b242SToby Isaac 
6864f7c74593SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
6865f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
6866f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
6867e228b242SToby Isaac     ierr = DMGetDS(dm,&ds);CHKERRQ(ierr);
6868302440fdSBarry Smith     ierr = PetscDSGetNumFields(ds,&nf);CHKERRQ(ierr);
6869e228b242SToby Isaac     if (nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
687066ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
687166ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
687266ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
687366ad2231SToby Isaac   }
687466ad2231SToby Isaac   PetscFunctionReturn(0);
687566ad2231SToby Isaac }
6876