xref: /petsc/src/dm/impls/plex/plex.c (revision f0cfc026409476e9bc8bbc8fc904e2930eb9d6f4)
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 {
104485ad865SMatthew G. Knepley   PetscInt       dim, pStart, pEnd, vStart, vEnd, cStart, cEnd, cMax;
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);
113485ad865SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
114485ad865SMatthew G. Knepley   cEnd = cMax < 0 ? cEnd : cMax;
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);}
588087ef6b2SMatthew G. Knepley           ierr = PetscViewerASCIIPrintf(viewer, "%g", (double) 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     }
763087ef6b2SMatthew G. Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\\begin{tikzpicture}[scale = %g,font=\\fontsize{8}{8}\\selectfont]\n", (double) 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);}
786087ef6b2SMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double) 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);}
870087ef6b2SMatthew G. Knepley         ierr = PetscViewerASCIISynchronizedPrintf(viewer, "%g", (double) 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);
948b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
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);
1024b674149eSJunchao Zhang #if defined(PETSC_HAVE_MPI_PROCESS_SHARED_MEMORY)
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;
1035d80ece95SMatthew G. Knepley     PetscInt   *sizes, *hybsizes, *ghostsizes;
1036f73eea6eSMatthew G. Knepley     PetscInt    locDepth, depth, cellHeight, dim, d, pMax[4];
1037d80ece95SMatthew G. Knepley     PetscInt    pStart, pEnd, p, gcStart, gcEnd, gcNum;
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);
1053d80ece95SMatthew G. Knepley     ierr = DMPlexGetGhostCellStratum(dm, &gcStart, &gcEnd);CHKERRQ(ierr);
1054d80ece95SMatthew G. Knepley     gcNum = gcEnd - gcStart;
1055d80ece95SMatthew G. Knepley     ierr = PetscCalloc3(size,&sizes,size,&hybsizes,size,&ghostsizes);CHKERRQ(ierr);
1056552f7358SJed Brown     if (depth == 1) {
1057552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
1058552f7358SJed Brown       pEnd = pEnd - pStart;
1059a04427b4SStefano Zampini       pMax[0] -= pStart;
1060552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1061a04427b4SStefano Zampini       ierr = MPI_Gather(&pMax[0], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1062d80ece95SMatthew G. Knepley       ierr = MPI_Gather(&gcNum, 1, MPIU_INT, ghostsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1063f347f43bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer, "  %d-cells:", 0);CHKERRQ(ierr);
1064a04427b4SStefano Zampini       for (p = 0; p < size; ++p) {
1065a04427b4SStefano Zampini         if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " %D (%D)", sizes[p], sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
1066a04427b4SStefano Zampini         else                  {ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);}
1067a04427b4SStefano Zampini       }
1068552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
1069552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, 0, &pStart, &pEnd);CHKERRQ(ierr);
1070552f7358SJed Brown       pEnd = pEnd - pStart;
1071a04427b4SStefano Zampini       pMax[depth] -= pStart;
1072552f7358SJed Brown       ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1073a04427b4SStefano Zampini       ierr = MPI_Gather(&pMax[depth], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1074552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", dim);CHKERRQ(ierr);
1075a04427b4SStefano Zampini       for (p = 0; p < size; ++p) {
1076d80ece95SMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);
1077d80ece95SMatthew G. Knepley         if (hybsizes[p] >= 0)   {ierr = PetscViewerASCIIPrintf(viewer, " (%D)", sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
1078d80ece95SMatthew G. Knepley         if (ghostsizes[p] > 0) {ierr = PetscViewerASCIIPrintf(viewer, " [%D]", ghostsizes[p]);CHKERRQ(ierr);}
1079a04427b4SStefano Zampini       }
1080552f7358SJed Brown       ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
1081552f7358SJed Brown     } else {
1082cbb7f117SMark Adams       PetscMPIInt rank;
1083cbb7f117SMark Adams       ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1084552f7358SJed Brown       for (d = 0; d <= dim; d++) {
1085552f7358SJed Brown         ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
1086834065abSMatthew G. Knepley         pEnd    -= pStart;
1087834065abSMatthew G. Knepley         pMax[d] -= pStart;
1088552f7358SJed Brown         ierr = MPI_Gather(&pEnd, 1, MPIU_INT, sizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1089834065abSMatthew G. Knepley         ierr = MPI_Gather(&pMax[d], 1, MPIU_INT, hybsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);
1090d80ece95SMatthew G. Knepley         if (d == dim) {ierr = MPI_Gather(&gcNum, 1, MPIU_INT, ghostsizes, 1, MPIU_INT, 0, comm);CHKERRQ(ierr);}
1091552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "  %D-cells:", d);CHKERRQ(ierr);
1092834065abSMatthew G. Knepley         for (p = 0; p < size; ++p) {
1093cbb7f117SMark Adams           if (!rank) {
1094d80ece95SMatthew G. Knepley             ierr = PetscViewerASCIIPrintf(viewer, " %D", sizes[p]);CHKERRQ(ierr);
1095d80ece95SMatthew G. Knepley             if (hybsizes[p] >= 0) {ierr = PetscViewerASCIIPrintf(viewer, " (%D)", sizes[p] - hybsizes[p]);CHKERRQ(ierr);}
1096d80ece95SMatthew G. Knepley             if (d == dim && ghostsizes[p] > 0) {ierr = PetscViewerASCIIPrintf(viewer, " [%D]", ghostsizes[p]);CHKERRQ(ierr);}
1097834065abSMatthew G. Knepley           }
1098cbb7f117SMark Adams         }
1099552f7358SJed Brown         ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
1100552f7358SJed Brown       }
1101552f7358SJed Brown     }
1102d80ece95SMatthew G. Knepley     ierr = PetscFree3(sizes,hybsizes,ghostsizes);CHKERRQ(ierr);
1103c58f1c22SToby Isaac     ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
1104a57dd577SMatthew G Knepley     if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Labels:\n");CHKERRQ(ierr);}
1105a57dd577SMatthew G Knepley     for (l = 0; l < numLabels; ++l) {
1106a57dd577SMatthew G Knepley       DMLabel         label;
1107a57dd577SMatthew G Knepley       const char     *name;
1108a57dd577SMatthew G Knepley       IS              valueIS;
1109a57dd577SMatthew G Knepley       const PetscInt *values;
1110a57dd577SMatthew G Knepley       PetscInt        numValues, v;
1111a57dd577SMatthew G Knepley 
1112c58f1c22SToby Isaac       ierr = DMGetLabelName(dm, l, &name);CHKERRQ(ierr);
1113c58f1c22SToby Isaac       ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1114a57dd577SMatthew G Knepley       ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
1115d72f73ffSMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, "  %s: %D strata with value/size (", name, numValues);CHKERRQ(ierr);
1116a57dd577SMatthew G Knepley       ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
1117a57dd577SMatthew G Knepley       ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
1118120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);CHKERRQ(ierr);
1119a57dd577SMatthew G Knepley       for (v = 0; v < numValues; ++v) {
1120a57dd577SMatthew G Knepley         PetscInt size;
1121a57dd577SMatthew G Knepley 
1122a57dd577SMatthew G Knepley         ierr = DMLabelGetStratumSize(label, values[v], &size);CHKERRQ(ierr);
1123a57dd577SMatthew G Knepley         if (v > 0) {ierr = PetscViewerASCIIPrintf(viewer, ", ");CHKERRQ(ierr);}
1124d72f73ffSMatthew G. Knepley         ierr = PetscViewerASCIIPrintf(viewer, "%D (%D)", values[v], size);CHKERRQ(ierr);
1125a57dd577SMatthew G Knepley       }
1126a57dd577SMatthew G Knepley       ierr = PetscViewerASCIIPrintf(viewer, ")\n");CHKERRQ(ierr);
1127120dea56SMatthew G. Knepley       ierr = PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);CHKERRQ(ierr);
1128a57dd577SMatthew G Knepley       ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
11294d41221fSMatthew G Knepley       ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
1130a57dd577SMatthew G Knepley     }
113134aa8a36SMatthew G. Knepley     /* If no fields are specified, people do not want to see adjacency */
113234aa8a36SMatthew G. Knepley     if (dm->Nf) {
113334aa8a36SMatthew G. Knepley       PetscInt f;
113434aa8a36SMatthew G. Knepley 
113534aa8a36SMatthew G. Knepley       for (f = 0; f < dm->Nf; ++f) {
113634aa8a36SMatthew G. Knepley         const char *name;
113734aa8a36SMatthew G. Knepley 
113834aa8a36SMatthew G. Knepley         ierr = PetscObjectGetName(dm->fields[f].disc, &name);CHKERRQ(ierr);
113934aa8a36SMatthew G. Knepley         if (numLabels) {ierr = PetscViewerASCIIPrintf(viewer, "Field %s:\n", name);CHKERRQ(ierr);}
114034aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
114134aa8a36SMatthew G. Knepley         if (dm->fields[f].label) {ierr = DMLabelView(dm->fields[f].label, viewer);CHKERRQ(ierr);}
114234aa8a36SMatthew G. Knepley         if (dm->fields[f].adjacency[0]) {
114334aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM++\n");CHKERRQ(ierr);}
1144ed59e46eSMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FVM\n");CHKERRQ(ierr);}
114534aa8a36SMatthew G. Knepley         } else {
114634aa8a36SMatthew G. Knepley           if (dm->fields[f].adjacency[1]) {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FEM\n");CHKERRQ(ierr);}
114734aa8a36SMatthew G. Knepley           else                            {ierr = PetscViewerASCIIPrintf(viewer, "adjacency FUNKY\n");CHKERRQ(ierr);}
114834aa8a36SMatthew G. Knepley         }
114934aa8a36SMatthew G. Knepley         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
115034aa8a36SMatthew G. Knepley       }
115134aa8a36SMatthew G. Knepley     }
1152a8fb8f29SToby Isaac     ierr = DMGetCoarseDM(dm, &cdm);CHKERRQ(ierr);
11538e7ff633SMatthew G. Knepley     if (cdm) {
11548e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
11558e7ff633SMatthew G. Knepley       ierr = DMPlexView_Ascii(cdm, viewer);CHKERRQ(ierr);
11568e7ff633SMatthew G. Knepley       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
11578e7ff633SMatthew G. Knepley     }
1158552f7358SJed Brown   }
1159552f7358SJed Brown   PetscFunctionReturn(0);
1160552f7358SJed Brown }
1161552f7358SJed Brown 
11627cd05799SMatthew G. Knepley static PetscErrorCode DMPlexView_Draw(DM dm, PetscViewer viewer)
1163e412dcbdSMatthew G. Knepley {
1164e412dcbdSMatthew G. Knepley   PetscDraw          draw;
1165e412dcbdSMatthew G. Knepley   DM                 cdm;
1166e412dcbdSMatthew G. Knepley   PetscSection       coordSection;
1167e412dcbdSMatthew G. Knepley   Vec                coordinates;
1168e412dcbdSMatthew G. Knepley   const PetscScalar *coords;
116929494db1SLisandro Dalcin   PetscReal          xyl[2],xyr[2],bound[4] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
1170e412dcbdSMatthew G. Knepley   PetscBool          isnull;
1171e412dcbdSMatthew G. Knepley   PetscInt           dim, vStart, vEnd, cStart, cEnd, c, N;
1172cf3064d3SMatthew G. Knepley   PetscMPIInt        rank;
1173e412dcbdSMatthew G. Knepley   PetscErrorCode     ierr;
1174e412dcbdSMatthew G. Knepley 
1175e412dcbdSMatthew G. Knepley   PetscFunctionBegin;
1176e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
1177e412dcbdSMatthew G. Knepley   if (dim != 2) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Cannot draw meshes of dimension %D", dim);
1178e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
117992fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, &coordSection);CHKERRQ(ierr);
1180e412dcbdSMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
1181e412dcbdSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1182e412dcbdSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1183e412dcbdSMatthew G. Knepley 
1184e412dcbdSMatthew G. Knepley   ierr = PetscViewerDrawGetDraw(viewer, 0, &draw);CHKERRQ(ierr);
1185e412dcbdSMatthew G. Knepley   ierr = PetscDrawIsNull(draw, &isnull);CHKERRQ(ierr);
1186e412dcbdSMatthew G. Knepley   if (isnull) PetscFunctionReturn(0);
1187e412dcbdSMatthew G. Knepley   ierr = PetscDrawSetTitle(draw, "Mesh");CHKERRQ(ierr);
1188e412dcbdSMatthew G. Knepley 
1189e412dcbdSMatthew G. Knepley   ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr);
1190e412dcbdSMatthew G. Knepley   ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr);
1191e412dcbdSMatthew G. Knepley   for (c = 0; c < N; c += dim) {
11920c81f2a8SMatthew G. Knepley     bound[0] = PetscMin(bound[0], PetscRealPart(coords[c]));   bound[2] = PetscMax(bound[2], PetscRealPart(coords[c]));
11930c81f2a8SMatthew G. Knepley     bound[1] = PetscMin(bound[1], PetscRealPart(coords[c+1])); bound[3] = PetscMax(bound[3], PetscRealPart(coords[c+1]));
1194e412dcbdSMatthew G. Knepley   }
1195e412dcbdSMatthew G. Knepley   ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr);
119629494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[0],xyl,2,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
119729494db1SLisandro Dalcin   ierr = MPIU_Allreduce(&bound[2],xyr,2,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
119829494db1SLisandro Dalcin   ierr = PetscDrawSetCoordinates(draw, xyl[0], xyl[1], xyr[0], xyr[1]);CHKERRQ(ierr);
1199e412dcbdSMatthew G. Knepley   ierr = PetscDrawClear(draw);CHKERRQ(ierr);
1200e412dcbdSMatthew G. Knepley 
1201cf3064d3SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
1202cf3064d3SMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1203cf3064d3SMatthew G. Knepley     PetscScalar *coords = NULL;
1204cf3064d3SMatthew G. Knepley     PetscInt     numCoords,coneSize;
1205cf3064d3SMatthew G. Knepley 
1206cf3064d3SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
1207cf3064d3SMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1208cf3064d3SMatthew G. Knepley     switch (coneSize) {
1209cf3064d3SMatthew G. Knepley     case 3:
1210cf3064d3SMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), 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);CHKERRQ(ierr);
1214cf3064d3SMatthew G. Knepley       break;
1215cf3064d3SMatthew G. Knepley     case 4:
1216a46c2b5aSMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]),
1217cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1218cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1219a46c2b5aSMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1220a46c2b5aSMatthew G. Knepley       ierr = PetscDrawTriangle(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]),
1221a46c2b5aSMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1222cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2,
1223cf3064d3SMatthew G. Knepley                                 PETSC_DRAW_WHITE + rank % (PETSC_DRAW_BASIC_COLORS-2) + 2);CHKERRQ(ierr);
1224cf3064d3SMatthew G. Knepley       break;
1225cf3064d3SMatthew G. Knepley     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D facets", coneSize);
1226cf3064d3SMatthew G. Knepley     }
1227cf3064d3SMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1228cf3064d3SMatthew G. Knepley   }
1229e412dcbdSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1230e412dcbdSMatthew G. Knepley     PetscScalar *coords = NULL;
123129494db1SLisandro Dalcin     PetscInt     numCoords,coneSize;
1232e412dcbdSMatthew G. Knepley 
123329494db1SLisandro Dalcin     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
1234e412dcbdSMatthew G. Knepley     ierr = DMPlexVecGetClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
123529494db1SLisandro Dalcin     switch (coneSize) {
123629494db1SLisandro Dalcin     case 3:
12370c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12380c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12390c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1240e412dcbdSMatthew G. Knepley       break;
124129494db1SLisandro Dalcin     case 4:
12420c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[0]), PetscRealPart(coords[1]), PetscRealPart(coords[2]), PetscRealPart(coords[3]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12430c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[2]), PetscRealPart(coords[3]), PetscRealPart(coords[4]), PetscRealPart(coords[5]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12440c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[4]), PetscRealPart(coords[5]), PetscRealPart(coords[6]), PetscRealPart(coords[7]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
12450c81f2a8SMatthew G. Knepley       ierr = PetscDrawLine(draw, PetscRealPart(coords[6]), PetscRealPart(coords[7]), PetscRealPart(coords[0]), PetscRealPart(coords[1]), PETSC_DRAW_BLACK);CHKERRQ(ierr);
1246e412dcbdSMatthew G. Knepley       break;
124729494db1SLisandro Dalcin     default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot draw cells with %D facets", coneSize);
1248e412dcbdSMatthew G. Knepley     }
1249e412dcbdSMatthew G. Knepley     ierr = DMPlexVecRestoreClosure(dm, coordSection, coordinates, c, &numCoords, &coords);CHKERRQ(ierr);
1250e412dcbdSMatthew G. Knepley   }
1251e412dcbdSMatthew G. Knepley   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
1252e412dcbdSMatthew G. Knepley   ierr = PetscDrawPause(draw);CHKERRQ(ierr);
1253e412dcbdSMatthew G. Knepley   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
1254e412dcbdSMatthew G. Knepley   PetscFunctionReturn(0);
1255e412dcbdSMatthew G. Knepley }
1256e412dcbdSMatthew G. Knepley 
1257552f7358SJed Brown PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer)
1258552f7358SJed Brown {
1259e655db49SSatish Balay   PetscBool      iascii, ishdf5, isvtk, isdraw, flg, isglvis;
1260002a2709SMatthew G. Knepley   char           name[PETSC_MAX_PATH_LEN];
1261552f7358SJed Brown   PetscErrorCode ierr;
1262552f7358SJed Brown 
1263552f7358SJed Brown   PetscFunctionBegin;
1264552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1265552f7358SJed Brown   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
1266552f7358SJed Brown   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr);
1267fcf6c8fdSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERVTK,   &isvtk);CHKERRQ(ierr);
1268c6ccd67eSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,  &ishdf5);CHKERRQ(ierr);
1269e412dcbdSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERDRAW,  &isdraw);CHKERRQ(ierr);
12708135c375SStefano Zampini   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERGLVIS, &isglvis);CHKERRQ(ierr);
1271552f7358SJed Brown   if (iascii) {
12728135c375SStefano Zampini     PetscViewerFormat format;
12738135c375SStefano Zampini     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
12748135c375SStefano Zampini     if (format == PETSC_VIEWER_ASCII_GLVIS) {
12758135c375SStefano Zampini       ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
12768135c375SStefano Zampini     } else {
1277552f7358SJed Brown       ierr = DMPlexView_Ascii(dm, viewer);CHKERRQ(ierr);
12788135c375SStefano Zampini     }
1279c6ccd67eSMatthew G. Knepley   } else if (ishdf5) {
1280c6ccd67eSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
128139d25373SMatthew G. Knepley     ierr = DMPlexView_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1282c6ccd67eSMatthew G. Knepley #else
1283c6ccd67eSMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1284552f7358SJed Brown #endif
1285e412dcbdSMatthew G. Knepley   } else if (isvtk) {
1286fcf6c8fdSToby Isaac     ierr = DMPlexVTKWriteAll((PetscObject) dm,viewer);CHKERRQ(ierr);
1287e412dcbdSMatthew G. Knepley   } else if (isdraw) {
1288e412dcbdSMatthew G. Knepley     ierr = DMPlexView_Draw(dm, viewer);CHKERRQ(ierr);
12898135c375SStefano Zampini   } else if (isglvis) {
12908135c375SStefano Zampini     ierr = DMPlexView_GLVis(dm, viewer);CHKERRQ(ierr);
129162201deeSVaclav Hapla   } else {
1292a94aea51SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex writing", ((PetscObject)viewer)->type_name);
1293fcf6c8fdSToby Isaac   }
1294cb3ba0daSMatthew G. Knepley   /* Optionally view the partition */
1295cb3ba0daSMatthew G. Knepley   ierr = PetscOptionsHasName(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_partition_view", &flg);CHKERRQ(ierr);
1296cb3ba0daSMatthew G. Knepley   if (flg) {
1297cb3ba0daSMatthew G. Knepley     Vec ranks;
1298cb3ba0daSMatthew G. Knepley     ierr = DMPlexCreateRankField(dm, &ranks);CHKERRQ(ierr);
1299cb3ba0daSMatthew G. Knepley     ierr = VecView(ranks, viewer);CHKERRQ(ierr);
1300cb3ba0daSMatthew G. Knepley     ierr = VecDestroy(&ranks);CHKERRQ(ierr);
1301cb3ba0daSMatthew G. Knepley   }
1302002a2709SMatthew G. Knepley   /* Optionally view a label */
1303002a2709SMatthew G. Knepley   ierr = PetscOptionsGetString(((PetscObject) dm)->options, ((PetscObject) dm)->prefix, "-dm_label_view", name, PETSC_MAX_PATH_LEN, &flg);CHKERRQ(ierr);
1304002a2709SMatthew G. Knepley   if (flg) {
1305002a2709SMatthew G. Knepley     DMLabel label;
1306002a2709SMatthew G. Knepley     Vec     val;
1307002a2709SMatthew G. Knepley 
1308002a2709SMatthew G. Knepley     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
1309002a2709SMatthew 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);
1310002a2709SMatthew G. Knepley     ierr = DMPlexCreateLabelField(dm, label, &val);CHKERRQ(ierr);
1311002a2709SMatthew G. Knepley     ierr = VecView(val, viewer);CHKERRQ(ierr);
1312002a2709SMatthew G. Knepley     ierr = VecDestroy(&val);CHKERRQ(ierr);
1313002a2709SMatthew G. Knepley   }
1314552f7358SJed Brown   PetscFunctionReturn(0);
1315552f7358SJed Brown }
1316552f7358SJed Brown 
13172c40f234SMatthew G. Knepley PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer)
13182c40f234SMatthew G. Knepley {
1319d4f5a9a0SVaclav Hapla   PetscBool      ishdf5;
13202c40f234SMatthew G. Knepley   PetscErrorCode ierr;
13212c40f234SMatthew G. Knepley 
13222c40f234SMatthew G. Knepley   PetscFunctionBegin;
13232c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13242c40f234SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2);
13252c40f234SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5,   &ishdf5);CHKERRQ(ierr);
1326d4f5a9a0SVaclav Hapla   if (ishdf5) {
13272c40f234SMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
13289c48423bSVaclav Hapla     PetscViewerFormat format;
13299c48423bSVaclav Hapla     ierr = PetscViewerGetFormat(viewer, &format);CHKERRQ(ierr);
13309c48423bSVaclav Hapla     if (format == PETSC_VIEWER_HDF5_XDMF || format == PETSC_VIEWER_HDF5_VIZ) {
13319c48423bSVaclav Hapla       ierr = DMPlexLoad_HDF5_Xdmf_Internal(dm, viewer);CHKERRQ(ierr);
1332509517efSVaclav Hapla     } else if (format == PETSC_VIEWER_HDF5_PETSC || format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_NATIVE) {
133339d25373SMatthew G. Knepley       ierr = DMPlexLoad_HDF5_Internal(dm, viewer);CHKERRQ(ierr);
1334509517efSVaclav Hapla     } else SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "PetscViewerFormat %s not supported for HDF5 input.", PetscViewerFormats[format]);
13352c40f234SMatthew G. Knepley #else
13362c40f234SMatthew G. Knepley     SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
1337552f7358SJed Brown #endif
1338d4f5a9a0SVaclav Hapla   } else {
1339d4f5a9a0SVaclav Hapla     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Viewer type %s not yet supported for DMPlex loading", ((PetscObject)viewer)->type_name);
1340552f7358SJed Brown   }
1341552f7358SJed Brown   PetscFunctionReturn(0);
1342552f7358SJed Brown }
1343552f7358SJed Brown 
1344552f7358SJed Brown PetscErrorCode DMDestroy_Plex(DM dm)
1345552f7358SJed Brown {
1346552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1347552f7358SJed Brown   PetscErrorCode ierr;
1348552f7358SJed Brown 
1349552f7358SJed Brown   PetscFunctionBegin;
13508135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",NULL);CHKERRQ(ierr);
13518135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C", NULL);CHKERRQ(ierr);
135228d58a37SPierre Jolivet   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C", NULL);CHKERRQ(ierr);
13530d644c17SKarl Rupp   if (--mesh->refct > 0) PetscFunctionReturn(0);
1354552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->coneSection);CHKERRQ(ierr);
1355552f7358SJed Brown   ierr = PetscFree(mesh->cones);CHKERRQ(ierr);
1356552f7358SJed Brown   ierr = PetscFree(mesh->coneOrientations);CHKERRQ(ierr);
1357552f7358SJed Brown   ierr = PetscSectionDestroy(&mesh->supportSection);CHKERRQ(ierr);
1358be36d101SStefano Zampini   ierr = PetscSectionDestroy(&mesh->subdomainSection);CHKERRQ(ierr);
1359552f7358SJed Brown   ierr = PetscFree(mesh->supports);CHKERRQ(ierr);
1360552f7358SJed Brown   ierr = PetscFree(mesh->facesTmp);CHKERRQ(ierr);
1361d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->tetgenOpts);CHKERRQ(ierr);
1362d9deefdfSMatthew G. Knepley   ierr = PetscFree(mesh->triangleOpts);CHKERRQ(ierr);
136377623264SMatthew G. Knepley   ierr = PetscPartitionerDestroy(&mesh->partitioner);CHKERRQ(ierr);
1364a89082eeSMatthew G Knepley   ierr = DMLabelDestroy(&mesh->subpointMap);CHKERRQ(ierr);
1365552f7358SJed Brown   ierr = ISDestroy(&mesh->globalVertexNumbers);CHKERRQ(ierr);
1366552f7358SJed Brown   ierr = ISDestroy(&mesh->globalCellNumbers);CHKERRQ(ierr);
1367a68b90caSToby Isaac   ierr = PetscSectionDestroy(&mesh->anchorSection);CHKERRQ(ierr);
1368a68b90caSToby Isaac   ierr = ISDestroy(&mesh->anchorIS);CHKERRQ(ierr);
1369d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->parentSection);CHKERRQ(ierr);
1370d961a43aSToby Isaac   ierr = PetscFree(mesh->parents);CHKERRQ(ierr);
1371d961a43aSToby Isaac   ierr = PetscFree(mesh->childIDs);CHKERRQ(ierr);
1372d961a43aSToby Isaac   ierr = PetscSectionDestroy(&mesh->childSection);CHKERRQ(ierr);
1373d961a43aSToby Isaac   ierr = PetscFree(mesh->children);CHKERRQ(ierr);
1374d6a7ad0dSToby Isaac   ierr = DMDestroy(&mesh->referenceTree);CHKERRQ(ierr);
1375fec49543SMatthew G. Knepley   ierr = PetscGridHashDestroy(&mesh->lbox);CHKERRQ(ierr);
1376552f7358SJed Brown   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1377552f7358SJed Brown   ierr = PetscFree(mesh);CHKERRQ(ierr);
1378552f7358SJed Brown   PetscFunctionReturn(0);
1379552f7358SJed Brown }
1380552f7358SJed Brown 
1381b412c318SBarry Smith PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J)
1382552f7358SJed Brown {
13838d1174e4SMatthew G. Knepley   PetscSection           sectionGlobal;
1384acd755d7SMatthew G. Knepley   PetscInt               bs = -1, mbs;
1385552f7358SJed Brown   PetscInt               localSize;
1386837628f4SStefano Zampini   PetscBool              isShell, isBlock, isSeqBlock, isMPIBlock, isSymBlock, isSymSeqBlock, isSymMPIBlock, isMatIS;
1387552f7358SJed Brown   PetscErrorCode         ierr;
1388b412c318SBarry Smith   MatType                mtype;
13891428887cSShri Abhyankar   ISLocalToGlobalMapping ltog;
1390552f7358SJed Brown 
1391552f7358SJed Brown   PetscFunctionBegin;
1392607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1393b412c318SBarry Smith   mtype = dm->mattype;
1394e87a4003SBarry Smith   ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1395552f7358SJed Brown   /* ierr = PetscSectionGetStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr); */
1396552f7358SJed Brown   ierr = PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize);CHKERRQ(ierr);
139782f516ccSBarry Smith   ierr = MatCreate(PetscObjectComm((PetscObject)dm), J);CHKERRQ(ierr);
1398552f7358SJed Brown   ierr = MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
1399552f7358SJed Brown   ierr = MatSetType(*J, mtype);CHKERRQ(ierr);
1400552f7358SJed Brown   ierr = MatSetFromOptions(*J);CHKERRQ(ierr);
1401acd755d7SMatthew G. Knepley   ierr = MatGetBlockSize(*J, &mbs);CHKERRQ(ierr);
1402acd755d7SMatthew G. Knepley   if (mbs > 1) bs = mbs;
1403552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSHELL, &isShell);CHKERRQ(ierr);
1404552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATBAIJ, &isBlock);CHKERRQ(ierr);
1405552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQBAIJ, &isSeqBlock);CHKERRQ(ierr);
1406552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPIBAIJ, &isMPIBlock);CHKERRQ(ierr);
1407552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSBAIJ, &isSymBlock);CHKERRQ(ierr);
1408552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATSEQSBAIJ, &isSymSeqBlock);CHKERRQ(ierr);
1409552f7358SJed Brown   ierr = PetscStrcmp(mtype, MATMPISBAIJ, &isSymMPIBlock);CHKERRQ(ierr);
1410837628f4SStefano Zampini   ierr = PetscStrcmp(mtype, MATIS, &isMatIS);CHKERRQ(ierr);
1411552f7358SJed Brown   if (!isShell) {
1412be36d101SStefano Zampini     PetscSection subSection;
1413837628f4SStefano Zampini     PetscBool    fillMatrix = (PetscBool)(!dm->prealloc_only && !isMatIS);
14140be3e97aSMatthew G. Knepley     PetscInt    *dnz, *onz, *dnzu, *onzu, bsLocal[2], bsMinMax[2], *ltogidx, lsize;
1415fad22124SMatthew G Knepley     PetscInt     pStart, pEnd, p, dof, cdof;
1416552f7358SJed Brown 
141700140cc3SStefano Zampini     /* Set localtoglobalmapping on the matrix for MatSetValuesLocal() to work (it also creates the local matrices in case of MATIS) */
1418be36d101SStefano Zampini     if (isMatIS) { /* need a different l2g map than the one computed by DMGetLocalToGlobalMapping */
1419be36d101SStefano Zampini       PetscSection section;
1420be36d101SStefano Zampini       PetscInt     size;
1421be36d101SStefano Zampini 
142292fd8e1eSJed Brown       ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1423be36d101SStefano Zampini       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
1424be36d101SStefano Zampini       ierr = PetscMalloc1(size,&ltogidx);CHKERRQ(ierr);
1425be36d101SStefano Zampini       ierr = DMPlexGetSubdomainSection(dm, &subSection);CHKERRQ(ierr);
1426be36d101SStefano Zampini     } else {
142700140cc3SStefano Zampini       ierr = DMGetLocalToGlobalMapping(dm,&ltog);CHKERRQ(ierr);
1428be36d101SStefano Zampini     }
1429552f7358SJed Brown     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1430be36d101SStefano Zampini     for (p = pStart, lsize = 0; p < pEnd; ++p) {
1431a9d99c84SMatthew G. Knepley       PetscInt bdof;
1432a9d99c84SMatthew G. Knepley 
1433552f7358SJed Brown       ierr = PetscSectionGetDof(sectionGlobal, p, &dof);CHKERRQ(ierr);
1434fad22124SMatthew G Knepley       ierr = PetscSectionGetConstraintDof(sectionGlobal, p, &cdof);CHKERRQ(ierr);
14351d17a0a3SMatthew G. Knepley       dof  = dof < 0 ? -(dof+1) : dof;
14361d17a0a3SMatthew G. Knepley       bdof = cdof && (dof-cdof) ? 1 : dof;
14371d17a0a3SMatthew G. Knepley       if (dof) {
14381d17a0a3SMatthew G. Knepley         if (bs < 0)          {bs = bdof;}
1439be36d101SStefano Zampini         else if (bs != bdof) {bs = 1; if (!isMatIS) break;}
1440be36d101SStefano Zampini       }
1441be36d101SStefano Zampini       if (isMatIS) {
1442be36d101SStefano Zampini         PetscInt loff,c,off;
1443be36d101SStefano Zampini         ierr = PetscSectionGetOffset(subSection, p, &loff);CHKERRQ(ierr);
1444be36d101SStefano Zampini         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1445be36d101SStefano Zampini         for (c = 0; c < dof-cdof; ++c, ++lsize) ltogidx[loff+c] = off > -1 ? off+c : -(off+1)+c;
1446552f7358SJed Brown       }
14472a28c762SMatthew G Knepley     }
14482a28c762SMatthew G Knepley     /* Must have same blocksize on all procs (some might have no points) */
14490be3e97aSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
14500be3e97aSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
14510be3e97aSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
14520be3e97aSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
14536fd5c86aSStefano Zampini     bs = PetscMax(1,bs);
14546fd5c86aSStefano Zampini     if (isMatIS) { /* Must reduce indices by blocksize */
14554fa26246SMatthew G. Knepley       PetscInt l;
14566fd5c86aSStefano Zampini 
14576fd5c86aSStefano Zampini       lsize = lsize/bs;
14586fd5c86aSStefano Zampini       if (bs > 1) for (l = 0; l < lsize; ++l) ltogidx[l] = ltogidx[l*bs]/bs;
14594fa26246SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, lsize, ltogidx, PETSC_OWN_POINTER, &ltog);CHKERRQ(ierr);
1460be36d101SStefano Zampini     }
1461be36d101SStefano Zampini     ierr = MatSetLocalToGlobalMapping(*J,ltog,ltog);CHKERRQ(ierr);
1462be36d101SStefano Zampini     if (isMatIS) {
1463be36d101SStefano Zampini       ierr = ISLocalToGlobalMappingDestroy(&ltog);CHKERRQ(ierr);
1464be36d101SStefano Zampini     }
14651795a4d1SJed Brown     ierr = PetscCalloc4(localSize/bs, &dnz, localSize/bs, &onz, localSize/bs, &dnzu, localSize/bs, &onzu);CHKERRQ(ierr);
14668d1174e4SMatthew G. Knepley     ierr = DMPlexPreallocateOperator(dm, bs, dnz, onz, dnzu, onzu, *J, fillMatrix);CHKERRQ(ierr);
1467552f7358SJed Brown     ierr = PetscFree4(dnz, onz, dnzu, onzu);CHKERRQ(ierr);
1468552f7358SJed Brown   }
1469b1f74addSMatthew G. Knepley   ierr = MatSetDM(*J, dm);CHKERRQ(ierr);
1470552f7358SJed Brown   PetscFunctionReturn(0);
1471552f7358SJed Brown }
1472552f7358SJed Brown 
14737cd05799SMatthew G. Knepley /*@
1474a8f00d21SMatthew G. Knepley   DMPlexGetSubdomainSection - Returns the section associated with the subdomain
1475be36d101SStefano Zampini 
1476be36d101SStefano Zampini   Not collective
1477be36d101SStefano Zampini 
1478be36d101SStefano Zampini   Input Parameter:
1479be36d101SStefano Zampini . mesh - The DMPlex
1480be36d101SStefano Zampini 
1481be36d101SStefano Zampini   Output Parameters:
1482be36d101SStefano Zampini . subsection - The subdomain section
1483be36d101SStefano Zampini 
1484be36d101SStefano Zampini   Level: developer
1485be36d101SStefano Zampini 
1486be36d101SStefano Zampini .seealso:
14877cd05799SMatthew G. Knepley @*/
1488be36d101SStefano Zampini PetscErrorCode DMPlexGetSubdomainSection(DM dm, PetscSection *subsection)
1489be36d101SStefano Zampini {
1490be36d101SStefano Zampini   DM_Plex       *mesh = (DM_Plex*) dm->data;
1491be36d101SStefano Zampini   PetscErrorCode ierr;
1492be36d101SStefano Zampini 
1493be36d101SStefano Zampini   PetscFunctionBegin;
1494be36d101SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1495be36d101SStefano Zampini   if (!mesh->subdomainSection) {
1496be36d101SStefano Zampini     PetscSection section;
1497be36d101SStefano Zampini     PetscSF      sf;
1498be36d101SStefano Zampini 
1499be36d101SStefano Zampini     ierr = PetscSFCreate(PETSC_COMM_SELF,&sf);CHKERRQ(ierr);
150092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
1501be36d101SStefano Zampini     ierr = PetscSectionCreateGlobalSection(section,sf,PETSC_FALSE,PETSC_TRUE,&mesh->subdomainSection);CHKERRQ(ierr);
1502be36d101SStefano Zampini     ierr = PetscSFDestroy(&sf);CHKERRQ(ierr);
1503be36d101SStefano Zampini   }
1504be36d101SStefano Zampini   *subsection = mesh->subdomainSection;
1505be36d101SStefano Zampini   PetscFunctionReturn(0);
1506be36d101SStefano Zampini }
1507be36d101SStefano Zampini 
1508552f7358SJed Brown /*@
1509552f7358SJed Brown   DMPlexGetChart - Return the interval for all mesh points [pStart, pEnd)
1510552f7358SJed Brown 
1511552f7358SJed Brown   Not collective
1512552f7358SJed Brown 
1513552f7358SJed Brown   Input Parameter:
1514552f7358SJed Brown . mesh - The DMPlex
1515552f7358SJed Brown 
1516552f7358SJed Brown   Output Parameters:
1517552f7358SJed Brown + pStart - The first mesh point
1518552f7358SJed Brown - pEnd   - The upper bound for mesh points
1519552f7358SJed Brown 
1520552f7358SJed Brown   Level: beginner
1521552f7358SJed Brown 
1522552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart()
1523552f7358SJed Brown @*/
1524552f7358SJed Brown PetscErrorCode DMPlexGetChart(DM dm, PetscInt *pStart, PetscInt *pEnd)
1525552f7358SJed Brown {
1526552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1527552f7358SJed Brown   PetscErrorCode ierr;
1528552f7358SJed Brown 
1529552f7358SJed Brown   PetscFunctionBegin;
1530552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1531552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1532552f7358SJed Brown   PetscFunctionReturn(0);
1533552f7358SJed Brown }
1534552f7358SJed Brown 
1535552f7358SJed Brown /*@
1536552f7358SJed Brown   DMPlexSetChart - Set the interval for all mesh points [pStart, pEnd)
1537552f7358SJed Brown 
1538552f7358SJed Brown   Not collective
1539552f7358SJed Brown 
1540552f7358SJed Brown   Input Parameters:
1541552f7358SJed Brown + mesh - The DMPlex
1542552f7358SJed Brown . pStart - The first mesh point
1543552f7358SJed Brown - pEnd   - The upper bound for mesh points
1544552f7358SJed Brown 
1545552f7358SJed Brown   Output Parameters:
1546552f7358SJed Brown 
1547552f7358SJed Brown   Level: beginner
1548552f7358SJed Brown 
1549552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetChart()
1550552f7358SJed Brown @*/
1551552f7358SJed Brown PetscErrorCode DMPlexSetChart(DM dm, PetscInt pStart, PetscInt pEnd)
1552552f7358SJed Brown {
1553552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1554552f7358SJed Brown   PetscErrorCode ierr;
1555552f7358SJed Brown 
1556552f7358SJed Brown   PetscFunctionBegin;
1557552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1558552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->coneSection, pStart, pEnd);CHKERRQ(ierr);
1559552f7358SJed Brown   ierr = PetscSectionSetChart(mesh->supportSection, pStart, pEnd);CHKERRQ(ierr);
1560552f7358SJed Brown   PetscFunctionReturn(0);
1561552f7358SJed Brown }
1562552f7358SJed Brown 
1563552f7358SJed Brown /*@
1564eaf898f9SPatrick Sanan   DMPlexGetConeSize - Return the number of in-edges for this point in the DAG
1565552f7358SJed Brown 
1566552f7358SJed Brown   Not collective
1567552f7358SJed Brown 
1568552f7358SJed Brown   Input Parameters:
1569552f7358SJed Brown + mesh - The DMPlex
1570eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1571552f7358SJed Brown 
1572552f7358SJed Brown   Output Parameter:
1573552f7358SJed Brown . size - The cone size for point p
1574552f7358SJed Brown 
1575552f7358SJed Brown   Level: beginner
1576552f7358SJed Brown 
1577552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
1578552f7358SJed Brown @*/
1579552f7358SJed Brown PetscErrorCode DMPlexGetConeSize(DM dm, PetscInt p, PetscInt *size)
1580552f7358SJed Brown {
1581552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1582552f7358SJed Brown   PetscErrorCode ierr;
1583552f7358SJed Brown 
1584552f7358SJed Brown   PetscFunctionBegin;
1585552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1586552f7358SJed Brown   PetscValidPointer(size, 3);
1587552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1588552f7358SJed Brown   PetscFunctionReturn(0);
1589552f7358SJed Brown }
1590552f7358SJed Brown 
1591552f7358SJed Brown /*@
1592eaf898f9SPatrick Sanan   DMPlexSetConeSize - Set the number of in-edges for this point in the DAG
1593552f7358SJed Brown 
1594552f7358SJed Brown   Not collective
1595552f7358SJed Brown 
1596552f7358SJed Brown   Input Parameters:
1597552f7358SJed Brown + mesh - The DMPlex
1598eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1599552f7358SJed Brown - size - The cone size for point p
1600552f7358SJed Brown 
1601552f7358SJed Brown   Output Parameter:
1602552f7358SJed Brown 
1603552f7358SJed Brown   Note:
1604552f7358SJed Brown   This should be called after DMPlexSetChart().
1605552f7358SJed Brown 
1606552f7358SJed Brown   Level: beginner
1607552f7358SJed Brown 
1608552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeSize(), DMPlexSetChart()
1609552f7358SJed Brown @*/
1610552f7358SJed Brown PetscErrorCode DMPlexSetConeSize(DM dm, PetscInt p, PetscInt size)
1611552f7358SJed Brown {
1612552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1613552f7358SJed Brown   PetscErrorCode ierr;
1614552f7358SJed Brown 
1615552f7358SJed Brown   PetscFunctionBegin;
1616552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1617552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->coneSection, p, size);CHKERRQ(ierr);
16180d644c17SKarl Rupp 
1619552f7358SJed Brown   mesh->maxConeSize = PetscMax(mesh->maxConeSize, size);
1620552f7358SJed Brown   PetscFunctionReturn(0);
1621552f7358SJed Brown }
1622552f7358SJed Brown 
1623f5a469b9SMatthew G. Knepley /*@
1624eaf898f9SPatrick Sanan   DMPlexAddConeSize - Add the given number of in-edges to this point in the DAG
1625f5a469b9SMatthew G. Knepley 
1626f5a469b9SMatthew G. Knepley   Not collective
1627f5a469b9SMatthew G. Knepley 
1628f5a469b9SMatthew G. Knepley   Input Parameters:
1629f5a469b9SMatthew G. Knepley + mesh - The DMPlex
1630eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1631f5a469b9SMatthew G. Knepley - size - The additional cone size for point p
1632f5a469b9SMatthew G. Knepley 
1633f5a469b9SMatthew G. Knepley   Output Parameter:
1634f5a469b9SMatthew G. Knepley 
1635f5a469b9SMatthew G. Knepley   Note:
1636f5a469b9SMatthew G. Knepley   This should be called after DMPlexSetChart().
1637f5a469b9SMatthew G. Knepley 
1638f5a469b9SMatthew G. Knepley   Level: beginner
1639f5a469b9SMatthew G. Knepley 
1640f5a469b9SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexGetConeSize(), DMPlexSetChart()
1641f5a469b9SMatthew G. Knepley @*/
1642f5a469b9SMatthew G. Knepley PetscErrorCode DMPlexAddConeSize(DM dm, PetscInt p, PetscInt size)
1643f5a469b9SMatthew G. Knepley {
1644f5a469b9SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
1645f5a469b9SMatthew G. Knepley   PetscInt       csize;
1646f5a469b9SMatthew G. Knepley   PetscErrorCode ierr;
1647f5a469b9SMatthew G. Knepley 
1648f5a469b9SMatthew G. Knepley   PetscFunctionBegin;
1649f5a469b9SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1650f5a469b9SMatthew G. Knepley   ierr = PetscSectionAddDof(mesh->coneSection, p, size);CHKERRQ(ierr);
1651f5a469b9SMatthew G. Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &csize);CHKERRQ(ierr);
1652f5a469b9SMatthew G. Knepley 
1653f5a469b9SMatthew G. Knepley   mesh->maxConeSize = PetscMax(mesh->maxConeSize, csize);
1654f5a469b9SMatthew G. Knepley   PetscFunctionReturn(0);
1655f5a469b9SMatthew G. Knepley }
1656f5a469b9SMatthew G. Knepley 
1657552f7358SJed Brown /*@C
1658eaf898f9SPatrick Sanan   DMPlexGetCone - Return the points on the in-edges for this point in the DAG
1659552f7358SJed Brown 
1660552f7358SJed Brown   Not collective
1661552f7358SJed Brown 
1662552f7358SJed Brown   Input Parameters:
1663833c876bSVaclav Hapla + dm - The DMPlex
1664eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1665552f7358SJed Brown 
1666552f7358SJed Brown   Output Parameter:
1667552f7358SJed Brown . cone - An array of points which are on the in-edges for point p
1668552f7358SJed Brown 
1669552f7358SJed Brown   Level: beginner
1670552f7358SJed Brown 
16713813dfbdSMatthew G Knepley   Fortran Notes:
16723813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
16733813dfbdSMatthew G Knepley   include petsc.h90 in your code.
1674922102d1SVaclav Hapla   You must also call DMPlexRestoreCone() after you finish using the returned array.
1675922102d1SVaclav Hapla   DMPlexRestoreCone() is not needed/available in C.
16763813dfbdSMatthew G Knepley 
16770ce7577fSVaclav Hapla .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexGetConeTuple(), DMPlexSetChart()
1678552f7358SJed Brown @*/
1679552f7358SJed Brown PetscErrorCode DMPlexGetCone(DM dm, PetscInt p, const PetscInt *cone[])
1680552f7358SJed Brown {
1681552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1682552f7358SJed Brown   PetscInt       off;
1683552f7358SJed Brown   PetscErrorCode ierr;
1684552f7358SJed Brown 
1685552f7358SJed Brown   PetscFunctionBegin;
1686552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1687552f7358SJed Brown   PetscValidPointer(cone, 3);
1688552f7358SJed Brown   ierr  = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
1689552f7358SJed Brown   *cone = &mesh->cones[off];
1690552f7358SJed Brown   PetscFunctionReturn(0);
1691552f7358SJed Brown }
1692552f7358SJed Brown 
16930ce7577fSVaclav Hapla /*@C
16940ce7577fSVaclav Hapla   DMPlexGetConeTuple - Return the points on the in-edges of several points in the DAG
16950ce7577fSVaclav Hapla 
16960ce7577fSVaclav Hapla   Not collective
16970ce7577fSVaclav Hapla 
16980ce7577fSVaclav Hapla   Input Parameters:
16990ce7577fSVaclav Hapla + dm - The DMPlex
17000ce7577fSVaclav Hapla - p - The IS of points, which must lie in the chart set with DMPlexSetChart()
17010ce7577fSVaclav Hapla 
17020ce7577fSVaclav Hapla   Output Parameter:
17030ce7577fSVaclav Hapla + pConesSection - PetscSection describing the layout of pCones
17040ce7577fSVaclav Hapla - pCones - An array of points which are on the in-edges for the point set p
17050ce7577fSVaclav Hapla 
17060ce7577fSVaclav Hapla   Level: intermediate
17070ce7577fSVaclav Hapla 
1708d4636a37SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeRecursive(), DMPlexSetChart()
17090ce7577fSVaclav Hapla @*/
17100ce7577fSVaclav Hapla PetscErrorCode DMPlexGetConeTuple(DM dm, IS p, PetscSection *pConesSection, IS *pCones)
17110ce7577fSVaclav Hapla {
17120ce7577fSVaclav Hapla   PetscSection        cs, newcs;
17130ce7577fSVaclav Hapla   PetscInt            *cones;
17140ce7577fSVaclav Hapla   PetscInt            *newarr=NULL;
17150ce7577fSVaclav Hapla   PetscInt            n;
17160ce7577fSVaclav Hapla   PetscErrorCode      ierr;
17170ce7577fSVaclav Hapla 
17180ce7577fSVaclav Hapla   PetscFunctionBegin;
17190ce7577fSVaclav Hapla   ierr = DMPlexGetCones(dm, &cones);CHKERRQ(ierr);
17200ce7577fSVaclav Hapla   ierr = DMPlexGetConeSection(dm, &cs);CHKERRQ(ierr);
17210ce7577fSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_INT, cones, p, &newcs, pCones ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
17220ce7577fSVaclav Hapla   if (pConesSection) *pConesSection = newcs;
17230ce7577fSVaclav Hapla   if (pCones) {
17240ce7577fSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
17250ce7577fSVaclav Hapla     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)p), n, newarr, PETSC_OWN_POINTER, pCones);CHKERRQ(ierr);
17260ce7577fSVaclav Hapla   }
17270ce7577fSVaclav Hapla   PetscFunctionReturn(0);
17280ce7577fSVaclav Hapla }
17290ce7577fSVaclav Hapla 
1730af9eab45SVaclav Hapla /*@
1731af9eab45SVaclav Hapla   DMPlexGetConeRecursiveVertices - Expand each given point into its cone points and do that recursively until we end up just with vertices.
1732d4636a37SVaclav Hapla 
1733d4636a37SVaclav Hapla   Not collective
1734d4636a37SVaclav Hapla 
1735d4636a37SVaclav Hapla   Input Parameters:
1736d4636a37SVaclav Hapla + dm - The DMPlex
1737af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1738d4636a37SVaclav Hapla 
1739d4636a37SVaclav Hapla   Output Parameter:
1740af9eab45SVaclav Hapla . expandedPoints - An array of vertices recursively expanded from input points
1741d4636a37SVaclav Hapla 
1742d4636a37SVaclav Hapla   Level: advanced
1743d4636a37SVaclav Hapla 
1744af9eab45SVaclav Hapla   Notes:
1745af9eab45SVaclav Hapla   Like DMPlexGetConeRecursive but returns only the 0-depth IS (i.e. vertices only) and no sections.
1746af9eab45SVaclav Hapla   There is no corresponding Restore function, just call ISDestroy() on the returned IS to deallocate.
1747af9eab45SVaclav Hapla 
1748af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexRestoreConeRecursive(), DMPlexGetDepth()
1749d4636a37SVaclav Hapla @*/
1750af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursiveVertices(DM dm, IS points, IS *expandedPoints)
1751d4636a37SVaclav Hapla {
1752af9eab45SVaclav Hapla   IS                  *expandedPointsAll;
1753af9eab45SVaclav Hapla   PetscInt            depth;
1754d4636a37SVaclav Hapla   PetscErrorCode      ierr;
1755d4636a37SVaclav Hapla 
1756d4636a37SVaclav Hapla   PetscFunctionBegin;
1757af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1758af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
1759af9eab45SVaclav Hapla   PetscValidPointer(expandedPoints, 3);
1760af9eab45SVaclav Hapla   ierr = DMPlexGetConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
1761af9eab45SVaclav Hapla   *expandedPoints = expandedPointsAll[0];
1762af9eab45SVaclav Hapla   ierr = PetscObjectReference((PetscObject)expandedPointsAll[0]);
1763af9eab45SVaclav Hapla   ierr = DMPlexRestoreConeRecursive(dm, points, &depth, &expandedPointsAll, NULL);CHKERRQ(ierr);
1764af9eab45SVaclav Hapla   PetscFunctionReturn(0);
1765af9eab45SVaclav Hapla }
1766af9eab45SVaclav Hapla 
1767af9eab45SVaclav Hapla /*@
1768af9eab45SVaclav 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).
1769af9eab45SVaclav Hapla 
1770af9eab45SVaclav Hapla   Not collective
1771af9eab45SVaclav Hapla 
1772af9eab45SVaclav Hapla   Input Parameters:
1773af9eab45SVaclav Hapla + dm - The DMPlex
1774af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1775af9eab45SVaclav Hapla 
1776af9eab45SVaclav Hapla   Output Parameter:
1777af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
1778af9eab45SVaclav Hapla . expandedPoints - (optional) An array of index sets with recursively expanded cones
1779af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
1780af9eab45SVaclav Hapla 
1781af9eab45SVaclav Hapla   Level: advanced
1782af9eab45SVaclav Hapla 
1783af9eab45SVaclav Hapla   Notes:
1784af9eab45SVaclav Hapla   Like DMPlexGetConeTuple() but recursive.
1785af9eab45SVaclav Hapla 
1786af9eab45SVaclav 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.
1787af9eab45SVaclav Hapla   For example, for d=0 it contains only vertices, for d=1 it can contain vertices and edges, etc.
1788af9eab45SVaclav Hapla 
1789af9eab45SVaclav 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:
1790af9eab45SVaclav Hapla   (1) DAG points in expandedPoints[d+1] with depth d+1 to their cone points in expandedPoints[d];
1791af9eab45SVaclav Hapla   (2) DAG points in expandedPoints[d+1] with depth in [0,d] to the same points in expandedPoints[d].
1792af9eab45SVaclav Hapla 
1793af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexRestoreConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
1794af9eab45SVaclav Hapla @*/
1795af9eab45SVaclav Hapla PetscErrorCode DMPlexGetConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
1796af9eab45SVaclav Hapla {
1797af9eab45SVaclav Hapla   const PetscInt      *arr0=NULL, *cone=NULL;
1798af9eab45SVaclav Hapla   PetscInt            *arr=NULL, *newarr=NULL;
1799af9eab45SVaclav Hapla   PetscInt            d, depth_, i, n, newn, cn, co, start, end;
1800af9eab45SVaclav Hapla   IS                  *expandedPoints_;
1801af9eab45SVaclav Hapla   PetscSection        *sections_;
1802af9eab45SVaclav Hapla   PetscErrorCode      ierr;
1803af9eab45SVaclav Hapla 
1804af9eab45SVaclav Hapla   PetscFunctionBegin;
1805af9eab45SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1806af9eab45SVaclav Hapla   PetscValidHeaderSpecific(points, IS_CLASSID, 2);
1807af9eab45SVaclav Hapla   if (depth) PetscValidIntPointer(depth, 3);
1808af9eab45SVaclav Hapla   if (expandedPoints) PetscValidPointer(expandedPoints, 4);
1809af9eab45SVaclav Hapla   if (sections) PetscValidPointer(sections, 5);
1810af9eab45SVaclav Hapla   ierr = ISGetLocalSize(points, &n);CHKERRQ(ierr);
1811af9eab45SVaclav Hapla   ierr = ISGetIndices(points, &arr0);CHKERRQ(ierr);
1812af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
1813af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &expandedPoints_);CHKERRQ(ierr);
1814af9eab45SVaclav Hapla   ierr = PetscCalloc1(depth_, &sections_);CHKERRQ(ierr);
1815af9eab45SVaclav Hapla   arr = (PetscInt*) arr0; /* this is ok because first generation of arr is not modified */
1816af9eab45SVaclav Hapla   for (d=depth_-1; d>=0; d--) {
1817af9eab45SVaclav Hapla     ierr = PetscSectionCreate(PETSC_COMM_SELF, &sections_[d]);CHKERRQ(ierr);
1818af9eab45SVaclav Hapla     ierr = PetscSectionSetChart(sections_[d], 0, n);CHKERRQ(ierr);
1819af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
1820af9eab45SVaclav Hapla       ierr = DMPlexGetDepthStratum(dm, d+1, &start, &end);CHKERRQ(ierr);
1821af9eab45SVaclav Hapla       if (arr[i] >= start && arr[i] < end) {
1822af9eab45SVaclav Hapla         ierr = DMPlexGetConeSize(dm, arr[i], &cn);CHKERRQ(ierr);
1823af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, cn);CHKERRQ(ierr);
1824af9eab45SVaclav Hapla       } else {
1825af9eab45SVaclav Hapla         ierr = PetscSectionSetDof(sections_[d], i, 1);CHKERRQ(ierr);
1826af9eab45SVaclav Hapla       }
1827af9eab45SVaclav Hapla     }
1828af9eab45SVaclav Hapla     ierr = PetscSectionSetUp(sections_[d]);CHKERRQ(ierr);
1829af9eab45SVaclav Hapla     ierr = PetscSectionGetStorageSize(sections_[d], &newn);CHKERRQ(ierr);
1830af9eab45SVaclav Hapla     ierr = PetscMalloc1(newn, &newarr);CHKERRQ(ierr);
1831af9eab45SVaclav Hapla     for (i=0; i<n; i++) {
1832af9eab45SVaclav Hapla       ierr = PetscSectionGetDof(sections_[d], i, &cn);CHKERRQ(ierr);
1833af9eab45SVaclav Hapla       ierr = PetscSectionGetOffset(sections_[d], i, &co);CHKERRQ(ierr);
1834af9eab45SVaclav Hapla       if (cn > 1) {
1835af9eab45SVaclav Hapla         ierr = DMPlexGetCone(dm, arr[i], &cone);CHKERRQ(ierr);
1836af9eab45SVaclav Hapla         ierr = PetscMemcpy(&newarr[co], cone, cn*sizeof(PetscInt));CHKERRQ(ierr);
1837af9eab45SVaclav Hapla       } else {
1838af9eab45SVaclav Hapla         newarr[co] = arr[i];
1839af9eab45SVaclav Hapla       }
1840af9eab45SVaclav Hapla     }
1841af9eab45SVaclav Hapla     ierr = ISCreateGeneral(PETSC_COMM_SELF, newn, newarr, PETSC_OWN_POINTER, &expandedPoints_[d]);CHKERRQ(ierr);
1842af9eab45SVaclav Hapla     arr = newarr;
1843af9eab45SVaclav Hapla     n = newn;
1844af9eab45SVaclav Hapla   }
1845af9eab45SVaclav Hapla   *depth = depth_;
1846af9eab45SVaclav Hapla   if (expandedPoints) *expandedPoints = expandedPoints_;
1847af9eab45SVaclav Hapla   else {
1848af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&expandedPoints_[d]);CHKERRQ(ierr);}
1849af9eab45SVaclav Hapla     ierr = PetscFree(expandedPoints_);CHKERRQ(ierr);
1850af9eab45SVaclav Hapla   }
1851af9eab45SVaclav Hapla   if (sections) *sections = sections_;
1852af9eab45SVaclav Hapla   else {
1853af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&sections_[d]);CHKERRQ(ierr);}
1854af9eab45SVaclav Hapla     ierr = PetscFree(sections_);CHKERRQ(ierr);
1855af9eab45SVaclav Hapla   }
1856af9eab45SVaclav Hapla   PetscFunctionReturn(0);
1857af9eab45SVaclav Hapla }
1858af9eab45SVaclav Hapla 
1859af9eab45SVaclav Hapla /*@
1860af9eab45SVaclav Hapla   DMPlexRestoreConeRecursive - Deallocates arrays created by DMPlexGetConeRecursive
1861af9eab45SVaclav Hapla 
1862af9eab45SVaclav Hapla   Not collective
1863af9eab45SVaclav Hapla 
1864af9eab45SVaclav Hapla   Input Parameters:
1865af9eab45SVaclav Hapla + dm - The DMPlex
1866af9eab45SVaclav Hapla - points - The IS of points, which must lie in the chart set with DMPlexSetChart()
1867af9eab45SVaclav Hapla 
1868af9eab45SVaclav Hapla   Output Parameter:
1869af9eab45SVaclav Hapla + depth - (optional) Size of the output arrays, equal to DMPlex depth, returned by DMPlexGetDepth()
1870af9eab45SVaclav Hapla . expandedPoints - (optional) An array of recursively expanded cones
1871af9eab45SVaclav Hapla - sections - (optional) An array of sections which describe mappings from points to their cone points
1872af9eab45SVaclav Hapla 
1873af9eab45SVaclav Hapla   Level: advanced
1874af9eab45SVaclav Hapla 
1875af9eab45SVaclav Hapla   Notes:
1876af9eab45SVaclav Hapla   See DMPlexGetConeRecursive() for details.
1877af9eab45SVaclav Hapla 
1878af9eab45SVaclav Hapla .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexGetConeTuple(), DMPlexGetConeRecursive(), DMPlexGetConeRecursiveVertices(), DMPlexGetDepth()
1879af9eab45SVaclav Hapla @*/
1880af9eab45SVaclav Hapla PetscErrorCode DMPlexRestoreConeRecursive(DM dm, IS points, PetscInt *depth, IS *expandedPoints[], PetscSection *sections[])
1881af9eab45SVaclav Hapla {
1882af9eab45SVaclav Hapla   PetscInt            d, depth_;
1883af9eab45SVaclav Hapla   PetscErrorCode      ierr;
1884af9eab45SVaclav Hapla 
1885af9eab45SVaclav Hapla   PetscFunctionBegin;
1886af9eab45SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth_);CHKERRQ(ierr);
1887af9eab45SVaclav Hapla   if (depth && *depth != depth_) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "depth changed since last call to DMPlexGetConeRecursive");
1888af9eab45SVaclav Hapla   if (depth) *depth = 0;
1889af9eab45SVaclav Hapla   if (expandedPoints) {
1890af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = ISDestroy(&((*expandedPoints)[d]));CHKERRQ(ierr);}
1891af9eab45SVaclav Hapla     ierr = PetscFree(*expandedPoints);CHKERRQ(ierr);
1892af9eab45SVaclav Hapla   }
1893af9eab45SVaclav Hapla   if (sections)  {
1894af9eab45SVaclav Hapla     for (d=0; d<depth_; d++) {ierr = PetscSectionDestroy(&((*sections)[d]));CHKERRQ(ierr);}
1895af9eab45SVaclav Hapla     ierr = PetscFree(*sections);CHKERRQ(ierr);
1896af9eab45SVaclav Hapla   }
1897d4636a37SVaclav Hapla   PetscFunctionReturn(0);
1898d4636a37SVaclav Hapla }
1899d4636a37SVaclav Hapla 
1900552f7358SJed Brown /*@
190192371b87SBarry 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
1902552f7358SJed Brown 
1903552f7358SJed Brown   Not collective
1904552f7358SJed Brown 
1905552f7358SJed Brown   Input Parameters:
1906552f7358SJed Brown + mesh - The DMPlex
1907eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1908552f7358SJed Brown - cone - An array of points which are on the in-edges for point p
1909552f7358SJed Brown 
1910552f7358SJed Brown   Output Parameter:
1911552f7358SJed Brown 
1912552f7358SJed Brown   Note:
1913552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
1914552f7358SJed Brown 
191592371b87SBarry Smith   Developer Note: Why not call this DMPlexSetCover()
191692371b87SBarry Smith 
1917552f7358SJed Brown   Level: beginner
1918552f7358SJed Brown 
191992371b87SBarry Smith .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp(), DMPlexSetSupport(), DMPlexSetSupportSize()
1920552f7358SJed Brown @*/
1921552f7358SJed Brown PetscErrorCode DMPlexSetCone(DM dm, PetscInt p, const PetscInt cone[])
1922552f7358SJed Brown {
1923552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1924552f7358SJed Brown   PetscInt       pStart, pEnd;
1925552f7358SJed Brown   PetscInt       dof, off, c;
1926552f7358SJed Brown   PetscErrorCode ierr;
1927552f7358SJed Brown 
1928552f7358SJed Brown   PetscFunctionBegin;
1929552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1930552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
1931552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1932552f7358SJed Brown   if (dof) PetscValidPointer(cone, 3);
1933552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
193482f516ccSBarry 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);
1935552f7358SJed Brown   for (c = 0; c < dof; ++c) {
193682f516ccSBarry 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);
1937552f7358SJed Brown     mesh->cones[off+c] = cone[c];
1938552f7358SJed Brown   }
1939552f7358SJed Brown   PetscFunctionReturn(0);
1940552f7358SJed Brown }
1941552f7358SJed Brown 
1942552f7358SJed Brown /*@C
1943eaf898f9SPatrick Sanan   DMPlexGetConeOrientation - Return the orientations on the in-edges for this point in the DAG
1944552f7358SJed Brown 
1945552f7358SJed Brown   Not collective
1946552f7358SJed Brown 
1947552f7358SJed Brown   Input Parameters:
1948552f7358SJed Brown + mesh - The DMPlex
1949eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
1950552f7358SJed Brown 
1951552f7358SJed Brown   Output Parameter:
1952552f7358SJed Brown . coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1953552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1954552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1955552f7358SJed Brown                     the index of the cone point on which to start.
1956552f7358SJed Brown 
1957552f7358SJed Brown   Level: beginner
1958552f7358SJed Brown 
19593813dfbdSMatthew G Knepley   Fortran Notes:
19603813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
19613813dfbdSMatthew G Knepley   include petsc.h90 in your code.
19623b12b3d8SVaclav Hapla   You must also call DMPlexRestoreConeOrientation() after you finish using the returned array.
1963922102d1SVaclav Hapla   DMPlexRestoreConeOrientation() is not needed/available in C.
19643813dfbdSMatthew G Knepley 
1965552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetCone(), DMPlexSetChart()
1966552f7358SJed Brown @*/
1967552f7358SJed Brown PetscErrorCode DMPlexGetConeOrientation(DM dm, PetscInt p, const PetscInt *coneOrientation[])
1968552f7358SJed Brown {
1969552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
1970552f7358SJed Brown   PetscInt       off;
1971552f7358SJed Brown   PetscErrorCode ierr;
1972552f7358SJed Brown 
1973552f7358SJed Brown   PetscFunctionBegin;
1974552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1975552f7358SJed Brown #if defined(PETSC_USE_DEBUG)
1976552f7358SJed Brown   {
1977552f7358SJed Brown     PetscInt dof;
1978552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
1979552f7358SJed Brown     if (dof) PetscValidPointer(coneOrientation, 3);
1980552f7358SJed Brown   }
1981552f7358SJed Brown #endif
1982552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
19830d644c17SKarl Rupp 
1984552f7358SJed Brown   *coneOrientation = &mesh->coneOrientations[off];
1985552f7358SJed Brown   PetscFunctionReturn(0);
1986552f7358SJed Brown }
1987552f7358SJed Brown 
1988552f7358SJed Brown /*@
1989eaf898f9SPatrick Sanan   DMPlexSetConeOrientation - Set the orientations on the in-edges for this point in the DAG
1990552f7358SJed Brown 
1991552f7358SJed Brown   Not collective
1992552f7358SJed Brown 
1993552f7358SJed Brown   Input Parameters:
1994552f7358SJed Brown + mesh - The DMPlex
1995eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
1996552f7358SJed Brown - coneOrientation - An array of orientations which are on the in-edges for point p. An orientation is an
1997552f7358SJed Brown                     integer giving the prescription for cone traversal. If it is negative, the cone is
1998552f7358SJed Brown                     traversed in the opposite direction. Its value 'o', or if negative '-(o+1)', gives
1999552f7358SJed Brown                     the index of the cone point on which to start.
2000552f7358SJed Brown 
2001552f7358SJed Brown   Output Parameter:
2002552f7358SJed Brown 
2003552f7358SJed Brown   Note:
2004552f7358SJed Brown   This should be called after all calls to DMPlexSetConeSize() and DMSetUp().
2005552f7358SJed Brown 
2006552f7358SJed Brown   Level: beginner
2007552f7358SJed Brown 
2008552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetConeOrientation(), DMPlexSetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
2009552f7358SJed Brown @*/
2010552f7358SJed Brown PetscErrorCode DMPlexSetConeOrientation(DM dm, PetscInt p, const PetscInt coneOrientation[])
2011552f7358SJed Brown {
2012552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2013552f7358SJed Brown   PetscInt       pStart, pEnd;
2014552f7358SJed Brown   PetscInt       dof, off, c;
2015552f7358SJed Brown   PetscErrorCode ierr;
2016552f7358SJed Brown 
2017552f7358SJed Brown   PetscFunctionBegin;
2018552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2019552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
2020552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2021552f7358SJed Brown   if (dof) PetscValidPointer(coneOrientation, 3);
2022552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
202382f516ccSBarry 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);
2024552f7358SJed Brown   for (c = 0; c < dof; ++c) {
2025552f7358SJed Brown     PetscInt cdof, o = coneOrientation[c];
2026552f7358SJed Brown 
2027552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, mesh->cones[off+c], &cdof);CHKERRQ(ierr);
202882f516ccSBarry 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);
2029552f7358SJed Brown     mesh->coneOrientations[off+c] = o;
2030552f7358SJed Brown   }
2031552f7358SJed Brown   PetscFunctionReturn(0);
2032552f7358SJed Brown }
2033552f7358SJed Brown 
20347cd05799SMatthew G. Knepley /*@
2035eaf898f9SPatrick Sanan   DMPlexInsertCone - Insert a point into the in-edges for the point p in the DAG
20367cd05799SMatthew G. Knepley 
20377cd05799SMatthew G. Knepley   Not collective
20387cd05799SMatthew G. Knepley 
20397cd05799SMatthew G. Knepley   Input Parameters:
20407cd05799SMatthew G. Knepley + mesh - The DMPlex
2041eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
20427cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
20437cd05799SMatthew G. Knepley - conePoint - The mesh point to insert
20447cd05799SMatthew G. Knepley 
20457cd05799SMatthew G. Knepley   Level: beginner
20467cd05799SMatthew G. Knepley 
20477cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
20487cd05799SMatthew G. Knepley @*/
2049552f7358SJed Brown PetscErrorCode DMPlexInsertCone(DM dm, PetscInt p, PetscInt conePos, PetscInt conePoint)
2050552f7358SJed Brown {
2051552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2052552f7358SJed Brown   PetscInt       pStart, pEnd;
2053552f7358SJed Brown   PetscInt       dof, off;
2054552f7358SJed Brown   PetscErrorCode ierr;
2055552f7358SJed Brown 
2056552f7358SJed Brown   PetscFunctionBegin;
2057552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2058552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
205982f516ccSBarry 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);
206082f516ccSBarry 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);
206177c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
206277c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
206377c88f5bSMatthew 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);
2064552f7358SJed Brown   mesh->cones[off+conePos] = conePoint;
2065552f7358SJed Brown   PetscFunctionReturn(0);
2066552f7358SJed Brown }
2067552f7358SJed Brown 
20687cd05799SMatthew G. Knepley /*@
2069eaf898f9SPatrick Sanan   DMPlexInsertConeOrientation - Insert a point orientation for the in-edge for the point p in the DAG
20707cd05799SMatthew G. Knepley 
20717cd05799SMatthew G. Knepley   Not collective
20727cd05799SMatthew G. Knepley 
20737cd05799SMatthew G. Knepley   Input Parameters:
20747cd05799SMatthew G. Knepley + mesh - The DMPlex
2075eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
20767cd05799SMatthew G. Knepley . conePos - The local index in the cone where the point should be put
20777cd05799SMatthew G. Knepley - coneOrientation - The point orientation to insert
20787cd05799SMatthew G. Knepley 
20797cd05799SMatthew G. Knepley   Level: beginner
20807cd05799SMatthew G. Knepley 
20817cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
20827cd05799SMatthew G. Knepley @*/
208377c88f5bSMatthew G Knepley PetscErrorCode DMPlexInsertConeOrientation(DM dm, PetscInt p, PetscInt conePos, PetscInt coneOrientation)
208477c88f5bSMatthew G Knepley {
208577c88f5bSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
208677c88f5bSMatthew G Knepley   PetscInt       pStart, pEnd;
208777c88f5bSMatthew G Knepley   PetscInt       dof, off;
208877c88f5bSMatthew G Knepley   PetscErrorCode ierr;
208977c88f5bSMatthew G Knepley 
209077c88f5bSMatthew G Knepley   PetscFunctionBegin;
209177c88f5bSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
209277c88f5bSMatthew G Knepley   ierr = PetscSectionGetChart(mesh->coneSection, &pStart, &pEnd);CHKERRQ(ierr);
209377c88f5bSMatthew 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);
209477c88f5bSMatthew G Knepley   ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
209577c88f5bSMatthew G Knepley   ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
209677c88f5bSMatthew 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);
209777c88f5bSMatthew G Knepley   mesh->coneOrientations[off+conePos] = coneOrientation;
209877c88f5bSMatthew G Knepley   PetscFunctionReturn(0);
209977c88f5bSMatthew G Knepley }
210077c88f5bSMatthew G Knepley 
2101552f7358SJed Brown /*@
2102eaf898f9SPatrick Sanan   DMPlexGetSupportSize - Return the number of out-edges for this point in the DAG
2103552f7358SJed Brown 
2104552f7358SJed Brown   Not collective
2105552f7358SJed Brown 
2106552f7358SJed Brown   Input Parameters:
2107552f7358SJed Brown + mesh - The DMPlex
2108eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2109552f7358SJed Brown 
2110552f7358SJed Brown   Output Parameter:
2111552f7358SJed Brown . size - The support size for point p
2112552f7358SJed Brown 
2113552f7358SJed Brown   Level: beginner
2114552f7358SJed Brown 
2115552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart(), DMPlexGetConeSize()
2116552f7358SJed Brown @*/
2117552f7358SJed Brown PetscErrorCode DMPlexGetSupportSize(DM dm, PetscInt p, PetscInt *size)
2118552f7358SJed Brown {
2119552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2120552f7358SJed Brown   PetscErrorCode ierr;
2121552f7358SJed Brown 
2122552f7358SJed Brown   PetscFunctionBegin;
2123552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2124552f7358SJed Brown   PetscValidPointer(size, 3);
2125552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
2126552f7358SJed Brown   PetscFunctionReturn(0);
2127552f7358SJed Brown }
2128552f7358SJed Brown 
2129552f7358SJed Brown /*@
2130eaf898f9SPatrick Sanan   DMPlexSetSupportSize - Set the number of out-edges for this point in the DAG
2131552f7358SJed Brown 
2132552f7358SJed Brown   Not collective
2133552f7358SJed Brown 
2134552f7358SJed Brown   Input Parameters:
2135552f7358SJed Brown + mesh - The DMPlex
2136eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2137552f7358SJed Brown - size - The support size for point p
2138552f7358SJed Brown 
2139552f7358SJed Brown   Output Parameter:
2140552f7358SJed Brown 
2141552f7358SJed Brown   Note:
2142552f7358SJed Brown   This should be called after DMPlexSetChart().
2143552f7358SJed Brown 
2144552f7358SJed Brown   Level: beginner
2145552f7358SJed Brown 
2146552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexGetSupportSize(), DMPlexSetChart()
2147552f7358SJed Brown @*/
2148552f7358SJed Brown PetscErrorCode DMPlexSetSupportSize(DM dm, PetscInt p, PetscInt size)
2149552f7358SJed Brown {
2150552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2151552f7358SJed Brown   PetscErrorCode ierr;
2152552f7358SJed Brown 
2153552f7358SJed Brown   PetscFunctionBegin;
2154552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2155552f7358SJed Brown   ierr = PetscSectionSetDof(mesh->supportSection, p, size);CHKERRQ(ierr);
21560d644c17SKarl Rupp 
2157552f7358SJed Brown   mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, size);
2158552f7358SJed Brown   PetscFunctionReturn(0);
2159552f7358SJed Brown }
2160552f7358SJed Brown 
2161552f7358SJed Brown /*@C
2162eaf898f9SPatrick Sanan   DMPlexGetSupport - Return the points on the out-edges for this point in the DAG
2163552f7358SJed Brown 
2164552f7358SJed Brown   Not collective
2165552f7358SJed Brown 
2166552f7358SJed Brown   Input Parameters:
2167552f7358SJed Brown + mesh - The DMPlex
2168eaf898f9SPatrick Sanan - p - The point, which must lie in the chart set with DMPlexSetChart()
2169552f7358SJed Brown 
2170552f7358SJed Brown   Output Parameter:
2171552f7358SJed Brown . support - An array of points which are on the out-edges for point p
2172552f7358SJed Brown 
2173552f7358SJed Brown   Level: beginner
2174552f7358SJed Brown 
21753813dfbdSMatthew G Knepley   Fortran Notes:
21763813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
21773813dfbdSMatthew G Knepley   include petsc.h90 in your code.
21783b12b3d8SVaclav Hapla   You must also call DMPlexRestoreSupport() after you finish using the returned array.
2179922102d1SVaclav Hapla   DMPlexRestoreSupport() is not needed/available in C.
21803813dfbdSMatthew G Knepley 
2181552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2182552f7358SJed Brown @*/
2183552f7358SJed Brown PetscErrorCode DMPlexGetSupport(DM dm, PetscInt p, const PetscInt *support[])
2184552f7358SJed Brown {
2185552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2186552f7358SJed Brown   PetscInt       off;
2187552f7358SJed Brown   PetscErrorCode ierr;
2188552f7358SJed Brown 
2189552f7358SJed Brown   PetscFunctionBegin;
2190552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2191552f7358SJed Brown   PetscValidPointer(support, 3);
2192552f7358SJed Brown   ierr     = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
2193552f7358SJed Brown   *support = &mesh->supports[off];
2194552f7358SJed Brown   PetscFunctionReturn(0);
2195552f7358SJed Brown }
2196552f7358SJed Brown 
2197552f7358SJed Brown /*@
219892371b87SBarry 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
2199552f7358SJed Brown 
2200552f7358SJed Brown   Not collective
2201552f7358SJed Brown 
2202552f7358SJed Brown   Input Parameters:
2203552f7358SJed Brown + mesh - The DMPlex
2204eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
220592371b87SBarry Smith - support - An array of points which are on the out-edges for point p
2206552f7358SJed Brown 
2207552f7358SJed Brown   Output Parameter:
2208552f7358SJed Brown 
2209552f7358SJed Brown   Note:
2210552f7358SJed Brown   This should be called after all calls to DMPlexSetSupportSize() and DMSetUp().
2211552f7358SJed Brown 
2212552f7358SJed Brown   Level: beginner
2213552f7358SJed Brown 
221492371b87SBarry Smith .seealso: DMPlexSetCone(), DMPlexSetConeSize(), DMPlexCreate(), DMPlexGetSupport(), DMPlexSetChart(), DMPlexSetSupportSize(), DMSetUp()
2215552f7358SJed Brown @*/
2216552f7358SJed Brown PetscErrorCode DMPlexSetSupport(DM dm, PetscInt p, const PetscInt support[])
2217552f7358SJed Brown {
2218552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2219552f7358SJed Brown   PetscInt       pStart, pEnd;
2220552f7358SJed Brown   PetscInt       dof, off, c;
2221552f7358SJed Brown   PetscErrorCode ierr;
2222552f7358SJed Brown 
2223552f7358SJed Brown   PetscFunctionBegin;
2224552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2225552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2226552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2227552f7358SJed Brown   if (dof) PetscValidPointer(support, 3);
2228552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
222982f516ccSBarry 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);
2230552f7358SJed Brown   for (c = 0; c < dof; ++c) {
223182f516ccSBarry 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);
2232552f7358SJed Brown     mesh->supports[off+c] = support[c];
2233552f7358SJed Brown   }
2234552f7358SJed Brown   PetscFunctionReturn(0);
2235552f7358SJed Brown }
2236552f7358SJed Brown 
22377cd05799SMatthew G. Knepley /*@
2238eaf898f9SPatrick Sanan   DMPlexInsertSupport - Insert a point into the out-edges for the point p in the DAG
22397cd05799SMatthew G. Knepley 
22407cd05799SMatthew G. Knepley   Not collective
22417cd05799SMatthew G. Knepley 
22427cd05799SMatthew G. Knepley   Input Parameters:
22437cd05799SMatthew G. Knepley + mesh - The DMPlex
2244eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
22457cd05799SMatthew G. Knepley . supportPos - The local index in the cone where the point should be put
22467cd05799SMatthew G. Knepley - supportPoint - The mesh point to insert
22477cd05799SMatthew G. Knepley 
22487cd05799SMatthew G. Knepley   Level: beginner
22497cd05799SMatthew G. Knepley 
22507cd05799SMatthew G. Knepley .seealso: DMPlexCreate(), DMPlexGetCone(), DMPlexSetChart(), DMPlexSetConeSize(), DMSetUp()
22517cd05799SMatthew G. Knepley @*/
2252552f7358SJed Brown PetscErrorCode DMPlexInsertSupport(DM dm, PetscInt p, PetscInt supportPos, PetscInt supportPoint)
2253552f7358SJed Brown {
2254552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2255552f7358SJed Brown   PetscInt       pStart, pEnd;
2256552f7358SJed Brown   PetscInt       dof, off;
2257552f7358SJed Brown   PetscErrorCode ierr;
2258552f7358SJed Brown 
2259552f7358SJed Brown   PetscFunctionBegin;
2260552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2261552f7358SJed Brown   ierr = PetscSectionGetChart(mesh->supportSection, &pStart, &pEnd);CHKERRQ(ierr);
2262552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
2263552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, p, &off);CHKERRQ(ierr);
226482f516ccSBarry 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);
226582f516ccSBarry 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);
226682f516ccSBarry 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);
2267552f7358SJed Brown   mesh->supports[off+supportPos] = supportPoint;
2268552f7358SJed Brown   PetscFunctionReturn(0);
2269552f7358SJed Brown }
2270552f7358SJed Brown 
2271552f7358SJed Brown /*@C
2272eaf898f9SPatrick Sanan   DMPlexGetTransitiveClosure - Return the points on the transitive closure of the in-edges or out-edges for this point in the DAG
2273552f7358SJed Brown 
2274552f7358SJed Brown   Not collective
2275552f7358SJed Brown 
2276552f7358SJed Brown   Input Parameters:
2277552f7358SJed Brown + mesh - The DMPlex
2278eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2279552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
22800298fd71SBarry Smith - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
2281552f7358SJed Brown 
2282552f7358SJed Brown   Output Parameters:
2283552f7358SJed Brown + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
2284552f7358SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
2285552f7358SJed Brown 
2286552f7358SJed Brown   Note:
22870298fd71SBarry Smith   If using internal storage (points is NULL on input), each call overwrites the last output.
2288552f7358SJed Brown 
22893813dfbdSMatthew G Knepley   Fortran Notes:
22903813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
22913813dfbdSMatthew G Knepley   include petsc.h90 in your code.
22923813dfbdSMatthew G Knepley 
22933813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
22943813dfbdSMatthew G Knepley 
2295552f7358SJed Brown   Level: beginner
2296552f7358SJed Brown 
2297552f7358SJed Brown .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2298552f7358SJed Brown @*/
2299552f7358SJed Brown PetscErrorCode DMPlexGetTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2300552f7358SJed Brown {
2301552f7358SJed Brown   DM_Plex        *mesh = (DM_Plex*) dm->data;
2302552f7358SJed Brown   PetscInt       *closure, *fifo;
23030298fd71SBarry Smith   const PetscInt *tmp = NULL, *tmpO = NULL;
2304552f7358SJed Brown   PetscInt        tmpSize, t;
2305552f7358SJed Brown   PetscInt        depth       = 0, maxSize;
2306552f7358SJed Brown   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
2307552f7358SJed Brown   PetscErrorCode  ierr;
2308552f7358SJed Brown 
2309552f7358SJed Brown   PetscFunctionBegin;
2310552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2311552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2312552f7358SJed Brown   /* This is only 1-level */
2313552f7358SJed Brown   if (useCone) {
2314552f7358SJed Brown     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
2315552f7358SJed Brown     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
2316552f7358SJed Brown     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
2317552f7358SJed Brown   } else {
2318552f7358SJed Brown     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
2319552f7358SJed Brown     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
2320552f7358SJed Brown   }
2321bfbcdd7aSMatthew G. Knepley   if (depth == 1) {
2322bfbcdd7aSMatthew G. Knepley     if (*points) {
2323bfbcdd7aSMatthew G. Knepley       closure = *points;
2324bfbcdd7aSMatthew G. Knepley     } else {
2325bfbcdd7aSMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
232669291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2327bfbcdd7aSMatthew G. Knepley     }
2328bfbcdd7aSMatthew G. Knepley     closure[0] = p; closure[1] = 0;
2329bfbcdd7aSMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
2330bfbcdd7aSMatthew G. Knepley       closure[closureSize]   = tmp[t];
2331bfbcdd7aSMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[t] : 0;
2332bfbcdd7aSMatthew G. Knepley     }
2333bfbcdd7aSMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
2334bfbcdd7aSMatthew G. Knepley     if (points)    *points    = closure;
2335bfbcdd7aSMatthew G. Knepley     PetscFunctionReturn(0);
2336bfbcdd7aSMatthew G. Knepley   }
233724c766afSToby Isaac   {
233824c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
233924c766afSToby Isaac 
234024c766afSToby Isaac     c = mesh->maxConeSize;
234124c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
234224c766afSToby Isaac     s = mesh->maxSupportSize;
234324c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
234424c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
234524c766afSToby Isaac   }
234669291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2347bfbcdd7aSMatthew G. Knepley   if (*points) {
2348bfbcdd7aSMatthew G. Knepley     closure = *points;
2349bfbcdd7aSMatthew G. Knepley   } else {
235069291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
2351bfbcdd7aSMatthew G. Knepley   }
2352bfbcdd7aSMatthew G. Knepley   closure[0] = p; closure[1] = 0;
2353552f7358SJed Brown   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
2354552f7358SJed Brown     const PetscInt cp = tmp[t];
2355552f7358SJed Brown     const PetscInt co = tmpO ? tmpO[t] : 0;
2356552f7358SJed Brown 
2357552f7358SJed Brown     closure[closureSize]   = cp;
2358552f7358SJed Brown     closure[closureSize+1] = co;
2359552f7358SJed Brown     fifo[fifoSize]         = cp;
2360552f7358SJed Brown     fifo[fifoSize+1]       = co;
2361552f7358SJed Brown   }
2362bfbcdd7aSMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
2363552f7358SJed Brown   while (fifoSize - fifoStart) {
2364552f7358SJed Brown     const PetscInt q   = fifo[fifoStart];
2365552f7358SJed Brown     const PetscInt o   = fifo[fifoStart+1];
2366552f7358SJed Brown     const PetscInt rev = o >= 0 ? 0 : 1;
2367552f7358SJed Brown     const PetscInt off = rev ? -(o+1) : o;
2368552f7358SJed Brown 
2369552f7358SJed Brown     if (useCone) {
2370552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
2371552f7358SJed Brown       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
2372552f7358SJed Brown       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
2373552f7358SJed Brown     } else {
2374552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
2375552f7358SJed Brown       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
23760298fd71SBarry Smith       tmpO = NULL;
2377552f7358SJed Brown     }
2378552f7358SJed Brown     for (t = 0; t < tmpSize; ++t) {
2379552f7358SJed Brown       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
2380552f7358SJed Brown       const PetscInt cp = tmp[i];
23812e1b13c2SMatthew 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. */
23822e1b13c2SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
23832e1b13c2SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
23842e1b13c2SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
2385552f7358SJed Brown       PetscInt       c;
2386552f7358SJed Brown 
23872e1b13c2SMatthew G. Knepley       if (rev) {
23882e1b13c2SMatthew G. Knepley         PetscInt childSize, coff;
23892e1b13c2SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
23902e1b13c2SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
23912e1b13c2SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
23922e1b13c2SMatthew G. Knepley       }
2393552f7358SJed Brown       /* Check for duplicate */
2394552f7358SJed Brown       for (c = 0; c < closureSize; c += 2) {
2395552f7358SJed Brown         if (closure[c] == cp) break;
2396552f7358SJed Brown       }
2397552f7358SJed Brown       if (c == closureSize) {
2398552f7358SJed Brown         closure[closureSize]   = cp;
2399552f7358SJed Brown         closure[closureSize+1] = co;
2400552f7358SJed Brown         fifo[fifoSize]         = cp;
2401552f7358SJed Brown         fifo[fifoSize+1]       = co;
2402552f7358SJed Brown         closureSize           += 2;
2403552f7358SJed Brown         fifoSize              += 2;
2404552f7358SJed Brown       }
2405552f7358SJed Brown     }
2406552f7358SJed Brown     fifoStart += 2;
2407552f7358SJed Brown   }
2408552f7358SJed Brown   if (numPoints) *numPoints = closureSize/2;
2409552f7358SJed Brown   if (points)    *points    = closure;
241069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
2411552f7358SJed Brown   PetscFunctionReturn(0);
2412552f7358SJed Brown }
2413552f7358SJed Brown 
24149bf0dad6SMatthew G. Knepley /*@C
2415eaf898f9SPatrick 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
24169bf0dad6SMatthew G. Knepley 
24179bf0dad6SMatthew G. Knepley   Not collective
24189bf0dad6SMatthew G. Knepley 
24199bf0dad6SMatthew G. Knepley   Input Parameters:
24209bf0dad6SMatthew G. Knepley + mesh - The DMPlex
2421eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
24229bf0dad6SMatthew G. Knepley . orientation - The orientation of the point
24239bf0dad6SMatthew G. Knepley . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
24249bf0dad6SMatthew G. Knepley - points - If points is NULL on input, internal storage will be returned, otherwise the provided array is used
24259bf0dad6SMatthew G. Knepley 
24269bf0dad6SMatthew G. Knepley   Output Parameters:
24279bf0dad6SMatthew G. Knepley + numPoints - The number of points in the closure, so points[] is of size 2*numPoints
24289bf0dad6SMatthew G. Knepley - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...]
24299bf0dad6SMatthew G. Knepley 
24309bf0dad6SMatthew G. Knepley   Note:
24319bf0dad6SMatthew G. Knepley   If using internal storage (points is NULL on input), each call overwrites the last output.
24329bf0dad6SMatthew G. Knepley 
24339bf0dad6SMatthew G. Knepley   Fortran Notes:
24349bf0dad6SMatthew G. Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
24359bf0dad6SMatthew G. Knepley   include petsc.h90 in your code.
24369bf0dad6SMatthew G. Knepley 
24379bf0dad6SMatthew G. Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
24389bf0dad6SMatthew G. Knepley 
24399bf0dad6SMatthew G. Knepley   Level: beginner
24409bf0dad6SMatthew G. Knepley 
24419bf0dad6SMatthew G. Knepley .seealso: DMPlexRestoreTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
24429bf0dad6SMatthew G. Knepley @*/
24439bf0dad6SMatthew G. Knepley PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM dm, PetscInt p, PetscInt ornt, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
24449bf0dad6SMatthew G. Knepley {
24459bf0dad6SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex*) dm->data;
24469bf0dad6SMatthew G. Knepley   PetscInt       *closure, *fifo;
24479bf0dad6SMatthew G. Knepley   const PetscInt *tmp = NULL, *tmpO = NULL;
24489bf0dad6SMatthew G. Knepley   PetscInt        tmpSize, t;
24499bf0dad6SMatthew G. Knepley   PetscInt        depth       = 0, maxSize;
24509bf0dad6SMatthew G. Knepley   PetscInt        closureSize = 2, fifoSize = 0, fifoStart = 0;
24519bf0dad6SMatthew G. Knepley   PetscErrorCode  ierr;
24529bf0dad6SMatthew G. Knepley 
24539bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
24549bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24559bf0dad6SMatthew G. Knepley   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
24569bf0dad6SMatthew G. Knepley   /* This is only 1-level */
24579bf0dad6SMatthew G. Knepley   if (useCone) {
24589bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &tmpSize);CHKERRQ(ierr);
24599bf0dad6SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &tmp);CHKERRQ(ierr);
24609bf0dad6SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dm, p, &tmpO);CHKERRQ(ierr);
24619bf0dad6SMatthew G. Knepley   } else {
24629bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &tmpSize);CHKERRQ(ierr);
24639bf0dad6SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &tmp);CHKERRQ(ierr);
24649bf0dad6SMatthew G. Knepley   }
24659bf0dad6SMatthew G. Knepley   if (depth == 1) {
24669bf0dad6SMatthew G. Knepley     if (*points) {
24679bf0dad6SMatthew G. Knepley       closure = *points;
24689bf0dad6SMatthew G. Knepley     } else {
24699bf0dad6SMatthew G. Knepley       maxSize = 2*(PetscMax(mesh->maxConeSize, mesh->maxSupportSize)+1);
247069291d52SBarry Smith       ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
24719bf0dad6SMatthew G. Knepley     }
24729bf0dad6SMatthew G. Knepley     closure[0] = p; closure[1] = ornt;
24739bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t, closureSize += 2) {
24749bf0dad6SMatthew G. Knepley       const PetscInt i = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
24759bf0dad6SMatthew G. Knepley       closure[closureSize]   = tmp[i];
24769bf0dad6SMatthew G. Knepley       closure[closureSize+1] = tmpO ? tmpO[i] : 0;
24779bf0dad6SMatthew G. Knepley     }
24789bf0dad6SMatthew G. Knepley     if (numPoints) *numPoints = closureSize/2;
24799bf0dad6SMatthew G. Knepley     if (points)    *points    = closure;
24809bf0dad6SMatthew G. Knepley     PetscFunctionReturn(0);
24819bf0dad6SMatthew G. Knepley   }
248224c766afSToby Isaac   {
248324c766afSToby Isaac     PetscInt c, coneSeries, s,supportSeries;
248424c766afSToby Isaac 
248524c766afSToby Isaac     c = mesh->maxConeSize;
248624c766afSToby Isaac     coneSeries = (c > 1) ? ((PetscPowInt(c,depth+1)-1)/(c-1)) : depth+1;
248724c766afSToby Isaac     s = mesh->maxSupportSize;
248824c766afSToby Isaac     supportSeries = (s > 1) ? ((PetscPowInt(s,depth+1)-1)/(s-1)) : depth+1;
248924c766afSToby Isaac     maxSize = 2*PetscMax(coneSeries,supportSeries);
249024c766afSToby Isaac   }
249169291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
24929bf0dad6SMatthew G. Knepley   if (*points) {
24939bf0dad6SMatthew G. Knepley     closure = *points;
24949bf0dad6SMatthew G. Knepley   } else {
249569291d52SBarry Smith     ierr = DMGetWorkArray(dm, maxSize, MPIU_INT, &closure);CHKERRQ(ierr);
24969bf0dad6SMatthew G. Knepley   }
24979bf0dad6SMatthew G. Knepley   closure[0] = p; closure[1] = ornt;
24989bf0dad6SMatthew G. Knepley   for (t = 0; t < tmpSize; ++t, closureSize += 2, fifoSize += 2) {
24999bf0dad6SMatthew G. Knepley     const PetscInt i  = ornt >= 0 ? (t+ornt)%tmpSize : (-(ornt+1) + tmpSize-t)%tmpSize;
25009bf0dad6SMatthew G. Knepley     const PetscInt cp = tmp[i];
25019bf0dad6SMatthew G. Knepley     PetscInt       co = tmpO ? tmpO[i] : 0;
25029bf0dad6SMatthew G. Knepley 
25039bf0dad6SMatthew G. Knepley     if (ornt < 0) {
25049bf0dad6SMatthew G. Knepley       PetscInt childSize, coff;
25059bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
250686b63641SMatthew G. Knepley       coff = co < 0 ? -(tmpO[i]+1) : tmpO[i];
25079bf0dad6SMatthew G. Knepley       co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
25089bf0dad6SMatthew G. Knepley     }
25099bf0dad6SMatthew G. Knepley     closure[closureSize]   = cp;
25109bf0dad6SMatthew G. Knepley     closure[closureSize+1] = co;
25119bf0dad6SMatthew G. Knepley     fifo[fifoSize]         = cp;
25129bf0dad6SMatthew G. Knepley     fifo[fifoSize+1]       = co;
25139bf0dad6SMatthew G. Knepley   }
25149bf0dad6SMatthew G. Knepley   /* Should kick out early when depth is reached, rather than checking all vertices for empty cones */
25159bf0dad6SMatthew G. Knepley   while (fifoSize - fifoStart) {
25169bf0dad6SMatthew G. Knepley     const PetscInt q   = fifo[fifoStart];
25179bf0dad6SMatthew G. Knepley     const PetscInt o   = fifo[fifoStart+1];
25189bf0dad6SMatthew G. Knepley     const PetscInt rev = o >= 0 ? 0 : 1;
25199bf0dad6SMatthew G. Knepley     const PetscInt off = rev ? -(o+1) : o;
25209bf0dad6SMatthew G. Knepley 
25219bf0dad6SMatthew G. Knepley     if (useCone) {
25229bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, q, &tmpSize);CHKERRQ(ierr);
25239bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, q, &tmp);CHKERRQ(ierr);
25249bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, q, &tmpO);CHKERRQ(ierr);
25259bf0dad6SMatthew G. Knepley     } else {
25269bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, q, &tmpSize);CHKERRQ(ierr);
25279bf0dad6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, q, &tmp);CHKERRQ(ierr);
25289bf0dad6SMatthew G. Knepley       tmpO = NULL;
25299bf0dad6SMatthew G. Knepley     }
25309bf0dad6SMatthew G. Knepley     for (t = 0; t < tmpSize; ++t) {
25319bf0dad6SMatthew G. Knepley       const PetscInt i  = ((rev ? tmpSize-t : t) + off)%tmpSize;
25329bf0dad6SMatthew G. Knepley       const PetscInt cp = tmp[i];
25339bf0dad6SMatthew 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. */
25349bf0dad6SMatthew G. Knepley       /* HACK: It is worse to get the size here, than to change the interpretation of -(*+1)
25359bf0dad6SMatthew G. Knepley        const PetscInt co = tmpO ? (rev ? -(tmpO[i]+1) : tmpO[i]) : 0; */
25369bf0dad6SMatthew G. Knepley       PetscInt       co = tmpO ? tmpO[i] : 0;
25379bf0dad6SMatthew G. Knepley       PetscInt       c;
25389bf0dad6SMatthew G. Knepley 
25399bf0dad6SMatthew G. Knepley       if (rev) {
25409bf0dad6SMatthew G. Knepley         PetscInt childSize, coff;
25419bf0dad6SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, cp, &childSize);CHKERRQ(ierr);
25429bf0dad6SMatthew G. Knepley         coff = tmpO[i] < 0 ? -(tmpO[i]+1) : tmpO[i];
25439bf0dad6SMatthew G. Knepley         co   = childSize ? -(((coff+childSize-1)%childSize)+1) : 0;
25449bf0dad6SMatthew G. Knepley       }
25459bf0dad6SMatthew G. Knepley       /* Check for duplicate */
25469bf0dad6SMatthew G. Knepley       for (c = 0; c < closureSize; c += 2) {
25479bf0dad6SMatthew G. Knepley         if (closure[c] == cp) break;
25489bf0dad6SMatthew G. Knepley       }
25499bf0dad6SMatthew G. Knepley       if (c == closureSize) {
25509bf0dad6SMatthew G. Knepley         closure[closureSize]   = cp;
25519bf0dad6SMatthew G. Knepley         closure[closureSize+1] = co;
25529bf0dad6SMatthew G. Knepley         fifo[fifoSize]         = cp;
25539bf0dad6SMatthew G. Knepley         fifo[fifoSize+1]       = co;
25549bf0dad6SMatthew G. Knepley         closureSize           += 2;
25559bf0dad6SMatthew G. Knepley         fifoSize              += 2;
25569bf0dad6SMatthew G. Knepley       }
25579bf0dad6SMatthew G. Knepley     }
25589bf0dad6SMatthew G. Knepley     fifoStart += 2;
25599bf0dad6SMatthew G. Knepley   }
25609bf0dad6SMatthew G. Knepley   if (numPoints) *numPoints = closureSize/2;
25619bf0dad6SMatthew G. Knepley   if (points)    *points    = closure;
256269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, maxSize, MPIU_INT, &fifo);CHKERRQ(ierr);
25639bf0dad6SMatthew G. Knepley   PetscFunctionReturn(0);
25649bf0dad6SMatthew G. Knepley }
25659bf0dad6SMatthew G. Knepley 
2566552f7358SJed Brown /*@C
2567eaf898f9SPatrick Sanan   DMPlexRestoreTransitiveClosure - Restore the array of points on the transitive closure of the in-edges or out-edges for this point in the DAG
2568552f7358SJed Brown 
2569552f7358SJed Brown   Not collective
2570552f7358SJed Brown 
2571552f7358SJed Brown   Input Parameters:
2572552f7358SJed Brown + mesh - The DMPlex
2573eaf898f9SPatrick Sanan . p - The point, which must lie in the chart set with DMPlexSetChart()
2574552f7358SJed Brown . useCone - PETSC_TRUE for in-edges,  otherwise use out-edges
2575e5c84f05SJed Brown . numPoints - The number of points in the closure, so points[] is of size 2*numPoints, zeroed on exit
2576e5c84f05SJed Brown - points - The points and point orientations, interleaved as pairs [p0, o0, p1, o1, ...], zeroed on exit
2577552f7358SJed Brown 
2578552f7358SJed Brown   Note:
25790298fd71SBarry Smith   If not using internal storage (points is not NULL on input), this call is unnecessary
2580552f7358SJed Brown 
25813813dfbdSMatthew G Knepley   Fortran Notes:
25823813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
25833813dfbdSMatthew G Knepley   include petsc.h90 in your code.
25843813dfbdSMatthew G Knepley 
25853813dfbdSMatthew G Knepley   The numPoints argument is not present in the Fortran 90 binding since it is internal to the array.
25863813dfbdSMatthew G Knepley 
2587552f7358SJed Brown   Level: beginner
2588552f7358SJed Brown 
2589552f7358SJed Brown .seealso: DMPlexGetTransitiveClosure(), DMPlexCreate(), DMPlexSetCone(), DMPlexSetChart(), DMPlexGetCone()
2590552f7358SJed Brown @*/
2591552f7358SJed Brown PetscErrorCode DMPlexRestoreTransitiveClosure(DM dm, PetscInt p, PetscBool useCone, PetscInt *numPoints, PetscInt *points[])
2592552f7358SJed Brown {
2593552f7358SJed Brown   PetscErrorCode ierr;
2594552f7358SJed Brown 
2595552f7358SJed Brown   PetscFunctionBegin;
2596552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2597e5c84f05SJed Brown   if (numPoints) PetscValidIntPointer(numPoints,4);
2598e5c84f05SJed Brown   if (points) PetscValidPointer(points,5);
259969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, points);CHKERRQ(ierr);
26004ff43b2cSJed Brown   if (numPoints) *numPoints = 0;
2601552f7358SJed Brown   PetscFunctionReturn(0);
2602552f7358SJed Brown }
2603552f7358SJed Brown 
2604552f7358SJed Brown /*@
2605eaf898f9SPatrick Sanan   DMPlexGetMaxSizes - Return the maximum number of in-edges (cone) and out-edges (support) for any point in the DAG
2606552f7358SJed Brown 
2607552f7358SJed Brown   Not collective
2608552f7358SJed Brown 
2609552f7358SJed Brown   Input Parameter:
2610552f7358SJed Brown . mesh - The DMPlex
2611552f7358SJed Brown 
2612552f7358SJed Brown   Output Parameters:
2613552f7358SJed Brown + maxConeSize - The maximum number of in-edges
2614552f7358SJed Brown - maxSupportSize - The maximum number of out-edges
2615552f7358SJed Brown 
2616552f7358SJed Brown   Level: beginner
2617552f7358SJed Brown 
2618552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetConeSize(), DMPlexSetChart()
2619552f7358SJed Brown @*/
2620552f7358SJed Brown PetscErrorCode DMPlexGetMaxSizes(DM dm, PetscInt *maxConeSize, PetscInt *maxSupportSize)
2621552f7358SJed Brown {
2622552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
2623552f7358SJed Brown 
2624552f7358SJed Brown   PetscFunctionBegin;
2625552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2626552f7358SJed Brown   if (maxConeSize)    *maxConeSize    = mesh->maxConeSize;
2627552f7358SJed Brown   if (maxSupportSize) *maxSupportSize = mesh->maxSupportSize;
2628552f7358SJed Brown   PetscFunctionReturn(0);
2629552f7358SJed Brown }
2630552f7358SJed Brown 
2631552f7358SJed Brown PetscErrorCode DMSetUp_Plex(DM dm)
2632552f7358SJed Brown {
2633552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2634552f7358SJed Brown   PetscInt       size;
2635552f7358SJed Brown   PetscErrorCode ierr;
2636552f7358SJed Brown 
2637552f7358SJed Brown   PetscFunctionBegin;
2638552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2639552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->coneSection);CHKERRQ(ierr);
2640552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->coneSection, &size);CHKERRQ(ierr);
26411795a4d1SJed Brown   ierr = PetscMalloc1(size, &mesh->cones);CHKERRQ(ierr);
26421795a4d1SJed Brown   ierr = PetscCalloc1(size, &mesh->coneOrientations);CHKERRQ(ierr);
2643552f7358SJed Brown   if (mesh->maxSupportSize) {
2644552f7358SJed Brown     ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2645552f7358SJed Brown     ierr = PetscSectionGetStorageSize(mesh->supportSection, &size);CHKERRQ(ierr);
26461795a4d1SJed Brown     ierr = PetscMalloc1(size, &mesh->supports);CHKERRQ(ierr);
2647552f7358SJed Brown   }
2648552f7358SJed Brown   PetscFunctionReturn(0);
2649552f7358SJed Brown }
2650552f7358SJed Brown 
2651276c5506SMatthew G. Knepley PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
2652552f7358SJed Brown {
2653552f7358SJed Brown   PetscErrorCode ierr;
2654552f7358SJed Brown 
2655552f7358SJed Brown   PetscFunctionBegin;
26564d9407bcSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
2657792b654fSMatthew G. Knepley   ierr = DMCreateSectionSubDM(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
2658c2939958SSatish Balay   if (subdm) {(*subdm)->useNatural = dm->useNatural;}
2659736995cdSBlaise Bourdin   if (dm->useNatural && dm->sfMigration) {
2660f94b4a02SBlaise Bourdin     PetscSF        sfMigrationInv,sfNatural;
2661f94b4a02SBlaise Bourdin     PetscSection   section, sectionSeq;
2662f94b4a02SBlaise Bourdin 
26633dcd263cSBlaise Bourdin     (*subdm)->sfMigration = dm->sfMigration;
26643dcd263cSBlaise Bourdin     ierr = PetscObjectReference((PetscObject) dm->sfMigration);CHKERRQ(ierr);
266592fd8e1eSJed Brown     ierr = DMGetLocalSection((*subdm), &section);CHKERRQ(ierr);CHKERRQ(ierr);
2666f94b4a02SBlaise Bourdin     ierr = PetscSFCreateInverseSF((*subdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
2667f94b4a02SBlaise Bourdin     ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*subdm)), &sectionSeq);CHKERRQ(ierr);
2668f94b4a02SBlaise Bourdin     ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
2669f94b4a02SBlaise Bourdin 
2670f94b4a02SBlaise Bourdin     ierr = DMPlexCreateGlobalToNaturalSF(*subdm, sectionSeq, (*subdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2671c40e9495SBlaise Bourdin     (*subdm)->sfNatural = sfNatural;
2672f94b4a02SBlaise Bourdin     ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
2673f94b4a02SBlaise Bourdin     ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
2674f94b4a02SBlaise Bourdin   }
2675552f7358SJed Brown   PetscFunctionReturn(0);
2676552f7358SJed Brown }
2677552f7358SJed Brown 
26782adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm)
26792adcc780SMatthew G. Knepley {
26802adcc780SMatthew G. Knepley   PetscErrorCode ierr;
26813dcd263cSBlaise Bourdin   PetscInt       i = 0;
26822adcc780SMatthew G. Knepley 
26832adcc780SMatthew G. Knepley   PetscFunctionBegin;
2684435bcee1SStefano Zampini   ierr = DMClone(dms[0], superdm);CHKERRQ(ierr);
2685792b654fSMatthew G. Knepley   ierr = DMCreateSectionSuperDM(dms, len, is, superdm);CHKERRQ(ierr);
2686c40e9495SBlaise Bourdin   (*superdm)->useNatural = PETSC_FALSE;
26873dcd263cSBlaise Bourdin   for (i = 0; i < len; i++){
26883dcd263cSBlaise Bourdin     if (dms[i]->useNatural && dms[i]->sfMigration) {
26893dcd263cSBlaise Bourdin       PetscSF        sfMigrationInv,sfNatural;
26903dcd263cSBlaise Bourdin       PetscSection   section, sectionSeq;
26913dcd263cSBlaise Bourdin 
26923dcd263cSBlaise Bourdin       (*superdm)->sfMigration = dms[i]->sfMigration;
26933dcd263cSBlaise Bourdin       ierr = PetscObjectReference((PetscObject) dms[i]->sfMigration);CHKERRQ(ierr);
2694c40e9495SBlaise Bourdin       (*superdm)->useNatural = PETSC_TRUE;
269592fd8e1eSJed Brown       ierr = DMGetLocalSection((*superdm), &section);CHKERRQ(ierr);
26963dcd263cSBlaise Bourdin       ierr = PetscSFCreateInverseSF((*superdm)->sfMigration, &sfMigrationInv);CHKERRQ(ierr);
26973dcd263cSBlaise Bourdin       ierr = PetscSectionCreate(PetscObjectComm((PetscObject) (*superdm)), &sectionSeq);CHKERRQ(ierr);
26983dcd263cSBlaise Bourdin       ierr = PetscSFDistributeSection(sfMigrationInv, section, NULL, sectionSeq);CHKERRQ(ierr);
26993dcd263cSBlaise Bourdin 
27003dcd263cSBlaise Bourdin       ierr = DMPlexCreateGlobalToNaturalSF(*superdm, sectionSeq, (*superdm)->sfMigration, &sfNatural);CHKERRQ(ierr);
2701c40e9495SBlaise Bourdin       (*superdm)->sfNatural = sfNatural;
27023dcd263cSBlaise Bourdin       ierr = PetscSectionDestroy(&sectionSeq);CHKERRQ(ierr);
27033dcd263cSBlaise Bourdin       ierr = PetscSFDestroy(&sfMigrationInv);CHKERRQ(ierr);
27043dcd263cSBlaise Bourdin       break;
27053dcd263cSBlaise Bourdin     }
27063dcd263cSBlaise Bourdin   }
27072adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
27082adcc780SMatthew G. Knepley }
27092adcc780SMatthew G. Knepley 
2710552f7358SJed Brown /*@
2711eaf898f9SPatrick Sanan   DMPlexSymmetrize - Create support (out-edge) information from cone (in-edge) information
2712552f7358SJed Brown 
2713552f7358SJed Brown   Not collective
2714552f7358SJed Brown 
2715552f7358SJed Brown   Input Parameter:
2716552f7358SJed Brown . mesh - The DMPlex
2717552f7358SJed Brown 
2718552f7358SJed Brown   Output Parameter:
2719552f7358SJed Brown 
2720552f7358SJed Brown   Note:
2721552f7358SJed Brown   This should be called after all calls to DMPlexSetCone()
2722552f7358SJed Brown 
2723552f7358SJed Brown   Level: beginner
2724552f7358SJed Brown 
2725552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSetChart(), DMPlexSetConeSize(), DMPlexSetCone()
2726552f7358SJed Brown @*/
2727552f7358SJed Brown PetscErrorCode DMPlexSymmetrize(DM dm)
2728552f7358SJed Brown {
2729552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2730552f7358SJed Brown   PetscInt      *offsets;
2731552f7358SJed Brown   PetscInt       supportSize;
2732552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2733552f7358SJed Brown   PetscErrorCode ierr;
2734552f7358SJed Brown 
2735552f7358SJed Brown   PetscFunctionBegin;
2736552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
273782f516ccSBarry Smith   if (mesh->supports) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Supports were already setup in this DMPlex");
273830b0ce1bSStefano Zampini   ierr = PetscLogEventBegin(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
2739552f7358SJed Brown   /* Calculate support sizes */
2740552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2741552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2742552f7358SJed Brown     PetscInt dof, off, c;
2743552f7358SJed Brown 
2744552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2745552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2746552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2747552f7358SJed Brown       ierr = PetscSectionAddDof(mesh->supportSection, mesh->cones[c], 1);CHKERRQ(ierr);
2748552f7358SJed Brown     }
2749552f7358SJed Brown   }
2750552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2751552f7358SJed Brown     PetscInt dof;
2752552f7358SJed Brown 
2753552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, p, &dof);CHKERRQ(ierr);
27540d644c17SKarl Rupp 
2755552f7358SJed Brown     mesh->maxSupportSize = PetscMax(mesh->maxSupportSize, dof);
2756552f7358SJed Brown   }
2757552f7358SJed Brown   ierr = PetscSectionSetUp(mesh->supportSection);CHKERRQ(ierr);
2758552f7358SJed Brown   /* Calculate supports */
2759552f7358SJed Brown   ierr = PetscSectionGetStorageSize(mesh->supportSection, &supportSize);CHKERRQ(ierr);
27601795a4d1SJed Brown   ierr = PetscMalloc1(supportSize, &mesh->supports);CHKERRQ(ierr);
27611795a4d1SJed Brown   ierr = PetscCalloc1(pEnd - pStart, &offsets);CHKERRQ(ierr);
2762552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
2763552f7358SJed Brown     PetscInt dof, off, c;
2764552f7358SJed Brown 
2765552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, p, &dof);CHKERRQ(ierr);
2766552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, p, &off);CHKERRQ(ierr);
2767552f7358SJed Brown     for (c = off; c < off+dof; ++c) {
2768552f7358SJed Brown       const PetscInt q = mesh->cones[c];
2769552f7358SJed Brown       PetscInt       offS;
2770552f7358SJed Brown 
2771552f7358SJed Brown       ierr = PetscSectionGetOffset(mesh->supportSection, q, &offS);CHKERRQ(ierr);
27720d644c17SKarl Rupp 
2773552f7358SJed Brown       mesh->supports[offS+offsets[q]] = p;
2774552f7358SJed Brown       ++offsets[q];
2775552f7358SJed Brown     }
2776552f7358SJed Brown   }
2777552f7358SJed Brown   ierr = PetscFree(offsets);CHKERRQ(ierr);
277830b0ce1bSStefano Zampini   ierr = PetscLogEventEnd(DMPLEX_Symmetrize,dm,0,0,0);CHKERRQ(ierr);
2779552f7358SJed Brown   PetscFunctionReturn(0);
2780552f7358SJed Brown }
2781552f7358SJed Brown 
2782277ea44aSLisandro Dalcin static PetscErrorCode DMPlexCreateDepthStratum(DM dm, DMLabel label, PetscInt depth, PetscInt pStart, PetscInt pEnd)
2783277ea44aSLisandro Dalcin {
2784277ea44aSLisandro Dalcin   IS             stratumIS;
2785277ea44aSLisandro Dalcin   PetscErrorCode ierr;
2786277ea44aSLisandro Dalcin 
2787277ea44aSLisandro Dalcin   PetscFunctionBegin;
2788277ea44aSLisandro Dalcin   if (pStart >= pEnd) PetscFunctionReturn(0);
2789277ea44aSLisandro Dalcin #if defined(PETSC_USE_DEBUG)
2790277ea44aSLisandro Dalcin   {
2791277ea44aSLisandro Dalcin     PetscInt  qStart, qEnd, numLevels, level;
2792277ea44aSLisandro Dalcin     PetscBool overlap = PETSC_FALSE;
2793277ea44aSLisandro Dalcin     ierr = DMLabelGetNumValues(label, &numLevels);CHKERRQ(ierr);
2794277ea44aSLisandro Dalcin     for (level = 0; level < numLevels; level++) {
2795277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
2796277ea44aSLisandro Dalcin       if ((pStart >= qStart && pStart < qEnd) || (pEnd > qStart && pEnd <= qEnd)) {overlap = PETSC_TRUE; break;}
2797277ea44aSLisandro Dalcin     }
2798277ea44aSLisandro 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);
2799277ea44aSLisandro Dalcin   }
2800277ea44aSLisandro Dalcin #endif
2801277ea44aSLisandro Dalcin   ierr = ISCreateStride(PETSC_COMM_SELF, pEnd-pStart, pStart, 1, &stratumIS);CHKERRQ(ierr);
2802277ea44aSLisandro Dalcin   ierr = DMLabelSetStratumIS(label, depth, stratumIS);CHKERRQ(ierr);
2803277ea44aSLisandro Dalcin   ierr = ISDestroy(&stratumIS);CHKERRQ(ierr);
2804277ea44aSLisandro Dalcin   PetscFunctionReturn(0);
2805277ea44aSLisandro Dalcin }
2806277ea44aSLisandro Dalcin 
28077d5acc75SStefano Zampini static PetscErrorCode DMPlexCreateDimStratum(DM,DMLabel,DMLabel,PetscInt,PetscInt);
28087d5acc75SStefano Zampini 
2809552f7358SJed Brown /*@
2810a8d69d7bSBarry Smith   DMPlexStratify - The DAG for most topologies is a graded poset (https://en.wikipedia.org/wiki/Graded_poset), and
28116dd80730SBarry Smith   can be illustrated by a Hasse Diagram (https://en.wikipedia.org/wiki/Hasse_diagram). The strata group all points of the
2812552f7358SJed Brown   same grade, and this function calculates the strata. This grade can be seen as the height (or depth) of the point in
2813552f7358SJed Brown   the DAG.
2814552f7358SJed Brown 
2815bf4602e4SToby Isaac   Collective on dm
2816552f7358SJed Brown 
2817552f7358SJed Brown   Input Parameter:
2818552f7358SJed Brown . mesh - The DMPlex
2819552f7358SJed Brown 
2820552f7358SJed Brown   Output Parameter:
2821552f7358SJed Brown 
2822552f7358SJed Brown   Notes:
2823b1bb481bSMatthew Knepley   Concretely, DMPlexStratify() creates a new label named "depth" containing the depth in the DAG of each point. For cell-vertex
2824b1bb481bSMatthew Knepley   meshes, vertices are depth 0 and cells are depth 1. For fully interpolated meshes, depth 0 for vertices, 1 for edges, and so on
2825b1bb481bSMatthew Knepley   until cells have depth equal to the dimension of the mesh. The depth label can be accessed through DMPlexGetDepthLabel() or DMPlexGetDepthStratum(), or
2826c58f1c22SToby Isaac   manually via DMGetLabel().  The height is defined implicitly by height = maxDimension - depth, and can be accessed
2827150b719bSJed Brown   via DMPlexGetHeightStratum().  For example, cells have height 0 and faces have height 1.
2828552f7358SJed Brown 
2829b1bb481bSMatthew Knepley   The depth of a point is calculated by executing a breadth-first search (BFS) on the DAG. This could produce surprising results
2830b1bb481bSMatthew Knepley   if run on a partially interpolated mesh, meaning one that had some edges and faces, but not others. For example, suppose that
2831b1bb481bSMatthew Knepley   we had a mesh consisting of one triangle (c0) and three vertices (v0, v1, v2), and only one edge is on the boundary so we choose
2832b1bb481bSMatthew Knepley   to interpolate only that one (e0), so that
2833b1bb481bSMatthew Knepley   $  cone(c0) = {e0, v2}
2834b1bb481bSMatthew Knepley   $  cone(e0) = {v0, v1}
2835b1bb481bSMatthew Knepley   If DMPlexStratify() is run on this mesh, it will give depths
2836b1bb481bSMatthew Knepley   $  depth 0 = {v0, v1, v2}
2837b1bb481bSMatthew Knepley   $  depth 1 = {e0, c0}
2838b1bb481bSMatthew Knepley   where the triangle has been given depth 1, instead of 2, because it is reachable from vertex v2.
2839b1bb481bSMatthew Knepley 
2840150b719bSJed Brown   DMPlexStratify() should be called after all calls to DMPlexSymmetrize()
2841552f7358SJed Brown 
2842552f7358SJed Brown   Level: beginner
2843552f7358SJed Brown 
2844552f7358SJed Brown .seealso: DMPlexCreate(), DMPlexSymmetrize()
2845552f7358SJed Brown @*/
2846552f7358SJed Brown PetscErrorCode DMPlexStratify(DM dm)
2847552f7358SJed Brown {
2848df0420ecSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
2849aa50250dSMatthew G. Knepley   DMLabel        label;
2850552f7358SJed Brown   PetscInt       pStart, pEnd, p;
2851552f7358SJed Brown   PetscInt       numRoots = 0, numLeaves = 0;
28527d5acc75SStefano Zampini   PetscInt       cMax, fMax, eMax, vMax;
2853552f7358SJed Brown   PetscErrorCode ierr;
2854552f7358SJed Brown 
2855552f7358SJed Brown   PetscFunctionBegin;
2856552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2857552f7358SJed Brown   ierr = PetscLogEventBegin(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2858277ea44aSLisandro Dalcin 
2859277ea44aSLisandro Dalcin   /* Create depth label */
2860aa50250dSMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2861c58f1c22SToby Isaac   ierr = DMCreateLabel(dm, "depth");CHKERRQ(ierr);
2862aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
2863277ea44aSLisandro Dalcin 
2864277ea44aSLisandro Dalcin   {
2865552f7358SJed Brown     /* Initialize roots and count leaves */
2866277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
2867277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
2868552f7358SJed Brown     PetscInt coneSize, supportSize;
2869552f7358SJed Brown 
2870277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
2871552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2872552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2873552f7358SJed Brown       if (!coneSize && supportSize) {
2874277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2875277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2876552f7358SJed Brown         ++numRoots;
2877552f7358SJed Brown       } else if (!supportSize && coneSize) {
2878552f7358SJed Brown         ++numLeaves;
2879552f7358SJed Brown       } else if (!supportSize && !coneSize) {
2880552f7358SJed Brown         /* Isolated points */
2881277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2882277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2883552f7358SJed Brown       }
2884552f7358SJed Brown     }
2885277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 0, sMin, sMax+1);CHKERRQ(ierr);
2886277ea44aSLisandro Dalcin   }
2887277ea44aSLisandro Dalcin 
2888552f7358SJed Brown   if (numRoots + numLeaves == (pEnd - pStart)) {
2889277ea44aSLisandro Dalcin     PetscInt sMin = PETSC_MAX_INT;
2890277ea44aSLisandro Dalcin     PetscInt sMax = PETSC_MIN_INT;
2891552f7358SJed Brown     PetscInt coneSize, supportSize;
2892552f7358SJed Brown 
2893277ea44aSLisandro Dalcin     for (p = pStart; p < pEnd; ++p) {
2894552f7358SJed Brown       ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
2895552f7358SJed Brown       ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
2896552f7358SJed Brown       if (!supportSize && coneSize) {
2897277ea44aSLisandro Dalcin         sMin = PetscMin(p, sMin);
2898277ea44aSLisandro Dalcin         sMax = PetscMax(p, sMax);
2899552f7358SJed Brown       }
2900552f7358SJed Brown     }
2901277ea44aSLisandro Dalcin     ierr = DMPlexCreateDepthStratum(dm, label, 1, sMin, sMax+1);CHKERRQ(ierr);
2902552f7358SJed Brown   } else {
2903277ea44aSLisandro Dalcin     PetscInt level = 0;
2904277ea44aSLisandro Dalcin     PetscInt qStart, qEnd, q;
2905552f7358SJed Brown 
2906277ea44aSLisandro Dalcin     ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
2907277ea44aSLisandro Dalcin     while (qEnd > qStart) {
2908277ea44aSLisandro Dalcin       PetscInt sMin = PETSC_MAX_INT;
2909277ea44aSLisandro Dalcin       PetscInt sMax = PETSC_MIN_INT;
291074ef644bSMatthew G. Knepley 
2911277ea44aSLisandro Dalcin       for (q = qStart; q < qEnd; ++q) {
291274ef644bSMatthew G. Knepley         const PetscInt *support;
291374ef644bSMatthew G. Knepley         PetscInt        supportSize, s;
291474ef644bSMatthew G. Knepley 
2915277ea44aSLisandro Dalcin         ierr = DMPlexGetSupportSize(dm, q, &supportSize);CHKERRQ(ierr);
2916277ea44aSLisandro Dalcin         ierr = DMPlexGetSupport(dm, q, &support);CHKERRQ(ierr);
291774ef644bSMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
2918277ea44aSLisandro Dalcin           sMin = PetscMin(support[s], sMin);
2919277ea44aSLisandro Dalcin           sMax = PetscMax(support[s], sMax);
2920552f7358SJed Brown         }
2921552f7358SJed Brown       }
2922277ea44aSLisandro Dalcin       ierr = DMLabelGetNumValues(label, &level);CHKERRQ(ierr);
2923277ea44aSLisandro Dalcin       ierr = DMPlexCreateDepthStratum(dm, label, level, sMin, sMax+1);CHKERRQ(ierr);
2924277ea44aSLisandro Dalcin       ierr = DMLabelGetStratumBounds(label, level, &qStart, &qEnd);CHKERRQ(ierr);
292574ef644bSMatthew G. Knepley     }
292674ef644bSMatthew G. Knepley   }
2927bf4602e4SToby Isaac   { /* just in case there is an empty process */
2928bf4602e4SToby Isaac     PetscInt numValues, maxValues = 0, v;
2929bf4602e4SToby Isaac 
2930bf4602e4SToby Isaac     ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
29316d1e82d3SBarry Smith     ierr = MPI_Allreduce(&numValues,&maxValues,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
2932bf4602e4SToby Isaac     for (v = numValues; v < maxValues; v++) {
2933367003a6SStefano Zampini       ierr = DMLabelAddStratum(label, v);CHKERRQ(ierr);
2934bf4602e4SToby Isaac     }
2935bf4602e4SToby Isaac   }
2936d67d17b1SMatthew G. Knepley   ierr = PetscObjectStateGet((PetscObject) label, &mesh->depthState);CHKERRQ(ierr);
29377d5acc75SStefano Zampini 
29387d5acc75SStefano Zampini   ierr = DMPlexGetHybridBounds(dm, &cMax, &fMax, &eMax, &vMax);CHKERRQ(ierr);
29397d5acc75SStefano Zampini   if (cMax >= 0 || fMax >= 0 || eMax >= 0 || vMax >= 0) {
29407d5acc75SStefano Zampini     PetscInt dim;
2941277ea44aSLisandro Dalcin     DMLabel  dimLabel;
29427d5acc75SStefano Zampini 
29437d5acc75SStefano Zampini     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
29447d5acc75SStefano Zampini     ierr = DMCreateLabel(dm, "dim");CHKERRQ(ierr);
29457d5acc75SStefano Zampini     ierr = DMGetLabel(dm, "dim", &dimLabel);CHKERRQ(ierr);
29467d5acc75SStefano Zampini     if (cMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, dim, cMax);CHKERRQ(ierr);}
29477d5acc75SStefano Zampini     if (fMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, dim - 1, fMax);CHKERRQ(ierr);}
29487d5acc75SStefano Zampini     if (eMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, 1, eMax);CHKERRQ(ierr);}
29497d5acc75SStefano Zampini     if (vMax >= 0) {ierr = DMPlexCreateDimStratum(dm, label, dimLabel, 0, vMax);CHKERRQ(ierr);}
29507d5acc75SStefano Zampini   }
2951552f7358SJed Brown   ierr = PetscLogEventEnd(DMPLEX_Stratify,dm,0,0,0);CHKERRQ(ierr);
2952552f7358SJed Brown   PetscFunctionReturn(0);
2953552f7358SJed Brown }
2954552f7358SJed Brown 
2955552f7358SJed Brown /*@C
2956552f7358SJed Brown   DMPlexGetJoin - Get an array for the join of the set of points
2957552f7358SJed Brown 
2958552f7358SJed Brown   Not Collective
2959552f7358SJed Brown 
2960552f7358SJed Brown   Input Parameters:
2961552f7358SJed Brown + dm - The DMPlex object
2962552f7358SJed Brown . numPoints - The number of input points for the join
2963552f7358SJed Brown - points - The input points
2964552f7358SJed Brown 
2965552f7358SJed Brown   Output Parameters:
2966552f7358SJed Brown + numCoveredPoints - The number of points in the join
2967552f7358SJed Brown - coveredPoints - The points in the join
2968552f7358SJed Brown 
2969552f7358SJed Brown   Level: intermediate
2970552f7358SJed Brown 
2971552f7358SJed Brown   Note: Currently, this is restricted to a single level join
2972552f7358SJed Brown 
29733813dfbdSMatthew G Knepley   Fortran Notes:
29743813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
29753813dfbdSMatthew G Knepley   include petsc.h90 in your code.
29763813dfbdSMatthew G Knepley 
29773813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
29783813dfbdSMatthew G Knepley 
2979552f7358SJed Brown .seealso: DMPlexRestoreJoin(), DMPlexGetMeet()
2980552f7358SJed Brown @*/
2981552f7358SJed Brown PetscErrorCode DMPlexGetJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
2982552f7358SJed Brown {
2983552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
2984552f7358SJed Brown   PetscInt      *join[2];
2985552f7358SJed Brown   PetscInt       joinSize, i = 0;
2986552f7358SJed Brown   PetscInt       dof, off, p, c, m;
2987552f7358SJed Brown   PetscErrorCode ierr;
2988552f7358SJed Brown 
2989552f7358SJed Brown   PetscFunctionBegin;
2990552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
299148bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
299248bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
299348bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
299469291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
299569291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
2996552f7358SJed Brown   /* Copy in support of first point */
2997552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->supportSection, points[0], &dof);CHKERRQ(ierr);
2998552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->supportSection, points[0], &off);CHKERRQ(ierr);
2999552f7358SJed Brown   for (joinSize = 0; joinSize < dof; ++joinSize) {
3000552f7358SJed Brown     join[i][joinSize] = mesh->supports[off+joinSize];
3001552f7358SJed Brown   }
3002552f7358SJed Brown   /* Check each successive support */
3003552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3004552f7358SJed Brown     PetscInt newJoinSize = 0;
3005552f7358SJed Brown 
3006552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->supportSection, points[p], &dof);CHKERRQ(ierr);
3007552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->supportSection, points[p], &off);CHKERRQ(ierr);
3008552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3009552f7358SJed Brown       const PetscInt point = mesh->supports[off+c];
3010552f7358SJed Brown 
3011552f7358SJed Brown       for (m = 0; m < joinSize; ++m) {
3012552f7358SJed Brown         if (point == join[i][m]) {
3013552f7358SJed Brown           join[1-i][newJoinSize++] = point;
3014552f7358SJed Brown           break;
3015552f7358SJed Brown         }
3016552f7358SJed Brown       }
3017552f7358SJed Brown     }
3018552f7358SJed Brown     joinSize = newJoinSize;
3019552f7358SJed Brown     i        = 1-i;
3020552f7358SJed Brown   }
3021552f7358SJed Brown   *numCoveredPoints = joinSize;
3022552f7358SJed Brown   *coveredPoints    = join[i];
302369291d52SBarry Smith   ierr              = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3024552f7358SJed Brown   PetscFunctionReturn(0);
3025552f7358SJed Brown }
3026552f7358SJed Brown 
3027552f7358SJed Brown /*@C
3028552f7358SJed Brown   DMPlexRestoreJoin - Restore an array for the join of the set of points
3029552f7358SJed Brown 
3030552f7358SJed Brown   Not Collective
3031552f7358SJed Brown 
3032552f7358SJed Brown   Input Parameters:
3033552f7358SJed Brown + dm - The DMPlex object
3034552f7358SJed Brown . numPoints - The number of input points for the join
3035552f7358SJed Brown - points - The input points
3036552f7358SJed Brown 
3037552f7358SJed Brown   Output Parameters:
3038552f7358SJed Brown + numCoveredPoints - The number of points in the join
3039552f7358SJed Brown - coveredPoints - The points in the join
3040552f7358SJed Brown 
30413813dfbdSMatthew G Knepley   Fortran Notes:
30423813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
30433813dfbdSMatthew G Knepley   include petsc.h90 in your code.
30443813dfbdSMatthew G Knepley 
30453813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
30463813dfbdSMatthew G Knepley 
3047552f7358SJed Brown   Level: intermediate
3048552f7358SJed Brown 
3049552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexGetFullJoin(), DMPlexGetMeet()
3050552f7358SJed Brown @*/
3051552f7358SJed Brown PetscErrorCode DMPlexRestoreJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3052552f7358SJed Brown {
3053552f7358SJed Brown   PetscErrorCode ierr;
3054552f7358SJed Brown 
3055552f7358SJed Brown   PetscFunctionBegin;
3056552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3057d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3058d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3059d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints, 5);
306069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3061d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3062552f7358SJed Brown   PetscFunctionReturn(0);
3063552f7358SJed Brown }
3064552f7358SJed Brown 
3065552f7358SJed Brown /*@C
3066552f7358SJed Brown   DMPlexGetFullJoin - Get an array for the join of the set of points
3067552f7358SJed Brown 
3068552f7358SJed Brown   Not Collective
3069552f7358SJed Brown 
3070552f7358SJed Brown   Input Parameters:
3071552f7358SJed Brown + dm - The DMPlex object
3072552f7358SJed Brown . numPoints - The number of input points for the join
3073552f7358SJed Brown - points - The input points
3074552f7358SJed Brown 
3075552f7358SJed Brown   Output Parameters:
3076552f7358SJed Brown + numCoveredPoints - The number of points in the join
3077552f7358SJed Brown - coveredPoints - The points in the join
3078552f7358SJed Brown 
30793813dfbdSMatthew G Knepley   Fortran Notes:
30803813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
30813813dfbdSMatthew G Knepley   include petsc.h90 in your code.
30823813dfbdSMatthew G Knepley 
30833813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
30843813dfbdSMatthew G Knepley 
3085552f7358SJed Brown   Level: intermediate
3086552f7358SJed Brown 
3087552f7358SJed Brown .seealso: DMPlexGetJoin(), DMPlexRestoreJoin(), DMPlexGetMeet()
3088552f7358SJed Brown @*/
3089552f7358SJed Brown PetscErrorCode DMPlexGetFullJoin(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3090552f7358SJed Brown {
3091552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3092552f7358SJed Brown   PetscInt      *offsets, **closures;
3093552f7358SJed Brown   PetscInt      *join[2];
3094552f7358SJed Brown   PetscInt       depth = 0, maxSize, joinSize = 0, i = 0;
309524c766afSToby Isaac   PetscInt       p, d, c, m, ms;
3096552f7358SJed Brown   PetscErrorCode ierr;
3097552f7358SJed Brown 
3098552f7358SJed Brown   PetscFunctionBegin;
3099552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
310048bb261cSVaclav Hapla   PetscValidIntPointer(points, 3);
310148bb261cSVaclav Hapla   PetscValidIntPointer(numCoveredPoints, 4);
310248bb261cSVaclav Hapla   PetscValidPointer(coveredPoints, 5);
3103552f7358SJed Brown 
3104552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
31051795a4d1SJed Brown   ierr    = PetscCalloc1(numPoints, &closures);CHKERRQ(ierr);
310669291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
310724c766afSToby Isaac   ms      = mesh->maxSupportSize;
310824c766afSToby Isaac   maxSize = (ms > 1) ? ((PetscPowInt(ms,depth+1)-1)/(ms-1)) : depth + 1;
310969291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[0]);CHKERRQ(ierr);
311069291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &join[1]);CHKERRQ(ierr);
3111552f7358SJed Brown 
3112552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3113552f7358SJed Brown     PetscInt closureSize;
3114552f7358SJed Brown 
3115552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closures[p]);CHKERRQ(ierr);
31160d644c17SKarl Rupp 
3117552f7358SJed Brown     offsets[p*(depth+2)+0] = 0;
3118552f7358SJed Brown     for (d = 0; d < depth+1; ++d) {
3119552f7358SJed Brown       PetscInt pStart, pEnd, i;
3120552f7358SJed Brown 
3121552f7358SJed Brown       ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
3122552f7358SJed Brown       for (i = offsets[p*(depth+2)+d]; i < closureSize; ++i) {
3123552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3124552f7358SJed Brown           offsets[p*(depth+2)+d+1] = i;
3125552f7358SJed Brown           break;
3126552f7358SJed Brown         }
3127552f7358SJed Brown       }
3128552f7358SJed Brown       if (i == closureSize) offsets[p*(depth+2)+d+1] = i;
3129552f7358SJed Brown     }
313082f516ccSBarry 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);
3131552f7358SJed Brown   }
3132552f7358SJed Brown   for (d = 0; d < depth+1; ++d) {
3133552f7358SJed Brown     PetscInt dof;
3134552f7358SJed Brown 
3135552f7358SJed Brown     /* Copy in support of first point */
3136552f7358SJed Brown     dof = offsets[d+1] - offsets[d];
3137552f7358SJed Brown     for (joinSize = 0; joinSize < dof; ++joinSize) {
3138552f7358SJed Brown       join[i][joinSize] = closures[0][(offsets[d]+joinSize)*2];
3139552f7358SJed Brown     }
3140552f7358SJed Brown     /* Check each successive cone */
3141552f7358SJed Brown     for (p = 1; p < numPoints && joinSize; ++p) {
3142552f7358SJed Brown       PetscInt newJoinSize = 0;
3143552f7358SJed Brown 
3144552f7358SJed Brown       dof = offsets[p*(depth+2)+d+1] - offsets[p*(depth+2)+d];
3145552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3146552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(depth+2)+d]+c)*2];
3147552f7358SJed Brown 
3148552f7358SJed Brown         for (m = 0; m < joinSize; ++m) {
3149552f7358SJed Brown           if (point == join[i][m]) {
3150552f7358SJed Brown             join[1-i][newJoinSize++] = point;
3151552f7358SJed Brown             break;
3152552f7358SJed Brown           }
3153552f7358SJed Brown         }
3154552f7358SJed Brown       }
3155552f7358SJed Brown       joinSize = newJoinSize;
3156552f7358SJed Brown       i        = 1-i;
3157552f7358SJed Brown     }
3158552f7358SJed Brown     if (joinSize) break;
3159552f7358SJed Brown   }
3160552f7358SJed Brown   *numCoveredPoints = joinSize;
3161552f7358SJed Brown   *coveredPoints    = join[i];
3162552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
31630298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, NULL, &closures[p]);CHKERRQ(ierr);
3164552f7358SJed Brown   }
3165552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
316669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(depth+2), MPIU_INT, &offsets);CHKERRQ(ierr);
316769291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxSupportSize, MPIU_INT, &join[1-i]);CHKERRQ(ierr);
3168552f7358SJed Brown   PetscFunctionReturn(0);
3169552f7358SJed Brown }
3170552f7358SJed Brown 
3171552f7358SJed Brown /*@C
3172552f7358SJed Brown   DMPlexGetMeet - Get an array for the meet of the set of points
3173552f7358SJed Brown 
3174552f7358SJed Brown   Not Collective
3175552f7358SJed Brown 
3176552f7358SJed Brown   Input Parameters:
3177552f7358SJed Brown + dm - The DMPlex object
3178552f7358SJed Brown . numPoints - The number of input points for the meet
3179552f7358SJed Brown - points - The input points
3180552f7358SJed Brown 
3181552f7358SJed Brown   Output Parameters:
3182552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3183552f7358SJed Brown - coveredPoints - The points in the meet
3184552f7358SJed Brown 
3185552f7358SJed Brown   Level: intermediate
3186552f7358SJed Brown 
3187552f7358SJed Brown   Note: Currently, this is restricted to a single level meet
3188552f7358SJed Brown 
31893813dfbdSMatthew G Knepley   Fortran Notes:
31903813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
31913813dfbdSMatthew G Knepley   include petsc.h90 in your code.
31923813dfbdSMatthew G Knepley 
31933813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
31943813dfbdSMatthew G Knepley 
3195552f7358SJed Brown .seealso: DMPlexRestoreMeet(), DMPlexGetJoin()
3196552f7358SJed Brown @*/
3197552f7358SJed Brown PetscErrorCode DMPlexGetMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveringPoints, const PetscInt **coveringPoints)
3198552f7358SJed Brown {
3199552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3200552f7358SJed Brown   PetscInt      *meet[2];
3201552f7358SJed Brown   PetscInt       meetSize, i = 0;
3202552f7358SJed Brown   PetscInt       dof, off, p, c, m;
3203552f7358SJed Brown   PetscErrorCode ierr;
3204552f7358SJed Brown 
3205552f7358SJed Brown   PetscFunctionBegin;
3206552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3207552f7358SJed Brown   PetscValidPointer(points, 2);
3208552f7358SJed Brown   PetscValidPointer(numCoveringPoints, 3);
3209552f7358SJed Brown   PetscValidPointer(coveringPoints, 4);
321069291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
321169291d52SBarry Smith   ierr = DMGetWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3212552f7358SJed Brown   /* Copy in cone of first point */
3213552f7358SJed Brown   ierr = PetscSectionGetDof(mesh->coneSection, points[0], &dof);CHKERRQ(ierr);
3214552f7358SJed Brown   ierr = PetscSectionGetOffset(mesh->coneSection, points[0], &off);CHKERRQ(ierr);
3215552f7358SJed Brown   for (meetSize = 0; meetSize < dof; ++meetSize) {
3216552f7358SJed Brown     meet[i][meetSize] = mesh->cones[off+meetSize];
3217552f7358SJed Brown   }
3218552f7358SJed Brown   /* Check each successive cone */
3219552f7358SJed Brown   for (p = 1; p < numPoints; ++p) {
3220552f7358SJed Brown     PetscInt newMeetSize = 0;
3221552f7358SJed Brown 
3222552f7358SJed Brown     ierr = PetscSectionGetDof(mesh->coneSection, points[p], &dof);CHKERRQ(ierr);
3223552f7358SJed Brown     ierr = PetscSectionGetOffset(mesh->coneSection, points[p], &off);CHKERRQ(ierr);
3224552f7358SJed Brown     for (c = 0; c < dof; ++c) {
3225552f7358SJed Brown       const PetscInt point = mesh->cones[off+c];
3226552f7358SJed Brown 
3227552f7358SJed Brown       for (m = 0; m < meetSize; ++m) {
3228552f7358SJed Brown         if (point == meet[i][m]) {
3229552f7358SJed Brown           meet[1-i][newMeetSize++] = point;
3230552f7358SJed Brown           break;
3231552f7358SJed Brown         }
3232552f7358SJed Brown       }
3233552f7358SJed Brown     }
3234552f7358SJed Brown     meetSize = newMeetSize;
3235552f7358SJed Brown     i        = 1-i;
3236552f7358SJed Brown   }
3237552f7358SJed Brown   *numCoveringPoints = meetSize;
3238552f7358SJed Brown   *coveringPoints    = meet[i];
323969291d52SBarry Smith   ierr               = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3240552f7358SJed Brown   PetscFunctionReturn(0);
3241552f7358SJed Brown }
3242552f7358SJed Brown 
3243552f7358SJed Brown /*@C
3244552f7358SJed Brown   DMPlexRestoreMeet - Restore an array for the meet of the set of points
3245552f7358SJed Brown 
3246552f7358SJed Brown   Not Collective
3247552f7358SJed Brown 
3248552f7358SJed Brown   Input Parameters:
3249552f7358SJed Brown + dm - The DMPlex object
3250552f7358SJed Brown . numPoints - The number of input points for the meet
3251552f7358SJed Brown - points - The input points
3252552f7358SJed Brown 
3253552f7358SJed Brown   Output Parameters:
3254552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3255552f7358SJed Brown - coveredPoints - The points in the meet
3256552f7358SJed Brown 
3257552f7358SJed Brown   Level: intermediate
3258552f7358SJed Brown 
32593813dfbdSMatthew G Knepley   Fortran Notes:
32603813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
32613813dfbdSMatthew G Knepley   include petsc.h90 in your code.
32623813dfbdSMatthew G Knepley 
32633813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
32643813dfbdSMatthew G Knepley 
3265552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexGetFullMeet(), DMPlexGetJoin()
3266552f7358SJed Brown @*/
3267552f7358SJed Brown PetscErrorCode DMPlexRestoreMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3268552f7358SJed Brown {
3269552f7358SJed Brown   PetscErrorCode ierr;
3270552f7358SJed Brown 
3271552f7358SJed Brown   PetscFunctionBegin;
3272552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3273d7902bd2SMatthew G. Knepley   if (points) PetscValidIntPointer(points,3);
3274d7902bd2SMatthew G. Knepley   if (numCoveredPoints) PetscValidIntPointer(numCoveredPoints,4);
3275d7902bd2SMatthew G. Knepley   PetscValidPointer(coveredPoints,5);
327669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, (void*) coveredPoints);CHKERRQ(ierr);
3277d7902bd2SMatthew G. Knepley   if (numCoveredPoints) *numCoveredPoints = 0;
3278552f7358SJed Brown   PetscFunctionReturn(0);
3279552f7358SJed Brown }
3280552f7358SJed Brown 
3281552f7358SJed Brown /*@C
3282552f7358SJed Brown   DMPlexGetFullMeet - Get an array for the meet of the set of points
3283552f7358SJed Brown 
3284552f7358SJed Brown   Not Collective
3285552f7358SJed Brown 
3286552f7358SJed Brown   Input Parameters:
3287552f7358SJed Brown + dm - The DMPlex object
3288552f7358SJed Brown . numPoints - The number of input points for the meet
3289552f7358SJed Brown - points - The input points
3290552f7358SJed Brown 
3291552f7358SJed Brown   Output Parameters:
3292552f7358SJed Brown + numCoveredPoints - The number of points in the meet
3293552f7358SJed Brown - coveredPoints - The points in the meet
3294552f7358SJed Brown 
3295552f7358SJed Brown   Level: intermediate
3296552f7358SJed Brown 
32973813dfbdSMatthew G Knepley   Fortran Notes:
32983813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
32993813dfbdSMatthew G Knepley   include petsc.h90 in your code.
33003813dfbdSMatthew G Knepley 
33013813dfbdSMatthew G Knepley   The numCoveredPoints argument is not present in the Fortran 90 binding since it is internal to the array.
33023813dfbdSMatthew G Knepley 
3303552f7358SJed Brown .seealso: DMPlexGetMeet(), DMPlexRestoreMeet(), DMPlexGetJoin()
3304552f7358SJed Brown @*/
3305552f7358SJed Brown PetscErrorCode DMPlexGetFullMeet(DM dm, PetscInt numPoints, const PetscInt points[], PetscInt *numCoveredPoints, const PetscInt **coveredPoints)
3306552f7358SJed Brown {
3307552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
3308552f7358SJed Brown   PetscInt      *offsets, **closures;
3309552f7358SJed Brown   PetscInt      *meet[2];
3310552f7358SJed Brown   PetscInt       height = 0, maxSize, meetSize = 0, i = 0;
331124c766afSToby Isaac   PetscInt       p, h, c, m, mc;
3312552f7358SJed Brown   PetscErrorCode ierr;
3313552f7358SJed Brown 
3314552f7358SJed Brown   PetscFunctionBegin;
3315552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3316552f7358SJed Brown   PetscValidPointer(points, 2);
3317552f7358SJed Brown   PetscValidPointer(numCoveredPoints, 3);
3318552f7358SJed Brown   PetscValidPointer(coveredPoints, 4);
3319552f7358SJed Brown 
3320552f7358SJed Brown   ierr    = DMPlexGetDepth(dm, &height);CHKERRQ(ierr);
3321785e854fSJed Brown   ierr    = PetscMalloc1(numPoints, &closures);CHKERRQ(ierr);
332269291d52SBarry Smith   ierr    = DMGetWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
332324c766afSToby Isaac   mc      = mesh->maxConeSize;
332424c766afSToby Isaac   maxSize = (mc > 1) ? ((PetscPowInt(mc,height+1)-1)/(mc-1)) : height + 1;
332569291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[0]);CHKERRQ(ierr);
332669291d52SBarry Smith   ierr    = DMGetWorkArray(dm, maxSize, MPIU_INT, &meet[1]);CHKERRQ(ierr);
3327552f7358SJed Brown 
3328552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
3329552f7358SJed Brown     PetscInt closureSize;
3330552f7358SJed Brown 
3331552f7358SJed Brown     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closures[p]);CHKERRQ(ierr);
33320d644c17SKarl Rupp 
3333552f7358SJed Brown     offsets[p*(height+2)+0] = 0;
3334552f7358SJed Brown     for (h = 0; h < height+1; ++h) {
3335552f7358SJed Brown       PetscInt pStart, pEnd, i;
3336552f7358SJed Brown 
3337552f7358SJed Brown       ierr = DMPlexGetHeightStratum(dm, h, &pStart, &pEnd);CHKERRQ(ierr);
3338552f7358SJed Brown       for (i = offsets[p*(height+2)+h]; i < closureSize; ++i) {
3339552f7358SJed Brown         if ((pStart > closures[p][i*2]) || (pEnd <= closures[p][i*2])) {
3340552f7358SJed Brown           offsets[p*(height+2)+h+1] = i;
3341552f7358SJed Brown           break;
3342552f7358SJed Brown         }
3343552f7358SJed Brown       }
3344552f7358SJed Brown       if (i == closureSize) offsets[p*(height+2)+h+1] = i;
3345552f7358SJed Brown     }
334682f516ccSBarry 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);
3347552f7358SJed Brown   }
3348552f7358SJed Brown   for (h = 0; h < height+1; ++h) {
3349552f7358SJed Brown     PetscInt dof;
3350552f7358SJed Brown 
3351552f7358SJed Brown     /* Copy in cone of first point */
3352552f7358SJed Brown     dof = offsets[h+1] - offsets[h];
3353552f7358SJed Brown     for (meetSize = 0; meetSize < dof; ++meetSize) {
3354552f7358SJed Brown       meet[i][meetSize] = closures[0][(offsets[h]+meetSize)*2];
3355552f7358SJed Brown     }
3356552f7358SJed Brown     /* Check each successive cone */
3357552f7358SJed Brown     for (p = 1; p < numPoints && meetSize; ++p) {
3358552f7358SJed Brown       PetscInt newMeetSize = 0;
3359552f7358SJed Brown 
3360552f7358SJed Brown       dof = offsets[p*(height+2)+h+1] - offsets[p*(height+2)+h];
3361552f7358SJed Brown       for (c = 0; c < dof; ++c) {
3362552f7358SJed Brown         const PetscInt point = closures[p][(offsets[p*(height+2)+h]+c)*2];
3363552f7358SJed Brown 
3364552f7358SJed Brown         for (m = 0; m < meetSize; ++m) {
3365552f7358SJed Brown           if (point == meet[i][m]) {
3366552f7358SJed Brown             meet[1-i][newMeetSize++] = point;
3367552f7358SJed Brown             break;
3368552f7358SJed Brown           }
3369552f7358SJed Brown         }
3370552f7358SJed Brown       }
3371552f7358SJed Brown       meetSize = newMeetSize;
3372552f7358SJed Brown       i        = 1-i;
3373552f7358SJed Brown     }
3374552f7358SJed Brown     if (meetSize) break;
3375552f7358SJed Brown   }
3376552f7358SJed Brown   *numCoveredPoints = meetSize;
3377552f7358SJed Brown   *coveredPoints    = meet[i];
3378552f7358SJed Brown   for (p = 0; p < numPoints; ++p) {
33790298fd71SBarry Smith     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, NULL, &closures[p]);CHKERRQ(ierr);
3380552f7358SJed Brown   }
3381552f7358SJed Brown   ierr = PetscFree(closures);CHKERRQ(ierr);
338269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numPoints*(height+2), MPIU_INT, &offsets);CHKERRQ(ierr);
338369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, mesh->maxConeSize, MPIU_INT, &meet[1-i]);CHKERRQ(ierr);
3384552f7358SJed Brown   PetscFunctionReturn(0);
3385552f7358SJed Brown }
3386552f7358SJed Brown 
33874e3744c5SMatthew G. Knepley /*@C
33884e3744c5SMatthew G. Knepley   DMPlexEqual - Determine if two DMs have the same topology
33894e3744c5SMatthew G. Knepley 
33904e3744c5SMatthew G. Knepley   Not Collective
33914e3744c5SMatthew G. Knepley 
33924e3744c5SMatthew G. Knepley   Input Parameters:
33934e3744c5SMatthew G. Knepley + dmA - A DMPlex object
33944e3744c5SMatthew G. Knepley - dmB - A DMPlex object
33954e3744c5SMatthew G. Knepley 
33964e3744c5SMatthew G. Knepley   Output Parameters:
33974e3744c5SMatthew G. Knepley . equal - PETSC_TRUE if the topologies are identical
33984e3744c5SMatthew G. Knepley 
33994e3744c5SMatthew G. Knepley   Level: intermediate
34004e3744c5SMatthew G. Knepley 
34014e3744c5SMatthew G. Knepley   Notes:
34024e3744c5SMatthew G. Knepley   We are not solving graph isomorphism, so we do not permutation.
34034e3744c5SMatthew G. Knepley 
34044e3744c5SMatthew G. Knepley .seealso: DMPlexGetCone()
34054e3744c5SMatthew G. Knepley @*/
34064e3744c5SMatthew G. Knepley PetscErrorCode DMPlexEqual(DM dmA, DM dmB, PetscBool *equal)
34074e3744c5SMatthew G. Knepley {
34084e3744c5SMatthew G. Knepley   PetscInt       depth, depthB, pStart, pEnd, pStartB, pEndB, p;
34094e3744c5SMatthew G. Knepley   PetscErrorCode ierr;
34104e3744c5SMatthew G. Knepley 
34114e3744c5SMatthew G. Knepley   PetscFunctionBegin;
34124e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
34134e3744c5SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
34144e3744c5SMatthew G. Knepley   PetscValidPointer(equal, 3);
34154e3744c5SMatthew G. Knepley 
34164e3744c5SMatthew G. Knepley   *equal = PETSC_FALSE;
34174e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmA, &depth);CHKERRQ(ierr);
34184e3744c5SMatthew G. Knepley   ierr = DMPlexGetDepth(dmB, &depthB);CHKERRQ(ierr);
34194e3744c5SMatthew G. Knepley   if (depth != depthB) PetscFunctionReturn(0);
34204e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmA, &pStart,  &pEnd);CHKERRQ(ierr);
34214e3744c5SMatthew G. Knepley   ierr = DMPlexGetChart(dmB, &pStartB, &pEndB);CHKERRQ(ierr);
34224e3744c5SMatthew G. Knepley   if ((pStart != pStartB) || (pEnd != pEndB)) PetscFunctionReturn(0);
34234e3744c5SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
34244e3744c5SMatthew G. Knepley     const PetscInt *cone, *coneB, *ornt, *orntB, *support, *supportB;
34254e3744c5SMatthew G. Knepley     PetscInt        coneSize, coneSizeB, c, supportSize, supportSizeB, s;
34264e3744c5SMatthew G. Knepley 
34274e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmA, p, &coneSize);CHKERRQ(ierr);
34284e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmA, p, &cone);CHKERRQ(ierr);
34294e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmA, p, &ornt);CHKERRQ(ierr);
34304e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeSize(dmB, p, &coneSizeB);CHKERRQ(ierr);
34314e3744c5SMatthew G. Knepley     ierr = DMPlexGetCone(dmB, p, &coneB);CHKERRQ(ierr);
34324e3744c5SMatthew G. Knepley     ierr = DMPlexGetConeOrientation(dmB, p, &orntB);CHKERRQ(ierr);
34334e3744c5SMatthew G. Knepley     if (coneSize != coneSizeB) PetscFunctionReturn(0);
34344e3744c5SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
34354e3744c5SMatthew G. Knepley       if (cone[c] != coneB[c]) PetscFunctionReturn(0);
34364e3744c5SMatthew G. Knepley       if (ornt[c] != orntB[c]) PetscFunctionReturn(0);
34374e3744c5SMatthew G. Knepley     }
34384e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmA, p, &supportSize);CHKERRQ(ierr);
34394e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmA, p, &support);CHKERRQ(ierr);
34404e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmB, p, &supportSizeB);CHKERRQ(ierr);
34414e3744c5SMatthew G. Knepley     ierr = DMPlexGetSupport(dmB, p, &supportB);CHKERRQ(ierr);
34424e3744c5SMatthew G. Knepley     if (supportSize != supportSizeB) PetscFunctionReturn(0);
34434e3744c5SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
34444e3744c5SMatthew G. Knepley       if (support[s] != supportB[s]) PetscFunctionReturn(0);
34454e3744c5SMatthew G. Knepley     }
34464e3744c5SMatthew G. Knepley   }
34474e3744c5SMatthew G. Knepley   *equal = PETSC_TRUE;
34484e3744c5SMatthew G. Knepley   PetscFunctionReturn(0);
34494e3744c5SMatthew G. Knepley }
34504e3744c5SMatthew G. Knepley 
34517cd05799SMatthew G. Knepley /*@C
34527cd05799SMatthew G. Knepley   DMPlexGetNumFaceVertices - Returns the number of vertices on a face
34537cd05799SMatthew G. Knepley 
34547cd05799SMatthew G. Knepley   Not Collective
34557cd05799SMatthew G. Knepley 
34567cd05799SMatthew G. Knepley   Input Parameters:
34577cd05799SMatthew G. Knepley + dm         - The DMPlex
34587cd05799SMatthew G. Knepley . cellDim    - The cell dimension
34597cd05799SMatthew G. Knepley - numCorners - The number of vertices on a cell
34607cd05799SMatthew G. Knepley 
34617cd05799SMatthew G. Knepley   Output Parameters:
34627cd05799SMatthew G. Knepley . numFaceVertices - The number of vertices on a face
34637cd05799SMatthew G. Knepley 
34647cd05799SMatthew G. Knepley   Level: developer
34657cd05799SMatthew G. Knepley 
34667cd05799SMatthew G. Knepley   Notes:
34677cd05799SMatthew G. Knepley   Of course this can only work for a restricted set of symmetric shapes
34687cd05799SMatthew G. Knepley 
34697cd05799SMatthew G. Knepley .seealso: DMPlexGetCone()
34707cd05799SMatthew G. Knepley @*/
347118ad9376SMatthew G. Knepley PetscErrorCode DMPlexGetNumFaceVertices(DM dm, PetscInt cellDim, PetscInt numCorners, PetscInt *numFaceVertices)
3472a6dfd86eSKarl Rupp {
347382f516ccSBarry Smith   MPI_Comm       comm;
3474552f7358SJed Brown   PetscErrorCode ierr;
3475552f7358SJed Brown 
3476552f7358SJed Brown   PetscFunctionBegin;
347782f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3478552f7358SJed Brown   PetscValidPointer(numFaceVertices,3);
3479552f7358SJed Brown   switch (cellDim) {
3480552f7358SJed Brown   case 0:
3481552f7358SJed Brown     *numFaceVertices = 0;
3482552f7358SJed Brown     break;
3483552f7358SJed Brown   case 1:
3484552f7358SJed Brown     *numFaceVertices = 1;
3485552f7358SJed Brown     break;
3486552f7358SJed Brown   case 2:
3487552f7358SJed Brown     switch (numCorners) {
348819436ca2SJed Brown     case 3: /* triangle */
348919436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3490552f7358SJed Brown       break;
349119436ca2SJed Brown     case 4: /* quadrilateral */
349219436ca2SJed Brown       *numFaceVertices = 2; /* Edge has 2 vertices */
3493552f7358SJed Brown       break;
349419436ca2SJed Brown     case 6: /* quadratic triangle, tri and quad cohesive Lagrange cells */
349519436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3496552f7358SJed Brown       break;
349719436ca2SJed Brown     case 9: /* quadratic quadrilateral, quadratic quad cohesive Lagrange cells */
349819436ca2SJed Brown       *numFaceVertices = 3; /* Edge has 3 vertices */
3499552f7358SJed Brown       break;
3500552f7358SJed Brown     default:
3501f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3502552f7358SJed Brown     }
3503552f7358SJed Brown     break;
3504552f7358SJed Brown   case 3:
3505552f7358SJed Brown     switch (numCorners) {
350619436ca2SJed Brown     case 4: /* tetradehdron */
350719436ca2SJed Brown       *numFaceVertices = 3; /* Face has 3 vertices */
3508552f7358SJed Brown       break;
350919436ca2SJed Brown     case 6: /* tet cohesive cells */
351019436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3511552f7358SJed Brown       break;
351219436ca2SJed Brown     case 8: /* hexahedron */
351319436ca2SJed Brown       *numFaceVertices = 4; /* Face has 4 vertices */
3514552f7358SJed Brown       break;
351519436ca2SJed Brown     case 9: /* tet cohesive Lagrange cells */
351619436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3517552f7358SJed Brown       break;
351819436ca2SJed Brown     case 10: /* quadratic tetrahedron */
351919436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3520552f7358SJed Brown       break;
352119436ca2SJed Brown     case 12: /* hex cohesive Lagrange cells */
352219436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3523552f7358SJed Brown       break;
352419436ca2SJed Brown     case 18: /* quadratic tet cohesive Lagrange cells */
352519436ca2SJed Brown       *numFaceVertices = 6; /* Face has 6 vertices */
3526552f7358SJed Brown       break;
352719436ca2SJed Brown     case 27: /* quadratic hexahedron, quadratic hex cohesive Lagrange cells */
352819436ca2SJed Brown       *numFaceVertices = 9; /* Face has 9 vertices */
3529552f7358SJed Brown       break;
3530552f7358SJed Brown     default:
3531f347f43bSBarry Smith       SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid number of face corners %D for dimension %D", numCorners, cellDim);
3532552f7358SJed Brown     }
3533552f7358SJed Brown     break;
3534552f7358SJed Brown   default:
3535f347f43bSBarry Smith     SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid cell dimension %D", cellDim);
3536552f7358SJed Brown   }
3537552f7358SJed Brown   PetscFunctionReturn(0);
3538552f7358SJed Brown }
3539552f7358SJed Brown 
3540552f7358SJed Brown /*@
3541aa50250dSMatthew G. Knepley   DMPlexGetDepthLabel - Get the DMLabel recording the depth of each point
3542552f7358SJed Brown 
3543552f7358SJed Brown   Not Collective
3544552f7358SJed Brown 
3545aa50250dSMatthew G. Knepley   Input Parameter:
3546552f7358SJed Brown . dm    - The DMPlex object
3547552f7358SJed Brown 
3548aa50250dSMatthew G. Knepley   Output Parameter:
3549aa50250dSMatthew G. Knepley . depthLabel - The DMLabel recording point depth
3550552f7358SJed Brown 
3551552f7358SJed Brown   Level: developer
3552552f7358SJed Brown 
3553aa50250dSMatthew G. Knepley .seealso: DMPlexGetDepth(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum()
3554aa50250dSMatthew G. Knepley @*/
3555aa50250dSMatthew G. Knepley PetscErrorCode DMPlexGetDepthLabel(DM dm, DMLabel *depthLabel)
3556aa50250dSMatthew G. Knepley {
3557aa50250dSMatthew G. Knepley   PetscFunctionBegin;
3558aa50250dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3559aa50250dSMatthew G. Knepley   PetscValidPointer(depthLabel, 2);
3560c58f1c22SToby Isaac   *depthLabel = dm->depthLabel;
3561aa50250dSMatthew G. Knepley   PetscFunctionReturn(0);
3562aa50250dSMatthew G. Knepley }
3563aa50250dSMatthew G. Knepley 
3564aa50250dSMatthew G. Knepley /*@
3565aa50250dSMatthew G. Knepley   DMPlexGetDepth - Get the depth of the DAG representing this mesh
3566aa50250dSMatthew G. Knepley 
3567aa50250dSMatthew G. Knepley   Not Collective
3568aa50250dSMatthew G. Knepley 
3569aa50250dSMatthew G. Knepley   Input Parameter:
3570aa50250dSMatthew G. Knepley . dm    - The DMPlex object
3571aa50250dSMatthew G. Knepley 
3572aa50250dSMatthew G. Knepley   Output Parameter:
3573aa50250dSMatthew G. Knepley . depth - The number of strata (breadth first levels) in the DAG
3574aa50250dSMatthew G. Knepley 
3575aa50250dSMatthew G. Knepley   Level: developer
3576552f7358SJed Brown 
3577b1bb481bSMatthew Knepley   Notes:
3578b1bb481bSMatthew Knepley   This returns maximum of point depths over all points, i.e. maximum value of the label returned by DMPlexGetDepthLabel().
3579b1bb481bSMatthew Knepley   The point depth is described more in detail in DMPlexSymmetrize().
3580b1bb481bSMatthew Knepley 
3581b1bb481bSMatthew Knepley .seealso: DMPlexGetDepthLabel(), DMPlexGetHeightStratum(), DMPlexGetDepthStratum(), DMPlexSymmetrize()
3582552f7358SJed Brown @*/
3583552f7358SJed Brown PetscErrorCode DMPlexGetDepth(DM dm, PetscInt *depth)
3584552f7358SJed Brown {
3585aa50250dSMatthew G. Knepley   DMLabel        label;
3586aa50250dSMatthew G. Knepley   PetscInt       d = 0;
3587552f7358SJed Brown   PetscErrorCode ierr;
3588552f7358SJed Brown 
3589552f7358SJed Brown   PetscFunctionBegin;
3590552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3591552f7358SJed Brown   PetscValidPointer(depth, 2);
3592aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3593aa50250dSMatthew G. Knepley   if (label) {ierr = DMLabelGetNumValues(label, &d);CHKERRQ(ierr);}
3594552f7358SJed Brown   *depth = d-1;
3595552f7358SJed Brown   PetscFunctionReturn(0);
3596552f7358SJed Brown }
3597552f7358SJed Brown 
3598552f7358SJed Brown /*@
3599552f7358SJed Brown   DMPlexGetDepthStratum - Get the bounds [start, end) for all points at a certain depth.
3600552f7358SJed Brown 
3601552f7358SJed Brown   Not Collective
3602552f7358SJed Brown 
3603552f7358SJed Brown   Input Parameters:
3604552f7358SJed Brown + dm           - The DMPlex object
3605552f7358SJed Brown - stratumValue - The requested depth
3606552f7358SJed Brown 
3607552f7358SJed Brown   Output Parameters:
3608552f7358SJed Brown + start - The first point at this depth
3609552f7358SJed Brown - end   - One beyond the last point at this depth
3610552f7358SJed Brown 
3611647867b2SJed Brown   Notes:
3612647867b2SJed Brown   Depth indexing is related to topological dimension.  Depth stratum 0 contains the lowest topological dimension points,
3613647867b2SJed Brown   often "vertices".  If the mesh is "interpolated" (see DMPlexInterpolate()), then depth stratum 1 contains the next
3614647867b2SJed Brown   higher dimension, e.g., "edges".
3615647867b2SJed Brown 
3616552f7358SJed Brown   Level: developer
3617552f7358SJed Brown 
3618552f7358SJed Brown .seealso: DMPlexGetHeightStratum(), DMPlexGetDepth()
3619552f7358SJed Brown @*/
36200adebc6cSBarry Smith PetscErrorCode DMPlexGetDepthStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
36210adebc6cSBarry Smith {
3622aa50250dSMatthew G. Knepley   DMLabel        label;
362363d1a920SMatthew G. Knepley   PetscInt       pStart, pEnd;
3624552f7358SJed Brown   PetscErrorCode ierr;
3625552f7358SJed Brown 
3626552f7358SJed Brown   PetscFunctionBegin;
3627552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
362863d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
362963d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3630552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
36310d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
363263d1a920SMatthew G. Knepley   if (stratumValue < 0) {
363363d1a920SMatthew G. Knepley     if (start) *start = pStart;
363463d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
363563d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3636552f7358SJed Brown   }
3637aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
363863d1a920SMatthew G. Knepley   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
363963d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, stratumValue, start, end);CHKERRQ(ierr);
3640552f7358SJed Brown   PetscFunctionReturn(0);
3641552f7358SJed Brown }
3642552f7358SJed Brown 
3643552f7358SJed Brown /*@
3644552f7358SJed Brown   DMPlexGetHeightStratum - Get the bounds [start, end) for all points at a certain height.
3645552f7358SJed Brown 
3646552f7358SJed Brown   Not Collective
3647552f7358SJed Brown 
3648552f7358SJed Brown   Input Parameters:
3649552f7358SJed Brown + dm           - The DMPlex object
3650552f7358SJed Brown - stratumValue - The requested height
3651552f7358SJed Brown 
3652552f7358SJed Brown   Output Parameters:
3653552f7358SJed Brown + start - The first point at this height
3654552f7358SJed Brown - end   - One beyond the last point at this height
3655552f7358SJed Brown 
3656647867b2SJed Brown   Notes:
3657647867b2SJed Brown   Height indexing is related to topological codimension.  Height stratum 0 contains the highest topological dimension
3658647867b2SJed Brown   points, often called "cells" or "elements".  If the mesh is "interpolated" (see DMPlexInterpolate()), then height
3659647867b2SJed Brown   stratum 1 contains the boundary of these "cells", often called "faces" or "facets".
3660647867b2SJed Brown 
3661552f7358SJed Brown   Level: developer
3662552f7358SJed Brown 
3663552f7358SJed Brown .seealso: DMPlexGetDepthStratum(), DMPlexGetDepth()
3664552f7358SJed Brown @*/
36650adebc6cSBarry Smith PetscErrorCode DMPlexGetHeightStratum(DM dm, PetscInt stratumValue, PetscInt *start, PetscInt *end)
36660adebc6cSBarry Smith {
3667aa50250dSMatthew G. Knepley   DMLabel        label;
366863d1a920SMatthew G. Knepley   PetscInt       depth, pStart, pEnd;
3669552f7358SJed Brown   PetscErrorCode ierr;
3670552f7358SJed Brown 
3671552f7358SJed Brown   PetscFunctionBegin;
3672552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
367363d1a920SMatthew G. Knepley   if (start) {PetscValidPointer(start, 3); *start = 0;}
367463d1a920SMatthew G. Knepley   if (end)   {PetscValidPointer(end,   4); *end   = 0;}
3675552f7358SJed Brown   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
36760d644c17SKarl Rupp   if (pStart == pEnd) PetscFunctionReturn(0);
367763d1a920SMatthew G. Knepley   if (stratumValue < 0) {
367863d1a920SMatthew G. Knepley     if (start) *start = pStart;
367963d1a920SMatthew G. Knepley     if (end)   *end   = pEnd;
368063d1a920SMatthew G. Knepley     PetscFunctionReturn(0);
3681552f7358SJed Brown   }
3682aa50250dSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
368313903a91SSatish Balay   if (!label) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "No label named depth was found");
368463d1a920SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &depth);CHKERRQ(ierr);
368563d1a920SMatthew G. Knepley   ierr = DMLabelGetStratumBounds(label, depth-1-stratumValue, start, end);CHKERRQ(ierr);
3686552f7358SJed Brown   PetscFunctionReturn(0);
3687552f7358SJed Brown }
3688552f7358SJed Brown 
36890adebc6cSBarry Smith PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm)
36900adebc6cSBarry Smith {
3691efe440bfSMatthew G. Knepley   PetscSection   section, s;
3692efe440bfSMatthew G. Knepley   Mat            m;
36933e922f36SToby Isaac   PetscInt       maxHeight;
3694552f7358SJed Brown   PetscErrorCode ierr;
3695552f7358SJed Brown 
3696552f7358SJed Brown   PetscFunctionBegin;
369738221697SMatthew G. Knepley   ierr = DMClone(dm, cdm);CHKERRQ(ierr);
36983e922f36SToby Isaac   ierr = DMPlexGetMaxProjectionHeight(dm, &maxHeight);CHKERRQ(ierr);
36993e922f36SToby Isaac   ierr = DMPlexSetMaxProjectionHeight(*cdm, maxHeight);CHKERRQ(ierr);
370082f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
370192fd8e1eSJed Brown   ierr = DMSetLocalSection(*cdm, section);CHKERRQ(ierr);
37021d799100SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
3703efe440bfSMatthew G. Knepley   ierr = PetscSectionCreate(PETSC_COMM_SELF, &s);CHKERRQ(ierr);
3704efe440bfSMatthew G. Knepley   ierr = MatCreate(PETSC_COMM_SELF, &m);CHKERRQ(ierr);
3705efe440bfSMatthew G. Knepley   ierr = DMSetDefaultConstraints(*cdm, s, m);CHKERRQ(ierr);
3706efe440bfSMatthew G. Knepley   ierr = PetscSectionDestroy(&s);CHKERRQ(ierr);
3707efe440bfSMatthew G. Knepley   ierr = MatDestroy(&m);CHKERRQ(ierr);
37088f4c458bSMatthew G. Knepley 
37098f4c458bSMatthew G. Knepley   ierr = DMSetNumFields(*cdm, 1);CHKERRQ(ierr);
37108f4c458bSMatthew G. Knepley   ierr = DMCreateDS(*cdm);CHKERRQ(ierr);
3711552f7358SJed Brown   PetscFunctionReturn(0);
3712552f7358SJed Brown }
3713552f7358SJed Brown 
3714f19dbd58SToby Isaac PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field)
3715f19dbd58SToby Isaac {
3716f19dbd58SToby Isaac   Vec            coordsLocal;
3717f19dbd58SToby Isaac   DM             coordsDM;
3718f19dbd58SToby Isaac   PetscErrorCode ierr;
3719f19dbd58SToby Isaac 
3720f19dbd58SToby Isaac   PetscFunctionBegin;
3721f19dbd58SToby Isaac   *field = NULL;
3722f19dbd58SToby Isaac   ierr = DMGetCoordinatesLocal(dm,&coordsLocal);CHKERRQ(ierr);
3723f19dbd58SToby Isaac   ierr = DMGetCoordinateDM(dm,&coordsDM);CHKERRQ(ierr);
3724f19dbd58SToby Isaac   if (coordsLocal && coordsDM) {
3725f19dbd58SToby Isaac     ierr = DMFieldCreateDS(coordsDM, 0, coordsLocal, field);CHKERRQ(ierr);
3726f19dbd58SToby Isaac   }
3727f19dbd58SToby Isaac   PetscFunctionReturn(0);
3728f19dbd58SToby Isaac }
3729f19dbd58SToby Isaac 
37307cd05799SMatthew G. Knepley /*@C
37317cd05799SMatthew G. Knepley   DMPlexGetConeSection - Return a section which describes the layout of cone data
37327cd05799SMatthew G. Knepley 
37337cd05799SMatthew G. Knepley   Not Collective
37347cd05799SMatthew G. Knepley 
37357cd05799SMatthew G. Knepley   Input Parameters:
37367cd05799SMatthew G. Knepley . dm        - The DMPlex object
37377cd05799SMatthew G. Knepley 
37387cd05799SMatthew G. Knepley   Output Parameter:
37397cd05799SMatthew G. Knepley . section - The PetscSection object
37407cd05799SMatthew G. Knepley 
37417cd05799SMatthew G. Knepley   Level: developer
37427cd05799SMatthew G. Knepley 
37437cd05799SMatthew G. Knepley .seealso: DMPlexGetSupportSection(), DMPlexGetCones(), DMPlexGetConeOrientations()
37447cd05799SMatthew G. Knepley @*/
37450adebc6cSBarry Smith PetscErrorCode DMPlexGetConeSection(DM dm, PetscSection *section)
37460adebc6cSBarry Smith {
3747552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3748552f7358SJed Brown 
3749552f7358SJed Brown   PetscFunctionBegin;
3750552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3751552f7358SJed Brown   if (section) *section = mesh->coneSection;
3752552f7358SJed Brown   PetscFunctionReturn(0);
3753552f7358SJed Brown }
3754552f7358SJed Brown 
37557cd05799SMatthew G. Knepley /*@C
37567cd05799SMatthew G. Knepley   DMPlexGetSupportSection - Return a section which describes the layout of support data
37577cd05799SMatthew G. Knepley 
37587cd05799SMatthew G. Knepley   Not Collective
37597cd05799SMatthew G. Knepley 
37607cd05799SMatthew G. Knepley   Input Parameters:
37617cd05799SMatthew G. Knepley . dm        - The DMPlex object
37627cd05799SMatthew G. Knepley 
37637cd05799SMatthew G. Knepley   Output Parameter:
37647cd05799SMatthew G. Knepley . section - The PetscSection object
37657cd05799SMatthew G. Knepley 
37667cd05799SMatthew G. Knepley   Level: developer
37677cd05799SMatthew G. Knepley 
37687cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
37697cd05799SMatthew G. Knepley @*/
37708cb4d582SMatthew G. Knepley PetscErrorCode DMPlexGetSupportSection(DM dm, PetscSection *section)
37718cb4d582SMatthew G. Knepley {
37728cb4d582SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex*) dm->data;
37738cb4d582SMatthew G. Knepley 
37748cb4d582SMatthew G. Knepley   PetscFunctionBegin;
37758cb4d582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37768cb4d582SMatthew G. Knepley   if (section) *section = mesh->supportSection;
37778cb4d582SMatthew G. Knepley   PetscFunctionReturn(0);
37788cb4d582SMatthew G. Knepley }
37798cb4d582SMatthew G. Knepley 
37807cd05799SMatthew G. Knepley /*@C
37817cd05799SMatthew G. Knepley   DMPlexGetCones - Return cone data
37827cd05799SMatthew G. Knepley 
37837cd05799SMatthew G. Knepley   Not Collective
37847cd05799SMatthew G. Knepley 
37857cd05799SMatthew G. Knepley   Input Parameters:
37867cd05799SMatthew G. Knepley . dm        - The DMPlex object
37877cd05799SMatthew G. Knepley 
37887cd05799SMatthew G. Knepley   Output Parameter:
37897cd05799SMatthew G. Knepley . cones - The cone for each point
37907cd05799SMatthew G. Knepley 
37917cd05799SMatthew G. Knepley   Level: developer
37927cd05799SMatthew G. Knepley 
37937cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
37947cd05799SMatthew G. Knepley @*/
3795a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetCones(DM dm, PetscInt *cones[])
3796a6dfd86eSKarl Rupp {
3797552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3798552f7358SJed Brown 
3799552f7358SJed Brown   PetscFunctionBegin;
3800552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3801552f7358SJed Brown   if (cones) *cones = mesh->cones;
3802552f7358SJed Brown   PetscFunctionReturn(0);
3803552f7358SJed Brown }
3804552f7358SJed Brown 
38057cd05799SMatthew G. Knepley /*@C
38067cd05799SMatthew G. Knepley   DMPlexGetConeOrientations - Return cone orientation data
38077cd05799SMatthew G. Knepley 
38087cd05799SMatthew G. Knepley   Not Collective
38097cd05799SMatthew G. Knepley 
38107cd05799SMatthew G. Knepley   Input Parameters:
38117cd05799SMatthew G. Knepley . dm        - The DMPlex object
38127cd05799SMatthew G. Knepley 
38137cd05799SMatthew G. Knepley   Output Parameter:
38147cd05799SMatthew G. Knepley . coneOrientations - The cone orientation for each point
38157cd05799SMatthew G. Knepley 
38167cd05799SMatthew G. Knepley   Level: developer
38177cd05799SMatthew G. Knepley 
38187cd05799SMatthew G. Knepley .seealso: DMPlexGetConeSection()
38197cd05799SMatthew G. Knepley @*/
3820a6dfd86eSKarl Rupp PetscErrorCode DMPlexGetConeOrientations(DM dm, PetscInt *coneOrientations[])
3821a6dfd86eSKarl Rupp {
3822552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
3823552f7358SJed Brown 
3824552f7358SJed Brown   PetscFunctionBegin;
3825552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3826552f7358SJed Brown   if (coneOrientations) *coneOrientations = mesh->coneOrientations;
3827552f7358SJed Brown   PetscFunctionReturn(0);
3828552f7358SJed Brown }
3829552f7358SJed Brown 
3830552f7358SJed Brown /******************************** FEM Support **********************************/
3831552f7358SJed Brown 
38329e8305c2SJed Brown /*
38339e8305c2SJed Brown  Returns number of components and tensor degree for the field.  For interpolated meshes, line should be a point
38349e8305c2SJed Brown  representing a line in the section.
38359e8305c2SJed Brown */
38369e8305c2SJed Brown static PetscErrorCode PetscSectionFieldGetTensorDegree_Private(PetscSection section,PetscInt field,PetscInt line,PetscBool vertexchart,PetscInt *Nc,PetscInt *k)
38379e8305c2SJed Brown {
38389e8305c2SJed Brown   PetscErrorCode ierr;
38399e8305c2SJed Brown 
38409e8305c2SJed Brown   PetscFunctionBeginHot;
38419e8305c2SJed Brown   ierr = PetscSectionGetFieldComponents(section, field, Nc);CHKERRQ(ierr);
3842a433471fSStefano Zampini   if (line < 0) {
3843a433471fSStefano Zampini     *k = 0;
3844a433471fSStefano Zampini     *Nc = 0;
3845a433471fSStefano Zampini   } else if (vertexchart) {            /* If we only have a vertex chart, we must have degree k=1 */
38469e8305c2SJed Brown     *k = 1;
38479e8305c2SJed Brown   } else {                      /* Assume the full interpolated mesh is in the chart; lines in particular */
38489e8305c2SJed Brown     /* An order k SEM disc has k-1 dofs on an edge */
38499e8305c2SJed Brown     ierr = PetscSectionGetFieldDof(section, line, field, k);CHKERRQ(ierr);
38509e8305c2SJed Brown     *k = *k / *Nc + 1;
38519e8305c2SJed Brown   }
38529e8305c2SJed Brown   PetscFunctionReturn(0);
38539e8305c2SJed Brown }
38549e8305c2SJed Brown 
3855a4355906SMatthew Knepley /*@
3856bc1eb3faSJed Brown 
3857bc1eb3faSJed Brown   DMPlexSetClosurePermutationTensor - Create a permutation from the default (BFS) point ordering in the closure, to a
3858bc1eb3faSJed Brown   lexicographic ordering over the tensor product cell (i.e., line, quad, hex, etc.), and set this permutation in the
38591bb6d2a8SBarry Smith   section provided (or the section of the DM).
3860a4355906SMatthew Knepley 
3861a4355906SMatthew Knepley   Input Parameters:
3862a4355906SMatthew Knepley + dm      - The DM
3863a4355906SMatthew Knepley . point   - Either a cell (highest dim point) or an edge (dim 1 point), or PETSC_DETERMINE
3864a4355906SMatthew Knepley - section - The PetscSection to reorder, or NULL for the default section
3865a4355906SMatthew Knepley 
3866a4355906SMatthew Knepley   Note: The point is used to determine the number of dofs/field on an edge. For SEM, this is related to the polynomial
3867a4355906SMatthew Knepley   degree of the basis.
3868a4355906SMatthew Knepley 
3869bc1eb3faSJed Brown   Example:
3870bc1eb3faSJed Brown   A typical interpolated single-quad mesh might order points as
3871bc1eb3faSJed Brown .vb
3872bc1eb3faSJed Brown   [c0, v1, v2, v3, v4, e5, e6, e7, e8]
3873bc1eb3faSJed Brown 
3874bc1eb3faSJed Brown   v4 -- e6 -- v3
3875bc1eb3faSJed Brown   |           |
3876bc1eb3faSJed Brown   e7    c0    e8
3877bc1eb3faSJed Brown   |           |
3878bc1eb3faSJed Brown   v1 -- e5 -- v2
3879bc1eb3faSJed Brown .ve
3880bc1eb3faSJed Brown 
3881bc1eb3faSJed Brown   (There is no significance to the ordering described here.)  The default section for a Q3 quad might typically assign
3882bc1eb3faSJed Brown   dofs in the order of points, e.g.,
3883bc1eb3faSJed Brown .vb
3884bc1eb3faSJed Brown     c0 -> [0,1,2,3]
3885bc1eb3faSJed Brown     v1 -> [4]
3886bc1eb3faSJed Brown     ...
3887bc1eb3faSJed Brown     e5 -> [8, 9]
3888bc1eb3faSJed Brown .ve
3889bc1eb3faSJed Brown 
3890bc1eb3faSJed Brown   which corresponds to the dofs
3891bc1eb3faSJed Brown .vb
3892bc1eb3faSJed Brown     6   10  11  7
3893bc1eb3faSJed Brown     13  2   3   15
3894bc1eb3faSJed Brown     12  0   1   14
3895bc1eb3faSJed Brown     4   8   9   5
3896bc1eb3faSJed Brown .ve
3897bc1eb3faSJed Brown 
3898bc1eb3faSJed Brown   The closure in BFS ordering works through height strata (cells, edges, vertices) to produce the ordering
3899bc1eb3faSJed Brown .vb
3900bc1eb3faSJed Brown   0 1 2 3 8 9 14 15 11 10 13 12 4 5 7 6
3901bc1eb3faSJed Brown .ve
3902bc1eb3faSJed Brown 
3903bc1eb3faSJed Brown   After calling DMPlexSetClosurePermutationTensor(), the closure will be ordered lexicographically,
3904bc1eb3faSJed Brown .vb
3905bc1eb3faSJed Brown    4 8 9 5 12 0 1 14 13 2 3 15 6 10 11 7
3906bc1eb3faSJed Brown .ve
3907bc1eb3faSJed Brown 
3908a4355906SMatthew Knepley   Level: developer
3909a4355906SMatthew Knepley 
39101bb6d2a8SBarry Smith .seealso: DMGetLocalSection(), PetscSectionSetClosurePermutation(), DMSetGlocalSection()
3911a4355906SMatthew Knepley @*/
3912bc1eb3faSJed Brown PetscErrorCode DMPlexSetClosurePermutationTensor(DM dm, PetscInt point, PetscSection section)
39133194fc30SMatthew G. Knepley {
39147391a63aSMatthew G. Knepley   DMLabel        label;
39153194fc30SMatthew G. Knepley   PetscInt      *perm;
3916a433471fSStefano Zampini   PetscInt       dim, depth = -1, eStart = -1, k, Nf, f, Nc, c, i, j, size = 0, offset = 0, foffset = 0;
39179e8305c2SJed Brown   PetscBool      vertexchart;
39183194fc30SMatthew G. Knepley   PetscErrorCode ierr;
39193194fc30SMatthew G. Knepley 
39203194fc30SMatthew G. Knepley   PetscFunctionBegin;
39213194fc30SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3922a433471fSStefano Zampini   if (dim < 1) PetscFunctionReturn(0);
3923a433471fSStefano Zampini   if (point < 0) {
3924a433471fSStefano Zampini     PetscInt sStart,sEnd;
3925a433471fSStefano Zampini 
3926a433471fSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, 1, &sStart, &sEnd);CHKERRQ(ierr);
3927a433471fSStefano Zampini     point = sEnd-sStart ? sStart : point;
3928a433471fSStefano Zampini   }
39297391a63aSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &label);CHKERRQ(ierr);
3930a433471fSStefano Zampini   if (point >= 0) { ierr = DMLabelGetValue(label, point, &depth);CHKERRQ(ierr); }
3931a433471fSStefano Zampini   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
39327391a63aSMatthew G. Knepley   if (depth == 1) {eStart = point;}
39337391a63aSMatthew G. Knepley   else if  (depth == dim) {
39347391a63aSMatthew G. Knepley     const PetscInt *cone;
39357391a63aSMatthew G. Knepley 
39367391a63aSMatthew G. Knepley     ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
3937d4e6627bSStefano Zampini     if (dim == 2) eStart = cone[0];
3938d4e6627bSStefano Zampini     else if (dim == 3) {
3939d4e6627bSStefano Zampini       const PetscInt *cone2;
3940d4e6627bSStefano Zampini       ierr = DMPlexGetCone(dm, cone[0], &cone2);CHKERRQ(ierr);
3941d4e6627bSStefano Zampini       eStart = cone2[0];
3942d4e6627bSStefano 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);
3943a433471fSStefano Zampini   } else if (depth >= 0) 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);
39449e8305c2SJed Brown   {                             /* Determine whether the chart covers all points or just vertices. */
39459e8305c2SJed Brown     PetscInt pStart,pEnd,cStart,cEnd;
39469e8305c2SJed Brown     ierr = DMPlexGetDepthStratum(dm,0,&pStart,&pEnd);CHKERRQ(ierr);
39479e8305c2SJed Brown     ierr = PetscSectionGetChart(section,&cStart,&cEnd);CHKERRQ(ierr);
39489e8305c2SJed Brown     if (pStart == cStart && pEnd == cEnd) vertexchart = PETSC_TRUE; /* Just vertices */
39499e8305c2SJed Brown     else vertexchart = PETSC_FALSE;                                 /* Assume all interpolated points are in chart */
39509e8305c2SJed Brown   }
39513194fc30SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
39523194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
39539e8305c2SJed Brown     ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
395489eabcffSMatthew G. Knepley     size += PetscPowInt(k+1, dim)*Nc;
39553194fc30SMatthew G. Knepley   }
39563194fc30SMatthew G. Knepley   ierr = PetscMalloc1(size, &perm);CHKERRQ(ierr);
39573194fc30SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
395889eabcffSMatthew G. Knepley     switch (dim) {
3959babf31e0SJed Brown     case 1:
39609e8305c2SJed Brown       ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
3961babf31e0SJed Brown       /*
3962babf31e0SJed Brown         Original ordering is [ edge of length k-1; vtx0; vtx1 ]
3963babf31e0SJed Brown         We want              [ vtx0; edge of length k-1; vtx1 ]
3964babf31e0SJed Brown       */
3965babf31e0SJed Brown       for (c=0; c<Nc; c++,offset++) perm[offset] = (k-1)*Nc + c + foffset;
3966babf31e0SJed Brown       for (i=0; i<k-1; i++) for (c=0; c<Nc; c++,offset++) perm[offset] = i*Nc + c + foffset;
3967babf31e0SJed Brown       for (c=0; c<Nc; c++,offset++) perm[offset] = k*Nc + c + foffset;
3968babf31e0SJed Brown       foffset = offset;
3969babf31e0SJed Brown       break;
397089eabcffSMatthew G. Knepley     case 2:
39713194fc30SMatthew 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} */
39729e8305c2SJed Brown       ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
39733194fc30SMatthew G. Knepley       /* The SEM order is
39743194fc30SMatthew G. Knepley 
39753194fc30SMatthew G. Knepley          v_lb, {e_b}, v_rb,
397689eabcffSMatthew G. Knepley          e^{(k-1)-i}_l, {f^{i*(k-1)}}, e^i_r,
39773194fc30SMatthew G. Knepley          v_lt, reverse {e_t}, v_rt
39783194fc30SMatthew G. Knepley       */
39793194fc30SMatthew G. Knepley       {
39803194fc30SMatthew G. Knepley         const PetscInt of   = 0;
39813194fc30SMatthew G. Knepley         const PetscInt oeb  = of   + PetscSqr(k-1);
39823194fc30SMatthew G. Knepley         const PetscInt oer  = oeb  + (k-1);
39833194fc30SMatthew G. Knepley         const PetscInt oet  = oer  + (k-1);
39843194fc30SMatthew G. Knepley         const PetscInt oel  = oet  + (k-1);
39853194fc30SMatthew G. Knepley         const PetscInt ovlb = oel  + (k-1);
39863194fc30SMatthew G. Knepley         const PetscInt ovrb = ovlb + 1;
39873194fc30SMatthew G. Knepley         const PetscInt ovrt = ovrb + 1;
39883194fc30SMatthew G. Knepley         const PetscInt ovlt = ovrt + 1;
39893194fc30SMatthew G. Knepley         PetscInt       o;
39903194fc30SMatthew G. Knepley 
39913194fc30SMatthew G. Knepley         /* bottom */
39923194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlb*Nc + c + foffset;
39933194fc30SMatthew G. Knepley         for (o = oeb; o < oer; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
39943194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrb*Nc + c + foffset;
39953194fc30SMatthew G. Knepley         /* middle */
39963194fc30SMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
39973194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oel+(k-2)-i)*Nc + c + foffset;
39983194fc30SMatthew 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;
39993194fc30SMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oer+i)*Nc + c + foffset;
40003194fc30SMatthew G. Knepley         }
40013194fc30SMatthew G. Knepley         /* top */
40023194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovlt*Nc + c + foffset;
40033194fc30SMatthew G. Knepley         for (o = oel-1; o >= oet; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
40043194fc30SMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovrt*Nc + c + foffset;
40053194fc30SMatthew G. Knepley         foffset = offset;
40063194fc30SMatthew G. Knepley       }
400789eabcffSMatthew G. Knepley       break;
400889eabcffSMatthew G. Knepley     case 3:
400989eabcffSMatthew G. Knepley       /* The original hex closure is
401089eabcffSMatthew G. Knepley 
401189eabcffSMatthew G. Knepley          {c,
401289eabcffSMatthew G. Knepley           f_b, f_t, f_f, f_b, f_r, f_l,
401389eabcffSMatthew 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,
401489eabcffSMatthew G. Knepley           v_blf, v_blb, v_brb, v_brf, v_tlf, v_trf, v_trb, v_tlb}
401589eabcffSMatthew G. Knepley       */
40169e8305c2SJed Brown       ierr = PetscSectionFieldGetTensorDegree_Private(section,f,eStart,vertexchart,&Nc,&k);CHKERRQ(ierr);
401789eabcffSMatthew G. Knepley       /* The SEM order is
401889eabcffSMatthew G. Knepley          Bottom Slice
401989eabcffSMatthew G. Knepley          v_blf, {e^{(k-1)-n}_bf}, v_brf,
402089eabcffSMatthew G. Knepley          e^{i}_bl, f^{n*(k-1)+(k-1)-i}_b, e^{(k-1)-i}_br,
402189eabcffSMatthew G. Knepley          v_blb, {e_bb}, v_brb,
402289eabcffSMatthew G. Knepley 
402389eabcffSMatthew G. Knepley          Middle Slice (j)
402489eabcffSMatthew G. Knepley          {e^{(k-1)-j}_lf}, {f^{j*(k-1)+n}_f}, e^j_rf,
402589eabcffSMatthew G. Knepley          f^{i*(k-1)+j}_l, {c^{(j*(k-1) + i)*(k-1)+n}_t}, f^{j*(k-1)+i}_r,
402689eabcffSMatthew G. Knepley          e^j_lb, {f^{j*(k-1)+(k-1)-n}_b}, e^{(k-1)-j}_rb,
402789eabcffSMatthew G. Knepley 
402889eabcffSMatthew G. Knepley          Top Slice
402989eabcffSMatthew G. Knepley          v_tlf, {e_tf}, v_trf,
403089eabcffSMatthew G. Knepley          e^{(k-1)-i}_tl, {f^{i*(k-1)}_t}, e^{i}_tr,
403189eabcffSMatthew G. Knepley          v_tlb, {e^{(k-1)-n}_tb}, v_trb,
403289eabcffSMatthew G. Knepley       */
403389eabcffSMatthew G. Knepley       {
403489eabcffSMatthew G. Knepley         const PetscInt oc    = 0;
403589eabcffSMatthew G. Knepley         const PetscInt ofb   = oc    + PetscSqr(k-1)*(k-1);
403689eabcffSMatthew G. Knepley         const PetscInt oft   = ofb   + PetscSqr(k-1);
403789eabcffSMatthew G. Knepley         const PetscInt off   = oft   + PetscSqr(k-1);
403889eabcffSMatthew G. Knepley         const PetscInt ofk   = off   + PetscSqr(k-1);
403989eabcffSMatthew G. Knepley         const PetscInt ofr   = ofk   + PetscSqr(k-1);
404089eabcffSMatthew G. Knepley         const PetscInt ofl   = ofr   + PetscSqr(k-1);
404189eabcffSMatthew G. Knepley         const PetscInt oebl  = ofl   + PetscSqr(k-1);
404289eabcffSMatthew G. Knepley         const PetscInt oebb  = oebl  + (k-1);
404389eabcffSMatthew G. Knepley         const PetscInt oebr  = oebb  + (k-1);
404489eabcffSMatthew G. Knepley         const PetscInt oebf  = oebr  + (k-1);
404589eabcffSMatthew G. Knepley         const PetscInt oetf  = oebf  + (k-1);
404689eabcffSMatthew G. Knepley         const PetscInt oetr  = oetf  + (k-1);
404789eabcffSMatthew G. Knepley         const PetscInt oetb  = oetr  + (k-1);
404889eabcffSMatthew G. Knepley         const PetscInt oetl  = oetb  + (k-1);
404989eabcffSMatthew G. Knepley         const PetscInt oerf  = oetl  + (k-1);
405089eabcffSMatthew G. Knepley         const PetscInt oelf  = oerf  + (k-1);
405189eabcffSMatthew G. Knepley         const PetscInt oelb  = oelf  + (k-1);
405289eabcffSMatthew G. Knepley         const PetscInt oerb  = oelb  + (k-1);
405389eabcffSMatthew G. Knepley         const PetscInt ovblf = oerb  + (k-1);
405489eabcffSMatthew G. Knepley         const PetscInt ovblb = ovblf + 1;
405589eabcffSMatthew G. Knepley         const PetscInt ovbrb = ovblb + 1;
405689eabcffSMatthew G. Knepley         const PetscInt ovbrf = ovbrb + 1;
405789eabcffSMatthew G. Knepley         const PetscInt ovtlf = ovbrf + 1;
405889eabcffSMatthew G. Knepley         const PetscInt ovtrf = ovtlf + 1;
405989eabcffSMatthew G. Knepley         const PetscInt ovtrb = ovtrf + 1;
406089eabcffSMatthew G. Knepley         const PetscInt ovtlb = ovtrb + 1;
406189eabcffSMatthew G. Knepley         PetscInt       o, n;
406289eabcffSMatthew G. Knepley 
406389eabcffSMatthew G. Knepley         /* Bottom Slice */
406489eabcffSMatthew G. Knepley         /*   bottom */
406589eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblf*Nc + c + foffset;
406689eabcffSMatthew G. Knepley         for (o = oetf-1; o >= oebf; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
406789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrf*Nc + c + foffset;
406889eabcffSMatthew G. Knepley         /*   middle */
406989eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
407089eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebl+i)*Nc + c + foffset;
4071316b7f87SMax 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;}
407289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oebr+(k-2)-i)*Nc + c + foffset;
40733194fc30SMatthew G. Knepley         }
407489eabcffSMatthew G. Knepley         /*   top */
407589eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovblb*Nc + c + foffset;
407689eabcffSMatthew G. Knepley         for (o = oebb; o < oebr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
407789eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovbrb*Nc + c + foffset;
407889eabcffSMatthew G. Knepley 
407989eabcffSMatthew G. Knepley         /* Middle Slice */
408089eabcffSMatthew G. Knepley         for (j = 0; j < k-1; ++j) {
408189eabcffSMatthew G. Knepley           /*   bottom */
408289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelf+(k-2)-j)*Nc + c + foffset;
408389eabcffSMatthew 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;
408489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerf+j)*Nc + c + foffset;
408589eabcffSMatthew G. Knepley           /*   middle */
408689eabcffSMatthew G. Knepley           for (i = 0; i < k-1; ++i) {
408789eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofl+i*(k-1)+j)*Nc + c + foffset;
408889eabcffSMatthew 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;
408989eabcffSMatthew G. Knepley             for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (ofr+j*(k-1)+i)*Nc + c + foffset;
409089eabcffSMatthew G. Knepley           }
409189eabcffSMatthew G. Knepley           /*   top */
409289eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oelb+j)*Nc + c + foffset;
409389eabcffSMatthew 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;
409489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oerb+(k-2)-j)*Nc + c + foffset;
409589eabcffSMatthew G. Knepley         }
409689eabcffSMatthew G. Knepley 
409789eabcffSMatthew G. Knepley         /* Top Slice */
409889eabcffSMatthew G. Knepley         /*   bottom */
409989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlf*Nc + c + foffset;
410089eabcffSMatthew G. Knepley         for (o = oetf; o < oetr; ++o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
410189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrf*Nc + c + foffset;
410289eabcffSMatthew G. Knepley         /*   middle */
410389eabcffSMatthew G. Knepley         for (i = 0; i < k-1; ++i) {
410489eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetl+(k-2)-i)*Nc + c + foffset;
410589eabcffSMatthew 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;
410689eabcffSMatthew G. Knepley           for (c = 0; c < Nc; ++c, ++offset) perm[offset] = (oetr+i)*Nc + c + foffset;
410789eabcffSMatthew G. Knepley         }
410889eabcffSMatthew G. Knepley         /*   top */
410989eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtlb*Nc + c + foffset;
411089eabcffSMatthew G. Knepley         for (o = oetl-1; o >= oetb; --o) for (c = 0; c < Nc; ++c, ++offset) perm[offset] = o*Nc + c + foffset;
411189eabcffSMatthew G. Knepley         for (c = 0; c < Nc; ++c, ++offset) perm[offset] = ovtrb*Nc + c + foffset;
411289eabcffSMatthew G. Knepley 
411389eabcffSMatthew G. Knepley         foffset = offset;
411489eabcffSMatthew G. Knepley       }
411589eabcffSMatthew G. Knepley       break;
411689eabcffSMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "No spectral ordering for dimension %D", dim);
411789eabcffSMatthew G. Knepley     }
411889eabcffSMatthew G. Knepley   }
411989eabcffSMatthew G. Knepley   if (offset != size) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Number of permutation entries %D != %D", offset, size);
41203194fc30SMatthew G. Knepley   /* Check permutation */
41213194fc30SMatthew G. Knepley   {
41223194fc30SMatthew G. Knepley     PetscInt *check;
41233194fc30SMatthew G. Knepley 
41243194fc30SMatthew G. Knepley     ierr = PetscMalloc1(size, &check);CHKERRQ(ierr);
41253194fc30SMatthew 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]);}
41263194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) check[perm[i]] = i;
41273194fc30SMatthew G. Knepley     for (i = 0; i < size; ++i) {if (check[i] < 0) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Missing permutation index %D", i);}
41283194fc30SMatthew G. Knepley     ierr = PetscFree(check);CHKERRQ(ierr);
41293194fc30SMatthew G. Knepley   }
41301fdd32a2SMatthew G. Knepley   ierr = PetscSectionSetClosurePermutation_Internal(section, (PetscObject) dm, size, PETSC_OWN_POINTER, perm);CHKERRQ(ierr);
41313194fc30SMatthew G. Knepley   PetscFunctionReturn(0);
41323194fc30SMatthew G. Knepley }
41333194fc30SMatthew G. Knepley 
4134e071409bSToby Isaac PetscErrorCode DMPlexGetPointDualSpaceFEM(DM dm, PetscInt point, PetscInt field, PetscDualSpace *dspace)
4135e071409bSToby Isaac {
4136e071409bSToby Isaac   PetscDS        prob;
4137e071409bSToby Isaac   PetscInt       depth, Nf, h;
4138e071409bSToby Isaac   DMLabel        label;
4139e071409bSToby Isaac   PetscErrorCode ierr;
4140e071409bSToby Isaac 
4141e071409bSToby Isaac   PetscFunctionBeginHot;
4142e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &prob);CHKERRQ(ierr);
4143e071409bSToby Isaac   Nf      = prob->Nf;
4144e071409bSToby Isaac   label   = dm->depthLabel;
4145e071409bSToby Isaac   *dspace = NULL;
4146e071409bSToby Isaac   if (field < Nf) {
4147e071409bSToby Isaac     PetscObject disc = prob->disc[field];
4148e071409bSToby Isaac 
4149e071409bSToby Isaac     if (disc->classid == PETSCFE_CLASSID) {
4150e071409bSToby Isaac       PetscDualSpace dsp;
4151e071409bSToby Isaac 
4152e071409bSToby Isaac       ierr = PetscFEGetDualSpace((PetscFE)disc,&dsp);CHKERRQ(ierr);
4153e071409bSToby Isaac       ierr = DMLabelGetNumValues(label,&depth);CHKERRQ(ierr);
4154e071409bSToby Isaac       ierr = DMLabelGetValue(label,point,&h);CHKERRQ(ierr);
4155e071409bSToby Isaac       h    = depth - 1 - h;
4156e071409bSToby Isaac       if (h) {
4157e071409bSToby Isaac         ierr = PetscDualSpaceGetHeightSubspace(dsp,h,dspace);CHKERRQ(ierr);
4158e071409bSToby Isaac       } else {
4159e071409bSToby Isaac         *dspace = dsp;
4160e071409bSToby Isaac       }
4161e071409bSToby Isaac     }
4162e071409bSToby Isaac   }
4163e071409bSToby Isaac   PetscFunctionReturn(0);
4164e071409bSToby Isaac }
4165e071409bSToby Isaac 
4166e071409bSToby Isaac 
41671a271a75SMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecGetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4168a6dfd86eSKarl Rupp {
4169552f7358SJed Brown   PetscScalar    *array, *vArray;
4170d9917b9dSMatthew G. Knepley   const PetscInt *cone, *coneO;
41711a271a75SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, size = 0, offset = 0;
4172552f7358SJed Brown   PetscErrorCode  ierr;
4173552f7358SJed Brown 
41741b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
41752a3aaacfSMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
41765a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
41775a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
41785a1bb5cfSMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
41793f7cbbe7SMatthew G. Knepley   if (!values || !*values) {
41809df71ca4SMatthew G. Knepley     if ((point >= pStart) && (point < pEnd)) {
41819df71ca4SMatthew G. Knepley       PetscInt dof;
4182d9917b9dSMatthew G. Knepley 
41839df71ca4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
41849df71ca4SMatthew G. Knepley       size += dof;
41859df71ca4SMatthew G. Knepley     }
41869df71ca4SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
41879df71ca4SMatthew G. Knepley       const PetscInt cp = cone[p];
41882a3aaacfSMatthew G. Knepley       PetscInt       dof;
41895a1bb5cfSMatthew G. Knepley 
41905a1bb5cfSMatthew G. Knepley       if ((cp < pStart) || (cp >= pEnd)) continue;
41912a3aaacfSMatthew G. Knepley       ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
41925a1bb5cfSMatthew G. Knepley       size += dof;
41935a1bb5cfSMatthew G. Knepley     }
41943f7cbbe7SMatthew G. Knepley     if (!values) {
41953f7cbbe7SMatthew G. Knepley       if (csize) *csize = size;
41963f7cbbe7SMatthew G. Knepley       PetscFunctionReturn(0);
41973f7cbbe7SMatthew G. Knepley     }
419869291d52SBarry Smith     ierr = DMGetWorkArray(dm, size, MPIU_SCALAR, &array);CHKERRQ(ierr);
4199982e9ed1SMatthew G. Knepley   } else {
4200982e9ed1SMatthew G. Knepley     array = *values;
4201982e9ed1SMatthew G. Knepley   }
42029df71ca4SMatthew G. Knepley   size = 0;
42035a1bb5cfSMatthew G. Knepley   ierr = VecGetArray(v, &vArray);CHKERRQ(ierr);
42049df71ca4SMatthew G. Knepley   if ((point >= pStart) && (point < pEnd)) {
42059df71ca4SMatthew G. Knepley     PetscInt     dof, off, d;
42069df71ca4SMatthew G. Knepley     PetscScalar *varr;
4207d9917b9dSMatthew G. Knepley 
42089df71ca4SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
42099df71ca4SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
42109df71ca4SMatthew G. Knepley     varr = &vArray[off];
42111a271a75SMatthew G. Knepley     for (d = 0; d < dof; ++d, ++offset) {
42121a271a75SMatthew G. Knepley       array[offset] = varr[d];
42139df71ca4SMatthew G. Knepley     }
42149df71ca4SMatthew G. Knepley     size += dof;
42159df71ca4SMatthew G. Knepley   }
42169df71ca4SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
42179df71ca4SMatthew G. Knepley     const PetscInt cp = cone[p];
42189df71ca4SMatthew G. Knepley     PetscInt       o  = coneO[p];
42195a1bb5cfSMatthew G. Knepley     PetscInt       dof, off, d;
42205a1bb5cfSMatthew G. Knepley     PetscScalar   *varr;
42215a1bb5cfSMatthew G. Knepley 
422252ed52e8SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) continue;
42235a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
42245a1bb5cfSMatthew G. Knepley     ierr = PetscSectionGetOffset(section, cp, &off);CHKERRQ(ierr);
42255a1bb5cfSMatthew G. Knepley     varr = &vArray[off];
42265a1bb5cfSMatthew G. Knepley     if (o >= 0) {
42271a271a75SMatthew G. Knepley       for (d = 0; d < dof; ++d, ++offset) {
42281a271a75SMatthew G. Knepley         array[offset] = varr[d];
42295a1bb5cfSMatthew G. Knepley       }
42305a1bb5cfSMatthew G. Knepley     } else {
42311a271a75SMatthew G. Knepley       for (d = dof-1; d >= 0; --d, ++offset) {
42321a271a75SMatthew G. Knepley         array[offset] = varr[d];
42335a1bb5cfSMatthew G. Knepley       }
42345a1bb5cfSMatthew G. Knepley     }
42359df71ca4SMatthew G. Knepley     size += dof;
42365a1bb5cfSMatthew G. Knepley   }
42375a1bb5cfSMatthew G. Knepley   ierr = VecRestoreArray(v, &vArray);CHKERRQ(ierr);
42389df71ca4SMatthew G. Knepley   if (!*values) {
42395a1bb5cfSMatthew G. Knepley     if (csize) *csize = size;
42405a1bb5cfSMatthew G. Knepley     *values = array;
42419df71ca4SMatthew G. Knepley   } else {
42428ccfff9cSToby Isaac     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
42438c312ff3SMatthew G. Knepley     *csize = size;
42449df71ca4SMatthew G. Knepley   }
42455a1bb5cfSMatthew G. Knepley   PetscFunctionReturn(0);
42465a1bb5cfSMatthew G. Knepley }
4247d9917b9dSMatthew G. Knepley 
42481dc59d61SMatthew G. Knepley PetscErrorCode DMPlexGetCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4249923c78e0SToby Isaac {
4250923c78e0SToby Isaac   const PetscInt *cla;
4251923c78e0SToby Isaac   PetscInt       np, *pts = NULL;
4252923c78e0SToby Isaac   PetscErrorCode ierr;
4253923c78e0SToby Isaac 
4254923c78e0SToby Isaac   PetscFunctionBeginHot;
4255923c78e0SToby Isaac   ierr = PetscSectionGetClosureIndex(section, (PetscObject) dm, clSec, clPoints);CHKERRQ(ierr);
4256923c78e0SToby Isaac   if (!*clPoints) {
4257923c78e0SToby Isaac     PetscInt pStart, pEnd, p, q;
4258923c78e0SToby Isaac 
4259923c78e0SToby Isaac     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4260923c78e0SToby Isaac     ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &np, &pts);CHKERRQ(ierr);
4261923c78e0SToby Isaac     /* Compress out points not in the section */
4262923c78e0SToby Isaac     for (p = 0, q = 0; p < np; p++) {
4263923c78e0SToby Isaac       PetscInt r = pts[2*p];
4264923c78e0SToby Isaac       if ((r >= pStart) && (r < pEnd)) {
4265923c78e0SToby Isaac         pts[q*2]   = r;
4266923c78e0SToby Isaac         pts[q*2+1] = pts[2*p+1];
4267923c78e0SToby Isaac         ++q;
4268923c78e0SToby Isaac       }
4269923c78e0SToby Isaac     }
4270923c78e0SToby Isaac     np = q;
4271923c78e0SToby Isaac     cla = NULL;
4272923c78e0SToby Isaac   } else {
4273923c78e0SToby Isaac     PetscInt dof, off;
4274923c78e0SToby Isaac 
4275923c78e0SToby Isaac     ierr = PetscSectionGetDof(*clSec, point, &dof);CHKERRQ(ierr);
4276923c78e0SToby Isaac     ierr = PetscSectionGetOffset(*clSec, point, &off);CHKERRQ(ierr);
4277923c78e0SToby Isaac     ierr = ISGetIndices(*clPoints, &cla);CHKERRQ(ierr);
4278923c78e0SToby Isaac     np   = dof/2;
4279923c78e0SToby Isaac     pts  = (PetscInt *) &cla[off];
4280923c78e0SToby Isaac   }
4281923c78e0SToby Isaac   *numPoints = np;
4282923c78e0SToby Isaac   *points    = pts;
4283923c78e0SToby Isaac   *clp       = cla;
4284923c78e0SToby Isaac 
4285923c78e0SToby Isaac   PetscFunctionReturn(0);
4286923c78e0SToby Isaac }
4287923c78e0SToby Isaac 
42881dc59d61SMatthew G. Knepley PetscErrorCode DMPlexRestoreCompressedClosure(DM dm, PetscSection section, PetscInt point, PetscInt *numPoints, PetscInt **points, PetscSection *clSec, IS *clPoints, const PetscInt **clp)
4289923c78e0SToby Isaac {
4290923c78e0SToby Isaac   PetscErrorCode ierr;
4291923c78e0SToby Isaac 
4292923c78e0SToby Isaac   PetscFunctionBeginHot;
4293923c78e0SToby Isaac   if (!*clPoints) {
4294923c78e0SToby Isaac     ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, numPoints, points);CHKERRQ(ierr);
4295923c78e0SToby Isaac   } else {
4296923c78e0SToby Isaac     ierr = ISRestoreIndices(*clPoints, clp);CHKERRQ(ierr);
4297923c78e0SToby Isaac   }
4298923c78e0SToby Isaac   *numPoints = 0;
4299923c78e0SToby Isaac   *points    = NULL;
4300923c78e0SToby Isaac   *clSec     = NULL;
4301923c78e0SToby Isaac   *clPoints  = NULL;
4302923c78e0SToby Isaac   *clp       = NULL;
4303923c78e0SToby Isaac   PetscFunctionReturn(0);
4304923c78e0SToby Isaac }
4305923c78e0SToby Isaac 
430697e99dd9SToby 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[])
43071a271a75SMatthew G. Knepley {
43081a271a75SMatthew G. Knepley   PetscInt          offset = 0, p;
430997e99dd9SToby Isaac   const PetscInt    **perms = NULL;
431097e99dd9SToby Isaac   const PetscScalar **flips = NULL;
43111a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
43121a271a75SMatthew G. Knepley 
43131a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4314fe02ba77SJed Brown   *size = 0;
431597e99dd9SToby Isaac   ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
431697e99dd9SToby Isaac   for (p = 0; p < numPoints; p++) {
431797e99dd9SToby Isaac     const PetscInt    point = points[2*p];
431897e99dd9SToby Isaac     const PetscInt    *perm = perms ? perms[p] : NULL;
431997e99dd9SToby Isaac     const PetscScalar *flip = flips ? flips[p] : NULL;
43201a271a75SMatthew G. Knepley     PetscInt          dof, off, d;
43211a271a75SMatthew G. Knepley     const PetscScalar *varr;
43221a271a75SMatthew G. Knepley 
43231a271a75SMatthew G. Knepley     ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
43241a271a75SMatthew G. Knepley     ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
43251a271a75SMatthew G. Knepley     varr = &vArray[off];
432697e99dd9SToby Isaac     if (clperm) {
432797e99dd9SToby Isaac       if (perm) {
432897e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset + perm[d]]]  = varr[d];
43291a271a75SMatthew G. Knepley       } else {
433097e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]]  = varr[d];
433197e99dd9SToby Isaac       }
433297e99dd9SToby Isaac       if (flip) {
433397e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[clperm[offset +      d ]] *= flip[d];
433497e99dd9SToby Isaac       }
433597e99dd9SToby Isaac     } else {
433697e99dd9SToby Isaac       if (perm) {
433797e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset + perm[d]]  = varr[d];
433897e99dd9SToby Isaac       } else {
433997e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ]  = varr[d];
434097e99dd9SToby Isaac       }
434197e99dd9SToby Isaac       if (flip) {
434297e99dd9SToby Isaac         for (d = 0; d < dof; d++) array[offset +      d ] *= flip[d];
43431a271a75SMatthew G. Knepley       }
43441a271a75SMatthew G. Knepley     }
434597e99dd9SToby Isaac     offset += dof;
434697e99dd9SToby Isaac   }
434797e99dd9SToby Isaac   ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
43481a271a75SMatthew G. Knepley   *size = offset;
43491a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
43501a271a75SMatthew G. Knepley }
43511a271a75SMatthew G. Knepley 
435297e99dd9SToby 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[])
43531a271a75SMatthew G. Knepley {
43541a271a75SMatthew G. Knepley   PetscInt          offset = 0, f;
43551a271a75SMatthew G. Knepley   PetscErrorCode    ierr;
43561a271a75SMatthew G. Knepley 
43571a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
4358fe02ba77SJed Brown   *size = 0;
43591a271a75SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
436097e99dd9SToby Isaac     PetscInt          p;
436197e99dd9SToby Isaac     const PetscInt    **perms = NULL;
436297e99dd9SToby Isaac     const PetscScalar **flips = NULL;
43631a271a75SMatthew G. Knepley 
436497e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
436597e99dd9SToby Isaac     for (p = 0; p < numPoints; p++) {
436697e99dd9SToby Isaac       const PetscInt    point = points[2*p];
436797e99dd9SToby Isaac       PetscInt          fdof, foff, b;
43681a271a75SMatthew G. Knepley       const PetscScalar *varr;
436997e99dd9SToby Isaac       const PetscInt    *perm = perms ? perms[p] : NULL;
437097e99dd9SToby Isaac       const PetscScalar *flip = flips ? flips[p] : NULL;
43711a271a75SMatthew G. Knepley 
43721a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
43731a271a75SMatthew G. Knepley       ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
43741a271a75SMatthew G. Knepley       varr = &vArray[foff];
437597e99dd9SToby Isaac       if (clperm) {
437697e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[clperm[offset + perm[b]]]  = varr[b];}}
437797e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]]  = varr[b];}}
437897e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[clperm[offset +      b ]] *= flip[b];}}
43791a271a75SMatthew G. Knepley       } else {
438097e99dd9SToby Isaac         if (perm) {for (b = 0; b < fdof; b++) {array[offset + perm[b]]  = varr[b];}}
438197e99dd9SToby Isaac         else      {for (b = 0; b < fdof; b++) {array[offset +      b ]  = varr[b];}}
438297e99dd9SToby Isaac         if (flip) {for (b = 0; b < fdof; b++) {array[offset +      b ] *= flip[b];}}
43831a271a75SMatthew G. Knepley       }
438497e99dd9SToby Isaac       offset += fdof;
43851a271a75SMatthew G. Knepley     }
438697e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
43871a271a75SMatthew G. Knepley   }
43881a271a75SMatthew G. Knepley   *size = offset;
43891a271a75SMatthew G. Knepley   PetscFunctionReturn(0);
43901a271a75SMatthew G. Knepley }
43911a271a75SMatthew G. Knepley 
4392552f7358SJed Brown /*@C
4393552f7358SJed Brown   DMPlexVecGetClosure - Get an array of the values on the closure of 'point'
4394552f7358SJed Brown 
4395552f7358SJed Brown   Not collective
4396552f7358SJed Brown 
4397552f7358SJed Brown   Input Parameters:
4398552f7358SJed Brown + dm - The DM
4399552f7358SJed Brown . section - The section describing the layout in v, or NULL to use the default section
4400552f7358SJed Brown . v - The local vector
440122c1ee49SMatthew G. Knepley . point - The point in the DM
440222c1ee49SMatthew G. Knepley . csize - The size of the input values array, or NULL
440322c1ee49SMatthew G. Knepley - values - An array to use for the values, or NULL to have it allocated automatically
4404552f7358SJed Brown 
4405552f7358SJed Brown   Output Parameters:
440622c1ee49SMatthew G. Knepley + csize - The number of values in the closure
440722c1ee49SMatthew G. Knepley - values - The array of values. If the user provided NULL, it is a borrowed array and should not be freed
440822c1ee49SMatthew G. Knepley 
440922c1ee49SMatthew G. Knepley $ Note that DMPlexVecGetClosure/DMPlexVecRestoreClosure only allocates the values array if it set to NULL in the
441022c1ee49SMatthew G. Knepley $ calling function. This is because DMPlexVecGetClosure() is typically called in the inner loop of a Vec or Mat
441122c1ee49SMatthew G. Knepley $ assembly function, and a user may already have allocated storage for this operation.
441222c1ee49SMatthew G. Knepley $
441322c1ee49SMatthew G. Knepley $ A typical use could be
441422c1ee49SMatthew G. Knepley $
441522c1ee49SMatthew G. Knepley $  values = NULL;
441622c1ee49SMatthew G. Knepley $  ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
441722c1ee49SMatthew G. Knepley $  for (cl = 0; cl < clSize; ++cl) {
441822c1ee49SMatthew G. Knepley $    <Compute on closure>
441922c1ee49SMatthew G. Knepley $  }
442022c1ee49SMatthew G. Knepley $  ierr = DMPlexVecRestoreClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
442122c1ee49SMatthew G. Knepley $
442222c1ee49SMatthew G. Knepley $ or
442322c1ee49SMatthew G. Knepley $
442422c1ee49SMatthew G. Knepley $  PetscMalloc1(clMaxSize, &values);
442522c1ee49SMatthew G. Knepley $  for (p = pStart; p < pEnd; ++p) {
442622c1ee49SMatthew G. Knepley $    clSize = clMaxSize;
442722c1ee49SMatthew G. Knepley $    ierr = DMPlexVecGetClosure(dm, NULL, v, p, &clSize, &values);CHKERRQ(ierr);
442822c1ee49SMatthew G. Knepley $    for (cl = 0; cl < clSize; ++cl) {
442922c1ee49SMatthew G. Knepley $      <Compute on closure>
443022c1ee49SMatthew G. Knepley $    }
443122c1ee49SMatthew G. Knepley $  }
443222c1ee49SMatthew G. Knepley $  PetscFree(values);
4433552f7358SJed Brown 
4434552f7358SJed Brown   Fortran Notes:
4435552f7358SJed Brown   Since it returns an array, this routine is only available in Fortran 90, and you must
4436552f7358SJed Brown   include petsc.h90 in your code.
4437552f7358SJed Brown 
4438552f7358SJed Brown   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
4439552f7358SJed Brown 
4440552f7358SJed Brown   Level: intermediate
4441552f7358SJed Brown 
4442552f7358SJed Brown .seealso DMPlexVecRestoreClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4443552f7358SJed Brown @*/
4444552f7358SJed Brown PetscErrorCode DMPlexVecGetClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4445552f7358SJed Brown {
4446552f7358SJed Brown   PetscSection       clSection;
4447d9917b9dSMatthew G. Knepley   IS                 clPoints;
44488a84db2dSMatthew G. Knepley   PetscScalar       *array;
44498a84db2dSMatthew G. Knepley   const PetscScalar *vArray;
4450552f7358SJed Brown   PetscInt          *points = NULL;
44518f3be42fSMatthew G. Knepley   const PetscInt    *clp, *perm;
44521a271a75SMatthew G. Knepley   PetscInt           depth, numFields, numPoints, size;
4453552f7358SJed Brown   PetscErrorCode     ierr;
4454552f7358SJed Brown 
4455d9917b9dSMatthew G. Knepley   PetscFunctionBeginHot;
4456552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
445792fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
44581a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
44591a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
4460552f7358SJed Brown   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
4461552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
4462552f7358SJed Brown   if (depth == 1 && numFields < 2) {
44631a271a75SMatthew G. Knepley     ierr = DMPlexVecGetClosure_Depth1_Static(dm, section, v, point, csize, values);CHKERRQ(ierr);
4464552f7358SJed Brown     PetscFunctionReturn(0);
4465552f7358SJed Brown   }
44661a271a75SMatthew G. Knepley   /* Get points */
4467923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
44681fdd32a2SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &perm);CHKERRQ(ierr);
44691a271a75SMatthew G. Knepley   /* Get array */
4470bd6c0d32SMatthew G. Knepley   if (!values || !*values) {
44711a271a75SMatthew G. Knepley     PetscInt asize = 0, dof, p;
4472552f7358SJed Brown 
44731a271a75SMatthew G. Knepley     for (p = 0; p < numPoints*2; p += 2) {
4474552f7358SJed Brown       ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
44751a271a75SMatthew G. Knepley       asize += dof;
4476552f7358SJed Brown     }
4477bd6c0d32SMatthew G. Knepley     if (!values) {
4478923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
44791a271a75SMatthew G. Knepley       if (csize) *csize = asize;
4480bd6c0d32SMatthew G. Knepley       PetscFunctionReturn(0);
4481bd6c0d32SMatthew G. Knepley     }
448269291d52SBarry Smith     ierr = DMGetWorkArray(dm, asize, MPIU_SCALAR, &array);CHKERRQ(ierr);
4483d0f6b257SMatthew G. Knepley   } else {
4484d0f6b257SMatthew G. Knepley     array = *values;
4485d0f6b257SMatthew G. Knepley   }
44868a84db2dSMatthew G. Knepley   ierr = VecGetArrayRead(v, &vArray);CHKERRQ(ierr);
44871a271a75SMatthew G. Knepley   /* Get values */
448897e99dd9SToby Isaac   if (numFields > 0) {ierr = DMPlexVecGetClosure_Fields_Static(dm, section, numPoints, points, numFields, perm, vArray, &size, array);CHKERRQ(ierr);}
448997e99dd9SToby Isaac   else               {ierr = DMPlexVecGetClosure_Static(dm, section, numPoints, points, perm, vArray, &size, array);CHKERRQ(ierr);}
44901a271a75SMatthew G. Knepley   /* Cleanup points */
4491923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
44921a271a75SMatthew G. Knepley   /* Cleanup array */
44938a84db2dSMatthew G. Knepley   ierr = VecRestoreArrayRead(v, &vArray);CHKERRQ(ierr);
4494d0f6b257SMatthew G. Knepley   if (!*values) {
4495552f7358SJed Brown     if (csize) *csize = size;
4496552f7358SJed Brown     *values = array;
4497d0f6b257SMatthew G. Knepley   } else {
4498f347f43bSBarry Smith     if (size > *csize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Size of input array %D < actual size %D", *csize, size);
4499d0f6b257SMatthew G. Knepley     *csize = size;
4500d0f6b257SMatthew G. Knepley   }
4501552f7358SJed Brown   PetscFunctionReturn(0);
4502552f7358SJed Brown }
4503552f7358SJed Brown 
4504552f7358SJed Brown /*@C
4505552f7358SJed Brown   DMPlexVecRestoreClosure - Restore the array of the values on the closure of 'point'
4506552f7358SJed Brown 
4507552f7358SJed Brown   Not collective
4508552f7358SJed Brown 
4509552f7358SJed Brown   Input Parameters:
4510552f7358SJed Brown + dm - The DM
45110298fd71SBarry Smith . section - The section describing the layout in v, or NULL to use the default section
4512552f7358SJed Brown . v - The local vector
4513eaf898f9SPatrick Sanan . point - The point in the DM
45140298fd71SBarry Smith . csize - The number of values in the closure, or NULL
4515552f7358SJed Brown - values - The array of values, which is a borrowed array and should not be freed
4516552f7358SJed Brown 
451722c1ee49SMatthew 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()
451822c1ee49SMatthew G. Knepley 
45193813dfbdSMatthew G Knepley   Fortran Notes:
45203813dfbdSMatthew G Knepley   Since it returns an array, this routine is only available in Fortran 90, and you must
45213813dfbdSMatthew G Knepley   include petsc.h90 in your code.
45223813dfbdSMatthew G Knepley 
45233813dfbdSMatthew G Knepley   The csize argument is not present in the Fortran 90 binding since it is internal to the array.
45243813dfbdSMatthew G Knepley 
4525552f7358SJed Brown   Level: intermediate
4526552f7358SJed Brown 
4527552f7358SJed Brown .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure(), DMPlexMatSetClosure()
4528552f7358SJed Brown @*/
45297c1f9639SMatthew G Knepley PetscErrorCode DMPlexVecRestoreClosure(DM dm, PetscSection section, Vec v, PetscInt point, PetscInt *csize, PetscScalar *values[])
4530a6dfd86eSKarl Rupp {
4531552f7358SJed Brown   PetscInt       size = 0;
4532552f7358SJed Brown   PetscErrorCode ierr;
4533552f7358SJed Brown 
4534552f7358SJed Brown   PetscFunctionBegin;
4535552f7358SJed Brown   /* Should work without recalculating size */
453669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, size, MPIU_SCALAR, (void*) values);CHKERRQ(ierr);
4537c9fdaa05SMatthew G. Knepley   *values = NULL;
4538552f7358SJed Brown   PetscFunctionReturn(0);
4539552f7358SJed Brown }
4540552f7358SJed Brown 
4541552f7358SJed Brown PETSC_STATIC_INLINE void add   (PetscScalar *x, PetscScalar y) {*x += y;}
4542552f7358SJed Brown PETSC_STATIC_INLINE void insert(PetscScalar *x, PetscScalar y) {*x  = y;}
4543552f7358SJed Brown 
454497e99dd9SToby 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[])
4545552f7358SJed Brown {
4546552f7358SJed Brown   PetscInt        cdof;   /* The number of constraints on this point */
4547552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4548552f7358SJed Brown   PetscScalar    *a;
4549552f7358SJed Brown   PetscInt        off, cind = 0, k;
4550552f7358SJed Brown   PetscErrorCode  ierr;
4551552f7358SJed Brown 
4552552f7358SJed Brown   PetscFunctionBegin;
4553552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4554552f7358SJed Brown   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4555552f7358SJed Brown   a    = &array[off];
4556552f7358SJed Brown   if (!cdof || setBC) {
455797e99dd9SToby Isaac     if (clperm) {
455897e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));}}
455997e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));}}
4560552f7358SJed Brown     } else {
456197e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));}}
456297e99dd9SToby Isaac       else      {for (k = 0; k < dof; ++k) {fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));}}
4563552f7358SJed Brown     }
4564552f7358SJed Brown   } else {
4565552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
456697e99dd9SToby Isaac     if (clperm) {
456797e99dd9SToby Isaac       if (perm) {for (k = 0; k < dof; ++k) {
4568552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
456997e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
4570552f7358SJed Brown         }
4571552f7358SJed Brown       } else {
4572552f7358SJed Brown         for (k = 0; k < dof; ++k) {
4573552f7358SJed Brown           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
457497e99dd9SToby Isaac           fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
457597e99dd9SToby Isaac         }
457697e99dd9SToby Isaac       }
457797e99dd9SToby Isaac     } else {
457897e99dd9SToby Isaac       if (perm) {
457997e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
458097e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
458197e99dd9SToby Isaac           fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
458297e99dd9SToby Isaac         }
458397e99dd9SToby Isaac       } else {
458497e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
458597e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
458697e99dd9SToby Isaac           fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
458797e99dd9SToby Isaac         }
4588552f7358SJed Brown       }
4589552f7358SJed Brown     }
4590552f7358SJed Brown   }
4591552f7358SJed Brown   PetscFunctionReturn(0);
4592552f7358SJed Brown }
4593552f7358SJed Brown 
459497e99dd9SToby 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[])
4595a5e93ea8SMatthew G. Knepley {
4596a5e93ea8SMatthew G. Knepley   PetscInt        cdof;   /* The number of constraints on this point */
4597a5e93ea8SMatthew G. Knepley   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4598a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
4599a5e93ea8SMatthew G. Knepley   PetscInt        off, cind = 0, k;
4600a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4601a5e93ea8SMatthew G. Knepley 
4602a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4603a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
4604a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetOffset(section, point, &off);CHKERRQ(ierr);
4605a5e93ea8SMatthew G. Knepley   a    = &array[off];
4606a5e93ea8SMatthew G. Knepley   if (cdof) {
4607a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
460897e99dd9SToby Isaac     if (clperm) {
460997e99dd9SToby Isaac       if (perm) {
4610a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4611a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
461297e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+perm[k]]] * (flip ? flip[perm[k]] : 1.));
461397e99dd9SToby Isaac             cind++;
4614a5e93ea8SMatthew G. Knepley           }
4615a5e93ea8SMatthew G. Knepley         }
4616a5e93ea8SMatthew G. Knepley       } else {
4617a5e93ea8SMatthew G. Knepley         for (k = 0; k < dof; ++k) {
4618a5e93ea8SMatthew G. Knepley           if ((cind < cdof) && (k == cdofs[cind])) {
461997e99dd9SToby Isaac             fuse(&a[k], values[clperm[offset+     k ]] * (flip ? flip[     k ] : 1.));
462097e99dd9SToby Isaac             cind++;
462197e99dd9SToby Isaac           }
462297e99dd9SToby Isaac         }
462397e99dd9SToby Isaac       }
462497e99dd9SToby Isaac     } else {
462597e99dd9SToby Isaac       if (perm) {
462697e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
462797e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
462897e99dd9SToby Isaac             fuse(&a[k], values[offset+perm[k]] * (flip ? flip[perm[k]] : 1.));
462997e99dd9SToby Isaac             cind++;
463097e99dd9SToby Isaac           }
463197e99dd9SToby Isaac         }
463297e99dd9SToby Isaac       } else {
463397e99dd9SToby Isaac         for (k = 0; k < dof; ++k) {
463497e99dd9SToby Isaac           if ((cind < cdof) && (k == cdofs[cind])) {
463597e99dd9SToby Isaac             fuse(&a[k], values[offset+     k ] * (flip ? flip[     k ] : 1.));
463697e99dd9SToby Isaac             cind++;
463797e99dd9SToby Isaac           }
4638a5e93ea8SMatthew G. Knepley         }
4639a5e93ea8SMatthew G. Knepley       }
4640a5e93ea8SMatthew G. Knepley     }
4641a5e93ea8SMatthew G. Knepley   }
4642a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4643a5e93ea8SMatthew G. Knepley }
4644a5e93ea8SMatthew G. Knepley 
464597e99dd9SToby 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[])
4646a6dfd86eSKarl Rupp {
4647552f7358SJed Brown   PetscScalar    *a;
46481a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
46491a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
465097e99dd9SToby Isaac   PetscInt        cind = 0, b;
4651552f7358SJed Brown   PetscErrorCode  ierr;
4652552f7358SJed Brown 
4653552f7358SJed Brown   PetscFunctionBegin;
4654552f7358SJed Brown   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4655552f7358SJed Brown   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
46561a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
46571a271a75SMatthew G. Knepley   a    = &array[foff];
4658552f7358SJed Brown   if (!fcdof || setBC) {
465997e99dd9SToby Isaac     if (clperm) {
466097e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}}
466197e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}}
4662552f7358SJed Brown     } else {
466397e99dd9SToby Isaac       if (perm) {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}}
466497e99dd9SToby Isaac       else      {for (b = 0; b < fdof; b++) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}}
4665552f7358SJed Brown     }
4666552f7358SJed Brown   } else {
4667552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
466897e99dd9SToby Isaac     if (clperm) {
466997e99dd9SToby Isaac       if (perm) {
467097e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
467197e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
467297e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4673552f7358SJed Brown         }
4674552f7358SJed Brown       } else {
467597e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
467697e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
467797e99dd9SToby Isaac           fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
467897e99dd9SToby Isaac         }
467997e99dd9SToby Isaac       }
468097e99dd9SToby Isaac     } else {
468197e99dd9SToby Isaac       if (perm) {
468297e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
468397e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
468497e99dd9SToby Isaac           fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
468597e99dd9SToby Isaac         }
468697e99dd9SToby Isaac       } else {
468797e99dd9SToby Isaac         for (b = 0; b < fdof; b++) {
468897e99dd9SToby Isaac           if ((cind < fcdof) && (b == fcdofs[cind])) {++cind; continue;}
468997e99dd9SToby Isaac           fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4690552f7358SJed Brown         }
4691552f7358SJed Brown       }
4692552f7358SJed Brown     }
4693552f7358SJed Brown   }
46941a271a75SMatthew G. Knepley   *offset += fdof;
4695552f7358SJed Brown   PetscFunctionReturn(0);
4696552f7358SJed Brown }
4697552f7358SJed Brown 
4698ba322698SMatthew 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[])
4699a5e93ea8SMatthew G. Knepley {
4700a5e93ea8SMatthew G. Knepley   PetscScalar    *a;
47011a271a75SMatthew G. Knepley   PetscInt        fdof, foff, fcdof, foffset = *offset;
47021a271a75SMatthew G. Knepley   const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
4703ba322698SMatthew G. Knepley   PetscInt        cind = 0, ncind = 0, b;
4704ba322698SMatthew G. Knepley   PetscBool       ncSet, fcSet;
4705a5e93ea8SMatthew G. Knepley   PetscErrorCode  ierr;
4706a5e93ea8SMatthew G. Knepley 
4707a5e93ea8SMatthew G. Knepley   PetscFunctionBegin;
4708a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
4709a5e93ea8SMatthew G. Knepley   ierr = PetscSectionGetFieldConstraintDof(section, point, f, &fcdof);CHKERRQ(ierr);
47101a271a75SMatthew G. Knepley   ierr = PetscSectionGetFieldOffset(section, point, f, &foff);CHKERRQ(ierr);
47111a271a75SMatthew G. Knepley   a    = &array[foff];
4712a5e93ea8SMatthew G. Knepley   if (fcdof) {
4713ba322698SMatthew G. Knepley     /* We just override fcdof and fcdofs with Ncc and comps */
4714a5e93ea8SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
471597e99dd9SToby Isaac     if (clperm) {
471697e99dd9SToby Isaac       if (perm) {
4717ba322698SMatthew G. Knepley         if (comps) {
4718ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4719ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4720ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4721ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4722ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));}
4723ba322698SMatthew G. Knepley           }
4724ba322698SMatthew G. Knepley         } else {
472597e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
472697e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
472797e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+perm[b]]] * (flip ? flip[perm[b]] : 1.));
4728a5e93ea8SMatthew G. Knepley               ++cind;
4729a5e93ea8SMatthew G. Knepley             }
4730a5e93ea8SMatthew G. Knepley           }
4731ba322698SMatthew G. Knepley         }
4732ba322698SMatthew G. Knepley       } else {
4733ba322698SMatthew G. Knepley         if (comps) {
4734ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4735ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4736ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4737ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4738ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));}
4739ba322698SMatthew G. Knepley           }
4740a5e93ea8SMatthew G. Knepley         } else {
474197e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
474297e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
474397e99dd9SToby Isaac               fuse(&a[b], values[clperm[foffset+     b ]] * (flip ? flip[     b ] : 1.));
474497e99dd9SToby Isaac               ++cind;
474597e99dd9SToby Isaac             }
474697e99dd9SToby Isaac           }
474797e99dd9SToby Isaac         }
4748ba322698SMatthew G. Knepley       }
474997e99dd9SToby Isaac     } else {
475097e99dd9SToby Isaac       if (perm) {
4751ba322698SMatthew G. Knepley         if (comps) {
4752ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4753ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4754ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4755ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4756ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));}
4757ba322698SMatthew G. Knepley           }
4758ba322698SMatthew G. Knepley         } else {
475997e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
476097e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
476197e99dd9SToby Isaac               fuse(&a[b], values[foffset+perm[b]] * (flip ? flip[perm[b]] : 1.));
476297e99dd9SToby Isaac               ++cind;
476397e99dd9SToby Isaac             }
476497e99dd9SToby Isaac           }
4765ba322698SMatthew G. Knepley         }
4766ba322698SMatthew G. Knepley       } else {
4767ba322698SMatthew G. Knepley         if (comps) {
4768ba322698SMatthew G. Knepley           for (b = 0; b < fdof; b++) {
4769ba322698SMatthew G. Knepley             ncSet = fcSet = PETSC_FALSE;
4770ba322698SMatthew G. Knepley             if ((ncind < Ncc)  && (b == comps[ncind])) {++ncind; ncSet = PETSC_TRUE;}
4771ba322698SMatthew G. Knepley             if ((cind < fcdof) && (b == fcdofs[cind])) {++cind;  fcSet = PETSC_TRUE;}
4772ba322698SMatthew G. Knepley             if (ncSet && fcSet) {fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));}
4773ba322698SMatthew G. Knepley           }
477497e99dd9SToby Isaac         } else {
477597e99dd9SToby Isaac           for (b = 0; b < fdof; b++) {
477697e99dd9SToby Isaac             if ((cind < fcdof) && (b == fcdofs[cind])) {
477797e99dd9SToby Isaac               fuse(&a[b], values[foffset+     b ] * (flip ? flip[     b ] : 1.));
4778a5e93ea8SMatthew G. Knepley               ++cind;
4779a5e93ea8SMatthew G. Knepley             }
4780a5e93ea8SMatthew G. Knepley           }
4781a5e93ea8SMatthew G. Knepley         }
4782a5e93ea8SMatthew G. Knepley       }
4783a5e93ea8SMatthew G. Knepley     }
4784ba322698SMatthew G. Knepley   }
47851a271a75SMatthew G. Knepley   *offset += fdof;
4786a5e93ea8SMatthew G. Knepley   PetscFunctionReturn(0);
4787a5e93ea8SMatthew G. Knepley }
4788a5e93ea8SMatthew G. Knepley 
47898f3be42fSMatthew G. Knepley PETSC_STATIC_INLINE PetscErrorCode DMPlexVecSetClosure_Depth1_Static(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
4790a6dfd86eSKarl Rupp {
4791552f7358SJed Brown   PetscScalar    *array;
47921b406b76SMatthew G. Knepley   const PetscInt *cone, *coneO;
47931b406b76SMatthew G. Knepley   PetscInt        pStart, pEnd, p, numPoints, off, dof;
4794552f7358SJed Brown   PetscErrorCode  ierr;
4795552f7358SJed Brown 
47961b406b76SMatthew G. Knepley   PetscFunctionBeginHot;
4797b6ebb6e6SMatthew G. Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
4798b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, point, &numPoints);CHKERRQ(ierr);
4799b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
4800b6ebb6e6SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, point, &coneO);CHKERRQ(ierr);
4801b6ebb6e6SMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
4802b6ebb6e6SMatthew G. Knepley   for (p = 0, off = 0; p <= numPoints; ++p, off += dof) {
4803b6ebb6e6SMatthew G. Knepley     const PetscInt cp = !p ? point : cone[p-1];
4804b6ebb6e6SMatthew G. Knepley     const PetscInt o  = !p ? 0     : coneO[p-1];
4805b6ebb6e6SMatthew G. Knepley 
4806b6ebb6e6SMatthew G. Knepley     if ((cp < pStart) || (cp >= pEnd)) {dof = 0; continue;}
4807b6ebb6e6SMatthew G. Knepley     ierr = PetscSectionGetDof(section, cp, &dof);CHKERRQ(ierr);
4808b6ebb6e6SMatthew G. Knepley     /* ADD_VALUES */
4809b6ebb6e6SMatthew G. Knepley     {
4810b6ebb6e6SMatthew G. Knepley       const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
4811b6ebb6e6SMatthew G. Knepley       PetscScalar    *a;
4812b6ebb6e6SMatthew G. Knepley       PetscInt        cdof, coff, cind = 0, k;
4813b6ebb6e6SMatthew G. Knepley 
4814b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(section, cp, &cdof);CHKERRQ(ierr);
4815b6ebb6e6SMatthew G. Knepley       ierr = PetscSectionGetOffset(section, cp, &coff);CHKERRQ(ierr);
4816b6ebb6e6SMatthew G. Knepley       a    = &array[coff];
4817b6ebb6e6SMatthew G. Knepley       if (!cdof) {
4818b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4819b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4820b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4821b6ebb6e6SMatthew G. Knepley           }
4822b6ebb6e6SMatthew G. Knepley         } else {
4823b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4824b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4825b6ebb6e6SMatthew G. Knepley           }
4826b6ebb6e6SMatthew G. Knepley         }
4827b6ebb6e6SMatthew G. Knepley       } else {
4828b6ebb6e6SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, cp, &cdofs);CHKERRQ(ierr);
4829b6ebb6e6SMatthew G. Knepley         if (o >= 0) {
4830b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4831b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4832b6ebb6e6SMatthew G. Knepley             a[k] += values[off+k];
4833b6ebb6e6SMatthew G. Knepley           }
4834b6ebb6e6SMatthew G. Knepley         } else {
4835b6ebb6e6SMatthew G. Knepley           for (k = 0; k < dof; ++k) {
4836b6ebb6e6SMatthew G. Knepley             if ((cind < cdof) && (k == cdofs[cind])) {++cind; continue;}
4837b6ebb6e6SMatthew G. Knepley             a[k] += values[off+dof-k-1];
4838b6ebb6e6SMatthew G. Knepley           }
4839b6ebb6e6SMatthew G. Knepley         }
4840b6ebb6e6SMatthew G. Knepley       }
4841b6ebb6e6SMatthew G. Knepley     }
4842b6ebb6e6SMatthew G. Knepley   }
4843b6ebb6e6SMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
4844b6ebb6e6SMatthew G. Knepley   PetscFunctionReturn(0);
4845b6ebb6e6SMatthew G. Knepley }
48461b406b76SMatthew G. Knepley 
48471b406b76SMatthew G. Knepley /*@C
48481b406b76SMatthew G. Knepley   DMPlexVecSetClosure - Set an array of the values on the closure of 'point'
48491b406b76SMatthew G. Knepley 
48501b406b76SMatthew G. Knepley   Not collective
48511b406b76SMatthew G. Knepley 
48521b406b76SMatthew G. Knepley   Input Parameters:
48531b406b76SMatthew G. Knepley + dm - The DM
48541b406b76SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
48551b406b76SMatthew G. Knepley . v - The local vector
4856eaf898f9SPatrick Sanan . point - The point in the DM
48571b406b76SMatthew G. Knepley . values - The array of values
485822c1ee49SMatthew 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,
485922c1ee49SMatthew G. Knepley          where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions.
48601b406b76SMatthew G. Knepley 
48611b406b76SMatthew G. Knepley   Fortran Notes:
48621b406b76SMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
48631b406b76SMatthew G. Knepley 
48641b406b76SMatthew G. Knepley   Level: intermediate
48651b406b76SMatthew G. Knepley 
48661b406b76SMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexMatSetClosure()
48671b406b76SMatthew G. Knepley @*/
48681b406b76SMatthew G. Knepley PetscErrorCode DMPlexVecSetClosure(DM dm, PetscSection section, Vec v, PetscInt point, const PetscScalar values[], InsertMode mode)
48691b406b76SMatthew G. Knepley {
48701b406b76SMatthew G. Knepley   PetscSection    clSection;
48711b406b76SMatthew G. Knepley   IS              clPoints;
48721b406b76SMatthew G. Knepley   PetscScalar    *array;
48731b406b76SMatthew G. Knepley   PetscInt       *points = NULL;
487497e99dd9SToby Isaac   const PetscInt *clp, *clperm;
48751a271a75SMatthew G. Knepley   PetscInt        depth, numFields, numPoints, p;
48761b406b76SMatthew G. Knepley   PetscErrorCode  ierr;
48771b406b76SMatthew G. Knepley 
48781a271a75SMatthew G. Knepley   PetscFunctionBeginHot;
48791b406b76SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
488092fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
48811a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
48821a271a75SMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
48831b406b76SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
48841b406b76SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
48851b406b76SMatthew G. Knepley   if (depth == 1 && numFields < 2 && mode == ADD_VALUES) {
48868f3be42fSMatthew G. Knepley     ierr = DMPlexVecSetClosure_Depth1_Static(dm, section, v, point, values, mode);CHKERRQ(ierr);
48871b406b76SMatthew G. Knepley     PetscFunctionReturn(0);
48881b406b76SMatthew G. Knepley   }
48891a271a75SMatthew G. Knepley   /* Get points */
489097e99dd9SToby Isaac   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
4891923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
48921a271a75SMatthew G. Knepley   /* Get array */
4893552f7358SJed Brown   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
48941a271a75SMatthew G. Knepley   /* Get values */
4895ef90cfe2SMatthew G. Knepley   if (numFields > 0) {
489697e99dd9SToby Isaac     PetscInt offset = 0, f;
4897552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
489897e99dd9SToby Isaac       const PetscInt    **perms = NULL;
489997e99dd9SToby Isaac       const PetscScalar **flips = NULL;
490097e99dd9SToby Isaac 
490197e99dd9SToby Isaac       ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4902552f7358SJed Brown       switch (mode) {
4903552f7358SJed Brown       case INSERT_VALUES:
490497e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
490597e99dd9SToby Isaac           const PetscInt    point = points[2*p];
490697e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
490797e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
490897e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
4909552f7358SJed Brown         } break;
4910552f7358SJed Brown       case INSERT_ALL_VALUES:
491197e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
491297e99dd9SToby Isaac           const PetscInt    point = points[2*p];
491397e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
491497e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
491597e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
4916552f7358SJed Brown         } break;
4917a5e93ea8SMatthew G. Knepley       case INSERT_BC_VALUES:
491897e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
491997e99dd9SToby Isaac           const PetscInt    point = points[2*p];
492097e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
492197e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4922ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, insert, clperm, values, &offset, array);
4923a5e93ea8SMatthew G. Knepley         } break;
4924552f7358SJed Brown       case ADD_VALUES:
492597e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
492697e99dd9SToby Isaac           const PetscInt    point = points[2*p];
492797e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
492897e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
492997e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
4930552f7358SJed Brown         } break;
4931552f7358SJed Brown       case ADD_ALL_VALUES:
493297e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
493397e99dd9SToby Isaac           const PetscInt    point = points[2*p];
493497e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
493597e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
493697e99dd9SToby Isaac           updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
4937552f7358SJed Brown         } break;
4938304ab55fSMatthew G. Knepley       case ADD_BC_VALUES:
493997e99dd9SToby Isaac         for (p = 0; p < numPoints; p++) {
494097e99dd9SToby Isaac           const PetscInt    point = points[2*p];
494197e99dd9SToby Isaac           const PetscInt    *perm = perms ? perms[p] : NULL;
494297e99dd9SToby Isaac           const PetscScalar *flip = flips ? flips[p] : NULL;
4943ba322698SMatthew G. Knepley           updatePointFieldsBC_private(section, point, perm, flip, f, -1, NULL, add, clperm, values, &offset, array);
4944304ab55fSMatthew G. Knepley         } break;
4945552f7358SJed Brown       default:
4946f347f43bSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
4947552f7358SJed Brown       }
494897e99dd9SToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
49491a271a75SMatthew G. Knepley     }
4950552f7358SJed Brown   } else {
49511a271a75SMatthew G. Knepley     PetscInt dof, off;
495297e99dd9SToby Isaac     const PetscInt    **perms = NULL;
495397e99dd9SToby Isaac     const PetscScalar **flips = NULL;
49541a271a75SMatthew G. Knepley 
495597e99dd9SToby Isaac     ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
4956552f7358SJed Brown     switch (mode) {
4957552f7358SJed Brown     case INSERT_VALUES:
495897e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
495997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
496097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
496197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
496297e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
496397e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_FALSE, perm, flip, clperm, values, off, array);
4964552f7358SJed Brown       } break;
4965552f7358SJed Brown     case INSERT_ALL_VALUES:
496697e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
496797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
496897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
496997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
497097e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
497197e99dd9SToby Isaac         updatePoint_private(section, point, dof, insert, PETSC_TRUE,  perm, flip, clperm, values, off, array);
4972552f7358SJed Brown       } break;
4973a5e93ea8SMatthew G. Knepley     case INSERT_BC_VALUES:
497497e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
497597e99dd9SToby Isaac         const PetscInt    point = points[2*p];
497697e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
497797e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
497897e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
497997e99dd9SToby Isaac         updatePointBC_private(section, point, dof, insert,  perm, flip, clperm, values, off, array);
4980a5e93ea8SMatthew G. Knepley       } break;
4981552f7358SJed Brown     case ADD_VALUES:
498297e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
498397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
498497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
498597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
498697e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
498797e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_FALSE, perm, flip, clperm, values, off, array);
4988552f7358SJed Brown       } break;
4989552f7358SJed Brown     case ADD_ALL_VALUES:
499097e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
499197e99dd9SToby Isaac         const PetscInt    point = points[2*p];
499297e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
499397e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
499497e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
499597e99dd9SToby Isaac         updatePoint_private(section, point, dof, add,    PETSC_TRUE,  perm, flip, clperm, values, off, array);
4996552f7358SJed Brown       } break;
4997304ab55fSMatthew G. Knepley     case ADD_BC_VALUES:
499897e99dd9SToby Isaac       for (p = 0, off = 0; p < numPoints; p++, off += dof) {
499997e99dd9SToby Isaac         const PetscInt    point = points[2*p];
500097e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
500197e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
500297e99dd9SToby Isaac         ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
500397e99dd9SToby Isaac         updatePointBC_private(section, point, dof, add,  perm, flip, clperm, values, off, array);
5004304ab55fSMatthew G. Knepley       } break;
5005552f7358SJed Brown     default:
5006f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5007552f7358SJed Brown     }
500897e99dd9SToby Isaac     ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5009552f7358SJed Brown   }
50101a271a75SMatthew G. Knepley   /* Cleanup points */
5011923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
50121a271a75SMatthew G. Knepley   /* Cleanup array */
5013552f7358SJed Brown   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5014552f7358SJed Brown   PetscFunctionReturn(0);
5015552f7358SJed Brown }
5016552f7358SJed Brown 
5017ba322698SMatthew 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)
5018e07394fbSMatthew G. Knepley {
5019e07394fbSMatthew G. Knepley   PetscSection      clSection;
5020e07394fbSMatthew G. Knepley   IS                clPoints;
5021e07394fbSMatthew G. Knepley   PetscScalar       *array;
5022e07394fbSMatthew G. Knepley   PetscInt          *points = NULL;
5023b04c866fSMatthew G. Knepley   const PetscInt    *clp, *clperm;
5024e07394fbSMatthew G. Knepley   PetscInt          numFields, numPoints, p;
502597e99dd9SToby Isaac   PetscInt          offset = 0, f;
5026e07394fbSMatthew G. Knepley   PetscErrorCode    ierr;
5027e07394fbSMatthew G. Knepley 
5028e07394fbSMatthew G. Knepley   PetscFunctionBeginHot;
5029e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
503092fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
5031e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5032e07394fbSMatthew G. Knepley   PetscValidHeaderSpecific(v, VEC_CLASSID, 3);
5033e07394fbSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5034e07394fbSMatthew G. Knepley   /* Get points */
5035b04c866fSMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
5036923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5037e07394fbSMatthew G. Knepley   /* Get array */
5038e07394fbSMatthew G. Knepley   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5039e07394fbSMatthew G. Knepley   /* Get values */
5040e07394fbSMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
504197e99dd9SToby Isaac     const PetscInt    **perms = NULL;
504297e99dd9SToby Isaac     const PetscScalar **flips = NULL;
504397e99dd9SToby Isaac 
5044e07394fbSMatthew G. Knepley     if (!fieldActive[f]) {
5045e07394fbSMatthew G. Knepley       for (p = 0; p < numPoints*2; p += 2) {
5046e07394fbSMatthew G. Knepley         PetscInt fdof;
5047e07394fbSMatthew G. Knepley         ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
5048e07394fbSMatthew G. Knepley         offset += fdof;
5049e07394fbSMatthew G. Knepley       }
5050e07394fbSMatthew G. Knepley       continue;
5051e07394fbSMatthew G. Knepley     }
505297e99dd9SToby Isaac     ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5053e07394fbSMatthew G. Knepley     switch (mode) {
5054e07394fbSMatthew G. Knepley     case INSERT_VALUES:
505597e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
505697e99dd9SToby Isaac         const PetscInt    point = points[2*p];
505797e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
505897e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
5059b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_FALSE, clperm, values, &offset, array);
5060e07394fbSMatthew G. Knepley       } break;
5061e07394fbSMatthew G. Knepley     case INSERT_ALL_VALUES:
506297e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
506397e99dd9SToby Isaac         const PetscInt    point = points[2*p];
506497e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
506597e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
5066b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, insert, PETSC_TRUE, clperm, values, &offset, array);
5067e07394fbSMatthew G. Knepley         } break;
5068e07394fbSMatthew G. Knepley     case INSERT_BC_VALUES:
506997e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
507097e99dd9SToby Isaac         const PetscInt    point = points[2*p];
507197e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
507297e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
5073ba322698SMatthew G. Knepley         updatePointFieldsBC_private(section, point, perm, flip, f, Ncc, comps, insert, clperm, values, &offset, array);
5074e07394fbSMatthew G. Knepley       } break;
5075e07394fbSMatthew G. Knepley     case ADD_VALUES:
507697e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
507797e99dd9SToby Isaac         const PetscInt    point = points[2*p];
507897e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
507997e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
5080b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_FALSE, clperm, values, &offset, array);
5081e07394fbSMatthew G. Knepley       } break;
5082e07394fbSMatthew G. Knepley     case ADD_ALL_VALUES:
508397e99dd9SToby Isaac       for (p = 0; p < numPoints; p++) {
508497e99dd9SToby Isaac         const PetscInt    point = points[2*p];
508597e99dd9SToby Isaac         const PetscInt    *perm = perms ? perms[p] : NULL;
508697e99dd9SToby Isaac         const PetscScalar *flip = flips ? flips[p] : NULL;
5087b04c866fSMatthew G. Knepley         updatePointFields_private(section, point, perm, flip, f, add, PETSC_TRUE, clperm, values, &offset, array);
5088e07394fbSMatthew G. Knepley       } break;
5089e07394fbSMatthew G. Knepley     default:
5090f347f43bSBarry Smith       SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insert mode %d", mode);
5091e07394fbSMatthew G. Knepley     }
509297e99dd9SToby Isaac     ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms,&flips);CHKERRQ(ierr);
5093e07394fbSMatthew G. Knepley   }
5094e07394fbSMatthew G. Knepley   /* Cleanup points */
5095923c78e0SToby Isaac   ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5096e07394fbSMatthew G. Knepley   /* Cleanup array */
5097e07394fbSMatthew G. Knepley   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5098e07394fbSMatthew G. Knepley   PetscFunctionReturn(0);
5099e07394fbSMatthew G. Knepley }
5100e07394fbSMatthew G. Knepley 
51017cd05799SMatthew G. Knepley static PetscErrorCode DMPlexPrintMatSetValues(PetscViewer viewer, Mat A, PetscInt point, PetscInt numRIndices, const PetscInt rindices[], PetscInt numCIndices, const PetscInt cindices[], const PetscScalar values[])
5102552f7358SJed Brown {
5103552f7358SJed Brown   PetscMPIInt    rank;
5104552f7358SJed Brown   PetscInt       i, j;
5105552f7358SJed Brown   PetscErrorCode ierr;
5106552f7358SJed Brown 
5107552f7358SJed Brown   PetscFunctionBegin;
510882f516ccSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr);
5109eaf898f9SPatrick Sanan   ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat for point %D\n", rank, point);CHKERRQ(ierr);
5110e4b003c7SBarry Smith   for (i = 0; i < numRIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat row indices[%D] = %D\n", rank, i, rindices[i]);CHKERRQ(ierr);}
5111e4b003c7SBarry Smith   for (i = 0; i < numCIndices; i++) {ierr = PetscViewerASCIIPrintf(viewer, "[%d]mat col indices[%D] = %D\n", rank, i, cindices[i]);CHKERRQ(ierr);}
5112b0ecff45SMatthew G. Knepley   numCIndices = numCIndices ? numCIndices : numRIndices;
5113b0ecff45SMatthew G. Knepley   for (i = 0; i < numRIndices; i++) {
5114e4b003c7SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer, "[%d]", rank);CHKERRQ(ierr);
5115b0ecff45SMatthew G. Knepley     for (j = 0; j < numCIndices; j++) {
5116519f805aSKarl Rupp #if defined(PETSC_USE_COMPLEX)
51177eba1a17SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " (%g,%g)", (double)PetscRealPart(values[i*numCIndices+j]), (double)PetscImaginaryPart(values[i*numCIndices+j]));CHKERRQ(ierr);
5118552f7358SJed Brown #else
5119b0ecff45SMatthew G. Knepley       ierr = PetscViewerASCIIPrintf(viewer, " %g", (double)values[i*numCIndices+j]);CHKERRQ(ierr);
5120552f7358SJed Brown #endif
5121552f7358SJed Brown     }
512277a6746dSMatthew G Knepley     ierr = PetscViewerASCIIPrintf(viewer, "\n");CHKERRQ(ierr);
5123552f7358SJed Brown   }
5124552f7358SJed Brown   PetscFunctionReturn(0);
5125552f7358SJed Brown }
5126552f7358SJed Brown 
512705586334SMatthew G. Knepley /*
512805586334SMatthew G. Knepley   DMPlexGetIndicesPoint_Internal - Add the indices for dofs on a point to an index array
512905586334SMatthew G. Knepley 
513005586334SMatthew G. Knepley   Input Parameters:
513105586334SMatthew G. Knepley + section - The section for this data layout
513205586334SMatthew G. Knepley . point   - The point contributing dofs with these indices
513305586334SMatthew G. Knepley . off     - The global offset of this point
513405586334SMatthew G. Knepley . loff    - The local offset of each field
513505586334SMatthew G. Knepley . setBC   - The flag determining whether to include indices of bounsary values
513605586334SMatthew G. Knepley . perm    - A permutation of the dofs on this point, or NULL
513705586334SMatthew G. Knepley - indperm - A permutation of the entire indices array, or NULL
513805586334SMatthew G. Knepley 
513905586334SMatthew G. Knepley   Output Parameter:
514005586334SMatthew G. Knepley . indices - Indices for dofs on this point
514105586334SMatthew G. Knepley 
514205586334SMatthew G. Knepley   Level: developer
514305586334SMatthew G. Knepley 
514405586334SMatthew G. Knepley   Note: The indices could be local or global, depending on the value of 'off'.
514505586334SMatthew G. Knepley */
514605586334SMatthew G. Knepley PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection section, PetscInt point, PetscInt off, PetscInt *loff, PetscBool setBC, const PetscInt perm[], const PetscInt indperm[], PetscInt indices[])
5147a6dfd86eSKarl Rupp {
5148e6ccafaeSMatthew G Knepley   PetscInt        dof;   /* The number of unknowns on this point */
5149552f7358SJed Brown   PetscInt        cdof;  /* The number of constraints on this point */
5150552f7358SJed Brown   const PetscInt *cdofs; /* The indices of the constrained dofs on this point */
5151552f7358SJed Brown   PetscInt        cind = 0, k;
5152552f7358SJed Brown   PetscErrorCode  ierr;
5153552f7358SJed Brown 
5154552f7358SJed Brown   PetscFunctionBegin;
5155552f7358SJed Brown   ierr = PetscSectionGetDof(section, point, &dof);CHKERRQ(ierr);
5156552f7358SJed Brown   ierr = PetscSectionGetConstraintDof(section, point, &cdof);CHKERRQ(ierr);
5157552f7358SJed Brown   if (!cdof || setBC) {
515805586334SMatthew G. Knepley     for (k = 0; k < dof; ++k) {
515905586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
516005586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
516105586334SMatthew G. Knepley 
516205586334SMatthew G. Knepley       indices[ind] = off + k;
5163552f7358SJed Brown     }
5164552f7358SJed Brown   } else {
5165552f7358SJed Brown     ierr = PetscSectionGetConstraintIndices(section, point, &cdofs);CHKERRQ(ierr);
51664acb8e1eSToby Isaac     for (k = 0; k < dof; ++k) {
516705586334SMatthew G. Knepley       const PetscInt preind = perm ? *loff+perm[k] : *loff+k;
516805586334SMatthew G. Knepley       const PetscInt ind    = indperm ? indperm[preind] : preind;
516905586334SMatthew G. Knepley 
51704acb8e1eSToby Isaac       if ((cind < cdof) && (k == cdofs[cind])) {
51714acb8e1eSToby Isaac         /* Insert check for returning constrained indices */
517205586334SMatthew G. Knepley         indices[ind] = -(off+k+1);
51734acb8e1eSToby Isaac         ++cind;
51744acb8e1eSToby Isaac       } else {
517505586334SMatthew G. Knepley         indices[ind] = off+k-cind;
5176552f7358SJed Brown       }
5177552f7358SJed Brown     }
5178552f7358SJed Brown   }
5179e6ccafaeSMatthew G Knepley   *loff += dof;
5180552f7358SJed Brown   PetscFunctionReturn(0);
5181552f7358SJed Brown }
5182552f7358SJed Brown 
51837e29afd2SMatthew G. Knepley /*
51847e29afd2SMatthew G. Knepley   This version only believes the point offset from the globalSection
51857e29afd2SMatthew G. Knepley 
51867e29afd2SMatthew G. Knepley  . off - The global offset of this point
51877e29afd2SMatthew G. Knepley */
518805586334SMatthew 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[])
5189a6dfd86eSKarl Rupp {
5190552f7358SJed Brown   PetscInt       numFields, foff, f;
5191552f7358SJed Brown   PetscErrorCode ierr;
5192552f7358SJed Brown 
5193552f7358SJed Brown   PetscFunctionBegin;
5194552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5195552f7358SJed Brown   for (f = 0, foff = 0; f < numFields; ++f) {
51964acb8e1eSToby Isaac     PetscInt        fdof, cfdof;
5197552f7358SJed Brown     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
51984acb8e1eSToby Isaac     PetscInt        cind = 0, b;
51994acb8e1eSToby Isaac     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
5200552f7358SJed Brown 
5201552f7358SJed Brown     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
5202552f7358SJed Brown     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
5203552f7358SJed Brown     if (!cfdof || setBC) {
520405586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
520505586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
520605586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
520705586334SMatthew G. Knepley 
520805586334SMatthew G. Knepley         indices[ind] = off+foff+b;
520905586334SMatthew G. Knepley       }
5210552f7358SJed Brown     } else {
5211552f7358SJed Brown       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
521205586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
521305586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
521405586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
521505586334SMatthew G. Knepley 
52164acb8e1eSToby Isaac         if ((cind < cfdof) && (b == fcdofs[cind])) {
521705586334SMatthew G. Knepley           indices[ind] = -(off+foff+b+1);
5218552f7358SJed Brown           ++cind;
5219552f7358SJed Brown         } else {
522005586334SMatthew G. Knepley           indices[ind] = off+foff+b-cind;
5221552f7358SJed Brown         }
5222552f7358SJed Brown       }
5223552f7358SJed Brown     }
5224a9096520SToby Isaac     foff     += (setBC ? fdof : (fdof - cfdof));
5225552f7358SJed Brown     foffs[f] += fdof;
5226552f7358SJed Brown   }
5227552f7358SJed Brown   PetscFunctionReturn(0);
5228552f7358SJed Brown }
5229552f7358SJed Brown 
52307e29afd2SMatthew G. Knepley /*
52317e29afd2SMatthew G. Knepley   This version believes the globalSection offsets for each field, rather than just the point offset
52327e29afd2SMatthew G. Knepley 
52337e29afd2SMatthew G. Knepley  . foffs - The offset into 'indices' for each field, since it is segregated by field
52347e29afd2SMatthew G. Knepley */
523505586334SMatthew 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[])
52367e29afd2SMatthew G. Knepley {
52377e29afd2SMatthew G. Knepley   PetscInt       numFields, foff, f;
52387e29afd2SMatthew G. Knepley   PetscErrorCode ierr;
52397e29afd2SMatthew G. Knepley 
52407e29afd2SMatthew G. Knepley   PetscFunctionBegin;
52417e29afd2SMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
52427e29afd2SMatthew G. Knepley   for (f = 0; f < numFields; ++f) {
52437e29afd2SMatthew G. Knepley     PetscInt        fdof, cfdof;
52447e29afd2SMatthew G. Knepley     const PetscInt *fcdofs; /* The indices of the constrained dofs for field f on this point */
52457e29afd2SMatthew G. Knepley     PetscInt        cind = 0, b;
52467e29afd2SMatthew G. Knepley     const PetscInt  *perm = (perms && perms[f]) ? perms[f][permsoff] : NULL;
52477e29afd2SMatthew G. Knepley 
52487e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldDof(section, point, f, &fdof);CHKERRQ(ierr);
52497e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldConstraintDof(section, point, f, &cfdof);CHKERRQ(ierr);
52507e29afd2SMatthew G. Knepley     ierr = PetscSectionGetFieldOffset(globalSection, point, f, &foff);CHKERRQ(ierr);
52517e29afd2SMatthew G. Knepley     if (!cfdof || setBC) {
525205586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
525305586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
525405586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
525505586334SMatthew G. Knepley 
525605586334SMatthew G. Knepley         indices[ind] = foff+b;
525705586334SMatthew G. Knepley       }
52587e29afd2SMatthew G. Knepley     } else {
52597e29afd2SMatthew G. Knepley       ierr = PetscSectionGetFieldConstraintIndices(section, point, f, &fcdofs);CHKERRQ(ierr);
526005586334SMatthew G. Knepley       for (b = 0; b < fdof; ++b) {
526105586334SMatthew G. Knepley         const PetscInt preind = perm ? foffs[f]+perm[b] : foffs[f]+b;
526205586334SMatthew G. Knepley         const PetscInt ind    = indperm ? indperm[preind] : preind;
526305586334SMatthew G. Knepley 
52647e29afd2SMatthew G. Knepley         if ((cind < cfdof) && (b == fcdofs[cind])) {
526505586334SMatthew G. Knepley           indices[ind] = -(foff+b+1);
52667e29afd2SMatthew G. Knepley           ++cind;
52677e29afd2SMatthew G. Knepley         } else {
526805586334SMatthew G. Knepley           indices[ind] = foff+b-cind;
52697e29afd2SMatthew G. Knepley         }
52707e29afd2SMatthew G. Knepley       }
52717e29afd2SMatthew G. Knepley     }
52727e29afd2SMatthew G. Knepley     foffs[f] += fdof;
52737e29afd2SMatthew G. Knepley   }
52747e29afd2SMatthew G. Knepley   PetscFunctionReturn(0);
52757e29afd2SMatthew G. Knepley }
52767e29afd2SMatthew G. Knepley 
52774acb8e1eSToby 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)
5278d3d1a6afSToby Isaac {
5279d3d1a6afSToby Isaac   Mat             cMat;
5280d3d1a6afSToby Isaac   PetscSection    aSec, cSec;
5281d3d1a6afSToby Isaac   IS              aIS;
5282d3d1a6afSToby Isaac   PetscInt        aStart = -1, aEnd = -1;
5283d3d1a6afSToby Isaac   const PetscInt  *anchors;
5284e17c06e0SMatthew G. Knepley   PetscInt        numFields, f, p, q, newP = 0;
5285d3d1a6afSToby Isaac   PetscInt        newNumPoints = 0, newNumIndices = 0;
5286d3d1a6afSToby Isaac   PetscInt        *newPoints, *indices, *newIndices;
5287d3d1a6afSToby Isaac   PetscInt        maxAnchor, maxDof;
5288d3d1a6afSToby Isaac   PetscInt        newOffsets[32];
5289d3d1a6afSToby Isaac   PetscInt        *pointMatOffsets[32];
5290d3d1a6afSToby Isaac   PetscInt        *newPointOffsets[32];
5291d3d1a6afSToby Isaac   PetscScalar     *pointMat[32];
52926ecaa68aSToby Isaac   PetscScalar     *newValues=NULL,*tmpValues;
5293d3d1a6afSToby Isaac   PetscBool       anyConstrained = PETSC_FALSE;
5294d3d1a6afSToby Isaac   PetscErrorCode  ierr;
5295d3d1a6afSToby Isaac 
5296d3d1a6afSToby Isaac   PetscFunctionBegin;
5297d3d1a6afSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5298d3d1a6afSToby Isaac   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5299d3d1a6afSToby Isaac   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
5300d3d1a6afSToby Isaac 
5301a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
5302d3d1a6afSToby Isaac   /* if there are point-to-point constraints */
5303d3d1a6afSToby Isaac   if (aSec) {
5304580bdb30SBarry Smith     ierr = PetscArrayzero(newOffsets, 32);CHKERRQ(ierr);
5305d3d1a6afSToby Isaac     ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
5306d3d1a6afSToby Isaac     ierr = PetscSectionGetChart(aSec,&aStart,&aEnd);CHKERRQ(ierr);
5307d3d1a6afSToby Isaac     /* figure out how many points are going to be in the new element matrix
5308d3d1a6afSToby Isaac      * (we allow double counting, because it's all just going to be summed
5309d3d1a6afSToby Isaac      * into the global matrix anyway) */
5310d3d1a6afSToby Isaac     for (p = 0; p < 2*numPoints; p+=2) {
5311d3d1a6afSToby Isaac       PetscInt b    = points[p];
53124b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5313d3d1a6afSToby Isaac 
53144b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
53154b2f2278SToby Isaac       if (!bSecDof) {
53164b2f2278SToby Isaac         continue;
53174b2f2278SToby Isaac       }
5318d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5319d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec,b,&bDof);CHKERRQ(ierr);
5320d3d1a6afSToby Isaac       }
5321d3d1a6afSToby Isaac       if (bDof) {
5322d3d1a6afSToby Isaac         /* this point is constrained */
5323d3d1a6afSToby Isaac         /* it is going to be replaced by its anchors */
5324d3d1a6afSToby Isaac         PetscInt bOff, q;
5325d3d1a6afSToby Isaac 
5326d3d1a6afSToby Isaac         anyConstrained = PETSC_TRUE;
5327d3d1a6afSToby Isaac         newNumPoints  += bDof;
5328d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec,b,&bOff);CHKERRQ(ierr);
5329d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5330d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q];
5331d3d1a6afSToby Isaac           PetscInt aDof;
5332d3d1a6afSToby Isaac 
5333d3d1a6afSToby Isaac           ierr           = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
5334d3d1a6afSToby Isaac           newNumIndices += aDof;
5335d3d1a6afSToby Isaac           for (f = 0; f < numFields; ++f) {
5336d3d1a6afSToby Isaac             PetscInt fDof;
5337d3d1a6afSToby Isaac 
5338d3d1a6afSToby Isaac             ierr             = PetscSectionGetFieldDof(section, a, f, &fDof);CHKERRQ(ierr);
5339d3d1a6afSToby Isaac             newOffsets[f+1] += fDof;
5340d3d1a6afSToby Isaac           }
5341d3d1a6afSToby Isaac         }
5342d3d1a6afSToby Isaac       }
5343d3d1a6afSToby Isaac       else {
5344d3d1a6afSToby Isaac         /* this point is not constrained */
5345d3d1a6afSToby Isaac         newNumPoints++;
53464b2f2278SToby Isaac         newNumIndices += bSecDof;
5347d3d1a6afSToby Isaac         for (f = 0; f < numFields; ++f) {
5348d3d1a6afSToby Isaac           PetscInt fDof;
5349d3d1a6afSToby Isaac 
5350d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5351d3d1a6afSToby Isaac           newOffsets[f+1] += fDof;
5352d3d1a6afSToby Isaac         }
5353d3d1a6afSToby Isaac       }
5354d3d1a6afSToby Isaac     }
5355d3d1a6afSToby Isaac   }
5356d3d1a6afSToby Isaac   if (!anyConstrained) {
535772b80496SMatthew G. Knepley     if (outNumPoints)  *outNumPoints  = 0;
535872b80496SMatthew G. Knepley     if (outNumIndices) *outNumIndices = 0;
535972b80496SMatthew G. Knepley     if (outPoints)     *outPoints     = NULL;
536072b80496SMatthew G. Knepley     if (outValues)     *outValues     = NULL;
536172b80496SMatthew G. Knepley     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
5362d3d1a6afSToby Isaac     PetscFunctionReturn(0);
5363d3d1a6afSToby Isaac   }
5364d3d1a6afSToby Isaac 
53656ecaa68aSToby Isaac   if (outNumPoints)  *outNumPoints  = newNumPoints;
53666ecaa68aSToby Isaac   if (outNumIndices) *outNumIndices = newNumIndices;
53676ecaa68aSToby Isaac 
5368f13f9184SToby Isaac   for (f = 0; f < numFields; ++f) newOffsets[f+1] += newOffsets[f];
5369d3d1a6afSToby Isaac 
53706ecaa68aSToby Isaac   if (!outPoints && !outValues) {
53716ecaa68aSToby Isaac     if (offsets) {
53726ecaa68aSToby Isaac       for (f = 0; f <= numFields; f++) {
53736ecaa68aSToby Isaac         offsets[f] = newOffsets[f];
53746ecaa68aSToby Isaac       }
53756ecaa68aSToby Isaac     }
53766ecaa68aSToby Isaac     if (aSec) {ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);}
53776ecaa68aSToby Isaac     PetscFunctionReturn(0);
53786ecaa68aSToby Isaac   }
53796ecaa68aSToby Isaac 
5380f347f43bSBarry Smith   if (numFields && newOffsets[numFields] != newNumIndices) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", newOffsets[numFields], newNumIndices);
5381d3d1a6afSToby Isaac 
5382f7c74593SToby Isaac   ierr = DMGetDefaultConstraints(dm, &cSec, &cMat);CHKERRQ(ierr);
5383d3d1a6afSToby Isaac 
5384d3d1a6afSToby Isaac   /* workspaces */
5385d3d1a6afSToby Isaac   if (numFields) {
5386d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
538769291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
538869291d52SBarry Smith       ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5389d3d1a6afSToby Isaac     }
5390d3d1a6afSToby Isaac   }
5391d3d1a6afSToby Isaac   else {
539269291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
539369291d52SBarry Smith     ierr = DMGetWorkArray(dm,numPoints,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5394d3d1a6afSToby Isaac   }
5395d3d1a6afSToby Isaac 
5396d3d1a6afSToby Isaac   /* get workspaces for the point-to-point matrices */
5397d3d1a6afSToby Isaac   if (numFields) {
53984b2f2278SToby Isaac     PetscInt totalOffset, totalMatOffset;
53994b2f2278SToby Isaac 
5400d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5401d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
54024b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5403d3d1a6afSToby Isaac 
54044b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
54054b2f2278SToby Isaac       if (!bSecDof) {
54064b2f2278SToby Isaac         for (f = 0; f < numFields; f++) {
54074b2f2278SToby Isaac           newPointOffsets[f][p + 1] = 0;
54084b2f2278SToby Isaac           pointMatOffsets[f][p + 1] = 0;
54094b2f2278SToby Isaac         }
54104b2f2278SToby Isaac         continue;
54114b2f2278SToby Isaac       }
5412d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5413d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5414d3d1a6afSToby Isaac       }
5415d3d1a6afSToby Isaac       if (bDof) {
5416d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5417d3d1a6afSToby Isaac           PetscInt fDof, q, bOff, allFDof = 0;
5418d3d1a6afSToby Isaac 
5419d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5420d3d1a6afSToby Isaac           ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5421d3d1a6afSToby Isaac           for (q = 0; q < bDof; q++) {
5422d3d1a6afSToby Isaac             PetscInt a = anchors[bOff + q];
5423d3d1a6afSToby Isaac             PetscInt aFDof;
5424d3d1a6afSToby Isaac 
5425d3d1a6afSToby Isaac             ierr     = PetscSectionGetFieldDof(section, a, f, &aFDof);CHKERRQ(ierr);
5426d3d1a6afSToby Isaac             allFDof += aFDof;
5427d3d1a6afSToby Isaac           }
5428d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = allFDof;
5429d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = fDof * allFDof;
5430d3d1a6afSToby Isaac         }
5431d3d1a6afSToby Isaac       }
5432d3d1a6afSToby Isaac       else {
5433d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5434d3d1a6afSToby Isaac           PetscInt fDof;
5435d3d1a6afSToby Isaac 
5436d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section, b, f, &fDof);CHKERRQ(ierr);
5437d3d1a6afSToby Isaac           newPointOffsets[f][p+1] = fDof;
5438d3d1a6afSToby Isaac           pointMatOffsets[f][p+1] = 0;
5439d3d1a6afSToby Isaac         }
5440d3d1a6afSToby Isaac       }
5441d3d1a6afSToby Isaac     }
54424b2f2278SToby Isaac     for (f = 0, totalOffset = 0, totalMatOffset = 0; f < numFields; f++) {
54434b2f2278SToby Isaac       newPointOffsets[f][0] = totalOffset;
54444b2f2278SToby Isaac       pointMatOffsets[f][0] = totalMatOffset;
5445d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5446d3d1a6afSToby Isaac         newPointOffsets[f][p+1] += newPointOffsets[f][p];
5447d3d1a6afSToby Isaac         pointMatOffsets[f][p+1] += pointMatOffsets[f][p];
5448d3d1a6afSToby Isaac       }
544919f70fd5SToby Isaac       totalOffset    = newPointOffsets[f][numPoints];
545019f70fd5SToby Isaac       totalMatOffset = pointMatOffsets[f][numPoints];
545169291d52SBarry Smith       ierr = DMGetWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
5452d3d1a6afSToby Isaac     }
5453d3d1a6afSToby Isaac   }
5454d3d1a6afSToby Isaac   else {
5455d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5456d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
54574b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5458d3d1a6afSToby Isaac 
54594b2f2278SToby Isaac       ierr = PetscSectionGetDof(section,b,&bSecDof);CHKERRQ(ierr);
54604b2f2278SToby Isaac       if (!bSecDof) {
54614b2f2278SToby Isaac         newPointOffsets[0][p + 1] = 0;
54624b2f2278SToby Isaac         pointMatOffsets[0][p + 1] = 0;
54634b2f2278SToby Isaac         continue;
54644b2f2278SToby Isaac       }
5465d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5466d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5467d3d1a6afSToby Isaac       }
5468d3d1a6afSToby Isaac       if (bDof) {
54694b2f2278SToby Isaac         PetscInt bOff, q, allDof = 0;
5470d3d1a6afSToby Isaac 
5471d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5472d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5473d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aDof;
5474d3d1a6afSToby Isaac 
5475d3d1a6afSToby Isaac           ierr    = PetscSectionGetDof(section, a, &aDof);CHKERRQ(ierr);
5476d3d1a6afSToby Isaac           allDof += aDof;
5477d3d1a6afSToby Isaac         }
5478d3d1a6afSToby Isaac         newPointOffsets[0][p+1] = allDof;
54794b2f2278SToby Isaac         pointMatOffsets[0][p+1] = bSecDof * allDof;
5480d3d1a6afSToby Isaac       }
5481d3d1a6afSToby Isaac       else {
54824b2f2278SToby Isaac         newPointOffsets[0][p+1] = bSecDof;
5483d3d1a6afSToby Isaac         pointMatOffsets[0][p+1] = 0;
5484d3d1a6afSToby Isaac       }
5485d3d1a6afSToby Isaac     }
5486d3d1a6afSToby Isaac     newPointOffsets[0][0] = 0;
5487d3d1a6afSToby Isaac     pointMatOffsets[0][0] = 0;
5488d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5489d3d1a6afSToby Isaac       newPointOffsets[0][p+1] += newPointOffsets[0][p];
5490d3d1a6afSToby Isaac       pointMatOffsets[0][p+1] += pointMatOffsets[0][p];
5491d3d1a6afSToby Isaac     }
549269291d52SBarry Smith     ierr = DMGetWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
5493d3d1a6afSToby Isaac   }
5494d3d1a6afSToby Isaac 
54956ecaa68aSToby Isaac   /* output arrays */
549669291d52SBarry Smith   ierr = DMGetWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
54976ecaa68aSToby Isaac 
5498d3d1a6afSToby Isaac   /* get the point-to-point matrices; construct newPoints */
5499d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(aSec, &maxAnchor);CHKERRQ(ierr);
5500d3d1a6afSToby Isaac   ierr = PetscSectionGetMaxDof(section, &maxDof);CHKERRQ(ierr);
550169291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
550269291d52SBarry Smith   ierr = DMGetWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
5503d3d1a6afSToby Isaac   if (numFields) {
5504d3d1a6afSToby Isaac     for (p = 0, newP = 0; p < numPoints; p++) {
5505d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5506d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
55074b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5508d3d1a6afSToby Isaac 
55094b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
55104b2f2278SToby Isaac       if (!bSecDof) {
55114b2f2278SToby Isaac         continue;
55124b2f2278SToby Isaac       }
5513d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5514d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5515d3d1a6afSToby Isaac       }
5516d3d1a6afSToby Isaac       if (bDof) {
5517d3d1a6afSToby Isaac         PetscInt fStart[32], fEnd[32], fAnchorStart[32], fAnchorEnd[32], bOff, q;
5518d3d1a6afSToby Isaac 
5519d3d1a6afSToby Isaac         fStart[0] = 0;
5520d3d1a6afSToby Isaac         fEnd[0]   = 0;
5521d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5522d3d1a6afSToby Isaac           PetscInt fDof;
5523d3d1a6afSToby Isaac 
5524d3d1a6afSToby Isaac           ierr        = PetscSectionGetFieldDof(cSec, b, f, &fDof);CHKERRQ(ierr);
5525d3d1a6afSToby Isaac           fStart[f+1] = fStart[f] + fDof;
5526d3d1a6afSToby Isaac           fEnd[f+1]   = fStart[f+1];
5527d3d1a6afSToby Isaac         }
5528d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
552905586334SMatthew G. Knepley         ierr = DMPlexGetIndicesPointFields_Internal(cSec, b, bOff, fEnd, PETSC_TRUE, perms, p, NULL, indices);CHKERRQ(ierr);
5530d3d1a6afSToby Isaac 
5531d3d1a6afSToby Isaac         fAnchorStart[0] = 0;
5532d3d1a6afSToby Isaac         fAnchorEnd[0]   = 0;
5533d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5534d3d1a6afSToby Isaac           PetscInt fDof = newPointOffsets[f][p + 1] - newPointOffsets[f][p];
5535d3d1a6afSToby Isaac 
5536d3d1a6afSToby Isaac           fAnchorStart[f+1] = fAnchorStart[f] + fDof;
5537d3d1a6afSToby Isaac           fAnchorEnd[f+1]   = fAnchorStart[f + 1];
5538d3d1a6afSToby Isaac         }
5539d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(aSec, b, &bOff);CHKERRQ(ierr);
5540d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5541d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5542d3d1a6afSToby Isaac 
5543d3d1a6afSToby 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 */
5544d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5545d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5546302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
554705586334SMatthew G. Knepley           ierr = DMPlexGetIndicesPointFields_Internal(section, a, aOff, fAnchorEnd, PETSC_TRUE, NULL, -1, NULL, newIndices);CHKERRQ(ierr);
5548d3d1a6afSToby Isaac         }
5549d3d1a6afSToby Isaac         newP += bDof;
5550d3d1a6afSToby Isaac 
55516ecaa68aSToby Isaac         if (outValues) {
5552d3d1a6afSToby Isaac           /* get the point-to-point submatrix */
5553d3d1a6afSToby Isaac           for (f = 0; f < numFields; f++) {
5554d3d1a6afSToby 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);
5555d3d1a6afSToby Isaac           }
5556d3d1a6afSToby Isaac         }
55576ecaa68aSToby Isaac       }
5558d3d1a6afSToby Isaac       else {
5559d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5560d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5561d3d1a6afSToby Isaac         newP++;
5562d3d1a6afSToby Isaac       }
5563d3d1a6afSToby Isaac     }
5564d3d1a6afSToby Isaac   } else {
5565d3d1a6afSToby Isaac     for (p = 0; p < numPoints; p++) {
5566d3d1a6afSToby Isaac       PetscInt b    = points[2*p];
5567d3d1a6afSToby Isaac       PetscInt o    = points[2*p+1];
55684b2f2278SToby Isaac       PetscInt bDof = 0, bSecDof;
5569d3d1a6afSToby Isaac 
55704b2f2278SToby Isaac       ierr = PetscSectionGetDof(section, b, &bSecDof);CHKERRQ(ierr);
55714b2f2278SToby Isaac       if (!bSecDof) {
55724b2f2278SToby Isaac         continue;
55734b2f2278SToby Isaac       }
5574d3d1a6afSToby Isaac       if (b >= aStart && b < aEnd) {
5575d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(aSec, b, &bDof);CHKERRQ(ierr);
5576d3d1a6afSToby Isaac       }
5577d3d1a6afSToby Isaac       if (bDof) {
5578d3d1a6afSToby Isaac         PetscInt bEnd = 0, bAnchorEnd = 0, bOff;
5579d3d1a6afSToby Isaac 
5580d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset(cSec, b, &bOff);CHKERRQ(ierr);
558105586334SMatthew G. Knepley         ierr = DMPlexGetIndicesPoint_Internal(cSec, b, bOff, &bEnd, PETSC_TRUE, (perms && perms[0]) ? perms[0][p] : NULL, NULL, indices);CHKERRQ(ierr);
5582d3d1a6afSToby Isaac 
5583d3d1a6afSToby Isaac         ierr = PetscSectionGetOffset (aSec, b, &bOff);CHKERRQ(ierr);
5584d3d1a6afSToby Isaac         for (q = 0; q < bDof; q++) {
5585d3d1a6afSToby Isaac           PetscInt a = anchors[bOff + q], aOff;
5586d3d1a6afSToby Isaac 
5587d3d1a6afSToby 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 */
5588d3d1a6afSToby Isaac 
5589d3d1a6afSToby Isaac           newPoints[2*(newP + q)]     = a;
5590d3d1a6afSToby Isaac           newPoints[2*(newP + q) + 1] = 0;
5591302440fdSBarry Smith           ierr = PetscSectionGetOffset(section, a, &aOff);CHKERRQ(ierr);
559205586334SMatthew G. Knepley           ierr = DMPlexGetIndicesPoint_Internal(section, a, aOff, &bAnchorEnd, PETSC_TRUE, NULL, NULL, newIndices);CHKERRQ(ierr);
5593d3d1a6afSToby Isaac         }
5594d3d1a6afSToby Isaac         newP += bDof;
5595d3d1a6afSToby Isaac 
5596d3d1a6afSToby Isaac         /* get the point-to-point submatrix */
55976ecaa68aSToby Isaac         if (outValues) {
5598d3d1a6afSToby Isaac           ierr = MatGetValues(cMat,bEnd,indices,bAnchorEnd,newIndices,pointMat[0] + pointMatOffsets[0][p]);CHKERRQ(ierr);
5599d3d1a6afSToby Isaac         }
56006ecaa68aSToby Isaac       }
5601d3d1a6afSToby Isaac       else {
5602d3d1a6afSToby Isaac         newPoints[2 * newP]     = b;
5603d3d1a6afSToby Isaac         newPoints[2 * newP + 1] = o;
5604d3d1a6afSToby Isaac         newP++;
5605d3d1a6afSToby Isaac       }
5606d3d1a6afSToby Isaac     }
5607d3d1a6afSToby Isaac   }
5608d3d1a6afSToby Isaac 
56096ecaa68aSToby Isaac   if (outValues) {
561069291d52SBarry Smith     ierr = DMGetWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
5611580bdb30SBarry Smith     ierr = PetscArrayzero(tmpValues,newNumIndices*numIndices);CHKERRQ(ierr);
5612d3d1a6afSToby Isaac     /* multiply constraints on the right */
5613d3d1a6afSToby Isaac     if (numFields) {
5614d3d1a6afSToby Isaac       for (f = 0; f < numFields; f++) {
5615d3d1a6afSToby Isaac         PetscInt oldOff = offsets[f];
5616d3d1a6afSToby Isaac 
5617d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5618d3d1a6afSToby Isaac           PetscInt cStart = newPointOffsets[f][p];
5619d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5620d3d1a6afSToby Isaac           PetscInt c, r, k;
5621d3d1a6afSToby Isaac           PetscInt dof;
5622d3d1a6afSToby Isaac 
5623d3d1a6afSToby Isaac           ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
56244b2f2278SToby Isaac           if (!dof) {
56254b2f2278SToby Isaac             continue;
56264b2f2278SToby Isaac           }
5627d3d1a6afSToby Isaac           if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5628d3d1a6afSToby Isaac             PetscInt nCols         = newPointOffsets[f][p+1]-cStart;
5629d3d1a6afSToby Isaac             const PetscScalar *mat = pointMat[f] + pointMatOffsets[f][p];
5630d3d1a6afSToby Isaac 
5631d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5632d3d1a6afSToby Isaac               for (c = 0; c < nCols; c++) {
5633d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
56344acb8e1eSToby Isaac                   tmpValues[r * newNumIndices + cStart + c] += values[r * numIndices + oldOff + k] * mat[k * nCols + c];
5635d3d1a6afSToby Isaac                 }
5636d3d1a6afSToby Isaac               }
5637d3d1a6afSToby Isaac             }
5638d3d1a6afSToby Isaac           }
5639d3d1a6afSToby Isaac           else {
5640d3d1a6afSToby Isaac             /* copy this column as is */
5641d3d1a6afSToby Isaac             for (r = 0; r < numIndices; r++) {
5642d3d1a6afSToby Isaac               for (c = 0; c < dof; c++) {
5643d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5644d3d1a6afSToby Isaac               }
5645d3d1a6afSToby Isaac             }
5646d3d1a6afSToby Isaac           }
5647d3d1a6afSToby Isaac           oldOff += dof;
5648d3d1a6afSToby Isaac         }
5649d3d1a6afSToby Isaac       }
5650d3d1a6afSToby Isaac     }
5651d3d1a6afSToby Isaac     else {
5652d3d1a6afSToby Isaac       PetscInt oldOff = 0;
5653d3d1a6afSToby Isaac       for (p = 0; p < numPoints; p++) {
5654d3d1a6afSToby Isaac         PetscInt cStart = newPointOffsets[0][p];
5655d3d1a6afSToby Isaac         PetscInt b      = points[2 * p];
5656d3d1a6afSToby Isaac         PetscInt c, r, k;
5657d3d1a6afSToby Isaac         PetscInt dof;
5658d3d1a6afSToby Isaac 
5659d3d1a6afSToby Isaac         ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
56604b2f2278SToby Isaac         if (!dof) {
56614b2f2278SToby Isaac           continue;
56624b2f2278SToby Isaac         }
5663d3d1a6afSToby Isaac         if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5664d3d1a6afSToby Isaac           PetscInt nCols         = newPointOffsets[0][p+1]-cStart;
5665d3d1a6afSToby Isaac           const PetscScalar *mat = pointMat[0] + pointMatOffsets[0][p];
5666d3d1a6afSToby Isaac 
5667d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5668d3d1a6afSToby Isaac             for (c = 0; c < nCols; c++) {
5669d3d1a6afSToby Isaac               for (k = 0; k < dof; k++) {
5670d3d1a6afSToby Isaac                 tmpValues[r * newNumIndices + cStart + c] += mat[k * nCols + c] * values[r * numIndices + oldOff + k];
5671d3d1a6afSToby Isaac               }
5672d3d1a6afSToby Isaac             }
5673d3d1a6afSToby Isaac           }
5674d3d1a6afSToby Isaac         }
5675d3d1a6afSToby Isaac         else {
5676d3d1a6afSToby Isaac           /* copy this column as is */
5677d3d1a6afSToby Isaac           for (r = 0; r < numIndices; r++) {
5678d3d1a6afSToby Isaac             for (c = 0; c < dof; c++) {
5679d3d1a6afSToby Isaac               tmpValues[r * newNumIndices + cStart + c] = values[r * numIndices + oldOff + c];
5680d3d1a6afSToby Isaac             }
5681d3d1a6afSToby Isaac           }
5682d3d1a6afSToby Isaac         }
5683d3d1a6afSToby Isaac         oldOff += dof;
5684d3d1a6afSToby Isaac       }
5685d3d1a6afSToby Isaac     }
5686d3d1a6afSToby Isaac 
56876ecaa68aSToby Isaac     if (multiplyLeft) {
568869291d52SBarry Smith       ierr = DMGetWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
5689580bdb30SBarry Smith       ierr = PetscArrayzero(newValues,newNumIndices*newNumIndices);CHKERRQ(ierr);
5690d3d1a6afSToby Isaac       /* multiply constraints transpose on the left */
5691d3d1a6afSToby Isaac       if (numFields) {
5692d3d1a6afSToby Isaac         for (f = 0; f < numFields; f++) {
5693d3d1a6afSToby Isaac           PetscInt oldOff = offsets[f];
5694d3d1a6afSToby Isaac 
5695d3d1a6afSToby Isaac           for (p = 0; p < numPoints; p++) {
5696d3d1a6afSToby Isaac             PetscInt rStart = newPointOffsets[f][p];
5697d3d1a6afSToby Isaac             PetscInt b      = points[2 * p];
5698d3d1a6afSToby Isaac             PetscInt c, r, k;
5699d3d1a6afSToby Isaac             PetscInt dof;
5700d3d1a6afSToby Isaac 
5701d3d1a6afSToby Isaac             ierr = PetscSectionGetFieldDof(section,b,f,&dof);CHKERRQ(ierr);
5702d3d1a6afSToby Isaac             if (pointMatOffsets[f][p] < pointMatOffsets[f][p + 1]) {
5703d3d1a6afSToby Isaac               PetscInt nRows                        = newPointOffsets[f][p+1]-rStart;
5704d3d1a6afSToby Isaac               const PetscScalar *PETSC_RESTRICT mat = pointMat[f] + pointMatOffsets[f][p];
5705d3d1a6afSToby Isaac 
5706d3d1a6afSToby Isaac               for (r = 0; r < nRows; r++) {
5707d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5708d3d1a6afSToby Isaac                   for (k = 0; k < dof; k++) {
5709d3d1a6afSToby Isaac                     newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5710d3d1a6afSToby Isaac                   }
5711d3d1a6afSToby Isaac                 }
5712d3d1a6afSToby Isaac               }
5713d3d1a6afSToby Isaac             }
5714d3d1a6afSToby Isaac             else {
5715d3d1a6afSToby Isaac               /* copy this row as is */
5716d3d1a6afSToby Isaac               for (r = 0; r < dof; r++) {
5717d3d1a6afSToby Isaac                 for (c = 0; c < newNumIndices; c++) {
5718d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5719d3d1a6afSToby Isaac                 }
5720d3d1a6afSToby Isaac               }
5721d3d1a6afSToby Isaac             }
5722d3d1a6afSToby Isaac             oldOff += dof;
5723d3d1a6afSToby Isaac           }
5724d3d1a6afSToby Isaac         }
5725d3d1a6afSToby Isaac       }
5726d3d1a6afSToby Isaac       else {
5727d3d1a6afSToby Isaac         PetscInt oldOff = 0;
5728d3d1a6afSToby Isaac 
5729d3d1a6afSToby Isaac         for (p = 0; p < numPoints; p++) {
5730d3d1a6afSToby Isaac           PetscInt rStart = newPointOffsets[0][p];
5731d3d1a6afSToby Isaac           PetscInt b      = points[2 * p];
5732d3d1a6afSToby Isaac           PetscInt c, r, k;
5733d3d1a6afSToby Isaac           PetscInt dof;
5734d3d1a6afSToby Isaac 
5735d3d1a6afSToby Isaac           ierr = PetscSectionGetDof(section,b,&dof);CHKERRQ(ierr);
5736d3d1a6afSToby Isaac           if (pointMatOffsets[0][p] < pointMatOffsets[0][p + 1]) {
5737d3d1a6afSToby Isaac             PetscInt nRows                        = newPointOffsets[0][p+1]-rStart;
5738d3d1a6afSToby Isaac             const PetscScalar *PETSC_RESTRICT mat = pointMat[0] + pointMatOffsets[0][p];
5739d3d1a6afSToby Isaac 
5740d3d1a6afSToby Isaac             for (r = 0; r < nRows; r++) {
5741d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5742d3d1a6afSToby Isaac                 for (k = 0; k < dof; k++) {
5743d3d1a6afSToby Isaac                   newValues[(rStart + r) * newNumIndices + c] += mat[k * nRows + r] * tmpValues[(oldOff + k) * newNumIndices + c];
5744d3d1a6afSToby Isaac                 }
5745d3d1a6afSToby Isaac               }
5746d3d1a6afSToby Isaac             }
5747d3d1a6afSToby Isaac           }
5748d3d1a6afSToby Isaac           else {
5749d3d1a6afSToby Isaac             /* copy this row as is */
57509fc93327SToby Isaac             for (r = 0; r < dof; r++) {
5751d3d1a6afSToby Isaac               for (c = 0; c < newNumIndices; c++) {
5752d3d1a6afSToby Isaac                 newValues[(rStart + r) * newNumIndices + c] = tmpValues[(oldOff + r) * newNumIndices + c];
5753d3d1a6afSToby Isaac               }
5754d3d1a6afSToby Isaac             }
5755d3d1a6afSToby Isaac           }
5756d3d1a6afSToby Isaac           oldOff += dof;
5757d3d1a6afSToby Isaac         }
5758d3d1a6afSToby Isaac       }
5759d3d1a6afSToby Isaac 
576069291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,newNumIndices*numIndices,MPIU_SCALAR,&tmpValues);CHKERRQ(ierr);
57616ecaa68aSToby Isaac     }
57626ecaa68aSToby Isaac     else {
57636ecaa68aSToby Isaac       newValues = tmpValues;
57646ecaa68aSToby Isaac     }
57656ecaa68aSToby Isaac   }
57666ecaa68aSToby Isaac 
5767d3d1a6afSToby Isaac   /* clean up */
576869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxDof,MPIU_INT,&indices);CHKERRQ(ierr);
576969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,maxAnchor*maxDof,MPIU_INT,&newIndices);CHKERRQ(ierr);
57706ecaa68aSToby Isaac 
5771d3d1a6afSToby Isaac   if (numFields) {
5772d3d1a6afSToby Isaac     for (f = 0; f < numFields; f++) {
577369291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,pointMatOffsets[f][numPoints],MPIU_SCALAR,&pointMat[f]);CHKERRQ(ierr);
577469291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[f]);CHKERRQ(ierr);
577569291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[f]);CHKERRQ(ierr);
5776d3d1a6afSToby Isaac     }
5777d3d1a6afSToby Isaac   }
5778d3d1a6afSToby Isaac   else {
577969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,pointMatOffsets[0][numPoints],MPIU_SCALAR,&pointMat[0]);CHKERRQ(ierr);
578069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&pointMatOffsets[0]);CHKERRQ(ierr);
578169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,numPoints+1,MPIU_INT,&newPointOffsets[0]);CHKERRQ(ierr);
5782d3d1a6afSToby Isaac   }
5783d3d1a6afSToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
5784d3d1a6afSToby Isaac 
5785d3d1a6afSToby Isaac   /* output */
57866ecaa68aSToby Isaac   if (outPoints) {
5787d3d1a6afSToby Isaac     *outPoints = newPoints;
57886ecaa68aSToby Isaac   }
57896ecaa68aSToby Isaac   else {
579069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
57916ecaa68aSToby Isaac   }
579231620726SToby Isaac   if (outValues) {
5793d3d1a6afSToby Isaac     *outValues = newValues;
57946ecaa68aSToby Isaac   }
57956ecaa68aSToby Isaac   for (f = 0; f <= numFields; f++) {
5796d3d1a6afSToby Isaac     offsets[f] = newOffsets[f];
5797d3d1a6afSToby Isaac   }
5798d3d1a6afSToby Isaac   PetscFunctionReturn(0);
5799d3d1a6afSToby Isaac }
5800d3d1a6afSToby Isaac 
58017cd05799SMatthew G. Knepley /*@C
58022cdc749cSStefano Zampini   DMPlexGetClosureIndices - Get the global indices for all local points in the closure of the given point
58037cd05799SMatthew G. Knepley 
58047cd05799SMatthew G. Knepley   Not collective
58057cd05799SMatthew G. Knepley 
58067cd05799SMatthew G. Knepley   Input Parameters:
58077cd05799SMatthew G. Knepley + dm - The DM
58082cdc749cSStefano Zampini . section - The section describing the local layout
58092cdc749cSStefano Zampini . globalSection - The section describing the parallel layout
58107cd05799SMatthew G. Knepley - point - The mesh point
58117cd05799SMatthew G. Knepley 
58127cd05799SMatthew G. Knepley   Output parameters:
58137cd05799SMatthew G. Knepley + numIndices - The number of indices
58147cd05799SMatthew G. Knepley . indices - The indices
58157cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
58167cd05799SMatthew G. Knepley 
58177cd05799SMatthew G. Knepley   Note: Must call DMPlexRestoreClosureIndices() to free allocated memory
58187cd05799SMatthew G. Knepley 
58197cd05799SMatthew G. Knepley   Level: advanced
58207cd05799SMatthew G. Knepley 
58217cd05799SMatthew G. Knepley .seealso DMPlexRestoreClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
58227cd05799SMatthew G. Knepley @*/
58236ecaa68aSToby Isaac PetscErrorCode DMPlexGetClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices, PetscInt *outOffsets)
58247773e69fSMatthew G. Knepley {
58257773e69fSMatthew G. Knepley   PetscSection    clSection;
58267773e69fSMatthew G. Knepley   IS              clPoints;
582705586334SMatthew G. Knepley   const PetscInt *clp, *clperm;
58284acb8e1eSToby Isaac   const PetscInt  **perms[32] = {NULL};
58297773e69fSMatthew G. Knepley   PetscInt       *points = NULL, *pointsNew;
58307773e69fSMatthew G. Knepley   PetscInt        numPoints, numPointsNew;
58317773e69fSMatthew G. Knepley   PetscInt        offsets[32];
58327773e69fSMatthew G. Knepley   PetscInt        Nf, Nind, NindNew, off, globalOff, f, p;
58337773e69fSMatthew G. Knepley   PetscErrorCode  ierr;
58347773e69fSMatthew G. Knepley 
58357773e69fSMatthew G. Knepley   PetscFunctionBegin;
58367773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58377773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
58387773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
58397773e69fSMatthew G. Knepley   if (numIndices) PetscValidPointer(numIndices, 4);
58407773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
58417773e69fSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
58427773e69fSMatthew G. Knepley   if (Nf > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", Nf);
5843580bdb30SBarry Smith   ierr = PetscArrayzero(offsets, 32);CHKERRQ(ierr);
58447773e69fSMatthew G. Knepley   /* Get points in closure */
5845923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
584605586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
58477773e69fSMatthew G. Knepley   /* Get number of indices and indices per field */
58487773e69fSMatthew G. Knepley   for (p = 0, Nind = 0; p < numPoints*2; p += 2) {
58497773e69fSMatthew G. Knepley     PetscInt dof, fdof;
58507773e69fSMatthew G. Knepley 
58517773e69fSMatthew G. Knepley     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
58527773e69fSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
58537773e69fSMatthew G. Knepley       ierr = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
58547773e69fSMatthew G. Knepley       offsets[f+1] += fdof;
58557773e69fSMatthew G. Knepley     }
58567773e69fSMatthew G. Knepley     Nind += dof;
58577773e69fSMatthew G. Knepley   }
58587773e69fSMatthew G. Knepley   for (f = 1; f < Nf; ++f) offsets[f+1] += offsets[f];
58598ccfff9cSToby Isaac   if (Nf && offsets[Nf] != Nind) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[Nf], Nind);
58604acb8e1eSToby Isaac   if (!Nf) offsets[1] = Nind;
58614acb8e1eSToby Isaac   /* Get dual space symmetries */
58624acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
58634acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
58644acb8e1eSToby Isaac     else    {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
58654acb8e1eSToby Isaac   }
58667773e69fSMatthew G. Knepley   /* Correct for hanging node constraints */
58677773e69fSMatthew G. Knepley   {
58684acb8e1eSToby Isaac     ierr = DMPlexAnchorsModifyMat(dm, section, numPoints, Nind, points, perms, NULL, &numPointsNew, &NindNew, &pointsNew, NULL, offsets, PETSC_TRUE);CHKERRQ(ierr);
58697773e69fSMatthew G. Knepley     if (numPointsNew) {
58704acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
58714acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
58724acb8e1eSToby Isaac         else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
58734acb8e1eSToby Isaac       }
58744acb8e1eSToby Isaac       for (f = 0; f < PetscMax(1,Nf); f++) {
58754acb8e1eSToby Isaac         if (Nf) {ierr = PetscSectionGetFieldPointSyms(section,f,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
58764acb8e1eSToby Isaac         else    {ierr = PetscSectionGetPointSyms(section,numPointsNew,pointsNew,&perms[f],NULL);CHKERRQ(ierr);}
58774acb8e1eSToby Isaac       }
5878923c78e0SToby Isaac       ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
58797773e69fSMatthew G. Knepley       numPoints = numPointsNew;
58807773e69fSMatthew G. Knepley       Nind      = NindNew;
58817773e69fSMatthew G. Knepley       points    = pointsNew;
58827773e69fSMatthew G. Knepley     }
58837773e69fSMatthew G. Knepley   }
58847773e69fSMatthew G. Knepley   /* Calculate indices */
588569291d52SBarry Smith   ierr = DMGetWorkArray(dm, Nind, MPIU_INT, indices);CHKERRQ(ierr);
58867773e69fSMatthew G. Knepley   if (Nf) {
58876ecaa68aSToby Isaac     if (outOffsets) {
58886ecaa68aSToby Isaac       PetscInt f;
58896ecaa68aSToby Isaac 
589046bdb399SToby Isaac       for (f = 0; f <= Nf; f++) {
58916ecaa68aSToby Isaac         outOffsets[f] = offsets[f];
58926ecaa68aSToby Isaac       }
58936ecaa68aSToby Isaac     }
58944acb8e1eSToby Isaac     for (p = 0; p < numPoints; p++) {
58954acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
58962cdc749cSStefano Zampini       ierr = DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, clperm, *indices);CHKERRQ(ierr);
58977773e69fSMatthew G. Knepley     }
58987773e69fSMatthew G. Knepley   } else {
58994acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
59004acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
59014acb8e1eSToby Isaac 
59024acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
59032cdc749cSStefano Zampini       ierr = DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, clperm, *indices);CHKERRQ(ierr);
59047773e69fSMatthew G. Knepley     }
59057773e69fSMatthew G. Knepley   }
59067773e69fSMatthew G. Knepley   /* Cleanup points */
59074acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,Nf); f++) {
59084acb8e1eSToby Isaac     if (Nf) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
59094acb8e1eSToby Isaac     else    {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],NULL);CHKERRQ(ierr);}
59104acb8e1eSToby Isaac   }
59117773e69fSMatthew G. Knepley   if (numPointsNew) {
591269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2*numPointsNew, MPIU_INT, &pointsNew);CHKERRQ(ierr);
59137773e69fSMatthew G. Knepley   } else {
5914923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
59157773e69fSMatthew G. Knepley   }
59167773e69fSMatthew G. Knepley   if (numIndices) *numIndices = Nind;
59177773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
59187773e69fSMatthew G. Knepley }
59197773e69fSMatthew G. Knepley 
59207cd05799SMatthew G. Knepley /*@C
59217cd05799SMatthew G. Knepley   DMPlexRestoreClosureIndices - Restore the indices in a vector v for all points in the closure of the given point
59227cd05799SMatthew G. Knepley 
59237cd05799SMatthew G. Knepley   Not collective
59247cd05799SMatthew G. Knepley 
59257cd05799SMatthew G. Knepley   Input Parameters:
59267cd05799SMatthew G. Knepley + dm - The DM
59277cd05799SMatthew G. Knepley . section - The section describing the layout in v, or NULL to use the default section
59287cd05799SMatthew G. Knepley . globalSection - The section describing the parallel layout in v, or NULL to use the default section
59297cd05799SMatthew G. Knepley . point - The mesh point
59307cd05799SMatthew G. Knepley . numIndices - The number of indices
59317cd05799SMatthew G. Knepley . indices - The indices
59327cd05799SMatthew G. Knepley - outOffsets - Field offset if not NULL
59337cd05799SMatthew G. Knepley 
59347cd05799SMatthew G. Knepley   Level: advanced
59357cd05799SMatthew G. Knepley 
59367cd05799SMatthew G. Knepley .seealso DMPlexGetClosureIndices(), DMPlexVecGetClosure(), DMPlexMatSetClosure()
59377cd05799SMatthew G. Knepley @*/
593846bdb399SToby Isaac PetscErrorCode DMPlexRestoreClosureIndices(DM dm, PetscSection section, PetscSection globalSection, PetscInt point, PetscInt *numIndices, PetscInt **indices,PetscInt *outOffsets)
59397773e69fSMatthew G. Knepley {
59407773e69fSMatthew G. Knepley   PetscErrorCode ierr;
59417773e69fSMatthew G. Knepley 
59427773e69fSMatthew G. Knepley   PetscFunctionBegin;
59437773e69fSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
59447773e69fSMatthew G. Knepley   PetscValidPointer(indices, 5);
594569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 0, MPIU_INT, indices);CHKERRQ(ierr);
59467773e69fSMatthew G. Knepley   PetscFunctionReturn(0);
59477773e69fSMatthew G. Knepley }
59487773e69fSMatthew G. Knepley 
59497f5d1fdeSMatthew G. Knepley /*@C
59507f5d1fdeSMatthew G. Knepley   DMPlexMatSetClosure - Set an array of the values on the closure of 'point'
59517f5d1fdeSMatthew G. Knepley 
59527f5d1fdeSMatthew G. Knepley   Not collective
59537f5d1fdeSMatthew G. Knepley 
59547f5d1fdeSMatthew G. Knepley   Input Parameters:
59557f5d1fdeSMatthew G. Knepley + dm - The DM
5956ebd6d717SJed Brown . section - The section describing the layout in v, or NULL to use the default section
5957ebd6d717SJed Brown . globalSection - The section describing the layout in v, or NULL to use the default global section
59587f5d1fdeSMatthew G. Knepley . A - The matrix
5959eaf898f9SPatrick Sanan . point - The point in the DM
59607f5d1fdeSMatthew G. Knepley . values - The array of values
59617f5d1fdeSMatthew G. Knepley - mode - The insert mode, where INSERT_ALL_VALUES and ADD_ALL_VALUES also overwrite boundary conditions
59627f5d1fdeSMatthew G. Knepley 
59637f5d1fdeSMatthew G. Knepley   Fortran Notes:
59647f5d1fdeSMatthew G. Knepley   This routine is only available in Fortran 90, and you must include petsc.h90 in your code.
59657f5d1fdeSMatthew G. Knepley 
59667f5d1fdeSMatthew G. Knepley   Level: intermediate
59677f5d1fdeSMatthew G. Knepley 
59687f5d1fdeSMatthew G. Knepley .seealso DMPlexVecGetClosure(), DMPlexVecSetClosure()
59697f5d1fdeSMatthew G. Knepley @*/
59707c1f9639SMatthew G Knepley PetscErrorCode DMPlexMatSetClosure(DM dm, PetscSection section, PetscSection globalSection, Mat A, PetscInt point, const PetscScalar values[], InsertMode mode)
5971552f7358SJed Brown {
5972552f7358SJed Brown   DM_Plex            *mesh   = (DM_Plex*) dm->data;
59731b406b76SMatthew G. Knepley   PetscSection        clSection;
59741b406b76SMatthew G. Knepley   IS                  clPoints;
5975d3d1a6afSToby Isaac   PetscInt           *points = NULL, *newPoints;
597605586334SMatthew G. Knepley   const PetscInt     *clp, *clperm;
5977552f7358SJed Brown   PetscInt           *indices;
5978552f7358SJed Brown   PetscInt            offsets[32];
59794acb8e1eSToby Isaac   const PetscInt    **perms[32] = {NULL};
59804acb8e1eSToby Isaac   const PetscScalar **flips[32] = {NULL};
5981923c78e0SToby Isaac   PetscInt            numFields, numPoints, newNumPoints, numIndices, newNumIndices, dof, off, globalOff, p, f;
59824acb8e1eSToby Isaac   PetscScalar        *valCopy = NULL;
5983d3d1a6afSToby Isaac   PetscScalar        *newValues;
5984552f7358SJed Brown   PetscErrorCode      ierr;
5985552f7358SJed Brown 
5986552f7358SJed Brown   PetscFunctionBegin;
5987552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
598892fd8e1eSJed Brown   if (!section) {ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);}
59893dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(section, PETSC_SECTION_CLASSID, 2);
5990e87a4003SBarry Smith   if (!globalSection) {ierr = DMGetGlobalSection(dm, &globalSection);CHKERRQ(ierr);}
59913dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(globalSection, PETSC_SECTION_CLASSID, 3);
59923dc93601SMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 4);
5993552f7358SJed Brown   ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);
599482f516ccSBarry Smith   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
5995580bdb30SBarry Smith   ierr = PetscArrayzero(offsets, 32);CHKERRQ(ierr);
599605586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(section, (PetscObject) dm, NULL, &clperm);CHKERRQ(ierr);
5997923c78e0SToby Isaac   ierr = DMPlexGetCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
5998552f7358SJed Brown   for (p = 0, numIndices = 0; p < numPoints*2; p += 2) {
5999552f7358SJed Brown     PetscInt fdof;
6000552f7358SJed Brown 
6001552f7358SJed Brown     ierr = PetscSectionGetDof(section, points[p], &dof);CHKERRQ(ierr);
6002552f7358SJed Brown     for (f = 0; f < numFields; ++f) {
6003552f7358SJed Brown       ierr          = PetscSectionGetFieldDof(section, points[p], f, &fdof);CHKERRQ(ierr);
6004552f7358SJed Brown       offsets[f+1] += fdof;
6005552f7358SJed Brown     }
6006552f7358SJed Brown     numIndices += dof;
6007552f7358SJed Brown   }
60080d644c17SKarl Rupp   for (f = 1; f < numFields; ++f) offsets[f+1] += offsets[f];
60090d644c17SKarl Rupp 
60108ccfff9cSToby Isaac   if (numFields && offsets[numFields] != numIndices) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", offsets[numFields], numIndices);
60114acb8e1eSToby Isaac   /* Get symmetries */
60124acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
60134acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
60144acb8e1eSToby Isaac     else           {ierr = PetscSectionGetPointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
60154acb8e1eSToby Isaac     if (values && flips[f]) { /* may need to apply sign changes to the element matrix */
60164acb8e1eSToby Isaac       PetscInt foffset = offsets[f];
60174acb8e1eSToby Isaac 
60184acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
60194acb8e1eSToby Isaac         PetscInt point          = points[2*p], fdof;
60204acb8e1eSToby Isaac         const PetscScalar *flip = flips[f] ? flips[f][p] : NULL;
60214acb8e1eSToby Isaac 
60224acb8e1eSToby Isaac         if (!numFields) {
60234acb8e1eSToby Isaac           ierr = PetscSectionGetDof(section,point,&fdof);CHKERRQ(ierr);
60244acb8e1eSToby Isaac         } else {
60254acb8e1eSToby Isaac           ierr = PetscSectionGetFieldDof(section,point,f,&fdof);CHKERRQ(ierr);
60264acb8e1eSToby Isaac         }
60274acb8e1eSToby Isaac         if (flip) {
60284acb8e1eSToby Isaac           PetscInt i, j, k;
60294acb8e1eSToby Isaac 
60304acb8e1eSToby Isaac           if (!valCopy) {
603169291d52SBarry Smith             ierr = DMGetWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
60324acb8e1eSToby Isaac             for (j = 0; j < numIndices * numIndices; j++) valCopy[j] = values[j];
60334acb8e1eSToby Isaac             values = valCopy;
60344acb8e1eSToby Isaac           }
60354acb8e1eSToby Isaac           for (i = 0; i < fdof; i++) {
6036775f7be2SToby Isaac             PetscScalar fval = flip[i];
60374acb8e1eSToby Isaac 
60384acb8e1eSToby Isaac             for (k = 0; k < numIndices; k++) {
60394acb8e1eSToby Isaac               valCopy[numIndices * (foffset + i) + k] *= fval;
60404acb8e1eSToby Isaac               valCopy[numIndices * k + (foffset + i)] *= fval;
60414acb8e1eSToby Isaac             }
60424acb8e1eSToby Isaac           }
60434acb8e1eSToby Isaac         }
60444acb8e1eSToby Isaac         foffset += fdof;
60454acb8e1eSToby Isaac       }
60464acb8e1eSToby Isaac     }
60474acb8e1eSToby Isaac   }
60484acb8e1eSToby Isaac   ierr = DMPlexAnchorsModifyMat(dm,section,numPoints,numIndices,points,perms,values,&newNumPoints,&newNumIndices,&newPoints,&newValues,offsets,PETSC_TRUE);CHKERRQ(ierr);
6049d3d1a6afSToby Isaac   if (newNumPoints) {
60504acb8e1eSToby Isaac     if (valCopy) {
605169291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
60524acb8e1eSToby Isaac     }
60534acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
60544acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
60554acb8e1eSToby Isaac       else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
60564acb8e1eSToby Isaac     }
60574acb8e1eSToby Isaac     for (f = 0; f < PetscMax(1,numFields); f++) {
60584acb8e1eSToby Isaac       if (numFields) {ierr = PetscSectionGetFieldPointSyms(section,f,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
60594acb8e1eSToby Isaac       else           {ierr = PetscSectionGetPointSyms(section,newNumPoints,newPoints,&perms[f],&flips[f]);CHKERRQ(ierr);}
60604acb8e1eSToby Isaac     }
6061923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
6062d3d1a6afSToby Isaac     numPoints  = newNumPoints;
6063d3d1a6afSToby Isaac     numIndices = newNumIndices;
6064d3d1a6afSToby Isaac     points     = newPoints;
6065d3d1a6afSToby Isaac     values     = newValues;
6066d3d1a6afSToby Isaac   }
606769291d52SBarry Smith   ierr = DMGetWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
6068552f7358SJed Brown   if (numFields) {
60697e29afd2SMatthew G. Knepley     PetscBool useFieldOffsets;
60707e29afd2SMatthew G. Knepley 
60717e29afd2SMatthew G. Knepley     ierr = PetscSectionGetUseFieldOffsets(globalSection, &useFieldOffsets);CHKERRQ(ierr);
60727e29afd2SMatthew G. Knepley     if (useFieldOffsets) {
60737e29afd2SMatthew G. Knepley       for (p = 0; p < numPoints; p++) {
607405586334SMatthew G. Knepley         DMPlexGetIndicesPointFieldsSplit_Internal(section, globalSection, points[2*p], offsets, PETSC_FALSE, perms, p, clperm, indices);
60757e29afd2SMatthew G. Knepley       }
60767e29afd2SMatthew G. Knepley     } else {
60774acb8e1eSToby Isaac       for (p = 0; p < numPoints; p++) {
60784acb8e1eSToby Isaac         ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
607905586334SMatthew G. Knepley         DMPlexGetIndicesPointFields_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, offsets, PETSC_FALSE, perms, p, clperm, indices);
6080552f7358SJed Brown       }
60817e29afd2SMatthew G. Knepley     }
6082552f7358SJed Brown   } else {
60834acb8e1eSToby Isaac     for (p = 0, off = 0; p < numPoints; p++) {
60844acb8e1eSToby Isaac       const PetscInt *perm = perms[0] ? perms[0][p] : NULL;
60854acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalSection, points[2*p], &globalOff);CHKERRQ(ierr);
608605586334SMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(section, points[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, clperm, indices);
6087552f7358SJed Brown     }
6088552f7358SJed Brown   }
6089b0ecff45SMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr);}
6090552f7358SJed Brown   ierr = MatSetValues(A, numIndices, indices, numIndices, indices, values, mode);
6091ab1d0545SMatthew G. Knepley   if (mesh->printFEM > 1) {
6092ab1d0545SMatthew G. Knepley     PetscInt i;
6093ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "  Indices:");CHKERRQ(ierr);
60948ccfff9cSToby Isaac     for (i = 0; i < numIndices; ++i) {ierr = PetscPrintf(PETSC_COMM_SELF, " %D", indices[i]);CHKERRQ(ierr);}
6095ab1d0545SMatthew G. Knepley     ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
6096ab1d0545SMatthew G. Knepley   }
6097552f7358SJed Brown   if (ierr) {
6098552f7358SJed Brown     PetscMPIInt    rank;
6099552f7358SJed Brown     PetscErrorCode ierr2;
6100552f7358SJed Brown 
610182f516ccSBarry Smith     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
6102e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6103b0ecff45SMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numIndices, indices, 0, NULL, values);CHKERRQ(ierr2);
610469291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr2);
6105552f7358SJed Brown     CHKERRQ(ierr);
6106552f7358SJed Brown   }
61074acb8e1eSToby Isaac   for (f = 0; f < PetscMax(1,numFields); f++) {
61084acb8e1eSToby Isaac     if (numFields) {ierr = PetscSectionRestoreFieldPointSyms(section,f,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
61094acb8e1eSToby Isaac     else           {ierr = PetscSectionRestorePointSyms(section,numPoints,points,&perms[f],&flips[f]);CHKERRQ(ierr);}
61104acb8e1eSToby Isaac   }
6111d3d1a6afSToby Isaac   if (newNumPoints) {
611269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,newNumIndices*newNumIndices,MPIU_SCALAR,&newValues);CHKERRQ(ierr);
611369291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*newNumPoints,MPIU_INT,&newPoints);CHKERRQ(ierr);
6114d3d1a6afSToby Isaac   }
6115d3d1a6afSToby Isaac   else {
61164acb8e1eSToby Isaac     if (valCopy) {
611769291d52SBarry Smith       ierr = DMRestoreWorkArray(dm,numIndices*numIndices,MPIU_SCALAR,&valCopy);CHKERRQ(ierr);
61184acb8e1eSToby Isaac     }
6119923c78e0SToby Isaac     ierr = DMPlexRestoreCompressedClosure(dm,section,point,&numPoints,&points,&clSection,&clPoints,&clp);CHKERRQ(ierr);
6120d3d1a6afSToby Isaac   }
612169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, numIndices, MPIU_INT, &indices);CHKERRQ(ierr);
6122552f7358SJed Brown   PetscFunctionReturn(0);
6123552f7358SJed Brown }
6124552f7358SJed Brown 
6125de41b84cSMatthew 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)
6126de41b84cSMatthew G. Knepley {
6127de41b84cSMatthew G. Knepley   DM_Plex        *mesh   = (DM_Plex*) dmf->data;
6128de41b84cSMatthew G. Knepley   PetscInt       *fpoints = NULL, *ftotpoints = NULL;
6129de41b84cSMatthew G. Knepley   PetscInt       *cpoints = NULL;
6130de41b84cSMatthew G. Knepley   PetscInt       *findices, *cindices;
613105586334SMatthew G. Knepley   const PetscInt *fclperm, *cclperm;
6132de41b84cSMatthew G. Knepley   PetscInt        foffsets[32], coffsets[32];
6133de41b84cSMatthew G. Knepley   CellRefiner     cellRefiner;
61344ca5e9f5SMatthew G. Knepley   PetscInt        numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
6135de41b84cSMatthew G. Knepley   PetscErrorCode  ierr;
6136de41b84cSMatthew G. Knepley 
6137de41b84cSMatthew G. Knepley   PetscFunctionBegin;
6138de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
6139de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
614092fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
6141de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
614292fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
6143de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
6144e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
6145de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
6146e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
6147de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
6148de41b84cSMatthew G. Knepley   PetscValidHeaderSpecific(A, MAT_CLASSID, 7);
6149de41b84cSMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
6150de41b84cSMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6151580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
6152580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
615305586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(fsection, (PetscObject) dmf, NULL, &fclperm);CHKERRQ(ierr);
615405586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(csection, (PetscObject) dmc, NULL, &cclperm);CHKERRQ(ierr);
6155de41b84cSMatthew G. Knepley   /* Column indices */
6156de41b84cSMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
61574ca5e9f5SMatthew G. Knepley   maxFPoints = numCPoints;
6158de41b84cSMatthew G. Knepley   /* Compress out points not in the section */
6159de41b84cSMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
6160de41b84cSMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
6161de41b84cSMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
6162de41b84cSMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
6163de41b84cSMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
6164de41b84cSMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
6165de41b84cSMatthew G. Knepley       ++q;
6166de41b84cSMatthew G. Knepley     }
6167de41b84cSMatthew G. Knepley   }
6168de41b84cSMatthew G. Knepley   numCPoints = q;
6169de41b84cSMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
6170de41b84cSMatthew G. Knepley     PetscInt fdof;
6171de41b84cSMatthew G. Knepley 
6172de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
61734ca5e9f5SMatthew G. Knepley     if (!dof) continue;
6174de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
6175de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
6176de41b84cSMatthew G. Knepley       coffsets[f+1] += fdof;
6177de41b84cSMatthew G. Knepley     }
6178de41b84cSMatthew G. Knepley     numCIndices += dof;
6179de41b84cSMatthew G. Knepley   }
6180de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
6181de41b84cSMatthew G. Knepley   /* Row indices */
6182de41b84cSMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
6183de41b84cSMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
618469291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
6185de41b84cSMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
6186de41b84cSMatthew G. Knepley     /* TODO Map from coarse to fine cells */
6187de41b84cSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
6188de41b84cSMatthew G. Knepley     /* Compress out points not in the section */
6189de41b84cSMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
6190de41b84cSMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
6191de41b84cSMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
61924ca5e9f5SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
61934ca5e9f5SMatthew G. Knepley         if (!dof) continue;
61944ca5e9f5SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
61954ca5e9f5SMatthew G. Knepley         if (s < q) continue;
6196de41b84cSMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
6197de41b84cSMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
6198de41b84cSMatthew G. Knepley         ++q;
6199de41b84cSMatthew G. Knepley       }
6200de41b84cSMatthew G. Knepley     }
6201de41b84cSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
6202de41b84cSMatthew G. Knepley   }
6203de41b84cSMatthew G. Knepley   numFPoints = q;
6204de41b84cSMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
6205de41b84cSMatthew G. Knepley     PetscInt fdof;
6206de41b84cSMatthew G. Knepley 
6207de41b84cSMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
62084ca5e9f5SMatthew G. Knepley     if (!dof) continue;
6209de41b84cSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
6210de41b84cSMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
6211de41b84cSMatthew G. Knepley       foffsets[f+1] += fdof;
6212de41b84cSMatthew G. Knepley     }
6213de41b84cSMatthew G. Knepley     numFIndices += dof;
6214de41b84cSMatthew G. Knepley   }
6215de41b84cSMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
6216de41b84cSMatthew G. Knepley 
62178ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
62188ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
621969291d52SBarry Smith   ierr = DMGetWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
622069291d52SBarry Smith   ierr = DMGetWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
6221de41b84cSMatthew G. Knepley   if (numFields) {
62224acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
62234acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
62244acb8e1eSToby Isaac 
62254acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
62264acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
62274acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
6228de41b84cSMatthew G. Knepley     }
62294acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
62304acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
623105586334SMatthew G. Knepley       ierr = DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);CHKERRQ(ierr);
62324acb8e1eSToby Isaac     }
62334acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
62344acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
623505586334SMatthew G. Knepley       ierr = DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);CHKERRQ(ierr);
62364acb8e1eSToby Isaac     }
62374acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
62384acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
62394acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
6240de41b84cSMatthew G. Knepley     }
6241de41b84cSMatthew G. Knepley   } else {
62424acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
62434acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
62444acb8e1eSToby Isaac 
62454acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
62464acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
62474acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
62484acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
62494acb8e1eSToby Isaac 
62504acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
625105586334SMatthew G. Knepley       ierr = DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);CHKERRQ(ierr);
6252de41b84cSMatthew G. Knepley     }
62534acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
62544acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
62554acb8e1eSToby Isaac 
62564acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
625705586334SMatthew G. Knepley       ierr = DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);CHKERRQ(ierr);
6258de41b84cSMatthew G. Knepley     }
62594acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
62604acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
6261de41b84cSMatthew G. Knepley   }
6262de41b84cSMatthew G. Knepley   if (mesh->printSetValues) {ierr = DMPlexPrintMatSetValues(PETSC_VIEWER_STDOUT_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr);}
62634acb8e1eSToby Isaac   /* TODO: flips */
6264de41b84cSMatthew G. Knepley   ierr = MatSetValues(A, numFIndices, findices, numCIndices, cindices, values, mode);
6265de41b84cSMatthew G. Knepley   if (ierr) {
6266de41b84cSMatthew G. Knepley     PetscMPIInt    rank;
6267de41b84cSMatthew G. Knepley     PetscErrorCode ierr2;
6268de41b84cSMatthew G. Knepley 
6269de41b84cSMatthew G. Knepley     ierr2 = MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank);CHKERRQ(ierr2);
6270e4b003c7SBarry Smith     ierr2 = (*PetscErrorPrintf)("[%d]ERROR in DMPlexMatSetClosure\n", rank);CHKERRQ(ierr2);
6271de41b84cSMatthew G. Knepley     ierr2 = DMPlexPrintMatSetValues(PETSC_VIEWER_STDERR_SELF, A, point, numFIndices, findices, numCIndices, cindices, values);CHKERRQ(ierr2);
627269291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr2);
627369291d52SBarry Smith     ierr2 = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr2);
6274de41b84cSMatthew G. Knepley     CHKERRQ(ierr);
6275de41b84cSMatthew G. Knepley   }
627669291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
6277de41b84cSMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
627869291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numFIndices, MPIU_INT, &findices);CHKERRQ(ierr);
627969291d52SBarry Smith   ierr = DMRestoreWorkArray(dmc, numCIndices, MPIU_INT, &cindices);CHKERRQ(ierr);
6280de41b84cSMatthew G. Knepley   PetscFunctionReturn(0);
6281de41b84cSMatthew G. Knepley }
6282de41b84cSMatthew G. Knepley 
62837c927364SMatthew G. Knepley PetscErrorCode DMPlexMatGetClosureIndicesRefined(DM dmf, PetscSection fsection, PetscSection globalFSection, DM dmc, PetscSection csection, PetscSection globalCSection, PetscInt point, PetscInt cindices[], PetscInt findices[])
62847c927364SMatthew G. Knepley {
62857c927364SMatthew G. Knepley   PetscInt      *fpoints = NULL, *ftotpoints = NULL;
62867c927364SMatthew G. Knepley   PetscInt      *cpoints = NULL;
62877c927364SMatthew G. Knepley   PetscInt       foffsets[32], coffsets[32];
628805586334SMatthew G. Knepley   const PetscInt *fclperm, *cclperm;
62897c927364SMatthew G. Knepley   CellRefiner    cellRefiner;
62907c927364SMatthew G. Knepley   PetscInt       numFields, numSubcells, maxFPoints, numFPoints, numCPoints, numFIndices, numCIndices, dof, off, globalOff, pStart, pEnd, p, q, r, s, f;
62917c927364SMatthew G. Knepley   PetscErrorCode ierr;
62927c927364SMatthew G. Knepley 
62937c927364SMatthew G. Knepley   PetscFunctionBegin;
62947c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmf, DM_CLASSID, 1);
62957c927364SMatthew G. Knepley   PetscValidHeaderSpecific(dmc, DM_CLASSID, 4);
629692fd8e1eSJed Brown   if (!fsection) {ierr = DMGetLocalSection(dmf, &fsection);CHKERRQ(ierr);}
62977c927364SMatthew G. Knepley   PetscValidHeaderSpecific(fsection, PETSC_SECTION_CLASSID, 2);
629892fd8e1eSJed Brown   if (!csection) {ierr = DMGetLocalSection(dmc, &csection);CHKERRQ(ierr);}
62997c927364SMatthew G. Knepley   PetscValidHeaderSpecific(csection, PETSC_SECTION_CLASSID, 5);
6300e87a4003SBarry Smith   if (!globalFSection) {ierr = DMGetGlobalSection(dmf, &globalFSection);CHKERRQ(ierr);}
63017c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalFSection, PETSC_SECTION_CLASSID, 3);
6302e87a4003SBarry Smith   if (!globalCSection) {ierr = DMGetGlobalSection(dmc, &globalCSection);CHKERRQ(ierr);}
63037c927364SMatthew G. Knepley   PetscValidHeaderSpecific(globalCSection, PETSC_SECTION_CLASSID, 6);
63047c927364SMatthew G. Knepley   ierr = PetscSectionGetNumFields(fsection, &numFields);CHKERRQ(ierr);
63057c927364SMatthew G. Knepley   if (numFields > 31) SETERRQ1(PetscObjectComm((PetscObject)dmf), PETSC_ERR_ARG_OUTOFRANGE, "Number of fields %D limited to 31", numFields);
6306580bdb30SBarry Smith   ierr = PetscArrayzero(foffsets, 32);CHKERRQ(ierr);
6307580bdb30SBarry Smith   ierr = PetscArrayzero(coffsets, 32);CHKERRQ(ierr);
630805586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(fsection, (PetscObject) dmf, NULL, &fclperm);CHKERRQ(ierr);
630905586334SMatthew G. Knepley   ierr = PetscSectionGetClosureInversePermutation_Internal(csection, (PetscObject) dmc, NULL, &cclperm);CHKERRQ(ierr);
63107c927364SMatthew G. Knepley   /* Column indices */
63117c927364SMatthew G. Knepley   ierr = DMPlexGetTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
63127c927364SMatthew G. Knepley   maxFPoints = numCPoints;
63137c927364SMatthew G. Knepley   /* Compress out points not in the section */
63147c927364SMatthew G. Knepley   /*   TODO: Squeeze out points with 0 dof as well */
63157c927364SMatthew G. Knepley   ierr = PetscSectionGetChart(csection, &pStart, &pEnd);CHKERRQ(ierr);
63167c927364SMatthew G. Knepley   for (p = 0, q = 0; p < numCPoints*2; p += 2) {
63177c927364SMatthew G. Knepley     if ((cpoints[p] >= pStart) && (cpoints[p] < pEnd)) {
63187c927364SMatthew G. Knepley       cpoints[q*2]   = cpoints[p];
63197c927364SMatthew G. Knepley       cpoints[q*2+1] = cpoints[p+1];
63207c927364SMatthew G. Knepley       ++q;
63217c927364SMatthew G. Knepley     }
63227c927364SMatthew G. Knepley   }
63237c927364SMatthew G. Knepley   numCPoints = q;
63247c927364SMatthew G. Knepley   for (p = 0, numCIndices = 0; p < numCPoints*2; p += 2) {
63257c927364SMatthew G. Knepley     PetscInt fdof;
63267c927364SMatthew G. Knepley 
63277c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(csection, cpoints[p], &dof);CHKERRQ(ierr);
63287c927364SMatthew G. Knepley     if (!dof) continue;
63297c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
63307c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(csection, cpoints[p], f, &fdof);CHKERRQ(ierr);
63317c927364SMatthew G. Knepley       coffsets[f+1] += fdof;
63327c927364SMatthew G. Knepley     }
63337c927364SMatthew G. Knepley     numCIndices += dof;
63347c927364SMatthew G. Knepley   }
63357c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) coffsets[f+1] += coffsets[f];
63367c927364SMatthew G. Knepley   /* Row indices */
63377c927364SMatthew G. Knepley   ierr = DMPlexGetCellRefiner_Internal(dmc, &cellRefiner);CHKERRQ(ierr);
63387c927364SMatthew G. Knepley   ierr = CellRefinerGetAffineTransforms_Internal(cellRefiner, &numSubcells, NULL, NULL, NULL);CHKERRQ(ierr);
633969291d52SBarry Smith   ierr = DMGetWorkArray(dmf, maxFPoints*2*numSubcells, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
63407c927364SMatthew G. Knepley   for (r = 0, q = 0; r < numSubcells; ++r) {
63417c927364SMatthew G. Knepley     /* TODO Map from coarse to fine cells */
63427c927364SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dmf, point*numSubcells + r, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
63437c927364SMatthew G. Knepley     /* Compress out points not in the section */
63447c927364SMatthew G. Knepley     ierr = PetscSectionGetChart(fsection, &pStart, &pEnd);CHKERRQ(ierr);
63457c927364SMatthew G. Knepley     for (p = 0; p < numFPoints*2; p += 2) {
63467c927364SMatthew G. Knepley       if ((fpoints[p] >= pStart) && (fpoints[p] < pEnd)) {
63477c927364SMatthew G. Knepley         ierr = PetscSectionGetDof(fsection, fpoints[p], &dof);CHKERRQ(ierr);
63487c927364SMatthew G. Knepley         if (!dof) continue;
63497c927364SMatthew G. Knepley         for (s = 0; s < q; ++s) if (fpoints[p] == ftotpoints[s*2]) break;
63507c927364SMatthew G. Knepley         if (s < q) continue;
63517c927364SMatthew G. Knepley         ftotpoints[q*2]   = fpoints[p];
63527c927364SMatthew G. Knepley         ftotpoints[q*2+1] = fpoints[p+1];
63537c927364SMatthew G. Knepley         ++q;
63547c927364SMatthew G. Knepley       }
63557c927364SMatthew G. Knepley     }
63567c927364SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dmf, point, PETSC_TRUE, &numFPoints, &fpoints);CHKERRQ(ierr);
63577c927364SMatthew G. Knepley   }
63587c927364SMatthew G. Knepley   numFPoints = q;
63597c927364SMatthew G. Knepley   for (p = 0, numFIndices = 0; p < numFPoints*2; p += 2) {
63607c927364SMatthew G. Knepley     PetscInt fdof;
63617c927364SMatthew G. Knepley 
63627c927364SMatthew G. Knepley     ierr = PetscSectionGetDof(fsection, ftotpoints[p], &dof);CHKERRQ(ierr);
63637c927364SMatthew G. Knepley     if (!dof) continue;
63647c927364SMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
63657c927364SMatthew G. Knepley       ierr           = PetscSectionGetFieldDof(fsection, ftotpoints[p], f, &fdof);CHKERRQ(ierr);
63667c927364SMatthew G. Knepley       foffsets[f+1] += fdof;
63677c927364SMatthew G. Knepley     }
63687c927364SMatthew G. Knepley     numFIndices += dof;
63697c927364SMatthew G. Knepley   }
63707c927364SMatthew G. Knepley   for (f = 1; f < numFields; ++f) foffsets[f+1] += foffsets[f];
63717c927364SMatthew G. Knepley 
63728ccfff9cSToby Isaac   if (numFields && foffsets[numFields] != numFIndices) SETERRQ2(PetscObjectComm((PetscObject)dmf), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", foffsets[numFields], numFIndices);
63738ccfff9cSToby Isaac   if (numFields && coffsets[numFields] != numCIndices) SETERRQ2(PetscObjectComm((PetscObject)dmc), PETSC_ERR_PLIB, "Invalid size for closure %D should be %D", coffsets[numFields], numCIndices);
63747c927364SMatthew G. Knepley   if (numFields) {
63754acb8e1eSToby Isaac     const PetscInt **permsF[32] = {NULL};
63764acb8e1eSToby Isaac     const PetscInt **permsC[32] = {NULL};
63774acb8e1eSToby Isaac 
63784acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
63794acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
63804acb8e1eSToby Isaac       ierr = PetscSectionGetFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
63817c927364SMatthew G. Knepley     }
63824acb8e1eSToby Isaac     for (p = 0; p < numFPoints; p++) {
63834acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
638405586334SMatthew G. Knepley       DMPlexGetIndicesPointFields_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, foffsets, PETSC_FALSE, permsF, p, fclperm, findices);
63854acb8e1eSToby Isaac     }
63864acb8e1eSToby Isaac     for (p = 0; p < numCPoints; p++) {
63874acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
638805586334SMatthew G. Knepley       DMPlexGetIndicesPointFields_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, coffsets, PETSC_FALSE, permsC, p, cclperm, cindices);
63894acb8e1eSToby Isaac     }
63904acb8e1eSToby Isaac     for (f = 0; f < numFields; f++) {
63914acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(fsection,f,numFPoints,ftotpoints,&permsF[f],NULL);CHKERRQ(ierr);
63924acb8e1eSToby Isaac       ierr = PetscSectionRestoreFieldPointSyms(csection,f,numCPoints,cpoints,&permsC[f],NULL);CHKERRQ(ierr);
63937c927364SMatthew G. Knepley     }
63947c927364SMatthew G. Knepley   } else {
63954acb8e1eSToby Isaac     const PetscInt **permsF = NULL;
63964acb8e1eSToby Isaac     const PetscInt **permsC = NULL;
63974acb8e1eSToby Isaac 
63984acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
63994acb8e1eSToby Isaac     ierr = PetscSectionGetPointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
64004acb8e1eSToby Isaac     for (p = 0, off = 0; p < numFPoints; p++) {
64014acb8e1eSToby Isaac       const PetscInt *perm = permsF ? permsF[p] : NULL;
64024acb8e1eSToby Isaac 
64034acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalFSection, ftotpoints[2*p], &globalOff);CHKERRQ(ierr);
640405586334SMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(fsection, ftotpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, fclperm, findices);
64057c927364SMatthew G. Knepley     }
64064acb8e1eSToby Isaac     for (p = 0, off = 0; p < numCPoints; p++) {
64074acb8e1eSToby Isaac       const PetscInt *perm = permsC ? permsC[p] : NULL;
64084acb8e1eSToby Isaac 
64094acb8e1eSToby Isaac       ierr = PetscSectionGetOffset(globalCSection, cpoints[2*p], &globalOff);CHKERRQ(ierr);
641005586334SMatthew G. Knepley       DMPlexGetIndicesPoint_Internal(csection, cpoints[2*p], globalOff < 0 ? -(globalOff+1) : globalOff, &off, PETSC_FALSE, perm, cclperm, cindices);
64117c927364SMatthew G. Knepley     }
64124acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(fsection,numFPoints,ftotpoints,&permsF,NULL);CHKERRQ(ierr);
64134acb8e1eSToby Isaac     ierr = PetscSectionRestorePointSyms(csection,numCPoints,cpoints,&permsC,NULL);CHKERRQ(ierr);
64147c927364SMatthew G. Knepley   }
641569291d52SBarry Smith   ierr = DMRestoreWorkArray(dmf, numCPoints*2*4, MPIU_INT, &ftotpoints);CHKERRQ(ierr);
64167c927364SMatthew G. Knepley   ierr = DMPlexRestoreTransitiveClosure(dmc, point, PETSC_TRUE, &numCPoints, &cpoints);CHKERRQ(ierr);
64177c927364SMatthew G. Knepley   PetscFunctionReturn(0);
64187c927364SMatthew G. Knepley }
64197c927364SMatthew G. Knepley 
6420f96281f8SMatthew G. Knepley /*@
6421f96281f8SMatthew G. Knepley   DMPlexGetHybridBounds - Get the first mesh point of each dimension which is a hybrid
6422f96281f8SMatthew G. Knepley 
6423f96281f8SMatthew G. Knepley   Input Parameter:
6424f96281f8SMatthew G. Knepley . dm - The DMPlex object
6425f96281f8SMatthew G. Knepley 
6426f96281f8SMatthew G. Knepley   Output Parameters:
6427f96281f8SMatthew G. Knepley + cMax - The first hybrid cell
6428dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6429dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6430dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6431f96281f8SMatthew G. Knepley 
6432f96281f8SMatthew G. Knepley   Level: developer
6433f96281f8SMatthew G. Knepley 
6434dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexSetHybridBounds()
6435f96281f8SMatthew G. Knepley @*/
6436770b213bSMatthew G Knepley PetscErrorCode DMPlexGetHybridBounds(DM dm, PetscInt *cMax, PetscInt *fMax, PetscInt *eMax, PetscInt *vMax)
6437552f7358SJed Brown {
6438552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6439770b213bSMatthew G Knepley   PetscInt       dim;
6440770b213bSMatthew G Knepley   PetscErrorCode ierr;
6441552f7358SJed Brown 
6442552f7358SJed Brown   PetscFunctionBegin;
6443552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6444c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
64457d5acc75SStefano Zampini   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6446770b213bSMatthew G Knepley   if (cMax) *cMax = mesh->hybridPointMax[dim];
64477d5acc75SStefano Zampini   if (fMax) *fMax = mesh->hybridPointMax[PetscMax(dim-1,0)];
6448770b213bSMatthew G Knepley   if (eMax) *eMax = mesh->hybridPointMax[1];
6449770b213bSMatthew G Knepley   if (vMax) *vMax = mesh->hybridPointMax[0];
6450552f7358SJed Brown   PetscFunctionReturn(0);
6451552f7358SJed Brown }
6452552f7358SJed Brown 
6453aeadca18SToby Isaac static PetscErrorCode DMPlexCreateDimStratum(DM dm, DMLabel depthLabel, DMLabel dimLabel, PetscInt d, PetscInt dMax)
64544a3e9fdbSToby Isaac {
64554a3e9fdbSToby Isaac   IS             is, his;
64567d5acc75SStefano Zampini   PetscInt       first = 0, stride;
64574a3e9fdbSToby Isaac   PetscBool      isStride;
64584a3e9fdbSToby Isaac   PetscErrorCode ierr;
64594a3e9fdbSToby Isaac 
64604a3e9fdbSToby Isaac   PetscFunctionBegin;
64614a3e9fdbSToby Isaac   ierr = DMLabelGetStratumIS(depthLabel, d, &is);CHKERRQ(ierr);
64624a3e9fdbSToby Isaac   ierr = PetscObjectTypeCompare((PetscObject) is, ISSTRIDE, &isStride);CHKERRQ(ierr);
64634a3e9fdbSToby Isaac   if (isStride) {
64644a3e9fdbSToby Isaac     ierr = ISStrideGetInfo(is, &first, &stride);CHKERRQ(ierr);
64654a3e9fdbSToby Isaac   }
64667d5acc75SStefano 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);
64674a3e9fdbSToby Isaac   ierr = ISCreateStride(PETSC_COMM_SELF, (dMax - first), first, 1, &his);CHKERRQ(ierr);
6468aeadca18SToby Isaac   ierr = DMLabelSetStratumIS(dimLabel, d, his);CHKERRQ(ierr);
64694a3e9fdbSToby Isaac   ierr = ISDestroy(&his);CHKERRQ(ierr);
64704a3e9fdbSToby Isaac   ierr = ISDestroy(&is);CHKERRQ(ierr);
64714a3e9fdbSToby Isaac   PetscFunctionReturn(0);
64724a3e9fdbSToby Isaac }
64734a3e9fdbSToby Isaac 
6474dc5a3409SMatthew G. Knepley /*@
6475dc5a3409SMatthew G. Knepley   DMPlexSetHybridBounds - Set the first mesh point of each dimension which is a hybrid
6476dc5a3409SMatthew G. Knepley 
6477dc5a3409SMatthew G. Knepley   Input Parameters:
6478a2b725a8SWilliam Gropp + dm   - The DMPlex object
6479dc5a3409SMatthew G. Knepley . cMax - The first hybrid cell
6480dc5a3409SMatthew G. Knepley . fMax - The first hybrid face
6481dc5a3409SMatthew G. Knepley . eMax - The first hybrid edge
6482dc5a3409SMatthew G. Knepley - vMax - The first hybrid vertex
6483dc5a3409SMatthew G. Knepley 
6484dc5a3409SMatthew G. Knepley   Level: developer
6485dc5a3409SMatthew G. Knepley 
6486dc5a3409SMatthew G. Knepley .seealso DMPlexCreateHybridMesh(), DMPlexGetHybridBounds()
6487dc5a3409SMatthew G. Knepley @*/
6488770b213bSMatthew G Knepley PetscErrorCode DMPlexSetHybridBounds(DM dm, PetscInt cMax, PetscInt fMax, PetscInt eMax, PetscInt vMax)
6489552f7358SJed Brown {
6490552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6491770b213bSMatthew G Knepley   PetscInt       dim;
6492770b213bSMatthew G Knepley   PetscErrorCode ierr;
6493552f7358SJed Brown 
6494552f7358SJed Brown   PetscFunctionBegin;
6495552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6496c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
64977d5acc75SStefano Zampini   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6498770b213bSMatthew G Knepley   if (cMax >= 0) mesh->hybridPointMax[dim]               = cMax;
64997d5acc75SStefano Zampini   if (fMax >= 0) mesh->hybridPointMax[PetscMax(dim-1,0)] = fMax;
6500770b213bSMatthew G Knepley   if (eMax >= 0) mesh->hybridPointMax[1]                 = eMax;
6501770b213bSMatthew G Knepley   if (vMax >= 0) mesh->hybridPointMax[0]                 = vMax;
6502552f7358SJed Brown   PetscFunctionReturn(0);
6503552f7358SJed Brown }
6504552f7358SJed Brown 
65057cd05799SMatthew G. Knepley /*@C
65067cd05799SMatthew G. Knepley   DMPlexGetVTKCellHeight - Returns the height in the DAG used to determine which points are cells (normally 0)
65077cd05799SMatthew G. Knepley 
65087cd05799SMatthew G. Knepley   Input Parameter:
65097cd05799SMatthew G. Knepley . dm   - The DMPlex object
65107cd05799SMatthew G. Knepley 
65117cd05799SMatthew G. Knepley   Output Parameter:
65127cd05799SMatthew G. Knepley . cellHeight - The height of a cell
65137cd05799SMatthew G. Knepley 
65147cd05799SMatthew G. Knepley   Level: developer
65157cd05799SMatthew G. Knepley 
65167cd05799SMatthew G. Knepley .seealso DMPlexSetVTKCellHeight()
65177cd05799SMatthew G. Knepley @*/
6518552f7358SJed Brown PetscErrorCode DMPlexGetVTKCellHeight(DM dm, PetscInt *cellHeight)
6519552f7358SJed Brown {
6520552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6521552f7358SJed Brown 
6522552f7358SJed Brown   PetscFunctionBegin;
6523552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6524552f7358SJed Brown   PetscValidPointer(cellHeight, 2);
6525552f7358SJed Brown   *cellHeight = mesh->vtkCellHeight;
6526552f7358SJed Brown   PetscFunctionReturn(0);
6527552f7358SJed Brown }
6528552f7358SJed Brown 
65297cd05799SMatthew G. Knepley /*@C
65307cd05799SMatthew G. Knepley   DMPlexSetVTKCellHeight - Sets the height in the DAG used to determine which points are cells (normally 0)
65317cd05799SMatthew G. Knepley 
65327cd05799SMatthew G. Knepley   Input Parameters:
65337cd05799SMatthew G. Knepley + dm   - The DMPlex object
65347cd05799SMatthew G. Knepley - cellHeight - The height of a cell
65357cd05799SMatthew G. Knepley 
65367cd05799SMatthew G. Knepley   Level: developer
65377cd05799SMatthew G. Knepley 
65387cd05799SMatthew G. Knepley .seealso DMPlexGetVTKCellHeight()
65397cd05799SMatthew G. Knepley @*/
6540552f7358SJed Brown PetscErrorCode DMPlexSetVTKCellHeight(DM dm, PetscInt cellHeight)
6541552f7358SJed Brown {
6542552f7358SJed Brown   DM_Plex *mesh = (DM_Plex*) dm->data;
6543552f7358SJed Brown 
6544552f7358SJed Brown   PetscFunctionBegin;
6545552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6546552f7358SJed Brown   mesh->vtkCellHeight = cellHeight;
6547552f7358SJed Brown   PetscFunctionReturn(0);
6548552f7358SJed Brown }
6549552f7358SJed Brown 
6550e6139122SMatthew G. Knepley /*@
6551e6139122SMatthew G. Knepley   DMPlexGetGhostCellStratum - Get the range of cells which are used to enforce FV boundary conditions
6552e6139122SMatthew G. Knepley 
6553e6139122SMatthew G. Knepley   Input Parameter:
6554e6139122SMatthew G. Knepley . dm - The DMPlex object
6555e6139122SMatthew G. Knepley 
6556e6139122SMatthew G. Knepley   Output Parameters:
65572a9f31c0SMatthew G. Knepley + gcStart - The first ghost cell, or NULL
65582a9f31c0SMatthew G. Knepley - gcEnd   - The upper bound on ghost cells, or NULL
6559e6139122SMatthew G. Knepley 
65602a9f31c0SMatthew G. Knepley   Level: advanced
6561e6139122SMatthew G. Knepley 
6562e6139122SMatthew G. Knepley .seealso DMPlexConstructGhostCells(), DMPlexSetGhostCellStratum(), DMPlexGetHybridBounds()
6563e6139122SMatthew G. Knepley @*/
6564e6139122SMatthew G. Knepley PetscErrorCode DMPlexGetGhostCellStratum(DM dm, PetscInt *gcStart, PetscInt *gcEnd)
6565e6139122SMatthew G. Knepley {
6566e6139122SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
6567e6139122SMatthew G. Knepley   PetscInt       dim;
6568e6139122SMatthew G. Knepley   PetscErrorCode ierr;
6569e6139122SMatthew G. Knepley 
6570e6139122SMatthew G. Knepley   PetscFunctionBegin;
6571e6139122SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6572e6139122SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6573e6139122SMatthew G. Knepley   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6574e6139122SMatthew G. Knepley   if (gcStart) {PetscValidIntPointer(gcStart, 2); *gcStart = mesh->ghostCellStart;}
6575e6139122SMatthew G. Knepley   if (gcEnd)   {
6576e6139122SMatthew G. Knepley     PetscValidIntPointer(gcEnd, 3);
6577e6139122SMatthew G. Knepley     if (mesh->ghostCellStart >= 0) {ierr = DMPlexGetHeightStratum(dm, 0, NULL, gcEnd);CHKERRQ(ierr);}
6578e6139122SMatthew G. Knepley     else                           {*gcEnd = -1;}
6579e6139122SMatthew G. Knepley   }
6580e6139122SMatthew G. Knepley   PetscFunctionReturn(0);
6581e6139122SMatthew G. Knepley }
6582e6139122SMatthew G. Knepley 
6583e6139122SMatthew G. Knepley /*@
6584e6139122SMatthew G. Knepley   DMPlexSetGhostCellStratum - Set the range of cells which are used to enforce FV boundary conditions
6585e6139122SMatthew G. Knepley 
6586e6139122SMatthew G. Knepley   Input Parameters:
6587e6139122SMatthew G. Knepley + dm      - The DMPlex object
65882a9f31c0SMatthew G. Knepley . gcStart - The first ghost cell, or PETSC_DETERMINE
65892a9f31c0SMatthew G. Knepley - gcEnd   - The upper bound on ghost cells, or PETSC_DETERMINE
6590e6139122SMatthew G. Knepley 
65912a9f31c0SMatthew G. Knepley   Level: advanced
65922a9f31c0SMatthew G. Knepley 
65932a9f31c0SMatthew G. Knepley   Note: This is not usually called directly by a user.
6594e6139122SMatthew G. Knepley 
6595e6139122SMatthew G. Knepley .seealso DMPlexConstructGhostCells(), DMPlexGetGhostCellStratum(), DMPlexSetHybridBounds()
6596e6139122SMatthew G. Knepley @*/
6597e6139122SMatthew G. Knepley PetscErrorCode DMPlexSetGhostCellStratum(DM dm, PetscInt gcStart, PetscInt gcEnd)
6598e6139122SMatthew G. Knepley {
6599e6139122SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
6600e6139122SMatthew G. Knepley   PetscInt       dim;
6601e6139122SMatthew G. Knepley   PetscErrorCode ierr;
6602e6139122SMatthew G. Knepley 
6603e6139122SMatthew G. Knepley   PetscFunctionBegin;
6604e6139122SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6605e6139122SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
6606e6139122SMatthew G. Knepley   if (dim < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM dimension not yet set");
6607e6139122SMatthew G. Knepley   mesh->ghostCellStart = gcStart;
6608e6139122SMatthew G. Knepley   if (gcEnd >= 0) {
6609e6139122SMatthew G. Knepley     PetscInt cEnd;
6610e6139122SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
6611e6139122SMatthew G. Knepley     if (gcEnd != cEnd) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Ghost cells must appear at the end of the cell range, but gcEnd %D is not equal to cEnd %D", gcEnd, cEnd);
6612e6139122SMatthew G. Knepley   }
6613e6139122SMatthew G. Knepley   PetscFunctionReturn(0);
6614e6139122SMatthew G. Knepley }
6615e6139122SMatthew G. Knepley 
6616e6139122SMatthew G. Knepley /*@
6617e6139122SMatthew G. Knepley   DMPlexGetInteriorCellStratum - Get the range of cells which are neither hybrid nor ghost FV cells
6618e6139122SMatthew G. Knepley 
6619e6139122SMatthew G. Knepley   Input Parameter:
6620e6139122SMatthew G. Knepley . dm - The DMPlex object
6621e6139122SMatthew G. Knepley 
6622e6139122SMatthew G. Knepley   Output Parameters:
6623e6139122SMatthew G. Knepley + cStartInterior - The first ghost cell
6624e6139122SMatthew G. Knepley - cEndInterior   - The upper bound on ghost cells
6625e6139122SMatthew G. Knepley 
6626e6139122SMatthew G. Knepley   Level: developer
6627e6139122SMatthew G. Knepley 
6628e6139122SMatthew G. Knepley .seealso DMPlexConstructGhostCells(), DMPlexSetGhostCellStratum(), DMPlexGetHybridBounds()
6629e6139122SMatthew G. Knepley @*/
6630e6139122SMatthew G. Knepley PetscErrorCode DMPlexGetInteriorCellStratum(DM dm, PetscInt *cStartInterior, PetscInt *cEndInterior)
6631e6139122SMatthew G. Knepley {
6632e6139122SMatthew G. Knepley   PetscInt       gcEnd, cMax;
6633e6139122SMatthew G. Knepley   PetscErrorCode ierr;
6634e6139122SMatthew G. Knepley 
6635e6139122SMatthew G. Knepley   PetscFunctionBegin;
6636e6139122SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, cStartInterior, cEndInterior);CHKERRQ(ierr);
6637e6139122SMatthew G. Knepley   ierr = DMPlexGetGhostCellStratum(dm, &gcEnd, NULL);CHKERRQ(ierr);
6638e6139122SMatthew G. Knepley   *cEndInterior = gcEnd < 0 ? *cEndInterior : gcEnd;
6639e6139122SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
6640e6139122SMatthew G. Knepley   *cEndInterior = cMax  < 0 ? *cEndInterior : cMax;
6641e6139122SMatthew G. Knepley   PetscFunctionReturn(0);
6642e6139122SMatthew G. Knepley }
6643e6139122SMatthew G. Knepley 
6644552f7358SJed Brown /* We can easily have a form that takes an IS instead */
664550ad7711SMatthew G. Knepley PetscErrorCode DMPlexCreateNumbering_Internal(DM dm, PetscInt pStart, PetscInt pEnd, PetscInt shift, PetscInt *globalSize, PetscSF sf, IS *numbering)
6646552f7358SJed Brown {
6647552f7358SJed Brown   PetscSection   section, globalSection;
6648552f7358SJed Brown   PetscInt      *numbers, p;
6649552f7358SJed Brown   PetscErrorCode ierr;
6650552f7358SJed Brown 
6651552f7358SJed Brown   PetscFunctionBegin;
665282f516ccSBarry Smith   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &section);CHKERRQ(ierr);
6653552f7358SJed Brown   ierr = PetscSectionSetChart(section, pStart, pEnd);CHKERRQ(ierr);
6654552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6655552f7358SJed Brown     ierr = PetscSectionSetDof(section, p, 1);CHKERRQ(ierr);
6656552f7358SJed Brown   }
6657552f7358SJed Brown   ierr = PetscSectionSetUp(section);CHKERRQ(ierr);
665815b58121SMatthew G. Knepley   ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_FALSE, PETSC_FALSE, &globalSection);CHKERRQ(ierr);
6659854ce69bSBarry Smith   ierr = PetscMalloc1(pEnd - pStart, &numbers);CHKERRQ(ierr);
6660552f7358SJed Brown   for (p = pStart; p < pEnd; ++p) {
6661552f7358SJed Brown     ierr = PetscSectionGetOffset(globalSection, p, &numbers[p-pStart]);CHKERRQ(ierr);
6662ef48cebcSMatthew G. Knepley     if (numbers[p-pStart] < 0) numbers[p-pStart] -= shift;
6663ef48cebcSMatthew G. Knepley     else                       numbers[p-pStart] += shift;
6664552f7358SJed Brown   }
666582f516ccSBarry Smith   ierr = ISCreateGeneral(PetscObjectComm((PetscObject) dm), pEnd - pStart, numbers, PETSC_OWN_POINTER, numbering);CHKERRQ(ierr);
6666ef48cebcSMatthew G. Knepley   if (globalSize) {
6667ef48cebcSMatthew G. Knepley     PetscLayout layout;
6668ef48cebcSMatthew G. Knepley     ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject) dm), globalSection, &layout);CHKERRQ(ierr);
6669ef48cebcSMatthew G. Knepley     ierr = PetscLayoutGetSize(layout, globalSize);CHKERRQ(ierr);
6670ef48cebcSMatthew G. Knepley     ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
6671ef48cebcSMatthew G. Knepley   }
6672552f7358SJed Brown   ierr = PetscSectionDestroy(&section);CHKERRQ(ierr);
6673552f7358SJed Brown   ierr = PetscSectionDestroy(&globalSection);CHKERRQ(ierr);
6674552f7358SJed Brown   PetscFunctionReturn(0);
6675552f7358SJed Brown }
6676552f7358SJed Brown 
667781ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateCellNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalCellNumbers)
6678552f7358SJed Brown {
6679552f7358SJed Brown   PetscInt       cellHeight, cStart, cEnd, cMax;
6680552f7358SJed Brown   PetscErrorCode ierr;
6681552f7358SJed Brown 
6682552f7358SJed Brown   PetscFunctionBegin;
6683552f7358SJed Brown   ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
6684552f7358SJed Brown   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
66850298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
668681ed3555SMatthew G. Knepley   if (cMax >= 0 && !includeHybrid) cEnd = PetscMin(cEnd, cMax);
668750ad7711SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Internal(dm, cStart, cEnd, 0, NULL, dm->sf, globalCellNumbers);CHKERRQ(ierr);
668881ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
6689552f7358SJed Brown }
669081ed3555SMatthew G. Knepley 
66918dab3259SMatthew G. Knepley /*@
66927cd05799SMatthew G. Knepley   DMPlexGetCellNumbering - Get a global cell numbering for all cells on this process
66937cd05799SMatthew G. Knepley 
66947cd05799SMatthew G. Knepley   Input Parameter:
66957cd05799SMatthew G. Knepley . dm   - The DMPlex object
66967cd05799SMatthew G. Knepley 
66977cd05799SMatthew G. Knepley   Output Parameter:
66987cd05799SMatthew G. Knepley . globalCellNumbers - Global cell numbers for all cells on this process
66997cd05799SMatthew G. Knepley 
67007cd05799SMatthew G. Knepley   Level: developer
67017cd05799SMatthew G. Knepley 
67027cd05799SMatthew G. Knepley .seealso DMPlexGetVertexNumbering()
67037cd05799SMatthew G. Knepley @*/
670481ed3555SMatthew G. Knepley PetscErrorCode DMPlexGetCellNumbering(DM dm, IS *globalCellNumbers)
670581ed3555SMatthew G. Knepley {
670681ed3555SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
670781ed3555SMatthew G. Knepley   PetscErrorCode ierr;
670881ed3555SMatthew G. Knepley 
670981ed3555SMatthew G. Knepley   PetscFunctionBegin;
671081ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
671181ed3555SMatthew G. Knepley   if (!mesh->globalCellNumbers) {ierr = DMPlexCreateCellNumbering_Internal(dm, PETSC_FALSE, &mesh->globalCellNumbers);CHKERRQ(ierr);}
6712552f7358SJed Brown   *globalCellNumbers = mesh->globalCellNumbers;
6713552f7358SJed Brown   PetscFunctionReturn(0);
6714552f7358SJed Brown }
6715552f7358SJed Brown 
671681ed3555SMatthew G. Knepley PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM dm, PetscBool includeHybrid, IS *globalVertexNumbers)
671781ed3555SMatthew G. Knepley {
671881ed3555SMatthew G. Knepley   PetscInt       vStart, vEnd, vMax;
671981ed3555SMatthew G. Knepley   PetscErrorCode ierr;
672081ed3555SMatthew G. Knepley 
672181ed3555SMatthew G. Knepley   PetscFunctionBegin;
672281ed3555SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
672381ed3555SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
672481ed3555SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
672581ed3555SMatthew G. Knepley   if (vMax >= 0 && !includeHybrid) vEnd = PetscMin(vEnd, vMax);
672650ad7711SMatthew G. Knepley   ierr = DMPlexCreateNumbering_Internal(dm, vStart, vEnd, 0, NULL, dm->sf, globalVertexNumbers);CHKERRQ(ierr);
672781ed3555SMatthew G. Knepley   PetscFunctionReturn(0);
672881ed3555SMatthew G. Knepley }
672981ed3555SMatthew G. Knepley 
67308dab3259SMatthew G. Knepley /*@
67316aa51ba9SJacob Faibussowitsch   DMPlexGetVertexNumbering - Get a global vertex numbering for all vertices on this process
67327cd05799SMatthew G. Knepley 
67337cd05799SMatthew G. Knepley   Input Parameter:
67347cd05799SMatthew G. Knepley . dm   - The DMPlex object
67357cd05799SMatthew G. Knepley 
67367cd05799SMatthew G. Knepley   Output Parameter:
67377cd05799SMatthew G. Knepley . globalVertexNumbers - Global vertex numbers for all vertices on this process
67387cd05799SMatthew G. Knepley 
67397cd05799SMatthew G. Knepley   Level: developer
67407cd05799SMatthew G. Knepley 
67417cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
67427cd05799SMatthew G. Knepley @*/
6743552f7358SJed Brown PetscErrorCode DMPlexGetVertexNumbering(DM dm, IS *globalVertexNumbers)
6744552f7358SJed Brown {
6745552f7358SJed Brown   DM_Plex       *mesh = (DM_Plex*) dm->data;
6746552f7358SJed Brown   PetscErrorCode ierr;
6747552f7358SJed Brown 
6748552f7358SJed Brown   PetscFunctionBegin;
6749552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
675081ed3555SMatthew G. Knepley   if (!mesh->globalVertexNumbers) {ierr = DMPlexCreateVertexNumbering_Internal(dm, PETSC_FALSE, &mesh->globalVertexNumbers);CHKERRQ(ierr);}
6751552f7358SJed Brown   *globalVertexNumbers = mesh->globalVertexNumbers;
6752552f7358SJed Brown   PetscFunctionReturn(0);
6753552f7358SJed Brown }
6754552f7358SJed Brown 
67558dab3259SMatthew G. Knepley /*@
67567cd05799SMatthew G. Knepley   DMPlexCreatePointNumbering - Create a global numbering for all points on this process
67577cd05799SMatthew G. Knepley 
67587cd05799SMatthew G. Knepley   Input Parameter:
67597cd05799SMatthew G. Knepley . dm   - The DMPlex object
67607cd05799SMatthew G. Knepley 
67617cd05799SMatthew G. Knepley   Output Parameter:
67627cd05799SMatthew G. Knepley . globalPointNumbers - Global numbers for all points on this process
67637cd05799SMatthew G. Knepley 
67647cd05799SMatthew G. Knepley   Level: developer
67657cd05799SMatthew G. Knepley 
67667cd05799SMatthew G. Knepley .seealso DMPlexGetCellNumbering()
67677cd05799SMatthew G. Knepley @*/
6768ef48cebcSMatthew G. Knepley PetscErrorCode DMPlexCreatePointNumbering(DM dm, IS *globalPointNumbers)
6769ef48cebcSMatthew G. Knepley {
6770ef48cebcSMatthew G. Knepley   IS             nums[4];
6771862913ffSStefano Zampini   PetscInt       depths[4], gdepths[4], starts[4];
6772ef48cebcSMatthew G. Knepley   PetscInt       depth, d, shift = 0;
6773ef48cebcSMatthew G. Knepley   PetscErrorCode ierr;
6774ef48cebcSMatthew G. Knepley 
6775ef48cebcSMatthew G. Knepley   PetscFunctionBegin;
6776ef48cebcSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6777ef48cebcSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
67788abc87a0SMichael Lange   /* For unstratified meshes use dim instead of depth */
67798abc87a0SMichael Lange   if (depth < 0) {ierr = DMGetDimension(dm, &depth);CHKERRQ(ierr);}
6780862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
6781862913ffSStefano Zampini     PetscInt end;
6782862913ffSStefano Zampini 
6783862913ffSStefano Zampini     depths[d] = depth-d;
6784862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, depths[d], &starts[d], &end);CHKERRQ(ierr);
6785862913ffSStefano Zampini     if (!(starts[d]-end)) { starts[d] = depths[d] = -1; }
6786862913ffSStefano Zampini   }
6787862913ffSStefano Zampini   ierr = PetscSortIntWithArray(depth+1, starts, depths);CHKERRQ(ierr);
6788862913ffSStefano Zampini   ierr = MPIU_Allreduce(depths, gdepths, depth+1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
6789862913ffSStefano Zampini   for (d = 0; d <= depth; ++d) {
6790862913ffSStefano 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]);
6791862913ffSStefano Zampini   }
6792ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
6793ef48cebcSMatthew G. Knepley     PetscInt pStart, pEnd, gsize;
6794ef48cebcSMatthew G. Knepley 
6795862913ffSStefano Zampini     ierr = DMPlexGetDepthStratum(dm, gdepths[d], &pStart, &pEnd);CHKERRQ(ierr);
679650ad7711SMatthew G. Knepley     ierr = DMPlexCreateNumbering_Internal(dm, pStart, pEnd, shift, &gsize, dm->sf, &nums[d]);CHKERRQ(ierr);
6797ef48cebcSMatthew G. Knepley     shift += gsize;
6798ef48cebcSMatthew G. Knepley   }
6799302440fdSBarry Smith   ierr = ISConcatenate(PetscObjectComm((PetscObject) dm), depth+1, nums, globalPointNumbers);CHKERRQ(ierr);
6800ef48cebcSMatthew G. Knepley   for (d = 0; d <= depth; ++d) {ierr = ISDestroy(&nums[d]);CHKERRQ(ierr);}
6801ef48cebcSMatthew G. Knepley   PetscFunctionReturn(0);
6802ef48cebcSMatthew G. Knepley }
6803ef48cebcSMatthew G. Knepley 
680408a22f4bSMatthew G. Knepley 
680508a22f4bSMatthew G. Knepley /*@
680608a22f4bSMatthew G. Knepley   DMPlexCreateRankField - Create a cell field whose value is the rank of the owner
680708a22f4bSMatthew G. Knepley 
680808a22f4bSMatthew G. Knepley   Input Parameter:
680908a22f4bSMatthew G. Knepley . dm - The DMPlex object
681008a22f4bSMatthew G. Knepley 
681108a22f4bSMatthew G. Knepley   Output Parameter:
681208a22f4bSMatthew G. Knepley . ranks - The rank field
681308a22f4bSMatthew G. Knepley 
681408a22f4bSMatthew G. Knepley   Options Database Keys:
681508a22f4bSMatthew G. Knepley . -dm_partition_view - Adds the rank field into the DM output from -dm_view using the same viewer
681608a22f4bSMatthew G. Knepley 
681708a22f4bSMatthew G. Knepley   Level: intermediate
681808a22f4bSMatthew G. Knepley 
681908a22f4bSMatthew G. Knepley .seealso: DMView()
682008a22f4bSMatthew G. Knepley @*/
682108a22f4bSMatthew G. Knepley PetscErrorCode DMPlexCreateRankField(DM dm, Vec *ranks)
682208a22f4bSMatthew G. Knepley {
682308a22f4bSMatthew G. Knepley   DM             rdm;
682408a22f4bSMatthew G. Knepley   PetscFE        fe;
682508a22f4bSMatthew G. Knepley   PetscScalar   *r;
682608a22f4bSMatthew G. Knepley   PetscMPIInt    rank;
682708a22f4bSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
682808a22f4bSMatthew G. Knepley   PetscErrorCode ierr;
682908a22f4bSMatthew G. Knepley 
683008a22f4bSMatthew G. Knepley   PetscFunctionBeginUser;
6831f95ace6aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6832f95ace6aSMatthew G. Knepley   PetscValidPointer(ranks, 2);
683308a22f4bSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
683408a22f4bSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
683508a22f4bSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
6836f95ace6aSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___rank_", -1, &fe);CHKERRQ(ierr);
683708a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "rank");CHKERRQ(ierr);
6838e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
683908a22f4bSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
6840e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
684108a22f4bSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
684208a22f4bSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, ranks);CHKERRQ(ierr);
684308a22f4bSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *ranks, "partition");CHKERRQ(ierr);
684408a22f4bSMatthew G. Knepley   ierr = VecGetArray(*ranks, &r);CHKERRQ(ierr);
684508a22f4bSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
684608a22f4bSMatthew G. Knepley     PetscScalar *lr;
684708a22f4bSMatthew G. Knepley 
684808a22f4bSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, r, &lr);CHKERRQ(ierr);
684908a22f4bSMatthew G. Knepley     *lr = rank;
685008a22f4bSMatthew G. Knepley   }
685108a22f4bSMatthew G. Knepley   ierr = VecRestoreArray(*ranks, &r);CHKERRQ(ierr);
685208a22f4bSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
685308a22f4bSMatthew G. Knepley   PetscFunctionReturn(0);
685408a22f4bSMatthew G. Knepley }
685508a22f4bSMatthew G. Knepley 
6856ca8062c8SMatthew G. Knepley /*@
685718e14f0cSMatthew G. Knepley   DMPlexCreateLabelField - Create a cell field whose value is the label value for that cell
685818e14f0cSMatthew G. Knepley 
685918e14f0cSMatthew G. Knepley   Input Parameters:
686018e14f0cSMatthew G. Knepley + dm    - The DMPlex
686118e14f0cSMatthew G. Knepley - label - The DMLabel
686218e14f0cSMatthew G. Knepley 
686318e14f0cSMatthew G. Knepley   Output Parameter:
686418e14f0cSMatthew G. Knepley . val - The label value field
686518e14f0cSMatthew G. Knepley 
686618e14f0cSMatthew G. Knepley   Options Database Keys:
686718e14f0cSMatthew G. Knepley . -dm_label_view - Adds the label value field into the DM output from -dm_view using the same viewer
686818e14f0cSMatthew G. Knepley 
686918e14f0cSMatthew G. Knepley   Level: intermediate
687018e14f0cSMatthew G. Knepley 
687118e14f0cSMatthew G. Knepley .seealso: DMView()
687218e14f0cSMatthew G. Knepley @*/
687318e14f0cSMatthew G. Knepley PetscErrorCode DMPlexCreateLabelField(DM dm, DMLabel label, Vec *val)
687418e14f0cSMatthew G. Knepley {
687518e14f0cSMatthew G. Knepley   DM             rdm;
687618e14f0cSMatthew G. Knepley   PetscFE        fe;
687718e14f0cSMatthew G. Knepley   PetscScalar   *v;
687818e14f0cSMatthew G. Knepley   PetscInt       dim, cStart, cEnd, c;
687918e14f0cSMatthew G. Knepley   PetscErrorCode ierr;
688018e14f0cSMatthew G. Knepley 
688118e14f0cSMatthew G. Knepley   PetscFunctionBeginUser;
688218e14f0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
688318e14f0cSMatthew G. Knepley   PetscValidPointer(label, 2);
688418e14f0cSMatthew G. Knepley   PetscValidPointer(val, 3);
688518e14f0cSMatthew G. Knepley   ierr = DMClone(dm, &rdm);CHKERRQ(ierr);
688618e14f0cSMatthew G. Knepley   ierr = DMGetDimension(rdm, &dim);CHKERRQ(ierr);
688718e14f0cSMatthew G. Knepley   ierr = PetscFECreateDefault(PetscObjectComm((PetscObject) rdm), dim, 1, PETSC_TRUE, "PETSc___label_value_", -1, &fe);CHKERRQ(ierr);
688818e14f0cSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) fe, "label_value");CHKERRQ(ierr);
6889e5e52638SMatthew G. Knepley   ierr = DMSetField(rdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
689018e14f0cSMatthew G. Knepley   ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
6891e5e52638SMatthew G. Knepley   ierr = DMCreateDS(rdm);CHKERRQ(ierr);
689218e14f0cSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(rdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
689318e14f0cSMatthew G. Knepley   ierr = DMCreateGlobalVector(rdm, val);CHKERRQ(ierr);
6894effbd7cbSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) *val, "label_value");CHKERRQ(ierr);
689518e14f0cSMatthew G. Knepley   ierr = VecGetArray(*val, &v);CHKERRQ(ierr);
689618e14f0cSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
689718e14f0cSMatthew G. Knepley     PetscScalar *lv;
689818e14f0cSMatthew G. Knepley     PetscInt     cval;
689918e14f0cSMatthew G. Knepley 
690018e14f0cSMatthew G. Knepley     ierr = DMPlexPointGlobalRef(rdm, c, v, &lv);CHKERRQ(ierr);
690118e14f0cSMatthew G. Knepley     ierr = DMLabelGetValue(label, c, &cval);CHKERRQ(ierr);
690218e14f0cSMatthew G. Knepley     *lv = cval;
690318e14f0cSMatthew G. Knepley   }
690418e14f0cSMatthew G. Knepley   ierr = VecRestoreArray(*val, &v);CHKERRQ(ierr);
690518e14f0cSMatthew G. Knepley   ierr = DMDestroy(&rdm);CHKERRQ(ierr);
690618e14f0cSMatthew G. Knepley   PetscFunctionReturn(0);
690718e14f0cSMatthew G. Knepley }
690818e14f0cSMatthew G. Knepley 
690918e14f0cSMatthew G. Knepley /*@
6910ca8062c8SMatthew G. Knepley   DMPlexCheckSymmetry - Check that the adjacency information in the mesh is symmetric.
6911ca8062c8SMatthew G. Knepley 
691269916449SMatthew G. Knepley   Input Parameter:
691369916449SMatthew G. Knepley . dm - The DMPlex object
6914ca8062c8SMatthew G. Knepley 
6915ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
6916ca8062c8SMatthew G. Knepley 
6917ca8062c8SMatthew G. Knepley   Level: developer
6918ca8062c8SMatthew G. Knepley 
69197d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
6920ca8062c8SMatthew G. Knepley @*/
6921ca8062c8SMatthew G. Knepley PetscErrorCode DMPlexCheckSymmetry(DM dm)
6922ca8062c8SMatthew G. Knepley {
6923ca8062c8SMatthew G. Knepley   PetscSection    coneSection, supportSection;
6924ca8062c8SMatthew G. Knepley   const PetscInt *cone, *support;
6925ca8062c8SMatthew G. Knepley   PetscInt        coneSize, c, supportSize, s;
692657beb4faSStefano Zampini   PetscInt        pStart, pEnd, p, pp, csize, ssize;
692757beb4faSStefano Zampini   PetscBool       storagecheck = PETSC_TRUE;
6928ca8062c8SMatthew G. Knepley   PetscErrorCode  ierr;
6929ca8062c8SMatthew G. Knepley 
6930ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
6931ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6932ca8062c8SMatthew G. Knepley   ierr = DMPlexGetConeSection(dm, &coneSection);CHKERRQ(ierr);
6933ca8062c8SMatthew G. Knepley   ierr = DMPlexGetSupportSection(dm, &supportSection);CHKERRQ(ierr);
6934ca8062c8SMatthew G. Knepley   /* Check that point p is found in the support of its cone points, and vice versa */
6935ca8062c8SMatthew G. Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
6936ca8062c8SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
6937ca8062c8SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
6938ca8062c8SMatthew G. Knepley     ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
6939ca8062c8SMatthew G. Knepley     for (c = 0; c < coneSize; ++c) {
694042e66dfaSMatthew G. Knepley       PetscBool dup = PETSC_FALSE;
694142e66dfaSMatthew G. Knepley       PetscInt  d;
694242e66dfaSMatthew G. Knepley       for (d = c-1; d >= 0; --d) {
694342e66dfaSMatthew G. Knepley         if (cone[c] == cone[d]) {dup = PETSC_TRUE; break;}
694442e66dfaSMatthew G. Knepley       }
6945ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[c], &supportSize);CHKERRQ(ierr);
6946ca8062c8SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[c], &support);CHKERRQ(ierr);
6947ca8062c8SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
6948ca8062c8SMatthew G. Knepley         if (support[s] == p) break;
6949ca8062c8SMatthew G. Knepley       }
695042e66dfaSMatthew G. Knepley       if ((s >= supportSize) || (dup && (support[s+1] != p))) {
69518ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", p);CHKERRQ(ierr);
6952ca8062c8SMatthew G. Knepley         for (s = 0; s < coneSize; ++s) {
69538ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[s]);CHKERRQ(ierr);
6954ca8062c8SMatthew G. Knepley         }
6955302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
69568ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", cone[c]);CHKERRQ(ierr);
6957ca8062c8SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
69588ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[s]);CHKERRQ(ierr);
6959ca8062c8SMatthew G. Knepley         }
6960302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
69618ccfff9cSToby 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]);
69628ccfff9cSToby Isaac         else SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in support of cone point %D", p, cone[c]);
6963ca8062c8SMatthew G. Knepley       }
696442e66dfaSMatthew G. Knepley     }
696557beb4faSStefano Zampini     ierr = DMPlexGetTreeParent(dm, p, &pp, NULL);CHKERRQ(ierr);
696657beb4faSStefano Zampini     if (p != pp) { storagecheck = PETSC_FALSE; continue; }
6967ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, p, &supportSize);CHKERRQ(ierr);
6968ca8062c8SMatthew G. Knepley     ierr = DMPlexGetSupport(dm, p, &support);CHKERRQ(ierr);
6969ca8062c8SMatthew G. Knepley     for (s = 0; s < supportSize; ++s) {
6970ca8062c8SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
6971ca8062c8SMatthew G. Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
6972ca8062c8SMatthew G. Knepley       for (c = 0; c < coneSize; ++c) {
697357beb4faSStefano Zampini         ierr = DMPlexGetTreeParent(dm, cone[c], &pp, NULL);CHKERRQ(ierr);
697457beb4faSStefano Zampini         if (cone[c] != pp) { c = 0; break; }
6975ca8062c8SMatthew G. Knepley         if (cone[c] == p) break;
6976ca8062c8SMatthew G. Knepley       }
6977ca8062c8SMatthew G. Knepley       if (c >= coneSize) {
69788ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D support: ", p);CHKERRQ(ierr);
6979ca8062c8SMatthew G. Knepley         for (c = 0; c < supportSize; ++c) {
69808ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", support[c]);CHKERRQ(ierr);
6981ca8062c8SMatthew G. Knepley         }
6982302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
69838ccfff9cSToby Isaac         ierr = PetscPrintf(PETSC_COMM_SELF, "p: %D cone: ", support[s]);CHKERRQ(ierr);
6984ca8062c8SMatthew G. Knepley         for (c = 0; c < coneSize; ++c) {
69858ccfff9cSToby Isaac           ierr = PetscPrintf(PETSC_COMM_SELF, "%D, ", cone[c]);CHKERRQ(ierr);
6986ca8062c8SMatthew G. Knepley         }
6987302440fdSBarry Smith         ierr = PetscPrintf(PETSC_COMM_SELF, "\n");CHKERRQ(ierr);
69888ccfff9cSToby Isaac         SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %D not found in cone of support point %D", p, support[s]);
6989ca8062c8SMatthew G. Knepley       }
6990ca8062c8SMatthew G. Knepley     }
6991ca8062c8SMatthew G. Knepley   }
699257beb4faSStefano Zampini   if (storagecheck) {
6993ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coneSection, &csize);CHKERRQ(ierr);
6994ca8062c8SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(supportSection, &ssize);CHKERRQ(ierr);
69958ccfff9cSToby Isaac     if (csize != ssize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total cone size %D != Total support size %D", csize, ssize);
699657beb4faSStefano Zampini   }
6997ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
6998ca8062c8SMatthew G. Knepley }
6999ca8062c8SMatthew G. Knepley 
7000ca8062c8SMatthew G. Knepley /*@
7001ca8062c8SMatthew G. Knepley   DMPlexCheckSkeleton - Check that each cell has the correct number of vertices
7002ca8062c8SMatthew G. Knepley 
7003ca8062c8SMatthew G. Knepley   Input Parameters:
7004ca8062c8SMatthew G. Knepley + dm - The DMPlex object
700558723a97SMatthew G. Knepley - cellHeight - Normally 0
7006ca8062c8SMatthew G. Knepley 
7007ca8062c8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
700825c50c26SVaclav Hapla   Currently applicable only to homogeneous simplex or tensor meshes.
7009ca8062c8SMatthew G. Knepley 
7010ca8062c8SMatthew G. Knepley   Level: developer
7011ca8062c8SMatthew G. Knepley 
70127d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSymmetry(), DMPlexCheckFaces()
7013ca8062c8SMatthew G. Knepley @*/
701425c50c26SVaclav Hapla PetscErrorCode DMPlexCheckSkeleton(DM dm, PetscInt cellHeight)
7015ca8062c8SMatthew G. Knepley {
701642363296SMatthew G. Knepley   PetscInt       dim, numCorners, numHybridCorners, vStart, vEnd, cStart, cEnd, cMax, c;
701725c50c26SVaclav Hapla   PetscBool      isSimplex = PETSC_FALSE;
7018ca8062c8SMatthew G. Knepley   PetscErrorCode ierr;
7019ca8062c8SMatthew G. Knepley 
7020ca8062c8SMatthew G. Knepley   PetscFunctionBegin;
7021ca8062c8SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7022c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
702325c50c26SVaclav Hapla   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
702425c50c26SVaclav Hapla   if (cStart < cEnd) {
702525c50c26SVaclav Hapla     ierr = DMPlexGetConeSize(dm, cStart, &c);CHKERRQ(ierr);
702625c50c26SVaclav Hapla     isSimplex = c == dim+1 ? PETSC_TRUE : PETSC_FALSE;
702725c50c26SVaclav Hapla   }
7028ca8062c8SMatthew G. Knepley   switch (dim) {
702942363296SMatthew G. Knepley   case 1: numCorners = isSimplex ? 2 : 2; numHybridCorners = isSimplex ? 2 : 2; break;
703042363296SMatthew G. Knepley   case 2: numCorners = isSimplex ? 3 : 4; numHybridCorners = isSimplex ? 4 : 4; break;
703142363296SMatthew G. Knepley   case 3: numCorners = isSimplex ? 4 : 8; numHybridCorners = isSimplex ? 6 : 8; break;
7032ca8062c8SMatthew G. Knepley   default:
70338ccfff9cSToby Isaac     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle meshes of dimension %D", dim);
7034ca8062c8SMatthew G. Knepley   }
703558723a97SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
7036ca8062c8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
7037ca8062c8SMatthew G. Knepley   cMax = cMax >= 0 ? cMax : cEnd;
7038ca8062c8SMatthew G. Knepley   for (c = cStart; c < cMax; ++c) {
703958723a97SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
704058723a97SMatthew G. Knepley 
704158723a97SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
704258723a97SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
704358723a97SMatthew G. Knepley       const PetscInt p = closure[cl];
704458723a97SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
704558723a97SMatthew G. Knepley     }
704658723a97SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
70478ccfff9cSToby Isaac     if (coneSize != numCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has  %D vertices != %D", c, coneSize, numCorners);
7048ca8062c8SMatthew G. Knepley   }
704942363296SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
705042363296SMatthew G. Knepley     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
705142363296SMatthew G. Knepley 
705242363296SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
705342363296SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
705442363296SMatthew G. Knepley       const PetscInt p = closure[cl];
705542363296SMatthew G. Knepley       if ((p >= vStart) && (p < vEnd)) ++coneSize;
705642363296SMatthew G. Knepley     }
705742363296SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
70588ccfff9cSToby Isaac     if (coneSize > numHybridCorners) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Hybrid cell %D has  %D vertices > %D", c, coneSize, numHybridCorners);
705942363296SMatthew G. Knepley   }
7060ca8062c8SMatthew G. Knepley   PetscFunctionReturn(0);
7061ca8062c8SMatthew G. Knepley }
70629bf0dad6SMatthew G. Knepley 
70639bf0dad6SMatthew G. Knepley /*@
70649bf0dad6SMatthew 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
70659bf0dad6SMatthew G. Knepley 
70669bf0dad6SMatthew G. Knepley   Input Parameters:
70679bf0dad6SMatthew G. Knepley + dm - The DMPlex object
70689bf0dad6SMatthew G. Knepley - cellHeight - Normally 0
70699bf0dad6SMatthew G. Knepley 
70709bf0dad6SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
70719bf0dad6SMatthew G. Knepley 
70729bf0dad6SMatthew G. Knepley   Level: developer
70739bf0dad6SMatthew G. Knepley 
70747d5acc75SStefano Zampini .seealso: DMCreate(), DMPlexCheckSymmetry(), DMPlexCheckSkeleton()
70759bf0dad6SMatthew G. Knepley @*/
707625c50c26SVaclav Hapla PetscErrorCode DMPlexCheckFaces(DM dm, PetscInt cellHeight)
70779bf0dad6SMatthew G. Knepley {
70783554e41dSMatthew G. Knepley   PetscInt       pMax[4];
7079ab91121cSMatthew G. Knepley   PetscInt       dim, depth, vStart, vEnd, cStart, cEnd, c, h;
70809bf0dad6SMatthew G. Knepley   PetscErrorCode ierr;
70819bf0dad6SMatthew G. Knepley 
70829bf0dad6SMatthew G. Knepley   PetscFunctionBegin;
70839bf0dad6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7084c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7085ab91121cSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
70869bf0dad6SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
708725abba81SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], &pMax[dim-1], &pMax[1], &pMax[0]);CHKERRQ(ierr);
7088ab91121cSMatthew G. Knepley   for (h = cellHeight; h < PetscMin(depth, dim); ++h) {
70893554e41dSMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm, h, &cStart, &cEnd);CHKERRQ(ierr);
70903554e41dSMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
70919bf0dad6SMatthew G. Knepley       const PetscInt *cone, *ornt, *faces;
70929bf0dad6SMatthew G. Knepley       PetscInt        numFaces, faceSize, coneSize,f;
70939bf0dad6SMatthew G. Knepley       PetscInt       *closure = NULL, closureSize, cl, numCorners = 0;
70949bf0dad6SMatthew G. Knepley 
709525abba81SMatthew G. Knepley       if (pMax[dim-h] >= 0 && c >= pMax[dim-h]) continue;
70969bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
70979bf0dad6SMatthew G. Knepley       ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
70989bf0dad6SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, c, &ornt);CHKERRQ(ierr);
70999bf0dad6SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
71009bf0dad6SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
71019bf0dad6SMatthew G. Knepley         const PetscInt p = closure[cl];
71029bf0dad6SMatthew G. Knepley         if ((p >= vStart) && (p < vEnd)) closure[numCorners++] = p;
71039bf0dad6SMatthew G. Knepley       }
71043554e41dSMatthew G. Knepley       ierr = DMPlexGetRawFaces_Internal(dm, dim-h, numCorners, closure, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
71058ccfff9cSToby Isaac       if (coneSize != numFaces) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cell %D has %D faces but should have %D", c, coneSize, numFaces);
71069bf0dad6SMatthew G. Knepley       for (f = 0; f < numFaces; ++f) {
71079bf0dad6SMatthew G. Knepley         PetscInt *fclosure = NULL, fclosureSize, cl, fnumCorners = 0, v;
71089bf0dad6SMatthew G. Knepley 
71099bf0dad6SMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure_Internal(dm, cone[f], ornt[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
71109bf0dad6SMatthew G. Knepley         for (cl = 0; cl < fclosureSize*2; cl += 2) {
71119bf0dad6SMatthew G. Knepley           const PetscInt p = fclosure[cl];
71129bf0dad6SMatthew G. Knepley           if ((p >= vStart) && (p < vEnd)) fclosure[fnumCorners++] = p;
71139bf0dad6SMatthew G. Knepley         }
71148ccfff9cSToby 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);
71159bf0dad6SMatthew G. Knepley         for (v = 0; v < fnumCorners; ++v) {
71168ccfff9cSToby 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]);
71179bf0dad6SMatthew G. Knepley         }
71189bf0dad6SMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, cone[f], PETSC_TRUE, &fclosureSize, &fclosure);CHKERRQ(ierr);
71199bf0dad6SMatthew G. Knepley       }
71209bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreFaces_Internal(dm, dim, c, &numFaces, &faceSize, &faces);CHKERRQ(ierr);
71219bf0dad6SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
71229bf0dad6SMatthew G. Knepley     }
71233554e41dSMatthew G. Knepley   }
7124552f7358SJed Brown   PetscFunctionReturn(0);
7125552f7358SJed Brown }
71263913d7c8SMatthew G. Knepley 
7127bb6a34a8SMatthew G. Knepley /*@
7128bb6a34a8SMatthew G. Knepley   DMPlexCheckGeometry - Check the geometry of mesh cells
7129bb6a34a8SMatthew G. Knepley 
7130bb6a34a8SMatthew G. Knepley   Input Parameter:
7131bb6a34a8SMatthew G. Knepley . dm - The DMPlex object
7132bb6a34a8SMatthew G. Knepley 
7133bb6a34a8SMatthew G. Knepley   Note: This is a useful diagnostic when creating meshes programmatically.
7134bb6a34a8SMatthew G. Knepley 
7135bb6a34a8SMatthew G. Knepley   Level: developer
7136bb6a34a8SMatthew G. Knepley 
7137bb6a34a8SMatthew G. Knepley .seealso: DMCreate(), DMCheckSymmetry(), DMCheckSkeleton(), DMCheckFaces()
7138bb6a34a8SMatthew G. Knepley @*/
7139bb6a34a8SMatthew G. Knepley PetscErrorCode DMPlexCheckGeometry(DM dm)
7140bb6a34a8SMatthew G. Knepley {
7141bb6a34a8SMatthew G. Knepley   PetscReal      detJ, J[9], refVol = 1.0;
7142bb6a34a8SMatthew G. Knepley   PetscReal      vol;
71436210d0f3SStefano Zampini   PetscInt       dim, depth, d, cStart, cEnd, c, cMax;
7144bb6a34a8SMatthew G. Knepley   PetscErrorCode ierr;
7145bb6a34a8SMatthew G. Knepley 
7146bb6a34a8SMatthew G. Knepley   PetscFunctionBegin;
7147bb6a34a8SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
7148bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
7149bb6a34a8SMatthew G. Knepley   for (d = 0; d < dim; ++d) refVol *= 2.0;
7150bb6a34a8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
71516210d0f3SStefano Zampini   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
71526210d0f3SStefano Zampini   cMax = cMax < 0 ? cEnd : cMax;
71536210d0f3SStefano Zampini   for (c = cStart; c < cMax; ++c) {
7154bb6a34a8SMatthew G. Knepley     ierr = DMPlexComputeCellGeometryFEM(dm, c, NULL, NULL, J, NULL, &detJ);CHKERRQ(ierr);
7155bb6a34a8SMatthew G. Knepley     if (detJ <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted, |J| = %g", c, (double) detJ);
7156bb6a34a8SMatthew G. Knepley     ierr = PetscInfo2(dm, "Cell %D FEM Volume %g\n", c, (double) detJ*refVol);CHKERRQ(ierr);
7157bb6a34a8SMatthew G. Knepley     if (depth > 1) {
7158bb6a34a8SMatthew G. Knepley       ierr = DMPlexComputeCellGeometryFVM(dm, c, &vol, NULL, NULL);CHKERRQ(ierr);
7159bb6a34a8SMatthew G. Knepley       if (vol <= 0.0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %d is inverted, vol = %g", c, (double) vol);
7160bb6a34a8SMatthew G. Knepley       ierr = PetscInfo2(dm, "Cell %D FVM Volume %g\n", c, (double) vol);CHKERRQ(ierr);
7161bb6a34a8SMatthew G. Knepley     }
7162bb6a34a8SMatthew G. Knepley   }
7163bb6a34a8SMatthew G. Knepley   PetscFunctionReturn(0);
7164bb6a34a8SMatthew G. Knepley }
7165bb6a34a8SMatthew G. Knepley 
716603da9461SVaclav Hapla static PetscErrorCode DMPlexAreAllConePointsInArray_Private(DM dm, PetscInt p, PetscInt npoints, const PetscInt *points, PetscInt *missingPoint)
716703da9461SVaclav Hapla {
716803da9461SVaclav Hapla   PetscInt i,l,n;
716903da9461SVaclav Hapla   const PetscInt *cone;
717003da9461SVaclav Hapla   PetscErrorCode ierr;
717103da9461SVaclav Hapla 
717203da9461SVaclav Hapla   PetscFunctionBegin;
717303da9461SVaclav Hapla   *missingPoint = -1;
717403da9461SVaclav Hapla   ierr = DMPlexGetConeSize(dm, p, &n);CHKERRQ(ierr);
717503da9461SVaclav Hapla   ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
717603da9461SVaclav Hapla   for (i=0; i<n; i++) {
717703da9461SVaclav Hapla     ierr = PetscFindInt(cone[i], npoints, points, &l);CHKERRQ(ierr);
717803da9461SVaclav Hapla     if (l < 0) {
717903da9461SVaclav Hapla       *missingPoint = cone[i];
718003da9461SVaclav Hapla       break;
718103da9461SVaclav Hapla     }
718203da9461SVaclav Hapla   }
718303da9461SVaclav Hapla   PetscFunctionReturn(0);
718403da9461SVaclav Hapla }
718503da9461SVaclav Hapla 
718603da9461SVaclav Hapla /*@
7187e83a0d2dSVaclav Hapla   DMPlexCheckPointSF - Check that several necessary conditions are met for the point SF of this plex.
718803da9461SVaclav Hapla 
718903da9461SVaclav Hapla   Input Parameters:
719003da9461SVaclav Hapla . dm - The DMPlex object
719103da9461SVaclav Hapla 
7192e83a0d2dSVaclav Hapla   Notes:
7193e83a0d2dSVaclav Hapla   This is mainly intended for debugging/testing purposes.
71948918e3e2SVaclav Hapla   It currently checks only meshes with no partition overlapping.
719503da9461SVaclav Hapla 
719603da9461SVaclav Hapla   Level: developer
719703da9461SVaclav Hapla 
719803da9461SVaclav Hapla .seealso: DMGetPointSF(), DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
719903da9461SVaclav Hapla @*/
720003da9461SVaclav Hapla PetscErrorCode DMPlexCheckPointSF(DM dm)
720103da9461SVaclav Hapla {
7202*f0cfc026SVaclav Hapla   PetscSF         pointSF;
720303da9461SVaclav Hapla   PetscInt        d,depth,i,nleaves,p,plo,phi,missingPoint;
7204*f0cfc026SVaclav Hapla   PetscInt        nroots,overlap;
720503da9461SVaclav Hapla   const PetscInt *locals;
7206*f0cfc026SVaclav Hapla   PetscBool       distributed;
720703da9461SVaclav Hapla   PetscErrorCode  ierr;
720803da9461SVaclav Hapla 
720903da9461SVaclav Hapla   PetscFunctionBegin;
721003da9461SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
721103da9461SVaclav Hapla   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
7212*f0cfc026SVaclav Hapla   ierr = DMGetPointSF(dm, &pointSF);CHKERRQ(ierr);
7213*f0cfc026SVaclav Hapla   ierr = DMPlexIsDistributed(dm, &distributed);CHKERRQ(ierr);
7214*f0cfc026SVaclav Hapla   if (!distributed) PetscFunctionReturn(0);
7215*f0cfc026SVaclav Hapla   ierr = DMPlexGetOverlap(dm, &overlap);CHKERRQ(ierr);
7216*f0cfc026SVaclav Hapla   if (overlap) {
72178918e3e2SVaclav Hapla     ierr = PetscPrintf(PetscObjectComm((PetscObject)dm), "Warning: DMPlexCheckPointSF() is currently not implemented for meshes with partition overlapping");
72188918e3e2SVaclav Hapla     PetscFunctionReturn(0);
72198918e3e2SVaclav Hapla   }
7220*f0cfc026SVaclav Hapla   if (!pointSF) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but does not have PointSF attached");
7221*f0cfc026SVaclav Hapla   ierr = PetscSFGetGraph(pointSF, &nroots, &nleaves, &locals, NULL);CHKERRQ(ierr);
7222*f0cfc026SVaclav Hapla   if (nroots < 0) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "This DMPlex is distributed but its PointSF has no graph set");
722303da9461SVaclav Hapla 
7224ece87651SVaclav Hapla   /* 1) check there are no faces in 2D, cells in 3D, in interface */
722503da9461SVaclav Hapla   ierr = DMPlexGetVTKCellHeight(dm, &d);CHKERRQ(ierr);
722603da9461SVaclav Hapla   ierr = DMPlexGetHeightStratum(dm, d, &plo, &phi);CHKERRQ(ierr);
722703da9461SVaclav Hapla   for (i=0; i<nleaves; i++) {
722803da9461SVaclav Hapla     p = locals[i];
722903da9461SVaclav Hapla     if (p >= plo && p < phi) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point SF contains %d which is a cell",p);
723003da9461SVaclav Hapla   }
7231ece87651SVaclav Hapla 
7232ece87651SVaclav Hapla   /* 2) if some point is in interface, then all its cone points must be also in interface  */
7233ece87651SVaclav Hapla   for (i=0; i<nleaves; i++) {
7234ece87651SVaclav Hapla     p = locals[i];
7235ece87651SVaclav Hapla     ierr = DMPlexAreAllConePointsInArray_Private(dm, p, nleaves, locals, &missingPoint);CHKERRQ(ierr);
7236ece87651SVaclav Hapla     if (missingPoint >= 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "point SF contains %d but not %d from its cone",p,missingPoint);
7237ece87651SVaclav Hapla   }
723803da9461SVaclav Hapla   PetscFunctionReturn(0);
723903da9461SVaclav Hapla }
724003da9461SVaclav Hapla 
7241068a5610SStefano Zampini typedef struct cell_stats
7242068a5610SStefano Zampini {
7243068a5610SStefano Zampini   PetscReal min, max, sum, squaresum;
7244068a5610SStefano Zampini   PetscInt  count;
7245068a5610SStefano Zampini } cell_stats_t;
7246068a5610SStefano Zampini 
724725befc3bSSatish Balay static void MPIAPI cell_stats_reduce(void *a, void *b, int * len, MPI_Datatype *datatype)
7248068a5610SStefano Zampini {
7249068a5610SStefano Zampini   PetscInt i, N = *len;
7250068a5610SStefano Zampini 
7251068a5610SStefano Zampini   for (i = 0; i < N; i++) {
7252068a5610SStefano Zampini     cell_stats_t *A = (cell_stats_t *) a;
7253068a5610SStefano Zampini     cell_stats_t *B = (cell_stats_t *) b;
7254068a5610SStefano Zampini 
7255068a5610SStefano Zampini     B->min = PetscMin(A->min,B->min);
7256068a5610SStefano Zampini     B->max = PetscMax(A->max,B->max);
7257068a5610SStefano Zampini     B->sum += A->sum;
7258068a5610SStefano Zampini     B->squaresum += A->squaresum;
7259068a5610SStefano Zampini     B->count += A->count;
7260068a5610SStefano Zampini   }
7261068a5610SStefano Zampini }
7262068a5610SStefano Zampini 
7263068a5610SStefano Zampini /*@
726443fa8764SMatthew G. Knepley   DMPlexCheckCellShape - Checks the Jacobian of the mapping from reference to real cells and computes some minimal statistics.
7265068a5610SStefano Zampini 
72668261a58bSMatthew G. Knepley   Collective on dm
72678261a58bSMatthew G. Knepley 
7268068a5610SStefano Zampini   Input Parameters:
7269068a5610SStefano Zampini + dm        - The DMPlex object
727043fa8764SMatthew G. Knepley . output    - If true, statistics will be displayed on stdout
727143fa8764SMatthew G. Knepley - condLimit - Display all cells above this condition number, or PETSC_DETERMINE for no cell output
7272068a5610SStefano Zampini 
7273068a5610SStefano Zampini   Note: This is mainly intended for debugging/testing purposes.
7274068a5610SStefano Zampini 
7275068a5610SStefano Zampini   Level: developer
7276068a5610SStefano Zampini 
7277068a5610SStefano Zampini .seealso: DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces()
7278068a5610SStefano Zampini @*/
727943fa8764SMatthew G. Knepley PetscErrorCode DMPlexCheckCellShape(DM dm, PetscBool output, PetscReal condLimit)
7280068a5610SStefano Zampini {
7281068a5610SStefano Zampini   DM             dmCoarse;
728243fa8764SMatthew G. Knepley   cell_stats_t   stats, globalStats;
728343fa8764SMatthew G. Knepley   MPI_Comm       comm = PetscObjectComm((PetscObject)dm);
728443fa8764SMatthew G. Knepley   PetscReal      *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
728543fa8764SMatthew G. Knepley   PetscReal      limit = condLimit > 0 ? condLimit : PETSC_MAX_REAL;
728643fa8764SMatthew G. Knepley   PetscInt       cdim, cStart, cEnd, cMax, c, eStart, eEnd, count = 0;
728743fa8764SMatthew G. Knepley   PetscMPIInt    rank,size;
7288068a5610SStefano Zampini   PetscErrorCode ierr;
7289068a5610SStefano Zampini 
7290068a5610SStefano Zampini   PetscFunctionBegin;
7291068a5610SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7292068a5610SStefano Zampini   stats.min   = PETSC_MAX_REAL;
7293068a5610SStefano Zampini   stats.max   = PETSC_MIN_REAL;
7294068a5610SStefano Zampini   stats.sum   = stats.squaresum = 0.;
7295068a5610SStefano Zampini   stats.count = 0;
7296068a5610SStefano Zampini 
729743fa8764SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
729843fa8764SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
729943fa8764SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm,&cdim);CHKERRQ(ierr);
730043fa8764SMatthew G. Knepley   ierr = PetscMalloc2(PetscSqr(cdim), &J, PetscSqr(cdim), &invJ);CHKERRQ(ierr);
7301068a5610SStefano Zampini   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
730243fa8764SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm,1,&eStart,&eEnd);CHKERRQ(ierr);
7303068a5610SStefano Zampini   ierr = DMPlexGetHybridBounds(dm,&cMax,NULL,NULL,NULL);CHKERRQ(ierr);
7304068a5610SStefano Zampini   cMax = cMax < 0 ? cEnd : cMax;
7305068a5610SStefano Zampini   for (c = cStart; c < cMax; c++) {
7306068a5610SStefano Zampini     PetscInt  i;
7307068a5610SStefano Zampini     PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;
7308068a5610SStefano Zampini 
7309068a5610SStefano Zampini     ierr = DMPlexComputeCellGeometryAffineFEM(dm,c,NULL,J,invJ,&detJ);CHKERRQ(ierr);
7310068a5610SStefano Zampini     if (detJ < 0.0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mesh cell %D is inverted", c);
731143fa8764SMatthew G. Knepley     for (i = 0; i < PetscSqr(cdim); ++i) {
7312068a5610SStefano Zampini       frobJ    += J[i] * J[i];
7313068a5610SStefano Zampini       frobInvJ += invJ[i] * invJ[i];
7314068a5610SStefano Zampini     }
7315068a5610SStefano Zampini     cond2 = frobJ * frobInvJ;
7316068a5610SStefano Zampini     cond  = PetscSqrtReal(cond2);
7317068a5610SStefano Zampini 
7318068a5610SStefano Zampini     stats.min        = PetscMin(stats.min,cond);
7319068a5610SStefano Zampini     stats.max        = PetscMax(stats.max,cond);
7320068a5610SStefano Zampini     stats.sum       += cond;
7321068a5610SStefano Zampini     stats.squaresum += cond2;
7322068a5610SStefano Zampini     stats.count++;
73238261a58bSMatthew G. Knepley     if (output && cond > limit) {
732443fa8764SMatthew G. Knepley       PetscSection coordSection;
732543fa8764SMatthew G. Knepley       Vec          coordsLocal;
732643fa8764SMatthew G. Knepley       PetscScalar *coords = NULL;
732743fa8764SMatthew G. Knepley       PetscInt     Nv, d, clSize, cl, *closure = NULL;
732843fa8764SMatthew G. Knepley 
732943fa8764SMatthew G. Knepley       ierr = DMGetCoordinatesLocal(dm, &coordsLocal);CHKERRQ(ierr);
733043fa8764SMatthew G. Knepley       ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
733143fa8764SMatthew G. Knepley       ierr = DMPlexVecGetClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
7332087ef6b2SMatthew G. Knepley       ierr = PetscSynchronizedPrintf(comm, "[%d] Cell %D cond %g\n", rank, c, (double) cond);CHKERRQ(ierr);
733343fa8764SMatthew G. Knepley       for (i = 0; i < Nv/cdim; ++i) {
733443fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, "  Vertex %D: (", i);CHKERRQ(ierr);
733543fa8764SMatthew G. Knepley         for (d = 0; d < cdim; ++d) {
733643fa8764SMatthew G. Knepley           if (d > 0) {ierr = PetscSynchronizedPrintf(comm, ", ");CHKERRQ(ierr);}
733748afe810SSatish Balay           ierr = PetscSynchronizedPrintf(comm, "%g", (double) PetscRealPart(coords[i*cdim+d]));CHKERRQ(ierr);
733843fa8764SMatthew G. Knepley         }
733943fa8764SMatthew G. Knepley         ierr = PetscSynchronizedPrintf(comm, ")\n");CHKERRQ(ierr);
734043fa8764SMatthew G. Knepley       }
734143fa8764SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
734243fa8764SMatthew G. Knepley       for (cl = 0; cl < clSize*2; cl += 2) {
734343fa8764SMatthew G. Knepley         const PetscInt edge = closure[cl];
734443fa8764SMatthew G. Knepley 
734543fa8764SMatthew G. Knepley         if ((edge >= eStart) && (edge < eEnd)) {
734643fa8764SMatthew G. Knepley           PetscReal len;
734743fa8764SMatthew G. Knepley 
734843fa8764SMatthew G. Knepley           ierr = DMPlexComputeCellGeometryFVM(dm, edge, &len, NULL, NULL);CHKERRQ(ierr);
7349087ef6b2SMatthew G. Knepley           ierr = PetscSynchronizedPrintf(comm, "  Edge %D: length %g\n", edge, (double) len);CHKERRQ(ierr);
735043fa8764SMatthew G. Knepley         }
735143fa8764SMatthew G. Knepley       }
735243fa8764SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
735343fa8764SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(dm, coordSection, coordsLocal, c, &Nv, &coords);CHKERRQ(ierr);
735443fa8764SMatthew G. Knepley     }
7355068a5610SStefano Zampini   }
73568261a58bSMatthew G. Knepley   if (output) {ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);}
7357068a5610SStefano Zampini 
7358068a5610SStefano Zampini   if (size > 1) {
7359068a5610SStefano Zampini     PetscMPIInt   blockLengths[2] = {4,1};
7360068a5610SStefano Zampini     MPI_Aint      blockOffsets[2] = {offsetof(cell_stats_t,min),offsetof(cell_stats_t,count)};
7361068a5610SStefano Zampini     MPI_Datatype  blockTypes[2]   = {MPIU_REAL,MPIU_INT}, statType;
7362068a5610SStefano Zampini     MPI_Op        statReduce;
7363068a5610SStefano Zampini 
7364068a5610SStefano Zampini     ierr = MPI_Type_create_struct(2,blockLengths,blockOffsets,blockTypes,&statType);CHKERRQ(ierr);
7365068a5610SStefano Zampini     ierr = MPI_Type_commit(&statType);CHKERRQ(ierr);
7366068a5610SStefano Zampini     ierr = MPI_Op_create(cell_stats_reduce, PETSC_TRUE, &statReduce);CHKERRQ(ierr);
7367068a5610SStefano Zampini     ierr = MPI_Reduce(&stats,&globalStats,1,statType,statReduce,0,comm);CHKERRQ(ierr);
7368068a5610SStefano Zampini     ierr = MPI_Op_free(&statReduce);CHKERRQ(ierr);
7369068a5610SStefano Zampini     ierr = MPI_Type_free(&statType);CHKERRQ(ierr);
7370068a5610SStefano Zampini   } else {
7371580bdb30SBarry Smith     ierr = PetscArraycpy(&globalStats,&stats,1);CHKERRQ(ierr);
7372068a5610SStefano Zampini   }
7373068a5610SStefano Zampini   if (!rank) {
7374068a5610SStefano Zampini     count = globalStats.count;
7375068a5610SStefano Zampini     min   = globalStats.min;
7376068a5610SStefano Zampini     max   = globalStats.max;
7377068a5610SStefano Zampini     mean  = globalStats.sum / globalStats.count;
7378068a5610SStefano Zampini     stdev = globalStats.count > 1 ? PetscSqrtReal(PetscMax((globalStats.squaresum - globalStats.count * mean * mean) / (globalStats.count - 1),0)) : 0.0;
7379068a5610SStefano Zampini   }
7380068a5610SStefano Zampini 
7381068a5610SStefano Zampini   if (output) {
7382068a5610SStefano 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);
7383068a5610SStefano Zampini   }
7384068a5610SStefano Zampini   ierr = PetscFree2(J,invJ);CHKERRQ(ierr);
7385068a5610SStefano Zampini 
7386068a5610SStefano Zampini   ierr = DMGetCoarseDM(dm,&dmCoarse);CHKERRQ(ierr);
7387068a5610SStefano Zampini   if (dmCoarse) {
7388068a5610SStefano Zampini     PetscBool isplex;
7389068a5610SStefano Zampini 
7390068a5610SStefano Zampini     ierr = PetscObjectTypeCompare((PetscObject)dmCoarse,DMPLEX,&isplex);CHKERRQ(ierr);
7391068a5610SStefano Zampini     if (isplex) {
739243fa8764SMatthew G. Knepley       ierr = DMPlexCheckCellShape(dmCoarse,output,condLimit);CHKERRQ(ierr);
7393068a5610SStefano Zampini     }
7394068a5610SStefano Zampini   }
7395068a5610SStefano Zampini   PetscFunctionReturn(0);
7396068a5610SStefano Zampini }
7397068a5610SStefano Zampini 
7398bceba477SMatthew G. Knepley /* Pointwise interpolation
7399bceba477SMatthew G. Knepley      Just code FEM for now
7400bceba477SMatthew G. Knepley      u^f = I u^c
74014ca5e9f5SMatthew G. Knepley      sum_k u^f_k phi^f_k = I sum_j u^c_j phi^c_j
74024ca5e9f5SMatthew G. Knepley      u^f_i = sum_j psi^f_i I phi^c_j u^c_j
74034ca5e9f5SMatthew G. Knepley      I_{ij} = psi^f_i phi^c_j
7404bceba477SMatthew G. Knepley */
7405bceba477SMatthew G. Knepley PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling)
7406bceba477SMatthew G. Knepley {
7407bceba477SMatthew G. Knepley   PetscSection   gsc, gsf;
7408bceba477SMatthew G. Knepley   PetscInt       m, n;
7409a063dac3SMatthew G. Knepley   void          *ctx;
741068132eb9SMatthew G. Knepley   DM             cdm;
7411fd194bc8SStefano Zampini   PetscBool      regular, ismatis;
7412bceba477SMatthew G. Knepley   PetscErrorCode ierr;
7413bceba477SMatthew G. Knepley 
7414bceba477SMatthew G. Knepley   PetscFunctionBegin;
7415e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
7416bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
7417e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
7418bceba477SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
741968132eb9SMatthew G. Knepley 
7420fd194bc8SStefano Zampini   ierr = PetscStrcmp(dmCoarse->mattype, MATIS, &ismatis);CHKERRQ(ierr);
7421bceba477SMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), interpolation);CHKERRQ(ierr);
7422bceba477SMatthew G. Knepley   ierr = MatSetSizes(*interpolation, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
7423fd194bc8SStefano Zampini   ierr = MatSetType(*interpolation, ismatis ? MATAIJ : dmCoarse->mattype);CHKERRQ(ierr);
7424a063dac3SMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
742568132eb9SMatthew G. Knepley 
7426a8fb8f29SToby Isaac   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
742768132eb9SMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
742868132eb9SMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeInterpolatorNested(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
742968132eb9SMatthew G. Knepley   else                            {ierr = DMPlexComputeInterpolatorGeneral(dmCoarse, dmFine, *interpolation, ctx);CHKERRQ(ierr);}
743068132eb9SMatthew G. Knepley   ierr = MatViewFromOptions(*interpolation, NULL, "-interp_mat_view");CHKERRQ(ierr);
74314db47ee9SStefano Zampini   if (scaling) {
74325d1c2e58SMatthew G. Knepley     /* Use naive scaling */
74335d1c2e58SMatthew G. Knepley     ierr = DMCreateInterpolationScale(dmCoarse, dmFine, *interpolation, scaling);CHKERRQ(ierr);
74344db47ee9SStefano Zampini   }
7435a063dac3SMatthew G. Knepley   PetscFunctionReturn(0);
7436a063dac3SMatthew G. Knepley }
7437bceba477SMatthew G. Knepley 
74386dbf9973SLawrence Mitchell PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat)
7439a063dac3SMatthew G. Knepley {
744090748bafSMatthew G. Knepley   PetscErrorCode ierr;
74416dbf9973SLawrence Mitchell   VecScatter     ctx;
744290748bafSMatthew G. Knepley 
7443a063dac3SMatthew G. Knepley   PetscFunctionBegin;
74446dbf9973SLawrence Mitchell   ierr = DMPlexComputeInjectorFEM(dmCoarse, dmFine, &ctx, NULL);CHKERRQ(ierr);
74456dbf9973SLawrence Mitchell   ierr = MatCreateScatter(PetscObjectComm((PetscObject)ctx), ctx, mat);CHKERRQ(ierr);
74466dbf9973SLawrence Mitchell   ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr);
7447bceba477SMatthew G. Knepley   PetscFunctionReturn(0);
7448bceba477SMatthew G. Knepley }
7449bceba477SMatthew G. Knepley 
7450bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mass)
7451bd041c0cSMatthew G. Knepley {
7452bd041c0cSMatthew G. Knepley   PetscSection   gsc, gsf;
7453bd041c0cSMatthew G. Knepley   PetscInt       m, n;
7454bd041c0cSMatthew G. Knepley   void          *ctx;
7455bd041c0cSMatthew G. Knepley   DM             cdm;
7456bd041c0cSMatthew G. Knepley   PetscBool      regular;
7457bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
7458bd041c0cSMatthew G. Knepley 
7459bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
7460e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmFine, &gsf);CHKERRQ(ierr);
7461bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsf, &m);CHKERRQ(ierr);
7462e87a4003SBarry Smith   ierr = DMGetGlobalSection(dmCoarse, &gsc);CHKERRQ(ierr);
7463bd041c0cSMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(gsc, &n);CHKERRQ(ierr);
7464bd041c0cSMatthew G. Knepley 
7465bd041c0cSMatthew G. Knepley   ierr = MatCreate(PetscObjectComm((PetscObject) dmCoarse), mass);CHKERRQ(ierr);
7466bd041c0cSMatthew G. Knepley   ierr = MatSetSizes(*mass, m, n, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
7467bd041c0cSMatthew G. Knepley   ierr = MatSetType(*mass, dmCoarse->mattype);CHKERRQ(ierr);
7468bd041c0cSMatthew G. Knepley   ierr = DMGetApplicationContext(dmFine, &ctx);CHKERRQ(ierr);
7469bd041c0cSMatthew G. Knepley 
7470bd041c0cSMatthew G. Knepley   ierr = DMGetCoarseDM(dmFine, &cdm);CHKERRQ(ierr);
7471bd041c0cSMatthew G. Knepley   ierr = DMPlexGetRegularRefinement(dmFine, &regular);CHKERRQ(ierr);
7472bd041c0cSMatthew G. Knepley   if (regular && cdm == dmCoarse) {ierr = DMPlexComputeMassMatrixNested(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
7473bd041c0cSMatthew G. Knepley   else                            {ierr = DMPlexComputeMassMatrixGeneral(dmCoarse, dmFine, *mass, ctx);CHKERRQ(ierr);}
7474bd041c0cSMatthew G. Knepley   ierr = MatViewFromOptions(*mass, NULL, "-mass_mat_view");CHKERRQ(ierr);
7475bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
7476bd041c0cSMatthew G. Knepley }
7477bd041c0cSMatthew G. Knepley 
74780aef6b92SMatthew G. Knepley /*@
74790aef6b92SMatthew G. Knepley   DMPlexGetRegularRefinement - Get the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
74800aef6b92SMatthew G. Knepley 
74810aef6b92SMatthew G. Knepley   Input Parameter:
74820aef6b92SMatthew G. Knepley . dm - The DMPlex object
74830aef6b92SMatthew G. Knepley 
74840aef6b92SMatthew G. Knepley   Output Parameter:
74850aef6b92SMatthew G. Knepley . regular - The flag
74860aef6b92SMatthew G. Knepley 
74870aef6b92SMatthew G. Knepley   Level: intermediate
74880aef6b92SMatthew G. Knepley 
74890aef6b92SMatthew G. Knepley .seealso: DMPlexSetRegularRefinement()
74900aef6b92SMatthew G. Knepley @*/
74910aef6b92SMatthew G. Knepley PetscErrorCode DMPlexGetRegularRefinement(DM dm, PetscBool *regular)
74920aef6b92SMatthew G. Knepley {
74930aef6b92SMatthew G. Knepley   PetscFunctionBegin;
74940aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74950aef6b92SMatthew G. Knepley   PetscValidPointer(regular, 2);
74960aef6b92SMatthew G. Knepley   *regular = ((DM_Plex *) dm->data)->regularRefinement;
74970aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
74980aef6b92SMatthew G. Knepley }
74990aef6b92SMatthew G. Knepley 
75000aef6b92SMatthew G. Knepley /*@
75010aef6b92SMatthew G. Knepley   DMPlexSetRegularRefinement - Set the flag indicating that this mesh was obtained by regular refinement from its coarse mesh
75020aef6b92SMatthew G. Knepley 
75030aef6b92SMatthew G. Knepley   Input Parameters:
75040aef6b92SMatthew G. Knepley + dm - The DMPlex object
75050aef6b92SMatthew G. Knepley - regular - The flag
75060aef6b92SMatthew G. Knepley 
75070aef6b92SMatthew G. Knepley   Level: intermediate
75080aef6b92SMatthew G. Knepley 
75090aef6b92SMatthew G. Knepley .seealso: DMPlexGetRegularRefinement()
75100aef6b92SMatthew G. Knepley @*/
75110aef6b92SMatthew G. Knepley PetscErrorCode DMPlexSetRegularRefinement(DM dm, PetscBool regular)
75120aef6b92SMatthew G. Knepley {
75130aef6b92SMatthew G. Knepley   PetscFunctionBegin;
75140aef6b92SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
75150aef6b92SMatthew G. Knepley   ((DM_Plex *) dm->data)->regularRefinement = regular;
75160aef6b92SMatthew G. Knepley   PetscFunctionReturn(0);
75170aef6b92SMatthew G. Knepley }
75180aef6b92SMatthew G. Knepley 
7519f7c74593SToby Isaac /* anchors */
7520a68b90caSToby Isaac /*@
7521f7c74593SToby Isaac   DMPlexGetAnchors - Get the layout of the anchor (point-to-point) constraints.  Typically, the user will not have to
7522f7c74593SToby Isaac   call DMPlexGetAnchors() directly: if there are anchors, then DMPlexGetAnchors() is called during DMGetConstraints().
7523a68b90caSToby Isaac 
7524e228b242SToby Isaac   not collective
7525a68b90caSToby Isaac 
7526a68b90caSToby Isaac   Input Parameters:
7527a68b90caSToby Isaac . dm - The DMPlex object
7528a68b90caSToby Isaac 
7529a68b90caSToby Isaac   Output Parameters:
7530a68b90caSToby Isaac + anchorSection - If not NULL, set to the section describing which points anchor the constrained points.
7531a68b90caSToby Isaac - anchorIS - If not NULL, set to the list of anchors indexed by anchorSection
7532a68b90caSToby Isaac 
7533a68b90caSToby Isaac 
7534a68b90caSToby Isaac   Level: intermediate
7535a68b90caSToby Isaac 
7536f7c74593SToby Isaac .seealso: DMPlexSetAnchors(), DMGetConstraints(), DMSetConstraints()
7537a68b90caSToby Isaac @*/
7538a17985deSToby Isaac PetscErrorCode DMPlexGetAnchors(DM dm, PetscSection *anchorSection, IS *anchorIS)
7539a68b90caSToby Isaac {
7540a68b90caSToby Isaac   DM_Plex *plex = (DM_Plex *)dm->data;
754141e6d900SToby Isaac   PetscErrorCode ierr;
7542a68b90caSToby Isaac 
7543a68b90caSToby Isaac   PetscFunctionBegin;
7544a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
754541e6d900SToby Isaac   if (!plex->anchorSection && !plex->anchorIS && plex->createanchors) {ierr = (*plex->createanchors)(dm);CHKERRQ(ierr);}
7546a68b90caSToby Isaac   if (anchorSection) *anchorSection = plex->anchorSection;
7547a68b90caSToby Isaac   if (anchorIS) *anchorIS = plex->anchorIS;
7548a68b90caSToby Isaac   PetscFunctionReturn(0);
7549a68b90caSToby Isaac }
7550a68b90caSToby Isaac 
7551a68b90caSToby Isaac /*@
7552f7c74593SToby Isaac   DMPlexSetAnchors - Set the layout of the local anchor (point-to-point) constraints.  Unlike boundary conditions,
7553f7c74593SToby Isaac   when a point's degrees of freedom in a section are constrained to an outside value, the anchor constraints set a
7554a68b90caSToby Isaac   point's degrees of freedom to be a linear combination of other points' degrees of freedom.
7555a68b90caSToby Isaac 
7556a17985deSToby Isaac   After specifying the layout of constraints with DMPlexSetAnchors(), one specifies the constraints by calling
7557f7c74593SToby Isaac   DMGetConstraints() and filling in the entries in the constraint matrix.
7558a68b90caSToby Isaac 
7559e228b242SToby Isaac   collective on dm
7560a68b90caSToby Isaac 
7561a68b90caSToby Isaac   Input Parameters:
7562a68b90caSToby Isaac + dm - The DMPlex object
7563e228b242SToby 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).
7564e228b242SToby Isaac - anchorIS - The list of all anchor points.  Must have a local communicator (PETSC_COMM_SELF or derivative).
7565a68b90caSToby Isaac 
7566a68b90caSToby Isaac   The reference counts of anchorSection and anchorIS are incremented.
7567a68b90caSToby Isaac 
7568a68b90caSToby Isaac   Level: intermediate
7569a68b90caSToby Isaac 
7570f7c74593SToby Isaac .seealso: DMPlexGetAnchors(), DMGetConstraints(), DMSetConstraints()
7571a68b90caSToby Isaac @*/
7572a17985deSToby Isaac PetscErrorCode DMPlexSetAnchors(DM dm, PetscSection anchorSection, IS anchorIS)
7573a68b90caSToby Isaac {
7574a68b90caSToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
7575e228b242SToby Isaac   PetscMPIInt    result;
7576a68b90caSToby Isaac   PetscErrorCode ierr;
7577a68b90caSToby Isaac 
7578a68b90caSToby Isaac   PetscFunctionBegin;
7579a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7580e228b242SToby Isaac   if (anchorSection) {
7581e228b242SToby Isaac     PetscValidHeaderSpecific(anchorSection,PETSC_SECTION_CLASSID,2);
7582e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorSection),&result);CHKERRQ(ierr);
7583f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor section must have local communicator");
7584e228b242SToby Isaac   }
7585e228b242SToby Isaac   if (anchorIS) {
7586e228b242SToby Isaac     PetscValidHeaderSpecific(anchorIS,IS_CLASSID,3);
7587e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)anchorIS),&result);CHKERRQ(ierr);
7588f6a3d38cSToby Isaac     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"anchor IS must have local communicator");
7589e228b242SToby Isaac   }
7590a68b90caSToby Isaac 
7591a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorSection);CHKERRQ(ierr);
7592a68b90caSToby Isaac   ierr = PetscSectionDestroy(&plex->anchorSection);CHKERRQ(ierr);
7593a68b90caSToby Isaac   plex->anchorSection = anchorSection;
7594a68b90caSToby Isaac 
7595a68b90caSToby Isaac   ierr = PetscObjectReference((PetscObject)anchorIS);CHKERRQ(ierr);
7596a68b90caSToby Isaac   ierr = ISDestroy(&plex->anchorIS);CHKERRQ(ierr);
7597a68b90caSToby Isaac   plex->anchorIS = anchorIS;
7598a68b90caSToby Isaac 
7599a68b90caSToby Isaac #if defined(PETSC_USE_DEBUG)
7600a68b90caSToby Isaac   if (anchorIS && anchorSection) {
7601a68b90caSToby Isaac     PetscInt size, a, pStart, pEnd;
7602a68b90caSToby Isaac     const PetscInt *anchors;
7603a68b90caSToby Isaac 
7604a68b90caSToby Isaac     ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
7605a68b90caSToby Isaac     ierr = ISGetLocalSize(anchorIS,&size);CHKERRQ(ierr);
7606a68b90caSToby Isaac     ierr = ISGetIndices(anchorIS,&anchors);CHKERRQ(ierr);
7607a68b90caSToby Isaac     for (a = 0; a < size; a++) {
7608a68b90caSToby Isaac       PetscInt p;
7609a68b90caSToby Isaac 
7610a68b90caSToby Isaac       p = anchors[a];
7611a68b90caSToby Isaac       if (p >= pStart && p < pEnd) {
7612a68b90caSToby Isaac         PetscInt dof;
7613a68b90caSToby Isaac 
7614a68b90caSToby Isaac         ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
7615a68b90caSToby Isaac         if (dof) {
7616a68b90caSToby Isaac           PetscErrorCode ierr2;
7617a68b90caSToby Isaac 
7618a68b90caSToby Isaac           ierr2 = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr2);
76198ccfff9cSToby Isaac           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Point %D cannot be constrained and an anchor",p);
7620a68b90caSToby Isaac         }
7621a68b90caSToby Isaac       }
7622a68b90caSToby Isaac     }
7623a68b90caSToby Isaac     ierr = ISRestoreIndices(anchorIS,&anchors);CHKERRQ(ierr);
7624a68b90caSToby Isaac   }
7625a68b90caSToby Isaac #endif
7626f7c74593SToby Isaac   /* reset the generic constraints */
7627f7c74593SToby Isaac   ierr = DMSetDefaultConstraints(dm,NULL,NULL);CHKERRQ(ierr);
7628a68b90caSToby Isaac   PetscFunctionReturn(0);
7629a68b90caSToby Isaac }
7630a68b90caSToby Isaac 
7631f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintSection_Anchors(DM dm, PetscSection section, PetscSection *cSec)
7632a68b90caSToby Isaac {
7633f7c74593SToby Isaac   PetscSection anchorSection;
76346995de1eSToby Isaac   PetscInt pStart, pEnd, sStart, sEnd, p, dof, numFields, f;
7635a68b90caSToby Isaac   PetscErrorCode ierr;
7636a68b90caSToby Isaac 
7637a68b90caSToby Isaac   PetscFunctionBegin;
7638a68b90caSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7639a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
7640e228b242SToby Isaac   ierr = PetscSectionCreate(PETSC_COMM_SELF,cSec);CHKERRQ(ierr);
7641a68b90caSToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
76426995de1eSToby Isaac   if (numFields) {
7643719ab38cSToby Isaac     PetscInt f;
7644a68b90caSToby Isaac     ierr = PetscSectionSetNumFields(*cSec,numFields);CHKERRQ(ierr);
7645719ab38cSToby Isaac 
7646719ab38cSToby Isaac     for (f = 0; f < numFields; f++) {
7647719ab38cSToby Isaac       PetscInt numComp;
7648719ab38cSToby Isaac 
7649719ab38cSToby Isaac       ierr = PetscSectionGetFieldComponents(section,f,&numComp);CHKERRQ(ierr);
7650719ab38cSToby Isaac       ierr = PetscSectionSetFieldComponents(*cSec,f,numComp);CHKERRQ(ierr);
7651719ab38cSToby Isaac     }
76526995de1eSToby Isaac   }
7653a68b90caSToby Isaac   ierr = PetscSectionGetChart(anchorSection,&pStart,&pEnd);CHKERRQ(ierr);
76546995de1eSToby Isaac   ierr = PetscSectionGetChart(section,&sStart,&sEnd);CHKERRQ(ierr);
76556995de1eSToby Isaac   pStart = PetscMax(pStart,sStart);
76566995de1eSToby Isaac   pEnd   = PetscMin(pEnd,sEnd);
76576995de1eSToby Isaac   pEnd   = PetscMax(pStart,pEnd);
7658a68b90caSToby Isaac   ierr = PetscSectionSetChart(*cSec,pStart,pEnd);CHKERRQ(ierr);
7659a68b90caSToby Isaac   for (p = pStart; p < pEnd; p++) {
7660a68b90caSToby Isaac     ierr = PetscSectionGetDof(anchorSection,p,&dof);CHKERRQ(ierr);
7661a68b90caSToby Isaac     if (dof) {
7662a68b90caSToby Isaac       ierr = PetscSectionGetDof(section,p,&dof);CHKERRQ(ierr);
7663a68b90caSToby Isaac       ierr = PetscSectionSetDof(*cSec,p,dof);CHKERRQ(ierr);
7664a68b90caSToby Isaac       for (f = 0; f < numFields; f++) {
7665a68b90caSToby Isaac         ierr = PetscSectionGetFieldDof(section,p,f,&dof);CHKERRQ(ierr);
7666a68b90caSToby Isaac         ierr = PetscSectionSetFieldDof(*cSec,p,f,dof);CHKERRQ(ierr);
7667a68b90caSToby Isaac       }
7668a68b90caSToby Isaac     }
7669a68b90caSToby Isaac   }
7670a68b90caSToby Isaac   ierr = PetscSectionSetUp(*cSec);CHKERRQ(ierr);
7671a68b90caSToby Isaac   PetscFunctionReturn(0);
7672a68b90caSToby Isaac }
7673a68b90caSToby Isaac 
7674f7c74593SToby Isaac static PetscErrorCode DMPlexCreateConstraintMatrix_Anchors(DM dm, PetscSection section, PetscSection cSec, Mat *cMat)
7675a68b90caSToby Isaac {
7676f7c74593SToby Isaac   PetscSection aSec;
76770ac89760SToby Isaac   PetscInt pStart, pEnd, p, dof, aDof, aOff, off, nnz, annz, m, n, q, a, offset, *i, *j;
76780ac89760SToby Isaac   const PetscInt *anchors;
76790ac89760SToby Isaac   PetscInt numFields, f;
768066ad2231SToby Isaac   IS aIS;
76810ac89760SToby Isaac   PetscErrorCode ierr;
76820ac89760SToby Isaac 
76830ac89760SToby Isaac   PetscFunctionBegin;
76840ac89760SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
76850ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(cSec, &m);CHKERRQ(ierr);
76860ac89760SToby Isaac   ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
76870ac89760SToby Isaac   ierr = MatCreate(PETSC_COMM_SELF,cMat);CHKERRQ(ierr);
76880ac89760SToby Isaac   ierr = MatSetSizes(*cMat,m,n,m,n);CHKERRQ(ierr);
7689302440fdSBarry Smith   ierr = MatSetType(*cMat,MATSEQAIJ);CHKERRQ(ierr);
7690a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&aSec,&aIS);CHKERRQ(ierr);
769166ad2231SToby Isaac   ierr = ISGetIndices(aIS,&anchors);CHKERRQ(ierr);
76926995de1eSToby Isaac   /* cSec will be a subset of aSec and section */
76936995de1eSToby Isaac   ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
76940ac89760SToby Isaac   ierr = PetscMalloc1(m+1,&i);CHKERRQ(ierr);
76950ac89760SToby Isaac   i[0] = 0;
76960ac89760SToby Isaac   ierr = PetscSectionGetNumFields(section,&numFields);CHKERRQ(ierr);
76970ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
7698f19733c5SToby Isaac     PetscInt rDof, rOff, r;
7699f19733c5SToby Isaac 
7700f19733c5SToby Isaac     ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
7701f19733c5SToby Isaac     if (!rDof) continue;
7702f19733c5SToby Isaac     ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
77030ac89760SToby Isaac     if (numFields) {
77040ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
77050ac89760SToby Isaac         annz = 0;
7706f19733c5SToby Isaac         for (r = 0; r < rDof; r++) {
7707f19733c5SToby Isaac           a = anchors[rOff + r];
77080ac89760SToby Isaac           ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
77090ac89760SToby Isaac           annz += aDof;
77100ac89760SToby Isaac         }
77110ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
77120ac89760SToby Isaac         ierr = PetscSectionGetFieldOffset(cSec,p,f,&off);CHKERRQ(ierr);
77130ac89760SToby Isaac         for (q = 0; q < dof; q++) {
77140ac89760SToby Isaac           i[off + q + 1] = i[off + q] + annz;
77150ac89760SToby Isaac         }
77160ac89760SToby Isaac       }
77170ac89760SToby Isaac     }
77180ac89760SToby Isaac     else {
77190ac89760SToby Isaac       annz = 0;
77200ac89760SToby Isaac       for (q = 0; q < dof; q++) {
77210ac89760SToby Isaac         a = anchors[off + q];
77220ac89760SToby Isaac         ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
77230ac89760SToby Isaac         annz += aDof;
77240ac89760SToby Isaac       }
77250ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
77260ac89760SToby Isaac       ierr = PetscSectionGetOffset(cSec,p,&off);CHKERRQ(ierr);
77270ac89760SToby Isaac       for (q = 0; q < dof; q++) {
77280ac89760SToby Isaac         i[off + q + 1] = i[off + q] + annz;
77290ac89760SToby Isaac       }
77300ac89760SToby Isaac     }
77310ac89760SToby Isaac   }
77320ac89760SToby Isaac   nnz = i[m];
77330ac89760SToby Isaac   ierr = PetscMalloc1(nnz,&j);CHKERRQ(ierr);
77340ac89760SToby Isaac   offset = 0;
77350ac89760SToby Isaac   for (p = pStart; p < pEnd; p++) {
77360ac89760SToby Isaac     if (numFields) {
77370ac89760SToby Isaac       for (f = 0; f < numFields; f++) {
77380ac89760SToby Isaac         ierr = PetscSectionGetFieldDof(cSec,p,f,&dof);CHKERRQ(ierr);
77390ac89760SToby Isaac         for (q = 0; q < dof; q++) {
77400ac89760SToby Isaac           PetscInt rDof, rOff, r;
77410ac89760SToby Isaac           ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
77420ac89760SToby Isaac           ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
77430ac89760SToby Isaac           for (r = 0; r < rDof; r++) {
77440ac89760SToby Isaac             PetscInt s;
77450ac89760SToby Isaac 
77460ac89760SToby Isaac             a = anchors[rOff + r];
77470ac89760SToby Isaac             ierr = PetscSectionGetFieldDof(section,a,f,&aDof);CHKERRQ(ierr);
77480ac89760SToby Isaac             ierr = PetscSectionGetFieldOffset(section,a,f,&aOff);CHKERRQ(ierr);
77490ac89760SToby Isaac             for (s = 0; s < aDof; s++) {
77500ac89760SToby Isaac               j[offset++] = aOff + s;
77510ac89760SToby Isaac             }
77520ac89760SToby Isaac           }
77530ac89760SToby Isaac         }
77540ac89760SToby Isaac       }
77550ac89760SToby Isaac     }
77560ac89760SToby Isaac     else {
77570ac89760SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
77580ac89760SToby Isaac       for (q = 0; q < dof; q++) {
77590ac89760SToby Isaac         PetscInt rDof, rOff, r;
77600ac89760SToby Isaac         ierr = PetscSectionGetDof(aSec,p,&rDof);CHKERRQ(ierr);
77610ac89760SToby Isaac         ierr = PetscSectionGetOffset(aSec,p,&rOff);CHKERRQ(ierr);
77620ac89760SToby Isaac         for (r = 0; r < rDof; r++) {
77630ac89760SToby Isaac           PetscInt s;
77640ac89760SToby Isaac 
77650ac89760SToby Isaac           a = anchors[rOff + r];
77660ac89760SToby Isaac           ierr = PetscSectionGetDof(section,a,&aDof);CHKERRQ(ierr);
77670ac89760SToby Isaac           ierr = PetscSectionGetOffset(section,a,&aOff);CHKERRQ(ierr);
77680ac89760SToby Isaac           for (s = 0; s < aDof; s++) {
77690ac89760SToby Isaac             j[offset++] = aOff + s;
77700ac89760SToby Isaac           }
77710ac89760SToby Isaac         }
77720ac89760SToby Isaac       }
77730ac89760SToby Isaac     }
77740ac89760SToby Isaac   }
77750ac89760SToby Isaac   ierr = MatSeqAIJSetPreallocationCSR(*cMat,i,j,NULL);CHKERRQ(ierr);
777625570a81SToby Isaac   ierr = PetscFree(i);CHKERRQ(ierr);
777725570a81SToby Isaac   ierr = PetscFree(j);CHKERRQ(ierr);
777866ad2231SToby Isaac   ierr = ISRestoreIndices(aIS,&anchors);CHKERRQ(ierr);
77790ac89760SToby Isaac   PetscFunctionReturn(0);
77800ac89760SToby Isaac }
77810ac89760SToby Isaac 
778266ad2231SToby Isaac PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm)
778366ad2231SToby Isaac {
7784f7c74593SToby Isaac   DM_Plex        *plex = (DM_Plex *)dm->data;
7785f7c74593SToby Isaac   PetscSection   anchorSection, section, cSec;
778666ad2231SToby Isaac   Mat            cMat;
778766ad2231SToby Isaac   PetscErrorCode ierr;
778866ad2231SToby Isaac 
778966ad2231SToby Isaac   PetscFunctionBegin;
779066ad2231SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7791a17985deSToby Isaac   ierr = DMPlexGetAnchors(dm,&anchorSection,NULL);CHKERRQ(ierr);
779266ad2231SToby Isaac   if (anchorSection) {
779344a7f3ddSMatthew G. Knepley     PetscInt Nf;
7794e228b242SToby Isaac 
779592fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
7796f7c74593SToby Isaac     ierr = DMPlexCreateConstraintSection_Anchors(dm,section,&cSec);CHKERRQ(ierr);
7797f7c74593SToby Isaac     ierr = DMPlexCreateConstraintMatrix_Anchors(dm,section,cSec,&cMat);CHKERRQ(ierr);
779844a7f3ddSMatthew G. Knepley     ierr = DMGetNumFields(dm,&Nf);CHKERRQ(ierr);
779944a7f3ddSMatthew G. Knepley     if (Nf && plex->computeanchormatrix) {ierr = (*plex->computeanchormatrix)(dm,section,cSec,cMat);CHKERRQ(ierr);}
780066ad2231SToby Isaac     ierr = DMSetDefaultConstraints(dm,cSec,cMat);CHKERRQ(ierr);
780166ad2231SToby Isaac     ierr = PetscSectionDestroy(&cSec);CHKERRQ(ierr);
780266ad2231SToby Isaac     ierr = MatDestroy(&cMat);CHKERRQ(ierr);
780366ad2231SToby Isaac   }
780466ad2231SToby Isaac   PetscFunctionReturn(0);
780566ad2231SToby Isaac }
7806a93c429eSMatthew G. Knepley 
7807a93c429eSMatthew G. Knepley PetscErrorCode DMCreateSubDomainDM_Plex(DM dm, DMLabel label, PetscInt value, IS *is, DM *subdm)
7808a93c429eSMatthew G. Knepley {
7809a93c429eSMatthew G. Knepley   IS             subis;
7810a93c429eSMatthew G. Knepley   PetscSection   section, subsection;
7811a93c429eSMatthew G. Knepley   PetscErrorCode ierr;
7812a93c429eSMatthew G. Knepley 
7813a93c429eSMatthew G. Knepley   PetscFunctionBegin;
781492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
7815a93c429eSMatthew G. Knepley   if (!section) SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting subdomain");
7816a93c429eSMatthew G. Knepley   if (!subdm)   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Must set output subDM for splitting subdomain");
7817a93c429eSMatthew G. Knepley   /* Create subdomain */
7818a93c429eSMatthew G. Knepley   ierr = DMPlexFilter(dm, label, value, subdm);CHKERRQ(ierr);
7819a93c429eSMatthew G. Knepley   /* Create submodel */
7820a93c429eSMatthew G. Knepley   ierr = DMPlexCreateSubpointIS(*subdm, &subis);CHKERRQ(ierr);
7821a93c429eSMatthew G. Knepley   ierr = PetscSectionCreateSubmeshSection(section, subis, &subsection);CHKERRQ(ierr);
7822a93c429eSMatthew G. Knepley   ierr = ISDestroy(&subis);CHKERRQ(ierr);
782392fd8e1eSJed Brown   ierr = DMSetLocalSection(*subdm, subsection);CHKERRQ(ierr);
7824a93c429eSMatthew G. Knepley   ierr = PetscSectionDestroy(&subsection);CHKERRQ(ierr);
7825e5e52638SMatthew G. Knepley   ierr = DMCopyDisc(dm, *subdm);CHKERRQ(ierr);
7826a93c429eSMatthew G. Knepley   /* Create map from submodel to global model */
7827a93c429eSMatthew G. Knepley   if (is) {
7828a93c429eSMatthew G. Knepley     PetscSection    sectionGlobal, subsectionGlobal;
7829a93c429eSMatthew G. Knepley     IS              spIS;
7830a93c429eSMatthew G. Knepley     const PetscInt *spmap;
7831a93c429eSMatthew G. Knepley     PetscInt       *subIndices;
7832a93c429eSMatthew G. Knepley     PetscInt        subSize = 0, subOff = 0, pStart, pEnd, p;
7833a93c429eSMatthew G. Knepley     PetscInt        Nf, f, bs = -1, bsLocal[2], bsMinMax[2];
7834a93c429eSMatthew G. Knepley 
7835a93c429eSMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(*subdm, &spIS);CHKERRQ(ierr);
7836a93c429eSMatthew G. Knepley     ierr = ISGetIndices(spIS, &spmap);CHKERRQ(ierr);
7837a93c429eSMatthew G. Knepley     ierr = PetscSectionGetNumFields(section, &Nf);CHKERRQ(ierr);
78386f0eb057SJed Brown     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
78396f0eb057SJed Brown     ierr = DMGetGlobalSection(*subdm, &subsectionGlobal);CHKERRQ(ierr);
7840a93c429eSMatthew G. Knepley     ierr = PetscSectionGetChart(subsection, &pStart, &pEnd);CHKERRQ(ierr);
7841a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
7842a93c429eSMatthew G. Knepley       PetscInt gdof, pSubSize  = 0;
7843a93c429eSMatthew G. Knepley 
7844a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
7845a93c429eSMatthew G. Knepley       if (gdof > 0) {
7846a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
7847a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof;
7848a93c429eSMatthew G. Knepley 
7849a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldDof(subsection, p, f, &fdof);CHKERRQ(ierr);
7850a93c429eSMatthew G. Knepley           ierr     = PetscSectionGetFieldConstraintDof(subsection, p, f, &fcdof);CHKERRQ(ierr);
7851a93c429eSMatthew G. Knepley           pSubSize += fdof-fcdof;
7852a93c429eSMatthew G. Knepley         }
7853a93c429eSMatthew G. Knepley         subSize += pSubSize;
7854a93c429eSMatthew G. Knepley         if (pSubSize) {
7855a93c429eSMatthew G. Knepley           if (bs < 0) {
7856a93c429eSMatthew G. Knepley             bs = pSubSize;
7857a93c429eSMatthew G. Knepley           } else if (bs != pSubSize) {
7858a93c429eSMatthew G. Knepley             /* Layout does not admit a pointwise block size */
7859a93c429eSMatthew G. Knepley             bs = 1;
7860a93c429eSMatthew G. Knepley           }
7861a93c429eSMatthew G. Knepley         }
7862a93c429eSMatthew G. Knepley       }
7863a93c429eSMatthew G. Knepley     }
7864a93c429eSMatthew G. Knepley     /* Must have same blocksize on all procs (some might have no points) */
7865a93c429eSMatthew G. Knepley     bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
7866a93c429eSMatthew G. Knepley     ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
7867a93c429eSMatthew G. Knepley     if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
7868a93c429eSMatthew G. Knepley     else                            {bs = bsMinMax[0];}
7869a93c429eSMatthew G. Knepley     ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr);
7870a93c429eSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
7871a93c429eSMatthew G. Knepley       PetscInt gdof, goff;
7872a93c429eSMatthew G. Knepley 
7873a93c429eSMatthew G. Knepley       ierr = PetscSectionGetDof(subsectionGlobal, p, &gdof);CHKERRQ(ierr);
7874a93c429eSMatthew G. Knepley       if (gdof > 0) {
7875a93c429eSMatthew G. Knepley         const PetscInt point = spmap[p];
7876a93c429eSMatthew G. Knepley 
7877a93c429eSMatthew G. Knepley         ierr = PetscSectionGetOffset(sectionGlobal, point, &goff);CHKERRQ(ierr);
7878a93c429eSMatthew G. Knepley         for (f = 0; f < Nf; ++f) {
7879a93c429eSMatthew G. Knepley           PetscInt fdof, fcdof, fc, f2, poff = 0;
7880a93c429eSMatthew G. Knepley 
7881a93c429eSMatthew G. Knepley           /* Can get rid of this loop by storing field information in the global section */
7882a93c429eSMatthew G. Knepley           for (f2 = 0; f2 < f; ++f2) {
7883a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldDof(section, p, f2, &fdof);CHKERRQ(ierr);
7884a93c429eSMatthew G. Knepley             ierr  = PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);CHKERRQ(ierr);
7885a93c429eSMatthew G. Knepley             poff += fdof-fcdof;
7886a93c429eSMatthew G. Knepley           }
7887a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
7888a93c429eSMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
7889a93c429eSMatthew G. Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
7890a93c429eSMatthew G. Knepley             subIndices[subOff] = goff+poff+fc;
7891a93c429eSMatthew G. Knepley           }
7892a93c429eSMatthew G. Knepley         }
7893a93c429eSMatthew G. Knepley       }
7894a93c429eSMatthew G. Knepley     }
7895a93c429eSMatthew G. Knepley     ierr = ISRestoreIndices(spIS, &spmap);CHKERRQ(ierr);
7896a93c429eSMatthew G. Knepley     ierr = ISDestroy(&spIS);CHKERRQ(ierr);
7897a93c429eSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);CHKERRQ(ierr);
7898a93c429eSMatthew G. Knepley     if (bs > 1) {
7899a93c429eSMatthew G. Knepley       /* We need to check that the block size does not come from non-contiguous fields */
7900a93c429eSMatthew G. Knepley       PetscInt i, j, set = 1;
7901a93c429eSMatthew G. Knepley       for (i = 0; i < subSize; i += bs) {
7902a93c429eSMatthew G. Knepley         for (j = 0; j < bs; ++j) {
7903a93c429eSMatthew G. Knepley           if (subIndices[i+j] != subIndices[i]+j) {set = 0; break;}
7904a93c429eSMatthew G. Knepley         }
7905a93c429eSMatthew G. Knepley       }
7906a93c429eSMatthew G. Knepley       if (set) {ierr = ISSetBlockSize(*is, bs);CHKERRQ(ierr);}
7907a93c429eSMatthew G. Knepley     }
7908a93c429eSMatthew G. Knepley     /* Attach nullspace */
7909a93c429eSMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
7910a93c429eSMatthew G. Knepley       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[f];
7911a93c429eSMatthew G. Knepley       if ((*subdm)->nullspaceConstructors[f]) break;
7912a93c429eSMatthew G. Knepley     }
7913a93c429eSMatthew G. Knepley     if (f < Nf) {
7914a93c429eSMatthew G. Knepley       MatNullSpace nullSpace;
7915a93c429eSMatthew G. Knepley 
7916a93c429eSMatthew G. Knepley       ierr = (*(*subdm)->nullspaceConstructors[f])(*subdm, f, &nullSpace);CHKERRQ(ierr);
7917a93c429eSMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr);
7918a93c429eSMatthew G. Knepley       ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
7919a93c429eSMatthew G. Knepley     }
7920a93c429eSMatthew G. Knepley   }
7921a93c429eSMatthew G. Knepley   PetscFunctionReturn(0);
7922a93c429eSMatthew G. Knepley }
7923