xref: /petsc/src/dm/impls/plex/plex.c (revision e83a0d2d6d8db0ab0fb54c27366b189d71509fa8)
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>
48135c375SStefano Zampini #include <petsc/private/glvisvecimpl.h>
50c312b8eSJed Brown #include <petscsf.h>
6e228b242SToby Isaac #include <petscds.h>
7e412dcbdSMatthew G. Knepley #include <petscdraw.h>
8f19dbd58SToby Isaac #include <petscdmfield.h>
9552f7358SJed Brown 
10552f7358SJed Brown /* Logging support */
1130b0ce1bSStefano Zampini PetscLogEvent DMPLEX_Interpolate, DMPLEX_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_Symmetrize, DMPLEX_Preallocate, DMPLEX_ResidualFEM, DMPLEX_JacobianFEM, DMPLEX_InterpolatorFEM, DMPLEX_InjectorFEM, DMPLEX_IntegralFEM, DMPLEX_CreateGmsh, DMPLEX_RebalanceSharedPoints, DMPLEX_PartSelf, DMPLEX_PartLabelInvert, DMPLEX_PartLabelCreateSF, DMPLEX_PartStratSF, DMPLEX_CreatePointSF;
12552f7358SJed Brown 
135a576424SJed Brown PETSC_EXTERN PetscErrorCode VecView_MPI(Vec, PetscViewer);
14552f7358SJed Brown 
15e5337592SStefano Zampini /*@
16e5337592SStefano Zampini   DMPlexRefineSimplexToTensor - Uniformly refines simplicial cells into tensor product cells.
17e5337592SStefano Zampini   3 quadrilaterals per triangle in 2D and 4 hexahedra per tetrahedron in 3D.
18e5337592SStefano Zampini 
19e5337592SStefano Zampini   Collective
20e5337592SStefano Zampini 
21e5337592SStefano Zampini   Input Parameters:
22e5337592SStefano Zampini . dm - The DMPlex object
23e5337592SStefano Zampini 
24e5337592SStefano Zampini   Output Parameters:
25e5337592SStefano Zampini . dmRefined - The refined DMPlex object
26e5337592SStefano Zampini 
27e5337592SStefano Zampini   Note: Returns NULL if the mesh is already a tensor product mesh.
28e5337592SStefano Zampini 
29e5337592SStefano Zampini   Level: intermediate
30e5337592SStefano Zampini 
31e5337592SStefano Zampini .seealso: DMPlexCreate(), DMPlexSetRefinementUniform()
32e5337592SStefano Zampini @*/
33e5337592SStefano Zampini PetscErrorCode DMPlexRefineSimplexToTensor(DM dm, DM *dmRefined)
34e5337592SStefano Zampini {
35e5337592SStefano Zampini   PetscInt         dim, cMax, fMax, cStart, cEnd, coneSize;
36e5337592SStefano Zampini   CellRefiner      cellRefiner;
37e5337592SStefano Zampini   PetscBool        lop, allnoop, localized;
38e5337592SStefano Zampini   PetscErrorCode   ierr;
39e5337592SStefano Zampini 
40e5337592SStefano Zampini   PetscFunctionBegin;
41e5337592SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
424330a3fcSStefano Zampini   PetscValidPointer(dmRefined, 1);
43e5337592SStefano Zampini   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
44e5337592SStefano Zampini   ierr = DMPlexGetHybridBounds(dm,&cMax,&fMax,NULL,NULL);CHKERRQ(ierr);
45e5337592SStefano Zampini   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
46e5337592SStefano Zampini   if (!(cEnd - cStart)) cellRefiner = REFINER_NOOP;
47e5337592SStefano Zampini   else {
48e5337592SStefano Zampini     ierr = DMPlexGetConeSize(dm,cStart,&coneSize);CHKERRQ(ierr);
49e5337592SStefano Zampini     switch (dim) {
50e5337592SStefano Zampini     case 1:
51e5337592SStefano Zampini       cellRefiner = REFINER_NOOP;
52e5337592SStefano Zampini     break;
53e5337592SStefano Zampini     case 2:
54e5337592SStefano Zampini       switch (coneSize) {
55e5337592SStefano Zampini       case 3:
564330a3fcSStefano Zampini         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_2D;
574330a3fcSStefano Zampini         else cellRefiner = REFINER_SIMPLEX_TO_HEX_2D;
58e5337592SStefano Zampini       break;
59e5337592SStefano Zampini       case 4:
604330a3fcSStefano Zampini         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_2D;
614330a3fcSStefano Zampini         else cellRefiner = REFINER_NOOP;
62e5337592SStefano Zampini       break;
63e5337592SStefano Zampini       default: SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle coneSize %D with dimension %D",coneSize,dim);
64e5337592SStefano Zampini       }
65e5337592SStefano Zampini     break;
66e5337592SStefano Zampini     case 3:
67e5337592SStefano Zampini       switch (coneSize) {
68e5337592SStefano Zampini       case 4:
694330a3fcSStefano Zampini         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_3D;
704330a3fcSStefano Zampini         else cellRefiner = REFINER_SIMPLEX_TO_HEX_3D;
714330a3fcSStefano Zampini       break;
724330a3fcSStefano Zampini       case 5:
734330a3fcSStefano Zampini         if (cMax >= 0) cellRefiner = REFINER_HYBRID_SIMPLEX_TO_HEX_3D;
744330a3fcSStefano Zampini         else cellRefiner = REFINER_NOOP;
75e5337592SStefano Zampini       break;
76e5337592SStefano Zampini       case 6:
774330a3fcSStefano Zampini         if (cMax >= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Simplex2Tensor in 3D with Hybrid mesh not yet done");
78e5337592SStefano Zampini         cellRefiner = REFINER_NOOP;
79e5337592SStefano Zampini       break;
80e5337592SStefano Zampini       default: SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle coneSize %D with dimension %D",coneSize,dim);
81e5337592SStefano Zampini       }
82e5337592SStefano Zampini     break;
83e5337592SStefano Zampini     default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot handle dimension %D",dim);
84e5337592SStefano Zampini     }
85e5337592SStefano Zampini   }
86e5337592SStefano Zampini   /* return if we don't need to refine */
87e5337592SStefano Zampini   lop = (cellRefiner == REFINER_NOOP) ? PETSC_TRUE : PETSC_FALSE;
88e5337592SStefano Zampini   ierr = MPIU_Allreduce(&lop,&allnoop,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
89e5337592SStefano Zampini   if (allnoop) {
90e5337592SStefano Zampini     *dmRefined = NULL;
91e5337592SStefano Zampini     PetscFunctionReturn(0);
92e5337592SStefano Zampini   }
93e5337592SStefano Zampini   ierr = DMPlexRefineUniform_Internal(dm, cellRefiner, dmRefined);CHKERRQ(ierr);
94e5337592SStefano Zampini   ierr = DMCopyBoundary(dm, *dmRefined);CHKERRQ(ierr);
95e5337592SStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
96e5337592SStefano Zampini   if (localized) {
97e5337592SStefano Zampini     ierr = DMLocalizeCoordinates(*dmRefined);CHKERRQ(ierr);
98e5337592SStefano Zampini   }
99e5337592SStefano Zampini   PetscFunctionReturn(0);
100e5337592SStefano Zampini }
101e5337592SStefano Zampini 
1027afe7537SMatthew G. Knepley PetscErrorCode DMPlexGetFieldType_Internal(DM dm, PetscSection section, PetscInt field, PetscInt *sStart, PetscInt *sEnd, PetscViewerVTKFieldType *ft)
1037e42fee7SMatthew G. Knepley {
104a99a26bcSAdrian Croucher   PetscInt       dim, pStart, pEnd, vStart, vEnd, cStart, cEnd, cEndInterior;
105a99a26bcSAdrian Croucher   PetscInt       vcdof[2] = {0,0}, globalvcdof[2];
1067e42fee7SMatthew G. Knepley   PetscErrorCode ierr;
1077e42fee7SMatthew G. Knepley 
1087e42fee7SMatthew G. Knepley   PetscFunctionBegin;
1097e42fee7SMatthew G. Knepley   *ft  = PETSC_VTK_POINT_FIELD;
110c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1117e42fee7SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1127e42fee7SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
11380d5bdc6SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
11480d5bdc6SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1157e42fee7SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1167e42fee7SMatthew G. Knepley   if (field >= 0) {
117a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, vStart, field, &vcdof[0]);CHKERRQ(ierr);}
118a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetFieldDof(section, cStart, field, &vcdof[1]);CHKERRQ(ierr);}
1197e42fee7SMatthew G. Knepley   } else {
120a99a26bcSAdrian Croucher     if ((vStart >= pStart) && (vStart < pEnd)) {ierr = PetscSectionGetDof(section, vStart, &vcdof[0]);CHKERRQ(ierr);}
121a99a26bcSAdrian Croucher     if ((cStart >= pStart) && (cStart < pEnd)) {ierr = PetscSectionGetDof(section, cStart, &vcdof[1]);CHKERRQ(ierr);}
1227e42fee7SMatthew G. Knepley   }
1235483465cSMatthew G. Knepley   ierr = MPI_Allreduce(vcdof, globalvcdof, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
124a99a26bcSAdrian Croucher   if (globalvcdof[0]) {
1257e42fee7SMatthew G. Knepley     *sStart = vStart;
1267e42fee7SMatthew G. Knepley     *sEnd   = vEnd;
127a99a26bcSAdrian Croucher     if (globalvcdof[0] == dim) *ft = PETSC_VTK_POINT_VECTOR_FIELD;
1287e42fee7SMatthew G. Knepley     else                       *ft = PETSC_VTK_POINT_FIELD;
129a99a26bcSAdrian Croucher   } else if (globalvcdof[1]) {
1307e42fee7SMatthew G. Knepley     *sStart = cStart;
1317e42fee7SMatthew G. Knepley     *sEnd   = cEnd;
132a99a26bcSAdrian Croucher     if (globalvcdof[1] == dim) *ft = PETSC_VTK_CELL_VECTOR_FIELD;
1337e42fee7SMatthew G. Knepley     else                       *ft = PETSC_VTK_CELL_FIELD;
1347e42fee7SMatthew G. Knepley   } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Could not classify input Vec for VTK");
1357e42fee7SMatthew G. Knepley   PetscFunctionReturn(0);
1367e42fee7SMatthew G. Knepley }
1377e42fee7SMatthew G. Knepley 
1387cd05799SMatthew G. Knepley static PetscErrorCode VecView_Plex_Local_Draw(Vec v, PetscViewer viewer)
139e412dcbdSMatthew G. Knepley {
140e412dcbdSMatthew G. Knepley   DM                 dm;
141d1df6f1dSMatthew G. Knepley   PetscSection       s;
142e412dcbdSMatthew G. Knepley   PetscDraw          draw, popup;
143e412dcbdSMatthew G. Knepley   DM                 cdm;
144e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
145e412dcbdSMatthew G. Knepley   Vec                coordinates;
146e412dcbdSMatthew G. Knepley   const PetscScalar *coords, *array;
147e412dcbdSMatthew G. Knepley   PetscReal          bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
148339e3443SMatthew G. Knepley   PetscReal          vbound[2], time;
149339e3443SMatthew G. Knepley   PetscBool          isnull, flg;
150d1df6f1dSMatthew G. Knepley   PetscInt           dim, Nf, f, Nc, comp, vStart, vEnd, cStart, cEnd, c, N, level, step, w = 0;
151e412dcbdSMatthew G. Knepley   const char        *name;
152339e3443SMatthew G. Knepley   char               title[PETSC_MAX_PATH_LEN];
153e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
154e412dcbdSMatthew G. Knepley 
155e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
156d1df6f1dSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
157d1df6f1dSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
158d1df6f1dSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
159d1df6f1dSMatthew G. Knepley 
160e412dcbdSMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
161e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1628135c375SStefano Zampini   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D. Use PETSCVIEWERGLVIS", dim);
16392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
164d1df6f1dSMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
165e412dcbdSMatthew G. Knepley   ierr = DMGetCoarsenLevel(dm, &level);CHKERRQ(ierr);
166e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
16792fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
168e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
169e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
170e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
171e412dcbdSMatthew G. Knepley 
172e412dcbdSMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
173339e3443SMatthew G. Knepley   ierr = DMGetOutputSequenceNumber(dm, &step, &time);CHKERRQ(ierr);
174e412dcbdSMatthew G. Knepley 
175e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
176e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
177e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
1780c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
1790c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
180e412dcbdSMatthew G. Knepley   }
181e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
182e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
183e412dcbdSMatthew G. Knepley 
184d1df6f1dSMatthew G. Knepley   /* Could implement something like DMDASelectFields() */
185d1df6f1dSMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
186d1df6f1dSMatthew G. Knepley     DM   fdm = dm;
187d1df6f1dSMatthew G. Knepley     Vec  fv  = v;
188d1df6f1dSMatthew G. Knepley     IS   fis;
189d1df6f1dSMatthew G. Knepley     char prefix[PETSC_MAX_PATH_LEN];
190d1df6f1dSMatthew G. Knepley     const char *fname;
191d1df6f1dSMatthew G. Knepley 
192d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
193d1df6f1dSMatthew G. Knepley     ierr = PetscSectionGetFieldName(s, f, &fname);CHKERRQ(ierr);
194d1df6f1dSMatthew G. Knepley 
195a126751eSBarry Smith     if (v->hdr.prefix) {ierr = PetscStrncpy(prefix, v->hdr.prefix,sizeof(prefix));CHKERRQ(ierr);}
196d1df6f1dSMatthew G. Knepley     else               {prefix[0] = '\0';}
197d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
198d1df6f1dSMatthew G. Knepley       ierr = DMCreateSubDM(dm, 1, &f, &fis, &fdm);CHKERRQ(ierr);
199d1df6f1dSMatthew G. Knepley       ierr = VecGetSubVector(v, fis, &fv);CHKERRQ(ierr);
200a126751eSBarry Smith       ierr = PetscStrlcat(prefix, fname,sizeof(prefix));CHKERRQ(ierr);
201a126751eSBarry Smith       ierr = PetscStrlcat(prefix, "_",sizeof(prefix));CHKERRQ(ierr);
202d1df6f1dSMatthew G. Knepley     }
203d1df6f1dSMatthew G. Knepley     for (comp = 0; comp < Nc; ++comp, ++w) {
204d1df6f1dSMatthew G. Knepley       PetscInt nmax = 2;
205d1df6f1dSMatthew G. Knepley 
206d1df6f1dSMatthew G. Knepley       ierr = PetscViewerDrawGetDraw(viewer, w, &draw);CHKERRQ(ierr);
207d1df6f1dSMatthew G. Knepley       if (Nc > 1) {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s_%D Step: %D Time: %.4g", name, fname, comp, step, time);CHKERRQ(ierr);}
208d1df6f1dSMatthew G. Knepley       else        {ierr = PetscSNPrintf(title, sizeof(title), "%s:%s Step: %D Time: %.4g", name, fname, step, time);CHKERRQ(ierr);}
209d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetTitle(draw, title);CHKERRQ(ierr);
210d1df6f1dSMatthew G. Knepley 
211d1df6f1dSMatthew G. Knepley       /* TODO Get max and min only for this component */
212d1df6f1dSMatthew G. Knepley       ierr = PetscOptionsGetRealArray(NULL, prefix, "-vec_view_bounds", vbound, &nmax, &flg);CHKERRQ(ierr);
213339e3443SMatthew G. Knepley       if (!flg) {
214d1df6f1dSMatthew G. Knepley         ierr = VecMin(fv, NULL, &vbound[0]);CHKERRQ(ierr);
215d1df6f1dSMatthew G. Knepley         ierr = VecMax(fv, NULL, &vbound[1]);CHKERRQ(ierr);
216d1df6f1dSMatthew G. Knepley         if (vbound[1] <= vbound[0]) vbound[1] = vbound[0] + 1.0;
217339e3443SMatthew G. Knepley       }
218e412dcbdSMatthew G. Knepley       ierr = PetscDrawGetPopup(draw, &popup);CHKERRQ(ierr);
219339e3443SMatthew G. Knepley       ierr = PetscDrawScalePopup(popup, vbound[0], vbound[1]);CHKERRQ(ierr);
220d1df6f1dSMatthew G. Knepley       ierr = PetscDrawSetCoordinates(draw, bound[0], bound[1], bound[2], bound[3]);CHKERRQ(ierr);
221e412dcbdSMatthew G. Knepley 
222d1df6f1dSMatthew G. Knepley       ierr = VecGetArrayRead(fv, &array);CHKERRQ(ierr);
223e412dcbdSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
22499a2f7bcSMatthew G. Knepley         PetscScalar *coords = NULL, *a = NULL;
225e56f9228SJed Brown         PetscInt     numCoords, color[4] = {-1,-1,-1,-1};
226e412dcbdSMatthew G. Knepley 
227d1df6f1dSMatthew G. Knepley         ierr = DMPlexPointLocalRead(fdm, c, array, &a);CHKERRQ(ierr);
228339e3443SMatthew G. Knepley         if (a) {
229d1df6f1dSMatthew G. Knepley           color[0] = PetscDrawRealToColor(PetscRealPart(a[comp]), vbound[0], vbound[1]);
230339e3443SMatthew G. Knepley           color[1] = color[2] = color[3] = color[0];
231339e3443SMatthew G. Knepley         } else {
232339e3443SMatthew G. Knepley           PetscScalar *vals = NULL;
233339e3443SMatthew G. Knepley           PetscInt     numVals, va;
234339e3443SMatthew G. Knepley 
235d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecGetClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
236d1df6f1dSMatthew 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);
237d1df6f1dSMatthew G. Knepley           switch (numVals/Nc) {
238d1df6f1dSMatthew G. Knepley           case 3: /* P1 Triangle */
239d1df6f1dSMatthew G. Knepley           case 4: /* P1 Quadrangle */
240d1df6f1dSMatthew G. Knepley             for (va = 0; va < numVals/Nc; ++va) color[va] = PetscDrawRealToColor(PetscRealPart(vals[va*Nc+comp]), vbound[0], vbound[1]);
241339e3443SMatthew G. Knepley             break;
242d1df6f1dSMatthew G. Knepley           case 6: /* P2 Triangle */
243d1df6f1dSMatthew G. Knepley           case 8: /* P2 Quadrangle */
244d1df6f1dSMatthew 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]);
245d1df6f1dSMatthew G. Knepley             break;
246d1df6f1dSMatthew G. Knepley           default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of values for cell closure %D cannot be handled", numVals/Nc);
247339e3443SMatthew G. Knepley           }
248d1df6f1dSMatthew G. Knepley           ierr = DMPlexVecRestoreClosure(fdm, NULL, fv, c, &numVals, &vals);CHKERRQ(ierr);
249339e3443SMatthew G. Knepley         }
250e412dcbdSMatthew G. Knepley         ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
251e412dcbdSMatthew G. Knepley         switch (numCoords) {
252e412dcbdSMatthew G. Knepley         case 6:
253339e3443SMatthew 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);
254e412dcbdSMatthew G. Knepley           break;
255e412dcbdSMatthew G. Knepley         case 8:
256339e3443SMatthew 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);
257339e3443SMatthew 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);
258e412dcbdSMatthew G. Knepley           break;
259e412dcbdSMatthew G. Knepley         default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D coordinates", numCoords);
260e412dcbdSMatthew G. Knepley         }
261e412dcbdSMatthew G. Knepley         ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
262e412dcbdSMatthew G. Knepley       }
263d1df6f1dSMatthew G. Knepley       ierr = VecRestoreArrayRead(fv, &array);CHKERRQ(ierr);
264e412dcbdSMatthew G. Knepley       ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
265e412dcbdSMatthew G. Knepley       ierr = PetscDrawPause(draw);CHKERRQ(ierr);
266e412dcbdSMatthew G. Knepley       ierr = PetscDrawSave(draw);CHKERRQ(ierr);
267d1df6f1dSMatthew G. Knepley     }
268d1df6f1dSMatthew G. Knepley     if (Nf > 1) {
269d1df6f1dSMatthew G. Knepley       ierr = VecRestoreSubVector(v, fis, &fv);CHKERRQ(ierr);
270d1df6f1dSMatthew G. Knepley       ierr = ISDestroy(&fis);CHKERRQ(ierr);
271d1df6f1dSMatthew G. Knepley       ierr = DMDestroy(&fdm);CHKERRQ(ierr);
272d1df6f1dSMatthew G. Knepley     }
273d1df6f1dSMatthew G. Knepley   }
274e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
275e412dcbdSMatthew G. Knepley }
276e412dcbdSMatthew G. Knepley 
277684b87d9SLisandro Dalcin static PetscErrorCode VecView_Plex_Local_VTK(Vec v, PetscViewer viewer)
278684b87d9SLisandro Dalcin {
279684b87d9SLisandro Dalcin   DM                      dm;
280684b87d9SLisandro Dalcin   Vec                     locv;
281684b87d9SLisandro Dalcin   const char              *name;
282684b87d9SLisandro Dalcin   PetscSection            section;
283684b87d9SLisandro Dalcin   PetscInt                pStart, pEnd;
284684b87d9SLisandro Dalcin   PetscViewerVTKFieldType ft;
285684b87d9SLisandro Dalcin   PetscErrorCode          ierr;
286684b87d9SLisandro Dalcin 
287684b87d9SLisandro Dalcin   PetscFunctionBegin;
288684b87d9SLisandro Dalcin   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
289684b87d9SLisandro Dalcin   ierr = DMCreateLocalVector(dm, &locv);CHKERRQ(ierr); /* VTK viewer requires exclusive ownership of the vector */
290684b87d9SLisandro Dalcin   ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
291684b87d9SLisandro Dalcin   ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
292684b87d9SLisandro Dalcin   ierr = VecCopy(v, locv);CHKERRQ(ierr);
29392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
294684b87d9SLisandro Dalcin   ierr = DMPlexGetFieldType_Internal(dm, section, PETSC_DETERMINE, &pStart, &pEnd, &ft);CHKERRQ(ierr);
295a8f87f1dSPatrick Sanan   ierr = PetscViewerVTKAddField(viewer, (PetscObject) dm, DMPlexVTKWriteAll, ft, PETSC_TRUE,(PetscObject) locv);CHKERRQ(ierr);
296684b87d9SLisandro Dalcin   PetscFunctionReturn(0);
297684b87d9SLisandro Dalcin }
298684b87d9SLisandro Dalcin 
299552f7358SJed Brown PetscErrorCode VecView_Plex_Local(Vec v, PetscViewer viewer)
300552f7358SJed Brown {
301552f7358SJed Brown   DM             dm;
302684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
303552f7358SJed Brown   PetscErrorCode ierr;
304552f7358SJed Brown 
305552f7358SJed Brown   PetscFunctionBegin;
306552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
30782f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
308552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
309b136c2c9SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
310f13a32a3SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3118135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
312684b87d9SLisandro Dalcin   if (isvtk || ishdf5 || isdraw || isglvis) {
313684b87d9SLisandro Dalcin     PetscInt    i,numFields;
314684b87d9SLisandro Dalcin     PetscObject fe;
315ef31f671SMatthew G. Knepley     PetscBool   fem = PETSC_FALSE;
316684b87d9SLisandro Dalcin     Vec         locv = v;
317684b87d9SLisandro Dalcin     const char  *name;
318684b87d9SLisandro Dalcin     PetscInt    step;
319684b87d9SLisandro Dalcin     PetscReal   time;
320ef31f671SMatthew G. Knepley 
321ef31f671SMatthew G. Knepley     ierr = DMGetNumFields(dm, &numFields);CHKERRQ(ierr);
322684b87d9SLisandro Dalcin     for (i=0; i<numFields; i++) {
32344a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, i, NULL, &fe);CHKERRQ(ierr);
324684b87d9SLisandro Dalcin       if (fe->classid == PETSCFE_CLASSID) { fem = PETSC_TRUE; break; }
325ef31f671SMatthew G. Knepley     }
326684b87d9SLisandro Dalcin     if (fem) {
327684b87d9SLisandro Dalcin       ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
328684b87d9SLisandro Dalcin       ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
329684b87d9SLisandro Dalcin       ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
330684b87d9SLisandro Dalcin       ierr = VecCopy(v, locv);CHKERRQ(ierr);
331684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, NULL, &time);CHKERRQ(ierr);
332684b87d9SLisandro Dalcin       ierr = DMPlexInsertBoundaryValues(dm, PETSC_TRUE, locv, time, NULL, NULL, NULL);CHKERRQ(ierr);
333ef31f671SMatthew G. Knepley     }
334552f7358SJed Brown     if (isvtk) {
335684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_VTK(locv, viewer);CHKERRQ(ierr);
336b136c2c9SMatthew G. Knepley     } else if (ishdf5) {
337b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
338684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_HDF5_Internal(locv, viewer);CHKERRQ(ierr);
339b136c2c9SMatthew G. Knepley #else
340b136c2c9SMatthew G. Knepley       SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
341b136c2c9SMatthew G. Knepley #endif
342f13a32a3SMatthew G. Knepley     } else if (isdraw) {
343684b87d9SLisandro Dalcin       ierr = VecView_Plex_Local_Draw(locv, viewer);CHKERRQ(ierr);
344684b87d9SLisandro Dalcin     } else if (isglvis) {
345684b87d9SLisandro Dalcin       ierr = DMGetOutputSequenceNumber(dm, &step, NULL);CHKERRQ(ierr);
346684b87d9SLisandro Dalcin       ierr = PetscViewerGLVisSetSnapId(viewer, step);CHKERRQ(ierr);
347684b87d9SLisandro Dalcin       ierr = VecView_GLVis(locv, viewer);CHKERRQ(ierr);
348684b87d9SLisandro Dalcin     }
349684b87d9SLisandro Dalcin     if (fem) {ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);}
350552f7358SJed Brown   } else {
351684b87d9SLisandro Dalcin     PetscBool isseq;
352684b87d9SLisandro Dalcin 
353684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
35455f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
35555f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
356552f7358SJed Brown   }
357552f7358SJed Brown   PetscFunctionReturn(0);
358552f7358SJed Brown }
359552f7358SJed Brown 
360552f7358SJed Brown PetscErrorCode VecView_Plex(Vec v, PetscViewer viewer)
361552f7358SJed Brown {
362552f7358SJed Brown   DM             dm;
363684b87d9SLisandro Dalcin   PetscBool      isvtk, ishdf5, isdraw, isglvis;
364552f7358SJed Brown   PetscErrorCode ierr;
365552f7358SJed Brown 
366552f7358SJed Brown   PetscFunctionBegin;
367552f7358SJed Brown   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
36882f516ccSBarry Smith   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
369552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
37033c3e6b4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
371e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
3728135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
373684b87d9SLisandro Dalcin   if (isvtk || isdraw || isglvis) {
374552f7358SJed Brown     Vec         locv;
375552f7358SJed Brown     const char *name;
376552f7358SJed Brown 
377c8dd51e9SBarry Smith     ierr = DMGetLocalVector(dm, &locv);CHKERRQ(ierr);
378552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
379552f7358SJed Brown     ierr = PetscObjectSetName((PetscObject) locv, name);CHKERRQ(ierr);
380552f7358SJed Brown     ierr = DMGlobalToLocalBegin(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
381552f7358SJed Brown     ierr = DMGlobalToLocalEnd(dm, v, INSERT_VALUES, locv);CHKERRQ(ierr);
382552f7358SJed Brown     ierr = VecView_Plex_Local(locv, viewer);CHKERRQ(ierr);
383c8dd51e9SBarry Smith     ierr = DMRestoreLocalVector(dm, &locv);CHKERRQ(ierr);
384b136c2c9SMatthew G. Knepley   } else if (ishdf5) {
385b136c2c9SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
38639d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
387b136c2c9SMatthew G. Knepley #else
388b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
389b136c2c9SMatthew G. Knepley #endif
390552f7358SJed Brown   } else {
391684b87d9SLisandro Dalcin     PetscBool isseq;
392684b87d9SLisandro Dalcin 
393684b87d9SLisandro Dalcin     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
39455f2e967SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
39555f2e967SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
396552f7358SJed Brown   }
397552f7358SJed Brown   PetscFunctionReturn(0);
398552f7358SJed Brown }
399552f7358SJed Brown 
400d930f514SMatthew G. Knepley PetscErrorCode VecView_Plex_Native(Vec originalv, PetscViewer viewer)
401d930f514SMatthew G. Knepley {
402d930f514SMatthew G. Knepley   DM                dm;
403d930f514SMatthew G. Knepley   MPI_Comm          comm;
404d930f514SMatthew G. Knepley   PetscViewerFormat format;
405d930f514SMatthew G. Knepley   Vec               v;
406d930f514SMatthew G. Knepley   PetscBool         isvtk, ishdf5;
407d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
408d930f514SMatthew G. Knepley 
409d930f514SMatthew G. Knepley   PetscFunctionBegin;
410d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
411d930f514SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) originalv, &comm);CHKERRQ(ierr);
4122c4c0c81SMatthew G. Knepley   if (!dm) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
413d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
414d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
415d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,  &isvtk);CHKERRQ(ierr);
416d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
417a8ad634aSStefano Zampini     /* Natural ordering is the common case for DMDA, NATIVE means plain vector, for PLEX is the opposite */
418a8ad634aSStefano Zampini     /* this need a better fix */
419a8ad634aSStefano Zampini     if (dm->useNatural) {
420a8ad634aSStefano Zampini       if (dm->sfNatural) {
421d930f514SMatthew G. Knepley         const char *vecname;
422d930f514SMatthew G. Knepley         PetscInt    n, nroots;
423d930f514SMatthew G. Knepley 
424ca43db0aSBarry Smith         ierr = VecGetLocalSize(originalv, &n);CHKERRQ(ierr);
425d930f514SMatthew G. Knepley         ierr = PetscSFGetGraph(dm->sfNatural, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
426d930f514SMatthew G. Knepley         if (n == nroots) {
427d930f514SMatthew G. Knepley           ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
428d930f514SMatthew G. Knepley           ierr = DMPlexGlobalToNaturalBegin(dm, originalv, v);CHKERRQ(ierr);
429d930f514SMatthew G. Knepley           ierr = DMPlexGlobalToNaturalEnd(dm, originalv, v);CHKERRQ(ierr);
430d930f514SMatthew G. Knepley           ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
431d930f514SMatthew G. Knepley           ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
432d930f514SMatthew G. Knepley         } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "DM global to natural SF only handles global vectors");
433d930f514SMatthew G. Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONGSTATE, "DM global to natural SF was not created");
434a8ad634aSStefano Zampini     } else v = originalv;
435a8ad634aSStefano Zampini   } else v = originalv;
436a8ad634aSStefano Zampini 
437d930f514SMatthew G. Knepley   if (ishdf5) {
438d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
43939d25373SMatthew G. Knepley     ierr = VecView_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
440d930f514SMatthew G. Knepley #else
441d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
442d930f514SMatthew G. Knepley #endif
443d930f514SMatthew G. Knepley   } else if (isvtk) {
444d930f514SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_SUP, "VTK format does not support viewing in natural order. Please switch to HDF5.");
445d930f514SMatthew G. Knepley   } else {
446d930f514SMatthew G. Knepley     PetscBool isseq;
447d930f514SMatthew G. Knepley 
448d930f514SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) v, VECSEQ, &isseq);CHKERRQ(ierr);
449d930f514SMatthew G. Knepley     if (isseq) {ierr = VecView_Seq(v, viewer);CHKERRQ(ierr);}
450d930f514SMatthew G. Knepley     else       {ierr = VecView_MPI(v, viewer);CHKERRQ(ierr);}
451d930f514SMatthew G. Knepley   }
452a8ad634aSStefano Zampini   if (v != originalv) {ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);}
453d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
454d930f514SMatthew G. Knepley }
455d930f514SMatthew G. Knepley 
4562c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Local(Vec v, PetscViewer viewer)
4572c40f234SMatthew G. Knepley {
4582c40f234SMatthew G. Knepley   DM             dm;
4592c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4602c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4612c40f234SMatthew G. Knepley 
4622c40f234SMatthew G. Knepley   PetscFunctionBegin;
4632c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4642c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4652c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4662c40f234SMatthew G. Knepley   if (ishdf5) {
4672c40f234SMatthew G. Knepley     DM          dmBC;
4682c40f234SMatthew G. Knepley     Vec         gv;
4692c40f234SMatthew G. Knepley     const char *name;
4702c40f234SMatthew G. Knepley 
4712c40f234SMatthew G. Knepley     ierr = DMGetOutputDM(dm, &dmBC);CHKERRQ(ierr);
4722c40f234SMatthew G. Knepley     ierr = DMGetGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4732c40f234SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) v, &name);CHKERRQ(ierr);
4742c40f234SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) gv, name);CHKERRQ(ierr);
4752c40f234SMatthew G. Knepley     ierr = VecLoad_Default(gv, viewer);CHKERRQ(ierr);
4762c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalBegin(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4772c40f234SMatthew G. Knepley     ierr = DMGlobalToLocalEnd(dmBC, gv, INSERT_VALUES, v);CHKERRQ(ierr);
4782c40f234SMatthew G. Knepley     ierr = DMRestoreGlobalVector(dmBC, &gv);CHKERRQ(ierr);
4792c40f234SMatthew G. Knepley   } else {
4802c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
4812c40f234SMatthew G. Knepley   }
4822c40f234SMatthew G. Knepley   PetscFunctionReturn(0);
4832c40f234SMatthew G. Knepley }
4842c40f234SMatthew G. Knepley 
4852c40f234SMatthew G. Knepley PetscErrorCode VecLoad_Plex(Vec v, PetscViewer viewer)
4862c40f234SMatthew G. Knepley {
4872c40f234SMatthew G. Knepley   DM             dm;
4882c40f234SMatthew G. Knepley   PetscBool      ishdf5;
4892c40f234SMatthew G. Knepley   PetscErrorCode ierr;
4902c40f234SMatthew G. Knepley 
4912c40f234SMatthew G. Knepley   PetscFunctionBegin;
4922c40f234SMatthew G. Knepley   ierr = VecGetDM(v, &dm);CHKERRQ(ierr);
4932c40f234SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject)v), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
4942c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4952c40f234SMatthew G. Knepley   if (ishdf5) {
496878b459fSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
49739d25373SMatthew G. Knepley     ierr = VecLoad_Plex_HDF5_Internal(v, viewer);CHKERRQ(ierr);
498b136c2c9SMatthew G. Knepley #else
499b136c2c9SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
500878b459fSMatthew G. Knepley #endif
5012c40f234SMatthew G. Knepley   } else {
5022c40f234SMatthew G. Knepley     ierr = VecLoad_Default(v, viewer);CHKERRQ(ierr);
503552f7358SJed Brown   }
504552f7358SJed Brown   PetscFunctionReturn(0);
505552f7358SJed Brown }
506552f7358SJed Brown 
507d930f514SMatthew G. Knepley PetscErrorCode VecLoad_Plex_Native(Vec originalv, PetscViewer viewer)
508d930f514SMatthew G. Knepley {
509d930f514SMatthew G. Knepley   DM                dm;
510d930f514SMatthew G. Knepley   PetscViewerFormat format;
511d930f514SMatthew G. Knepley   PetscBool         ishdf5;
512d930f514SMatthew G. Knepley   PetscErrorCode    ierr;
513d930f514SMatthew G. Knepley 
514d930f514SMatthew G. Knepley   PetscFunctionBegin;
515d930f514SMatthew G. Knepley   ierr = VecGetDM(originalv, &dm);CHKERRQ(ierr);
516d930f514SMatthew G. Knepley   if (!dm) SETERRQ(PetscObjectComm((PetscObject) originalv), PETSC_ERR_ARG_WRONG, "Vector not generated from a DM");
517d930f514SMatthew G. Knepley   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
518d930f514SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
519d930f514SMatthew G. Knepley   if (format == PETSC_VIEWER_NATIVE) {
520a8ad634aSStefano Zampini     if (dm->useNatural) {
521d930f514SMatthew G. Knepley       if (dm->sfNatural) {
522d930f514SMatthew G. Knepley         if (ishdf5) {
523d930f514SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
524d930f514SMatthew G. Knepley           Vec         v;
525d930f514SMatthew G. Knepley           const char *vecname;
526d930f514SMatthew G. Knepley 
527d930f514SMatthew G. Knepley           ierr = DMGetGlobalVector(dm, &v);CHKERRQ(ierr);
528d930f514SMatthew G. Knepley           ierr = PetscObjectGetName((PetscObject) originalv, &vecname);CHKERRQ(ierr);
529d930f514SMatthew G. Knepley           ierr = PetscObjectSetName((PetscObject) v, vecname);CHKERRQ(ierr);
53039d25373SMatthew G. Knepley           ierr = VecLoad_Plex_HDF5_Native_Internal(v, viewer);CHKERRQ(ierr);
531d930f514SMatthew G. Knepley           ierr = DMPlexNaturalToGlobalBegin(dm, v, originalv);CHKERRQ(ierr);
532d930f514SMatthew G. Knepley           ierr = DMPlexNaturalToGlobalEnd(dm, v, originalv);CHKERRQ(ierr);
533d930f514SMatthew G. Knepley           ierr = DMRestoreGlobalVector(dm, &v);CHKERRQ(ierr);
534d930f514SMatthew G. Knepley #else
535d930f514SMatthew G. Knepley           SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
536d930f514SMatthew G. Knepley #endif
537d930f514SMatthew G. Knepley         } else SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Reading in natural order is not supported for anything but HDF5.");
538d930f514SMatthew G. Knepley       }
539a8ad634aSStefano Zampini     } else {
540a8ad634aSStefano Zampini       ierr = VecLoad_Default(originalv, viewer);CHKERRQ(ierr);
541a8ad634aSStefano Zampini     }
542d930f514SMatthew G. Knepley   }
543d930f514SMatthew G. Knepley   PetscFunctionReturn(0);
544d930f514SMatthew G. Knepley }
545d930f514SMatthew G. Knepley 
5467cd05799SMatthew G. Knepley PETSC_UNUSED static PetscErrorCode DMPlexView_Ascii_Geometry(DM dm, PetscViewer viewer)
547731e8ddeSMatthew G. Knepley {
548731e8ddeSMatthew G. Knepley   PetscSection       coordSection;
549731e8ddeSMatthew G. Knepley   Vec                coordinates;
550731e8ddeSMatthew G. Knepley   DMLabel            depthLabel;
551731e8ddeSMatthew G. Knepley   const char        *name[4];
552731e8ddeSMatthew G. Knepley   const PetscScalar *a;
553731e8ddeSMatthew G. Knepley   PetscInt           dim, pStart, pEnd, cStart, cEnd, c;
554731e8ddeSMatthew G. Knepley   PetscErrorCode     ierr;
555731e8ddeSMatthew G. Knepley 
556731e8ddeSMatthew G. Knepley   PetscFunctionBegin;
557731e8ddeSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
558731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
559731e8ddeSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
560731e8ddeSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
561731e8ddeSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
562731e8ddeSMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &pStart, &pEnd);CHKERRQ(ierr);
563731e8ddeSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &a);CHKERRQ(ierr);
564731e8ddeSMatthew G. Knepley   name[0]     = "vertex";
565731e8ddeSMatthew G. Knepley   name[1]     = "edge";
566731e8ddeSMatthew G. Knepley   name[dim-1] = "face";
567731e8ddeSMatthew G. Knepley   name[dim]   = "cell";
568731e8ddeSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
569731e8ddeSMatthew G. Knepley     PetscInt *closure = NULL;
570731e8ddeSMatthew G. Knepley     PetscInt  closureSize, cl;
571731e8ddeSMatthew G. Knepley 
572f347f43bSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "Geometry for cell %D:\n", c);CHKERRQ(ierr);
573731e8ddeSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
574731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
575731e8ddeSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
576731e8ddeSMatthew G. Knepley       PetscInt point = closure[cl], depth, dof, off, d, p;
577731e8ddeSMatthew G. Knepley 
578731e8ddeSMatthew G. Knepley       if ((point < pStart) || (point >= pEnd)) continue;
579731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
580731e8ddeSMatthew G. Knepley       if (!dof) continue;
581731e8ddeSMatthew G. Knepley       ierr = DMLabelGetValue(depthLabel, point, &depth);CHKERRQ(ierr);
582731e8ddeSMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
583f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "%s %D coords:", name[depth], point);CHKERRQ(ierr);
584731e8ddeSMatthew G. Knepley       for (p = 0; p < dof/dim; ++p) {
585731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " (");CHKERRQ(ierr);
586731e8ddeSMatthew G. Knepley         for (d = 0; d < dim; ++d) {
587731e8ddeSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
58841477eefSMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", PetscRealPart(a[off+p*dim+d]));CHKERRQ(ierr);
589731e8ddeSMatthew G. Knepley         }
590731e8ddeSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, ")");CHKERRQ(ierr);
591731e8ddeSMatthew G. Knepley       }
592731e8ddeSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
593731e8ddeSMatthew G. Knepley     }
594731e8ddeSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
595731e8ddeSMatthew G. Knepley     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
596731e8ddeSMatthew G. Knepley   }
597731e8ddeSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &a);CHKERRQ(ierr);
598731e8ddeSMatthew G. Knepley   PetscFunctionReturn(0);
599731e8ddeSMatthew G. Knepley }
600731e8ddeSMatthew G. Knepley 
6017cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Ascii(DM dm, PetscViewer viewer)
602552f7358SJed Brown {
603552f7358SJed Brown   DM_Plex          *mesh = (DM_Plex*) dm->data;
604552f7358SJed Brown   DM                cdm;
605552f7358SJed Brown   DMLabel           markers;
606552f7358SJed Brown   PetscSection      coordSection;
607552f7358SJed Brown   Vec               coordinates;
608552f7358SJed Brown   PetscViewerFormat format;
609552f7358SJed Brown   PetscErrorCode    ierr;
610552f7358SJed Brown 
611552f7358SJed Brown   PetscFunctionBegin;
612552f7358SJed Brown   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
61392fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
614552f7358SJed Brown   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
615552f7358SJed Brown   ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
616552f7358SJed Brown   if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
617552f7358SJed Brown     const char *name;
618f73eea6eSMatthew G. Knepley     PetscInt    dim, cellHeight, maxConeSize, maxSupportSize;
619552f7358SJed Brown     PetscInt    pStart, pEnd, p;
620552f7358SJed Brown     PetscMPIInt rank, size;
621552f7358SJed Brown 
62282f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
62382f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
624552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
625552f7358SJed Brown     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
626552f7358SJed Brown     ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
627f73eea6eSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
628f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
629f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
630f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
631f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
632e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Supports:\n", name);CHKERRQ(ierr);
6334d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
634e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max support size: %D\n", rank, maxSupportSize);CHKERRQ(ierr);
635552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
636552f7358SJed Brown       PetscInt dof, off, s;
637552f7358SJed Brown 
638552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
639552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
640552f7358SJed Brown       for (s = off; s < off+dof; ++s) {
641e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D ----> %D\n", rank, p, mesh->supports[s]);CHKERRQ(ierr);
642552f7358SJed Brown       }
643552f7358SJed Brown     }
644552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
645e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "Cones:\n", name);CHKERRQ(ierr);
646e9cb465cSMatthew G. Knepley     ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Max cone size: %D\n", rank, maxConeSize);CHKERRQ(ierr);
647552f7358SJed Brown     for (p = pStart; p < pEnd; ++p) {
648552f7358SJed Brown       PetscInt dof, off, c;
649552f7358SJed Brown 
650552f7358SJed Brown       ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
651552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
652552f7358SJed Brown       for (c = off; c < off+dof; ++c) {
653e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "[%d]: %D <---- %D (%D)\n", rank, p, mesh->cones[c], mesh->coneOrientations[c]);CHKERRQ(ierr);
654552f7358SJed Brown       }
655552f7358SJed Brown     }
656552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
6574d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
6583d2e540fSStefano Zampini     if (coordSection && coordinates) {
65980180ce3SStefano Zampini       ierr = PetscSectionVecView(coordSection, coordinates, viewer);CHKERRQ(ierr);
6603d2e540fSStefano Zampini     }
661c58f1c22SToby Isaac     ierr = DMGetLabel(dm, "marker", &markers);CHKERRQ(ierr);
6627422c0d1SMatthew G. Knepley     if (markers) {ierr = DMLabelView(markers,viewer);CHKERRQ(ierr);}
663552f7358SJed Brown     if (size > 1) {
664552f7358SJed Brown       PetscSF sf;
665552f7358SJed Brown 
666552f7358SJed Brown       ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
667552f7358SJed Brown       ierr = PetscSFView(sf, viewer);CHKERRQ(ierr);
668552f7358SJed Brown     }
669552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
670552f7358SJed Brown   } else if (format == PETSC_VIEWER_ASCII_LATEX) {
6710588280cSMatthew G. Knepley     const char  *name, *color;
6720588280cSMatthew G. Knepley     const char  *defcolors[3]  = {"gray", "orange", "green"};
6730588280cSMatthew G. Knepley     const char  *deflcolors[4] = {"blue", "cyan", "red", "magenta"};
674fe1cc32dSStefano Zampini     char         lname[PETSC_MAX_PATH_LEN];
675552f7358SJed Brown     PetscReal    scale         = 2.0;
67678081901SStefano Zampini     PetscReal    tikzscale     = 1.0;
6770588280cSMatthew G. Knepley     PetscBool    useNumbers    = PETSC_TRUE, useLabels, useColors;
6780588280cSMatthew G. Knepley     double       tcoords[3];
679552f7358SJed Brown     PetscScalar *coords;
6800588280cSMatthew G. Knepley     PetscInt     numLabels, l, numColors, numLColors, dim, depth, cStart, cEnd, c, vStart, vEnd, v, eStart = 0, eEnd = 0, e, p;
681552f7358SJed Brown     PetscMPIInt  rank, size;
6820588280cSMatthew G. Knepley     char         **names, **colors, **lcolors;
683fe1cc32dSStefano Zampini     PetscBool    plotEdges, flg, lflg;
684fe1cc32dSStefano Zampini     PetscBT      wp = NULL;
685fe1cc32dSStefano Zampini     PetscInt     pEnd, pStart;
686552f7358SJed Brown 
6870588280cSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
688552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
689c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
6900588280cSMatthew G. Knepley     numLabels  = PetscMax(numLabels, 10);
6910588280cSMatthew G. Knepley     numColors  = 10;
6920588280cSMatthew G. Knepley     numLColors = 10;
6930588280cSMatthew G. Knepley     ierr = PetscCalloc3(numLabels, &names, numColors, &colors, numLColors, &lcolors);CHKERRQ(ierr);
694c5929fdfSBarry Smith     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_scale", &scale, NULL);CHKERRQ(ierr);
69578081901SStefano Zampini     ierr = PetscOptionsGetReal(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_tikzscale", &tikzscale, NULL);CHKERRQ(ierr);
696c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_numbers", &useNumbers, NULL);CHKERRQ(ierr);
697c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_labels", names, &numLabels, &useLabels);CHKERRQ(ierr);
6980588280cSMatthew G. Knepley     if (!useLabels) numLabels = 0;
699c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_colors", colors, &numColors, &useColors);CHKERRQ(ierr);
7000588280cSMatthew G. Knepley     if (!useColors) {
7010588280cSMatthew G. Knepley       numColors = 3;
7020588280cSMatthew G. Knepley       for (c = 0; c < numColors; ++c) {ierr = PetscStrallocpy(defcolors[c], &colors[c]);CHKERRQ(ierr);}
7030588280cSMatthew G. Knepley     }
704c5929fdfSBarry Smith     ierr = PetscOptionsGetStringArray(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_lcolors", lcolors, &numLColors, &useColors);CHKERRQ(ierr);
7050588280cSMatthew G. Knepley     if (!useColors) {
7060588280cSMatthew G. Knepley       numLColors = 4;
7070588280cSMatthew G. Knepley       for (c = 0; c < numLColors; ++c) {ierr = PetscStrallocpy(deflcolors[c], &lcolors[c]);CHKERRQ(ierr);}
7080588280cSMatthew G. Knepley     }
709fe1cc32dSStefano Zampini     ierr = PetscOptionsGetString(((PetscObject) viewer)->options, ((PetscObject) viewer)->prefix, "-dm_plex_view_label_filter", lname, PETSC_MAX_PATH_LEN, &lflg);CHKERRQ(ierr);
710202fd40aSStefano Zampini     plotEdges = (PetscBool)(depth > 1 && useNumbers && dim < 3);
711202fd40aSStefano Zampini     ierr = PetscOptionsGetBool(((PetscObject) viewer)->options,((PetscObject) viewer)->prefix, "-dm_plex_view_edges", &plotEdges, &flg);CHKERRQ(ierr);
712202fd40aSStefano Zampini     if (flg && plotEdges && depth < dim) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Mesh must be interpolated");
713202fd40aSStefano Zampini     if (depth < dim) plotEdges = PETSC_FALSE;
714fe1cc32dSStefano Zampini 
715fe1cc32dSStefano Zampini     /* filter points with labelvalue != labeldefaultvalue */
716fe1cc32dSStefano Zampini     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
717fe1cc32dSStefano Zampini     if (lflg) {
718fe1cc32dSStefano Zampini       DMLabel lbl;
719fe1cc32dSStefano Zampini 
720fe1cc32dSStefano Zampini       ierr = DMGetLabel(dm, lname, &lbl);CHKERRQ(ierr);
721fe1cc32dSStefano Zampini       if (lbl) {
722fe1cc32dSStefano Zampini         PetscInt val, defval;
723fe1cc32dSStefano Zampini 
724fe1cc32dSStefano Zampini         ierr = DMLabelGetDefaultValue(lbl, &defval);CHKERRQ(ierr);
725fe1cc32dSStefano Zampini         ierr = PetscBTCreate(pEnd-pStart, &wp);CHKERRQ(ierr);
726fe1cc32dSStefano Zampini         for (c = pStart;  c < pEnd; c++) {
727fe1cc32dSStefano Zampini           PetscInt *closure = NULL;
728fe1cc32dSStefano Zampini           PetscInt  closureSize;
729fe1cc32dSStefano Zampini 
730fe1cc32dSStefano Zampini           ierr = DMLabelGetValue(lbl, c, &val);CHKERRQ(ierr);
731fe1cc32dSStefano Zampini           if (val == defval) continue;
732fe1cc32dSStefano Zampini 
733fe1cc32dSStefano Zampini           ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
734fe1cc32dSStefano Zampini           for (p = 0; p < closureSize*2; p += 2) {
735fe1cc32dSStefano Zampini             ierr = PetscBTSet(wp, closure[p] - pStart);CHKERRQ(ierr);
736fe1cc32dSStefano Zampini           }
737fe1cc32dSStefano Zampini           ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
738fe1cc32dSStefano Zampini         }
739fe1cc32dSStefano Zampini       }
740fe1cc32dSStefano Zampini     }
741fe1cc32dSStefano Zampini 
74282f516ccSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
74382f516ccSBarry Smith     ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size);CHKERRQ(ierr);
744552f7358SJed Brown     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
745770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\
7460588280cSMatthew G. Knepley \\documentclass[tikz]{standalone}\n\n\
747552f7358SJed Brown \\usepackage{pgflibraryshapes}\n\
748552f7358SJed Brown \\usetikzlibrary{backgrounds}\n\
749552f7358SJed Brown \\usetikzlibrary{arrows}\n\
7500588280cSMatthew G. Knepley \\begin{document}\n");CHKERRQ(ierr);
7510588280cSMatthew G. Knepley     if (size > 1) {
752f738dd31SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "%s for process ", name);CHKERRQ(ierr);
753770b213bSMatthew G Knepley       for (p = 0; p < size; ++p) {
754770b213bSMatthew G Knepley         if (p > 0 && p == size-1) {
755770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", and ", colors[p%numColors], p);CHKERRQ(ierr);
756770b213bSMatthew G Knepley         } else if (p > 0) {
757770b213bSMatthew G Knepley           ierr = PetscViewerASCIIPrintf(viewer, ", ", colors[p%numColors], p);CHKERRQ(ierr);
758770b213bSMatthew G Knepley         }
759770b213bSMatthew G Knepley         ierr = PetscViewerASCIIPrintf(viewer, "{\\textcolor{%s}%D}", colors[p%numColors], p);CHKERRQ(ierr);
760770b213bSMatthew G Knepley       }
7610588280cSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, ".\n\n\n");CHKERRQ(ierr);
7620588280cSMatthew G. Knepley     }
76378081901SStefano Zampini     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", tikzscale);CHKERRQ(ierr);
764fe1cc32dSStefano Zampini 
765552f7358SJed Brown     /* Plot vertices */
766552f7358SJed Brown     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
767552f7358SJed Brown     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
7684d4c343aSBarry Smith     ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
769552f7358SJed Brown     for (v = vStart; v < vEnd; ++v) {
770552f7358SJed Brown       PetscInt  off, dof, d;
7710588280cSMatthew G. Knepley       PetscBool isLabeled = PETSC_FALSE;
772552f7358SJed Brown 
773fe1cc32dSStefano Zampini       if (wp && !PetscBTLookup(wp,v - pStart)) continue;
774552f7358SJed Brown       ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
775552f7358SJed Brown       ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
7760588280cSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
777f6dae198SJed Brown       if (PetscUnlikely(dof > 3)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"coordSection vertex %D has dof %D > 3",v,dof);
7780588280cSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
7790588280cSMatthew G. Knepley         tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
780c068d9bbSLisandro Dalcin         tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
7810588280cSMatthew G. Knepley       }
7820588280cSMatthew G. Knepley       /* Rotate coordinates since PGF makes z point out of the page instead of up */
7830588280cSMatthew G. Knepley       if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
784552f7358SJed Brown       for (d = 0; d < dof; ++d) {
785552f7358SJed Brown         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
7860588280cSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", tcoords[d]);CHKERRQ(ierr);
787552f7358SJed Brown       }
7880588280cSMatthew G. Knepley       color = colors[rank%numColors];
7890588280cSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
7900588280cSMatthew G. Knepley         PetscInt val;
791c58f1c22SToby Isaac         ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
7920588280cSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
7930588280cSMatthew G. Knepley       }
7940588280cSMatthew G. Knepley       if (useNumbers) {
795e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", v, rank, color, v);CHKERRQ(ierr);
7960588280cSMatthew G. Knepley       } else {
797e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", v, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
7980588280cSMatthew G. Knepley       }
799552f7358SJed Brown     }
800552f7358SJed Brown     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
801552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
802846a3e8bSMatthew G. Knepley     /* Plot cells */
803846a3e8bSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
80478081901SStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd);CHKERRQ(ierr);
805846a3e8bSMatthew G. Knepley     if (dim == 3 || !useNumbers) {
806846a3e8bSMatthew G. Knepley       for (e = eStart; e < eEnd; ++e) {
807846a3e8bSMatthew G. Knepley         const PetscInt *cone;
808846a3e8bSMatthew G. Knepley 
809fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
810846a3e8bSMatthew G. Knepley         color = colors[rank%numColors];
811846a3e8bSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
812846a3e8bSMatthew G. Knepley           PetscInt val;
813846a3e8bSMatthew G. Knepley           ierr = DMGetLabelValue(dm, names[l], e, &val);CHKERRQ(ierr);
814846a3e8bSMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
815846a3e8bSMatthew G. Knepley         }
816846a3e8bSMatthew G. Knepley         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
817846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] (%D_%d) -- (%D_%d);\n", color, cone[0], rank, cone[1], rank);CHKERRQ(ierr);
818846a3e8bSMatthew G. Knepley       }
819846a3e8bSMatthew G. Knepley     } else {
820846a3e8bSMatthew G. Knepley       for (c = cStart; c < cEnd; ++c) {
821846a3e8bSMatthew G. Knepley         PetscInt *closure = NULL;
822846a3e8bSMatthew G. Knepley         PetscInt  closureSize, firstPoint = -1;
823846a3e8bSMatthew G. Knepley 
824fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,c - pStart)) continue;
825846a3e8bSMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
826846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\draw[color=%s] ", colors[rank%numColors]);CHKERRQ(ierr);
827846a3e8bSMatthew G. Knepley         for (p = 0; p < closureSize*2; p += 2) {
828846a3e8bSMatthew G. Knepley           const PetscInt point = closure[p];
829846a3e8bSMatthew G. Knepley 
830846a3e8bSMatthew G. Knepley           if ((point < vStart) || (point >= vEnd)) continue;
831846a3e8bSMatthew G. Knepley           if (firstPoint >= 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- ");CHKERRQ(ierr);}
832846a3e8bSMatthew G. Knepley           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(%D_%d)", point, rank);CHKERRQ(ierr);
833846a3e8bSMatthew G. Knepley           if (firstPoint < 0) firstPoint = point;
834846a3e8bSMatthew G. Knepley         }
835846a3e8bSMatthew G. Knepley         /* Why doesn't this work? ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- cycle;\n");CHKERRQ(ierr); */
836846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, " -- (%D_%d);\n", firstPoint, rank);CHKERRQ(ierr);
837846a3e8bSMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
838846a3e8bSMatthew G. Knepley       }
839846a3e8bSMatthew G. Knepley     }
840846a3e8bSMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
841846a3e8bSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
842846a3e8bSMatthew G. Knepley       double    ccoords[3] = {0.0, 0.0, 0.0};
843846a3e8bSMatthew G. Knepley       PetscBool isLabeled  = PETSC_FALSE;
844846a3e8bSMatthew G. Knepley       PetscInt *closure    = NULL;
845846a3e8bSMatthew G. Knepley       PetscInt  closureSize, dof, d, n = 0;
846846a3e8bSMatthew G. Knepley 
847fe1cc32dSStefano Zampini       if (wp && !PetscBTLookup(wp,c - pStart)) continue;
848846a3e8bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
849846a3e8bSMatthew G. Knepley       ierr = PetscViewerASCIISynchronizedPrintf(viewer, "\\path (");CHKERRQ(ierr);
850846a3e8bSMatthew G. Knepley       for (p = 0; p < closureSize*2; p += 2) {
851846a3e8bSMatthew G. Knepley         const PetscInt point = closure[p];
852846a3e8bSMatthew G. Knepley         PetscInt       off;
853846a3e8bSMatthew G. Knepley 
854846a3e8bSMatthew G. Knepley         if ((point < vStart) || (point >= vEnd)) continue;
855846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetDof(coordSection, point, &dof);CHKERRQ(ierr);
856846a3e8bSMatthew G. Knepley         ierr = PetscSectionGetOffset(coordSection, point, &off);CHKERRQ(ierr);
857846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
858846a3e8bSMatthew G. Knepley           tcoords[d] = (double) (scale*PetscRealPart(coords[off+d]));
859846a3e8bSMatthew G. Knepley           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
860846a3e8bSMatthew G. Knepley         }
861846a3e8bSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
862846a3e8bSMatthew G. Knepley         if (dof == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
863846a3e8bSMatthew G. Knepley         for (d = 0; d < dof; ++d) {ccoords[d] += tcoords[d];}
864846a3e8bSMatthew G. Knepley         ++n;
865846a3e8bSMatthew G. Knepley       }
866846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {ccoords[d] /= n;}
867846a3e8bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
868846a3e8bSMatthew G. Knepley       for (d = 0; d < dof; ++d) {
869846a3e8bSMatthew G. Knepley         if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
870846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", ccoords[d]);CHKERRQ(ierr);
871846a3e8bSMatthew G. Knepley       }
872846a3e8bSMatthew G. Knepley       color = colors[rank%numColors];
873846a3e8bSMatthew G. Knepley       for (l = 0; l < numLabels; ++l) {
874846a3e8bSMatthew G. Knepley         PetscInt val;
875846a3e8bSMatthew G. Knepley         ierr = DMGetLabelValue(dm, names[l], c, &val);CHKERRQ(ierr);
876846a3e8bSMatthew G. Knepley         if (val >= 0) {color = lcolors[l%numLColors]; isLabeled = PETSC_TRUE; break;}
877846a3e8bSMatthew G. Knepley       }
878846a3e8bSMatthew G. Knepley       if (useNumbers) {
879846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D};\n", c, rank, color, c);CHKERRQ(ierr);
880846a3e8bSMatthew G. Knepley       } else {
881846a3e8bSMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [fill,inner sep=%dpt,shape=circle,color=%s] {};\n", c, rank, !isLabeled ? 1 : 2, color);CHKERRQ(ierr);
882846a3e8bSMatthew G. Knepley       }
883846a3e8bSMatthew G. Knepley     }
884846a3e8bSMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
885552f7358SJed Brown     /* Plot edges */
886202fd40aSStefano Zampini     if (plotEdges) {
887552f7358SJed Brown       ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
888552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\\path\n");CHKERRQ(ierr);
889552f7358SJed Brown       for (e = eStart; e < eEnd; ++e) {
890552f7358SJed Brown         const PetscInt *cone;
891552f7358SJed Brown         PetscInt        coneSize, offA, offB, dof, d;
892552f7358SJed Brown 
893fe1cc32dSStefano Zampini         if (wp && !PetscBTLookup(wp,e - pStart)) continue;
894552f7358SJed Brown         ierr = DMPlexGetConeSize(dm, e, &coneSize);CHKERRQ(ierr);
895f347f43bSBarry Smith         if (coneSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Edge %D cone should have two vertices, not %D", e, coneSize);
896552f7358SJed Brown         ierr = DMPlexGetCone(dm, e, &cone);CHKERRQ(ierr);
897552f7358SJed Brown         ierr = PetscSectionGetDof(coordSection, cone[0], &dof);CHKERRQ(ierr);
898552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[0], &offA);CHKERRQ(ierr);
899552f7358SJed Brown         ierr = PetscSectionGetOffset(coordSection, cone[1], &offB);CHKERRQ(ierr);
900552f7358SJed Brown         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "(");CHKERRQ(ierr);
901552f7358SJed Brown         for (d = 0; d < dof; ++d) {
902202fd40aSStefano Zampini           tcoords[d] = (double) (0.5*scale*PetscRealPart(coords[offA+d]+coords[offB+d]));
903c068d9bbSLisandro Dalcin           tcoords[d] = PetscAbs(tcoords[d]) < 1e-10 ? 0.0 : tcoords[d];
904552f7358SJed Brown         }
9050588280cSMatthew G. Knepley         /* Rotate coordinates since PGF makes z point out of the page instead of up */
9060588280cSMatthew G. Knepley         if (dim == 3) {PetscReal tmp = tcoords[1]; tcoords[1] = tcoords[2]; tcoords[2] = -tmp;}
9070588280cSMatthew G. Knepley         for (d = 0; d < dof; ++d) {
9080588280cSMatthew G. Knepley           if (d > 0) {ierr = PetscViewerASCIISynchronizedPrintf(viewer, ",");CHKERRQ(ierr);}
909f347f43bSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double)tcoords[d]);CHKERRQ(ierr);
9100588280cSMatthew G. Knepley         }
9110588280cSMatthew G. Knepley         color = colors[rank%numColors];
9120588280cSMatthew G. Knepley         for (l = 0; l < numLabels; ++l) {
9130588280cSMatthew G. Knepley           PetscInt val;
914c58f1c22SToby Isaac           ierr = DMGetLabelValue(dm, names[l], v, &val);CHKERRQ(ierr);
9150750fff4SMatthew G. Knepley           if (val >= 0) {color = lcolors[l%numLColors]; break;}
9160588280cSMatthew G. Knepley         }
917e4b003c7SBarry Smith         ierr = PetscViewerASCIISynchronizedPrintf(viewer, ") node(%D_%d) [draw,shape=circle,color=%s] {%D} --\n", e, rank, color, e);CHKERRQ(ierr);
918552f7358SJed Brown       }
919552f7358SJed Brown       ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
920552f7358SJed Brown       ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
921552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "(0,0);\n");CHKERRQ(ierr);
9220588280cSMatthew G. Knepley     }
923552f7358SJed Brown     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
9244d4c343aSBarry Smith     ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
9250588280cSMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{tikzpicture}\n");CHKERRQ(ierr);
926770b213bSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\end{document}\n", name);CHKERRQ(ierr);
9270588280cSMatthew G. Knepley     for (l = 0; l < numLabels;  ++l) {ierr = PetscFree(names[l]);CHKERRQ(ierr);}
9280588280cSMatthew G. Knepley     for (c = 0; c < numColors;  ++c) {ierr = PetscFree(colors[c]);CHKERRQ(ierr);}
9290588280cSMatthew G. Knepley     for (c = 0; c < numLColors; ++c) {ierr = PetscFree(lcolors[c]);CHKERRQ(ierr);}
9300588280cSMatthew G. Knepley     ierr = PetscFree3(names, colors, lcolors);CHKERRQ(ierr);
931fe1cc32dSStefano Zampini     ierr = PetscBTDestroy(&wp);CHKERRQ(ierr);
9320f7d6e4aSStefano Zampini   } else if (format == PETSC_VIEWER_LOAD_BALANCE) {
9330f7d6e4aSStefano Zampini     Vec                    cown,acown;
9340f7d6e4aSStefano Zampini     VecScatter             sct;
9350f7d6e4aSStefano Zampini     ISLocalToGlobalMapping g2l;
9360f7d6e4aSStefano Zampini     IS                     gid,acis;
9370f7d6e4aSStefano Zampini     MPI_Comm               comm,ncomm = MPI_COMM_NULL;
9380f7d6e4aSStefano Zampini     MPI_Group              ggroup,ngroup;
9390f7d6e4aSStefano Zampini     PetscScalar            *array,nid;
9400f7d6e4aSStefano Zampini     const PetscInt         *idxs;
9410f7d6e4aSStefano Zampini     PetscInt               *idxs2,*start,*adjacency,*work;
9420f7d6e4aSStefano Zampini     PetscInt64             lm[3],gm[3];
9430f7d6e4aSStefano Zampini     PetscInt               i,c,cStart,cEnd,cum,numVertices,ect,ectn,cellHeight;
9440f7d6e4aSStefano Zampini     PetscMPIInt            d1,d2,rank;
9450f7d6e4aSStefano Zampini 
9460f7d6e4aSStefano Zampini     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
9470f7d6e4aSStefano Zampini     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
9480f7d6e4aSStefano Zampini #if defined(PETSC_HAVE_MPI_SHARED_COMM)
9490f7d6e4aSStefano Zampini     ierr = MPI_Comm_split_type(comm,MPI_COMM_TYPE_SHARED,rank,MPI_INFO_NULL,&ncomm);CHKERRQ(ierr);
9500f7d6e4aSStefano Zampini #endif
9510f7d6e4aSStefano Zampini     if (ncomm != MPI_COMM_NULL) {
9520f7d6e4aSStefano Zampini       ierr = MPI_Comm_group(comm,&ggroup);CHKERRQ(ierr);
9530f7d6e4aSStefano Zampini       ierr = MPI_Comm_group(ncomm,&ngroup);CHKERRQ(ierr);
9540f7d6e4aSStefano Zampini       d1   = 0;
9550f7d6e4aSStefano Zampini       ierr = MPI_Group_translate_ranks(ngroup,1,&d1,ggroup,&d2);CHKERRQ(ierr);
9560f7d6e4aSStefano Zampini       nid  = d2;
9570f7d6e4aSStefano Zampini       ierr = MPI_Group_free(&ggroup);CHKERRQ(ierr);
9580f7d6e4aSStefano Zampini       ierr = MPI_Group_free(&ngroup);CHKERRQ(ierr);
9590f7d6e4aSStefano Zampini       ierr = MPI_Comm_free(&ncomm);CHKERRQ(ierr);
9600f7d6e4aSStefano Zampini     } else nid = 0.0;
9610f7d6e4aSStefano Zampini 
9620f7d6e4aSStefano Zampini     /* Get connectivity */
9630f7d6e4aSStefano Zampini     ierr = DMPlexGetVTKCellHeight(dm,&cellHeight);CHKERRQ(ierr);
9640f7d6e4aSStefano Zampini     ierr = DMPlexCreatePartitionerGraph(dm,cellHeight,&numVertices,&start,&adjacency,&gid);CHKERRQ(ierr);
9650f7d6e4aSStefano Zampini 
9660f7d6e4aSStefano Zampini     /* filter overlapped local cells */
9670f7d6e4aSStefano Zampini     ierr = DMPlexGetHeightStratum(dm,cellHeight,&cStart,&cEnd);CHKERRQ(ierr);
9680f7d6e4aSStefano Zampini     ierr = ISGetIndices(gid,&idxs);CHKERRQ(ierr);
9690f7d6e4aSStefano Zampini     ierr = ISGetLocalSize(gid,&cum);CHKERRQ(ierr);
9700f7d6e4aSStefano Zampini     ierr = PetscMalloc1(cum,&idxs2);CHKERRQ(ierr);
9710f7d6e4aSStefano Zampini     for (c = cStart, cum = 0; c < cEnd; c++) {
9720f7d6e4aSStefano Zampini       if (idxs[c-cStart] < 0) continue;
9730f7d6e4aSStefano Zampini       idxs2[cum++] = idxs[c-cStart];
9740f7d6e4aSStefano Zampini     }
9750f7d6e4aSStefano Zampini     ierr = ISRestoreIndices(gid,&idxs);CHKERRQ(ierr);
9760f7d6e4aSStefano Zampini     if (numVertices != cum) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unexpected %D != %D",numVertices,cum);
9770f7d6e4aSStefano Zampini     ierr = ISDestroy(&gid);CHKERRQ(ierr);
9780f7d6e4aSStefano Zampini     ierr = ISCreateGeneral(comm,numVertices,idxs2,PETSC_OWN_POINTER,&gid);CHKERRQ(ierr);
9790f7d6e4aSStefano Zampini 
9800f7d6e4aSStefano Zampini     /* support for node-aware cell locality */
9810f7d6e4aSStefano Zampini     ierr = ISCreateGeneral(comm,start[numVertices],adjacency,PETSC_USE_POINTER,&acis);CHKERRQ(ierr);
9820f7d6e4aSStefano Zampini     ierr = VecCreateSeq(PETSC_COMM_SELF,start[numVertices],&acown);CHKERRQ(ierr);
9830f7d6e4aSStefano Zampini     ierr = VecCreateMPI(comm,numVertices,PETSC_DECIDE,&cown);CHKERRQ(ierr);
9840f7d6e4aSStefano Zampini     ierr = VecGetArray(cown,&array);CHKERRQ(ierr);
9850f7d6e4aSStefano Zampini     for (c = 0; c < numVertices; c++) array[c] = nid;
9860f7d6e4aSStefano Zampini     ierr = VecRestoreArray(cown,&array);CHKERRQ(ierr);
9870f7d6e4aSStefano Zampini     ierr = VecScatterCreate(cown,acis,acown,NULL,&sct);CHKERRQ(ierr);
9880f7d6e4aSStefano Zampini     ierr = VecScatterBegin(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
9890f7d6e4aSStefano Zampini     ierr = VecScatterEnd(sct,cown,acown,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
9900f7d6e4aSStefano Zampini     ierr = ISDestroy(&acis);CHKERRQ(ierr);
9910f7d6e4aSStefano Zampini     ierr = VecScatterDestroy(&sct);CHKERRQ(ierr);
9920f7d6e4aSStefano Zampini     ierr = VecDestroy(&cown);CHKERRQ(ierr);
9930f7d6e4aSStefano Zampini 
9940f7d6e4aSStefano Zampini     /* compute edgeCut */
9950f7d6e4aSStefano Zampini     for (c = 0, cum = 0; c < numVertices; c++) cum = PetscMax(cum,start[c+1]-start[c]);
9960f7d6e4aSStefano Zampini     ierr = PetscMalloc1(cum,&work);CHKERRQ(ierr);
9970f7d6e4aSStefano Zampini     ierr = ISLocalToGlobalMappingCreateIS(gid,&g2l);CHKERRQ(ierr);
9980f7d6e4aSStefano Zampini     ierr = ISLocalToGlobalMappingSetType(g2l,ISLOCALTOGLOBALMAPPINGHASH);CHKERRQ(ierr);
9990f7d6e4aSStefano Zampini     ierr = ISDestroy(&gid);CHKERRQ(ierr);
10000f7d6e4aSStefano Zampini     ierr = VecGetArray(acown,&array);CHKERRQ(ierr);
10010f7d6e4aSStefano Zampini     for (c = 0, ect = 0, ectn = 0; c < numVertices; c++) {
10020f7d6e4aSStefano Zampini       PetscInt totl;
10030f7d6e4aSStefano Zampini 
10040f7d6e4aSStefano Zampini       totl = start[c+1]-start[c];
10050f7d6e4aSStefano Zampini       ierr = ISGlobalToLocalMappingApply(g2l,IS_GTOLM_MASK,totl,adjacency+start[c],NULL,work);CHKERRQ(ierr);
10060f7d6e4aSStefano Zampini       for (i = 0; i < totl; i++) {
10070f7d6e4aSStefano Zampini         if (work[i] < 0) {
10080f7d6e4aSStefano Zampini           ect  += 1;
10090f7d6e4aSStefano Zampini           ectn += (array[i + start[c]] != nid) ? 0 : 1;
10100f7d6e4aSStefano Zampini         }
10110f7d6e4aSStefano Zampini       }
10120f7d6e4aSStefano Zampini     }
10130f7d6e4aSStefano Zampini     ierr  = PetscFree(work);CHKERRQ(ierr);
10140f7d6e4aSStefano Zampini     ierr  = VecRestoreArray(acown,&array);CHKERRQ(ierr);
10150f7d6e4aSStefano Zampini     lm[0] = numVertices > 0 ?  numVertices : PETSC_MAX_INT;
10160f7d6e4aSStefano Zampini     lm[1] = -numVertices;
10170f7d6e4aSStefano Zampini     ierr  = MPIU_Allreduce(lm,gm,2,MPIU_INT64,MPI_MIN,comm);CHKERRQ(ierr);
10180f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,"  Cell balance: %.2f (max %D, min %D",-((double)gm[1])/((double)gm[0]),-(PetscInt)gm[1],(PetscInt)gm[0]);CHKERRQ(ierr);
10190f7d6e4aSStefano Zampini     lm[0] = ect; /* edgeCut */
10200f7d6e4aSStefano Zampini     lm[1] = ectn; /* node-aware edgeCut */
10210f7d6e4aSStefano Zampini     lm[2] = numVertices > 0 ? 0 : 1; /* empty processes */
10220f7d6e4aSStefano Zampini     ierr  = MPIU_Allreduce(lm,gm,3,MPIU_INT64,MPI_SUM,comm);CHKERRQ(ierr);
10230f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,", empty %D)\n",(PetscInt)gm[2]);CHKERRQ(ierr);
10240f7d6e4aSStefano Zampini #if defined(PETSC_HAVE_MPI_SHARED_COMM)
10250f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,"  Edge Cut: %D (on node %.3f)\n",(PetscInt)(gm[0]/2),gm[0] ? ((double)(gm[1]))/((double)gm[0]) : 1.);CHKERRQ(ierr);
10260f7d6e4aSStefano Zampini #else
10270f7d6e4aSStefano Zampini     ierr  = PetscViewerASCIIPrintf(viewer,"  Edge Cut: %D (on node %.3f)\n",(PetscInt)(gm[0]/2),0.0);CHKERRQ(ierr);
10280f7d6e4aSStefano Zampini #endif
10290f7d6e4aSStefano Zampini     ierr  = ISLocalToGlobalMappingDestroy(&g2l);CHKERRQ(ierr);
10300f7d6e4aSStefano Zampini     ierr  = PetscFree(start);CHKERRQ(ierr);
10310f7d6e4aSStefano Zampini     ierr  = PetscFree(adjacency);CHKERRQ(ierr);
10320f7d6e4aSStefano Zampini     ierr  = VecDestroy(&acown);CHKERRQ(ierr);
1033552f7358SJed Brown   } else {
103482f516ccSBarry Smith     MPI_Comm    comm;
1035834065abSMatthew G. Knepley     PetscInt   *sizes, *hybsizes;
1036f73eea6eSMatthew G. Knepley     PetscInt    locDepth, depth, cellHeight, dim, d, pMax[4];
1037552f7358SJed Brown     PetscInt    pStart, pEnd, p;
1038a57dd577SMatthew G Knepley     PetscInt    numLabels, l;
10391143a3c0SMatthew G. Knepley     const char *name;
1040552f7358SJed Brown     PetscMPIInt size;
1041552f7358SJed Brown 
104282f516ccSBarry Smith     ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
1043552f7358SJed Brown     ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
1044c73cfb54SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1045f73eea6eSMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
10465f64d76eSMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) dm, &name);CHKERRQ(ierr);
1047f73eea6eSMatthew G. Knepley     if (name) {ierr = PetscViewerASCIIPrintf(viewer, "%s in %D dimension%s:\n", name, dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
1048f73eea6eSMatthew G. Knepley     else      {ierr = PetscViewerASCIIPrintf(viewer, "Mesh in %D dimension%s:\n", dim, dim == 1 ? "" : "s");CHKERRQ(ierr);}
1049f73eea6eSMatthew G. Knepley     if (cellHeight) {ierr = PetscViewerASCIIPrintf(viewer, "  Cells are at height %D\n", cellHeight);CHKERRQ(ierr);}
1050552f7358SJed Brown     ierr = DMPlexGetDepth(dm, &locDepth);CHKERRQ(ierr);
1051b2566f29SBarry Smith     ierr = MPIU_Allreduce(&locDepth, &depth, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
1052a04427b4SStefano Zampini     ierr = DMPlexGetHybridBounds(dm, &pMax[depth], depth > 0 ? &pMax[depth-1] : NULL, depth > 1 ? &pMax[depth - 2] : NULL, &pMax[0]);CHKERRQ(ierr);
1053b0138d5dSStefano Zampini     ierr = PetscCalloc2(size,&sizes,size,&hybsizes);CHKERRQ(ierr);
1054552f7358SJed Brown     if (depth == 1) {
1055552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
1056552f7358SJed Brown       pEnd = pEnd - pStart;
1057a04427b4SStefano Zampini       pMax[0] -= pStart;
1058552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1059a04427b4SStefano Zampini       ierr = MPI_Gather(&pMax[0], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1060f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);CHKERRQ(ierr);
1061a04427b4SStefano Zampini       for (p = 0; p < size; ++p) {
1062a04427b4SStefano Zampini         if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
1063a04427b4SStefano Zampini         else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
1064a04427b4SStefano Zampini       }
1065552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
1066552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
1067552f7358SJed Brown       pEnd = pEnd - pStart;
1068a04427b4SStefano Zampini       pMax[depth] -= pStart;
1069552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1070a04427b4SStefano Zampini       ierr = MPI_Gather(&pMax[depth], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1071552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
1072a04427b4SStefano Zampini       for (p = 0; p < size; ++p) {
1073a04427b4SStefano Zampini         if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
1074a04427b4SStefano Zampini         else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
1075a04427b4SStefano Zampini       }
1076552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
1077552f7358SJed Brown     } else {
1078cbb7f117SMark Adams       PetscMPIInt rank;
1079cbb7f117SMark Adams       ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1080552f7358SJed Brown       for (d = 0; d <= dim; d++) {
1081552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
1082834065abSMatthew G. Knepley         pEnd    -= pStart;
1083834065abSMatthew G. Knepley         pMax[d] -= pStart;
1084552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1085834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1086552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
1087834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
1088cbb7f117SMark Adams           if (!rank) {
1089834065abSMatthew G. Knepley             if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
1090834065abSMatthew G. Knepley             else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
1091834065abSMatthew G. Knepley           }
1092cbb7f117SMark Adams         }
1093552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
1094552f7358SJed Brown       }
1095552f7358SJed Brown     }
1096834065abSMatthew G. Knepley     ierr = PetscFree2(sizes,hybsizes);CHKERRQ(ierr);
1097c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
1098a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
1099a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
1100a57dd577SMatthew G Knepley       DMLabel         label;
1101a57dd577SMatthew G Knepley       const char     *name;
1102a57dd577SMatthew G Knepley       IS              valueIS;
1103a57dd577SMatthew G Knepley       const PetscInt *values;
1104a57dd577SMatthew G Knepley       PetscInt        numValues, v;
1105a57dd577SMatthew G Knepley 
1106c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
1107c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1108a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
1109d72f73ffSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);CHKERRQ(ierr);
1110a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
1111a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
1112120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
1113a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
1114a57dd577SMatthew G Knepley         PetscInt size;
1115a57dd577SMatthew G Knepley 
1116a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
1117a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
1118d72f73ffSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);CHKERRQ(ierr);
1119a57dd577SMatthew G Knepley       }
1120a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
1121120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
1122a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
11234d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
1124a57dd577SMatthew G Knepley     }
112534aa8a36SMatthew G. Knepley     /* If no fields are specified, people do not want to see adjacency */
112634aa8a36SMatthew G. Knepley     if (dm->Nf) {
112734aa8a36SMatthew G. Knepley       PetscInt f;
112834aa8a36SMatthew G. Knepley 
112934aa8a36SMatthew G. Knepley       for (f = 0; f < dm->Nf; ++f) {
113034aa8a36SMatthew G. Knepley         const char *name;
113134aa8a36SMatthew G. Knepley 
113234aa8a36SMatthew G. Knepley         ierr = PetscObjectGetName(dm->fields[f].disc, &name);CHKERRQ(ierr);
113334aa8a36SMatthew G. Knepley         if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Field %s:\n", name);CHKERRQ(ierr);}
113434aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
113534aa8a36SMatthew G. Knepley         if (dm->fields[f].label) {ierr = DMLabelView(dm->fields[f].label, viewer);CHKERRQ(ierr);}
113634aa8a36SMatthew G. Knepley         if (dm->fields[f].adjacency[0]) {
113734aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM++\n");CHKERRQ(ierr);}
1138ed59e46eSMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM\n");CHKERRQ(ierr);}
113934aa8a36SMatthew G. Knepley         } else {
114034aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FEM\n");CHKERRQ(ierr);}
114134aa8a36SMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FUNKY\n");CHKERRQ(ierr);}
114234aa8a36SMatthew G. Knepley         }
114334aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
114434aa8a36SMatthew G. Knepley       }
114534aa8a36SMatthew G. Knepley     }
1146a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
11478e7ff633SMatthew G. Knepley     if (cdm) {
11488e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
11498e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
11508e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
11518e7ff633SMatthew G. Knepley     }
1152552f7358SJed Brown   }
1153552f7358SJed Brown   PetscFunctionReturn(0);
1154552f7358SJed Brown }
1155552f7358SJed Brown 
11567cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
1157e412dcbdSMatthew G. Knepley {
1158e412dcbdSMatthew G. Knepley   PetscDraw          draw;
1159e412dcbdSMatthew G. Knepley   DM                 cdm;
1160e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
1161e412dcbdSMatthew G. Knepley   Vec                coordinates;
1162e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
116329494db1SLisandro Dalcin   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
1164e412dcbdSMatthew G. Knepley   PetscBool          isnull;
1165e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
1166cf3064d3SMatthew G. Knepley   PetscMPIInt        rank;
1167e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
1168e412dcbdSMatthew G. Knepley 
1169e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
1170e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1171e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
1172e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
117392fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
1174e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1175e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1176e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1177e412dcbdSMatthew G. Knepley 
1178e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
1179e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
1180e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
1181e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
1182e412dcbdSMatthew G. Knepley 
1183e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
1184e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
1185e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
11860c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
11870c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
1188e412dcbdSMatthew G. Knepley   }
1189e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
119029494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
119129494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
119229494db1SLisandro Dalcin   ierr = PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);CHKERRQ(ierr);
1193e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
1194e412dcbdSMatthew G. Knepley 
1195cf3064d3SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
1196cf3064d3SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1197cf3064d3SMatthew G. Knepley     PetscScalar *coords = NULL;
1198cf3064d3SMatthew G. Knepley     PetscInt     numCoords,coneSize;
1199cf3064d3SMatthew G. Knepley 
1200cf3064d3SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
1201cf3064d3SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1202cf3064d3SMatthew G. Knepley     switch (coneSize) {
1203cf3064d3SMatthew G. Knepley     case 3:
1204cf3064d3SMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1205cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1206cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1207cf3064d3SMatthew G. Knepley                                PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1208cf3064d3SMatthew G. Knepley       break;
1209cf3064d3SMatthew G. Knepley     case 4:
1210cf3064d3SMatthew G. Knepley       ierr = PetscDrawRectangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1211cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1212cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1213cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1214cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1215cf3064d3SMatthew G. Knepley       break;
1216cf3064d3SMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D facets", coneSize);
1217cf3064d3SMatthew G. Knepley     }
1218cf3064d3SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1219cf3064d3SMatthew G. Knepley   }
1220e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1221e412dcbdSMatthew G. Knepley     PetscScalar *coords = NULL;
122229494db1SLisandro Dalcin     PetscInt     numCoords,coneSize;
1223e412dcbdSMatthew G. Knepley 
122429494db1SLisandro Dalcin     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
1225e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
122629494db1SLisandro Dalcin     switch (coneSize) {
122729494db1SLisandro Dalcin     case 3:
12280c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12290c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12300c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1231e412dcbdSMatthew G. Knepley       break;
123229494db1SLisandro Dalcin     case 4:
12330c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12340c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12350c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12360c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1237e412dcbdSMatthew G. Knepley       break;
123829494db1SLisandro Dalcin     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D facets", coneSize);
1239e412dcbdSMatthew G. Knepley     }
1240e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1241e412dcbdSMatthew G. Knepley   }
1242e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
1243e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
1244e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
1245e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
1246e412dcbdSMatthew G. Knepley }
1247e412dcbdSMatthew G. Knepley 
1248552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
1249552f7358SJed Brown {
1250e655db49SSatish Balay   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis;
1251002a2709SMatthew G. Knepley   char           name[PETSC_MAX_PATH_LEN];
1252552f7358SJed Brown   PetscErrorCode ierr;
1253552f7358SJed Brown 
1254552f7358SJed Brown   PetscFunctionBegin;
1255552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1256552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1257552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
1258fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
1259c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
1260e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
12618135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
1262552f7358SJed Brown   if (iascii) {
12638135c375SStefano Zampini     PetscViewerFormat format;
12648135c375SStefano Zampini     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
12658135c375SStefano Zampini     if (format == PETSC_VIEWER_ASCII_GLVIS) {
12668135c375SStefano Zampini       ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
12678135c375SStefano Zampini     } else {
1268552f7358SJed Brown       ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
12698135c375SStefano Zampini     }
1270c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
1271c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
127239d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1273c6ccd67eSMatthew G. Knepley #else
1274c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1275552f7358SJed Brown #endif
1276e412dcbdSMatthew G. Knepley   } else if (isvtk) {
1277fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
1278e412dcbdSMatthew G. Knepley   } else if (isdraw) {
1279e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
12808135c375SStefano Zampini   } else if (isglvis) {
12818135c375SStefano Zampini     ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
128262201deeSVaclav Hapla   } else {
1283a94aea51SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1284fcf6c8fdSToby Isaac   }
1285cb3ba0daSMatthew G. Knepley   /* Optionally view the partition */
1286cb3ba0daSMatthew G. Knepley   ierr = PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);CHKERRQ(ierr);
1287cb3ba0daSMatthew G. Knepley   if (flg) {
1288cb3ba0daSMatthew G. Knepley     Vec ranks;
1289cb3ba0daSMatthew G. Knepley     ierr = DMPlexCreateRankField(dm, &ranks);CHKERRQ(ierr);
1290cb3ba0daSMatthew G. Knepley     ierr = VecView(ranks, viewer);CHKERRQ(ierr);
1291cb3ba0daSMatthew G. Knepley     ierr = VecDestroy(&ranks);CHKERRQ(ierr);
1292cb3ba0daSMatthew G. Knepley   }
1293002a2709SMatthew G. Knepley   /* Optionally view a label */
1294002a2709SMatthew G. Knepley   ierr = PetscOptionsGetString(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_label_view", name, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr);
1295002a2709SMatthew G. Knepley   if (flg) {
1296002a2709SMatthew G. Knepley     DMLabel label;
1297002a2709SMatthew G. Knepley     Vec     val;
1298002a2709SMatthew G. Knepley 
1299002a2709SMatthew G. Knepley     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1300002a2709SMatthew G. Knepley     if (!label) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Label %s provided to -dm_label_view does not exist in this DM", name);
1301002a2709SMatthew G. Knepley     ierr = DMPlexCreateLabelField(dm, label, &val);CHKERRQ(ierr);
1302002a2709SMatthew G. Knepley     ierr = VecView(val, viewer);CHKERRQ(ierr);
1303002a2709SMatthew G. Knepley     ierr = VecDestroy(&val);CHKERRQ(ierr);
1304002a2709SMatthew G. Knepley   }
1305552f7358SJed Brown   PetscFunctionReturn(0);
1306552f7358SJed Brown }
1307552f7358SJed Brown 
13082c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
13092c40f234SMatthew G. Knepley {
1310d4f5a9a0SVaclav Hapla   PetscBool      ishdf5;
13112c40f234SMatthew G. Knepley   PetscErrorCode ierr;
13122c40f234SMatthew G. Knepley 
13132c40f234SMatthew G. Knepley   PetscFunctionBegin;
13142c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13152c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
13162c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
1317d4f5a9a0SVaclav Hapla   if (ishdf5) {
13182c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
13199c48423bSVaclav Hapla     PetscViewerFormat format;
13209c48423bSVaclav Hapla     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
13219c48423bSVaclav Hapla     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
13229c48423bSVaclav Hapla       ierr = DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);CHKERRQ(ierr);
1323509517efSVaclav Hapla     } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
132439d25373SMatthew G. Knepley       ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1325509517efSVaclav Hapla     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
13262c40f234SMatthew G. Knepley #else
13272c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1328552f7358SJed Brown #endif
1329d4f5a9a0SVaclav Hapla   } else {
1330d4f5a9a0SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
1331552f7358SJed Brown   }
1332552f7358SJed Brown   PetscFunctionReturn(0);
1333552f7358SJed Brown }
1334552f7358SJed Brown 
1335552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1336552f7358SJed Brown {
1337552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1338552f7358SJed Brown   PetscErrorCode ierr;
1339552f7358SJed Brown 
1340552f7358SJed Brown   PetscFunctionBegin;
13418135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
13428135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
13430d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1344552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1345552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1346552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1347552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1348be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1349552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1350552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1351d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1352d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
135377623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1354a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
1355552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1356552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1357a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1358a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1359d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1360d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1361d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1362d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1363d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1364d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1365fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
1366552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1367552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1368552f7358SJed Brown   PetscFunctionReturn(0);
1369552f7358SJed Brown }
1370552f7358SJed Brown 
1371b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1372552f7358SJed Brown {
13738d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1374acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1375552f7358SJed Brown   PetscInt               localSize;
1376837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1377552f7358SJed Brown   PetscErrorCode         ierr;
1378b412c318SBarry Smith   MatType                mtype;
13791428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1380552f7358SJed Brown 
1381552f7358SJed Brown   PetscFunctionBegin;
1382607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1383b412c318SBarry Smith   mtype = dm->mattype;
1384e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1385552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1386552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
138782f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1388552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1389552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1390552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1391acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1392acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1393552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1394552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1395552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1396552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1397552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1398552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1399552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1400837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1401552f7358SJed Brown   if (!isShell) {
1402be36d101SStefano Zampini     PetscSection subSection;
1403837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
14040be3e97aSMatthew G. Knepley     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1405fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1406552f7358SJed Brown 
140700140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1408be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1409be36d101SStefano Zampini       PetscSection section;
1410be36d101SStefano Zampini       PetscInt     size;
1411be36d101SStefano Zampini 
141292fd8e1eSJed Brown       ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1413be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1414be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1415be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1416be36d101SStefano Zampini     } else {
141700140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1418be36d101SStefano Zampini     }
1419552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1420be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1421a9d99c84SMatthew G. Knepley       PetscInt bdof;
1422a9d99c84SMatthew G. Knepley 
1423552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1424fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
14251d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
14261d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
14271d17a0a3SMatthew G. Knepley       if (dof) {
14281d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1429be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1430be36d101SStefano Zampini       }
1431be36d101SStefano Zampini       if (isMatIS) {
1432be36d101SStefano Zampini         PetscInt loff,c,off;
1433be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1434be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1435be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1436552f7358SJed Brown       }
14372a28c762SMatthew G Knepley     }
14382a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
14390be3e97aSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
14400be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
14410be3e97aSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
14420be3e97aSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
14436fd5c86aSStefano Zampini     bs = PetscMax(1,bs);
14446fd5c86aSStefano Zampini     if (isMatIS) { /* Must reduce indices by blocksize */
14454fa26246SMatthew G. Knepley       PetscInt l;
14466fd5c86aSStefano Zampini 
14476fd5c86aSStefano Zampini       lsize = lsize/bs;
14486fd5c86aSStefano Zampini       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] = ltogidx[l*bs]/bs;
14494fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1450be36d101SStefano Zampini     }
1451be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1452be36d101SStefano Zampini     if (isMatIS) {
1453be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1454be36d101SStefano Zampini     }
14551795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
14568d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1457552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1458552f7358SJed Brown   }
1459b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1460552f7358SJed Brown   PetscFunctionReturn(0);
1461552f7358SJed Brown }
1462552f7358SJed Brown 
14637cd05799SMatthew G. Knepley /*@
1464a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1465be36d101SStefano Zampini 
1466be36d101SStefano Zampini   Not collective
1467be36d101SStefano Zampini 
1468be36d101SStefano Zampini   Input Parameter:
1469be36d101SStefano Zampini . mesh - The DMPlex
1470be36d101SStefano Zampini 
1471be36d101SStefano Zampini   Output Parameters:
1472be36d101SStefano Zampini . subsection - The subdomain section
1473be36d101SStefano Zampini 
1474be36d101SStefano Zampini   Level: developer
1475be36d101SStefano Zampini 
1476be36d101SStefano Zampini .seealso:
14777cd05799SMatthew G. Knepley @*/
1478be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1479be36d101SStefano Zampini {
1480be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1481be36d101SStefano Zampini   PetscErrorCode ierr;
1482be36d101SStefano Zampini 
1483be36d101SStefano Zampini   PetscFunctionBegin;
1484be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1485be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1486be36d101SStefano Zampini     PetscSection section;
1487be36d101SStefano Zampini     PetscSF      sf;
1488be36d101SStefano Zampini 
1489be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
149092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
1491be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1492be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1493be36d101SStefano Zampini   }
1494be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1495be36d101SStefano Zampini   PetscFunctionReturn(0);
1496be36d101SStefano Zampini }
1497be36d101SStefano Zampini 
1498552f7358SJed Brown /*@
1499552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1500552f7358SJed Brown 
1501552f7358SJed Brown   Not collective
1502552f7358SJed Brown 
1503552f7358SJed Brown   Input Parameter:
1504552f7358SJed Brown . mesh - The DMPlex
1505552f7358SJed Brown 
1506552f7358SJed Brown   Output Parameters:
1507552f7358SJed Brown + pStart - The first mesh point
1508552f7358SJed Brown - pEnd   - The upper bound for mesh points
1509552f7358SJed Brown 
1510552f7358SJed Brown   Level: beginner
1511552f7358SJed Brown 
1512552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1513552f7358SJed Brown @*/
1514552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1515552f7358SJed Brown {
1516552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1517552f7358SJed Brown   PetscErrorCode ierr;
1518552f7358SJed Brown 
1519552f7358SJed Brown   PetscFunctionBegin;
1520552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1521552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1522552f7358SJed Brown   PetscFunctionReturn(0);
1523552f7358SJed Brown }
1524552f7358SJed Brown 
1525552f7358SJed Brown /*@
1526552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1527552f7358SJed Brown 
1528552f7358SJed Brown   Not collective
1529552f7358SJed Brown 
1530552f7358SJed Brown   Input Parameters:
1531552f7358SJed Brown + mesh - The DMPlex
1532552f7358SJed Brown . pStart - The first mesh point
1533552f7358SJed Brown - pEnd   - The upper bound for mesh points
1534552f7358SJed Brown 
1535552f7358SJed Brown   Output Parameters:
1536552f7358SJed Brown 
1537552f7358SJed Brown   Level: beginner
1538552f7358SJed Brown 
1539552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1540552f7358SJed Brown @*/
1541552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1542552f7358SJed Brown {
1543552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1544552f7358SJed Brown   PetscErrorCode ierr;
1545552f7358SJed Brown 
1546552f7358SJed Brown   PetscFunctionBegin;
1547552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1548552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1549552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1550552f7358SJed Brown   PetscFunctionReturn(0);
1551552f7358SJed Brown }
1552552f7358SJed Brown 
1553552f7358SJed Brown /*@
1554eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
1555552f7358SJed Brown 
1556552f7358SJed Brown   Not collective
1557552f7358SJed Brown 
1558552f7358SJed Brown   Input Parameters:
1559552f7358SJed Brown + mesh - The DMPlex
1560eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1561552f7358SJed Brown 
1562552f7358SJed Brown   Output Parameter:
1563552f7358SJed Brown . size - The cone size for point p
1564552f7358SJed Brown 
1565552f7358SJed Brown   Level: beginner
1566552f7358SJed Brown 
1567552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1568552f7358SJed Brown @*/
1569552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1570552f7358SJed Brown {
1571552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1572552f7358SJed Brown   PetscErrorCode ierr;
1573552f7358SJed Brown 
1574552f7358SJed Brown   PetscFunctionBegin;
1575552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1576552f7358SJed Brown   PetscValidPointer(size, 3);
1577552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1578552f7358SJed Brown   PetscFunctionReturn(0);
1579552f7358SJed Brown }
1580552f7358SJed Brown 
1581552f7358SJed Brown /*@
1582eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
1583552f7358SJed Brown 
1584552f7358SJed Brown   Not collective
1585552f7358SJed Brown 
1586552f7358SJed Brown   Input Parameters:
1587552f7358SJed Brown + mesh - The DMPlex
1588eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1589552f7358SJed Brown - size - The cone size for point p
1590552f7358SJed Brown 
1591552f7358SJed Brown   Output Parameter:
1592552f7358SJed Brown 
1593552f7358SJed Brown   Note:
1594552f7358SJed Brown   This should be called after DMPlexSetChart().
1595552f7358SJed Brown 
1596552f7358SJed Brown   Level: beginner
1597552f7358SJed Brown 
1598552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1599552f7358SJed Brown @*/
1600552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1601552f7358SJed Brown {
1602552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1603552f7358SJed Brown   PetscErrorCode ierr;
1604552f7358SJed Brown 
1605552f7358SJed Brown   PetscFunctionBegin;
1606552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1607552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
16080d644c17SKarl Rupp 
1609552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1610552f7358SJed Brown   PetscFunctionReturn(0);
1611552f7358SJed Brown }
1612552f7358SJed Brown 
1613f5a469b9SMatthew G. Knepley /*@
1614eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
1615f5a469b9SMatthew G. Knepley 
1616f5a469b9SMatthew G. Knepley   Not collective
1617f5a469b9SMatthew G. Knepley 
1618f5a469b9SMatthew G. Knepley   Input Parameters:
1619f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1620eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1621f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1622f5a469b9SMatthew G. Knepley 
1623f5a469b9SMatthew G. Knepley   Output Parameter:
1624f5a469b9SMatthew G. Knepley 
1625f5a469b9SMatthew G. Knepley   Note:
1626f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1627f5a469b9SMatthew G. Knepley 
1628f5a469b9SMatthew G. Knepley   Level: beginner
1629f5a469b9SMatthew G. Knepley 
1630f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1631f5a469b9SMatthew G. Knepley @*/
1632f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1633f5a469b9SMatthew G. Knepley {
1634f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1635f5a469b9SMatthew G. Knepley   PetscInt       csize;
1636f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1637f5a469b9SMatthew G. Knepley 
1638f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1639f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1640f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1641f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1642f5a469b9SMatthew G. Knepley 
1643f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1644f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1645f5a469b9SMatthew G. Knepley }
1646f5a469b9SMatthew G. Knepley 
1647552f7358SJed Brown /*@C
1648eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
1649552f7358SJed Brown 
1650552f7358SJed Brown   Not collective
1651552f7358SJed Brown 
1652552f7358SJed Brown   Input Parameters:
1653833c876bSVaclav Hapla + dm - The DMPlex
1654eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1655552f7358SJed Brown 
1656552f7358SJed Brown   Output Parameter:
1657552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1658552f7358SJed Brown 
1659552f7358SJed Brown   Level: beginner
1660552f7358SJed Brown 
16613813dfbdSMatthew G Knepley   Fortran Notes:
16623813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16633813dfbdSMatthew G Knepley   include petsc.h90 in your code.
1664922102d1SVaclav Hapla   You must also call DMPlexRestoreCone() after you finish using the returned array.
1665922102d1SVaclav Hapla   DMPlexRestoreCone() is not needed/available in C.
16663813dfbdSMatthew G Knepley 
16670ce7577fSVaclav Hapla .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexGetConeTuple(), DMPlexSetChart()
1668552f7358SJed Brown @*/
1669552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1670552f7358SJed Brown {
1671552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1672552f7358SJed Brown   PetscInt       off;
1673552f7358SJed Brown   PetscErrorCode ierr;
1674552f7358SJed Brown 
1675552f7358SJed Brown   PetscFunctionBegin;
1676552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1677552f7358SJed Brown   PetscValidPointer(cone, 3);
1678552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1679552f7358SJed Brown   *cone = &mesh->cones[off];
1680552f7358SJed Brown   PetscFunctionReturn(0);
1681552f7358SJed Brown }
1682552f7358SJed Brown 
16830ce7577fSVaclav Hapla /*@C
16840ce7577fSVaclav Hapla   DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG
16850ce7577fSVaclav Hapla 
16860ce7577fSVaclav Hapla   Not collective
16870ce7577fSVaclav Hapla 
16880ce7577fSVaclav Hapla   Input Parameters:
16890ce7577fSVaclav Hapla + dm - The DMPlex
16900ce7577fSVaclav Hapla - p - The IS of points, which must lie in the chart set with DMPlexSetChart()
16910ce7577fSVaclav Hapla 
16920ce7577fSVaclav Hapla   Output Parameter:
16930ce7577fSVaclav Hapla + pConesSection - PetscSection describing the layout of pCones
16940ce7577fSVaclav Hapla - pCones - An array of points which are on the in-edges for the point set p
16950ce7577fSVaclav Hapla 
16960ce7577fSVaclav Hapla   Level: intermediate
16970ce7577fSVaclav Hapla 
1698d4636a37SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeRecursive(), DMPlexSetChart()
16990ce7577fSVaclav Hapla @*/
17000ce7577fSVaclav Hapla PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
17010ce7577fSVaclav Hapla {
17020ce7577fSVaclav Hapla   PetscSection        cs, newcs;
17030ce7577fSVaclav Hapla   PetscInt            *cones;
17040ce7577fSVaclav Hapla   PetscInt            *newarr=NULL;
17050ce7577fSVaclav Hapla   PetscInt            n;
17060ce7577fSVaclav Hapla   PetscErrorCode      ierr;
17070ce7577fSVaclav Hapla 
17080ce7577fSVaclav Hapla   PetscFunctionBegin;
17090ce7577fSVaclav Hapla   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
17100ce7577fSVaclav Hapla   ierr = DMPlexGetConeSection(dm, &cs);CHKERRQ(ierr);
17110ce7577fSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
17120ce7577fSVaclav Hapla   if (pConesSection) *pConesSection = newcs;
17130ce7577fSVaclav Hapla   if (pCones) {
17140ce7577fSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
17150ce7577fSVaclav Hapla     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);CHKERRQ(ierr);
17160ce7577fSVaclav Hapla   }
17170ce7577fSVaclav Hapla   PetscFunctionReturn(0);
17180ce7577fSVaclav Hapla }
17190ce7577fSVaclav Hapla 
1720af9eab45SVaclav Hapla /*@
1721af9eab45SVaclav Hapla   DMPlexGetConeRecursiveVertices - Expand each given point into its cone points and do that recursively until we end up just with vertices.
1722d4636a37SVaclav Hapla 
1723d4636a37SVaclav Hapla   Not collective
1724d4636a37SVaclav Hapla 
1725d4636a37SVaclav Hapla   Input Parameters:
1726d4636a37SVaclav Hapla + dm - The DMPlex
1727af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1728d4636a37SVaclav Hapla 
1729d4636a37SVaclav Hapla   Output Parameter:
1730af9eab45SVaclav Hapla . expandedPoints - An array of vertices recursively expanded from input points
1731d4636a37SVaclav Hapla 
1732d4636a37SVaclav Hapla   Level: advanced
1733d4636a37SVaclav Hapla 
1734af9eab45SVaclav Hapla   Notes:
1735af9eab45SVaclav Hapla   Like DMPlexGetConeRecursive but returns only the 0-depth IS (i.e. vertices only) and no sections.
1736af9eab45SVaclav Hapla   There is no corresponding Restore function, just call ISDestroy() on the returned IS to deallocate.
1737af9eab45SVaclav Hapla 
1738af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexRestoreConeRecursive(), DMPlexGetDepth()
1739d4636a37SVaclav Hapla @*/
1740af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursiveVertices(DM dm, IS points, IS *expandedPoints)
1741d4636a37SVaclav Hapla {
1742af9eab45SVaclav Hapla   IS                  *expandedPointsAll;
1743af9eab45SVaclav Hapla   PetscInt            depth;
1744d4636a37SVaclav Hapla   PetscErrorCode      ierr;
1745d4636a37SVaclav Hapla 
1746d4636a37SVaclav Hapla   PetscFunctionBegin;
1747af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1748af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
1749af9eab45SVaclav Hapla   PetscValidPointer(expandedPoints, 3);
1750af9eab45SVaclav Hapla   ierr = DMPlexGetConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
1751af9eab45SVaclav Hapla   *expandedPoints = expandedPointsAll[0];
1752af9eab45SVaclav Hapla   ierr = PetscObjectReference((PetscObject)expandedPointsAll[0]);
1753af9eab45SVaclav Hapla   ierr = DMPlexRestoreConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
1754af9eab45SVaclav Hapla   PetscFunctionReturn(0);
1755af9eab45SVaclav Hapla }
1756af9eab45SVaclav Hapla 
1757af9eab45SVaclav Hapla /*@
1758af9eab45SVaclav Hapla   DMPlexGetConeRecursive - Expand each given point into its cone points and do that recursively until we end up just with vertices (DAG points of depth 0, i.e. without cones).
1759af9eab45SVaclav Hapla 
1760af9eab45SVaclav Hapla   Not collective
1761af9eab45SVaclav Hapla 
1762af9eab45SVaclav Hapla   Input Parameters:
1763af9eab45SVaclav Hapla + dm - The DMPlex
1764af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1765af9eab45SVaclav Hapla 
1766af9eab45SVaclav Hapla   Output Parameter:
1767af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
1768af9eab45SVaclav Hapla . expandedPoints - (optional) An array of index sets with recursively expanded cones
1769af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
1770af9eab45SVaclav Hapla 
1771af9eab45SVaclav Hapla   Level: advanced
1772af9eab45SVaclav Hapla 
1773af9eab45SVaclav Hapla   Notes:
1774af9eab45SVaclav Hapla   Like DMPlexGetConeTuple() but recursive.
1775af9eab45SVaclav Hapla 
1776af9eab45SVaclav Hapla   Array expandedPoints has size equal to depth. Each expandedPoints[d] contains DAG points with maximum depth d, recursively cone-wise expanded from the input points.
1777af9eab45SVaclav Hapla   For example, for d=0 it contains only vertices, for d=1 it can contain vertices and edges, etc.
1778af9eab45SVaclav Hapla 
1779af9eab45SVaclav Hapla   Array section has size equal to depth.  Each PetscSection sections[d] realizes mapping from expandedPoints[d+1] (section points) to expandedPoints[d] (section dofs) as follows:
1780af9eab45SVaclav Hapla   (1) DAG points in expandedPoints[d+1] with depth d+1 to their cone points in expandedPoints[d];
1781af9eab45SVaclav Hapla   (2) DAG points in expandedPoints[d+1] with depth in [0,d] to the same points in expandedPoints[d].
1782af9eab45SVaclav Hapla 
1783af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexRestoreConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
1784af9eab45SVaclav Hapla @*/
1785af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
1786af9eab45SVaclav Hapla {
1787af9eab45SVaclav Hapla   const PetscInt      *arr0=NULL, *cone=NULL;
1788af9eab45SVaclav Hapla   PetscInt            *arr=NULL, *newarr=NULL;
1789af9eab45SVaclav Hapla   PetscInt            d, depth_, i, n, newn, cn, co, start, end;
1790af9eab45SVaclav Hapla   IS                  *expandedPoints_;
1791af9eab45SVaclav Hapla   PetscSection        *sections_;
1792af9eab45SVaclav Hapla   PetscErrorCode      ierr;
1793af9eab45SVaclav Hapla 
1794af9eab45SVaclav Hapla   PetscFunctionBegin;
1795af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1796af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
1797af9eab45SVaclav Hapla   if (depth) PetscValidIntPointer(depth, 3);
1798af9eab45SVaclav Hapla   if (expandedPoints) PetscValidPointer(expandedPoints, 4);
1799af9eab45SVaclav Hapla   if (sections) PetscValidPointer(sections, 5);
1800af9eab45SVaclav Hapla   ierr = ISGetLocalSize(points, &n);CHKERRQ(ierr);
1801af9eab45SVaclav Hapla   ierr = ISGetIndices(points, &arr0);CHKERRQ(ierr);
1802af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
1803af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &expandedPoints_);CHKERRQ(ierr);
1804af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &sections_);CHKERRQ(ierr);
1805af9eab45SVaclav Hapla   arr = (PetscInt*) arr0; /* this is ok because first generation of arr is not modified */
1806af9eab45SVaclav Hapla   for (d=depth_-1; d>=0; d--) {
1807af9eab45SVaclav Hapla     ierr = PetscSectionCreate(PETSC_COMM_SELF, &sections_[d]);CHKERRQ(ierr);
1808af9eab45SVaclav Hapla     ierr = PetscSectionSetChart(sections_[d], 0, n);CHKERRQ(ierr);
1809af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
1810af9eab45SVaclav Hapla       ierr = DMPlexGetDepthStratum(dm, d+1, &start, &end);CHKERRQ(ierr);
1811af9eab45SVaclav Hapla       if (arr[i] >= start && arr[i] < end) {
1812af9eab45SVaclav Hapla         ierr = DMPlexGetConeSize(dm, arr[i], &cn);CHKERRQ(ierr);
1813af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, cn);CHKERRQ(ierr);
1814af9eab45SVaclav Hapla       } else {
1815af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, 1);CHKERRQ(ierr);
1816af9eab45SVaclav Hapla       }
1817af9eab45SVaclav Hapla     }
1818af9eab45SVaclav Hapla     ierr = PetscSectionSetUp(sections_[d]);CHKERRQ(ierr);
1819af9eab45SVaclav Hapla     ierr = PetscSectionGetStorageSize(sections_[d], &newn);CHKERRQ(ierr);
1820af9eab45SVaclav Hapla     ierr = PetscMalloc1(newn, &newarr);CHKERRQ(ierr);
1821af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
1822af9eab45SVaclav Hapla       ierr = PetscSectionGetDof(sections_[d], i, &cn);CHKERRQ(ierr);
1823af9eab45SVaclav Hapla       ierr = PetscSectionGetOffset(sections_[d], i, &co);CHKERRQ(ierr);
1824af9eab45SVaclav Hapla       if (cn > 1) {
1825af9eab45SVaclav Hapla         ierr = DMPlexGetCone(dm, arr[i], &cone);CHKERRQ(ierr);
1826af9eab45SVaclav Hapla         ierr = PetscMemcpy(&newarr[co], cone, cn*sizeof(PetscInt));CHKERRQ(ierr);
1827af9eab45SVaclav Hapla       } else {
1828af9eab45SVaclav Hapla         newarr[co] = arr[i];
1829af9eab45SVaclav Hapla       }
1830af9eab45SVaclav Hapla     }
1831af9eab45SVaclav Hapla     ierr = ISCreateGeneral(PETSC_COMM_SELF, newn, newarr, PETSC_OWN_POINTER, &expandedPoints_[d]);CHKERRQ(ierr);
1832af9eab45SVaclav Hapla     arr = newarr;
1833af9eab45SVaclav Hapla     n = newn;
1834af9eab45SVaclav Hapla   }
1835af9eab45SVaclav Hapla   *depth = depth_;
1836af9eab45SVaclav Hapla   if (expandedPoints) *expandedPoints = expandedPoints_;
1837af9eab45SVaclav Hapla   else {
1838af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&expandedPoints_[d]);CHKERRQ(ierr);}
1839af9eab45SVaclav Hapla     ierr = PetscFree(expandedPoints_);CHKERRQ(ierr);
1840af9eab45SVaclav Hapla   }
1841af9eab45SVaclav Hapla   if (sections) *sections = sections_;
1842af9eab45SVaclav Hapla   else {
1843af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&sections_[d]);CHKERRQ(ierr);}
1844af9eab45SVaclav Hapla     ierr = PetscFree(sections_);CHKERRQ(ierr);
1845af9eab45SVaclav Hapla   }
1846af9eab45SVaclav Hapla   PetscFunctionReturn(0);
1847af9eab45SVaclav Hapla }
1848af9eab45SVaclav Hapla 
1849af9eab45SVaclav Hapla /*@
1850af9eab45SVaclav Hapla   DMPlexRestoreConeRecursive - Deallocates arrays created by DMPlexGetConeRecursive
1851af9eab45SVaclav Hapla 
1852af9eab45SVaclav Hapla   Not collective
1853af9eab45SVaclav Hapla 
1854af9eab45SVaclav Hapla   Input Parameters:
1855af9eab45SVaclav Hapla + dm - The DMPlex
1856af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1857af9eab45SVaclav Hapla 
1858af9eab45SVaclav Hapla   Output Parameter:
1859af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
1860af9eab45SVaclav Hapla . expandedPoints - (optional) An array of recursively expanded cones
1861af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
1862af9eab45SVaclav Hapla 
1863af9eab45SVaclav Hapla   Level: advanced
1864af9eab45SVaclav Hapla 
1865af9eab45SVaclav Hapla   Notes:
1866af9eab45SVaclav Hapla   See DMPlexGetConeRecursive() for details.
1867af9eab45SVaclav Hapla 
1868af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
1869af9eab45SVaclav Hapla @*/
1870af9eab45SVaclav Hapla PetscErrorCode DMPlexRestoreConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
1871af9eab45SVaclav Hapla {
1872af9eab45SVaclav Hapla   PetscInt            d, depth_;
1873af9eab45SVaclav Hapla   PetscErrorCode      ierr;
1874af9eab45SVaclav Hapla 
1875af9eab45SVaclav Hapla   PetscFunctionBegin;
1876af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
1877af9eab45SVaclav Hapla   if (depth && *depth != depth_) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "depth changed since last call to DMPlexGetConeRecursive");
1878af9eab45SVaclav Hapla   if (depth) *depth = 0;
1879af9eab45SVaclav Hapla   if (expandedPoints) {
1880af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&((*expandedPoints)[d]));CHKERRQ(ierr);}
1881af9eab45SVaclav Hapla     ierr = PetscFree(*expandedPoints);CHKERRQ(ierr);
1882af9eab45SVaclav Hapla   }
1883af9eab45SVaclav Hapla   if (sections)  {
1884af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&((*sections)[d]));CHKERRQ(ierr);}
1885af9eab45SVaclav Hapla     ierr = PetscFree(*sections);CHKERRQ(ierr);
1886af9eab45SVaclav Hapla   }
1887d4636a37SVaclav Hapla   PetscFunctionReturn(0);
1888d4636a37SVaclav Hapla }
1889d4636a37SVaclav Hapla 
1890552f7358SJed Brown /*@
189192371b87SBarry Smith   DMPlexSetCone - Set the points on the in-edges for this point in the DAG; that is these are the points that cover the specific point
1892552f7358SJed Brown 
1893552f7358SJed Brown   Not collective
1894552f7358SJed Brown 
1895552f7358SJed Brown   Input Parameters:
1896552f7358SJed Brown + mesh - The DMPlex
1897eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1898552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1899552f7358SJed Brown 
1900552f7358SJed Brown   Output Parameter:
1901552f7358SJed Brown 
1902552f7358SJed Brown   Note:
1903552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1904552f7358SJed Brown 
190592371b87SBarry Smith   Developer Note: Why not call this DMPlexSetCover()
190692371b87SBarry Smith 
1907552f7358SJed Brown   Level: beginner
1908552f7358SJed Brown 
190992371b87SBarry Smith .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
1910552f7358SJed Brown @*/
1911552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1912552f7358SJed Brown {
1913552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1914552f7358SJed Brown   PetscInt       pStart, pEnd;
1915552f7358SJed Brown   PetscInt       dof, off, c;
1916552f7358SJed Brown   PetscErrorCode ierr;
1917552f7358SJed Brown 
1918552f7358SJed Brown   PetscFunctionBegin;
1919552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1920552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1921552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1922552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1923552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
192482f516ccSBarry 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);
1925552f7358SJed Brown   for (c = 0; c < dof; ++c) {
192682f516ccSBarry 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);
1927552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1928552f7358SJed Brown   }
1929552f7358SJed Brown   PetscFunctionReturn(0);
1930552f7358SJed Brown }
1931552f7358SJed Brown 
1932552f7358SJed Brown /*@C
1933eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
1934552f7358SJed Brown 
1935552f7358SJed Brown   Not collective
1936552f7358SJed Brown 
1937552f7358SJed Brown   Input Parameters:
1938552f7358SJed Brown + mesh - The DMPlex
1939eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1940552f7358SJed Brown 
1941552f7358SJed Brown   Output Parameter:
1942552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1943552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1944552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1945552f7358SJed Brown                     the index of the cone point on which to start.
1946552f7358SJed Brown 
1947552f7358SJed Brown   Level: beginner
1948552f7358SJed Brown 
19493813dfbdSMatthew G Knepley   Fortran Notes:
19503813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
19513813dfbdSMatthew G Knepley   include petsc.h90 in your code.
19523b12b3d8SVaclav Hapla   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1953922102d1SVaclav Hapla   DMPlexRestoreConeOrientation() is not needed/available in C.
19543813dfbdSMatthew G Knepley 
1955552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1956552f7358SJed Brown @*/
1957552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1958552f7358SJed Brown {
1959552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1960552f7358SJed Brown   PetscInt       off;
1961552f7358SJed Brown   PetscErrorCode ierr;
1962552f7358SJed Brown 
1963552f7358SJed Brown   PetscFunctionBegin;
1964552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1965552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1966552f7358SJed Brown   {
1967552f7358SJed Brown     PetscInt dof;
1968552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1969552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1970552f7358SJed Brown   }
1971552f7358SJed Brown #endif
1972552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
19730d644c17SKarl Rupp 
1974552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1975552f7358SJed Brown   PetscFunctionReturn(0);
1976552f7358SJed Brown }
1977552f7358SJed Brown 
1978552f7358SJed Brown /*@
1979eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
1980552f7358SJed Brown 
1981552f7358SJed Brown   Not collective
1982552f7358SJed Brown 
1983552f7358SJed Brown   Input Parameters:
1984552f7358SJed Brown + mesh - The DMPlex
1985eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1986552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1987552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1988552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1989552f7358SJed Brown                     the index of the cone point on which to start.
1990552f7358SJed Brown 
1991552f7358SJed Brown   Output Parameter:
1992552f7358SJed Brown 
1993552f7358SJed Brown   Note:
1994552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1995552f7358SJed Brown 
1996552f7358SJed Brown   Level: beginner
1997552f7358SJed Brown 
1998552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
1999552f7358SJed Brown @*/
2000552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
2001552f7358SJed Brown {
2002552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2003552f7358SJed Brown   PetscInt       pStart, pEnd;
2004552f7358SJed Brown   PetscInt       dof, off, c;
2005552f7358SJed Brown   PetscErrorCode ierr;
2006552f7358SJed Brown 
2007552f7358SJed Brown   PetscFunctionBegin;
2008552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2009552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
2010552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2011552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
2012552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
201382f516ccSBarry 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);
2014552f7358SJed Brown   for (c = 0; c < dof; ++c) {
2015552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
2016552f7358SJed Brown 
2017552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
201882f516ccSBarry 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);
2019552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
2020552f7358SJed Brown   }
2021552f7358SJed Brown   PetscFunctionReturn(0);
2022552f7358SJed Brown }
2023552f7358SJed Brown 
20247cd05799SMatthew G. Knepley /*@
2025eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
20267cd05799SMatthew G. Knepley 
20277cd05799SMatthew G. Knepley   Not collective
20287cd05799SMatthew G. Knepley 
20297cd05799SMatthew G. Knepley   Input Parameters:
20307cd05799SMatthew G. Knepley + mesh - The DMPlex
2031eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
20327cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
20337cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
20347cd05799SMatthew G. Knepley 
20357cd05799SMatthew G. Knepley   Level: beginner
20367cd05799SMatthew G. Knepley 
20377cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
20387cd05799SMatthew G. Knepley @*/
2039552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
2040552f7358SJed Brown {
2041552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2042552f7358SJed Brown   PetscInt       pStart, pEnd;
2043552f7358SJed Brown   PetscInt       dof, off;
2044552f7358SJed Brown   PetscErrorCode ierr;
2045552f7358SJed Brown 
2046552f7358SJed Brown   PetscFunctionBegin;
2047552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2048552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
204982f516ccSBarry 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);
205082f516ccSBarry 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);
205177c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
205277c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
205377c88f5bSMatthew 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);
2054552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
2055552f7358SJed Brown   PetscFunctionReturn(0);
2056552f7358SJed Brown }
2057552f7358SJed Brown 
20587cd05799SMatthew G. Knepley /*@
2059eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
20607cd05799SMatthew G. Knepley 
20617cd05799SMatthew G. Knepley   Not collective
20627cd05799SMatthew G. Knepley 
20637cd05799SMatthew G. Knepley   Input Parameters:
20647cd05799SMatthew G. Knepley + mesh - The DMPlex
2065eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
20667cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
20677cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
20687cd05799SMatthew G. Knepley 
20697cd05799SMatthew G. Knepley   Level: beginner
20707cd05799SMatthew G. Knepley 
20717cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
20727cd05799SMatthew G. Knepley @*/
207377c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
207477c88f5bSMatthew G Knepley {
207577c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
207677c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
207777c88f5bSMatthew G Knepley   PetscInt       dof, off;
207877c88f5bSMatthew G Knepley   PetscErrorCode ierr;
207977c88f5bSMatthew G Knepley 
208077c88f5bSMatthew G Knepley   PetscFunctionBegin;
208177c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
208277c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
208377c88f5bSMatthew 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);
208477c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
208577c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
208677c88f5bSMatthew 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);
208777c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
208877c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
208977c88f5bSMatthew G Knepley }
209077c88f5bSMatthew G Knepley 
2091552f7358SJed Brown /*@
2092eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
2093552f7358SJed Brown 
2094552f7358SJed Brown   Not collective
2095552f7358SJed Brown 
2096552f7358SJed Brown   Input Parameters:
2097552f7358SJed Brown + mesh - The DMPlex
2098eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2099552f7358SJed Brown 
2100552f7358SJed Brown   Output Parameter:
2101552f7358SJed Brown . size - The support size for point p
2102552f7358SJed Brown 
2103552f7358SJed Brown   Level: beginner
2104552f7358SJed Brown 
2105552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
2106552f7358SJed Brown @*/
2107552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
2108552f7358SJed Brown {
2109552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2110552f7358SJed Brown   PetscErrorCode ierr;
2111552f7358SJed Brown 
2112552f7358SJed Brown   PetscFunctionBegin;
2113552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2114552f7358SJed Brown   PetscValidPointer(size, 3);
2115552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
2116552f7358SJed Brown   PetscFunctionReturn(0);
2117552f7358SJed Brown }
2118552f7358SJed Brown 
2119552f7358SJed Brown /*@
2120eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
2121552f7358SJed Brown 
2122552f7358SJed Brown   Not collective
2123552f7358SJed Brown 
2124552f7358SJed Brown   Input Parameters:
2125552f7358SJed Brown + mesh - The DMPlex
2126eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2127552f7358SJed Brown - size - The support size for point p
2128552f7358SJed Brown 
2129552f7358SJed Brown   Output Parameter:
2130552f7358SJed Brown 
2131552f7358SJed Brown   Note:
2132552f7358SJed Brown   This should be called after DMPlexSetChart().
2133552f7358SJed Brown 
2134552f7358SJed Brown   Level: beginner
2135552f7358SJed Brown 
2136552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
2137552f7358SJed Brown @*/
2138552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
2139552f7358SJed Brown {
2140552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2141552f7358SJed Brown   PetscErrorCode ierr;
2142552f7358SJed Brown 
2143552f7358SJed Brown   PetscFunctionBegin;
2144552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2145552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
21460d644c17SKarl Rupp 
2147552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
2148552f7358SJed Brown   PetscFunctionReturn(0);
2149552f7358SJed Brown }
2150552f7358SJed Brown 
2151552f7358SJed Brown /*@C
2152eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
2153552f7358SJed Brown 
2154552f7358SJed Brown   Not collective
2155552f7358SJed Brown 
2156552f7358SJed Brown   Input Parameters:
2157552f7358SJed Brown + mesh - The DMPlex
2158eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2159552f7358SJed Brown 
2160552f7358SJed Brown   Output Parameter:
2161552f7358SJed Brown . support - An array of points which are on the out-edges for point p
2162552f7358SJed Brown 
2163552f7358SJed Brown   Level: beginner
2164552f7358SJed Brown 
21653813dfbdSMatthew G Knepley   Fortran Notes:
21663813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
21673813dfbdSMatthew G Knepley   include petsc.h90 in your code.
21683b12b3d8SVaclav Hapla   You must also call DMPlexRestoreSupport() after you finish using the returned array.
2169922102d1SVaclav Hapla   DMPlexRestoreSupport() is not needed/available in C.
21703813dfbdSMatthew G Knepley 
2171552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2172552f7358SJed Brown @*/
2173552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
2174552f7358SJed Brown {
2175552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2176552f7358SJed Brown   PetscInt       off;
2177552f7358SJed Brown   PetscErrorCode ierr;
2178552f7358SJed Brown 
2179552f7358SJed Brown   PetscFunctionBegin;
2180552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2181552f7358SJed Brown   PetscValidPointer(support, 3);
2182552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
2183552f7358SJed Brown   *support = &mesh->supports[off];
2184552f7358SJed Brown   PetscFunctionReturn(0);
2185552f7358SJed Brown }
2186552f7358SJed Brown 
2187552f7358SJed Brown /*@
218892371b87SBarry Smith   DMPlexSetSupport - Set the points on the out-edges for this point in the DAG, that is the list of points that this point covers
2189552f7358SJed Brown 
2190552f7358SJed Brown   Not collective
2191552f7358SJed Brown 
2192552f7358SJed Brown   Input Parameters:
2193552f7358SJed Brown + mesh - The DMPlex
2194eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
219592371b87SBarry Smith - support - An array of points which are on the out-edges for point p
2196552f7358SJed Brown 
2197552f7358SJed Brown   Output Parameter:
2198552f7358SJed Brown 
2199552f7358SJed Brown   Note:
2200552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
2201552f7358SJed Brown 
2202552f7358SJed Brown   Level: beginner
2203552f7358SJed Brown 
220492371b87SBarry Smith .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
2205552f7358SJed Brown @*/
2206552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
2207552f7358SJed Brown {
2208552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2209552f7358SJed Brown   PetscInt       pStart, pEnd;
2210552f7358SJed Brown   PetscInt       dof, off, c;
2211552f7358SJed Brown   PetscErrorCode ierr;
2212552f7358SJed Brown 
2213552f7358SJed Brown   PetscFunctionBegin;
2214552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2215552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2216552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2217552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
2218552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
221982f516ccSBarry 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);
2220552f7358SJed Brown   for (c = 0; c < dof; ++c) {
222182f516ccSBarry 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);
2222552f7358SJed Brown     mesh->supports[off+c] = support[c];
2223552f7358SJed Brown   }
2224552f7358SJed Brown   PetscFunctionReturn(0);
2225552f7358SJed Brown }
2226552f7358SJed Brown 
22277cd05799SMatthew G. Knepley /*@
2228eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
22297cd05799SMatthew G. Knepley 
22307cd05799SMatthew G. Knepley   Not collective
22317cd05799SMatthew G. Knepley 
22327cd05799SMatthew G. Knepley   Input Parameters:
22337cd05799SMatthew G. Knepley + mesh - The DMPlex
2234eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
22357cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
22367cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
22377cd05799SMatthew G. Knepley 
22387cd05799SMatthew G. Knepley   Level: beginner
22397cd05799SMatthew G. Knepley 
22407cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
22417cd05799SMatthew G. Knepley @*/
2242552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
2243552f7358SJed Brown {
2244552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2245552f7358SJed Brown   PetscInt       pStart, pEnd;
2246552f7358SJed Brown   PetscInt       dof, off;
2247552f7358SJed Brown   PetscErrorCode ierr;
2248552f7358SJed Brown 
2249552f7358SJed Brown   PetscFunctionBegin;
2250552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2251552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2252552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2253552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
225482f516ccSBarry 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);
225582f516ccSBarry 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);
225682f516ccSBarry 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);
2257552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
2258552f7358SJed Brown   PetscFunctionReturn(0);
2259552f7358SJed Brown }
2260552f7358SJed Brown 
2261552f7358SJed Brown /*@C
2262eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
2263552f7358SJed Brown 
2264552f7358SJed Brown   Not collective
2265552f7358SJed Brown 
2266552f7358SJed Brown   Input Parameters:
2267552f7358SJed Brown + mesh - The DMPlex
2268eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2269552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
22700298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
2271552f7358SJed Brown 
2272552f7358SJed Brown   Output Parameters:
2273552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
2274552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
2275552f7358SJed Brown 
2276552f7358SJed Brown   Note:
22770298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
2278552f7358SJed Brown 
22793813dfbdSMatthew G Knepley   Fortran Notes:
22803813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22813813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22823813dfbdSMatthew G Knepley 
22833813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
22843813dfbdSMatthew G Knepley 
2285552f7358SJed Brown   Level: beginner
2286552f7358SJed Brown 
2287552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2288552f7358SJed Brown @*/
2289552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2290552f7358SJed Brown {
2291552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
2292552f7358SJed Brown   PetscInt       *closure, *fifo;
22930298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
2294552f7358SJed Brown   PetscInt        tmpSize, t;
2295552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
2296552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
2297552f7358SJed Brown   PetscErrorCode  ierr;
2298552f7358SJed Brown 
2299552f7358SJed Brown   PetscFunctionBegin;
2300552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2301552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2302552f7358SJed Brown   /* This is only 1-level */
2303552f7358SJed Brown   if (useCone) {
2304552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
2305552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
2306552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
2307552f7358SJed Brown   } else {
2308552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
2309552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
2310552f7358SJed Brown   }
2311bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
2312bfbcdd7aSMatthew G. Knepley     if (*points) {
2313bfbcdd7aSMatthew G. Knepley       closure = *points;
2314bfbcdd7aSMatthew G. Knepley     } else {
2315bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
231669291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2317bfbcdd7aSMatthew G. Knepley     }
2318bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
2319bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
2320bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
2321bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
2322bfbcdd7aSMatthew G. Knepley     }
2323bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
2324bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
2325bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
2326bfbcdd7aSMatthew G. Knepley   }
232724c766afSToby Isaac   {
232824c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
232924c766afSToby Isaac 
233024c766afSToby Isaac     c = mesh->maxConeSize;
233124c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
233224c766afSToby Isaac     s = mesh->maxSupportSize;
233324c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
233424c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
233524c766afSToby Isaac   }
233669291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2337bfbcdd7aSMatthew G. Knepley   if (*points) {
2338bfbcdd7aSMatthew G. Knepley     closure = *points;
2339bfbcdd7aSMatthew G. Knepley   } else {
234069291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2341bfbcdd7aSMatthew G. Knepley   }
2342bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
2343552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
2344552f7358SJed Brown     const PetscInt cp = tmp[t];
2345552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
2346552f7358SJed Brown 
2347552f7358SJed Brown     closure[closureSize]   = cp;
2348552f7358SJed Brown     closure[closureSize+1] = co;
2349552f7358SJed Brown     fifo[fifoSize]         = cp;
2350552f7358SJed Brown     fifo[fifoSize+1]       = co;
2351552f7358SJed Brown   }
2352bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
2353552f7358SJed Brown   while (fifoSize - fifoStart) {
2354552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
2355552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
2356552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
2357552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
2358552f7358SJed Brown 
2359552f7358SJed Brown     if (useCone) {
2360552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
2361552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
2362552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
2363552f7358SJed Brown     } else {
2364552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
2365552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
23660298fd71SBarry Smith       tmpO = NULL;
2367552f7358SJed Brown     }
2368552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
2369552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
2370552f7358SJed Brown       const PetscInt cp = tmp[i];
23712e1b13c2SMatthew 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. */
23722e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
23732e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
23742e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
2375552f7358SJed Brown       PetscInt       c;
2376552f7358SJed Brown 
23772e1b13c2SMatthew G. Knepley       if (rev) {
23782e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
23792e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
23802e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
23812e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
23822e1b13c2SMatthew G. Knepley       }
2383552f7358SJed Brown       /* Check for duplicate */
2384552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
2385552f7358SJed Brown         if (closure[c] == cp) break;
2386552f7358SJed Brown       }
2387552f7358SJed Brown       if (c == closureSize) {
2388552f7358SJed Brown         closure[closureSize]   = cp;
2389552f7358SJed Brown         closure[closureSize+1] = co;
2390552f7358SJed Brown         fifo[fifoSize]         = cp;
2391552f7358SJed Brown         fifo[fifoSize+1]       = co;
2392552f7358SJed Brown         closureSize           += 2;
2393552f7358SJed Brown         fifoSize              += 2;
2394552f7358SJed Brown       }
2395552f7358SJed Brown     }
2396552f7358SJed Brown     fifoStart += 2;
2397552f7358SJed Brown   }
2398552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
2399552f7358SJed Brown   if (points)    *points    = closure;
240069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2401552f7358SJed Brown   PetscFunctionReturn(0);
2402552f7358SJed Brown }
2403552f7358SJed Brown 
24049bf0dad6SMatthew G. Knepley /*@C
2405eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure_Internal - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG with a specified initial orientation
24069bf0dad6SMatthew G. Knepley 
24079bf0dad6SMatthew G. Knepley   Not collective
24089bf0dad6SMatthew G. Knepley 
24099bf0dad6SMatthew G. Knepley   Input Parameters:
24109bf0dad6SMatthew G. Knepley + mesh - The DMPlex
2411eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
24129bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
24139bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
24149bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
24159bf0dad6SMatthew G. Knepley 
24169bf0dad6SMatthew G. Knepley   Output Parameters:
24179bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
24189bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
24199bf0dad6SMatthew G. Knepley 
24209bf0dad6SMatthew G. Knepley   Note:
24219bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
24229bf0dad6SMatthew G. Knepley 
24239bf0dad6SMatthew G. Knepley   Fortran Notes:
24249bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24259bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
24269bf0dad6SMatthew G. Knepley 
24279bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24289bf0dad6SMatthew G. Knepley 
24299bf0dad6SMatthew G. Knepley   Level: beginner
24309bf0dad6SMatthew G. Knepley 
24319bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
24329bf0dad6SMatthew G. Knepley @*/
24339bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
24349bf0dad6SMatthew G. Knepley {
24359bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
24369bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
24379bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
24389bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
24399bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
24409bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
24419bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
24429bf0dad6SMatthew G. Knepley 
24439bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
24449bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24459bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
24469bf0dad6SMatthew G. Knepley   /* This is only 1-level */
24479bf0dad6SMatthew G. Knepley   if (useCone) {
24489bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
24499bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
24509bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
24519bf0dad6SMatthew G. Knepley   } else {
24529bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
24539bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
24549bf0dad6SMatthew G. Knepley   }
24559bf0dad6SMatthew G. Knepley   if (depth == 1) {
24569bf0dad6SMatthew G. Knepley     if (*points) {
24579bf0dad6SMatthew G. Knepley       closure = *points;
24589bf0dad6SMatthew G. Knepley     } else {
24599bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
246069291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
24619bf0dad6SMatthew G. Knepley     }
24629bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
24639bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
24649bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
24659bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
24669bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
24679bf0dad6SMatthew G. Knepley     }
24689bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
24699bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
24709bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
24719bf0dad6SMatthew G. Knepley   }
247224c766afSToby Isaac   {
247324c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
247424c766afSToby Isaac 
247524c766afSToby Isaac     c = mesh->maxConeSize;
247624c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
247724c766afSToby Isaac     s = mesh->maxSupportSize;
247824c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
247924c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
248024c766afSToby Isaac   }
248169291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
24829bf0dad6SMatthew G. Knepley   if (*points) {
24839bf0dad6SMatthew G. Knepley     closure = *points;
24849bf0dad6SMatthew G. Knepley   } else {
248569291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
24869bf0dad6SMatthew G. Knepley   }
24879bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
24889bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
24899bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
24909bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
24919bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
24929bf0dad6SMatthew G. Knepley 
24939bf0dad6SMatthew G. Knepley     if (ornt < 0) {
24949bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
24959bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
249686b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
24979bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
24989bf0dad6SMatthew G. Knepley     }
24999bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
25009bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
25019bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
25029bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
25039bf0dad6SMatthew G. Knepley   }
25049bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
25059bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
25069bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
25079bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
25089bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
25099bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
25109bf0dad6SMatthew G. Knepley 
25119bf0dad6SMatthew G. Knepley     if (useCone) {
25129bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
25139bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
25149bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
25159bf0dad6SMatthew G. Knepley     } else {
25169bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
25179bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
25189bf0dad6SMatthew G. Knepley       tmpO = NULL;
25199bf0dad6SMatthew G. Knepley     }
25209bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
25219bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
25229bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
25239bf0dad6SMatthew 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. */
25249bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
25259bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
25269bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
25279bf0dad6SMatthew G. Knepley       PetscInt       c;
25289bf0dad6SMatthew G. Knepley 
25299bf0dad6SMatthew G. Knepley       if (rev) {
25309bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
25319bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
25329bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
25339bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
25349bf0dad6SMatthew G. Knepley       }
25359bf0dad6SMatthew G. Knepley       /* Check for duplicate */
25369bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
25379bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
25389bf0dad6SMatthew G. Knepley       }
25399bf0dad6SMatthew G. Knepley       if (c == closureSize) {
25409bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
25419bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
25429bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
25439bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
25449bf0dad6SMatthew G. Knepley         closureSize           += 2;
25459bf0dad6SMatthew G. Knepley         fifoSize              += 2;
25469bf0dad6SMatthew G. Knepley       }
25479bf0dad6SMatthew G. Knepley     }
25489bf0dad6SMatthew G. Knepley     fifoStart += 2;
25499bf0dad6SMatthew G. Knepley   }
25509bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
25519bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
255269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
25539bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
25549bf0dad6SMatthew G. Knepley }
25559bf0dad6SMatthew G. Knepley 
2556552f7358SJed Brown /*@C
2557eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
2558552f7358SJed Brown 
2559552f7358SJed Brown   Not collective
2560552f7358SJed Brown 
2561552f7358SJed Brown   Input Parameters:
2562552f7358SJed Brown + mesh - The DMPlex
2563eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2564552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2565e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2566e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2567552f7358SJed Brown 
2568552f7358SJed Brown   Note:
25690298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2570552f7358SJed Brown 
25713813dfbdSMatthew G Knepley   Fortran Notes:
25723813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25733813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25743813dfbdSMatthew G Knepley 
25753813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25763813dfbdSMatthew G Knepley 
2577552f7358SJed Brown   Level: beginner
2578552f7358SJed Brown 
2579552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2580552f7358SJed Brown @*/
2581552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2582552f7358SJed Brown {
2583552f7358SJed Brown   PetscErrorCode ierr;
2584552f7358SJed Brown 
2585552f7358SJed Brown   PetscFunctionBegin;
2586552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2587e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2588e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
258969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
25904ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2591552f7358SJed Brown   PetscFunctionReturn(0);
2592552f7358SJed Brown }
2593552f7358SJed Brown 
2594552f7358SJed Brown /*@
2595eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
2596552f7358SJed Brown 
2597552f7358SJed Brown   Not collective
2598552f7358SJed Brown 
2599552f7358SJed Brown   Input Parameter:
2600552f7358SJed Brown . mesh - The DMPlex
2601552f7358SJed Brown 
2602552f7358SJed Brown   Output Parameters:
2603552f7358SJed Brown + maxConeSize - The maximum number of in-edges
2604552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
2605552f7358SJed Brown 
2606552f7358SJed Brown   Level: beginner
2607552f7358SJed Brown 
2608552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2609552f7358SJed Brown @*/
2610552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2611552f7358SJed Brown {
2612552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2613552f7358SJed Brown 
2614552f7358SJed Brown   PetscFunctionBegin;
2615552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2616552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2617552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2618552f7358SJed Brown   PetscFunctionReturn(0);
2619552f7358SJed Brown }
2620552f7358SJed Brown 
2621552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2622552f7358SJed Brown {
2623552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2624552f7358SJed Brown   PetscInt       size;
2625552f7358SJed Brown   PetscErrorCode ierr;
2626552f7358SJed Brown 
2627552f7358SJed Brown   PetscFunctionBegin;
2628552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2629552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2630552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
26311795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
26321795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2633552f7358SJed Brown   if (mesh->maxSupportSize) {
2634552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2635552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
26361795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2637552f7358SJed Brown   }
2638552f7358SJed Brown   PetscFunctionReturn(0);
2639552f7358SJed Brown }
2640552f7358SJed Brown 
2641276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2642552f7358SJed Brown {
2643552f7358SJed Brown   PetscErrorCode ierr;
2644552f7358SJed Brown 
2645552f7358SJed Brown   PetscFunctionBegin;
26464d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
2647792b654fSMatthew G. Knepley   ierr = DMCreateSectionSubDM(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2648c2939958SSatish Balay   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
2649736995cdSBlaise Bourdin   if (dm->useNatural && dm->sfMigration) {
2650f94b4a02SBlaise Bourdin     PetscSF        sfMigrationInv,sfNatural;
2651f94b4a02SBlaise Bourdin     PetscSection   section, sectionSeq;
2652f94b4a02SBlaise Bourdin 
26533dcd263cSBlaise Bourdin     (*subdm)->sfMigration = dm->sfMigration;
26543dcd263cSBlaise Bourdin     ierr = PetscObjectReference((PetscObject) dm->sfMigration);CHKERRQ(ierr);
265592fd8e1eSJed Brown     ierr = DMGetLocalSection((*subdm), &section);CHKERRQ(ierr);CHKERRQ(ierr);
2656f94b4a02SBlaise Bourdin     ierr = PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
2657f94b4a02SBlaise Bourdin     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);CHKERRQ(ierr);
2658f94b4a02SBlaise Bourdin     ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
2659f94b4a02SBlaise Bourdin 
2660f94b4a02SBlaise Bourdin     ierr = DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2661c40e9495SBlaise Bourdin     (*subdm)->sfNatural = sfNatural;
2662f94b4a02SBlaise Bourdin     ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
2663f94b4a02SBlaise Bourdin     ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
2664f94b4a02SBlaise Bourdin   }
2665552f7358SJed Brown   PetscFunctionReturn(0);
2666552f7358SJed Brown }
2667552f7358SJed Brown 
26682adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
26692adcc780SMatthew G. Knepley {
26702adcc780SMatthew G. Knepley   PetscErrorCode ierr;
26713dcd263cSBlaise Bourdin   PetscInt       i = 0;
26722adcc780SMatthew G. Knepley 
26732adcc780SMatthew G. Knepley   PetscFunctionBegin;
2674435bcee1SStefano Zampini   ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);
2675792b654fSMatthew G. Knepley   ierr = DMCreateSectionSuperDM(dms, len, is, superdm);CHKERRQ(ierr);
2676c40e9495SBlaise Bourdin   (*superdm)->useNatural = PETSC_FALSE;
26773dcd263cSBlaise Bourdin   for (i = 0; i < len; i++){
26783dcd263cSBlaise Bourdin     if (dms[i]->useNatural && dms[i]->sfMigration) {
26793dcd263cSBlaise Bourdin       PetscSF        sfMigrationInv,sfNatural;
26803dcd263cSBlaise Bourdin       PetscSection   section, sectionSeq;
26813dcd263cSBlaise Bourdin 
26823dcd263cSBlaise Bourdin       (*superdm)->sfMigration = dms[i]->sfMigration;
26833dcd263cSBlaise Bourdin       ierr = PetscObjectReference((PetscObject) dms[i]->sfMigration);CHKERRQ(ierr);
2684c40e9495SBlaise Bourdin       (*superdm)->useNatural = PETSC_TRUE;
268592fd8e1eSJed Brown       ierr = DMGetLocalSection((*superdm), &section);CHKERRQ(ierr);
26863dcd263cSBlaise Bourdin       ierr = PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
26873dcd263cSBlaise Bourdin       ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);CHKERRQ(ierr);
26883dcd263cSBlaise Bourdin       ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
26893dcd263cSBlaise Bourdin 
26903dcd263cSBlaise Bourdin       ierr = DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2691c40e9495SBlaise Bourdin       (*superdm)->sfNatural = sfNatural;
26923dcd263cSBlaise Bourdin       ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
26933dcd263cSBlaise Bourdin       ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
26943dcd263cSBlaise Bourdin       break;
26953dcd263cSBlaise Bourdin     }
26963dcd263cSBlaise Bourdin   }
26972adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
26982adcc780SMatthew G. Knepley }
26992adcc780SMatthew G. Knepley 
2700552f7358SJed Brown /*@
2701eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
2702552f7358SJed Brown 
2703552f7358SJed Brown   Not collective
2704552f7358SJed Brown 
2705552f7358SJed Brown   Input Parameter:
2706552f7358SJed Brown . mesh - The DMPlex
2707552f7358SJed Brown 
2708552f7358SJed Brown   Output Parameter:
2709552f7358SJed Brown 
2710552f7358SJed Brown   Note:
2711552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2712552f7358SJed Brown 
2713552f7358SJed Brown   Level: beginner
2714552f7358SJed Brown 
2715552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2716552f7358SJed Brown @*/
2717552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2718552f7358SJed Brown {
2719552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2720552f7358SJed Brown   PetscInt      *offsets;
2721552f7358SJed Brown   PetscInt       supportSize;
2722552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2723552f7358SJed Brown   PetscErrorCode ierr;
2724552f7358SJed Brown 
2725552f7358SJed Brown   PetscFunctionBegin;
2726552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
272782f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
272830b0ce1bSStefano Zampini   ierr = PetscLogEventBegin(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
2729552f7358SJed Brown   /* Calculate support sizes */
2730552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2731552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2732552f7358SJed Brown     PetscInt dof, off, c;
2733552f7358SJed Brown 
2734552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2735552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2736552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2737552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2738552f7358SJed Brown     }
2739552f7358SJed Brown   }
2740552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2741552f7358SJed Brown     PetscInt dof;
2742552f7358SJed Brown 
2743552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
27440d644c17SKarl Rupp 
2745552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2746552f7358SJed Brown   }
2747552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2748552f7358SJed Brown   /* Calculate supports */
2749552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
27501795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
27511795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2752552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2753552f7358SJed Brown     PetscInt dof, off, c;
2754552f7358SJed Brown 
2755552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2756552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2757552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2758552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2759552f7358SJed Brown       PetscInt       offS;
2760552f7358SJed Brown 
2761552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
27620d644c17SKarl Rupp 
2763552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2764552f7358SJed Brown       ++offsets[q];
2765552f7358SJed Brown     }
2766552f7358SJed Brown   }
2767552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
276830b0ce1bSStefano Zampini   ierr = PetscLogEventEnd(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
2769552f7358SJed Brown   PetscFunctionReturn(0);
2770552f7358SJed Brown }
2771552f7358SJed Brown 
2772277ea44aSLisandro Dalcin static PetscErrorCode DMPlexCreateDepthStratum(DM dm, DMLabel label, PetscInt depth, PetscInt pStart, PetscInt pEnd)
2773277ea44aSLisandro Dalcin {
2774277ea44aSLisandro Dalcin   IS             stratumIS;
2775277ea44aSLisandro Dalcin   PetscErrorCode ierr;
2776277ea44aSLisandro Dalcin 
2777277ea44aSLisandro Dalcin   PetscFunctionBegin;
2778277ea44aSLisandro Dalcin   if (pStart >= pEnd) PetscFunctionReturn(0);
2779277ea44aSLisandro Dalcin #if defined(PETSC_USE_DEBUG)
2780277ea44aSLisandro Dalcin   {
2781277ea44aSLisandro Dalcin     PetscInt  qStart, qEnd, numLevels, level;
2782277ea44aSLisandro Dalcin     PetscBool overlap = PETSC_FALSE;
2783277ea44aSLisandro Dalcin     ierr = DMLabelGetNumValues(label, &numLevels);CHKERRQ(ierr);
2784277ea44aSLisandro Dalcin     for (level = 0; level < numLevels; level++) {
2785277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
2786277ea44aSLisandro Dalcin       if ((pStart >= qStart && pStart < qEnd) || (pEnd > qStart && pEnd <= qEnd)) {overlap = PETSC_TRUE; break;}
2787277ea44aSLisandro Dalcin     }
2788277ea44aSLisandro Dalcin     if (overlap) SETERRQ6(PETSC_COMM_SELF, PETSC_ERR_PLIB, "New depth %D range [%D,%D) overlaps with depth %D range [%D,%D)", depth, pStart, pEnd, level, qStart, qEnd);
2789277ea44aSLisandro Dalcin   }
2790277ea44aSLisandro Dalcin #endif
2791277ea44aSLisandro Dalcin   ierr = ISCreateStride(PETSC_COMM_SELF, pEnd-pStart, pStart, 1, &stratumIS);CHKERRQ(ierr);
2792277ea44aSLisandro Dalcin   ierr = DMLabelSetStratumIS(label, depth, stratumIS);CHKERRQ(ierr);
2793277ea44aSLisandro Dalcin   ierr = ISDestroy(&stratumIS);CHKERRQ(ierr);
2794277ea44aSLisandro Dalcin   PetscFunctionReturn(0);
2795277ea44aSLisandro Dalcin }
2796277ea44aSLisandro Dalcin 
27977d5acc75SStefano Zampini static PetscErrorCode DMPlexCreateDimStratum(DM,DMLabel,DMLabel,PetscInt,PetscInt);
27987d5acc75SStefano Zampini 
2799552f7358SJed Brown /*@
2800a8d69d7bSBarry Smith   DMPlexStratify - The DAG for most topologies is a graded poset (https://en.wikipedia.org/wiki/Graded_poset), and
28016dd80730SBarry Smith   can be illustrated by a Hasse Diagram (https://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2802552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2803552f7358SJed Brown   the DAG.
2804552f7358SJed Brown 
2805bf4602e4SToby Isaac   Collective on dm
2806552f7358SJed Brown 
2807552f7358SJed Brown   Input Parameter:
2808552f7358SJed Brown . mesh - The DMPlex
2809552f7358SJed Brown 
2810552f7358SJed Brown   Output Parameter:
2811552f7358SJed Brown 
2812552f7358SJed Brown   Notes:
2813150b719bSJed Brown   Concretely, DMPlexStratify() creates a new label named "depth" containing the dimension of each element: 0 for vertices,
2814150b719bSJed Brown   1 for edges, and so on.  The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2815c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2816150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2817552f7358SJed Brown 
2818150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2819552f7358SJed Brown 
2820552f7358SJed Brown   Level: beginner
2821552f7358SJed Brown 
2822552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
2823552f7358SJed Brown @*/
2824552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2825552f7358SJed Brown {
2826df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2827aa50250dSMatthew G. Knepley   DMLabel        label;
2828552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2829552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
28307d5acc75SStefano Zampini   PetscInt       cMax, fMax, eMax, vMax;
2831552f7358SJed Brown   PetscErrorCode ierr;
2832552f7358SJed Brown 
2833552f7358SJed Brown   PetscFunctionBegin;
2834552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2835552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2836277ea44aSLisandro Dalcin 
2837277ea44aSLisandro Dalcin   /* Create depth label */
2838aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2839c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2840aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2841277ea44aSLisandro Dalcin 
2842277ea44aSLisandro Dalcin   {
2843552f7358SJed Brown     /* Initialize roots and count leaves */
2844277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
2845277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
2846552f7358SJed Brown     PetscInt coneSize, supportSize;
2847552f7358SJed Brown 
2848277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
2849552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2850552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2851552f7358SJed Brown       if (!coneSize && supportSize) {
2852277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2853277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2854552f7358SJed Brown         ++numRoots;
2855552f7358SJed Brown       } else if (!supportSize && coneSize) {
2856552f7358SJed Brown         ++numLeaves;
2857552f7358SJed Brown       } else if (!supportSize && !coneSize) {
2858552f7358SJed Brown         /* Isolated points */
2859277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2860277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2861552f7358SJed Brown       }
2862552f7358SJed Brown     }
2863277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 0, sMin, sMax+1);CHKERRQ(ierr);
2864277ea44aSLisandro Dalcin   }
2865277ea44aSLisandro Dalcin 
2866552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2867277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
2868277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
2869552f7358SJed Brown     PetscInt coneSize, supportSize;
2870552f7358SJed Brown 
2871277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
2872552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2873552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2874552f7358SJed Brown       if (!supportSize && coneSize) {
2875277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2876277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2877552f7358SJed Brown       }
2878552f7358SJed Brown     }
2879277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 1, sMin, sMax+1);CHKERRQ(ierr);
2880552f7358SJed Brown   } else {
2881277ea44aSLisandro Dalcin     PetscInt level = 0;
2882277ea44aSLisandro Dalcin     PetscInt qStart, qEnd, q;
2883552f7358SJed Brown 
2884277ea44aSLisandro Dalcin     ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
2885277ea44aSLisandro Dalcin     while (qEnd > qStart) {
2886277ea44aSLisandro Dalcin       PetscInt sMin = PETSC_MAX_INT;
2887277ea44aSLisandro Dalcin       PetscInt sMax = PETSC_MIN_INT;
288874ef644bSMatthew G. Knepley 
2889277ea44aSLisandro Dalcin       for (q = qStart; q < qEnd; ++q) {
289074ef644bSMatthew G. Knepley         const PetscInt *support;
289174ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
289274ef644bSMatthew G. Knepley 
2893277ea44aSLisandro Dalcin         ierr = DMPlexGetSupportSize(dm, q, &supportSize);CHKERRQ(ierr);
2894277ea44aSLisandro Dalcin         ierr = DMPlexGetSupport(dm, q, &support);CHKERRQ(ierr);
289574ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
2896277ea44aSLisandro Dalcin           sMin = PetscMin(support[s], sMin);
2897277ea44aSLisandro Dalcin           sMax = PetscMax(support[s], sMax);
2898552f7358SJed Brown         }
2899552f7358SJed Brown       }
2900277ea44aSLisandro Dalcin       ierr = DMLabelGetNumValues(label, &level);CHKERRQ(ierr);
2901277ea44aSLisandro Dalcin       ierr = DMPlexCreateDepthStratum(dm, label, level, sMin, sMax+1);CHKERRQ(ierr);
2902277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
290374ef644bSMatthew G. Knepley     }
290474ef644bSMatthew G. Knepley   }
2905bf4602e4SToby Isaac   { /* just in case there is an empty process */
2906bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2907bf4602e4SToby Isaac 
2908bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
29096d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2910bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2911367003a6SStefano Zampini       ierr = DMLabelAddStratum(label, v);CHKERRQ(ierr);
2912bf4602e4SToby Isaac     }
2913bf4602e4SToby Isaac   }
2914d67d17b1SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) label, &mesh->depthState);CHKERRQ(ierr);
29157d5acc75SStefano Zampini 
29167d5acc75SStefano Zampini   ierr = DMPlexGetHybridBounds(dm, &cMax, &fMax, &eMax, &vMax);CHKERRQ(ierr);
29177d5acc75SStefano Zampini   if (cMax >= 0 || fMax >= 0 || eMax >= 0 || vMax >= 0) {
29187d5acc75SStefano Zampini     PetscInt dim;
2919277ea44aSLisandro Dalcin     DMLabel  dimLabel;
29207d5acc75SStefano Zampini 
29217d5acc75SStefano Zampini     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
29227d5acc75SStefano Zampini     ierr = DMCreateLabel(dm, "dim");CHKERRQ(ierr);
29237d5acc75SStefano Zampini     ierr = DMGetLabel(dm, "dim", &dimLabel);CHKERRQ(ierr);
29247d5acc75SStefano Zampini     if (cMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, dim, cMax);CHKERRQ(ierr);}
29257d5acc75SStefano Zampini     if (fMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, dim - 1, fMax);CHKERRQ(ierr);}
29267d5acc75SStefano Zampini     if (eMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, 1, eMax);CHKERRQ(ierr);}
29277d5acc75SStefano Zampini     if (vMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, 0, vMax);CHKERRQ(ierr);}
29287d5acc75SStefano Zampini   }
2929552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2930552f7358SJed Brown   PetscFunctionReturn(0);
2931552f7358SJed Brown }
2932552f7358SJed Brown 
2933552f7358SJed Brown /*@C
2934552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
2935552f7358SJed Brown 
2936552f7358SJed Brown   Not Collective
2937552f7358SJed Brown 
2938552f7358SJed Brown   Input Parameters:
2939552f7358SJed Brown + dm - The DMPlex object
2940552f7358SJed Brown . numPoints - The number of input points for the join
2941552f7358SJed Brown - points - The input points
2942552f7358SJed Brown 
2943552f7358SJed Brown   Output Parameters:
2944552f7358SJed Brown + numCoveredPoints - The number of points in the join
2945552f7358SJed Brown - coveredPoints - The points in the join
2946552f7358SJed Brown 
2947552f7358SJed Brown   Level: intermediate
2948552f7358SJed Brown 
2949552f7358SJed Brown   Note: Currently, this is restricted to a single level join
2950552f7358SJed Brown 
29513813dfbdSMatthew G Knepley   Fortran Notes:
29523813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
29533813dfbdSMatthew G Knepley   include petsc.h90 in your code.
29543813dfbdSMatthew G Knepley 
29553813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
29563813dfbdSMatthew G Knepley 
2957552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2958552f7358SJed Brown @*/
2959552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2960552f7358SJed Brown {
2961552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2962552f7358SJed Brown   PetscInt      *join[2];
2963552f7358SJed Brown   PetscInt       joinSize, i = 0;
2964552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2965552f7358SJed Brown   PetscErrorCode ierr;
2966552f7358SJed Brown 
2967552f7358SJed Brown   PetscFunctionBegin;
2968552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
296948bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
297048bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
297148bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
297269291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
297369291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2974552f7358SJed Brown   /* Copy in support of first point */
2975552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
2976552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
2977552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
2978552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
2979552f7358SJed Brown   }
2980552f7358SJed Brown   /* Check each successive support */
2981552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
2982552f7358SJed Brown     PetscInt newJoinSize = 0;
2983552f7358SJed Brown 
2984552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
2985552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
2986552f7358SJed Brown     for (c = 0; c < dof; ++c) {
2987552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
2988552f7358SJed Brown 
2989552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
2990552f7358SJed Brown         if (point == join[i][m]) {
2991552f7358SJed Brown           join[1-i][newJoinSize++] = point;
2992552f7358SJed Brown           break;
2993552f7358SJed Brown         }
2994552f7358SJed Brown       }
2995552f7358SJed Brown     }
2996552f7358SJed Brown     joinSize = newJoinSize;
2997552f7358SJed Brown     i        = 1-i;
2998552f7358SJed Brown   }
2999552f7358SJed Brown   *numCoveredPoints = joinSize;
3000552f7358SJed Brown   *coveredPoints    = join[i];
300169291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3002552f7358SJed Brown   PetscFunctionReturn(0);
3003552f7358SJed Brown }
3004552f7358SJed Brown 
3005552f7358SJed Brown /*@C
3006552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
3007552f7358SJed Brown 
3008552f7358SJed Brown   Not Collective
3009552f7358SJed Brown 
3010552f7358SJed Brown   Input Parameters:
3011552f7358SJed Brown + dm - The DMPlex object
3012552f7358SJed Brown . numPoints - The number of input points for the join
3013552f7358SJed Brown - points - The input points
3014552f7358SJed Brown 
3015552f7358SJed Brown   Output Parameters:
3016552f7358SJed Brown + numCoveredPoints - The number of points in the join
3017552f7358SJed Brown - coveredPoints - The points in the join
3018552f7358SJed Brown 
30193813dfbdSMatthew G Knepley   Fortran Notes:
30203813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
30213813dfbdSMatthew G Knepley   include petsc.h90 in your code.
30223813dfbdSMatthew G Knepley 
30233813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
30243813dfbdSMatthew G Knepley 
3025552f7358SJed Brown   Level: intermediate
3026552f7358SJed Brown 
3027552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
3028552f7358SJed Brown @*/
3029552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3030552f7358SJed Brown {
3031552f7358SJed Brown   PetscErrorCode ierr;
3032552f7358SJed Brown 
3033552f7358SJed Brown   PetscFunctionBegin;
3034552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3035d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3036d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3037d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
303869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3039d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3040552f7358SJed Brown   PetscFunctionReturn(0);
3041552f7358SJed Brown }
3042552f7358SJed Brown 
3043552f7358SJed Brown /*@C
3044552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
3045552f7358SJed Brown 
3046552f7358SJed Brown   Not Collective
3047552f7358SJed Brown 
3048552f7358SJed Brown   Input Parameters:
3049552f7358SJed Brown + dm - The DMPlex object
3050552f7358SJed Brown . numPoints - The number of input points for the join
3051552f7358SJed Brown - points - The input points
3052552f7358SJed Brown 
3053552f7358SJed Brown   Output Parameters:
3054552f7358SJed Brown + numCoveredPoints - The number of points in the join
3055552f7358SJed Brown - coveredPoints - The points in the join
3056552f7358SJed Brown 
30573813dfbdSMatthew G Knepley   Fortran Notes:
30583813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
30593813dfbdSMatthew G Knepley   include petsc.h90 in your code.
30603813dfbdSMatthew G Knepley 
30613813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
30623813dfbdSMatthew G Knepley 
3063552f7358SJed Brown   Level: intermediate
3064552f7358SJed Brown 
3065552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
3066552f7358SJed Brown @*/
3067552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3068552f7358SJed Brown {
3069552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3070552f7358SJed Brown   PetscInt      *offsets, **closures;
3071552f7358SJed Brown   PetscInt      *join[2];
3072552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
307324c766afSToby Isaac   PetscInt       p, d, c, m, ms;
3074552f7358SJed Brown   PetscErrorCode ierr;
3075552f7358SJed Brown 
3076552f7358SJed Brown   PetscFunctionBegin;
3077552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
307848bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
307948bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
308048bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
3081552f7358SJed Brown 
3082552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
30831795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
308469291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
308524c766afSToby Isaac   ms      = mesh->maxSupportSize;
308624c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
308769291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
308869291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
3089552f7358SJed Brown 
3090552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3091552f7358SJed Brown     PetscInt closureSize;
3092552f7358SJed Brown 
3093552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
30940d644c17SKarl Rupp 
3095552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
3096552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
3097552f7358SJed Brown       PetscInt pStart, pEnd, i;
3098552f7358SJed Brown 
3099552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
3100552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
3101552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3102552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
3103552f7358SJed Brown           break;
3104552f7358SJed Brown         }
3105552f7358SJed Brown       }
3106552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
3107552f7358SJed Brown     }
310882f516ccSBarry 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);
3109552f7358SJed Brown   }
3110552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
3111552f7358SJed Brown     PetscInt dof;
3112552f7358SJed Brown 
3113552f7358SJed Brown     /* Copy in support of first point */
3114552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
3115552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
3116552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
3117552f7358SJed Brown     }
3118552f7358SJed Brown     /* Check each successive cone */
3119552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
3120552f7358SJed Brown       PetscInt newJoinSize = 0;
3121552f7358SJed Brown 
3122552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
3123552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3124552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
3125552f7358SJed Brown 
3126552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
3127552f7358SJed Brown           if (point == join[i][m]) {
3128552f7358SJed Brown             join[1-i][newJoinSize++] = point;
3129552f7358SJed Brown             break;
3130552f7358SJed Brown           }
3131552f7358SJed Brown         }
3132552f7358SJed Brown       }
3133552f7358SJed Brown       joinSize = newJoinSize;
3134552f7358SJed Brown       i        = 1-i;
3135552f7358SJed Brown     }
3136552f7358SJed Brown     if (joinSize) break;
3137552f7358SJed Brown   }
3138552f7358SJed Brown   *numCoveredPoints = joinSize;
3139552f7358SJed Brown   *coveredPoints    = join[i];
3140552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
31410298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
3142552f7358SJed Brown   }
3143552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
314469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
314569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3146552f7358SJed Brown   PetscFunctionReturn(0);
3147552f7358SJed Brown }
3148552f7358SJed Brown 
3149552f7358SJed Brown /*@C
3150552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
3151552f7358SJed Brown 
3152552f7358SJed Brown   Not Collective
3153552f7358SJed Brown 
3154552f7358SJed Brown   Input Parameters:
3155552f7358SJed Brown + dm - The DMPlex object
3156552f7358SJed Brown . numPoints - The number of input points for the meet
3157552f7358SJed Brown - points - The input points
3158552f7358SJed Brown 
3159552f7358SJed Brown   Output Parameters:
3160552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3161552f7358SJed Brown - coveredPoints - The points in the meet
3162552f7358SJed Brown 
3163552f7358SJed Brown   Level: intermediate
3164552f7358SJed Brown 
3165552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
3166552f7358SJed Brown 
31673813dfbdSMatthew G Knepley   Fortran Notes:
31683813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
31693813dfbdSMatthew G Knepley   include petsc.h90 in your code.
31703813dfbdSMatthew G Knepley 
31713813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
31723813dfbdSMatthew G Knepley 
3173552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
3174552f7358SJed Brown @*/
3175552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
3176552f7358SJed Brown {
3177552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3178552f7358SJed Brown   PetscInt      *meet[2];
3179552f7358SJed Brown   PetscInt       meetSize, i = 0;
3180552f7358SJed Brown   PetscInt       dof, off, p, c, m;
3181552f7358SJed Brown   PetscErrorCode ierr;
3182552f7358SJed Brown 
3183552f7358SJed Brown   PetscFunctionBegin;
3184552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3185552f7358SJed Brown   PetscValidPointer(points, 2);
3186552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
3187552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
318869291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
318969291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3190552f7358SJed Brown   /* Copy in cone of first point */
3191552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
3192552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
3193552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
3194552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
3195552f7358SJed Brown   }
3196552f7358SJed Brown   /* Check each successive cone */
3197552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3198552f7358SJed Brown     PetscInt newMeetSize = 0;
3199552f7358SJed Brown 
3200552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
3201552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
3202552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3203552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
3204552f7358SJed Brown 
3205552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
3206552f7358SJed Brown         if (point == meet[i][m]) {
3207552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
3208552f7358SJed Brown           break;
3209552f7358SJed Brown         }
3210552f7358SJed Brown       }
3211552f7358SJed Brown     }
3212552f7358SJed Brown     meetSize = newMeetSize;
3213552f7358SJed Brown     i        = 1-i;
3214552f7358SJed Brown   }
3215552f7358SJed Brown   *numCoveringPoints = meetSize;
3216552f7358SJed Brown   *coveringPoints    = meet[i];
321769291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3218552f7358SJed Brown   PetscFunctionReturn(0);
3219552f7358SJed Brown }
3220552f7358SJed Brown 
3221552f7358SJed Brown /*@C
3222552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
3223552f7358SJed Brown 
3224552f7358SJed Brown   Not Collective
3225552f7358SJed Brown 
3226552f7358SJed Brown   Input Parameters:
3227552f7358SJed Brown + dm - The DMPlex object
3228552f7358SJed Brown . numPoints - The number of input points for the meet
3229552f7358SJed Brown - points - The input points
3230552f7358SJed Brown 
3231552f7358SJed Brown   Output Parameters:
3232552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3233552f7358SJed Brown - coveredPoints - The points in the meet
3234552f7358SJed Brown 
3235552f7358SJed Brown   Level: intermediate
3236552f7358SJed Brown 
32373813dfbdSMatthew G Knepley   Fortran Notes:
32383813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
32393813dfbdSMatthew G Knepley   include petsc.h90 in your code.
32403813dfbdSMatthew G Knepley 
32413813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
32423813dfbdSMatthew G Knepley 
3243552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
3244552f7358SJed Brown @*/
3245552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3246552f7358SJed Brown {
3247552f7358SJed Brown   PetscErrorCode ierr;
3248552f7358SJed Brown 
3249552f7358SJed Brown   PetscFunctionBegin;
3250552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3251d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3252d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3253d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
325469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3255d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3256552f7358SJed Brown   PetscFunctionReturn(0);
3257552f7358SJed Brown }
3258552f7358SJed Brown 
3259552f7358SJed Brown /*@C
3260552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
3261552f7358SJed Brown 
3262552f7358SJed Brown   Not Collective
3263552f7358SJed Brown 
3264552f7358SJed Brown   Input Parameters:
3265552f7358SJed Brown + dm - The DMPlex object
3266552f7358SJed Brown . numPoints - The number of input points for the meet
3267552f7358SJed Brown - points - The input points
3268552f7358SJed Brown 
3269552f7358SJed Brown   Output Parameters:
3270552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3271552f7358SJed Brown - coveredPoints - The points in the meet
3272552f7358SJed Brown 
3273552f7358SJed Brown   Level: intermediate
3274552f7358SJed Brown 
32753813dfbdSMatthew G Knepley   Fortran Notes:
32763813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
32773813dfbdSMatthew G Knepley   include petsc.h90 in your code.
32783813dfbdSMatthew G Knepley 
32793813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
32803813dfbdSMatthew G Knepley 
3281552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
3282552f7358SJed Brown @*/
3283552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3284552f7358SJed Brown {
3285552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3286552f7358SJed Brown   PetscInt      *offsets, **closures;
3287552f7358SJed Brown   PetscInt      *meet[2];
3288552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
328924c766afSToby Isaac   PetscInt       p, h, c, m, mc;
3290552f7358SJed Brown   PetscErrorCode ierr;
3291552f7358SJed Brown 
3292552f7358SJed Brown   PetscFunctionBegin;
3293552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3294552f7358SJed Brown   PetscValidPointer(points, 2);
3295552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
3296552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
3297552f7358SJed Brown 
3298552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
3299785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
330069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
330124c766afSToby Isaac   mc      = mesh->maxConeSize;
330224c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
330369291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
330469291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3305552f7358SJed Brown 
3306552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3307552f7358SJed Brown     PetscInt closureSize;
3308552f7358SJed Brown 
3309552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
33100d644c17SKarl Rupp 
3311552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
3312552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
3313552f7358SJed Brown       PetscInt pStart, pEnd, i;
3314552f7358SJed Brown 
3315552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
3316552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
3317552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3318552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
3319552f7358SJed Brown           break;
3320552f7358SJed Brown         }
3321552f7358SJed Brown       }
3322552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
3323552f7358SJed Brown     }
332482f516ccSBarry 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);
3325552f7358SJed Brown   }
3326552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
3327552f7358SJed Brown     PetscInt dof;
3328552f7358SJed Brown 
3329552f7358SJed Brown     /* Copy in cone of first point */
3330552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
3331552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
3332552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
3333552f7358SJed Brown     }
3334552f7358SJed Brown     /* Check each successive cone */
3335552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
3336552f7358SJed Brown       PetscInt newMeetSize = 0;
3337552f7358SJed Brown 
3338552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
3339552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3340552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
3341552f7358SJed Brown 
3342552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
3343552f7358SJed Brown           if (point == meet[i][m]) {
3344552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
3345552f7358SJed Brown             break;
3346552f7358SJed Brown           }
3347552f7358SJed Brown         }
3348552f7358SJed Brown       }
3349552f7358SJed Brown       meetSize = newMeetSize;
3350552f7358SJed Brown       i        = 1-i;
3351552f7358SJed Brown     }
3352552f7358SJed Brown     if (meetSize) break;
3353552f7358SJed Brown   }
3354552f7358SJed Brown   *numCoveredPoints = meetSize;
3355552f7358SJed Brown   *coveredPoints    = meet[i];
3356552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
33570298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
3358552f7358SJed Brown   }
3359552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
336069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
336169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3362552f7358SJed Brown   PetscFunctionReturn(0);
3363552f7358SJed Brown }
3364552f7358SJed Brown 
33654e3744c5SMatthew G. Knepley /*@C
33664e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
33674e3744c5SMatthew G. Knepley 
33684e3744c5SMatthew G. Knepley   Not Collective
33694e3744c5SMatthew G. Knepley 
33704e3744c5SMatthew G. Knepley   Input Parameters:
33714e3744c5SMatthew G. Knepley + dmA - A DMPlex object
33724e3744c5SMatthew G. Knepley - dmB - A DMPlex object
33734e3744c5SMatthew G. Knepley 
33744e3744c5SMatthew G. Knepley   Output Parameters:
33754e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
33764e3744c5SMatthew G. Knepley 
33774e3744c5SMatthew G. Knepley   Level: intermediate
33784e3744c5SMatthew G. Knepley 
33794e3744c5SMatthew G. Knepley   Notes:
33804e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
33814e3744c5SMatthew G. Knepley 
33824e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
33834e3744c5SMatthew G. Knepley @*/
33844e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
33854e3744c5SMatthew G. Knepley {
33864e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
33874e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
33884e3744c5SMatthew G. Knepley 
33894e3744c5SMatthew G. Knepley   PetscFunctionBegin;
33904e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
33914e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
33924e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
33934e3744c5SMatthew G. Knepley 
33944e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
33954e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
33964e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
33974e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
33984e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
33994e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
34004e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
34014e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
34024e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
34034e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
34044e3744c5SMatthew G. Knepley 
34054e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
34064e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
34074e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
34084e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
34094e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
34104e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
34114e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
34124e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
34134e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
34144e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
34154e3744c5SMatthew G. Knepley     }
34164e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
34174e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
34184e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
34194e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
34204e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
34214e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
34224e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
34234e3744c5SMatthew G. Knepley     }
34244e3744c5SMatthew G. Knepley   }
34254e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
34264e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
34274e3744c5SMatthew G. Knepley }
34284e3744c5SMatthew G. Knepley 
34297cd05799SMatthew G. Knepley /*@C
34307cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
34317cd05799SMatthew G. Knepley 
34327cd05799SMatthew G. Knepley   Not Collective
34337cd05799SMatthew G. Knepley 
34347cd05799SMatthew G. Knepley   Input Parameters:
34357cd05799SMatthew G. Knepley + dm         - The DMPlex
34367cd05799SMatthew G. Knepley . cellDim    - The cell dimension
34377cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
34387cd05799SMatthew G. Knepley 
34397cd05799SMatthew G. Knepley   Output Parameters:
34407cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
34417cd05799SMatthew G. Knepley 
34427cd05799SMatthew G. Knepley   Level: developer
34437cd05799SMatthew G. Knepley 
34447cd05799SMatthew G. Knepley   Notes:
34457cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
34467cd05799SMatthew G. Knepley 
34477cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
34487cd05799SMatthew G. Knepley @*/
344918ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
3450a6dfd86eSKarl Rupp {
345182f516ccSBarry Smith   MPI_Comm       comm;
3452552f7358SJed Brown   PetscErrorCode ierr;
3453552f7358SJed Brown 
3454552f7358SJed Brown   PetscFunctionBegin;
345582f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3456552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
3457552f7358SJed Brown   switch (cellDim) {
3458552f7358SJed Brown   case 0:
3459552f7358SJed Brown     *numFaceVertices = 0;
3460552f7358SJed Brown     break;
3461552f7358SJed Brown   case 1:
3462552f7358SJed Brown     *numFaceVertices = 1;
3463552f7358SJed Brown     break;
3464552f7358SJed Brown   case 2:
3465552f7358SJed Brown     switch (numCorners) {
346619436ca2SJed Brown     case 3: /* triangle */
346719436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3468552f7358SJed Brown       break;
346919436ca2SJed Brown     case 4: /* quadrilateral */
347019436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3471552f7358SJed Brown       break;
347219436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
347319436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3474552f7358SJed Brown       break;
347519436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
347619436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3477552f7358SJed Brown       break;
3478552f7358SJed Brown     default:
3479f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3480552f7358SJed Brown     }
3481552f7358SJed Brown     break;
3482552f7358SJed Brown   case 3:
3483552f7358SJed Brown     switch (numCorners) {
348419436ca2SJed Brown     case 4: /* tetradehdron */
348519436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
3486552f7358SJed Brown       break;
348719436ca2SJed Brown     case 6: /* tet cohesive cells */
348819436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3489552f7358SJed Brown       break;
349019436ca2SJed Brown     case 8: /* hexahedron */
349119436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3492552f7358SJed Brown       break;
349319436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
349419436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3495552f7358SJed Brown       break;
349619436ca2SJed Brown     case 10: /* quadratic tetrahedron */
349719436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3498552f7358SJed Brown       break;
349919436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
350019436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3501552f7358SJed Brown       break;
350219436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
350319436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3504552f7358SJed Brown       break;
350519436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
350619436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
3507552f7358SJed Brown       break;
3508552f7358SJed Brown     default:
3509f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3510552f7358SJed Brown     }
3511552f7358SJed Brown     break;
3512552f7358SJed Brown   default:
3513f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
3514552f7358SJed Brown   }
3515552f7358SJed Brown   PetscFunctionReturn(0);
3516552f7358SJed Brown }
3517552f7358SJed Brown 
3518552f7358SJed Brown /*@
3519aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
3520552f7358SJed Brown 
3521552f7358SJed Brown   Not Collective
3522552f7358SJed Brown 
3523aa50250dSMatthew G. Knepley   Input Parameter:
3524552f7358SJed Brown . dm    - The DMPlex object
3525552f7358SJed Brown 
3526aa50250dSMatthew G. Knepley   Output Parameter:
3527aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
3528552f7358SJed Brown 
3529552f7358SJed Brown   Level: developer
3530552f7358SJed Brown 
3531aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3532aa50250dSMatthew G. Knepley @*/
3533aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
3534aa50250dSMatthew G. Knepley {
3535aa50250dSMatthew G. Knepley   PetscErrorCode ierr;
3536aa50250dSMatthew G. Knepley 
3537aa50250dSMatthew G. Knepley   PetscFunctionBegin;
3538aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3539aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
3540c58f1c22SToby Isaac   if (!dm->depthLabel) {ierr = DMGetLabel(dm, "depth", &dm->depthLabel);CHKERRQ(ierr);}
3541c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
3542aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
3543aa50250dSMatthew G. Knepley }
3544aa50250dSMatthew G. Knepley 
3545aa50250dSMatthew G. Knepley /*@
3546aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
3547aa50250dSMatthew G. Knepley 
3548aa50250dSMatthew G. Knepley   Not Collective
3549aa50250dSMatthew G. Knepley 
3550aa50250dSMatthew G. Knepley   Input Parameter:
3551aa50250dSMatthew G. Knepley . dm    - The DMPlex object
3552aa50250dSMatthew G. Knepley 
3553aa50250dSMatthew G. Knepley   Output Parameter:
3554aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
3555aa50250dSMatthew G. Knepley 
3556aa50250dSMatthew G. Knepley   Level: developer
3557552f7358SJed Brown 
3558aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3559552f7358SJed Brown @*/
3560552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
3561552f7358SJed Brown {
3562aa50250dSMatthew G. Knepley   DMLabel        label;
3563aa50250dSMatthew G. Knepley   PetscInt       d = 0;
3564552f7358SJed Brown   PetscErrorCode ierr;
3565552f7358SJed Brown 
3566552f7358SJed Brown   PetscFunctionBegin;
3567552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3568552f7358SJed Brown   PetscValidPointer(depth, 2);
3569aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3570aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
3571552f7358SJed Brown   *depth = d-1;
3572552f7358SJed Brown   PetscFunctionReturn(0);
3573552f7358SJed Brown }
3574552f7358SJed Brown 
3575552f7358SJed Brown /*@
3576552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
3577552f7358SJed Brown 
3578552f7358SJed Brown   Not Collective
3579552f7358SJed Brown 
3580552f7358SJed Brown   Input Parameters:
3581552f7358SJed Brown + dm           - The DMPlex object
3582552f7358SJed Brown - stratumValue - The requested depth
3583552f7358SJed Brown 
3584552f7358SJed Brown   Output Parameters:
3585552f7358SJed Brown + start - The first point at this depth
3586552f7358SJed Brown - end   - One beyond the last point at this depth
3587552f7358SJed Brown 
3588647867b2SJed Brown   Notes:
3589647867b2SJed Brown   Depth indexing is related to topological dimension.  Depth stratum 0 contains the lowest topological dimension points,
3590647867b2SJed Brown   often "vertices".  If the mesh is "interpolated" (see DMPlexInterpolate()), then depth stratum 1 contains the next
3591647867b2SJed Brown   higher dimension, e.g., "edges".
3592647867b2SJed Brown 
3593552f7358SJed Brown   Level: developer
3594552f7358SJed Brown 
3595552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
3596552f7358SJed Brown @*/
35970adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
35980adebc6cSBarry Smith {
3599aa50250dSMatthew G. Knepley   DMLabel        label;
360063d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
3601552f7358SJed Brown   PetscErrorCode ierr;
3602552f7358SJed Brown 
3603552f7358SJed Brown   PetscFunctionBegin;
3604552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
360563d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
360663d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3607552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
36080d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
360963d1a920SMatthew G. Knepley   if (stratumValue < 0) {
361063d1a920SMatthew G. Knepley     if (start) *start = pStart;
361163d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
361263d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3613552f7358SJed Brown   }
3614aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
361563d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
361663d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
3617552f7358SJed Brown   PetscFunctionReturn(0);
3618552f7358SJed Brown }
3619552f7358SJed Brown 
3620552f7358SJed Brown /*@
3621552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
3622552f7358SJed Brown 
3623552f7358SJed Brown   Not Collective
3624552f7358SJed Brown 
3625552f7358SJed Brown   Input Parameters:
3626552f7358SJed Brown + dm           - The DMPlex object
3627552f7358SJed Brown - stratumValue - The requested height
3628552f7358SJed Brown 
3629552f7358SJed Brown   Output Parameters:
3630552f7358SJed Brown + start - The first point at this height
3631552f7358SJed Brown - end   - One beyond the last point at this height
3632552f7358SJed Brown 
3633647867b2SJed Brown   Notes:
3634647867b2SJed Brown   Height indexing is related to topological codimension.  Height stratum 0 contains the highest topological dimension
3635647867b2SJed Brown   points, often called "cells" or "elements".  If the mesh is "interpolated" (see DMPlexInterpolate()), then height
3636647867b2SJed Brown   stratum 1 contains the boundary of these "cells", often called "faces" or "facets".
3637647867b2SJed Brown 
3638552f7358SJed Brown   Level: developer
3639552f7358SJed Brown 
3640552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
3641552f7358SJed Brown @*/
36420adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
36430adebc6cSBarry Smith {
3644aa50250dSMatthew G. Knepley   DMLabel        label;
364563d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
3646552f7358SJed Brown   PetscErrorCode ierr;
3647552f7358SJed Brown 
3648552f7358SJed Brown   PetscFunctionBegin;
3649552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
365063d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
365163d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3652552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
36530d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
365463d1a920SMatthew G. Knepley   if (stratumValue < 0) {
365563d1a920SMatthew G. Knepley     if (start) *start = pStart;
365663d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
365763d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3658552f7358SJed Brown   }
3659aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
366013903a91SSatish Balay   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
366163d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
366263d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
3663552f7358SJed Brown   PetscFunctionReturn(0);
3664552f7358SJed Brown }
3665552f7358SJed Brown 
36660adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
36670adebc6cSBarry Smith {
3668efe440bfSMatthew G. Knepley   PetscSection   section, s;
3669efe440bfSMatthew G. Knepley   Mat            m;
36703e922f36SToby Isaac   PetscInt       maxHeight;
3671552f7358SJed Brown   PetscErrorCode ierr;
3672552f7358SJed Brown 
3673552f7358SJed Brown   PetscFunctionBegin;
367438221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
36753e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
36763e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
367782f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
367892fd8e1eSJed Brown   ierr = DMSetLocalSection(*cdm, section);CHKERRQ(ierr);
36791d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3680efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3681efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3682efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3683efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3684efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
36858f4c458bSMatthew G. Knepley 
36868f4c458bSMatthew G. Knepley   ierr = DMSetNumFields(*cdm, 1);CHKERRQ(ierr);
36878f4c458bSMatthew G. Knepley   ierr = DMCreateDS(*cdm);CHKERRQ(ierr);
3688552f7358SJed Brown   PetscFunctionReturn(0);
3689552f7358SJed Brown }
3690552f7358SJed Brown 
3691f19dbd58SToby Isaac PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
3692f19dbd58SToby Isaac {
3693f19dbd58SToby Isaac   Vec            coordsLocal;
3694f19dbd58SToby Isaac   DM             coordsDM;
3695f19dbd58SToby Isaac   PetscErrorCode ierr;
3696f19dbd58SToby Isaac 
3697f19dbd58SToby Isaac   PetscFunctionBegin;
3698f19dbd58SToby Isaac   *field = NULL;
3699f19dbd58SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coordsLocal);CHKERRQ(ierr);
3700f19dbd58SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordsDM);CHKERRQ(ierr);
3701f19dbd58SToby Isaac   if (coordsLocal && coordsDM) {
3702f19dbd58SToby Isaac     ierr = DMFieldCreateDS(coordsDM, 0, coordsLocal, field);CHKERRQ(ierr);
3703f19dbd58SToby Isaac   }
3704f19dbd58SToby Isaac   PetscFunctionReturn(0);
3705f19dbd58SToby Isaac }
3706f19dbd58SToby Isaac 
37077cd05799SMatthew G. Knepley /*@C
37087cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
37097cd05799SMatthew G. Knepley 
37107cd05799SMatthew G. Knepley   Not Collective
37117cd05799SMatthew G. Knepley 
37127cd05799SMatthew G. Knepley   Input Parameters:
37137cd05799SMatthew G. Knepley . dm        - The DMPlex object
37147cd05799SMatthew G. Knepley 
37157cd05799SMatthew G. Knepley   Output Parameter:
37167cd05799SMatthew G. Knepley . section - The PetscSection object
37177cd05799SMatthew G. Knepley 
37187cd05799SMatthew G. Knepley   Level: developer
37197cd05799SMatthew G. Knepley 
37207cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
37217cd05799SMatthew G. Knepley @*/
37220adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
37230adebc6cSBarry Smith {
3724552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3725552f7358SJed Brown 
3726552f7358SJed Brown   PetscFunctionBegin;
3727552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3728552f7358SJed Brown   if (section) *section = mesh->coneSection;
3729552f7358SJed Brown   PetscFunctionReturn(0);
3730552f7358SJed Brown }
3731552f7358SJed Brown 
37327cd05799SMatthew G. Knepley /*@C
37337cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
37347cd05799SMatthew G. Knepley 
37357cd05799SMatthew G. Knepley   Not Collective
37367cd05799SMatthew G. Knepley 
37377cd05799SMatthew G. Knepley   Input Parameters:
37387cd05799SMatthew G. Knepley . dm        - The DMPlex object
37397cd05799SMatthew G. Knepley 
37407cd05799SMatthew G. Knepley   Output Parameter:
37417cd05799SMatthew G. Knepley . section - The PetscSection object
37427cd05799SMatthew G. Knepley 
37437cd05799SMatthew G. Knepley   Level: developer
37447cd05799SMatthew G. Knepley 
37457cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
37467cd05799SMatthew G. Knepley @*/
37478cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
37488cb4d582SMatthew G. Knepley {
37498cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
37508cb4d582SMatthew G. Knepley 
37518cb4d582SMatthew G. Knepley   PetscFunctionBegin;
37528cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37538cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
37548cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
37558cb4d582SMatthew G. Knepley }
37568cb4d582SMatthew G. Knepley 
37577cd05799SMatthew G. Knepley /*@C
37587cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
37597cd05799SMatthew G. Knepley 
37607cd05799SMatthew G. Knepley   Not Collective
37617cd05799SMatthew G. Knepley 
37627cd05799SMatthew G. Knepley   Input Parameters:
37637cd05799SMatthew G. Knepley . dm        - The DMPlex object
37647cd05799SMatthew G. Knepley 
37657cd05799SMatthew G. Knepley   Output Parameter:
37667cd05799SMatthew G. Knepley . cones - The cone for each point
37677cd05799SMatthew G. Knepley 
37687cd05799SMatthew G. Knepley   Level: developer
37697cd05799SMatthew G. Knepley 
37707cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
37717cd05799SMatthew G. Knepley @*/
3772a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3773a6dfd86eSKarl Rupp {
3774552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3775552f7358SJed Brown 
3776552f7358SJed Brown   PetscFunctionBegin;
3777552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3778552f7358SJed Brown   if (cones) *cones = mesh->cones;
3779552f7358SJed Brown   PetscFunctionReturn(0);
3780552f7358SJed Brown }
3781552f7358SJed Brown 
37827cd05799SMatthew G. Knepley /*@C
37837cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
37847cd05799SMatthew G. Knepley 
37857cd05799SMatthew G. Knepley   Not Collective
37867cd05799SMatthew G. Knepley 
37877cd05799SMatthew G. Knepley   Input Parameters:
37887cd05799SMatthew G. Knepley . dm        - The DMPlex object
37897cd05799SMatthew G. Knepley 
37907cd05799SMatthew G. Knepley   Output Parameter:
37917cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
37927cd05799SMatthew G. Knepley 
37937cd05799SMatthew G. Knepley   Level: developer
37947cd05799SMatthew G. Knepley 
37957cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
37967cd05799SMatthew G. Knepley @*/
3797a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3798a6dfd86eSKarl Rupp {
3799552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3800552f7358SJed Brown 
3801552f7358SJed Brown   PetscFunctionBegin;
3802552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3803552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3804552f7358SJed Brown   PetscFunctionReturn(0);
3805552f7358SJed Brown }
3806552f7358SJed Brown 
3807552f7358SJed Brown /******************************** FEM Support **********************************/
3808552f7358SJed Brown 
38099e8305c2SJed Brown /*
38109e8305c2SJed Brown  Returns number of components and tensor degree for the field.  For interpolated meshes, line should be a point
38119e8305c2SJed Brown  representing a line in the section.
38129e8305c2SJed Brown */
38139e8305c2SJed Brown static PetscErrorCode PetscSectionFieldGetTensorDegree_Private(PetscSection section,PetscInt field,PetscInt line,PetscBool vertexchart,PetscInt *Nc,PetscInt *k)
38149e8305c2SJed Brown {
38159e8305c2SJed Brown   PetscErrorCode ierr;
38169e8305c2SJed Brown 
38179e8305c2SJed Brown   PetscFunctionBeginHot;
38189e8305c2SJed Brown   ierr = PetscSectionGetFieldComponents(section, field, Nc);CHKERRQ(ierr);
38199e8305c2SJed Brown   if (vertexchart) {            /* If we only have a vertex chart, we must have degree k=1 */
38209e8305c2SJed Brown     *k = 1;
38219e8305c2SJed Brown   } else {                      /* Assume the full interpolated mesh is in the chart; lines in particular */
38229e8305c2SJed Brown     /* An order k SEM disc has k-1 dofs on an edge */
38239e8305c2SJed Brown     ierr = PetscSectionGetFieldDof(section, line, field, k);CHKERRQ(ierr);
38249e8305c2SJed Brown     *k = *k / *Nc + 1;
38259e8305c2SJed Brown   }
38269e8305c2SJed Brown   PetscFunctionReturn(0);
38279e8305c2SJed Brown }
38289e8305c2SJed Brown 
3829a4355906SMatthew Knepley /*@
3830bc1eb3faSJed Brown 
3831bc1eb3faSJed Brown   DMPlexSetClosurePermutationTensor - Create a permutation from the default (BFS) point ordering in the closure, to a
3832bc1eb3faSJed Brown   lexicographic ordering over the tensor product cell (i.e., line, quad, hex, etc.), and set this permutation in the
38331bb6d2a8SBarry Smith   section provided (or the section of the DM).
3834a4355906SMatthew Knepley 
3835a4355906SMatthew Knepley   Input Parameters:
3836a4355906SMatthew Knepley + dm      - The DM
3837a4355906SMatthew Knepley . point   - Either a cell (highest dim point) or an edge (dim 1 point), or PETSC_DETERMINE
3838a4355906SMatthew Knepley - section - The PetscSection to reorder, or NULL for the default section
3839a4355906SMatthew Knepley 
3840a4355906SMatthew Knepley   Note: The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
3841a4355906SMatthew Knepley   degree of the basis.
3842a4355906SMatthew Knepley 
3843bc1eb3faSJed Brown   Example:
3844bc1eb3faSJed Brown   A typical interpolated single-quad mesh might order points as
3845bc1eb3faSJed Brown .vb
3846bc1eb3faSJed Brown   [c0, v1, v2, v3, v4, e5, e6, e7, e8]
3847bc1eb3faSJed Brown 
3848bc1eb3faSJed Brown   v4 -- e6 -- v3
3849bc1eb3faSJed Brown   |           |
3850bc1eb3faSJed Brown   e7    c0    e8
3851bc1eb3faSJed Brown   |           |
3852bc1eb3faSJed Brown   v1 -- e5 -- v2
3853bc1eb3faSJed Brown .ve
3854bc1eb3faSJed Brown 
3855bc1eb3faSJed Brown   (There is no significance to the ordering described here.)  The default section for a Q3 quad might typically assign
3856bc1eb3faSJed Brown   dofs in the order of points, e.g.,
3857bc1eb3faSJed Brown .vb
3858bc1eb3faSJed Brown     c0 -> [0,1,2,3]
3859bc1eb3faSJed Brown     v1 -> [4]
3860bc1eb3faSJed Brown     ...
3861bc1eb3faSJed Brown     e5 -> [8, 9]
3862bc1eb3faSJed Brown .ve
3863bc1eb3faSJed Brown 
3864bc1eb3faSJed Brown   which corresponds to the dofs
3865bc1eb3faSJed Brown .vb
3866bc1eb3faSJed Brown     6   10  11  7
3867bc1eb3faSJed Brown     13  2   3   15
3868bc1eb3faSJed Brown     12  0   1   14
3869bc1eb3faSJed Brown     4   8   9   5
3870bc1eb3faSJed Brown .ve
3871bc1eb3faSJed Brown 
3872bc1eb3faSJed Brown   The closure in BFS ordering works through height strata (cells, edges, vertices) to produce the ordering
3873bc1eb3faSJed Brown .vb
3874bc1eb3faSJed Brown   0 1 2 3 8 9 14 15 11 10 13 12 4 5 7 6
3875bc1eb3faSJed Brown .ve
3876bc1eb3faSJed Brown 
3877bc1eb3faSJed Brown   After calling DMPlexSetClosurePermutationTensor(), the closure will be ordered lexicographically,
3878bc1eb3faSJed Brown .vb
3879bc1eb3faSJed Brown    4 8 9 5 12 0 1 14 13 2 3 15 6 10 11 7
3880bc1eb3faSJed Brown .ve
3881bc1eb3faSJed Brown 
3882a4355906SMatthew Knepley   Level: developer
3883a4355906SMatthew Knepley 
38841bb6d2a8SBarry Smith .seealso: DMGetLocalSection(), PetscSectionSetClosurePermutation(), DMSetGlocalSection()
3885a4355906SMatthew Knepley @*/
3886bc1eb3faSJed Brown PetscErrorCode DMPlexSetClosurePermutationTensor(DM dm, PetscInt point, PetscSection section)
38873194fc30SMatthew G. Knepley {
38887391a63aSMatthew G. Knepley   DMLabel        label;
38893194fc30SMatthew G. Knepley   PetscInt      *perm;
38907391a63aSMatthew G. Knepley   PetscInt       dim, depth, eStart, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
38919e8305c2SJed Brown   PetscBool      vertexchart;
38923194fc30SMatthew G. Knepley   PetscErrorCode ierr;
38933194fc30SMatthew G. Knepley 
38943194fc30SMatthew G. Knepley   PetscFunctionBegin;
38957391a63aSMatthew G. Knepley   if (point < 0) {ierr = DMPlexGetDepthStratum(dm, 1, &point, NULL);CHKERRQ(ierr);}
38963194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
38977391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
38987391a63aSMatthew G. Knepley   ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr);
38997391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
39007391a63aSMatthew G. Knepley   else if  (depth == dim) {
39017391a63aSMatthew G. Knepley     const PetscInt *cone;
39027391a63aSMatthew G. Knepley 
39037391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
3904d4e6627bSStefano Zampini     if (dim == 2) eStart = cone[0];
3905d4e6627bSStefano Zampini     else if (dim == 3) {
3906d4e6627bSStefano Zampini       const PetscInt *cone2;
3907d4e6627bSStefano Zampini       ierr = DMPlexGetCone(dm, cone[0], &cone2);CHKERRQ(ierr);
3908d4e6627bSStefano Zampini       eStart = cone2[0];
3909d4e6627bSStefano Zampini     } else SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Point %D of depth %D cannot be used to bootstrap spectral ordering for dim %D", point, depth, dim);
3910d4e6627bSStefano Zampini   } else SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Point %D of depth %D cannot be used to bootstrap spectral ordering for dim %D", point, depth, dim);
391192fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
39129e8305c2SJed Brown   {                             /* Determine whether the chart covers all points or just vertices. */
39139e8305c2SJed Brown     PetscInt pStart,pEnd,cStart,cEnd;
39149e8305c2SJed Brown     ierr = DMPlexGetDepthStratum(dm,0,&pStart,&pEnd);CHKERRQ(ierr);
39159e8305c2SJed Brown     ierr = PetscSectionGetChart(section,&cStart,&cEnd);CHKERRQ(ierr);
39169e8305c2SJed Brown     if (pStart == cStart && pEnd == cEnd) vertexchart = PETSC_TRUE; /* Just vertices */
39179e8305c2SJed Brown     else vertexchart = PETSC_FALSE;                                 /* Assume all interpolated points are in chart */
39189e8305c2SJed Brown   }
39193194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
3920babf31e0SJed Brown   if (dim < 1) PetscFunctionReturn(0);
39213194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
39229e8305c2SJed Brown     ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
392389eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
39243194fc30SMatthew G. Knepley   }
39253194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
39263194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
392789eabcffSMatthew G. Knepley     switch (dim) {
3928babf31e0SJed Brown     case 1:
39299e8305c2SJed Brown       ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
3930babf31e0SJed Brown       /*
3931babf31e0SJed Brown         Original ordering is [ edge of length k-1; vtx0; vtx1 ]
3932babf31e0SJed Brown         We want              [ vtx0; edge of length k-1; vtx1 ]
3933babf31e0SJed Brown       */
3934babf31e0SJed Brown       for (c=0; c<Nc; c++,offset++) perm[offset] = (k-1)*Nc + c + foffset;
3935babf31e0SJed Brown       for (i=0; i<k-1; i++) for (c=0; c<Nc; c++,offset++) perm[offset] = i*Nc + c + foffset;
3936babf31e0SJed Brown       for (c=0; c<Nc; c++,offset++) perm[offset] = k*Nc + c + foffset;
3937babf31e0SJed Brown       foffset = offset;
3938babf31e0SJed Brown       break;
393989eabcffSMatthew G. Knepley     case 2:
39403194fc30SMatthew 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} */
39419e8305c2SJed Brown       ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
39423194fc30SMatthew G. Knepley       /* The SEM order is
39433194fc30SMatthew G. Knepley 
39443194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
394589eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
39463194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
39473194fc30SMatthew G. Knepley       */
39483194fc30SMatthew G. Knepley       {
39493194fc30SMatthew G. Knepley         const PetscInt of   = 0;
39503194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
39513194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
39523194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
39533194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
39543194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
39553194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
39563194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
39573194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
39583194fc30SMatthew G. Knepley         PetscInt       o;
39593194fc30SMatthew G. Knepley 
39603194fc30SMatthew G. Knepley         /* bottom */
39613194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
39623194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
39633194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
39643194fc30SMatthew G. Knepley         /* middle */
39653194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
39663194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
39673194fc30SMatthew 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;
39683194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
39693194fc30SMatthew G. Knepley         }
39703194fc30SMatthew G. Knepley         /* top */
39713194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
39723194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
39733194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
39743194fc30SMatthew G. Knepley         foffset = offset;
39753194fc30SMatthew G. Knepley       }
397689eabcffSMatthew G. Knepley       break;
397789eabcffSMatthew G. Knepley     case 3:
397889eabcffSMatthew G. Knepley       /* The original hex closure is
397989eabcffSMatthew G. Knepley 
398089eabcffSMatthew G. Knepley          {c,
398189eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
398289eabcffSMatthew 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,
398389eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
398489eabcffSMatthew G. Knepley       */
39859e8305c2SJed Brown       ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
398689eabcffSMatthew G. Knepley       /* The SEM order is
398789eabcffSMatthew G. Knepley          Bottom Slice
398889eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
398989eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
399089eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
399189eabcffSMatthew G. Knepley 
399289eabcffSMatthew G. Knepley          Middle Slice (j)
399389eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
399489eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
399589eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
399689eabcffSMatthew G. Knepley 
399789eabcffSMatthew G. Knepley          Top Slice
399889eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
399989eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
400089eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
400189eabcffSMatthew G. Knepley       */
400289eabcffSMatthew G. Knepley       {
400389eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
400489eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
400589eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
400689eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
400789eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
400889eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
400989eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
401089eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
401189eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
401289eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
401389eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
401489eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
401589eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
401689eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
401789eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
401889eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
401989eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
402089eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
402189eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
402289eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
402389eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
402489eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
402589eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
402689eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
402789eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
402889eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
402989eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
403089eabcffSMatthew G. Knepley         PetscInt       o, n;
403189eabcffSMatthew G. Knepley 
403289eabcffSMatthew G. Knepley         /* Bottom Slice */
403389eabcffSMatthew G. Knepley         /*   bottom */
403489eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
403589eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
403689eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
403789eabcffSMatthew G. Knepley         /*   middle */
403889eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
403989eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
4040316b7f87SMax 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;}
404189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
40423194fc30SMatthew G. Knepley         }
404389eabcffSMatthew G. Knepley         /*   top */
404489eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
404589eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
404689eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
404789eabcffSMatthew G. Knepley 
404889eabcffSMatthew G. Knepley         /* Middle Slice */
404989eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
405089eabcffSMatthew G. Knepley           /*   bottom */
405189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
405289eabcffSMatthew 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;
405389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
405489eabcffSMatthew G. Knepley           /*   middle */
405589eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
405689eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
405789eabcffSMatthew 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;
405889eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
405989eabcffSMatthew G. Knepley           }
406089eabcffSMatthew G. Knepley           /*   top */
406189eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
406289eabcffSMatthew 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;
406389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
406489eabcffSMatthew G. Knepley         }
406589eabcffSMatthew G. Knepley 
406689eabcffSMatthew G. Knepley         /* Top Slice */
406789eabcffSMatthew G. Knepley         /*   bottom */
406889eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
406989eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
407089eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
407189eabcffSMatthew G. Knepley         /*   middle */
407289eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
407389eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
407489eabcffSMatthew 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;
407589eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
407689eabcffSMatthew G. Knepley         }
407789eabcffSMatthew G. Knepley         /*   top */
407889eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
407989eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
408089eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
408189eabcffSMatthew G. Knepley 
408289eabcffSMatthew G. Knepley         foffset = offset;
408389eabcffSMatthew G. Knepley       }
408489eabcffSMatthew G. Knepley       break;
408589eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
408689eabcffSMatthew G. Knepley     }
408789eabcffSMatthew G. Knepley   }
408889eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
40893194fc30SMatthew G. Knepley   /* Check permutation */
40903194fc30SMatthew G. Knepley   {
40913194fc30SMatthew G. Knepley     PetscInt *check;
40923194fc30SMatthew G. Knepley 
40933194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
40943194fc30SMatthew 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]);}
40953194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
40963194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
40973194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
40983194fc30SMatthew G. Knepley   }
40991fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
41003194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
41013194fc30SMatthew G. Knepley }
41023194fc30SMatthew G. Knepley 
4103e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
4104e071409bSToby Isaac {
4105e071409bSToby Isaac   PetscDS        prob;
4106e071409bSToby Isaac   PetscInt       depth, Nf, h;
4107e071409bSToby Isaac   DMLabel        label;
4108e071409bSToby Isaac   PetscErrorCode ierr;
4109e071409bSToby Isaac 
4110e071409bSToby Isaac   PetscFunctionBeginHot;
4111e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
4112e071409bSToby Isaac   Nf      = prob->Nf;
4113e071409bSToby Isaac   label   = dm->depthLabel;
4114e071409bSToby Isaac   *dspace = NULL;
4115e071409bSToby Isaac   if (field < Nf) {
4116e071409bSToby Isaac     PetscObject disc = prob->disc[field];
4117e071409bSToby Isaac 
4118e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
4119e071409bSToby Isaac       PetscDualSpace dsp;
4120e071409bSToby Isaac 
4121e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
4122e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
4123e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
4124e071409bSToby Isaac       h    = depth - 1 - h;
4125e071409bSToby Isaac       if (h) {
4126e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
4127e071409bSToby Isaac       } else {
4128e071409bSToby Isaac         *dspace = dsp;
4129e071409bSToby Isaac       }
4130e071409bSToby Isaac     }
4131e071409bSToby Isaac   }
4132e071409bSToby Isaac   PetscFunctionReturn(0);
4133e071409bSToby Isaac }
4134e071409bSToby Isaac 
4135e071409bSToby Isaac 
41361a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4137a6dfd86eSKarl Rupp {
4138552f7358SJed Brown   PetscScalar    *array, *vArray;
4139d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
41401a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
4141552f7358SJed Brown   PetscErrorCode  ierr;
4142552f7358SJed Brown 
41431b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
41442a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
41455a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
41465a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
41475a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
41483f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
41499df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
41509df71ca4SMatthew G. Knepley       PetscInt dof;
4151d9917b9dSMatthew G. Knepley 
41529df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
41539df71ca4SMatthew G. Knepley       size += dof;
41549df71ca4SMatthew G. Knepley     }
41559df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
41569df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
41572a3aaacfSMatthew G. Knepley       PetscInt       dof;
41585a1bb5cfSMatthew G. Knepley 
41595a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
41602a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
41615a1bb5cfSMatthew G. Knepley       size += dof;
41625a1bb5cfSMatthew G. Knepley     }
41633f7cbbe7SMatthew G. Knepley     if (!values) {
41643f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
41653f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
41663f7cbbe7SMatthew G. Knepley     }
416769291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
4168982e9ed1SMatthew G. Knepley   } else {
4169982e9ed1SMatthew G. Knepley     array = *values;
4170982e9ed1SMatthew G. Knepley   }
41719df71ca4SMatthew G. Knepley   size = 0;
41725a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
41739df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
41749df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
41759df71ca4SMatthew G. Knepley     PetscScalar *varr;
4176d9917b9dSMatthew G. Knepley 
41779df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
41789df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
41799df71ca4SMatthew G. Knepley     varr = &vArray[off];
41801a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
41811a271a75SMatthew G. Knepley       array[offset] = varr[d];
41829df71ca4SMatthew G. Knepley     }
41839df71ca4SMatthew G. Knepley     size += dof;
41849df71ca4SMatthew G. Knepley   }
41859df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
41869df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
41879df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
41885a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
41895a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
41905a1bb5cfSMatthew G. Knepley 
419152ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
41925a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
41935a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
41945a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
41955a1bb5cfSMatthew G. Knepley     if (o >= 0) {
41961a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
41971a271a75SMatthew G. Knepley         array[offset] = varr[d];
41985a1bb5cfSMatthew G. Knepley       }
41995a1bb5cfSMatthew G. Knepley     } else {
42001a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
42011a271a75SMatthew G. Knepley         array[offset] = varr[d];
42025a1bb5cfSMatthew G. Knepley       }
42035a1bb5cfSMatthew G. Knepley     }
42049df71ca4SMatthew G. Knepley     size += dof;
42055a1bb5cfSMatthew G. Knepley   }
42065a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
42079df71ca4SMatthew G. Knepley   if (!*values) {
42085a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
42095a1bb5cfSMatthew G. Knepley     *values = array;
42109df71ca4SMatthew G. Knepley   } else {
42118ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
42128c312ff3SMatthew G. Knepley     *csize = size;
42139df71ca4SMatthew G. Knepley   }
42145a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
42155a1bb5cfSMatthew G. Knepley }
4216d9917b9dSMatthew G. Knepley 
42171dc59d61SMatthew G. Knepley PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4218923c78e0SToby Isaac {
4219923c78e0SToby Isaac   const PetscInt *cla;
4220923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
4221923c78e0SToby Isaac   PetscErrorCode ierr;
4222923c78e0SToby Isaac 
4223923c78e0SToby Isaac   PetscFunctionBeginHot;
4224923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
4225923c78e0SToby Isaac   if (!*clPoints) {
4226923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
4227923c78e0SToby Isaac 
4228923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4229923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
4230923c78e0SToby Isaac     /* Compress out points not in the section */
4231923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
4232923c78e0SToby Isaac       PetscInt r = pts[2*p];
4233923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
4234923c78e0SToby Isaac         pts[q*2]   = r;
4235923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
4236923c78e0SToby Isaac         ++q;
4237923c78e0SToby Isaac       }
4238923c78e0SToby Isaac     }
4239923c78e0SToby Isaac     np = q;
4240923c78e0SToby Isaac     cla = NULL;
4241923c78e0SToby Isaac   } else {
4242923c78e0SToby Isaac     PetscInt dof, off;
4243923c78e0SToby Isaac 
4244923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
4245923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
4246923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
4247923c78e0SToby Isaac     np   = dof/2;
4248923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
4249923c78e0SToby Isaac   }
4250923c78e0SToby Isaac   *numPoints = np;
4251923c78e0SToby Isaac   *points    = pts;
4252923c78e0SToby Isaac   *clp       = cla;
4253923c78e0SToby Isaac 
4254923c78e0SToby Isaac   PetscFunctionReturn(0);
4255923c78e0SToby Isaac }
4256923c78e0SToby Isaac 
42571dc59d61SMatthew G. Knepley PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4258923c78e0SToby Isaac {
4259923c78e0SToby Isaac   PetscErrorCode ierr;
4260923c78e0SToby Isaac 
4261923c78e0SToby Isaac   PetscFunctionBeginHot;
4262923c78e0SToby Isaac   if (!*clPoints) {
4263923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
4264923c78e0SToby Isaac   } else {
4265923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
4266923c78e0SToby Isaac   }
4267923c78e0SToby Isaac   *numPoints = 0;
4268923c78e0SToby Isaac   *points    = NULL;
4269923c78e0SToby Isaac   *clSec     = NULL;
4270923c78e0SToby Isaac   *clPoints  = NULL;
4271923c78e0SToby Isaac   *clp       = NULL;
4272923c78e0SToby Isaac   PetscFunctionReturn(0);
4273923c78e0SToby Isaac }
4274923c78e0SToby Isaac 
427597e99dd9SToby 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[])
42761a271a75SMatthew G. Knepley {
42771a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
427897e99dd9SToby Isaac   const PetscInt    **perms = NULL;
427997e99dd9SToby Isaac   const PetscScalar **flips = NULL;
42801a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
42811a271a75SMatthew G. Knepley 
42821a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4283fe02ba77SJed Brown   *size = 0;
428497e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
428597e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
428697e99dd9SToby Isaac     const PetscInt    point = points[2*p];
428797e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
428897e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
42891a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
42901a271a75SMatthew G. Knepley     const PetscScalar *varr;
42911a271a75SMatthew G. Knepley 
42921a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
42931a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
42941a271a75SMatthew G. Knepley     varr = &vArray[off];
429597e99dd9SToby Isaac     if (clperm) {
429697e99dd9SToby Isaac       if (perm) {
429797e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
42981a271a75SMatthew G. Knepley       } else {
429997e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
430097e99dd9SToby Isaac       }
430197e99dd9SToby Isaac       if (flip) {
430297e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
430397e99dd9SToby Isaac       }
430497e99dd9SToby Isaac     } else {
430597e99dd9SToby Isaac       if (perm) {
430697e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
430797e99dd9SToby Isaac       } else {
430897e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
430997e99dd9SToby Isaac       }
431097e99dd9SToby Isaac       if (flip) {
431197e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
43121a271a75SMatthew G. Knepley       }
43131a271a75SMatthew G. Knepley     }
431497e99dd9SToby Isaac     offset += dof;
431597e99dd9SToby Isaac   }
431697e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
43171a271a75SMatthew G. Knepley   *size = offset;
43181a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
43191a271a75SMatthew G. Knepley }
43201a271a75SMatthew G. Knepley 
432197e99dd9SToby 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[])
43221a271a75SMatthew G. Knepley {
43231a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
43241a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
43251a271a75SMatthew G. Knepley 
43261a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4327fe02ba77SJed Brown   *size = 0;
43281a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
432997e99dd9SToby Isaac     PetscInt          p;
433097e99dd9SToby Isaac     const PetscInt    **perms = NULL;
433197e99dd9SToby Isaac     const PetscScalar **flips = NULL;
43321a271a75SMatthew G. Knepley 
433397e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
433497e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
433597e99dd9SToby Isaac       const PetscInt    point = points[2*p];
433697e99dd9SToby Isaac       PetscInt          fdof, foff, b;
43371a271a75SMatthew G. Knepley       const PetscScalar *varr;
433897e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
433997e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
43401a271a75SMatthew G. Knepley 
43411a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
43421a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
43431a271a75SMatthew G. Knepley       varr = &vArray[foff];
434497e99dd9SToby Isaac       if (clperm) {
434597e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
434697e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
434797e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
43481a271a75SMatthew G. Knepley       } else {
434997e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
435097e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
435197e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
43521a271a75SMatthew G. Knepley       }
435397e99dd9SToby Isaac       offset += fdof;
43541a271a75SMatthew G. Knepley     }
435597e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
43561a271a75SMatthew G. Knepley   }
43571a271a75SMatthew G. Knepley   *size = offset;
43581a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
43591a271a75SMatthew G. Knepley }
43601a271a75SMatthew G. Knepley 
4361552f7358SJed Brown /*@C
4362552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
4363552f7358SJed Brown 
4364552f7358SJed Brown   Not collective
4365552f7358SJed Brown 
4366552f7358SJed Brown   Input Parameters:
4367552f7358SJed Brown + dm - The DM
4368552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
4369552f7358SJed Brown . v - The local vector
437022c1ee49SMatthew G. Knepley . point - The point in the DM
437122c1ee49SMatthew G. Knepley . csize - The size of the input values array, or NULL
437222c1ee49SMatthew G. Knepley - values - An array to use for the values, or NULL to have it allocated automatically
4373552f7358SJed Brown 
4374552f7358SJed Brown   Output Parameters:
437522c1ee49SMatthew G. Knepley + csize - The number of values in the closure
437622c1ee49SMatthew G. Knepley - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed
437722c1ee49SMatthew G. Knepley 
437822c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
437922c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
438022c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
438122c1ee49SMatthew G. Knepley $
438222c1ee49SMatthew G. Knepley $ A typical use could be
438322c1ee49SMatthew G. Knepley $
438422c1ee49SMatthew G. Knepley $  values = NULL;
438522c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
438622c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
438722c1ee49SMatthew G. Knepley $    <Compute on closure>
438822c1ee49SMatthew G. Knepley $  }
438922c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
439022c1ee49SMatthew G. Knepley $
439122c1ee49SMatthew G. Knepley $ or
439222c1ee49SMatthew G. Knepley $
439322c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
439422c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
439522c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
439622c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
439722c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
439822c1ee49SMatthew G. Knepley $      <Compute on closure>
439922c1ee49SMatthew G. Knepley $    }
440022c1ee49SMatthew G. Knepley $  }
440122c1ee49SMatthew G. Knepley $  PetscFree(values);
4402552f7358SJed Brown 
4403552f7358SJed Brown   Fortran Notes:
4404552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
4405552f7358SJed Brown   include petsc.h90 in your code.
4406552f7358SJed Brown 
4407552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
4408552f7358SJed Brown 
4409552f7358SJed Brown   Level: intermediate
4410552f7358SJed Brown 
4411552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4412552f7358SJed Brown @*/
4413552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4414552f7358SJed Brown {
4415552f7358SJed Brown   PetscSection       clSection;
4416d9917b9dSMatthew G. Knepley   IS                 clPoints;
44178a84db2dSMatthew G. Knepley   PetscScalar       *array;
44188a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
4419552f7358SJed Brown   PetscInt          *points = NULL;
44208f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
44211a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
4422552f7358SJed Brown   PetscErrorCode     ierr;
4423552f7358SJed Brown 
4424d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
4425552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
442692fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
44271a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44281a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4429552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4430552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4431552f7358SJed Brown   if (depth == 1 && numFields < 2) {
44321a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
4433552f7358SJed Brown     PetscFunctionReturn(0);
4434552f7358SJed Brown   }
44351a271a75SMatthew G. Knepley   /* Get points */
4436923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
44371fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
44381a271a75SMatthew G. Knepley   /* Get array */
4439bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
44401a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
4441552f7358SJed Brown 
44421a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
4443552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
44441a271a75SMatthew G. Knepley       asize += dof;
4445552f7358SJed Brown     }
4446bd6c0d32SMatthew G. Knepley     if (!values) {
4447923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
44481a271a75SMatthew G. Knepley       if (csize) *csize = asize;
4449bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
4450bd6c0d32SMatthew G. Knepley     }
445169291d52SBarry Smith     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
4452d0f6b257SMatthew G. Knepley   } else {
4453d0f6b257SMatthew G. Knepley     array = *values;
4454d0f6b257SMatthew G. Knepley   }
44558a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
44561a271a75SMatthew G. Knepley   /* Get values */
445797e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
445897e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
44591a271a75SMatthew G. Knepley   /* Cleanup points */
4460923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
44611a271a75SMatthew G. Knepley   /* Cleanup array */
44628a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
4463d0f6b257SMatthew G. Knepley   if (!*values) {
4464552f7358SJed Brown     if (csize) *csize = size;
4465552f7358SJed Brown     *values = array;
4466d0f6b257SMatthew G. Knepley   } else {
4467f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4468d0f6b257SMatthew G. Knepley     *csize = size;
4469d0f6b257SMatthew G. Knepley   }
4470552f7358SJed Brown   PetscFunctionReturn(0);
4471552f7358SJed Brown }
4472552f7358SJed Brown 
4473552f7358SJed Brown /*@C
4474552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
4475552f7358SJed Brown 
4476552f7358SJed Brown   Not collective
4477552f7358SJed Brown 
4478552f7358SJed Brown   Input Parameters:
4479552f7358SJed Brown + dm - The DM
44800298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
4481552f7358SJed Brown . v - The local vector
4482eaf898f9SPatrick Sanan . point - The point in the DM
44830298fd71SBarry Smith . csize - The number of values in the closure, or NULL
4484552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
4485552f7358SJed Brown 
448622c1ee49SMatthew G. Knepley   Note that the array values are discarded and not copied back into v. In order to copy values back to v, use DMPlexVecSetClosure()
448722c1ee49SMatthew G. Knepley 
44883813dfbdSMatthew G Knepley   Fortran Notes:
44893813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
44903813dfbdSMatthew G Knepley   include petsc.h90 in your code.
44913813dfbdSMatthew G Knepley 
44923813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
44933813dfbdSMatthew G Knepley 
4494552f7358SJed Brown   Level: intermediate
4495552f7358SJed Brown 
4496552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4497552f7358SJed Brown @*/
44987c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4499a6dfd86eSKarl Rupp {
4500552f7358SJed Brown   PetscInt       size = 0;
4501552f7358SJed Brown   PetscErrorCode ierr;
4502552f7358SJed Brown 
4503552f7358SJed Brown   PetscFunctionBegin;
4504552f7358SJed Brown   /* Should work without recalculating size */
450569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
4506c9fdaa05SMatthew G. Knepley   *values = NULL;
4507552f7358SJed Brown   PetscFunctionReturn(0);
4508552f7358SJed Brown }
4509552f7358SJed Brown 
4510552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
4511552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
4512552f7358SJed Brown 
451397e99dd9SToby 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[])
4514552f7358SJed Brown {
4515552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4516552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4517552f7358SJed Brown   PetscScalar    *a;
4518552f7358SJed Brown   PetscInt        off, cind = 0, k;
4519552f7358SJed Brown   PetscErrorCode  ierr;
4520552f7358SJed Brown 
4521552f7358SJed Brown   PetscFunctionBegin;
4522552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4523552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4524552f7358SJed Brown   a    = &array[off];
4525552f7358SJed Brown   if (!cdof || setBC) {
452697e99dd9SToby Isaac     if (clperm) {
452797e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
452897e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
4529552f7358SJed Brown     } else {
453097e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
453197e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4532552f7358SJed Brown     }
4533552f7358SJed Brown   } else {
4534552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
453597e99dd9SToby Isaac     if (clperm) {
453697e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
4537552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
453897e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4539552f7358SJed Brown         }
4540552f7358SJed Brown       } else {
4541552f7358SJed Brown         for (k = 0; k < dof; ++k) {
4542552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
454397e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
454497e99dd9SToby Isaac         }
454597e99dd9SToby Isaac       }
454697e99dd9SToby Isaac     } else {
454797e99dd9SToby Isaac       if (perm) {
454897e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
454997e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
455097e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
455197e99dd9SToby Isaac         }
455297e99dd9SToby Isaac       } else {
455397e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
455497e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
455597e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
455697e99dd9SToby Isaac         }
4557552f7358SJed Brown       }
4558552f7358SJed Brown     }
4559552f7358SJed Brown   }
4560552f7358SJed Brown   PetscFunctionReturn(0);
4561552f7358SJed Brown }
4562552f7358SJed Brown 
456397e99dd9SToby 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[])
4564a5e93ea8SMatthew G. Knepley {
4565a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
4566a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4567a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
4568a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
4569a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4570a5e93ea8SMatthew G. Knepley 
4571a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4572a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4573a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4574a5e93ea8SMatthew G. Knepley   a    = &array[off];
4575a5e93ea8SMatthew G. Knepley   if (cdof) {
4576a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
457797e99dd9SToby Isaac     if (clperm) {
457897e99dd9SToby Isaac       if (perm) {
4579a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4580a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
458197e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
458297e99dd9SToby Isaac             cind++;
4583a5e93ea8SMatthew G. Knepley           }
4584a5e93ea8SMatthew G. Knepley         }
4585a5e93ea8SMatthew G. Knepley       } else {
4586a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4587a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
458897e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
458997e99dd9SToby Isaac             cind++;
459097e99dd9SToby Isaac           }
459197e99dd9SToby Isaac         }
459297e99dd9SToby Isaac       }
459397e99dd9SToby Isaac     } else {
459497e99dd9SToby Isaac       if (perm) {
459597e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
459697e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
459797e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
459897e99dd9SToby Isaac             cind++;
459997e99dd9SToby Isaac           }
460097e99dd9SToby Isaac         }
460197e99dd9SToby Isaac       } else {
460297e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
460397e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
460497e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
460597e99dd9SToby Isaac             cind++;
460697e99dd9SToby Isaac           }
4607a5e93ea8SMatthew G. Knepley         }
4608a5e93ea8SMatthew G. Knepley       }
4609a5e93ea8SMatthew G. Knepley     }
4610a5e93ea8SMatthew G. Knepley   }
4611a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4612a5e93ea8SMatthew G. Knepley }
4613a5e93ea8SMatthew G. Knepley 
461497e99dd9SToby 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[])
4615a6dfd86eSKarl Rupp {
4616552f7358SJed Brown   PetscScalar    *a;
46171a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
46181a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
461997e99dd9SToby Isaac   PetscInt        cind = 0, b;
4620552f7358SJed Brown   PetscErrorCode  ierr;
4621552f7358SJed Brown 
4622552f7358SJed Brown   PetscFunctionBegin;
4623552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4624552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
46251a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
46261a271a75SMatthew G. Knepley   a    = &array[foff];
4627552f7358SJed Brown   if (!fcdof || setBC) {
462897e99dd9SToby Isaac     if (clperm) {
462997e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
463097e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4631552f7358SJed Brown     } else {
463297e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
463397e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4634552f7358SJed Brown     }
4635552f7358SJed Brown   } else {
4636552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
463797e99dd9SToby Isaac     if (clperm) {
463897e99dd9SToby Isaac       if (perm) {
463997e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
464097e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
464197e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4642552f7358SJed Brown         }
4643552f7358SJed Brown       } else {
464497e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
464597e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
464697e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
464797e99dd9SToby Isaac         }
464897e99dd9SToby Isaac       }
464997e99dd9SToby Isaac     } else {
465097e99dd9SToby Isaac       if (perm) {
465197e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
465297e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
465397e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
465497e99dd9SToby Isaac         }
465597e99dd9SToby Isaac       } else {
465697e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
465797e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
465897e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4659552f7358SJed Brown         }
4660552f7358SJed Brown       }
4661552f7358SJed Brown     }
4662552f7358SJed Brown   }
46631a271a75SMatthew G. Knepley   *offset += fdof;
4664552f7358SJed Brown   PetscFunctionReturn(0);
4665552f7358SJed Brown }
4666552f7358SJed Brown 
4667ba322698SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode updatePointFieldsBC_private(PetscSection section, PetscInt point, const PetscInt perm[], const PetscScalar flip[], PetscInt f, PetscInt Ncc, const PetscInt comps[], void (*fuse)(PetscScalar*, PetscScalar), const PetscInt clperm[], const PetscScalar values[], PetscInt *offset, PetscScalar array[])
4668a5e93ea8SMatthew G. Knepley {
4669a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
46701a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
46711a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
4672ba322698SMatthew G. Knepley   PetscInt        cind = 0, ncind = 0, b;
4673ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
4674a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4675a5e93ea8SMatthew G. Knepley 
4676a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4677a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4678a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
46791a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
46801a271a75SMatthew G. Knepley   a    = &array[foff];
4681a5e93ea8SMatthew G. Knepley   if (fcdof) {
4682ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
4683a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
468497e99dd9SToby Isaac     if (clperm) {
468597e99dd9SToby Isaac       if (perm) {
4686ba322698SMatthew G. Knepley         if (comps) {
4687ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4688ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4689ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4690ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4691ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
4692ba322698SMatthew G. Knepley           }
4693ba322698SMatthew G. Knepley         } else {
469497e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
469597e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
469697e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4697a5e93ea8SMatthew G. Knepley               ++cind;
4698a5e93ea8SMatthew G. Knepley             }
4699a5e93ea8SMatthew G. Knepley           }
4700ba322698SMatthew G. Knepley         }
4701ba322698SMatthew G. Knepley       } else {
4702ba322698SMatthew G. Knepley         if (comps) {
4703ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4704ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4705ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4706ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4707ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
4708ba322698SMatthew G. Knepley           }
4709a5e93ea8SMatthew G. Knepley         } else {
471097e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
471197e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
471297e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
471397e99dd9SToby Isaac               ++cind;
471497e99dd9SToby Isaac             }
471597e99dd9SToby Isaac           }
471697e99dd9SToby Isaac         }
4717ba322698SMatthew G. Knepley       }
471897e99dd9SToby Isaac     } else {
471997e99dd9SToby Isaac       if (perm) {
4720ba322698SMatthew G. Knepley         if (comps) {
4721ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4722ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4723ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4724ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4725ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
4726ba322698SMatthew G. Knepley           }
4727ba322698SMatthew G. Knepley         } else {
472897e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
472997e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
473097e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
473197e99dd9SToby Isaac               ++cind;
473297e99dd9SToby Isaac             }
473397e99dd9SToby Isaac           }
4734ba322698SMatthew G. Knepley         }
4735ba322698SMatthew G. Knepley       } else {
4736ba322698SMatthew G. Knepley         if (comps) {
4737ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4738ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4739ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4740ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4741ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
4742ba322698SMatthew G. Knepley           }
474397e99dd9SToby Isaac         } else {
474497e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
474597e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
474697e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4747a5e93ea8SMatthew G. Knepley               ++cind;
4748a5e93ea8SMatthew G. Knepley             }
4749a5e93ea8SMatthew G. Knepley           }
4750a5e93ea8SMatthew G. Knepley         }
4751a5e93ea8SMatthew G. Knepley       }
4752a5e93ea8SMatthew G. Knepley     }
4753ba322698SMatthew G. Knepley   }
47541a271a75SMatthew G. Knepley   *offset += fdof;
4755a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4756a5e93ea8SMatthew G. Knepley }
4757a5e93ea8SMatthew G. Knepley 
47588f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4759a6dfd86eSKarl Rupp {
4760552f7358SJed Brown   PetscScalar    *array;
47611b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
47621b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4763552f7358SJed Brown   PetscErrorCode  ierr;
4764552f7358SJed Brown 
47651b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
4766b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4767b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
4768b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4769b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
4770b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4771b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4772b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
4773b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
4774b6ebb6e6SMatthew G. Knepley 
4775b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4776b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
4777b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
4778b6ebb6e6SMatthew G. Knepley     {
4779b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4780b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
4781b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
4782b6ebb6e6SMatthew G. Knepley 
4783b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
4784b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
4785b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
4786b6ebb6e6SMatthew G. Knepley       if (!cdof) {
4787b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4788b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4789b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4790b6ebb6e6SMatthew G. Knepley           }
4791b6ebb6e6SMatthew G. Knepley         } else {
4792b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4793b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4794b6ebb6e6SMatthew G. Knepley           }
4795b6ebb6e6SMatthew G. Knepley         }
4796b6ebb6e6SMatthew G. Knepley       } else {
4797b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
4798b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4799b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4800b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4801b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4802b6ebb6e6SMatthew G. Knepley           }
4803b6ebb6e6SMatthew G. Knepley         } else {
4804b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4805b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4806b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4807b6ebb6e6SMatthew G. Knepley           }
4808b6ebb6e6SMatthew G. Knepley         }
4809b6ebb6e6SMatthew G. Knepley       }
4810b6ebb6e6SMatthew G. Knepley     }
4811b6ebb6e6SMatthew G. Knepley   }
4812b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4813b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4814b6ebb6e6SMatthew G. Knepley }
48151b406b76SMatthew G. Knepley 
48161b406b76SMatthew G. Knepley /*@C
48171b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
48181b406b76SMatthew G. Knepley 
48191b406b76SMatthew G. Knepley   Not collective
48201b406b76SMatthew G. Knepley 
48211b406b76SMatthew G. Knepley   Input Parameters:
48221b406b76SMatthew G. Knepley + dm - The DM
48231b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
48241b406b76SMatthew G. Knepley . v - The local vector
4825eaf898f9SPatrick Sanan . point - The point in the DM
48261b406b76SMatthew G. Knepley . values - The array of values
482722c1ee49SMatthew G. Knepley - mode - The insert mode. One of INSERT_ALL_VALUES, ADD_ALL_VALUES, INSERT_VALUES, ADD_VALUES, INSERT_BC_VALUES, and ADD_BC_VALUES,
482822c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
48291b406b76SMatthew G. Knepley 
48301b406b76SMatthew G. Knepley   Fortran Notes:
48311b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
48321b406b76SMatthew G. Knepley 
48331b406b76SMatthew G. Knepley   Level: intermediate
48341b406b76SMatthew G. Knepley 
48351b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
48361b406b76SMatthew G. Knepley @*/
48371b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
48381b406b76SMatthew G. Knepley {
48391b406b76SMatthew G. Knepley   PetscSection    clSection;
48401b406b76SMatthew G. Knepley   IS              clPoints;
48411b406b76SMatthew G. Knepley   PetscScalar    *array;
48421b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
484397e99dd9SToby Isaac   const PetscInt *clp, *clperm;
48441a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
48451b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
48461b406b76SMatthew G. Knepley 
48471a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
48481b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
484992fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
48501a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
48511a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
48521b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
48531b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
48541b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
48558f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
48561b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
48571b406b76SMatthew G. Knepley   }
48581a271a75SMatthew G. Knepley   /* Get points */
485997e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4860923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
48611a271a75SMatthew G. Knepley   /* Get array */
4862552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
48631a271a75SMatthew G. Knepley   /* Get values */
4864ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
486597e99dd9SToby Isaac     PetscInt offset = 0, f;
4866552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
486797e99dd9SToby Isaac       const PetscInt    **perms = NULL;
486897e99dd9SToby Isaac       const PetscScalar **flips = NULL;
486997e99dd9SToby Isaac 
487097e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4871552f7358SJed Brown       switch (mode) {
4872552f7358SJed Brown       case INSERT_VALUES:
487397e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
487497e99dd9SToby Isaac           const PetscInt    point = points[2*p];
487597e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
487697e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
487797e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4878552f7358SJed Brown         } break;
4879552f7358SJed Brown       case INSERT_ALL_VALUES:
488097e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
488197e99dd9SToby Isaac           const PetscInt    point = points[2*p];
488297e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
488397e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
488497e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4885552f7358SJed Brown         } break;
4886a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
488797e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
488897e99dd9SToby Isaac           const PetscInt    point = points[2*p];
488997e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
489097e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4891ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
4892a5e93ea8SMatthew G. Knepley         } break;
4893552f7358SJed Brown       case ADD_VALUES:
489497e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
489597e99dd9SToby Isaac           const PetscInt    point = points[2*p];
489697e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
489797e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
489897e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4899552f7358SJed Brown         } break;
4900552f7358SJed Brown       case ADD_ALL_VALUES:
490197e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
490297e99dd9SToby Isaac           const PetscInt    point = points[2*p];
490397e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
490497e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
490597e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4906552f7358SJed Brown         } break;
4907304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
490897e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
490997e99dd9SToby Isaac           const PetscInt    point = points[2*p];
491097e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
491197e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4912ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
4913304ab55fSMatthew G. Knepley         } break;
4914552f7358SJed Brown       default:
4915f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4916552f7358SJed Brown       }
491797e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
49181a271a75SMatthew G. Knepley     }
4919552f7358SJed Brown   } else {
49201a271a75SMatthew G. Knepley     PetscInt dof, off;
492197e99dd9SToby Isaac     const PetscInt    **perms = NULL;
492297e99dd9SToby Isaac     const PetscScalar **flips = NULL;
49231a271a75SMatthew G. Knepley 
492497e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4925552f7358SJed Brown     switch (mode) {
4926552f7358SJed Brown     case INSERT_VALUES:
492797e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
492897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
492997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
493097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
493197e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
493297e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4933552f7358SJed Brown       } break;
4934552f7358SJed Brown     case INSERT_ALL_VALUES:
493597e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
493697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
493797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
493897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
493997e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
494097e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4941552f7358SJed Brown       } break;
4942a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
494397e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
494497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
494597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
494697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
494797e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
494897e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4949a5e93ea8SMatthew G. Knepley       } break;
4950552f7358SJed Brown     case ADD_VALUES:
495197e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
495297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
495397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
495497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
495597e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
495697e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4957552f7358SJed Brown       } break;
4958552f7358SJed Brown     case ADD_ALL_VALUES:
495997e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
496097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
496197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
496297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
496397e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
496497e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4965552f7358SJed Brown       } break;
4966304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
496797e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
496897e99dd9SToby Isaac         const PetscInt    point = points[2*p];
496997e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
497097e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
497197e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
497297e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
4973304ab55fSMatthew G. Knepley       } break;
4974552f7358SJed Brown     default:
4975f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4976552f7358SJed Brown     }
497797e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4978552f7358SJed Brown   }
49791a271a75SMatthew G. Knepley   /* Cleanup points */
4980923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
49811a271a75SMatthew G. Knepley   /* Cleanup array */
4982552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4983552f7358SJed Brown   PetscFunctionReturn(0);
4984552f7358SJed Brown }
4985552f7358SJed Brown 
4986ba322698SMatthew G. Knepley PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM dm, PetscSection section, Vec v, PetscBool fieldActive[], PetscInt point, PetscInt Ncc, const PetscInt comps[], const PetscScalar values[], InsertMode mode)
4987e07394fbSMatthew G. Knepley {
4988e07394fbSMatthew G. Knepley   PetscSection      clSection;
4989e07394fbSMatthew G. Knepley   IS                clPoints;
4990e07394fbSMatthew G. Knepley   PetscScalar       *array;
4991e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
4992b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
4993e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
499497e99dd9SToby Isaac   PetscInt          offset = 0, f;
4995e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
4996e07394fbSMatthew G. Knepley 
4997e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
4998e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
499992fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
5000e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5001e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5002e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5003e07394fbSMatthew G. Knepley   /* Get points */
5004b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
5005923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5006e07394fbSMatthew G. Knepley   /* Get array */
5007e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5008e07394fbSMatthew G. Knepley   /* Get values */
5009e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
501097e99dd9SToby Isaac     const PetscInt    **perms = NULL;
501197e99dd9SToby Isaac     const PetscScalar **flips = NULL;
501297e99dd9SToby Isaac 
5013e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
5014e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
5015e07394fbSMatthew G. Knepley         PetscInt fdof;
5016e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5017e07394fbSMatthew G. Knepley         offset += fdof;
5018e07394fbSMatthew G. Knepley       }
5019e07394fbSMatthew G. Knepley       continue;
5020e07394fbSMatthew G. Knepley     }
502197e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5022e07394fbSMatthew G. Knepley     switch (mode) {
5023e07394fbSMatthew G. Knepley     case INSERT_VALUES:
502497e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
502597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
502697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
502797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
5028b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
5029e07394fbSMatthew G. Knepley       } break;
5030e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
503197e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
503297e99dd9SToby Isaac         const PetscInt    point = points[2*p];
503397e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
503497e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
5035b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
5036e07394fbSMatthew G. Knepley         } break;
5037e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
503897e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
503997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
504097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
504197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
5042ba322698SMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, clperm, values, &offset, array);
5043e07394fbSMatthew G. Knepley       } break;
5044e07394fbSMatthew G. Knepley     case ADD_VALUES:
504597e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
504697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
504797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
504897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
5049b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
5050e07394fbSMatthew G. Knepley       } break;
5051e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
505297e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
505397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
505497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
505597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
5056b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
5057e07394fbSMatthew G. Knepley       } break;
5058e07394fbSMatthew G. Knepley     default:
5059f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5060e07394fbSMatthew G. Knepley     }
506197e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5062e07394fbSMatthew G. Knepley   }
5063e07394fbSMatthew G. Knepley   /* Cleanup points */
5064923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5065e07394fbSMatthew G. Knepley   /* Cleanup array */
5066e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5067e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
5068e07394fbSMatthew G. Knepley }
5069e07394fbSMatthew G. Knepley 
50707cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
5071552f7358SJed Brown {
5072552f7358SJed Brown   PetscMPIInt    rank;
5073552f7358SJed Brown   PetscInt       i, j;
5074552f7358SJed Brown   PetscErrorCode ierr;
5075552f7358SJed Brown 
5076552f7358SJed Brown   PetscFunctionBegin;
507782f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
5078eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
5079e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
5080e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
5081b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
5082b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
5083e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
5084b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
5085519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
50867eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
5087552f7358SJed Brown #else
5088b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
5089552f7358SJed Brown #endif
5090552f7358SJed Brown     }
509177a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
5092552f7358SJed Brown   }
5093552f7358SJed Brown   PetscFunctionReturn(0);
5094552f7358SJed Brown }
5095552f7358SJed Brown 
509605586334SMatthew G. Knepley /*
509705586334SMatthew G. Knepley   DMPlexGetIndicesPoint_Internal - Add the indices for dofs on a point to an index array
509805586334SMatthew G. Knepley 
509905586334SMatthew G. Knepley   Input Parameters:
510005586334SMatthew G. Knepley + section - The section for this data layout
510105586334SMatthew G. Knepley . point   - The point contributing dofs with these indices
510205586334SMatthew G. Knepley . off     - The global offset of this point
510305586334SMatthew G. Knepley . loff    - The local offset of each field
510405586334SMatthew G. Knepley . setBC   - The flag determining whether to include indices of bounsary values
510505586334SMatthew G. Knepley . perm    - A permutation of the dofs on this point, or NULL
510605586334SMatthew G. Knepley - indperm - A permutation of the entire indices array, or NULL
510705586334SMatthew G. Knepley 
510805586334SMatthew G. Knepley   Output Parameter:
510905586334SMatthew G. Knepley . indices - Indices for dofs on this point
511005586334SMatthew G. Knepley 
511105586334SMatthew G. Knepley   Level: developer
511205586334SMatthew G. Knepley 
511305586334SMatthew G. Knepley   Note: The indices could be local or global, depending on the value of 'off'.
511405586334SMatthew G. Knepley */
511505586334SMatthew G. Knepley PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], const PetscInt indperm[], PetscInt indices[])
5116a6dfd86eSKarl Rupp {
5117e6ccafaeSMatthew G Knepley   PetscInt        dof;   /* The number of unknowns on this point */
5118552f7358SJed Brown   PetscInt        cdof;  /* The number of constraints on this point */
5119552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5120552f7358SJed Brown   PetscInt        cind = 0, k;
5121552f7358SJed Brown   PetscErrorCode  ierr;
5122552f7358SJed Brown 
5123552f7358SJed Brown   PetscFunctionBegin;
5124552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
5125552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5126552f7358SJed Brown   if (!cdof || setBC) {
512705586334SMatthew G. Knepley     for (k = 0; k < dof; ++k) {
512805586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
512905586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
513005586334SMatthew G. Knepley 
513105586334SMatthew G. Knepley       indices[ind] = off + k;
5132552f7358SJed Brown     }
5133552f7358SJed Brown   } else {
5134552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
51354acb8e1eSToby Isaac     for (k = 0; k < dof; ++k) {
513605586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
513705586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
513805586334SMatthew G. Knepley 
51394acb8e1eSToby Isaac       if ((cind < cdof) && (k == cdofs[cind])) {
51404acb8e1eSToby Isaac         /* Insert check for returning constrained indices */
514105586334SMatthew G. Knepley         indices[ind] = -(off+k+1);
51424acb8e1eSToby Isaac         ++cind;
51434acb8e1eSToby Isaac       } else {
514405586334SMatthew G. Knepley         indices[ind] = off+k-cind;
5145552f7358SJed Brown       }
5146552f7358SJed Brown     }
5147552f7358SJed Brown   }
5148e6ccafaeSMatthew G Knepley   *loff += dof;
5149552f7358SJed Brown   PetscFunctionReturn(0);
5150552f7358SJed Brown }
5151552f7358SJed Brown 
51527e29afd2SMatthew G. Knepley /*
51537e29afd2SMatthew G. Knepley   This version only believes the point offset from the globalSection
51547e29afd2SMatthew G. Knepley 
51557e29afd2SMatthew G. Knepley  . off - The global offset of this point
51567e29afd2SMatthew G. Knepley */
515705586334SMatthew G. Knepley PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
5158a6dfd86eSKarl Rupp {
5159552f7358SJed Brown   PetscInt       numFields, foff, f;
5160552f7358SJed Brown   PetscErrorCode ierr;
5161552f7358SJed Brown 
5162552f7358SJed Brown   PetscFunctionBegin;
5163552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5164552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
51654acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
5166552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
51674acb8e1eSToby Isaac     PetscInt        cind = 0, b;
51684acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
5169552f7358SJed Brown 
5170552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5171552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
5172552f7358SJed Brown     if (!cfdof || setBC) {
517305586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
517405586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
517505586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
517605586334SMatthew G. Knepley 
517705586334SMatthew G. Knepley         indices[ind] = off+foff+b;
517805586334SMatthew G. Knepley       }
5179552f7358SJed Brown     } else {
5180552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
518105586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
518205586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
518305586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
518405586334SMatthew G. Knepley 
51854acb8e1eSToby Isaac         if ((cind < cfdof) && (b == fcdofs[cind])) {
518605586334SMatthew G. Knepley           indices[ind] = -(off+foff+b+1);
5187552f7358SJed Brown           ++cind;
5188552f7358SJed Brown         } else {
518905586334SMatthew G. Knepley           indices[ind] = off+foff+b-cind;
5190552f7358SJed Brown         }
5191552f7358SJed Brown       }
5192552f7358SJed Brown     }
5193a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
5194552f7358SJed Brown     foffs[f] += fdof;
5195552f7358SJed Brown   }
5196552f7358SJed Brown   PetscFunctionReturn(0);
5197552f7358SJed Brown }
5198552f7358SJed Brown 
51997e29afd2SMatthew G. Knepley /*
52007e29afd2SMatthew G. Knepley   This version believes the globalSection offsets for each field, rather than just the point offset
52017e29afd2SMatthew G. Knepley 
52027e29afd2SMatthew G. Knepley  . foffs - The offset into 'indices' for each field, since it is segregated by field
52037e29afd2SMatthew G. Knepley */
520405586334SMatthew G. Knepley PetscErrorCode DMPlexGetIndicesPointFieldsSplit_Internal(PetscSection section, PetscSection globalSection, PetscInt point, PetscInt foffs[], PetscBool setBC, const PetscInt ***perms, PetscInt permsoff, const PetscInt indperm[], PetscInt indices[])
52057e29afd2SMatthew G. Knepley {
52067e29afd2SMatthew G. Knepley   PetscInt       numFields, foff, f;
52077e29afd2SMatthew G. Knepley   PetscErrorCode ierr;
52087e29afd2SMatthew G. Knepley 
52097e29afd2SMatthew G. Knepley   PetscFunctionBegin;
52107e29afd2SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
52117e29afd2SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
52127e29afd2SMatthew G. Knepley     PetscInt        fdof, cfdof;
52137e29afd2SMatthew G. Knepley     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
52147e29afd2SMatthew G. Knepley     PetscInt        cind = 0, b;
52157e29afd2SMatthew G. Knepley     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
52167e29afd2SMatthew G. Knepley 
52177e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
52187e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
52197e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldOffset(globalSection, point, f, &foff);CHKERRQ(ierr);
52207e29afd2SMatthew G. Knepley     if (!cfdof || setBC) {
522105586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
522205586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
522305586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
522405586334SMatthew G. Knepley 
522505586334SMatthew G. Knepley         indices[ind] = foff+b;
522605586334SMatthew G. Knepley       }
52277e29afd2SMatthew G. Knepley     } else {
52287e29afd2SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
522905586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
523005586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
523105586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
523205586334SMatthew G. Knepley 
52337e29afd2SMatthew G. Knepley         if ((cind < cfdof) && (b == fcdofs[cind])) {
523405586334SMatthew G. Knepley           indices[ind] = -(foff+b+1);
52357e29afd2SMatthew G. Knepley           ++cind;
52367e29afd2SMatthew G. Knepley         } else {
523705586334SMatthew G. Knepley           indices[ind] = foff+b-cind;
52387e29afd2SMatthew G. Knepley         }
52397e29afd2SMatthew G. Knepley       }
52407e29afd2SMatthew G. Knepley     }
52417e29afd2SMatthew G. Knepley     foffs[f] += fdof;
52427e29afd2SMatthew G. Knepley   }
52437e29afd2SMatthew G. Knepley   PetscFunctionReturn(0);
52447e29afd2SMatthew G. Knepley }
52457e29afd2SMatthew G. Knepley 
52464acb8e1eSToby 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)
5247d3d1a6afSToby Isaac {
5248d3d1a6afSToby Isaac   Mat             cMat;
5249d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
5250d3d1a6afSToby Isaac   IS              aIS;
5251d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
5252d3d1a6afSToby Isaac   const PetscInt  *anchors;
5253e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
5254d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
5255d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
5256d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
5257d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
5258d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
5259d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
5260d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
52616ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
5262d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
5263d3d1a6afSToby Isaac   PetscErrorCode  ierr;
5264d3d1a6afSToby Isaac 
5265d3d1a6afSToby Isaac   PetscFunctionBegin;
5266d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5267d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5268d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5269d3d1a6afSToby Isaac 
5270a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
5271d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
5272d3d1a6afSToby Isaac   if (aSec) {
5273580bdb30SBarry Smith     ierr = PetscArrayzero(newOffsets, 32);CHKERRQ(ierr);
5274d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
5275d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
5276d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
5277d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
5278d3d1a6afSToby Isaac      * into the global matrix anyway) */
5279d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
5280d3d1a6afSToby Isaac       PetscInt b    = points[p];
52814b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5282d3d1a6afSToby Isaac 
52834b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
52844b2f2278SToby Isaac       if (!bSecDof) {
52854b2f2278SToby Isaac         continue;
52864b2f2278SToby Isaac       }
5287d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5288d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
5289d3d1a6afSToby Isaac       }
5290d3d1a6afSToby Isaac       if (bDof) {
5291d3d1a6afSToby Isaac         /* this point is constrained */
5292d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
5293d3d1a6afSToby Isaac         PetscInt bOff, q;
5294d3d1a6afSToby Isaac 
5295d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
5296d3d1a6afSToby Isaac         newNumPoints  += bDof;
5297d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
5298d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5299d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
5300d3d1a6afSToby Isaac           PetscInt aDof;
5301d3d1a6afSToby Isaac 
5302d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
5303d3d1a6afSToby Isaac           newNumIndices += aDof;
5304d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
5305d3d1a6afSToby Isaac             PetscInt fDof;
5306d3d1a6afSToby Isaac 
5307d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
5308d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
5309d3d1a6afSToby Isaac           }
5310d3d1a6afSToby Isaac         }
5311d3d1a6afSToby Isaac       }
5312d3d1a6afSToby Isaac       else {
5313d3d1a6afSToby Isaac         /* this point is not constrained */
5314d3d1a6afSToby Isaac         newNumPoints++;
53154b2f2278SToby Isaac         newNumIndices += bSecDof;
5316d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
5317d3d1a6afSToby Isaac           PetscInt fDof;
5318d3d1a6afSToby Isaac 
5319d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5320d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
5321d3d1a6afSToby Isaac         }
5322d3d1a6afSToby Isaac       }
5323d3d1a6afSToby Isaac     }
5324d3d1a6afSToby Isaac   }
5325d3d1a6afSToby Isaac   if (!anyConstrained) {
532672b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
532772b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
532872b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
532972b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
533072b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
5331d3d1a6afSToby Isaac     PetscFunctionReturn(0);
5332d3d1a6afSToby Isaac   }
5333d3d1a6afSToby Isaac 
53346ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
53356ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
53366ecaa68aSToby Isaac 
5337f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
5338d3d1a6afSToby Isaac 
53396ecaa68aSToby Isaac   if (!outPoints && !outValues) {
53406ecaa68aSToby Isaac     if (offsets) {
53416ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
53426ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
53436ecaa68aSToby Isaac       }
53446ecaa68aSToby Isaac     }
53456ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
53466ecaa68aSToby Isaac     PetscFunctionReturn(0);
53476ecaa68aSToby Isaac   }
53486ecaa68aSToby Isaac 
5349f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
5350d3d1a6afSToby Isaac 
5351f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
5352d3d1a6afSToby Isaac 
5353d3d1a6afSToby Isaac   /* workspaces */
5354d3d1a6afSToby Isaac   if (numFields) {
5355d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
535669291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
535769291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5358d3d1a6afSToby Isaac     }
5359d3d1a6afSToby Isaac   }
5360d3d1a6afSToby Isaac   else {
536169291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
536269291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5363d3d1a6afSToby Isaac   }
5364d3d1a6afSToby Isaac 
5365d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
5366d3d1a6afSToby Isaac   if (numFields) {
53674b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
53684b2f2278SToby Isaac 
5369d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5370d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
53714b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5372d3d1a6afSToby Isaac 
53734b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
53744b2f2278SToby Isaac       if (!bSecDof) {
53754b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
53764b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
53774b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
53784b2f2278SToby Isaac         }
53794b2f2278SToby Isaac         continue;
53804b2f2278SToby Isaac       }
5381d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5382d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5383d3d1a6afSToby Isaac       }
5384d3d1a6afSToby Isaac       if (bDof) {
5385d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5386d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
5387d3d1a6afSToby Isaac 
5388d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5389d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5390d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
5391d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
5392d3d1a6afSToby Isaac             PetscInt aFDof;
5393d3d1a6afSToby Isaac 
5394d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
5395d3d1a6afSToby Isaac             allFDof += aFDof;
5396d3d1a6afSToby Isaac           }
5397d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
5398d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
5399d3d1a6afSToby Isaac         }
5400d3d1a6afSToby Isaac       }
5401d3d1a6afSToby Isaac       else {
5402d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5403d3d1a6afSToby Isaac           PetscInt fDof;
5404d3d1a6afSToby Isaac 
5405d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5406d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
5407d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
5408d3d1a6afSToby Isaac         }
5409d3d1a6afSToby Isaac       }
5410d3d1a6afSToby Isaac     }
54114b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
54124b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
54134b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
5414d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5415d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
5416d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
5417d3d1a6afSToby Isaac       }
541819f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
541919f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
542069291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5421d3d1a6afSToby Isaac     }
5422d3d1a6afSToby Isaac   }
5423d3d1a6afSToby Isaac   else {
5424d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5425d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
54264b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5427d3d1a6afSToby Isaac 
54284b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
54294b2f2278SToby Isaac       if (!bSecDof) {
54304b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
54314b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
54324b2f2278SToby Isaac         continue;
54334b2f2278SToby Isaac       }
5434d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5435d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5436d3d1a6afSToby Isaac       }
5437d3d1a6afSToby Isaac       if (bDof) {
54384b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
5439d3d1a6afSToby Isaac 
5440d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5441d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5442d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
5443d3d1a6afSToby Isaac 
5444d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
5445d3d1a6afSToby Isaac           allDof += aDof;
5446d3d1a6afSToby Isaac         }
5447d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
54484b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
5449d3d1a6afSToby Isaac       }
5450d3d1a6afSToby Isaac       else {
54514b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
5452d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
5453d3d1a6afSToby Isaac       }
5454d3d1a6afSToby Isaac     }
5455d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
5456d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
5457d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5458d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
5459d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
5460d3d1a6afSToby Isaac     }
546169291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5462d3d1a6afSToby Isaac   }
5463d3d1a6afSToby Isaac 
54646ecaa68aSToby Isaac   /* output arrays */
546569291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
54666ecaa68aSToby Isaac 
5467d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
5468d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
5469d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
547069291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
547169291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
5472d3d1a6afSToby Isaac   if (numFields) {
5473d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
5474d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5475d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
54764b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5477d3d1a6afSToby Isaac 
54784b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
54794b2f2278SToby Isaac       if (!bSecDof) {
54804b2f2278SToby Isaac         continue;
54814b2f2278SToby Isaac       }
5482d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5483d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5484d3d1a6afSToby Isaac       }
5485d3d1a6afSToby Isaac       if (bDof) {
5486d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
5487d3d1a6afSToby Isaac 
5488d3d1a6afSToby Isaac         fStart[0] = 0;
5489d3d1a6afSToby Isaac         fEnd[0]   = 0;
5490d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5491d3d1a6afSToby Isaac           PetscInt fDof;
5492d3d1a6afSToby Isaac 
5493d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
5494d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
5495d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
5496d3d1a6afSToby Isaac         }
5497d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
549805586334SMatthew G. Knepley         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, NULL, indices);CHKERRQ(ierr);
5499d3d1a6afSToby Isaac 
5500d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
5501d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
5502d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5503d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
5504d3d1a6afSToby Isaac 
5505d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
5506d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
5507d3d1a6afSToby Isaac         }
5508d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5509d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5510d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5511d3d1a6afSToby Isaac 
5512d3d1a6afSToby 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 */
5513d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5514d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5515302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
551605586334SMatthew G. Knepley           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, NULL, newIndices);CHKERRQ(ierr);
5517d3d1a6afSToby Isaac         }
5518d3d1a6afSToby Isaac         newP += bDof;
5519d3d1a6afSToby Isaac 
55206ecaa68aSToby Isaac         if (outValues) {
5521d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
5522d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
5523d3d1a6afSToby 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);
5524d3d1a6afSToby Isaac           }
5525d3d1a6afSToby Isaac         }
55266ecaa68aSToby Isaac       }
5527d3d1a6afSToby Isaac       else {
5528d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5529d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5530d3d1a6afSToby Isaac         newP++;
5531d3d1a6afSToby Isaac       }
5532d3d1a6afSToby Isaac     }
5533d3d1a6afSToby Isaac   } else {
5534d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5535d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5536d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
55374b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5538d3d1a6afSToby Isaac 
55394b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
55404b2f2278SToby Isaac       if (!bSecDof) {
55414b2f2278SToby Isaac         continue;
55424b2f2278SToby Isaac       }
5543d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5544d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5545d3d1a6afSToby Isaac       }
5546d3d1a6afSToby Isaac       if (bDof) {
5547d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
5548d3d1a6afSToby Isaac 
5549d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
555005586334SMatthew G. Knepley         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, NULL, indices);CHKERRQ(ierr);
5551d3d1a6afSToby Isaac 
5552d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
5553d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5554d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5555d3d1a6afSToby Isaac 
5556d3d1a6afSToby 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 */
5557d3d1a6afSToby Isaac 
5558d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5559d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5560302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
556105586334SMatthew G. Knepley           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, NULL, newIndices);CHKERRQ(ierr);
5562d3d1a6afSToby Isaac         }
5563d3d1a6afSToby Isaac         newP += bDof;
5564d3d1a6afSToby Isaac 
5565d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
55666ecaa68aSToby Isaac         if (outValues) {
5567d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
5568d3d1a6afSToby Isaac         }
55696ecaa68aSToby Isaac       }
5570d3d1a6afSToby Isaac       else {
5571d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5572d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5573d3d1a6afSToby Isaac         newP++;
5574d3d1a6afSToby Isaac       }
5575d3d1a6afSToby Isaac     }
5576d3d1a6afSToby Isaac   }
5577d3d1a6afSToby Isaac 
55786ecaa68aSToby Isaac   if (outValues) {
557969291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
5580580bdb30SBarry Smith     ierr = PetscArrayzero(tmpValues,newNumIndices*numIndices);CHKERRQ(ierr);
5581d3d1a6afSToby Isaac     /* multiply constraints on the right */
5582d3d1a6afSToby Isaac     if (numFields) {
5583d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
5584d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
5585d3d1a6afSToby Isaac 
5586d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5587d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
5588d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5589d3d1a6afSToby Isaac           PetscInt c, r, k;
5590d3d1a6afSToby Isaac           PetscInt dof;
5591d3d1a6afSToby Isaac 
5592d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
55934b2f2278SToby Isaac           if (!dof) {
55944b2f2278SToby Isaac             continue;
55954b2f2278SToby Isaac           }
5596d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5597d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
5598d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
5599d3d1a6afSToby Isaac 
5600d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5601d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
5602d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
56034acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
5604d3d1a6afSToby Isaac                 }
5605d3d1a6afSToby Isaac               }
5606d3d1a6afSToby Isaac             }
5607d3d1a6afSToby Isaac           }
5608d3d1a6afSToby Isaac           else {
5609d3d1a6afSToby Isaac             /* copy this column as is */
5610d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5611d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
5612d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5613d3d1a6afSToby Isaac               }
5614d3d1a6afSToby Isaac             }
5615d3d1a6afSToby Isaac           }
5616d3d1a6afSToby Isaac           oldOff += dof;
5617d3d1a6afSToby Isaac         }
5618d3d1a6afSToby Isaac       }
5619d3d1a6afSToby Isaac     }
5620d3d1a6afSToby Isaac     else {
5621d3d1a6afSToby Isaac       PetscInt oldOff = 0;
5622d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5623d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
5624d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
5625d3d1a6afSToby Isaac         PetscInt c, r, k;
5626d3d1a6afSToby Isaac         PetscInt dof;
5627d3d1a6afSToby Isaac 
5628d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
56294b2f2278SToby Isaac         if (!dof) {
56304b2f2278SToby Isaac           continue;
56314b2f2278SToby Isaac         }
5632d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5633d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5634d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
5635d3d1a6afSToby Isaac 
5636d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5637d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
5638d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
5639d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5640d3d1a6afSToby Isaac               }
5641d3d1a6afSToby Isaac             }
5642d3d1a6afSToby Isaac           }
5643d3d1a6afSToby Isaac         }
5644d3d1a6afSToby Isaac         else {
5645d3d1a6afSToby Isaac           /* copy this column as is */
5646d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5647d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
5648d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5649d3d1a6afSToby Isaac             }
5650d3d1a6afSToby Isaac           }
5651d3d1a6afSToby Isaac         }
5652d3d1a6afSToby Isaac         oldOff += dof;
5653d3d1a6afSToby Isaac       }
5654d3d1a6afSToby Isaac     }
5655d3d1a6afSToby Isaac 
56566ecaa68aSToby Isaac     if (multiplyLeft) {
565769291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
5658580bdb30SBarry Smith       ierr = PetscArrayzero(newValues,newNumIndices*newNumIndices);CHKERRQ(ierr);
5659d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
5660d3d1a6afSToby Isaac       if (numFields) {
5661d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5662d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
5663d3d1a6afSToby Isaac 
5664d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
5665d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
5666d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
5667d3d1a6afSToby Isaac             PetscInt c, r, k;
5668d3d1a6afSToby Isaac             PetscInt dof;
5669d3d1a6afSToby Isaac 
5670d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
5671d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5672d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5673d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
5674d3d1a6afSToby Isaac 
5675d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
5676d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5677d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
5678d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5679d3d1a6afSToby Isaac                   }
5680d3d1a6afSToby Isaac                 }
5681d3d1a6afSToby Isaac               }
5682d3d1a6afSToby Isaac             }
5683d3d1a6afSToby Isaac             else {
5684d3d1a6afSToby Isaac               /* copy this row as is */
5685d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
5686d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5687d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5688d3d1a6afSToby Isaac                 }
5689d3d1a6afSToby Isaac               }
5690d3d1a6afSToby Isaac             }
5691d3d1a6afSToby Isaac             oldOff += dof;
5692d3d1a6afSToby Isaac           }
5693d3d1a6afSToby Isaac         }
5694d3d1a6afSToby Isaac       }
5695d3d1a6afSToby Isaac       else {
5696d3d1a6afSToby Isaac         PetscInt oldOff = 0;
5697d3d1a6afSToby Isaac 
5698d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5699d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
5700d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5701d3d1a6afSToby Isaac           PetscInt c, r, k;
5702d3d1a6afSToby Isaac           PetscInt dof;
5703d3d1a6afSToby Isaac 
5704d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
5705d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5706d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5707d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5708d3d1a6afSToby Isaac 
5709d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5710d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5711d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5712d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5713d3d1a6afSToby Isaac                 }
5714d3d1a6afSToby Isaac               }
5715d3d1a6afSToby Isaac             }
5716d3d1a6afSToby Isaac           }
5717d3d1a6afSToby Isaac           else {
5718d3d1a6afSToby Isaac             /* copy this row as is */
57199fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5720d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5721d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5722d3d1a6afSToby Isaac               }
5723d3d1a6afSToby Isaac             }
5724d3d1a6afSToby Isaac           }
5725d3d1a6afSToby Isaac           oldOff += dof;
5726d3d1a6afSToby Isaac         }
5727d3d1a6afSToby Isaac       }
5728d3d1a6afSToby Isaac 
572969291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
57306ecaa68aSToby Isaac     }
57316ecaa68aSToby Isaac     else {
57326ecaa68aSToby Isaac       newValues = tmpValues;
57336ecaa68aSToby Isaac     }
57346ecaa68aSToby Isaac   }
57356ecaa68aSToby Isaac 
5736d3d1a6afSToby Isaac   /* clean up */
573769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
573869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
57396ecaa68aSToby Isaac 
5740d3d1a6afSToby Isaac   if (numFields) {
5741d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
574269291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
574369291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
574469291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5745d3d1a6afSToby Isaac     }
5746d3d1a6afSToby Isaac   }
5747d3d1a6afSToby Isaac   else {
574869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
574969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
575069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5751d3d1a6afSToby Isaac   }
5752d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5753d3d1a6afSToby Isaac 
5754d3d1a6afSToby Isaac   /* output */
57556ecaa68aSToby Isaac   if (outPoints) {
5756d3d1a6afSToby Isaac     *outPoints = newPoints;
57576ecaa68aSToby Isaac   }
57586ecaa68aSToby Isaac   else {
575969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
57606ecaa68aSToby Isaac   }
576131620726SToby Isaac   if (outValues) {
5762d3d1a6afSToby Isaac     *outValues = newValues;
57636ecaa68aSToby Isaac   }
57646ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
5765d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
5766d3d1a6afSToby Isaac   }
5767d3d1a6afSToby Isaac   PetscFunctionReturn(0);
5768d3d1a6afSToby Isaac }
5769d3d1a6afSToby Isaac 
57707cd05799SMatthew G. Knepley /*@C
5771a87adde4SMatthew G. Knepley   DMPlexGetClosureIndices - Get the global indices in a vector v for all points in the closure of the given point
57727cd05799SMatthew G. Knepley 
57737cd05799SMatthew G. Knepley   Not collective
57747cd05799SMatthew G. Knepley 
57757cd05799SMatthew G. Knepley   Input Parameters:
57767cd05799SMatthew G. Knepley + dm - The DM
57777cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
57787cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
57797cd05799SMatthew G. Knepley - point - The mesh point
57807cd05799SMatthew G. Knepley 
57817cd05799SMatthew G. Knepley   Output parameters:
57827cd05799SMatthew G. Knepley + numIndices - The number of indices
57837cd05799SMatthew G. Knepley . indices - The indices
57847cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
57857cd05799SMatthew G. Knepley 
57867cd05799SMatthew G. Knepley   Note: Must call DMPlexRestoreClosureIndices() to free allocated memory
57877cd05799SMatthew G. Knepley 
57887cd05799SMatthew G. Knepley   Level: advanced
57897cd05799SMatthew G. Knepley 
57907cd05799SMatthew G. Knepley .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
57917cd05799SMatthew G. Knepley @*/
57926ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
57937773e69fSMatthew G. Knepley {
57947773e69fSMatthew G. Knepley   PetscSection    clSection;
57957773e69fSMatthew G. Knepley   IS              clPoints;
579605586334SMatthew G. Knepley   const PetscInt *clp, *clperm;
57974acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
57987773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
57997773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
58007773e69fSMatthew G. Knepley   PetscInt        offsets[32];
58017773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
58027773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
58037773e69fSMatthew G. Knepley 
58047773e69fSMatthew G. Knepley   PetscFunctionBegin;
58057773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58067773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
58077773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
58087773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
58097773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
58107773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
58117773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
5812580bdb30SBarry Smith   ierr = PetscArrayzero(offsets, 32);CHKERRQ(ierr);
58137773e69fSMatthew G. Knepley   /* Get points in closure */
5814923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
581505586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
58167773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
58177773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
58187773e69fSMatthew G. Knepley     PetscInt dof, fdof;
58197773e69fSMatthew G. Knepley 
58207773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
58217773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
58227773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
58237773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
58247773e69fSMatthew G. Knepley     }
58257773e69fSMatthew G. Knepley     Nind += dof;
58267773e69fSMatthew G. Knepley   }
58277773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
58288ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
58294acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
58304acb8e1eSToby Isaac   /* Get dual space symmetries */
58314acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
58324acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
58334acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
58344acb8e1eSToby Isaac   }
58357773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
58367773e69fSMatthew G. Knepley   {
58374acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
58387773e69fSMatthew G. Knepley     if (numPointsNew) {
58394acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
58404acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
58414acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
58424acb8e1eSToby Isaac       }
58434acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
58444acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
58454acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
58464acb8e1eSToby Isaac       }
5847923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
58487773e69fSMatthew G. Knepley       numPoints = numPointsNew;
58497773e69fSMatthew G. Knepley       Nind      = NindNew;
58507773e69fSMatthew G. Knepley       points    = pointsNew;
58517773e69fSMatthew G. Knepley     }
58527773e69fSMatthew G. Knepley   }
58537773e69fSMatthew G. Knepley   /* Calculate indices */
585469291d52SBarry Smith   ierr = DMGetWorkArray(dm, Nind, MPIU_INT, indices);CHKERRQ(ierr);
58557773e69fSMatthew G. Knepley   if (Nf) {
58566ecaa68aSToby Isaac     if (outOffsets) {
58576ecaa68aSToby Isaac       PetscInt f;
58586ecaa68aSToby Isaac 
585946bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
58606ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
58616ecaa68aSToby Isaac       }
58626ecaa68aSToby Isaac     }
58634acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
58644acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
586505586334SMatthew G. Knepley       DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, clperm, *indices);
58667773e69fSMatthew G. Knepley     }
58677773e69fSMatthew G. Knepley   } else {
58684acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
58694acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
58704acb8e1eSToby Isaac 
58714acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
587205586334SMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, clperm, *indices);
58737773e69fSMatthew G. Knepley     }
58747773e69fSMatthew G. Knepley   }
58757773e69fSMatthew G. Knepley   /* Cleanup points */
58764acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
58774acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
58784acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
58794acb8e1eSToby Isaac   }
58807773e69fSMatthew G. Knepley   if (numPointsNew) {
588169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, MPIU_INT, &pointsNew);CHKERRQ(ierr);
58827773e69fSMatthew G. Knepley   } else {
5883923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
58847773e69fSMatthew G. Knepley   }
58857773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
58867773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
58877773e69fSMatthew G. Knepley }
58887773e69fSMatthew G. Knepley 
58897cd05799SMatthew G. Knepley /*@C
58907cd05799SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restore the indices in a vector v for all points in the closure of the given point
58917cd05799SMatthew G. Knepley 
58927cd05799SMatthew G. Knepley   Not collective
58937cd05799SMatthew G. Knepley 
58947cd05799SMatthew G. Knepley   Input Parameters:
58957cd05799SMatthew G. Knepley + dm - The DM
58967cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
58977cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
58987cd05799SMatthew G. Knepley . point - The mesh point
58997cd05799SMatthew G. Knepley . numIndices - The number of indices
59007cd05799SMatthew G. Knepley . indices - The indices
59017cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
59027cd05799SMatthew G. Knepley 
59037cd05799SMatthew G. Knepley   Level: advanced
59047cd05799SMatthew G. Knepley 
59057cd05799SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
59067cd05799SMatthew G. Knepley @*/
590746bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
59087773e69fSMatthew G. Knepley {
59097773e69fSMatthew G. Knepley   PetscErrorCode ierr;
59107773e69fSMatthew G. Knepley 
59117773e69fSMatthew G. Knepley   PetscFunctionBegin;
59127773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
59137773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
591469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
59157773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
59167773e69fSMatthew G. Knepley }
59177773e69fSMatthew G. Knepley 
59187f5d1fdeSMatthew G. Knepley /*@C
59197f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
59207f5d1fdeSMatthew G. Knepley 
59217f5d1fdeSMatthew G. Knepley   Not collective
59227f5d1fdeSMatthew G. Knepley 
59237f5d1fdeSMatthew G. Knepley   Input Parameters:
59247f5d1fdeSMatthew G. Knepley + dm - The DM
5925ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5926ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
59277f5d1fdeSMatthew G. Knepley . A - The matrix
5928eaf898f9SPatrick Sanan . point - The point in the DM
59297f5d1fdeSMatthew G. Knepley . values - The array of values
59307f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
59317f5d1fdeSMatthew G. Knepley 
59327f5d1fdeSMatthew G. Knepley   Fortran Notes:
59337f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
59347f5d1fdeSMatthew G. Knepley 
59357f5d1fdeSMatthew G. Knepley   Level: intermediate
59367f5d1fdeSMatthew G. Knepley 
59377f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
59387f5d1fdeSMatthew G. Knepley @*/
59397c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5940552f7358SJed Brown {
5941552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
59421b406b76SMatthew G. Knepley   PetscSection        clSection;
59431b406b76SMatthew G. Knepley   IS                  clPoints;
5944d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
594505586334SMatthew G. Knepley   const PetscInt     *clp, *clperm;
5946552f7358SJed Brown   PetscInt           *indices;
5947552f7358SJed Brown   PetscInt            offsets[32];
59484acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
59494acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
5950923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
59514acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
5952d3d1a6afSToby Isaac   PetscScalar        *newValues;
5953552f7358SJed Brown   PetscErrorCode      ierr;
5954552f7358SJed Brown 
5955552f7358SJed Brown   PetscFunctionBegin;
5956552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
595792fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
59583dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5959e87a4003SBarry Smith   if (!globalSection) {ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
59603dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
59613dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5962552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
596382f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5964580bdb30SBarry Smith   ierr = PetscArrayzero(offsets, 32);CHKERRQ(ierr);
596505586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
5966923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5967552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5968552f7358SJed Brown     PetscInt fdof;
5969552f7358SJed Brown 
5970552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
5971552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
5972552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5973552f7358SJed Brown       offsets[f+1] += fdof;
5974552f7358SJed Brown     }
5975552f7358SJed Brown     numIndices += dof;
5976552f7358SJed Brown   }
59770d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
59780d644c17SKarl Rupp 
59798ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
59804acb8e1eSToby Isaac   /* Get symmetries */
59814acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
59824acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
59834acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
59844acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
59854acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
59864acb8e1eSToby Isaac 
59874acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
59884acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
59894acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
59904acb8e1eSToby Isaac 
59914acb8e1eSToby Isaac         if (!numFields) {
59924acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
59934acb8e1eSToby Isaac         } else {
59944acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
59954acb8e1eSToby Isaac         }
59964acb8e1eSToby Isaac         if (flip) {
59974acb8e1eSToby Isaac           PetscInt i, j, k;
59984acb8e1eSToby Isaac 
59994acb8e1eSToby Isaac           if (!valCopy) {
600069291d52SBarry Smith             ierr = DMGetWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
60014acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
60024acb8e1eSToby Isaac             values = valCopy;
60034acb8e1eSToby Isaac           }
60044acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
6005775f7be2SToby Isaac             PetscScalar fval = flip[i];
60064acb8e1eSToby Isaac 
60074acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
60084acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
60094acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
60104acb8e1eSToby Isaac             }
60114acb8e1eSToby Isaac           }
60124acb8e1eSToby Isaac         }
60134acb8e1eSToby Isaac         foffset += fdof;
60144acb8e1eSToby Isaac       }
60154acb8e1eSToby Isaac     }
60164acb8e1eSToby Isaac   }
60174acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
6018d3d1a6afSToby Isaac   if (newNumPoints) {
60194acb8e1eSToby Isaac     if (valCopy) {
602069291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
60214acb8e1eSToby Isaac     }
60224acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
60234acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
60244acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
60254acb8e1eSToby Isaac     }
60264acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
60274acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
60284acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
60294acb8e1eSToby Isaac     }
6030923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
6031d3d1a6afSToby Isaac     numPoints  = newNumPoints;
6032d3d1a6afSToby Isaac     numIndices = newNumIndices;
6033d3d1a6afSToby Isaac     points     = newPoints;
6034d3d1a6afSToby Isaac     values     = newValues;
6035d3d1a6afSToby Isaac   }
603669291d52SBarry Smith   ierr = DMGetWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
6037552f7358SJed Brown   if (numFields) {
60387e29afd2SMatthew G. Knepley     PetscBool useFieldOffsets;
60397e29afd2SMatthew G. Knepley 
60407e29afd2SMatthew G. Knepley     ierr = PetscSectionGetUseFieldOffsets(globalSection, &useFieldOffsets);CHKERRQ(ierr);
60417e29afd2SMatthew G. Knepley     if (useFieldOffsets) {
60427e29afd2SMatthew G. Knepley       for (p = 0; p < numPoints; p++) {
604305586334SMatthew G. Knepley         DMPlexGetIndicesPointFieldsSplit_Internal(section, globalSection, points[2*p], offsets, PETSC_FALSE, perms, p, clperm, indices);
60447e29afd2SMatthew G. Knepley       }
60457e29afd2SMatthew G. Knepley     } else {
60464acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
60474acb8e1eSToby Isaac         ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
604805586334SMatthew G. Knepley         DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, clperm, indices);
6049552f7358SJed Brown       }
60507e29afd2SMatthew G. Knepley     }
6051552f7358SJed Brown   } else {
60524acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
60534acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
60544acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
605505586334SMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, clperm, indices);
6056552f7358SJed Brown     }
6057552f7358SJed Brown   }
6058b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
6059552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
6060ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
6061ab1d0545SMatthew G. Knepley     PetscInt i;
6062ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
60638ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
6064ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
6065ab1d0545SMatthew G. Knepley   }
6066552f7358SJed Brown   if (ierr) {
6067552f7358SJed Brown     PetscMPIInt    rank;
6068552f7358SJed Brown     PetscErrorCode ierr2;
6069552f7358SJed Brown 
607082f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
6071e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6072b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
607369291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr2);
6074552f7358SJed Brown     CHKERRQ(ierr);
6075552f7358SJed Brown   }
60764acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
60774acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
60784acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
60794acb8e1eSToby Isaac   }
6080d3d1a6afSToby Isaac   if (newNumPoints) {
608169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
608269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
6083d3d1a6afSToby Isaac   }
6084d3d1a6afSToby Isaac   else {
60854acb8e1eSToby Isaac     if (valCopy) {
608669291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
60874acb8e1eSToby Isaac     }
6088923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
6089d3d1a6afSToby Isaac   }
609069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
6091552f7358SJed Brown   PetscFunctionReturn(0);
6092552f7358SJed Brown }
6093552f7358SJed Brown 
6094de41b84cSMatthew 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)
6095de41b84cSMatthew G. Knepley {
6096de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
6097de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
6098de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
6099de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
610005586334SMatthew G. Knepley   const PetscInt *fclperm, *cclperm;
6101de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
6102de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
61034ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
6104de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
6105de41b84cSMatthew G. Knepley 
6106de41b84cSMatthew G. Knepley   PetscFunctionBegin;
6107de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
6108de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
610992fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
6110de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
611192fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
6112de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
6113e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
6114de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
6115e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
6116de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
6117de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
6118de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
6119de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6120580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
6121580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
612205586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(fsection, (PetscObject) dmf, NULL, &fclperm);CHKERRQ(ierr);
612305586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(csection, (PetscObject) dmc, NULL, &cclperm);CHKERRQ(ierr);
6124de41b84cSMatthew G. Knepley   /* Column indices */
6125de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
61264ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
6127de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
6128de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
6129de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
6130de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
6131de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
6132de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
6133de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
6134de41b84cSMatthew G. Knepley       ++q;
6135de41b84cSMatthew G. Knepley     }
6136de41b84cSMatthew G. Knepley   }
6137de41b84cSMatthew G. Knepley   numCPoints = q;
6138de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
6139de41b84cSMatthew G. Knepley     PetscInt fdof;
6140de41b84cSMatthew G. Knepley 
6141de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
61424ca5e9f5SMatthew G. Knepley     if (!dof) continue;
6143de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
6144de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
6145de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
6146de41b84cSMatthew G. Knepley     }
6147de41b84cSMatthew G. Knepley     numCIndices += dof;
6148de41b84cSMatthew G. Knepley   }
6149de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
6150de41b84cSMatthew G. Knepley   /* Row indices */
6151de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
6152de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
615369291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
6154de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
6155de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
6156de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
6157de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
6158de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
6159de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
6160de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
61614ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
61624ca5e9f5SMatthew G. Knepley         if (!dof) continue;
61634ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
61644ca5e9f5SMatthew G. Knepley         if (s < q) continue;
6165de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
6166de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
6167de41b84cSMatthew G. Knepley         ++q;
6168de41b84cSMatthew G. Knepley       }
6169de41b84cSMatthew G. Knepley     }
6170de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
6171de41b84cSMatthew G. Knepley   }
6172de41b84cSMatthew G. Knepley   numFPoints = q;
6173de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
6174de41b84cSMatthew G. Knepley     PetscInt fdof;
6175de41b84cSMatthew G. Knepley 
6176de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
61774ca5e9f5SMatthew G. Knepley     if (!dof) continue;
6178de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
6179de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
6180de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
6181de41b84cSMatthew G. Knepley     }
6182de41b84cSMatthew G. Knepley     numFIndices += dof;
6183de41b84cSMatthew G. Knepley   }
6184de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
6185de41b84cSMatthew G. Knepley 
61868ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
61878ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
618869291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
618969291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
6190de41b84cSMatthew G. Knepley   if (numFields) {
61914acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
61924acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
61934acb8e1eSToby Isaac 
61944acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
61954acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
61964acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
6197de41b84cSMatthew G. Knepley     }
61984acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
61994acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
620005586334SMatthew G. Knepley       ierr = DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
62014acb8e1eSToby Isaac     }
62024acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
62034acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
620405586334SMatthew G. Knepley       ierr = DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
62054acb8e1eSToby Isaac     }
62064acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
62074acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
62084acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
6209de41b84cSMatthew G. Knepley     }
6210de41b84cSMatthew G. Knepley   } else {
62114acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
62124acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
62134acb8e1eSToby Isaac 
62144acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
62154acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
62164acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
62174acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
62184acb8e1eSToby Isaac 
62194acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
622005586334SMatthew G. Knepley       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
6221de41b84cSMatthew G. Knepley     }
62224acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
62234acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
62244acb8e1eSToby Isaac 
62254acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
622605586334SMatthew G. Knepley       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
6227de41b84cSMatthew G. Knepley     }
62284acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
62294acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
6230de41b84cSMatthew G. Knepley   }
6231de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
62324acb8e1eSToby Isaac   /* TODO: flips */
6233de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
6234de41b84cSMatthew G. Knepley   if (ierr) {
6235de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
6236de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
6237de41b84cSMatthew G. Knepley 
6238de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
6239e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6240de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
624169291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
624269291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
6243de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
6244de41b84cSMatthew G. Knepley   }
624569291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
6246de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
624769291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
624869291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
6249de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
6250de41b84cSMatthew G. Knepley }
6251de41b84cSMatthew G. Knepley 
62527c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
62537c927364SMatthew G. Knepley {
62547c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
62557c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
62567c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
625705586334SMatthew G. Knepley   const PetscInt *fclperm, *cclperm;
62587c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
62597c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
62607c927364SMatthew G. Knepley   PetscErrorCode ierr;
62617c927364SMatthew G. Knepley 
62627c927364SMatthew G. Knepley   PetscFunctionBegin;
62637c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
62647c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
626592fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
62667c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
626792fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
62687c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
6269e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
62707c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
6271e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
62727c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
62737c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
62747c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6275580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
6276580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
627705586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(fsection, (PetscObject) dmf, NULL, &fclperm);CHKERRQ(ierr);
627805586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(csection, (PetscObject) dmc, NULL, &cclperm);CHKERRQ(ierr);
62797c927364SMatthew G. Knepley   /* Column indices */
62807c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
62817c927364SMatthew G. Knepley   maxFPoints = numCPoints;
62827c927364SMatthew G. Knepley   /* Compress out points not in the section */
62837c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
62847c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
62857c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
62867c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
62877c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
62887c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
62897c927364SMatthew G. Knepley       ++q;
62907c927364SMatthew G. Knepley     }
62917c927364SMatthew G. Knepley   }
62927c927364SMatthew G. Knepley   numCPoints = q;
62937c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
62947c927364SMatthew G. Knepley     PetscInt fdof;
62957c927364SMatthew G. Knepley 
62967c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
62977c927364SMatthew G. Knepley     if (!dof) continue;
62987c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
62997c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
63007c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
63017c927364SMatthew G. Knepley     }
63027c927364SMatthew G. Knepley     numCIndices += dof;
63037c927364SMatthew G. Knepley   }
63047c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
63057c927364SMatthew G. Knepley   /* Row indices */
63067c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
63077c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
630869291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
63097c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
63107c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
63117c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
63127c927364SMatthew G. Knepley     /* Compress out points not in the section */
63137c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
63147c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
63157c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
63167c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
63177c927364SMatthew G. Knepley         if (!dof) continue;
63187c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
63197c927364SMatthew G. Knepley         if (s < q) continue;
63207c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
63217c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
63227c927364SMatthew G. Knepley         ++q;
63237c927364SMatthew G. Knepley       }
63247c927364SMatthew G. Knepley     }
63257c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
63267c927364SMatthew G. Knepley   }
63277c927364SMatthew G. Knepley   numFPoints = q;
63287c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
63297c927364SMatthew G. Knepley     PetscInt fdof;
63307c927364SMatthew G. Knepley 
63317c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
63327c927364SMatthew G. Knepley     if (!dof) continue;
63337c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
63347c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
63357c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
63367c927364SMatthew G. Knepley     }
63377c927364SMatthew G. Knepley     numFIndices += dof;
63387c927364SMatthew G. Knepley   }
63397c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
63407c927364SMatthew G. Knepley 
63418ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
63428ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
63437c927364SMatthew G. Knepley   if (numFields) {
63444acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
63454acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
63464acb8e1eSToby Isaac 
63474acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
63484acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
63494acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
63507c927364SMatthew G. Knepley     }
63514acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
63524acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
635305586334SMatthew G. Knepley       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);
63544acb8e1eSToby Isaac     }
63554acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
63564acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
635705586334SMatthew G. Knepley       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);
63584acb8e1eSToby Isaac     }
63594acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
63604acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
63614acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
63627c927364SMatthew G. Knepley     }
63637c927364SMatthew G. Knepley   } else {
63644acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
63654acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
63664acb8e1eSToby Isaac 
63674acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
63684acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
63694acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
63704acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
63714acb8e1eSToby Isaac 
63724acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
637305586334SMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);
63747c927364SMatthew G. Knepley     }
63754acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
63764acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
63774acb8e1eSToby Isaac 
63784acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
637905586334SMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);
63807c927364SMatthew G. Knepley     }
63814acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
63824acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
63837c927364SMatthew G. Knepley   }
638469291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
63857c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
63867c927364SMatthew G. Knepley   PetscFunctionReturn(0);
63877c927364SMatthew G. Knepley }
63887c927364SMatthew G. Knepley 
6389f96281f8SMatthew G. Knepley /*@
6390f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
6391f96281f8SMatthew G. Knepley 
6392f96281f8SMatthew G. Knepley   Input Parameter:
6393f96281f8SMatthew G. Knepley . dm - The DMPlex object
6394f96281f8SMatthew G. Knepley 
6395f96281f8SMatthew G. Knepley   Output Parameters:
6396f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
6397dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6398dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6399dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6400f96281f8SMatthew G. Knepley 
6401f96281f8SMatthew G. Knepley   Level: developer
6402f96281f8SMatthew G. Knepley 
6403dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
6404f96281f8SMatthew G. Knepley @*/
6405770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
6406552f7358SJed Brown {
6407552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6408770b213bSMatthew G Knepley   PetscInt       dim;
6409770b213bSMatthew G Knepley   PetscErrorCode ierr;
6410552f7358SJed Brown 
6411552f7358SJed Brown   PetscFunctionBegin;
6412552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6413c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
64147d5acc75SStefano Zampini   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6415770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
64167d5acc75SStefano Zampini   if (fMax) *fMax = mesh->hybridPointMax[PetscMax(dim-1,0)];
6417770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
6418770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
6419552f7358SJed Brown   PetscFunctionReturn(0);
6420552f7358SJed Brown }
6421552f7358SJed Brown 
6422aeadca18SToby Isaac static PetscErrorCode DMPlexCreateDimStratum(DM dm, DMLabel depthLabel, DMLabel dimLabel, PetscInt d, PetscInt dMax)
64234a3e9fdbSToby Isaac {
64244a3e9fdbSToby Isaac   IS             is, his;
64257d5acc75SStefano Zampini   PetscInt       first = 0, stride;
64264a3e9fdbSToby Isaac   PetscBool      isStride;
64274a3e9fdbSToby Isaac   PetscErrorCode ierr;
64284a3e9fdbSToby Isaac 
64294a3e9fdbSToby Isaac   PetscFunctionBegin;
64304a3e9fdbSToby Isaac   ierr = DMLabelGetStratumIS(depthLabel, d, &is);CHKERRQ(ierr);
64314a3e9fdbSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) is, ISSTRIDE, &isStride);CHKERRQ(ierr);
64324a3e9fdbSToby Isaac   if (isStride) {
64334a3e9fdbSToby Isaac     ierr = ISStrideGetInfo(is, &first, &stride);CHKERRQ(ierr);
64344a3e9fdbSToby Isaac   }
64357d5acc75SStefano Zampini   if (is && (!isStride || stride != 1)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "DM is not stratified: depth %D IS is not contiguous", d);
64364a3e9fdbSToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF, (dMax - first), first, 1, &his);CHKERRQ(ierr);
6437aeadca18SToby Isaac   ierr = DMLabelSetStratumIS(dimLabel, d, his);CHKERRQ(ierr);
64384a3e9fdbSToby Isaac   ierr = ISDestroy(&his);CHKERRQ(ierr);
64394a3e9fdbSToby Isaac   ierr = ISDestroy(&is);CHKERRQ(ierr);
64404a3e9fdbSToby Isaac   PetscFunctionReturn(0);
64414a3e9fdbSToby Isaac }
64424a3e9fdbSToby Isaac 
6443dc5a3409SMatthew G. Knepley /*@
6444dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
6445dc5a3409SMatthew G. Knepley 
6446dc5a3409SMatthew G. Knepley   Input Parameters:
6447a2b725a8SWilliam Gropp + dm   - The DMPlex object
6448dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
6449dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6450dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6451dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6452dc5a3409SMatthew G. Knepley 
6453dc5a3409SMatthew G. Knepley   Level: developer
6454dc5a3409SMatthew G. Knepley 
6455dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
6456dc5a3409SMatthew G. Knepley @*/
6457770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
6458552f7358SJed Brown {
6459552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6460770b213bSMatthew G Knepley   PetscInt       dim;
6461770b213bSMatthew G Knepley   PetscErrorCode ierr;
6462552f7358SJed Brown 
6463552f7358SJed Brown   PetscFunctionBegin;
6464552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6465c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
64667d5acc75SStefano Zampini   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6467770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]               = cMax;
64687d5acc75SStefano Zampini   if (fMax >= 0) mesh->hybridPointMax[PetscMax(dim-1,0)] = fMax;
6469770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]                 = eMax;
6470770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]                 = vMax;
6471552f7358SJed Brown   PetscFunctionReturn(0);
6472552f7358SJed Brown }
6473552f7358SJed Brown 
64747cd05799SMatthew G. Knepley /*@C
64757cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
64767cd05799SMatthew G. Knepley 
64777cd05799SMatthew G. Knepley   Input Parameter:
64787cd05799SMatthew G. Knepley . dm   - The DMPlex object
64797cd05799SMatthew G. Knepley 
64807cd05799SMatthew G. Knepley   Output Parameter:
64817cd05799SMatthew G. Knepley . cellHeight - The height of a cell
64827cd05799SMatthew G. Knepley 
64837cd05799SMatthew G. Knepley   Level: developer
64847cd05799SMatthew G. Knepley 
64857cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
64867cd05799SMatthew G. Knepley @*/
6487552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
6488552f7358SJed Brown {
6489552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6490552f7358SJed Brown 
6491552f7358SJed Brown   PetscFunctionBegin;
6492552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6493552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
6494552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
6495552f7358SJed Brown   PetscFunctionReturn(0);
6496552f7358SJed Brown }
6497552f7358SJed Brown 
64987cd05799SMatthew G. Knepley /*@C
64997cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
65007cd05799SMatthew G. Knepley 
65017cd05799SMatthew G. Knepley   Input Parameters:
65027cd05799SMatthew G. Knepley + dm   - The DMPlex object
65037cd05799SMatthew G. Knepley - cellHeight - The height of a cell
65047cd05799SMatthew G. Knepley 
65057cd05799SMatthew G. Knepley   Level: developer
65067cd05799SMatthew G. Knepley 
65077cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
65087cd05799SMatthew G. Knepley @*/
6509552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
6510552f7358SJed Brown {
6511552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6512552f7358SJed Brown 
6513552f7358SJed Brown   PetscFunctionBegin;
6514552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6515552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
6516552f7358SJed Brown   PetscFunctionReturn(0);
6517552f7358SJed Brown }
6518552f7358SJed Brown 
6519552f7358SJed Brown /* We can easily have a form that takes an IS instead */
652050ad7711SMatthew G. Knepley PetscErrorCode DMPlexCreateNumbering_Internal(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
6521552f7358SJed Brown {
6522552f7358SJed Brown   PetscSection   section, globalSection;
6523552f7358SJed Brown   PetscInt      *numbers, p;
6524552f7358SJed Brown   PetscErrorCode ierr;
6525552f7358SJed Brown 
6526552f7358SJed Brown   PetscFunctionBegin;
652782f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
6528552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
6529552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6530552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
6531552f7358SJed Brown   }
6532552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
653315b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
6534854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
6535552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6536552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
6537ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
6538ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
6539552f7358SJed Brown   }
654082f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
6541ef48cebcSMatthew G. Knepley   if (globalSize) {
6542ef48cebcSMatthew G. Knepley     PetscLayout layout;
6543ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
6544ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
6545ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
6546ef48cebcSMatthew G. Knepley   }
6547552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
6548552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
6549552f7358SJed Brown   PetscFunctionReturn(0);
6550552f7358SJed Brown }
6551552f7358SJed Brown 
655281ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
6553552f7358SJed Brown {
6554552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
6555552f7358SJed Brown   PetscErrorCode ierr;
6556552f7358SJed Brown 
6557552f7358SJed Brown   PetscFunctionBegin;
6558552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
6559552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
65600298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
656181ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
656250ad7711SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Internal(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
656381ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
6564552f7358SJed Brown }
656581ed3555SMatthew G. Knepley 
65668dab3259SMatthew G. Knepley /*@
65677cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
65687cd05799SMatthew G. Knepley 
65697cd05799SMatthew G. Knepley   Input Parameter:
65707cd05799SMatthew G. Knepley . dm   - The DMPlex object
65717cd05799SMatthew G. Knepley 
65727cd05799SMatthew G. Knepley   Output Parameter:
65737cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
65747cd05799SMatthew G. Knepley 
65757cd05799SMatthew G. Knepley   Level: developer
65767cd05799SMatthew G. Knepley 
65777cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
65787cd05799SMatthew G. Knepley @*/
657981ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
658081ed3555SMatthew G. Knepley {
658181ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
658281ed3555SMatthew G. Knepley   PetscErrorCode ierr;
658381ed3555SMatthew G. Knepley 
658481ed3555SMatthew G. Knepley   PetscFunctionBegin;
658581ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
658681ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
6587552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
6588552f7358SJed Brown   PetscFunctionReturn(0);
6589552f7358SJed Brown }
6590552f7358SJed Brown 
659181ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
659281ed3555SMatthew G. Knepley {
659381ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
659481ed3555SMatthew G. Knepley   PetscErrorCode ierr;
659581ed3555SMatthew G. Knepley 
659681ed3555SMatthew G. Knepley   PetscFunctionBegin;
659781ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
659881ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
659981ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
660081ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
660150ad7711SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Internal(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
660281ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
660381ed3555SMatthew G. Knepley }
660481ed3555SMatthew G. Knepley 
66058dab3259SMatthew G. Knepley /*@
66066aa51ba9SJacob Faibussowitsch   DMPlexGetVertexNumbering - Get a global vertex numbering for all vertices on this process
66077cd05799SMatthew G. Knepley 
66087cd05799SMatthew G. Knepley   Input Parameter:
66097cd05799SMatthew G. Knepley . dm   - The DMPlex object
66107cd05799SMatthew G. Knepley 
66117cd05799SMatthew G. Knepley   Output Parameter:
66127cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
66137cd05799SMatthew G. Knepley 
66147cd05799SMatthew G. Knepley   Level: developer
66157cd05799SMatthew G. Knepley 
66167cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
66177cd05799SMatthew G. Knepley @*/
6618552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
6619552f7358SJed Brown {
6620552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6621552f7358SJed Brown   PetscErrorCode ierr;
6622552f7358SJed Brown 
6623552f7358SJed Brown   PetscFunctionBegin;
6624552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
662581ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
6626552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
6627552f7358SJed Brown   PetscFunctionReturn(0);
6628552f7358SJed Brown }
6629552f7358SJed Brown 
66308dab3259SMatthew G. Knepley /*@
66317cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
66327cd05799SMatthew G. Knepley 
66337cd05799SMatthew G. Knepley   Input Parameter:
66347cd05799SMatthew G. Knepley . dm   - The DMPlex object
66357cd05799SMatthew G. Knepley 
66367cd05799SMatthew G. Knepley   Output Parameter:
66377cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
66387cd05799SMatthew G. Knepley 
66397cd05799SMatthew G. Knepley   Level: developer
66407cd05799SMatthew G. Knepley 
66417cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
66427cd05799SMatthew G. Knepley @*/
6643ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
6644ef48cebcSMatthew G. Knepley {
6645ef48cebcSMatthew G. Knepley   IS             nums[4];
6646862913ffSStefano Zampini   PetscInt       depths[4], gdepths[4], starts[4];
6647ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
6648ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
6649ef48cebcSMatthew G. Knepley 
6650ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
6651ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6652ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
66538abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
66548abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
6655862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
6656862913ffSStefano Zampini     PetscInt end;
6657862913ffSStefano Zampini 
6658862913ffSStefano Zampini     depths[d] = depth-d;
6659862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);CHKERRQ(ierr);
6660862913ffSStefano Zampini     if (!(starts[d]-end)) { starts[d] = depths[d] = -1; }
6661862913ffSStefano Zampini   }
6662862913ffSStefano Zampini   ierr = PetscSortIntWithArray(depth+1, starts, depths);CHKERRQ(ierr);
6663862913ffSStefano Zampini   ierr = MPIU_Allreduce(depths, gdepths, depth+1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
6664862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
6665862913ffSStefano Zampini     if (starts[d] >= 0 && depths[d] != gdepths[d]) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Expected depth %D, found %D",depths[d],gdepths[d]);
6666862913ffSStefano Zampini   }
6667ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
6668ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
6669ef48cebcSMatthew G. Knepley 
6670862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);CHKERRQ(ierr);
667150ad7711SMatthew G. Knepley     ierr = DMPlexCreateNumbering_Internal(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
6672ef48cebcSMatthew G. Knepley     shift += gsize;
6673ef48cebcSMatthew G. Knepley   }
6674302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
6675ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
6676ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
6677ef48cebcSMatthew G. Knepley }
6678ef48cebcSMatthew G. Knepley 
667908a22f4bSMatthew G. Knepley 
668008a22f4bSMatthew G. Knepley /*@
668108a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
668208a22f4bSMatthew G. Knepley 
668308a22f4bSMatthew G. Knepley   Input Parameter:
668408a22f4bSMatthew G. Knepley . dm - The DMPlex object
668508a22f4bSMatthew G. Knepley 
668608a22f4bSMatthew G. Knepley   Output Parameter:
668708a22f4bSMatthew G. Knepley . ranks - The rank field
668808a22f4bSMatthew G. Knepley 
668908a22f4bSMatthew G. Knepley   Options Database Keys:
669008a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
669108a22f4bSMatthew G. Knepley 
669208a22f4bSMatthew G. Knepley   Level: intermediate
669308a22f4bSMatthew G. Knepley 
669408a22f4bSMatthew G. Knepley .seealso: DMView()
669508a22f4bSMatthew G. Knepley @*/
669608a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
669708a22f4bSMatthew G. Knepley {
669808a22f4bSMatthew G. Knepley   DM             rdm;
669908a22f4bSMatthew G. Knepley   PetscFE        fe;
670008a22f4bSMatthew G. Knepley   PetscScalar   *r;
670108a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
670208a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
670308a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
670408a22f4bSMatthew G. Knepley 
670508a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
6706f95ace6aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6707f95ace6aSMatthew G. Knepley   PetscValidPointer(ranks, 2);
670808a22f4bSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
670908a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
671008a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
6711f95ace6aSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___rank_", -1, &fe);CHKERRQ(ierr);
671208a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
6713e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
671408a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
6715e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
671608a22f4bSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
671708a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
671808a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
671908a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
672008a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
672108a22f4bSMatthew G. Knepley     PetscScalar *lr;
672208a22f4bSMatthew G. Knepley 
672308a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
672408a22f4bSMatthew G. Knepley     *lr = rank;
672508a22f4bSMatthew G. Knepley   }
672608a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
672708a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
672808a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
672908a22f4bSMatthew G. Knepley }
673008a22f4bSMatthew G. Knepley 
6731ca8062c8SMatthew G. Knepley /*@
673218e14f0cSMatthew G. Knepley   DMPlexCreateLabelField - Create a cell field whose value is the label value for that cell
673318e14f0cSMatthew G. Knepley 
673418e14f0cSMatthew G. Knepley   Input Parameters:
673518e14f0cSMatthew G. Knepley + dm    - The DMPlex
673618e14f0cSMatthew G. Knepley - label - The DMLabel
673718e14f0cSMatthew G. Knepley 
673818e14f0cSMatthew G. Knepley   Output Parameter:
673918e14f0cSMatthew G. Knepley . val - The label value field
674018e14f0cSMatthew G. Knepley 
674118e14f0cSMatthew G. Knepley   Options Database Keys:
674218e14f0cSMatthew G. Knepley . -dm_label_view - Adds the label value field into the DM output from -dm_view using the same viewer
674318e14f0cSMatthew G. Knepley 
674418e14f0cSMatthew G. Knepley   Level: intermediate
674518e14f0cSMatthew G. Knepley 
674618e14f0cSMatthew G. Knepley .seealso: DMView()
674718e14f0cSMatthew G. Knepley @*/
674818e14f0cSMatthew G. Knepley PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
674918e14f0cSMatthew G. Knepley {
675018e14f0cSMatthew G. Knepley   DM             rdm;
675118e14f0cSMatthew G. Knepley   PetscFE        fe;
675218e14f0cSMatthew G. Knepley   PetscScalar   *v;
675318e14f0cSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
675418e14f0cSMatthew G. Knepley   PetscErrorCode ierr;
675518e14f0cSMatthew G. Knepley 
675618e14f0cSMatthew G. Knepley   PetscFunctionBeginUser;
675718e14f0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
675818e14f0cSMatthew G. Knepley   PetscValidPointer(label, 2);
675918e14f0cSMatthew G. Knepley   PetscValidPointer(val, 3);
676018e14f0cSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
676118e14f0cSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
676218e14f0cSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);CHKERRQ(ierr);
676318e14f0cSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "label_value");CHKERRQ(ierr);
6764e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
676518e14f0cSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
6766e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
676718e14f0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
676818e14f0cSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, val);CHKERRQ(ierr);
6769effbd7cbSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *val, "label_value");CHKERRQ(ierr);
677018e14f0cSMatthew G. Knepley   ierr = VecGetArray(*val, &v);CHKERRQ(ierr);
677118e14f0cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
677218e14f0cSMatthew G. Knepley     PetscScalar *lv;
677318e14f0cSMatthew G. Knepley     PetscInt     cval;
677418e14f0cSMatthew G. Knepley 
677518e14f0cSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, v, &lv);CHKERRQ(ierr);
677618e14f0cSMatthew G. Knepley     ierr = DMLabelGetValue(label, c, &cval);CHKERRQ(ierr);
677718e14f0cSMatthew G. Knepley     *lv = cval;
677818e14f0cSMatthew G. Knepley   }
677918e14f0cSMatthew G. Knepley   ierr = VecRestoreArray(*val, &v);CHKERRQ(ierr);
678018e14f0cSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
678118e14f0cSMatthew G. Knepley   PetscFunctionReturn(0);
678218e14f0cSMatthew G. Knepley }
678318e14f0cSMatthew G. Knepley 
678418e14f0cSMatthew G. Knepley /*@
6785ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
6786ca8062c8SMatthew G. Knepley 
678769916449SMatthew G. Knepley   Input Parameter:
678869916449SMatthew G. Knepley . dm - The DMPlex object
6789ca8062c8SMatthew G. Knepley 
6790ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6791ca8062c8SMatthew G. Knepley 
6792ca8062c8SMatthew G. Knepley   Level: developer
6793ca8062c8SMatthew G. Knepley 
67947d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
6795ca8062c8SMatthew G. Knepley @*/
6796ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
6797ca8062c8SMatthew G. Knepley {
6798ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
6799ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
6800ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
680157beb4faSStefano Zampini   PetscInt        pStart, pEnd, p, pp, csize, ssize;
680257beb4faSStefano Zampini   PetscBool       storagecheck = PETSC_TRUE;
6803ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
6804ca8062c8SMatthew G. Knepley 
6805ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6806ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6807ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
6808ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
6809ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
6810ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
6811ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
6812ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
6813ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
6814ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
681542e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
681642e66dfaSMatthew G. Knepley       PetscInt  d;
681742e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
681842e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
681942e66dfaSMatthew G. Knepley       }
6820ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
6821ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
6822ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
6823ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
6824ca8062c8SMatthew G. Knepley       }
682542e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
68268ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
6827ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
68288ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
6829ca8062c8SMatthew G. Knepley         }
6830302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
68318ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
6832ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
68338ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
6834ca8062c8SMatthew G. Knepley         }
6835302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
68368ccfff9cSToby 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]);
68378ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
6838ca8062c8SMatthew G. Knepley       }
683942e66dfaSMatthew G. Knepley     }
684057beb4faSStefano Zampini     ierr = DMPlexGetTreeParent(dm, p, &pp, NULL);CHKERRQ(ierr);
684157beb4faSStefano Zampini     if (p != pp) { storagecheck = PETSC_FALSE; continue; }
6842ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
6843ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
6844ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
6845ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
6846ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
6847ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
684857beb4faSStefano Zampini         ierr = DMPlexGetTreeParent(dm, cone[c], &pp, NULL);CHKERRQ(ierr);
684957beb4faSStefano Zampini         if (cone[c] != pp) { c = 0; break; }
6850ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
6851ca8062c8SMatthew G. Knepley       }
6852ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
68538ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
6854ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
68558ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
6856ca8062c8SMatthew G. Knepley         }
6857302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
68588ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
6859ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
68608ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
6861ca8062c8SMatthew G. Knepley         }
6862302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
68638ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
6864ca8062c8SMatthew G. Knepley       }
6865ca8062c8SMatthew G. Knepley     }
6866ca8062c8SMatthew G. Knepley   }
686757beb4faSStefano Zampini   if (storagecheck) {
6868ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
6869ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
68708ccfff9cSToby Isaac     if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
687157beb4faSStefano Zampini   }
6872ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6873ca8062c8SMatthew G. Knepley }
6874ca8062c8SMatthew G. Knepley 
6875ca8062c8SMatthew G. Knepley /*@
6876ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
6877ca8062c8SMatthew G. Knepley 
6878ca8062c8SMatthew G. Knepley   Input Parameters:
6879ca8062c8SMatthew G. Knepley + dm - The DMPlex object
688058723a97SMatthew G. Knepley - cellHeight - Normally 0
6881ca8062c8SMatthew G. Knepley 
6882ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
688325c50c26SVaclav Hapla   Currently applicable only to homogeneous simplex or tensor meshes.
6884ca8062c8SMatthew G. Knepley 
6885ca8062c8SMatthew G. Knepley   Level: developer
6886ca8062c8SMatthew G. Knepley 
68877d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSymmetry(), DMPlexCheckFaces()
6888ca8062c8SMatthew G. Knepley @*/
688925c50c26SVaclav Hapla PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
6890ca8062c8SMatthew G. Knepley {
689142363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
689225c50c26SVaclav Hapla   PetscBool      isSimplex = PETSC_FALSE;
6893ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
6894ca8062c8SMatthew G. Knepley 
6895ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6896ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6897c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
689825c50c26SVaclav Hapla   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
689925c50c26SVaclav Hapla   if (cStart < cEnd) {
690025c50c26SVaclav Hapla     ierr = DMPlexGetConeSize(dm, cStart, &c);CHKERRQ(ierr);
690125c50c26SVaclav Hapla     isSimplex = c == dim+1 ? PETSC_TRUE : PETSC_FALSE;
690225c50c26SVaclav Hapla   }
6903ca8062c8SMatthew G. Knepley   switch (dim) {
690442363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
690542363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
690642363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
6907ca8062c8SMatthew G. Knepley   default:
69088ccfff9cSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
6909ca8062c8SMatthew G. Knepley   }
691058723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
6911ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
6912ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
6913ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
691458723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
691558723a97SMatthew G. Knepley 
691658723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
691758723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
691858723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
691958723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
692058723a97SMatthew G. Knepley     }
692158723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
69228ccfff9cSToby Isaac     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
6923ca8062c8SMatthew G. Knepley   }
692442363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
692542363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
692642363296SMatthew G. Knepley 
692742363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
692842363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
692942363296SMatthew G. Knepley       const PetscInt p = closure[cl];
693042363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
693142363296SMatthew G. Knepley     }
693242363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
69338ccfff9cSToby Isaac     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
693442363296SMatthew G. Knepley   }
6935ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6936ca8062c8SMatthew G. Knepley }
69379bf0dad6SMatthew G. Knepley 
69389bf0dad6SMatthew G. Knepley /*@
69399bf0dad6SMatthew 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
69409bf0dad6SMatthew G. Knepley 
69419bf0dad6SMatthew G. Knepley   Input Parameters:
69429bf0dad6SMatthew G. Knepley + dm - The DMPlex object
69439bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
69449bf0dad6SMatthew G. Knepley 
69459bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
69469bf0dad6SMatthew G. Knepley 
69479bf0dad6SMatthew G. Knepley   Level: developer
69489bf0dad6SMatthew G. Knepley 
69497d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSymmetry(), DMPlexCheckSkeleton()
69509bf0dad6SMatthew G. Knepley @*/
695125c50c26SVaclav Hapla PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
69529bf0dad6SMatthew G. Knepley {
69533554e41dSMatthew G. Knepley   PetscInt       pMax[4];
6954ab91121cSMatthew G. Knepley   PetscInt       dim, depth, vStart, vEnd, cStart, cEnd, c, h;
69559bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
69569bf0dad6SMatthew G. Knepley 
69579bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
69589bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6959c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6960ab91121cSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
69619bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
696225abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
6963ab91121cSMatthew G. Knepley   for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
69643554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
69653554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
69669bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
69679bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
69689bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
69699bf0dad6SMatthew G. Knepley 
697025abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
69719bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
69729bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
69739bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
69749bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
69759bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
69769bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
69779bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
69789bf0dad6SMatthew G. Knepley       }
69793554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
69808ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
69819bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
69829bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
69839bf0dad6SMatthew G. Knepley 
69849bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
69859bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
69869bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
69879bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
69889bf0dad6SMatthew G. Knepley         }
69898ccfff9cSToby 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);
69909bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
69918ccfff9cSToby 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]);
69929bf0dad6SMatthew G. Knepley         }
69939bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
69949bf0dad6SMatthew G. Knepley       }
69959bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
69969bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
69979bf0dad6SMatthew G. Knepley     }
69983554e41dSMatthew G. Knepley   }
6999552f7358SJed Brown   PetscFunctionReturn(0);
7000552f7358SJed Brown }
70013913d7c8SMatthew G. Knepley 
7002bb6a34a8SMatthew G. Knepley /*@
7003bb6a34a8SMatthew G. Knepley   DMPlexCheckGeometry - Check the geometry of mesh cells
7004bb6a34a8SMatthew G. Knepley 
7005bb6a34a8SMatthew G. Knepley   Input Parameter:
7006bb6a34a8SMatthew G. Knepley . dm - The DMPlex object
7007bb6a34a8SMatthew G. Knepley 
7008bb6a34a8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
7009bb6a34a8SMatthew G. Knepley 
7010bb6a34a8SMatthew G. Knepley   Level: developer
7011bb6a34a8SMatthew G. Knepley 
7012bb6a34a8SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckSkeleton(), DMCheckFaces()
7013bb6a34a8SMatthew G. Knepley @*/
7014bb6a34a8SMatthew G. Knepley PetscErrorCode DMPlexCheckGeometry(DM dm)
7015bb6a34a8SMatthew G. Knepley {
7016bb6a34a8SMatthew G. Knepley   PetscReal      detJ, J[9], refVol = 1.0;
7017bb6a34a8SMatthew G. Knepley   PetscReal      vol;
70186210d0f3SStefano Zampini   PetscInt       dim, depth, d, cStart, cEnd, c, cMax;
7019bb6a34a8SMatthew G. Knepley   PetscErrorCode ierr;
7020bb6a34a8SMatthew G. Knepley 
7021bb6a34a8SMatthew G. Knepley   PetscFunctionBegin;
7022bb6a34a8SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7023bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
7024bb6a34a8SMatthew G. Knepley   for (d = 0; d < dim; ++d) refVol *= 2.0;
7025bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
70266210d0f3SStefano Zampini   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
70276210d0f3SStefano Zampini   cMax = cMax < 0 ? cEnd : cMax;
70286210d0f3SStefano Zampini   for (c = cStart; c < cMax; ++c) {
7029bb6a34a8SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ);CHKERRQ(ierr);
7030bb6a34a8SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted, |J| = %g", c, (double) detJ);
7031bb6a34a8SMatthew G. Knepley     ierr = PetscInfo2(dm, "Cell %D FEM Volume %g\n", c, (double) detJ*refVol);CHKERRQ(ierr);
7032bb6a34a8SMatthew G. Knepley     if (depth > 1) {
7033bb6a34a8SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);CHKERRQ(ierr);
7034bb6a34a8SMatthew G. Knepley       if (vol <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %d is inverted, vol = %g", c, (double) vol);
7035bb6a34a8SMatthew G. Knepley       ierr = PetscInfo2(dm, "Cell %D FVM Volume %g\n", c, (double) vol);CHKERRQ(ierr);
7036bb6a34a8SMatthew G. Knepley     }
7037bb6a34a8SMatthew G. Knepley   }
7038bb6a34a8SMatthew G. Knepley   PetscFunctionReturn(0);
7039bb6a34a8SMatthew G. Knepley }
7040bb6a34a8SMatthew G. Knepley 
704103da9461SVaclav Hapla static PetscErrorCode DMPlexAreAllConePointsInArray_Private(DM dm, PetscInt p, PetscInt npoints, const PetscInt *points, PetscInt *missingPoint)
704203da9461SVaclav Hapla {
704303da9461SVaclav Hapla   PetscInt i,l,n;
704403da9461SVaclav Hapla   const PetscInt *cone;
704503da9461SVaclav Hapla   PetscErrorCode ierr;
704603da9461SVaclav Hapla 
704703da9461SVaclav Hapla   PetscFunctionBegin;
704803da9461SVaclav Hapla   *missingPoint = -1;
704903da9461SVaclav Hapla   ierr = DMPlexGetConeSize(dm, p, &n);CHKERRQ(ierr);
705003da9461SVaclav Hapla   ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
705103da9461SVaclav Hapla   for (i=0; i<n; i++) {
705203da9461SVaclav Hapla     ierr = PetscFindInt(cone[i], npoints, points, &l);CHKERRQ(ierr);
705303da9461SVaclav Hapla     if (l < 0) {
705403da9461SVaclav Hapla       *missingPoint = cone[i];
705503da9461SVaclav Hapla       break;
705603da9461SVaclav Hapla     }
705703da9461SVaclav Hapla   }
705803da9461SVaclav Hapla   PetscFunctionReturn(0);
705903da9461SVaclav Hapla }
706003da9461SVaclav Hapla 
706103da9461SVaclav Hapla /*@
7062*e83a0d2dSVaclav Hapla   DMPlexCheckPointSF - Check that several necessary conditions are met for the point SF of this plex.
706303da9461SVaclav Hapla 
706403da9461SVaclav Hapla   Input Parameters:
706503da9461SVaclav Hapla . dm - The DMPlex object
706603da9461SVaclav Hapla 
7067*e83a0d2dSVaclav Hapla   Notes:
7068*e83a0d2dSVaclav Hapla   This is mainly intended for debugging/testing purposes.
706903da9461SVaclav Hapla 
707003da9461SVaclav Hapla   Level: developer
707103da9461SVaclav Hapla 
707203da9461SVaclav Hapla .seealso: DMGetPointSF(), DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
707303da9461SVaclav Hapla @*/
707403da9461SVaclav Hapla PetscErrorCode DMPlexCheckPointSF(DM dm)
707503da9461SVaclav Hapla {
707603da9461SVaclav Hapla   PetscSF         sf;
707703da9461SVaclav Hapla   PetscInt        d,depth,i,nleaves,p,plo,phi,missingPoint;
707803da9461SVaclav Hapla   const PetscInt *locals;
707903da9461SVaclav Hapla   PetscErrorCode  ierr;
708003da9461SVaclav Hapla 
708103da9461SVaclav Hapla   PetscFunctionBegin;
708203da9461SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
708303da9461SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
708403da9461SVaclav Hapla   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
708503da9461SVaclav Hapla   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &locals, NULL);CHKERRQ(ierr);
708603da9461SVaclav Hapla 
7087ece87651SVaclav Hapla   /* 1) check there are no faces in 2D, cells in 3D, in interface */
708803da9461SVaclav Hapla   ierr = DMPlexGetVTKCellHeight(dm, &d);CHKERRQ(ierr);
708903da9461SVaclav Hapla   ierr = DMPlexGetHeightStratum(dm, d, &plo, &phi);CHKERRQ(ierr);
709003da9461SVaclav Hapla   for (i=0; i<nleaves; i++) {
709103da9461SVaclav Hapla     p = locals[i];
709203da9461SVaclav Hapla     if (p >= plo && p < phi) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point SF contains %d which is a cell",p);
709303da9461SVaclav Hapla   }
7094ece87651SVaclav Hapla 
7095ece87651SVaclav Hapla   /* 2) if some point is in interface, then all its cone points must be also in interface  */
7096ece87651SVaclav Hapla   for (i=0; i<nleaves; i++) {
7097ece87651SVaclav Hapla     p = locals[i];
7098ece87651SVaclav Hapla     ierr = DMPlexAreAllConePointsInArray_Private(dm, p, nleaves, locals, &missingPoint);CHKERRQ(ierr);
7099ece87651SVaclav Hapla     if (missingPoint >= 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point SF contains %d but not %d from its cone",p,missingPoint);
7100ece87651SVaclav Hapla   }
710103da9461SVaclav Hapla   PetscFunctionReturn(0);
710203da9461SVaclav Hapla }
710303da9461SVaclav Hapla 
7104068a5610SStefano Zampini typedef struct cell_stats
7105068a5610SStefano Zampini {
7106068a5610SStefano Zampini   PetscReal min, max, sum, squaresum;
7107068a5610SStefano Zampini   PetscInt  count;
7108068a5610SStefano Zampini } cell_stats_t;
7109068a5610SStefano Zampini 
7110068a5610SStefano Zampini static void cell_stats_reduce(void *a, void *b, int * len, MPI_Datatype *datatype)
7111068a5610SStefano Zampini {
7112068a5610SStefano Zampini   PetscInt i, N = *len;
7113068a5610SStefano Zampini 
7114068a5610SStefano Zampini   for (i = 0; i < N; i++) {
7115068a5610SStefano Zampini     cell_stats_t *A = (cell_stats_t *) a;
7116068a5610SStefano Zampini     cell_stats_t *B = (cell_stats_t *) b;
7117068a5610SStefano Zampini 
7118068a5610SStefano Zampini     B->min = PetscMin(A->min,B->min);
7119068a5610SStefano Zampini     B->max = PetscMax(A->max,B->max);
7120068a5610SStefano Zampini     B->sum += A->sum;
7121068a5610SStefano Zampini     B->squaresum += A->squaresum;
7122068a5610SStefano Zampini     B->count += A->count;
7123068a5610SStefano Zampini   }
7124068a5610SStefano Zampini }
7125068a5610SStefano Zampini 
7126068a5610SStefano Zampini /*@
7127068a5610SStefano Zampini   DMPlexCheckCellShape - Checks the Jacobian of the mapping and computes some minimal statistics.
7128068a5610SStefano Zampini 
7129068a5610SStefano Zampini   Input Parameters:
7130068a5610SStefano Zampini + dm - The DMPlex object
7131068a5610SStefano Zampini - output - If true, statistics will be displayed on stdout
7132068a5610SStefano Zampini 
7133068a5610SStefano Zampini   Note: This is mainly intended for debugging/testing purposes.
7134068a5610SStefano Zampini 
7135068a5610SStefano Zampini   Level: developer
7136068a5610SStefano Zampini 
7137068a5610SStefano Zampini .seealso: DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
7138068a5610SStefano Zampini @*/
7139068a5610SStefano Zampini PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output)
7140068a5610SStefano Zampini {
7141068a5610SStefano Zampini   PetscMPIInt    rank,size;
7142068a5610SStefano Zampini   PetscInt       dim, c, cStart, cEnd, cMax, count = 0;
7143068a5610SStefano Zampini   cell_stats_t   stats, globalStats;
7144068a5610SStefano Zampini   PetscReal      *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
7145068a5610SStefano Zampini   MPI_Comm       comm = PetscObjectComm((PetscObject)dm);
7146068a5610SStefano Zampini   DM             dmCoarse;
7147068a5610SStefano Zampini   PetscErrorCode ierr;
7148068a5610SStefano Zampini 
7149068a5610SStefano Zampini   PetscFunctionBegin;
7150068a5610SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7151068a5610SStefano Zampini   stats.min   = PETSC_MAX_REAL;
7152068a5610SStefano Zampini   stats.max   = PETSC_MIN_REAL;
7153068a5610SStefano Zampini   stats.sum   = stats.squaresum = 0.;
7154068a5610SStefano Zampini   stats.count = 0;
7155068a5610SStefano Zampini 
7156068a5610SStefano Zampini   ierr = DMGetCoordinateDim(dm,&dim);CHKERRQ(ierr);
7157068a5610SStefano Zampini   ierr = PetscMalloc2(dim * dim, &J, dim * dim, &invJ);CHKERRQ(ierr);
7158068a5610SStefano Zampini   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
7159068a5610SStefano Zampini   ierr = DMPlexGetHybridBounds(dm,&cMax,NULL,NULL,NULL);CHKERRQ(ierr);
7160068a5610SStefano Zampini   cMax = cMax < 0 ? cEnd : cMax;
7161068a5610SStefano Zampini   for (c = cStart; c < cMax; c++) {
7162068a5610SStefano Zampini     PetscInt  i;
7163068a5610SStefano Zampini     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;
7164068a5610SStefano Zampini 
7165068a5610SStefano Zampini     ierr = DMPlexComputeCellGeometryAffineFEM(dm,c,NULL,J,invJ,&detJ);CHKERRQ(ierr);
7166068a5610SStefano Zampini     if (detJ < 0.0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted", c);
7167068a5610SStefano Zampini     for (i = 0; i < dim * dim; i++) {
7168068a5610SStefano Zampini       frobJ    += J[i] * J[i];
7169068a5610SStefano Zampini       frobInvJ += invJ[i] * invJ[i];
7170068a5610SStefano Zampini     }
7171068a5610SStefano Zampini     cond2 = frobJ * frobInvJ;
7172068a5610SStefano Zampini     cond  = PetscSqrtReal(cond2);
7173068a5610SStefano Zampini 
7174068a5610SStefano Zampini     stats.min        = PetscMin(stats.min,cond);
7175068a5610SStefano Zampini     stats.max        = PetscMax(stats.max,cond);
7176068a5610SStefano Zampini     stats.sum       += cond;
7177068a5610SStefano Zampini     stats.squaresum += cond2;
7178068a5610SStefano Zampini     stats.count++;
7179068a5610SStefano Zampini   }
7180068a5610SStefano Zampini 
7181068a5610SStefano Zampini   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
7182068a5610SStefano Zampini   if (size > 1) {
7183068a5610SStefano Zampini     PetscMPIInt   blockLengths[2] = {4,1};
7184068a5610SStefano Zampini     MPI_Aint      blockOffsets[2] = {offsetof(cell_stats_t,min),offsetof(cell_stats_t,count)};
7185068a5610SStefano Zampini     MPI_Datatype  blockTypes[2]   = {MPIU_REAL,MPIU_INT}, statType;
7186068a5610SStefano Zampini     MPI_Op        statReduce;
7187068a5610SStefano Zampini 
7188068a5610SStefano Zampini     ierr = MPI_Type_create_struct(2,blockLengths,blockOffsets,blockTypes,&statType);CHKERRQ(ierr);
7189068a5610SStefano Zampini     ierr = MPI_Type_commit(&statType);CHKERRQ(ierr);
7190068a5610SStefano Zampini     ierr = MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);CHKERRQ(ierr);
7191068a5610SStefano Zampini     ierr = MPI_Reduce(&stats,&globalStats,1,statType,statReduce,0,comm);CHKERRQ(ierr);
7192068a5610SStefano Zampini     ierr = MPI_Op_free(&statReduce);CHKERRQ(ierr);
7193068a5610SStefano Zampini     ierr = MPI_Type_free(&statType);CHKERRQ(ierr);
7194068a5610SStefano Zampini   } else {
7195580bdb30SBarry Smith     ierr = PetscArraycpy(&globalStats,&stats,1);CHKERRQ(ierr);
7196068a5610SStefano Zampini   }
7197068a5610SStefano Zampini 
7198068a5610SStefano Zampini   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
7199068a5610SStefano Zampini   if (!rank) {
7200068a5610SStefano Zampini     count = globalStats.count;
7201068a5610SStefano Zampini     min   = globalStats.min;
7202068a5610SStefano Zampini     max   = globalStats.max;
7203068a5610SStefano Zampini     mean  = globalStats.sum / globalStats.count;
7204068a5610SStefano Zampini     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1),0)) : 0.0;
7205068a5610SStefano Zampini   }
7206068a5610SStefano Zampini 
7207068a5610SStefano Zampini   if (output) {
7208068a5610SStefano Zampini     ierr = PetscPrintf(comm,"Mesh with %D cells, shape condition numbers: min = %g, max = %g, mean = %g, stddev = %g\n", count, (double) min, (double) max, (double) mean, (double) stdev);CHKERRQ(ierr);
7209068a5610SStefano Zampini   }
7210068a5610SStefano Zampini   ierr = PetscFree2(J,invJ);CHKERRQ(ierr);
7211068a5610SStefano Zampini 
7212068a5610SStefano Zampini   ierr = DMGetCoarseDM(dm,&dmCoarse);CHKERRQ(ierr);
7213068a5610SStefano Zampini   if (dmCoarse) {
7214068a5610SStefano Zampini     PetscBool isplex;
7215068a5610SStefano Zampini 
7216068a5610SStefano Zampini     ierr = PetscObjectTypeCompare((PetscObject)dmCoarse,DMPLEX,&isplex);CHKERRQ(ierr);
7217068a5610SStefano Zampini     if (isplex) {
7218068a5610SStefano Zampini       ierr = DMPlexCheckCellShape(dmCoarse,output);CHKERRQ(ierr);
7219068a5610SStefano Zampini     }
7220068a5610SStefano Zampini   }
7221068a5610SStefano Zampini   PetscFunctionReturn(0);
7222068a5610SStefano Zampini }
7223068a5610SStefano Zampini 
7224bceba477SMatthew G. Knepley /* Pointwise interpolation
7225bceba477SMatthew G. Knepley      Just code FEM for now
7226bceba477SMatthew G. Knepley      u^f = I u^c
72274ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
72284ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
72294ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
7230bceba477SMatthew G. Knepley */
7231bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
7232bceba477SMatthew G. Knepley {
7233bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
7234bceba477SMatthew G. Knepley   PetscInt       m, n;
7235a063dac3SMatthew G. Knepley   void          *ctx;
723668132eb9SMatthew G. Knepley   DM             cdm;
7237fd194bc8SStefano Zampini   PetscBool      regular, ismatis;
7238bceba477SMatthew G. Knepley   PetscErrorCode ierr;
7239bceba477SMatthew G. Knepley 
7240bceba477SMatthew G. Knepley   PetscFunctionBegin;
7241e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
7242bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
7243e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
7244bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
724568132eb9SMatthew G. Knepley 
7246fd194bc8SStefano Zampini   ierr = PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);CHKERRQ(ierr);
7247bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
7248bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
7249fd194bc8SStefano Zampini   ierr = MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);CHKERRQ(ierr);
7250a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
725168132eb9SMatthew G. Knepley 
7252a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
725368132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
725468132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
725568132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
725668132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
72574db47ee9SStefano Zampini   if (scaling) {
72585d1c2e58SMatthew G. Knepley     /* Use naive scaling */
72595d1c2e58SMatthew G. Knepley     ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
72604db47ee9SStefano Zampini   }
7261a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
7262a063dac3SMatthew G. Knepley }
7263bceba477SMatthew G. Knepley 
72646dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
7265a063dac3SMatthew G. Knepley {
726690748bafSMatthew G. Knepley   PetscErrorCode ierr;
72676dbf9973SLawrence Mitchell   VecScatter     ctx;
726890748bafSMatthew G. Knepley 
7269a063dac3SMatthew G. Knepley   PetscFunctionBegin;
72706dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
72716dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
72726dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
7273bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
7274bceba477SMatthew G. Knepley }
7275bceba477SMatthew G. Knepley 
7276bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
7277bd041c0cSMatthew G. Knepley {
7278bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
7279bd041c0cSMatthew G. Knepley   PetscInt       m, n;
7280bd041c0cSMatthew G. Knepley   void          *ctx;
7281bd041c0cSMatthew G. Knepley   DM             cdm;
7282bd041c0cSMatthew G. Knepley   PetscBool      regular;
7283bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
7284bd041c0cSMatthew G. Knepley 
7285bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
7286e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
7287bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
7288e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
7289bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
7290bd041c0cSMatthew G. Knepley 
7291bd041c0cSMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
7292bd041c0cSMatthew G. Knepley   ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
7293bd041c0cSMatthew G. Knepley   ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
7294bd041c0cSMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
7295bd041c0cSMatthew G. Knepley 
7296bd041c0cSMatthew G. Knepley   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
7297bd041c0cSMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
7298bd041c0cSMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
7299bd041c0cSMatthew G. Knepley   else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
7300bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
7301bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
7302bd041c0cSMatthew G. Knepley }
7303bd041c0cSMatthew G. Knepley 
73040aef6b92SMatthew G. Knepley /*@
73050aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
73060aef6b92SMatthew G. Knepley 
73070aef6b92SMatthew G. Knepley   Input Parameter:
73080aef6b92SMatthew G. Knepley . dm - The DMPlex object
73090aef6b92SMatthew G. Knepley 
73100aef6b92SMatthew G. Knepley   Output Parameter:
73110aef6b92SMatthew G. Knepley . regular - The flag
73120aef6b92SMatthew G. Knepley 
73130aef6b92SMatthew G. Knepley   Level: intermediate
73140aef6b92SMatthew G. Knepley 
73150aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
73160aef6b92SMatthew G. Knepley @*/
73170aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
73180aef6b92SMatthew G. Knepley {
73190aef6b92SMatthew G. Knepley   PetscFunctionBegin;
73200aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73210aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
73220aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
73230aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
73240aef6b92SMatthew G. Knepley }
73250aef6b92SMatthew G. Knepley 
73260aef6b92SMatthew G. Knepley /*@
73270aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
73280aef6b92SMatthew G. Knepley 
73290aef6b92SMatthew G. Knepley   Input Parameters:
73300aef6b92SMatthew G. Knepley + dm - The DMPlex object
73310aef6b92SMatthew G. Knepley - regular - The flag
73320aef6b92SMatthew G. Knepley 
73330aef6b92SMatthew G. Knepley   Level: intermediate
73340aef6b92SMatthew G. Knepley 
73350aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
73360aef6b92SMatthew G. Knepley @*/
73370aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
73380aef6b92SMatthew G. Knepley {
73390aef6b92SMatthew G. Knepley   PetscFunctionBegin;
73400aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
73410aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
73420aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
73430aef6b92SMatthew G. Knepley }
73440aef6b92SMatthew G. Knepley 
7345f7c74593SToby Isaac /* anchors */
7346a68b90caSToby Isaac /*@
7347f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
7348f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
7349a68b90caSToby Isaac 
7350e228b242SToby Isaac   not collective
7351a68b90caSToby Isaac 
7352a68b90caSToby Isaac   Input Parameters:
7353a68b90caSToby Isaac . dm - The DMPlex object
7354a68b90caSToby Isaac 
7355a68b90caSToby Isaac   Output Parameters:
7356a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
7357a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
7358a68b90caSToby Isaac 
7359a68b90caSToby Isaac 
7360a68b90caSToby Isaac   Level: intermediate
7361a68b90caSToby Isaac 
7362f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
7363a68b90caSToby Isaac @*/
7364a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
7365a68b90caSToby Isaac {
7366a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
736741e6d900SToby Isaac   PetscErrorCode ierr;
7368a68b90caSToby Isaac 
7369a68b90caSToby Isaac   PetscFunctionBegin;
7370a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
737141e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
7372a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
7373a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
7374a68b90caSToby Isaac   PetscFunctionReturn(0);
7375a68b90caSToby Isaac }
7376a68b90caSToby Isaac 
7377a68b90caSToby Isaac /*@
7378f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
7379f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
7380a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
7381a68b90caSToby Isaac 
7382a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
7383f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
7384a68b90caSToby Isaac 
7385e228b242SToby Isaac   collective on dm
7386a68b90caSToby Isaac 
7387a68b90caSToby Isaac   Input Parameters:
7388a68b90caSToby Isaac + dm - The DMPlex object
7389e228b242SToby 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).
7390e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
7391a68b90caSToby Isaac 
7392a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
7393a68b90caSToby Isaac 
7394a68b90caSToby Isaac   Level: intermediate
7395a68b90caSToby Isaac 
7396f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
7397a68b90caSToby Isaac @*/
7398a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
7399a68b90caSToby Isaac {
7400a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
7401e228b242SToby Isaac   PetscMPIInt    result;
7402a68b90caSToby Isaac   PetscErrorCode ierr;
7403a68b90caSToby Isaac 
7404a68b90caSToby Isaac   PetscFunctionBegin;
7405a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7406e228b242SToby Isaac   if (anchorSection) {
7407e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
7408e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
7409f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
7410e228b242SToby Isaac   }
7411e228b242SToby Isaac   if (anchorIS) {
7412e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
7413e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
7414f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
7415e228b242SToby Isaac   }
7416a68b90caSToby Isaac 
7417a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
7418a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
7419a68b90caSToby Isaac   plex->anchorSection = anchorSection;
7420a68b90caSToby Isaac 
7421a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
7422a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
7423a68b90caSToby Isaac   plex->anchorIS = anchorIS;
7424a68b90caSToby Isaac 
7425a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
7426a68b90caSToby Isaac   if (anchorIS && anchorSection) {
7427a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
7428a68b90caSToby Isaac     const PetscInt *anchors;
7429a68b90caSToby Isaac 
7430a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
7431a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
7432a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
7433a68b90caSToby Isaac     for (a = 0; a < size; a++) {
7434a68b90caSToby Isaac       PetscInt p;
7435a68b90caSToby Isaac 
7436a68b90caSToby Isaac       p = anchors[a];
7437a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
7438a68b90caSToby Isaac         PetscInt dof;
7439a68b90caSToby Isaac 
7440a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
7441a68b90caSToby Isaac         if (dof) {
7442a68b90caSToby Isaac           PetscErrorCode ierr2;
7443a68b90caSToby Isaac 
7444a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
74458ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
7446a68b90caSToby Isaac         }
7447a68b90caSToby Isaac       }
7448a68b90caSToby Isaac     }
7449a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
7450a68b90caSToby Isaac   }
7451a68b90caSToby Isaac #endif
7452f7c74593SToby Isaac   /* reset the generic constraints */
7453f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
7454a68b90caSToby Isaac   PetscFunctionReturn(0);
7455a68b90caSToby Isaac }
7456a68b90caSToby Isaac 
7457f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
7458a68b90caSToby Isaac {
7459f7c74593SToby Isaac   PetscSection anchorSection;
74606995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
7461a68b90caSToby Isaac   PetscErrorCode ierr;
7462a68b90caSToby Isaac 
7463a68b90caSToby Isaac   PetscFunctionBegin;
7464a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7465a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
7466e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
7467a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
74686995de1eSToby Isaac   if (numFields) {
7469719ab38cSToby Isaac     PetscInt f;
7470a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
7471719ab38cSToby Isaac 
7472719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
7473719ab38cSToby Isaac       PetscInt numComp;
7474719ab38cSToby Isaac 
7475719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
7476719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
7477719ab38cSToby Isaac     }
74786995de1eSToby Isaac   }
7479a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
74806995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
74816995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
74826995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
74836995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
7484a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
7485a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
7486a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
7487a68b90caSToby Isaac     if (dof) {
7488a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
7489a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
7490a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
7491a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
7492a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
7493a68b90caSToby Isaac       }
7494a68b90caSToby Isaac     }
7495a68b90caSToby Isaac   }
7496a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
7497a68b90caSToby Isaac   PetscFunctionReturn(0);
7498a68b90caSToby Isaac }
7499a68b90caSToby Isaac 
7500f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
7501a68b90caSToby Isaac {
7502f7c74593SToby Isaac   PetscSection aSec;
75030ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
75040ac89760SToby Isaac   const PetscInt *anchors;
75050ac89760SToby Isaac   PetscInt numFields, f;
750666ad2231SToby Isaac   IS aIS;
75070ac89760SToby Isaac   PetscErrorCode ierr;
75080ac89760SToby Isaac 
75090ac89760SToby Isaac   PetscFunctionBegin;
75100ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
75110ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
75120ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
75130ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
75140ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
7515302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
7516a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
751766ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
75186995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
75196995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
75200ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
75210ac89760SToby Isaac   i[0] = 0;
75220ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
75230ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
7524f19733c5SToby Isaac     PetscInt rDof, rOff, r;
7525f19733c5SToby Isaac 
7526f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
7527f19733c5SToby Isaac     if (!rDof) continue;
7528f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
75290ac89760SToby Isaac     if (numFields) {
75300ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
75310ac89760SToby Isaac         annz = 0;
7532f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
7533f19733c5SToby Isaac           a = anchors[rOff + r];
75340ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
75350ac89760SToby Isaac           annz += aDof;
75360ac89760SToby Isaac         }
75370ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
75380ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
75390ac89760SToby Isaac         for (q = 0; q < dof; q++) {
75400ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
75410ac89760SToby Isaac         }
75420ac89760SToby Isaac       }
75430ac89760SToby Isaac     }
75440ac89760SToby Isaac     else {
75450ac89760SToby Isaac       annz = 0;
75460ac89760SToby Isaac       for (q = 0; q < dof; q++) {
75470ac89760SToby Isaac         a = anchors[off + q];
75480ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
75490ac89760SToby Isaac         annz += aDof;
75500ac89760SToby Isaac       }
75510ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
75520ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
75530ac89760SToby Isaac       for (q = 0; q < dof; q++) {
75540ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
75550ac89760SToby Isaac       }
75560ac89760SToby Isaac     }
75570ac89760SToby Isaac   }
75580ac89760SToby Isaac   nnz = i[m];
75590ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
75600ac89760SToby Isaac   offset = 0;
75610ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
75620ac89760SToby Isaac     if (numFields) {
75630ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
75640ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
75650ac89760SToby Isaac         for (q = 0; q < dof; q++) {
75660ac89760SToby Isaac           PetscInt rDof, rOff, r;
75670ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
75680ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
75690ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
75700ac89760SToby Isaac             PetscInt s;
75710ac89760SToby Isaac 
75720ac89760SToby Isaac             a = anchors[rOff + r];
75730ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
75740ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
75750ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
75760ac89760SToby Isaac               j[offset++] = aOff + s;
75770ac89760SToby Isaac             }
75780ac89760SToby Isaac           }
75790ac89760SToby Isaac         }
75800ac89760SToby Isaac       }
75810ac89760SToby Isaac     }
75820ac89760SToby Isaac     else {
75830ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
75840ac89760SToby Isaac       for (q = 0; q < dof; q++) {
75850ac89760SToby Isaac         PetscInt rDof, rOff, r;
75860ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
75870ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
75880ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
75890ac89760SToby Isaac           PetscInt s;
75900ac89760SToby Isaac 
75910ac89760SToby Isaac           a = anchors[rOff + r];
75920ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
75930ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
75940ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
75950ac89760SToby Isaac             j[offset++] = aOff + s;
75960ac89760SToby Isaac           }
75970ac89760SToby Isaac         }
75980ac89760SToby Isaac       }
75990ac89760SToby Isaac     }
76000ac89760SToby Isaac   }
76010ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
760225570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
760325570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
760466ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
76050ac89760SToby Isaac   PetscFunctionReturn(0);
76060ac89760SToby Isaac }
76070ac89760SToby Isaac 
760866ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
760966ad2231SToby Isaac {
7610f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
7611f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
761266ad2231SToby Isaac   Mat            cMat;
761366ad2231SToby Isaac   PetscErrorCode ierr;
761466ad2231SToby Isaac 
761566ad2231SToby Isaac   PetscFunctionBegin;
761666ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7617a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
761866ad2231SToby Isaac   if (anchorSection) {
761944a7f3ddSMatthew G. Knepley     PetscInt Nf;
7620e228b242SToby Isaac 
762192fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
7622f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
7623f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
762444a7f3ddSMatthew G. Knepley     ierr = DMGetNumFields(dm,&Nf);CHKERRQ(ierr);
762544a7f3ddSMatthew G. Knepley     if (Nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
762666ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
762766ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
762866ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
762966ad2231SToby Isaac   }
763066ad2231SToby Isaac   PetscFunctionReturn(0);
763166ad2231SToby Isaac }
7632a93c429eSMatthew G. Knepley 
7633a93c429eSMatthew G. Knepley PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
7634a93c429eSMatthew G. Knepley {
7635a93c429eSMatthew G. Knepley   IS             subis;
7636a93c429eSMatthew G. Knepley   PetscSection   section, subsection;
7637a93c429eSMatthew G. Knepley   PetscErrorCode ierr;
7638a93c429eSMatthew G. Knepley 
7639a93c429eSMatthew G. Knepley   PetscFunctionBegin;
764092fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
7641a93c429eSMatthew G. Knepley   if (!section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
7642a93c429eSMatthew G. Knepley   if (!subdm)   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
7643a93c429eSMatthew G. Knepley   /* Create subdomain */
7644a93c429eSMatthew G. Knepley   ierr = DMPlexFilter(dm, label, value, subdm);CHKERRQ(ierr);
7645a93c429eSMatthew G. Knepley   /* Create submodel */
7646a93c429eSMatthew G. Knepley   ierr = DMPlexCreateSubpointIS(*subdm, &subis);CHKERRQ(ierr);
7647a93c429eSMatthew G. Knepley   ierr = PetscSectionCreateSubmeshSection(section, subis, &subsection);CHKERRQ(ierr);
7648a93c429eSMatthew G. Knepley   ierr = ISDestroy(&subis);CHKERRQ(ierr);
764992fd8e1eSJed Brown   ierr = DMSetLocalSection(*subdm, subsection);CHKERRQ(ierr);
7650a93c429eSMatthew G. Knepley   ierr = PetscSectionDestroy(&subsection);CHKERRQ(ierr);
7651e5e52638SMatthew G. Knepley   ierr = DMCopyDisc(dm, *subdm);CHKERRQ(ierr);
7652a93c429eSMatthew G. Knepley   /* Create map from submodel to global model */
7653a93c429eSMatthew G. Knepley   if (is) {
7654a93c429eSMatthew G. Knepley     PetscSection    sectionGlobal, subsectionGlobal;
7655a93c429eSMatthew G. Knepley     IS              spIS;
7656a93c429eSMatthew G. Knepley     const PetscInt *spmap;
7657a93c429eSMatthew G. Knepley     PetscInt       *subIndices;
7658a93c429eSMatthew G. Knepley     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
7659a93c429eSMatthew G. Knepley     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];
7660a93c429eSMatthew G. Knepley 
7661a93c429eSMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(*subdm, &spIS);CHKERRQ(ierr);
7662a93c429eSMatthew G. Knepley     ierr = ISGetIndices(spIS, &spmap);CHKERRQ(ierr);
7663a93c429eSMatthew G. Knepley     ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
76646f0eb057SJed Brown     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
76656f0eb057SJed Brown     ierr = DMGetGlobalSection(*subdm, &subsectionGlobal);CHKERRQ(ierr);
7666a93c429eSMatthew G. Knepley     ierr = PetscSectionGetChart(subsection, &pStart, &pEnd);CHKERRQ(ierr);
7667a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
7668a93c429eSMatthew G. Knepley       PetscInt gdof, pSubSize  = 0;
7669a93c429eSMatthew G. Knepley 
7670a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
7671a93c429eSMatthew G. Knepley       if (gdof > 0) {
7672a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
7673a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof;
7674a93c429eSMatthew G. Knepley 
7675a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldDof(subsection, p, f, &fdof);CHKERRQ(ierr);
7676a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);CHKERRQ(ierr);
7677a93c429eSMatthew G. Knepley           pSubSize += fdof-fcdof;
7678a93c429eSMatthew G. Knepley         }
7679a93c429eSMatthew G. Knepley         subSize += pSubSize;
7680a93c429eSMatthew G. Knepley         if (pSubSize) {
7681a93c429eSMatthew G. Knepley           if (bs < 0) {
7682a93c429eSMatthew G. Knepley             bs = pSubSize;
7683a93c429eSMatthew G. Knepley           } else if (bs != pSubSize) {
7684a93c429eSMatthew G. Knepley             /* Layout does not admit a pointwise block size */
7685a93c429eSMatthew G. Knepley             bs = 1;
7686a93c429eSMatthew G. Knepley           }
7687a93c429eSMatthew G. Knepley         }
7688a93c429eSMatthew G. Knepley       }
7689a93c429eSMatthew G. Knepley     }
7690a93c429eSMatthew G. Knepley     /* Must have same blocksize on all procs (some might have no points) */
7691a93c429eSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
7692a93c429eSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
7693a93c429eSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
7694a93c429eSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
7695a93c429eSMatthew G. Knepley     ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr);
7696a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
7697a93c429eSMatthew G. Knepley       PetscInt gdof, goff;
7698a93c429eSMatthew G. Knepley 
7699a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(subsectionGlobal, p, &gdof);CHKERRQ(ierr);
7700a93c429eSMatthew G. Knepley       if (gdof > 0) {
7701a93c429eSMatthew G. Knepley         const PetscInt point = spmap[p];
7702a93c429eSMatthew G. Knepley 
7703a93c429eSMatthew G. Knepley         ierr = PetscSectionGetOffset(sectionGlobal, point, &goff);CHKERRQ(ierr);
7704a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
7705a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof, fc, f2, poff = 0;
7706a93c429eSMatthew G. Knepley 
7707a93c429eSMatthew G. Knepley           /* Can get rid of this loop by storing field information in the global section */
7708a93c429eSMatthew G. Knepley           for (f2 = 0; f2 < f; ++f2) {
7709a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldDof(section, p, f2, &fdof);CHKERRQ(ierr);
7710a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);CHKERRQ(ierr);
7711a93c429eSMatthew G. Knepley             poff += fdof-fcdof;
7712a93c429eSMatthew G. Knepley           }
7713a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
7714a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
7715a93c429eSMatthew G. Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
7716a93c429eSMatthew G. Knepley             subIndices[subOff] = goff+poff+fc;
7717a93c429eSMatthew G. Knepley           }
7718a93c429eSMatthew G. Knepley         }
7719a93c429eSMatthew G. Knepley       }
7720a93c429eSMatthew G. Knepley     }
7721a93c429eSMatthew G. Knepley     ierr = ISRestoreIndices(spIS, &spmap);CHKERRQ(ierr);
7722a93c429eSMatthew G. Knepley     ierr = ISDestroy(&spIS);CHKERRQ(ierr);
7723a93c429eSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);CHKERRQ(ierr);
7724a93c429eSMatthew G. Knepley     if (bs > 1) {
7725a93c429eSMatthew G. Knepley       /* We need to check that the block size does not come from non-contiguous fields */
7726a93c429eSMatthew G. Knepley       PetscInt i, j, set = 1;
7727a93c429eSMatthew G. Knepley       for (i = 0; i < subSize; i += bs) {
7728a93c429eSMatthew G. Knepley         for (j = 0; j < bs; ++j) {
7729a93c429eSMatthew G. Knepley           if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;}
7730a93c429eSMatthew G. Knepley         }
7731a93c429eSMatthew G. Knepley       }
7732a93c429eSMatthew G. Knepley       if (set) {ierr = ISSetBlockSize(*is, bs);CHKERRQ(ierr);}
7733a93c429eSMatthew G. Knepley     }
7734a93c429eSMatthew G. Knepley     /* Attach nullspace */
7735a93c429eSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
7736a93c429eSMatthew G. Knepley       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
7737a93c429eSMatthew G. Knepley       if ((*subdm)->nullspaceConstructors[f]) break;
7738a93c429eSMatthew G. Knepley     }
7739a93c429eSMatthew G. Knepley     if (f < Nf) {
7740a93c429eSMatthew G. Knepley       MatNullSpace nullSpace;
7741a93c429eSMatthew G. Knepley 
7742a93c429eSMatthew G. Knepley       ierr = (*(*subdm)->nullspaceConstructors[f])(*subdm, f, &nullSpace);CHKERRQ(ierr);
7743a93c429eSMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr);
7744a93c429eSMatthew G. Knepley       ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
7745a93c429eSMatthew G. Knepley     }
7746a93c429eSMatthew G. Knepley   }
7747a93c429eSMatthew G. Knepley   PetscFunctionReturn(0);
7748a93c429eSMatthew G. Knepley }
7749